asoc: AIC3262: Fix headset detection failure during bootup
[linux-2.6.git] / sound / soc / tegra / tegra_aic326x.c
1 /*
2  * tegra_aic326x.c - Tegra machine ASoC driver for boards using TI 3262 codec.
3  *
4  * Author: Vinod G. <vinodg@nvidia.com>
5  *
6  * Copyright (c) 2010-2013, NVIDIA CORPORATION. All rights reserved.
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22
23 #include <asm/mach-types.h>
24
25 #include <linux/clk.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/gpio.h>
30 #include <linux/regulator/consumer.h>
31 #ifdef CONFIG_SWITCH
32 #include <linux/switch.h>
33 #endif
34 #include <mach/tegra_asoc_pdata.h>
35
36 #include <sound/core.h>
37 #include <sound/jack.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <linux/mfd/tlv320aic3262-registers.h>
42 #include <linux/mfd/tlv320aic3xxx-core.h>
43 #include "../codecs/tlv320aic326x.h"
44
45 #include "tegra_pcm.h"
46 #include "tegra_asoc_utils.h"
47
48 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
49 #include "tegra20_das.h"
50 #else
51 #include "tegra30_ahub.h"
52 #include "tegra30_i2s.h"
53 #include "tegra30_dam.h"
54 #endif
55
56
57 #define DRV_NAME "tegra-snd-aic326x"
58
59 #define GPIO_SPKR_EN    BIT(0)
60 #define GPIO_HP_MUTE    BIT(1)
61 #define GPIO_INT_MIC_EN BIT(2)
62 #define GPIO_EXT_MIC_EN BIT(3)
63 #define GPIO_HP_DET     BIT(4)
64
65 #define DAI_LINK_HIFI           0
66 #define DAI_LINK_SPDIF          1
67 #define DAI_LINK_BTSCO          2
68 #define DAI_LINK_VOICE_CALL     3
69 #define DAI_LINK_BT_VOICE_CALL  4
70 #define NUM_DAI_LINKS   5
71
72 extern int g_is_call_mode;
73
74 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
75 const char *tegra_i2s_dai_name[TEGRA30_NR_I2S_IFC] = {
76         "tegra30-i2s.0",
77         "tegra30-i2s.1",
78         "tegra30-i2s.2",
79         "tegra30-i2s.3",
80         "tegra30-i2s.4",
81 };
82 #endif
83
84 struct tegra_aic326x {
85         struct tegra_asoc_utils_data util_data;
86         struct tegra_asoc_platform_data *pdata;
87         struct regulator *audio_reg;
88         int gpio_requested;
89         bool init_done;
90         int is_call_mode;
91         int is_device_bt;
92 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
93         struct codec_config codec_info[NUM_I2S_DEVICES];
94         struct snd_soc_card *pcard;
95         struct regulator *dmic_reg;
96         struct regulator *dmic_1v8_reg;
97         struct regulator *hmic_reg;
98         enum snd_soc_bias_level bias_level;
99 #endif
100         int clock_enabled;
101 };
102
103 static int tegra_aic326x_call_mode_info(struct snd_kcontrol *kcontrol,
104                         struct snd_ctl_elem_info *uinfo)
105 {
106         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
107         uinfo->count = 1;
108         uinfo->value.integer.min = 0;
109         uinfo->value.integer.max = 1;
110         return 0;
111 }
112
113 static int tegra_aic326x_call_mode_get(struct snd_kcontrol *kcontrol,
114                 struct snd_ctl_elem_value *ucontrol)
115 {
116         struct tegra_aic326x *machine = snd_kcontrol_chip(kcontrol);
117
118         ucontrol->value.integer.value[0] = machine->is_call_mode;
119
120         return 0;
121 }
122
123 static int tegra_aic326x_call_mode_put(struct snd_kcontrol *kcontrol,
124                 struct snd_ctl_elem_value *ucontrol)
125 {
126         struct tegra_aic326x *machine = snd_kcontrol_chip(kcontrol);
127         int is_call_mode_new = ucontrol->value.integer.value[0];
128 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
129         int codec_dap_id, codec_dap_sel, bb_dap_id, bb_dap_sel;
130 #else /*assumes tegra3*/
131         int codec_index;
132         unsigned int i;
133         int uses_voice_codec;
134 #endif
135
136         if (machine->is_call_mode == is_call_mode_new)
137                 return 0;
138
139 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
140         bb_dap_id = TEGRA20_DAS_DAP_ID_3;
141         bb_dap_sel = TEGRA20_DAS_DAP_SEL_DAP3;
142
143         if (machine->is_device_bt) {
144                 codec_dap_id = TEGRA20_DAS_DAP_ID_4;
145                 codec_dap_sel = TEGRA20_DAS_DAP_SEL_DAP4;
146         }
147         else {
148                 codec_dap_id = TEGRA20_DAS_DAP_ID_2;
149                 codec_dap_sel = TEGRA20_DAS_DAP_SEL_DAP2;
150         }
151 #else /*assumes tegra3*/
152         if (machine->is_device_bt) {
153                 codec_index = BT_SCO;
154                 uses_voice_codec = 0;
155         } else {
156                 codec_index = VOICE_CODEC;
157                 uses_voice_codec = 1;
158         }
159 #endif
160
161         if (is_call_mode_new) {
162 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
163                 tegra20_das_set_tristate(codec_dap_id, 1);
164                 tegra20_das_set_tristate(bb_dap_id, 1);
165                 tegra20_das_connect_dap_to_dap(codec_dap_id,
166                         bb_dap_sel, 0, 0, 0);
167                 tegra20_das_connect_dap_to_dap(bb_dap_id,
168                         codec_dap_sel, 1, 0, 0);
169                 tegra20_das_set_tristate(codec_dap_id, 0);
170                 tegra20_das_set_tristate(bb_dap_id, 0);
171 #else /*assumes tegra3*/
172                 if (machine->codec_info[codec_index].rate == 0 ||
173                         machine->codec_info[codec_index].channels == 0)
174                                 return -EINVAL;
175
176                 for (i = 0; i < machine->pcard->num_links; i++)
177                         machine->pcard->dai_link[i].ignore_suspend = 1;
178
179                 tegra30_make_voice_call_connections(
180                         &machine->codec_info[codec_index],
181                         &machine->codec_info[BASEBAND], uses_voice_codec);
182 #endif
183         } else {
184 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
185                 tegra20_das_set_tristate(codec_dap_id, 1);
186                 tegra20_das_set_tristate(bb_dap_id, 1);
187                 tegra20_das_connect_dap_to_dap(bb_dap_id,
188                         bb_dap_sel, 0, 0, 0);
189                 tegra20_das_connect_dap_to_dap(codec_dap_id,
190                         codec_dap_sel, 0, 0, 0);
191                 tegra20_das_set_tristate(codec_dap_id, 0);
192                 tegra20_das_set_tristate(bb_dap_id, 0);
193 #else /*assumes tegra3*/
194                 tegra30_break_voice_call_connections(
195                         &machine->codec_info[codec_index],
196                         &machine->codec_info[BASEBAND], uses_voice_codec);
197
198                 for (i = 0; i < machine->pcard->num_links; i++)
199                         machine->pcard->dai_link[i].ignore_suspend = 0;
200 #endif
201         }
202
203         machine->is_call_mode = is_call_mode_new;
204         g_is_call_mode = machine->is_call_mode;
205
206         return 1;
207 }
208
209 struct snd_kcontrol_new tegra_aic326x_call_mode_control = {
210         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
211         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
212         .name = "Call Mode Switch",
213         .private_value = 0xffff,
214         .info = tegra_aic326x_call_mode_info,
215         .get = tegra_aic326x_call_mode_get,
216         .put = tegra_aic326x_call_mode_put
217 };
218
219 static int tegra_aic326x_get_mclk(int srate)
220 {
221         int mclk = 0;
222         switch (srate) {
223         case 8000:
224         case 16000:
225         case 24000:
226         case 32000:
227         case 48000:
228         case 64000:
229         case 96000:
230                 mclk = 12288000;
231                 break;
232         case 11025:
233         case 22050:
234         case 44100:
235         case 88200:
236                 mclk = 11289600;
237                 break;
238         default:
239                 mclk = -EINVAL;
240                 break;
241         }
242
243         return mclk;
244 }
245
246 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
247 static int tegra_aic326x_set_dam_cif(int dam_ifc, int srate,
248                         int channels, int bit_size, int src_on, int src_srate,
249                         int src_channels, int src_bit_size)
250 {
251         tegra30_dam_set_gain(dam_ifc, TEGRA30_DAM_CHIN1, 0x1000);
252         tegra30_dam_set_samplerate(dam_ifc, TEGRA30_DAM_CHOUT,
253                                 srate);
254         tegra30_dam_set_samplerate(dam_ifc, TEGRA30_DAM_CHIN1,
255                                 srate);
256 #ifndef CONFIG_ARCH_TEGRA_3x_SOC
257         tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHIN1,
258                 channels, bit_size, channels,
259                                 32);
260         tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHOUT,
261                 channels, bit_size, channels,
262                                 32);
263 #else
264         tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHIN1,
265                 channels, bit_size, channels,
266                                 bit_size);
267         tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHOUT,
268                 channels, bit_size, channels,
269                                 bit_size);
270 #endif
271
272         if (src_on) {
273                 tegra30_dam_set_gain(dam_ifc, TEGRA30_DAM_CHIN0_SRC, 0x1000);
274                 tegra30_dam_set_samplerate(dam_ifc, TEGRA30_DAM_CHIN0_SRC,
275                         src_srate);
276 #ifndef CONFIG_ARCH_TEGRA_3x_SOC
277                 tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHIN0_SRC,
278                         src_channels, src_bit_size, 1, 32);
279 #else
280                 tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHIN0_SRC,
281                         src_channels, src_bit_size, 1, 16);
282 #endif
283         }
284
285         return 0;
286 }
287 #endif
288
289 static int tegra_aic326x_hw_params(struct snd_pcm_substream *substream,
290                                         struct snd_pcm_hw_params *params)
291 {
292         struct snd_soc_pcm_runtime *rtd = substream->private_data;
293         struct snd_soc_dai *codec_dai = rtd->codec_dai;
294         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
295         struct snd_soc_codec *codec = rtd->codec;
296         struct snd_soc_card *card = codec->card;
297         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
298         struct tegra_asoc_platform_data *pdata = machine->pdata;
299         int srate, mclk, sample_size, i2s_daifmt;
300         int err, rate;
301
302         switch (params_format(params)) {
303         case SNDRV_PCM_FORMAT_S16_LE:
304                 sample_size = 16;
305                 break;
306         default:
307                 return -EINVAL;
308         }
309
310         srate = params_rate(params);
311
312         mclk = tegra_aic326x_get_mclk(srate);
313         if (mclk < 0)
314                 return mclk;
315
316         i2s_daifmt = SND_SOC_DAIFMT_NB_NF;
317         i2s_daifmt |= pdata->i2s_param[HIFI_CODEC].is_i2s_master ?
318                         SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
319
320         switch (pdata->i2s_param[HIFI_CODEC].i2s_mode) {
321                 case TEGRA_DAIFMT_I2S :
322                         i2s_daifmt |= SND_SOC_DAIFMT_I2S;
323                         break;
324                 case TEGRA_DAIFMT_DSP_A :
325                         i2s_daifmt |= SND_SOC_DAIFMT_DSP_A;
326                         break;
327                 case TEGRA_DAIFMT_DSP_B :
328                         i2s_daifmt |= SND_SOC_DAIFMT_DSP_B;
329                         break;
330                 case TEGRA_DAIFMT_LEFT_J :
331                         i2s_daifmt |= SND_SOC_DAIFMT_LEFT_J;
332                         break;
333                 case TEGRA_DAIFMT_RIGHT_J :
334                         i2s_daifmt |= SND_SOC_DAIFMT_RIGHT_J;
335                         break;
336                 default :
337                         dev_err(card->dev, "Can't configure i2s format\n");
338                         return -EINVAL;
339         }
340
341         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
342         if (err < 0) {
343                 if (!(machine->util_data.set_mclk % mclk))
344                         mclk = machine->util_data.set_mclk;
345                 else {
346                         dev_err(card->dev, "Can't configure clocks\n");
347                         return err;
348                 }
349         }
350
351         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
352
353         rate = clk_get_rate(machine->util_data.clk_cdev1);
354
355         err = snd_soc_dai_set_fmt(codec_dai, i2s_daifmt);
356         if (err < 0) {
357                 dev_err(card->dev, "codec_dai fmt not set\n");
358                 return err;
359         }
360
361         err = snd_soc_dai_set_fmt(cpu_dai, i2s_daifmt);
362         if (err < 0) {
363                 dev_err(card->dev, "cpu_dai fmt not set\n");
364                 return err;
365         }
366
367         err = snd_soc_dai_set_pll(codec_dai, 0, AIC3262_PLL_CLKIN_MCLK1 , rate,
368                 params_rate(params));
369
370 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
371         err = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAP_SEL_DAC1,
372                                         TEGRA20_DAS_DAP_ID_1);
373         if (err < 0) {
374                 dev_err(card->dev, "failed to set dap-dac path\n");
375                 return err;
376         }
377
378         err = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1,
379                                         TEGRA20_DAS_DAP_SEL_DAC1);
380         if (err < 0) {
381                 dev_err(card->dev, "failed to set dac-dap path\n");
382                 return err;
383         }
384 #endif
385         return 0;
386 }
387
388 static int tegra_aic326x_spdif_hw_params(struct snd_pcm_substream *substream,
389                                         struct snd_pcm_hw_params *params)
390 {
391         struct snd_soc_pcm_runtime *rtd = substream->private_data;
392         struct snd_soc_card *card = rtd->card;
393         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
394         int srate, mclk, min_mclk;
395         int err;
396
397         srate = params_rate(params);
398
399         mclk = tegra_aic326x_get_mclk(srate);
400         if (mclk < 0)
401                 return mclk;
402
403         min_mclk = 128 * srate;
404
405         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
406         if (err < 0) {
407                 if (!(machine->util_data.set_mclk % min_mclk))
408                         mclk = machine->util_data.set_mclk;
409                 else {
410                         dev_err(card->dev, "Can't configure clocks\n");
411                         return err;
412                 }
413         }
414
415         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
416
417         return 0;
418 }
419
420 static int tegra_aic326x_bt_hw_params(struct snd_pcm_substream *substream,
421                 struct snd_pcm_hw_params *params)
422 {
423         struct snd_soc_pcm_runtime *rtd = substream->private_data;
424         struct snd_soc_card *card = rtd->card;
425         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
426         struct tegra_asoc_platform_data *pdata = machine->pdata;
427         int err, srate, mclk, min_mclk, sample_size, i2s_daifmt;
428 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
429         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
430 #endif
431
432         switch (params_format(params)) {
433         case SNDRV_PCM_FORMAT_S16_LE:
434                 sample_size = 16;
435                 break;
436         default:
437                 return -EINVAL;
438         }
439
440         srate = params_rate(params);
441
442         mclk = tegra_aic326x_get_mclk(srate);
443         if (mclk < 0)
444                 return mclk;
445
446         min_mclk = 64 * srate;
447
448         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
449         if (err < 0) {
450                 if (!(machine->util_data.set_mclk % min_mclk))
451                         mclk = machine->util_data.set_mclk;
452                 else {
453                         dev_err(card->dev, "Can't configure clocks\n");
454                         return err;
455                 }
456         }
457
458         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
459
460         i2s_daifmt = SND_SOC_DAIFMT_NB_NF;
461         i2s_daifmt |= pdata->i2s_param[BT_SCO].is_i2s_master ?
462                         SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
463
464         switch (pdata->i2s_param[BT_SCO].i2s_mode) {
465                 case TEGRA_DAIFMT_I2S :
466                         i2s_daifmt |= SND_SOC_DAIFMT_I2S;
467                         break;
468                 case TEGRA_DAIFMT_DSP_A :
469                         i2s_daifmt |= SND_SOC_DAIFMT_DSP_A;
470                         break;
471                 case TEGRA_DAIFMT_DSP_B :
472                         i2s_daifmt |= SND_SOC_DAIFMT_DSP_B;
473                         break;
474                 case TEGRA_DAIFMT_LEFT_J :
475                         i2s_daifmt |= SND_SOC_DAIFMT_LEFT_J;
476                         break;
477                 case TEGRA_DAIFMT_RIGHT_J :
478                         i2s_daifmt |= SND_SOC_DAIFMT_RIGHT_J;
479                         break;
480                 default :
481                         dev_err(card->dev, "Can't configure i2s format\n");
482                         return -EINVAL;
483         }
484
485         err = snd_soc_dai_set_fmt(rtd->cpu_dai, i2s_daifmt);
486
487         if (err < 0) {
488                 dev_err(rtd->codec->card->dev, "cpu_dai fmt not set\n");
489                 return err;
490         }
491
492 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
493         err = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAP_SEL_DAC2,
494                                         TEGRA20_DAS_DAP_ID_4);
495         if (err < 0) {
496                 dev_err(card->dev, "failed to set dac-dap path\n");
497                 return err;
498         }
499
500         err = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_4,
501                                         TEGRA20_DAS_DAP_SEL_DAC2);
502         if (err < 0) {
503                 dev_err(card->dev, "failed to set dac-dap path\n");
504                 return err;
505         }
506 #else
507         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && i2s->is_dam_used)
508                 tegra_aic326x_set_dam_cif(i2s->dam_ifc, params_rate(params),
509                         params_channels(params), sample_size, 0, 0, 0, 0);
510 #endif
511
512         return 0;
513 }
514
515 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
516 static int tegra_aic326x_startup(struct snd_pcm_substream *substream)
517 {
518         struct snd_soc_pcm_runtime *rtd = substream->private_data;
519         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
520         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
521         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(rtd->card);
522         struct codec_config *codec_info;
523         struct codec_config *bb_info;
524         struct codec_config *hifi_info;
525         int codec_index;
526
527         if (!i2s->is_dam_used)
528                 return 0;
529
530         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
531                 /*
532                 *make apbif tx to i2s rx connection if this is the only client
533                 *using i2s for playback
534                 */
535                 if (i2s->playback_ref_count == 1) {
536                         tegra30_ahub_set_rx_cif_source(
537                                 TEGRA30_AHUB_RXCIF_I2S0_RX0 + i2s->id,
538                                 i2s->txcif);
539                         tegra30_ahub_enable_clocks();
540                 }
541
542         } else {
543                 i2s->is_call_mode_rec = machine->is_call_mode;
544                 if (!i2s->is_call_mode_rec)
545                         return 0;
546
547                 if (machine->is_device_bt)
548                         codec_index = BT_SCO;
549                 else
550                         codec_index = VOICE_CODEC;
551
552                 codec_info = &machine->codec_info[codec_index];
553                 bb_info = &machine->codec_info[BASEBAND];
554                 hifi_info = &machine->codec_info[HIFI_CODEC];
555
556                 /* allocate a dams for voice call recording */
557
558                 i2s->call_record_dam_ifc = tegra30_dam_allocate_controller();
559
560                 if (i2s->call_record_dam_ifc < 0)
561                         return i2s->call_record_dam_ifc;
562
563                 tegra30_dam_allocate_channel(i2s->call_record_dam_ifc,
564                         TEGRA30_DAM_CHIN0_SRC);
565                 tegra30_dam_allocate_channel(i2s->call_record_dam_ifc,
566                         TEGRA30_DAM_CHIN1);
567                 tegra30_dam_enable_clock(i2s->call_record_dam_ifc);
568
569                 i2s->call_record_dam_ifc2 = tegra30_dam_allocate_controller();
570
571                 if (i2s->call_record_dam_ifc2 < 0)
572                         return i2s->call_record_dam_ifc2;
573
574                 tegra30_dam_allocate_channel(i2s->call_record_dam_ifc2,
575                         TEGRA30_DAM_CHIN0_SRC);
576                 tegra30_dam_allocate_channel(i2s->call_record_dam_ifc2,
577                         TEGRA30_DAM_CHIN1);
578                 tegra30_dam_enable_clock(i2s->call_record_dam_ifc2);
579
580                 /* configure the dams */
581                 /* DAM0 SRC bb rate to hifi rate */
582                 tegra_aic326x_set_dam_cif(i2s->call_record_dam_ifc,
583                         codec_info->rate, codec_info->channels,
584                         codec_info->bitsize, 1, hifi_info->rate,
585                         hifi_info->channels, hifi_info->bitsize);
586                 /* DAM1 UL + DL Mix */
587                 tegra_aic326x_set_dam_cif(i2s->call_record_dam_ifc2,
588                         codec_info->rate, codec_info->channels,
589                         codec_info->bitsize, 1, bb_info->rate,
590                         bb_info->channels, bb_info->bitsize);
591
592                 /* setup the connections for voice call record */
593                 tegra30_ahub_unset_rx_cif_source(i2s->rxcif);
594                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
595                         (i2s->call_record_dam_ifc2*2),
596                         TEGRA30_AHUB_TXCIF_I2S0_TX0 + bb_info->i2s_id);
597                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
598                         (i2s->call_record_dam_ifc2*2),
599                         TEGRA30_AHUB_TXCIF_I2S0_TX0 + codec_info->i2s_id);
600                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
601                         (i2s->call_record_dam_ifc*2),
602                         TEGRA30_AHUB_TXCIF_DAM0_TX0 +
603                         i2s->call_record_dam_ifc2);
604                 tegra30_ahub_set_rx_cif_source(i2s->rxcif,
605                         TEGRA30_AHUB_TXCIF_DAM0_TX0 + i2s->call_record_dam_ifc);
606 #ifndef CONFIG_ARCH_TEGRA_3x_SOC
607                 /* Configure DAM0 for SRC */
608                 if (bb_info->rate != hifi_info->rate) {
609                         tegra30_dam_write_coeff_ram(i2s->call_record_dam_ifc,
610                                                 bb_info->rate, hifi_info->rate);
611                         tegra30_dam_set_farrow_param(i2s->call_record_dam_ifc,
612                                                 bb_info->rate, hifi_info->rate);
613                         tegra30_dam_set_biquad_fixed_coef(
614                                                 i2s->call_record_dam_ifc);
615                         tegra30_dam_enable_coeff_ram(i2s->call_record_dam_ifc);
616                         tegra30_dam_set_filter_stages(i2s->call_record_dam_ifc,
617                                                         bb_info->rate,
618                                                         hifi_info->rate);
619                 }
620 #endif
621                 /* enable the dam */
622                 tegra30_dam_enable(i2s->call_record_dam_ifc, TEGRA30_DAM_ENABLE,
623                                 TEGRA30_DAM_CHIN1);
624                 tegra30_dam_enable(i2s->call_record_dam_ifc, TEGRA30_DAM_ENABLE,
625                                 TEGRA30_DAM_CHIN0_SRC);
626                 tegra30_dam_enable(i2s->call_record_dam_ifc2,
627                                         TEGRA30_DAM_ENABLE,
628                                         TEGRA30_DAM_CHIN1);
629                 tegra30_dam_enable(i2s->call_record_dam_ifc2,
630                                         TEGRA30_DAM_ENABLE,
631                                         TEGRA30_DAM_CHIN0_SRC);
632         }
633
634         return 0;
635 }
636
637 static void tegra_aic326x_shutdown(struct snd_pcm_substream *substream)
638 {
639         struct snd_soc_pcm_runtime *rtd = substream->private_data;
640         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
641         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
642
643         if (!i2s->is_dam_used)
644                 return;
645
646         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
647                 tegra30_ahub_unset_rx_cif_source(
648                                         TEGRA30_AHUB_RXCIF_I2S0_RX0 + i2s->id);
649                 tegra30_ahub_disable_clocks();
650          } else {
651                 if (!i2s->is_call_mode_rec)
652                         return;
653
654                 i2s->is_call_mode_rec = 0;
655
656                 /* disable the dams*/
657                 tegra30_dam_enable(i2s->call_record_dam_ifc,
658                         TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN1);
659                 tegra30_dam_enable(i2s->call_record_dam_ifc,
660                         TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN0_SRC);
661                 tegra30_dam_enable(i2s->call_record_dam_ifc2,
662                         TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN1);
663                 tegra30_dam_enable(i2s->call_record_dam_ifc2,
664                         TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN0_SRC);
665
666                 /* disconnect the ahub connections*/
667                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
668                                                 (i2s->call_record_dam_ifc2*2));
669                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
670                                                 (i2s->call_record_dam_ifc2*2));
671                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
672                                                 (i2s->call_record_dam_ifc*2));
673                 tegra30_ahub_unset_rx_cif_source(i2s->rxcif);
674
675                 /* free the dam channels and dam controllers */
676                 tegra30_dam_disable_clock(i2s->call_record_dam_ifc);
677                 tegra30_dam_free_channel(i2s->call_record_dam_ifc,
678                                         TEGRA30_DAM_CHIN1);
679                 tegra30_dam_free_channel(i2s->call_record_dam_ifc,
680                                         TEGRA30_DAM_CHIN0_SRC);
681                 tegra30_dam_free_controller(i2s->call_record_dam_ifc);
682
683                 tegra30_dam_disable_clock(i2s->call_record_dam_ifc2);
684                 tegra30_dam_free_channel(i2s->call_record_dam_ifc2,
685                                         TEGRA30_DAM_CHIN1);
686                 tegra30_dam_free_channel(i2s->call_record_dam_ifc2,
687                                         TEGRA30_DAM_CHIN0_SRC);
688                 tegra30_dam_free_controller(i2s->call_record_dam_ifc2);
689          }
690
691         return;
692 }
693 #endif
694
695
696 static int tegra_aic326x_hw_free(struct snd_pcm_substream *substream)
697 {
698         struct snd_soc_pcm_runtime *rtd = substream->private_data;
699         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(rtd->card);
700
701         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 0);
702
703         return 0;
704 }
705
706 static int tegra_aic326x_voice_call_hw_params(
707                         struct snd_pcm_substream *substream,
708                         struct snd_pcm_hw_params *params)
709 {
710         struct snd_soc_pcm_runtime *rtd = substream->private_data;
711         struct snd_soc_dai *codec_dai = rtd->codec_dai;
712         struct snd_soc_codec *codec = rtd->codec;
713         struct snd_soc_card *card = codec->card;
714         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
715         struct tegra_asoc_platform_data *pdata = machine->pdata;
716         int srate, mclk, rate, i2s_daifmt;
717         int err, pcmdiv, vxclkdiv;
718
719         srate = params_rate(params);
720         mclk = tegra_aic326x_get_mclk(srate);
721         if (mclk < 0)
722                 return mclk;
723
724         i2s_daifmt = SND_SOC_DAIFMT_NB_NF;
725
726         i2s_daifmt |= pdata->i2s_param[VOICE_CODEC].is_i2s_master ?
727                 SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
728
729         switch (pdata->i2s_param[VOICE_CODEC].i2s_mode) {
730         case TEGRA_DAIFMT_I2S:
731                 i2s_daifmt |= SND_SOC_DAIFMT_I2S;
732                 break;
733         case TEGRA_DAIFMT_DSP_A:
734                 i2s_daifmt |= SND_SOC_DAIFMT_DSP_A;
735                 break;
736         case TEGRA_DAIFMT_DSP_B:
737                 i2s_daifmt |= SND_SOC_DAIFMT_DSP_B;
738                 break;
739         case TEGRA_DAIFMT_LEFT_J:
740                 i2s_daifmt |= SND_SOC_DAIFMT_LEFT_J;
741                 break;
742         case TEGRA_DAIFMT_RIGHT_J:
743                 i2s_daifmt |= SND_SOC_DAIFMT_RIGHT_J;
744                 break;
745         default:
746                 dev_err(card->dev,
747                 "Can't configure i2s format\n");
748                 return -EINVAL;
749         }
750
751         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
752         if (err < 0) {
753                 if (!(machine->util_data.set_mclk % mclk))
754                         mclk = machine->util_data.set_mclk;
755                 else {
756                         dev_err(card->dev, "Can't configure clocks\n");
757                         return err;
758                 }
759         }
760
761         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
762
763         rate = clk_get_rate(machine->util_data.clk_cdev1);
764
765         err = snd_soc_dai_set_fmt(codec_dai, i2s_daifmt);
766         if (err < 0) {
767                 dev_err(card->dev, "codec_dai fmt not set\n");
768                 return err;
769         }
770
771
772         err = snd_soc_dai_set_pll(codec_dai, 0, AIC3262_PLL_CLKIN_MCLK1 , rate,
773                         params_rate(params));
774
775         if (err < 0) {
776                 dev_err(card->dev, "codec_dai PLL clock not set\n");
777                 return err;
778         }
779
780         if (!machine_is_tegra_enterprise()) {
781                 if (params_rate(params) == 8000) {
782                         /* Change these Settings for 8KHz*/
783                         pcmdiv = 1;
784                         /* BB expecting 2048Khz bclk */
785                         vxclkdiv = 27;
786                 } else if (params_rate(params) == 16000) {
787                         pcmdiv = 1;
788                         /* BB expecting 2048Khz bclk */
789                         vxclkdiv = 27;
790                 } else {
791                         dev_err(card->dev, "codec_dai unsupported voice rate\n");
792                         return -EINVAL;
793                 }
794         }
795
796         //snd_soc_dai_set_clkdiv(codec_dai, ASI2_BCLK_N, vxclkdiv);
797         //snd_soc_dai_set_clkdiv(codec_dai, ASI2_WCLK_N, pcmdiv);
798
799 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
800         /* codec configuration */
801         machine->codec_info[VOICE_CODEC].rate = params_rate(params);
802         machine->codec_info[VOICE_CODEC].channels = params_channels(params);
803 #endif
804
805         machine->is_device_bt = 0;
806
807         return 0;
808 }
809
810 static void tegra_aic326x_voice_call_shutdown(
811                                         struct snd_pcm_substream *substream)
812 {
813         struct snd_soc_pcm_runtime *rtd = substream->private_data;
814         struct tegra_aic326x *machine  =
815                         snd_soc_card_get_drvdata(rtd->codec->card);
816
817 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
818         machine->codec_info[VOICE_CODEC].rate = 0;
819         machine->codec_info[VOICE_CODEC].channels = 0;
820 #endif
821
822         machine->is_device_bt = 0;
823 }
824
825 static int tegra_aic326x_bt_voice_call_hw_params(
826                         struct snd_pcm_substream *substream,
827                         struct snd_pcm_hw_params *params)
828 {
829         struct snd_soc_pcm_runtime *rtd = substream->private_data;
830         struct snd_soc_card *card = rtd->card;
831         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
832         int err, srate, mclk, min_mclk;
833
834         srate = params_rate(params);
835
836         mclk = tegra_aic326x_get_mclk(srate);
837         if (mclk < 0)
838                 return mclk;
839
840         min_mclk = 64 * srate;
841
842         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
843         if (err < 0) {
844                 if (!(machine->util_data.set_mclk % min_mclk))
845                         mclk = machine->util_data.set_mclk;
846                 else {
847                         dev_err(card->dev, "Can't configure clocks\n");
848                         return err;
849                 }
850         }
851
852         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
853
854 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
855         /* codec configuration */
856         machine->codec_info[BT_SCO].rate = params_rate(params);
857         machine->codec_info[BT_SCO].channels = params_channels(params);
858 #endif
859
860         machine->is_device_bt = 1;
861
862         return 0;
863 }
864
865 static void tegra_aic326x_bt_voice_call_shutdown(
866                                 struct snd_pcm_substream *substream)
867 {
868         struct snd_soc_pcm_runtime *rtd = substream->private_data;
869         struct tegra_aic326x *machine  =
870                         snd_soc_card_get_drvdata(rtd->codec->card);
871
872 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
873         machine->codec_info[BT_SCO].rate = 0;
874         machine->codec_info[BT_SCO].channels = 0;
875 #endif
876
877         machine->is_device_bt = 0;
878 }
879
880 static struct snd_soc_ops tegra_aic326x_hifi_ops = {
881         .hw_params = tegra_aic326x_hw_params,
882         .hw_free = tegra_aic326x_hw_free,
883 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
884         .startup = tegra_aic326x_startup,
885         .shutdown = tegra_aic326x_shutdown,
886 #endif
887 };
888
889 static struct snd_soc_ops tegra_aic326x_spdif_ops = {
890         .hw_params = tegra_aic326x_spdif_hw_params,
891         .hw_free = tegra_aic326x_hw_free,
892 };
893
894 static struct snd_soc_ops tegra_aic326x_voice_call_ops = {
895         .hw_params = tegra_aic326x_voice_call_hw_params,
896         .shutdown = tegra_aic326x_voice_call_shutdown,
897         .hw_free = tegra_aic326x_hw_free,
898 };
899
900 static struct snd_soc_ops tegra_aic326x_bt_voice_call_ops = {
901         .hw_params = tegra_aic326x_bt_voice_call_hw_params,
902         .shutdown = tegra_aic326x_bt_voice_call_shutdown,
903         .hw_free = tegra_aic326x_hw_free,
904 };
905
906 static struct snd_soc_ops tegra_aic326x_bt_ops = {
907         .hw_params = tegra_aic326x_bt_hw_params,
908         .hw_free = tegra_aic326x_hw_free,
909 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
910         .startup = tegra_aic326x_startup,
911         .shutdown = tegra_aic326x_shutdown,
912 #endif
913 };
914
915 static struct snd_soc_jack tegra_aic326x_hp_jack;
916
917 #ifdef CONFIG_SWITCH
918 static struct switch_dev aic326x_wired_switch_dev = {
919         .name = "h2w",
920 };
921
922 /* Headset jack detection gpios */
923 static struct snd_soc_jack_gpio tegra_aic326x_hp_jack_gpio = {
924         .name = "headphone detect",
925         .report = SND_JACK_HEADPHONE,
926         .debounce_time = 150,
927         .invert = 1,
928 };
929
930 /* These values are copied from WiredAccessoryObserver */
931 enum headset_state {
932         BIT_NO_HEADSET = 0,
933         BIT_HEADSET = (1 << 0),
934         BIT_HEADSET_NO_MIC = (1 << 1),
935 };
936
937 static int aic326x_headset_switch_notify(struct notifier_block *self,
938         unsigned long action, void *dev)
939 {
940         int state = BIT_NO_HEADSET;
941
942         switch (action) {
943         case SND_JACK_HEADPHONE:
944                 state |= BIT_HEADSET_NO_MIC;
945                 break;
946         case SND_JACK_HEADSET:
947                 state |= BIT_HEADSET;
948                 break;
949         default:
950                 state |= BIT_NO_HEADSET;
951         }
952
953         switch_set_state(&aic326x_wired_switch_dev, state);
954
955         return NOTIFY_OK;
956 }
957
958 static struct notifier_block aic326x_headset_switch_nb = {
959         .notifier_call = aic326x_headset_switch_notify,
960 };
961 #else
962 static struct snd_soc_jack_pin tegra_aic326x_hp_jack_pins[] = {
963         {
964                 .pin = "Headphone Jack",
965                 .mask = SND_JACK_HEADPHONE,
966         },
967 };
968 #endif
969
970 static int tegra_aic326x_event_int_spk(struct snd_soc_dapm_widget *w,
971                                         struct snd_kcontrol *k, int event)
972 {
973         struct snd_soc_dapm_context *dapm = w->dapm;
974         struct snd_soc_card *card = dapm->card;
975         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
976         struct tegra_asoc_platform_data *pdata = machine->pdata;
977
978         if (!(machine->gpio_requested & GPIO_SPKR_EN))
979                 return 0;
980
981         gpio_set_value_cansleep(pdata->gpio_spkr_en,
982                                 !!SND_SOC_DAPM_EVENT_ON(event));
983
984         return 0;
985 }
986
987 static int tegra_aic326x_event_hp(struct snd_soc_dapm_widget *w,
988                                         struct snd_kcontrol *k, int event)
989 {
990         struct snd_soc_dapm_context *dapm = w->dapm;
991         struct snd_soc_card *card = dapm->card;
992         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
993         struct tegra_asoc_platform_data *pdata = machine->pdata;
994
995         if (!(machine->gpio_requested & GPIO_HP_MUTE))
996                 return 0;
997
998         gpio_set_value_cansleep(pdata->gpio_hp_mute,
999                                 !SND_SOC_DAPM_EVENT_ON(event));
1000
1001         return 0;
1002 }
1003
1004 static int tegra_aic326x_event_dmic(struct snd_soc_dapm_widget *w,
1005                                         struct snd_kcontrol *k, int event)
1006 {
1007         struct snd_soc_dapm_context *dapm = w->dapm;
1008         struct snd_soc_card *card = dapm->card;
1009         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1010         struct tegra_asoc_platform_data *pdata = machine->pdata;
1011
1012         if (machine->dmic_reg && machine->dmic_1v8_reg) {
1013                 if (SND_SOC_DAPM_EVENT_ON(event)) {
1014                         regulator_enable(machine->dmic_reg);
1015                         regulator_enable(machine->dmic_1v8_reg);
1016                 } else {
1017                         regulator_disable(machine->dmic_reg);
1018                         regulator_disable(machine->dmic_1v8_reg);
1019                 }
1020         }
1021
1022         if (!(machine->gpio_requested & GPIO_INT_MIC_EN))
1023                 return 0;
1024
1025         gpio_set_value_cansleep(pdata->gpio_int_mic_en,
1026                                 !!SND_SOC_DAPM_EVENT_ON(event));
1027
1028         return 0;
1029 }
1030
1031 static const struct snd_soc_dapm_widget tegra_aic326x_dapm_widgets[] = {
1032         SND_SOC_DAPM_SPK("Int Spk", tegra_aic326x_event_int_spk),
1033         SND_SOC_DAPM_HP("Earpiece", NULL),
1034         SND_SOC_DAPM_HP("Headphone Jack", tegra_aic326x_event_hp),
1035         SND_SOC_DAPM_MIC("Mic Jack", NULL),
1036         SND_SOC_DAPM_INPUT("Ext Mic"),
1037         SND_SOC_DAPM_LINE("Linein", NULL),
1038         SND_SOC_DAPM_MIC("Int Mic", tegra_aic326x_event_dmic),
1039         SND_SOC_DAPM_MIC("DMIC", NULL),
1040 };
1041
1042 static const struct snd_soc_dapm_route aic326x_audio_map[] = {
1043         {"Int Spk", NULL, "SPK Left Playback"},
1044         {"Int Spk", NULL, "SPK Right Playback"},
1045         {"Earpiece", NULL, "RECP Playback"},
1046         {"Earpiece", NULL, "RECM Playback"},
1047         {"Headphone Jack", NULL, "HP Left Playback"},
1048         {"Headphone Jack", NULL, "HP Right Playback"},
1049         /* internal (IN2L/IN2R) mic is stero */
1050         {"Mic Bias Int" ,NULL, "Int Mic"},
1051         {"IN2 Left Capture", NULL, "Mic Bias Int"},
1052         {"Mic Bias Int" ,NULL, "Int Mic"},
1053         {"IN2 Right Capture", NULL, "Mic Bias Int"},
1054         {"IN1 Left Capture", NULL, "Mic Bias Ext"},
1055         {"Mic Bias Ext" , NULL, "Mic Jack"},
1056         /* Connect LDMIC and RDMIC to DMIC widget*/
1057         {"Left DMIC Capture", NULL, "Mic Bias Int"},
1058         {"Right DMIC Capture", NULL, "Mic Bias Int"},
1059         {"Mic Bias Int", NULL, "Int Mic"},
1060 };
1061
1062 static const struct snd_kcontrol_new tegra_aic326x_controls[] = {
1063         SOC_DAPM_PIN_SWITCH("Int Spk"),
1064         SOC_DAPM_PIN_SWITCH("Earpiece"),
1065         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
1066         SOC_DAPM_PIN_SWITCH("Mic Jack"),
1067         SOC_DAPM_PIN_SWITCH("Ext Mic"),
1068         SOC_DAPM_PIN_SWITCH("Linein"),
1069         SOC_DAPM_PIN_SWITCH("Int Mic"),
1070 };
1071
1072 static int tegra_aic326x_init(struct snd_soc_pcm_runtime *rtd)
1073 {
1074         struct snd_soc_codec *codec = rtd->codec;
1075         struct snd_soc_dapm_context *dapm = &codec->dapm;
1076         struct snd_soc_card *card = codec->card;
1077         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1078         struct tegra_asoc_platform_data *pdata = machine->pdata;
1079 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1080         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1081 #endif
1082         int ret;
1083
1084 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1085         if (machine->codec_info[BASEBAND].i2s_id != -1)
1086                 i2s->is_dam_used = true;
1087 #endif
1088
1089         if (machine->init_done)
1090                 return 0;
1091
1092         machine->init_done = true;
1093
1094 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1095         machine->pcard = card;
1096 #endif
1097
1098         if (machine_is_whistler()) {
1099                 machine->audio_reg = regulator_get(NULL, "avddio_audio");
1100                 if (IS_ERR(machine->audio_reg)) {
1101                         dev_err(card->dev, "cannot get avddio_audio reg\n");
1102                         ret = PTR_ERR(machine->audio_reg);
1103                         return ret;
1104                 }
1105
1106                 ret = regulator_enable(machine->audio_reg);
1107                 if (ret) {
1108                         dev_err(card->dev, "cannot enable avddio_audio reg\n");
1109                         regulator_put(machine->audio_reg);
1110                         machine->audio_reg = NULL;
1111                         return ret;
1112                 }
1113         }
1114
1115         if (gpio_is_valid(pdata->gpio_spkr_en)) {
1116                 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
1117                 if (ret) {
1118                         dev_err(card->dev, "cannot get spkr_en gpio\n");
1119                         return ret;
1120                 }
1121                 machine->gpio_requested |= GPIO_SPKR_EN;
1122
1123                 gpio_direction_output(pdata->gpio_spkr_en, 0);
1124         }
1125
1126         if (gpio_is_valid(pdata->gpio_hp_mute)) {
1127                 ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
1128                 if (ret) {
1129                         dev_err(card->dev, "cannot get hp_mute gpio\n");
1130                         return ret;
1131                 }
1132                 machine->gpio_requested |= GPIO_HP_MUTE;
1133
1134                 gpio_direction_output(pdata->gpio_hp_mute, 0);
1135         }
1136
1137         if (gpio_is_valid(pdata->gpio_int_mic_en)) {
1138                 ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
1139                 if (ret) {
1140                         dev_err(card->dev, "cannot get int_mic_en gpio\n");
1141                         return ret;
1142                 }
1143                 machine->gpio_requested |= GPIO_INT_MIC_EN;
1144
1145                 /* Disable int mic; enable signal is active-high */
1146                 gpio_direction_output(pdata->gpio_int_mic_en, 0);
1147         }
1148
1149         if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
1150                 ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
1151                 if (ret) {
1152                         dev_err(card->dev, "cannot get ext_mic_en gpio\n");
1153                         return ret;
1154                 }
1155                 machine->gpio_requested |= GPIO_EXT_MIC_EN;
1156
1157                 /* Enable ext mic; enable signal is active-low */
1158                 gpio_direction_output(pdata->gpio_ext_mic_en, 0);
1159         }
1160
1161         ret = snd_soc_add_card_controls(card, tegra_aic326x_controls,
1162                                    ARRAY_SIZE(tegra_aic326x_controls));
1163         if (ret < 0)
1164                 return ret;
1165
1166         snd_soc_dapm_new_controls(dapm, tegra_aic326x_dapm_widgets,
1167                                         ARRAY_SIZE(tegra_aic326x_dapm_widgets));
1168
1169         snd_soc_dapm_add_routes(dapm, aic326x_audio_map,
1170                                         ARRAY_SIZE(aic326x_audio_map));
1171
1172         ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
1173                         &tegra_aic326x_hp_jack);
1174         if (ret < 0)
1175                 return ret;
1176
1177         if (gpio_is_valid(pdata->gpio_hp_det)) {
1178                 /* Headphone detection */
1179                 tegra_aic326x_hp_jack_gpio.gpio = pdata->gpio_hp_det;
1180                 snd_soc_jack_new(codec, "Headphone Jack",
1181                                 SND_JACK_HEADSET, &tegra_aic326x_hp_jack);
1182
1183 #ifndef CONFIG_SWITCH
1184         snd_soc_jack_add_pins(&tegra_aic326x_hp_jack,
1185                 ARRAY_SIZE(tegra_aic326x_hp_jack_pins),
1186                 tegra_aic326x_hp_jack_pins);
1187 #else
1188                 snd_soc_jack_notifier_register(&tegra_aic326x_hp_jack,
1189                                         &aic326x_headset_switch_nb);
1190 #endif
1191                 snd_soc_jack_add_gpios(&tegra_aic326x_hp_jack,
1192                                         1,
1193                                         &tegra_aic326x_hp_jack_gpio);
1194
1195                 machine->gpio_requested |= GPIO_HP_DET;
1196         }
1197
1198 #ifndef CONFIG_ARCH_TEGRA_11x_SOC
1199         /* update jack status during boot */
1200         aic3262_hs_jack_detect(codec, &tegra_aic326x_hp_jack,
1201                 SND_JACK_HEADSET);
1202 #endif
1203
1204         /* Add call mode switch control */
1205         ret = snd_ctl_add(codec->card->snd_card,
1206                         snd_ctl_new1(&tegra_aic326x_call_mode_control,
1207                                 machine));
1208         if (ret < 0)
1209                 return ret;
1210
1211         ret = tegra_asoc_utils_register_ctls(&machine->util_data);
1212         if (ret < 0)
1213                 return ret;
1214
1215         return 0;
1216 }
1217
1218 static struct snd_soc_dai_link tegra_aic326x_dai[] = {
1219         [DAI_LINK_HIFI] = {
1220                 .name = "AIC3262",
1221                 .stream_name = "AIC3262 PCM HIFI",
1222                 .codec_name = "tlv320aic3262-codec",
1223                 .platform_name = "tegra-pcm-audio",
1224 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1225                 .cpu_dai_name = "tegra20-i2s.0",
1226 #else
1227                 .cpu_dai_name = "tegra30-i2s.1",
1228 #endif
1229                 .codec_dai_name = "aic326x-asi1",
1230                 .init = tegra_aic326x_init,
1231                 .ops = &tegra_aic326x_hifi_ops,
1232                 },
1233         [DAI_LINK_SPDIF] = {
1234                 .name = "SPDIF",
1235                 .stream_name = "SPDIF PCM",
1236                 .codec_name = "spdif-dit.0",
1237                 .platform_name = "tegra-pcm-audio",
1238 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1239                 .cpu_dai_name = "tegra20-spdif",
1240 #else
1241                 .cpu_dai_name = "tegra30-spdif",
1242 #endif
1243                 .codec_dai_name = "dit-hifi",
1244                 .ops = &tegra_aic326x_spdif_ops,
1245                 },
1246         [DAI_LINK_BTSCO] = {
1247                 .name = "BT-SCO",
1248                 .stream_name = "BT SCO PCM",
1249                 .codec_name = "spdif-dit.1",
1250                 .platform_name = "tegra-pcm-audio",
1251 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1252                 .cpu_dai_name = "tegra20-i2s.1",
1253 #else
1254                 .cpu_dai_name = "tegra30-i2s.3",
1255 #endif
1256                 .codec_dai_name = "dit-hifi",
1257                 .init = tegra_aic326x_init,
1258                 .ops = &tegra_aic326x_bt_ops,
1259                 },
1260         [DAI_LINK_VOICE_CALL] = {
1261                         .name = "VOICE CALL",
1262                         .stream_name = "VOICE CALL PCM",
1263                         .codec_name = "tlv320aic3262-codec",
1264                         .platform_name = "tegra-pcm-audio",
1265                         .cpu_dai_name = "dit-hifi",
1266                         .codec_dai_name = "aic326x-asi3",
1267                         .ops = &tegra_aic326x_voice_call_ops,
1268                 },
1269         [DAI_LINK_BT_VOICE_CALL] = {
1270                         .name = "BT VOICE CALL",
1271                         .stream_name = "BT VOICE CALL PCM",
1272                         .codec_name = "spdif-dit.2",
1273                         .platform_name = "tegra-pcm-audio",
1274                         .cpu_dai_name = "dit-hifi",
1275                         .codec_dai_name = "dit-hifi",
1276                         .ops = &tegra_aic326x_bt_voice_call_ops,
1277                 },
1278 };
1279
1280 static int tegra_aic326x_suspend_post(struct snd_soc_card *card)
1281 {
1282         struct snd_soc_jack_gpio *gpio = &tegra_aic326x_hp_jack_gpio;
1283         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1284
1285         if (gpio_is_valid(gpio->gpio))
1286                 disable_irq(gpio_to_irq(gpio->gpio));
1287
1288         if (machine->clock_enabled) {
1289                 machine->clock_enabled = 0;
1290                 tegra_asoc_utils_clk_disable(&machine->util_data);
1291         }
1292
1293
1294         return 0;
1295 }
1296
1297 static int tegra_aic326x_resume_pre(struct snd_soc_card *card)
1298 {
1299         int val;
1300         struct snd_soc_jack_gpio *gpio = &tegra_aic326x_hp_jack_gpio;
1301         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1302
1303         if (gpio_is_valid(gpio->gpio)) {
1304                 val = gpio_get_value(gpio->gpio);
1305                 val = gpio->invert ? !val : val;
1306                 snd_soc_jack_report(gpio->jack, val, gpio->report);
1307                 enable_irq(gpio_to_irq(gpio->gpio));
1308         }
1309
1310         if (!machine->clock_enabled) {
1311                 machine->clock_enabled = 1;
1312                 tegra_asoc_utils_clk_enable(&machine->util_data);
1313         }
1314
1315         return 0;
1316 }
1317
1318
1319 static int tegra_aic326x_set_bias_level(struct snd_soc_card *card,
1320         struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
1321 {
1322         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1323
1324         if (machine->bias_level == SND_SOC_BIAS_OFF &&
1325                 level != SND_SOC_BIAS_OFF && (!machine->clock_enabled)) {
1326                 machine->clock_enabled = 1;
1327                 tegra_asoc_utils_clk_enable(&machine->util_data);
1328                 machine->bias_level = level;
1329         }
1330
1331         return 0;
1332 }
1333
1334 static int tegra_aic326x_set_bias_level_post(struct snd_soc_card *card,
1335         struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
1336 {
1337         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1338
1339         if (machine->bias_level != SND_SOC_BIAS_OFF &&
1340                 level == SND_SOC_BIAS_OFF && machine->clock_enabled) {
1341                 machine->clock_enabled = 0;
1342                 tegra_asoc_utils_clk_disable(&machine->util_data);
1343         }
1344
1345         machine->bias_level = level;
1346
1347         return 0 ;
1348 }
1349
1350
1351
1352 static struct snd_soc_card snd_soc_tegra_aic326x = {
1353         .name = "tegra-aic326x",
1354         .owner = THIS_MODULE,
1355         .dai_link = tegra_aic326x_dai,
1356         .num_links = ARRAY_SIZE(tegra_aic326x_dai),
1357         .set_bias_level = tegra_aic326x_set_bias_level,
1358         .set_bias_level_post = tegra_aic326x_set_bias_level_post,
1359         .suspend_post = tegra_aic326x_suspend_post,
1360         .resume_pre = tegra_aic326x_resume_pre,
1361 };
1362
1363 static __devinit int tegra_aic326x_driver_probe(struct platform_device *pdev)
1364 {
1365         struct snd_soc_card *card = &snd_soc_tegra_aic326x;
1366         struct tegra_aic326x *machine;
1367         struct tegra_asoc_platform_data *pdata;
1368         int ret;
1369 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1370         int i;
1371 #endif
1372         pdata = pdev->dev.platform_data;
1373         if (!pdata) {
1374                 dev_err(&pdev->dev, "No platform data supplied\n");
1375                 return -EINVAL;
1376         }
1377
1378         machine = kzalloc(sizeof(struct tegra_aic326x), GFP_KERNEL);
1379         if (!machine) {
1380                 dev_err(&pdev->dev, "Can't allocate tegra_aic326x struct\n");
1381                 return -ENOMEM;
1382         }
1383
1384         machine->pdata = pdata;
1385
1386         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev, card);
1387         if (ret)
1388                 goto err_free_machine;
1389
1390         machine->dmic_reg = regulator_get(&pdev->dev, "vdd_mic");
1391         if (IS_ERR(machine->dmic_reg)) {
1392                 dev_info(&pdev->dev, "No digital mic regulator found\n");
1393                 machine->dmic_reg = 0;
1394         }
1395
1396         machine->dmic_1v8_reg = regulator_get(&pdev->dev, "vdd_1v8_mic");
1397         if (IS_ERR(machine->dmic_1v8_reg)) {
1398                 dev_info(&pdev->dev, "No digital mic regulator found\n");
1399                 machine->dmic_1v8_reg = 0;
1400         }
1401
1402         machine->hmic_reg = regulator_get(&pdev->dev, "mic_ventral");
1403         if (IS_ERR(machine->hmic_reg)) {
1404                 dev_info(&pdev->dev, "No headset mic regulator found\n");
1405                 machine->hmic_reg = 0;
1406         }
1407
1408         card->dev = &pdev->dev;
1409         platform_set_drvdata(pdev, card);
1410         snd_soc_card_set_drvdata(card, machine);
1411
1412 #ifdef CONFIG_SWITCH
1413         /* Add h2w switch class support */
1414         ret = tegra_asoc_switch_register(&aic326x_wired_switch_dev);
1415         if (ret < 0) {
1416                 dev_err(&pdev->dev, "not able to register switch device %d\n",
1417                         ret);
1418                 goto err_fini_utils;
1419         }
1420 #endif
1421
1422 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1423         for (i = 0; i < NUM_I2S_DEVICES ; i++) {
1424                 machine->codec_info[i].i2s_id =
1425                         pdata->i2s_param[i].audio_port_id;
1426                 machine->codec_info[i].bitsize =
1427                         pdata->i2s_param[i].sample_size;
1428                 machine->codec_info[i].is_i2smaster =
1429                         pdata->i2s_param[i].is_i2s_master;
1430                 machine->codec_info[i].rate =
1431                         pdata->i2s_param[i].rate;
1432                 machine->codec_info[i].channels =
1433                         pdata->i2s_param[i].channels;
1434                 machine->codec_info[i].i2s_mode =
1435                         pdata->i2s_param[i].i2s_mode;
1436                 machine->codec_info[i].bit_clk =
1437                         pdata->i2s_param[i].bit_clk;
1438
1439         }
1440
1441         tegra_aic326x_dai[DAI_LINK_HIFI].cpu_dai_name =
1442         tegra_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1443
1444         tegra_aic326x_dai[DAI_LINK_BTSCO].cpu_dai_name =
1445         tegra_i2s_dai_name[machine->codec_info[BT_SCO].i2s_id];
1446 #endif
1447
1448         if (machine_is_tegra_enterprise()) {
1449                 tegra_aic326x_dai[DAI_LINK_HIFI].codec_name =
1450                                                 "tlv320aic3262-codec";
1451                 tegra_aic326x_dai[DAI_LINK_VOICE_CALL].codec_name =
1452                                                 "tlv320aic3262-codec";
1453                 tegra_aic326x_dai[DAI_LINK_VOICE_CALL].codec_dai_name =
1454                                                 "aic326x-asi1";
1455         }
1456
1457         ret = snd_soc_register_card(card);
1458         if (ret) {
1459                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
1460                         ret);
1461                 goto err_switch_unregister;
1462         }
1463
1464         if (!card->instantiated) {
1465                 dev_err(&pdev->dev, "No TI AIC3262 codec\n");
1466                 goto err_unregister_card;
1467         }
1468
1469 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1470         ret = tegra_asoc_utils_set_parent(&machine->util_data,
1471                                 pdata->i2s_param[HIFI_CODEC].is_i2s_master);
1472         if (ret) {
1473                 dev_err(&pdev->dev, "tegra_asoc_utils_set_parent failed (%d)\n",
1474                         ret);
1475                 goto err_unregister_card;
1476         }
1477 #endif
1478
1479         return 0;
1480
1481 err_unregister_card:
1482         snd_soc_unregister_card(card);
1483 err_switch_unregister:
1484 #ifdef CONFIG_SWITCH
1485         tegra_asoc_switch_unregister(&aic326x_wired_switch_dev);
1486 #endif
1487 err_fini_utils:
1488         tegra_asoc_utils_fini(&machine->util_data);
1489 err_free_machine:
1490         kfree(machine);
1491         return ret;
1492 }
1493
1494 static int __devexit tegra_aic326x_driver_remove(struct platform_device *pdev)
1495 {
1496         struct snd_soc_card *card = platform_get_drvdata(pdev);
1497         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1498         struct tegra_asoc_platform_data *pdata = machine->pdata;
1499
1500         if (machine->gpio_requested & GPIO_HP_DET)
1501                 snd_soc_jack_free_gpios(&tegra_aic326x_hp_jack,
1502                                         1,
1503                                         &tegra_aic326x_hp_jack_gpio);
1504
1505         snd_soc_unregister_card(card);
1506
1507 #ifdef CONFIG_SWITCH
1508         tegra_asoc_switch_unregister(&aic326x_wired_switch_dev);
1509 #endif
1510
1511         tegra_asoc_utils_fini(&machine->util_data);
1512
1513         if (machine->gpio_requested & GPIO_EXT_MIC_EN)
1514                 gpio_free(pdata->gpio_ext_mic_en);
1515         if (machine->gpio_requested & GPIO_INT_MIC_EN)
1516                 gpio_free(pdata->gpio_int_mic_en);
1517         if (machine->gpio_requested & GPIO_HP_MUTE)
1518                 gpio_free(pdata->gpio_hp_mute);
1519         if (machine->gpio_requested & GPIO_SPKR_EN)
1520                 gpio_free(pdata->gpio_spkr_en);
1521
1522         kfree(machine);
1523
1524         return 0;
1525 }
1526
1527 static struct platform_driver tegra_aic326x_driver = {
1528         .driver = {
1529                 .name = DRV_NAME,
1530                 .owner = THIS_MODULE,
1531                 .pm = &snd_soc_pm_ops,
1532         },
1533         .probe = tegra_aic326x_driver_probe,
1534         .remove = __devexit_p(tegra_aic326x_driver_remove),
1535 };
1536
1537 static int __init tegra_aic326x_modinit(void)
1538 {
1539         return platform_driver_register(&tegra_aic326x_driver);
1540 }
1541 module_init(tegra_aic326x_modinit);
1542
1543 static void __exit tegra_aic326x_modexit(void)
1544 {
1545         platform_driver_unregister(&tegra_aic326x_driver);
1546 }
1547 module_exit(tegra_aic326x_modexit);
1548
1549 /* Module information */
1550 MODULE_AUTHOR("Vinod G. <vinodg@nvidia.com>");
1551 MODULE_DESCRIPTION("Tegra+AIC3262 machine ASoC driver");
1552 MODULE_DESCRIPTION("Tegra ALSA SoC");
1553 MODULE_LICENSE("GPL");
1554