2 * soc-jack.c -- ALSA SoC jack handling
4 * Copyright 2008 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
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>
26 * snd_soc_codec_set_jack - configure codec jack.
28 * @jack: structure to use for the jack
29 * @data: can be used if codec driver need extra data for configuring jack
31 * Configures and enables jack detection function.
33 int snd_soc_codec_set_jack(struct snd_soc_codec *codec,
34 struct snd_soc_jack *jack, void *data)
36 if (codec->driver->set_jack)
37 return codec->driver->set_jack(codec, jack, data);
41 EXPORT_SYMBOL_GPL(snd_soc_codec_set_jack);
44 * snd_soc_component_set_jack - configure component jack.
45 * @component: COMPONENTs
46 * @jack: structure to use for the jack
47 * @data: can be used if codec driver need extra data for configuring jack
49 * Configures and enables jack detection function.
51 int snd_soc_component_set_jack(struct snd_soc_component *component,
52 struct snd_soc_jack *jack, void *data)
55 if (component->set_jack)
56 return component->set_jack(component, jack, data);
58 if (component->driver->set_jack)
59 return component->driver->set_jack(component, jack, data);
63 EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
66 * snd_soc_card_jack_new - Create a new jack
68 * @id: an identifying string for this jack
69 * @type: a bitmask of enum snd_jack_type values that can be detected by
71 * @jack: structure to use for the jack
72 * @pins: Array of jack pins to be added to the jack or NULL
73 * @num_pins: Number of elements in the @pins array
75 * Creates a new jack object.
77 * Returns zero if successful, or a negative error code on failure.
78 * On success jack will be initialised.
80 int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
81 struct snd_soc_jack *jack, struct snd_soc_jack_pin *pins,
82 unsigned int num_pins)
86 mutex_init(&jack->mutex);
88 INIT_LIST_HEAD(&jack->pins);
89 INIT_LIST_HEAD(&jack->jack_zones);
90 BLOCKING_INIT_NOTIFIER_HEAD(&jack->notifier);
92 ret = snd_jack_new(card->snd_card, id, type, &jack->jack, false, false);
97 return snd_soc_jack_add_pins(jack, num_pins, pins);
101 EXPORT_SYMBOL_GPL(snd_soc_card_jack_new);
104 * snd_soc_jack_report - Report the current status for a jack
107 * @status: a bitmask of enum snd_jack_type values that are currently detected.
108 * @mask: a bitmask of enum snd_jack_type values that being reported.
110 * If configured using snd_soc_jack_add_pins() then the associated
111 * DAPM pins will be enabled or disabled as appropriate and DAPM
114 * Note: This function uses mutexes and should be called from a
115 * context which can sleep (such as a workqueue).
117 void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
119 struct snd_soc_dapm_context *dapm;
120 struct snd_soc_jack_pin *pin;
121 unsigned int sync = 0;
124 trace_snd_soc_jack_report(jack, mask, status);
129 dapm = &jack->card->dapm;
131 mutex_lock(&jack->mutex);
133 jack->status &= ~mask;
134 jack->status |= status & mask;
136 trace_snd_soc_jack_notify(jack, status);
138 list_for_each_entry(pin, &jack->pins, list) {
139 enable = pin->mask & jack->status;
145 snd_soc_dapm_enable_pin(dapm, pin->pin);
147 snd_soc_dapm_disable_pin(dapm, pin->pin);
149 /* we need to sync for this case only */
153 /* Report before the DAPM sync to help users updating micbias status */
154 blocking_notifier_call_chain(&jack->notifier, jack->status, jack);
157 snd_soc_dapm_sync(dapm);
159 snd_jack_report(jack->jack, jack->status);
161 mutex_unlock(&jack->mutex);
163 EXPORT_SYMBOL_GPL(snd_soc_jack_report);
166 * snd_soc_jack_add_zones - Associate voltage zones with jack
169 * @count: Number of zones
170 * @zones: Array of zones
172 * After this function has been called the zones specified in the
173 * array will be associated with the jack.
175 int snd_soc_jack_add_zones(struct snd_soc_jack *jack, int count,
176 struct snd_soc_jack_zone *zones)
180 for (i = 0; i < count; i++) {
181 INIT_LIST_HEAD(&zones[i].list);
182 list_add(&(zones[i].list), &jack->jack_zones);
186 EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones);
189 * snd_soc_jack_get_type - Based on the mic bias value, this function returns
190 * the type of jack from the zones declared in the jack type
193 * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in
195 * Based on the mic bias value passed, this function helps identify
196 * the type of jack from the already declared jack zones
198 int snd_soc_jack_get_type(struct snd_soc_jack *jack, int micbias_voltage)
200 struct snd_soc_jack_zone *zone;
202 list_for_each_entry(zone, &jack->jack_zones, list) {
203 if (micbias_voltage >= zone->min_mv &&
204 micbias_voltage < zone->max_mv)
205 return zone->jack_type;
209 EXPORT_SYMBOL_GPL(snd_soc_jack_get_type);
212 * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
215 * @count: Number of pins
216 * @pins: Array of pins
218 * After this function has been called the DAPM pins specified in the
219 * pins array will have their status updated to reflect the current
220 * state of the jack whenever the jack status is updated.
222 int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
223 struct snd_soc_jack_pin *pins)
227 for (i = 0; i < count; i++) {
229 dev_err(jack->card->dev, "ASoC: No name for pin %d\n",
234 dev_err(jack->card->dev, "ASoC: No mask for pin %d"
235 " (%s)\n", i, pins[i].pin);
239 INIT_LIST_HEAD(&pins[i].list);
240 list_add(&(pins[i].list), &jack->pins);
241 snd_jack_add_new_kctl(jack->jack, pins[i].pin, pins[i].mask);
244 /* Update to reflect the last reported status; canned jack
245 * implementations are likely to set their state before the
246 * card has an opportunity to associate pins.
248 snd_soc_jack_report(jack, 0, 0);
252 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
255 * snd_soc_jack_notifier_register - Register a notifier for jack status
258 * @nb: Notifier block to register
260 * Register for notification of the current status of the jack. Note
261 * that it is not possible to report additional jack events in the
262 * callback from the notifier, this is intended to support
263 * applications such as enabling electrical detection only when a
264 * mechanical detection event has occurred.
266 void snd_soc_jack_notifier_register(struct snd_soc_jack *jack,
267 struct notifier_block *nb)
269 blocking_notifier_chain_register(&jack->notifier, nb);
271 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register);
274 * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
277 * @nb: Notifier block to unregister
279 * Stop notifying for status changes.
281 void snd_soc_jack_notifier_unregister(struct snd_soc_jack *jack,
282 struct notifier_block *nb)
284 blocking_notifier_chain_unregister(&jack->notifier, nb);
286 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister);
288 #ifdef CONFIG_GPIOLIB
290 static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
292 struct snd_soc_jack *jack = gpio->jack;
296 enable = gpiod_get_value_cansleep(gpio->desc);
301 report = gpio->report;
305 if (gpio->jack_status_check)
306 report = gpio->jack_status_check(gpio->data);
308 snd_soc_jack_report(jack, report, gpio->report);
311 /* irq handler for gpio pin */
312 static irqreturn_t gpio_handler(int irq, void *data)
314 struct snd_soc_jack_gpio *gpio = data;
315 struct device *dev = gpio->jack->card->dev;
317 trace_snd_soc_jack_irq(gpio->name);
319 if (device_may_wakeup(dev))
320 pm_wakeup_event(dev, gpio->debounce_time + 50);
322 queue_delayed_work(system_power_efficient_wq, &gpio->work,
323 msecs_to_jiffies(gpio->debounce_time));
329 static void gpio_work(struct work_struct *work)
331 struct snd_soc_jack_gpio *gpio;
333 gpio = container_of(work, struct snd_soc_jack_gpio, work.work);
334 snd_soc_jack_gpio_detect(gpio);
337 static int snd_soc_jack_pm_notifier(struct notifier_block *nb,
338 unsigned long action, void *data)
340 struct snd_soc_jack_gpio *gpio =
341 container_of(nb, struct snd_soc_jack_gpio, pm_notifier);
344 case PM_POST_SUSPEND:
345 case PM_POST_HIBERNATION:
346 case PM_POST_RESTORE:
348 * Use workqueue so we do not have to care about running
349 * concurrently with work triggered by the interrupt handler.
351 queue_delayed_work(system_power_efficient_wq, &gpio->work, 0);
359 * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
362 * @count: number of pins
363 * @gpios: array of gpio pins
365 * This function will request gpio, set data direction and request irq
366 * for each gpio in the array.
368 int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
369 struct snd_soc_jack_gpio *gpios)
373 for (i = 0; i < count; i++) {
374 if (!gpios[i].name) {
375 dev_err(jack->card->dev,
376 "ASoC: No name for gpio at index %d\n", i);
382 /* Already have a GPIO descriptor. */
384 } else if (gpios[i].gpiod_dev) {
385 /* Get a GPIO descriptor */
386 gpios[i].desc = gpiod_get_index(gpios[i].gpiod_dev,
388 gpios[i].idx, GPIOD_IN);
389 if (IS_ERR(gpios[i].desc)) {
390 ret = PTR_ERR(gpios[i].desc);
391 dev_err(gpios[i].gpiod_dev,
392 "ASoC: Cannot get gpio at index %d: %d",
397 /* legacy GPIO number */
398 if (!gpio_is_valid(gpios[i].gpio)) {
399 dev_err(jack->card->dev,
400 "ASoC: Invalid gpio %d\n",
406 ret = gpio_request_one(gpios[i].gpio, GPIOF_IN,
411 gpios[i].desc = gpio_to_desc(gpios[i].gpio);
414 INIT_DELAYED_WORK(&gpios[i].work, gpio_work);
415 gpios[i].jack = jack;
417 ret = request_any_context_irq(gpiod_to_irq(gpios[i].desc),
419 IRQF_TRIGGER_RISING |
420 IRQF_TRIGGER_FALLING,
427 ret = irq_set_irq_wake(gpiod_to_irq(gpios[i].desc), 1);
429 dev_err(jack->card->dev,
430 "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
435 * Register PM notifier so we do not miss state transitions
436 * happening while system is asleep.
438 gpios[i].pm_notifier.notifier_call = snd_soc_jack_pm_notifier;
439 register_pm_notifier(&gpios[i].pm_notifier);
441 /* Expose GPIO value over sysfs for diagnostic purposes */
442 gpiod_export(gpios[i].desc, false);
444 /* Update initial jack status */
445 schedule_delayed_work(&gpios[i].work,
446 msecs_to_jiffies(gpios[i].debounce_time));
452 gpio_free(gpios[i].gpio);
454 snd_soc_jack_free_gpios(jack, i, gpios);
458 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
461 * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
463 * @gpiod_dev: GPIO consumer device
465 * @count: number of pins
466 * @gpios: array of gpio pins
468 * This function will request gpio, set data direction and request irq
469 * for each gpio in the array.
471 int snd_soc_jack_add_gpiods(struct device *gpiod_dev,
472 struct snd_soc_jack *jack,
473 int count, struct snd_soc_jack_gpio *gpios)
477 for (i = 0; i < count; i++)
478 gpios[i].gpiod_dev = gpiod_dev;
480 return snd_soc_jack_add_gpios(jack, count, gpios);
482 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods);
485 * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
488 * @count: number of pins
489 * @gpios: array of gpio pins
491 * Release gpio and irq resources for gpio pins associated with an ASoC jack.
493 void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
494 struct snd_soc_jack_gpio *gpios)
498 for (i = 0; i < count; i++) {
499 gpiod_unexport(gpios[i].desc);
500 unregister_pm_notifier(&gpios[i].pm_notifier);
501 free_irq(gpiod_to_irq(gpios[i].desc), &gpios[i]);
502 cancel_delayed_work_sync(&gpios[i].work);
503 gpiod_put(gpios[i].desc);
504 gpios[i].jack = NULL;
507 EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
508 #endif /* CONFIG_GPIOLIB */