14e02e25e4d811f0a9051ce5f917f535b984474e
[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         {"DMIC3", NULL, "DMic Sec"},
982         {"DMIC4", NULL, "DMic Sec"},
983 };
984
985 static const struct snd_kcontrol_new tegra_max98090_controls[] = {
986         SOC_DAPM_PIN_SWITCH("Int Spk"),
987         SOC_DAPM_PIN_SWITCH("Earpiece"),
988         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
989         SOC_DAPM_PIN_SWITCH("Mic Jack"),
990         SOC_DAPM_PIN_SWITCH("Int Mic"),
991         SOC_DAPM_PIN_SWITCH("Ext Mic"),
992         SOC_DAPM_PIN_SWITCH("DMic Pri"),
993         SOC_DAPM_PIN_SWITCH("DMic Sec"),
994 };
995
996 static int tegra_max98090_init(struct snd_soc_pcm_runtime *rtd)
997 {
998         struct snd_soc_codec *codec = rtd->codec;
999         struct snd_soc_dapm_context *dapm = &codec->dapm;
1000         struct snd_soc_card *card = codec->card;
1001         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
1002         struct tegra_asoc_platform_data *pdata = machine->pdata;
1003         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1004         int ret;
1005
1006         i2s->is_dam_used = false;
1007         if (i2s->id == machine->codec_info[BT_SCO].i2s_id)
1008                 i2s->is_dam_used = true;
1009
1010         if (machine->init_done)
1011                 return 0;
1012
1013         machine->init_done = true;
1014
1015         if (gpio_is_valid(pdata->gpio_hp_mute)) {
1016                 ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
1017                 if (ret) {
1018                         dev_err(card->dev, "cannot get hp_mute gpio\n");
1019                         return ret;
1020                 }
1021                 machine->gpio_requested |= GPIO_HP_MUTE;
1022
1023                 gpio_direction_output(pdata->gpio_hp_mute, 0);
1024         }
1025
1026         if (gpio_is_valid(pdata->gpio_hp_det) &&
1027                 (of_machine_is_compatible("nvidia,norrin") ||
1028                 (of_machine_is_compatible("nvidia,laguna")))) {
1029                 tegra_max98090_hp_jack_gpio.gpio = pdata->gpio_hp_det;
1030                 tegra_max98090_hp_jack_gpio.invert =
1031                         !pdata->gpio_hp_det_active_high;
1032                 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
1033                                         &tegra_max98090_hp_jack);
1034 #ifndef CONFIG_SWITCH
1035                 snd_soc_jack_add_pins(&tegra_max98090_hp_jack,
1036                                         ARRAY_SIZE(tegra_max98090_hs_jack_pins),
1037                                         tegra_max98090_hs_jack_pins);
1038 #else
1039                 snd_soc_jack_notifier_register(&tegra_max98090_hp_jack,
1040                                                 &tegra_max98090_jack_detect_nb);
1041 #endif
1042                 snd_soc_jack_add_gpios(&tegra_max98090_hp_jack,
1043                                         1, &tegra_max98090_hp_jack_gpio);
1044                 machine->gpio_requested |= GPIO_HP_DET;
1045         }
1046
1047         /* Add call mode switch control */
1048         ret = snd_ctl_add(codec->card->snd_card,
1049                 snd_ctl_new1(&tegra_max98090_call_mode_control, machine));
1050         if (ret < 0)
1051                 return ret;
1052
1053         ret = tegra_asoc_utils_register_ctls(&machine->util_data);
1054         if (ret < 0)
1055                 return ret;
1056
1057         snd_soc_dapm_sync(dapm);
1058         return 0;
1059 }
1060
1061 static struct snd_soc_dai_link tegra_max98090_dai[] = {
1062         [DAI_LINK_HIFI] = {
1063                         .name = "MAX98090",
1064                         .stream_name = "MAX98090 HIFI",
1065                         .codec_name = "max98090.5-0010",
1066                         .codec_dai_name = "HiFi",
1067                         .init = tegra_max98090_init,
1068                         .ops = &tegra_max98090_ops,
1069                 },
1070         [DAI_LINK_BTSCO] = {
1071                         .name = "BT SCO",
1072                         .stream_name = "BT SCO PCM",
1073                         .codec_name = "spdif-dit.1",
1074                         .codec_dai_name = "dit-hifi",
1075                         .init = tegra_max98090_init,
1076                         .ops = &tegra_bt_ops,
1077                 },
1078         [DAI_LINK_VOICE_CALL] = {
1079                         .name = "VOICE CALL",
1080                         .stream_name = "VOICE CALL PCM",
1081                         .codec_name = "max98090.5-0010",
1082                         .cpu_dai_name = "dit-hifi",
1083                         .codec_dai_name = "HiFi",
1084                         .ops = &tegra_voice_call_ops,
1085                 },
1086         [DAI_LINK_BT_VOICE_CALL] = {
1087                         .name = "BT VOICE CALL",
1088                         .stream_name = "BT VOICE CALL PCM",
1089                         .codec_name = "spdif-dit.2",
1090                         .cpu_dai_name = "dit-hifi",
1091                         .codec_dai_name = "dit-hifi",
1092                         .ops = &tegra_bt_voice_call_ops,
1093                 },
1094         [DAI_LINK_HIFI_MAX97236] {
1095                 .name = "MAX97236",
1096                 .stream_name = "MAX97236 HIFI",
1097                 .codec_name = "max97236.5-0040",
1098                 .platform_name = "tegra-pcm-audio",
1099                 .codec_dai_name = "max97236-HiFi",
1100                 .ops = NULL,
1101         },
1102 };
1103
1104 static int tegra_max98090_suspend_post(struct snd_soc_card *card)
1105 {
1106         struct snd_soc_jack_gpio *gpio = &tegra_max98090_hp_jack_gpio;
1107         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
1108         int i, suspend_allowed = 1;
1109
1110         for (i = 0; i < machine->pcard->num_links; i++) {
1111                 if (machine->pcard->dai_link[i].ignore_suspend) {
1112                         suspend_allowed = 0;
1113                         break;
1114                 }
1115         }
1116
1117         if (suspend_allowed) {
1118                 if (gpio_is_valid(gpio->gpio))
1119                         disable_irq(gpio_to_irq(gpio->gpio));
1120
1121                 if (machine->clock_enabled) {
1122                         machine->clock_enabled = 0;
1123                         tegra_asoc_utils_clk_disable(&machine->util_data);
1124                 }
1125
1126                 if (machine->avdd_aud_reg)
1127                         regulator_disable(machine->avdd_aud_reg);
1128                 if (machine->vdd_sw_1v8_reg)
1129                         regulator_disable(machine->vdd_sw_1v8_reg);
1130                 if (machine->spkvdd_5v0)
1131                         regulator_disable(machine->spkvdd_5v0);
1132         }
1133
1134         return 0;
1135 }
1136
1137 static int tegra_max98090_resume_pre(struct snd_soc_card *card)
1138 {
1139         int val;
1140         struct snd_soc_jack_gpio *gpio = &tegra_max98090_hp_jack_gpio;
1141         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
1142         int i, suspend_allowed = 1;
1143         int ret;
1144
1145         for (i = 0; i < machine->pcard->num_links; i++) {
1146                 if (machine->pcard->dai_link[i].ignore_suspend) {
1147                         suspend_allowed = 0;
1148                         break;
1149                 }
1150         }
1151
1152         if (suspend_allowed) {
1153                 if (gpio_is_valid(gpio->gpio)) {
1154                         val = gpio_get_value(gpio->gpio);
1155                         val = gpio->invert ? !val : val;
1156                         snd_soc_jack_report(gpio->jack, val, gpio->report);
1157                         enable_irq(gpio_to_irq(gpio->gpio));
1158                 }
1159
1160                 if (!machine->clock_enabled) {
1161                         machine->clock_enabled = 1;
1162                         tegra_asoc_utils_clk_enable(&machine->util_data);
1163                 }
1164
1165                 if (machine->avdd_aud_reg)
1166                         ret = regulator_enable(machine->avdd_aud_reg);
1167                 if (machine->vdd_sw_1v8_reg)
1168                         ret = regulator_enable(machine->vdd_sw_1v8_reg);
1169                 if (machine->spkvdd_5v0)
1170                         ret = regulator_enable(machine->spkvdd_5v0);
1171         }
1172
1173         return 0;
1174 }
1175
1176 static int tegra_max98090_set_bias_level(struct snd_soc_card *card,
1177                                         struct snd_soc_dapm_context *dapm,
1178                                         enum snd_soc_bias_level level)
1179 {
1180         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
1181
1182         if (machine->bias_level == SND_SOC_BIAS_OFF &&
1183                 level != SND_SOC_BIAS_OFF && (!machine->clock_enabled)) {
1184                 machine->clock_enabled = 1;
1185                 machine->bias_level = level;
1186         }
1187
1188         return 0;
1189 }
1190
1191 static int tegra_max98090_set_bias_level_post(struct snd_soc_card *card,
1192                                         struct snd_soc_dapm_context *dapm,
1193                                         enum snd_soc_bias_level level)
1194 {
1195         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
1196
1197         if (machine->bias_level != SND_SOC_BIAS_OFF &&
1198                 level == SND_SOC_BIAS_OFF && (machine->clock_enabled)) {
1199                 machine->clock_enabled = 0;
1200         }
1201
1202         machine->bias_level = level;
1203
1204         return 0 ;
1205 }
1206
1207 static int tegra_late_probe(struct snd_soc_card *card)
1208 {
1209         struct snd_soc_codec *codec236 =
1210                                 card->rtd[DAI_LINK_HIFI_MAX97236].codec;
1211         int ret;
1212
1213         if (of_machine_is_compatible("nvidia,laguna")) {
1214                 return 0;
1215         }
1216
1217         ret = snd_soc_jack_new(codec236,
1218                         "Headphone Jack",
1219                         SND_JACK_HEADSET | SND_JACK_LINEOUT | 0x7E00,
1220                         &tegra_max98090_hp_jack);
1221         if (ret) {
1222                 dev_err(codec236->dev, "snd_soc_jack_new returned %d\n", ret);
1223                 return ret;
1224         }
1225
1226 #ifdef CONFIG_SWITCH
1227         snd_soc_jack_notifier_register(&tegra_max98090_hp_jack,
1228                         &tegra_max98090_jack_detect_nb);
1229 #else /*gpio based headset detection*/
1230         snd_soc_jack_add_pins(&tegra_max98090_hp_jack,
1231                         ARRAY_SIZE(tegra_max98090_hs_jack_pins),
1232                         tegra_max98090_hs_jack_pins);
1233 #endif
1234
1235         max97236_mic_detect(codec236, &tegra_max98090_hp_jack);
1236
1237         return 0;
1238 }
1239
1240 static struct snd_soc_card snd_soc_tegra_max98090 = {
1241         .name = "tegra-max98090",
1242         .owner = THIS_MODULE,
1243         .dai_link = tegra_max98090_dai,
1244         .num_links = ARRAY_SIZE(tegra_max98090_dai),
1245         .suspend_post = tegra_max98090_suspend_post,
1246         .resume_pre = tegra_max98090_resume_pre,
1247         .set_bias_level = tegra_max98090_set_bias_level,
1248         .set_bias_level_post = tegra_max98090_set_bias_level_post,
1249
1250         .controls = tegra_max98090_controls,
1251         .num_controls = ARRAY_SIZE(tegra_max98090_controls),
1252         .dapm_widgets = tegra_max98090_dapm_widgets,
1253         .num_dapm_widgets = ARRAY_SIZE(tegra_max98090_dapm_widgets),
1254         .dapm_routes = tegra_max98090_audio_map,
1255         .num_dapm_routes = ARRAY_SIZE(tegra_max98090_audio_map),
1256         .fully_routed = true,
1257
1258         .late_probe     = tegra_late_probe,
1259 };
1260
1261 static __devinit int tegra_max98090_driver_probe(struct platform_device *pdev)
1262 {
1263         struct snd_soc_card *card = &snd_soc_tegra_max98090;
1264         struct tegra_max98090 *machine;
1265         struct tegra_asoc_platform_data *pdata;
1266         int ret, i;
1267
1268         if (of_machine_is_compatible("nvidia,laguna")) {
1269                 card->num_links = 1;
1270                 card->dai_link[DAI_LINK_HIFI].codec_name = "max98090.7-0010";
1271         }
1272
1273         pdata = pdev->dev.platform_data;
1274         if (!pdata) {
1275                 dev_err(&pdev->dev, "No platform data supplied\n");
1276                 return -EINVAL;
1277         }
1278
1279         if (pdata->codec_name)
1280                 card->dai_link->codec_name = pdata->codec_name;
1281
1282         if (pdata->codec_dai_name)
1283                 card->dai_link->codec_dai_name = pdata->codec_dai_name;
1284
1285         machine = kzalloc(sizeof(struct tegra_max98090), GFP_KERNEL);
1286         if (!machine) {
1287                 dev_err(&pdev->dev, "Can't allocate tegra_max98090 struct\n");
1288                 return -ENOMEM;
1289         }
1290
1291         machine->pdata = pdata;
1292         machine->pcard = card;
1293         machine->bias_level = SND_SOC_BIAS_STANDBY;
1294         machine->clock_enabled = 1;
1295
1296         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev, card);
1297         if (ret)
1298                 goto err_free_machine;
1299
1300         machine->avdd_aud_reg = regulator_get(&pdev->dev, "avdd_aud");
1301         if (IS_ERR(machine->avdd_aud_reg)) {
1302                 dev_info(&pdev->dev, "avdd_aud regulator not found\n");
1303                 machine->avdd_aud_reg = 0;
1304         }
1305
1306         machine->vdd_sw_1v8_reg = regulator_get(&pdev->dev, "vdd_aud_dgtl");
1307         if (IS_ERR(machine->vdd_sw_1v8_reg)) {
1308                 dev_info(&pdev->dev, "vdd_sw_1v8_reg regulator not found\n");
1309                 machine->vdd_sw_1v8_reg = 0;
1310         }
1311
1312         machine->spkvdd_5v0 = regulator_get(&pdev->dev, "spkvdd");
1313         if (IS_ERR(machine->spkvdd_5v0)) {
1314                 dev_info(&pdev->dev, "spkvdd_5v0 regulator not found\n");
1315                 machine->spkvdd_5v0 = 0;
1316         }
1317
1318         if (machine->spkvdd_5v0)
1319                 ret = regulator_enable(machine->spkvdd_5v0);
1320         if (machine->vdd_sw_1v8_reg)
1321                 ret = regulator_enable(machine->vdd_sw_1v8_reg);
1322         if (machine->avdd_aud_reg)
1323                 ret = regulator_enable(machine->avdd_aud_reg);
1324
1325
1326 #ifdef CONFIG_SWITCH
1327         /* Add h2w switch class support */
1328         ret = tegra_asoc_switch_register(&tegra_max98090_headset_switch);
1329         if (ret < 0) {
1330                 dev_err(&pdev->dev, "not able to register switch device\n");
1331                 goto err_fini_utils;
1332         }
1333 #endif
1334
1335         card->dev = &pdev->dev;
1336         platform_set_drvdata(pdev, card);
1337         snd_soc_card_set_drvdata(card, machine);
1338
1339         for (i = 0; i < NUM_I2S_DEVICES ; i++) {
1340                 machine->codec_info[i].i2s_id =
1341                         pdata->i2s_param[i].audio_port_id;
1342                 machine->codec_info[i].bitsize =
1343                         pdata->i2s_param[i].sample_size;
1344                 machine->codec_info[i].is_i2smaster =
1345                         pdata->i2s_param[i].is_i2s_master;
1346                 machine->codec_info[i].rate =
1347                         pdata->i2s_param[i].rate;
1348                 machine->codec_info[i].channels =
1349                         pdata->i2s_param[i].channels;
1350                 machine->codec_info[i].i2s_mode =
1351                         pdata->i2s_param[i].i2s_mode;
1352                 machine->codec_info[i].bit_clk =
1353                         pdata->i2s_param[i].bit_clk;
1354         }
1355
1356         if (pdata->ahub_bbc1_param) {
1357                 machine->ahub_bbc1_info.port_id =
1358                         pdata->ahub_bbc1_param->port_id;
1359                 machine->ahub_bbc1_info.sample_size =
1360                         pdata->ahub_bbc1_param->sample_size;
1361                 machine->ahub_bbc1_info.rate =
1362                         pdata->ahub_bbc1_param->rate;
1363                 machine->ahub_bbc1_info.channels =
1364                         pdata->ahub_bbc1_param->channels;
1365                 machine->ahub_bbc1_info.bit_clk =
1366                         pdata->ahub_bbc1_param->bit_clk;
1367         }
1368
1369         tegra_max98090_dai[DAI_LINK_HIFI].cpu_dai_name =
1370         tegra_max98090_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1371         tegra_max98090_dai[DAI_LINK_HIFI].platform_name =
1372         tegra_max98090_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1373
1374         tegra_max98090_dai[DAI_LINK_HIFI_MAX97236].cpu_dai_name =
1375         tegra_max98090_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1376         tegra_max98090_dai[DAI_LINK_HIFI_MAX97236].platform_name =
1377         tegra_max98090_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1378
1379         tegra_max98090_dai[DAI_LINK_BTSCO].cpu_dai_name =
1380         tegra_max98090_i2s_dai_name[machine->codec_info[BT_SCO].i2s_id];
1381         tegra_max98090_dai[DAI_LINK_BTSCO].platform_name =
1382         tegra_max98090_i2s_dai_name[machine->codec_info[BT_SCO].i2s_id];
1383
1384         card->dapm.idle_bias_off = 1;
1385         ret = snd_soc_register_card(card);
1386         if (ret) {
1387                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
1388                         ret);
1389                 goto err_switch_unregister;
1390         }
1391
1392         if (!card->instantiated) {
1393                 dev_err(&pdev->dev, "No MAX98090 codec\n");
1394                 goto err_unregister_card;
1395         }
1396
1397         ret = tegra_asoc_utils_set_parent(&machine->util_data,
1398                                 pdata->i2s_param[HIFI_CODEC].is_i2s_master);
1399         if (ret) {
1400                 dev_err(&pdev->dev,
1401                                 "tegra_asoc_utils_set_parent failed (%d)\n",
1402                                 ret);
1403                 goto err_unregister_card;
1404         }
1405
1406         return 0;
1407
1408 err_unregister_card:
1409         snd_soc_unregister_card(card);
1410 err_switch_unregister:
1411 #ifdef CONFIG_SWITCH
1412         switch_dev_unregister(&tegra_max98090_headset_switch);
1413 #endif
1414 err_fini_utils:
1415         tegra_asoc_utils_fini(&machine->util_data);
1416 err_free_machine:
1417         kfree(machine);
1418         return ret;
1419 }
1420
1421 static int __devexit tegra_max98090_driver_remove(struct platform_device *pdev)
1422 {
1423         struct snd_soc_card *card = platform_get_drvdata(pdev);
1424         struct tegra_max98090 *machine = snd_soc_card_get_drvdata(card);
1425         struct tegra_asoc_platform_data *pdata = machine->pdata;
1426
1427 #ifdef CONFIG_SWITCH
1428         switch_dev_unregister(&tegra_max98090_headset_switch);
1429 #endif
1430
1431         if (machine->gpio_requested & GPIO_HP_MUTE)
1432                 gpio_free(pdata->gpio_hp_mute);
1433         if (machine->gpio_requested & GPIO_HP_DET)
1434                 snd_soc_jack_free_gpios(&tegra_max98090_hp_jack,
1435                         1,
1436                         &tegra_max98090_hp_jack_gpio);
1437
1438         machine->gpio_requested = 0;
1439
1440         if (machine->avdd_aud_reg)
1441                 regulator_put(machine->avdd_aud_reg);
1442
1443         if (machine->vdd_sw_1v8_reg)
1444                 regulator_put(machine->vdd_sw_1v8_reg);
1445
1446         if (machine->spkvdd_5v0)
1447                 regulator_put(machine->spkvdd_5v0);
1448
1449         snd_soc_unregister_card(card);
1450
1451         tegra_asoc_utils_fini(&machine->util_data);
1452
1453         kfree(machine);
1454
1455         return 0;
1456 }
1457
1458 static struct platform_driver tegra_max98090_driver = {
1459         .driver = {
1460                 .name = DRV_NAME,
1461                 .owner = THIS_MODULE,
1462                 .pm = &snd_soc_pm_ops,
1463         },
1464         .probe = tegra_max98090_driver_probe,
1465         .remove = __devexit_p(tegra_max98090_driver_remove),
1466 };
1467
1468 static int __init tegra_max98090_modinit(void)
1469 {
1470         return platform_driver_register(&tegra_max98090_driver);
1471 }
1472 module_init(tegra_max98090_modinit);
1473
1474 static void __exit tegra_max98090_modexit(void)
1475 {
1476         platform_driver_unregister(&tegra_max98090_driver);
1477 }
1478 module_exit(tegra_max98090_modexit);
1479
1480 MODULE_AUTHOR("Kamal Kannan Balagopalan <kbalagopalan@nvidia.com>");
1481 MODULE_DESCRIPTION("Tegra+MAX98090 machine ASoC driver");
1482 MODULE_LICENSE("GPL");
1483 MODULE_ALIAS("platform:" DRV_NAME);