arm: tegra: Remove reduntant headers for platform data
[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
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]);
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]);
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_sysclk(codec_dai, 0, rate, SND_SOC_CLOCK_IN);
347         if (err < 0) {
348                 dev_err(card->dev, "codec_dai clock not set\n");
349                 return err;
350         }
351
352         err = snd_soc_dai_set_pll(codec_dai, 0, AIC3262_PLL_CLKIN_MCLK1 , rate,
353                 params_rate(params));
354
355 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
356         err = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAP_SEL_DAC1,
357                                         TEGRA20_DAS_DAP_ID_1);
358         if (err < 0) {
359                 dev_err(card->dev, "failed to set dap-dac path\n");
360                 return err;
361         }
362
363         err = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1,
364                                         TEGRA20_DAS_DAP_SEL_DAC1);
365         if (err < 0) {
366                 dev_err(card->dev, "failed to set dac-dap path\n");
367                 return err;
368         }
369 #else /*assumes tegra3*/
370         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK  && i2s->is_dam_used)
371                 tegra_aic326x_set_dam_cif(i2s->dam_ifc, srate,
372                         params_channels(params), sample_size, 0, 0, 0, 0);
373 #endif
374
375         return 0;
376 }
377
378 static int tegra_aic326x_spdif_hw_params(struct snd_pcm_substream *substream,
379                                         struct snd_pcm_hw_params *params)
380 {
381         struct snd_soc_pcm_runtime *rtd = substream->private_data;
382         struct snd_soc_card *card = rtd->card;
383         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
384         int srate, mclk, min_mclk;
385         int err;
386
387         srate = params_rate(params);
388
389         mclk = tegra_aic326x_get_mclk(srate);
390         if (mclk < 0)
391                 return mclk;
392
393         min_mclk = 128 * srate;
394
395         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
396         if (err < 0) {
397                 if (!(machine->util_data.set_mclk % min_mclk))
398                         mclk = machine->util_data.set_mclk;
399                 else {
400                         dev_err(card->dev, "Can't configure clocks\n");
401                         return err;
402                 }
403         }
404
405         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
406
407         return 0;
408 }
409
410 static int tegra_aic326x_bt_hw_params(struct snd_pcm_substream *substream,
411                 struct snd_pcm_hw_params *params)
412 {
413         struct snd_soc_pcm_runtime *rtd = substream->private_data;
414         struct snd_soc_card *card = rtd->card;
415         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
416         struct tegra_asoc_platform_data *pdata = machine->pdata;
417         int err, srate, mclk, min_mclk, sample_size, i2s_daifmt;
418 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
419         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
420 #endif
421
422         switch (params_format(params)) {
423         case SNDRV_PCM_FORMAT_S16_LE:
424                 sample_size = 16;
425                 break;
426         default:
427                 return -EINVAL;
428         }
429
430         srate = params_rate(params);
431
432         mclk = tegra_aic326x_get_mclk(srate);
433         if (mclk < 0)
434                 return mclk;
435
436         min_mclk = 64 * srate;
437
438         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
439         if (err < 0) {
440                 if (!(machine->util_data.set_mclk % min_mclk))
441                         mclk = machine->util_data.set_mclk;
442                 else {
443                         dev_err(card->dev, "Can't configure clocks\n");
444                         return err;
445                 }
446         }
447
448         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
449
450         i2s_daifmt = SND_SOC_DAIFMT_NB_NF;
451         i2s_daifmt |= pdata->i2s_param[BT_SCO].is_i2s_master ?
452                         SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
453
454         switch (pdata->i2s_param[BT_SCO].i2s_mode) {
455                 case TEGRA_DAIFMT_I2S :
456                         i2s_daifmt |= SND_SOC_DAIFMT_I2S;
457                         break;
458                 case TEGRA_DAIFMT_DSP_A :
459                         i2s_daifmt |= SND_SOC_DAIFMT_DSP_A;
460                         break;
461                 case TEGRA_DAIFMT_DSP_B :
462                         i2s_daifmt |= SND_SOC_DAIFMT_DSP_B;
463                         break;
464                 case TEGRA_DAIFMT_LEFT_J :
465                         i2s_daifmt |= SND_SOC_DAIFMT_LEFT_J;
466                         break;
467                 case TEGRA_DAIFMT_RIGHT_J :
468                         i2s_daifmt |= SND_SOC_DAIFMT_RIGHT_J;
469                         break;
470                 default :
471                         dev_err(card->dev, "Can't configure i2s format\n");
472                         return -EINVAL;
473         }
474
475         err = snd_soc_dai_set_fmt(rtd->cpu_dai, i2s_daifmt);
476
477         if (err < 0) {
478                 dev_err(rtd->codec->card->dev, "cpu_dai fmt not set\n");
479                 return err;
480         }
481
482 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
483         err = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAP_SEL_DAC2,
484                                         TEGRA20_DAS_DAP_ID_4);
485         if (err < 0) {
486                 dev_err(card->dev, "failed to set dac-dap path\n");
487                 return err;
488         }
489
490         err = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_4,
491                                         TEGRA20_DAS_DAP_SEL_DAC2);
492         if (err < 0) {
493                 dev_err(card->dev, "failed to set dac-dap path\n");
494                 return err;
495         }
496 #else
497         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && i2s->is_dam_used)
498                 tegra_aic326x_set_dam_cif(i2s->dam_ifc, params_rate(params),
499                         params_channels(params), sample_size, 0, 0, 0, 0);
500 #endif
501
502         return 0;
503 }
504
505 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
506 static int tegra_aic326x_startup(struct snd_pcm_substream *substream)
507 {
508         struct snd_soc_pcm_runtime *rtd = substream->private_data;
509         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
510         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
511         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(rtd->card);
512         struct codec_config *codec_info;
513         struct codec_config *bb_info;
514         int codec_index;
515
516         if (!i2s->is_dam_used)
517                 return 0;
518
519         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
520                 /*dam configuration*/
521                 if (!i2s->dam_ch_refcount)
522                         i2s->dam_ifc = tegra30_dam_allocate_controller();
523                 if (i2s->dam_ifc < 0)
524                         return i2s->dam_ifc;
525                 tegra30_dam_allocate_channel(i2s->dam_ifc, TEGRA30_DAM_CHIN1);
526                 i2s->dam_ch_refcount++;
527                 tegra30_dam_enable_clock(i2s->dam_ifc);
528
529                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
530                                 (i2s->dam_ifc*2), i2s->txcif);
531
532                 /*
533                 *make the dam tx to i2s rx connection if this is the only client
534                 *using i2s for playback
535                 */
536                 if (i2s->playback_ref_count == 1)
537                         tegra30_ahub_set_rx_cif_source(
538                                 TEGRA30_AHUB_RXCIF_I2S0_RX0 + i2s->id,
539                                 TEGRA30_AHUB_TXCIF_DAM0_TX0 + i2s->dam_ifc);
540
541                 /* enable the dam*/
542                 tegra30_dam_enable(i2s->dam_ifc, TEGRA30_DAM_ENABLE,
543                                 TEGRA30_DAM_CHIN1);
544         } else {
545
546                 i2s->is_call_mode_rec = machine->is_call_mode;
547
548                 if (!i2s->is_call_mode_rec)
549                         return 0;
550
551                 if (machine->is_device_bt)
552                         codec_index = BT_SCO;
553                 else
554                         codec_index = VOICE_CODEC;
555
556                 codec_info = &machine->codec_info[codec_index];
557                 bb_info = &machine->codec_info[BASEBAND];
558
559                 /* allocate a dam for voice call recording */
560
561                 i2s->call_record_dam_ifc = tegra30_dam_allocate_controller();
562
563                 if (i2s->call_record_dam_ifc < 0)
564                         return i2s->call_record_dam_ifc;
565
566                 tegra30_dam_allocate_channel(i2s->call_record_dam_ifc,
567                         TEGRA30_DAM_CHIN0_SRC);
568                 tegra30_dam_allocate_channel(i2s->call_record_dam_ifc,
569                         TEGRA30_DAM_CHIN1);
570                 tegra30_dam_enable_clock(i2s->call_record_dam_ifc);
571
572                 /* configure the dam */
573                 tegra_aic326x_set_dam_cif(i2s->call_record_dam_ifc,
574                         codec_info->rate, codec_info->channels,
575                         codec_info->bitsize, 1, bb_info->rate,
576                         bb_info->channels, bb_info->bitsize);
577
578                 /* setup the connections for voice call record */
579
580                 tegra30_ahub_unset_rx_cif_source(i2s->rxcif);
581                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
582                         (i2s->call_record_dam_ifc*2),
583                         TEGRA30_AHUB_TXCIF_I2S0_TX0 + bb_info->i2s_id);
584                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
585                         (i2s->call_record_dam_ifc*2),
586                         TEGRA30_AHUB_TXCIF_I2S0_TX0 + codec_info->i2s_id);
587                 tegra30_ahub_set_rx_cif_source(i2s->rxcif,
588                         TEGRA30_AHUB_TXCIF_DAM0_TX0 + i2s->call_record_dam_ifc);
589
590                 /* enable the dam*/
591
592                 tegra30_dam_enable(i2s->call_record_dam_ifc, TEGRA30_DAM_ENABLE,
593                                 TEGRA30_DAM_CHIN1);
594                 tegra30_dam_enable(i2s->call_record_dam_ifc, TEGRA30_DAM_ENABLE,
595                                 TEGRA30_DAM_CHIN0_SRC);
596         }
597
598         return 0;
599 }
600
601 static void tegra_aic326x_shutdown(struct snd_pcm_substream *substream)
602 {
603         struct snd_soc_pcm_runtime *rtd = substream->private_data;
604         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
605         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
606
607         if (!i2s->is_dam_used)
608                 return;
609
610         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
611                 /* disable the dam*/
612                 tegra30_dam_enable(i2s->dam_ifc, TEGRA30_DAM_DISABLE,
613                                 TEGRA30_DAM_CHIN1);
614
615                 /* disconnect the ahub connections*/
616                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
617                                         (i2s->dam_ifc*2));
618
619                 /* disable the dam and free the controller */
620                 tegra30_dam_disable_clock(i2s->dam_ifc);
621                 tegra30_dam_free_channel(i2s->dam_ifc, TEGRA30_DAM_CHIN1);
622                 i2s->dam_ch_refcount--;
623                 if (!i2s->dam_ch_refcount)
624                         tegra30_dam_free_controller(i2s->dam_ifc);
625          } else {
626
627                 if (!i2s->is_call_mode_rec)
628                         return;
629
630                 i2s->is_call_mode_rec = 0;
631
632                 /* disable the dam*/
633                 tegra30_dam_enable(i2s->call_record_dam_ifc,
634                         TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN1);
635                 tegra30_dam_enable(i2s->call_record_dam_ifc,
636                         TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN0_SRC);
637
638                 /* disconnect the ahub connections*/
639                 tegra30_ahub_unset_rx_cif_source(i2s->rxcif);
640                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
641                         (i2s->call_record_dam_ifc*2));
642                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
643                         (i2s->call_record_dam_ifc*2));
644
645                 /* free the dam channels and dam controller */
646                 tegra30_dam_disable_clock(i2s->call_record_dam_ifc);
647                 tegra30_dam_free_channel(i2s->call_record_dam_ifc,
648                         TEGRA30_DAM_CHIN1);
649                 tegra30_dam_free_channel(i2s->call_record_dam_ifc,
650                         TEGRA30_DAM_CHIN0_SRC);
651                 tegra30_dam_free_controller(i2s->call_record_dam_ifc);
652          }
653
654         return;
655 }
656 #endif
657
658
659 static int tegra_aic326x_hw_free(struct snd_pcm_substream *substream)
660 {
661         struct snd_soc_pcm_runtime *rtd = substream->private_data;
662         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(rtd->card);
663
664         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 0);
665
666         return 0;
667 }
668
669 static int tegra_aic326x_voice_call_hw_params(
670                         struct snd_pcm_substream *substream,
671                         struct snd_pcm_hw_params *params)
672 {
673         struct snd_soc_pcm_runtime *rtd = substream->private_data;
674         struct snd_soc_dai *codec_dai = rtd->codec_dai;
675         struct snd_soc_codec *codec = rtd->codec;
676         struct snd_soc_card *card = codec->card;
677         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
678         struct tegra_asoc_platform_data *pdata = machine->pdata;
679         int srate, mclk, rate, i2s_daifmt;
680         int err, pcmdiv, vxclkdiv;
681
682         srate = params_rate(params);
683         mclk = tegra_aic326x_get_mclk(srate);
684         if (mclk < 0)
685                 return mclk;
686
687         i2s_daifmt = SND_SOC_DAIFMT_NB_NF;
688
689         i2s_daifmt |= pdata->i2s_param[VOICE_CODEC].is_i2s_master ?
690                 SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM;
691
692         switch (pdata->i2s_param[VOICE_CODEC].i2s_mode) {
693         case TEGRA_DAIFMT_I2S:
694                 i2s_daifmt |= SND_SOC_DAIFMT_I2S;
695                 break;
696         case TEGRA_DAIFMT_DSP_A:
697                 i2s_daifmt |= SND_SOC_DAIFMT_DSP_A;
698                 break;
699         case TEGRA_DAIFMT_DSP_B:
700                 i2s_daifmt |= SND_SOC_DAIFMT_DSP_B;
701                 break;
702         case TEGRA_DAIFMT_LEFT_J:
703                 i2s_daifmt |= SND_SOC_DAIFMT_LEFT_J;
704                 break;
705         case TEGRA_DAIFMT_RIGHT_J:
706                 i2s_daifmt |= SND_SOC_DAIFMT_RIGHT_J;
707                 break;
708         default:
709                 dev_err(card->dev,
710                 "Can't configure i2s format\n");
711                 return -EINVAL;
712         }
713
714         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
715         if (err < 0) {
716                 if (!(machine->util_data.set_mclk % mclk))
717                         mclk = machine->util_data.set_mclk;
718                 else {
719                         dev_err(card->dev, "Can't configure clocks\n");
720                         return err;
721                 }
722         }
723
724         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
725
726         rate = clk_get_rate(machine->util_data.clk_cdev1);
727
728         err = snd_soc_dai_set_fmt(codec_dai, i2s_daifmt);
729         if (err < 0) {
730                 dev_err(card->dev, "codec_dai fmt not set\n");
731                 return err;
732         }
733
734         err = snd_soc_dai_set_sysclk(codec_dai, 0, rate, SND_SOC_CLOCK_IN);
735         if (err < 0) {
736                 dev_err(card->dev, "codec_dai clock not set\n");
737                 return err;
738         }
739
740         err = snd_soc_dai_set_pll(codec_dai, 0, AIC3262_PLL_CLKIN_MCLK1 , rate,
741                         params_rate(params));
742
743         if (err < 0) {
744                 dev_err(card->dev, "codec_dai PLL clock not set\n");
745                 return err;
746         }
747
748         if (!machine_is_tegra_enterprise()) {
749                 if (params_rate(params) == 8000) {
750                         /* Change these Settings for 8KHz*/
751                         pcmdiv = 1;
752                         /* BB expecting 2048Khz bclk */
753                         vxclkdiv = 27;
754                 } else if (params_rate(params) == 16000) {
755                         pcmdiv = 1;
756                         /* BB expecting 2048Khz bclk */
757                         vxclkdiv = 27;
758                 } else {
759                         dev_err(card->dev, "codec_dai unsupported voice rate\n");
760                         return -EINVAL;
761                 }
762         }
763
764         //snd_soc_dai_set_clkdiv(codec_dai, ASI2_BCLK_N, vxclkdiv);
765         //snd_soc_dai_set_clkdiv(codec_dai, ASI2_WCLK_N, pcmdiv);
766
767 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
768         /* codec configuration */
769         machine->codec_info[VOICE_CODEC].rate = params_rate(params);
770         machine->codec_info[VOICE_CODEC].channels = params_channels(params);
771 #endif
772
773         machine->is_device_bt = 0;
774
775         return 0;
776 }
777
778 static void tegra_aic326x_voice_call_shutdown(
779                                         struct snd_pcm_substream *substream)
780 {
781         struct snd_soc_pcm_runtime *rtd = substream->private_data;
782         struct tegra_aic326x *machine  =
783                         snd_soc_card_get_drvdata(rtd->codec->card);
784
785 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
786         machine->codec_info[VOICE_CODEC].rate = 0;
787         machine->codec_info[VOICE_CODEC].channels = 0;
788 #endif
789
790         machine->is_device_bt = 0;
791 }
792
793 static int tegra_aic326x_bt_voice_call_hw_params(
794                         struct snd_pcm_substream *substream,
795                         struct snd_pcm_hw_params *params)
796 {
797         struct snd_soc_pcm_runtime *rtd = substream->private_data;
798         struct snd_soc_card *card = rtd->card;
799         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
800         int err, srate, mclk, min_mclk;
801
802         srate = params_rate(params);
803
804         mclk = tegra_aic326x_get_mclk(srate);
805         if (mclk < 0)
806                 return mclk;
807
808         min_mclk = 64 * srate;
809
810         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
811         if (err < 0) {
812                 if (!(machine->util_data.set_mclk % min_mclk))
813                         mclk = machine->util_data.set_mclk;
814                 else {
815                         dev_err(card->dev, "Can't configure clocks\n");
816                         return err;
817                 }
818         }
819
820         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
821
822 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
823         /* codec configuration */
824         machine->codec_info[BT_SCO].rate = params_rate(params);
825         machine->codec_info[BT_SCO].channels = params_channels(params);
826 #endif
827
828         machine->is_device_bt = 1;
829
830         return 0;
831 }
832
833 static void tegra_aic326x_bt_voice_call_shutdown(
834                                 struct snd_pcm_substream *substream)
835 {
836         struct snd_soc_pcm_runtime *rtd = substream->private_data;
837         struct tegra_aic326x *machine  =
838                         snd_soc_card_get_drvdata(rtd->codec->card);
839
840 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
841         machine->codec_info[BT_SCO].rate = 0;
842         machine->codec_info[BT_SCO].channels = 0;
843 #endif
844
845         machine->is_device_bt = 0;
846 }
847
848 static struct snd_soc_ops tegra_aic326x_hifi_ops = {
849         .hw_params = tegra_aic326x_hw_params,
850         .hw_free = tegra_aic326x_hw_free,
851 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
852         .startup = tegra_aic326x_startup,
853         .shutdown = tegra_aic326x_shutdown,
854 #endif
855 };
856
857 static struct snd_soc_ops tegra_aic326x_spdif_ops = {
858         .hw_params = tegra_aic326x_spdif_hw_params,
859         .hw_free = tegra_aic326x_hw_free,
860 };
861
862 static struct snd_soc_ops tegra_aic326x_voice_call_ops = {
863         .hw_params = tegra_aic326x_voice_call_hw_params,
864         .shutdown = tegra_aic326x_voice_call_shutdown,
865         .hw_free = tegra_aic326x_hw_free,
866 };
867
868 static struct snd_soc_ops tegra_aic326x_bt_voice_call_ops = {
869         .hw_params = tegra_aic326x_bt_voice_call_hw_params,
870         .shutdown = tegra_aic326x_bt_voice_call_shutdown,
871         .hw_free = tegra_aic326x_hw_free,
872 };
873
874 static struct snd_soc_ops tegra_aic326x_bt_ops = {
875         .hw_params = tegra_aic326x_bt_hw_params,
876         .hw_free = tegra_aic326x_hw_free,
877 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
878         .startup = tegra_aic326x_startup,
879         .shutdown = tegra_aic326x_shutdown,
880 #endif
881 };
882
883 static struct snd_soc_jack tegra_aic326x_hp_jack;
884
885 #ifdef CONFIG_SWITCH
886 static struct switch_dev aic326x_wired_switch_dev = {
887         .name = "h2w",
888 };
889
890 /* These values are copied from WiredAccessoryObserver */
891 enum headset_state {
892         BIT_NO_HEADSET = 0,
893         BIT_HEADSET = (1 << 0),
894         BIT_HEADSET_NO_MIC = (1 << 1),
895 };
896
897 static int aic326x_headset_switch_notify(struct notifier_block *self,
898         unsigned long action, void *dev)
899 {
900         int state = BIT_NO_HEADSET;
901
902         switch (action) {
903         case SND_JACK_HEADPHONE:
904                 state |= BIT_HEADSET_NO_MIC;
905                 break;
906         case SND_JACK_HEADSET:
907                 state |= BIT_HEADSET;
908                 break;
909         default:
910                 state |= BIT_NO_HEADSET;
911         }
912
913         switch_set_state(&aic326x_wired_switch_dev, state);
914
915         return NOTIFY_OK;
916 }
917
918 static struct notifier_block aic326x_headset_switch_nb = {
919         .notifier_call = aic326x_headset_switch_notify,
920 };
921 #else
922 static struct snd_soc_jack_pin tegra_aic326x_hp_jack_pins[] = {
923         {
924                 .pin = "Headphone Jack",
925                 .mask = SND_JACK_HEADPHONE,
926         },
927 };
928 #endif
929
930 static int tegra_aic326x_event_int_spk(struct snd_soc_dapm_widget *w,
931                                         struct snd_kcontrol *k, int event)
932 {
933         struct snd_soc_dapm_context *dapm = w->dapm;
934         struct snd_soc_card *card = dapm->card;
935         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
936         struct tegra_asoc_platform_data *pdata = machine->pdata;
937
938         if (!(machine->gpio_requested & GPIO_SPKR_EN))
939                 return 0;
940
941         gpio_set_value_cansleep(pdata->gpio_spkr_en,
942                                 SND_SOC_DAPM_EVENT_ON(event));
943
944         return 0;
945 }
946
947 static int tegra_aic326x_event_hp(struct snd_soc_dapm_widget *w,
948                                         struct snd_kcontrol *k, int event)
949 {
950         struct snd_soc_dapm_context *dapm = w->dapm;
951         struct snd_soc_card *card = dapm->card;
952         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
953         struct tegra_asoc_platform_data *pdata = machine->pdata;
954
955         if (!(machine->gpio_requested & GPIO_HP_MUTE))
956                 return 0;
957
958         gpio_set_value_cansleep(pdata->gpio_hp_mute,
959                                 !SND_SOC_DAPM_EVENT_ON(event));
960
961         return 0;
962 }
963
964 static const struct snd_soc_dapm_widget tegra_aic326x_dapm_widgets[] = {
965         SND_SOC_DAPM_SPK("Int Spk", tegra_aic326x_event_int_spk),
966         SND_SOC_DAPM_HP("Earpiece", NULL),
967         SND_SOC_DAPM_HP("Headphone Jack", tegra_aic326x_event_hp),
968         SND_SOC_DAPM_MIC("Mic Jack", NULL),
969         SND_SOC_DAPM_INPUT("Ext Mic"),
970         SND_SOC_DAPM_LINE("Linein", NULL),
971         SND_SOC_DAPM_MIC("Int Mic", NULL),
972         SND_SOC_DAPM_MIC("DMIC", NULL),
973 };
974
975 static const struct snd_soc_dapm_route aic326x_audio_map[] = {
976         {"Int Spk", NULL, "SPKL"},
977         {"Int Spk", NULL, "SPKR"},
978         {"Earpiece", NULL, "RECP"},
979         {"Earpiece", NULL, "RECM"},
980         {"Headphone Jack", NULL, "HPL"},
981         {"Headphone Jack", NULL, "HPR"},
982         /* internal (IN2L/IN2R) mic is stero */
983         {"Mic Bias Int" ,NULL, "Int Mic"},
984         {"IN2L", NULL, "Mic Bias Int"},
985         {"Mic Bias Int" ,NULL, "Int Mic"},
986         {"IN2R", NULL, "Mic Bias Int"},
987         {"IN1L", NULL, "Mic Bias Ext"},
988         {"Mic Bias Ext" ,NULL, "Mic Jack"},
989         /* Connect LDMIC and RDMIC to DMIC widget*/
990         {"Left DMIC", NULL, "Mic Bias Int"},
991         {"Right DMIC", NULL, "Mic Bias Int"},
992         {"Mic Bias Int", NULL, "Int Mic"},
993 /*      {"CM1L" ,NULL, "Mic Jack"}, */
994 /*      {"IN1L", NULL, "Mic Bias Ext"}, */
995 /*      {"IN1L", NULL, "CM1L"}, */
996 };
997
998 static const struct snd_kcontrol_new tegra_aic326x_controls[] = {
999         SOC_DAPM_PIN_SWITCH("Int Spk"),
1000         SOC_DAPM_PIN_SWITCH("Earpiece"),
1001         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
1002         SOC_DAPM_PIN_SWITCH("Mic Jack"),
1003         SOC_DAPM_PIN_SWITCH("Ext Mic"),
1004         SOC_DAPM_PIN_SWITCH("Linein"),
1005         SOC_DAPM_PIN_SWITCH("Int Mic"),
1006 };
1007
1008 static int tegra_aic326x_init(struct snd_soc_pcm_runtime *rtd)
1009 {
1010         struct snd_soc_codec *codec = rtd->codec;
1011         struct snd_soc_dapm_context *dapm = &codec->dapm;
1012         struct snd_soc_card *card = codec->card;
1013         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1014         struct tegra_asoc_platform_data *pdata = machine->pdata;
1015 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1016         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1017 #endif
1018         int ret;
1019
1020 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1021         if (machine->codec_info[BASEBAND].i2s_id != -1)
1022                 i2s->is_dam_used = true;
1023 #endif
1024
1025         if ((i2s->id == machine->codec_info[HIFI_CODEC].i2s_id) &&
1026                 (i2s->id != machine->codec_info[VOICE_CODEC].i2s_id))
1027                 i2s->is_dam_used = false;
1028
1029         if (machine->init_done)
1030                 return 0;
1031
1032         machine->init_done = true;
1033
1034 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1035         machine->pcard = card;
1036 #endif
1037
1038         if (machine_is_whistler()) {
1039                 machine->audio_reg = regulator_get(NULL, "avddio_audio");
1040                 if (IS_ERR(machine->audio_reg)) {
1041                         dev_err(card->dev, "cannot get avddio_audio reg\n");
1042                         ret = PTR_ERR(machine->audio_reg);
1043                         return ret;
1044                 }
1045
1046                 ret = regulator_enable(machine->audio_reg);
1047                 if (ret) {
1048                         dev_err(card->dev, "cannot enable avddio_audio reg\n");
1049                         regulator_put(machine->audio_reg);
1050                         machine->audio_reg = NULL;
1051                         return ret;
1052                 }
1053         }
1054
1055         if (gpio_is_valid(pdata->gpio_spkr_en)) {
1056                 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
1057                 if (ret) {
1058                         dev_err(card->dev, "cannot get spkr_en gpio\n");
1059                         return ret;
1060                 }
1061                 machine->gpio_requested |= GPIO_SPKR_EN;
1062
1063                 gpio_direction_output(pdata->gpio_spkr_en, 0);
1064         }
1065
1066         if (gpio_is_valid(pdata->gpio_hp_mute)) {
1067                 ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
1068                 if (ret) {
1069                         dev_err(card->dev, "cannot get hp_mute gpio\n");
1070                         return ret;
1071                 }
1072                 machine->gpio_requested |= GPIO_HP_MUTE;
1073
1074                 gpio_direction_output(pdata->gpio_hp_mute, 0);
1075         }
1076
1077         if (gpio_is_valid(pdata->gpio_int_mic_en)) {
1078                 ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
1079                 if (ret) {
1080                         dev_err(card->dev, "cannot get int_mic_en gpio\n");
1081                         return ret;
1082                 }
1083                 machine->gpio_requested |= GPIO_INT_MIC_EN;
1084
1085                 /* Disable int mic; enable signal is active-high */
1086                 gpio_direction_output(pdata->gpio_int_mic_en, 0);
1087         }
1088
1089         if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
1090                 ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
1091                 if (ret) {
1092                         dev_err(card->dev, "cannot get ext_mic_en gpio\n");
1093                         return ret;
1094                 }
1095                 machine->gpio_requested |= GPIO_EXT_MIC_EN;
1096
1097                 /* Enable ext mic; enable signal is active-low */
1098                 gpio_direction_output(pdata->gpio_ext_mic_en, 0);
1099         }
1100
1101         ret = snd_soc_add_card_controls(card, tegra_aic326x_controls,
1102                                    ARRAY_SIZE(tegra_aic326x_controls));
1103         if (ret < 0)
1104                 return ret;
1105
1106         snd_soc_dapm_new_controls(dapm, tegra_aic326x_dapm_widgets,
1107                                         ARRAY_SIZE(tegra_aic326x_dapm_widgets));
1108
1109         snd_soc_dapm_add_routes(dapm, aic326x_audio_map,
1110                                         ARRAY_SIZE(aic326x_audio_map));
1111
1112         ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
1113                         &tegra_aic326x_hp_jack);
1114         if (ret < 0)
1115                 return ret;
1116
1117 #ifdef CONFIG_SWITCH
1118         snd_soc_jack_notifier_register(&tegra_aic326x_hp_jack,
1119                 &aic326x_headset_switch_nb);
1120 #else /*gpio based headset detection*/
1121         snd_soc_jack_add_pins(&tegra_aic326x_hp_jack,
1122                 ARRAY_SIZE(tegra_aic326x_hp_jack_pins),
1123                 tegra_aic326x_hp_jack_pins);
1124 #endif
1125
1126         /* update jack status during boot */
1127         aic3262_hs_jack_detect(codec, &tegra_aic326x_hp_jack,
1128                 SND_JACK_HEADSET);
1129
1130         /* Add call mode switch control */
1131         ret = snd_ctl_add(codec->card->snd_card,
1132                         snd_ctl_new1(&tegra_aic326x_call_mode_control,
1133                                 machine));
1134         if (ret < 0)
1135                 return ret;
1136
1137         ret = tegra_asoc_utils_register_ctls(&machine->util_data);
1138         if (ret < 0)
1139                 return ret;
1140
1141 /*      snd_soc_dapm_force_enable_pin(dapm, "Mic Bias Ext");
1142         snd_soc_dapm_force_enable_pin(dapm,"Mic Bias Int");
1143         snd_soc_dapm_sync(dapm); */
1144
1145         return 0;
1146 }
1147
1148 static struct snd_soc_dai_link tegra_aic326x_dai[] = {
1149         [DAI_LINK_HIFI] = {
1150                 .name = "AIC3262",
1151                 .stream_name = "AIC3262 PCM HIFI",
1152                 .codec_name = "tlv320aic3262-codec",
1153                 .platform_name = "tegra-pcm-audio",
1154 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1155                 .cpu_dai_name = "tegra20-i2s.0",
1156 #else
1157                 .cpu_dai_name = "tegra30-i2s.0",
1158 #endif
1159                 .codec_dai_name = "aic326x-asi1",
1160                 .init = tegra_aic326x_init,
1161                 .ops = &tegra_aic326x_hifi_ops,
1162                 },
1163         [DAI_LINK_SPDIF] = {
1164                 .name = "SPDIF",
1165                 .stream_name = "SPDIF PCM",
1166                 .codec_name = "spdif-dit.0",
1167                 .platform_name = "tegra-pcm-audio",
1168 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1169                 .cpu_dai_name = "tegra20-spdif",
1170 #else
1171                 .cpu_dai_name = "tegra30-spdif",
1172 #endif
1173                 .codec_dai_name = "dit-hifi",
1174                 .ops = &tegra_aic326x_spdif_ops,
1175                 },
1176         [DAI_LINK_BTSCO] = {
1177                 .name = "BT-SCO",
1178                 .stream_name = "BT SCO PCM",
1179                 .codec_name = "spdif-dit.1",
1180                 .platform_name = "tegra-pcm-audio",
1181 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1182                 .cpu_dai_name = "tegra20-i2s.1",
1183 #else
1184                 .cpu_dai_name = "tegra30-i2s.3",
1185 #endif
1186                 .codec_dai_name = "dit-hifi",
1187                 .init = tegra_aic326x_init,
1188                 .ops = &tegra_aic326x_bt_ops,
1189                 },
1190         [DAI_LINK_VOICE_CALL] = {
1191                         .name = "VOICE CALL",
1192                         .stream_name = "VOICE CALL PCM",
1193                         .codec_name = "tlv320aic3262-codec",
1194                         .platform_name = "tegra-pcm-audio",
1195                         .cpu_dai_name = "dit-hifi",
1196                         .codec_dai_name = "aic326x-asi2",
1197                         .ops = &tegra_aic326x_voice_call_ops,
1198                 },
1199         [DAI_LINK_BT_VOICE_CALL] = {
1200                         .name = "BT VOICE CALL",
1201                         .stream_name = "BT VOICE CALL PCM",
1202                         .codec_name = "spdif-dit.2",
1203                         .platform_name = "tegra-pcm-audio",
1204                         .cpu_dai_name = "dit-hifi",
1205                         .codec_dai_name = "dit-hifi",
1206                         .ops = &tegra_aic326x_bt_voice_call_ops,
1207                 },
1208 };
1209
1210 static struct snd_soc_card snd_soc_tegra_aic326x = {
1211         .name = "tegra-aic326x",
1212         .owner = THIS_MODULE,
1213         .dai_link = tegra_aic326x_dai,
1214         .num_links = ARRAY_SIZE(tegra_aic326x_dai),
1215 };
1216
1217 static __devinit int tegra_aic326x_driver_probe(struct platform_device *pdev)
1218 {
1219         struct snd_soc_card *card = &snd_soc_tegra_aic326x;
1220         struct tegra_aic326x *machine;
1221         struct tegra_asoc_platform_data *pdata;
1222         int ret;
1223 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1224         int i;
1225 #endif
1226         pdata = pdev->dev.platform_data;
1227         if (!pdata) {
1228                 dev_err(&pdev->dev, "No platform data supplied\n");
1229                 return -EINVAL;
1230         }
1231
1232         machine = kzalloc(sizeof(struct tegra_aic326x), GFP_KERNEL);
1233         if (!machine) {
1234                 dev_err(&pdev->dev, "Can't allocate tegra_aic326x struct\n");
1235                 return -ENOMEM;
1236         }
1237
1238         machine->pdata = pdata;
1239
1240         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev, card);
1241         if (ret)
1242                 goto err_free_machine;
1243
1244         card->dev = &pdev->dev;
1245         platform_set_drvdata(pdev, card);
1246         snd_soc_card_set_drvdata(card, machine);
1247
1248 #ifdef CONFIG_SWITCH
1249         /* Add h2w switch class support */
1250         ret = tegra_asoc_switch_register(&aic326x_wired_switch_dev);
1251         if (ret < 0) {
1252                 dev_err(&pdev->dev, "not able to register switch device %d\n",
1253                         ret);
1254                 goto err_fini_utils;
1255         }
1256 #endif
1257
1258 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1259         for (i = 0; i < NUM_I2S_DEVICES ; i++) {
1260                 machine->codec_info[i].i2s_id =
1261                         pdata->i2s_param[i].audio_port_id;
1262                 machine->codec_info[i].bitsize =
1263                         pdata->i2s_param[i].sample_size;
1264                 machine->codec_info[i].is_i2smaster =
1265                         pdata->i2s_param[i].is_i2s_master;
1266                 machine->codec_info[i].rate =
1267                         pdata->i2s_param[i].rate;
1268                 machine->codec_info[i].channels =
1269                         pdata->i2s_param[i].channels;
1270                 if ((pdata->i2s_param[i].i2s_mode == TEGRA_DAIFMT_DSP_A) ||
1271                         (pdata->i2s_param[i].i2s_mode == TEGRA_DAIFMT_DSP_B))
1272                         machine->codec_info[i].is_format_dsp = 1;
1273                 else
1274                         machine->codec_info[i].is_format_dsp = 0;
1275         }
1276
1277         tegra_aic326x_dai[DAI_LINK_HIFI].cpu_dai_name =
1278         tegra_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1279
1280         tegra_aic326x_dai[DAI_LINK_BTSCO].cpu_dai_name =
1281         tegra_i2s_dai_name[machine->codec_info[BT_SCO].i2s_id];
1282 #endif
1283
1284         if (machine_is_tegra_enterprise()) {
1285                 tegra_aic326x_dai[DAI_LINK_HIFI].codec_name =
1286                                                 "tlv320aic3262-codec";
1287                 tegra_aic326x_dai[DAI_LINK_VOICE_CALL].codec_name =
1288                                                 "tlv320aic3262-codec";
1289                 tegra_aic326x_dai[DAI_LINK_VOICE_CALL].codec_dai_name =
1290                                                 "aic326x-asi1";
1291         }
1292
1293         ret = snd_soc_register_card(card);
1294         if (ret) {
1295                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
1296                         ret);
1297                 goto err_switch_unregister;
1298         }
1299
1300         if (!card->instantiated) {
1301                 dev_err(&pdev->dev, "No TI AIC3262 codec\n");
1302                 goto err_unregister_card;
1303         }
1304
1305 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1306         ret = tegra_asoc_utils_set_parent(&machine->util_data,
1307                                 pdata->i2s_param[HIFI_CODEC].is_i2s_master);
1308         if (ret) {
1309                 dev_err(&pdev->dev, "tegra_asoc_utils_set_parent failed (%d)\n",
1310                         ret);
1311                 goto err_unregister_card;
1312         }
1313 #endif
1314
1315         return 0;
1316
1317 err_unregister_card:
1318         snd_soc_unregister_card(card);
1319 err_switch_unregister:
1320 #ifdef CONFIG_SWITCH
1321         tegra_asoc_switch_unregister(&aic326x_wired_switch_dev);
1322 #endif
1323 err_fini_utils:
1324         tegra_asoc_utils_fini(&machine->util_data);
1325 err_free_machine:
1326         kfree(machine);
1327         return ret;
1328 }
1329
1330 static int __devexit tegra_aic326x_driver_remove(struct platform_device *pdev)
1331 {
1332         struct snd_soc_card *card = platform_get_drvdata(pdev);
1333         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1334         struct tegra_asoc_platform_data *pdata = machine->pdata;
1335
1336         snd_soc_unregister_card(card);
1337
1338 #ifdef CONFIG_SWITCH
1339         tegra_asoc_switch_unregister(&aic326x_wired_switch_dev);
1340 #endif
1341
1342         tegra_asoc_utils_fini(&machine->util_data);
1343
1344         if (machine->gpio_requested & GPIO_EXT_MIC_EN)
1345                 gpio_free(pdata->gpio_ext_mic_en);
1346         if (machine->gpio_requested & GPIO_INT_MIC_EN)
1347                 gpio_free(pdata->gpio_int_mic_en);
1348         if (machine->gpio_requested & GPIO_HP_MUTE)
1349                 gpio_free(pdata->gpio_hp_mute);
1350         if (machine->gpio_requested & GPIO_SPKR_EN)
1351                 gpio_free(pdata->gpio_spkr_en);
1352
1353         kfree(machine);
1354
1355         return 0;
1356 }
1357
1358 static struct platform_driver tegra_aic326x_driver = {
1359         .driver = {
1360                 .name = DRV_NAME,
1361                 .owner = THIS_MODULE,
1362                 .pm = &snd_soc_pm_ops,
1363         },
1364         .probe = tegra_aic326x_driver_probe,
1365         .remove = __devexit_p(tegra_aic326x_driver_remove),
1366 };
1367
1368 static int __init tegra_aic326x_modinit(void)
1369 {
1370         return platform_driver_register(&tegra_aic326x_driver);
1371 }
1372 module_init(tegra_aic326x_modinit);
1373
1374 static void __exit tegra_aic326x_modexit(void)
1375 {
1376         platform_driver_unregister(&tegra_aic326x_driver);
1377 }
1378 module_exit(tegra_aic326x_modexit);
1379
1380 /* Module information */
1381 MODULE_AUTHOR("Vinod G. <vinodg@nvidia.com>");
1382 MODULE_DESCRIPTION("Tegra+AIC3262 machine ASoC driver");
1383 MODULE_DESCRIPTION("Tegra ALSA SoC");
1384 MODULE_LICENSE("GPL");
1385