19cb2929800ad7b07867c4372ae55d4aa3c2f8d5
[h-e-n] / sound / soc / omap / rx51.c
1 /*
2  * n810.c  --  SoC audio for Nokia RX51
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/clk.h>
25 #include <linux/delay.h>
26 #include <linux/platform_device.h>
27 #include <linux/i2c/tpa6130a2.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/jack.h>
33 #include <sound/tlv.h>
34
35 #include <linux/i2c/twl4030.h>
36 #include <asm/mach-types.h>
37 #include <mach/hardware.h>
38 #include <linux/gpio.h>
39 #include <mach/mcbsp.h>
40
41 #include "omap-mcbsp.h"
42 #include "omap-pcm.h"
43 #include "../codecs/tlv320aic3x.h"
44 #include "aic34b_dummy.h"
45
46 #define RX51_CODEC_RESET_GPIO           60
47 #define RX51_TVOUT_SEL_GPIO             40
48 #define RX51_ECI_SWITCH_1_GPIO          178
49 #define RX51_ECI_SWITCH_2_GPIO          182
50 /* REVISIT: TWL4030 GPIO base in RX51. Now statically defined to 192 */
51 #define RX51_SPEAKER_AMP_TWL_GPIO       (192 + 7)
52
53 enum {
54         RX51_JACK_DISABLED,
55         RX51_JACK_HP,           /* headphone: stereo output, no mic */
56         RX51_JACK_HS,           /* headset: stereo output with mic */
57         RX51_JACK_MIC,          /* mic input only */
58         RX51_JACK_ECI,          /* ECI headset */
59         RX51_JACK_TVOUT,        /* stereo output with tv-out */
60 };
61
62 static int hp_lim = 63;
63 module_param(hp_lim, int, 0);
64
65 static int rx51_new_hw_audio;
66 static int rx51_spk_func;
67 static int rx51_jack_func;
68 static int rx51_fmtx_func;
69 static int rx51_dmic_func;
70 static int rx51_ear_func;
71 static struct snd_jack *rx51_jack;
72
73 static DEFINE_MUTEX(eci_mutex);
74 static int rx51_eci_mode = 1;
75 static int rx51_dapm_jack_bias;
76 static int tpa6130_volume = -1;
77 static int tpa6130_enable;
78 static int aic34b_volume;
79
80 static void rx51_set_eci_switches(int mode)
81 {
82         switch (mode) {
83         case 0: /* Bias off */
84         case 1: /* Bias according to rx51_dapm_jack_bias */
85         case 4: /* Bias on */
86                 /* Codec connected to mic/bias line */
87                 gpio_set_value(RX51_ECI_SWITCH_1_GPIO, 0);
88                 gpio_set_value(RX51_ECI_SWITCH_2_GPIO, 1);
89                 break;
90         case 2:
91                 /* ECI INT#2 detect connected to mic/bias line */
92                 gpio_set_value(RX51_ECI_SWITCH_1_GPIO, 0);
93                 gpio_set_value(RX51_ECI_SWITCH_2_GPIO, 0);
94                 break;
95         case 3:
96                 /* ECI RX/TX connected to mic/bias line */
97                 gpio_set_value(RX51_ECI_SWITCH_1_GPIO, 1);
98                 gpio_set_value(RX51_ECI_SWITCH_2_GPIO, 0);
99                 break;
100         }
101 }
102
103 static void rx51_set_jack_bias(void)
104 {
105         int enable_bias = 0;
106
107         mutex_lock(&eci_mutex);
108         if ((rx51_eci_mode == 1 && rx51_dapm_jack_bias) || rx51_eci_mode == 4)
109                 enable_bias = 1;
110         else if (rx51_eci_mode == 1 && rx51_jack_func == RX51_JACK_ECI)
111                 enable_bias = 1;
112         mutex_unlock(&eci_mutex);
113         if (enable_bias)
114                 aic34b_set_mic_bias(2); /* 2.5 V */
115         else
116                 aic34b_set_mic_bias(0);
117 }
118
119 static void rx51_set_jack_bias_handler(struct work_struct *unused)
120 {
121         rx51_set_jack_bias();
122 }
123 DECLARE_WORK(rx51_jack_bias_work, rx51_set_jack_bias_handler);
124
125 static void rx51_ext_control(struct snd_soc_codec *codec)
126 {
127         int hp = 0, mic = 0, tvout = 0;
128
129         switch (rx51_jack_func) {
130         case RX51_JACK_ECI:
131         case RX51_JACK_HS:
132                 mic = 1;
133         case RX51_JACK_HP:
134                 hp = 1;
135                 break;
136         case RX51_JACK_MIC:
137                 mic = 1;
138                 break;
139         case RX51_JACK_TVOUT:
140                 hp = 1;
141                 tvout = 1;
142                 break;
143         }
144
145         gpio_set_value(RX51_TVOUT_SEL_GPIO, tvout);
146
147         if (rx51_spk_func)
148                 snd_soc_dapm_enable_pin(codec, "Ext Spk");
149         else
150                 snd_soc_dapm_disable_pin(codec, "Ext Spk");
151         if (hp)
152                 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
153         else
154                 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
155         if (mic)
156                 snd_soc_dapm_enable_pin(codec, "Mic Jack");
157         else
158                 snd_soc_dapm_disable_pin(codec, "Mic Jack");
159         if (rx51_fmtx_func)
160                 snd_soc_dapm_enable_pin(codec, "FM Transmitter");
161         else
162                 snd_soc_dapm_disable_pin(codec, "FM Transmitter");
163         if (rx51_dmic_func)
164                 snd_soc_dapm_enable_pin(codec, "DMic");
165         else
166                 snd_soc_dapm_disable_pin(codec, "DMic");
167         if (rx51_ear_func)
168                 snd_soc_dapm_enable_pin(codec, "Earphone");
169         else
170                 snd_soc_dapm_disable_pin(codec, "Earphone");
171
172         snd_soc_dapm_sync(codec);
173 }
174
175 int rx51_set_eci_mode(int mode)
176 {
177         if (mode < 0 || mode > 4)
178                 return -EINVAL;
179
180         mutex_lock(&eci_mutex);
181         if (rx51_eci_mode == mode) {
182                 mutex_unlock(&eci_mutex);
183                 return 0;
184         }
185
186         rx51_eci_mode = mode;
187         rx51_set_eci_switches(rx51_eci_mode);
188         mutex_unlock(&eci_mutex);
189
190         rx51_set_jack_bias();
191
192         return 0;
193 }
194 EXPORT_SYMBOL(rx51_set_eci_mode);
195
196 static ssize_t eci_mode_show(struct device *dev, struct device_attribute *attr,
197                              char *buf)
198 {
199         return sprintf(buf, "%d\n", rx51_eci_mode);
200 }
201
202 static ssize_t eci_mode_store(struct device *dev,
203                               struct device_attribute *attr,
204                               const char *buf, size_t count)
205 {
206         int mode, retval;
207         if (sscanf(buf, "%d", &mode) != 1)
208                 return -EINVAL;
209         retval = rx51_set_eci_mode(mode);
210
211         return (retval < 0) ? retval : count;
212 }
213
214 static DEVICE_ATTR(eci_mode, S_IRUGO | S_IWUSR,
215                    eci_mode_show, eci_mode_store);
216
217 void rx51_jack_report(int status)
218 {
219         snd_jack_report(rx51_jack, status);
220 }
221 EXPORT_SYMBOL(rx51_jack_report);
222
223 static int rx51_startup(struct snd_pcm_substream *substream)
224 {
225         struct snd_pcm_runtime *runtime = substream->runtime;
226         struct snd_soc_pcm_runtime *rtd = substream->private_data;
227         struct snd_soc_codec *codec = rtd->socdev->codec;
228
229         snd_pcm_hw_constraint_minmax(runtime,
230                                      SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
231
232         rx51_ext_control(codec);
233
234         return 0;
235 }
236
237 static void rx51_shutdown(struct snd_pcm_substream *substream)
238 {
239 }
240
241 static int pre_events;
242
243 static int rx51_hw_params(struct snd_pcm_substream *substream,
244         struct snd_pcm_hw_params *params)
245 {
246         struct snd_soc_pcm_runtime *rtd = substream->private_data;
247         struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
248         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
249         int err;
250
251         if (rx51_new_hw_audio) {
252                 if (!pre_events) {
253                         pre_events = 1;
254                         err = twl4030_enable_regulator(RES_VMMC2);
255                         if (err < 0)
256                                 return err;
257                 }
258         }
259
260         /* Set codec DAI configuration */
261         err = snd_soc_dai_set_fmt(codec_dai,
262                                   SND_SOC_DAIFMT_DSP_A |
263                                   SND_SOC_DAIFMT_IB_NF |
264                                   SND_SOC_DAIFMT_CBM_CFM);
265         if (err < 0)
266                 return err;
267
268         /* Set cpu DAI configuration */
269         err = snd_soc_dai_set_fmt(cpu_dai,
270                                   SND_SOC_DAIFMT_DSP_A |
271                                   SND_SOC_DAIFMT_IB_NF |
272                                   SND_SOC_DAIFMT_CBM_CFM);
273         if (err < 0)
274                 return err;
275
276         /* Set the codec system clock for DAC and ADC */
277         return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
278                                       SND_SOC_CLOCK_IN);
279 }
280
281 static int rx51_bt_hw_params(struct snd_pcm_substream *substream,
282                              struct snd_pcm_hw_params *params)
283 {
284         struct snd_soc_pcm_runtime *rtd = substream->private_data;
285         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
286
287         /* Set cpu DAI configuration */
288         return cpu_dai->dai_ops.set_fmt(cpu_dai,
289                                         SND_SOC_DAIFMT_DSP_A |
290                                         SND_SOC_DAIFMT_IB_NF |
291                                         SND_SOC_DAIFMT_CBM_CFM);
292 }
293
294 static struct snd_soc_ops rx51_bt_ops = {
295         .hw_params = rx51_bt_hw_params,
296 };
297
298 static struct snd_soc_ops rx51_ops = {
299         .startup = rx51_startup,
300         .hw_params = rx51_hw_params,
301         .shutdown = rx51_shutdown,
302 };
303
304 static int rx51_get_spk(struct snd_kcontrol *kcontrol,
305                         struct snd_ctl_elem_value *ucontrol)
306 {
307         ucontrol->value.integer.value[0] = rx51_spk_func;
308
309         return 0;
310 }
311
312 static int rx51_set_spk(struct snd_kcontrol *kcontrol,
313                         struct snd_ctl_elem_value *ucontrol)
314 {
315         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
316
317         if (rx51_spk_func == ucontrol->value.integer.value[0])
318                 return 0;
319
320         rx51_spk_func = ucontrol->value.integer.value[0];
321         rx51_ext_control(codec);
322
323         return 1;
324 }
325
326 static int rx51_spk_event(struct snd_soc_dapm_widget *w,
327                           struct snd_kcontrol *k, int event)
328 {
329         if (SND_SOC_DAPM_EVENT_ON(event))
330                 gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 1);
331         else
332                 gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 0);
333
334         return 0;
335 }
336
337 static int rx51_get_jack(struct snd_kcontrol *kcontrol,
338                          struct snd_ctl_elem_value *ucontrol)
339 {
340         ucontrol->value.integer.value[0] = rx51_jack_func;
341
342         return 0;
343 }
344
345 static int rx51_set_jack(struct snd_kcontrol *kcontrol,
346                          struct snd_ctl_elem_value *ucontrol)
347 {
348         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
349
350         if (rx51_jack_func == ucontrol->value.integer.value[0])
351                 return 0;
352
353         rx51_jack_func = ucontrol->value.integer.value[0];
354
355         mutex_lock(&eci_mutex);
356         if (rx51_jack_func == RX51_JACK_ECI) {
357                 /* Set ECI switches according to ECI mode */
358                 rx51_set_eci_switches(rx51_eci_mode);
359                 schedule_work(&rx51_jack_bias_work);
360         } else {
361                 /*
362                  * Let the codec always be connected to mic/bias line when
363                  * jack is in non-ECI function
364                  */
365                 rx51_set_eci_switches(1);
366                 schedule_work(&rx51_jack_bias_work);
367         }
368         mutex_unlock(&eci_mutex);
369
370         rx51_ext_control(codec);
371
372         return 1;
373 }
374
375 static int rx51_jack_hp_event(struct snd_soc_dapm_widget *w,
376                               struct snd_kcontrol *k, int event)
377 {
378         /*
379          * Note: HP amp and fmtx must not be enabled at the same
380          * time. We keep a shadow copy of the desired tpa_enable value but
381          * keep the hpamp really disabled whenever fmtx is enabled. If
382          * hpamp is requested on but fmtx is enabled, hpamp is kept
383          * disabled and enabled later from rx51_set_fmtx function when
384          * user disables fmtx.
385          */
386         if (SND_SOC_DAPM_EVENT_ON(event)) {
387                 if (!rx51_fmtx_func)
388                         tpa6130a2_set_enabled(1);
389                 tpa6130_enable = 1;
390         } else {
391                 tpa6130a2_set_enabled(0);
392                 tpa6130_enable = 0;
393         }
394
395         return 0;
396 }
397
398 static int rx51_jack_mic_event(struct snd_soc_dapm_widget *w,
399                                struct snd_kcontrol *k, int event)
400 {
401         if (SND_SOC_DAPM_EVENT_ON(event))
402                 rx51_dapm_jack_bias = 1;
403         else
404                 rx51_dapm_jack_bias = 0;
405         schedule_work(&rx51_jack_bias_work);
406
407         return 0;
408 }
409
410 static int rx51_get_fmtx(struct snd_kcontrol *kcontrol,
411                          struct snd_ctl_elem_value *ucontrol)
412 {
413         ucontrol->value.integer.value[0] = rx51_fmtx_func;
414
415         return 0;
416 }
417
418 static int rx51_set_fmtx(struct snd_kcontrol *kcontrol,
419                          struct snd_ctl_elem_value *ucontrol)
420 {
421         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
422
423         if (rx51_fmtx_func == ucontrol->value.integer.value[0])
424                 return 0;
425
426         rx51_fmtx_func = ucontrol->value.integer.value[0];
427         rx51_ext_control(codec);
428
429         /* fmtx and tpa must not be enabled at the same time */
430         if (rx51_fmtx_func && tpa6130_enable)
431                 tpa6130a2_set_enabled(0);
432         if (!rx51_fmtx_func && tpa6130_enable)
433                 tpa6130a2_set_enabled(1);
434
435         return 1;
436 }
437
438 static int rx51_get_input(struct snd_kcontrol *kcontrol,
439                           struct snd_ctl_elem_value *ucontrol)
440 {
441         ucontrol->value.integer.value[0] = rx51_dmic_func;
442
443         return 0;
444 }
445
446 static int rx51_set_input(struct snd_kcontrol *kcontrol,
447                           struct snd_ctl_elem_value *ucontrol)
448 {
449         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
450
451         if (rx51_dmic_func == ucontrol->value.integer.value[0])
452                 return 0;
453
454         rx51_dmic_func = ucontrol->value.integer.value[0];
455         rx51_ext_control(codec);
456
457         return 1;
458 }
459
460 static int rx51_get_ear(struct snd_kcontrol *kcontrol,
461                         struct snd_ctl_elem_value *ucontrol)
462 {
463         ucontrol->value.integer.value[0] = rx51_ear_func;
464
465         return 0;
466 }
467
468 static int rx51_set_ear(struct snd_kcontrol *kcontrol,
469                         struct snd_ctl_elem_value *ucontrol)
470 {
471         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
472
473         if (rx51_ear_func == ucontrol->value.integer.value[0])
474                 return 0;
475
476         rx51_ear_func = ucontrol->value.integer.value[0];
477         rx51_ext_control(codec);
478
479         return 1;
480 }
481
482 static int rx51_ear_event(struct snd_soc_dapm_widget *w,
483                           struct snd_kcontrol *k, int event)
484 {
485         if (SND_SOC_DAPM_EVENT_ON(event))
486                 aic34b_ear_enable(1);
487         else
488                 aic34b_ear_enable(0);
489
490         return 0;
491 }
492
493 static int rx51_pre_spk_event(struct snd_soc_dapm_widget *w,
494                           struct snd_kcontrol *k, int event)
495 {
496         if (!rx51_new_hw_audio)
497                 return 0;
498
499         if (SND_SOC_DAPM_EVENT_ON(event))
500                 return twl4030_enable_regulator(RES_VMMC2);
501
502         return 0;
503 }
504
505 static int rx51_post_spk_event(struct snd_soc_dapm_widget *w,
506                           struct snd_kcontrol *k, int event)
507 {
508         if (!rx51_new_hw_audio)
509                 return 0;
510
511         if (!SND_SOC_DAPM_EVENT_ON(event))
512                 return twl4030_disable_regulator(RES_VMMC2);
513
514         return 0;
515 }
516
517 static int rx51_pre_event(struct snd_soc_dapm_widget *w,
518                           struct snd_kcontrol *k, int event)
519 {
520         if (!rx51_new_hw_audio)
521                 return 0;
522
523         if (SND_SOC_DAPM_EVENT_ON(event)) {
524                 if (!pre_events) {
525                         pre_events = 1;
526                         return twl4030_enable_regulator(RES_VMMC2);
527                 }
528         }
529
530         return 0;
531 }
532
533 static int rx51_post_event(struct snd_soc_dapm_widget *w,
534                           struct snd_kcontrol *k, int event)
535 {
536         if (!rx51_new_hw_audio)
537                 return 0;
538
539         if (!SND_SOC_DAPM_EVENT_ON(event)) {
540                 if (pre_events && !w->codec->active) {
541                         pre_events = 0;
542                         return twl4030_disable_regulator(RES_VMMC2);
543                 }
544         }
545
546         return 0;
547 }
548
549 enum {
550        RX51_EXT_API_TPA6130,
551        RX51_EXT_API_AIC34B,
552 };
553 #define SOC_RX51_EXT_SINGLE_TLV(xname, ext_api, max, tlv_array) \
554 { \
555         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
556         .name = xname, \
557         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
558                   SNDRV_CTL_ELEM_ACCESS_READWRITE, \
559         .tlv.p = (tlv_array), \
560         .info = rx51_ext_info_volsw, \
561         .get = rx51_ext_get_volsw, \
562         .put = rx51_ext_put_volsw, \
563         .private_value = (ext_api) << 26 | (max) << 16, \
564 }
565
566 static int rx51_ext_info_volsw(struct snd_kcontrol *kcontrol,
567                                struct snd_ctl_elem_info *uinfo)
568 {
569         int ext_api = (kcontrol->private_value >> 26) & 0x0f;
570         int max = (kcontrol->private_value >> 16) & 0xff;
571
572         if (ext_api == RX51_EXT_API_TPA6130)
573                 if (hp_lim != max && hp_lim >= 2 && hp_lim <= 63) {
574                         kcontrol->private_value &= ~(0xff << 16);
575                         kcontrol->private_value |= (hp_lim << 16);
576                         max = hp_lim;
577                 }
578
579         if (max == 1)
580                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
581         else
582                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
583
584         uinfo->count = 1;
585         uinfo->value.integer.min = 0;
586         uinfo->value.integer.max = max;
587
588         return 0;
589 }
590
591 static int rx51_ext_get_volsw(struct snd_kcontrol *kcontrol,
592                               struct snd_ctl_elem_value *ucontrol)
593 {
594         int ext_api = (kcontrol->private_value >> 26) & 0x0f;
595
596         switch (ext_api) {
597         case RX51_EXT_API_TPA6130:
598                 if (tpa6130_volume < 0)
599                         tpa6130_volume = tpa6130a2_get_volume();
600                 ucontrol->value.integer.value[0] = tpa6130_volume;
601                 break;
602         case RX51_EXT_API_AIC34B:
603                 ucontrol->value.integer.value[0] = aic34b_volume;
604                 break;
605         default:
606                 return -EINVAL;
607         }
608
609         return 0;
610 }
611
612 static int rx51_ext_put_volsw(struct snd_kcontrol *kcontrol,
613                               struct snd_ctl_elem_value *ucontrol)
614 {
615         int ext_api = (kcontrol->private_value >> 26) & 0x0f;
616         int change = 0;
617
618         switch (ext_api) {
619         case RX51_EXT_API_TPA6130:
620                 change = (tpa6130_volume != ucontrol->value.integer.value[0]);
621                 tpa6130_volume = ucontrol->value.integer.value[0];
622                 tpa6130a2_set_volume(tpa6130_volume);
623                 break;
624         case RX51_EXT_API_AIC34B:
625                 change = (aic34b_volume != ucontrol->value.integer.value[0]);
626                 aic34b_volume = ucontrol->value.integer.value[0];
627                 aic34b_set_volume(aic34b_volume);
628                 break;
629         default:
630                 return -EINVAL;
631         }
632
633         return change;
634 }
635
636 #define SOC_RX51_SINGLE_JACK_BIAS(xname) \
637 { \
638         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
639         .name = xname, \
640         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
641                   SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
642         .info = rx51_info_jack_bias, \
643         .get = rx51_get_jack_bias, \
644         .put = rx51_put_jack_bias, \
645 }
646
647 static int rx51_info_jack_bias(struct snd_kcontrol *kcontrol,
648                                struct snd_ctl_elem_info *uinfo)
649 {
650         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
651         uinfo->count = 1;
652         uinfo->value.integer.min = 0;
653         uinfo->value.integer.max = 1;
654
655         return 0;
656 }
657
658 static int rx51_get_jack_bias(struct snd_kcontrol *kcontrol,
659                               struct snd_ctl_elem_value *ucontrol)
660 {
661         ucontrol->value.integer.value[0] = (aic34b_get_mic_bias() != 0);
662
663         return 0;
664 }
665
666 static int rx51_put_jack_bias(struct snd_kcontrol *kcontrol,
667                               struct snd_ctl_elem_value *ucontrol)
668 {
669         int change, new_value;
670
671         new_value = ucontrol->value.integer.value[0];
672         change = (new_value != aic34b_get_mic_bias());
673
674         if (change) {
675                 switch (rx51_jack_func) {
676                 case RX51_JACK_ECI:
677                 case RX51_JACK_HS:
678                 case RX51_JACK_MIC:
679                         aic34b_set_mic_bias(new_value * 2); /* 2.5 V */
680                         break;
681                 default:
682                         change = 0;
683                 }
684         }
685
686         return change;
687 }
688
689 static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
690         SND_SOC_DAPM_POST("Post event", rx51_post_event),
691         SND_SOC_DAPM_SPK("Post spk", rx51_post_spk_event),
692         SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
693         SND_SOC_DAPM_SPK("Headphone Jack", rx51_jack_hp_event),
694         SND_SOC_DAPM_MIC("Mic Jack", rx51_jack_mic_event),
695         SND_SOC_DAPM_OUTPUT("FM Transmitter"),
696         SND_SOC_DAPM_MIC("DMic", NULL),
697         SND_SOC_DAPM_SPK("Earphone", rx51_ear_event),
698         SND_SOC_DAPM_SPK("Pre spk", rx51_pre_spk_event),
699         SND_SOC_DAPM_PRE("Pre event", rx51_pre_event),
700 };
701
702 static const struct snd_soc_dapm_route audio_map[] = {
703         {"Post spk", NULL, "LLOUT"},
704         {"Post spk", NULL, "RLOUT"},
705
706         {"Ext Spk", NULL, "HPLOUT"},
707         {"Ext Spk", NULL, "HPROUT"},
708
709         {"Headphone Jack", NULL, "LLOUT"},
710         {"Headphone Jack", NULL, "RLOUT"},
711         {"LINE1L", NULL, "Mic Jack"},
712
713         {"FM Transmitter", NULL, "LLOUT"},
714         {"FM Transmitter", NULL, "RLOUT"},
715
716         {"Earphone", NULL, "MONO_LOUT"},
717
718         {"DMic Rate 64", NULL, "Mic Bias 2V"},
719         {"Mic Bias 2V", NULL, "DMic"},
720
721         {"Pre spk", NULL, "LLOUT"},
722         {"Pre spk", NULL, "RLOUT"},
723 };
724
725 static const char *spk_function[] = {"Off", "On"};
726 static const char *jack_function[] = {"Off", "Headphone", "Headset",
727                                       "Mic", "ECI Headset", "TV-OUT"};
728 static const char *fmtx_function[] = {"Off", "On"};
729 static const char *input_function[] = {"ADC", "Digital Mic"};
730 static const char *ear_function[] = {"Off", "On"};
731
732 static const struct soc_enum rx51_enum[] = {
733         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
734         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
735         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(fmtx_function), fmtx_function),
736         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
737         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ear_function), ear_function),
738 };
739
740 /*
741  * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
742  * down in gain. Justify scale so that it is quite correct from -20 dB and
743  * up. This setting shows -30 dB at minimum, -12.95 dB at 49 % (actual
744  * is -10.3 dB) and 4.65 dB at maximum (actual is 4 dB).
745  */
746 static const unsigned int tpa6130_tlv[] = {
747         TLV_DB_RANGE_HEAD(10),
748         0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
749         2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
750         4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
751         6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
752         8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
753         10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
754         12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
755         14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
756         21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
757         38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
758 };
759
760 /*
761  * TLV320AIC3x output stage volumes. From -78.3 to 0 dB. Muted below -78.3 dB.
762  * Step size is approximately 0.5 dB over most of the scale but increasing
763  * near the very low levels.
764  * Define dB scale so that it is mostly correct for range about -55 to 0 dB
765  * but having increasing dB difference below that (and where it doesn't count
766  * so much). This setting shows -50 dB (actual is -50.3 dB) for register
767  * value 100 and -58.5 dB (actual is -78.3 dB) for register value 117.
768  */
769 static DECLARE_TLV_DB_SCALE(aic3x_output_stage_tlv, -5900, 50, 1);
770
771 static const struct snd_kcontrol_new aic34_rx51_controls[] = {
772         SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
773                      rx51_get_spk, rx51_set_spk),
774         SOC_ENUM_EXT("Jack Function", rx51_enum[1],
775                      rx51_get_jack, rx51_set_jack),
776         SOC_ENUM_EXT("FMTX Function", rx51_enum[2],
777                      rx51_get_fmtx, rx51_set_fmtx),
778         SOC_ENUM_EXT("Input Select",  rx51_enum[3],
779                      rx51_get_input, rx51_set_input),
780         SOC_ENUM_EXT("Earphone Function",  rx51_enum[4],
781                      rx51_get_ear, rx51_set_ear),
782         SOC_RX51_EXT_SINGLE_TLV("Headphone Playback Volume",
783                                 RX51_EXT_API_TPA6130, 63,
784                                 tpa6130_tlv),
785         SOC_RX51_EXT_SINGLE_TLV("Earphone Playback Volume",
786                                 RX51_EXT_API_AIC34B, 118,
787                                 aic3x_output_stage_tlv),
788         SOC_RX51_SINGLE_JACK_BIAS("Jack Bias Switch"),
789 };
790
791 static int rx51_aic34_init(struct snd_soc_codec *codec)
792 {
793         int i, err;
794
795         /* set up NC codec pins */
796         snd_soc_dapm_nc_pin(codec, "MIC3L");
797         snd_soc_dapm_nc_pin(codec, "MIC3R");
798         snd_soc_dapm_nc_pin(codec, "LINE1R");
799
800         /* Create jack for accessory reporting */
801         err = snd_jack_new(codec->card, "Jack", SND_JACK_MECHANICAL |
802                         SND_JACK_HEADSET | SND_JACK_AVOUT, &rx51_jack);
803         if (err < 0)
804                 return err;
805
806         /* Add RX51 specific controls */
807         for (i = 0; i < ARRAY_SIZE(aic34_rx51_controls); i++) {
808                 err = snd_ctl_add(codec->card,
809                         snd_soc_cnew(&aic34_rx51_controls[i], codec, NULL));
810                 if (err < 0)
811                         return err;
812         }
813
814         /* Add RX51 specific widgets */
815         snd_soc_dapm_new_controls(codec, aic34_dapm_widgets,
816                                   ARRAY_SIZE(aic34_dapm_widgets));
817
818         /* Set up RX51 specific audio path audio_map */
819         snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
820
821         snd_soc_dapm_enable_pin(codec, "Earphone");
822
823         snd_soc_dapm_sync(codec);
824
825         return 0;
826 }
827
828 /* Since all codec control is done by Bluetooth hardware
829    only some constrains need to be set for it */
830 struct snd_soc_dai btcodec_dai = {
831         .name = "Bluetooth codec",
832         .playback = {
833                 .stream_name = "BT Playback",
834                 .channels_min = 1,
835                 .channels_max = 1,
836                 .rates = SNDRV_PCM_RATE_8000,
837                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
838         .capture = {
839                 .stream_name = "BT Capture",
840                 .channels_min = 1,
841                 .channels_max = 1,
842                 .rates = SNDRV_PCM_RATE_8000,
843                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
844 };
845
846 /* Digital audio interface glue - connects codec <--> CPU */
847 static struct snd_soc_dai_link rx51_dai[] = {
848         {
849                 .name = "TLV320AIC34",
850                 .stream_name = "AIC34",
851                 .cpu_dai = &omap_mcbsp_dai[0],
852                 .codec_dai = &aic3x_dai,
853                 .init = rx51_aic34_init,
854                 .ops = &rx51_ops,
855         }, {
856                 .name = "Bluetooth PCM",
857                 .stream_name = "Bluetooth",
858                 .cpu_dai = &omap_mcbsp_dai[1],
859                 .codec_dai = &btcodec_dai,
860                 .ops = &rx51_bt_ops,
861         }
862 };
863
864 /* Audio machine driver */
865 static struct snd_soc_machine snd_soc_machine_rx51 = {
866         .name = "RX51",
867         .dai_link = rx51_dai,
868         .num_links = ARRAY_SIZE(rx51_dai),
869 };
870
871 /* Audio private data */
872 static struct aic3x_setup_data rx51_aic34_setup = {
873         .i2c_bus = 2,
874         .i2c_address = 0x18,
875         .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
876         .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
877 };
878
879 /* Audio subsystem */
880 static struct snd_soc_device rx51_snd_devdata = {
881         .machine = &snd_soc_machine_rx51,
882         .platform = &omap_soc_platform,
883         .codec_dev = &soc_codec_dev_aic3x,
884         .codec_data = &rx51_aic34_setup,
885 };
886
887 static struct platform_device *rx51_snd_device;
888
889 #define REMAP_OFFSET            2
890 #define DEDICATED_OFFSET        3
891 #define VMMC2_DEV_GRP           0x2B
892 #define VMMC2_285V              0x0a
893
894 static int __init rx51_soc_init(void)
895 {
896         int err;
897         struct device *dev;
898
899         if (!machine_is_nokia_rx51())
900                 return -ENODEV;
901
902         if ((system_rev >= 0x08 && system_rev <= 0x13) || /* Macros */
903                                                 system_rev >= 0x1901) {
904                 rx51_new_hw_audio = 1;
905                 err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
906                                         VMMC2_285V,
907                                         VMMC2_DEV_GRP + DEDICATED_OFFSET);
908                 err |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0xee,
909                                         VMMC2_DEV_GRP + REMAP_OFFSET);
910                 if (err) {
911                         printk(KERN_ERR "%s rx51 audio failed!\n", __func__);
912                         return -ENODEV;
913                 }
914         }
915
916         if (gpio_request(RX51_CODEC_RESET_GPIO, NULL) < 0)
917                 BUG();
918         if (gpio_request(RX51_TVOUT_SEL_GPIO, "tvout_sel") < 0)
919                 BUG();
920         if (gpio_request(RX51_ECI_SWITCH_1_GPIO, "ECI switch 1") < 0)
921                 BUG();
922         if (gpio_request(RX51_ECI_SWITCH_2_GPIO, "ECI switch 2") < 0)
923                 BUG();
924         gpio_direction_output(RX51_CODEC_RESET_GPIO, 0);
925         gpio_direction_output(RX51_TVOUT_SEL_GPIO, 0);
926         gpio_direction_output(RX51_ECI_SWITCH_1_GPIO, 0);
927         gpio_direction_output(RX51_ECI_SWITCH_2_GPIO, 1);
928
929         gpio_set_value(RX51_CODEC_RESET_GPIO, 0);
930         udelay(1);
931         gpio_set_value(RX51_CODEC_RESET_GPIO, 1);
932         msleep(1);
933
934         if (gpio_request(RX51_SPEAKER_AMP_TWL_GPIO, NULL) < 0)
935                 BUG();
936         gpio_direction_output(RX51_SPEAKER_AMP_TWL_GPIO, 0);
937
938         rx51_snd_device = platform_device_alloc("soc-audio", -1);
939         if (!rx51_snd_device)
940                 return -ENOMEM;
941
942         platform_set_drvdata(rx51_snd_device, &rx51_snd_devdata);
943         rx51_snd_devdata.dev = &rx51_snd_device->dev;
944         err = platform_device_add(rx51_snd_device);
945         if (err)
946                 goto err1;
947
948         dev = &rx51_snd_device->dev;
949
950         *(unsigned int *)rx51_dai[0].cpu_dai->private_data = 1;
951         *(unsigned int *)rx51_dai[1].cpu_dai->private_data = 2;
952
953         err = device_create_file(dev, &dev_attr_eci_mode);
954         if (err)
955                 goto err2;
956
957         return err;
958 err2:
959         platform_device_del(rx51_snd_device);
960 err1:
961         platform_device_put(rx51_snd_device);
962
963         return err;
964
965 }
966
967 static void __exit rx51_soc_exit(void)
968 {
969         platform_device_unregister(rx51_snd_device);
970 }
971
972 module_init(rx51_soc_init);
973 module_exit(rx51_soc_exit);
974
975 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
976 MODULE_DESCRIPTION("ALSA SoC Nokia RX51");
977 MODULE_LICENSE("GPL");