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