blob: 14718cd6c29f28f60ccb9f7aee433a14ef17451d [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 Lindgrene27e35e2012-09-19 10:33:43 -070035#include <mach/board-ams-delta.h>
Arnd Bergmann22037472012-08-24 15:21:06 +020036#include <linux/platform_data/asoc-ti-mcbsp.h>
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020037
38#include "omap-mcbsp.h"
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020039#include "../codecs/cx20442.h"
40
41
42/* Board specific DAPM widgets */
Janusz Krzysztofik02624622009-10-21 04:40:55 +020043static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020044 /* Handset */
45 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
46 SND_SOC_DAPM_HP("Earpiece", NULL),
47 /* Handsfree/Speakerphone */
48 SND_SOC_DAPM_MIC("Microphone", NULL),
49 SND_SOC_DAPM_SPK("Speaker", NULL),
50};
51
52/* How they are connected to codec pins */
53static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
54 {"TELIN", NULL, "Mouthpiece"},
55 {"Earpiece", NULL, "TELOUT"},
56
57 {"MIC", NULL, "Microphone"},
58 {"Speaker", NULL, "SPKOUT"},
59};
60
61/*
62 * Controls, functional after the modem line discipline is activated.
63 */
64
65/* Virtual switch: audio input/output constellations */
66static const char *ams_delta_audio_mode[] =
67 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
68
69/* Selection <-> pin translation */
70#define AMS_DELTA_MOUTHPIECE 0
71#define AMS_DELTA_EARPIECE 1
72#define AMS_DELTA_MICROPHONE 2
73#define AMS_DELTA_SPEAKER 3
74#define AMS_DELTA_AGC 4
75
76#define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
77 (1 << AMS_DELTA_MICROPHONE))
78#define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
79 (1 << AMS_DELTA_EARPIECE))
80#define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
81 (1 << AMS_DELTA_SPEAKER))
82#define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
83
Janusz Krzysztofik02624622009-10-21 04:40:55 +020084static const unsigned short ams_delta_audio_mode_pins[] = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020085 AMS_DELTA_MIXED,
86 AMS_DELTA_HANDSET,
87 AMS_DELTA_HANDSFREE,
88 AMS_DELTA_SPEAKERPHONE,
89};
90
91static unsigned short ams_delta_audio_agc;
92
93static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
94 struct snd_ctl_elem_value *ucontrol)
95{
96 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodce6120c2010-11-05 15:53:46 +020097 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +020098 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
99 unsigned short pins;
100 int pin, changed = 0;
101
102 /* Refuse any mode changes if we are not able to control the codec. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000103 if (!codec->hw_write)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200104 return -EUNATCH;
105
106 if (ucontrol->value.enumerated.item[0] >= control->max)
107 return -EINVAL;
108
Charles Keepax03510ca2014-02-18 15:22:22 +0000109 snd_soc_dapm_mutex_lock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200110
111 /* Translate selection to bitmap */
112 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
113
114 /* Setup pins after corresponding bits if changed */
115 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
Charles Keepax03510ca2014-02-18 15:22:22 +0000116
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)
Charles Keepax03510ca2014-02-18 15:22:22 +0000120 snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200121 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000122 snd_soc_dapm_disable_pin_unlocked(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)
Charles Keepax03510ca2014-02-18 15:22:22 +0000128 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200129 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000130 snd_soc_dapm_disable_pin_unlocked(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)
Charles Keepax03510ca2014-02-18 15:22:22 +0000136 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200137 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000138 snd_soc_dapm_disable_pin_unlocked(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)
Charles Keepax03510ca2014-02-18 15:22:22 +0000144 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200145 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000146 snd_soc_dapm_disable_pin_unlocked(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)
Charles Keepax03510ca2014-02-18 15:22:22 +0000153 snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200154 else
Charles Keepax03510ca2014-02-18 15:22:22 +0000155 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200156 }
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200157
Charles Keepax03510ca2014-02-18 15:22:22 +0000158 if (changed)
159 snd_soc_dapm_sync_unlocked(dapm);
160
161 snd_soc_dapm_mutex_unlock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200162
163 return changed;
164}
165
166static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
168{
169 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200170 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200171 unsigned short pins, mode;
172
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200173 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200174 AMS_DELTA_MOUTHPIECE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200175 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200176 AMS_DELTA_EARPIECE));
177 if (pins)
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200178 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200179 AMS_DELTA_MICROPHONE);
180 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200181 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200182 AMS_DELTA_MICROPHONE) |
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200183 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200184 AMS_DELTA_SPEAKER) |
185 (ams_delta_audio_agc << AMS_DELTA_AGC));
186
187 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
188 if (pins == ams_delta_audio_mode_pins[mode])
189 break;
190
191 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
192 return -EINVAL;
193
194 ucontrol->value.enumerated.item[0] = mode;
195
196 return 0;
197}
198
199static const struct soc_enum ams_delta_audio_enum[] = {
200 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ams_delta_audio_mode),
201 ams_delta_audio_mode),
202};
203
204static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
205 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum[0],
206 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
207};
208
209/* Hook switch */
210static struct snd_soc_jack ams_delta_hook_switch;
211static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
212 {
213 .gpio = 4,
214 .name = "hook_switch",
215 .report = SND_JACK_HEADSET,
216 .invert = 1,
217 .debounce_time = 150,
218 }
219};
220
221/* After we are able to control the codec over the modem,
222 * the hook switch can be used for dynamic DAPM reconfiguration. */
223static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
224 /* Handset */
225 {
226 .pin = "Mouthpiece",
227 .mask = SND_JACK_MICROPHONE,
228 },
229 {
230 .pin = "Earpiece",
231 .mask = SND_JACK_HEADPHONE,
232 },
233 /* Handsfree */
234 {
235 .pin = "Microphone",
236 .mask = SND_JACK_MICROPHONE,
237 .invert = 1,
238 },
239 {
240 .pin = "Speaker",
241 .mask = SND_JACK_HEADPHONE,
242 .invert = 1,
243 },
244};
245
246
247/*
248 * Modem line discipline, required for making above controls functional.
249 * Activated from userspace with ldattach, possibly invoked from udev rule.
250 */
251
252/* To actually apply any modem controlled configuration changes to the codec,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300253 * we must connect codec DAI pins to the modem for a moment. Be careful not
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200254 * to interfere with our digital mute function that shares the same hardware. */
255static struct timer_list cx81801_timer;
256static bool cx81801_cmd_pending;
257static bool ams_delta_muted;
258static DEFINE_SPINLOCK(ams_delta_lock);
259
260static void cx81801_timeout(unsigned long data)
261{
262 int muted;
263
264 spin_lock(&ams_delta_lock);
265 cx81801_cmd_pending = 0;
266 muted = ams_delta_muted;
267 spin_unlock(&ams_delta_lock);
268
269 /* Reconnect the codec DAI back from the modem to the CPU DAI
270 * only if digital mute still off */
271 if (!muted)
272 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC, 0);
273}
274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275/*
276 * Used for passing a codec structure pointer
277 * from the board initialization code to the tty line discipline.
278 */
279static struct snd_soc_codec *cx20442_codec;
280
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200281/* Line discipline .open() */
282static int cx81801_open(struct tty_struct *tty)
283{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284 int ret;
285
286 if (!cx20442_codec)
287 return -ENODEV;
288
289 /*
290 * Pass the codec structure pointer for use by other ldisc callbacks,
291 * both the card and the codec specific parts.
292 */
293 tty->disc_data = cx20442_codec;
294
295 ret = v253_ops.open(tty);
296
297 if (ret < 0)
298 tty->disc_data = NULL;
299
300 return ret;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200301}
302
303/* Line discipline .close() */
304static void cx81801_close(struct tty_struct *tty)
305{
306 struct snd_soc_codec *codec = tty->disc_data;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200307 struct snd_soc_dapm_context *dapm = &codec->dapm;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200308
309 del_timer_sync(&cx81801_timer);
310
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200311 /* Prevent the hook switch from further changing the DAPM pins */
312 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
313
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000314 if (!codec)
315 return;
316
317 v253_ops.close(tty);
318
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200319 /* Revert back to default audio input/output constellation */
Charles Keepax03510ca2014-02-18 15:22:22 +0000320 snd_soc_dapm_mutex_lock(dapm);
321
322 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
323 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
324 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
325 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
326 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
327
328 snd_soc_dapm_sync_unlocked(dapm);
329
Lars-Peter Clausene95d73c2014-03-12 15:27:29 +0100330 snd_soc_dapm_mutex_unlock(dapm);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200331}
332
333/* Line discipline .hangup() */
334static int cx81801_hangup(struct tty_struct *tty)
335{
336 cx81801_close(tty);
337 return 0;
338}
339
Joe Perchesdbc62212011-06-23 11:39:19 -0700340/* Line discipline .receive_buf() */
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200341static void cx81801_receive(struct tty_struct *tty,
342 const unsigned char *cp, char *fp, int count)
343{
344 struct snd_soc_codec *codec = tty->disc_data;
345 const unsigned char *c;
346 int apply, ret;
347
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000348 if (!codec)
349 return;
350
351 if (!codec->hw_write) {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200352 /* First modem response, complete setup procedure */
353
354 /* Initialize timer used for config pulse generation */
355 setup_timer(&cx81801_timer, cx81801_timeout, 0);
356
357 v253_ops.receive_buf(tty, cp, fp, count);
358
359 /* Link hook switch to DAPM pins */
360 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
361 ARRAY_SIZE(ams_delta_hook_switch_pins),
362 ams_delta_hook_switch_pins);
363 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000364 dev_warn(codec->dev,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200365 "Failed to link hook switch to DAPM pins, "
366 "will continue with hook switch unlinked.\n");
367
368 return;
369 }
370
371 v253_ops.receive_buf(tty, cp, fp, count);
372
373 for (c = &cp[count - 1]; c >= cp; c--) {
374 if (*c != '\r')
375 continue;
376 /* Complete modem response received, apply config to codec */
377
378 spin_lock_bh(&ams_delta_lock);
379 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
380 apply = !ams_delta_muted && !cx81801_cmd_pending;
381 cx81801_cmd_pending = 1;
382 spin_unlock_bh(&ams_delta_lock);
383
384 /* Apply config pulse by connecting the codec to the modem
385 * if not already done */
386 if (apply)
387 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
388 AMS_DELTA_LATCH2_MODEM_CODEC);
389 break;
390 }
391}
392
393/* Line discipline .write_wakeup() */
394static void cx81801_wakeup(struct tty_struct *tty)
395{
396 v253_ops.write_wakeup(tty);
397}
398
399static struct tty_ldisc_ops cx81801_ops = {
400 .magic = TTY_LDISC_MAGIC,
401 .name = "cx81801",
402 .owner = THIS_MODULE,
403 .open = cx81801_open,
404 .close = cx81801_close,
405 .hangup = cx81801_hangup,
406 .receive_buf = cx81801_receive,
407 .write_wakeup = cx81801_wakeup,
408};
409
410
411/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300412 * Even if not very useful, the sound card can still work without any of the
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200413 * above functonality activated. You can still control its audio input/output
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300414 * constellation and speakerphone gain from userspace by issuing AT commands
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200415 * over the modem port.
416 */
417
418static int ams_delta_hw_params(struct snd_pcm_substream *substream,
419 struct snd_pcm_hw_params *params)
420{
421 struct snd_soc_pcm_runtime *rtd = substream->private_data;
422
423 /* Set cpu DAI configuration */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000424 return snd_soc_dai_set_fmt(rtd->cpu_dai,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200425 SND_SOC_DAIFMT_DSP_A |
426 SND_SOC_DAIFMT_NB_NF |
427 SND_SOC_DAIFMT_CBM_CFM);
428}
429
430static struct snd_soc_ops ams_delta_ops = {
431 .hw_params = ams_delta_hw_params,
432};
433
434
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200435/* Digital mute implemented using modem/CPU multiplexer.
436 * Shares hardware with codec config pulse generation */
437static bool ams_delta_muted = 1;
438
439static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
440{
441 int apply;
442
443 if (ams_delta_muted == mute)
444 return 0;
445
446 spin_lock_bh(&ams_delta_lock);
447 ams_delta_muted = mute;
448 apply = !cx81801_cmd_pending;
449 spin_unlock_bh(&ams_delta_lock);
450
451 if (apply)
452 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
453 mute ? AMS_DELTA_LATCH2_MODEM_CODEC : 0);
454 return 0;
455}
456
457/* Our codec DAI probably doesn't have its own .ops structure */
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100458static const struct snd_soc_dai_ops ams_delta_dai_ops = {
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200459 .digital_mute = ams_delta_digital_mute,
460};
461
462/* Will be used if the codec ever has its own digital_mute function */
463static int ams_delta_startup(struct snd_pcm_substream *substream)
464{
465 return ams_delta_digital_mute(NULL, 0);
466}
467
468static void ams_delta_shutdown(struct snd_pcm_substream *substream)
469{
470 ams_delta_digital_mute(NULL, 1);
471}
472
473
474/*
475 * Card initialization
476 */
477
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000478static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200479{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000480 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200481 struct snd_soc_dapm_context *dapm = &codec->dapm;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000482 struct snd_soc_dai *codec_dai = rtd->codec_dai;
483 struct snd_soc_card *card = rtd->card;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200484 int ret;
485 /* Codec is ready, now add/activate board specific controls */
486
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000487 /* Store a pointer to the codec structure for tty ldisc use */
488 cx20442_codec = codec;
489
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200490 /* Set up digital mute if not provided by the codec */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000491 if (!codec_dai->driver->ops) {
492 codec_dai->driver->ops = &ams_delta_dai_ops;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200493 } else {
494 ams_delta_ops.startup = ams_delta_startup;
495 ams_delta_ops.shutdown = ams_delta_shutdown;
496 }
497
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200498 /* Add hook switch - can be used to control the codec from userspace
499 * even if line discipline fails */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000500 ret = snd_soc_jack_new(rtd->codec, "hook_switch",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200501 SND_JACK_HEADSET, &ams_delta_hook_switch);
502 if (ret)
503 dev_warn(card->dev,
504 "Failed to allocate resources for hook switch, "
505 "will continue without one.\n");
506 else {
507 ret = snd_soc_jack_add_gpios(&ams_delta_hook_switch,
508 ARRAY_SIZE(ams_delta_hook_switch_gpios),
509 ams_delta_hook_switch_gpios);
510 if (ret)
511 dev_warn(card->dev,
512 "Failed to set up hook switch GPIO line, "
513 "will continue with hook switch inactive.\n");
514 }
515
516 /* Register optional line discipline for over the modem control */
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200517 ret = tty_register_ldisc(N_V253, &cx81801_ops);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200518 if (ret) {
519 dev_warn(card->dev,
520 "Failed to register line discipline, "
521 "will continue without any controls.\n");
522 return 0;
523 }
524
525 /* Add board specific DAPM widgets and routes */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200526 ret = snd_soc_dapm_new_controls(dapm, ams_delta_dapm_widgets,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200527 ARRAY_SIZE(ams_delta_dapm_widgets));
528 if (ret) {
529 dev_warn(card->dev,
530 "Failed to register DAPM controls, "
531 "will continue without any.\n");
532 return 0;
533 }
534
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200535 ret = snd_soc_dapm_add_routes(dapm, ams_delta_audio_map,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200536 ARRAY_SIZE(ams_delta_audio_map));
537 if (ret) {
538 dev_warn(card->dev,
539 "Failed to set up DAPM routes, "
540 "will continue with codec default map.\n");
541 return 0;
542 }
543
544 /* Set up initial pin constellation */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200545 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
546 snd_soc_dapm_enable_pin(dapm, "Earpiece");
547 snd_soc_dapm_enable_pin(dapm, "Microphone");
548 snd_soc_dapm_disable_pin(dapm, "Speaker");
549 snd_soc_dapm_disable_pin(dapm, "AGCIN");
550 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200551
552 /* Add virtual switch */
Liam Girdwood022658b2012-02-03 17:43:09 +0000553 ret = snd_soc_add_codec_controls(codec, ams_delta_audio_controls,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200554 ARRAY_SIZE(ams_delta_audio_controls));
555 if (ret)
556 dev_warn(card->dev,
557 "Failed to register audio mode control, "
558 "will continue without it.\n");
559
560 return 0;
561}
562
563/* DAI glue - connects codec <--> CPU */
564static struct snd_soc_dai_link ams_delta_dai_link = {
565 .name = "CX20442",
566 .stream_name = "CX20442",
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200567 .cpu_dai_name = "omap-mcbsp.1",
Janusz Krzysztofik53946372010-08-19 15:15:50 +0200568 .codec_dai_name = "cx20442-voice",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200569 .init = ams_delta_cx20442_init,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 .platform_name = "omap-pcm-audio",
571 .codec_name = "cx20442-codec",
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200572 .ops = &ams_delta_ops,
573};
574
575/* Audio card driver */
576static struct snd_soc_card ams_delta_audio_card = {
577 .name = "AMS_DELTA",
Axel Linb425b882011-12-22 11:08:59 +0800578 .owner = THIS_MODULE,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200579 .dai_link = &ams_delta_dai_link,
580 .num_links = 1,
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200581};
582
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200583/* Module init/exit */
Bill Pemberton7ff60002012-12-07 09:26:29 -0500584static int ams_delta_probe(struct platform_device *pdev)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200585{
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200586 struct snd_soc_card *card = &ams_delta_audio_card;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200587 int ret;
588
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200589 card->dev = &pdev->dev;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200590
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200591 ret = snd_soc_register_card(card);
592 if (ret) {
593 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
594 card->dev = NULL;
595 return ret;
596 }
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200597 return 0;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200598}
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200599
Bill Pemberton7ff60002012-12-07 09:26:29 -0500600static int ams_delta_remove(struct platform_device *pdev)
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200601{
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200602 struct snd_soc_card *card = platform_get_drvdata(pdev);
603
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200604 if (tty_unregister_ldisc(N_V253) != 0)
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200605 dev_warn(&pdev->dev,
Janusz Krzysztofikb7b8f9b2009-08-06 13:07:32 +0200606 "failed to unregister V253 line discipline\n");
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200607
608 snd_soc_jack_free_gpios(&ams_delta_hook_switch,
609 ARRAY_SIZE(ams_delta_hook_switch_gpios),
610 ams_delta_hook_switch_gpios);
611
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200612 snd_soc_unregister_card(card);
613 card->dev = NULL;
614 return 0;
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200615}
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200616
617#define DRV_NAME "ams-delta-audio"
618
619static struct platform_driver ams_delta_driver = {
620 .driver = {
621 .name = DRV_NAME,
622 .owner = THIS_MODULE,
623 },
624 .probe = ams_delta_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500625 .remove = ams_delta_remove,
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200626};
627
628module_platform_driver(ams_delta_driver);
Janusz Krzysztofik6d7f68a2009-07-29 13:18:53 +0200629
630MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
631MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
632MODULE_LICENSE("GPL");
Janusz Krzysztofikb764de22012-10-03 12:46:57 +0200633MODULE_ALIAS("platform:" DRV_NAME);