Merge branch 'master' of ssh://ra.kernel.org/pub/scm/linux/kernel/git/linville/wirele...
[pandora-kernel.git] / sound / soc / codecs / sn95031.c
1 /*
2  *  sn95031.c -  TI sn95031 Codec driver
3  *
4  *  Copyright (C) 2010 Intel Corp
5  *  Author: Vinod Koul <vinod.koul@intel.com>
6  *  Author: Harsha Priya <priya.harsha@intel.com>
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  *
24  *
25  */
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28 #include <linux/platform_device.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31
32 #include <asm/intel_scu_ipc.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
38 #include <sound/tlv.h>
39 #include <sound/jack.h>
40 #include "sn95031.h"
41
42 #define SN95031_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100)
43 #define SN95031_FORMATS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
44
45 /* adc helper functions */
46
47 /* enables mic bias voltage */
48 static void sn95031_enable_mic_bias(struct snd_soc_codec *codec)
49 {
50         snd_soc_write(codec, SN95031_VAUD, BIT(2)|BIT(1)|BIT(0));
51         snd_soc_update_bits(codec, SN95031_MICBIAS, BIT(2), BIT(2));
52 }
53
54 /* Enable/Disable the ADC depending on the argument */
55 static void configure_adc(struct snd_soc_codec *sn95031_codec, int val)
56 {
57         int value = snd_soc_read(sn95031_codec, SN95031_ADC1CNTL1);
58
59         if (val) {
60                 /* Enable and start the ADC */
61                 value |= (SN95031_ADC_ENBL | SN95031_ADC_START);
62                 value &= (~SN95031_ADC_NO_LOOP);
63         } else {
64                 /* Just stop the ADC */
65                 value &= (~SN95031_ADC_START);
66         }
67         snd_soc_write(sn95031_codec, SN95031_ADC1CNTL1, value);
68 }
69
70 /*
71  * finds an empty channel for conversion
72  * If the ADC is not enabled then start using 0th channel
73  * itself. Otherwise find an empty channel by looking for a
74  * channel in which the stopbit is set to 1. returns the index
75  * of the first free channel if succeeds or an error code.
76  *
77  * Context: can sleep
78  *
79  */
80 static int find_free_channel(struct snd_soc_codec *sn95031_codec)
81 {
82         int i, value;
83
84         /* check whether ADC is enabled */
85         value = snd_soc_read(sn95031_codec, SN95031_ADC1CNTL1);
86
87         if ((value & SN95031_ADC_ENBL) == 0)
88                 return 0;
89
90         /* ADC is already enabled; Looking for an empty channel */
91         for (i = 0; i < SN95031_ADC_CHANLS_MAX; i++) {
92                 value = snd_soc_read(sn95031_codec,
93                                 SN95031_ADC_CHNL_START_ADDR + i);
94                 if (value & SN95031_STOPBIT_MASK)
95                         break;
96         }
97         return (i == SN95031_ADC_CHANLS_MAX) ? (-EINVAL) : i;
98 }
99
100 /* Initialize the ADC for reading micbias values. Can sleep. */
101 static int sn95031_initialize_adc(struct snd_soc_codec *sn95031_codec)
102 {
103         int base_addr, chnl_addr;
104         int value;
105         int channel_index;
106
107         /* Index of the first channel in which the stop bit is set */
108         channel_index = find_free_channel(sn95031_codec);
109         if (channel_index < 0) {
110                 pr_err("No free ADC channels");
111                 return channel_index;
112         }
113
114         base_addr = SN95031_ADC_CHNL_START_ADDR + channel_index;
115
116         if (!(channel_index == 0 || channel_index ==  SN95031_ADC_LOOP_MAX)) {
117                 /* Reset stop bit for channels other than 0 and 12 */
118                 value = snd_soc_read(sn95031_codec, base_addr);
119                 /* Set the stop bit to zero */
120                 snd_soc_write(sn95031_codec, base_addr, value & 0xEF);
121                 /* Index of the first free channel */
122                 base_addr++;
123                 channel_index++;
124         }
125
126         /* Since this is the last channel, set the stop bit
127            to 1 by ORing the DIE_SENSOR_CODE with 0x10 */
128         snd_soc_write(sn95031_codec, base_addr,
129                                 SN95031_AUDIO_DETECT_CODE | 0x10);
130
131         chnl_addr = SN95031_ADC_DATA_START_ADDR + 2 * channel_index;
132         pr_debug("mid_initialize : %x", chnl_addr);
133         configure_adc(sn95031_codec, 1);
134         return chnl_addr;
135 }
136
137
138 /* reads the ADC registers and gets the mic bias value in mV. */
139 static unsigned int sn95031_get_mic_bias(struct snd_soc_codec *codec)
140 {
141         u16 adc_adr = sn95031_initialize_adc(codec);
142         u16 adc_val1, adc_val2;
143         unsigned int mic_bias;
144
145         sn95031_enable_mic_bias(codec);
146
147         /* Enable the sound card for conversion before reading */
148         snd_soc_write(codec, SN95031_ADC1CNTL3, 0x05);
149         /* Re-toggle the RRDATARD bit */
150         snd_soc_write(codec, SN95031_ADC1CNTL3, 0x04);
151
152         /* Read the higher bits of data */
153         msleep(1000);
154         adc_val1 = snd_soc_read(codec, adc_adr);
155         adc_adr++;
156         adc_val2 = snd_soc_read(codec, adc_adr);
157
158         /* Adding lower two bits to the higher bits */
159         mic_bias = (adc_val1 << 2) + (adc_val2 & 3);
160         mic_bias = (mic_bias * SN95031_ADC_ONE_LSB_MULTIPLIER) / 1000;
161         pr_debug("mic bias = %dmV\n", mic_bias);
162         return mic_bias;
163 }
164 /*end - adc helper functions */
165
166 static inline unsigned int sn95031_read(struct snd_soc_codec *codec,
167                         unsigned int reg)
168 {
169         u8 value = 0;
170         int ret;
171
172         ret = intel_scu_ipc_ioread8(reg, &value);
173         if (ret)
174                 pr_err("read of %x failed, err %d\n", reg, ret);
175         return value;
176
177 }
178
179 static inline int sn95031_write(struct snd_soc_codec *codec,
180                         unsigned int reg, unsigned int value)
181 {
182         int ret;
183
184         ret = intel_scu_ipc_iowrite8(reg, value);
185         if (ret)
186                 pr_err("write of %x failed, err %d\n", reg, ret);
187         return ret;
188 }
189
190 static int sn95031_set_vaud_bias(struct snd_soc_codec *codec,
191                 enum snd_soc_bias_level level)
192 {
193         switch (level) {
194         case SND_SOC_BIAS_ON:
195                 break;
196
197         case SND_SOC_BIAS_PREPARE:
198                 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
199                         pr_debug("vaud_bias powering up pll\n");
200                         /* power up the pll */
201                         snd_soc_write(codec, SN95031_AUDPLLCTRL, BIT(5));
202                         /* enable pcm 2 */
203                         snd_soc_update_bits(codec, SN95031_PCM2C2,
204                                         BIT(0), BIT(0));
205                 }
206                 break;
207
208         case SND_SOC_BIAS_STANDBY:
209                 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
210                         pr_debug("vaud_bias power up rail\n");
211                         /* power up the rail */
212                         snd_soc_write(codec, SN95031_VAUD,
213                                         BIT(2)|BIT(1)|BIT(0));
214                         msleep(1);
215                 } else if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
216                         /* turn off pcm */
217                         pr_debug("vaud_bias power dn pcm\n");
218                         snd_soc_update_bits(codec, SN95031_PCM2C2, BIT(0), 0);
219                         snd_soc_write(codec, SN95031_AUDPLLCTRL, 0);
220                 }
221                 break;
222
223
224         case SND_SOC_BIAS_OFF:
225                 pr_debug("vaud_bias _OFF doing rail shutdown\n");
226                 snd_soc_write(codec, SN95031_VAUD, BIT(3));
227                 break;
228         }
229
230         codec->dapm.bias_level = level;
231         return 0;
232 }
233
234 static int sn95031_vhs_event(struct snd_soc_dapm_widget *w,
235                     struct snd_kcontrol *kcontrol, int event)
236 {
237         if (SND_SOC_DAPM_EVENT_ON(event)) {
238                 pr_debug("VHS SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
239                 /* power up the rail */
240                 snd_soc_write(w->codec, SN95031_VHSP, 0x3D);
241                 snd_soc_write(w->codec, SN95031_VHSN, 0x3F);
242                 msleep(1);
243         } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
244                 pr_debug("VHS SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
245                 snd_soc_write(w->codec, SN95031_VHSP, 0xC4);
246                 snd_soc_write(w->codec, SN95031_VHSN, 0x04);
247         }
248         return 0;
249 }
250
251 static int sn95031_vihf_event(struct snd_soc_dapm_widget *w,
252                     struct snd_kcontrol *kcontrol, int event)
253 {
254         if (SND_SOC_DAPM_EVENT_ON(event)) {
255                 pr_debug("VIHF SND_SOC_DAPM_EVENT_ON doing rail startup now\n");
256                 /* power up the rail */
257                 snd_soc_write(w->codec, SN95031_VIHF, 0x27);
258                 msleep(1);
259         } else if (SND_SOC_DAPM_EVENT_OFF(event)) {
260                 pr_debug("VIHF SND_SOC_DAPM_EVENT_OFF doing rail shutdown\n");
261                 snd_soc_write(w->codec, SN95031_VIHF, 0x24);
262         }
263         return 0;
264 }
265
266 static int sn95031_dmic12_event(struct snd_soc_dapm_widget *w,
267                         struct snd_kcontrol *k, int event)
268 {
269         unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
270
271         if (SND_SOC_DAPM_EVENT_ON(event)) {
272                 ldo = BIT(5)|BIT(4);
273                 clk_dir = BIT(0);
274                 data_dir = BIT(7);
275         }
276         /* program DMIC LDO, clock and set clock */
277         snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
278         snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(0), clk_dir);
279         snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(7), data_dir);
280         return 0;
281 }
282
283 static int sn95031_dmic34_event(struct snd_soc_dapm_widget *w,
284                         struct snd_kcontrol *k, int event)
285 {
286         unsigned int ldo = 0, clk_dir = 0, data_dir = 0;
287
288         if (SND_SOC_DAPM_EVENT_ON(event)) {
289                 ldo = BIT(5)|BIT(4);
290                 clk_dir = BIT(2);
291                 data_dir = BIT(1);
292         }
293         /* program DMIC LDO, clock and set clock */
294         snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(5)|BIT(4), ldo);
295         snd_soc_update_bits(w->codec, SN95031_DMICBUF0123, BIT(2), clk_dir);
296         snd_soc_update_bits(w->codec, SN95031_DMICBUF45, BIT(1), data_dir);
297         return 0;
298 }
299
300 static int sn95031_dmic56_event(struct snd_soc_dapm_widget *w,
301                         struct snd_kcontrol *k, int event)
302 {
303         unsigned int ldo = 0;
304
305         if (SND_SOC_DAPM_EVENT_ON(event))
306                 ldo = BIT(7)|BIT(6);
307
308         /* program DMIC LDO */
309         snd_soc_update_bits(w->codec, SN95031_MICBIAS, BIT(7)|BIT(6), ldo);
310         return 0;
311 }
312
313 /* mux controls */
314 static const char *sn95031_mic_texts[] = { "AMIC", "LineIn" };
315
316 static const struct soc_enum sn95031_micl_enum =
317         SOC_ENUM_SINGLE(SN95031_ADCCONFIG, 1, 2, sn95031_mic_texts);
318
319 static const struct snd_kcontrol_new sn95031_micl_mux_control =
320         SOC_DAPM_ENUM("Route", sn95031_micl_enum);
321
322 static const struct soc_enum sn95031_micr_enum =
323         SOC_ENUM_SINGLE(SN95031_ADCCONFIG, 3, 2, sn95031_mic_texts);
324
325 static const struct snd_kcontrol_new sn95031_micr_mux_control =
326         SOC_DAPM_ENUM("Route", sn95031_micr_enum);
327
328 static const char *sn95031_input_texts[] = {    "DMIC1", "DMIC2", "DMIC3",
329                                                 "DMIC4", "DMIC5", "DMIC6",
330                                                 "ADC Left", "ADC Right" };
331
332 static const struct soc_enum sn95031_input1_enum =
333         SOC_ENUM_SINGLE(SN95031_AUDIOMUX12, 0, 8, sn95031_input_texts);
334
335 static const struct snd_kcontrol_new sn95031_input1_mux_control =
336         SOC_DAPM_ENUM("Route", sn95031_input1_enum);
337
338 static const struct soc_enum sn95031_input2_enum =
339         SOC_ENUM_SINGLE(SN95031_AUDIOMUX12, 4, 8, sn95031_input_texts);
340
341 static const struct snd_kcontrol_new sn95031_input2_mux_control =
342         SOC_DAPM_ENUM("Route", sn95031_input2_enum);
343
344 static const struct soc_enum sn95031_input3_enum =
345         SOC_ENUM_SINGLE(SN95031_AUDIOMUX34, 0, 8, sn95031_input_texts);
346
347 static const struct snd_kcontrol_new sn95031_input3_mux_control =
348         SOC_DAPM_ENUM("Route", sn95031_input3_enum);
349
350 static const struct soc_enum sn95031_input4_enum =
351         SOC_ENUM_SINGLE(SN95031_AUDIOMUX34, 4, 8, sn95031_input_texts);
352
353 static const struct snd_kcontrol_new sn95031_input4_mux_control =
354         SOC_DAPM_ENUM("Route", sn95031_input4_enum);
355
356 /* capture path controls */
357
358 static const char *sn95031_micmode_text[] = {"Single Ended", "Differential"};
359
360 /* 0dB to 30dB in 10dB steps */
361 static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 10, 0);
362
363 static const struct soc_enum sn95031_micmode1_enum =
364         SOC_ENUM_SINGLE(SN95031_MICAMP1, 1, 2, sn95031_micmode_text);
365 static const struct soc_enum sn95031_micmode2_enum =
366         SOC_ENUM_SINGLE(SN95031_MICAMP2, 1, 2, sn95031_micmode_text);
367
368 static const char *sn95031_dmic_cfg_text[] = {"GPO", "DMIC"};
369
370 static const struct soc_enum sn95031_dmic12_cfg_enum =
371         SOC_ENUM_SINGLE(SN95031_DMICMUX, 0, 2, sn95031_dmic_cfg_text);
372 static const struct soc_enum sn95031_dmic34_cfg_enum =
373         SOC_ENUM_SINGLE(SN95031_DMICMUX, 1, 2, sn95031_dmic_cfg_text);
374 static const struct soc_enum sn95031_dmic56_cfg_enum =
375         SOC_ENUM_SINGLE(SN95031_DMICMUX, 2, 2, sn95031_dmic_cfg_text);
376
377 static const struct snd_kcontrol_new sn95031_snd_controls[] = {
378         SOC_ENUM("Mic1Mode Capture Route", sn95031_micmode1_enum),
379         SOC_ENUM("Mic2Mode Capture Route", sn95031_micmode2_enum),
380         SOC_ENUM("DMIC12 Capture Route", sn95031_dmic12_cfg_enum),
381         SOC_ENUM("DMIC34 Capture Route", sn95031_dmic34_cfg_enum),
382         SOC_ENUM("DMIC56 Capture Route", sn95031_dmic56_cfg_enum),
383         SOC_SINGLE_TLV("Mic1 Capture Volume", SN95031_MICAMP1,
384                         2, 4, 0, mic_tlv),
385         SOC_SINGLE_TLV("Mic2 Capture Volume", SN95031_MICAMP2,
386                         2, 4, 0, mic_tlv),
387 };
388
389 /* DAPM widgets */
390 static const struct snd_soc_dapm_widget sn95031_dapm_widgets[] = {
391
392         /* all end points mic, hs etc */
393         SND_SOC_DAPM_OUTPUT("HPOUTL"),
394         SND_SOC_DAPM_OUTPUT("HPOUTR"),
395         SND_SOC_DAPM_OUTPUT("EPOUT"),
396         SND_SOC_DAPM_OUTPUT("IHFOUTL"),
397         SND_SOC_DAPM_OUTPUT("IHFOUTR"),
398         SND_SOC_DAPM_OUTPUT("LINEOUTL"),
399         SND_SOC_DAPM_OUTPUT("LINEOUTR"),
400         SND_SOC_DAPM_OUTPUT("VIB1OUT"),
401         SND_SOC_DAPM_OUTPUT("VIB2OUT"),
402
403         SND_SOC_DAPM_INPUT("AMIC1"), /* headset mic */
404         SND_SOC_DAPM_INPUT("AMIC2"),
405         SND_SOC_DAPM_INPUT("DMIC1"),
406         SND_SOC_DAPM_INPUT("DMIC2"),
407         SND_SOC_DAPM_INPUT("DMIC3"),
408         SND_SOC_DAPM_INPUT("DMIC4"),
409         SND_SOC_DAPM_INPUT("DMIC5"),
410         SND_SOC_DAPM_INPUT("DMIC6"),
411         SND_SOC_DAPM_INPUT("LINEINL"),
412         SND_SOC_DAPM_INPUT("LINEINR"),
413
414         SND_SOC_DAPM_MICBIAS("AMIC1Bias", SN95031_MICBIAS, 2, 0),
415         SND_SOC_DAPM_MICBIAS("AMIC2Bias", SN95031_MICBIAS, 3, 0),
416         SND_SOC_DAPM_MICBIAS("DMIC12Bias", SN95031_DMICMUX, 3, 0),
417         SND_SOC_DAPM_MICBIAS("DMIC34Bias", SN95031_DMICMUX, 4, 0),
418         SND_SOC_DAPM_MICBIAS("DMIC56Bias", SN95031_DMICMUX, 5, 0),
419
420         SND_SOC_DAPM_SUPPLY("DMIC12supply", SN95031_DMICLK, 0, 0,
421                                 sn95031_dmic12_event,
422                                 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
423         SND_SOC_DAPM_SUPPLY("DMIC34supply", SN95031_DMICLK, 1, 0,
424                                 sn95031_dmic34_event,
425                                 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
426         SND_SOC_DAPM_SUPPLY("DMIC56supply", SN95031_DMICLK, 2, 0,
427                                 sn95031_dmic56_event,
428                                 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
429
430         SND_SOC_DAPM_AIF_OUT("PCM_Out", "Capture", 0,
431                         SND_SOC_NOPM, 0, 0),
432
433         SND_SOC_DAPM_SUPPLY("Headset Rail", SND_SOC_NOPM, 0, 0,
434                         sn95031_vhs_event,
435                         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
436         SND_SOC_DAPM_SUPPLY("Speaker Rail", SND_SOC_NOPM, 0, 0,
437                         sn95031_vihf_event,
438                         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
439
440         /* playback path driver enables */
441         SND_SOC_DAPM_PGA("Headset Left Playback",
442                         SN95031_DRIVEREN, 0, 0, NULL, 0),
443         SND_SOC_DAPM_PGA("Headset Right Playback",
444                         SN95031_DRIVEREN, 1, 0, NULL, 0),
445         SND_SOC_DAPM_PGA("Speaker Left Playback",
446                         SN95031_DRIVEREN, 2, 0, NULL, 0),
447         SND_SOC_DAPM_PGA("Speaker Right Playback",
448                         SN95031_DRIVEREN, 3, 0, NULL, 0),
449         SND_SOC_DAPM_PGA("Vibra1 Playback",
450                         SN95031_DRIVEREN, 4, 0, NULL, 0),
451         SND_SOC_DAPM_PGA("Vibra2 Playback",
452                         SN95031_DRIVEREN, 5, 0, NULL, 0),
453         SND_SOC_DAPM_PGA("Earpiece Playback",
454                         SN95031_DRIVEREN, 6, 0, NULL, 0),
455         SND_SOC_DAPM_PGA("Lineout Left Playback",
456                         SN95031_LOCTL, 0, 0, NULL, 0),
457         SND_SOC_DAPM_PGA("Lineout Right Playback",
458                         SN95031_LOCTL, 4, 0, NULL, 0),
459
460         /* playback path filter enable */
461         SND_SOC_DAPM_PGA("Headset Left Filter",
462                         SN95031_HSEPRXCTRL, 4, 0, NULL, 0),
463         SND_SOC_DAPM_PGA("Headset Right Filter",
464                         SN95031_HSEPRXCTRL, 5, 0,  NULL, 0),
465         SND_SOC_DAPM_PGA("Speaker Left Filter",
466                         SN95031_IHFRXCTRL, 0, 0,  NULL, 0),
467         SND_SOC_DAPM_PGA("Speaker Right Filter",
468                         SN95031_IHFRXCTRL, 1, 0,  NULL, 0),
469
470         /* DACs */
471         SND_SOC_DAPM_DAC("HSDAC Left", "Headset",
472                         SN95031_DACCONFIG, 0, 0),
473         SND_SOC_DAPM_DAC("HSDAC Right", "Headset",
474                         SN95031_DACCONFIG, 1, 0),
475         SND_SOC_DAPM_DAC("IHFDAC Left", "Speaker",
476                         SN95031_DACCONFIG, 2, 0),
477         SND_SOC_DAPM_DAC("IHFDAC Right", "Speaker",
478                         SN95031_DACCONFIG, 3, 0),
479         SND_SOC_DAPM_DAC("Vibra1 DAC", "Vibra1",
480                         SN95031_VIB1C5, 1, 0),
481         SND_SOC_DAPM_DAC("Vibra2 DAC", "Vibra2",
482                         SN95031_VIB2C5, 1, 0),
483
484         /* capture widgets */
485         SND_SOC_DAPM_PGA("LineIn Enable Left", SN95031_MICAMP1,
486                                 7, 0, NULL, 0),
487         SND_SOC_DAPM_PGA("LineIn Enable Right", SN95031_MICAMP2,
488                                 7, 0, NULL, 0),
489
490         SND_SOC_DAPM_PGA("MIC1 Enable", SN95031_MICAMP1, 0, 0, NULL, 0),
491         SND_SOC_DAPM_PGA("MIC2 Enable", SN95031_MICAMP2, 0, 0, NULL, 0),
492         SND_SOC_DAPM_PGA("TX1 Enable", SN95031_AUDIOTXEN, 2, 0, NULL, 0),
493         SND_SOC_DAPM_PGA("TX2 Enable", SN95031_AUDIOTXEN, 3, 0, NULL, 0),
494         SND_SOC_DAPM_PGA("TX3 Enable", SN95031_AUDIOTXEN, 4, 0, NULL, 0),
495         SND_SOC_DAPM_PGA("TX4 Enable", SN95031_AUDIOTXEN, 5, 0, NULL, 0),
496
497         /* ADC have null stream as they will be turned ON by TX path */
498         SND_SOC_DAPM_ADC("ADC Left", NULL,
499                         SN95031_ADCCONFIG, 0, 0),
500         SND_SOC_DAPM_ADC("ADC Right", NULL,
501                         SN95031_ADCCONFIG, 2, 0),
502
503         SND_SOC_DAPM_MUX("Mic_InputL Capture Route",
504                         SND_SOC_NOPM, 0, 0, &sn95031_micl_mux_control),
505         SND_SOC_DAPM_MUX("Mic_InputR Capture Route",
506                         SND_SOC_NOPM, 0, 0, &sn95031_micr_mux_control),
507
508         SND_SOC_DAPM_MUX("Txpath1 Capture Route",
509                         SND_SOC_NOPM, 0, 0, &sn95031_input1_mux_control),
510         SND_SOC_DAPM_MUX("Txpath2 Capture Route",
511                         SND_SOC_NOPM, 0, 0, &sn95031_input2_mux_control),
512         SND_SOC_DAPM_MUX("Txpath3 Capture Route",
513                         SND_SOC_NOPM, 0, 0, &sn95031_input3_mux_control),
514         SND_SOC_DAPM_MUX("Txpath4 Capture Route",
515                         SND_SOC_NOPM, 0, 0, &sn95031_input4_mux_control),
516
517 };
518
519 static const struct snd_soc_dapm_route sn95031_audio_map[] = {
520         /* headset and earpiece map */
521         { "HPOUTL", NULL, "Headset Rail"},
522         { "HPOUTR", NULL, "Headset Rail"},
523         { "HPOUTL", NULL, "Headset Left Playback" },
524         { "HPOUTR", NULL, "Headset Right Playback" },
525         { "EPOUT", NULL, "Earpiece Playback" },
526         { "Headset Left Playback", NULL, "Headset Left Filter"},
527         { "Headset Right Playback", NULL, "Headset Right Filter"},
528         { "Earpiece Playback", NULL, "Headset Left Filter"},
529         { "Headset Left Filter", NULL, "HSDAC Left"},
530         { "Headset Right Filter", NULL, "HSDAC Right"},
531
532         /* speaker map */
533         { "IHFOUTL", NULL, "Speaker Rail"},
534         { "IHFOUTR", NULL, "Speaker Rail"},
535         { "IHFOUTL", "NULL", "Speaker Left Playback"},
536         { "IHFOUTR", "NULL", "Speaker Right Playback"},
537         { "Speaker Left Playback", NULL, "Speaker Left Filter"},
538         { "Speaker Right Playback", NULL, "Speaker Right Filter"},
539         { "Speaker Left Filter", NULL, "IHFDAC Left"},
540         { "Speaker Right Filter", NULL, "IHFDAC Right"},
541
542         /* vibra map */
543         { "VIB1OUT", NULL, "Vibra1 Playback"},
544         { "Vibra1 Playback", NULL, "Vibra1 DAC"},
545
546         { "VIB2OUT", NULL, "Vibra2 Playback"},
547         { "Vibra2 Playback", NULL, "Vibra2 DAC"},
548
549         /* lineout */
550         { "LINEOUTL", NULL, "Lineout Left Playback"},
551         { "LINEOUTR", NULL, "Lineout Right Playback"},
552         { "Lineout Left Playback", NULL, "Headset Left Filter"},
553         { "Lineout Left Playback", NULL, "Speaker Left Filter"},
554         { "Lineout Left Playback", NULL, "Vibra1 DAC"},
555         { "Lineout Right Playback", NULL, "Headset Right Filter"},
556         { "Lineout Right Playback", NULL, "Speaker Right Filter"},
557         { "Lineout Right Playback", NULL, "Vibra2 DAC"},
558
559         /* Headset (AMIC1) mic */
560         { "AMIC1Bias", NULL, "AMIC1"},
561         { "MIC1 Enable", NULL, "AMIC1Bias"},
562         { "Mic_InputL Capture Route", "AMIC", "MIC1 Enable"},
563
564         /* AMIC2 */
565         { "AMIC2Bias", NULL, "AMIC2"},
566         { "MIC2 Enable", NULL, "AMIC2Bias"},
567         { "Mic_InputR Capture Route", "AMIC", "MIC2 Enable"},
568
569
570         /* Linein */
571         { "LineIn Enable Left", NULL, "LINEINL"},
572         { "LineIn Enable Right", NULL, "LINEINR"},
573         { "Mic_InputL Capture Route", "LineIn", "LineIn Enable Left"},
574         { "Mic_InputR Capture Route", "LineIn", "LineIn Enable Right"},
575
576         /* ADC connection */
577         { "ADC Left", NULL, "Mic_InputL Capture Route"},
578         { "ADC Right", NULL, "Mic_InputR Capture Route"},
579
580         /*DMIC connections */
581         { "DMIC1", NULL, "DMIC12supply"},
582         { "DMIC2", NULL, "DMIC12supply"},
583         { "DMIC3", NULL, "DMIC34supply"},
584         { "DMIC4", NULL, "DMIC34supply"},
585         { "DMIC5", NULL, "DMIC56supply"},
586         { "DMIC6", NULL, "DMIC56supply"},
587
588         { "DMIC12Bias", NULL, "DMIC1"},
589         { "DMIC12Bias", NULL, "DMIC2"},
590         { "DMIC34Bias", NULL, "DMIC3"},
591         { "DMIC34Bias", NULL, "DMIC4"},
592         { "DMIC56Bias", NULL, "DMIC5"},
593         { "DMIC56Bias", NULL, "DMIC6"},
594
595         /*TX path inputs*/
596         { "Txpath1 Capture Route", "ADC Left", "ADC Left"},
597         { "Txpath2 Capture Route", "ADC Left", "ADC Left"},
598         { "Txpath3 Capture Route", "ADC Left", "ADC Left"},
599         { "Txpath4 Capture Route", "ADC Left", "ADC Left"},
600         { "Txpath1 Capture Route", "ADC Right", "ADC Right"},
601         { "Txpath2 Capture Route", "ADC Right", "ADC Right"},
602         { "Txpath3 Capture Route", "ADC Right", "ADC Right"},
603         { "Txpath4 Capture Route", "ADC Right", "ADC Right"},
604         { "Txpath1 Capture Route", "DMIC1", "DMIC1"},
605         { "Txpath2 Capture Route", "DMIC1", "DMIC1"},
606         { "Txpath3 Capture Route", "DMIC1", "DMIC1"},
607         { "Txpath4 Capture Route", "DMIC1", "DMIC1"},
608         { "Txpath1 Capture Route", "DMIC2", "DMIC2"},
609         { "Txpath2 Capture Route", "DMIC2", "DMIC2"},
610         { "Txpath3 Capture Route", "DMIC2", "DMIC2"},
611         { "Txpath4 Capture Route", "DMIC2", "DMIC2"},
612         { "Txpath1 Capture Route", "DMIC3", "DMIC3"},
613         { "Txpath2 Capture Route", "DMIC3", "DMIC3"},
614         { "Txpath3 Capture Route", "DMIC3", "DMIC3"},
615         { "Txpath4 Capture Route", "DMIC3", "DMIC3"},
616         { "Txpath1 Capture Route", "DMIC4", "DMIC4"},
617         { "Txpath2 Capture Route", "DMIC4", "DMIC4"},
618         { "Txpath3 Capture Route", "DMIC4", "DMIC4"},
619         { "Txpath4 Capture Route", "DMIC4", "DMIC4"},
620         { "Txpath1 Capture Route", "DMIC5", "DMIC5"},
621         { "Txpath2 Capture Route", "DMIC5", "DMIC5"},
622         { "Txpath3 Capture Route", "DMIC5", "DMIC5"},
623         { "Txpath4 Capture Route", "DMIC5", "DMIC5"},
624         { "Txpath1 Capture Route", "DMIC6", "DMIC6"},
625         { "Txpath2 Capture Route", "DMIC6", "DMIC6"},
626         { "Txpath3 Capture Route", "DMIC6", "DMIC6"},
627         { "Txpath4 Capture Route", "DMIC6", "DMIC6"},
628
629         /* tx path */
630         { "TX1 Enable", NULL, "Txpath1 Capture Route"},
631         { "TX2 Enable", NULL, "Txpath2 Capture Route"},
632         { "TX3 Enable", NULL, "Txpath3 Capture Route"},
633         { "TX4 Enable", NULL, "Txpath4 Capture Route"},
634         { "PCM_Out", NULL, "TX1 Enable"},
635         { "PCM_Out", NULL, "TX2 Enable"},
636         { "PCM_Out", NULL, "TX3 Enable"},
637         { "PCM_Out", NULL, "TX4 Enable"},
638
639 };
640
641 /* speaker and headset mutes, for audio pops and clicks */
642 static int sn95031_pcm_hs_mute(struct snd_soc_dai *dai, int mute)
643 {
644         snd_soc_update_bits(dai->codec,
645                         SN95031_HSLVOLCTRL, BIT(7), (!mute << 7));
646         snd_soc_update_bits(dai->codec,
647                         SN95031_HSRVOLCTRL, BIT(7), (!mute << 7));
648         return 0;
649 }
650
651 static int sn95031_pcm_spkr_mute(struct snd_soc_dai *dai, int mute)
652 {
653         snd_soc_update_bits(dai->codec,
654                         SN95031_IHFLVOLCTRL, BIT(7), (!mute << 7));
655         snd_soc_update_bits(dai->codec,
656                         SN95031_IHFRVOLCTRL, BIT(7), (!mute << 7));
657         return 0;
658 }
659
660 static int sn95031_pcm_hw_params(struct snd_pcm_substream *substream,
661                 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
662 {
663         unsigned int format, rate;
664
665         switch (params_format(params)) {
666         case SNDRV_PCM_FORMAT_S16_LE:
667                 format = BIT(4)|BIT(5);
668                 break;
669
670         case SNDRV_PCM_FORMAT_S24_LE:
671                 format = 0;
672                 break;
673         default:
674                 return -EINVAL;
675         }
676         snd_soc_update_bits(dai->codec, SN95031_PCM2C2,
677                         BIT(4)|BIT(5), format);
678
679         switch (params_rate(params)) {
680         case 48000:
681                 pr_debug("RATE_48000\n");
682                 rate = 0;
683                 break;
684
685         case 44100:
686                 pr_debug("RATE_44100\n");
687                 rate = BIT(7);
688                 break;
689
690         default:
691                 pr_err("ERR rate %d\n", params_rate(params));
692                 return -EINVAL;
693         }
694         snd_soc_update_bits(dai->codec, SN95031_PCM1C1, BIT(7), rate);
695
696         return 0;
697 }
698
699 /* Codec DAI section */
700 static struct snd_soc_dai_ops sn95031_headset_dai_ops = {
701         .digital_mute   = sn95031_pcm_hs_mute,
702         .hw_params      = sn95031_pcm_hw_params,
703 };
704
705 static struct snd_soc_dai_ops sn95031_speaker_dai_ops = {
706         .digital_mute   = sn95031_pcm_spkr_mute,
707         .hw_params      = sn95031_pcm_hw_params,
708 };
709
710 static struct snd_soc_dai_ops sn95031_vib1_dai_ops = {
711         .hw_params      = sn95031_pcm_hw_params,
712 };
713
714 static struct snd_soc_dai_ops sn95031_vib2_dai_ops = {
715         .hw_params      = sn95031_pcm_hw_params,
716 };
717
718 static struct snd_soc_dai_driver sn95031_dais[] = {
719 {
720         .name = "SN95031 Headset",
721         .playback = {
722                 .stream_name = "Headset",
723                 .channels_min = 2,
724                 .channels_max = 2,
725                 .rates = SN95031_RATES,
726                 .formats = SN95031_FORMATS,
727         },
728         .capture = {
729                 .stream_name = "Capture",
730                 .channels_min = 1,
731                 .channels_max = 5,
732                 .rates = SN95031_RATES,
733                 .formats = SN95031_FORMATS,
734         },
735         .ops = &sn95031_headset_dai_ops,
736 },
737 {       .name = "SN95031 Speaker",
738         .playback = {
739                 .stream_name = "Speaker",
740                 .channels_min = 2,
741                 .channels_max = 2,
742                 .rates = SN95031_RATES,
743                 .formats = SN95031_FORMATS,
744         },
745         .ops = &sn95031_speaker_dai_ops,
746 },
747 {       .name = "SN95031 Vibra1",
748         .playback = {
749                 .stream_name = "Vibra1",
750                 .channels_min = 1,
751                 .channels_max = 1,
752                 .rates = SN95031_RATES,
753                 .formats = SN95031_FORMATS,
754         },
755         .ops = &sn95031_vib1_dai_ops,
756 },
757 {       .name = "SN95031 Vibra2",
758         .playback = {
759                 .stream_name = "Vibra2",
760                 .channels_min = 1,
761                 .channels_max = 1,
762                 .rates = SN95031_RATES,
763                 .formats = SN95031_FORMATS,
764         },
765         .ops = &sn95031_vib2_dai_ops,
766 },
767 };
768
769 static inline void sn95031_disable_jack_btn(struct snd_soc_codec *codec)
770 {
771         snd_soc_write(codec, SN95031_BTNCTRL2, 0x00);
772 }
773
774 static inline void sn95031_enable_jack_btn(struct snd_soc_codec *codec)
775 {
776         snd_soc_write(codec, SN95031_BTNCTRL1, 0x77);
777         snd_soc_write(codec, SN95031_BTNCTRL2, 0x01);
778 }
779
780 static int sn95031_get_headset_state(struct snd_soc_jack *mfld_jack)
781 {
782         int micbias = sn95031_get_mic_bias(mfld_jack->codec);
783
784         int jack_type = snd_soc_jack_get_type(mfld_jack, micbias);
785
786         pr_debug("jack type detected = %d\n", jack_type);
787         if (jack_type == SND_JACK_HEADSET)
788                 sn95031_enable_jack_btn(mfld_jack->codec);
789         return jack_type;
790 }
791
792 void sn95031_jack_detection(struct mfld_jack_data *jack_data)
793 {
794         unsigned int status;
795         unsigned int mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 | SND_JACK_HEADSET;
796
797         pr_debug("interrupt id read in sram = 0x%x\n", jack_data->intr_id);
798         if (jack_data->intr_id & 0x1) {
799                 pr_debug("short_push detected\n");
800                 status = SND_JACK_HEADSET | SND_JACK_BTN_0;
801         } else if (jack_data->intr_id & 0x2) {
802                 pr_debug("long_push detected\n");
803                 status = SND_JACK_HEADSET | SND_JACK_BTN_1;
804         } else if (jack_data->intr_id & 0x4) {
805                 pr_debug("headset or headphones inserted\n");
806                 status = sn95031_get_headset_state(jack_data->mfld_jack);
807         } else if (jack_data->intr_id & 0x8) {
808                 pr_debug("headset or headphones removed\n");
809                 status = 0;
810                 sn95031_disable_jack_btn(jack_data->mfld_jack->codec);
811         } else {
812                 pr_err("unidentified interrupt\n");
813                 return;
814         }
815
816         snd_soc_jack_report(jack_data->mfld_jack, status, mask);
817         /*button pressed and released so we send explicit button release */
818         if ((status & SND_JACK_BTN_0) | (status & SND_JACK_BTN_1))
819                 snd_soc_jack_report(jack_data->mfld_jack,
820                                 SND_JACK_HEADSET, mask);
821 }
822 EXPORT_SYMBOL_GPL(sn95031_jack_detection);
823
824 /* codec registration */
825 static int sn95031_codec_probe(struct snd_soc_codec *codec)
826 {
827         pr_debug("codec_probe called\n");
828
829         codec->dapm.idle_bias_off = 1;
830
831         /* PCM interface config
832          * This sets the pcm rx slot conguration to max 6 slots
833          * for max 4 dais (2 stereo and 2 mono)
834          */
835         snd_soc_write(codec, SN95031_PCM2RXSLOT01, 0x10);
836         snd_soc_write(codec, SN95031_PCM2RXSLOT23, 0x32);
837         snd_soc_write(codec, SN95031_PCM2RXSLOT45, 0x54);
838         snd_soc_write(codec, SN95031_PCM2TXSLOT01, 0x10);
839         snd_soc_write(codec, SN95031_PCM2TXSLOT23, 0x32);
840         /* pcm port setting
841          * This sets the pcm port to slave and clock at 19.2Mhz which
842          * can support 6slots, sampling rate set per stream in hw-params
843          */
844         snd_soc_write(codec, SN95031_PCM1C1, 0x00);
845         snd_soc_write(codec, SN95031_PCM2C1, 0x01);
846         snd_soc_write(codec, SN95031_PCM2C2, 0x0A);
847         snd_soc_write(codec, SN95031_HSMIXER, BIT(0)|BIT(4));
848         /* vendor vibra workround, the vibras are muted by
849          * custom register so unmute them
850          */
851         snd_soc_write(codec, SN95031_SSR5, 0x80);
852         snd_soc_write(codec, SN95031_SSR6, 0x80);
853         snd_soc_write(codec, SN95031_VIB1C5, 0x00);
854         snd_soc_write(codec, SN95031_VIB2C5, 0x00);
855         /* configure vibras for pcm port */
856         snd_soc_write(codec, SN95031_VIB1C3, 0x00);
857         snd_soc_write(codec, SN95031_VIB2C3, 0x00);
858
859         /* soft mute ramp time */
860         snd_soc_write(codec, SN95031_SOFTMUTE, 0x3);
861         /* fix the initial volume at 1dB,
862          * default in +9dB,
863          * 1dB give optimal swing on DAC, amps
864          */
865         snd_soc_write(codec, SN95031_HSLVOLCTRL, 0x08);
866         snd_soc_write(codec, SN95031_HSRVOLCTRL, 0x08);
867         snd_soc_write(codec, SN95031_IHFLVOLCTRL, 0x08);
868         snd_soc_write(codec, SN95031_IHFRVOLCTRL, 0x08);
869         /* dac mode and lineout workaround */
870         snd_soc_write(codec, SN95031_SSR2, 0x10);
871         snd_soc_write(codec, SN95031_SSR3, 0x40);
872
873         snd_soc_add_controls(codec, sn95031_snd_controls,
874                              ARRAY_SIZE(sn95031_snd_controls));
875
876         return 0;
877 }
878
879 static int sn95031_codec_remove(struct snd_soc_codec *codec)
880 {
881         pr_debug("codec_remove called\n");
882         sn95031_set_vaud_bias(codec, SND_SOC_BIAS_OFF);
883
884         return 0;
885 }
886
887 struct snd_soc_codec_driver sn95031_codec = {
888         .probe          = sn95031_codec_probe,
889         .remove         = sn95031_codec_remove,
890         .read           = sn95031_read,
891         .write          = sn95031_write,
892         .set_bias_level = sn95031_set_vaud_bias,
893         .dapm_widgets   = sn95031_dapm_widgets,
894         .num_dapm_widgets       = ARRAY_SIZE(sn95031_dapm_widgets),
895         .dapm_routes    = sn95031_audio_map,
896         .num_dapm_routes        = ARRAY_SIZE(sn95031_audio_map),
897 };
898
899 static int __devinit sn95031_device_probe(struct platform_device *pdev)
900 {
901         pr_debug("codec device probe called for %s\n", dev_name(&pdev->dev));
902         return snd_soc_register_codec(&pdev->dev, &sn95031_codec,
903                         sn95031_dais, ARRAY_SIZE(sn95031_dais));
904 }
905
906 static int __devexit sn95031_device_remove(struct platform_device *pdev)
907 {
908         pr_debug("codec device remove called\n");
909         snd_soc_unregister_codec(&pdev->dev);
910         return 0;
911 }
912
913 static struct platform_driver sn95031_codec_driver = {
914         .driver         = {
915                 .name           = "sn95031",
916                 .owner          = THIS_MODULE,
917         },
918         .probe          = sn95031_device_probe,
919         .remove         = __devexit_p(sn95031_device_remove),
920 };
921
922 static int __init sn95031_init(void)
923 {
924         pr_debug("driver init called\n");
925         return platform_driver_register(&sn95031_codec_driver);
926 }
927 module_init(sn95031_init);
928
929 static void __exit sn95031_exit(void)
930 {
931         pr_debug("driver exit called\n");
932         platform_driver_unregister(&sn95031_codec_driver);
933 }
934 module_exit(sn95031_exit);
935
936 MODULE_DESCRIPTION("ASoC TI SN95031 codec driver");
937 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
938 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
939 MODULE_LICENSE("GPL v2");
940 MODULE_ALIAS("platform:sn95031");