1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/gpio.h>
26#include <linux/spinlock.h>
27#include <linux/tty.h>
28#include <linux/module.h>
29
30#include <sound/soc.h>
31#include <sound/jack.h>
32
33#include <asm/mach-types.h>
34
35#include <mach/board-ams-delta.h>
36#include <linux/platform_data/asoc-ti-mcbsp.h>
37
38#include "omap-mcbsp.h"
39#include "../codecs/cx20442.h"
40
41
42static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
43
44 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
45 SND_SOC_DAPM_HP("Earpiece", NULL),
46
47 SND_SOC_DAPM_MIC("Microphone", NULL),
48 SND_SOC_DAPM_SPK("Speaker", NULL),
49};
50
51
52static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
53 {"TELIN", NULL, "Mouthpiece"},
54 {"Earpiece", NULL, "TELOUT"},
55
56 {"MIC", NULL, "Microphone"},
57 {"Speaker", NULL, "SPKOUT"},
58};
59
60
61
62
63
64
65static const char *ams_delta_audio_mode[] =
66 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
67
68
69#define AMS_DELTA_MOUTHPIECE 0
70#define AMS_DELTA_EARPIECE 1
71#define AMS_DELTA_MICROPHONE 2
72#define AMS_DELTA_SPEAKER 3
73#define AMS_DELTA_AGC 4
74
75#define AMS_DELTA_MIXED ((1 << AMS_DELTA_EARPIECE) | \
76 (1 << AMS_DELTA_MICROPHONE))
77#define AMS_DELTA_HANDSET ((1 << AMS_DELTA_MOUTHPIECE) | \
78 (1 << AMS_DELTA_EARPIECE))
79#define AMS_DELTA_HANDSFREE ((1 << AMS_DELTA_MICROPHONE) | \
80 (1 << AMS_DELTA_SPEAKER))
81#define AMS_DELTA_SPEAKERPHONE (AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
82
83static const unsigned short ams_delta_audio_mode_pins[] = {
84 AMS_DELTA_MIXED,
85 AMS_DELTA_HANDSET,
86 AMS_DELTA_HANDSFREE,
87 AMS_DELTA_SPEAKERPHONE,
88};
89
90static unsigned short ams_delta_audio_agc;
91
92
93
94
95
96static struct snd_soc_codec *cx20442_codec;
97
98static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
99 struct snd_ctl_elem_value *ucontrol)
100{
101 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
102 struct snd_soc_dapm_context *dapm = &card->dapm;
103 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
104 unsigned short pins;
105 int pin, changed = 0;
106
107
108 if (!cx20442_codec->component.card->pop_time)
109 return -EUNATCH;
110
111 if (ucontrol->value.enumerated.item[0] >= control->items)
112 return -EINVAL;
113
114 snd_soc_dapm_mutex_lock(dapm);
115
116
117 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
118
119
120 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
121
122 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
123 changed = 1;
124 if (pin)
125 snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
126 else
127 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
128 }
129 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
130 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
131 changed = 1;
132 if (pin)
133 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
134 else
135 snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece");
136 }
137 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
138 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
139 changed = 1;
140 if (pin)
141 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
142 else
143 snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone");
144 }
145 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
146 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
147 changed = 1;
148 if (pin)
149 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
150 else
151 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
152 }
153 pin = !!(pins & (1 << AMS_DELTA_AGC));
154 if (pin != ams_delta_audio_agc) {
155 ams_delta_audio_agc = pin;
156 changed = 1;
157 if (pin)
158 snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
159 else
160 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
161 }
162
163 if (changed)
164 snd_soc_dapm_sync_unlocked(dapm);
165
166 snd_soc_dapm_mutex_unlock(dapm);
167
168 return changed;
169}
170
171static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *ucontrol)
173{
174 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
175 struct snd_soc_dapm_context *dapm = &card->dapm;
176 unsigned short pins, mode;
177
178 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
179 AMS_DELTA_MOUTHPIECE) |
180 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
181 AMS_DELTA_EARPIECE));
182 if (pins)
183 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
184 AMS_DELTA_MICROPHONE);
185 else
186 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
187 AMS_DELTA_MICROPHONE) |
188 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
189 AMS_DELTA_SPEAKER) |
190 (ams_delta_audio_agc << AMS_DELTA_AGC));
191
192 for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
193 if (pins == ams_delta_audio_mode_pins[mode])
194 break;
195
196 if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
197 return -EINVAL;
198
199 ucontrol->value.enumerated.item[0] = mode;
200
201 return 0;
202}
203
204static const SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
205 ams_delta_audio_mode);
206
207static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
208 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum,
209 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
210};
211
212
213static struct snd_soc_jack ams_delta_hook_switch;
214static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
215 {
216 .gpio = 4,
217 .name = "hook_switch",
218 .report = SND_JACK_HEADSET,
219 .invert = 1,
220 .debounce_time = 150,
221 }
222};
223
224
225
226static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
227
228 {
229 .pin = "Mouthpiece",
230 .mask = SND_JACK_MICROPHONE,
231 },
232 {
233 .pin = "Earpiece",
234 .mask = SND_JACK_HEADPHONE,
235 },
236
237 {
238 .pin = "Microphone",
239 .mask = SND_JACK_MICROPHONE,
240 .invert = 1,
241 },
242 {
243 .pin = "Speaker",
244 .mask = SND_JACK_HEADPHONE,
245 .invert = 1,
246 },
247};
248
249
250
251
252
253
254
255
256
257
258static struct timer_list cx81801_timer;
259static bool cx81801_cmd_pending;
260static bool ams_delta_muted;
261static DEFINE_SPINLOCK(ams_delta_lock);
262
263static void cx81801_timeout(struct timer_list *unused)
264{
265 int muted;
266
267 spin_lock(&ams_delta_lock);
268 cx81801_cmd_pending = 0;
269 muted = ams_delta_muted;
270 spin_unlock(&ams_delta_lock);
271
272
273
274 if (!muted)
275 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC, 0);
276}
277
278
279static int cx81801_open(struct tty_struct *tty)
280{
281 int ret;
282
283 if (!cx20442_codec)
284 return -ENODEV;
285
286
287
288
289
290 tty->disc_data = cx20442_codec;
291
292 ret = v253_ops.open(tty);
293
294 if (ret < 0)
295 tty->disc_data = NULL;
296
297 return ret;
298}
299
300
301static void cx81801_close(struct tty_struct *tty)
302{
303 struct snd_soc_codec *codec = tty->disc_data;
304 struct snd_soc_dapm_context *dapm = &codec->component.card->dapm;
305
306 del_timer_sync(&cx81801_timer);
307
308
309 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
310
311 if (!codec)
312 return;
313
314 v253_ops.close(tty);
315
316
317 snd_soc_dapm_mutex_lock(dapm);
318
319 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
320 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
321 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
322 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
323 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
324
325 snd_soc_dapm_sync_unlocked(dapm);
326
327 snd_soc_dapm_mutex_unlock(dapm);
328}
329
330
331static int cx81801_hangup(struct tty_struct *tty)
332{
333 cx81801_close(tty);
334 return 0;
335}
336
337
338static void cx81801_receive(struct tty_struct *tty,
339 const unsigned char *cp, char *fp, int count)
340{
341 struct snd_soc_codec *codec = tty->disc_data;
342 const unsigned char *c;
343 int apply, ret;
344
345 if (!codec)
346 return;
347
348 if (!codec->component.card->pop_time) {
349
350
351
352 timer_setup(&cx81801_timer, cx81801_timeout, 0);
353
354 v253_ops.receive_buf(tty, cp, fp, count);
355
356
357 ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
358 ARRAY_SIZE(ams_delta_hook_switch_pins),
359 ams_delta_hook_switch_pins);
360 if (ret)
361 dev_warn(codec->dev,
362 "Failed to link hook switch to DAPM pins, "
363 "will continue with hook switch unlinked.\n");
364
365 return;
366 }
367
368 v253_ops.receive_buf(tty, cp, fp, count);
369
370 for (c = &cp[count - 1]; c >= cp; c--) {
371 if (*c != '\r')
372 continue;
373
374
375 spin_lock_bh(&ams_delta_lock);
376 mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
377 apply = !ams_delta_muted && !cx81801_cmd_pending;
378 cx81801_cmd_pending = 1;
379 spin_unlock_bh(&ams_delta_lock);
380
381
382
383 if (apply)
384 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
385 AMS_DELTA_LATCH2_MODEM_CODEC);
386 break;
387 }
388}
389
390
391static void cx81801_wakeup(struct tty_struct *tty)
392{
393 v253_ops.write_wakeup(tty);
394}
395
396static struct tty_ldisc_ops cx81801_ops = {
397 .magic = TTY_LDISC_MAGIC,
398 .name = "cx81801",
399 .owner = THIS_MODULE,
400 .open = cx81801_open,
401 .close = cx81801_close,
402 .hangup = cx81801_hangup,
403 .receive_buf = cx81801_receive,
404 .write_wakeup = cx81801_wakeup,
405};
406
407
408
409
410
411
412
413
414
415static struct snd_soc_ops ams_delta_ops;
416
417
418
419
420static bool ams_delta_muted = 1;
421
422static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
423{
424 int apply;
425
426 if (ams_delta_muted == mute)
427 return 0;
428
429 spin_lock_bh(&ams_delta_lock);
430 ams_delta_muted = mute;
431 apply = !cx81801_cmd_pending;
432 spin_unlock_bh(&ams_delta_lock);
433
434 if (apply)
435 ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
436 mute ? AMS_DELTA_LATCH2_MODEM_CODEC : 0);
437 return 0;
438}
439
440
441static const struct snd_soc_dai_ops ams_delta_dai_ops = {
442 .digital_mute = ams_delta_digital_mute,
443};
444
445
446static int ams_delta_startup(struct snd_pcm_substream *substream)
447{
448 return ams_delta_digital_mute(NULL, 0);
449}
450
451static void ams_delta_shutdown(struct snd_pcm_substream *substream)
452{
453 ams_delta_digital_mute(NULL, 1);
454}
455
456
457
458
459
460
461static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
462{
463 struct snd_soc_dai *codec_dai = rtd->codec_dai;
464 struct snd_soc_card *card = rtd->card;
465 struct snd_soc_dapm_context *dapm = &card->dapm;
466 int ret;
467
468
469
470 cx20442_codec = rtd->codec;
471
472
473 if (!codec_dai->driver->ops) {
474 codec_dai->driver->ops = &ams_delta_dai_ops;
475 } else {
476 ams_delta_ops.startup = ams_delta_startup;
477 ams_delta_ops.shutdown = ams_delta_shutdown;
478 }
479
480
481
482 ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET,
483 &ams_delta_hook_switch, NULL, 0);
484 if (ret)
485 dev_warn(card->dev,
486 "Failed to allocate resources for hook switch, "
487 "will continue without one.\n");
488 else {
489 ret = snd_soc_jack_add_gpios(&ams_delta_hook_switch,
490 ARRAY_SIZE(ams_delta_hook_switch_gpios),
491 ams_delta_hook_switch_gpios);
492 if (ret)
493 dev_warn(card->dev,
494 "Failed to set up hook switch GPIO line, "
495 "will continue with hook switch inactive.\n");
496 }
497
498
499 ret = tty_register_ldisc(N_V253, &cx81801_ops);
500 if (ret) {
501 dev_warn(card->dev,
502 "Failed to register line discipline, "
503 "will continue without any controls.\n");
504 return 0;
505 }
506
507
508 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
509 snd_soc_dapm_disable_pin(dapm, "Speaker");
510 snd_soc_dapm_disable_pin(dapm, "AGCIN");
511 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
512
513 return 0;
514}
515
516
517static struct snd_soc_dai_link ams_delta_dai_link = {
518 .name = "CX20442",
519 .stream_name = "CX20442",
520 .cpu_dai_name = "omap-mcbsp.1",
521 .codec_dai_name = "cx20442-voice",
522 .init = ams_delta_cx20442_init,
523 .platform_name = "omap-mcbsp.1",
524 .codec_name = "cx20442-codec",
525 .ops = &ams_delta_ops,
526 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
527 SND_SOC_DAIFMT_CBM_CFM,
528};
529
530
531static struct snd_soc_card ams_delta_audio_card = {
532 .name = "AMS_DELTA",
533 .owner = THIS_MODULE,
534 .dai_link = &ams_delta_dai_link,
535 .num_links = 1,
536
537 .controls = ams_delta_audio_controls,
538 .num_controls = ARRAY_SIZE(ams_delta_audio_controls),
539 .dapm_widgets = ams_delta_dapm_widgets,
540 .num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
541 .dapm_routes = ams_delta_audio_map,
542 .num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
543};
544
545
546static int ams_delta_probe(struct platform_device *pdev)
547{
548 struct snd_soc_card *card = &ams_delta_audio_card;
549 int ret;
550
551 card->dev = &pdev->dev;
552
553 ret = snd_soc_register_card(card);
554 if (ret) {
555 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
556 card->dev = NULL;
557 return ret;
558 }
559 return 0;
560}
561
562static int ams_delta_remove(struct platform_device *pdev)
563{
564 struct snd_soc_card *card = platform_get_drvdata(pdev);
565
566 if (tty_unregister_ldisc(N_V253) != 0)
567 dev_warn(&pdev->dev,
568 "failed to unregister V253 line discipline\n");
569
570 snd_soc_unregister_card(card);
571 card->dev = NULL;
572 return 0;
573}
574
575#define DRV_NAME "ams-delta-audio"
576
577static struct platform_driver ams_delta_driver = {
578 .driver = {
579 .name = DRV_NAME,
580 },
581 .probe = ams_delta_probe,
582 .remove = ams_delta_remove,
583};
584
585module_platform_driver(ams_delta_driver);
586
587MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
588MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
589MODULE_LICENSE("GPL");
590MODULE_ALIAS("platform:" DRV_NAME);
591