2 * rx51.c -- SoC audio for Nokia RX-51
4 * Copyright (C) 2008 - 2009 Nokia Corporation
6 * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
7 * Eduardo Valentin <eduardo.valentin@nokia.com>
8 * Jarkko Nikula <jarkko.nikula@bitmer.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 #include <linux/delay.h>
27 #include <linux/gpio.h>
28 #include <linux/platform_device.h>
29 #include <linux/gpio/consumer.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include <sound/pcm.h>
34 #include <sound/soc.h>
35 #include <linux/platform_data/asoc-ti-mcbsp.h>
36 #include "../codecs/tpa6130a2.h"
38 #include <asm/mach-types.h>
40 #include "omap-mcbsp.h"
44 RX51_JACK_TVOUT
, /* tv-out with stereo output */
45 RX51_JACK_HP
, /* headphone: stereo output, no mic */
46 RX51_JACK_HS
, /* headset: stereo output with mic */
49 struct rx51_audio_pdata
{
50 struct gpio_desc
*tvout_selection_gpio
;
51 struct gpio_desc
*jack_detection_gpio
;
52 struct gpio_desc
*eci_sw_gpio
;
53 struct gpio_desc
*speaker_amp_gpio
;
56 static int rx51_spk_func
;
57 static int rx51_dmic_func
;
58 static int rx51_jack_func
;
60 static void rx51_ext_control(struct snd_soc_dapm_context
*dapm
)
62 struct snd_soc_card
*card
= dapm
->card
;
63 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
64 int hp
= 0, hs
= 0, tvout
= 0;
66 switch (rx51_jack_func
) {
78 snd_soc_dapm_mutex_lock(dapm
);
81 snd_soc_dapm_enable_pin_unlocked(dapm
, "Ext Spk");
83 snd_soc_dapm_disable_pin_unlocked(dapm
, "Ext Spk");
85 snd_soc_dapm_enable_pin_unlocked(dapm
, "DMic");
87 snd_soc_dapm_disable_pin_unlocked(dapm
, "DMic");
89 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headphone Jack");
91 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headphone Jack");
93 snd_soc_dapm_enable_pin_unlocked(dapm
, "HS Mic");
95 snd_soc_dapm_disable_pin_unlocked(dapm
, "HS Mic");
97 gpiod_set_value(pdata
->tvout_selection_gpio
, tvout
);
99 snd_soc_dapm_sync_unlocked(dapm
);
101 snd_soc_dapm_mutex_unlock(dapm
);
104 static int rx51_startup(struct snd_pcm_substream
*substream
)
106 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
107 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
108 struct snd_soc_card
*card
= rtd
->card
;
110 snd_pcm_hw_constraint_minmax(runtime
,
111 SNDRV_PCM_HW_PARAM_CHANNELS
, 2, 2);
112 rx51_ext_control(&card
->dapm
);
117 static int rx51_hw_params(struct snd_pcm_substream
*substream
,
118 struct snd_pcm_hw_params
*params
)
120 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
121 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
123 /* Set the codec system clock for DAC and ADC */
124 return snd_soc_dai_set_sysclk(codec_dai
, 0, 19200000,
128 static struct snd_soc_ops rx51_ops
= {
129 .startup
= rx51_startup
,
130 .hw_params
= rx51_hw_params
,
133 static int rx51_get_spk(struct snd_kcontrol
*kcontrol
,
134 struct snd_ctl_elem_value
*ucontrol
)
136 ucontrol
->value
.integer
.value
[0] = rx51_spk_func
;
141 static int rx51_set_spk(struct snd_kcontrol
*kcontrol
,
142 struct snd_ctl_elem_value
*ucontrol
)
144 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
146 if (rx51_spk_func
== ucontrol
->value
.integer
.value
[0])
149 rx51_spk_func
= ucontrol
->value
.integer
.value
[0];
150 rx51_ext_control(&card
->dapm
);
155 static int rx51_spk_event(struct snd_soc_dapm_widget
*w
,
156 struct snd_kcontrol
*k
, int event
)
158 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
159 struct snd_soc_card
*card
= dapm
->card
;
160 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
162 gpiod_set_raw_value_cansleep(pdata
->speaker_amp_gpio
,
163 !!SND_SOC_DAPM_EVENT_ON(event
));
168 static int rx51_hp_event(struct snd_soc_dapm_widget
*w
,
169 struct snd_kcontrol
*k
, int event
)
171 struct snd_soc_codec
*codec
= w
->dapm
->codec
;
173 if (SND_SOC_DAPM_EVENT_ON(event
))
174 tpa6130a2_stereo_enable(codec
, 1);
176 tpa6130a2_stereo_enable(codec
, 0);
181 static int rx51_get_input(struct snd_kcontrol
*kcontrol
,
182 struct snd_ctl_elem_value
*ucontrol
)
184 ucontrol
->value
.integer
.value
[0] = rx51_dmic_func
;
189 static int rx51_set_input(struct snd_kcontrol
*kcontrol
,
190 struct snd_ctl_elem_value
*ucontrol
)
192 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
194 if (rx51_dmic_func
== ucontrol
->value
.integer
.value
[0])
197 rx51_dmic_func
= ucontrol
->value
.integer
.value
[0];
198 rx51_ext_control(&card
->dapm
);
203 static int rx51_get_jack(struct snd_kcontrol
*kcontrol
,
204 struct snd_ctl_elem_value
*ucontrol
)
206 ucontrol
->value
.integer
.value
[0] = rx51_jack_func
;
211 static int rx51_set_jack(struct snd_kcontrol
*kcontrol
,
212 struct snd_ctl_elem_value
*ucontrol
)
214 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
216 if (rx51_jack_func
== ucontrol
->value
.integer
.value
[0])
219 rx51_jack_func
= ucontrol
->value
.integer
.value
[0];
220 rx51_ext_control(&card
->dapm
);
225 static struct snd_soc_jack rx51_av_jack
;
227 static struct snd_soc_jack_gpio rx51_av_jack_gpios
[] = {
229 .name
= "avdet-gpio",
230 .report
= SND_JACK_HEADSET
,
232 .debounce_time
= 200,
236 static const struct snd_soc_dapm_widget aic34_dapm_widgets
[] = {
237 SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event
),
238 SND_SOC_DAPM_MIC("DMic", NULL
),
239 SND_SOC_DAPM_HP("Headphone Jack", rx51_hp_event
),
240 SND_SOC_DAPM_MIC("HS Mic", NULL
),
241 SND_SOC_DAPM_LINE("FM Transmitter", NULL
),
242 SND_SOC_DAPM_SPK("Earphone", NULL
),
245 static const struct snd_soc_dapm_route audio_map
[] = {
246 {"Ext Spk", NULL
, "HPLOUT"},
247 {"Ext Spk", NULL
, "HPROUT"},
248 {"Headphone Jack", NULL
, "LLOUT"},
249 {"Headphone Jack", NULL
, "RLOUT"},
250 {"FM Transmitter", NULL
, "LLOUT"},
251 {"FM Transmitter", NULL
, "RLOUT"},
253 {"DMic Rate 64", NULL
, "Mic Bias"},
254 {"Mic Bias", NULL
, "DMic"},
256 {"b LINE2R", NULL
, "MONO_LOUT"},
257 {"Earphone", NULL
, "b HPLOUT"},
259 {"LINE1L", NULL
, "b Mic Bias"},
260 {"b Mic Bias", NULL
, "HS Mic"}
263 static const char * const spk_function
[] = {"Off", "On"};
264 static const char * const input_function
[] = {"ADC", "Digital Mic"};
265 static const char * const jack_function
[] = {
266 "Off", "TV-OUT", "Headphone", "Headset"
269 static const struct soc_enum rx51_enum
[] = {
270 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function
), spk_function
),
271 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function
), input_function
),
272 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function
), jack_function
),
275 static const struct snd_kcontrol_new aic34_rx51_controls
[] = {
276 SOC_ENUM_EXT("Speaker Function", rx51_enum
[0],
277 rx51_get_spk
, rx51_set_spk
),
278 SOC_ENUM_EXT("Input Select", rx51_enum
[1],
279 rx51_get_input
, rx51_set_input
),
280 SOC_ENUM_EXT("Jack Function", rx51_enum
[2],
281 rx51_get_jack
, rx51_set_jack
),
282 SOC_DAPM_PIN_SWITCH("FM Transmitter"),
283 SOC_DAPM_PIN_SWITCH("Earphone"),
286 static int rx51_aic34_init(struct snd_soc_pcm_runtime
*rtd
)
288 struct snd_soc_codec
*codec
= rtd
->codec
;
289 struct snd_soc_card
*card
= rtd
->card
;
290 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
292 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
295 /* Set up NC codec pins */
296 snd_soc_dapm_nc_pin(dapm
, "MIC3L");
297 snd_soc_dapm_nc_pin(dapm
, "MIC3R");
298 snd_soc_dapm_nc_pin(dapm
, "LINE1R");
300 err
= tpa6130a2_add_controls(codec
);
302 dev_err(card
->dev
, "Failed to add TPA6130A2 controls\n");
305 snd_soc_limit_volume(codec
, "TPA6130A2 Headphone Playback Volume", 42);
307 err
= omap_mcbsp_st_add_controls(rtd
, 2);
309 dev_err(card
->dev
, "Failed to add MCBSP controls\n");
313 /* AV jack detection */
314 err
= snd_soc_jack_new(codec
, "AV Jack",
315 SND_JACK_HEADSET
| SND_JACK_VIDEOOUT
,
318 dev_err(card
->dev
, "Failed to add AV Jack\n");
322 /* prepare gpio for snd_soc_jack_add_gpios */
323 rx51_av_jack_gpios
[0].gpio
= desc_to_gpio(pdata
->jack_detection_gpio
);
324 devm_gpiod_put(card
->dev
, pdata
->jack_detection_gpio
);
326 err
= snd_soc_jack_add_gpios(&rx51_av_jack
,
327 ARRAY_SIZE(rx51_av_jack_gpios
),
330 dev_err(card
->dev
, "Failed to add GPIOs\n");
337 static int rx51_card_remove(struct snd_soc_card
*card
)
339 snd_soc_jack_free_gpios(&rx51_av_jack
, ARRAY_SIZE(rx51_av_jack_gpios
),
345 /* Digital audio interface glue - connects codec <--> CPU */
346 static struct snd_soc_dai_link rx51_dai
[] = {
348 .name
= "TLV320AIC34",
349 .stream_name
= "AIC34",
350 .cpu_dai_name
= "omap-mcbsp.2",
351 .codec_dai_name
= "tlv320aic3x-hifi",
352 .platform_name
= "omap-mcbsp.2",
353 .codec_name
= "tlv320aic3x-codec.2-0018",
354 .dai_fmt
= SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_IB_NF
|
355 SND_SOC_DAIFMT_CBM_CFM
,
356 .init
= rx51_aic34_init
,
361 static struct snd_soc_aux_dev rx51_aux_dev
[] = {
363 .name
= "TLV320AIC34b",
364 .codec_name
= "tlv320aic3x-codec.2-0019",
368 static struct snd_soc_codec_conf rx51_codec_conf
[] = {
370 .dev_name
= "tlv320aic3x-codec.2-0019",
376 static struct snd_soc_card rx51_sound_card
= {
378 .owner
= THIS_MODULE
,
379 .remove
= rx51_card_remove
,
380 .dai_link
= rx51_dai
,
381 .num_links
= ARRAY_SIZE(rx51_dai
),
382 .aux_dev
= rx51_aux_dev
,
383 .num_aux_devs
= ARRAY_SIZE(rx51_aux_dev
),
384 .codec_conf
= rx51_codec_conf
,
385 .num_configs
= ARRAY_SIZE(rx51_codec_conf
),
387 .controls
= aic34_rx51_controls
,
388 .num_controls
= ARRAY_SIZE(aic34_rx51_controls
),
389 .dapm_widgets
= aic34_dapm_widgets
,
390 .num_dapm_widgets
= ARRAY_SIZE(aic34_dapm_widgets
),
391 .dapm_routes
= audio_map
,
392 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
395 static int rx51_soc_probe(struct platform_device
*pdev
)
397 struct rx51_audio_pdata
*pdata
;
398 struct device_node
*np
= pdev
->dev
.of_node
;
399 struct snd_soc_card
*card
= &rx51_sound_card
;
402 if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
405 card
->dev
= &pdev
->dev
;
408 struct device_node
*dai_node
;
410 dai_node
= of_parse_phandle(np
, "nokia,cpu-dai", 0);
412 dev_err(&pdev
->dev
, "McBSP node is not provided\n");
415 rx51_dai
[0].cpu_dai_name
= NULL
;
416 rx51_dai
[0].platform_name
= NULL
;
417 rx51_dai
[0].cpu_of_node
= dai_node
;
418 rx51_dai
[0].platform_of_node
= dai_node
;
420 dai_node
= of_parse_phandle(np
, "nokia,audio-codec", 0);
422 dev_err(&pdev
->dev
, "Codec node is not provided\n");
425 rx51_dai
[0].codec_name
= NULL
;
426 rx51_dai
[0].codec_of_node
= dai_node
;
428 dai_node
= of_parse_phandle(np
, "nokia,audio-codec", 1);
430 dev_err(&pdev
->dev
, "Auxiliary Codec node is not provided\n");
433 rx51_aux_dev
[0].codec_name
= NULL
;
434 rx51_aux_dev
[0].codec_of_node
= dai_node
;
435 rx51_codec_conf
[0].dev_name
= NULL
;
436 rx51_codec_conf
[0].of_node
= dai_node
;
438 dai_node
= of_parse_phandle(np
, "nokia,headphone-amplifier", 0);
440 dev_err(&pdev
->dev
, "Headphone amplifier node is not provided\n");
444 /* TODO: tpa6130a2a driver supports only a single instance, so
445 * this driver ignores the headphone-amplifier node for now.
446 * It's already mandatory in the DT binding to be future proof.
450 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
452 dev_err(card
->dev
, "failed to create private data\n");
455 snd_soc_card_set_drvdata(card
, pdata
);
457 pdata
->tvout_selection_gpio
= devm_gpiod_get(card
->dev
,
459 if (IS_ERR(pdata
->tvout_selection_gpio
)) {
460 dev_err(card
->dev
, "could not get tvout selection gpio\n");
461 return PTR_ERR(pdata
->tvout_selection_gpio
);
464 err
= gpiod_direction_output(pdata
->tvout_selection_gpio
, 0);
466 dev_err(card
->dev
, "could not setup tvout selection gpio\n");
470 pdata
->jack_detection_gpio
= devm_gpiod_get(card
->dev
,
472 if (IS_ERR(pdata
->jack_detection_gpio
)) {
473 dev_err(card
->dev
, "could not get jack detection gpio\n");
474 return PTR_ERR(pdata
->jack_detection_gpio
);
477 pdata
->eci_sw_gpio
= devm_gpiod_get(card
->dev
, "eci-switch");
478 if (IS_ERR(pdata
->eci_sw_gpio
)) {
479 dev_err(card
->dev
, "could not get eci switch gpio\n");
480 return PTR_ERR(pdata
->eci_sw_gpio
);
483 err
= gpiod_direction_output(pdata
->eci_sw_gpio
, 1);
485 dev_err(card
->dev
, "could not setup eci switch gpio\n");
489 pdata
->speaker_amp_gpio
= devm_gpiod_get(card
->dev
,
490 "speaker-amplifier");
491 if (IS_ERR(pdata
->speaker_amp_gpio
)) {
492 dev_err(card
->dev
, "could not get speaker enable gpio\n");
493 return PTR_ERR(pdata
->speaker_amp_gpio
);
496 err
= gpiod_direction_output(pdata
->speaker_amp_gpio
, 0);
498 dev_err(card
->dev
, "could not setup speaker enable gpio\n");
502 err
= devm_snd_soc_register_card(card
->dev
, card
);
504 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n", err
);
511 #if defined(CONFIG_OF)
512 static const struct of_device_id rx51_audio_of_match
[] = {
513 { .compatible
= "nokia,n900-audio", },
516 MODULE_DEVICE_TABLE(of
, rx51_audio_of_match
);
519 static struct platform_driver rx51_soc_driver
= {
521 .name
= "rx51-audio",
522 .owner
= THIS_MODULE
,
523 .of_match_table
= of_match_ptr(rx51_audio_of_match
),
525 .probe
= rx51_soc_probe
,
528 module_platform_driver(rx51_soc_driver
);
530 MODULE_AUTHOR("Nokia Corporation");
531 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
532 MODULE_LICENSE("GPL");
533 MODULE_ALIAS("platform:rx51-audio");