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