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
->control_data
)
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);
271 /* Line discipline .open() */
272 static int cx81801_open(struct tty_struct
*tty
)
274 return v253_ops
.open(tty
);
277 /* Line discipline .close() */
278 static void cx81801_close(struct tty_struct
*tty
)
280 struct snd_soc_codec
*codec
= tty
->disc_data
;
282 del_timer_sync(&cx81801_timer
);
286 /* Prevent the hook switch from further changing the DAPM pins */
287 INIT_LIST_HEAD(&ams_delta_hook_switch
.pins
);
289 /* Revert back to default audio input/output constellation */
290 snd_soc_dapm_disable_pin(codec
, "Mouthpiece");
291 snd_soc_dapm_enable_pin(codec
, "Earpiece");
292 snd_soc_dapm_enable_pin(codec
, "Microphone");
293 snd_soc_dapm_disable_pin(codec
, "Speaker");
294 snd_soc_dapm_disable_pin(codec
, "AGCIN");
295 snd_soc_dapm_sync(codec
);
298 /* Line discipline .hangup() */
299 static int cx81801_hangup(struct tty_struct
*tty
)
305 /* Line discipline .recieve_buf() */
306 static void cx81801_receive(struct tty_struct
*tty
,
307 const unsigned char *cp
, char *fp
, int count
)
309 struct snd_soc_codec
*codec
= tty
->disc_data
;
310 const unsigned char *c
;
313 if (!codec
->control_data
) {
314 /* First modem response, complete setup procedure */
316 /* Initialize timer used for config pulse generation */
317 setup_timer(&cx81801_timer
, cx81801_timeout
, 0);
319 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
321 /* Link hook switch to DAPM pins */
322 ret
= snd_soc_jack_add_pins(&ams_delta_hook_switch
,
323 ARRAY_SIZE(ams_delta_hook_switch_pins
),
324 ams_delta_hook_switch_pins
);
326 dev_warn(codec
->socdev
->card
->dev
,
327 "Failed to link hook switch to DAPM pins, "
328 "will continue with hook switch unlinked.\n");
333 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
335 for (c
= &cp
[count
- 1]; c
>= cp
; c
--) {
338 /* Complete modem response received, apply config to codec */
340 spin_lock_bh(&ams_delta_lock
);
341 mod_timer(&cx81801_timer
, jiffies
+ msecs_to_jiffies(150));
342 apply
= !ams_delta_muted
&& !cx81801_cmd_pending
;
343 cx81801_cmd_pending
= 1;
344 spin_unlock_bh(&ams_delta_lock
);
346 /* Apply config pulse by connecting the codec to the modem
347 * if not already done */
349 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
,
350 AMS_DELTA_LATCH2_MODEM_CODEC
);
355 /* Line discipline .write_wakeup() */
356 static void cx81801_wakeup(struct tty_struct
*tty
)
358 v253_ops
.write_wakeup(tty
);
361 static struct tty_ldisc_ops cx81801_ops
= {
362 .magic
= TTY_LDISC_MAGIC
,
364 .owner
= THIS_MODULE
,
365 .open
= cx81801_open
,
366 .close
= cx81801_close
,
367 .hangup
= cx81801_hangup
,
368 .receive_buf
= cx81801_receive
,
369 .write_wakeup
= cx81801_wakeup
,
374 * Even if not very usefull, the sound card can still work without any of the
375 * above functonality activated. You can still control its audio input/output
376 * constellation and speakerphone gain from userspace by issueing AT commands
377 * over the modem port.
380 static int ams_delta_hw_params(struct snd_pcm_substream
*substream
,
381 struct snd_pcm_hw_params
*params
)
383 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
385 /* Set cpu DAI configuration */
386 return snd_soc_dai_set_fmt(rtd
->dai
->cpu_dai
,
387 SND_SOC_DAIFMT_DSP_A
|
388 SND_SOC_DAIFMT_NB_NF
|
389 SND_SOC_DAIFMT_CBM_CFM
);
392 static struct snd_soc_ops ams_delta_ops
= {
393 .hw_params
= ams_delta_hw_params
,
397 /* Board specific codec bias level control */
398 static int ams_delta_set_bias_level(struct snd_soc_card
*card
,
399 enum snd_soc_bias_level level
)
401 struct snd_soc_codec
*codec
= card
->codec
;
404 case SND_SOC_BIAS_ON
:
405 case SND_SOC_BIAS_PREPARE
:
406 case SND_SOC_BIAS_STANDBY
:
407 if (codec
->bias_level
== SND_SOC_BIAS_OFF
)
408 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET
,
409 AMS_DELTA_LATCH2_MODEM_NRESET
);
411 case SND_SOC_BIAS_OFF
:
412 if (codec
->bias_level
!= SND_SOC_BIAS_OFF
)
413 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET
,
416 codec
->bias_level
= level
;
421 /* Digital mute implemented using modem/CPU multiplexer.
422 * Shares hardware with codec config pulse generation */
423 static bool ams_delta_muted
= 1;
425 static int ams_delta_digital_mute(struct snd_soc_dai
*dai
, int mute
)
429 if (ams_delta_muted
== mute
)
432 spin_lock_bh(&ams_delta_lock
);
433 ams_delta_muted
= mute
;
434 apply
= !cx81801_cmd_pending
;
435 spin_unlock_bh(&ams_delta_lock
);
438 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC
,
439 mute
? AMS_DELTA_LATCH2_MODEM_CODEC
: 0);
443 /* Our codec DAI probably doesn't have its own .ops structure */
444 static struct snd_soc_dai_ops ams_delta_dai_ops
= {
445 .digital_mute
= ams_delta_digital_mute
,
448 /* Will be used if the codec ever has its own digital_mute function */
449 static int ams_delta_startup(struct snd_pcm_substream
*substream
)
451 return ams_delta_digital_mute(NULL
, 0);
454 static void ams_delta_shutdown(struct snd_pcm_substream
*substream
)
456 ams_delta_digital_mute(NULL
, 1);
461 * Card initialization
464 static int ams_delta_cx20442_init(struct snd_soc_codec
*codec
)
466 struct snd_soc_dai
*codec_dai
= codec
->dai
;
467 struct snd_soc_card
*card
= codec
->socdev
->card
;
469 /* Codec is ready, now add/activate board specific controls */
471 /* Set up digital mute if not provided by the codec */
472 if (!codec_dai
->ops
) {
473 codec_dai
->ops
= &ams_delta_dai_ops
;
474 } else if (!codec_dai
->ops
->digital_mute
) {
475 codec_dai
->ops
->digital_mute
= ams_delta_digital_mute
;
477 ams_delta_ops
.startup
= ams_delta_startup
;
478 ams_delta_ops
.shutdown
= ams_delta_shutdown
;
481 /* Set codec bias level */
482 ams_delta_set_bias_level(card
, SND_SOC_BIAS_STANDBY
);
484 /* Add hook switch - can be used to control the codec from userspace
485 * even if line discipline fails */
486 ret
= snd_soc_jack_new(card
, "hook_switch",
487 SND_JACK_HEADSET
, &ams_delta_hook_switch
);
490 "Failed to allocate resources for hook switch, "
491 "will continue without one.\n");
493 ret
= snd_soc_jack_add_gpios(&ams_delta_hook_switch
,
494 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
495 ams_delta_hook_switch_gpios
);
498 "Failed to set up hook switch GPIO line, "
499 "will continue with hook switch inactive.\n");
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 /* Add board specific DAPM widgets and routes */
512 ret
= snd_soc_dapm_new_controls(codec
, ams_delta_dapm_widgets
,
513 ARRAY_SIZE(ams_delta_dapm_widgets
));
516 "Failed to register DAPM controls, "
517 "will continue without any.\n");
521 ret
= snd_soc_dapm_add_routes(codec
, ams_delta_audio_map
,
522 ARRAY_SIZE(ams_delta_audio_map
));
525 "Failed to set up DAPM routes, "
526 "will continue with codec default map.\n");
530 /* Set up initial pin constellation */
531 snd_soc_dapm_disable_pin(codec
, "Mouthpiece");
532 snd_soc_dapm_enable_pin(codec
, "Earpiece");
533 snd_soc_dapm_enable_pin(codec
, "Microphone");
534 snd_soc_dapm_disable_pin(codec
, "Speaker");
535 snd_soc_dapm_disable_pin(codec
, "AGCIN");
536 snd_soc_dapm_disable_pin(codec
, "AGCOUT");
537 snd_soc_dapm_sync(codec
);
539 /* Add virtual switch */
540 ret
= snd_soc_add_controls(codec
, ams_delta_audio_controls
,
541 ARRAY_SIZE(ams_delta_audio_controls
));
544 "Failed to register audio mode control, "
545 "will continue without it.\n");
550 /* DAI glue - connects codec <--> CPU */
551 static struct snd_soc_dai_link ams_delta_dai_link
= {
553 .stream_name
= "CX20442",
554 .cpu_dai
= &omap_mcbsp_dai
[0],
555 .codec_dai
= &cx20442_dai
,
556 .init
= ams_delta_cx20442_init
,
557 .ops
= &ams_delta_ops
,
560 /* Audio card driver */
561 static struct snd_soc_card ams_delta_audio_card
= {
563 .platform
= &omap_soc_platform
,
564 .dai_link
= &ams_delta_dai_link
,
566 .set_bias_level
= ams_delta_set_bias_level
,
569 /* Audio subsystem */
570 static struct snd_soc_device ams_delta_snd_soc_device
= {
571 .card
= &ams_delta_audio_card
,
572 .codec_dev
= &cx20442_codec_dev
,
575 /* Module init/exit */
576 static struct platform_device
*ams_delta_audio_platform_device
;
577 static struct platform_device
*cx20442_platform_device
;
579 static int __init
ams_delta_module_init(void)
583 if (!(machine_is_ams_delta()))
586 ams_delta_audio_platform_device
=
587 platform_device_alloc("soc-audio", -1);
588 if (!ams_delta_audio_platform_device
)
591 platform_set_drvdata(ams_delta_audio_platform_device
,
592 &ams_delta_snd_soc_device
);
593 ams_delta_snd_soc_device
.dev
= &ams_delta_audio_platform_device
->dev
;
594 *(unsigned int *)ams_delta_dai_link
.cpu_dai
->private_data
= OMAP_MCBSP1
;
596 ret
= platform_device_add(ams_delta_audio_platform_device
);
601 * Codec platform device could be registered from elsewhere (board?),
602 * but I do it here as it makes sense only if used with the card.
604 cx20442_platform_device
= platform_device_register_simple("cx20442",
608 platform_device_put(ams_delta_audio_platform_device
);
611 module_init(ams_delta_module_init
);
613 static void __exit
ams_delta_module_exit(void)
615 struct snd_soc_codec
*codec
;
616 struct tty_struct
*tty
;
618 if (ams_delta_audio_card
.codec
) {
619 codec
= ams_delta_audio_card
.codec
;
621 if (codec
->control_data
) {
622 tty
= codec
->control_data
;
628 if (tty_unregister_ldisc(N_V253
) != 0)
629 dev_warn(&ams_delta_audio_platform_device
->dev
,
630 "failed to unregister V253 line discipline\n");
632 snd_soc_jack_free_gpios(&ams_delta_hook_switch
,
633 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
634 ams_delta_hook_switch_gpios
);
636 /* Keep modem power on */
637 ams_delta_set_bias_level(&ams_delta_audio_card
, SND_SOC_BIAS_STANDBY
);
639 platform_device_unregister(cx20442_platform_device
);
640 platform_device_unregister(ams_delta_audio_platform_device
);
642 module_exit(ams_delta_module_exit
);
644 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
645 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
646 MODULE_LICENSE("GPL");