2 * ams-delta.c -- SoC audio for Amstrad E3 (Delta) videophone
4 * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6 * Initially based on sound/soc/omap/osk5912.x
7 * Copyright (C) 2008 Mistral Solutions
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/gpio/consumer.h>
26 #include <linux/spinlock.h>
27 #include <linux/tty.h>
28 #include <linux/module.h>
30 #include <sound/soc.h>
31 #include <sound/jack.h>
33 #include <asm/mach-types.h>
35 #include <linux/platform_data/asoc-ti-mcbsp.h>
37 #include "omap-mcbsp.h"
38 #include "../codecs/cx20442.h"
40 /* Board specific DAPM widgets */
41 static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
43 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
44 SND_SOC_DAPM_HP("Earpiece", NULL),
45 /* Handsfree/Speakerphone */
46 SND_SOC_DAPM_MIC("Microphone", NULL),
47 SND_SOC_DAPM_SPK("Speaker", NULL),
50 /* How they are connected to codec pins */
51 static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
52 {"TELIN", NULL, "Mouthpiece"},
53 {"Earpiece", NULL, "TELOUT"},
55 {"MIC", NULL, "Microphone"},
56 {"Speaker", NULL, "SPKOUT"},
60 * Controls, functional after the modem line discipline is activated.
63 /* Virtual switch: audio input/output constellations */
64 static const char *ams_delta_audio_mode[] =
65 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
67 /* Selection <-> pin translation */
68 #define AMS_DELTA_MOUTHPIECE 0
69 #define AMS_DELTA_EARPIECE 1
70 #define AMS_DELTA_MICROPHONE 2
71 #define AMS_DELTA_SPEAKER 3
72 #define AMS_DELTA_AGC 4
74 #define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
75 (1 << AMS_DELTA_MICROPHONE))
76 #define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
77 (1 << AMS_DELTA_EARPIECE))
78 #define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
79 (1 << AMS_DELTA_SPEAKER))
80 #define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
82 static const unsigned short ams_delta_audio_mode_pins[] = {
86 AMS_DELTA_SPEAKERPHONE,
89 static unsigned short ams_delta_audio_agc;
92 * Used for passing a codec structure pointer
93 * from the board initialization code to the tty line discipline.
95 static struct snd_soc_component *cx20442_codec;
97 static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
98 struct snd_ctl_elem_value *ucontrol)
100 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
101 struct snd_soc_dapm_context *dapm = &card->dapm;
102 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
104 int pin, changed = 0;
106 /* Refuse any mode changes if we are not able to control the codec. */
107 if (!cx20442_codec->card->pop_time)
110 if (ucontrol->value.enumerated.item[0] >= control->items)
113 snd_soc_dapm_mutex_lock(dapm);
115 /* Translate selection to bitmap */
116 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
118 /* Setup pins after corresponding bits if changed */
119 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
121 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
124 snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
126 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
128 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
129 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
132 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
134 snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece");
136 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
137 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
140 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
142 snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone");
144 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
145 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
148 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
150 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
152 pin = !!(pins & (1 << AMS_DELTA_AGC));
153 if (pin != ams_delta_audio_agc) {
154 ams_delta_audio_agc = pin;
157 snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
159 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
163 snd_soc_dapm_sync_unlocked(dapm);
165 snd_soc_dapm_mutex_unlock(dapm);
170 static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
173 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
174 struct snd_soc_dapm_context *dapm = &card->dapm;
175 unsigned short pins, mode;
177 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
178 AMS_DELTA_MOUTHPIECE) |
179 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
180 AMS_DELTA_EARPIECE));
182 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
183 AMS_DELTA_MICROPHONE);
185 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
186 AMS_DELTA_MICROPHONE) |
187 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
189 (ams_delta_audio_agc << AMS_DELTA_AGC));
191 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
192 if (pins == ams_delta_audio_mode_pins[mode])
195 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
198 ucontrol->value.enumerated.item[0] = mode;
203 static const SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
204 ams_delta_audio_mode);
206 static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
207 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum,
208 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
212 static struct snd_soc_jack ams_delta_hook_switch;
213 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
215 .name = "hook_switch",
216 .report = SND_JACK_HEADSET,
218 .debounce_time = 150,
222 /* After we are able to control the codec over the modem,
223 * the hook switch can be used for dynamic DAPM reconfiguration. */
224 static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
228 .mask = SND_JACK_MICROPHONE,
232 .mask = SND_JACK_HEADPHONE,
237 .mask = SND_JACK_MICROPHONE,
242 .mask = SND_JACK_HEADPHONE,
249 * Modem line discipline, required for making above controls functional.
250 * Activated from userspace with ldattach, possibly invoked from udev rule.
253 /* To actually apply any modem controlled configuration changes to the codec,
254 * we must connect codec DAI pins to the modem for a moment. Be careful not
255 * to interfere with our digital mute function that shares the same hardware. */
256 static struct timer_list cx81801_timer;
257 static bool cx81801_cmd_pending;
258 static bool ams_delta_muted;
259 static DEFINE_SPINLOCK(ams_delta_lock);
260 static struct gpio_desc *gpiod_modem_codec;
262 static void cx81801_timeout(struct timer_list *unused)
266 spin_lock(&ams_delta_lock);
267 cx81801_cmd_pending = 0;
268 muted = ams_delta_muted;
269 spin_unlock(&ams_delta_lock);
271 /* Reconnect the codec DAI back from the modem to the CPU DAI
272 * only if digital mute still off */
274 gpiod_set_value(gpiod_modem_codec, 0);
277 /* Line discipline .open() */
278 static int cx81801_open(struct tty_struct *tty)
286 * Pass the codec structure pointer for use by other ldisc callbacks,
287 * both the card and the codec specific parts.
289 tty->disc_data = cx20442_codec;
291 ret = v253_ops.open(tty);
294 tty->disc_data = NULL;
299 /* Line discipline .close() */
300 static void cx81801_close(struct tty_struct *tty)
302 struct snd_soc_component *component = tty->disc_data;
303 struct snd_soc_dapm_context *dapm = &component->card->dapm;
305 del_timer_sync(&cx81801_timer);
307 /* Prevent the hook switch from further changing the DAPM pins */
308 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
315 /* Revert back to default audio input/output constellation */
316 snd_soc_dapm_mutex_lock(dapm);
318 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
319 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
320 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
321 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
322 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
324 snd_soc_dapm_sync_unlocked(dapm);
326 snd_soc_dapm_mutex_unlock(dapm);
329 /* Line discipline .hangup() */
330 static int cx81801_hangup(struct tty_struct *tty)
336 /* Line discipline .receive_buf() */
337 static void cx81801_receive(struct tty_struct *tty,
338 const unsigned char *cp, char *fp, int count)
340 struct snd_soc_component *component = tty->disc_data;
341 const unsigned char *c;
347 if (!component->card->pop_time) {
348 /* First modem response, complete setup procedure */
350 /* Initialize timer used for config pulse generation */
351 timer_setup(&cx81801_timer, cx81801_timeout, 0);
353 v253_ops.receive_buf(tty, cp, fp, count);
355 /* Link hook switch to DAPM pins */
356 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
357 ARRAY_SIZE(ams_delta_hook_switch_pins),
358 ams_delta_hook_switch_pins);
360 dev_warn(component->dev,
361 "Failed to link hook switch to DAPM pins, "
362 "will continue with hook switch unlinked.\n");
367 v253_ops.receive_buf(tty, cp, fp, count);
369 for (c = &cp[count - 1]; c >= cp; c--) {
372 /* Complete modem response received, apply config to codec */
374 spin_lock_bh(&ams_delta_lock);
375 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
376 apply = !ams_delta_muted && !cx81801_cmd_pending;
377 cx81801_cmd_pending = 1;
378 spin_unlock_bh(&ams_delta_lock);
380 /* Apply config pulse by connecting the codec to the modem
381 * if not already done */
383 gpiod_set_value(gpiod_modem_codec, 1);
388 /* Line discipline .write_wakeup() */
389 static void cx81801_wakeup(struct tty_struct *tty)
391 v253_ops.write_wakeup(tty);
394 static struct tty_ldisc_ops cx81801_ops = {
395 .magic = TTY_LDISC_MAGIC,
397 .owner = THIS_MODULE,
398 .open = cx81801_open,
399 .close = cx81801_close,
400 .hangup = cx81801_hangup,
401 .receive_buf = cx81801_receive,
402 .write_wakeup = cx81801_wakeup,
407 * Even if not very useful, the sound card can still work without any of the
408 * above functonality activated. You can still control its audio input/output
409 * constellation and speakerphone gain from userspace by issuing AT commands
410 * over the modem port.
413 static struct snd_soc_ops ams_delta_ops;
416 /* Digital mute implemented using modem/CPU multiplexer.
417 * Shares hardware with codec config pulse generation */
418 static bool ams_delta_muted = 1;
420 static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
424 if (ams_delta_muted == mute)
427 spin_lock_bh(&ams_delta_lock);
428 ams_delta_muted = mute;
429 apply = !cx81801_cmd_pending;
430 spin_unlock_bh(&ams_delta_lock);
433 gpiod_set_value(gpiod_modem_codec, !!mute);
437 /* Our codec DAI probably doesn't have its own .ops structure */
438 static const struct snd_soc_dai_ops ams_delta_dai_ops = {
439 .digital_mute = ams_delta_digital_mute,
442 /* Will be used if the codec ever has its own digital_mute function */
443 static int ams_delta_startup(struct snd_pcm_substream *substream)
445 return ams_delta_digital_mute(NULL, 0);
448 static void ams_delta_shutdown(struct snd_pcm_substream *substream)
450 ams_delta_digital_mute(NULL, 1);
455 * Card initialization
458 static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
460 struct snd_soc_dai *codec_dai = rtd->codec_dai;
461 struct snd_soc_card *card = rtd->card;
462 struct snd_soc_dapm_context *dapm = &card->dapm;
464 /* Codec is ready, now add/activate board specific controls */
466 /* Store a pointer to the codec structure for tty ldisc use */
467 cx20442_codec = rtd->codec_dai->component;
469 /* Add hook switch - can be used to control the codec from userspace
470 * even if line discipline fails */
471 ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET,
472 &ams_delta_hook_switch, NULL, 0);
475 "Failed to allocate resources for hook switch, "
476 "will continue without one.\n");
478 ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch,
479 ARRAY_SIZE(ams_delta_hook_switch_gpios),
480 ams_delta_hook_switch_gpios);
483 "Failed to set up hook switch GPIO line, "
484 "will continue with hook switch inactive.\n");
487 gpiod_modem_codec = devm_gpiod_get(card->dev, "modem_codec",
489 if (IS_ERR(gpiod_modem_codec)) {
490 dev_warn(card->dev, "Failed to obtain modem_codec GPIO\n");
494 /* Set up digital mute if not provided by the codec */
495 if (!codec_dai->driver->ops) {
496 codec_dai->driver->ops = &ams_delta_dai_ops;
498 ams_delta_ops.startup = ams_delta_startup;
499 ams_delta_ops.shutdown = ams_delta_shutdown;
502 /* Register optional line discipline for over the modem control */
503 ret = tty_register_ldisc(N_V253, &cx81801_ops);
506 "Failed to register line discipline, "
507 "will continue without any controls.\n");
511 /* Set up initial pin constellation */
512 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
513 snd_soc_dapm_disable_pin(dapm, "Speaker");
514 snd_soc_dapm_disable_pin(dapm, "AGCIN");
515 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
520 /* DAI glue - connects codec <--> CPU */
521 static struct snd_soc_dai_link ams_delta_dai_link = {
523 .stream_name = "CX20442",
524 .cpu_dai_name = "omap-mcbsp.1",
525 .codec_dai_name = "cx20442-voice",
526 .init = ams_delta_cx20442_init,
527 .platform_name = "omap-mcbsp.1",
528 .codec_name = "cx20442-codec",
529 .ops = &ams_delta_ops,
530 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
531 SND_SOC_DAIFMT_CBM_CFM,
534 /* Audio card driver */
535 static struct snd_soc_card ams_delta_audio_card = {
537 .owner = THIS_MODULE,
538 .dai_link = &ams_delta_dai_link,
541 .controls = ams_delta_audio_controls,
542 .num_controls = ARRAY_SIZE(ams_delta_audio_controls),
543 .dapm_widgets = ams_delta_dapm_widgets,
544 .num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
545 .dapm_routes = ams_delta_audio_map,
546 .num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
549 /* Module init/exit */
550 static int ams_delta_probe(struct platform_device *pdev)
552 struct snd_soc_card *card = &ams_delta_audio_card;
555 card->dev = &pdev->dev;
557 ret = snd_soc_register_card(card);
559 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
566 static int ams_delta_remove(struct platform_device *pdev)
568 struct snd_soc_card *card = platform_get_drvdata(pdev);
570 if (tty_unregister_ldisc(N_V253) != 0)
572 "failed to unregister V253 line discipline\n");
574 snd_soc_unregister_card(card);
579 #define DRV_NAME "ams-delta-audio"
581 static struct platform_driver ams_delta_driver = {
585 .probe = ams_delta_probe,
586 .remove = ams_delta_remove,
589 module_platform_driver(ams_delta_driver);
591 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
592 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
593 MODULE_LICENSE("GPL");
594 MODULE_ALIAS("platform:" DRV_NAME);