]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/soc/omap/rx51.c
Merge branch 'next' of git://git.infradead.org/users/pcmoore/selinux into next
[karo-tx-linux.git] / sound / soc / omap / rx51.c
1 /*
2  * rx51.c  --  SoC audio for Nokia RX-51
3  *
4  * Copyright (C) 2008 - 2009 Nokia Corporation
5  *
6  * Contact: Peter Ujfalusi <peter.ujfalusi@ti.com>
7  *          Eduardo Valentin <eduardo.valentin@nokia.com>
8  *          Jarkko Nikula <jarkko.nikula@bitmer.com>
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>
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"
37
38 #include <asm/mach-types.h>
39
40 #include "omap-mcbsp.h"
41
42 enum {
43         RX51_JACK_DISABLED,
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 */
47 };
48
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;
54 };
55
56 static int rx51_spk_func;
57 static int rx51_dmic_func;
58 static int rx51_jack_func;
59
60 static void rx51_ext_control(struct snd_soc_dapm_context *dapm)
61 {
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;
65
66         switch (rx51_jack_func) {
67         case RX51_JACK_TVOUT:
68                 tvout = 1;
69                 hp = 1;
70                 break;
71         case RX51_JACK_HS:
72                 hs = 1;
73         case RX51_JACK_HP:
74                 hp = 1;
75                 break;
76         }
77
78         snd_soc_dapm_mutex_lock(dapm);
79
80         if (rx51_spk_func)
81                 snd_soc_dapm_enable_pin_unlocked(dapm, "Ext Spk");
82         else
83                 snd_soc_dapm_disable_pin_unlocked(dapm, "Ext Spk");
84         if (rx51_dmic_func)
85                 snd_soc_dapm_enable_pin_unlocked(dapm, "DMic");
86         else
87                 snd_soc_dapm_disable_pin_unlocked(dapm, "DMic");
88         if (hp)
89                 snd_soc_dapm_enable_pin_unlocked(dapm, "Headphone Jack");
90         else
91                 snd_soc_dapm_disable_pin_unlocked(dapm, "Headphone Jack");
92         if (hs)
93                 snd_soc_dapm_enable_pin_unlocked(dapm, "HS Mic");
94         else
95                 snd_soc_dapm_disable_pin_unlocked(dapm, "HS Mic");
96
97         gpiod_set_value(pdata->tvout_selection_gpio, tvout);
98
99         snd_soc_dapm_sync_unlocked(dapm);
100
101         snd_soc_dapm_mutex_unlock(dapm);
102 }
103
104 static 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;
108         struct snd_soc_card *card = rtd->card;
109
110         snd_pcm_hw_constraint_minmax(runtime,
111                                      SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
112         rx51_ext_control(&card->dapm);
113
114         return 0;
115 }
116
117 static 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;
121         struct snd_soc_dai *codec_dai = rtd->codec_dai;
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
128 static struct snd_soc_ops rx51_ops = {
129         .startup = rx51_startup,
130         .hw_params = rx51_hw_params,
131 };
132
133 static 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
141 static int rx51_set_spk(struct snd_kcontrol *kcontrol,
142                         struct snd_ctl_elem_value *ucontrol)
143 {
144         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
145
146         if (rx51_spk_func == ucontrol->value.integer.value[0])
147                 return 0;
148
149         rx51_spk_func = ucontrol->value.integer.value[0];
150         rx51_ext_control(&card->dapm);
151
152         return 1;
153 }
154
155 static int rx51_spk_event(struct snd_soc_dapm_widget *w,
156                           struct snd_kcontrol *k, int event)
157 {
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));
164
165         return 0;
166 }
167
168 static int rx51_hp_event(struct snd_soc_dapm_widget *w,
169                          struct snd_kcontrol *k, int event)
170 {
171         struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
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
181 static 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
189 static int rx51_set_input(struct snd_kcontrol *kcontrol,
190                           struct snd_ctl_elem_value *ucontrol)
191 {
192         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
193
194         if (rx51_dmic_func == ucontrol->value.integer.value[0])
195                 return 0;
196
197         rx51_dmic_func = ucontrol->value.integer.value[0];
198         rx51_ext_control(&card->dapm);
199
200         return 1;
201 }
202
203 static 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
211 static int rx51_set_jack(struct snd_kcontrol *kcontrol,
212                          struct snd_ctl_elem_value *ucontrol)
213 {
214         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
215
216         if (rx51_jack_func == ucontrol->value.integer.value[0])
217                 return 0;
218
219         rx51_jack_func = ucontrol->value.integer.value[0];
220         rx51_ext_control(&card->dapm);
221
222         return 1;
223 }
224
225 static struct snd_soc_jack rx51_av_jack;
226
227 static struct snd_soc_jack_gpio rx51_av_jack_gpios[] = {
228         {
229                 .name = "avdet-gpio",
230                 .report = SND_JACK_HEADSET,
231                 .invert = 1,
232                 .debounce_time = 200,
233         },
234 };
235
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),
243 };
244
245 static const struct snd_soc_dapm_route audio_map[] = {
246         {"Ext Spk", NULL, "HPLOUT"},
247         {"Ext Spk", NULL, "HPROUT"},
248         {"Ext Spk", NULL, "HPLCOM"},
249         {"Ext Spk", NULL, "HPRCOM"},
250         {"Headphone Jack", NULL, "LLOUT"},
251         {"Headphone Jack", NULL, "RLOUT"},
252         {"FM Transmitter", NULL, "LLOUT"},
253         {"FM Transmitter", NULL, "RLOUT"},
254
255         {"DMic Rate 64", NULL, "DMic"},
256         {"DMic", NULL, "Mic Bias"},
257
258         {"b LINE2R", NULL, "MONO_LOUT"},
259         {"Earphone", NULL, "b HPLOUT"},
260
261         {"LINE1L", NULL, "HS Mic"},
262         {"HS Mic", NULL, "b Mic Bias"},
263 };
264
265 static const char * const spk_function[] = {"Off", "On"};
266 static const char * const input_function[] = {"ADC", "Digital Mic"};
267 static const char * const jack_function[] = {
268         "Off", "TV-OUT", "Headphone", "Headset"
269 };
270
271 static const struct soc_enum rx51_enum[] = {
272         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
273         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
274         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
275 };
276
277 static const struct snd_kcontrol_new aic34_rx51_controls[] = {
278         SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
279                      rx51_get_spk, rx51_set_spk),
280         SOC_ENUM_EXT("Input Select",  rx51_enum[1],
281                      rx51_get_input, rx51_set_input),
282         SOC_ENUM_EXT("Jack Function", rx51_enum[2],
283                      rx51_get_jack, rx51_set_jack),
284         SOC_DAPM_PIN_SWITCH("FM Transmitter"),
285         SOC_DAPM_PIN_SWITCH("Earphone"),
286 };
287
288 static int rx51_aic34_init(struct snd_soc_pcm_runtime *rtd)
289 {
290         struct snd_soc_codec *codec = rtd->codec;
291         struct snd_soc_card *card = rtd->card;
292         struct rx51_audio_pdata *pdata = snd_soc_card_get_drvdata(card);
293         int err;
294
295         err = tpa6130a2_add_controls(codec);
296         if (err < 0) {
297                 dev_err(card->dev, "Failed to add TPA6130A2 controls\n");
298                 return err;
299         }
300         snd_soc_limit_volume(codec, "TPA6130A2 Headphone Playback Volume", 42);
301
302         err = omap_mcbsp_st_add_controls(rtd, 2);
303         if (err < 0) {
304                 dev_err(card->dev, "Failed to add MCBSP controls\n");
305                 return err;
306         }
307
308         /* AV jack detection */
309         err = snd_soc_card_jack_new(rtd->card, "AV Jack",
310                                     SND_JACK_HEADSET | SND_JACK_VIDEOOUT,
311                                     &rx51_av_jack, NULL, 0);
312         if (err) {
313                 dev_err(card->dev, "Failed to add AV Jack\n");
314                 return err;
315         }
316
317         /* prepare gpio for snd_soc_jack_add_gpios */
318         rx51_av_jack_gpios[0].gpio = desc_to_gpio(pdata->jack_detection_gpio);
319         devm_gpiod_put(card->dev, pdata->jack_detection_gpio);
320
321         err = snd_soc_jack_add_gpios(&rx51_av_jack,
322                                      ARRAY_SIZE(rx51_av_jack_gpios),
323                                      rx51_av_jack_gpios);
324         if (err) {
325                 dev_err(card->dev, "Failed to add GPIOs\n");
326                 return err;
327         }
328
329         return err;
330 }
331
332 static int rx51_card_remove(struct snd_soc_card *card)
333 {
334         snd_soc_jack_free_gpios(&rx51_av_jack, ARRAY_SIZE(rx51_av_jack_gpios),
335                                 rx51_av_jack_gpios);
336
337         return 0;
338 }
339
340 /* Digital audio interface glue - connects codec <--> CPU */
341 static struct snd_soc_dai_link rx51_dai[] = {
342         {
343                 .name = "TLV320AIC34",
344                 .stream_name = "AIC34",
345                 .cpu_dai_name = "omap-mcbsp.2",
346                 .codec_dai_name = "tlv320aic3x-hifi",
347                 .platform_name = "omap-mcbsp.2",
348                 .codec_name = "tlv320aic3x-codec.2-0018",
349                 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
350                            SND_SOC_DAIFMT_CBM_CFM,
351                 .init = rx51_aic34_init,
352                 .ops = &rx51_ops,
353         },
354 };
355
356 static struct snd_soc_aux_dev rx51_aux_dev[] = {
357         {
358                 .name = "TLV320AIC34b",
359                 .codec_name = "tlv320aic3x-codec.2-0019",
360         },
361 };
362
363 static struct snd_soc_codec_conf rx51_codec_conf[] = {
364         {
365                 .dev_name = "tlv320aic3x-codec.2-0019",
366                 .name_prefix = "b",
367         },
368 };
369
370 /* Audio card */
371 static struct snd_soc_card rx51_sound_card = {
372         .name = "RX-51",
373         .owner = THIS_MODULE,
374         .remove = rx51_card_remove,
375         .dai_link = rx51_dai,
376         .num_links = ARRAY_SIZE(rx51_dai),
377         .aux_dev = rx51_aux_dev,
378         .num_aux_devs = ARRAY_SIZE(rx51_aux_dev),
379         .codec_conf = rx51_codec_conf,
380         .num_configs = ARRAY_SIZE(rx51_codec_conf),
381         .fully_routed = true,
382
383         .controls = aic34_rx51_controls,
384         .num_controls = ARRAY_SIZE(aic34_rx51_controls),
385         .dapm_widgets = aic34_dapm_widgets,
386         .num_dapm_widgets = ARRAY_SIZE(aic34_dapm_widgets),
387         .dapm_routes = audio_map,
388         .num_dapm_routes = ARRAY_SIZE(audio_map),
389 };
390
391 static int rx51_soc_probe(struct platform_device *pdev)
392 {
393         struct rx51_audio_pdata *pdata;
394         struct device_node *np = pdev->dev.of_node;
395         struct snd_soc_card *card = &rx51_sound_card;
396         int err;
397
398         if (!machine_is_nokia_rx51() && !of_machine_is_compatible("nokia,omap3-n900"))
399                 return -ENODEV;
400
401         card->dev = &pdev->dev;
402
403         if (np) {
404                 struct device_node *dai_node;
405
406                 dai_node = of_parse_phandle(np, "nokia,cpu-dai", 0);
407                 if (!dai_node) {
408                         dev_err(&pdev->dev, "McBSP node is not provided\n");
409                         return -EINVAL;
410                 }
411                 rx51_dai[0].cpu_dai_name = NULL;
412                 rx51_dai[0].platform_name = NULL;
413                 rx51_dai[0].cpu_of_node = dai_node;
414                 rx51_dai[0].platform_of_node = dai_node;
415
416                 dai_node = of_parse_phandle(np, "nokia,audio-codec", 0);
417                 if (!dai_node) {
418                         dev_err(&pdev->dev, "Codec node is not provided\n");
419                         return -EINVAL;
420                 }
421                 rx51_dai[0].codec_name = NULL;
422                 rx51_dai[0].codec_of_node = dai_node;
423
424                 dai_node = of_parse_phandle(np, "nokia,audio-codec", 1);
425                 if (!dai_node) {
426                         dev_err(&pdev->dev, "Auxiliary Codec node is not provided\n");
427                         return -EINVAL;
428                 }
429                 rx51_aux_dev[0].codec_name = NULL;
430                 rx51_aux_dev[0].codec_of_node = dai_node;
431                 rx51_codec_conf[0].dev_name = NULL;
432                 rx51_codec_conf[0].of_node = dai_node;
433
434                 dai_node = of_parse_phandle(np, "nokia,headphone-amplifier", 0);
435                 if (!dai_node) {
436                         dev_err(&pdev->dev, "Headphone amplifier node is not provided\n");
437                         return -EINVAL;
438                 }
439
440                 /* TODO: tpa6130a2a driver supports only a single instance, so
441                  * this driver ignores the headphone-amplifier node for now.
442                  * It's already mandatory in the DT binding to be future proof.
443                  */
444         }
445
446         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
447         if (pdata == NULL) {
448                 dev_err(card->dev, "failed to create private data\n");
449                 return -ENOMEM;
450         }
451         snd_soc_card_set_drvdata(card, pdata);
452
453         pdata->tvout_selection_gpio = devm_gpiod_get(card->dev,
454                                                      "tvout-selection",
455                                                      GPIOD_OUT_LOW);
456         if (IS_ERR(pdata->tvout_selection_gpio)) {
457                 dev_err(card->dev, "could not get tvout selection gpio\n");
458                 return PTR_ERR(pdata->tvout_selection_gpio);
459         }
460
461         pdata->jack_detection_gpio = devm_gpiod_get(card->dev,
462                                                     "jack-detection",
463                                                     GPIOD_ASIS);
464         if (IS_ERR(pdata->jack_detection_gpio)) {
465                 dev_err(card->dev, "could not get jack detection gpio\n");
466                 return PTR_ERR(pdata->jack_detection_gpio);
467         }
468
469         pdata->eci_sw_gpio = devm_gpiod_get(card->dev, "eci-switch",
470                                             GPIOD_OUT_HIGH);
471         if (IS_ERR(pdata->eci_sw_gpio)) {
472                 dev_err(card->dev, "could not get eci switch gpio\n");
473                 return PTR_ERR(pdata->eci_sw_gpio);
474         }
475
476         pdata->speaker_amp_gpio = devm_gpiod_get(card->dev,
477                                                  "speaker-amplifier",
478                                                  GPIOD_OUT_LOW);
479         if (IS_ERR(pdata->speaker_amp_gpio)) {
480                 dev_err(card->dev, "could not get speaker enable gpio\n");
481                 return PTR_ERR(pdata->speaker_amp_gpio);
482         }
483
484         err = devm_snd_soc_register_card(card->dev, card);
485         if (err) {
486                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", err);
487                 return err;
488         }
489
490         return 0;
491 }
492
493 #if defined(CONFIG_OF)
494 static const struct of_device_id rx51_audio_of_match[] = {
495         { .compatible = "nokia,n900-audio", },
496         {},
497 };
498 MODULE_DEVICE_TABLE(of, rx51_audio_of_match);
499 #endif
500
501 static struct platform_driver rx51_soc_driver = {
502         .driver = {
503                 .name = "rx51-audio",
504                 .of_match_table = of_match_ptr(rx51_audio_of_match),
505         },
506         .probe = rx51_soc_probe,
507 };
508
509 module_platform_driver(rx51_soc_driver);
510
511 MODULE_AUTHOR("Nokia Corporation");
512 MODULE_DESCRIPTION("ALSA SoC Nokia RX-51");
513 MODULE_LICENSE("GPL");
514 MODULE_ALIAS("platform:rx51-audio");