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