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