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