Added de-emphasis filter functions and speaker protection.
[aic34-eq] / kernel-2.6.28 / 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         return 0;
234 }
235
236 static void rx51_shutdown(struct snd_pcm_substream *substream)
237 {
238 }
239
240 static int pre_events;
241
242 static int rx51_hw_params(struct snd_pcm_substream *substream,
243         struct snd_pcm_hw_params *params)
244 {
245         struct snd_soc_pcm_runtime *rtd = substream->private_data;
246         struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
247         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
248         int err;
249
250         if (rx51_new_hw_audio) {
251                 if (!pre_events) {
252                         pre_events = 1;
253                         err = twl4030_enable_regulator(RES_VMMC2);
254                         if (err < 0)
255                                 return err;
256                 }
257         }
258
259         /* Set codec DAI configuration */
260         err = snd_soc_dai_set_fmt(codec_dai,
261                                   SND_SOC_DAIFMT_DSP_A |
262                                   SND_SOC_DAIFMT_IB_NF |
263                                   SND_SOC_DAIFMT_CBM_CFM);
264         if (err < 0)
265                 return err;
266
267         /* Set cpu DAI configuration */
268         err = snd_soc_dai_set_fmt(cpu_dai,
269                                   SND_SOC_DAIFMT_DSP_A |
270                                   SND_SOC_DAIFMT_IB_NF |
271                                   SND_SOC_DAIFMT_CBM_CFM);
272         if (err < 0)
273                 return err;
274
275         /* Set the codec system clock for DAC and ADC */
276         return snd_soc_dai_set_sysclk(codec_dai, 0, 19200000,
277                                       SND_SOC_CLOCK_IN);
278 }
279
280 static int rx51_bt_hw_params(struct snd_pcm_substream *substream,
281                              struct snd_pcm_hw_params *params)
282 {
283         struct snd_soc_pcm_runtime *rtd = substream->private_data;
284         struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
285
286         /* Set cpu DAI configuration */
287         return cpu_dai->dai_ops.set_fmt(cpu_dai,
288                                         SND_SOC_DAIFMT_DSP_A |
289                                         SND_SOC_DAIFMT_IB_NF |
290                                         SND_SOC_DAIFMT_CBM_CFM);
291 }
292
293 static struct snd_soc_ops rx51_bt_ops = {
294         .hw_params = rx51_bt_hw_params,
295 };
296
297 static struct snd_soc_ops rx51_ops = {
298         .startup = rx51_startup,
299         .hw_params = rx51_hw_params,
300         .shutdown = rx51_shutdown,
301 };
302
303 static int rx51_get_spk(struct snd_kcontrol *kcontrol,
304                         struct snd_ctl_elem_value *ucontrol)
305 {
306         ucontrol->value.integer.value[0] = rx51_spk_func;
307
308         return 0;
309 }
310
311 static int rx51_set_spk(struct snd_kcontrol *kcontrol,
312                         struct snd_ctl_elem_value *ucontrol)
313 {
314         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
315
316         if (rx51_spk_func == ucontrol->value.integer.value[0])
317                 return 0;
318
319         rx51_spk_func = ucontrol->value.integer.value[0];
320         rx51_ext_control(codec);
321
322         return 1;
323 }
324
325 static int rx51_spk_event(struct snd_soc_dapm_widget *w,
326                           struct snd_kcontrol *k, int event)
327 {
328         if (SND_SOC_DAPM_EVENT_ON(event)){
329                 aic3x_deemph_set_state(w->codec, 1);
330                 gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 1);
331         printk("MNZ: Speaker Amp on!\n");
332         } else {
333                 aic3x_deemph_set_state(w->codec, 0);
334                 gpio_set_value(RX51_SPEAKER_AMP_TWL_GPIO, 0);
335         printk("MNZ: Speaker Amp off!\n");
336         }
337         return 0;
338 }
339
340 static int rx51_get_jack(struct snd_kcontrol *kcontrol,
341                          struct snd_ctl_elem_value *ucontrol)
342 {
343         ucontrol->value.integer.value[0] = rx51_jack_func;
344
345         return 0;
346 }
347
348 static int rx51_set_jack(struct snd_kcontrol *kcontrol,
349                          struct snd_ctl_elem_value *ucontrol)
350 {
351         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
352
353         if (rx51_jack_func == ucontrol->value.integer.value[0])
354                 return 0;
355
356         rx51_jack_func = ucontrol->value.integer.value[0];
357
358         mutex_lock(&eci_mutex);
359         if (rx51_jack_func == RX51_JACK_ECI) {
360                 /* Set ECI switches according to ECI mode */
361                 rx51_set_eci_switches(rx51_eci_mode);
362                 schedule_work(&rx51_jack_bias_work);
363         } else {
364                 /*
365                  * Let the codec always be connected to mic/bias line when
366                  * jack is in non-ECI function
367                  */
368                 rx51_set_eci_switches(1);
369                 schedule_work(&rx51_jack_bias_work);
370         }
371         mutex_unlock(&eci_mutex);
372
373         rx51_ext_control(codec);
374
375         return 1;
376 }
377
378 static int rx51_jack_hp_event(struct snd_soc_dapm_widget *w,
379                               struct snd_kcontrol *k, int event)
380 {
381         /*
382          * Note: HP amp and fmtx must not be enabled at the same
383          * time. We keep a shadow copy of the desired tpa_enable value but
384          * keep the hpamp really disabled whenever fmtx is enabled. If
385          * hpamp is requested on but fmtx is enabled, hpamp is kept
386          * disabled and enabled later from rx51_set_fmtx function when
387          * user disables fmtx.
388          */
389         if (SND_SOC_DAPM_EVENT_ON(event)) {
390                 if (!rx51_fmtx_func)
391                         tpa6130a2_set_enabled(1);
392                 tpa6130_enable = 1;
393         } else {
394                 tpa6130a2_set_enabled(0);
395                 tpa6130_enable = 0;
396         }
397
398         return 0;
399 }
400
401 static int rx51_jack_mic_event(struct snd_soc_dapm_widget *w,
402                                struct snd_kcontrol *k, int event)
403 {
404         if (SND_SOC_DAPM_EVENT_ON(event))
405                 rx51_dapm_jack_bias = 1;
406         else
407                 rx51_dapm_jack_bias = 0;
408         schedule_work(&rx51_jack_bias_work);
409
410         return 0;
411 }
412
413 static int rx51_get_fmtx(struct snd_kcontrol *kcontrol,
414                          struct snd_ctl_elem_value *ucontrol)
415 {
416         ucontrol->value.integer.value[0] = rx51_fmtx_func;
417
418         return 0;
419 }
420
421 static int rx51_set_fmtx(struct snd_kcontrol *kcontrol,
422                          struct snd_ctl_elem_value *ucontrol)
423 {
424         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
425
426         if (rx51_fmtx_func == ucontrol->value.integer.value[0])
427                 return 0;
428
429         rx51_fmtx_func = ucontrol->value.integer.value[0];
430         rx51_ext_control(codec);
431
432         /* fmtx and tpa must not be enabled at the same time */
433         if (rx51_fmtx_func && tpa6130_enable)
434                 tpa6130a2_set_enabled(0);
435         if (!rx51_fmtx_func && tpa6130_enable)
436                 tpa6130a2_set_enabled(1);
437
438         return 1;
439 }
440
441 static int rx51_get_input(struct snd_kcontrol *kcontrol,
442                           struct snd_ctl_elem_value *ucontrol)
443 {
444         ucontrol->value.integer.value[0] = rx51_dmic_func;
445
446         return 0;
447 }
448
449 static int rx51_set_input(struct snd_kcontrol *kcontrol,
450                           struct snd_ctl_elem_value *ucontrol)
451 {
452         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
453
454         if (rx51_dmic_func == ucontrol->value.integer.value[0])
455                 return 0;
456
457         rx51_dmic_func = ucontrol->value.integer.value[0];
458         rx51_ext_control(codec);
459
460         return 1;
461 }
462
463 static int rx51_get_ear(struct snd_kcontrol *kcontrol,
464                         struct snd_ctl_elem_value *ucontrol)
465 {
466         ucontrol->value.integer.value[0] = rx51_ear_func;
467
468         return 0;
469 }
470
471 static int rx51_set_ear(struct snd_kcontrol *kcontrol,
472                         struct snd_ctl_elem_value *ucontrol)
473 {
474         struct snd_soc_codec *codec =  snd_kcontrol_chip(kcontrol);
475
476         if (rx51_ear_func == ucontrol->value.integer.value[0])
477                 return 0;
478
479         rx51_ear_func = ucontrol->value.integer.value[0];
480         rx51_ext_control(codec);
481
482         return 1;
483 }
484
485 static int rx51_ear_event(struct snd_soc_dapm_widget *w,
486                           struct snd_kcontrol *k, int event)
487 {
488         if (SND_SOC_DAPM_EVENT_ON(event))
489                 aic34b_ear_enable(1);
490         else
491                 aic34b_ear_enable(0);
492
493         return 0;
494 }
495
496 static int rx51_pre_spk_event(struct snd_soc_dapm_widget *w,
497                           struct snd_kcontrol *k, int event)
498 {
499         if (!rx51_new_hw_audio)
500                 return 0;
501
502         if (SND_SOC_DAPM_EVENT_ON(event))
503                 return twl4030_enable_regulator(RES_VMMC2);
504
505         return 0;
506 }
507
508 static int rx51_post_spk_event(struct snd_soc_dapm_widget *w,
509                           struct snd_kcontrol *k, int event)
510 {
511         if (!rx51_new_hw_audio)
512                 return 0;
513
514         if (!SND_SOC_DAPM_EVENT_ON(event))
515                 return twl4030_disable_regulator(RES_VMMC2);
516
517         return 0;
518 }
519
520 static int rx51_pre_event(struct snd_soc_dapm_widget *w,
521                           struct snd_kcontrol *k, int event)
522 {
523         if (!rx51_new_hw_audio)
524                 return 0;
525
526         if (SND_SOC_DAPM_EVENT_ON(event)) {
527                 if (!pre_events) {
528                         pre_events = 1;
529                         return twl4030_enable_regulator(RES_VMMC2);
530                 }
531         }
532
533         return 0;
534 }
535
536 static int rx51_post_event(struct snd_soc_dapm_widget *w,
537                           struct snd_kcontrol *k, int event)
538 {
539         if (!rx51_new_hw_audio)
540                 return 0;
541
542         if (!SND_SOC_DAPM_EVENT_ON(event)) {
543                 if (pre_events && !w->codec->active) {
544                         pre_events = 0;
545                         return twl4030_disable_regulator(RES_VMMC2);
546                 }
547         }
548
549         return 0;
550 }
551
552 enum {
553        RX51_EXT_API_TPA6130,
554        RX51_EXT_API_AIC34B,
555 };
556 #define SOC_RX51_EXT_SINGLE_TLV(xname, ext_api, max, tlv_array) \
557 { \
558         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
559         .name = xname, \
560         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
561                   SNDRV_CTL_ELEM_ACCESS_READWRITE, \
562         .tlv.p = (tlv_array), \
563         .info = rx51_ext_info_volsw, \
564         .get = rx51_ext_get_volsw, \
565         .put = rx51_ext_put_volsw, \
566         .private_value = (ext_api) << 26 | (max) << 16, \
567 }
568
569 static int rx51_ext_info_volsw(struct snd_kcontrol *kcontrol,
570                                struct snd_ctl_elem_info *uinfo)
571 {
572         int ext_api = (kcontrol->private_value >> 26) & 0x0f;
573         int max = (kcontrol->private_value >> 16) & 0xff;
574
575         if (ext_api == RX51_EXT_API_TPA6130)
576                 if (hp_lim != max && hp_lim >= 2 && hp_lim <= 63) {
577                         kcontrol->private_value &= ~(0xff << 16);
578                         kcontrol->private_value |= (hp_lim << 16);
579                         max = hp_lim;
580                 }
581
582         if (max == 1)
583                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
584         else
585                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
586
587         uinfo->count = 1;
588         uinfo->value.integer.min = 0;
589         uinfo->value.integer.max = max;
590
591         return 0;
592 }
593
594 static int rx51_ext_get_volsw(struct snd_kcontrol *kcontrol,
595                               struct snd_ctl_elem_value *ucontrol)
596 {
597         int ext_api = (kcontrol->private_value >> 26) & 0x0f;
598
599         switch (ext_api) {
600         case RX51_EXT_API_TPA6130:
601                 if (tpa6130_volume < 0)
602                         tpa6130_volume = tpa6130a2_get_volume();
603                 ucontrol->value.integer.value[0] = tpa6130_volume;
604                 break;
605         case RX51_EXT_API_AIC34B:
606                 ucontrol->value.integer.value[0] = aic34b_volume;
607                 break;
608         default:
609                 return -EINVAL;
610         }
611
612         return 0;
613 }
614
615 static int rx51_ext_put_volsw(struct snd_kcontrol *kcontrol,
616                               struct snd_ctl_elem_value *ucontrol)
617 {
618         int ext_api = (kcontrol->private_value >> 26) & 0x0f;
619         int change = 0;
620
621         switch (ext_api) {
622         case RX51_EXT_API_TPA6130:
623                 change = (tpa6130_volume != ucontrol->value.integer.value[0]);
624                 tpa6130_volume = ucontrol->value.integer.value[0];
625                 tpa6130a2_set_volume(tpa6130_volume);
626                 break;
627         case RX51_EXT_API_AIC34B:
628                 change = (aic34b_volume != ucontrol->value.integer.value[0]);
629                 aic34b_volume = ucontrol->value.integer.value[0];
630                 aic34b_set_volume(aic34b_volume);
631                 break;
632         default:
633                 return -EINVAL;
634         }
635
636         return change;
637 }
638
639 #define SOC_RX51_SINGLE_JACK_BIAS(xname) \
640 { \
641         .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
642         .name = xname, \
643         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
644                   SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
645         .info = rx51_info_jack_bias, \
646         .get = rx51_get_jack_bias, \
647         .put = rx51_put_jack_bias, \
648 }
649
650 static int rx51_info_jack_bias(struct snd_kcontrol *kcontrol,
651                                struct snd_ctl_elem_info *uinfo)
652 {
653         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
654         uinfo->count = 1;
655         uinfo->value.integer.min = 0;
656         uinfo->value.integer.max = 1;
657
658         return 0;
659 }
660
661 static int rx51_get_jack_bias(struct snd_kcontrol *kcontrol,
662                               struct snd_ctl_elem_value *ucontrol)
663 {
664         ucontrol->value.integer.value[0] = (aic34b_get_mic_bias() != 0);
665
666         return 0;
667 }
668
669 static int rx51_put_jack_bias(struct snd_kcontrol *kcontrol,
670                               struct snd_ctl_elem_value *ucontrol)
671 {
672         int change, new_value;
673
674         new_value = ucontrol->value.integer.value[0];
675         change = (new_value != aic34b_get_mic_bias());
676
677         if (change) {
678                 switch (rx51_jack_func) {
679                 case RX51_JACK_ECI:
680                 case RX51_JACK_HS:
681                 case RX51_JACK_MIC:
682                         aic34b_set_mic_bias(new_value * 2); /* 2.5 V */
683                         break;
684                 default:
685                         change = 0;
686                 }
687         }
688
689         return change;
690 }
691
692 static const struct snd_soc_dapm_widget aic34_dapm_widgets[] = {
693         SND_SOC_DAPM_POST("Post event", rx51_post_event),
694         SND_SOC_DAPM_SPK("Post spk", rx51_post_spk_event),
695         SND_SOC_DAPM_SPK("Ext Spk", rx51_spk_event),
696         SND_SOC_DAPM_SPK("Headphone Jack", rx51_jack_hp_event),
697         SND_SOC_DAPM_MIC("Mic Jack", rx51_jack_mic_event),
698         SND_SOC_DAPM_OUTPUT("FM Transmitter"),
699         SND_SOC_DAPM_MIC("DMic", NULL),
700         SND_SOC_DAPM_SPK("Earphone", rx51_ear_event),
701         SND_SOC_DAPM_SPK("Pre spk", rx51_pre_spk_event),
702         SND_SOC_DAPM_PRE("Pre event", rx51_pre_event),
703 };
704
705 static const struct snd_soc_dapm_route audio_map[] = {
706         {"Post spk", NULL, "LLOUT"},
707         {"Post spk", NULL, "RLOUT"},
708
709         {"Ext Spk", NULL, "HPLOUT"},
710         {"Ext Spk", NULL, "HPROUT"},
711
712         {"Headphone Jack", NULL, "LLOUT"},
713         {"Headphone Jack", NULL, "RLOUT"},
714         {"LINE1L", NULL, "Mic Jack"},
715
716         {"FM Transmitter", NULL, "LLOUT"},
717         {"FM Transmitter", NULL, "RLOUT"},
718
719         {"Earphone", NULL, "MONO_LOUT"},
720
721         {"DMic Rate 64", NULL, "Mic Bias 2V"},
722         {"Mic Bias 2V", NULL, "DMic"},
723
724         {"Pre spk", NULL, "LLOUT"},
725         {"Pre spk", NULL, "RLOUT"},
726 };
727
728 static const char *spk_function[] = {"Off", "On"};
729 static const char *jack_function[] = {"Off", "Headphone", "Headset",
730                                       "Mic", "ECI Headset", "TV-OUT"};
731 static const char *fmtx_function[] = {"Off", "On"};
732 static const char *input_function[] = {"ADC", "Digital Mic"};
733 static const char *ear_function[] = {"Off", "On"};
734
735 static const struct soc_enum rx51_enum[] = {
736         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
737         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
738         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(fmtx_function), fmtx_function),
739         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
740         SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ear_function), ear_function),
741 };
742
743 /*
744  * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
745  * down in gain. Justify scale so that it is quite correct from -20 dB and
746  * up. This setting shows -30 dB at minimum, -12.95 dB at 49 % (actual
747  * is -10.3 dB) and 4.65 dB at maximum (actual is 4 dB).
748  */
749 static const unsigned int tpa6130_tlv[] = {
750         TLV_DB_RANGE_HEAD(10),
751         0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
752         2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
753         4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
754         6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
755         8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
756         10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
757         12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
758         14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
759         21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
760         38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
761 };
762
763 /*
764  * TLV320AIC3x output stage volumes. From -78.3 to 0 dB. Muted below -78.3 dB.
765  * Step size is approximately 0.5 dB over most of the scale but increasing
766  * near the very low levels.
767  * Define dB scale so that it is mostly correct for range about -55 to 0 dB
768  * but having increasing dB difference below that (and where it doesn't count
769  * so much). This setting shows -50 dB (actual is -50.3 dB) for register
770  * value 100 and -58.5 dB (actual is -78.3 dB) for register value 117.
771  */
772 static DECLARE_TLV_DB_SCALE(aic3x_output_stage_tlv, -5900, 50, 1);
773
774 static const struct snd_kcontrol_new aic34_rx51_controls[] = {
775         SOC_ENUM_EXT("Speaker Function", rx51_enum[0],
776                      rx51_get_spk, rx51_set_spk),
777         SOC_ENUM_EXT("Jack Function", rx51_enum[1],
778                      rx51_get_jack, rx51_set_jack),
779         SOC_ENUM_EXT("FMTX Function", rx51_enum[2],
780                      rx51_get_fmtx, rx51_set_fmtx),
781         SOC_ENUM_EXT("Input Select",  rx51_enum[3],
782                      rx51_get_input, rx51_set_input),
783         SOC_ENUM_EXT("Earphone Function",  rx51_enum[4],
784                      rx51_get_ear, rx51_set_ear),
785         SOC_RX51_EXT_SINGLE_TLV("Headphone Playback Volume",
786                                 RX51_EXT_API_TPA6130, 63,
787                                 tpa6130_tlv),
788         SOC_RX51_EXT_SINGLE_TLV("Earphone Playback Volume",
789                                 RX51_EXT_API_AIC34B, 118,
790                                 aic3x_output_stage_tlv),
791         SOC_RX51_SINGLE_JACK_BIAS("Jack Bias Switch"),
792 };
793
794 static int rx51_aic34_init(struct snd_soc_codec *codec)
795 {
796         int i, err;
797
798         printk("MNZ: BEGIN rx51_aic3x_init\n");
799         /* set up NC codec pins */
800         snd_soc_dapm_nc_pin(codec, "MIC3L");
801         snd_soc_dapm_nc_pin(codec, "MIC3R");
802         snd_soc_dapm_nc_pin(codec, "LINE1R");
803
804         /* Create jack for accessory reporting */
805         err = snd_jack_new(codec->card, "Jack", SND_JACK_MECHANICAL |
806                         SND_JACK_HEADSET | SND_JACK_AVOUT, &rx51_jack);
807         if (err < 0)
808                 return err;
809
810         /* Add RX51 specific controls */
811         for (i = 0; i < ARRAY_SIZE(aic34_rx51_controls); i++) {
812                 err = snd_ctl_add(codec->card,
813                         snd_soc_cnew(&aic34_rx51_controls[i], codec, NULL));
814                 if (err < 0)
815                         return err;
816         }
817
818         /* Add RX51 specific widgets */
819         snd_soc_dapm_new_controls(codec, aic34_dapm_widgets,
820                                   ARRAY_SIZE(aic34_dapm_widgets));
821
822         /* Set up RX51 specific audio path audio_map */
823         snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
824
825         snd_soc_dapm_enable_pin(codec, "Earphone");
826
827         snd_soc_dapm_sync(codec);
828
829     /* Default De-emphasis filter coefficients to use as a highpass for
830      * cheap speaker protection */
831     aic3x_deemph_set_coeffs(codec, 32276, -32276, 31785);
832
833         printk("MNZ: END rx51_aic3x_init\n");
834         return 0;
835 }
836
837 /* Since all codec control is done by Bluetooth hardware
838    only some constrains need to be set for it */
839 struct snd_soc_dai btcodec_dai = {
840         .name = "Bluetooth codec",
841         .playback = {
842                 .stream_name = "BT Playback",
843                 .channels_min = 1,
844                 .channels_max = 1,
845                 .rates = SNDRV_PCM_RATE_8000,
846                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
847         .capture = {
848                 .stream_name = "BT Capture",
849                 .channels_min = 1,
850                 .channels_max = 1,
851                 .rates = SNDRV_PCM_RATE_8000,
852                 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
853 };
854
855 /* Digital audio interface glue - connects codec <--> CPU */
856 static struct snd_soc_dai_link rx51_dai[] = {
857         {
858                 .name = "TLV320AIC34",
859                 .stream_name = "AIC34",
860                 .cpu_dai = &omap_mcbsp_dai[0],
861                 .codec_dai = &aic3x_dai,
862                 .init = rx51_aic34_init,
863                 .ops = &rx51_ops,
864         }, {
865                 .name = "Bluetooth PCM",
866                 .stream_name = "Bluetooth",
867                 .cpu_dai = &omap_mcbsp_dai[1],
868                 .codec_dai = &btcodec_dai,
869                 .ops = &rx51_bt_ops,
870         }
871 };
872
873 /* Audio machine driver */
874 static struct snd_soc_machine snd_soc_machine_rx51 = {
875         .name = "RX51",
876         .dai_link = rx51_dai,
877         .num_links = ARRAY_SIZE(rx51_dai),
878 };
879
880 /* Audio private data */
881 static struct aic3x_setup_data rx51_aic34_setup = {
882         .i2c_bus = 2,
883         .i2c_address = 0x18,
884         .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
885         .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
886 };
887
888 /* Audio subsystem */
889 static struct snd_soc_device rx51_snd_devdata = {
890         .machine = &snd_soc_machine_rx51,
891         .platform = &omap_soc_platform,
892         .codec_dev = &soc_codec_dev_aic3x,
893         .codec_data = &rx51_aic34_setup,
894 };
895
896 static struct platform_device *rx51_snd_device;
897
898 #define REMAP_OFFSET            2
899 #define DEDICATED_OFFSET        3
900 #define VMMC2_DEV_GRP           0x2B
901 #define VMMC2_285V              0x0a
902
903 static int __init rx51_soc_init(void)
904 {
905         int err;
906         struct device *dev;
907
908         if (!machine_is_nokia_rx51())
909                 return -ENODEV;
910
911         if ((system_rev >= 0x08 && system_rev <= 0x13) || /* Macros */
912                                                 system_rev >= 0x1901) {
913                 rx51_new_hw_audio = 1;
914                 err = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
915                                         VMMC2_285V,
916                                         VMMC2_DEV_GRP + DEDICATED_OFFSET);
917                 err |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER, 0xee,
918                                         VMMC2_DEV_GRP + REMAP_OFFSET);
919                 if (err) {
920                         printk(KERN_ERR "%s rx51 audio failed!\n", __func__);
921                         return -ENODEV;
922                 }
923         }
924
925         if (gpio_request(RX51_CODEC_RESET_GPIO, NULL) < 0)
926                 BUG();
927         if (gpio_request(RX51_TVOUT_SEL_GPIO, "tvout_sel") < 0)
928                 BUG();
929         if (gpio_request(RX51_ECI_SWITCH_1_GPIO, "ECI switch 1") < 0)
930                 BUG();
931         if (gpio_request(RX51_ECI_SWITCH_2_GPIO, "ECI switch 2") < 0)
932                 BUG();
933         gpio_direction_output(RX51_CODEC_RESET_GPIO, 0);
934         gpio_direction_output(RX51_TVOUT_SEL_GPIO, 0);
935         gpio_direction_output(RX51_ECI_SWITCH_1_GPIO, 0);
936         gpio_direction_output(RX51_ECI_SWITCH_2_GPIO, 1);
937
938         gpio_set_value(RX51_CODEC_RESET_GPIO, 0);
939         udelay(1);
940         gpio_set_value(RX51_CODEC_RESET_GPIO, 1);
941         msleep(1);
942
943         if (gpio_request(RX51_SPEAKER_AMP_TWL_GPIO, NULL) < 0)
944                 BUG();
945         gpio_direction_output(RX51_SPEAKER_AMP_TWL_GPIO, 0);
946
947         rx51_snd_device = platform_device_alloc("soc-audio", -1);
948         if (!rx51_snd_device)
949                 return -ENOMEM;
950
951         platform_set_drvdata(rx51_snd_device, &rx51_snd_devdata);
952         rx51_snd_devdata.dev = &rx51_snd_device->dev;
953         err = platform_device_add(rx51_snd_device);
954         if (err)
955                 goto err1;
956
957         dev = &rx51_snd_device->dev;
958
959         *(unsigned int *)rx51_dai[0].cpu_dai->private_data = 1;
960         *(unsigned int *)rx51_dai[1].cpu_dai->private_data = 2;
961
962         err = device_create_file(dev, &dev_attr_eci_mode);
963         if (err)
964                 goto err2;
965
966         return err;
967 err2:
968         platform_device_del(rx51_snd_device);
969 err1:
970         platform_device_put(rx51_snd_device);
971
972         return err;
973
974 }
975
976 static void __exit rx51_soc_exit(void)
977 {
978         platform_device_unregister(rx51_snd_device);
979 }
980
981 module_init(rx51_soc_init);
982 module_exit(rx51_soc_exit);
983
984 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
985 MODULE_DESCRIPTION("ALSA SoC Nokia RX51");
986 MODULE_LICENSE("GPL");