blob: 40190775b0342d78ddb8017c61171926fe33400e [file] [log] [blame]
Mark Browne76d8ce2008-07-28 19:05:35 +01001/*
2 * Jack abstraction layer
3 *
4 * Copyright 2008 Wolfson Microelectronics
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Mark Browne76d8ce2008-07-28 19:05:35 +010025#include <sound/jack.h>
26#include <sound/core.h>
Jie Yang9058cbe2015-04-27 21:20:56 +080027#include <sound/control.h>
28
29struct snd_jack_kctl {
30 struct snd_kcontrol *kctl;
31 struct list_head list; /* list of controls belong to the same jack */
32 unsigned int mask_bits; /* only masked status bits are reported via kctl */
33};
Mark Browne76d8ce2008-07-28 19:05:35 +010034
Takashi Iwaife0d1282016-02-17 09:44:25 +010035#ifdef CONFIG_SND_JACK_INPUT_DEV
Banajit Goswamid566f792016-11-21 17:39:12 -080036static int jack_switch_types[] = {
Mark Brownbd8a71a2009-01-03 16:56:56 +000037 SW_HEADPHONE_INSERT,
38 SW_MICROPHONE_INSERT,
39 SW_LINEOUT_INSERT,
40 SW_JACK_PHYSICAL_INSERT,
Jani Nikulad506fc32009-01-07 11:54:25 +020041 SW_VIDEOOUT_INSERT,
David Henningsson7c2f8e42011-10-05 15:53:25 +020042 SW_LINEIN_INSERT,
Banajit Goswamid566f792016-11-21 17:39:12 -080043 SW_HPHL_OVERCURRENT,
44 SW_HPHR_OVERCURRENT,
45 SW_UNSUPPORT_INSERT,
Banajit Goswamiebc05932016-11-21 17:49:35 -080046 SW_MICROPHONE2_INSERT,
Mark Brownbd8a71a2009-01-03 16:56:56 +000047};
Takashi Iwaife0d1282016-02-17 09:44:25 +010048#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Brownbd8a71a2009-01-03 16:56:56 +000049
Takashi Iwai32b85442013-11-14 15:35:46 +010050static int snd_jack_dev_disconnect(struct snd_device *device)
Mark Browne76d8ce2008-07-28 19:05:35 +010051{
Takashi Iwaife0d1282016-02-17 09:44:25 +010052#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +010053 struct snd_jack *jack = device->device_data;
54
Takashi Iwai32b85442013-11-14 15:35:46 +010055 if (!jack->input_dev)
56 return 0;
Takashi Iwai9d590652009-04-14 16:13:58 +020057
Mark Browne76d8ce2008-07-28 19:05:35 +010058 /* If the input device is registered with the input subsystem
59 * then we need to use a different deallocator. */
60 if (jack->registered)
61 input_unregister_device(jack->input_dev);
62 else
63 input_free_device(jack->input_dev);
Takashi Iwai32b85442013-11-14 15:35:46 +010064 jack->input_dev = NULL;
Takashi Iwaife0d1282016-02-17 09:44:25 +010065#endif /* CONFIG_SND_JACK_INPUT_DEV */
Takashi Iwai32b85442013-11-14 15:35:46 +010066 return 0;
67}
68
69static int snd_jack_dev_free(struct snd_device *device)
70{
71 struct snd_jack *jack = device->device_data;
Jie Yang9058cbe2015-04-27 21:20:56 +080072 struct snd_card *card = device->card;
73 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
Takashi Iwai32b85442013-11-14 15:35:46 +010074
Jie Yang9058cbe2015-04-27 21:20:56 +080075 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
76 list_del_init(&jack_kctl->list);
77 snd_ctl_remove(card, jack_kctl->kctl);
78 }
Takashi Iwai32b85442013-11-14 15:35:46 +010079 if (jack->private_free)
80 jack->private_free(jack);
81
82 snd_jack_dev_disconnect(device);
Mark Browne76d8ce2008-07-28 19:05:35 +010083
Matthew Ranostay282cd762008-10-25 01:05:29 -040084 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010085 kfree(jack);
86
87 return 0;
88}
89
Takashi Iwaife0d1282016-02-17 09:44:25 +010090#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +010091static int snd_jack_dev_register(struct snd_device *device)
92{
93 struct snd_jack *jack = device->device_data;
94 struct snd_card *card = device->card;
Mark Brownebb812c2010-03-17 18:07:12 +000095 int err, i;
Mark Browne76d8ce2008-07-28 19:05:35 +010096
97 snprintf(jack->name, sizeof(jack->name), "%s %s",
Takashi Iwai2678f602009-02-18 16:46:27 +010098 card->shortname, jack->id);
Takashi Iwai43b2cd52015-04-30 15:25:00 +020099
100 if (!jack->input_dev)
101 return 0;
102
Mark Browne76d8ce2008-07-28 19:05:35 +0100103 jack->input_dev->name = jack->name;
104
105 /* Default to the sound card device. */
106 if (!jack->input_dev->dev.parent)
Kay Sievers1f3fff72009-06-10 19:50:33 +0200107 jack->input_dev->dev.parent = snd_card_get_device_link(card);
Mark Browne76d8ce2008-07-28 19:05:35 +0100108
Mark Brownebb812c2010-03-17 18:07:12 +0000109 /* Add capabilities for any keys that are enabled */
110 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
111 int testbit = SND_JACK_BTN_0 >> i;
112
113 if (!(jack->type & testbit))
114 continue;
115
116 if (!jack->key[i])
117 jack->key[i] = BTN_0 + i;
118
119 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
120 }
121
Mark Browne76d8ce2008-07-28 19:05:35 +0100122 err = input_register_device(jack->input_dev);
123 if (err == 0)
124 jack->registered = 1;
125
126 return err;
127}
Takashi Iwaife0d1282016-02-17 09:44:25 +0100128#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100129
Jie Yang9058cbe2015-04-27 21:20:56 +0800130static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
131{
132 struct snd_jack_kctl *jack_kctl;
133
134 jack_kctl = kctl->private_data;
135 if (jack_kctl) {
136 list_del(&jack_kctl->list);
137 kfree(jack_kctl);
138 }
139}
140
141static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
142{
143 list_add_tail(&jack_kctl->list, &jack->kctl_list);
144}
145
146static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
147{
148 struct snd_kcontrol *kctl;
149 struct snd_jack_kctl *jack_kctl;
150 int err;
151
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800152 kctl = snd_kctl_jack_new(name, card);
Jie Yang9058cbe2015-04-27 21:20:56 +0800153 if (!kctl)
154 return NULL;
155
156 err = snd_ctl_add(card, kctl);
157 if (err < 0)
158 return NULL;
159
160 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
161
162 if (!jack_kctl)
163 goto error;
164
165 jack_kctl->kctl = kctl;
166 jack_kctl->mask_bits = mask;
167
168 kctl->private_data = jack_kctl;
169 kctl->private_free = snd_jack_kctl_private_free;
170
171 return jack_kctl;
172error:
173 snd_ctl_free_one(kctl);
174 return NULL;
175}
176
177/**
178 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
179 * @jack: the jack instance which the kctl will attaching to
180 * @name: the name for the snd_kcontrol object
181 * @mask: a bitmask of enum snd_jack_type values that can be detected
182 * by this snd_jack_kctl object.
183 *
184 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
185 *
186 * Return: Zero if successful, or a negative error code on failure.
187 */
188int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
189{
190 struct snd_jack_kctl *jack_kctl;
191
192 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
193 if (!jack_kctl)
194 return -ENOMEM;
195
196 snd_jack_kctl_add(jack, jack_kctl);
197 return 0;
198}
199EXPORT_SYMBOL(snd_jack_add_new_kctl);
200
Mark Browne76d8ce2008-07-28 19:05:35 +0100201/**
202 * snd_jack_new - Create a new jack
203 * @card: the card instance
204 * @id: an identifying string for this jack
205 * @type: a bitmask of enum snd_jack_type values that can be detected by
206 * this jack
207 * @jjack: Used to provide the allocated jack object to the caller.
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800208 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
209 * @phantom_jack: Don't create a input device for phantom jacks.
Mark Browne76d8ce2008-07-28 19:05:35 +0100210 *
211 * Creates a new jack object.
212 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100213 * Return: Zero if successful, or a negative error code on failure.
214 * On success @jjack will be initialised.
Mark Browne76d8ce2008-07-28 19:05:35 +0100215 */
216int snd_jack_new(struct snd_card *card, const char *id, int type,
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800217 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
Mark Browne76d8ce2008-07-28 19:05:35 +0100218{
219 struct snd_jack *jack;
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800220 struct snd_jack_kctl *jack_kctl = NULL;
Mark Browne76d8ce2008-07-28 19:05:35 +0100221 int err;
222 static struct snd_device_ops ops = {
223 .dev_free = snd_jack_dev_free,
Takashi Iwaife0d1282016-02-17 09:44:25 +0100224#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +0100225 .dev_register = snd_jack_dev_register,
Takashi Iwai32b85442013-11-14 15:35:46 +0100226 .dev_disconnect = snd_jack_dev_disconnect,
Takashi Iwaife0d1282016-02-17 09:44:25 +0100227#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100228 };
229
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800230 if (initial_kctl) {
231 jack_kctl = snd_jack_kctl_new(card, id, type);
232 if (!jack_kctl)
233 return -ENOMEM;
234 }
235
Mark Browne76d8ce2008-07-28 19:05:35 +0100236 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
237 if (jack == NULL)
238 return -ENOMEM;
239
Matthew Ranostay282cd762008-10-25 01:05:29 -0400240 jack->id = kstrdup(id, GFP_KERNEL);
Mark Browne76d8ce2008-07-28 19:05:35 +0100241
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800242 /* don't creat input device for phantom jack */
243 if (!phantom_jack) {
Takashi Iwaife0d1282016-02-17 09:44:25 +0100244#ifdef CONFIG_SND_JACK_INPUT_DEV
245 int i;
246
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800247 jack->input_dev = input_allocate_device();
248 if (jack->input_dev == NULL) {
249 err = -ENOMEM;
250 goto fail_input;
251 }
252
253 jack->input_dev->phys = "ALSA";
254
255 jack->type = type;
256
Banajit Goswamid566f792016-11-21 17:39:12 -0800257 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++)
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800258 if (type & (1 << i))
259 input_set_capability(jack->input_dev, EV_SW,
260 jack_switch_types[i]);
261
Takashi Iwaife0d1282016-02-17 09:44:25 +0100262#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100263 }
264
Mark Browne76d8ce2008-07-28 19:05:35 +0100265 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
266 if (err < 0)
267 goto fail_input;
268
Jie Yang9058cbe2015-04-27 21:20:56 +0800269 jack->card = card;
270 INIT_LIST_HEAD(&jack->kctl_list);
271
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800272 if (initial_kctl)
273 snd_jack_kctl_add(jack, jack_kctl);
274
Mark Browne76d8ce2008-07-28 19:05:35 +0100275 *jjack = jack;
276
277 return 0;
278
279fail_input:
Takashi Iwaife0d1282016-02-17 09:44:25 +0100280#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +0100281 input_free_device(jack->input_dev);
Takashi Iwaife0d1282016-02-17 09:44:25 +0100282#endif
Lu Guanquneeda276b2011-02-21 13:45:04 +0800283 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +0100284 kfree(jack);
285 return err;
286}
287EXPORT_SYMBOL(snd_jack_new);
288
Takashi Iwaife0d1282016-02-17 09:44:25 +0100289#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +0100290/**
291 * snd_jack_set_parent - Set the parent device for a jack
292 *
293 * @jack: The jack to configure
294 * @parent: The device to set as parent for the jack.
295 *
Mark Browna2e888f2012-05-07 17:10:21 +0100296 * Set the parent for the jack devices in the device tree. This
Mark Browne76d8ce2008-07-28 19:05:35 +0100297 * function is only valid prior to registration of the jack. If no
298 * parent is configured then the parent device will be the sound card.
299 */
300void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
301{
302 WARN_ON(jack->registered);
Takashi Iwai43b2cd52015-04-30 15:25:00 +0200303 if (!jack->input_dev)
304 return;
Mark Browne76d8ce2008-07-28 19:05:35 +0100305
306 jack->input_dev->dev.parent = parent;
307}
308EXPORT_SYMBOL(snd_jack_set_parent);
309
310/**
Mark Brownebb812c2010-03-17 18:07:12 +0000311 * snd_jack_set_key - Set a key mapping on a jack
312 *
313 * @jack: The jack to configure
314 * @type: Jack report type for this key
315 * @keytype: Input layer key type to be reported
316 *
317 * Map a SND_JACK_BTN_ button type to an input layer key, allowing
318 * reporting of keys on accessories via the jack abstraction. If no
319 * mapping is provided but keys are enabled in the jack type then
320 * BTN_n numeric buttons will be reported.
321 *
Mark Browna2e888f2012-05-07 17:10:21 +0100322 * If jacks are not reporting via the input API this call will have no
323 * effect.
324 *
Mark Brownebb812c2010-03-17 18:07:12 +0000325 * Note that this is intended to be use by simple devices with small
326 * numbers of keys that can be reported. It is also possible to
327 * access the input device directly - devices with complex input
328 * capabilities on accessories should consider doing this rather than
329 * using this abstraction.
330 *
331 * This function may only be called prior to registration of the jack.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100332 *
333 * Return: Zero if successful, or a negative error code on failure.
Mark Brownebb812c2010-03-17 18:07:12 +0000334 */
335int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
336 int keytype)
337{
338 int key = fls(SND_JACK_BTN_0) - fls(type);
339
340 WARN_ON(jack->registered);
341
342 if (!keytype || key >= ARRAY_SIZE(jack->key))
343 return -EINVAL;
344
345 jack->type |= type;
346 jack->key[key] = keytype;
Mark Brownebb812c2010-03-17 18:07:12 +0000347 return 0;
348}
349EXPORT_SYMBOL(snd_jack_set_key);
Takashi Iwaife0d1282016-02-17 09:44:25 +0100350#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Brownebb812c2010-03-17 18:07:12 +0000351
352/**
Mark Browne76d8ce2008-07-28 19:05:35 +0100353 * snd_jack_report - Report the current status of a jack
354 *
355 * @jack: The jack to report status for
356 * @status: The current status of the jack
357 */
358void snd_jack_report(struct snd_jack *jack, int status)
359{
Jie Yang9058cbe2015-04-27 21:20:56 +0800360 struct snd_jack_kctl *jack_kctl;
Takashi Iwaife0d1282016-02-17 09:44:25 +0100361#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Brownbd8a71a2009-01-03 16:56:56 +0000362 int i;
Takashi Iwaife0d1282016-02-17 09:44:25 +0100363#endif
Mark Brownbd8a71a2009-01-03 16:56:56 +0000364
Mark Brown9a3f3712008-10-15 17:07:47 +0100365 if (!jack)
366 return;
367
Jie Yang6ed94952015-04-30 20:22:46 +0800368 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
369 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
370 status & jack_kctl->mask_bits);
371
Takashi Iwaife0d1282016-02-17 09:44:25 +0100372#ifdef CONFIG_SND_JACK_INPUT_DEV
Jie Yang6ed94952015-04-30 20:22:46 +0800373 if (!jack->input_dev)
374 return;
375
Mark Brownebb812c2010-03-17 18:07:12 +0000376 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
377 int testbit = SND_JACK_BTN_0 >> i;
378
379 if (jack->type & testbit)
380 input_report_key(jack->input_dev, jack->key[i],
381 status & testbit);
382 }
383
Mark Brown1c6e5552010-03-17 15:36:38 +0000384 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
Mark Brownbd8a71a2009-01-03 16:56:56 +0000385 int testbit = 1 << i;
386 if (jack->type & testbit)
Mark Brown1c6e5552010-03-17 15:36:38 +0000387 input_report_switch(jack->input_dev,
388 jack_switch_types[i],
Mark Brownbd8a71a2009-01-03 16:56:56 +0000389 status & testbit);
390 }
Mark Browne76d8ce2008-07-28 19:05:35 +0100391
392 input_sync(jack->input_dev);
Takashi Iwaife0d1282016-02-17 09:44:25 +0100393#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100394}
395EXPORT_SYMBOL(snd_jack_report);