ams-delta.c revision 5394637a246f4709e6f9c62b6af2356f49ef46a7
1/*
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>
28
29#include <sound/soc-dapm.h>
30#include <sound/jack.h>
31
32#include <asm/mach-types.h>
33
34#include <plat/board-ams-delta.h>
35#include <plat/mcbsp.h>
36
37#include "omap-mcbsp.h"
38#include "omap-pcm.h"
39#include "../codecs/cx20442.h"
40
41
42/* Board specific DAPM widgets */
43static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
44	/* 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
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 soc_enum *control = (struct soc_enum *)kcontrol->private_value;
98	unsigned short pins;
99	int pin, changed = 0;
100
101	/* Refuse any mode changes if we are not able to control the codec. */
102	if (!codec->hw_write)
103		return -EUNATCH;
104
105	if (ucontrol->value.enumerated.item[0] >= control->max)
106		return -EINVAL;
107
108	mutex_lock(&codec->mutex);
109
110	/* Translate selection to bitmap */
111	pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
112
113	/* Setup pins after corresponding bits if changed */
114	pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
115	if (pin != snd_soc_dapm_get_pin_status(codec, "Mouthpiece")) {
116		changed = 1;
117		if (pin)
118			snd_soc_dapm_enable_pin(codec, "Mouthpiece");
119		else
120			snd_soc_dapm_disable_pin(codec, "Mouthpiece");
121	}
122	pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
123	if (pin != snd_soc_dapm_get_pin_status(codec, "Earpiece")) {
124		changed = 1;
125		if (pin)
126			snd_soc_dapm_enable_pin(codec, "Earpiece");
127		else
128			snd_soc_dapm_disable_pin(codec, "Earpiece");
129	}
130	pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
131	if (pin != snd_soc_dapm_get_pin_status(codec, "Microphone")) {
132		changed = 1;
133		if (pin)
134			snd_soc_dapm_enable_pin(codec, "Microphone");
135		else
136			snd_soc_dapm_disable_pin(codec, "Microphone");
137	}
138	pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
139	if (pin != snd_soc_dapm_get_pin_status(codec, "Speaker")) {
140		changed = 1;
141		if (pin)
142			snd_soc_dapm_enable_pin(codec, "Speaker");
143		else
144			snd_soc_dapm_disable_pin(codec, "Speaker");
145	}
146	pin = !!(pins & (1 << AMS_DELTA_AGC));
147	if (pin != ams_delta_audio_agc) {
148		ams_delta_audio_agc = pin;
149		changed = 1;
150		if (pin)
151			snd_soc_dapm_enable_pin(codec, "AGCIN");
152		else
153			snd_soc_dapm_disable_pin(codec, "AGCIN");
154	}
155	if (changed)
156		snd_soc_dapm_sync(codec);
157
158	mutex_unlock(&codec->mutex);
159
160	return changed;
161}
162
163static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
164					struct snd_ctl_elem_value *ucontrol)
165{
166	struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
167	unsigned short pins, mode;
168
169	pins = ((snd_soc_dapm_get_pin_status(codec, "Mouthpiece") <<
170							AMS_DELTA_MOUTHPIECE) |
171			(snd_soc_dapm_get_pin_status(codec, "Earpiece") <<
172							AMS_DELTA_EARPIECE));
173	if (pins)
174		pins |= (snd_soc_dapm_get_pin_status(codec, "Microphone") <<
175							AMS_DELTA_MICROPHONE);
176	else
177		pins = ((snd_soc_dapm_get_pin_status(codec, "Microphone") <<
178							AMS_DELTA_MICROPHONE) |
179			(snd_soc_dapm_get_pin_status(codec, "Speaker") <<
180							AMS_DELTA_SPEAKER) |
181			(ams_delta_audio_agc << AMS_DELTA_AGC));
182
183	for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
184		if (pins == ams_delta_audio_mode_pins[mode])
185			break;
186
187	if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
188		return -EINVAL;
189
190	ucontrol->value.enumerated.item[0] = mode;
191
192	return 0;
193}
194
195static const struct soc_enum ams_delta_audio_enum[] = {
196	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ams_delta_audio_mode),
197						ams_delta_audio_mode),
198};
199
200static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
201	SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum[0],
202			ams_delta_get_audio_mode, ams_delta_set_audio_mode),
203};
204
205/* Hook switch */
206static struct snd_soc_jack ams_delta_hook_switch;
207static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
208	{
209		.gpio = 4,
210		.name = "hook_switch",
211		.report = SND_JACK_HEADSET,
212		.invert = 1,
213		.debounce_time = 150,
214	}
215};
216
217/* After we are able to control the codec over the modem,
218 * the hook switch can be used for dynamic DAPM reconfiguration. */
219static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
220	/* Handset */
221	{
222		.pin = "Mouthpiece",
223		.mask = SND_JACK_MICROPHONE,
224	},
225	{
226		.pin = "Earpiece",
227		.mask = SND_JACK_HEADPHONE,
228	},
229	/* Handsfree */
230	{
231		.pin = "Microphone",
232		.mask = SND_JACK_MICROPHONE,
233		.invert = 1,
234	},
235	{
236		.pin = "Speaker",
237		.mask = SND_JACK_HEADPHONE,
238		.invert = 1,
239	},
240};
241
242
243/*
244 * Modem line discipline, required for making above controls functional.
245 * Activated from userspace with ldattach, possibly invoked from udev rule.
246 */
247
248/* To actually apply any modem controlled configuration changes to the codec,
249 * we must connect codec DAI pins to the modem for a moment.  Be carefull not
250 * to interfere with our digital mute function that shares the same hardware. */
251static struct timer_list cx81801_timer;
252static bool cx81801_cmd_pending;
253static bool ams_delta_muted;
254static DEFINE_SPINLOCK(ams_delta_lock);
255
256static void cx81801_timeout(unsigned long data)
257{
258	int muted;
259
260	spin_lock(&ams_delta_lock);
261	cx81801_cmd_pending = 0;
262	muted = ams_delta_muted;
263	spin_unlock(&ams_delta_lock);
264
265	/* Reconnect the codec DAI back from the modem to the CPU DAI
266	 * only if digital mute still off */
267	if (!muted)
268		ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC, 0);
269}
270
271/*
272 * Used for passing a codec structure pointer
273 * from the board initialization code to the tty line discipline.
274 */
275static struct snd_soc_codec *cx20442_codec;
276
277/* Line discipline .open() */
278static int cx81801_open(struct tty_struct *tty)
279{
280	int ret;
281
282	if (!cx20442_codec)
283		return -ENODEV;
284
285	/*
286	 * Pass the codec structure pointer for use by other ldisc callbacks,
287	 * both the card and the codec specific parts.
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/* Line discipline .close() */
300static void cx81801_close(struct tty_struct *tty)
301{
302	struct snd_soc_codec *codec = tty->disc_data;
303
304	del_timer_sync(&cx81801_timer);
305
306	/* Prevent the hook switch from further changing the DAPM pins */
307	INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
308
309	if (!codec)
310		return;
311
312	v253_ops.close(tty);
313
314	/* Revert back to default audio input/output constellation */
315	snd_soc_dapm_disable_pin(codec, "Mouthpiece");
316	snd_soc_dapm_enable_pin(codec, "Earpiece");
317	snd_soc_dapm_enable_pin(codec, "Microphone");
318	snd_soc_dapm_disable_pin(codec, "Speaker");
319	snd_soc_dapm_disable_pin(codec, "AGCIN");
320	snd_soc_dapm_sync(codec);
321}
322
323/* Line discipline .hangup() */
324static int cx81801_hangup(struct tty_struct *tty)
325{
326	cx81801_close(tty);
327	return 0;
328}
329
330/* Line discipline .recieve_buf() */
331static void cx81801_receive(struct tty_struct *tty,
332				const unsigned char *cp, char *fp, int count)
333{
334	struct snd_soc_codec *codec = tty->disc_data;
335	const unsigned char *c;
336	int apply, ret;
337
338	if (!codec)
339		return;
340
341	if (!codec->hw_write) {
342		/* First modem response, complete setup procedure */
343
344		/* Initialize timer used for config pulse generation */
345		setup_timer(&cx81801_timer, cx81801_timeout, 0);
346
347		v253_ops.receive_buf(tty, cp, fp, count);
348
349		/* Link hook switch to DAPM pins */
350		ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
351					ARRAY_SIZE(ams_delta_hook_switch_pins),
352					ams_delta_hook_switch_pins);
353		if (ret)
354			dev_warn(codec->dev,
355				"Failed to link hook switch to DAPM pins, "
356				"will continue with hook switch unlinked.\n");
357
358		return;
359	}
360
361	v253_ops.receive_buf(tty, cp, fp, count);
362
363	for (c = &cp[count - 1]; c >= cp; c--) {
364		if (*c != '\r')
365			continue;
366		/* Complete modem response received, apply config to codec */
367
368		spin_lock_bh(&ams_delta_lock);
369		mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
370		apply = !ams_delta_muted && !cx81801_cmd_pending;
371		cx81801_cmd_pending = 1;
372		spin_unlock_bh(&ams_delta_lock);
373
374		/* Apply config pulse by connecting the codec to the modem
375		 * if not already done */
376		if (apply)
377			ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
378						AMS_DELTA_LATCH2_MODEM_CODEC);
379		break;
380	}
381}
382
383/* Line discipline .write_wakeup() */
384static void cx81801_wakeup(struct tty_struct *tty)
385{
386	v253_ops.write_wakeup(tty);
387}
388
389static struct tty_ldisc_ops cx81801_ops = {
390	.magic = TTY_LDISC_MAGIC,
391	.name = "cx81801",
392	.owner = THIS_MODULE,
393	.open = cx81801_open,
394	.close = cx81801_close,
395	.hangup = cx81801_hangup,
396	.receive_buf = cx81801_receive,
397	.write_wakeup = cx81801_wakeup,
398};
399
400
401/*
402 * Even if not very usefull, the sound card can still work without any of the
403 * above functonality activated.  You can still control its audio input/output
404 * constellation and speakerphone gain from userspace by issueing AT commands
405 * over the modem port.
406 */
407
408static int ams_delta_hw_params(struct snd_pcm_substream *substream,
409			 struct snd_pcm_hw_params *params)
410{
411	struct snd_soc_pcm_runtime *rtd = substream->private_data;
412
413	/* Set cpu DAI configuration */
414	return snd_soc_dai_set_fmt(rtd->cpu_dai,
415				   SND_SOC_DAIFMT_DSP_A |
416				   SND_SOC_DAIFMT_NB_NF |
417				   SND_SOC_DAIFMT_CBM_CFM);
418}
419
420static struct snd_soc_ops ams_delta_ops = {
421	.hw_params = ams_delta_hw_params,
422};
423
424
425/* Board specific codec bias level control */
426static int ams_delta_set_bias_level(struct snd_soc_card *card,
427					enum snd_soc_bias_level level)
428{
429	struct snd_soc_codec *codec = card->rtd->codec;
430
431	switch (level) {
432	case SND_SOC_BIAS_ON:
433	case SND_SOC_BIAS_PREPARE:
434	case SND_SOC_BIAS_STANDBY:
435		if (codec->bias_level == SND_SOC_BIAS_OFF)
436			ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET,
437						AMS_DELTA_LATCH2_MODEM_NRESET);
438		break;
439	case SND_SOC_BIAS_OFF:
440		if (codec->bias_level != SND_SOC_BIAS_OFF)
441			ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_NRESET,
442						0);
443	}
444	codec->bias_level = level;
445
446	return 0;
447}
448
449/* Digital mute implemented using modem/CPU multiplexer.
450 * Shares hardware with codec config pulse generation */
451static bool ams_delta_muted = 1;
452
453static int ams_delta_digital_mute(struct snd_soc_dai *dai, int mute)
454{
455	int apply;
456
457	if (ams_delta_muted == mute)
458		return 0;
459
460	spin_lock_bh(&ams_delta_lock);
461	ams_delta_muted = mute;
462	apply = !cx81801_cmd_pending;
463	spin_unlock_bh(&ams_delta_lock);
464
465	if (apply)
466		ams_delta_latch2_write(AMS_DELTA_LATCH2_MODEM_CODEC,
467				mute ? AMS_DELTA_LATCH2_MODEM_CODEC : 0);
468	return 0;
469}
470
471/* Our codec DAI probably doesn't have its own .ops structure */
472static struct snd_soc_dai_ops ams_delta_dai_ops = {
473	.digital_mute = ams_delta_digital_mute,
474};
475
476/* Will be used if the codec ever has its own digital_mute function */
477static int ams_delta_startup(struct snd_pcm_substream *substream)
478{
479	return ams_delta_digital_mute(NULL, 0);
480}
481
482static void ams_delta_shutdown(struct snd_pcm_substream *substream)
483{
484	ams_delta_digital_mute(NULL, 1);
485}
486
487
488/*
489 * Card initialization
490 */
491
492static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
493{
494	struct snd_soc_codec *codec = rtd->codec;
495	struct snd_soc_dai *codec_dai = rtd->codec_dai;
496	struct snd_soc_card *card = rtd->card;
497	int ret;
498	/* Codec is ready, now add/activate board specific controls */
499
500	/* Store a pointer to the codec structure for tty ldisc use */
501	cx20442_codec = codec;
502
503	/* Set up digital mute if not provided by the codec */
504	if (!codec_dai->driver->ops) {
505		codec_dai->driver->ops = &ams_delta_dai_ops;
506	} else if (!codec_dai->driver->ops->digital_mute) {
507		codec_dai->driver->ops->digital_mute = ams_delta_digital_mute;
508	} else {
509		ams_delta_ops.startup = ams_delta_startup;
510		ams_delta_ops.shutdown = ams_delta_shutdown;
511	}
512
513	/* Set codec bias level */
514	ams_delta_set_bias_level(card, SND_SOC_BIAS_STANDBY);
515
516	/* Add hook switch - can be used to control the codec from userspace
517	 * even if line discipline fails */
518	ret = snd_soc_jack_new(rtd->codec, "hook_switch",
519				SND_JACK_HEADSET, &ams_delta_hook_switch);
520	if (ret)
521		dev_warn(card->dev,
522				"Failed to allocate resources for hook switch, "
523				"will continue without one.\n");
524	else {
525		ret = snd_soc_jack_add_gpios(&ams_delta_hook_switch,
526					ARRAY_SIZE(ams_delta_hook_switch_gpios),
527					ams_delta_hook_switch_gpios);
528		if (ret)
529			dev_warn(card->dev,
530				"Failed to set up hook switch GPIO line, "
531				"will continue with hook switch inactive.\n");
532	}
533
534	/* Register optional line discipline for over the modem control */
535	ret = tty_register_ldisc(N_V253, &cx81801_ops);
536	if (ret) {
537		dev_warn(card->dev,
538				"Failed to register line discipline, "
539				"will continue without any controls.\n");
540		return 0;
541	}
542
543	/* Add board specific DAPM widgets and routes */
544	ret = snd_soc_dapm_new_controls(codec, ams_delta_dapm_widgets,
545					ARRAY_SIZE(ams_delta_dapm_widgets));
546	if (ret) {
547		dev_warn(card->dev,
548				"Failed to register DAPM controls, "
549				"will continue without any.\n");
550		return 0;
551	}
552
553	ret = snd_soc_dapm_add_routes(codec, ams_delta_audio_map,
554					ARRAY_SIZE(ams_delta_audio_map));
555	if (ret) {
556		dev_warn(card->dev,
557				"Failed to set up DAPM routes, "
558				"will continue with codec default map.\n");
559		return 0;
560	}
561
562	/* Set up initial pin constellation */
563	snd_soc_dapm_disable_pin(codec, "Mouthpiece");
564	snd_soc_dapm_enable_pin(codec, "Earpiece");
565	snd_soc_dapm_enable_pin(codec, "Microphone");
566	snd_soc_dapm_disable_pin(codec, "Speaker");
567	snd_soc_dapm_disable_pin(codec, "AGCIN");
568	snd_soc_dapm_disable_pin(codec, "AGCOUT");
569	snd_soc_dapm_sync(codec);
570
571	/* Add virtual switch */
572	ret = snd_soc_add_controls(codec, ams_delta_audio_controls,
573					ARRAY_SIZE(ams_delta_audio_controls));
574	if (ret)
575		dev_warn(card->dev,
576				"Failed to register audio mode control, "
577				"will continue without it.\n");
578
579	return 0;
580}
581
582/* DAI glue - connects codec <--> CPU */
583static struct snd_soc_dai_link ams_delta_dai_link = {
584	.name = "CX20442",
585	.stream_name = "CX20442",
586	.cpu_dai_name ="omap-mcbsp-dai.0",
587	.codec_dai_name = "cx20442-voice",
588	.init = ams_delta_cx20442_init,
589	.platform_name = "omap-pcm-audio",
590	.codec_name = "cx20442-codec",
591	.ops = &ams_delta_ops,
592};
593
594/* Audio card driver */
595static struct snd_soc_card ams_delta_audio_card = {
596	.name = "AMS_DELTA",
597	.dai_link = &ams_delta_dai_link,
598	.num_links = 1,
599	.set_bias_level = ams_delta_set_bias_level,
600};
601
602/* Module init/exit */
603static struct platform_device *ams_delta_audio_platform_device;
604static struct platform_device *cx20442_platform_device;
605
606static int __init ams_delta_module_init(void)
607{
608	int ret;
609
610	if (!(machine_is_ams_delta()))
611		return -ENODEV;
612
613	ams_delta_audio_platform_device =
614			platform_device_alloc("soc-audio", -1);
615	if (!ams_delta_audio_platform_device)
616		return -ENOMEM;
617
618	platform_set_drvdata(ams_delta_audio_platform_device,
619				&ams_delta_audio_card);
620
621	ret = platform_device_add(ams_delta_audio_platform_device);
622	if (ret)
623		goto err;
624
625	/*
626	 * Codec platform device could be registered from elsewhere (board?),
627	 * but I do it here as it makes sense only if used with the card.
628	 */
629	cx20442_platform_device =
630		platform_device_register_simple("cx20442-codec", -1, NULL, 0);
631	return 0;
632err:
633	platform_device_put(ams_delta_audio_platform_device);
634	return ret;
635}
636module_init(ams_delta_module_init);
637
638static void __exit ams_delta_module_exit(void)
639{
640	if (tty_unregister_ldisc(N_V253) != 0)
641		dev_warn(&ams_delta_audio_platform_device->dev,
642			"failed to unregister V253 line discipline\n");
643
644	snd_soc_jack_free_gpios(&ams_delta_hook_switch,
645			ARRAY_SIZE(ams_delta_hook_switch_gpios),
646			ams_delta_hook_switch_gpios);
647
648	/* Keep modem power on */
649	ams_delta_set_bias_level(&ams_delta_audio_card, SND_SOC_BIAS_STANDBY);
650
651	platform_device_unregister(cx20442_platform_device);
652	platform_device_unregister(ams_delta_audio_platform_device);
653}
654module_exit(ams_delta_module_exit);
655
656MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
657MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
658MODULE_LICENSE("GPL");
659