asoc: tegra: aic326x: add dapm route for capture path
[linux-2.6.git] / sound / soc / tegra / tegra_aic326x.c
1 /*
2  * tegra_aic326x.c - Tegra machine ASoC driver for boards using TI 3262 codec.
3  *
4  * Author: Vinod G. <vinodg@nvidia.com>
5  * Copyright (C) 2011 - NVIDIA, Inc.
6  *
7  * Based on code copyright/by:
8  *
9  * (c) 2010, 2011 Nvidia Graphics Pvt. Ltd.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  *
25  */
26
27 #include <asm/mach-types.h>
28
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/gpio.h>
33 #include <linux/regulator/consumer.h>
34 #ifdef CONFIG_SWITCH
35 #include <linux/switch.h>
36 #endif
37
38 #include <mach/tegra_aic326x_pdata.h>
39
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45
46 #include "../codecs/tlv320aic326x.h"
47
48 #include "tegra_pcm.h"
49 #include "tegra_asoc_utils.h"
50
51 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
52 #include "tegra20_das.h"
53 #else
54 #include "tegra30_ahub.h"
55 #include "tegra30_i2s.h"
56 #include "tegra30_dam.h"
57 #endif
58
59
60 #define DRV_NAME "tegra-snd-aic326x"
61
62 #define GPIO_SPKR_EN    BIT(0)
63 #define GPIO_HP_MUTE    BIT(1)
64 #define GPIO_INT_MIC_EN BIT(2)
65 #define GPIO_EXT_MIC_EN BIT(3)
66
67 #define DAI_LINK_HIFI           0
68 #define DAI_LINK_SPDIF          1
69 #define DAI_LINK_BTSCO          2
70 #define DAI_LINK_VOICE_CALL     3
71 #define DAI_LINK_BT_VOICE_CALL  4
72 #define NUM_DAI_LINKS   5
73
74 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
75 const char *tegra_aic326x_i2s_dai_name[TEGRA30_NR_I2S_IFC] = {
76         "tegra30-i2s.0",
77         "tegra30-i2s.1",
78         "tegra30-i2s.2",
79         "tegra30-i2s.3",
80         "tegra30-i2s.4",
81 };
82 #endif
83
84 struct tegra_aic326x {
85         struct tegra_asoc_utils_data util_data;
86         struct tegra_aic326x_platform_data *pdata;
87         struct regulator *audio_reg;
88         int gpio_requested;
89         bool init_done;
90         int is_call_mode;
91         int is_device_bt;
92 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
93         struct codec_config codec_info[NUM_I2S_DEVICES];
94 #endif
95 };
96
97 static int tegra_aic326x_call_mode_info(struct snd_kcontrol *kcontrol,
98                         struct snd_ctl_elem_info *uinfo)
99 {
100         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
101         uinfo->count = 1;
102         uinfo->value.integer.min = 0;
103         uinfo->value.integer.max = 1;
104         return 0;
105 }
106
107 static int tegra_aic326x_call_mode_get(struct snd_kcontrol *kcontrol,
108                 struct snd_ctl_elem_value *ucontrol)
109 {
110         struct tegra_aic326x *machine = snd_kcontrol_chip(kcontrol);
111
112         ucontrol->value.integer.value[0] = machine->is_call_mode;
113
114         return 0;
115 }
116
117 static int tegra_aic326x_call_mode_put(struct snd_kcontrol *kcontrol,
118                 struct snd_ctl_elem_value *ucontrol)
119 {
120         struct tegra_aic326x *machine = snd_kcontrol_chip(kcontrol);
121         int is_call_mode_new = ucontrol->value.integer.value[0];
122 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
123         int codec_dap_id, codec_dap_sel, bb_dap_id, bb_dap_sel;
124 #else
125         int codec_index;
126 #endif
127
128         if (machine->is_call_mode == is_call_mode_new)
129                 return 0;
130
131 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
132         bb_dap_id = TEGRA20_DAS_DAP_ID_3;
133         bb_dap_sel = TEGRA20_DAS_DAP_SEL_DAP3;
134
135         if (machine->is_device_bt) {
136                 codec_dap_id = TEGRA20_DAS_DAP_ID_4;
137                 codec_dap_sel = TEGRA20_DAS_DAP_SEL_DAP4;
138         }
139         else {
140                 codec_dap_id = TEGRA20_DAS_DAP_ID_2;
141                 codec_dap_sel = TEGRA20_DAS_DAP_SEL_DAP2;
142         }
143 #else
144         if (machine->is_device_bt)
145                 codec_index = BT_SCO;
146         else
147                 codec_index = HIFI_CODEC;
148 #endif
149
150         if (is_call_mode_new) {
151 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
152                 tegra20_das_set_tristate(codec_dap_id, 1);
153                 tegra20_das_set_tristate(bb_dap_id, 1);
154                 tegra20_das_connect_dap_to_dap(codec_dap_id,
155                         bb_dap_sel, 1, 0, 0);
156                 tegra20_das_connect_dap_to_dap(bb_dap_id,
157                         codec_dap_sel, 0, 0, 0);
158                 tegra20_das_set_tristate(codec_dap_id, 0);
159                 tegra20_das_set_tristate(bb_dap_id, 0);
160 #else
161                 if (machine->codec_info[codec_index].rate == 0 ||
162                         machine->codec_info[codec_index].channels == 0)
163                                 return -EINVAL;
164
165                 tegra30_make_voice_call_connections(
166                         &machine->codec_info[codec_index],
167                         &machine->codec_info[BASEBAND]);
168 #endif
169         } else {
170 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
171                 tegra20_das_set_tristate(codec_dap_id, 1);
172                 tegra20_das_set_tristate(bb_dap_id, 1);
173                 tegra20_das_connect_dap_to_dap(bb_dap_id,
174                         bb_dap_sel, 0, 0, 0);
175                 tegra20_das_connect_dap_to_dap(codec_dap_id,
176                         codec_dap_sel, 0, 0, 0);
177                 tegra20_das_set_tristate(codec_dap_id, 0);
178                 tegra20_das_set_tristate(bb_dap_id, 0);
179 #else
180                 tegra30_break_voice_call_connections(
181                         &machine->codec_info[codec_index],
182                         &machine->codec_info[BASEBAND]);
183 #endif
184         }
185
186         machine->is_call_mode = is_call_mode_new;
187
188         return 1;
189 }
190
191 struct snd_kcontrol_new tegra_aic326x_call_mode_control = {
192         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
193         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
194         .name = "Call Mode Switch",
195         .private_value = 0xffff,
196         .info = tegra_aic326x_call_mode_info,
197         .get = tegra_aic326x_call_mode_get,
198         .put = tegra_aic326x_call_mode_put
199 };
200
201 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
202 static int tegra_aic326x_set_dam_cif(int dam_ifc, int srate,
203                         int channels, int bit_size)
204 {
205         tegra30_dam_set_samplerate(dam_ifc, TEGRA30_DAM_CHOUT,
206                                 srate);
207         tegra30_dam_set_samplerate(dam_ifc, TEGRA30_DAM_CHIN1,
208                                 srate);
209         tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHIN1,
210                 channels, bit_size, channels,
211                                 bit_size);
212         tegra30_dam_set_acif(dam_ifc, TEGRA30_DAM_CHOUT,
213                 channels, bit_size, channels,
214                                 bit_size);
215
216         return 0;
217 }
218 #endif
219
220 static int tegra_aic326x_get_mclk(int srate)
221 {
222         int mclk = 0;
223         switch (srate) {
224         case 8000:
225         case 16000:
226         case 24000:
227         case 32000:
228         case 48000:
229         case 64000:
230         case 96000:
231                 mclk = 12288000;
232                 break;
233         case 11025:
234         case 22050:
235         case 44100:
236         case 88200:
237                 mclk = 11289600;
238                 break;
239         default:
240                 mclk = -EINVAL;
241                 break;
242         }
243
244         return mclk;
245 }
246
247 static int tegra_aic326x_hw_params(struct snd_pcm_substream *substream,
248                                         struct snd_pcm_hw_params *params)
249 {
250         struct snd_soc_pcm_runtime *rtd = substream->private_data;
251         struct snd_soc_dai *codec_dai = rtd->codec_dai;
252         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
253         struct snd_soc_codec *codec = rtd->codec;
254         struct snd_soc_card *card = codec->card;
255         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
256 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
257         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
258 #endif
259         int srate, mclk, sample_size, daifmt;
260         int err;
261
262         switch (params_format(params)) {
263         case SNDRV_PCM_FORMAT_S16_LE:
264                 sample_size = 16;
265                 break;
266         default:
267                 return -EINVAL;
268         }
269
270         srate = params_rate(params);
271
272         mclk = tegra_aic326x_get_mclk(srate);
273         if (mclk < 0)
274                 return mclk;
275
276         daifmt = SND_SOC_DAIFMT_I2S |
277                         SND_SOC_DAIFMT_NB_NF |
278                         SND_SOC_DAIFMT_CBS_CFS;
279
280         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
281         if (err < 0) {
282                 if (!(machine->util_data.set_mclk % mclk))
283                         mclk = machine->util_data.set_mclk;
284                 else {
285                         dev_err(card->dev, "Can't configure clocks\n");
286                         return err;
287                 }
288         }
289
290         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
291
292         err = snd_soc_dai_set_fmt(codec_dai, daifmt);
293         if (err < 0) {
294                 dev_err(card->dev, "codec_dai fmt not set\n");
295                 return err;
296         }
297
298         err = snd_soc_dai_set_fmt(cpu_dai, daifmt);
299         if (err < 0) {
300                 dev_err(card->dev, "cpu_dai fmt not set\n");
301                 return err;
302         }
303
304         err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
305                                         SND_SOC_CLOCK_IN);
306         if (err < 0) {
307                 dev_err(card->dev, "codec_dai clock not set\n");
308                 return err;
309         }
310
311 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
312         err = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAP_SEL_DAC1,
313                                         TEGRA20_DAS_DAP_ID_1);
314         if (err < 0) {
315                 dev_err(card->dev, "failed to set dap-dac path\n");
316                 return err;
317         }
318
319         err = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_1,
320                                         TEGRA20_DAS_DAP_SEL_DAC1);
321         if (err < 0) {
322                 dev_err(card->dev, "failed to set dac-dap path\n");
323                 return err;
324         }
325 #else
326         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
327                 tegra_aic326x_set_dam_cif(i2s->dam_ifc, srate,
328                                 params_channels(params), sample_size);
329 #endif
330
331         return 0;
332 }
333
334 static int tegra_aic326x_spdif_hw_params(struct snd_pcm_substream *substream,
335                                         struct snd_pcm_hw_params *params)
336 {
337         struct snd_soc_pcm_runtime *rtd = substream->private_data;
338         struct snd_soc_card *card = rtd->card;
339         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
340         int srate, mclk, min_mclk;
341         int err;
342
343         srate = params_rate(params);
344
345         mclk = tegra_aic326x_get_mclk(srate);
346         if (mclk < 0)
347                 return mclk;
348
349         min_mclk = 128 * srate;
350
351         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
352         if (err < 0) {
353                 if (!(machine->util_data.set_mclk % min_mclk))
354                         mclk = machine->util_data.set_mclk;
355                 else {
356                         dev_err(card->dev, "Can't configure clocks\n");
357                         return err;
358                 }
359         }
360
361         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
362
363         return 0;
364 }
365
366 static int tegra_aic326x_bt_hw_params(struct snd_pcm_substream *substream,
367                 struct snd_pcm_hw_params *params)
368 {
369         struct snd_soc_pcm_runtime *rtd = substream->private_data;
370 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
371         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
372 #endif
373         struct snd_soc_card *card = rtd->card;
374         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
375         int err, srate, mclk, min_mclk, sample_size;
376
377         switch (params_format(params)) {
378         case SNDRV_PCM_FORMAT_S16_LE:
379                 sample_size = 16;
380                 break;
381         default:
382                 return -EINVAL;
383         }
384
385         srate = params_rate(params);
386
387         mclk = tegra_aic326x_get_mclk(srate);
388         if (mclk < 0)
389                 return mclk;
390
391         min_mclk = 64 * srate;
392
393         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
394         if (err < 0) {
395                 if (!(machine->util_data.set_mclk % min_mclk))
396                         mclk = machine->util_data.set_mclk;
397                 else {
398                         dev_err(card->dev, "Can't configure clocks\n");
399                         return err;
400                 }
401         }
402
403         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
404
405         err = snd_soc_dai_set_fmt(rtd->cpu_dai,
406                         SND_SOC_DAIFMT_DSP_A |
407                         SND_SOC_DAIFMT_NB_NF |
408                         SND_SOC_DAIFMT_CBM_CFM);
409         if (err < 0) {
410                 dev_err(rtd->codec->card->dev, "cpu_dai fmt not set\n");
411                 return err;
412         }
413
414 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
415         err = tegra20_das_connect_dac_to_dap(TEGRA20_DAS_DAP_SEL_DAC2,
416                                         TEGRA20_DAS_DAP_ID_4);
417         if (err < 0) {
418                 dev_err(card->dev, "failed to set dac-dap path\n");
419                 return err;
420         }
421
422         err = tegra20_das_connect_dap_to_dac(TEGRA20_DAS_DAP_ID_4,
423                                         TEGRA20_DAS_DAP_SEL_DAC2);
424         if (err < 0) {
425                 dev_err(card->dev, "failed to set dac-dap path\n");
426                 return err;
427         }
428 #else
429         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
430                 tegra_aic326x_set_dam_cif(i2s->dam_ifc, params_rate(params),
431                                 params_channels(params), sample_size);
432 #endif
433
434         return 0;
435 }
436
437 static int tegra_aic326x_hw_free(struct snd_pcm_substream *substream)
438 {
439         struct snd_soc_pcm_runtime *rtd = substream->private_data;
440         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(rtd->card);
441
442         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 0);
443
444         return 0;
445 }
446
447 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
448 static int tegra_aic326x_startup(struct snd_pcm_substream *substream)
449 {
450         struct snd_soc_pcm_runtime *rtd = substream->private_data;
451         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
452         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
453
454         if ((substream->stream != SNDRV_PCM_STREAM_PLAYBACK) ||
455                 !(i2s->is_dam_used))
456                 return 0;
457
458         /*dam configuration*/
459         if (!i2s->dam_ch_refcount)
460                 i2s->dam_ifc = tegra30_dam_allocate_controller();
461
462         tegra30_dam_allocate_channel(i2s->dam_ifc, TEGRA30_DAM_CHIN1);
463         i2s->dam_ch_refcount++;
464         tegra30_dam_enable_clock(i2s->dam_ifc);
465         tegra30_dam_set_gain(i2s->dam_ifc, TEGRA30_DAM_CHIN1, 0x1000);
466
467         tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
468                         (i2s->dam_ifc*2), i2s->txcif);
469
470         /*
471         *make the dam tx to i2s rx connection if this is the only client
472         *using i2s for playback
473         */
474         if (i2s->playback_ref_count == 1)
475                 tegra30_ahub_set_rx_cif_source(
476                         TEGRA30_AHUB_RXCIF_I2S0_RX0 + i2s->id,
477                         TEGRA30_AHUB_TXCIF_DAM0_TX0 + i2s->dam_ifc);
478
479         /* enable the dam*/
480         tegra30_dam_enable(i2s->dam_ifc, TEGRA30_DAM_ENABLE,
481                         TEGRA30_DAM_CHIN1);
482
483         return 0;
484 }
485
486 static void tegra_aic326x_shutdown(struct snd_pcm_substream *substream)
487 {
488         struct snd_soc_pcm_runtime *rtd = substream->private_data;
489         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
490         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
491
492         if ((substream->stream != SNDRV_PCM_STREAM_PLAYBACK) ||
493                 !(i2s->is_dam_used))
494                 return;
495
496         /* disable the dam*/
497         tegra30_dam_enable(i2s->dam_ifc, TEGRA30_DAM_DISABLE,
498                         TEGRA30_DAM_CHIN1);
499
500         /* disconnect the ahub connections*/
501         tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX1 +
502                                 (i2s->dam_ifc*2));
503
504         /* disable the dam and free the controller */
505         tegra30_dam_disable_clock(i2s->dam_ifc);
506         tegra30_dam_free_channel(i2s->dam_ifc, TEGRA30_DAM_CHIN1);
507         i2s->dam_ch_refcount--;
508         if (!i2s->dam_ch_refcount)
509                 tegra30_dam_free_controller(i2s->dam_ifc);
510 }
511 #endif
512
513 static int tegra_aic326x_voice_call_hw_params(
514                         struct snd_pcm_substream *substream,
515                         struct snd_pcm_hw_params *params)
516 {
517         struct snd_soc_pcm_runtime *rtd = substream->private_data;
518         struct snd_soc_dai *codec_dai = rtd->codec_dai;
519         struct snd_soc_codec *codec = rtd->codec;
520         struct snd_soc_card *card = codec->card;
521         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
522         int srate, mclk;
523         int err;
524
525         srate = params_rate(params);
526         mclk = tegra_aic326x_get_mclk(srate);
527         if (mclk < 0)
528                 return mclk;
529
530
531         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
532         if (err < 0) {
533                 if (!(machine->util_data.set_mclk % mclk))
534                         mclk = machine->util_data.set_mclk;
535                 else {
536                         dev_err(card->dev, "Can't configure clocks\n");
537                         return err;
538                 }
539         }
540
541         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
542
543         err = snd_soc_dai_set_fmt(codec_dai,
544                                         SND_SOC_DAIFMT_DSP_B |
545                                         SND_SOC_DAIFMT_NB_NF |
546                                         SND_SOC_DAIFMT_CBS_CFS);
547         if (err < 0) {
548                 dev_err(card->dev, "codec_dai fmt not set\n");
549                 return err;
550         }
551
552         err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
553                                         SND_SOC_CLOCK_IN);
554         if (err < 0) {
555                 dev_err(card->dev, "codec_dai clock not set\n");
556                 return err;
557         }
558
559 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
560         /* codec configuration */
561         machine->codec_info[HIFI_CODEC].rate = params_rate(params);
562         machine->codec_info[HIFI_CODEC].channels = params_channels(params);
563         machine->codec_info[HIFI_CODEC].bitsize = 16;
564         machine->codec_info[HIFI_CODEC].is_i2smaster = 1;
565         machine->codec_info[HIFI_CODEC].is_format_dsp = 0;
566
567         /* baseband configuration */
568         machine->codec_info[BASEBAND].bitsize = 16;
569         machine->codec_info[BASEBAND].is_i2smaster = 1;
570         machine->codec_info[BASEBAND].is_format_dsp = 1;
571 #endif
572
573         machine->is_device_bt = 0;
574
575         return 0;
576 }
577
578 static void tegra_aic326x_voice_call_shutdown(
579                                         struct snd_pcm_substream *substream)
580 {
581         struct snd_soc_pcm_runtime *rtd = substream->private_data;
582         struct tegra_aic326x *machine  =
583                         snd_soc_card_get_drvdata(rtd->codec->card);
584
585 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
586         machine->codec_info[HIFI_CODEC].rate = 0;
587         machine->codec_info[HIFI_CODEC].channels = 0;
588 #endif
589
590         machine->is_device_bt = 0;
591 }
592
593 static int tegra_aic326x_bt_voice_call_hw_params(
594                         struct snd_pcm_substream *substream,
595                         struct snd_pcm_hw_params *params)
596 {
597         struct snd_soc_pcm_runtime *rtd = substream->private_data;
598         struct snd_soc_card *card = rtd->card;
599         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
600         int err, srate, mclk, min_mclk;
601
602         srate = params_rate(params);
603
604         mclk = tegra_aic326x_get_mclk(srate);
605         if (mclk < 0)
606                 return mclk;
607
608         min_mclk = 64 * srate;
609
610         err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
611         if (err < 0) {
612                 if (!(machine->util_data.set_mclk % min_mclk))
613                         mclk = machine->util_data.set_mclk;
614                 else {
615                         dev_err(card->dev, "Can't configure clocks\n");
616                         return err;
617                 }
618         }
619
620         tegra_asoc_utils_lock_clk_rate(&machine->util_data, 1);
621
622 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
623         /* codec configuration */
624         machine->codec_info[BT_SCO].rate = params_rate(params);
625         machine->codec_info[BT_SCO].channels = params_channels(params);
626         machine->codec_info[BT_SCO].bitsize = 16;
627         machine->codec_info[BT_SCO].is_i2smaster = 1;
628         machine->codec_info[BT_SCO].is_format_dsp = 1;
629
630         /* baseband configuration */
631         machine->codec_info[BASEBAND].bitsize = 16;
632         machine->codec_info[BASEBAND].is_i2smaster = 1;
633         machine->codec_info[BASEBAND].is_format_dsp = 1;
634 #endif
635
636         machine->is_device_bt = 1;
637
638         return 0;
639 }
640
641 static void tegra_aic326x_bt_voice_call_shutdown(
642                                 struct snd_pcm_substream *substream)
643 {
644         struct snd_soc_pcm_runtime *rtd = substream->private_data;
645         struct tegra_aic326x *machine  =
646                         snd_soc_card_get_drvdata(rtd->codec->card);
647
648 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
649         machine->codec_info[BT_SCO].rate = 0;
650         machine->codec_info[BT_SCO].channels = 0;
651 #endif
652
653         machine->is_device_bt = 0;
654 }
655
656 static struct snd_soc_ops tegra_aic326x_hifi_ops = {
657         .hw_params = tegra_aic326x_hw_params,
658         .hw_free = tegra_aic326x_hw_free,
659 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
660         .startup = tegra_aic326x_startup,
661         .shutdown = tegra_aic326x_shutdown,
662 #endif
663 };
664
665 static struct snd_soc_ops tegra_aic326x_spdif_ops = {
666         .hw_params = tegra_aic326x_spdif_hw_params,
667         .hw_free = tegra_aic326x_hw_free,
668 };
669
670 static struct snd_soc_ops tegra_aic326x_voice_call_ops = {
671         .hw_params = tegra_aic326x_voice_call_hw_params,
672         .shutdown = tegra_aic326x_voice_call_shutdown,
673         .hw_free = tegra_aic326x_hw_free,
674 };
675
676 static struct snd_soc_ops tegra_aic326x_bt_voice_call_ops = {
677         .hw_params = tegra_aic326x_bt_voice_call_hw_params,
678         .shutdown = tegra_aic326x_bt_voice_call_shutdown,
679         .hw_free = tegra_aic326x_hw_free,
680 };
681
682 static struct snd_soc_ops tegra_aic326x_bt_ops = {
683         .hw_params = tegra_aic326x_bt_hw_params,
684         .hw_free = tegra_aic326x_hw_free,
685 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
686         .startup = tegra_aic326x_startup,
687         .shutdown = tegra_aic326x_shutdown,
688 #endif
689 };
690
691 static struct snd_soc_jack tegra_aic326x_hp_jack;
692
693 #ifdef CONFIG_SWITCH
694 static struct switch_dev aic326x_wired_switch_dev = {
695         .name = "h2w",
696 };
697
698 /* These values are copied from WiredAccessoryObserver */
699 enum headset_state {
700         BIT_NO_HEADSET = 0,
701         BIT_HEADSET = (1 << 0),
702         BIT_HEADSET_NO_MIC = (1 << 1),
703 };
704
705 static int aic326x_headset_switch_notify(struct notifier_block *self,
706         unsigned long action, void *dev)
707 {
708         int state = 0;
709
710         switch (action) {
711         case SND_JACK_HEADPHONE:
712                 state |= BIT_HEADSET_NO_MIC;
713                 break;
714         case SND_JACK_HEADSET:
715                 state |= BIT_HEADSET;
716                 break;
717         default:
718                 state |= BIT_NO_HEADSET;
719         }
720
721         switch_set_state(&aic326x_wired_switch_dev, state);
722
723         return NOTIFY_OK;
724 }
725
726 static struct notifier_block aic326x_headset_switch_nb = {
727         .notifier_call = aic326x_headset_switch_notify,
728 };
729 #else
730 static struct snd_soc_jack_pin tegra_aic326x_hp_jack_pins[] = {
731         {
732                 .pin = "Headphone Jack",
733                 .mask = SND_JACK_HEADPHONE,
734         },
735 };
736 #endif
737
738 static int tegra_aic326x_event_int_spk(struct snd_soc_dapm_widget *w,
739                                         struct snd_kcontrol *k, int event)
740 {
741         struct snd_soc_dapm_context *dapm = w->dapm;
742         struct snd_soc_card *card = dapm->card;
743         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
744         struct tegra_aic326x_platform_data *pdata = machine->pdata;
745
746         if (!(machine->gpio_requested & GPIO_SPKR_EN))
747                 return 0;
748
749         gpio_set_value_cansleep(pdata->gpio_spkr_en,
750                                 SND_SOC_DAPM_EVENT_ON(event));
751
752         return 0;
753 }
754
755 static int tegra_aic326x_event_hp(struct snd_soc_dapm_widget *w,
756                                         struct snd_kcontrol *k, int event)
757 {
758         struct snd_soc_dapm_context *dapm = w->dapm;
759         struct snd_soc_card *card = dapm->card;
760         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
761         struct tegra_aic326x_platform_data *pdata = machine->pdata;
762
763         if (!(machine->gpio_requested & GPIO_HP_MUTE))
764                 return 0;
765
766         gpio_set_value_cansleep(pdata->gpio_hp_mute,
767                                 !SND_SOC_DAPM_EVENT_ON(event));
768
769         return 0;
770 }
771
772 static const struct snd_soc_dapm_widget tegra_aic326x_dapm_widgets[] = {
773         SND_SOC_DAPM_SPK("Int Spk", tegra_aic326x_event_int_spk),
774         SND_SOC_DAPM_HP("Earpiece", NULL),
775         SND_SOC_DAPM_HP("Headphone Jack", tegra_aic326x_event_hp),
776         SND_SOC_DAPM_MIC("Mic Jack", NULL),
777         SND_SOC_DAPM_INPUT("Ext Mic"),
778         SND_SOC_DAPM_LINE("Linein", NULL),
779         SND_SOC_DAPM_MIC("Int Mic", NULL),
780 };
781
782 static const struct snd_soc_dapm_route aic326x_audio_map[] = {
783         {"Int Spk", NULL, "SPKL"},
784         {"Int Spk", NULL, "SPKR"},
785         {"Earpiece", NULL, "RECP"},
786         {"Earpiece", NULL, "RECM"},
787         {"Headphone Jack", NULL, "HPL"},
788         {"Headphone Jack", NULL, "HPR"},
789         /* internal (IN2L/IN2R) mic is stero */
790         {"Mic Bias Ext" ,NULL, "Int Mic"},
791         {"IN2L", NULL, "Mic Bias Ext"},
792         {"Mic Bias Ext" ,NULL, "Int Mic"},
793         {"IN2R", NULL, "Mic Bias Ext"},
794         /* Line in */
795         //{"IN2L", NULL, "Linein"},
796         //{"IN2R", NULL, "Linein"},
797         /* Headset (IN1L) MIC */
798         {"Mic Bias Int" ,NULL, "Mic Jack"},
799         {"CM1L" ,NULL, "Mic Jack"},
800         {"IN1L", NULL, "Mic Bias Int"},
801         {"IN1L", NULL, "CM1L"},
802 };
803
804 static const struct snd_kcontrol_new tegra_aic326x_controls[] = {
805         SOC_DAPM_PIN_SWITCH("Int Spk"),
806         SOC_DAPM_PIN_SWITCH("Earpiece"),
807         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
808         SOC_DAPM_PIN_SWITCH("Mic Jack"),
809         SOC_DAPM_PIN_SWITCH("Ext Mic"),
810         SOC_DAPM_PIN_SWITCH("Linein"),
811         SOC_DAPM_PIN_SWITCH("Int Mic"),
812 };
813
814 static int tegra_aic326x_init(struct snd_soc_pcm_runtime *rtd)
815 {
816         struct snd_soc_codec *codec = rtd->codec;
817         struct snd_soc_dapm_context *dapm = &codec->dapm;
818         struct snd_soc_card *card = codec->card;
819         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
820         struct tegra_aic326x_platform_data *pdata = machine->pdata;
821 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
822         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
823 #endif
824         int ret;
825
826 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
827         if (machine->codec_info[BASEBAND].i2s_id != -1)
828                 i2s->is_dam_used = true;
829 #endif
830
831         if (machine->init_done)
832                 return 0;
833
834         machine->init_done = true;
835
836         if (machine_is_whistler()) {
837                 machine->audio_reg = regulator_get(NULL, "avddio_audio");
838                 if (IS_ERR(machine->audio_reg)) {
839                         dev_err(card->dev, "cannot get avddio_audio reg\n");
840                         ret = PTR_ERR(machine->audio_reg);
841                         return ret;
842                 }
843
844                 ret = regulator_enable(machine->audio_reg);
845                 if (ret) {
846                         dev_err(card->dev, "cannot enable avddio_audio reg\n");
847                         regulator_put(machine->audio_reg);
848                         machine->audio_reg = NULL;
849                         return ret;
850                 }
851         }
852
853         if (gpio_is_valid(pdata->gpio_spkr_en)) {
854                 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
855                 if (ret) {
856                         dev_err(card->dev, "cannot get spkr_en gpio\n");
857                         return ret;
858                 }
859                 machine->gpio_requested |= GPIO_SPKR_EN;
860
861                 gpio_direction_output(pdata->gpio_spkr_en, 0);
862         }
863
864         if (gpio_is_valid(pdata->gpio_hp_mute)) {
865                 ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
866                 if (ret) {
867                         dev_err(card->dev, "cannot get hp_mute gpio\n");
868                         return ret;
869                 }
870                 machine->gpio_requested |= GPIO_HP_MUTE;
871
872                 gpio_direction_output(pdata->gpio_hp_mute, 0);
873         }
874
875         if (gpio_is_valid(pdata->gpio_int_mic_en)) {
876                 ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
877                 if (ret) {
878                         dev_err(card->dev, "cannot get int_mic_en gpio\n");
879                         return ret;
880                 }
881                 machine->gpio_requested |= GPIO_INT_MIC_EN;
882
883                 /* Disable int mic; enable signal is active-high */
884                 gpio_direction_output(pdata->gpio_int_mic_en, 0);
885         }
886
887         if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
888                 ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
889                 if (ret) {
890                         dev_err(card->dev, "cannot get ext_mic_en gpio\n");
891                         return ret;
892                 }
893                 machine->gpio_requested |= GPIO_EXT_MIC_EN;
894
895                 /* Enable ext mic; enable signal is active-low */
896                 gpio_direction_output(pdata->gpio_ext_mic_en, 0);
897         }
898
899         ret = snd_soc_add_controls(codec, tegra_aic326x_controls,
900                                    ARRAY_SIZE(tegra_aic326x_controls));
901         if (ret < 0)
902                 return ret;
903
904         snd_soc_dapm_new_controls(dapm, tegra_aic326x_dapm_widgets,
905                                         ARRAY_SIZE(tegra_aic326x_dapm_widgets));
906
907         snd_soc_dapm_add_routes(dapm, aic326x_audio_map,
908                                         ARRAY_SIZE(aic326x_audio_map));
909
910         ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
911                         &tegra_aic326x_hp_jack);
912         if (ret < 0)
913                 return ret;
914
915 #ifdef CONFIG_SWITCH
916         snd_soc_jack_notifier_register(&tegra_aic326x_hp_jack,
917                 &aic326x_headset_switch_nb);
918 #else /*gpio based headset detection*/
919         snd_soc_jack_add_pins(&tegra_aic326x_hp_jack,
920                 ARRAY_SIZE(tegra_aic326x_hp_jack_pins),
921                 tegra_aic326x_hp_jack_pins);
922 #endif
923
924         aic326x_headset_detect(codec, &tegra_aic326x_hp_jack,
925                 SND_JACK_HEADSET);
926
927         /* Add call mode switch control */
928         ret = snd_ctl_add(codec->card->snd_card,
929                         snd_ctl_new1(&tegra_aic326x_call_mode_control,
930                                 machine));
931         if (ret < 0)
932                 return ret;
933
934         snd_soc_dapm_nc_pin(dapm, "IN2L");
935         snd_soc_dapm_nc_pin(dapm, "IN2R");
936         snd_soc_dapm_sync(dapm);
937
938         return 0;
939 }
940
941 static struct snd_soc_dai_link tegra_aic326x_dai[] = {
942         [DAI_LINK_HIFI] = {
943                 .name = "AIC3262",
944                 .stream_name = "AIC3262 PCM HIFI",
945                 .codec_name = "aic3262-codec.4-0018",
946                 .platform_name = "tegra-pcm-audio",
947 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
948                 .cpu_dai_name = "tegra20-i2s.0",
949 #endif
950                 .codec_dai_name = "aic3262-asi1",
951                 .init = tegra_aic326x_init,
952                 .ops = &tegra_aic326x_hifi_ops,
953                 },
954         [DAI_LINK_SPDIF] = {
955                 .name = "SPDIF",
956                 .stream_name = "SPDIF PCM",
957                 .codec_name = "spdif-dit.0",
958                 .platform_name = "tegra-pcm-audio",
959 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
960                 .cpu_dai_name = "tegra20-spdif",
961 #else
962                 .cpu_dai_name = "tegra30-spdif",
963 #endif
964                 .codec_dai_name = "dit-hifi",
965                 .ops = &tegra_aic326x_spdif_ops,
966                 },
967         [DAI_LINK_BTSCO] = {
968                 .name = "BT-SCO",
969                 .stream_name = "BT SCO PCM",
970                 .codec_name = "spdif-dit.1",
971                 .platform_name = "tegra-pcm-audio",
972 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
973                 .cpu_dai_name = "tegra20-i2s.1",
974 #endif
975                 .codec_dai_name = "dit-hifi",
976                 .ops = &tegra_aic326x_bt_ops,
977                 },
978         [DAI_LINK_VOICE_CALL] = {
979                         .name = "VOICE CALL",
980                         .stream_name = "VOICE CALL PCM",
981                         .codec_name = "aic3262-codec.4-0018",
982                         .platform_name = "tegra-pcm-audio",
983                         .cpu_dai_name = "dit-hifi",
984                         .codec_dai_name = "aic3262-asi2",
985                         .ops = &tegra_aic326x_voice_call_ops,
986                 },
987         [DAI_LINK_BT_VOICE_CALL] = {
988                         .name = "BT VOICE CALL",
989                         .stream_name = "BT VOICE CALL PCM",
990                         .codec_name = "spdif-dit.2",
991                         .platform_name = "tegra-pcm-audio",
992                         .cpu_dai_name = "dit-hifi",
993                         .codec_dai_name = "dit-hifi",
994                         .ops = &tegra_aic326x_bt_voice_call_ops,
995                 },
996 };
997
998 static struct snd_soc_card snd_soc_tegra_aic326x = {
999         .name = "tegra-aic326x",
1000         .dai_link = tegra_aic326x_dai,
1001         .num_links = ARRAY_SIZE(tegra_aic326x_dai),
1002 };
1003
1004 static __devinit int tegra_aic326x_driver_probe(struct platform_device *pdev)
1005 {
1006         struct snd_soc_card *card = &snd_soc_tegra_aic326x;
1007         struct tegra_aic326x *machine;
1008         struct tegra_aic326x_platform_data *pdata;
1009         int ret, i;
1010
1011         pdata = pdev->dev.platform_data;
1012         if (!pdata) {
1013                 dev_err(&pdev->dev, "No platform data supplied\n");
1014                 return -EINVAL;
1015         }
1016
1017         machine = kzalloc(sizeof(struct tegra_aic326x), GFP_KERNEL);
1018         if (!machine) {
1019                 dev_err(&pdev->dev, "Can't allocate tegra_aic326x struct\n");
1020                 return -ENOMEM;
1021         }
1022
1023         machine->pdata = pdata;
1024
1025         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
1026         if (ret)
1027                 goto err_free_machine;
1028
1029         card->dev = &pdev->dev;
1030         platform_set_drvdata(pdev, card);
1031         snd_soc_card_set_drvdata(card, machine);
1032
1033 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1034         for (i = 0; i < NUM_I2S_DEVICES ; i++)
1035                 machine->codec_info[i].i2s_id = pdata->audio_port_id[i];
1036
1037         machine->codec_info[BASEBAND].rate = pdata->baseband_param.rate;
1038         machine->codec_info[BASEBAND].channels = pdata->baseband_param.channels;
1039
1040         tegra_aic326x_dai[DAI_LINK_HIFI].cpu_dai_name =
1041         tegra_aic326x_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1042
1043         tegra_aic326x_dai[DAI_LINK_BTSCO].cpu_dai_name =
1044         tegra_aic326x_i2s_dai_name[machine->codec_info[BT_SCO].i2s_id];
1045 #endif
1046
1047         ret = snd_soc_register_card(card);
1048         if (ret) {
1049                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
1050                         ret);
1051                 goto err_fini_utils;
1052         }
1053
1054         if (!card->instantiated) {
1055                 dev_err(&pdev->dev, "No TI AIC3262 codec\n");
1056                 goto err_unregister_card;
1057         }
1058
1059 #ifdef CONFIG_SWITCH
1060         /* Add h2w switch class support */
1061         ret = switch_dev_register(&aic326x_wired_switch_dev);
1062         if (ret < 0) {
1063                 dev_err(&pdev->dev, "not able to register switch device %d\n",
1064                         ret);
1065                 goto err_unregister_card;
1066         }
1067 #endif
1068
1069         return 0;
1070
1071 err_unregister_card:
1072         snd_soc_unregister_card(card);
1073 err_fini_utils:
1074         tegra_asoc_utils_fini(&machine->util_data);
1075 err_free_machine:
1076         kfree(machine);
1077         return ret;
1078 }
1079
1080 static int __devexit tegra_aic326x_driver_remove(struct platform_device *pdev)
1081 {
1082         struct snd_soc_card *card = platform_get_drvdata(pdev);
1083         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1084         struct tegra_aic326x_platform_data *pdata = machine->pdata;
1085
1086         snd_soc_unregister_card(card);
1087
1088 #ifdef CONFIG_SWITCH
1089         switch_dev_unregister(&aic326x_wired_switch_dev);
1090 #endif
1091
1092         tegra_asoc_utils_fini(&machine->util_data);
1093
1094         if (machine->gpio_requested & GPIO_EXT_MIC_EN)
1095                 gpio_free(pdata->gpio_ext_mic_en);
1096         if (machine->gpio_requested & GPIO_INT_MIC_EN)
1097                 gpio_free(pdata->gpio_int_mic_en);
1098         if (machine->gpio_requested & GPIO_HP_MUTE)
1099                 gpio_free(pdata->gpio_hp_mute);
1100         if (machine->gpio_requested & GPIO_SPKR_EN)
1101                 gpio_free(pdata->gpio_spkr_en);
1102
1103         kfree(machine);
1104
1105         return 0;
1106 }
1107
1108 static struct platform_driver tegra_aic326x_driver = {
1109         .driver = {
1110                 .name = DRV_NAME,
1111                 .owner = THIS_MODULE,
1112                 .pm = &snd_soc_pm_ops,
1113         },
1114         .probe = tegra_aic326x_driver_probe,
1115         .remove = __devexit_p(tegra_aic326x_driver_remove),
1116 };
1117
1118 static int __init tegra_aic326x_modinit(void)
1119 {
1120         return platform_driver_register(&tegra_aic326x_driver);
1121 }
1122 module_init(tegra_aic326x_modinit);
1123
1124 static void __exit tegra_aic326x_modexit(void)
1125 {
1126         platform_driver_unregister(&tegra_aic326x_driver);
1127 }
1128 module_exit(tegra_aic326x_modexit);
1129
1130 /* Module information */
1131 MODULE_AUTHOR("Vinod G. <vinodg@nvidia.com>");
1132 MODULE_DESCRIPTION("Tegra+AIC3262 machine ASoC driver");
1133 MODULE_DESCRIPTION("Tegra ALSA SoC");
1134 MODULE_LICENSE("GPL");
1135