blob: 50ca3c42642f8b5418ed36ec4be5c0667ac2288f [file] [log] [blame]
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +02001/*
2 * ams-delta.c -- SoC audio for Amstrad E3 (Delta) videophone
3 *
4 * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
5 *
6 * Initially based on sound/soc/omap/osk5912.x
7 * Copyright (C) 2008 Mistral Solutions
8 *
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.
12 *
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.
17 *
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
21 * 02110-1301 USA
22 *
23 */
24
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +020025#include <linux/gpio/consumer.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020026#include <linux/spinlock.h>
27#include <linux/tty.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020029
Liam Girdwoodce6120c2010-11-05 15:53:46 +020030#include <sound/soc.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020031#include <sound/jack.h>
32
33#include <asm/mach-types.h>
34
Arnd Bergmann22037472012-08-24 15:21:06 +020035#include <linux/platform_data/asoc-ti-mcbsp.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020036
37#include "omap-mcbsp.h"
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020038#include "../codecs/cx20442.h"
39
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020040/* Board specific DAPM widgets */
Janusz Krzysztofik02624622009-10-21 04:40:55 +020041static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020042 /* Handset */
43 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
44 SND_SOC_DAPM_HP("Earpiece", NULL),
45 /* Handsfree/Speakerphone */
46 SND_SOC_DAPM_MIC("Microphone", NULL),
47 SND_SOC_DAPM_SPK("Speaker", NULL),
48};
49
50/* How they are connected to codec pins */
51static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
52 {"TELIN", NULL, "Mouthpiece"},
53 {"Earpiece", NULL, "TELOUT"},
54
55 {"MIC", NULL, "Microphone"},
56 {"Speaker", NULL, "SPKOUT"},
57};
58
59/*
60 * Controls, functional after the modem line discipline is activated.
61 */
62
63/* Virtual switch: audio input/output constellations */
64static const char *ams_delta_audio_mode[] =
65 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
66
67/* Selection <-> pin translation */
68#define AMS_DELTA_MOUTHPIECE 0
69#define AMS_DELTA_EARPIECE 1
70#define AMS_DELTA_MICROPHONE 2
71#define AMS_DELTA_SPEAKER 3
72#define AMS_DELTA_AGC 4
73
74#define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
75 (1 << AMS_DELTA_MICROPHONE))
76#define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
77 (1 << AMS_DELTA_EARPIECE))
78#define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
79 (1 << AMS_DELTA_SPEAKER))
80#define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
81
Janusz Krzysztofik02624622009-10-21 04:40:55 +020082static const unsigned short ams_delta_audio_mode_pins[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020083 AMS_DELTA_MIXED,
84 AMS_DELTA_HANDSET,
85 AMS_DELTA_HANDSFREE,
86 AMS_DELTA_SPEAKERPHONE,
87};
88
89static unsigned short ams_delta_audio_agc;
90
Lars-Peter Clausen74a16722014-03-12 15:27:30 +010091/*
92 * Used for passing a codec structure pointer
93 * from the board initialization code to the tty line discipline.
94 */
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +000095static struct snd_soc_component *cx20442_codec;
Lars-Peter Clausen74a16722014-03-12 15:27:30 +010096
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020097static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
98 struct snd_ctl_elem_value *ucontrol)
99{
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100100 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
101 struct snd_soc_dapm_context *dapm = &card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200102 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
103 unsigned short pins;
104 int pin, changed = 0;
105
106 /* Refuse any mode changes if we are not able to control the codec. */
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000107 if (!cx20442_codec->card->pop_time)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200108 return -EUNATCH;
109
Takashi Iwai9a8d38d2014-02-18 08:11:42 +0100110 if (ucontrol->value.enumerated.item[0] >= control->items)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200111 return -EINVAL;
112
Charles Keepax03510ca2014-02-18 15:22:22 +0000113 snd_soc_dapm_mutex_lock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200114
115 /* Translate selection to bitmap */
116 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
117
118 /* Setup pins after corresponding bits if changed */
119 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
Charles Keepax03510ca2014-02-18 15:22:22 +0000120
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200121 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200122 changed = 1;
123 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000124 snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200125 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000126 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200127 }
128 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200129 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200130 changed = 1;
131 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000132 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200133 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000134 snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200135 }
136 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200137 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200138 changed = 1;
139 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000140 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200141 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000142 snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200143 }
144 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200145 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200146 changed = 1;
147 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000148 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200149 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000150 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200151 }
152 pin = !!(pins & (1 << AMS_DELTA_AGC));
153 if (pin != ams_delta_audio_agc) {
154 ams_delta_audio_agc = pin;
155 changed = 1;
156 if (pin)
Charles Keepax03510ca2014-02-18 15:22:22 +0000157 snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200158 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000159 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200160 }
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200161
Charles Keepax03510ca2014-02-18 15:22:22 +0000162 if (changed)
163 snd_soc_dapm_sync_unlocked(dapm);
164
165 snd_soc_dapm_mutex_unlock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200166
167 return changed;
168}
169
170static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
172{
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100173 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
174 struct snd_soc_dapm_context *dapm = &card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200175 unsigned short pins, mode;
176
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200177 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200178 AMS_DELTA_MOUTHPIECE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200179 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200180 AMS_DELTA_EARPIECE));
181 if (pins)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200182 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200183 AMS_DELTA_MICROPHONE);
184 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200185 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200186 AMS_DELTA_MICROPHONE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200187 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200188 AMS_DELTA_SPEAKER) |
189 (ams_delta_audio_agc << AMS_DELTA_AGC));
190
191 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
192 if (pins == ams_delta_audio_mode_pins[mode])
193 break;
194
195 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
196 return -EINVAL;
197
198 ucontrol->value.enumerated.item[0] = mode;
199
200 return 0;
201}
202
Arnd Bergmannc9fbc1c2019-03-07 11:10:49 +0100203static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
Takashi Iwai7cb5e1b2014-02-18 10:47:34 +0100204 ams_delta_audio_mode);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200205
206static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
Takashi Iwai7cb5e1b2014-02-18 10:47:34 +0100207 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200208 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
209};
210
211/* Hook switch */
212static struct snd_soc_jack ams_delta_hook_switch;
213static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
214 {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200215 .name = "hook_switch",
216 .report = SND_JACK_HEADSET,
217 .invert = 1,
218 .debounce_time = 150,
219 }
220};
221
222/* After we are able to control the codec over the modem,
223 * the hook switch can be used for dynamic DAPM reconfiguration. */
224static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
225 /* Handset */
226 {
227 .pin = "Mouthpiece",
228 .mask = SND_JACK_MICROPHONE,
229 },
230 {
231 .pin = "Earpiece",
232 .mask = SND_JACK_HEADPHONE,
233 },
234 /* Handsfree */
235 {
236 .pin = "Microphone",
237 .mask = SND_JACK_MICROPHONE,
238 .invert = 1,
239 },
240 {
241 .pin = "Speaker",
242 .mask = SND_JACK_HEADPHONE,
243 .invert = 1,
244 },
245};
246
247
248/*
249 * Modem line discipline, required for making above controls functional.
250 * Activated from userspace with ldattach, possibly invoked from udev rule.
251 */
252
253/* To actually apply any modem controlled configuration changes to the codec,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300254 * we must connect codec DAI pins to the modem for a moment. Be careful not
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200255 * to interfere with our digital mute function that shares the same hardware. */
256static struct timer_list cx81801_timer;
257static bool cx81801_cmd_pending;
258static bool ams_delta_muted;
259static DEFINE_SPINLOCK(ams_delta_lock);
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200260static struct gpio_desc *gpiod_modem_codec;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200261
Kees Cook7211ec62017-10-25 08:09:27 -0700262static void cx81801_timeout(struct timer_list *unused)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200263{
264 int muted;
265
266 spin_lock(&ams_delta_lock);
267 cx81801_cmd_pending = 0;
268 muted = ams_delta_muted;
269 spin_unlock(&ams_delta_lock);
270
271 /* Reconnect the codec DAI back from the modem to the CPU DAI
272 * only if digital mute still off */
273 if (!muted)
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200274 gpiod_set_value(gpiod_modem_codec, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200275}
276
277/* Line discipline .open() */
278static int cx81801_open(struct tty_struct *tty)
279{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000280 int ret;
281
282 if (!cx20442_codec)
283 return -ENODEV;
284
285 /*
286 * Pass the codec structure pointer for use by other ldisc callbacks,
287 * both the card and the codec specific parts.
288 */
289 tty->disc_data = cx20442_codec;
290
291 ret = v253_ops.open(tty);
292
293 if (ret < 0)
294 tty->disc_data = NULL;
295
296 return ret;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200297}
298
299/* Line discipline .close() */
300static void cx81801_close(struct tty_struct *tty)
301{
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000302 struct snd_soc_component *component = tty->disc_data;
303 struct snd_soc_dapm_context *dapm = &component->card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200304
305 del_timer_sync(&cx81801_timer);
306
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200307 /* Prevent the hook switch from further changing the DAPM pins */
308 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
309
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000310 if (!component)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000311 return;
312
313 v253_ops.close(tty);
314
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200315 /* Revert back to default audio input/output constellation */
Charles Keepax03510ca2014-02-18 15:22:22 +0000316 snd_soc_dapm_mutex_lock(dapm);
317
318 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
319 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
320 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
321 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
322 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
323
324 snd_soc_dapm_sync_unlocked(dapm);
325
Lars-Peter Clausene95d73c2014-03-12 15:27:29 +0100326 snd_soc_dapm_mutex_unlock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200327}
328
329/* Line discipline .hangup() */
330static int cx81801_hangup(struct tty_struct *tty)
331{
332 cx81801_close(tty);
333 return 0;
334}
335
Joe Perchesdbc62212011-06-23 11:39:19 -0700336/* Line discipline .receive_buf() */
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200337static void cx81801_receive(struct tty_struct *tty,
338 const unsigned char *cp, char *fp, int count)
339{
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000340 struct snd_soc_component *component = tty->disc_data;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200341 const unsigned char *c;
342 int apply, ret;
343
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000344 if (!component)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000345 return;
346
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000347 if (!component->card->pop_time) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200348 /* First modem response, complete setup procedure */
349
350 /* Initialize timer used for config pulse generation */
Kees Cook7211ec62017-10-25 08:09:27 -0700351 timer_setup(&cx81801_timer, cx81801_timeout, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200352
353 v253_ops.receive_buf(tty, cp, fp, count);
354
355 /* Link hook switch to DAPM pins */
356 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
357 ARRAY_SIZE(ams_delta_hook_switch_pins),
358 ams_delta_hook_switch_pins);
359 if (ret)
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000360 dev_warn(component->dev,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200361 "Failed to link hook switch to DAPM pins, "
362 "will continue with hook switch unlinked.\n");
363
364 return;
365 }
366
367 v253_ops.receive_buf(tty, cp, fp, count);
368
369 for (c = &cp[count - 1]; c >= cp; c--) {
370 if (*c != '\r')
371 continue;
372 /* Complete modem response received, apply config to codec */
373
374 spin_lock_bh(&ams_delta_lock);
375 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
376 apply = !ams_delta_muted && !cx81801_cmd_pending;
377 cx81801_cmd_pending = 1;
378 spin_unlock_bh(&ams_delta_lock);
379
380 /* Apply config pulse by connecting the codec to the modem
381 * if not already done */
382 if (apply)
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200383 gpiod_set_value(gpiod_modem_codec, 1);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200384 break;
385 }
386}
387
388/* Line discipline .write_wakeup() */
389static void cx81801_wakeup(struct tty_struct *tty)
390{
391 v253_ops.write_wakeup(tty);
392}
393
394static struct tty_ldisc_ops cx81801_ops = {
395 .magic = TTY_LDISC_MAGIC,
396 .name = "cx81801",
397 .owner = THIS_MODULE,
398 .open = cx81801_open,
399 .close = cx81801_close,
400 .hangup = cx81801_hangup,
401 .receive_buf = cx81801_receive,
402 .write_wakeup = cx81801_wakeup,
403};
404
405
406/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300407 * Even if not very useful, the sound card can still work without any of the
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200408 * above functonality activated. You can still control its audio input/output
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300409 * constellation and speakerphone gain from userspace by issuing AT commands
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200410 * over the modem port.
411 */
412
Lars-Peter Clausen3aa273e2015-01-01 17:16:16 +0100413static struct snd_soc_ops ams_delta_ops;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200414
415
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200416/* Digital mute implemented using modem/CPU multiplexer.
417 * Shares hardware with codec config pulse generation */
418static bool ams_delta_muted = 1;
419
420static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
421{
422 int apply;
423
424 if (ams_delta_muted == mute)
425 return 0;
426
427 spin_lock_bh(&ams_delta_lock);
428 ams_delta_muted = mute;
429 apply = !cx81801_cmd_pending;
430 spin_unlock_bh(&ams_delta_lock);
431
432 if (apply)
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200433 gpiod_set_value(gpiod_modem_codec, !!mute);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200434 return 0;
435}
436
437/* Our codec DAI probably doesn't have its own .ops structure */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100438static const struct snd_soc_dai_ops ams_delta_dai_ops = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200439 .digital_mute = ams_delta_digital_mute,
440};
441
442/* Will be used if the codec ever has its own digital_mute function */
443static int ams_delta_startup(struct snd_pcm_substream *substream)
444{
445 return ams_delta_digital_mute(NULL, 0);
446}
447
448static void ams_delta_shutdown(struct snd_pcm_substream *substream)
449{
450 ams_delta_digital_mute(NULL, 1);
451}
452
453
454/*
455 * Card initialization
456 */
457
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000458static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200459{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000460 struct snd_soc_dai *codec_dai = rtd->codec_dai;
461 struct snd_soc_card *card = rtd->card;
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100462 struct snd_soc_dapm_context *dapm = &card->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200463 int ret;
464 /* Codec is ready, now add/activate board specific controls */
465
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000466 /* Store a pointer to the codec structure for tty ldisc use */
Kuninori Morimotod0fdfe32018-01-29 04:15:57 +0000467 cx20442_codec = rtd->codec_dai->component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000468
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200469 /* Add hook switch - can be used to control the codec from userspace
470 * even if line discipline fails */
Lars-Peter Clausendf8c6612015-03-04 10:33:25 +0100471 ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET,
472 &ams_delta_hook_switch, NULL, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200473 if (ret)
474 dev_warn(card->dev,
475 "Failed to allocate resources for hook switch, "
476 "will continue without one.\n");
477 else {
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200478 ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200479 ARRAY_SIZE(ams_delta_hook_switch_gpios),
480 ams_delta_hook_switch_gpios);
481 if (ret)
482 dev_warn(card->dev,
483 "Failed to set up hook switch GPIO line, "
484 "will continue with hook switch inactive.\n");
485 }
486
Janusz Krzysztofikd65777d2018-05-18 23:09:51 +0200487 gpiod_modem_codec = devm_gpiod_get(card->dev, "modem_codec",
488 GPIOD_OUT_HIGH);
489 if (IS_ERR(gpiod_modem_codec)) {
490 dev_warn(card->dev, "Failed to obtain modem_codec GPIO\n");
491 return 0;
492 }
493
494 /* Set up digital mute if not provided by the codec */
495 if (!codec_dai->driver->ops) {
496 codec_dai->driver->ops = &ams_delta_dai_ops;
497 } else {
498 ams_delta_ops.startup = ams_delta_startup;
499 ams_delta_ops.shutdown = ams_delta_shutdown;
500 }
501
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200502 /* Register optional line discipline for over the modem control */
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200503 ret = tty_register_ldisc(N_V253, &cx81801_ops);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200504 if (ret) {
505 dev_warn(card->dev,
506 "Failed to register line discipline, "
507 "will continue without any controls.\n");
508 return 0;
509 }
510
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200511 /* Set up initial pin constellation */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200512 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200513 snd_soc_dapm_disable_pin(dapm, "Speaker");
514 snd_soc_dapm_disable_pin(dapm, "AGCIN");
515 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200516
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200517 return 0;
518}
519
520/* DAI glue - connects codec <--> CPU */
Kuninori Morimotoacb9a2d2019-06-06 13:12:48 +0900521SND_SOC_DAILINK_DEFS(cx20442,
522 DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")),
523 DAILINK_COMP_ARRAY(COMP_CODEC("cx20442-codec", "cx20442-voice")),
524 DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1")));
525
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200526static struct snd_soc_dai_link ams_delta_dai_link = {
527 .name = "CX20442",
528 .stream_name = "CX20442",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200529 .init = ams_delta_cx20442_init,
530 .ops = &ams_delta_ops,
Lars-Peter Clausen3aa273e2015-01-01 17:16:16 +0100531 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
532 SND_SOC_DAIFMT_CBM_CFM,
Kuninori Morimotoacb9a2d2019-06-06 13:12:48 +0900533 SND_SOC_DAILINK_REG(cx20442),
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200534};
535
536/* Audio card driver */
537static struct snd_soc_card ams_delta_audio_card = {
538 .name = "AMS_DELTA",
Axel Linb425b882011-12-22 11:08:59 +0800539 .owner = THIS_MODULE,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200540 .dai_link = &ams_delta_dai_link,
541 .num_links = 1,
Lars-Peter Clausen74a16722014-03-12 15:27:30 +0100542
543 .controls = ams_delta_audio_controls,
544 .num_controls = ARRAY_SIZE(ams_delta_audio_controls),
545 .dapm_widgets = ams_delta_dapm_widgets,
546 .num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
547 .dapm_routes = ams_delta_audio_map,
548 .num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200549};
550
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200551/* Module init/exit */
Bill Pemberton7ff60002012-12-07 09:26:29 -0500552static int ams_delta_probe(struct platform_device *pdev)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200553{
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200554 struct snd_soc_card *card = &ams_delta_audio_card;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200555 int ret;
556
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200557 card->dev = &pdev->dev;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200558
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200559 ret = snd_soc_register_card(card);
560 if (ret) {
561 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
562 card->dev = NULL;
563 return ret;
564 }
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200565 return 0;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200566}
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200567
Bill Pemberton7ff60002012-12-07 09:26:29 -0500568static int ams_delta_remove(struct platform_device *pdev)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200569{
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200570 struct snd_soc_card *card = platform_get_drvdata(pdev);
571
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200572 if (tty_unregister_ldisc(N_V253) != 0)
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200573 dev_warn(&pdev->dev,
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200574 "failed to unregister V253 line discipline\n");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200575
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200576 snd_soc_unregister_card(card);
577 card->dev = NULL;
578 return 0;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200579}
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200580
581#define DRV_NAME "ams-delta-audio"
582
583static struct platform_driver ams_delta_driver = {
584 .driver = {
585 .name = DRV_NAME,
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200586 },
587 .probe = ams_delta_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500588 .remove = ams_delta_remove,
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200589};
590
591module_platform_driver(ams_delta_driver);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200592
593MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
594MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
595MODULE_LICENSE("GPL");
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200596MODULE_ALIAS("platform:" DRV_NAME);