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.h>
26 #include <linux/spinlock.h>
27 #include <linux/tty.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/jack.h>
32 #include <asm/mach-types.h>
34 #include <plat/board-ams-delta.h>
35 #include <plat/mcbsp.h>
37 #include "omap-mcbsp.h"
39 #include "../codecs/cx20442.h"
42 /* Board specific DAPM widgets */
43 static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
45 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
46 SND_SOC_DAPM_HP("Earpiece", NULL),
47 /* Handsfree/Speakerphone */
48 SND_SOC_DAPM_MIC("Microphone", NULL),
49 SND_SOC_DAPM_SPK("Speaker", NULL),
52 /* How they are connected to codec pins */
53 static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
54 {"TELIN", NULL, "Mouthpiece"},
55 {"Earpiece", NULL, "TELOUT"},
57 {"MIC", NULL, "Microphone"},
58 {"Speaker", NULL, "SPKOUT"},
62 * Controls, functional after the modem line discipline is activated.
65 /* Virtual switch: audio input/output constellations */
66 static const char *ams_delta_audio_mode[] =
67 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
69 /* Selection <-> pin translation */
70 #define AMS_DELTA_MOUTHPIECE 0
71 #define AMS_DELTA_EARPIECE 1
72 #define AMS_DELTA_MICROPHONE 2
73 #define AMS_DELTA_SPEAKER 3
74 #define AMS_DELTA_AGC 4
76 #define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
77 (1 << AMS_DELTA_MICROPHONE))
78 #define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
79 (1 << AMS_DELTA_EARPIECE))
80 #define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
81 (1 << AMS_DELTA_SPEAKER))
82 #define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
84 static const unsigned short ams_delta_audio_mode_pins[] = {
88 AMS_DELTA_SPEAKERPHONE,
91 static unsigned short ams_delta_audio_agc;
93 static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
94 struct snd_ctl_elem_value *ucontrol)
96 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
97 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
101 /* Refuse any mode changes if we are not able to control the codec. */
102 if (!codec->hw_write)
105 if (ucontrol->value.enumerated.item[0] >= control->max)
108 mutex_lock(&codec->mutex);
110 /* Translate selection to bitmap */
111 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
113 /* Setup pins after corresponding bits if changed */
114 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
115 if (pin != snd_soc_dapm_get_pin_status(codec, "Mouthpiece")) {
118 snd_soc_dapm_enable_pin(codec, "Mouthpiece");
120 snd_soc_dapm_disable_pin(codec, "Mouthpiece");
122 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
123 if (pin != snd_soc_dapm_get_pin_status(codec, "Earpiece")) {
126 snd_soc_dapm_enable_pin(codec, "Earpiece");
128 snd_soc_dapm_disable_pin(codec, "Earpiece");
130 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
131 if (pin != snd_soc_dapm_get_pin_status(codec, "Microphone")) {
134 snd_soc_dapm_enable_pin(codec, "Microphone");
136 snd_soc_dapm_disable_pin(codec, "Microphone");
138 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
139 if (pin != snd_soc_dapm_get_pin_status(codec, "Speaker")) {
142 snd_soc_dapm_enable_pin(codec, "Speaker");
144 snd_soc_dapm_disable_pin(codec, "Speaker");
146 pin = !!(pins & (1 << AMS_DELTA_AGC));
147 if (pin != ams_delta_audio_agc) {
148 ams_delta_audio_agc = pin;
151 snd_soc_dapm_enable_pin(codec, "AGCIN");
153 snd_soc_dapm_disable_pin(codec, "AGCIN");
156 snd_soc_dapm_sync(codec);
158 mutex_unlock(&codec->mutex);
163 static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
164 struct snd_ctl_elem_value *ucontrol)
166 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
167 unsigned short pins, mode;
169 pins = ((snd_soc_dapm_get_pin_status(codec, "Mouthpiece") <<
170 AMS_DELTA_MOUTHPIECE) |
171 (snd_soc_dapm_get_pin_status(codec, "Earpiece") <<
172 AMS_DELTA_EARPIECE));
174 pins |= (snd_soc_dapm_get_pin_status(codec, "Microphone") <<
175 AMS_DELTA_MICROPHONE);
177 pins = ((snd_soc_dapm_get_pin_status(codec, "Microphone") <<
178 AMS_DELTA_MICROPHONE) |
179 (snd_soc_dapm_get_pin_status(codec, "Speaker") <<
181 (ams_delta_audio_agc << AMS_DELTA_AGC));
183 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
184 if (pins == ams_delta_audio_mode_pins[mode])
187 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
190 ucontrol->value.enumerated.item[0] = mode;
195 static const struct soc_enum ams_delta_audio_enum[] = {
196 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ams_delta_audio_mode),
197 ams_delta_audio_mode),
200 static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
201 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum[0],
202 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
206 static struct snd_soc_jack ams_delta_hook_switch;
207 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
210 .name = "hook_switch",
211 .report = SND_JACK_HEADSET,
213 .debounce_time = 150,
217 /* After we are able to control the codec over the modem,
218 * the hook switch can be used for dynamic DAPM reconfiguration. */
219 static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
223 .mask = SND_JACK_MICROPHONE,
227 .mask = SND_JACK_HEADPHONE,
232 .mask = SND_JACK_MICROPHONE,
237 .mask = SND_JACK_HEADPHONE,
244 * Modem line discipline, required for making above controls functional.
245 * Activated from userspace with ldattach, possibly invoked from udev rule.
248 /* To actually apply any modem controlled configuration changes to the codec,
249 * we must connect codec DAI pins to the modem for a moment. Be carefull not
250 * to interfere with our digital mute function that shares the same hardware. */
251 static struct timer_list cx81801_timer;
252 static bool cx81801_cmd_pending;
253 static bool ams_delta_muted;
254 static DEFINE_SPINLOCK(ams_delta_lock);
256 static void cx81801_timeout(unsigned long data)
260 spin_lock(&ams_delta_lock);
261 cx81801_cmd_pending = 0;
262 muted = ams_delta_muted;
263 spin_unlock(&ams_delta_lock);
265 /* Reconnect the codec DAI back from the modem to the CPU DAI
266 * only if digital mute still off */
268 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC, 0);
272 * Used for passing a codec structure pointer
273 * from the board initialization code to the tty line discipline.
275 static struct snd_soc_codec *cx20442_codec;
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_codec *codec = tty->disc_data;
304 del_timer_sync(&cx81801_timer);
306 /* Prevent the hook switch from further changing the DAPM pins */
307 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
314 /* Revert back to default audio input/output constellation */
315 snd_soc_dapm_disable_pin(codec, "Mouthpiece");
316 snd_soc_dapm_enable_pin(codec, "Earpiece");
317 snd_soc_dapm_enable_pin(codec, "Microphone");
318 snd_soc_dapm_disable_pin(codec, "Speaker");
319 snd_soc_dapm_disable_pin(codec, "AGCIN");
320 snd_soc_dapm_sync(codec);
323 /* Line discipline .hangup() */
324 static int cx81801_hangup(struct tty_struct *tty)
330 /* Line discipline .recieve_buf() */
331 static void cx81801_receive(struct tty_struct *tty,
332 const unsigned char *cp, char *fp, int count)
334 struct snd_soc_codec *codec = tty->disc_data;
335 const unsigned char *c;
341 if (!codec->hw_write) {
342 /* First modem response, complete setup procedure */
344 /* Initialize timer used for config pulse generation */
345 setup_timer(&cx81801_timer, cx81801_timeout, 0);
347 v253_ops.receive_buf(tty, cp, fp, count);
349 /* Link hook switch to DAPM pins */
350 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
351 ARRAY_SIZE(ams_delta_hook_switch_pins),
352 ams_delta_hook_switch_pins);
355 "Failed to link hook switch to DAPM pins, "
356 "will continue with hook switch unlinked.\n");
361 v253_ops.receive_buf(tty, cp, fp, count);
363 for (c = &cp[count - 1]; c >= cp; c--) {
366 /* Complete modem response received, apply config to codec */
368 spin_lock_bh(&ams_delta_lock);
369 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
370 apply = !ams_delta_muted && !cx81801_cmd_pending;
371 cx81801_cmd_pending = 1;
372 spin_unlock_bh(&ams_delta_lock);
374 /* Apply config pulse by connecting the codec to the modem
375 * if not already done */
377 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
378 AMS_DELTA_LATCH2_MODEM_CODEC);
383 /* Line discipline .write_wakeup() */
384 static void cx81801_wakeup(struct tty_struct *tty)
386 v253_ops.write_wakeup(tty);
389 static struct tty_ldisc_ops cx81801_ops = {
390 .magic = TTY_LDISC_MAGIC,
392 .owner = THIS_MODULE,
393 .open = cx81801_open,
394 .close = cx81801_close,
395 .hangup = cx81801_hangup,
396 .receive_buf = cx81801_receive,
397 .write_wakeup = cx81801_wakeup,
402 * Even if not very usefull, the sound card can still work without any of the
403 * above functonality activated. You can still control its audio input/output
404 * constellation and speakerphone gain from userspace by issueing AT commands
405 * over the modem port.
408 static int ams_delta_hw_params(struct snd_pcm_substream *substream,
409 struct snd_pcm_hw_params *params)
411 struct snd_soc_pcm_runtime *rtd = substream->private_data;
413 /* Set cpu DAI configuration */
414 return snd_soc_dai_set_fmt(rtd->cpu_dai,
415 SND_SOC_DAIFMT_DSP_A |
416 SND_SOC_DAIFMT_NB_NF |
417 SND_SOC_DAIFMT_CBM_CFM);
420 static struct snd_soc_ops ams_delta_ops = {
421 .hw_params = ams_delta_hw_params,
425 /* Board specific codec bias level control */
426 static int ams_delta_set_bias_level(struct snd_soc_card *card,
427 enum snd_soc_bias_level level)
429 struct snd_soc_codec *codec = card->rtd->codec;
432 case SND_SOC_BIAS_ON:
433 case SND_SOC_BIAS_PREPARE:
434 case SND_SOC_BIAS_STANDBY:
435 if (codec->bias_level == SND_SOC_BIAS_OFF)
436 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET,
437 AMS_DELTA_LATCH2_MODEM_NRESET);
439 case SND_SOC_BIAS_OFF:
440 if (codec->bias_level != SND_SOC_BIAS_OFF)
441 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET,
444 codec->bias_level = level;
449 /* Digital mute implemented using modem/CPU multiplexer.
450 * Shares hardware with codec config pulse generation */
451 static bool ams_delta_muted = 1;
453 static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
457 if (ams_delta_muted == mute)
460 spin_lock_bh(&ams_delta_lock);
461 ams_delta_muted = mute;
462 apply = !cx81801_cmd_pending;
463 spin_unlock_bh(&ams_delta_lock);
466 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
467 mute ? AMS_DELTA_LATCH2_MODEM_CODEC : 0);
471 /* Our codec DAI probably doesn't have its own .ops structure */
472 static struct snd_soc_dai_ops ams_delta_dai_ops = {
473 .digital_mute = ams_delta_digital_mute,
476 /* Will be used if the codec ever has its own digital_mute function */
477 static int ams_delta_startup(struct snd_pcm_substream *substream)
479 return ams_delta_digital_mute(NULL, 0);
482 static void ams_delta_shutdown(struct snd_pcm_substream *substream)
484 ams_delta_digital_mute(NULL, 1);
489 * Card initialization
492 static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
494 struct snd_soc_codec *codec = rtd->codec;
495 struct snd_soc_dai *codec_dai = rtd->codec_dai;
496 struct snd_soc_card *card = rtd->card;
498 /* Codec is ready, now add/activate board specific controls */
500 /* Store a pointer to the codec structure for tty ldisc use */
501 cx20442_codec = codec;
503 /* Set up digital mute if not provided by the codec */
504 if (!codec_dai->driver->ops) {
505 codec_dai->driver->ops = &ams_delta_dai_ops;
506 } else if (!codec_dai->driver->ops->digital_mute) {
507 codec_dai->driver->ops->digital_mute = ams_delta_digital_mute;
509 ams_delta_ops.startup = ams_delta_startup;
510 ams_delta_ops.shutdown = ams_delta_shutdown;
513 /* Set codec bias level */
514 ams_delta_set_bias_level(card, SND_SOC_BIAS_STANDBY);
516 /* Add hook switch - can be used to control the codec from userspace
517 * even if line discipline fails */
518 ret = snd_soc_jack_new(rtd->codec, "hook_switch",
519 SND_JACK_HEADSET, &ams_delta_hook_switch);
522 "Failed to allocate resources for hook switch, "
523 "will continue without one.\n");
525 ret = snd_soc_jack_add_gpios(&ams_delta_hook_switch,
526 ARRAY_SIZE(ams_delta_hook_switch_gpios),
527 ams_delta_hook_switch_gpios);
530 "Failed to set up hook switch GPIO line, "
531 "will continue with hook switch inactive.\n");
534 /* Register optional line discipline for over the modem control */
535 ret = tty_register_ldisc(N_V253, &cx81801_ops);
538 "Failed to register line discipline, "
539 "will continue without any controls.\n");
543 /* Add board specific DAPM widgets and routes */
544 ret = snd_soc_dapm_new_controls(codec, ams_delta_dapm_widgets,
545 ARRAY_SIZE(ams_delta_dapm_widgets));
548 "Failed to register DAPM controls, "
549 "will continue without any.\n");
553 ret = snd_soc_dapm_add_routes(codec, ams_delta_audio_map,
554 ARRAY_SIZE(ams_delta_audio_map));
557 "Failed to set up DAPM routes, "
558 "will continue with codec default map.\n");
562 /* Set up initial pin constellation */
563 snd_soc_dapm_disable_pin(codec, "Mouthpiece");
564 snd_soc_dapm_enable_pin(codec, "Earpiece");
565 snd_soc_dapm_enable_pin(codec, "Microphone");
566 snd_soc_dapm_disable_pin(codec, "Speaker");
567 snd_soc_dapm_disable_pin(codec, "AGCIN");
568 snd_soc_dapm_disable_pin(codec, "AGCOUT");
569 snd_soc_dapm_sync(codec);
571 /* Add virtual switch */
572 ret = snd_soc_add_controls(codec, ams_delta_audio_controls,
573 ARRAY_SIZE(ams_delta_audio_controls));
576 "Failed to register audio mode control, "
577 "will continue without it.\n");
582 /* DAI glue - connects codec <--> CPU */
583 static struct snd_soc_dai_link ams_delta_dai_link = {
585 .stream_name = "CX20442",
586 .cpu_dai_name ="omap-mcbsp-dai.0",
587 .codec_dai_name = "cx20442-hifi",
588 .init = ams_delta_cx20442_init,
589 .platform_name = "omap-pcm-audio",
590 .codec_name = "cx20442-codec",
591 .ops = &ams_delta_ops,
594 /* Audio card driver */
595 static struct snd_soc_card ams_delta_audio_card = {
597 .dai_link = &ams_delta_dai_link,
599 .set_bias_level = ams_delta_set_bias_level,
602 /* Module init/exit */
603 static struct platform_device *ams_delta_audio_platform_device;
604 static struct platform_device *cx20442_platform_device;
606 static int __init ams_delta_module_init(void)
610 if (!(machine_is_ams_delta()))
613 ams_delta_audio_platform_device =
614 platform_device_alloc("soc-audio", -1);
615 if (!ams_delta_audio_platform_device)
618 platform_set_drvdata(ams_delta_audio_platform_device,
619 &ams_delta_audio_card);
621 ret = platform_device_add(ams_delta_audio_platform_device);
626 * Codec platform device could be registered from elsewhere (board?),
627 * but I do it here as it makes sense only if used with the card.
629 cx20442_platform_device =
630 platform_device_register_simple("cx20442-codec", -1, NULL, 0);
633 platform_device_put(ams_delta_audio_platform_device);
636 module_init(ams_delta_module_init);
638 static void __exit ams_delta_module_exit(void)
640 if (tty_unregister_ldisc(N_V253) != 0)
641 dev_warn(&ams_delta_audio_platform_device->dev,
642 "failed to unregister V253 line discipline\n");
644 snd_soc_jack_free_gpios(&ams_delta_hook_switch,
645 ARRAY_SIZE(ams_delta_hook_switch_gpios),
646 ams_delta_hook_switch_gpios);
648 /* Keep modem power on */
649 ams_delta_set_bias_level(&ams_delta_audio_card, SND_SOC_BIAS_STANDBY);
651 platform_device_unregister(cx20442_platform_device);
652 platform_device_unregister(ams_delta_audio_platform_device);
654 module_exit(ams_delta_module_exit);
656 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
657 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
658 MODULE_LICENSE("GPL");