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"
41 /* Board specific DAPM widgets */
42 static const struct snd_soc_dapm_widget ams_delta_dapm_widgets
[] = {
44 SND_SOC_DAPM_MIC("Mouthpiece", NULL
),
45 SND_SOC_DAPM_HP("Earpiece", NULL
),
46 /* Handsfree/Speakerphone */
47 SND_SOC_DAPM_MIC("Microphone", NULL
),
48 SND_SOC_DAPM_SPK("Speaker", NULL
),
51 /* How they are connected to codec pins */
52 static const struct snd_soc_dapm_route ams_delta_audio_map
[] = {
53 {"TELIN", NULL
, "Mouthpiece"},
54 {"Earpiece", NULL
, "TELOUT"},
56 {"MIC", NULL
, "Microphone"},
57 {"Speaker", NULL
, "SPKOUT"},
61 * Controls, functional after the modem line discipline is activated.
64 /* Virtual switch: audio input/output constellations */
65 static const char *ams_delta_audio_mode
[] =
66 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
68 /* Selection <-> pin translation */
69 #define AMS_DELTA_MOUTHPIECE 0
70 #define AMS_DELTA_EARPIECE 1
71 #define AMS_DELTA_MICROPHONE 2
72 #define AMS_DELTA_SPEAKER 3
73 #define AMS_DELTA_AGC 4
75 #define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
76 (1 << AMS_DELTA_MICROPHONE))
77 #define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
78 (1 << AMS_DELTA_EARPIECE))
79 #define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
80 (1 << AMS_DELTA_SPEAKER))
81 #define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
83 static const unsigned short ams_delta_audio_mode_pins
[] = {
87 AMS_DELTA_SPEAKERPHONE
,
90 static unsigned short ams_delta_audio_agc
;
93 * Used for passing a codec structure pointer
94 * from the board initialization code to the tty line discipline.
96 static struct snd_soc_codec
*cx20442_codec
;
98 static int ams_delta_set_audio_mode(struct snd_kcontrol
*kcontrol
,
99 struct snd_ctl_elem_value
*ucontrol
)
101 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
102 struct snd_soc_dapm_context
*dapm
= &card
->dapm
;
103 struct soc_enum
*control
= (struct soc_enum
*)kcontrol
->private_value
;
105 int pin
, changed
= 0;
107 /* Refuse any mode changes if we are not able to control the codec. */
108 if (!cx20442_codec
->hw_write
)
111 if (ucontrol
->value
.enumerated
.item
[0] >= control
->items
)
114 snd_soc_dapm_mutex_lock(dapm
);
116 /* Translate selection to bitmap */
117 pins
= ams_delta_audio_mode_pins
[ucontrol
->value
.enumerated
.item
[0]];
119 /* Setup pins after corresponding bits if changed */
120 pin
= !!(pins
& (1 << AMS_DELTA_MOUTHPIECE
));
122 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Mouthpiece")) {
125 snd_soc_dapm_enable_pin_unlocked(dapm
, "Mouthpiece");
127 snd_soc_dapm_disable_pin_unlocked(dapm
, "Mouthpiece");
129 pin
= !!(pins
& (1 << AMS_DELTA_EARPIECE
));
130 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Earpiece")) {
133 snd_soc_dapm_enable_pin_unlocked(dapm
, "Earpiece");
135 snd_soc_dapm_disable_pin_unlocked(dapm
, "Earpiece");
137 pin
= !!(pins
& (1 << AMS_DELTA_MICROPHONE
));
138 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Microphone")) {
141 snd_soc_dapm_enable_pin_unlocked(dapm
, "Microphone");
143 snd_soc_dapm_disable_pin_unlocked(dapm
, "Microphone");
145 pin
= !!(pins
& (1 << AMS_DELTA_SPEAKER
));
146 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Speaker")) {
149 snd_soc_dapm_enable_pin_unlocked(dapm
, "Speaker");
151 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
153 pin
= !!(pins
& (1 << AMS_DELTA_AGC
));
154 if (pin
!= ams_delta_audio_agc
) {
155 ams_delta_audio_agc
= pin
;
158 snd_soc_dapm_enable_pin_unlocked(dapm
, "AGCIN");
160 snd_soc_dapm_disable_pin_unlocked(dapm
, "AGCIN");
164 snd_soc_dapm_sync_unlocked(dapm
);
166 snd_soc_dapm_mutex_unlock(dapm
);
171 static int ams_delta_get_audio_mode(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
175 struct snd_soc_dapm_context
*dapm
= &card
->dapm
;
176 unsigned short pins
, mode
;
178 pins
= ((snd_soc_dapm_get_pin_status(dapm
, "Mouthpiece") <<
179 AMS_DELTA_MOUTHPIECE
) |
180 (snd_soc_dapm_get_pin_status(dapm
, "Earpiece") <<
181 AMS_DELTA_EARPIECE
));
183 pins
|= (snd_soc_dapm_get_pin_status(dapm
, "Microphone") <<
184 AMS_DELTA_MICROPHONE
);
186 pins
= ((snd_soc_dapm_get_pin_status(dapm
, "Microphone") <<
187 AMS_DELTA_MICROPHONE
) |
188 (snd_soc_dapm_get_pin_status(dapm
, "Speaker") <<
190 (ams_delta_audio_agc
<< AMS_DELTA_AGC
));
192 for (mode
= 0; mode
< ARRAY_SIZE(ams_delta_audio_mode
); mode
++)
193 if (pins
== ams_delta_audio_mode_pins
[mode
])
196 if (mode
>= ARRAY_SIZE(ams_delta_audio_mode
))
199 ucontrol
->value
.enumerated
.item
[0] = mode
;
204 static const SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum
,
205 ams_delta_audio_mode
);
207 static const struct snd_kcontrol_new ams_delta_audio_controls
[] = {
208 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum
,
209 ams_delta_get_audio_mode
, ams_delta_set_audio_mode
),
213 static struct snd_soc_jack ams_delta_hook_switch
;
214 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios
[] = {
217 .name
= "hook_switch",
218 .report
= SND_JACK_HEADSET
,
220 .debounce_time
= 150,
224 /* After we are able to control the codec over the modem,
225 * the hook switch can be used for dynamic DAPM reconfiguration. */
226 static struct snd_soc_jack_pin ams_delta_hook_switch_pins
[] = {
230 .mask
= SND_JACK_MICROPHONE
,
234 .mask
= SND_JACK_HEADPHONE
,
239 .mask
= SND_JACK_MICROPHONE
,
244 .mask
= SND_JACK_HEADPHONE
,
251 * Modem line discipline, required for making above controls functional.
252 * Activated from userspace with ldattach, possibly invoked from udev rule.
255 /* To actually apply any modem controlled configuration changes to the codec,
256 * we must connect codec DAI pins to the modem for a moment. Be careful not
257 * to interfere with our digital mute function that shares the same hardware. */
258 static struct timer_list cx81801_timer
;
259 static bool cx81801_cmd_pending
;
260 static bool ams_delta_muted
;
261 static DEFINE_SPINLOCK(ams_delta_lock
);
263 static void cx81801_timeout(unsigned long data
)
267 spin_lock(&ams_delta_lock
);
268 cx81801_cmd_pending
= 0;
269 muted
= ams_delta_muted
;
270 spin_unlock(&ams_delta_lock
);
272 /* Reconnect the codec DAI back from the modem to the CPU DAI
273 * only if digital mute still off */
275 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
, 0);
278 /* Line discipline .open() */
279 static int cx81801_open(struct tty_struct
*tty
)
287 * Pass the codec structure pointer for use by other ldisc callbacks,
288 * both the card and the codec specific parts.
290 tty
->disc_data
= cx20442_codec
;
292 ret
= v253_ops
.open(tty
);
295 tty
->disc_data
= NULL
;
300 /* Line discipline .close() */
301 static void cx81801_close(struct tty_struct
*tty
)
303 struct snd_soc_codec
*codec
= tty
->disc_data
;
304 struct snd_soc_dapm_context
*dapm
= &codec
->component
.card
->dapm
;
306 del_timer_sync(&cx81801_timer
);
308 /* Prevent the hook switch from further changing the DAPM pins */
309 INIT_LIST_HEAD(&ams_delta_hook_switch
.pins
);
316 /* Revert back to default audio input/output constellation */
317 snd_soc_dapm_mutex_lock(dapm
);
319 snd_soc_dapm_disable_pin_unlocked(dapm
, "Mouthpiece");
320 snd_soc_dapm_enable_pin_unlocked(dapm
, "Earpiece");
321 snd_soc_dapm_enable_pin_unlocked(dapm
, "Microphone");
322 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
323 snd_soc_dapm_disable_pin_unlocked(dapm
, "AGCIN");
325 snd_soc_dapm_sync_unlocked(dapm
);
327 snd_soc_dapm_mutex_unlock(dapm
);
330 /* Line discipline .hangup() */
331 static int cx81801_hangup(struct tty_struct
*tty
)
337 /* Line discipline .receive_buf() */
338 static void cx81801_receive(struct tty_struct
*tty
,
339 const unsigned char *cp
, char *fp
, int count
)
341 struct snd_soc_codec
*codec
= tty
->disc_data
;
342 const unsigned char *c
;
348 if (!codec
->hw_write
) {
349 /* First modem response, complete setup procedure */
351 /* Initialize timer used for config pulse generation */
352 setup_timer(&cx81801_timer
, cx81801_timeout
, 0);
354 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
356 /* Link hook switch to DAPM pins */
357 ret
= snd_soc_jack_add_pins(&ams_delta_hook_switch
,
358 ARRAY_SIZE(ams_delta_hook_switch_pins
),
359 ams_delta_hook_switch_pins
);
362 "Failed to link hook switch to DAPM pins, "
363 "will continue with hook switch unlinked.\n");
368 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
370 for (c
= &cp
[count
- 1]; c
>= cp
; c
--) {
373 /* Complete modem response received, apply config to codec */
375 spin_lock_bh(&ams_delta_lock
);
376 mod_timer(&cx81801_timer
, jiffies
+ msecs_to_jiffies(150));
377 apply
= !ams_delta_muted
&& !cx81801_cmd_pending
;
378 cx81801_cmd_pending
= 1;
379 spin_unlock_bh(&ams_delta_lock
);
381 /* Apply config pulse by connecting the codec to the modem
382 * if not already done */
384 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
,
385 AMS_DELTA_LATCH2_MODEM_CODEC
);
390 /* Line discipline .write_wakeup() */
391 static void cx81801_wakeup(struct tty_struct
*tty
)
393 v253_ops
.write_wakeup(tty
);
396 static struct tty_ldisc_ops cx81801_ops
= {
397 .magic
= TTY_LDISC_MAGIC
,
399 .owner
= THIS_MODULE
,
400 .open
= cx81801_open
,
401 .close
= cx81801_close
,
402 .hangup
= cx81801_hangup
,
403 .receive_buf
= cx81801_receive
,
404 .write_wakeup
= cx81801_wakeup
,
409 * Even if not very useful, the sound card can still work without any of the
410 * above functonality activated. You can still control its audio input/output
411 * constellation and speakerphone gain from userspace by issuing AT commands
412 * over the modem port.
415 static struct snd_soc_ops ams_delta_ops
;
418 /* Digital mute implemented using modem/CPU multiplexer.
419 * Shares hardware with codec config pulse generation */
420 static bool ams_delta_muted
= 1;
422 static int ams_delta_digital_mute(struct snd_soc_dai
*dai
, int mute
)
426 if (ams_delta_muted
== mute
)
429 spin_lock_bh(&ams_delta_lock
);
430 ams_delta_muted
= mute
;
431 apply
= !cx81801_cmd_pending
;
432 spin_unlock_bh(&ams_delta_lock
);
435 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
,
436 mute
? AMS_DELTA_LATCH2_MODEM_CODEC
: 0);
440 /* Our codec DAI probably doesn't have its own .ops structure */
441 static const struct snd_soc_dai_ops ams_delta_dai_ops
= {
442 .digital_mute
= ams_delta_digital_mute
,
445 /* Will be used if the codec ever has its own digital_mute function */
446 static int ams_delta_startup(struct snd_pcm_substream
*substream
)
448 return ams_delta_digital_mute(NULL
, 0);
451 static void ams_delta_shutdown(struct snd_pcm_substream
*substream
)
453 ams_delta_digital_mute(NULL
, 1);
458 * Card initialization
461 static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime
*rtd
)
463 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
464 struct snd_soc_card
*card
= rtd
->card
;
465 struct snd_soc_dapm_context
*dapm
= &card
->dapm
;
467 /* Codec is ready, now add/activate board specific controls */
469 /* Store a pointer to the codec structure for tty ldisc use */
470 cx20442_codec
= rtd
->codec
;
472 /* Set up digital mute if not provided by the codec */
473 if (!codec_dai
->driver
->ops
) {
474 codec_dai
->driver
->ops
= &ams_delta_dai_ops
;
476 ams_delta_ops
.startup
= ams_delta_startup
;
477 ams_delta_ops
.shutdown
= ams_delta_shutdown
;
480 /* Add hook switch - can be used to control the codec from userspace
481 * even if line discipline fails */
482 ret
= snd_soc_card_jack_new(card
, "hook_switch", SND_JACK_HEADSET
,
483 &ams_delta_hook_switch
, NULL
, 0);
486 "Failed to allocate resources for hook switch, "
487 "will continue without one.\n");
489 ret
= snd_soc_jack_add_gpios(&ams_delta_hook_switch
,
490 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
491 ams_delta_hook_switch_gpios
);
494 "Failed to set up hook switch GPIO line, "
495 "will continue with hook switch inactive.\n");
498 /* Register optional line discipline for over the modem control */
499 ret
= tty_register_ldisc(N_V253
, &cx81801_ops
);
502 "Failed to register line discipline, "
503 "will continue without any controls.\n");
507 /* Set up initial pin constellation */
508 snd_soc_dapm_disable_pin(dapm
, "Mouthpiece");
509 snd_soc_dapm_disable_pin(dapm
, "Speaker");
510 snd_soc_dapm_disable_pin(dapm
, "AGCIN");
511 snd_soc_dapm_disable_pin(dapm
, "AGCOUT");
516 static int ams_delta_card_remove(struct snd_soc_card
*card
)
518 snd_soc_jack_free_gpios(&ams_delta_hook_switch
,
519 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
520 ams_delta_hook_switch_gpios
);
525 /* DAI glue - connects codec <--> CPU */
526 static struct snd_soc_dai_link ams_delta_dai_link
= {
528 .stream_name
= "CX20442",
529 .cpu_dai_name
= "omap-mcbsp.1",
530 .codec_dai_name
= "cx20442-voice",
531 .init
= ams_delta_cx20442_init
,
532 .platform_name
= "omap-mcbsp.1",
533 .codec_name
= "cx20442-codec",
534 .ops
= &ams_delta_ops
,
535 .dai_fmt
= SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_NB_NF
|
536 SND_SOC_DAIFMT_CBM_CFM
,
539 /* Audio card driver */
540 static struct snd_soc_card ams_delta_audio_card
= {
542 .owner
= THIS_MODULE
,
543 .remove
= ams_delta_card_remove
,
544 .dai_link
= &ams_delta_dai_link
,
547 .controls
= ams_delta_audio_controls
,
548 .num_controls
= ARRAY_SIZE(ams_delta_audio_controls
),
549 .dapm_widgets
= ams_delta_dapm_widgets
,
550 .num_dapm_widgets
= ARRAY_SIZE(ams_delta_dapm_widgets
),
551 .dapm_routes
= ams_delta_audio_map
,
552 .num_dapm_routes
= ARRAY_SIZE(ams_delta_audio_map
),
555 /* Module init/exit */
556 static int ams_delta_probe(struct platform_device
*pdev
)
558 struct snd_soc_card
*card
= &ams_delta_audio_card
;
561 card
->dev
= &pdev
->dev
;
563 ret
= snd_soc_register_card(card
);
565 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n", ret
);
572 static int ams_delta_remove(struct platform_device
*pdev
)
574 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
576 if (tty_unregister_ldisc(N_V253
) != 0)
578 "failed to unregister V253 line discipline\n");
580 snd_soc_unregister_card(card
);
585 #define DRV_NAME "ams-delta-audio"
587 static struct platform_driver ams_delta_driver
= {
591 .probe
= ams_delta_probe
,
592 .remove
= ams_delta_remove
,
595 module_platform_driver(ams_delta_driver
);
597 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
598 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
599 MODULE_LICENSE("GPL");
600 MODULE_ALIAS("platform:" DRV_NAME
);