1 // SPDX-License-Identifier: GPL-2.0-only
3 * ams-delta.c -- SoC audio for Amstrad E3 (Delta) videophone
5 * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
7 * Initially based on sound/soc/omap/osk5912.x
8 * Copyright (C) 2008 Mistral Solutions
11 #include <linux/gpio/consumer.h>
12 #include <linux/spinlock.h>
13 #include <linux/tty.h>
14 #include <linux/module.h>
16 #include <sound/soc.h>
17 #include <sound/jack.h>
19 #include <asm/mach-types.h>
21 #include <linux/platform_data/asoc-ti-mcbsp.h>
23 #include "omap-mcbsp.h"
24 #include "../codecs/cx20442.h"
26 static struct gpio_desc
*handset_mute
;
27 static struct gpio_desc
*handsfree_mute
;
29 static int ams_delta_event_handset(struct snd_soc_dapm_widget
*w
,
30 struct snd_kcontrol
*k
, int event
)
32 gpiod_set_value_cansleep(handset_mute
, !SND_SOC_DAPM_EVENT_ON(event
));
36 static int ams_delta_event_handsfree(struct snd_soc_dapm_widget
*w
,
37 struct snd_kcontrol
*k
, int event
)
39 gpiod_set_value_cansleep(handsfree_mute
, !SND_SOC_DAPM_EVENT_ON(event
));
43 /* Board specific DAPM widgets */
44 static const struct snd_soc_dapm_widget ams_delta_dapm_widgets
[] = {
46 SND_SOC_DAPM_MIC("Mouthpiece", NULL
),
47 SND_SOC_DAPM_HP("Earpiece", ams_delta_event_handset
),
48 /* Handsfree/Speakerphone */
49 SND_SOC_DAPM_MIC("Microphone", NULL
),
50 SND_SOC_DAPM_SPK("Speaker", ams_delta_event_handsfree
),
53 /* How they are connected to codec pins */
54 static const struct snd_soc_dapm_route ams_delta_audio_map
[] = {
55 {"TELIN", NULL
, "Mouthpiece"},
56 {"Earpiece", NULL
, "TELOUT"},
58 {"MIC", NULL
, "Microphone"},
59 {"Speaker", NULL
, "SPKOUT"},
63 * Controls, functional after the modem line discipline is activated.
66 /* Virtual switch: audio input/output constellations */
67 static const char *ams_delta_audio_mode
[] =
68 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
70 /* Selection <-> pin translation */
71 #define AMS_DELTA_MOUTHPIECE 0
72 #define AMS_DELTA_EARPIECE 1
73 #define AMS_DELTA_MICROPHONE 2
74 #define AMS_DELTA_SPEAKER 3
75 #define AMS_DELTA_AGC 4
77 #define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
78 (1 << AMS_DELTA_MICROPHONE))
79 #define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
80 (1 << AMS_DELTA_EARPIECE))
81 #define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
82 (1 << AMS_DELTA_SPEAKER))
83 #define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
85 static const unsigned short ams_delta_audio_mode_pins
[] = {
89 AMS_DELTA_SPEAKERPHONE
,
92 static unsigned short ams_delta_audio_agc
;
95 * Used for passing a codec structure pointer
96 * from the board initialization code to the tty line discipline.
98 static struct snd_soc_component
*cx20442_codec
;
100 static int ams_delta_set_audio_mode(struct snd_kcontrol
*kcontrol
,
101 struct snd_ctl_elem_value
*ucontrol
)
103 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
104 struct snd_soc_dapm_context
*dapm
= &card
->dapm
;
105 struct soc_enum
*control
= (struct soc_enum
*)kcontrol
->private_value
;
107 int pin
, changed
= 0;
109 /* Refuse any mode changes if we are not able to control the codec. */
110 if (!cx20442_codec
->card
->pop_time
)
113 if (ucontrol
->value
.enumerated
.item
[0] >= control
->items
)
116 snd_soc_dapm_mutex_lock(dapm
);
118 /* Translate selection to bitmap */
119 pins
= ams_delta_audio_mode_pins
[ucontrol
->value
.enumerated
.item
[0]];
121 /* Setup pins after corresponding bits if changed */
122 pin
= !!(pins
& (1 << AMS_DELTA_MOUTHPIECE
));
124 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Mouthpiece")) {
127 snd_soc_dapm_enable_pin_unlocked(dapm
, "Mouthpiece");
129 snd_soc_dapm_disable_pin_unlocked(dapm
, "Mouthpiece");
131 pin
= !!(pins
& (1 << AMS_DELTA_EARPIECE
));
132 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Earpiece")) {
135 snd_soc_dapm_enable_pin_unlocked(dapm
, "Earpiece");
137 snd_soc_dapm_disable_pin_unlocked(dapm
, "Earpiece");
139 pin
= !!(pins
& (1 << AMS_DELTA_MICROPHONE
));
140 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Microphone")) {
143 snd_soc_dapm_enable_pin_unlocked(dapm
, "Microphone");
145 snd_soc_dapm_disable_pin_unlocked(dapm
, "Microphone");
147 pin
= !!(pins
& (1 << AMS_DELTA_SPEAKER
));
148 if (pin
!= snd_soc_dapm_get_pin_status(dapm
, "Speaker")) {
151 snd_soc_dapm_enable_pin_unlocked(dapm
, "Speaker");
153 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
155 pin
= !!(pins
& (1 << AMS_DELTA_AGC
));
156 if (pin
!= ams_delta_audio_agc
) {
157 ams_delta_audio_agc
= pin
;
160 snd_soc_dapm_enable_pin_unlocked(dapm
, "AGCIN");
162 snd_soc_dapm_disable_pin_unlocked(dapm
, "AGCIN");
166 snd_soc_dapm_sync_unlocked(dapm
);
168 snd_soc_dapm_mutex_unlock(dapm
);
173 static int ams_delta_get_audio_mode(struct snd_kcontrol
*kcontrol
,
174 struct snd_ctl_elem_value
*ucontrol
)
176 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
177 struct snd_soc_dapm_context
*dapm
= &card
->dapm
;
178 unsigned short pins
, mode
;
180 pins
= ((snd_soc_dapm_get_pin_status(dapm
, "Mouthpiece") <<
181 AMS_DELTA_MOUTHPIECE
) |
182 (snd_soc_dapm_get_pin_status(dapm
, "Earpiece") <<
183 AMS_DELTA_EARPIECE
));
185 pins
|= (snd_soc_dapm_get_pin_status(dapm
, "Microphone") <<
186 AMS_DELTA_MICROPHONE
);
188 pins
= ((snd_soc_dapm_get_pin_status(dapm
, "Microphone") <<
189 AMS_DELTA_MICROPHONE
) |
190 (snd_soc_dapm_get_pin_status(dapm
, "Speaker") <<
192 (ams_delta_audio_agc
<< AMS_DELTA_AGC
));
194 for (mode
= 0; mode
< ARRAY_SIZE(ams_delta_audio_mode
); mode
++)
195 if (pins
== ams_delta_audio_mode_pins
[mode
])
198 if (mode
>= ARRAY_SIZE(ams_delta_audio_mode
))
201 ucontrol
->value
.enumerated
.item
[0] = mode
;
206 static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum
,
207 ams_delta_audio_mode
);
209 static const struct snd_kcontrol_new ams_delta_audio_controls
[] = {
210 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum
,
211 ams_delta_get_audio_mode
, ams_delta_set_audio_mode
),
215 static struct snd_soc_jack ams_delta_hook_switch
;
216 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios
[] = {
218 .name
= "hook_switch",
219 .report
= SND_JACK_HEADSET
,
221 .debounce_time
= 150,
225 /* After we are able to control the codec over the modem,
226 * the hook switch can be used for dynamic DAPM reconfiguration. */
227 static struct snd_soc_jack_pin ams_delta_hook_switch_pins
[] = {
231 .mask
= SND_JACK_MICROPHONE
,
235 .mask
= SND_JACK_HEADPHONE
,
240 .mask
= SND_JACK_MICROPHONE
,
245 .mask
= SND_JACK_HEADPHONE
,
252 * Modem line discipline, required for making above controls functional.
253 * Activated from userspace with ldattach, possibly invoked from udev rule.
256 /* To actually apply any modem controlled configuration changes to the codec,
257 * we must connect codec DAI pins to the modem for a moment. Be careful not
258 * to interfere with our digital mute function that shares the same hardware. */
259 static struct timer_list cx81801_timer
;
260 static bool cx81801_cmd_pending
;
261 static bool ams_delta_muted
;
262 static DEFINE_SPINLOCK(ams_delta_lock
);
263 static struct gpio_desc
*gpiod_modem_codec
;
265 static void cx81801_timeout(struct timer_list
*unused
)
269 spin_lock(&ams_delta_lock
);
270 cx81801_cmd_pending
= 0;
271 muted
= ams_delta_muted
;
272 spin_unlock(&ams_delta_lock
);
274 /* Reconnect the codec DAI back from the modem to the CPU DAI
275 * only if digital mute still off */
277 gpiod_set_value(gpiod_modem_codec
, 0);
280 /* Line discipline .open() */
281 static int cx81801_open(struct tty_struct
*tty
)
289 * Pass the codec structure pointer for use by other ldisc callbacks,
290 * both the card and the codec specific parts.
292 tty
->disc_data
= cx20442_codec
;
294 ret
= v253_ops
.open(tty
);
297 tty
->disc_data
= NULL
;
302 /* Line discipline .close() */
303 static void cx81801_close(struct tty_struct
*tty
)
305 struct snd_soc_component
*component
= tty
->disc_data
;
306 struct snd_soc_dapm_context
*dapm
= &component
->card
->dapm
;
308 del_timer_sync(&cx81801_timer
);
310 /* Prevent the hook switch from further changing the DAPM pins */
311 INIT_LIST_HEAD(&ams_delta_hook_switch
.pins
);
318 /* Revert back to default audio input/output constellation */
319 snd_soc_dapm_mutex_lock(dapm
);
321 snd_soc_dapm_disable_pin_unlocked(dapm
, "Mouthpiece");
322 snd_soc_dapm_enable_pin_unlocked(dapm
, "Earpiece");
323 snd_soc_dapm_enable_pin_unlocked(dapm
, "Microphone");
324 snd_soc_dapm_disable_pin_unlocked(dapm
, "Speaker");
325 snd_soc_dapm_disable_pin_unlocked(dapm
, "AGCIN");
327 snd_soc_dapm_sync_unlocked(dapm
);
329 snd_soc_dapm_mutex_unlock(dapm
);
332 /* Line discipline .hangup() */
333 static int cx81801_hangup(struct tty_struct
*tty
)
339 /* Line discipline .receive_buf() */
340 static void cx81801_receive(struct tty_struct
*tty
,
341 const unsigned char *cp
, char *fp
, int count
)
343 struct snd_soc_component
*component
= tty
->disc_data
;
344 const unsigned char *c
;
350 if (!component
->card
->pop_time
) {
351 /* First modem response, complete setup procedure */
353 /* Initialize timer used for config pulse generation */
354 timer_setup(&cx81801_timer
, cx81801_timeout
, 0);
356 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
358 /* Link hook switch to DAPM pins */
359 ret
= snd_soc_jack_add_pins(&ams_delta_hook_switch
,
360 ARRAY_SIZE(ams_delta_hook_switch_pins
),
361 ams_delta_hook_switch_pins
);
363 dev_warn(component
->dev
,
364 "Failed to link hook switch to DAPM pins, "
365 "will continue with hook switch unlinked.\n");
370 v253_ops
.receive_buf(tty
, cp
, fp
, count
);
372 for (c
= &cp
[count
- 1]; c
>= cp
; c
--) {
375 /* Complete modem response received, apply config to codec */
377 spin_lock_bh(&ams_delta_lock
);
378 mod_timer(&cx81801_timer
, jiffies
+ msecs_to_jiffies(150));
379 apply
= !ams_delta_muted
&& !cx81801_cmd_pending
;
380 cx81801_cmd_pending
= 1;
381 spin_unlock_bh(&ams_delta_lock
);
383 /* Apply config pulse by connecting the codec to the modem
384 * if not already done */
386 gpiod_set_value(gpiod_modem_codec
, 1);
391 /* Line discipline .write_wakeup() */
392 static void cx81801_wakeup(struct tty_struct
*tty
)
394 v253_ops
.write_wakeup(tty
);
397 static struct tty_ldisc_ops cx81801_ops
= {
398 .magic
= TTY_LDISC_MAGIC
,
400 .owner
= THIS_MODULE
,
401 .open
= cx81801_open
,
402 .close
= cx81801_close
,
403 .hangup
= cx81801_hangup
,
404 .receive_buf
= cx81801_receive
,
405 .write_wakeup
= cx81801_wakeup
,
410 * Even if not very useful, the sound card can still work without any of the
411 * above functonality activated. You can still control its audio input/output
412 * constellation and speakerphone gain from userspace by issuing AT commands
413 * over the modem port.
416 static struct snd_soc_ops ams_delta_ops
;
419 /* Digital mute implemented using modem/CPU multiplexer.
420 * Shares hardware with codec config pulse generation */
421 static bool ams_delta_muted
= 1;
423 static int ams_delta_digital_mute(struct snd_soc_dai
*dai
, int mute
)
427 if (ams_delta_muted
== mute
)
430 spin_lock_bh(&ams_delta_lock
);
431 ams_delta_muted
= mute
;
432 apply
= !cx81801_cmd_pending
;
433 spin_unlock_bh(&ams_delta_lock
);
436 gpiod_set_value(gpiod_modem_codec
, !!mute
);
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_dai
->component
;
472 /* Add hook switch - can be used to control the codec from userspace
473 * even if line discipline fails */
474 ret
= snd_soc_card_jack_new(card
, "hook_switch", SND_JACK_HEADSET
,
475 &ams_delta_hook_switch
, NULL
, 0);
478 "Failed to allocate resources for hook switch, "
479 "will continue without one.\n");
481 ret
= snd_soc_jack_add_gpiods(card
->dev
, &ams_delta_hook_switch
,
482 ARRAY_SIZE(ams_delta_hook_switch_gpios
),
483 ams_delta_hook_switch_gpios
);
486 "Failed to set up hook switch GPIO line, "
487 "will continue with hook switch inactive.\n");
490 gpiod_modem_codec
= devm_gpiod_get(card
->dev
, "modem_codec",
492 if (IS_ERR(gpiod_modem_codec
)) {
493 dev_warn(card
->dev
, "Failed to obtain modem_codec GPIO\n");
497 /* Set up digital mute if not provided by the codec */
498 if (!codec_dai
->driver
->ops
) {
499 codec_dai
->driver
->ops
= &ams_delta_dai_ops
;
501 ams_delta_ops
.startup
= ams_delta_startup
;
502 ams_delta_ops
.shutdown
= ams_delta_shutdown
;
505 /* Register optional line discipline for over the modem control */
506 ret
= tty_register_ldisc(N_V253
, &cx81801_ops
);
509 "Failed to register line discipline, "
510 "will continue without any controls.\n");
514 /* Set up initial pin constellation */
515 snd_soc_dapm_disable_pin(dapm
, "Mouthpiece");
516 snd_soc_dapm_disable_pin(dapm
, "Speaker");
517 snd_soc_dapm_disable_pin(dapm
, "AGCIN");
518 snd_soc_dapm_disable_pin(dapm
, "AGCOUT");
523 /* DAI glue - connects codec <--> CPU */
524 SND_SOC_DAILINK_DEFS(cx20442
,
525 DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")),
526 DAILINK_COMP_ARRAY(COMP_CODEC("cx20442-codec", "cx20442-voice")),
527 DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1")));
529 static struct snd_soc_dai_link ams_delta_dai_link
= {
531 .stream_name
= "CX20442",
532 .init
= ams_delta_cx20442_init
,
533 .ops
= &ams_delta_ops
,
534 .dai_fmt
= SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_NB_NF
|
535 SND_SOC_DAIFMT_CBM_CFM
,
536 SND_SOC_DAILINK_REG(cx20442
),
539 /* Audio card driver */
540 static struct snd_soc_card ams_delta_audio_card
= {
542 .owner
= THIS_MODULE
,
543 .dai_link
= &ams_delta_dai_link
,
546 .controls
= ams_delta_audio_controls
,
547 .num_controls
= ARRAY_SIZE(ams_delta_audio_controls
),
548 .dapm_widgets
= ams_delta_dapm_widgets
,
549 .num_dapm_widgets
= ARRAY_SIZE(ams_delta_dapm_widgets
),
550 .dapm_routes
= ams_delta_audio_map
,
551 .num_dapm_routes
= ARRAY_SIZE(ams_delta_audio_map
),
554 /* Module init/exit */
555 static int ams_delta_probe(struct platform_device
*pdev
)
557 struct snd_soc_card
*card
= &ams_delta_audio_card
;
560 card
->dev
= &pdev
->dev
;
562 handset_mute
= devm_gpiod_get(card
->dev
, "handset_mute",
564 if (IS_ERR(handset_mute
))
565 return PTR_ERR(handset_mute
);
567 handsfree_mute
= devm_gpiod_get(card
->dev
, "handsfree_mute",
569 if (IS_ERR(handsfree_mute
))
570 return PTR_ERR(handsfree_mute
);
572 ret
= snd_soc_register_card(card
);
574 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n", ret
);
581 static int ams_delta_remove(struct platform_device
*pdev
)
583 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
585 if (tty_unregister_ldisc(N_V253
) != 0)
587 "failed to unregister V253 line discipline\n");
589 snd_soc_unregister_card(card
);
594 #define DRV_NAME "ams-delta-audio"
596 static struct platform_driver ams_delta_driver
= {
600 .probe
= ams_delta_probe
,
601 .remove
= ams_delta_remove
,
604 module_platform_driver(ams_delta_driver
);
606 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
607 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
608 MODULE_LICENSE("GPL");
609 MODULE_ALIAS("platform:" DRV_NAME
);