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>
28 #include <linux/module.h>
30 #include <sound/soc.h>
31 #include <sound/jack.h>
33 #include <asm/mach-types.h>
35 #include <mach/board-ams-delta.h>
36 #include <linux/platform_data/asoc-ti-mcbsp.h>
38 #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 snd_soc_dapm_context
*dapm
= &codec
->dapm
;
98 struct soc_enum
*control
= (struct soc_enum
*)kcontrol
->private_value
;
100 int pin
, changed
= 0;
102 /* Refuse any mode changes if we are not able to control the codec. */
103 if (!codec
->hw_write
)
106 if (ucontrol
->value
.enumerated
.item
[0] >= control
->items
)
109 snd_soc_dapm_mutex_lock(dapm
);
111 /* Translate selection to bitmap */
112 pins
= ams_delta_audio_mode_pins
[ucontrol
->value
.enumerated
.item
[0]];
114 /* Setup pins after corresponding bits if changed */
115 pin
= !!(pins
& (1 << AMS_DELTA_MOUTHPIECE
));
117 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Mouthpiece")) {
120 snd_soc_dapm_enable_pin_unlocked(dapm
, "Mouthpiece");
122 snd_soc_dapm_disable_pin_unlocked(dapm
, "Mouthpiece");
124 pin
= !!(pins
& (1 << AMS_DELTA_EARPIECE
));
125 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Earpiece")) {
128 snd_soc_dapm_enable_pin_unlocked(dapm
, "Earpiece");
130 snd_soc_dapm_disable_pin_unlocked(dapm
, "Earpiece");
132 pin
= !!(pins
& (1 << AMS_DELTA_MICROPHONE
));
133 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Microphone")) {
136 snd_soc_dapm_enable_pin_unlocked(dapm
, "Microphone");
138 snd_soc_dapm_disable_pin_unlocked(dapm
, "Microphone");
140 pin
= !!(pins
& (1 << AMS_DELTA_SPEAKER
));
141 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Speaker")) {
144 snd_soc_dapm_enable_pin_unlocked(dapm
, "Speaker");
146 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
148 pin
= !!(pins
& (1 << AMS_DELTA_AGC
));
149 if (pin
!= ams_delta_audio_agc
) {
150 ams_delta_audio_agc
= pin
;
153 snd_soc_dapm_enable_pin_unlocked(dapm
, "AGCIN");
155 snd_soc_dapm_disable_pin_unlocked(dapm
, "AGCIN");
159 snd_soc_dapm_sync_unlocked(dapm
);
161 snd_soc_dapm_mutex_unlock(dapm
);
166 static int ams_delta_get_audio_mode(struct snd_kcontrol
*kcontrol
,
167 struct snd_ctl_elem_value
*ucontrol
)
169 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
170 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
171 unsigned short pins
, mode
;
173 pins
= ((snd_soc_dapm_get_pin_status(dapm
, "Mouthpiece") <<
174 AMS_DELTA_MOUTHPIECE
) |
175 (snd_soc_dapm_get_pin_status(dapm
, "Earpiece") <<
176 AMS_DELTA_EARPIECE
));
178 pins
|= (snd_soc_dapm_get_pin_status(dapm
, "Microphone") <<
179 AMS_DELTA_MICROPHONE
);
181 pins
= ((snd_soc_dapm_get_pin_status(dapm
, "Microphone") <<
182 AMS_DELTA_MICROPHONE
) |
183 (snd_soc_dapm_get_pin_status(dapm
, "Speaker") <<
185 (ams_delta_audio_agc
<< AMS_DELTA_AGC
));
187 for (mode
= 0; mode
< ARRAY_SIZE(ams_delta_audio_mode
); mode
++)
188 if (pins
== ams_delta_audio_mode_pins
[mode
])
191 if (mode
>= ARRAY_SIZE(ams_delta_audio_mode
))
194 ucontrol
->value
.enumerated
.item
[0] = mode
;
199 static const struct soc_enum ams_delta_audio_enum
[] = {
200 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ams_delta_audio_mode
),
201 ams_delta_audio_mode
),
204 static const struct snd_kcontrol_new ams_delta_audio_controls
[] = {
205 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum
[0],
206 ams_delta_get_audio_mode
, ams_delta_set_audio_mode
),
210 static struct snd_soc_jack ams_delta_hook_switch
;
211 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios
[] = {
214 .name
= "hook_switch",
215 .report
= SND_JACK_HEADSET
,
217 .debounce_time
= 150,
221 /* After we are able to control the codec over the modem,
222 * the hook switch can be used for dynamic DAPM reconfiguration. */
223 static struct snd_soc_jack_pin ams_delta_hook_switch_pins
[] = {
227 .mask
= SND_JACK_MICROPHONE
,
231 .mask
= SND_JACK_HEADPHONE
,
236 .mask
= SND_JACK_MICROPHONE
,
241 .mask
= SND_JACK_HEADPHONE
,
248 * Modem line discipline, required for making above controls functional.
249 * Activated from userspace with ldattach, possibly invoked from udev rule.
252 /* To actually apply any modem controlled configuration changes to the codec,
253 * we must connect codec DAI pins to the modem for a moment. Be careful not
254 * to interfere with our digital mute function that shares the same hardware. */
255 static struct timer_list cx81801_timer
;
256 static bool cx81801_cmd_pending
;
257 static bool ams_delta_muted
;
258 static DEFINE_SPINLOCK(ams_delta_lock
);
260 static void cx81801_timeout(unsigned long data
)
264 spin_lock(&ams_delta_lock
);
265 cx81801_cmd_pending
= 0;
266 muted
= ams_delta_muted
;
267 spin_unlock(&ams_delta_lock
);
269 /* Reconnect the codec DAI back from the modem to the CPU DAI
270 * only if digital mute still off */
272 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
, 0);
276 * Used for passing a codec structure pointer
277 * from the board initialization code to the tty line discipline.
279 static struct snd_soc_codec
*cx20442_codec
;
281 /* Line discipline .open() */
282 static int cx81801_open(struct tty_struct
*tty
)
290 * Pass the codec structure pointer for use by other ldisc callbacks,
291 * both the card and the codec specific parts.
293 tty
->disc_data
= cx20442_codec
;
295 ret
= v253_ops
.open(tty
);
298 tty
->disc_data
= NULL
;
303 /* Line discipline .close() */
304 static void cx81801_close(struct tty_struct
*tty
)
306 struct snd_soc_codec
*codec
= tty
->disc_data
;
307 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
309 del_timer_sync(&cx81801_timer
);
311 /* Prevent the hook switch from further changing the DAPM pins */
312 INIT_LIST_HEAD(&ams_delta_hook_switch
.pins
);
319 /* Revert back to default audio input/output constellation */
320 snd_soc_dapm_mutex_lock(dapm
);
322 snd_soc_dapm_disable_pin_unlocked(dapm
, "Mouthpiece");
323 snd_soc_dapm_enable_pin_unlocked(dapm
, "Earpiece");
324 snd_soc_dapm_enable_pin_unlocked(dapm
, "Microphone");
325 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
326 snd_soc_dapm_disable_pin_unlocked(dapm
, "AGCIN");
328 snd_soc_dapm_sync_unlocked(dapm
);
330 snd_soc_dapm_mutex_unlock(codec
);
333 /* Line discipline .hangup() */
334 static int cx81801_hangup(struct tty_struct
*tty
)
340 /* Line discipline .receive_buf() */
341 static void cx81801_receive(struct tty_struct
*tty
,
342 const unsigned char *cp
, char *fp
, int count
)
344 struct snd_soc_codec
*codec
= tty
->disc_data
;
345 const unsigned char *c
;
351 if (!codec
->hw_write
) {
352 /* First modem response, complete setup procedure */
354 /* Initialize timer used for config pulse generation */
355 setup_timer(&cx81801_timer
, cx81801_timeout
, 0);
357 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
359 /* Link hook switch to DAPM pins */
360 ret
= snd_soc_jack_add_pins(&ams_delta_hook_switch
,
361 ARRAY_SIZE(ams_delta_hook_switch_pins
),
362 ams_delta_hook_switch_pins
);
365 "Failed to link hook switch to DAPM pins, "
366 "will continue with hook switch unlinked.\n");
371 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
373 for (c
= &cp
[count
- 1]; c
>= cp
; c
--) {
376 /* Complete modem response received, apply config to codec */
378 spin_lock_bh(&ams_delta_lock
);
379 mod_timer(&cx81801_timer
, jiffies
+ msecs_to_jiffies(150));
380 apply
= !ams_delta_muted
&& !cx81801_cmd_pending
;
381 cx81801_cmd_pending
= 1;
382 spin_unlock_bh(&ams_delta_lock
);
384 /* Apply config pulse by connecting the codec to the modem
385 * if not already done */
387 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
,
388 AMS_DELTA_LATCH2_MODEM_CODEC
);
393 /* Line discipline .write_wakeup() */
394 static void cx81801_wakeup(struct tty_struct
*tty
)
396 v253_ops
.write_wakeup(tty
);
399 static struct tty_ldisc_ops cx81801_ops
= {
400 .magic
= TTY_LDISC_MAGIC
,
402 .owner
= THIS_MODULE
,
403 .open
= cx81801_open
,
404 .close
= cx81801_close
,
405 .hangup
= cx81801_hangup
,
406 .receive_buf
= cx81801_receive
,
407 .write_wakeup
= cx81801_wakeup
,
412 * Even if not very useful, the sound card can still work without any of the
413 * above functonality activated. You can still control its audio input/output
414 * constellation and speakerphone gain from userspace by issuing AT commands
415 * over the modem port.
418 static int ams_delta_hw_params(struct snd_pcm_substream
*substream
,
419 struct snd_pcm_hw_params
*params
)
421 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
423 /* Set cpu DAI configuration */
424 return snd_soc_dai_set_fmt(rtd
->cpu_dai
,
425 SND_SOC_DAIFMT_DSP_A
|
426 SND_SOC_DAIFMT_NB_NF
|
427 SND_SOC_DAIFMT_CBM_CFM
);
430 static struct snd_soc_ops ams_delta_ops
= {
431 .hw_params
= ams_delta_hw_params
,
435 /* Digital mute implemented using modem/CPU multiplexer.
436 * Shares hardware with codec config pulse generation */
437 static bool ams_delta_muted
= 1;
439 static int ams_delta_digital_mute(struct snd_soc_dai
*dai
, int mute
)
443 if (ams_delta_muted
== mute
)
446 spin_lock_bh(&ams_delta_lock
);
447 ams_delta_muted
= mute
;
448 apply
= !cx81801_cmd_pending
;
449 spin_unlock_bh(&ams_delta_lock
);
452 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
,
453 mute
? AMS_DELTA_LATCH2_MODEM_CODEC
: 0);
457 /* Our codec DAI probably doesn't have its own .ops structure */
458 static const struct snd_soc_dai_ops ams_delta_dai_ops
= {
459 .digital_mute
= ams_delta_digital_mute
,
462 /* Will be used if the codec ever has its own digital_mute function */
463 static int ams_delta_startup(struct snd_pcm_substream
*substream
)
465 return ams_delta_digital_mute(NULL
, 0);
468 static void ams_delta_shutdown(struct snd_pcm_substream
*substream
)
470 ams_delta_digital_mute(NULL
, 1);
475 * Card initialization
478 static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime
*rtd
)
480 struct snd_soc_codec
*codec
= rtd
->codec
;
481 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
482 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
483 struct snd_soc_card
*card
= rtd
->card
;
485 /* Codec is ready, now add/activate board specific controls */
487 /* Store a pointer to the codec structure for tty ldisc use */
488 cx20442_codec
= codec
;
490 /* Set up digital mute if not provided by the codec */
491 if (!codec_dai
->driver
->ops
) {
492 codec_dai
->driver
->ops
= &ams_delta_dai_ops
;
494 ams_delta_ops
.startup
= ams_delta_startup
;
495 ams_delta_ops
.shutdown
= ams_delta_shutdown
;
498 /* Add hook switch - can be used to control the codec from userspace
499 * even if line discipline fails */
500 ret
= snd_soc_jack_new(rtd
->codec
, "hook_switch",
501 SND_JACK_HEADSET
, &ams_delta_hook_switch
);
504 "Failed to allocate resources for hook switch, "
505 "will continue without one.\n");
507 ret
= snd_soc_jack_add_gpios(&ams_delta_hook_switch
,
508 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
509 ams_delta_hook_switch_gpios
);
512 "Failed to set up hook switch GPIO line, "
513 "will continue with hook switch inactive.\n");
516 /* Register optional line discipline for over the modem control */
517 ret
= tty_register_ldisc(N_V253
, &cx81801_ops
);
520 "Failed to register line discipline, "
521 "will continue without any controls.\n");
525 /* Add board specific DAPM widgets and routes */
526 ret
= snd_soc_dapm_new_controls(dapm
, ams_delta_dapm_widgets
,
527 ARRAY_SIZE(ams_delta_dapm_widgets
));
530 "Failed to register DAPM controls, "
531 "will continue without any.\n");
535 ret
= snd_soc_dapm_add_routes(dapm
, ams_delta_audio_map
,
536 ARRAY_SIZE(ams_delta_audio_map
));
539 "Failed to set up DAPM routes, "
540 "will continue with codec default map.\n");
544 /* Set up initial pin constellation */
545 snd_soc_dapm_disable_pin(dapm
, "Mouthpiece");
546 snd_soc_dapm_enable_pin(dapm
, "Earpiece");
547 snd_soc_dapm_enable_pin(dapm
, "Microphone");
548 snd_soc_dapm_disable_pin(dapm
, "Speaker");
549 snd_soc_dapm_disable_pin(dapm
, "AGCIN");
550 snd_soc_dapm_disable_pin(dapm
, "AGCOUT");
552 /* Add virtual switch */
553 ret
= snd_soc_add_codec_controls(codec
, ams_delta_audio_controls
,
554 ARRAY_SIZE(ams_delta_audio_controls
));
557 "Failed to register audio mode control, "
558 "will continue without it.\n");
563 /* DAI glue - connects codec <--> CPU */
564 static struct snd_soc_dai_link ams_delta_dai_link
= {
566 .stream_name
= "CX20442",
567 .cpu_dai_name
= "omap-mcbsp.1",
568 .codec_dai_name
= "cx20442-voice",
569 .init
= ams_delta_cx20442_init
,
570 .platform_name
= "omap-pcm-audio",
571 .codec_name
= "cx20442-codec",
572 .ops
= &ams_delta_ops
,
575 /* Audio card driver */
576 static struct snd_soc_card ams_delta_audio_card
= {
578 .owner
= THIS_MODULE
,
579 .dai_link
= &ams_delta_dai_link
,
583 /* Module init/exit */
584 static int ams_delta_probe(struct platform_device
*pdev
)
586 struct snd_soc_card
*card
= &ams_delta_audio_card
;
589 card
->dev
= &pdev
->dev
;
591 ret
= snd_soc_register_card(card
);
593 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n", ret
);
600 static int ams_delta_remove(struct platform_device
*pdev
)
602 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
604 if (tty_unregister_ldisc(N_V253
) != 0)
606 "failed to unregister V253 line discipline\n");
608 snd_soc_jack_free_gpios(&ams_delta_hook_switch
,
609 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
610 ams_delta_hook_switch_gpios
);
612 snd_soc_unregister_card(card
);
617 #define DRV_NAME "ams-delta-audio"
619 static struct platform_driver ams_delta_driver
= {
622 .owner
= THIS_MODULE
,
624 .probe
= ams_delta_probe
,
625 .remove
= ams_delta_remove
,
628 module_platform_driver(ams_delta_driver
);
630 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
631 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
632 MODULE_LICENSE("GPL");
633 MODULE_ALIAS("platform:" DRV_NAME
);