xref: /linux/sound/soc/ti/ams-delta.c (revision d1ecaabe9f1a669354de7420261bd8737da4bf48)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ams-delta.c  --  SoC audio for Amstrad E3 (Delta) videophone
4  *
5  * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6  *
7  * Initially based on sound/soc/omap/osk5912.x
8  * Copyright (C) 2008 Mistral Solutions
9  */
10 
11 #include <linux/gpio/consumer.h>
12 #include <linux/spinlock.h>
13 #include <linux/tty.h>
14 #include <linux/module.h>
15 
16 #include <sound/soc.h>
17 #include <sound/jack.h>
18 
19 #include <linux/platform_data/asoc-ti-mcbsp.h>
20 
21 #include "omap-mcbsp.h"
22 #include "../codecs/cx20442.h"
23 
24 static struct gpio_desc *handset_mute;
25 static struct gpio_desc *handsfree_mute;
26 
27 static int ams_delta_event_handset(struct snd_soc_dapm_widget *w,
28 				   struct snd_kcontrol *k, int event)
29 {
30 	gpiod_set_value_cansleep(handset_mute, !SND_SOC_DAPM_EVENT_ON(event));
31 	return 0;
32 }
33 
34 static int ams_delta_event_handsfree(struct snd_soc_dapm_widget *w,
35 				     struct snd_kcontrol *k, int event)
36 {
37 	gpiod_set_value_cansleep(handsfree_mute, !SND_SOC_DAPM_EVENT_ON(event));
38 	return 0;
39 }
40 
41 /* Board specific DAPM widgets */
42 static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
43 	/* Handset */
44 	SND_SOC_DAPM_MIC("Mouthpiece", NULL),
45 	SND_SOC_DAPM_HP("Earpiece", ams_delta_event_handset),
46 	/* Handsfree/Speakerphone */
47 	SND_SOC_DAPM_MIC("Microphone", NULL),
48 	SND_SOC_DAPM_SPK("Speaker", ams_delta_event_handsfree),
49 };
50 
51 /* How they are connected to codec pins */
52 static 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  * Controls, functional after the modem line discipline is activated.
62  */
63 
64 /* Virtual switch: audio input/output constellations */
65 static const char *ams_delta_audio_mode[] =
66 	{"Mixed", "Handset", "Handsfree", "Speakerphone"};
67 
68 /* Selection <-> pin translation */
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 
83 static 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 
90 static unsigned short ams_delta_audio_agc;
91 
92 /*
93  * Used for passing a codec structure pointer
94  * from the board initialization code to the tty line discipline.
95  */
96 static struct snd_soc_component *cx20442_codec;
97 
98 static 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 	/* Refuse any mode changes if we are not able to control the codec. */
108 	if (!cx20442_codec->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 	/* Translate selection to bitmap */
117 	pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
118 
119 	/* Setup pins after corresponding bits if changed */
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 
171 static 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 
204 static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
205 				      ams_delta_audio_mode);
206 
207 static 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 /* Hook switch */
213 static struct snd_soc_jack ams_delta_hook_switch;
214 static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
215 	{
216 		.name = "hook_switch",
217 		.report = SND_JACK_HEADSET,
218 		.invert = 1,
219 		.debounce_time = 150,
220 	}
221 };
222 
223 /* After we are able to control the codec over the modem,
224  * the hook switch can be used for dynamic DAPM reconfiguration. */
225 static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
226 	/* Handset */
227 	{
228 		.pin = "Mouthpiece",
229 		.mask = SND_JACK_MICROPHONE,
230 	},
231 	{
232 		.pin = "Earpiece",
233 		.mask = SND_JACK_HEADPHONE,
234 	},
235 	/* Handsfree */
236 	{
237 		.pin = "Microphone",
238 		.mask = SND_JACK_MICROPHONE,
239 		.invert = 1,
240 	},
241 	{
242 		.pin = "Speaker",
243 		.mask = SND_JACK_HEADPHONE,
244 		.invert = 1,
245 	},
246 };
247 
248 
249 /*
250  * Modem line discipline, required for making above controls functional.
251  * Activated from userspace with ldattach, possibly invoked from udev rule.
252  */
253 
254 /* To actually apply any modem controlled configuration changes to the codec,
255  * we must connect codec DAI pins to the modem for a moment.  Be careful not
256  * to interfere with our digital mute function that shares the same hardware. */
257 static struct timer_list cx81801_timer;
258 static bool cx81801_cmd_pending;
259 static bool ams_delta_muted;
260 static DEFINE_SPINLOCK(ams_delta_lock);
261 static struct gpio_desc *gpiod_modem_codec;
262 
263 static 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 	/* Reconnect the codec DAI back from the modem to the CPU DAI
273 	 * only if digital mute still off */
274 	if (!muted)
275 		gpiod_set_value(gpiod_modem_codec, 0);
276 }
277 
278 /* Line discipline .open() */
279 static int cx81801_open(struct tty_struct *tty)
280 {
281 	int ret;
282 
283 	if (!cx20442_codec)
284 		return -ENODEV;
285 
286 	/*
287 	 * Pass the codec structure pointer for use by other ldisc callbacks,
288 	 * both the card and the codec specific parts.
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 /* Line discipline .close() */
301 static void cx81801_close(struct tty_struct *tty)
302 {
303 	struct snd_soc_component *component = tty->disc_data;
304 	struct snd_soc_dapm_context *dapm = &component->card->dapm;
305 
306 	del_timer_sync(&cx81801_timer);
307 
308 	/* Prevent the hook switch from further changing the DAPM pins */
309 	INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
310 
311 	if (!component)
312 		return;
313 
314 	v253_ops.close(tty);
315 
316 	/* Revert back to default audio input/output constellation */
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 /* Line discipline .hangup() */
331 static void cx81801_hangup(struct tty_struct *tty)
332 {
333 	cx81801_close(tty);
334 }
335 
336 /* Line discipline .receive_buf() */
337 static void cx81801_receive(struct tty_struct *tty, const u8 *cp, const u8 *fp,
338 			    size_t 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 		/* First modem response, complete setup procedure */
349 
350 		/* Initialize timer used for config pulse generation */
351 		timer_setup(&cx81801_timer, cx81801_timeout, 0);
352 
353 		v253_ops.receive_buf(tty, cp, fp, count);
354 
355 		/* Link hook switch to DAPM pins */
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 		/* Complete modem response received, apply config to codec */
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 		/* Apply config pulse by connecting the codec to the modem
381 		 * if not already done */
382 		if (apply)
383 			gpiod_set_value(gpiod_modem_codec, 1);
384 		break;
385 	}
386 }
387 
388 /* Line discipline .write_wakeup() */
389 static void cx81801_wakeup(struct tty_struct *tty)
390 {
391 	v253_ops.write_wakeup(tty);
392 }
393 
394 static struct tty_ldisc_ops cx81801_ops = {
395 	.name = "cx81801",
396 	.num = N_V253,
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  * Even if not very useful, the sound card can still work without any of the
408  * above functionality activated.  You can still control its audio input/output
409  * constellation and speakerphone gain from userspace by issuing AT commands
410  * over the modem port.
411  */
412 
413 static struct snd_soc_ops ams_delta_ops;
414 
415 
416 /* Digital mute implemented using modem/CPU multiplexer.
417  * Shares hardware with codec config pulse generation */
418 static bool ams_delta_muted = 1;
419 
420 static int ams_delta_mute(struct snd_soc_dai *dai, int mute, int direction)
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 /* Our codec DAI probably doesn't have its own .ops structure */
438 static const struct snd_soc_dai_ops ams_delta_dai_ops = {
439 	.mute_stream = ams_delta_mute,
440 	.no_capture_mute = 1,
441 };
442 
443 /* Will be used if the codec ever has its own digital_mute function */
444 static int ams_delta_startup(struct snd_pcm_substream *substream)
445 {
446 	return ams_delta_mute(NULL, 0, substream->stream);
447 }
448 
449 static void ams_delta_shutdown(struct snd_pcm_substream *substream)
450 {
451 	ams_delta_mute(NULL, 1, substream->stream);
452 }
453 
454 
455 /*
456  * Card initialization
457  */
458 
459 static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
460 {
461 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
462 	struct snd_soc_card *card = rtd->card;
463 	struct snd_soc_dapm_context *dapm = &card->dapm;
464 	int ret;
465 	/* Codec is ready, now add/activate board specific controls */
466 
467 	/* Store a pointer to the codec structure for tty ldisc use */
468 	cx20442_codec = snd_soc_rtd_to_codec(rtd, 0)->component;
469 
470 	/* Add hook switch - can be used to control the codec from userspace
471 	 * even if line discipline fails */
472 	ret = snd_soc_card_jack_new_pins(card, "hook_switch", SND_JACK_HEADSET,
473 					 &ams_delta_hook_switch, NULL, 0);
474 	if (ret)
475 		dev_warn(card->dev,
476 				"Failed to allocate resources for hook switch, "
477 				"will continue without one.\n");
478 	else {
479 		ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch,
480 					ARRAY_SIZE(ams_delta_hook_switch_gpios),
481 					ams_delta_hook_switch_gpios);
482 		if (ret)
483 			dev_warn(card->dev,
484 				"Failed to set up hook switch GPIO line, "
485 				"will continue with hook switch inactive.\n");
486 	}
487 
488 	gpiod_modem_codec = devm_gpiod_get(card->dev, "modem_codec",
489 					   GPIOD_OUT_HIGH);
490 	if (IS_ERR(gpiod_modem_codec)) {
491 		dev_warn(card->dev, "Failed to obtain modem_codec GPIO\n");
492 		return 0;
493 	}
494 
495 	/* Set up digital mute if not provided by the codec */
496 	if (!codec_dai->driver->ops) {
497 		codec_dai->driver->ops = &ams_delta_dai_ops;
498 	} else {
499 		ams_delta_ops.startup = ams_delta_startup;
500 		ams_delta_ops.shutdown = ams_delta_shutdown;
501 	}
502 
503 	/* Register optional line discipline for over the modem control */
504 	ret = tty_register_ldisc(&cx81801_ops);
505 	if (ret) {
506 		dev_warn(card->dev,
507 				"Failed to register line discipline, "
508 				"will continue without any controls.\n");
509 		return 0;
510 	}
511 
512 	/* Set up initial pin constellation */
513 	snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
514 	snd_soc_dapm_disable_pin(dapm, "Speaker");
515 	snd_soc_dapm_disable_pin(dapm, "AGCIN");
516 	snd_soc_dapm_disable_pin(dapm, "AGCOUT");
517 
518 	return 0;
519 }
520 
521 /* DAI glue - connects codec <--> CPU */
522 SND_SOC_DAILINK_DEFS(cx20442,
523 	DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")),
524 	DAILINK_COMP_ARRAY(COMP_CODEC("cx20442-codec", "cx20442-voice")),
525 	DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1")));
526 
527 static struct snd_soc_dai_link ams_delta_dai_link = {
528 	.name = "CX20442",
529 	.stream_name = "CX20442",
530 	.init = ams_delta_cx20442_init,
531 	.ops = &ams_delta_ops,
532 	.dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
533 		   SND_SOC_DAIFMT_CBM_CFM,
534 	SND_SOC_DAILINK_REG(cx20442),
535 };
536 
537 /* Audio card driver */
538 static struct snd_soc_card ams_delta_audio_card = {
539 	.name = "AMS_DELTA",
540 	.owner = THIS_MODULE,
541 	.dai_link = &ams_delta_dai_link,
542 	.num_links = 1,
543 
544 	.controls = ams_delta_audio_controls,
545 	.num_controls = ARRAY_SIZE(ams_delta_audio_controls),
546 	.dapm_widgets = ams_delta_dapm_widgets,
547 	.num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
548 	.dapm_routes = ams_delta_audio_map,
549 	.num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
550 };
551 
552 /* Module init/exit */
553 static int ams_delta_probe(struct platform_device *pdev)
554 {
555 	struct snd_soc_card *card = &ams_delta_audio_card;
556 	int ret;
557 
558 	card->dev = &pdev->dev;
559 
560 	handset_mute = devm_gpiod_get(card->dev, "handset_mute",
561 				      GPIOD_OUT_HIGH);
562 	if (IS_ERR(handset_mute))
563 		return PTR_ERR(handset_mute);
564 
565 	handsfree_mute = devm_gpiod_get(card->dev, "handsfree_mute",
566 					GPIOD_OUT_HIGH);
567 	if (IS_ERR(handsfree_mute))
568 		return PTR_ERR(handsfree_mute);
569 
570 	ret = snd_soc_register_card(card);
571 	if (ret) {
572 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
573 		card->dev = NULL;
574 		return ret;
575 	}
576 	return 0;
577 }
578 
579 static void ams_delta_remove(struct platform_device *pdev)
580 {
581 	struct snd_soc_card *card = platform_get_drvdata(pdev);
582 
583 	tty_unregister_ldisc(&cx81801_ops);
584 
585 	snd_soc_unregister_card(card);
586 	card->dev = NULL;
587 }
588 
589 #define DRV_NAME "ams-delta-audio"
590 
591 static struct platform_driver ams_delta_driver = {
592 	.driver = {
593 		.name = DRV_NAME,
594 	},
595 	.probe = ams_delta_probe,
596 	.remove_new = ams_delta_remove,
597 };
598 
599 module_platform_driver(ams_delta_driver);
600 
601 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
602 MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
603 MODULE_LICENSE("GPL");
604 MODULE_ALIAS("platform:" DRV_NAME);
605