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