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