Linux Audio

Check our new training course

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