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