1
2
3
4
5
6
7
8
9
10
11
12
13
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>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35int snd_soc_jack_new(struct snd_soc_card *card, const char *id, int type,
36 struct snd_soc_jack *jack)
37{
38 jack->card = card;
39 INIT_LIST_HEAD(&jack->pins);
40
41 return snd_jack_new(card->codec->card, id, type, &jack->jack);
42}
43EXPORT_SYMBOL_GPL(snd_soc_jack_new);
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59void snd_soc_jack_report(struct snd_soc_jack *jack, int status, int mask)
60{
61 struct snd_soc_codec *codec = jack->card->codec;
62 struct snd_soc_jack_pin *pin;
63 int enable;
64 int oldstatus;
65
66 if (!jack) {
67 WARN_ON_ONCE(!jack);
68 return;
69 }
70
71 mutex_lock(&codec->mutex);
72
73 oldstatus = jack->status;
74
75 jack->status &= ~mask;
76 jack->status |= status & mask;
77
78
79
80 if (mask && (jack->status == oldstatus))
81 goto out;
82
83 list_for_each_entry(pin, &jack->pins, list) {
84 enable = pin->mask & jack->status;
85
86 if (pin->invert)
87 enable = !enable;
88
89 if (enable)
90 snd_soc_dapm_enable_pin(codec, pin->pin);
91 else
92 snd_soc_dapm_disable_pin(codec, pin->pin);
93 }
94
95 snd_soc_dapm_sync(codec);
96
97 snd_jack_report(jack->jack, status);
98
99out:
100 mutex_unlock(&codec->mutex);
101}
102EXPORT_SYMBOL_GPL(snd_soc_jack_report);
103
104
105
106
107
108
109
110
111
112
113
114
115int snd_soc_jack_add_pins(struct snd_soc_jack *jack, int count,
116 struct snd_soc_jack_pin *pins)
117{
118 int i;
119
120 for (i = 0; i < count; i++) {
121 if (!pins[i].pin) {
122 printk(KERN_ERR "No name for pin %d\n", i);
123 return -EINVAL;
124 }
125 if (!pins[i].mask) {
126 printk(KERN_ERR "No mask for pin %d (%s)\n", i,
127 pins[i].pin);
128 return -EINVAL;
129 }
130
131 INIT_LIST_HEAD(&pins[i].list);
132 list_add(&(pins[i].list), &jack->pins);
133 }
134
135
136
137
138
139 snd_soc_jack_report(jack, 0, 0);
140
141 return 0;
142}
143EXPORT_SYMBOL_GPL(snd_soc_jack_add_pins);
144
145#ifdef CONFIG_GPIOLIB
146
147static void snd_soc_jack_gpio_detect(struct snd_soc_jack_gpio *gpio)
148{
149 struct snd_soc_jack *jack = gpio->jack;
150 int enable;
151 int report;
152
153 if (gpio->debounce_time > 0)
154 mdelay(gpio->debounce_time);
155
156 enable = gpio_get_value(gpio->gpio);
157 if (gpio->invert)
158 enable = !enable;
159
160 if (enable)
161 report = gpio->report;
162 else
163 report = 0;
164
165 snd_soc_jack_report(jack, report, gpio->report);
166}
167
168
169static irqreturn_t gpio_handler(int irq, void *data)
170{
171 struct snd_soc_jack_gpio *gpio = data;
172
173 schedule_work(&gpio->work);
174
175 return IRQ_HANDLED;
176}
177
178
179static void gpio_work(struct work_struct *work)
180{
181 struct snd_soc_jack_gpio *gpio;
182
183 gpio = container_of(work, struct snd_soc_jack_gpio, work);
184 snd_soc_jack_gpio_detect(gpio);
185}
186
187
188
189
190
191
192
193
194
195
196
197int snd_soc_jack_add_gpios(struct snd_soc_jack *jack, int count,
198 struct snd_soc_jack_gpio *gpios)
199{
200 int i, ret;
201
202 for (i = 0; i < count; i++) {
203 if (!gpio_is_valid(gpios[i].gpio)) {
204 printk(KERN_ERR "Invalid gpio %d\n",
205 gpios[i].gpio);
206 ret = -EINVAL;
207 goto undo;
208 }
209 if (!gpios[i].name) {
210 printk(KERN_ERR "No name for gpio %d\n",
211 gpios[i].gpio);
212 ret = -EINVAL;
213 goto undo;
214 }
215
216 ret = gpio_request(gpios[i].gpio, gpios[i].name);
217 if (ret)
218 goto undo;
219
220 ret = gpio_direction_input(gpios[i].gpio);
221 if (ret)
222 goto err;
223
224 INIT_WORK(&gpios[i].work, gpio_work);
225 gpios[i].jack = jack;
226
227 ret = request_irq(gpio_to_irq(gpios[i].gpio),
228 gpio_handler,
229 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
230 jack->card->dev->driver->name,
231 &gpios[i]);
232 if (ret)
233 goto err;
234
235#ifdef CONFIG_GPIO_SYSFS
236
237 gpio_export(gpios[i].gpio, false);
238#endif
239
240
241 snd_soc_jack_gpio_detect(&gpios[i]);
242 }
243
244 return 0;
245
246err:
247 gpio_free(gpios[i].gpio);
248undo:
249 snd_soc_jack_free_gpios(jack, i, gpios);
250
251 return ret;
252}
253EXPORT_SYMBOL_GPL(snd_soc_jack_add_gpios);
254
255
256
257
258
259
260
261
262
263
264void snd_soc_jack_free_gpios(struct snd_soc_jack *jack, int count,
265 struct snd_soc_jack_gpio *gpios)
266{
267 int i;
268
269 for (i = 0; i < count; i++) {
270#ifdef CONFIG_GPIO_SYSFS
271 gpio_unexport(gpios[i].gpio);
272#endif
273 free_irq(gpio_to_irq(gpios[i].gpio), &gpios[i]);
274 gpio_free(gpios[i].gpio);
275 gpios[i].jack = NULL;
276 }
277}
278EXPORT_SYMBOL_GPL(snd_soc_jack_free_gpios);
279#endif
280