ASoC: es8316: Add jack-detect support
[linux/fpc-iii.git] / sound / soc / soc-jack.c
blobc7b990abdbaa62a5182fb475c3e13884ad9f5341
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>
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>
20 struct jack_gpio_tbl {
21 int count;
22 struct snd_soc_jack *jack;
23 struct snd_soc_jack_gpio *gpios;
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
32 * Configures and enables jack detection function.
34 int snd_soc_component_set_jack(struct snd_soc_component *component,
35 struct snd_soc_jack *jack, void *data)
37 if (component->driver->set_jack)
38 return component->driver->set_jack(component, jack, data);
40 return -ENOTSUPP;
42 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
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
54 * Creates a new jack object.
56 * Returns zero if successful, or a negative error code on failure.
57 * On success jack will be initialised.
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)
63 int ret;
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);
71 ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, false);
72 if (ret)
73 return ret;
75 if (num_pins)
76 return snd_soc_jack_add_pins(jack, num_pins, pins);
78 return 0;
80 EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
82 /**
83 * snd_soc_jack_report - Report the current status for a jack
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.
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.
93 * Note: This function uses mutexes and should be called from a
94 * context which can sleep (such as a workqueue).
96 void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
98 struct snd_soc_dapm_context *dapm;
99 struct snd_soc_jack_pin *pin;
100 unsigned int sync = 0;
101 int enable;
103 trace_snd_soc_jack_report(jack, mask, status);
105 if (!jack)
106 return;
108 dapm = &jack->card->dapm;
110 mutex_lock(&jack->mutex);
112 jack->status &= ~mask;
113 jack->status |= status & mask;
115 trace_snd_soc_jack_notify(jack, status);
117 list_for_each_entry(pin, &jack->pins, list) {
118 enable = pin->mask & jack->status;
120 if (pin->invert)
121 enable = !enable;
123 if (enable)
124 snd_soc_dapm_enable_pin(dapm, pin->pin);
125 else
126 snd_soc_dapm_disable_pin(dapm, pin->pin);
128 /* we need to sync for this case only */
129 sync = 1;
132 /* Report before the DAPM sync to help users updating micbias status */
133 blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
135 if (sync)
136 snd_soc_dapm_sync(dapm);
138 snd_jack_report(jack->jack, jack->status);
140 mutex_unlock(&jack->mutex);
142 EXPORT_SYMBOL_GPL(snd_soc_jack_report);
145 * snd_soc_jack_add_zones - Associate voltage zones with jack
147 * @jack: ASoC jack
148 * @count: Number of zones
149 * @zones: Array of zones
151 * After this function has been called the zones specified in the
152 * array will be associated with the jack.
154 int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
155 struct snd_soc_jack_zone *zones)
157 int i;
159 for (i = 0; i < count; i++) {
160 INIT_LIST_HEAD(&zones[i].list);
161 list_add(&(zones[i].list), &jack->jack_zones);
163 return 0;
165 EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
168 * snd_soc_jack_get_type - Based on the mic bias value, this function returns
169 * the type of jack from the zones declared in the jack type
171 * @jack: ASoC jack
172 * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in
174 * Based on the mic bias value passed, this function helps identify
175 * the type of jack from the already declared jack zones
177 int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
179 struct snd_soc_jack_zone *zone;
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;
186 return 0;
188 EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
191 * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
193 * @jack: ASoC jack
194 * @count: Number of pins
195 * @pins: Array of pins
197 * After this function has been called the DAPM pins specified in the
198 * pins array will have their status updated to reflect the current
199 * state of the jack whenever the jack status is updated.
201 int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
202 struct snd_soc_jack_pin *pins)
204 int i;
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",
210 return -EINVAL;
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;
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);
223 /* Update to reflect the last reported status; canned jack
224 * implementations are likely to set their state before the
225 * card has an opportunity to associate pins.
227 snd_soc_jack_report(jack, 0, 0);
229 return 0;
231 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
234 * snd_soc_jack_notifier_register - Register a notifier for jack status
236 * @jack: ASoC jack
237 * @nb: Notifier block to register
239 * Register for notification of the current status of the jack. Note
240 * that it is not possible to report additional jack events in the
241 * callback from the notifier, this is intended to support
242 * applications such as enabling electrical detection only when a
243 * mechanical detection event has occurred.
245 void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
246 struct notifier_block *nb)
248 blocking_notifier_chain_register(&jack->notifier, nb);
250 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
253 * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
255 * @jack: ASoC jack
256 * @nb: Notifier block to unregister
258 * Stop notifying for status changes.
260 void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
261 struct notifier_block *nb)
263 blocking_notifier_chain_unregister(&jack->notifier, nb);
265 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
267 #ifdef CONFIG_GPIOLIB
268 /* gpio detect */
269 static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
271 struct snd_soc_jack *jack = gpio->jack;
272 int enable;
273 int report;
275 enable = gpiod_get_value_cansleep(gpio->desc);
276 if (gpio->invert)
277 enable = !enable;
279 if (enable)
280 report = gpio->report;
281 else
282 report = 0;
284 if (gpio->jack_status_check)
285 report = gpio->jack_status_check(gpio->data);
287 snd_soc_jack_report(jack, report, gpio->report);
290 /* irq handler for gpio pin */
291 static irqreturn_t gpio_handler(int irq, void *data)
293 struct snd_soc_jack_gpio *gpio = data;
294 struct device *dev = gpio->jack->card->dev;
296 trace_snd_soc_jack_irq(gpio->name);
298 if (device_may_wakeup(dev))
299 pm_wakeup_event(dev, gpio->debounce_time + 50);
301 queue_delayed_work(system_power_efficient_wq, &gpio->work,
302 msecs_to_jiffies(gpio->debounce_time));
304 return IRQ_HANDLED;
307 /* gpio work */
308 static void gpio_work(struct work_struct *work)
310 struct snd_soc_jack_gpio *gpio;
312 gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
313 snd_soc_jack_gpio_detect(gpio);
316 static int snd_soc_jack_pm_notifier(struct notifier_block *nb,
317 unsigned long action, void *data)
319 struct snd_soc_jack_gpio *gpio =
320 container_of(nb, struct snd_soc_jack_gpio, pm_notifier);
322 switch (action) {
323 case PM_POST_SUSPEND:
324 case PM_POST_HIBERNATION:
325 case PM_POST_RESTORE:
327 * Use workqueue so we do not have to care about running
328 * concurrently with work triggered by the interrupt handler.
330 queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
331 break;
334 return NOTIFY_DONE;
337 static void jack_free_gpios(struct snd_soc_jack *jack, int count,
338 struct snd_soc_jack_gpio *gpios)
340 int i;
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;
352 static void jack_devres_free_gpios(struct device *dev, void *res)
354 struct jack_gpio_tbl *tbl = res;
356 jack_free_gpios(tbl->jack, tbl->count, tbl->gpios);
360 * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
362 * @jack: ASoC jack
363 * @count: number of pins
364 * @gpios: array of gpio pins
366 * This function will request gpio, set data direction and request irq
367 * for each gpio in the array.
369 int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
370 struct snd_soc_jack_gpio *gpios)
372 int i, ret;
373 struct jack_gpio_tbl *tbl;
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;
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;
390 if (gpios[i].desc) {
391 /* Already have a GPIO descriptor. */
392 goto got_gpio;
393 } else if (gpios[i].gpiod_dev) {
394 /* Get a GPIO descriptor */
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;
405 } else {
406 /* legacy GPIO number */
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;
415 ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
416 gpios[i].name);
417 if (ret)
418 goto undo;
420 gpios[i].desc = gpio_to_desc(gpios[i].gpio);
422 got_gpio:
423 INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
424 gpios[i].jack = jack;
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;
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);
444 * Register PM notifier so we do not miss state transitions
445 * happening while system is asleep.
447 gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
448 register_pm_notifier(&gpios[i].pm_notifier);
450 /* Expose GPIO value over sysfs for diagnostic purposes */
451 gpiod_export(gpios[i].desc, false);
453 /* Update initial jack status */
454 schedule_delayed_work(&gpios[i].work,
455 msecs_to_jiffies(gpios[i].debounce_time));
458 devres_add(jack->card->dev, tbl);
459 return 0;
461 err:
462 gpio_free(gpios[i].gpio);
463 undo:
464 jack_free_gpios(jack, i, gpios);
465 devres_free(tbl);
467 return ret;
469 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
472 * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
474 * @gpiod_dev: GPIO consumer device
475 * @jack: ASoC jack
476 * @count: number of pins
477 * @gpios: array of gpio pins
479 * This function will request gpio, set data direction and request irq
480 * for each gpio in the array.
482 int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
483 struct snd_soc_jack *jack,
484 int count, struct snd_soc_jack_gpio *gpios)
486 int i;
488 for (i = 0; i < count; i++)
489 gpios[i].gpiod_dev = gpiod_dev;
491 return snd_soc_jack_add_gpios(jack, count, gpios);
493 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
496 * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
498 * @jack: ASoC jack
499 * @count: number of pins
500 * @gpios: array of gpio pins
502 * Release gpio and irq resources for gpio pins associated with an ASoC jack.
504 void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
505 struct snd_soc_jack_gpio *gpios)
507 jack_free_gpios(jack, count, gpios);
508 devres_destroy(jack->card->dev, jack_devres_free_gpios, NULL, NULL);
510 EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
511 #endif /* CONFIG_GPIOLIB */