GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / soc / soc-jack.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-jack.c  --  ALSA SoC jack handling
4 //
5 // Copyright 2008 Wolfson Microelectronics PLC.
6 //
7 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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
20 struct jack_gpio_tbl {
21         int count;
22         struct snd_soc_jack *jack;
23         struct snd_soc_jack_gpio *gpios;
24 };
25
26 /**
27  * snd_soc_component_set_jack - configure component jack.
28  * @component: COMPONENTs
29  * @jack: structure to use for the jack
30  * @data: can be used if codec driver need extra data for configuring jack
31  *
32  * Configures and enables jack detection function.
33  */
34 int 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 }
42 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
43
44 /**
45  * snd_soc_card_jack_new - Create a new jack
46  * @card:  ASoC card
47  * @id:    an identifying string for this jack
48  * @type:  a bitmask of enum snd_jack_type values that can be detected by
49  *         this jack
50  * @jack:  structure to use for the jack
51  * @pins:  Array of jack pins to be added to the jack or NULL
52  * @num_pins: Number of elements in the @pins array
53  *
54  * Creates a new jack object.
55  *
56  * Returns zero if successful, or a negative error code on failure.
57  * On success jack will be initialised.
58  */
59 int 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 }
80 EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
81
82 /**
83  * snd_soc_jack_report - Report the current status for a jack
84  *
85  * @jack:   the jack
86  * @status: a bitmask of enum snd_jack_type values that are currently detected.
87  * @mask:   a bitmask of enum snd_jack_type values that being reported.
88  *
89  * If configured using snd_soc_jack_add_pins() then the associated
90  * DAPM pins will be enabled or disabled as appropriate and DAPM
91  * synchronised.
92  *
93  * Note: This function uses mutexes and should be called from a
94  * context which can sleep (such as a workqueue).
95  */
96 void 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         if (!jack)
104                 return;
105         trace_snd_soc_jack_report(jack, mask, status);
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                 /* we need to sync for this case only */
128                 sync = 1;
129         }
130
131         /* Report before the DAPM sync to help users updating micbias status */
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 }
141 EXPORT_SYMBOL_GPL(snd_soc_jack_report);
142
143 /**
144  * snd_soc_jack_add_zones - Associate voltage zones with jack
145  *
146  * @jack:  ASoC jack
147  * @count: Number of zones
148  * @zones:  Array of zones
149  *
150  * After this function has been called the zones specified in the
151  * array will be associated with the jack.
152  */
153 int 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 }
164 EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
165
166 /**
167  * snd_soc_jack_get_type - Based on the mic bias value, this function returns
168  * the type of jack from the zones declared in the jack type
169  *
170  * @jack:  ASoC jack
171  * @micbias_voltage:  mic bias voltage at adc channel when jack is plugged in
172  *
173  * Based on the mic bias value passed, this function helps identify
174  * the type of jack from the already declared jack zones
175  */
176 int 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 }
187 EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
188
189 /**
190  * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
191  *
192  * @jack:  ASoC jack
193  * @count: Number of pins
194  * @pins:  Array of pins
195  *
196  * After this function has been called the DAPM pins specified in the
197  * pins array will have their status updated to reflect the current
198  * state of the jack whenever the jack status is updated.
199  */
200 int 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         /* Update to reflect the last reported status; canned jack
223          * implementations are likely to set their state before the
224          * card has an opportunity to associate pins.
225          */
226         snd_soc_jack_report(jack, 0, 0);
227
228         return 0;
229 }
230 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
231
232 /**
233  * snd_soc_jack_notifier_register - Register a notifier for jack status
234  *
235  * @jack:  ASoC jack
236  * @nb:    Notifier block to register
237  *
238  * Register for notification of the current status of the jack.  Note
239  * that it is not possible to report additional jack events in the
240  * callback from the notifier, this is intended to support
241  * applications such as enabling electrical detection only when a
242  * mechanical detection event has occurred.
243  */
244 void 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 }
249 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
250
251 /**
252  * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
253  *
254  * @jack:  ASoC jack
255  * @nb:    Notifier block to unregister
256  *
257  * Stop notifying for status changes.
258  */
259 void 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 }
264 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
265
266 #ifdef CONFIG_GPIOLIB
267 /* gpio detect */
268 static 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 /* irq handler for gpio pin */
290 static 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 /* gpio work */
307 static 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
315 static 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                  * Use workqueue so we do not have to care about running
327                  * concurrently with work triggered by the interrupt handler.
328                  */
329                 queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
330                 break;
331         }
332
333         return NOTIFY_DONE;
334 }
335
336 static void jack_free_gpios(struct snd_soc_jack *jack, int count,
337                             struct snd_soc_jack_gpio *gpios)
338 {
339         int i;
340
341         for (i = 0; i < count; i++) {
342                 gpiod_unexport(gpios[i].desc);
343                 unregister_pm_notifier(&gpios[i].pm_notifier);
344                 free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
345                 cancel_delayed_work_sync(&gpios[i].work);
346                 gpiod_put(gpios[i].desc);
347                 gpios[i].jack = NULL;
348         }
349 }
350
351 static void jack_devres_free_gpios(struct device *dev, void *res)
352 {
353         struct jack_gpio_tbl *tbl = res;
354
355         jack_free_gpios(tbl->jack, tbl->count, tbl->gpios);
356 }
357
358 /**
359  * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
360  *
361  * @jack:  ASoC jack
362  * @count: number of pins
363  * @gpios: array of gpio pins
364  *
365  * This function will request gpio, set data direction and request irq
366  * for each gpio in the array.
367  */
368 int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
369                         struct snd_soc_jack_gpio *gpios)
370 {
371         int i, ret;
372         struct jack_gpio_tbl *tbl;
373
374         tbl = devres_alloc(jack_devres_free_gpios, sizeof(*tbl), GFP_KERNEL);
375         if (!tbl)
376                 return -ENOMEM;
377         tbl->jack = jack;
378         tbl->count = count;
379         tbl->gpios = gpios;
380
381         for (i = 0; i < count; i++) {
382                 if (!gpios[i].name) {
383                         dev_err(jack->card->dev,
384                                 "ASoC: No name for gpio at index %d\n", i);
385                         ret = -EINVAL;
386                         goto undo;
387                 }
388
389                 if (gpios[i].desc) {
390                         /* Already have a GPIO descriptor. */
391                         goto got_gpio;
392                 } else if (gpios[i].gpiod_dev) {
393                         /* Get a GPIO descriptor */
394                         gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
395                                                         gpios[i].name,
396                                                         gpios[i].idx, GPIOD_IN);
397                         if (IS_ERR(gpios[i].desc)) {
398                                 ret = PTR_ERR(gpios[i].desc);
399                                 dev_err(gpios[i].gpiod_dev,
400                                         "ASoC: Cannot get gpio at index %d: %d",
401                                         i, ret);
402                                 goto undo;
403                         }
404                 } else {
405                         /* legacy GPIO number */
406                         if (!gpio_is_valid(gpios[i].gpio)) {
407                                 dev_err(jack->card->dev,
408                                         "ASoC: Invalid gpio %d\n",
409                                         gpios[i].gpio);
410                                 ret = -EINVAL;
411                                 goto undo;
412                         }
413
414                         ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
415                                                gpios[i].name);
416                         if (ret)
417                                 goto undo;
418
419                         gpios[i].desc = gpio_to_desc(gpios[i].gpio);
420                 }
421 got_gpio:
422                 INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
423                 gpios[i].jack = jack;
424
425                 ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
426                                               gpio_handler,
427                                               IRQF_TRIGGER_RISING |
428                                               IRQF_TRIGGER_FALLING,
429                                               gpios[i].name,
430                                               &gpios[i]);
431                 if (ret < 0)
432                         goto err;
433
434                 if (gpios[i].wake) {
435                         ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
436                         if (ret != 0)
437                                 dev_err(jack->card->dev,
438                                         "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
439                                         i, ret);
440                 }
441
442                 /*
443                  * Register PM notifier so we do not miss state transitions
444                  * happening while system is asleep.
445                  */
446                 gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
447                 register_pm_notifier(&gpios[i].pm_notifier);
448
449                 /* Expose GPIO value over sysfs for diagnostic purposes */
450                 gpiod_export(gpios[i].desc, false);
451
452                 /* Update initial jack status */
453                 schedule_delayed_work(&gpios[i].work,
454                                       msecs_to_jiffies(gpios[i].debounce_time));
455         }
456
457         devres_add(jack->card->dev, tbl);
458         return 0;
459
460 err:
461         gpio_free(gpios[i].gpio);
462 undo:
463         jack_free_gpios(jack, i, gpios);
464         devres_free(tbl);
465
466         return ret;
467 }
468 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
469
470 /**
471  * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
472  *
473  * @gpiod_dev: GPIO consumer device
474  * @jack:      ASoC jack
475  * @count:     number of pins
476  * @gpios:     array of gpio pins
477  *
478  * This function will request gpio, set data direction and request irq
479  * for each gpio in the array.
480  */
481 int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
482                             struct snd_soc_jack *jack,
483                             int count, struct snd_soc_jack_gpio *gpios)
484 {
485         int i;
486
487         for (i = 0; i < count; i++)
488                 gpios[i].gpiod_dev = gpiod_dev;
489
490         return snd_soc_jack_add_gpios(jack, count, gpios);
491 }
492 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
493
494 /**
495  * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
496  *
497  * @jack:  ASoC jack
498  * @count: number of pins
499  * @gpios: array of gpio pins
500  *
501  * Release gpio and irq resources for gpio pins associated with an ASoC jack.
502  */
503 void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
504                         struct snd_soc_jack_gpio *gpios)
505 {
506         jack_free_gpios(jack, count, gpios);
507         devres_destroy(jack->card->dev, jack_devres_free_gpios, NULL, NULL);
508 }
509 EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
510 #endif  /* CONFIG_GPIOLIB */