ASoC: omap: Amstrad E3 needs TTY support for codec
[deliverable/linux.git] / sound / soc / omap / rx51.c
CommitLineData
49100c98
JN
1/*
2 * rx51.c -- SoC audio for Nokia RX-51
3 *
4 * Copyright (C) 2008 - 2009 Nokia Corporation
5 *
0ac3a014 6 * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
49100c98 7 * Eduardo Valentin <eduardo.valentin@nokia.com>
7ec41ee5 8 * Jarkko Nikula <jarkko.nikula@bitmer.com>
49100c98
JN
9 *
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.
13 *
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.
18 *
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
22 * 02110-1301 USA
23 *
24 */
25
26#include <linux/delay.h>
27#include <linux/gpio.h>
28#include <linux/platform_device.h>
386e81ab 29#include <linux/gpio/consumer.h>
da155d5b 30#include <linux/module.h>
49100c98 31#include <sound/core.h>
8c523115 32#include <sound/jack.h>
49100c98
JN
33#include <sound/pcm.h>
34#include <sound/soc.h>
2203747c 35#include <linux/platform_data/asoc-ti-mcbsp.h>
48529b3b 36#include "../codecs/tpa6130a2.h"
49100c98
JN
37
38#include <asm/mach-types.h>
39
40#include "omap-mcbsp.h"
49100c98 41
4eb54703
JN
42enum {
43 RX51_JACK_DISABLED,
48529b3b
JN
44 RX51_JACK_TVOUT, /* tv-out with stereo output */
45 RX51_JACK_HP, /* headphone: stereo output, no mic */
31164c7c 46 RX51_JACK_HS, /* headset: stereo output with mic */
4eb54703
JN
47};
48
386e81ab
SR
49struct 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;
54};
55
49100c98
JN
56static int rx51_spk_func;
57static int rx51_dmic_func;
4eb54703 58static int rx51_jack_func;
49100c98 59
022658be 60static void rx51_ext_control(struct snd_soc_dapm_context *dapm)
49100c98 61{
386e81ab
SR
62 struct snd_soc_card *card = dapm->card;
63 struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
31164c7c 64 int hp = 0, hs = 0, tvout = 0;
48529b3b
JN
65
66 switch (rx51_jack_func) {
67 case RX51_JACK_TVOUT:
68 tvout = 1;
31164c7c
JN
69 hp = 1;
70 break;
71 case RX51_JACK_HS:
72 hs = 1;
48529b3b
JN
73 case RX51_JACK_HP:
74 hp = 1;
75 break;
76 }
ce6120cc 77
9dd2bb3f
CK
78 snd_soc_dapm_mutex_lock(dapm);
79
49100c98 80 if (rx51_spk_func)
9dd2bb3f 81 snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
49100c98 82 else
9dd2bb3f 83 snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
49100c98 84 if (rx51_dmic_func)
9dd2bb3f 85 snd_soc_dapm_enable_pin_unlocked(dapm, "DMic");
49100c98 86 else
9dd2bb3f 87 snd_soc_dapm_disable_pin_unlocked(dapm, "DMic");
48529b3b 88 if (hp)
9dd2bb3f 89 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
48529b3b 90 else
9dd2bb3f 91 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
31164c7c 92 if (hs)
9dd2bb3f 93 snd_soc_dapm_enable_pin_unlocked(dapm, "HS Mic");
31164c7c 94 else
9dd2bb3f 95 snd_soc_dapm_disable_pin_unlocked(dapm, "HS Mic");
49100c98 96
386e81ab 97 gpiod_set_value(pdata->tvout_selection_gpio, tvout);
4eb54703 98
9dd2bb3f
CK
99 snd_soc_dapm_sync_unlocked(dapm);
100
101 snd_soc_dapm_mutex_unlock(dapm);
49100c98
JN
102}
103
104static int rx51_startup(struct snd_pcm_substream *substream)
105{
106 struct snd_pcm_runtime *runtime = substream->runtime;
107 struct snd_soc_pcm_runtime *rtd = substream->private_data;
022658be 108 struct snd_soc_card *card = rtd->card;
49100c98
JN
109
110 snd_pcm_hw_constraint_minmax(runtime,
111 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
022658be 112 rx51_ext_control(&card->dapm);
49100c98
JN
113
114 return 0;
115}
116
117static int rx51_hw_params(struct snd_pcm_substream *substream,
118 struct snd_pcm_hw_params *params)
119{
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 121 struct snd_soc_dai *codec_dai = rtd->codec_dai;
49100c98
JN
122
123 /* Set the codec system clock for DAC and ADC */
124 return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
125 SND_SOC_CLOCK_IN);
126}
127
128static struct snd_soc_ops rx51_ops = {
129 .startup = rx51_startup,
130 .hw_params = rx51_hw_params,
131};
132
133static int rx51_get_spk(struct snd_kcontrol *kcontrol,
134 struct snd_ctl_elem_value *ucontrol)
135{
136 ucontrol->value.integer.value[0] = rx51_spk_func;
137
138 return 0;
139}
140
141static int rx51_set_spk(struct snd_kcontrol *kcontrol,
142 struct snd_ctl_elem_value *ucontrol)
143{
022658be 144 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
49100c98
JN
145
146 if (rx51_spk_func == ucontrol->value.integer.value[0])
147 return 0;
148
149 rx51_spk_func = ucontrol->value.integer.value[0];
022658be 150 rx51_ext_control(&card->dapm);
49100c98
JN
151
152 return 1;
153}
154
155static int rx51_spk_event(struct snd_soc_dapm_widget *w,
156 struct snd_kcontrol *k, int event)
157{
386e81ab
SR
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);
161
162 gpiod_set_raw_value_cansleep(pdata->speaker_amp_gpio,
163 !!SND_SOC_DAPM_EVENT_ON(event));
49100c98
JN
164
165 return 0;
166}
167
48529b3b
JN
168static int rx51_hp_event(struct snd_soc_dapm_widget *w,
169 struct snd_kcontrol *k, int event)
170{
171 struct snd_soc_codec *codec = w->dapm->codec;
172
173 if (SND_SOC_DAPM_EVENT_ON(event))
174 tpa6130a2_stereo_enable(codec, 1);
175 else
176 tpa6130a2_stereo_enable(codec, 0);
177
178 return 0;
179}
180
49100c98
JN
181static int rx51_get_input(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
183{
184 ucontrol->value.integer.value[0] = rx51_dmic_func;
185
186 return 0;
187}
188
189static int rx51_set_input(struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
191{
022658be 192 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
49100c98
JN
193
194 if (rx51_dmic_func == ucontrol->value.integer.value[0])
195 return 0;
196
197 rx51_dmic_func = ucontrol->value.integer.value[0];
022658be 198 rx51_ext_control(&card->dapm);
49100c98
JN
199
200 return 1;
201}
202
4eb54703
JN
203static int rx51_get_jack(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_value *ucontrol)
205{
206 ucontrol->value.integer.value[0] = rx51_jack_func;
207
208 return 0;
209}
210
211static int rx51_set_jack(struct snd_kcontrol *kcontrol,
212 struct snd_ctl_elem_value *ucontrol)
213{
022658be 214 struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
4eb54703
JN
215
216 if (rx51_jack_func == ucontrol->value.integer.value[0])
217 return 0;
218
219 rx51_jack_func = ucontrol->value.integer.value[0];
022658be 220 rx51_ext_control(&card->dapm);
4eb54703
JN
221
222 return 1;
223}
224
8c523115
JN
225static struct snd_soc_jack rx51_av_jack;
226
227static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = {
228 {
8c523115 229 .name = "avdet-gpio",
17840619 230 .report = SND_JACK_HEADSET,
8c523115
JN
231 .invert = 1,
232 .debounce_time = 200,
233 },
234};
235
49100c98
JN
236static 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),
48529b3b 239 SND_SOC_DAPM_HP("Headphone Jack", rx51_hp_event),
31164c7c 240 SND_SOC_DAPM_MIC("HS Mic", NULL),
9d7e584b 241 SND_SOC_DAPM_LINE("FM Transmitter", NULL),
8cda9751
JN
242 SND_SOC_DAPM_SPK("Earphone", NULL),
243};
244
49100c98
JN
245static const struct snd_soc_dapm_route audio_map[] = {
246 {"Ext Spk", NULL, "HPLOUT"},
247 {"Ext Spk", NULL, "HPROUT"},
48529b3b
JN
248 {"Headphone Jack", NULL, "LLOUT"},
249 {"Headphone Jack", NULL, "RLOUT"},
9d7e584b
JN
250 {"FM Transmitter", NULL, "LLOUT"},
251 {"FM Transmitter", NULL, "RLOUT"},
49100c98 252
e2e8bfdf
HG
253 {"DMic Rate 64", NULL, "Mic Bias"},
254 {"Mic Bias", NULL, "DMic"},
49100c98 255
8cda9751
JN
256 {"b LINE2R", NULL, "MONO_LOUT"},
257 {"Earphone", NULL, "b HPLOUT"},
31164c7c 258
e2e8bfdf
HG
259 {"LINE1L", NULL, "b Mic Bias"},
260 {"b Mic Bias", NULL, "HS Mic"}
8cda9751
JN
261};
262
441dc45a
SR
263static const char * const spk_function[] = {"Off", "On"};
264static const char * const input_function[] = {"ADC", "Digital Mic"};
265static const char * const jack_function[] = {
266 "Off", "TV-OUT", "Headphone", "Headset"
267};
49100c98
JN
268
269static 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),
4eb54703 272 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
49100c98
JN
273};
274
275static 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),
4eb54703
JN
280 SOC_ENUM_EXT("Jack Function", rx51_enum[2],
281 rx51_get_jack, rx51_set_jack),
9d7e584b 282 SOC_DAPM_PIN_SWITCH("FM Transmitter"),
8cda9751
JN
283 SOC_DAPM_PIN_SWITCH("Earphone"),
284};
285
f0fba2ad 286static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd)
49100c98 287{
f0fba2ad 288 struct snd_soc_codec *codec = rtd->codec;
386e81ab
SR
289 struct snd_soc_card *card = codec->card;
290 struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
291
ce6120cc 292 struct snd_soc_dapm_context *dapm = &codec->dapm;
49100c98
JN
293 int err;
294
295 /* Set up NC codec pins */
ce6120cc
LG
296 snd_soc_dapm_nc_pin(dapm, "MIC3L");
297 snd_soc_dapm_nc_pin(dapm, "MIC3R");
298 snd_soc_dapm_nc_pin(dapm, "LINE1R");
49100c98 299
48529b3b 300 err = tpa6130a2_add_controls(codec);
0265e1ae
SR
301 if (err < 0) {
302 dev_err(card->dev, "Failed to add TPA6130A2 controls\n");
48529b3b 303 return err;
0265e1ae 304 }
48529b3b
JN
305 snd_soc_limit_volume(codec, "TPA6130A2 Headphone Playback Volume", 42);
306
0a17a370 307 err = omap_mcbsp_st_add_controls(rtd, 2);
0265e1ae
SR
308 if (err < 0) {
309 dev_err(card->dev, "Failed to add MCBSP controls\n");
fa4d1f59 310 return err;
0265e1ae 311 }
fa4d1f59 312
8c523115 313 /* AV jack detection */
f0fba2ad 314 err = snd_soc_jack_new(codec, "AV Jack",
17840619
JN
315 SND_JACK_HEADSET | SND_JACK_VIDEOOUT,
316 &rx51_av_jack);
0265e1ae
SR
317 if (err) {
318 dev_err(card->dev, "Failed to add AV Jack\n");
8c523115 319 return err;
0265e1ae 320 }
386e81ab
SR
321
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);
325
8c523115
JN
326 err = snd_soc_jack_add_gpios(&rx51_av_jack,
327 ARRAY_SIZE(rx51_av_jack_gpios),
328 rx51_av_jack_gpios);
0265e1ae
SR
329 if (err) {
330 dev_err(card->dev, "Failed to add GPIOs\n");
331 return err;
332 }
8c523115
JN
333
334 return err;
49100c98
JN
335}
336
337/* Digital audio interface glue - connects codec <--> CPU */
338static struct snd_soc_dai_link rx51_dai[] = {
339 {
340 .name = "TLV320AIC34",
341 .stream_name = "AIC34",
45656b44 342 .cpu_dai_name = "omap-mcbsp.2",
f0fba2ad 343 .codec_dai_name = "tlv320aic3x-hifi",
aac25145 344 .platform_name = "omap-mcbsp.2",
f0fba2ad 345 .codec_name = "tlv320aic3x-codec.2-0018",
cf9feff2
JN
346 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
347 SND_SOC_DAIFMT_CBM_CFM,
49100c98
JN
348 .init = rx51_aic34_init,
349 .ops = &rx51_ops,
350 },
351};
352
a0e17b4e 353static struct snd_soc_aux_dev rx51_aux_dev[] = {
8cda9751
JN
354 {
355 .name = "TLV320AIC34b",
356 .codec_name = "tlv320aic3x-codec.2-0019",
8cda9751
JN
357 },
358};
359
360static struct snd_soc_codec_conf rx51_codec_conf[] = {
361 {
362 .dev_name = "tlv320aic3x-codec.2-0019",
363 .name_prefix = "b",
364 },
365};
366
49100c98
JN
367/* Audio card */
368static struct snd_soc_card rx51_sound_card = {
369 .name = "RX-51",
b425b884 370 .owner = THIS_MODULE,
49100c98
JN
371 .dai_link = rx51_dai,
372 .num_links = ARRAY_SIZE(rx51_dai),
8cda9751
JN
373 .aux_dev = rx51_aux_dev,
374 .num_aux_devs = ARRAY_SIZE(rx51_aux_dev),
375 .codec_conf = rx51_codec_conf,
376 .num_configs = ARRAY_SIZE(rx51_codec_conf),
81fc5dd4
LPC
377
378 .controls = aic34_rx51_controls,
379 .num_controls = ARRAY_SIZE(aic34_rx51_controls),
380 .dapm_widgets = aic34_dapm_widgets,
381 .num_dapm_widgets = ARRAY_SIZE(aic34_dapm_widgets),
382 .dapm_routes = audio_map,
383 .num_dapm_routes = ARRAY_SIZE(audio_map),
49100c98
JN
384};
385
a7d52028 386static int rx51_soc_probe(struct platform_device *pdev)
49100c98 387{
386e81ab 388 struct rx51_audio_pdata *pdata;
d052a3d6 389 struct device_node *np = pdev->dev.of_node;
a7d52028 390 struct snd_soc_card *card = &rx51_sound_card;
49100c98
JN
391 int err;
392
ea2409ba 393 if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
49100c98
JN
394 return -ENODEV;
395
a7d52028 396 card->dev = &pdev->dev;
49100c98 397
d052a3d6
SR
398 if (np) {
399 struct device_node *dai_node;
400
401 dai_node = of_parse_phandle(np, "nokia,cpu-dai", 0);
402 if (!dai_node) {
403 dev_err(&pdev->dev, "McBSP node is not provided\n");
404 return -EINVAL;
405 }
406 rx51_dai[0].cpu_dai_name = NULL;
407 rx51_dai[0].platform_name = NULL;
408 rx51_dai[0].cpu_of_node = dai_node;
409 rx51_dai[0].platform_of_node = dai_node;
410
411 dai_node = of_parse_phandle(np, "nokia,audio-codec", 0);
412 if (!dai_node) {
413 dev_err(&pdev->dev, "Codec node is not provided\n");
414 return -EINVAL;
415 }
416 rx51_dai[0].codec_name = NULL;
417 rx51_dai[0].codec_of_node = dai_node;
418
419 dai_node = of_parse_phandle(np, "nokia,audio-codec", 1);
420 if (!dai_node) {
421 dev_err(&pdev->dev, "Auxiliary Codec node is not provided\n");
422 return -EINVAL;
423 }
424 rx51_aux_dev[0].codec_name = NULL;
425 rx51_aux_dev[0].codec_of_node = dai_node;
426 rx51_codec_conf[0].dev_name = NULL;
427 rx51_codec_conf[0].of_node = dai_node;
428
429 dai_node = of_parse_phandle(np, "nokia,headphone-amplifier", 0);
430 if (!dai_node) {
431 dev_err(&pdev->dev, "Headphone amplifier node is not provided\n");
432 return -EINVAL;
433 }
434
435 /* TODO: tpa6130a2a driver supports only a single instance, so
436 * this driver ignores the headphone-amplifier node for now.
437 * It's already mandatory in the DT binding to be future proof.
438 */
439 }
440
386e81ab
SR
441 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
442 if (pdata == NULL) {
443 dev_err(card->dev, "failed to create private data\n");
444 return -ENOMEM;
445 }
446 snd_soc_card_set_drvdata(card, pdata);
447
448 pdata->tvout_selection_gpio = devm_gpiod_get(card->dev,
449 "tvout-selection");
450 if (IS_ERR(pdata->tvout_selection_gpio)) {
451 dev_err(card->dev, "could not get tvout selection gpio\n");
452 return PTR_ERR(pdata->tvout_selection_gpio);
453 }
454
455 err = gpiod_direction_output(pdata->tvout_selection_gpio, 0);
456 if (err) {
457 dev_err(card->dev, "could not setup tvout selection gpio\n");
458 return err;
459 }
460
461 pdata->jack_detection_gpio = devm_gpiod_get(card->dev,
462 "jack-detection");
463 if (IS_ERR(pdata->jack_detection_gpio)) {
464 dev_err(card->dev, "could not get jack detection gpio\n");
465 return PTR_ERR(pdata->jack_detection_gpio);
466 }
467
468 pdata->eci_sw_gpio = devm_gpiod_get(card->dev, "eci-switch");
469 if (IS_ERR(pdata->eci_sw_gpio)) {
470 dev_err(card->dev, "could not get eci switch gpio\n");
471 return PTR_ERR(pdata->eci_sw_gpio);
472 }
473
474 err = gpiod_direction_output(pdata->eci_sw_gpio, 1);
475 if (err) {
476 dev_err(card->dev, "could not setup eci switch gpio\n");
477 return err;
478 }
479
480 pdata->speaker_amp_gpio = devm_gpiod_get(card->dev,
481 "speaker-amplifier");
482 if (IS_ERR(pdata->speaker_amp_gpio)) {
483 dev_err(card->dev, "could not get speaker enable gpio\n");
484 return PTR_ERR(pdata->speaker_amp_gpio);
485 }
486
487 err = gpiod_direction_output(pdata->speaker_amp_gpio, 0);
488 if (err) {
489 dev_err(card->dev, "could not setup speaker enable gpio\n");
490 return err;
491 }
492
a7d52028
SR
493 err = devm_snd_soc_register_card(card->dev, card);
494 if (err) {
495 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", err);
386e81ab 496 return err;
a7d52028 497 }
49100c98
JN
498
499 return 0;
49100c98
JN
500}
501
a7d52028 502static int rx51_soc_remove(struct platform_device *pdev)
49100c98 503{
8c523115
JN
504 snd_soc_jack_free_gpios(&rx51_av_jack, ARRAY_SIZE(rx51_av_jack_gpios),
505 rx51_av_jack_gpios);
506
a7d52028 507 return 0;
49100c98
JN
508}
509
d052a3d6
SR
510#if defined(CONFIG_OF)
511static const struct of_device_id rx51_audio_of_match[] = {
512 { .compatible = "nokia,n900-audio", },
513 {},
514};
515MODULE_DEVICE_TABLE(of, rx51_audio_of_match);
516#endif
517
a7d52028
SR
518static struct platform_driver rx51_soc_driver = {
519 .driver = {
520 .name = "rx51-audio",
521 .owner = THIS_MODULE,
d052a3d6 522 .of_match_table = of_match_ptr(rx51_audio_of_match),
a7d52028
SR
523 },
524 .probe = rx51_soc_probe,
525 .remove = rx51_soc_remove,
526};
527
528module_platform_driver(rx51_soc_driver);
49100c98
JN
529
530MODULE_AUTHOR("Nokia Corporation");
531MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
532MODULE_LICENSE("GPL");
beab3da1 533MODULE_ALIAS("platform:rx51-audio");
This page took 0.19098 seconds and 5 git commands to generate.