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 <sound/soc-dapm.h>
17 #include <linux/gpio.h>
18 #include <linux/interrupt.h>
19 #include <linux/workqueue.h>
20 #include <linux/delay.h>
23 * snd_soc_jack_new - Create a new jack
25 * @id: an identifying string for this jack
26 * @type: a bitmask of enum snd_jack_type values that can be detected by
28 * @jack: structure to use for the jack
30 * Creates a new jack object.
32 * Returns zero if successful, or a negative error code on failure.
33 * On success jack will be initialised.
35 int snd_soc_jack_new(struct snd_soc_card
*card
, const char *id
, int type
,
36 struct snd_soc_jack
*jack
)
39 INIT_LIST_HEAD(&jack
->pins
);
41 return snd_jack_new(card
->codec
->card
, id
, type
, &jack
->jack
);
43 EXPORT_SYMBOL_GPL(snd_soc_jack_new
);
46 * snd_soc_jack_report - Report the current status for a jack
49 * @status: a bitmask of enum snd_jack_type values that are currently detected.
50 * @mask: a bitmask of enum snd_jack_type values that being reported.
52 * If configured using snd_soc_jack_add_pins() then the associated
53 * DAPM pins will be enabled or disabled as appropriate and DAPM
56 * Note: This function uses mutexes and should be called from a
57 * context which can sleep (such as a workqueue).
59 void snd_soc_jack_report(struct snd_soc_jack
*jack
, int status
, int mask
)
61 struct snd_soc_codec
*codec
;
62 struct snd_soc_jack_pin
*pin
;
70 codec
= jack
->card
->codec
;
72 mutex_lock(&codec
->mutex
);
74 oldstatus
= jack
->status
;
76 jack
->status
&= ~mask
;
77 jack
->status
|= status
& mask
;
79 /* The DAPM sync is expensive enough to be worth skipping.
80 * However, empty mask means pin synchronization is desired. */
81 if (mask
&& (jack
->status
== oldstatus
))
84 list_for_each_entry(pin
, &jack
->pins
, list
) {
85 enable
= pin
->mask
& jack
->status
;
91 snd_soc_dapm_enable_pin(codec
, pin
->pin
);
93 snd_soc_dapm_disable_pin(codec
, pin
->pin
);
96 snd_soc_dapm_sync(codec
);
98 snd_jack_report(jack
->jack
, status
);
101 mutex_unlock(&codec
->mutex
);
103 EXPORT_SYMBOL_GPL(snd_soc_jack_report
);
106 * snd_soc_jack_add_pins - Associate DAPM pins with an ASoC jack
109 * @count: Number of pins
110 * @pins: Array of pins
112 * After this function has been called the DAPM pins specified in the
113 * pins array will have their status updated to reflect the current
114 * state of the jack whenever the jack status is updated.
116 int snd_soc_jack_add_pins(struct snd_soc_jack
*jack
, int count
,
117 struct snd_soc_jack_pin
*pins
)
121 for (i
= 0; i
< count
; i
++) {
123 printk(KERN_ERR
"No name for pin %d\n", i
);
127 printk(KERN_ERR
"No mask for pin %d (%s)\n", i
,
132 INIT_LIST_HEAD(&pins
[i
].list
);
133 list_add(&(pins
[i
].list
), &jack
->pins
);
136 /* Update to reflect the last reported status; canned jack
137 * implementations are likely to set their state before the
138 * card has an opportunity to associate pins.
140 snd_soc_jack_report(jack
, 0, 0);
144 EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins
);
146 #ifdef CONFIG_GPIOLIB
148 static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio
*gpio
)
150 struct snd_soc_jack
*jack
= gpio
->jack
;
154 if (gpio
->debounce_time
> 0)
155 mdelay(gpio
->debounce_time
);
157 enable
= gpio_get_value(gpio
->gpio
);
162 report
= gpio
->report
;
166 if (gpio
->jack_status_check
)
167 report
= gpio
->jack_status_check();
169 snd_soc_jack_report(jack
, report
, gpio
->report
);
172 /* irq handler for gpio pin */
173 static irqreturn_t
gpio_handler(int irq
, void *data
)
175 struct snd_soc_jack_gpio
*gpio
= data
;
177 schedule_work(&gpio
->work
);
183 static void gpio_work(struct work_struct
*work
)
185 struct snd_soc_jack_gpio
*gpio
;
187 gpio
= container_of(work
, struct snd_soc_jack_gpio
, work
);
188 snd_soc_jack_gpio_detect(gpio
);
192 * snd_soc_jack_add_gpios - Associate GPIO pins with an ASoC jack
195 * @count: number of pins
196 * @gpios: array of gpio pins
198 * This function will request gpio, set data direction and request irq
199 * for each gpio in the array.
201 int snd_soc_jack_add_gpios(struct snd_soc_jack
*jack
, int count
,
202 struct snd_soc_jack_gpio
*gpios
)
206 for (i
= 0; i
< count
; i
++) {
207 if (!gpio_is_valid(gpios
[i
].gpio
)) {
208 printk(KERN_ERR
"Invalid gpio %d\n",
213 if (!gpios
[i
].name
) {
214 printk(KERN_ERR
"No name for gpio %d\n",
220 ret
= gpio_request(gpios
[i
].gpio
, gpios
[i
].name
);
224 ret
= gpio_direction_input(gpios
[i
].gpio
);
228 INIT_WORK(&gpios
[i
].work
, gpio_work
);
229 gpios
[i
].jack
= jack
;
231 ret
= request_irq(gpio_to_irq(gpios
[i
].gpio
),
233 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
234 jack
->card
->dev
->driver
->name
,
239 #ifdef CONFIG_GPIO_SYSFS
240 /* Expose GPIO value over sysfs for diagnostic purposes */
241 gpio_export(gpios
[i
].gpio
, false);
244 /* Update initial jack status */
245 snd_soc_jack_gpio_detect(&gpios
[i
]);
251 gpio_free(gpios
[i
].gpio
);
253 snd_soc_jack_free_gpios(jack
, i
, gpios
);
257 EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios
);
260 * snd_soc_jack_free_gpios - Release GPIO pins' resources of an ASoC jack
263 * @count: number of pins
264 * @gpios: array of gpio pins
266 * Release gpio and irq resources for gpio pins associated with an ASoC jack.
268 void snd_soc_jack_free_gpios(struct snd_soc_jack
*jack
, int count
,
269 struct snd_soc_jack_gpio
*gpios
)
273 for (i
= 0; i
< count
; i
++) {
274 #ifdef CONFIG_GPIO_SYSFS
275 gpio_unexport(gpios
[i
].gpio
);
277 free_irq(gpio_to_irq(gpios
[i
].gpio
), &gpios
[i
]);
278 gpio_free(gpios
[i
].gpio
);
279 gpios
[i
].jack
= NULL
;
282 EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios
);
283 #endif /* CONFIG_GPIOLIB */