1 // SPDX-License-Identifier: GPL-2.0-only
3 * rx51.c -- SoC audio for Nokia RX-51
5 * Copyright (C) 2008 - 2009 Nokia Corporation
7 * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
8 * Eduardo Valentin <eduardo.valentin@nokia.com>
9 * Jarkko Nikula <jarkko.nikula@bitmer.com>
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>
23 #include <asm/mach-types.h>
25 #include "omap-mcbsp.h"
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 */
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
;
41 static int rx51_spk_func
;
42 static int rx51_dmic_func
;
43 static int rx51_jack_func
;
45 static void rx51_ext_control(struct snd_soc_dapm_context
*dapm
)
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;
51 switch (rx51_jack_func
) {
64 snd_soc_dapm_mutex_lock(dapm
);
67 snd_soc_dapm_enable_pin_unlocked(dapm
, "Ext Spk");
69 snd_soc_dapm_disable_pin_unlocked(dapm
, "Ext Spk");
71 snd_soc_dapm_enable_pin_unlocked(dapm
, "DMic");
73 snd_soc_dapm_disable_pin_unlocked(dapm
, "DMic");
75 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headphone Jack");
77 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headphone Jack");
79 snd_soc_dapm_enable_pin_unlocked(dapm
, "HS Mic");
81 snd_soc_dapm_disable_pin_unlocked(dapm
, "HS Mic");
83 gpiod_set_value(pdata
->tvout_selection_gpio
, tvout
);
85 snd_soc_dapm_sync_unlocked(dapm
);
87 snd_soc_dapm_mutex_unlock(dapm
);
90 static int rx51_startup(struct snd_pcm_substream
*substream
)
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
;
96 snd_pcm_hw_constraint_single(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
97 rx51_ext_control(&card
->dapm
);
102 static int rx51_hw_params(struct snd_pcm_substream
*substream
,
103 struct snd_pcm_hw_params
*params
)
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);
108 /* Set the codec system clock for DAC and ADC */
109 return snd_soc_dai_set_sysclk(codec_dai
, 0, 19200000,
113 static const struct snd_soc_ops rx51_ops
= {
114 .startup
= rx51_startup
,
115 .hw_params
= rx51_hw_params
,
118 static int rx51_get_spk(struct snd_kcontrol
*kcontrol
,
119 struct snd_ctl_elem_value
*ucontrol
)
121 ucontrol
->value
.enumerated
.item
[0] = rx51_spk_func
;
126 static int rx51_set_spk(struct snd_kcontrol
*kcontrol
,
127 struct snd_ctl_elem_value
*ucontrol
)
129 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
131 if (rx51_spk_func
== ucontrol
->value
.enumerated
.item
[0])
134 rx51_spk_func
= ucontrol
->value
.enumerated
.item
[0];
135 rx51_ext_control(&card
->dapm
);
140 static int rx51_spk_event(struct snd_soc_dapm_widget
*w
,
141 struct snd_kcontrol
*k
, int event
)
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
);
147 gpiod_set_raw_value_cansleep(pdata
->speaker_amp_gpio
,
148 !!SND_SOC_DAPM_EVENT_ON(event
));
153 static int rx51_get_input(struct snd_kcontrol
*kcontrol
,
154 struct snd_ctl_elem_value
*ucontrol
)
156 ucontrol
->value
.enumerated
.item
[0] = rx51_dmic_func
;
161 static int rx51_set_input(struct snd_kcontrol
*kcontrol
,
162 struct snd_ctl_elem_value
*ucontrol
)
164 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
166 if (rx51_dmic_func
== ucontrol
->value
.enumerated
.item
[0])
169 rx51_dmic_func
= ucontrol
->value
.enumerated
.item
[0];
170 rx51_ext_control(&card
->dapm
);
175 static int rx51_get_jack(struct snd_kcontrol
*kcontrol
,
176 struct snd_ctl_elem_value
*ucontrol
)
178 ucontrol
->value
.enumerated
.item
[0] = rx51_jack_func
;
183 static int rx51_set_jack(struct snd_kcontrol
*kcontrol
,
184 struct snd_ctl_elem_value
*ucontrol
)
186 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
188 if (rx51_jack_func
== ucontrol
->value
.enumerated
.item
[0])
191 rx51_jack_func
= ucontrol
->value
.enumerated
.item
[0];
192 rx51_ext_control(&card
->dapm
);
197 static struct snd_soc_jack rx51_av_jack
;
199 static struct snd_soc_jack_gpio rx51_av_jack_gpios
[] = {
201 .name
= "avdet-gpio",
202 .report
= SND_JACK_HEADSET
,
204 .debounce_time
= 200,
208 static 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
),
217 static 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"},
225 {"Headphone Jack", NULL
, "TPA6130A2 HPLEFT"},
226 {"Headphone Jack", NULL
, "TPA6130A2 HPRIGHT"},
227 {"TPA6130A2 LEFTIN", NULL
, "LLOUT"},
228 {"TPA6130A2 RIGHTIN", NULL
, "RLOUT"},
230 {"DMic Rate 64", NULL
, "DMic"},
231 {"DMic", NULL
, "Mic Bias"},
233 {"b LINE2R", NULL
, "MONO_LOUT"},
234 {"Earphone", NULL
, "b HPLOUT"},
236 {"LINE1L", NULL
, "HS Mic"},
237 {"HS Mic", NULL
, "b Mic Bias"},
240 static const char * const spk_function
[] = {"Off", "On"};
241 static const char * const input_function
[] = {"ADC", "Digital Mic"};
242 static const char * const jack_function
[] = {
243 "Off", "TV-OUT", "Headphone", "Headset"
246 static 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
),
252 static 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"),
263 static int rx51_aic34_init(struct snd_soc_pcm_runtime
*rtd
)
265 struct snd_soc_card
*card
= rtd
->card
;
266 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
269 snd_soc_limit_volume(card
, "TPA6130A2 Headphone Playback Volume", 42);
271 err
= omap_mcbsp_st_add_controls(rtd
, 2);
273 dev_err(card
->dev
, "Failed to add MCBSP controls\n");
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
, NULL
, 0);
282 dev_err(card
->dev
, "Failed to add AV Jack\n");
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
);
290 err
= snd_soc_jack_add_gpios(&rx51_av_jack
,
291 ARRAY_SIZE(rx51_av_jack_gpios
),
294 dev_err(card
->dev
, "Failed to add GPIOs\n");
301 /* Digital audio interface glue - connects codec <--> CPU */
302 SND_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")));
308 static struct snd_soc_dai_link rx51_dai
[] = {
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
,
316 SND_SOC_DAILINK_REG(aic34
),
320 static struct snd_soc_aux_dev rx51_aux_dev
[] = {
322 .dlc
= COMP_AUX("tlv320aic3x-codec.2-0019"),
325 .dlc
= COMP_AUX("tpa6130a2.2-0060"),
329 static struct snd_soc_codec_conf rx51_codec_conf
[] = {
331 .dlc
= COMP_CODEC_CONF("tlv320aic3x-codec.2-0019"),
335 .dlc
= COMP_CODEC_CONF("tpa6130a2.2-0060"),
336 .name_prefix
= "TPA6130A2",
341 static struct snd_soc_card rx51_sound_card
= {
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,
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
),
360 static int rx51_soc_probe(struct platform_device
*pdev
)
362 struct rx51_audio_pdata
*pdata
;
363 struct device_node
*np
= pdev
->dev
.of_node
;
364 struct snd_soc_card
*card
= &rx51_sound_card
;
367 if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
370 card
->dev
= &pdev
->dev
;
373 struct device_node
*dai_node
;
375 dai_node
= of_parse_phandle(np
, "nokia,cpu-dai", 0);
377 dev_err(&pdev
->dev
, "McBSP node is not provided\n");
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
;
385 dai_node
= of_parse_phandle(np
, "nokia,audio-codec", 0);
387 dev_err(&pdev
->dev
, "Codec node is not provided\n");
390 rx51_dai
[0].codecs
->name
= NULL
;
391 rx51_dai
[0].codecs
->of_node
= dai_node
;
393 dai_node
= of_parse_phandle(np
, "nokia,audio-codec", 1);
395 dev_err(&pdev
->dev
, "Auxiliary Codec node is not provided\n");
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
;
403 dai_node
= of_parse_phandle(np
, "nokia,headphone-amplifier", 0);
405 dev_err(&pdev
->dev
, "Headphone amplifier node is not provided\n");
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
;
414 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
418 snd_soc_card_set_drvdata(card
, pdata
);
420 pdata
->tvout_selection_gpio
= devm_gpiod_get(card
->dev
,
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
);
428 pdata
->jack_detection_gpio
= devm_gpiod_get(card
->dev
,
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
);
436 pdata
->eci_sw_gpio
= devm_gpiod_get(card
->dev
, "eci-switch",
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
);
443 pdata
->speaker_amp_gpio
= devm_gpiod_get(card
->dev
,
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
);
451 err
= devm_snd_soc_register_card(card
->dev
, card
);
453 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n", err
);
460 #if defined(CONFIG_OF)
461 static const struct of_device_id rx51_audio_of_match
[] = {
462 { .compatible
= "nokia,n900-audio", },
465 MODULE_DEVICE_TABLE(of
, rx51_audio_of_match
);
468 static struct platform_driver rx51_soc_driver
= {
470 .name
= "rx51-audio",
471 .of_match_table
= of_match_ptr(rx51_audio_of_match
),
473 .probe
= rx51_soc_probe
,
476 module_platform_driver(rx51_soc_driver
);
478 MODULE_AUTHOR("Nokia Corporation");
479 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
480 MODULE_LICENSE("GPL");
481 MODULE_ALIAS("platform:rx51-audio");