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/consumer.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 <linux/platform_data/asoc-ti-mcbsp.h>
36
37#include "omap-mcbsp.h"
38#include "../codecs/cx20442.h"
39
40
41static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
42
43 SND_SOC_DAPM_MIC("Mouthpiece", NULL),
44 SND_SOC_DAPM_HP("Earpiece", NULL),
45
46 SND_SOC_DAPM_MIC("Microphone", NULL),
47 SND_SOC_DAPM_SPK("Speaker", NULL),
48};
49
50
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
61
62
63
64static const char *ams_delta_audio_mode[] =
65 {"Mixed", "Handset", "Handsfree", "Speakerphone"};
66
67
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
82static const unsigned short ams_delta_audio_mode_pins[] = {
83 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
91
92
93
94
95static struct snd_soc_component *cx20442_codec;
96
97static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
98 struct snd_ctl_elem_value *ucontrol)
99{
100 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
101 struct snd_soc_dapm_context *dapm = &card->dapm;
102 struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
103 unsigned short pins;
104 int pin, changed = 0;
105
106
107 if (!cx20442_codec->card->pop_time)
108 return -EUNATCH;
109
110 if (ucontrol->value.enumerated.item[0] >= control->items)
111 return -EINVAL;
112
113 snd_soc_dapm_mutex_lock(dapm);
114
115
116 pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
117
118
119 pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
120
121 if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
122 changed = 1;
123 if (pin)
124 snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
125 else
126 snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
127 }
128 pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
129 if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
130 changed = 1;
131 if (pin)
132 snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
133 else
134 snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece");
135 }
136 pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
137 if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
138 changed = 1;
139 if (pin)
140 snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
141 else
142 snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone");
143 }
144 pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
145 if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
146 changed = 1;
147 if (pin)
148 snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
149 else
150 snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
151 }
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)
157 snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
158 else
159 snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
160 }
161
162 if (changed)
163 snd_soc_dapm_sync_unlocked(dapm);
164
165 snd_soc_dapm_mutex_unlock(dapm);
166
167 return changed;
168}
169
170static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_value *ucontrol)
172{
173 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
174 struct snd_soc_dapm_context *dapm = &card->dapm;
175 unsigned short pins, mode;
176
177 pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
178 AMS_DELTA_MOUTHPIECE) |
179 (snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
180 AMS_DELTA_EARPIECE));
181 if (pins)
182 pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
183 AMS_DELTA_MICROPHONE);
184 else
185 pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
186 AMS_DELTA_MICROPHONE) |
187 (snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
188 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
203static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
204 ams_delta_audio_mode);
205
206static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
207 SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum,
208 ams_delta_get_audio_mode, ams_delta_set_audio_mode),
209};
210
211
212static struct snd_soc_jack ams_delta_hook_switch;
213static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
214 {
215 .name = "hook_switch",
216 .report = SND_JACK_HEADSET,
217 .invert = 1,
218 .debounce_time = 150,
219 }
220};
221
222
223
224static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
225
226 {
227 .pin = "Mouthpiece",
228 .mask = SND_JACK_MICROPHONE,
229 },
230 {
231 .pin = "Earpiece",
232 .mask = SND_JACK_HEADPHONE,
233 },
234
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
250
251
252
253
254
255
256static struct timer_list cx81801_timer;
257static bool cx81801_cmd_pending;
258static bool ams_delta_muted;
259static DEFINE_SPINLOCK(ams_delta_lock);
260static struct gpio_desc *gpiod_modem_codec;
261
262static void cx81801_timeout(struct timer_list *unused)
263{
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
272
273 if (!muted)
274 gpiod_set_value(gpiod_modem_codec, 0);
275}
276
277
278static int cx81801_open(struct tty_struct *tty)
279{
280 int ret;
281
282 if (!cx20442_codec)
283 return -ENODEV;
284
285
286
287
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;
297}
298
299
300static void cx81801_close(struct tty_struct *tty)
301{
302 struct snd_soc_component *component = tty->disc_data;
303 struct snd_soc_dapm_context *dapm = &component->card->dapm;
304
305 del_timer_sync(&cx81801_timer);
306
307
308 INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
309
310 if (!component)
311 return;
312
313 v253_ops.close(tty);
314
315
316 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
326 snd_soc_dapm_mutex_unlock(dapm);
327}
328
329
330static int cx81801_hangup(struct tty_struct *tty)
331{
332 cx81801_close(tty);
333 return 0;
334}
335
336
337static void cx81801_receive(struct tty_struct *tty,
338 const unsigned char *cp, char *fp, int count)
339{
340 struct snd_soc_component *component = tty->disc_data;
341 const unsigned char *c;
342 int apply, ret;
343
344 if (!component)
345 return;
346
347 if (!component->card->pop_time) {
348
349
350
351 timer_setup(&cx81801_timer, cx81801_timeout, 0);
352
353 v253_ops.receive_buf(tty, cp, fp, count);
354
355
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)
360 dev_warn(component->dev,
361 "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
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
381
382 if (apply)
383 gpiod_set_value(gpiod_modem_codec, 1);
384 break;
385 }
386}
387
388
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
407
408
409
410
411
412
413static struct snd_soc_ops ams_delta_ops;
414
415
416
417
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)
433 gpiod_set_value(gpiod_modem_codec, !!mute);
434 return 0;
435}
436
437
438static const struct snd_soc_dai_ops ams_delta_dai_ops = {
439 .digital_mute = ams_delta_digital_mute,
440};
441
442
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
456
457
458static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
459{
460 struct snd_soc_dai *codec_dai = rtd->codec_dai;
461 struct snd_soc_card *card = rtd->card;
462 struct snd_soc_dapm_context *dapm = &card->dapm;
463 int ret;
464
465
466
467 cx20442_codec = rtd->codec_dai->component;
468
469
470
471 ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET,
472 &ams_delta_hook_switch, NULL, 0);
473 if (ret)
474 dev_warn(card->dev,
475 "Failed to allocate resources for hook switch, "
476 "will continue without one.\n");
477 else {
478 ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch,
479 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
487 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
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
502
503 ret = tty_register_ldisc(N_V253, &cx81801_ops);
504 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
511
512 snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
513 snd_soc_dapm_disable_pin(dapm, "Speaker");
514 snd_soc_dapm_disable_pin(dapm, "AGCIN");
515 snd_soc_dapm_disable_pin(dapm, "AGCOUT");
516
517 return 0;
518}
519
520
521static struct snd_soc_dai_link ams_delta_dai_link = {
522 .name = "CX20442",
523 .stream_name = "CX20442",
524 .cpu_dai_name = "omap-mcbsp.1",
525 .codec_dai_name = "cx20442-voice",
526 .init = ams_delta_cx20442_init,
527 .platform_name = "omap-mcbsp.1",
528 .codec_name = "cx20442-codec",
529 .ops = &ams_delta_ops,
530 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
531 SND_SOC_DAIFMT_CBM_CFM,
532};
533
534
535static struct snd_soc_card ams_delta_audio_card = {
536 .name = "AMS_DELTA",
537 .owner = THIS_MODULE,
538 .dai_link = &ams_delta_dai_link,
539 .num_links = 1,
540
541 .controls = ams_delta_audio_controls,
542 .num_controls = ARRAY_SIZE(ams_delta_audio_controls),
543 .dapm_widgets = ams_delta_dapm_widgets,
544 .num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
545 .dapm_routes = ams_delta_audio_map,
546 .num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
547};
548
549
550static int ams_delta_probe(struct platform_device *pdev)
551{
552 struct snd_soc_card *card = &ams_delta_audio_card;
553 int ret;
554
555 card->dev = &pdev->dev;
556
557 ret = snd_soc_register_card(card);
558 if (ret) {
559 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
560 card->dev = NULL;
561 return ret;
562 }
563 return 0;
564}
565
566static int ams_delta_remove(struct platform_device *pdev)
567{
568 struct snd_soc_card *card = platform_get_drvdata(pdev);
569
570 if (tty_unregister_ldisc(N_V253) != 0)
571 dev_warn(&pdev->dev,
572 "failed to unregister V253 line discipline\n");
573
574 snd_soc_unregister_card(card);
575 card->dev = NULL;
576 return 0;
577}
578
579#define DRV_NAME "ams-delta-audio"
580
581static struct platform_driver ams_delta_driver = {
582 .driver = {
583 .name = DRV_NAME,
584 },
585 .probe = ams_delta_probe,
586 .remove = ams_delta_remove,
587};
588
589module_platform_driver(ams_delta_driver);
590
591MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
592MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
593MODULE_LICENSE("GPL");
594MODULE_ALIAS("platform:" DRV_NAME);
595