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_card_jack_new - Create a new jack
46 * @id: an identifying string for this jack
47 * @type: a bitmask of enum snd_jack_type values that can be detected by
49 * @jack: structure to use for the jack
50 * @pins: Array of jack pins to be added to the jack or NULL
51 * @num_pins: Number of elements in the @pins array
53 * Creates a new jack object.
55 * Returns zero if successful, or a negative error code on failure.
56 * On success jack will be initialised.
58 int 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
)
64 mutex_init(&jack
->mutex
);
66 INIT_LIST_HEAD(&jack
->pins
);
67 INIT_LIST_HEAD(&jack
->jack_zones
);
68 BLOCKING_INIT_NOTIFIER_HEAD(&jack
->notifier
);
70 ret
= snd_jack_new(card
->snd_card
, id
, type
, &jack
->jack
, false, false);
75 return snd_soc_jack_add_pins(jack
, num_pins
, pins
);
79 EXPORT_SYMBOL_GPL(snd_soc_card_jack_new
);
82 * snd_soc_jack_report - Report the current status for a jack
85 * @status: a bitmask of enum snd_jack_type values that are currently detected.
86 * @mask: a bitmask of enum snd_jack_type values that being reported.
88 * If configured using snd_soc_jack_add_pins() then the associated
89 * DAPM pins will be enabled or disabled as appropriate and DAPM
92 * Note: This function uses mutexes and should be called from a
93 * context which can sleep (such as a workqueue).
95 void snd_soc_jack_report(struct snd_soc_jack
*jack
, int status
, int mask
)
97 struct snd_soc_dapm_context
*dapm
;
98 struct snd_soc_jack_pin
*pin
;
99 unsigned int sync
= 0;
102 trace_snd_soc_jack_report(jack
, mask
, status
);
107 dapm
= &jack
->card
->dapm
;
109 mutex_lock(&jack
->mutex
);
111 jack
->status
&= ~mask
;
112 jack
->status
|= status
& mask
;
114 trace_snd_soc_jack_notify(jack
, status
);
116 list_for_each_entry(pin
, &jack
->pins
, list
) {
117 enable
= pin
->mask
& jack
->status
;
123 snd_soc_dapm_enable_pin(dapm
, pin
->pin
);
125 snd_soc_dapm_disable_pin(dapm
, pin
->pin
);
127 /* we need to sync for this case only */
131 /* Report before the DAPM sync to help users updating micbias status */
132 blocking_notifier_call_chain(&jack
->notifier
, jack
->status
, jack
);
135 snd_soc_dapm_sync(dapm
);
137 snd_jack_report(jack
->jack
, jack
->status
);
139 mutex_unlock(&jack
->mutex
);
141 EXPORT_SYMBOL_GPL(snd_soc_jack_report
);
144 * snd_soc_jack_add_zones - Associate voltage zones with jack
147 * @count: Number of zones
148 * @zones: Array of zones
150 * After this function has been called the zones specified in the
151 * array will be associated with the jack.
153 int snd_soc_jack_add_zones(struct snd_soc_jack
*jack
, int count
,
154 struct snd_soc_jack_zone
*zones
)
158 for (i
= 0; i
< count
; i
++) {
159 INIT_LIST_HEAD(&zones
[i
].list
);
160 list_add(&(zones
[i
].list
), &jack
->jack_zones
);
164 EXPORT_SYMBOL_GPL(snd_soc_jack_add_zones
);
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
171 * @micbias_voltage: mic bias voltage at adc channel when jack is plugged in
173 * Based on the mic bias value passed, this function helps identify
174 * the type of jack from the already declared jack zones
176 int snd_soc_jack_get_type(struct snd_soc_jack
*jack
, int micbias_voltage
)
178 struct snd_soc_jack_zone
*zone
;
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
;
187 EXPORT_SYMBOL_GPL(snd_soc_jack_get_type
);
190 * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
193 * @count: Number of pins
194 * @pins: Array of pins
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.
200 int snd_soc_jack_add_pins(struct snd_soc_jack
*jack
, int count
,
201 struct snd_soc_jack_pin
*pins
)
205 for (i
= 0; i
< count
; i
++) {
207 dev_err(jack
->card
->dev
, "ASoC: No name for pin %d\n",
212 dev_err(jack
->card
->dev
, "ASoC: No mask for pin %d"
213 " (%s)\n", i
, pins
[i
].pin
);
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
);
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.
226 snd_soc_jack_report(jack
, 0, 0);
230 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins
);
233 * snd_soc_jack_notifier_register - Register a notifier for jack status
236 * @nb: Notifier block to register
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.
244 void snd_soc_jack_notifier_register(struct snd_soc_jack
*jack
,
245 struct notifier_block
*nb
)
247 blocking_notifier_chain_register(&jack
->notifier
, nb
);
249 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_register
);
252 * snd_soc_jack_notifier_unregister - Unregister a notifier for jack status
255 * @nb: Notifier block to unregister
257 * Stop notifying for status changes.
259 void snd_soc_jack_notifier_unregister(struct snd_soc_jack
*jack
,
260 struct notifier_block
*nb
)
262 blocking_notifier_chain_unregister(&jack
->notifier
, nb
);
264 EXPORT_SYMBOL_GPL(snd_soc_jack_notifier_unregister
);
266 #ifdef CONFIG_GPIOLIB
268 static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio
*gpio
)
270 struct snd_soc_jack
*jack
= gpio
->jack
;
274 enable
= gpiod_get_value_cansleep(gpio
->desc
);
279 report
= gpio
->report
;
283 if (gpio
->jack_status_check
)
284 report
= gpio
->jack_status_check(gpio
->data
);
286 snd_soc_jack_report(jack
, report
, gpio
->report
);
289 /* irq handler for gpio pin */
290 static irqreturn_t
gpio_handler(int irq
, void *data
)
292 struct snd_soc_jack_gpio
*gpio
= data
;
293 struct device
*dev
= gpio
->jack
->card
->dev
;
295 trace_snd_soc_jack_irq(gpio
->name
);
297 if (device_may_wakeup(dev
))
298 pm_wakeup_event(dev
, gpio
->debounce_time
+ 50);
300 queue_delayed_work(system_power_efficient_wq
, &gpio
->work
,
301 msecs_to_jiffies(gpio
->debounce_time
));
307 static void gpio_work(struct work_struct
*work
)
309 struct snd_soc_jack_gpio
*gpio
;
311 gpio
= container_of(work
, struct snd_soc_jack_gpio
, work
.work
);
312 snd_soc_jack_gpio_detect(gpio
);
315 static int snd_soc_jack_pm_notifier(struct notifier_block
*nb
,
316 unsigned long action
, void *data
)
318 struct snd_soc_jack_gpio
*gpio
=
319 container_of(nb
, struct snd_soc_jack_gpio
, pm_notifier
);
322 case PM_POST_SUSPEND
:
323 case PM_POST_HIBERNATION
:
324 case PM_POST_RESTORE
:
326 * Use workqueue so we do not have to care about running
327 * concurrently with work triggered by the interrupt handler.
329 queue_delayed_work(system_power_efficient_wq
, &gpio
->work
, 0);
337 * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
340 * @count: number of pins
341 * @gpios: array of gpio pins
343 * This function will request gpio, set data direction and request irq
344 * for each gpio in the array.
346 int snd_soc_jack_add_gpios(struct snd_soc_jack
*jack
, int count
,
347 struct snd_soc_jack_gpio
*gpios
)
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
);
360 /* Already have a GPIO descriptor. */
362 } else if (gpios
[i
].gpiod_dev
) {
363 /* Get a GPIO descriptor */
364 gpios
[i
].desc
= gpiod_get_index(gpios
[i
].gpiod_dev
,
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",
375 /* legacy GPIO number */
376 if (!gpio_is_valid(gpios
[i
].gpio
)) {
377 dev_err(jack
->card
->dev
,
378 "ASoC: Invalid gpio %d\n",
384 ret
= gpio_request_one(gpios
[i
].gpio
, GPIOF_IN
,
389 gpios
[i
].desc
= gpio_to_desc(gpios
[i
].gpio
);
392 INIT_DELAYED_WORK(&gpios
[i
].work
, gpio_work
);
393 gpios
[i
].jack
= jack
;
395 ret
= request_any_context_irq(gpiod_to_irq(gpios
[i
].desc
),
397 IRQF_TRIGGER_RISING
|
398 IRQF_TRIGGER_FALLING
,
405 ret
= irq_set_irq_wake(gpiod_to_irq(gpios
[i
].desc
), 1);
407 dev_err(jack
->card
->dev
,
408 "ASoC: Failed to mark GPIO at index %d as wake source: %d\n",
413 * Register PM notifier so we do not miss state transitions
414 * happening while system is asleep.
416 gpios
[i
].pm_notifier
.notifier_call
= snd_soc_jack_pm_notifier
;
417 register_pm_notifier(&gpios
[i
].pm_notifier
);
419 /* Expose GPIO value over sysfs for diagnostic purposes */
420 gpiod_export(gpios
[i
].desc
, false);
422 /* Update initial jack status */
423 schedule_delayed_work(&gpios
[i
].work
,
424 msecs_to_jiffies(gpios
[i
].debounce_time
));
430 gpio_free(gpios
[i
].gpio
);
432 snd_soc_jack_free_gpios(jack
, i
, gpios
);
436 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios
);
439 * snd_soc_jack_add_gpiods - Associate GPIO descriptor pins with an ASoC jack
441 * @gpiod_dev: GPIO consumer device
443 * @count: number of pins
444 * @gpios: array of gpio pins
446 * This function will request gpio, set data direction and request irq
447 * for each gpio in the array.
449 int snd_soc_jack_add_gpiods(struct device
*gpiod_dev
,
450 struct snd_soc_jack
*jack
,
451 int count
, struct snd_soc_jack_gpio
*gpios
)
455 for (i
= 0; i
< count
; i
++)
456 gpios
[i
].gpiod_dev
= gpiod_dev
;
458 return snd_soc_jack_add_gpios(jack
, count
, gpios
);
460 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpiods
);
463 * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
466 * @count: number of pins
467 * @gpios: array of gpio pins
469 * Release gpio and irq resources for gpio pins associated with an ASoC jack.
471 void snd_soc_jack_free_gpios(struct snd_soc_jack
*jack
, int count
,
472 struct snd_soc_jack_gpio
*gpios
)
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
;
485 EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios
);
486 #endif /* CONFIG_GPIOLIB */