xref: /OK3568_Linux_fs/kernel/sound/soc/ti/ams-delta.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * ams-delta.c  --  SoC audio for Amstrad E3 (Delta) videophone
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2009 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Initially based on sound/soc/omap/osk5912.x
8*4882a593Smuzhiyun  * Copyright (C) 2008 Mistral Solutions
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
12*4882a593Smuzhiyun #include <linux/spinlock.h>
13*4882a593Smuzhiyun #include <linux/tty.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <sound/soc.h>
17*4882a593Smuzhiyun #include <sound/jack.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <asm/mach-types.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include <linux/platform_data/asoc-ti-mcbsp.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "omap-mcbsp.h"
24*4882a593Smuzhiyun #include "../codecs/cx20442.h"
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static struct gpio_desc *handset_mute;
27*4882a593Smuzhiyun static struct gpio_desc *handsfree_mute;
28*4882a593Smuzhiyun 
ams_delta_event_handset(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)29*4882a593Smuzhiyun static int ams_delta_event_handset(struct snd_soc_dapm_widget *w,
30*4882a593Smuzhiyun 				   struct snd_kcontrol *k, int event)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	gpiod_set_value_cansleep(handset_mute, !SND_SOC_DAPM_EVENT_ON(event));
33*4882a593Smuzhiyun 	return 0;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
ams_delta_event_handsfree(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)36*4882a593Smuzhiyun static int ams_delta_event_handsfree(struct snd_soc_dapm_widget *w,
37*4882a593Smuzhiyun 				     struct snd_kcontrol *k, int event)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	gpiod_set_value_cansleep(handsfree_mute, !SND_SOC_DAPM_EVENT_ON(event));
40*4882a593Smuzhiyun 	return 0;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /* Board specific DAPM widgets */
44*4882a593Smuzhiyun static const struct snd_soc_dapm_widget ams_delta_dapm_widgets[] = {
45*4882a593Smuzhiyun 	/* Handset */
46*4882a593Smuzhiyun 	SND_SOC_DAPM_MIC("Mouthpiece", NULL),
47*4882a593Smuzhiyun 	SND_SOC_DAPM_HP("Earpiece", ams_delta_event_handset),
48*4882a593Smuzhiyun 	/* Handsfree/Speakerphone */
49*4882a593Smuzhiyun 	SND_SOC_DAPM_MIC("Microphone", NULL),
50*4882a593Smuzhiyun 	SND_SOC_DAPM_SPK("Speaker", ams_delta_event_handsfree),
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /* How they are connected to codec pins */
54*4882a593Smuzhiyun static const struct snd_soc_dapm_route ams_delta_audio_map[] = {
55*4882a593Smuzhiyun 	{"TELIN", NULL, "Mouthpiece"},
56*4882a593Smuzhiyun 	{"Earpiece", NULL, "TELOUT"},
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	{"MIC", NULL, "Microphone"},
59*4882a593Smuzhiyun 	{"Speaker", NULL, "SPKOUT"},
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun  * Controls, functional after the modem line discipline is activated.
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /* Virtual switch: audio input/output constellations */
67*4882a593Smuzhiyun static const char *ams_delta_audio_mode[] =
68*4882a593Smuzhiyun 	{"Mixed", "Handset", "Handsfree", "Speakerphone"};
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* Selection <-> pin translation */
71*4882a593Smuzhiyun #define AMS_DELTA_MOUTHPIECE	0
72*4882a593Smuzhiyun #define AMS_DELTA_EARPIECE	1
73*4882a593Smuzhiyun #define AMS_DELTA_MICROPHONE	2
74*4882a593Smuzhiyun #define AMS_DELTA_SPEAKER	3
75*4882a593Smuzhiyun #define AMS_DELTA_AGC		4
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #define AMS_DELTA_MIXED		((1 << AMS_DELTA_EARPIECE) | \
78*4882a593Smuzhiyun 						(1 << AMS_DELTA_MICROPHONE))
79*4882a593Smuzhiyun #define AMS_DELTA_HANDSET	((1 << AMS_DELTA_MOUTHPIECE) | \
80*4882a593Smuzhiyun 						(1 << AMS_DELTA_EARPIECE))
81*4882a593Smuzhiyun #define AMS_DELTA_HANDSFREE	((1 << AMS_DELTA_MICROPHONE) | \
82*4882a593Smuzhiyun 						(1 << AMS_DELTA_SPEAKER))
83*4882a593Smuzhiyun #define AMS_DELTA_SPEAKERPHONE	(AMS_DELTA_HANDSFREE | (1 << AMS_DELTA_AGC))
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static const unsigned short ams_delta_audio_mode_pins[] = {
86*4882a593Smuzhiyun 	AMS_DELTA_MIXED,
87*4882a593Smuzhiyun 	AMS_DELTA_HANDSET,
88*4882a593Smuzhiyun 	AMS_DELTA_HANDSFREE,
89*4882a593Smuzhiyun 	AMS_DELTA_SPEAKERPHONE,
90*4882a593Smuzhiyun };
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun static unsigned short ams_delta_audio_agc;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun  * Used for passing a codec structure pointer
96*4882a593Smuzhiyun  * from the board initialization code to the tty line discipline.
97*4882a593Smuzhiyun  */
98*4882a593Smuzhiyun static struct snd_soc_component *cx20442_codec;
99*4882a593Smuzhiyun 
ams_delta_set_audio_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)100*4882a593Smuzhiyun static int ams_delta_set_audio_mode(struct snd_kcontrol *kcontrol,
101*4882a593Smuzhiyun 					struct snd_ctl_elem_value *ucontrol)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
104*4882a593Smuzhiyun 	struct snd_soc_dapm_context *dapm = &card->dapm;
105*4882a593Smuzhiyun 	struct soc_enum *control = (struct soc_enum *)kcontrol->private_value;
106*4882a593Smuzhiyun 	unsigned short pins;
107*4882a593Smuzhiyun 	int pin, changed = 0;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* Refuse any mode changes if we are not able to control the codec. */
110*4882a593Smuzhiyun 	if (!cx20442_codec->card->pop_time)
111*4882a593Smuzhiyun 		return -EUNATCH;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	if (ucontrol->value.enumerated.item[0] >= control->items)
114*4882a593Smuzhiyun 		return -EINVAL;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	snd_soc_dapm_mutex_lock(dapm);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* Translate selection to bitmap */
119*4882a593Smuzhiyun 	pins = ams_delta_audio_mode_pins[ucontrol->value.enumerated.item[0]];
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* Setup pins after corresponding bits if changed */
122*4882a593Smuzhiyun 	pin = !!(pins & (1 << AMS_DELTA_MOUTHPIECE));
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	if (pin != snd_soc_dapm_get_pin_status(dapm, "Mouthpiece")) {
125*4882a593Smuzhiyun 		changed = 1;
126*4882a593Smuzhiyun 		if (pin)
127*4882a593Smuzhiyun 			snd_soc_dapm_enable_pin_unlocked(dapm, "Mouthpiece");
128*4882a593Smuzhiyun 		else
129*4882a593Smuzhiyun 			snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 	pin = !!(pins & (1 << AMS_DELTA_EARPIECE));
132*4882a593Smuzhiyun 	if (pin != snd_soc_dapm_get_pin_status(dapm, "Earpiece")) {
133*4882a593Smuzhiyun 		changed = 1;
134*4882a593Smuzhiyun 		if (pin)
135*4882a593Smuzhiyun 			snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
136*4882a593Smuzhiyun 		else
137*4882a593Smuzhiyun 			snd_soc_dapm_disable_pin_unlocked(dapm, "Earpiece");
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 	pin = !!(pins & (1 << AMS_DELTA_MICROPHONE));
140*4882a593Smuzhiyun 	if (pin != snd_soc_dapm_get_pin_status(dapm, "Microphone")) {
141*4882a593Smuzhiyun 		changed = 1;
142*4882a593Smuzhiyun 		if (pin)
143*4882a593Smuzhiyun 			snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
144*4882a593Smuzhiyun 		else
145*4882a593Smuzhiyun 			snd_soc_dapm_disable_pin_unlocked(dapm, "Microphone");
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 	pin = !!(pins & (1 << AMS_DELTA_SPEAKER));
148*4882a593Smuzhiyun 	if (pin != snd_soc_dapm_get_pin_status(dapm, "Speaker")) {
149*4882a593Smuzhiyun 		changed = 1;
150*4882a593Smuzhiyun 		if (pin)
151*4882a593Smuzhiyun 			snd_soc_dapm_enable_pin_unlocked(dapm, "Speaker");
152*4882a593Smuzhiyun 		else
153*4882a593Smuzhiyun 			snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 	pin = !!(pins & (1 << AMS_DELTA_AGC));
156*4882a593Smuzhiyun 	if (pin != ams_delta_audio_agc) {
157*4882a593Smuzhiyun 		ams_delta_audio_agc = pin;
158*4882a593Smuzhiyun 		changed = 1;
159*4882a593Smuzhiyun 		if (pin)
160*4882a593Smuzhiyun 			snd_soc_dapm_enable_pin_unlocked(dapm, "AGCIN");
161*4882a593Smuzhiyun 		else
162*4882a593Smuzhiyun 			snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (changed)
166*4882a593Smuzhiyun 		snd_soc_dapm_sync_unlocked(dapm);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	snd_soc_dapm_mutex_unlock(dapm);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	return changed;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
ams_delta_get_audio_mode(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)173*4882a593Smuzhiyun static int ams_delta_get_audio_mode(struct snd_kcontrol *kcontrol,
174*4882a593Smuzhiyun 					struct snd_ctl_elem_value *ucontrol)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
177*4882a593Smuzhiyun 	struct snd_soc_dapm_context *dapm = &card->dapm;
178*4882a593Smuzhiyun 	unsigned short pins, mode;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	pins = ((snd_soc_dapm_get_pin_status(dapm, "Mouthpiece") <<
181*4882a593Smuzhiyun 							AMS_DELTA_MOUTHPIECE) |
182*4882a593Smuzhiyun 			(snd_soc_dapm_get_pin_status(dapm, "Earpiece") <<
183*4882a593Smuzhiyun 							AMS_DELTA_EARPIECE));
184*4882a593Smuzhiyun 	if (pins)
185*4882a593Smuzhiyun 		pins |= (snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
186*4882a593Smuzhiyun 							AMS_DELTA_MICROPHONE);
187*4882a593Smuzhiyun 	else
188*4882a593Smuzhiyun 		pins = ((snd_soc_dapm_get_pin_status(dapm, "Microphone") <<
189*4882a593Smuzhiyun 							AMS_DELTA_MICROPHONE) |
190*4882a593Smuzhiyun 			(snd_soc_dapm_get_pin_status(dapm, "Speaker") <<
191*4882a593Smuzhiyun 							AMS_DELTA_SPEAKER) |
192*4882a593Smuzhiyun 			(ams_delta_audio_agc << AMS_DELTA_AGC));
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	for (mode = 0; mode < ARRAY_SIZE(ams_delta_audio_mode); mode++)
195*4882a593Smuzhiyun 		if (pins == ams_delta_audio_mode_pins[mode])
196*4882a593Smuzhiyun 			break;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if (mode >= ARRAY_SIZE(ams_delta_audio_mode))
199*4882a593Smuzhiyun 		return -EINVAL;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	ucontrol->value.enumerated.item[0] = mode;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	return 0;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun static SOC_ENUM_SINGLE_EXT_DECL(ams_delta_audio_enum,
207*4882a593Smuzhiyun 				      ams_delta_audio_mode);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun static const struct snd_kcontrol_new ams_delta_audio_controls[] = {
210*4882a593Smuzhiyun 	SOC_ENUM_EXT("Audio Mode", ams_delta_audio_enum,
211*4882a593Smuzhiyun 			ams_delta_get_audio_mode, ams_delta_set_audio_mode),
212*4882a593Smuzhiyun };
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /* Hook switch */
215*4882a593Smuzhiyun static struct snd_soc_jack ams_delta_hook_switch;
216*4882a593Smuzhiyun static struct snd_soc_jack_gpio ams_delta_hook_switch_gpios[] = {
217*4882a593Smuzhiyun 	{
218*4882a593Smuzhiyun 		.name = "hook_switch",
219*4882a593Smuzhiyun 		.report = SND_JACK_HEADSET,
220*4882a593Smuzhiyun 		.invert = 1,
221*4882a593Smuzhiyun 		.debounce_time = 150,
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun };
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun /* After we are able to control the codec over the modem,
226*4882a593Smuzhiyun  * the hook switch can be used for dynamic DAPM reconfiguration. */
227*4882a593Smuzhiyun static struct snd_soc_jack_pin ams_delta_hook_switch_pins[] = {
228*4882a593Smuzhiyun 	/* Handset */
229*4882a593Smuzhiyun 	{
230*4882a593Smuzhiyun 		.pin = "Mouthpiece",
231*4882a593Smuzhiyun 		.mask = SND_JACK_MICROPHONE,
232*4882a593Smuzhiyun 	},
233*4882a593Smuzhiyun 	{
234*4882a593Smuzhiyun 		.pin = "Earpiece",
235*4882a593Smuzhiyun 		.mask = SND_JACK_HEADPHONE,
236*4882a593Smuzhiyun 	},
237*4882a593Smuzhiyun 	/* Handsfree */
238*4882a593Smuzhiyun 	{
239*4882a593Smuzhiyun 		.pin = "Microphone",
240*4882a593Smuzhiyun 		.mask = SND_JACK_MICROPHONE,
241*4882a593Smuzhiyun 		.invert = 1,
242*4882a593Smuzhiyun 	},
243*4882a593Smuzhiyun 	{
244*4882a593Smuzhiyun 		.pin = "Speaker",
245*4882a593Smuzhiyun 		.mask = SND_JACK_HEADPHONE,
246*4882a593Smuzhiyun 		.invert = 1,
247*4882a593Smuzhiyun 	},
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun  * Modem line discipline, required for making above controls functional.
253*4882a593Smuzhiyun  * Activated from userspace with ldattach, possibly invoked from udev rule.
254*4882a593Smuzhiyun  */
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /* To actually apply any modem controlled configuration changes to the codec,
257*4882a593Smuzhiyun  * we must connect codec DAI pins to the modem for a moment.  Be careful not
258*4882a593Smuzhiyun  * to interfere with our digital mute function that shares the same hardware. */
259*4882a593Smuzhiyun static struct timer_list cx81801_timer;
260*4882a593Smuzhiyun static bool cx81801_cmd_pending;
261*4882a593Smuzhiyun static bool ams_delta_muted;
262*4882a593Smuzhiyun static DEFINE_SPINLOCK(ams_delta_lock);
263*4882a593Smuzhiyun static struct gpio_desc *gpiod_modem_codec;
264*4882a593Smuzhiyun 
cx81801_timeout(struct timer_list * unused)265*4882a593Smuzhiyun static void cx81801_timeout(struct timer_list *unused)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	int muted;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	spin_lock(&ams_delta_lock);
270*4882a593Smuzhiyun 	cx81801_cmd_pending = 0;
271*4882a593Smuzhiyun 	muted = ams_delta_muted;
272*4882a593Smuzhiyun 	spin_unlock(&ams_delta_lock);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Reconnect the codec DAI back from the modem to the CPU DAI
275*4882a593Smuzhiyun 	 * only if digital mute still off */
276*4882a593Smuzhiyun 	if (!muted)
277*4882a593Smuzhiyun 		gpiod_set_value(gpiod_modem_codec, 0);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /* Line discipline .open() */
cx81801_open(struct tty_struct * tty)281*4882a593Smuzhiyun static int cx81801_open(struct tty_struct *tty)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	int ret;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	if (!cx20442_codec)
286*4882a593Smuzhiyun 		return -ENODEV;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/*
289*4882a593Smuzhiyun 	 * Pass the codec structure pointer for use by other ldisc callbacks,
290*4882a593Smuzhiyun 	 * both the card and the codec specific parts.
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	tty->disc_data = cx20442_codec;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	ret = v253_ops.open(tty);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	if (ret < 0)
297*4882a593Smuzhiyun 		tty->disc_data = NULL;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	return ret;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /* Line discipline .close() */
cx81801_close(struct tty_struct * tty)303*4882a593Smuzhiyun static void cx81801_close(struct tty_struct *tty)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	struct snd_soc_component *component = tty->disc_data;
306*4882a593Smuzhiyun 	struct snd_soc_dapm_context *dapm = &component->card->dapm;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	del_timer_sync(&cx81801_timer);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	/* Prevent the hook switch from further changing the DAPM pins */
311*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ams_delta_hook_switch.pins);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (!component)
314*4882a593Smuzhiyun 		return;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	v253_ops.close(tty);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* Revert back to default audio input/output constellation */
319*4882a593Smuzhiyun 	snd_soc_dapm_mutex_lock(dapm);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin_unlocked(dapm, "Mouthpiece");
322*4882a593Smuzhiyun 	snd_soc_dapm_enable_pin_unlocked(dapm, "Earpiece");
323*4882a593Smuzhiyun 	snd_soc_dapm_enable_pin_unlocked(dapm, "Microphone");
324*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin_unlocked(dapm, "Speaker");
325*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin_unlocked(dapm, "AGCIN");
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	snd_soc_dapm_sync_unlocked(dapm);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	snd_soc_dapm_mutex_unlock(dapm);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun /* Line discipline .hangup() */
cx81801_hangup(struct tty_struct * tty)333*4882a593Smuzhiyun static int cx81801_hangup(struct tty_struct *tty)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	cx81801_close(tty);
336*4882a593Smuzhiyun 	return 0;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun /* Line discipline .receive_buf() */
cx81801_receive(struct tty_struct * tty,const unsigned char * cp,char * fp,int count)340*4882a593Smuzhiyun static void cx81801_receive(struct tty_struct *tty,
341*4882a593Smuzhiyun 				const unsigned char *cp, char *fp, int count)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	struct snd_soc_component *component = tty->disc_data;
344*4882a593Smuzhiyun 	const unsigned char *c;
345*4882a593Smuzhiyun 	int apply, ret;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	if (!component)
348*4882a593Smuzhiyun 		return;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	if (!component->card->pop_time) {
351*4882a593Smuzhiyun 		/* First modem response, complete setup procedure */
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 		/* Initialize timer used for config pulse generation */
354*4882a593Smuzhiyun 		timer_setup(&cx81801_timer, cx81801_timeout, 0);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 		v253_ops.receive_buf(tty, cp, fp, count);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		/* Link hook switch to DAPM pins */
359*4882a593Smuzhiyun 		ret = snd_soc_jack_add_pins(&ams_delta_hook_switch,
360*4882a593Smuzhiyun 					ARRAY_SIZE(ams_delta_hook_switch_pins),
361*4882a593Smuzhiyun 					ams_delta_hook_switch_pins);
362*4882a593Smuzhiyun 		if (ret)
363*4882a593Smuzhiyun 			dev_warn(component->dev,
364*4882a593Smuzhiyun 				"Failed to link hook switch to DAPM pins, "
365*4882a593Smuzhiyun 				"will continue with hook switch unlinked.\n");
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		return;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	v253_ops.receive_buf(tty, cp, fp, count);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	for (c = &cp[count - 1]; c >= cp; c--) {
373*4882a593Smuzhiyun 		if (*c != '\r')
374*4882a593Smuzhiyun 			continue;
375*4882a593Smuzhiyun 		/* Complete modem response received, apply config to codec */
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		spin_lock_bh(&ams_delta_lock);
378*4882a593Smuzhiyun 		mod_timer(&cx81801_timer, jiffies + msecs_to_jiffies(150));
379*4882a593Smuzhiyun 		apply = !ams_delta_muted && !cx81801_cmd_pending;
380*4882a593Smuzhiyun 		cx81801_cmd_pending = 1;
381*4882a593Smuzhiyun 		spin_unlock_bh(&ams_delta_lock);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 		/* Apply config pulse by connecting the codec to the modem
384*4882a593Smuzhiyun 		 * if not already done */
385*4882a593Smuzhiyun 		if (apply)
386*4882a593Smuzhiyun 			gpiod_set_value(gpiod_modem_codec, 1);
387*4882a593Smuzhiyun 		break;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun /* Line discipline .write_wakeup() */
cx81801_wakeup(struct tty_struct * tty)392*4882a593Smuzhiyun static void cx81801_wakeup(struct tty_struct *tty)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	v253_ops.write_wakeup(tty);
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun static struct tty_ldisc_ops cx81801_ops = {
398*4882a593Smuzhiyun 	.magic = TTY_LDISC_MAGIC,
399*4882a593Smuzhiyun 	.name = "cx81801",
400*4882a593Smuzhiyun 	.owner = THIS_MODULE,
401*4882a593Smuzhiyun 	.open = cx81801_open,
402*4882a593Smuzhiyun 	.close = cx81801_close,
403*4882a593Smuzhiyun 	.hangup = cx81801_hangup,
404*4882a593Smuzhiyun 	.receive_buf = cx81801_receive,
405*4882a593Smuzhiyun 	.write_wakeup = cx81801_wakeup,
406*4882a593Smuzhiyun };
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun  * Even if not very useful, the sound card can still work without any of the
411*4882a593Smuzhiyun  * above functonality activated.  You can still control its audio input/output
412*4882a593Smuzhiyun  * constellation and speakerphone gain from userspace by issuing AT commands
413*4882a593Smuzhiyun  * over the modem port.
414*4882a593Smuzhiyun  */
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun static struct snd_soc_ops ams_delta_ops;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun /* Digital mute implemented using modem/CPU multiplexer.
420*4882a593Smuzhiyun  * Shares hardware with codec config pulse generation */
421*4882a593Smuzhiyun static bool ams_delta_muted = 1;
422*4882a593Smuzhiyun 
ams_delta_mute(struct snd_soc_dai * dai,int mute,int direction)423*4882a593Smuzhiyun static int ams_delta_mute(struct snd_soc_dai *dai, int mute, int direction)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun 	int apply;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (ams_delta_muted == mute)
428*4882a593Smuzhiyun 		return 0;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	spin_lock_bh(&ams_delta_lock);
431*4882a593Smuzhiyun 	ams_delta_muted = mute;
432*4882a593Smuzhiyun 	apply = !cx81801_cmd_pending;
433*4882a593Smuzhiyun 	spin_unlock_bh(&ams_delta_lock);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	if (apply)
436*4882a593Smuzhiyun 		gpiod_set_value(gpiod_modem_codec, !!mute);
437*4882a593Smuzhiyun 	return 0;
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /* Our codec DAI probably doesn't have its own .ops structure */
441*4882a593Smuzhiyun static const struct snd_soc_dai_ops ams_delta_dai_ops = {
442*4882a593Smuzhiyun 	.mute_stream = ams_delta_mute,
443*4882a593Smuzhiyun 	.no_capture_mute = 1,
444*4882a593Smuzhiyun };
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /* Will be used if the codec ever has its own digital_mute function */
ams_delta_startup(struct snd_pcm_substream * substream)447*4882a593Smuzhiyun static int ams_delta_startup(struct snd_pcm_substream *substream)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun 	return ams_delta_mute(NULL, 0, substream->stream);
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun 
ams_delta_shutdown(struct snd_pcm_substream * substream)452*4882a593Smuzhiyun static void ams_delta_shutdown(struct snd_pcm_substream *substream)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun 	ams_delta_mute(NULL, 1, substream->stream);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun /*
459*4882a593Smuzhiyun  * Card initialization
460*4882a593Smuzhiyun  */
461*4882a593Smuzhiyun 
ams_delta_cx20442_init(struct snd_soc_pcm_runtime * rtd)462*4882a593Smuzhiyun static int ams_delta_cx20442_init(struct snd_soc_pcm_runtime *rtd)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
465*4882a593Smuzhiyun 	struct snd_soc_card *card = rtd->card;
466*4882a593Smuzhiyun 	struct snd_soc_dapm_context *dapm = &card->dapm;
467*4882a593Smuzhiyun 	int ret;
468*4882a593Smuzhiyun 	/* Codec is ready, now add/activate board specific controls */
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	/* Store a pointer to the codec structure for tty ldisc use */
471*4882a593Smuzhiyun 	cx20442_codec = asoc_rtd_to_codec(rtd, 0)->component;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/* Add hook switch - can be used to control the codec from userspace
474*4882a593Smuzhiyun 	 * even if line discipline fails */
475*4882a593Smuzhiyun 	ret = snd_soc_card_jack_new(card, "hook_switch", SND_JACK_HEADSET,
476*4882a593Smuzhiyun 				    &ams_delta_hook_switch, NULL, 0);
477*4882a593Smuzhiyun 	if (ret)
478*4882a593Smuzhiyun 		dev_warn(card->dev,
479*4882a593Smuzhiyun 				"Failed to allocate resources for hook switch, "
480*4882a593Smuzhiyun 				"will continue without one.\n");
481*4882a593Smuzhiyun 	else {
482*4882a593Smuzhiyun 		ret = snd_soc_jack_add_gpiods(card->dev, &ams_delta_hook_switch,
483*4882a593Smuzhiyun 					ARRAY_SIZE(ams_delta_hook_switch_gpios),
484*4882a593Smuzhiyun 					ams_delta_hook_switch_gpios);
485*4882a593Smuzhiyun 		if (ret)
486*4882a593Smuzhiyun 			dev_warn(card->dev,
487*4882a593Smuzhiyun 				"Failed to set up hook switch GPIO line, "
488*4882a593Smuzhiyun 				"will continue with hook switch inactive.\n");
489*4882a593Smuzhiyun 	}
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	gpiod_modem_codec = devm_gpiod_get(card->dev, "modem_codec",
492*4882a593Smuzhiyun 					   GPIOD_OUT_HIGH);
493*4882a593Smuzhiyun 	if (IS_ERR(gpiod_modem_codec)) {
494*4882a593Smuzhiyun 		dev_warn(card->dev, "Failed to obtain modem_codec GPIO\n");
495*4882a593Smuzhiyun 		return 0;
496*4882a593Smuzhiyun 	}
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/* Set up digital mute if not provided by the codec */
499*4882a593Smuzhiyun 	if (!codec_dai->driver->ops) {
500*4882a593Smuzhiyun 		codec_dai->driver->ops = &ams_delta_dai_ops;
501*4882a593Smuzhiyun 	} else {
502*4882a593Smuzhiyun 		ams_delta_ops.startup = ams_delta_startup;
503*4882a593Smuzhiyun 		ams_delta_ops.shutdown = ams_delta_shutdown;
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	/* Register optional line discipline for over the modem control */
507*4882a593Smuzhiyun 	ret = tty_register_ldisc(N_V253, &cx81801_ops);
508*4882a593Smuzhiyun 	if (ret) {
509*4882a593Smuzhiyun 		dev_warn(card->dev,
510*4882a593Smuzhiyun 				"Failed to register line discipline, "
511*4882a593Smuzhiyun 				"will continue without any controls.\n");
512*4882a593Smuzhiyun 		return 0;
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	/* Set up initial pin constellation */
516*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin(dapm, "Mouthpiece");
517*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin(dapm, "Speaker");
518*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin(dapm, "AGCIN");
519*4882a593Smuzhiyun 	snd_soc_dapm_disable_pin(dapm, "AGCOUT");
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	return 0;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /* DAI glue - connects codec <--> CPU */
525*4882a593Smuzhiyun SND_SOC_DAILINK_DEFS(cx20442,
526*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_CPU("omap-mcbsp.1")),
527*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_CODEC("cx20442-codec", "cx20442-voice")),
528*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_PLATFORM("omap-mcbsp.1")));
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun static struct snd_soc_dai_link ams_delta_dai_link = {
531*4882a593Smuzhiyun 	.name = "CX20442",
532*4882a593Smuzhiyun 	.stream_name = "CX20442",
533*4882a593Smuzhiyun 	.init = ams_delta_cx20442_init,
534*4882a593Smuzhiyun 	.ops = &ams_delta_ops,
535*4882a593Smuzhiyun 	.dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF |
536*4882a593Smuzhiyun 		   SND_SOC_DAIFMT_CBM_CFM,
537*4882a593Smuzhiyun 	SND_SOC_DAILINK_REG(cx20442),
538*4882a593Smuzhiyun };
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /* Audio card driver */
541*4882a593Smuzhiyun static struct snd_soc_card ams_delta_audio_card = {
542*4882a593Smuzhiyun 	.name = "AMS_DELTA",
543*4882a593Smuzhiyun 	.owner = THIS_MODULE,
544*4882a593Smuzhiyun 	.dai_link = &ams_delta_dai_link,
545*4882a593Smuzhiyun 	.num_links = 1,
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	.controls = ams_delta_audio_controls,
548*4882a593Smuzhiyun 	.num_controls = ARRAY_SIZE(ams_delta_audio_controls),
549*4882a593Smuzhiyun 	.dapm_widgets = ams_delta_dapm_widgets,
550*4882a593Smuzhiyun 	.num_dapm_widgets = ARRAY_SIZE(ams_delta_dapm_widgets),
551*4882a593Smuzhiyun 	.dapm_routes = ams_delta_audio_map,
552*4882a593Smuzhiyun 	.num_dapm_routes = ARRAY_SIZE(ams_delta_audio_map),
553*4882a593Smuzhiyun };
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /* Module init/exit */
ams_delta_probe(struct platform_device * pdev)556*4882a593Smuzhiyun static int ams_delta_probe(struct platform_device *pdev)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	struct snd_soc_card *card = &ams_delta_audio_card;
559*4882a593Smuzhiyun 	int ret;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	card->dev = &pdev->dev;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	handset_mute = devm_gpiod_get(card->dev, "handset_mute",
564*4882a593Smuzhiyun 				      GPIOD_OUT_HIGH);
565*4882a593Smuzhiyun 	if (IS_ERR(handset_mute))
566*4882a593Smuzhiyun 		return PTR_ERR(handset_mute);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	handsfree_mute = devm_gpiod_get(card->dev, "handsfree_mute",
569*4882a593Smuzhiyun 					GPIOD_OUT_HIGH);
570*4882a593Smuzhiyun 	if (IS_ERR(handsfree_mute))
571*4882a593Smuzhiyun 		return PTR_ERR(handsfree_mute);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	ret = snd_soc_register_card(card);
574*4882a593Smuzhiyun 	if (ret) {
575*4882a593Smuzhiyun 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
576*4882a593Smuzhiyun 		card->dev = NULL;
577*4882a593Smuzhiyun 		return ret;
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 	return 0;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
ams_delta_remove(struct platform_device * pdev)582*4882a593Smuzhiyun static int ams_delta_remove(struct platform_device *pdev)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun 	struct snd_soc_card *card = platform_get_drvdata(pdev);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	if (tty_unregister_ldisc(N_V253) != 0)
587*4882a593Smuzhiyun 		dev_warn(&pdev->dev,
588*4882a593Smuzhiyun 			"failed to unregister V253 line discipline\n");
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	snd_soc_unregister_card(card);
591*4882a593Smuzhiyun 	card->dev = NULL;
592*4882a593Smuzhiyun 	return 0;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun #define DRV_NAME "ams-delta-audio"
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun static struct platform_driver ams_delta_driver = {
598*4882a593Smuzhiyun 	.driver = {
599*4882a593Smuzhiyun 		.name = DRV_NAME,
600*4882a593Smuzhiyun 	},
601*4882a593Smuzhiyun 	.probe = ams_delta_probe,
602*4882a593Smuzhiyun 	.remove = ams_delta_remove,
603*4882a593Smuzhiyun };
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun module_platform_driver(ams_delta_driver);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
608*4882a593Smuzhiyun MODULE_DESCRIPTION("ALSA SoC driver for Amstrad E3 (Delta) videophone");
609*4882a593Smuzhiyun MODULE_LICENSE("GPL");
610*4882a593Smuzhiyun MODULE_ALIAS("platform:" DRV_NAME);
611