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