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>
37 #include <asm/mach-types.h>
39 #include "omap-mcbsp.h"
43 RX51_JACK_TVOUT
, /* tv-out with stereo output */
44 RX51_JACK_HP
, /* headphone: stereo output, no mic */
45 RX51_JACK_HS
, /* headset: stereo output with mic */
48 struct rx51_audio_pdata
{
49 struct gpio_desc
*tvout_selection_gpio
;
50 struct gpio_desc
*jack_detection_gpio
;
51 struct gpio_desc
*eci_sw_gpio
;
52 struct gpio_desc
*speaker_amp_gpio
;
55 static int rx51_spk_func
;
56 static int rx51_dmic_func
;
57 static int rx51_jack_func
;
59 static void rx51_ext_control(struct snd_soc_dapm_context
*dapm
)
61 struct snd_soc_card
*card
= dapm
->card
;
62 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
63 int hp
= 0, hs
= 0, tvout
= 0;
65 switch (rx51_jack_func
) {
77 snd_soc_dapm_mutex_lock(dapm
);
80 snd_soc_dapm_enable_pin_unlocked(dapm
, "Ext Spk");
82 snd_soc_dapm_disable_pin_unlocked(dapm
, "Ext Spk");
84 snd_soc_dapm_enable_pin_unlocked(dapm
, "DMic");
86 snd_soc_dapm_disable_pin_unlocked(dapm
, "DMic");
88 snd_soc_dapm_enable_pin_unlocked(dapm
, "Headphone Jack");
90 snd_soc_dapm_disable_pin_unlocked(dapm
, "Headphone Jack");
92 snd_soc_dapm_enable_pin_unlocked(dapm
, "HS Mic");
94 snd_soc_dapm_disable_pin_unlocked(dapm
, "HS Mic");
96 gpiod_set_value(pdata
->tvout_selection_gpio
, tvout
);
98 snd_soc_dapm_sync_unlocked(dapm
);
100 snd_soc_dapm_mutex_unlock(dapm
);
103 static int rx51_startup(struct snd_pcm_substream
*substream
)
105 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
106 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
107 struct snd_soc_card
*card
= rtd
->card
;
109 snd_pcm_hw_constraint_single(runtime
, SNDRV_PCM_HW_PARAM_CHANNELS
, 2);
110 rx51_ext_control(&card
->dapm
);
115 static int rx51_hw_params(struct snd_pcm_substream
*substream
,
116 struct snd_pcm_hw_params
*params
)
118 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
119 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
121 /* Set the codec system clock for DAC and ADC */
122 return snd_soc_dai_set_sysclk(codec_dai
, 0, 19200000,
126 static struct snd_soc_ops rx51_ops
= {
127 .startup
= rx51_startup
,
128 .hw_params
= rx51_hw_params
,
131 static int rx51_get_spk(struct snd_kcontrol
*kcontrol
,
132 struct snd_ctl_elem_value
*ucontrol
)
134 ucontrol
->value
.enumerated
.item
[0] = rx51_spk_func
;
139 static int rx51_set_spk(struct snd_kcontrol
*kcontrol
,
140 struct snd_ctl_elem_value
*ucontrol
)
142 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
144 if (rx51_spk_func
== ucontrol
->value
.enumerated
.item
[0])
147 rx51_spk_func
= ucontrol
->value
.enumerated
.item
[0];
148 rx51_ext_control(&card
->dapm
);
153 static int rx51_spk_event(struct snd_soc_dapm_widget
*w
,
154 struct snd_kcontrol
*k
, int event
)
156 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
157 struct snd_soc_card
*card
= dapm
->card
;
158 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
160 gpiod_set_raw_value_cansleep(pdata
->speaker_amp_gpio
,
161 !!SND_SOC_DAPM_EVENT_ON(event
));
166 static int rx51_get_input(struct snd_kcontrol
*kcontrol
,
167 struct snd_ctl_elem_value
*ucontrol
)
169 ucontrol
->value
.enumerated
.item
[0] = rx51_dmic_func
;
174 static int rx51_set_input(struct snd_kcontrol
*kcontrol
,
175 struct snd_ctl_elem_value
*ucontrol
)
177 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
179 if (rx51_dmic_func
== ucontrol
->value
.enumerated
.item
[0])
182 rx51_dmic_func
= ucontrol
->value
.enumerated
.item
[0];
183 rx51_ext_control(&card
->dapm
);
188 static int rx51_get_jack(struct snd_kcontrol
*kcontrol
,
189 struct snd_ctl_elem_value
*ucontrol
)
191 ucontrol
->value
.enumerated
.item
[0] = rx51_jack_func
;
196 static int rx51_set_jack(struct snd_kcontrol
*kcontrol
,
197 struct snd_ctl_elem_value
*ucontrol
)
199 struct snd_soc_card
*card
= snd_kcontrol_chip(kcontrol
);
201 if (rx51_jack_func
== ucontrol
->value
.enumerated
.item
[0])
204 rx51_jack_func
= ucontrol
->value
.enumerated
.item
[0];
205 rx51_ext_control(&card
->dapm
);
210 static struct snd_soc_jack rx51_av_jack
;
212 static struct snd_soc_jack_gpio rx51_av_jack_gpios
[] = {
214 .name
= "avdet-gpio",
215 .report
= SND_JACK_HEADSET
,
217 .debounce_time
= 200,
221 static const struct snd_soc_dapm_widget aic34_dapm_widgets
[] = {
222 SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event
),
223 SND_SOC_DAPM_MIC("DMic", NULL
),
224 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
225 SND_SOC_DAPM_MIC("HS Mic", NULL
),
226 SND_SOC_DAPM_LINE("FM Transmitter", NULL
),
227 SND_SOC_DAPM_SPK("Earphone", NULL
),
230 static const struct snd_soc_dapm_route audio_map
[] = {
231 {"Ext Spk", NULL
, "HPLOUT"},
232 {"Ext Spk", NULL
, "HPROUT"},
233 {"Ext Spk", NULL
, "HPLCOM"},
234 {"Ext Spk", NULL
, "HPRCOM"},
235 {"FM Transmitter", NULL
, "LLOUT"},
236 {"FM Transmitter", NULL
, "RLOUT"},
238 {"Headphone Jack", NULL
, "TPA6130A2 HPLEFT"},
239 {"Headphone Jack", NULL
, "TPA6130A2 HPRIGHT"},
240 {"TPA6130A2 LEFTIN", NULL
, "LLOUT"},
241 {"TPA6130A2 RIGHTIN", NULL
, "RLOUT"},
243 {"DMic Rate 64", NULL
, "DMic"},
244 {"DMic", NULL
, "Mic Bias"},
246 {"b LINE2R", NULL
, "MONO_LOUT"},
247 {"Earphone", NULL
, "b HPLOUT"},
249 {"LINE1L", NULL
, "HS Mic"},
250 {"HS Mic", NULL
, "b Mic Bias"},
253 static const char * const spk_function
[] = {"Off", "On"};
254 static const char * const input_function
[] = {"ADC", "Digital Mic"};
255 static const char * const jack_function
[] = {
256 "Off", "TV-OUT", "Headphone", "Headset"
259 static const struct soc_enum rx51_enum
[] = {
260 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function
), spk_function
),
261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function
), input_function
),
262 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function
), jack_function
),
265 static const struct snd_kcontrol_new aic34_rx51_controls
[] = {
266 SOC_ENUM_EXT("Speaker Function", rx51_enum
[0],
267 rx51_get_spk
, rx51_set_spk
),
268 SOC_ENUM_EXT("Input Select", rx51_enum
[1],
269 rx51_get_input
, rx51_set_input
),
270 SOC_ENUM_EXT("Jack Function", rx51_enum
[2],
271 rx51_get_jack
, rx51_set_jack
),
272 SOC_DAPM_PIN_SWITCH("FM Transmitter"),
273 SOC_DAPM_PIN_SWITCH("Earphone"),
276 static int rx51_aic34_init(struct snd_soc_pcm_runtime
*rtd
)
278 struct snd_soc_card
*card
= rtd
->card
;
279 struct rx51_audio_pdata
*pdata
= snd_soc_card_get_drvdata(card
);
282 snd_soc_limit_volume(card
, "TPA6130A2 Headphone Playback Volume", 42);
284 err
= omap_mcbsp_st_add_controls(rtd
, 2);
286 dev_err(card
->dev
, "Failed to add MCBSP controls\n");
290 /* AV jack detection */
291 err
= snd_soc_card_jack_new(rtd
->card
, "AV Jack",
292 SND_JACK_HEADSET
| SND_JACK_VIDEOOUT
,
293 &rx51_av_jack
, NULL
, 0);
295 dev_err(card
->dev
, "Failed to add AV Jack\n");
299 /* prepare gpio for snd_soc_jack_add_gpios */
300 rx51_av_jack_gpios
[0].gpio
= desc_to_gpio(pdata
->jack_detection_gpio
);
301 devm_gpiod_put(card
->dev
, pdata
->jack_detection_gpio
);
303 err
= snd_soc_jack_add_gpios(&rx51_av_jack
,
304 ARRAY_SIZE(rx51_av_jack_gpios
),
307 dev_err(card
->dev
, "Failed to add GPIOs\n");
314 static int rx51_card_remove(struct snd_soc_card
*card
)
316 snd_soc_jack_free_gpios(&rx51_av_jack
, ARRAY_SIZE(rx51_av_jack_gpios
),
322 /* Digital audio interface glue - connects codec <--> CPU */
323 static struct snd_soc_dai_link rx51_dai
[] = {
325 .name
= "TLV320AIC34",
326 .stream_name
= "AIC34",
327 .cpu_dai_name
= "omap-mcbsp.2",
328 .codec_dai_name
= "tlv320aic3x-hifi",
329 .platform_name
= "omap-mcbsp.2",
330 .codec_name
= "tlv320aic3x-codec.2-0018",
331 .dai_fmt
= SND_SOC_DAIFMT_DSP_A
| SND_SOC_DAIFMT_IB_NF
|
332 SND_SOC_DAIFMT_CBM_CFM
,
333 .init
= rx51_aic34_init
,
338 static struct snd_soc_aux_dev rx51_aux_dev
[] = {
340 .name
= "TLV320AIC34b",
341 .codec_name
= "tlv320aic3x-codec.2-0019",
344 .name
= "TPA61320A2",
345 .codec_name
= "tpa6130a2.2-0060",
349 static struct snd_soc_codec_conf rx51_codec_conf
[] = {
351 .dev_name
= "tlv320aic3x-codec.2-0019",
355 .dev_name
= "tpa6130a2.2-0060",
356 .name_prefix
= "TPA6130A2",
361 static struct snd_soc_card rx51_sound_card
= {
363 .owner
= THIS_MODULE
,
364 .remove
= rx51_card_remove
,
365 .dai_link
= rx51_dai
,
366 .num_links
= ARRAY_SIZE(rx51_dai
),
367 .aux_dev
= rx51_aux_dev
,
368 .num_aux_devs
= ARRAY_SIZE(rx51_aux_dev
),
369 .codec_conf
= rx51_codec_conf
,
370 .num_configs
= ARRAY_SIZE(rx51_codec_conf
),
371 .fully_routed
= true,
373 .controls
= aic34_rx51_controls
,
374 .num_controls
= ARRAY_SIZE(aic34_rx51_controls
),
375 .dapm_widgets
= aic34_dapm_widgets
,
376 .num_dapm_widgets
= ARRAY_SIZE(aic34_dapm_widgets
),
377 .dapm_routes
= audio_map
,
378 .num_dapm_routes
= ARRAY_SIZE(audio_map
),
381 static int rx51_soc_probe(struct platform_device
*pdev
)
383 struct rx51_audio_pdata
*pdata
;
384 struct device_node
*np
= pdev
->dev
.of_node
;
385 struct snd_soc_card
*card
= &rx51_sound_card
;
388 if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
391 card
->dev
= &pdev
->dev
;
394 struct device_node
*dai_node
;
396 dai_node
= of_parse_phandle(np
, "nokia,cpu-dai", 0);
398 dev_err(&pdev
->dev
, "McBSP node is not provided\n");
401 rx51_dai
[0].cpu_dai_name
= NULL
;
402 rx51_dai
[0].platform_name
= NULL
;
403 rx51_dai
[0].cpu_of_node
= dai_node
;
404 rx51_dai
[0].platform_of_node
= dai_node
;
406 dai_node
= of_parse_phandle(np
, "nokia,audio-codec", 0);
408 dev_err(&pdev
->dev
, "Codec node is not provided\n");
411 rx51_dai
[0].codec_name
= NULL
;
412 rx51_dai
[0].codec_of_node
= dai_node
;
414 dai_node
= of_parse_phandle(np
, "nokia,audio-codec", 1);
416 dev_err(&pdev
->dev
, "Auxiliary Codec node is not provided\n");
419 rx51_aux_dev
[0].codec_name
= NULL
;
420 rx51_aux_dev
[0].codec_of_node
= dai_node
;
421 rx51_codec_conf
[0].dev_name
= NULL
;
422 rx51_codec_conf
[0].of_node
= dai_node
;
424 dai_node
= of_parse_phandle(np
, "nokia,headphone-amplifier", 0);
426 dev_err(&pdev
->dev
, "Headphone amplifier node is not provided\n");
429 rx51_aux_dev
[1].codec_name
= NULL
;
430 rx51_aux_dev
[1].codec_of_node
= dai_node
;
431 rx51_codec_conf
[1].dev_name
= NULL
;
432 rx51_codec_conf
[1].of_node
= dai_node
;
435 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
437 dev_err(card
->dev
, "failed to create private data\n");
440 snd_soc_card_set_drvdata(card
, pdata
);
442 pdata
->tvout_selection_gpio
= devm_gpiod_get(card
->dev
,
445 if (IS_ERR(pdata
->tvout_selection_gpio
)) {
446 dev_err(card
->dev
, "could not get tvout selection gpio\n");
447 return PTR_ERR(pdata
->tvout_selection_gpio
);
450 pdata
->jack_detection_gpio
= devm_gpiod_get(card
->dev
,
453 if (IS_ERR(pdata
->jack_detection_gpio
)) {
454 dev_err(card
->dev
, "could not get jack detection gpio\n");
455 return PTR_ERR(pdata
->jack_detection_gpio
);
458 pdata
->eci_sw_gpio
= devm_gpiod_get(card
->dev
, "eci-switch",
460 if (IS_ERR(pdata
->eci_sw_gpio
)) {
461 dev_err(card
->dev
, "could not get eci switch gpio\n");
462 return PTR_ERR(pdata
->eci_sw_gpio
);
465 pdata
->speaker_amp_gpio
= devm_gpiod_get(card
->dev
,
468 if (IS_ERR(pdata
->speaker_amp_gpio
)) {
469 dev_err(card
->dev
, "could not get speaker enable gpio\n");
470 return PTR_ERR(pdata
->speaker_amp_gpio
);
473 err
= devm_snd_soc_register_card(card
->dev
, card
);
475 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n", err
);
482 #if defined(CONFIG_OF)
483 static const struct of_device_id rx51_audio_of_match
[] = {
484 { .compatible
= "nokia,n900-audio", },
487 MODULE_DEVICE_TABLE(of
, rx51_audio_of_match
);
490 static struct platform_driver rx51_soc_driver
= {
492 .name
= "rx51-audio",
493 .of_match_table
= of_match_ptr(rx51_audio_of_match
),
495 .probe
= rx51_soc_probe
,
498 module_platform_driver(rx51_soc_driver
);
500 MODULE_AUTHOR("Nokia Corporation");
501 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
502 MODULE_LICENSE("GPL");
503 MODULE_ALIAS("platform:rx51-audio");