blob: a04a4338fdac131e5542dc0a41c757e3ed5a46a7 [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
25#include <linux/gpio.h>
26#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
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/board-ams-delta.h>
36#include <plat/mcbsp.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020037
38#include "omap-mcbsp.h"
39#include "omap-pcm.h"
40#include "../codecs/cx20442.h"
41
42
43/* Board specific DAPM widgets */
Janusz Krzysztofik02624622009-10-21 04:40:55 +020044static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020045 /* Handset */
46 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
47 SND_SOC_DAPM_HP("Earpiece", NULL),
48 /* Handsfree/Speakerphone */
49 SND_SOC_DAPM_MIC("Microphone", NULL),
50 SND_SOC_DAPM_SPK("Speaker", NULL),
51};
52
53/* How they are connected to codec pins */
54static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
55 {"TELIN", NULL, "Mouthpiece"},
56 {"Earpiece", NULL, "TELOUT"},
57
58 {"MIC", NULL, "Microphone"},
59 {"Speaker", NULL, "SPKOUT"},
60};
61
62/*
63 * Controls, functional after the modem line discipline is activated.
64 */
65
66/* Virtual switch: audio input/output constellations */
67static const char *ams_delta_audio_mode[] =
68 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
69
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
76
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))
84
Janusz Krzysztofik02624622009-10-21 04:40:55 +020085static const unsigned short ams_delta_audio_mode_pins[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020086 AMS_DELTA_MIXED,
87 AMS_DELTA_HANDSET,
88 AMS_DELTA_HANDSFREE,
89 AMS_DELTA_SPEAKERPHONE,
90};
91
92static unsigned short ams_delta_audio_agc;
93
94static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
95 struct snd_ctl_elem_value *ucontrol)
96{
97 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodce6120c2010-11-05 15:53:46 +020098 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020099 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
100 unsigned short pins;
101 int pin, changed = 0;
102
103 /* Refuse any mode changes if we are not able to control the codec. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000104 if (!codec->hw_write)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200105 return -EUNATCH;
106
107 if (ucontrol->value.enumerated.item[0] >= control->max)
108 return -EINVAL;
109
110 mutex_lock(&codec->mutex);
111
112 /* Translate selection to bitmap */
113 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
114
115 /* Setup pins after corresponding bits if changed */
116 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200117 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200118 changed = 1;
119 if (pin)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200120 snd_soc_dapm_enable_pin(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200121 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200122 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200123 }
124 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200125 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200126 changed = 1;
127 if (pin)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200128 snd_soc_dapm_enable_pin(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200129 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200130 snd_soc_dapm_disable_pin(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200131 }
132 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200133 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200134 changed = 1;
135 if (pin)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200136 snd_soc_dapm_enable_pin(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200137 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200138 snd_soc_dapm_disable_pin(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200139 }
140 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200141 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200142 changed = 1;
143 if (pin)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200144 snd_soc_dapm_enable_pin(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200145 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200146 snd_soc_dapm_disable_pin(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200147 }
148 pin = !!(pins & (1 << AMS_DELTA_AGC));
149 if (pin != ams_delta_audio_agc) {
150 ams_delta_audio_agc = pin;
151 changed = 1;
152 if (pin)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200153 snd_soc_dapm_enable_pin(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200154 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200155 snd_soc_dapm_disable_pin(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200156 }
157 if (changed)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200158 snd_soc_dapm_sync(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200159
160 mutex_unlock(&codec->mutex);
161
162 return changed;
163}
164
165static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
166 struct snd_ctl_elem_value *ucontrol)
167{
168 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200169 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200170 unsigned short pins, mode;
171
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200172 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200173 AMS_DELTA_MOUTHPIECE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200174 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200175 AMS_DELTA_EARPIECE));
176 if (pins)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200177 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200178 AMS_DELTA_MICROPHONE);
179 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200180 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200181 AMS_DELTA_MICROPHONE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200182 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200183 AMS_DELTA_SPEAKER) |
184 (ams_delta_audio_agc << AMS_DELTA_AGC));
185
186 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
187 if (pins == ams_delta_audio_mode_pins[mode])
188 break;
189
190 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
191 return -EINVAL;
192
193 ucontrol->value.enumerated.item[0] = mode;
194
195 return 0;
196}
197
198static const struct soc_enum ams_delta_audio_enum[] = {
199 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ams_delta_audio_mode),
200 ams_delta_audio_mode),
201};
202
203static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
204 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum[0],
205 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
206};
207
208/* Hook switch */
209static struct snd_soc_jack ams_delta_hook_switch;
210static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
211 {
212 .gpio = 4,
213 .name = "hook_switch",
214 .report = SND_JACK_HEADSET,
215 .invert = 1,
216 .debounce_time = 150,
217 }
218};
219
220/* After we are able to control the codec over the modem,
221 * the hook switch can be used for dynamic DAPM reconfiguration. */
222static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
223 /* Handset */
224 {
225 .pin = "Mouthpiece",
226 .mask = SND_JACK_MICROPHONE,
227 },
228 {
229 .pin = "Earpiece",
230 .mask = SND_JACK_HEADPHONE,
231 },
232 /* Handsfree */
233 {
234 .pin = "Microphone",
235 .mask = SND_JACK_MICROPHONE,
236 .invert = 1,
237 },
238 {
239 .pin = "Speaker",
240 .mask = SND_JACK_HEADPHONE,
241 .invert = 1,
242 },
243};
244
245
246/*
247 * Modem line discipline, required for making above controls functional.
248 * Activated from userspace with ldattach, possibly invoked from udev rule.
249 */
250
251/* To actually apply any modem controlled configuration changes to the codec,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300252 * we must connect codec DAI pins to the modem for a moment. Be careful not
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200253 * to interfere with our digital mute function that shares the same hardware. */
254static struct timer_list cx81801_timer;
255static bool cx81801_cmd_pending;
256static bool ams_delta_muted;
257static DEFINE_SPINLOCK(ams_delta_lock);
258
259static void cx81801_timeout(unsigned long data)
260{
261 int muted;
262
263 spin_lock(&ams_delta_lock);
264 cx81801_cmd_pending = 0;
265 muted = ams_delta_muted;
266 spin_unlock(&ams_delta_lock);
267
268 /* Reconnect the codec DAI back from the modem to the CPU DAI
269 * only if digital mute still off */
270 if (!muted)
271 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC, 0);
272}
273
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000274/*
275 * Used for passing a codec structure pointer
276 * from the board initialization code to the tty line discipline.
277 */
278static struct snd_soc_codec *cx20442_codec;
279
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200280/* Line discipline .open() */
281static int cx81801_open(struct tty_struct *tty)
282{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000283 int ret;
284
285 if (!cx20442_codec)
286 return -ENODEV;
287
288 /*
289 * Pass the codec structure pointer for use by other ldisc callbacks,
290 * both the card and the codec specific parts.
291 */
292 tty->disc_data = cx20442_codec;
293
294 ret = v253_ops.open(tty);
295
296 if (ret < 0)
297 tty->disc_data = NULL;
298
299 return ret;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200300}
301
302/* Line discipline .close() */
303static void cx81801_close(struct tty_struct *tty)
304{
305 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200306 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200307
308 del_timer_sync(&cx81801_timer);
309
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200310 /* Prevent the hook switch from further changing the DAPM pins */
311 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
312
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000313 if (!codec)
314 return;
315
316 v253_ops.close(tty);
317
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200318 /* Revert back to default audio input/output constellation */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200319 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
320 snd_soc_dapm_enable_pin(dapm, "Earpiece");
321 snd_soc_dapm_enable_pin(dapm, "Microphone");
322 snd_soc_dapm_disable_pin(dapm, "Speaker");
323 snd_soc_dapm_disable_pin(dapm, "AGCIN");
324 snd_soc_dapm_sync(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200325}
326
327/* Line discipline .hangup() */
328static int cx81801_hangup(struct tty_struct *tty)
329{
330 cx81801_close(tty);
331 return 0;
332}
333
Joe Perchesdbc62212011-06-23 11:39:19 -0700334/* Line discipline .receive_buf() */
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200335static void cx81801_receive(struct tty_struct *tty,
336 const unsigned char *cp, char *fp, int count)
337{
338 struct snd_soc_codec *codec = tty->disc_data;
339 const unsigned char *c;
340 int apply, ret;
341
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000342 if (!codec)
343 return;
344
345 if (!codec->hw_write) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200346 /* First modem response, complete setup procedure */
347
348 /* Initialize timer used for config pulse generation */
349 setup_timer(&cx81801_timer, cx81801_timeout, 0);
350
351 v253_ops.receive_buf(tty, cp, fp, count);
352
353 /* Link hook switch to DAPM pins */
354 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
355 ARRAY_SIZE(ams_delta_hook_switch_pins),
356 ams_delta_hook_switch_pins);
357 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000358 dev_warn(codec->dev,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200359 "Failed to link hook switch to DAPM pins, "
360 "will continue with hook switch unlinked.\n");
361
362 return;
363 }
364
365 v253_ops.receive_buf(tty, cp, fp, count);
366
367 for (c = &cp[count - 1]; c >= cp; c--) {
368 if (*c != '\r')
369 continue;
370 /* Complete modem response received, apply config to codec */
371
372 spin_lock_bh(&ams_delta_lock);
373 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
374 apply = !ams_delta_muted && !cx81801_cmd_pending;
375 cx81801_cmd_pending = 1;
376 spin_unlock_bh(&ams_delta_lock);
377
378 /* Apply config pulse by connecting the codec to the modem
379 * if not already done */
380 if (apply)
381 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
382 AMS_DELTA_LATCH2_MODEM_CODEC);
383 break;
384 }
385}
386
387/* Line discipline .write_wakeup() */
388static void cx81801_wakeup(struct tty_struct *tty)
389{
390 v253_ops.write_wakeup(tty);
391}
392
393static struct tty_ldisc_ops cx81801_ops = {
394 .magic = TTY_LDISC_MAGIC,
395 .name = "cx81801",
396 .owner = THIS_MODULE,
397 .open = cx81801_open,
398 .close = cx81801_close,
399 .hangup = cx81801_hangup,
400 .receive_buf = cx81801_receive,
401 .write_wakeup = cx81801_wakeup,
402};
403
404
405/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300406 * Even if not very useful, the sound card can still work without any of the
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200407 * above functonality activated. You can still control its audio input/output
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300408 * constellation and speakerphone gain from userspace by issuing AT commands
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200409 * over the modem port.
410 */
411
412static int ams_delta_hw_params(struct snd_pcm_substream *substream,
413 struct snd_pcm_hw_params *params)
414{
415 struct snd_soc_pcm_runtime *rtd = substream->private_data;
416
417 /* Set cpu DAI configuration */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000418 return snd_soc_dai_set_fmt(rtd->cpu_dai,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200419 SND_SOC_DAIFMT_DSP_A |
420 SND_SOC_DAIFMT_NB_NF |
421 SND_SOC_DAIFMT_CBM_CFM);
422}
423
424static struct snd_soc_ops ams_delta_ops = {
425 .hw_params = ams_delta_hw_params,
426};
427
428
429/* Board specific codec bias level control */
430static int ams_delta_set_bias_level(struct snd_soc_card *card,
Mark Brownd4c60052011-06-06 19:13:23 +0100431 struct snd_soc_dapm_context *dapm,
432 enum snd_soc_bias_level level)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200433{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000434 struct snd_soc_codec *codec = card->rtd->codec;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200435
436 switch (level) {
437 case SND_SOC_BIAS_ON:
438 case SND_SOC_BIAS_PREPARE:
439 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200440 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200441 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET,
442 AMS_DELTA_LATCH2_MODEM_NRESET);
443 break;
444 case SND_SOC_BIAS_OFF:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200445 if (codec->dapm.bias_level != SND_SOC_BIAS_OFF)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200446 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET,
447 0);
448 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200449 codec->dapm.bias_level = level;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200450
451 return 0;
452}
453
454/* Digital mute implemented using modem/CPU multiplexer.
455 * Shares hardware with codec config pulse generation */
456static bool ams_delta_muted = 1;
457
458static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
459{
460 int apply;
461
462 if (ams_delta_muted == mute)
463 return 0;
464
465 spin_lock_bh(&ams_delta_lock);
466 ams_delta_muted = mute;
467 apply = !cx81801_cmd_pending;
468 spin_unlock_bh(&ams_delta_lock);
469
470 if (apply)
471 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
472 mute ? AMS_DELTA_LATCH2_MODEM_CODEC : 0);
473 return 0;
474}
475
476/* Our codec DAI probably doesn't have its own .ops structure */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100477static const struct snd_soc_dai_ops ams_delta_dai_ops = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200478 .digital_mute = ams_delta_digital_mute,
479};
480
481/* Will be used if the codec ever has its own digital_mute function */
482static int ams_delta_startup(struct snd_pcm_substream *substream)
483{
484 return ams_delta_digital_mute(NULL, 0);
485}
486
487static void ams_delta_shutdown(struct snd_pcm_substream *substream)
488{
489 ams_delta_digital_mute(NULL, 1);
490}
491
492
493/*
494 * Card initialization
495 */
496
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200498{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000499 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200500 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000501 struct snd_soc_dai *codec_dai = rtd->codec_dai;
502 struct snd_soc_card *card = rtd->card;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200503 int ret;
504 /* Codec is ready, now add/activate board specific controls */
505
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000506 /* Store a pointer to the codec structure for tty ldisc use */
507 cx20442_codec = codec;
508
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200509 /* Set up digital mute if not provided by the codec */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000510 if (!codec_dai->driver->ops) {
511 codec_dai->driver->ops = &ams_delta_dai_ops;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200512 } else {
513 ams_delta_ops.startup = ams_delta_startup;
514 ams_delta_ops.shutdown = ams_delta_shutdown;
515 }
516
517 /* Set codec bias level */
Jarkko Nikulae5740442011-08-18 15:02:47 +0300518 ams_delta_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200519
520 /* Add hook switch - can be used to control the codec from userspace
521 * even if line discipline fails */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000522 ret = snd_soc_jack_new(rtd->codec, "hook_switch",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200523 SND_JACK_HEADSET, &ams_delta_hook_switch);
524 if (ret)
525 dev_warn(card->dev,
526 "Failed to allocate resources for hook switch, "
527 "will continue without one.\n");
528 else {
529 ret = snd_soc_jack_add_gpios(&ams_delta_hook_switch,
530 ARRAY_SIZE(ams_delta_hook_switch_gpios),
531 ams_delta_hook_switch_gpios);
532 if (ret)
533 dev_warn(card->dev,
534 "Failed to set up hook switch GPIO line, "
535 "will continue with hook switch inactive.\n");
536 }
537
538 /* Register optional line discipline for over the modem control */
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200539 ret = tty_register_ldisc(N_V253, &cx81801_ops);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200540 if (ret) {
541 dev_warn(card->dev,
542 "Failed to register line discipline, "
543 "will continue without any controls.\n");
544 return 0;
545 }
546
547 /* Add board specific DAPM widgets and routes */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200548 ret = snd_soc_dapm_new_controls(dapm, ams_delta_dapm_widgets,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200549 ARRAY_SIZE(ams_delta_dapm_widgets));
550 if (ret) {
551 dev_warn(card->dev,
552 "Failed to register DAPM controls, "
553 "will continue without any.\n");
554 return 0;
555 }
556
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200557 ret = snd_soc_dapm_add_routes(dapm, ams_delta_audio_map,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200558 ARRAY_SIZE(ams_delta_audio_map));
559 if (ret) {
560 dev_warn(card->dev,
561 "Failed to set up DAPM routes, "
562 "will continue with codec default map.\n");
563 return 0;
564 }
565
566 /* Set up initial pin constellation */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200567 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
568 snd_soc_dapm_enable_pin(dapm, "Earpiece");
569 snd_soc_dapm_enable_pin(dapm, "Microphone");
570 snd_soc_dapm_disable_pin(dapm, "Speaker");
571 snd_soc_dapm_disable_pin(dapm, "AGCIN");
572 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200573
574 /* Add virtual switch */
575 ret = snd_soc_add_controls(codec, ams_delta_audio_controls,
576 ARRAY_SIZE(ams_delta_audio_controls));
577 if (ret)
578 dev_warn(card->dev,
579 "Failed to register audio mode control, "
580 "will continue without it.\n");
581
582 return 0;
583}
584
585/* DAI glue - connects codec <--> CPU */
586static struct snd_soc_dai_link ams_delta_dai_link = {
587 .name = "CX20442",
588 .stream_name = "CX20442",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000589 .cpu_dai_name ="omap-mcbsp-dai.0",
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200590 .codec_dai_name = "cx20442-voice",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200591 .init = ams_delta_cx20442_init,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 .platform_name = "omap-pcm-audio",
593 .codec_name = "cx20442-codec",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200594 .ops = &ams_delta_ops,
595};
596
597/* Audio card driver */
598static struct snd_soc_card ams_delta_audio_card = {
599 .name = "AMS_DELTA",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200600 .dai_link = &ams_delta_dai_link,
601 .num_links = 1,
602 .set_bias_level = ams_delta_set_bias_level,
603};
604
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200605/* Module init/exit */
606static struct platform_device *ams_delta_audio_platform_device;
607static struct platform_device *cx20442_platform_device;
608
609static int __init ams_delta_module_init(void)
610{
611 int ret;
612
613 if (!(machine_is_ams_delta()))
614 return -ENODEV;
615
616 ams_delta_audio_platform_device =
617 platform_device_alloc("soc-audio", -1);
618 if (!ams_delta_audio_platform_device)
619 return -ENOMEM;
620
621 platform_set_drvdata(ams_delta_audio_platform_device,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000622 &ams_delta_audio_card);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200623
624 ret = platform_device_add(ams_delta_audio_platform_device);
625 if (ret)
626 goto err;
627
628 /*
629 * Codec platform device could be registered from elsewhere (board?),
630 * but I do it here as it makes sense only if used with the card.
631 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000632 cx20442_platform_device =
633 platform_device_register_simple("cx20442-codec", -1, NULL, 0);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200634 return 0;
635err:
636 platform_device_put(ams_delta_audio_platform_device);
637 return ret;
638}
639module_init(ams_delta_module_init);
640
641static void __exit ams_delta_module_exit(void)
642{
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200643 if (tty_unregister_ldisc(N_V253) != 0)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200644 dev_warn(&ams_delta_audio_platform_device->dev,
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200645 "failed to unregister V253 line discipline\n");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200646
647 snd_soc_jack_free_gpios(&ams_delta_hook_switch,
648 ARRAY_SIZE(ams_delta_hook_switch_gpios),
649 ams_delta_hook_switch_gpios);
650
651 /* Keep modem power on */
Jarkko Nikulae5740442011-08-18 15:02:47 +0300652 ams_delta_set_bias_level(&ams_delta_audio_card,
653 &ams_delta_audio_card.rtd[0].codec->dapm,
654 SND_SOC_BIAS_STANDBY);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200655
656 platform_device_unregister(cx20442_platform_device);
657 platform_device_unregister(ams_delta_audio_platform_device);
658}
659module_exit(ams_delta_module_exit);
660
661MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
662MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
663MODULE_LICENSE("GPL");