blob: ba448bf51cec0ce1e42a167c3f28a500a9b59e34 [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,
Mark Brownbd8a71a2009-01-03 16:56:56 +000046};
Takashi Iwaife0d1282016-02-17 09:44:25 +010047#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Brownbd8a71a2009-01-03 16:56:56 +000048
Takashi Iwai32b85442013-11-14 15:35:46 +010049static int snd_jack_dev_disconnect(struct snd_device *device)
Mark Browne76d8ce2008-07-28 19:05:35 +010050{
Takashi Iwaife0d1282016-02-17 09:44:25 +010051#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +010052 struct snd_jack *jack = device->device_data;
53
Takashi Iwai32b85442013-11-14 15:35:46 +010054 if (!jack->input_dev)
55 return 0;
Takashi Iwai9d590652009-04-14 16:13:58 +020056
Mark Browne76d8ce2008-07-28 19:05:35 +010057 /* If the input device is registered with the input subsystem
58 * then we need to use a different deallocator. */
59 if (jack->registered)
60 input_unregister_device(jack->input_dev);
61 else
62 input_free_device(jack->input_dev);
Takashi Iwai32b85442013-11-14 15:35:46 +010063 jack->input_dev = NULL;
Takashi Iwaife0d1282016-02-17 09:44:25 +010064#endif /* CONFIG_SND_JACK_INPUT_DEV */
Takashi Iwai32b85442013-11-14 15:35:46 +010065 return 0;
66}
67
68static int snd_jack_dev_free(struct snd_device *device)
69{
70 struct snd_jack *jack = device->device_data;
Jie Yang9058cbe2015-04-27 21:20:56 +080071 struct snd_card *card = device->card;
72 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
Takashi Iwai32b85442013-11-14 15:35:46 +010073
Jie Yang9058cbe2015-04-27 21:20:56 +080074 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
75 list_del_init(&jack_kctl->list);
76 snd_ctl_remove(card, jack_kctl->kctl);
77 }
Takashi Iwai32b85442013-11-14 15:35:46 +010078 if (jack->private_free)
79 jack->private_free(jack);
80
81 snd_jack_dev_disconnect(device);
Mark Browne76d8ce2008-07-28 19:05:35 +010082
Matthew Ranostay282cd762008-10-25 01:05:29 -040083 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010084 kfree(jack);
85
86 return 0;
87}
88
Takashi Iwaife0d1282016-02-17 09:44:25 +010089#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +010090static int snd_jack_dev_register(struct snd_device *device)
91{
92 struct snd_jack *jack = device->device_data;
93 struct snd_card *card = device->card;
Mark Brownebb812c2010-03-17 18:07:12 +000094 int err, i;
Mark Browne76d8ce2008-07-28 19:05:35 +010095
96 snprintf(jack->name, sizeof(jack->name), "%s %s",
Takashi Iwai2678f602009-02-18 16:46:27 +010097 card->shortname, jack->id);
Takashi Iwai43b2cd52015-04-30 15:25:00 +020098
99 if (!jack->input_dev)
100 return 0;
101
Mark Browne76d8ce2008-07-28 19:05:35 +0100102 jack->input_dev->name = jack->name;
103
104 /* Default to the sound card device. */
105 if (!jack->input_dev->dev.parent)
Kay Sievers1f3fff72009-06-10 19:50:33 +0200106 jack->input_dev->dev.parent = snd_card_get_device_link(card);
Mark Browne76d8ce2008-07-28 19:05:35 +0100107
Mark Brownebb812c2010-03-17 18:07:12 +0000108 /* Add capabilities for any keys that are enabled */
109 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
110 int testbit = SND_JACK_BTN_0 >> i;
111
112 if (!(jack->type & testbit))
113 continue;
114
115 if (!jack->key[i])
116 jack->key[i] = BTN_0 + i;
117
118 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
119 }
120
Mark Browne76d8ce2008-07-28 19:05:35 +0100121 err = input_register_device(jack->input_dev);
122 if (err == 0)
123 jack->registered = 1;
124
125 return err;
126}
Takashi Iwaife0d1282016-02-17 09:44:25 +0100127#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100128
Jie Yang9058cbe2015-04-27 21:20:56 +0800129static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
130{
131 struct snd_jack_kctl *jack_kctl;
132
133 jack_kctl = kctl->private_data;
134 if (jack_kctl) {
135 list_del(&jack_kctl->list);
136 kfree(jack_kctl);
137 }
138}
139
140static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
141{
142 list_add_tail(&jack_kctl->list, &jack->kctl_list);
143}
144
145static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
146{
147 struct snd_kcontrol *kctl;
148 struct snd_jack_kctl *jack_kctl;
149 int err;
150
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800151 kctl = snd_kctl_jack_new(name, card);
Jie Yang9058cbe2015-04-27 21:20:56 +0800152 if (!kctl)
153 return NULL;
154
155 err = snd_ctl_add(card, kctl);
156 if (err < 0)
157 return NULL;
158
159 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
160
161 if (!jack_kctl)
162 goto error;
163
164 jack_kctl->kctl = kctl;
165 jack_kctl->mask_bits = mask;
166
167 kctl->private_data = jack_kctl;
168 kctl->private_free = snd_jack_kctl_private_free;
169
170 return jack_kctl;
171error:
172 snd_ctl_free_one(kctl);
173 return NULL;
174}
175
176/**
177 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
178 * @jack: the jack instance which the kctl will attaching to
179 * @name: the name for the snd_kcontrol object
180 * @mask: a bitmask of enum snd_jack_type values that can be detected
181 * by this snd_jack_kctl object.
182 *
183 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
184 *
185 * Return: Zero if successful, or a negative error code on failure.
186 */
187int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
188{
189 struct snd_jack_kctl *jack_kctl;
190
191 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
192 if (!jack_kctl)
193 return -ENOMEM;
194
195 snd_jack_kctl_add(jack, jack_kctl);
196 return 0;
197}
198EXPORT_SYMBOL(snd_jack_add_new_kctl);
199
Mark Browne76d8ce2008-07-28 19:05:35 +0100200/**
201 * snd_jack_new - Create a new jack
202 * @card: the card instance
203 * @id: an identifying string for this jack
204 * @type: a bitmask of enum snd_jack_type values that can be detected by
205 * this jack
206 * @jjack: Used to provide the allocated jack object to the caller.
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800207 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
208 * @phantom_jack: Don't create a input device for phantom jacks.
Mark Browne76d8ce2008-07-28 19:05:35 +0100209 *
210 * Creates a new jack object.
211 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100212 * Return: Zero if successful, or a negative error code on failure.
213 * On success @jjack will be initialised.
Mark Browne76d8ce2008-07-28 19:05:35 +0100214 */
215int snd_jack_new(struct snd_card *card, const char *id, int type,
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800216 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
Mark Browne76d8ce2008-07-28 19:05:35 +0100217{
218 struct snd_jack *jack;
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800219 struct snd_jack_kctl *jack_kctl = NULL;
Mark Browne76d8ce2008-07-28 19:05:35 +0100220 int err;
221 static struct snd_device_ops ops = {
222 .dev_free = snd_jack_dev_free,
Takashi Iwaife0d1282016-02-17 09:44:25 +0100223#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +0100224 .dev_register = snd_jack_dev_register,
Takashi Iwai32b85442013-11-14 15:35:46 +0100225 .dev_disconnect = snd_jack_dev_disconnect,
Takashi Iwaife0d1282016-02-17 09:44:25 +0100226#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100227 };
228
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800229 if (initial_kctl) {
230 jack_kctl = snd_jack_kctl_new(card, id, type);
231 if (!jack_kctl)
232 return -ENOMEM;
233 }
234
Mark Browne76d8ce2008-07-28 19:05:35 +0100235 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
236 if (jack == NULL)
237 return -ENOMEM;
238
Matthew Ranostay282cd762008-10-25 01:05:29 -0400239 jack->id = kstrdup(id, GFP_KERNEL);
Mark Browne76d8ce2008-07-28 19:05:35 +0100240
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800241 /* don't creat input device for phantom jack */
242 if (!phantom_jack) {
Takashi Iwaife0d1282016-02-17 09:44:25 +0100243#ifdef CONFIG_SND_JACK_INPUT_DEV
244 int i;
245
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800246 jack->input_dev = input_allocate_device();
247 if (jack->input_dev == NULL) {
248 err = -ENOMEM;
249 goto fail_input;
250 }
251
252 jack->input_dev->phys = "ALSA";
253
254 jack->type = type;
255
Banajit Goswamid566f792016-11-21 17:39:12 -0800256 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++)
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800257 if (type & (1 << i))
258 input_set_capability(jack->input_dev, EV_SW,
259 jack_switch_types[i]);
260
Takashi Iwaife0d1282016-02-17 09:44:25 +0100261#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100262 }
263
Mark Browne76d8ce2008-07-28 19:05:35 +0100264 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
265 if (err < 0)
266 goto fail_input;
267
Jie Yang9058cbe2015-04-27 21:20:56 +0800268 jack->card = card;
269 INIT_LIST_HEAD(&jack->kctl_list);
270
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800271 if (initial_kctl)
272 snd_jack_kctl_add(jack, jack_kctl);
273
Mark Browne76d8ce2008-07-28 19:05:35 +0100274 *jjack = jack;
275
276 return 0;
277
278fail_input:
Takashi Iwaife0d1282016-02-17 09:44:25 +0100279#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +0100280 input_free_device(jack->input_dev);
Takashi Iwaife0d1282016-02-17 09:44:25 +0100281#endif
Lu Guanquneeda276b2011-02-21 13:45:04 +0800282 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +0100283 kfree(jack);
284 return err;
285}
286EXPORT_SYMBOL(snd_jack_new);
287
Takashi Iwaife0d1282016-02-17 09:44:25 +0100288#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Browne76d8ce2008-07-28 19:05:35 +0100289/**
290 * snd_jack_set_parent - Set the parent device for a jack
291 *
292 * @jack: The jack to configure
293 * @parent: The device to set as parent for the jack.
294 *
Mark Browna2e888f2012-05-07 17:10:21 +0100295 * Set the parent for the jack devices in the device tree. This
Mark Browne76d8ce2008-07-28 19:05:35 +0100296 * function is only valid prior to registration of the jack. If no
297 * parent is configured then the parent device will be the sound card.
298 */
299void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
300{
301 WARN_ON(jack->registered);
Takashi Iwai43b2cd52015-04-30 15:25:00 +0200302 if (!jack->input_dev)
303 return;
Mark Browne76d8ce2008-07-28 19:05:35 +0100304
305 jack->input_dev->dev.parent = parent;
306}
307EXPORT_SYMBOL(snd_jack_set_parent);
308
309/**
Mark Brownebb812c2010-03-17 18:07:12 +0000310 * snd_jack_set_key - Set a key mapping on a jack
311 *
312 * @jack: The jack to configure
313 * @type: Jack report type for this key
314 * @keytype: Input layer key type to be reported
315 *
316 * Map a SND_JACK_BTN_ button type to an input layer key, allowing
317 * reporting of keys on accessories via the jack abstraction. If no
318 * mapping is provided but keys are enabled in the jack type then
319 * BTN_n numeric buttons will be reported.
320 *
Mark Browna2e888f2012-05-07 17:10:21 +0100321 * If jacks are not reporting via the input API this call will have no
322 * effect.
323 *
Mark Brownebb812c2010-03-17 18:07:12 +0000324 * Note that this is intended to be use by simple devices with small
325 * numbers of keys that can be reported. It is also possible to
326 * access the input device directly - devices with complex input
327 * capabilities on accessories should consider doing this rather than
328 * using this abstraction.
329 *
330 * This function may only be called prior to registration of the jack.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100331 *
332 * Return: Zero if successful, or a negative error code on failure.
Mark Brownebb812c2010-03-17 18:07:12 +0000333 */
334int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
335 int keytype)
336{
337 int key = fls(SND_JACK_BTN_0) - fls(type);
338
339 WARN_ON(jack->registered);
340
341 if (!keytype || key >= ARRAY_SIZE(jack->key))
342 return -EINVAL;
343
344 jack->type |= type;
345 jack->key[key] = keytype;
Mark Brownebb812c2010-03-17 18:07:12 +0000346 return 0;
347}
348EXPORT_SYMBOL(snd_jack_set_key);
Takashi Iwaife0d1282016-02-17 09:44:25 +0100349#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Brownebb812c2010-03-17 18:07:12 +0000350
351/**
Mark Browne76d8ce2008-07-28 19:05:35 +0100352 * snd_jack_report - Report the current status of a jack
353 *
354 * @jack: The jack to report status for
355 * @status: The current status of the jack
356 */
357void snd_jack_report(struct snd_jack *jack, int status)
358{
Jie Yang9058cbe2015-04-27 21:20:56 +0800359 struct snd_jack_kctl *jack_kctl;
Takashi Iwaife0d1282016-02-17 09:44:25 +0100360#ifdef CONFIG_SND_JACK_INPUT_DEV
Mark Brownbd8a71a2009-01-03 16:56:56 +0000361 int i;
Takashi Iwaife0d1282016-02-17 09:44:25 +0100362#endif
Mark Brownbd8a71a2009-01-03 16:56:56 +0000363
Mark Brown9a3f3712008-10-15 17:07:47 +0100364 if (!jack)
365 return;
366
Jie Yang6ed94952015-04-30 20:22:46 +0800367 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
368 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
369 status & jack_kctl->mask_bits);
370
Takashi Iwaife0d1282016-02-17 09:44:25 +0100371#ifdef CONFIG_SND_JACK_INPUT_DEV
Jie Yang6ed94952015-04-30 20:22:46 +0800372 if (!jack->input_dev)
373 return;
374
Mark Brownebb812c2010-03-17 18:07:12 +0000375 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
376 int testbit = SND_JACK_BTN_0 >> i;
377
378 if (jack->type & testbit)
379 input_report_key(jack->input_dev, jack->key[i],
380 status & testbit);
381 }
382
Mark Brown1c6e5552010-03-17 15:36:38 +0000383 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
Mark Brownbd8a71a2009-01-03 16:56:56 +0000384 int testbit = 1 << i;
385 if (jack->type & testbit)
Mark Brown1c6e5552010-03-17 15:36:38 +0000386 input_report_switch(jack->input_dev,
387 jack_switch_types[i],
Mark Brownbd8a71a2009-01-03 16:56:56 +0000388 status & testbit);
389 }
Mark Browne76d8ce2008-07-28 19:05:35 +0100390
391 input_sync(jack->input_dev);
Takashi Iwaife0d1282016-02-17 09:44:25 +0100392#endif /* CONFIG_SND_JACK_INPUT_DEV */
Mark Browne76d8ce2008-07-28 19:05:35 +0100393}
394EXPORT_SYMBOL(snd_jack_report);