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