xref: /linux/sound/soc/ti/rx51.c (revision 44f57d78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * rx51.c  --  SoC audio for Nokia RX-51
4  *
5  * Copyright (C) 2008 - 2009 Nokia Corporation
6  *
7  * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
8  *          Eduardo Valentin <eduardo.valentin@nokia.com>
9  *          Jarkko Nikula <jarkko.nikula@bitmer.com>
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
17 #include <sound/core.h>
18 #include <sound/jack.h>
19 #include <sound/pcm.h>
20 #include <sound/soc.h>
21 #include <linux/platform_data/asoc-ti-mcbsp.h>
22 
23 #include <asm/mach-types.h>
24 
25 #include "omap-mcbsp.h"
26 
27 enum {
28 	RX51_JACK_DISABLED,
29 	RX51_JACK_TVOUT,		/* tv-out with stereo output */
30 	RX51_JACK_HP,			/* headphone: stereo output, no mic */
31 	RX51_JACK_HS,			/* headset: stereo output with mic */
32 };
33 
34 struct rx51_audio_pdata {
35 	struct gpio_desc *tvout_selection_gpio;
36 	struct gpio_desc *jack_detection_gpio;
37 	struct gpio_desc *eci_sw_gpio;
38 	struct gpio_desc *speaker_amp_gpio;
39 };
40 
41 static int rx51_spk_func;
42 static int rx51_dmic_func;
43 static int rx51_jack_func;
44 
45 static void rx51_ext_control(struct snd_soc_dapm_context *dapm)
46 {
47 	struct snd_soc_card *card = dapm->card;
48 	struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
49 	int hp = 0, hs = 0, tvout = 0;
50 
51 	switch (rx51_jack_func) {
52 	case RX51_JACK_TVOUT:
53 		tvout = 1;
54 		hp = 1;
55 		break;
56 	case RX51_JACK_HS:
57 		hs = 1;
58 	case RX51_JACK_HP:
59 		hp = 1;
60 		break;
61 	}
62 
63 	snd_soc_dapm_mutex_lock(dapm);
64 
65 	if (rx51_spk_func)
66 		snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
67 	else
68 		snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
69 	if (rx51_dmic_func)
70 		snd_soc_dapm_enable_pin_unlocked(dapm, "DMic");
71 	else
72 		snd_soc_dapm_disable_pin_unlocked(dapm, "DMic");
73 	if (hp)
74 		snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
75 	else
76 		snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
77 	if (hs)
78 		snd_soc_dapm_enable_pin_unlocked(dapm, "HS Mic");
79 	else
80 		snd_soc_dapm_disable_pin_unlocked(dapm, "HS Mic");
81 
82 	gpiod_set_value(pdata->tvout_selection_gpio, tvout);
83 
84 	snd_soc_dapm_sync_unlocked(dapm);
85 
86 	snd_soc_dapm_mutex_unlock(dapm);
87 }
88 
89 static int rx51_startup(struct snd_pcm_substream *substream)
90 {
91 	struct snd_pcm_runtime *runtime = substream->runtime;
92 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
93 	struct snd_soc_card *card = rtd->card;
94 
95 	snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
96 	rx51_ext_control(&card->dapm);
97 
98 	return 0;
99 }
100 
101 static int rx51_hw_params(struct snd_pcm_substream *substream,
102 	struct snd_pcm_hw_params *params)
103 {
104 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
105 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
106 
107 	/* Set the codec system clock for DAC and ADC */
108 	return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
109 				      SND_SOC_CLOCK_IN);
110 }
111 
112 static const struct snd_soc_ops rx51_ops = {
113 	.startup = rx51_startup,
114 	.hw_params = rx51_hw_params,
115 };
116 
117 static int rx51_get_spk(struct snd_kcontrol *kcontrol,
118 			struct snd_ctl_elem_value *ucontrol)
119 {
120 	ucontrol->value.enumerated.item[0] = rx51_spk_func;
121 
122 	return 0;
123 }
124 
125 static int rx51_set_spk(struct snd_kcontrol *kcontrol,
126 			struct snd_ctl_elem_value *ucontrol)
127 {
128 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
129 
130 	if (rx51_spk_func == ucontrol->value.enumerated.item[0])
131 		return 0;
132 
133 	rx51_spk_func = ucontrol->value.enumerated.item[0];
134 	rx51_ext_control(&card->dapm);
135 
136 	return 1;
137 }
138 
139 static int rx51_spk_event(struct snd_soc_dapm_widget *w,
140 			  struct snd_kcontrol *k, int event)
141 {
142 	struct snd_soc_dapm_context *dapm = w->dapm;
143 	struct snd_soc_card *card = dapm->card;
144 	struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
145 
146 	gpiod_set_raw_value_cansleep(pdata->speaker_amp_gpio,
147 				     !!SND_SOC_DAPM_EVENT_ON(event));
148 
149 	return 0;
150 }
151 
152 static int rx51_get_input(struct snd_kcontrol *kcontrol,
153 			  struct snd_ctl_elem_value *ucontrol)
154 {
155 	ucontrol->value.enumerated.item[0] = rx51_dmic_func;
156 
157 	return 0;
158 }
159 
160 static int rx51_set_input(struct snd_kcontrol *kcontrol,
161 			  struct snd_ctl_elem_value *ucontrol)
162 {
163 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
164 
165 	if (rx51_dmic_func == ucontrol->value.enumerated.item[0])
166 		return 0;
167 
168 	rx51_dmic_func = ucontrol->value.enumerated.item[0];
169 	rx51_ext_control(&card->dapm);
170 
171 	return 1;
172 }
173 
174 static int rx51_get_jack(struct snd_kcontrol *kcontrol,
175 			 struct snd_ctl_elem_value *ucontrol)
176 {
177 	ucontrol->value.enumerated.item[0] = rx51_jack_func;
178 
179 	return 0;
180 }
181 
182 static int rx51_set_jack(struct snd_kcontrol *kcontrol,
183 			 struct snd_ctl_elem_value *ucontrol)
184 {
185 	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
186 
187 	if (rx51_jack_func == ucontrol->value.enumerated.item[0])
188 		return 0;
189 
190 	rx51_jack_func = ucontrol->value.enumerated.item[0];
191 	rx51_ext_control(&card->dapm);
192 
193 	return 1;
194 }
195 
196 static struct snd_soc_jack rx51_av_jack;
197 
198 static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = {
199 	{
200 		.name = "avdet-gpio",
201 		.report = SND_JACK_HEADSET,
202 		.invert = 1,
203 		.debounce_time = 200,
204 	},
205 };
206 
207 static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
208 	SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
209 	SND_SOC_DAPM_MIC("DMic", NULL),
210 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
211 	SND_SOC_DAPM_MIC("HS Mic", NULL),
212 	SND_SOC_DAPM_LINE("FM Transmitter", NULL),
213 	SND_SOC_DAPM_SPK("Earphone", NULL),
214 };
215 
216 static const struct snd_soc_dapm_route audio_map[] = {
217 	{"Ext Spk", NULL, "HPLOUT"},
218 	{"Ext Spk", NULL, "HPROUT"},
219 	{"Ext Spk", NULL, "HPLCOM"},
220 	{"Ext Spk", NULL, "HPRCOM"},
221 	{"FM Transmitter", NULL, "LLOUT"},
222 	{"FM Transmitter", NULL, "RLOUT"},
223 
224 	{"Headphone Jack", NULL, "TPA6130A2 HPLEFT"},
225 	{"Headphone Jack", NULL, "TPA6130A2 HPRIGHT"},
226 	{"TPA6130A2 LEFTIN", NULL, "LLOUT"},
227 	{"TPA6130A2 RIGHTIN", NULL, "RLOUT"},
228 
229 	{"DMic Rate 64", NULL, "DMic"},
230 	{"DMic", NULL, "Mic Bias"},
231 
232 	{"b LINE2R", NULL, "MONO_LOUT"},
233 	{"Earphone", NULL, "b HPLOUT"},
234 
235 	{"LINE1L", NULL, "HS Mic"},
236 	{"HS Mic", NULL, "b Mic Bias"},
237 };
238 
239 static const char * const spk_function[] = {"Off", "On"};
240 static const char * const input_function[] = {"ADC", "Digital Mic"};
241 static const char * const jack_function[] = {
242 	"Off", "TV-OUT", "Headphone", "Headset"
243 };
244 
245 static const struct soc_enum rx51_enum[] = {
246 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
247 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
248 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
249 };
250 
251 static const struct snd_kcontrol_new aic34_rx51_controls[] = {
252 	SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
253 		     rx51_get_spk, rx51_set_spk),
254 	SOC_ENUM_EXT("Input Select",  rx51_enum[1],
255 		     rx51_get_input, rx51_set_input),
256 	SOC_ENUM_EXT("Jack Function", rx51_enum[2],
257 		     rx51_get_jack, rx51_set_jack),
258 	SOC_DAPM_PIN_SWITCH("FM Transmitter"),
259 	SOC_DAPM_PIN_SWITCH("Earphone"),
260 };
261 
262 static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd)
263 {
264 	struct snd_soc_card *card = rtd->card;
265 	struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
266 	int err;
267 
268 	snd_soc_limit_volume(card, "TPA6130A2 Headphone Playback Volume", 42);
269 
270 	err = omap_mcbsp_st_add_controls(rtd, 2);
271 	if (err < 0) {
272 		dev_err(card->dev, "Failed to add MCBSP controls\n");
273 		return err;
274 	}
275 
276 	/* AV jack detection */
277 	err = snd_soc_card_jack_new(rtd->card, "AV Jack",
278 				    SND_JACK_HEADSET | SND_JACK_VIDEOOUT,
279 				    &rx51_av_jack, NULL, 0);
280 	if (err) {
281 		dev_err(card->dev, "Failed to add AV Jack\n");
282 		return err;
283 	}
284 
285 	/* prepare gpio for snd_soc_jack_add_gpios */
286 	rx51_av_jack_gpios[0].gpio = desc_to_gpio(pdata->jack_detection_gpio);
287 	devm_gpiod_put(card->dev, pdata->jack_detection_gpio);
288 
289 	err = snd_soc_jack_add_gpios(&rx51_av_jack,
290 				     ARRAY_SIZE(rx51_av_jack_gpios),
291 				     rx51_av_jack_gpios);
292 	if (err) {
293 		dev_err(card->dev, "Failed to add GPIOs\n");
294 		return err;
295 	}
296 
297 	return err;
298 }
299 
300 /* Digital audio interface glue - connects codec <--> CPU */
301 static struct snd_soc_dai_link rx51_dai[] = {
302 	{
303 		.name = "TLV320AIC34",
304 		.stream_name = "AIC34",
305 		.cpu_dai_name = "omap-mcbsp.2",
306 		.codec_dai_name = "tlv320aic3x-hifi",
307 		.platform_name = "omap-mcbsp.2",
308 		.codec_name = "tlv320aic3x-codec.2-0018",
309 		.dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
310 			   SND_SOC_DAIFMT_CBM_CFM,
311 		.init = rx51_aic34_init,
312 		.ops = &rx51_ops,
313 	},
314 };
315 
316 static struct snd_soc_aux_dev rx51_aux_dev[] = {
317 	{
318 		.name = "TLV320AIC34b",
319 		.codec_name = "tlv320aic3x-codec.2-0019",
320 	},
321 	{
322 		.name = "TPA61320A2",
323 		.codec_name = "tpa6130a2.2-0060",
324 	},
325 };
326 
327 static struct snd_soc_codec_conf rx51_codec_conf[] = {
328 	{
329 		.dev_name = "tlv320aic3x-codec.2-0019",
330 		.name_prefix = "b",
331 	},
332 	{
333 		.dev_name = "tpa6130a2.2-0060",
334 		.name_prefix = "TPA6130A2",
335 	},
336 };
337 
338 /* Audio card */
339 static struct snd_soc_card rx51_sound_card = {
340 	.name = "RX-51",
341 	.owner = THIS_MODULE,
342 	.dai_link = rx51_dai,
343 	.num_links = ARRAY_SIZE(rx51_dai),
344 	.aux_dev = rx51_aux_dev,
345 	.num_aux_devs = ARRAY_SIZE(rx51_aux_dev),
346 	.codec_conf = rx51_codec_conf,
347 	.num_configs = ARRAY_SIZE(rx51_codec_conf),
348 	.fully_routed = true,
349 
350 	.controls = aic34_rx51_controls,
351 	.num_controls = ARRAY_SIZE(aic34_rx51_controls),
352 	.dapm_widgets = aic34_dapm_widgets,
353 	.num_dapm_widgets = ARRAY_SIZE(aic34_dapm_widgets),
354 	.dapm_routes = audio_map,
355 	.num_dapm_routes = ARRAY_SIZE(audio_map),
356 };
357 
358 static int rx51_soc_probe(struct platform_device *pdev)
359 {
360 	struct rx51_audio_pdata *pdata;
361 	struct device_node *np = pdev->dev.of_node;
362 	struct snd_soc_card *card = &rx51_sound_card;
363 	int err;
364 
365 	if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
366 		return -ENODEV;
367 
368 	card->dev = &pdev->dev;
369 
370 	if (np) {
371 		struct device_node *dai_node;
372 
373 		dai_node = of_parse_phandle(np, "nokia,cpu-dai", 0);
374 		if (!dai_node) {
375 			dev_err(&pdev->dev, "McBSP node is not provided\n");
376 			return -EINVAL;
377 		}
378 		rx51_dai[0].cpu_dai_name = NULL;
379 		rx51_dai[0].platform_name = NULL;
380 		rx51_dai[0].cpu_of_node = dai_node;
381 		rx51_dai[0].platform_of_node = dai_node;
382 
383 		dai_node = of_parse_phandle(np, "nokia,audio-codec", 0);
384 		if (!dai_node) {
385 			dev_err(&pdev->dev, "Codec node is not provided\n");
386 			return -EINVAL;
387 		}
388 		rx51_dai[0].codec_name = NULL;
389 		rx51_dai[0].codec_of_node = dai_node;
390 
391 		dai_node = of_parse_phandle(np, "nokia,audio-codec", 1);
392 		if (!dai_node) {
393 			dev_err(&pdev->dev, "Auxiliary Codec node is not provided\n");
394 			return -EINVAL;
395 		}
396 		rx51_aux_dev[0].codec_name = NULL;
397 		rx51_aux_dev[0].codec_of_node = dai_node;
398 		rx51_codec_conf[0].dev_name = NULL;
399 		rx51_codec_conf[0].of_node = dai_node;
400 
401 		dai_node = of_parse_phandle(np, "nokia,headphone-amplifier", 0);
402 		if (!dai_node) {
403 			dev_err(&pdev->dev, "Headphone amplifier node is not provided\n");
404 			return -EINVAL;
405 		}
406 		rx51_aux_dev[1].codec_name = NULL;
407 		rx51_aux_dev[1].codec_of_node = dai_node;
408 		rx51_codec_conf[1].dev_name = NULL;
409 		rx51_codec_conf[1].of_node = dai_node;
410 	}
411 
412 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
413 	if (pdata == NULL)
414 		return -ENOMEM;
415 
416 	snd_soc_card_set_drvdata(card, pdata);
417 
418 	pdata->tvout_selection_gpio = devm_gpiod_get(card->dev,
419 						     "tvout-selection",
420 						     GPIOD_OUT_LOW);
421 	if (IS_ERR(pdata->tvout_selection_gpio)) {
422 		dev_err(card->dev, "could not get tvout selection gpio\n");
423 		return PTR_ERR(pdata->tvout_selection_gpio);
424 	}
425 
426 	pdata->jack_detection_gpio = devm_gpiod_get(card->dev,
427 						    "jack-detection",
428 						    GPIOD_ASIS);
429 	if (IS_ERR(pdata->jack_detection_gpio)) {
430 		dev_err(card->dev, "could not get jack detection gpio\n");
431 		return PTR_ERR(pdata->jack_detection_gpio);
432 	}
433 
434 	pdata->eci_sw_gpio = devm_gpiod_get(card->dev, "eci-switch",
435 					    GPIOD_OUT_HIGH);
436 	if (IS_ERR(pdata->eci_sw_gpio)) {
437 		dev_err(card->dev, "could not get eci switch gpio\n");
438 		return PTR_ERR(pdata->eci_sw_gpio);
439 	}
440 
441 	pdata->speaker_amp_gpio = devm_gpiod_get(card->dev,
442 						 "speaker-amplifier",
443 						 GPIOD_OUT_LOW);
444 	if (IS_ERR(pdata->speaker_amp_gpio)) {
445 		dev_err(card->dev, "could not get speaker enable gpio\n");
446 		return PTR_ERR(pdata->speaker_amp_gpio);
447 	}
448 
449 	err = devm_snd_soc_register_card(card->dev, card);
450 	if (err) {
451 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", err);
452 		return err;
453 	}
454 
455 	return 0;
456 }
457 
458 #if defined(CONFIG_OF)
459 static const struct of_device_id rx51_audio_of_match[] = {
460 	{ .compatible = "nokia,n900-audio", },
461 	{},
462 };
463 MODULE_DEVICE_TABLE(of, rx51_audio_of_match);
464 #endif
465 
466 static struct platform_driver rx51_soc_driver = {
467 	.driver = {
468 		.name = "rx51-audio",
469 		.of_match_table = of_match_ptr(rx51_audio_of_match),
470 	},
471 	.probe = rx51_soc_probe,
472 };
473 
474 module_platform_driver(rx51_soc_driver);
475 
476 MODULE_AUTHOR("Nokia Corporation");
477 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
478 MODULE_LICENSE("GPL");
479 MODULE_ALIAS("platform:rx51-audio");
480