1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <sound/jack.h>
15#include <sound/soc.h>
16#include <linux/gpio.h>
17#include <linux/gpio/consumer.h>
18#include <linux/interrupt.h>
19#include <linux/workqueue.h>
20#include <linux/delay.h>
21#include <linux/export.h>
22#include <linux/suspend.h>
23#include <trace/events/asoc.h>
24
25
26
27
28
29
30
31
32
33int snd_soc_codec_set_jack(struct snd_soc_codec *codec,
34 struct snd_soc_jack *jack, void *data)
35{
36 if (codec->driver->set_jack)
37 return codec->driver->set_jack(codec, jack, data);
38 else
39 return -EINVAL;
40}
41EXPORT_SYMBOL_GPL(snd_soc_codec_set_jack);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
59 struct snd_soc_jack *jack, struct snd_soc_jack_pin *pins,
60 unsigned int num_pins)
61{
62 int ret;
63
64 mutex_init(&jack->mutex);
65 jack->card = card;
66 INIT_LIST_HEAD(&jack->pins);
67 INIT_LIST_HEAD(&jack->jack_zones);
68 BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
69
70 ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, false);
71 if (ret)
72 return ret;
73
74 if (num_pins)
75 return snd_soc_jack_add_pins(jack, num_pins, pins);
76
77 return 0;
78}
79EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
96{
97 struct snd_soc_dapm_context *dapm;
98 struct snd_soc_jack_pin *pin;
99 unsigned int sync = 0;
100 int enable;
101
102 trace_snd_soc_jack_report(jack, mask, status);
103
104 if (!jack)
105 return;
106
107 dapm = &jack->card->dapm;
108
109 mutex_lock(&jack->mutex);
110
111 jack->status &= ~mask;
112 jack->status |= status & mask;
113
114 trace_snd_soc_jack_notify(jack, status);
115
116 list_for_each_entry(pin, &jack->pins, list) {
117 enable = pin->mask & jack->status;
118
119 if (pin->invert)
120 enable = !enable;
121
122 if (enable)
123 snd_soc_dapm_enable_pin(dapm, pin->pin);
124 else
125 snd_soc_dapm_disable_pin(dapm, pin->pin);
126
127
128 sync = 1;
129 }
130
131
132 blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
133
134 if (sync)
135 snd_soc_dapm_sync(dapm);
136
137 snd_jack_report(jack->jack, jack->status);
138
139 mutex_unlock(&jack->mutex);
140}
141EXPORT_SYMBOL_GPL(snd_soc_jack_report);
142
143
144
145
146
147
148
149
150
151
152
153int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
154 struct snd_soc_jack_zone *zones)
155{
156 int i;
157
158 for (i = 0; i < count; i++) {
159 INIT_LIST_HEAD(&zones[i].list);
160 list_add(&(zones[i].list), &jack->jack_zones);
161 }
162 return 0;
163}
164EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
165
166
167
168
169
170
171
172
173
174
175
176int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
177{
178 struct snd_soc_jack_zone *zone;
179
180 list_for_each_entry(zone, &jack->jack_zones, list) {
181 if (micbias_voltage >= zone->min_mv &&
182 micbias_voltage < zone->max_mv)
183 return zone->jack_type;
184 }
185 return 0;
186}
187EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
188
189
190
191
192
193
194
195
196
197
198
199
200int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
201 struct snd_soc_jack_pin *pins)
202{
203 int i;
204
205 for (i = 0; i < count; i++) {
206 if (!pins[i].pin) {
207 dev_err(jack->card->dev, "ASoC: No name for pin %d\n",
208 i);
209 return -EINVAL;
210 }
211 if (!pins[i].mask) {
212 dev_err(jack->card->dev, "ASoC: No mask for pin %d"
213 " (%s)\n", i, pins[i].pin);
214 return -EINVAL;
215 }
216
217 INIT_LIST_HEAD(&pins[i].list);
218 list_add(&(pins[i].list), &jack->pins);
219 snd_jack_add_new_kctl(jack->jack, pins[i].pin, pins[i].mask);
220 }
221
222
223
224
225
226 snd_soc_jack_report(jack, 0, 0);
227
228 return 0;
229}
230EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
231
232
233
234
235
236
237
238
239
240
241
242
243
244void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
245 struct notifier_block *nb)
246{
247 blocking_notifier_chain_register(&jack->notifier, nb);
248}
249EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
250
251
252
253
254
255
256
257
258
259void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
260 struct notifier_block *nb)
261{
262 blocking_notifier_chain_unregister(&jack->notifier, nb);
263}
264EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
265
266#ifdef CONFIG_GPIOLIB
267
268static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
269{
270 struct snd_soc_jack *jack = gpio->jack;
271 int enable;
272 int report;
273
274 enable = gpiod_get_value_cansleep(gpio->desc);
275 if (gpio->invert)
276 enable = !enable;
277
278 if (enable)
279 report = gpio->report;
280 else
281 report = 0;
282
283 if (gpio->jack_status_check)
284 report = gpio->jack_status_check(gpio->data);
285
286 snd_soc_jack_report(jack, report, gpio->report);
287}
288
289
290static irqreturn_t gpio_handler(int irq, void *data)
291{
292 struct snd_soc_jack_gpio *gpio = data;
293 struct device *dev = gpio->jack->card->dev;
294
295 trace_snd_soc_jack_irq(gpio->name);
296
297 if (device_may_wakeup(dev))
298 pm_wakeup_event(dev, gpio->debounce_time + 50);
299
300 queue_delayed_work(system_power_efficient_wq, &gpio->work,
301 msecs_to_jiffies(gpio->debounce_time));
302
303 return IRQ_HANDLED;
304}
305
306
307static void gpio_work(struct work_struct *work)
308{
309 struct snd_soc_jack_gpio *gpio;
310
311 gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
312 snd_soc_jack_gpio_detect(gpio);
313}
314
315static int snd_soc_jack_pm_notifier(struct notifier_block *nb,
316 unsigned long action, void *data)
317{
318 struct snd_soc_jack_gpio *gpio =
319 container_of(nb, struct snd_soc_jack_gpio, pm_notifier);
320
321 switch (action) {
322 case PM_POST_SUSPEND:
323 case PM_POST_HIBERNATION:
324 case PM_POST_RESTORE:
325
326
327
328
329 queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
330 break;
331 }
332
333 return NOTIFY_DONE;
334}
335
336
337
338
339
340
341
342
343
344
345
346int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
347 struct snd_soc_jack_gpio *gpios)
348{
349 int i, ret;
350
351 for (i = 0; i < count; i++) {
352 if (!gpios[i].name) {
353 dev_err(jack->card->dev,
354 "ASoC: No name for gpio at index %d\n", i);
355 ret = -EINVAL;
356 goto undo;
357 }
358
359 if (gpios[i].desc) {
360
361 goto got_gpio;
362 } else if (gpios[i].gpiod_dev) {
363
364 gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
365 gpios[i].name,
366 gpios[i].idx, GPIOD_IN);
367 if (IS_ERR(gpios[i].desc)) {
368 ret = PTR_ERR(gpios[i].desc);
369 dev_err(gpios[i].gpiod_dev,
370 "ASoC: Cannot get gpio at index %d: %d",
371 i, ret);
372 goto undo;
373 }
374 } else {
375
376 if (!gpio_is_valid(gpios[i].gpio)) {
377 dev_err(jack->card->dev,
378 "ASoC: Invalid gpio %d\n",
379 gpios[i].gpio);
380 ret = -EINVAL;
381 goto undo;
382 }
383
384 ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
385 gpios[i].name);
386 if (ret)
387 goto undo;
388
389 gpios[i].desc = gpio_to_desc(gpios[i].gpio);
390 }
391got_gpio:
392 INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
393 gpios[i].jack = jack;
394
395 ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
396 gpio_handler,
397 IRQF_TRIGGER_RISING |
398 IRQF_TRIGGER_FALLING,
399 gpios[i].name,
400 &gpios[i]);
401 if (ret < 0)
402 goto err;
403
404 if (gpios[i].wake) {
405 ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
406 if (ret != 0)
407 dev_err(jack->card->dev,
408 "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
409 i, ret);
410 }
411
412
413
414
415
416 gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
417 register_pm_notifier(&gpios[i].pm_notifier);
418
419
420 gpiod_export(gpios[i].desc, false);
421
422
423 schedule_delayed_work(&gpios[i].work,
424 msecs_to_jiffies(gpios[i].debounce_time));
425 }
426
427 return 0;
428
429err:
430 gpio_free(gpios[i].gpio);
431undo:
432 snd_soc_jack_free_gpios(jack, i, gpios);
433
434 return ret;
435}
436EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
437
438
439
440
441
442
443
444
445
446
447
448
449int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
450 struct snd_soc_jack *jack,
451 int count, struct snd_soc_jack_gpio *gpios)
452{
453 int i;
454
455 for (i = 0; i < count; i++)
456 gpios[i].gpiod_dev = gpiod_dev;
457
458 return snd_soc_jack_add_gpios(jack, count, gpios);
459}
460EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
461
462
463
464
465
466
467
468
469
470
471void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
472 struct snd_soc_jack_gpio *gpios)
473{
474 int i;
475
476 for (i = 0; i < count; i++) {
477 gpiod_unexport(gpios[i].desc);
478 unregister_pm_notifier(&gpios[i].pm_notifier);
479 free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
480 cancel_delayed_work_sync(&gpios[i].work);
481 gpiod_put(gpios[i].desc);
482 gpios[i].jack = NULL;
483 }
484}
485EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
486#endif
487