blob: dee8fc70a64f5d8d374f0c434b1fbf87f17caae0 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +02002/*
3 * ams-delta.c -- SoC audio for Amstrad E3 (Delta) videophone
4 *
5 * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6 *
7 * Initially based on sound/soc/omap/osk5912.x
8 * Copyright (C) 2008 Mistral Solutions
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +02009 */
10
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +020011#include <linux/gpio/consumer.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020012#include <linux/spinlock.h>
13#include <linux/tty.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040014#include <linux/module.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020015
Liam Girdwoodce6120c2010-11-05 15:53:46 +020016#include <sound/soc.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020017#include <sound/jack.h>
18
19#include <asm/mach-types.h>
20
Arnd Bergmann22037472012-08-24 15:21:06 +020021#include <linux/platform_data/asoc-ti-mcbsp.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020022
23#include "omap-mcbsp.h"
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020024#include "../codecs/cx20442.h"
25
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020026/* Board specific DAPM widgets */
Janusz Krzysztofik02624622009-10-21 04:40:55 +020027static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020028 /* Handset */
29 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
30 SND_SOC_DAPM_HP("Earpiece", NULL),
31 /* Handsfree/Speakerphone */
32 SND_SOC_DAPM_MIC("Microphone", NULL),
33 SND_SOC_DAPM_SPK("Speaker", NULL),
34};
35
36/* How they are connected to codec pins */
37static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
38 {"TELIN", NULL, "Mouthpiece"},
39 {"Earpiece", NULL, "TELOUT"},
40
41 {"MIC", NULL, "Microphone"},
42 {"Speaker", NULL, "SPKOUT"},
43};
44
45/*
46 * Controls, functional after the modem line discipline is activated.
47 */
48
49/* Virtual switch: audio input/output constellations */
50static const char *ams_delta_audio_mode[] =
51 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
52
53/* Selection <-> pin translation */
54#define AMS_DELTA_MOUTHPIECE 0
55#define AMS_DELTA_EARPIECE 1
56#define AMS_DELTA_MICROPHONE 2
57#define AMS_DELTA_SPEAKER 3
58#define AMS_DELTA_AGC 4
59
60#define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
61 (1 << AMS_DELTA_MICROPHONE))
62#define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
63 (1 << AMS_DELTA_EARPIECE))
64#define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
65 (1 << AMS_DELTA_SPEAKER))
66#define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
67
Janusz Krzysztofik02624622009-10-21 04:40:55 +020068static const unsigned short ams_delta_audio_mode_pins[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020069 AMS_DELTA_MIXED,
70 AMS_DELTA_HANDSET,
71 AMS_DELTA_HANDSFREE,
72 AMS_DELTA_SPEAKERPHONE,
73};
74
75static unsigned short ams_delta_audio_agc;
76
Lars-Peter Clausen74a16722014-03-12 15:27:30 +010077/*
78 * Used for passing a codec structure pointer
79 * from the board initialization code to the tty line discipline.
80 */
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +000081static struct snd_soc_component *cx20442_codec;
Lars-Peter Clausen74a16722014-03-12 15:27:30 +010082
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020083static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
84 struct snd_ctl_elem_value *ucontrol)
85{
Lars-Peter Clausen74a16722014-03-12 15:27:30 +010086 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
87 struct snd_soc_dapm_context *dapm = &card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020088 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
89 unsigned short pins;
90 int pin, changed = 0;
91
92 /* Refuse any mode changes if we are not able to control the codec. */
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +000093 if (!cx20442_codec->card->pop_time)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020094 return -EUNATCH;
95
Takashi Iwai9a8d38d2014-02-18 08:11:42 +010096 if (ucontrol->value.enumerated.item[0] >= control->items)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020097 return -EINVAL;
98
Charles Keepax03510ca2014-02-18 15:22:22 +000099 snd_soc_dapm_mutex_lock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200100
101 /* Translate selection to bitmap */
102 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
103
104 /* Setup pins after corresponding bits if changed */
105 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
Charles Keepax03510ca2014-02-18 15:22:22 +0000106
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200107 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200108 changed = 1;
109 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000110 snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200111 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000112 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200113 }
114 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200115 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200116 changed = 1;
117 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000118 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200119 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000120 snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200121 }
122 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200123 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200124 changed = 1;
125 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000126 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200127 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000128 snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200129 }
130 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200131 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200132 changed = 1;
133 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000134 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200135 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000136 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200137 }
138 pin = !!(pins & (1 << AMS_DELTA_AGC));
139 if (pin != ams_delta_audio_agc) {
140 ams_delta_audio_agc = pin;
141 changed = 1;
142 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000143 snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200144 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000145 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200146 }
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200147
Charles Keepax03510ca2014-02-18 15:22:22 +0000148 if (changed)
149 snd_soc_dapm_sync_unlocked(dapm);
150
151 snd_soc_dapm_mutex_unlock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200152
153 return changed;
154}
155
156static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
157 struct snd_ctl_elem_value *ucontrol)
158{
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100159 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
160 struct snd_soc_dapm_context *dapm = &card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200161 unsigned short pins, mode;
162
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200163 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200164 AMS_DELTA_MOUTHPIECE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200165 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200166 AMS_DELTA_EARPIECE));
167 if (pins)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200168 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200169 AMS_DELTA_MICROPHONE);
170 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200171 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200172 AMS_DELTA_MICROPHONE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200173 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200174 AMS_DELTA_SPEAKER) |
175 (ams_delta_audio_agc << AMS_DELTA_AGC));
176
177 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
178 if (pins == ams_delta_audio_mode_pins[mode])
179 break;
180
181 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
182 return -EINVAL;
183
184 ucontrol->value.enumerated.item[0] = mode;
185
186 return 0;
187}
188
Arnd Bergmannc9fbc1c2019-03-07 11:10:49 +0100189static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
Takashi Iwai7cb5e1b2014-02-18 10:47:34 +0100190 ams_delta_audio_mode);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200191
192static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
Takashi Iwai7cb5e1b2014-02-18 10:47:34 +0100193 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200194 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
195};
196
197/* Hook switch */
198static struct snd_soc_jack ams_delta_hook_switch;
199static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
200 {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200201 .name = "hook_switch",
202 .report = SND_JACK_HEADSET,
203 .invert = 1,
204 .debounce_time = 150,
205 }
206};
207
208/* After we are able to control the codec over the modem,
209 * the hook switch can be used for dynamic DAPM reconfiguration. */
210static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
211 /* Handset */
212 {
213 .pin = "Mouthpiece",
214 .mask = SND_JACK_MICROPHONE,
215 },
216 {
217 .pin = "Earpiece",
218 .mask = SND_JACK_HEADPHONE,
219 },
220 /* Handsfree */
221 {
222 .pin = "Microphone",
223 .mask = SND_JACK_MICROPHONE,
224 .invert = 1,
225 },
226 {
227 .pin = "Speaker",
228 .mask = SND_JACK_HEADPHONE,
229 .invert = 1,
230 },
231};
232
233
234/*
235 * Modem line discipline, required for making above controls functional.
236 * Activated from userspace with ldattach, possibly invoked from udev rule.
237 */
238
239/* To actually apply any modem controlled configuration changes to the codec,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300240 * we must connect codec DAI pins to the modem for a moment. Be careful not
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200241 * to interfere with our digital mute function that shares the same hardware. */
242static struct timer_list cx81801_timer;
243static bool cx81801_cmd_pending;
244static bool ams_delta_muted;
245static DEFINE_SPINLOCK(ams_delta_lock);
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200246static struct gpio_desc *gpiod_modem_codec;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200247
Kees Cook7211ec62017-10-25 08:09:27 -0700248static void cx81801_timeout(struct timer_list *unused)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200249{
250 int muted;
251
252 spin_lock(&ams_delta_lock);
253 cx81801_cmd_pending = 0;
254 muted = ams_delta_muted;
255 spin_unlock(&ams_delta_lock);
256
257 /* Reconnect the codec DAI back from the modem to the CPU DAI
258 * only if digital mute still off */
259 if (!muted)
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200260 gpiod_set_value(gpiod_modem_codec, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200261}
262
263/* Line discipline .open() */
264static int cx81801_open(struct tty_struct *tty)
265{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000266 int ret;
267
268 if (!cx20442_codec)
269 return -ENODEV;
270
271 /*
272 * Pass the codec structure pointer for use by other ldisc callbacks,
273 * both the card and the codec specific parts.
274 */
275 tty->disc_data = cx20442_codec;
276
277 ret = v253_ops.open(tty);
278
279 if (ret < 0)
280 tty->disc_data = NULL;
281
282 return ret;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200283}
284
285/* Line discipline .close() */
286static void cx81801_close(struct tty_struct *tty)
287{
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000288 struct snd_soc_component *component = tty->disc_data;
289 struct snd_soc_dapm_context *dapm = &component->card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200290
291 del_timer_sync(&cx81801_timer);
292
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200293 /* Prevent the hook switch from further changing the DAPM pins */
294 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
295
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000296 if (!component)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000297 return;
298
299 v253_ops.close(tty);
300
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200301 /* Revert back to default audio input/output constellation */
Charles Keepax03510ca2014-02-18 15:22:22 +0000302 snd_soc_dapm_mutex_lock(dapm);
303
304 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
305 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
306 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
307 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
308 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
309
310 snd_soc_dapm_sync_unlocked(dapm);
311
Lars-Peter Clausene95d73c2014-03-12 15:27:29 +0100312 snd_soc_dapm_mutex_unlock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200313}
314
315/* Line discipline .hangup() */
316static int cx81801_hangup(struct tty_struct *tty)
317{
318 cx81801_close(tty);
319 return 0;
320}
321
Joe Perchesdbc62212011-06-23 11:39:19 -0700322/* Line discipline .receive_buf() */
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200323static void cx81801_receive(struct tty_struct *tty,
324 const unsigned char *cp, char *fp, int count)
325{
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000326 struct snd_soc_component *component = tty->disc_data;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200327 const unsigned char *c;
328 int apply, ret;
329
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000330 if (!component)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000331 return;
332
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000333 if (!component->card->pop_time) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200334 /* First modem response, complete setup procedure */
335
336 /* Initialize timer used for config pulse generation */
Kees Cook7211ec62017-10-25 08:09:27 -0700337 timer_setup(&cx81801_timer, cx81801_timeout, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200338
339 v253_ops.receive_buf(tty, cp, fp, count);
340
341 /* Link hook switch to DAPM pins */
342 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
343 ARRAY_SIZE(ams_delta_hook_switch_pins),
344 ams_delta_hook_switch_pins);
345 if (ret)
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000346 dev_warn(component->dev,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200347 "Failed to link hook switch to DAPM pins, "
348 "will continue with hook switch unlinked.\n");
349
350 return;
351 }
352
353 v253_ops.receive_buf(tty, cp, fp, count);
354
355 for (c = &cp[count - 1]; c >= cp; c--) {
356 if (*c != '\r')
357 continue;
358 /* Complete modem response received, apply config to codec */
359
360 spin_lock_bh(&ams_delta_lock);
361 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
362 apply = !ams_delta_muted && !cx81801_cmd_pending;
363 cx81801_cmd_pending = 1;
364 spin_unlock_bh(&ams_delta_lock);
365
366 /* Apply config pulse by connecting the codec to the modem
367 * if not already done */
368 if (apply)
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200369 gpiod_set_value(gpiod_modem_codec, 1);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200370 break;
371 }
372}
373
374/* Line discipline .write_wakeup() */
375static void cx81801_wakeup(struct tty_struct *tty)
376{
377 v253_ops.write_wakeup(tty);
378}
379
380static struct tty_ldisc_ops cx81801_ops = {
381 .magic = TTY_LDISC_MAGIC,
382 .name = "cx81801",
383 .owner = THIS_MODULE,
384 .open = cx81801_open,
385 .close = cx81801_close,
386 .hangup = cx81801_hangup,
387 .receive_buf = cx81801_receive,
388 .write_wakeup = cx81801_wakeup,
389};
390
391
392/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300393 * Even if not very useful, the sound card can still work without any of the
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200394 * above functonality activated. You can still control its audio input/output
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300395 * constellation and speakerphone gain from userspace by issuing AT commands
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200396 * over the modem port.
397 */
398
Lars-Peter Clausen3aa273e2015-01-01 17:16:16 +0100399static struct snd_soc_ops ams_delta_ops;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200400
401
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200402/* Digital mute implemented using modem/CPU multiplexer.
403 * Shares hardware with codec config pulse generation */
404static bool ams_delta_muted = 1;
405
406static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
407{
408 int apply;
409
410 if (ams_delta_muted == mute)
411 return 0;
412
413 spin_lock_bh(&ams_delta_lock);
414 ams_delta_muted = mute;
415 apply = !cx81801_cmd_pending;
416 spin_unlock_bh(&ams_delta_lock);
417
418 if (apply)
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200419 gpiod_set_value(gpiod_modem_codec, !!mute);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200420 return 0;
421}
422
423/* Our codec DAI probably doesn't have its own .ops structure */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100424static const struct snd_soc_dai_ops ams_delta_dai_ops = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200425 .digital_mute = ams_delta_digital_mute,
426};
427
428/* Will be used if the codec ever has its own digital_mute function */
429static int ams_delta_startup(struct snd_pcm_substream *substream)
430{
431 return ams_delta_digital_mute(NULL, 0);
432}
433
434static void ams_delta_shutdown(struct snd_pcm_substream *substream)
435{
436 ams_delta_digital_mute(NULL, 1);
437}
438
439
440/*
441 * Card initialization
442 */
443
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000444static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200445{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000446 struct snd_soc_dai *codec_dai = rtd->codec_dai;
447 struct snd_soc_card *card = rtd->card;
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100448 struct snd_soc_dapm_context *dapm = &card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200449 int ret;
450 /* Codec is ready, now add/activate board specific controls */
451
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000452 /* Store a pointer to the codec structure for tty ldisc use */
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000453 cx20442_codec = rtd->codec_dai->component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000454
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200455 /* Add hook switch - can be used to control the codec from userspace
456 * even if line discipline fails */
Lars-Peter Clausendf8c6612015-03-04 10:33:25 +0100457 ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET,
458 &ams_delta_hook_switch, NULL, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200459 if (ret)
460 dev_warn(card->dev,
461 "Failed to allocate resources for hook switch, "
462 "will continue without one.\n");
463 else {
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200464 ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200465 ARRAY_SIZE(ams_delta_hook_switch_gpios),
466 ams_delta_hook_switch_gpios);
467 if (ret)
468 dev_warn(card->dev,
469 "Failed to set up hook switch GPIO line, "
470 "will continue with hook switch inactive.\n");
471 }
472
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200473 gpiod_modem_codec = devm_gpiod_get(card->dev, "modem_codec",
474 GPIOD_OUT_HIGH);
475 if (IS_ERR(gpiod_modem_codec)) {
476 dev_warn(card->dev, "Failed to obtain modem_codec GPIO\n");
477 return 0;
478 }
479
480 /* Set up digital mute if not provided by the codec */
481 if (!codec_dai->driver->ops) {
482 codec_dai->driver->ops = &ams_delta_dai_ops;
483 } else {
484 ams_delta_ops.startup = ams_delta_startup;
485 ams_delta_ops.shutdown = ams_delta_shutdown;
486 }
487
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200488 /* Register optional line discipline for over the modem control */
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200489 ret = tty_register_ldisc(N_V253, &cx81801_ops);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200490 if (ret) {
491 dev_warn(card->dev,
492 "Failed to register line discipline, "
493 "will continue without any controls.\n");
494 return 0;
495 }
496
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200497 /* Set up initial pin constellation */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200498 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200499 snd_soc_dapm_disable_pin(dapm, "Speaker");
500 snd_soc_dapm_disable_pin(dapm, "AGCIN");
501 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200502
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200503 return 0;
504}
505
506/* DAI glue - connects codec <--> CPU */
Kuninori Morimotoacb9a2d2019-06-06 13:12:48 +0900507SND_SOC_DAILINK_DEFS(cx20442,
508 DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")),
509 DAILINK_COMP_ARRAY(COMP_CODEC("cx20442-codec", "cx20442-voice")),
510 DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1")));
511
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200512static struct snd_soc_dai_link ams_delta_dai_link = {
513 .name = "CX20442",
514 .stream_name = "CX20442",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200515 .init = ams_delta_cx20442_init,
516 .ops = &ams_delta_ops,
Lars-Peter Clausen3aa273e2015-01-01 17:16:16 +0100517 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
518 SND_SOC_DAIFMT_CBM_CFM,
Kuninori Morimotoacb9a2d2019-06-06 13:12:48 +0900519 SND_SOC_DAILINK_REG(cx20442),
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200520};
521
522/* Audio card driver */
523static struct snd_soc_card ams_delta_audio_card = {
524 .name = "AMS_DELTA",
Axel Linb425b882011-12-22 11:08:59 +0800525 .owner = THIS_MODULE,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200526 .dai_link = &ams_delta_dai_link,
527 .num_links = 1,
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100528
529 .controls = ams_delta_audio_controls,
530 .num_controls = ARRAY_SIZE(ams_delta_audio_controls),
531 .dapm_widgets = ams_delta_dapm_widgets,
532 .num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
533 .dapm_routes = ams_delta_audio_map,
534 .num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200535};
536
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200537/* Module init/exit */
Bill Pemberton7ff60002012-12-07 09:26:29 -0500538static int ams_delta_probe(struct platform_device *pdev)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200539{
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200540 struct snd_soc_card *card = &ams_delta_audio_card;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200541 int ret;
542
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200543 card->dev = &pdev->dev;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200544
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200545 ret = snd_soc_register_card(card);
546 if (ret) {
547 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
548 card->dev = NULL;
549 return ret;
550 }
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200551 return 0;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200552}
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200553
Bill Pemberton7ff60002012-12-07 09:26:29 -0500554static int ams_delta_remove(struct platform_device *pdev)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200555{
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200556 struct snd_soc_card *card = platform_get_drvdata(pdev);
557
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200558 if (tty_unregister_ldisc(N_V253) != 0)
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200559 dev_warn(&pdev->dev,
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200560 "failed to unregister V253 line discipline\n");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200561
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200562 snd_soc_unregister_card(card);
563 card->dev = NULL;
564 return 0;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200565}
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200566
567#define DRV_NAME "ams-delta-audio"
568
569static struct platform_driver ams_delta_driver = {
570 .driver = {
571 .name = DRV_NAME,
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200572 },
573 .probe = ams_delta_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500574 .remove = ams_delta_remove,
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200575};
576
577module_platform_driver(ams_delta_driver);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200578
579MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
580MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
581MODULE_LICENSE("GPL");
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200582MODULE_ALIAS("platform:" DRV_NAME);