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