d04d6ee053ce49d8c6aefb6e92d56ff7694d000e
[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_INPUT("Int Mic"),
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         {"IN2L", NULL, "Mic Jack"},
790         /*TODO correct */
791         /* external mic is stero */
792         {"IN2L", NULL, "Ext Mic"},
793         {"IN2R", NULL, "Ext Mic"},
794         /* Line in */
795         {"IN2L", NULL, "Linein"},
796         {"IN2R", NULL, "Linein"},
797         /* Internal MIC */
798         {"IN1L", NULL, "Int Mic"},
799 };
800
801 static const struct snd_kcontrol_new tegra_aic326x_controls[] = {
802         SOC_DAPM_PIN_SWITCH("Int Spk"),
803         SOC_DAPM_PIN_SWITCH("Earpiece"),
804         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
805         SOC_DAPM_PIN_SWITCH("Mic Jack"),
806         SOC_DAPM_PIN_SWITCH("Ext Mic"),
807         SOC_DAPM_PIN_SWITCH("Linein"),
808         SOC_DAPM_PIN_SWITCH("Int Mic"),
809 };
810
811 static int tegra_aic326x_init(struct snd_soc_pcm_runtime *rtd)
812 {
813         struct snd_soc_codec *codec = rtd->codec;
814         struct snd_soc_dapm_context *dapm = &codec->dapm;
815         struct snd_soc_card *card = codec->card;
816         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
817         struct tegra_aic326x_platform_data *pdata = machine->pdata;
818 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
819         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(rtd->cpu_dai);
820 #endif
821         int ret;
822
823 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
824         if (machine->codec_info[BASEBAND].i2s_id != -1)
825                 i2s->is_dam_used = true;
826 #endif
827
828         if (machine->init_done)
829                 return 0;
830
831         machine->init_done = true;
832
833         if (machine_is_whistler()) {
834                 machine->audio_reg = regulator_get(NULL, "avddio_audio");
835                 if (IS_ERR(machine->audio_reg)) {
836                         dev_err(card->dev, "cannot get avddio_audio reg\n");
837                         ret = PTR_ERR(machine->audio_reg);
838                         return ret;
839                 }
840
841                 ret = regulator_enable(machine->audio_reg);
842                 if (ret) {
843                         dev_err(card->dev, "cannot enable avddio_audio reg\n");
844                         regulator_put(machine->audio_reg);
845                         machine->audio_reg = NULL;
846                         return ret;
847                 }
848         }
849
850         if (gpio_is_valid(pdata->gpio_spkr_en)) {
851                 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
852                 if (ret) {
853                         dev_err(card->dev, "cannot get spkr_en gpio\n");
854                         return ret;
855                 }
856                 machine->gpio_requested |= GPIO_SPKR_EN;
857
858                 gpio_direction_output(pdata->gpio_spkr_en, 0);
859         }
860
861         if (gpio_is_valid(pdata->gpio_hp_mute)) {
862                 ret = gpio_request(pdata->gpio_hp_mute, "hp_mute");
863                 if (ret) {
864                         dev_err(card->dev, "cannot get hp_mute gpio\n");
865                         return ret;
866                 }
867                 machine->gpio_requested |= GPIO_HP_MUTE;
868
869                 gpio_direction_output(pdata->gpio_hp_mute, 0);
870         }
871
872         if (gpio_is_valid(pdata->gpio_int_mic_en)) {
873                 ret = gpio_request(pdata->gpio_int_mic_en, "int_mic_en");
874                 if (ret) {
875                         dev_err(card->dev, "cannot get int_mic_en gpio\n");
876                         return ret;
877                 }
878                 machine->gpio_requested |= GPIO_INT_MIC_EN;
879
880                 /* Disable int mic; enable signal is active-high */
881                 gpio_direction_output(pdata->gpio_int_mic_en, 0);
882         }
883
884         if (gpio_is_valid(pdata->gpio_ext_mic_en)) {
885                 ret = gpio_request(pdata->gpio_ext_mic_en, "ext_mic_en");
886                 if (ret) {
887                         dev_err(card->dev, "cannot get ext_mic_en gpio\n");
888                         return ret;
889                 }
890                 machine->gpio_requested |= GPIO_EXT_MIC_EN;
891
892                 /* Enable ext mic; enable signal is active-low */
893                 gpio_direction_output(pdata->gpio_ext_mic_en, 0);
894         }
895
896         ret = snd_soc_add_controls(codec, tegra_aic326x_controls,
897                                    ARRAY_SIZE(tegra_aic326x_controls));
898         if (ret < 0)
899                 return ret;
900
901         snd_soc_dapm_new_controls(dapm, tegra_aic326x_dapm_widgets,
902                                         ARRAY_SIZE(tegra_aic326x_dapm_widgets));
903
904         snd_soc_dapm_add_routes(dapm, aic326x_audio_map,
905                                         ARRAY_SIZE(aic326x_audio_map));
906
907         ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
908                         &tegra_aic326x_hp_jack);
909         if (ret < 0)
910                 return ret;
911
912 #ifdef CONFIG_SWITCH
913         snd_soc_jack_notifier_register(&tegra_aic326x_hp_jack,
914                 &aic326x_headset_switch_nb);
915 #else /*gpio based headset detection*/
916         snd_soc_jack_add_pins(&tegra_aic326x_hp_jack,
917                 ARRAY_SIZE(tegra_aic326x_hp_jack_pins),
918                 tegra_aic326x_hp_jack_pins);
919 #endif
920
921         aic326x_headset_detect(codec, &tegra_aic326x_hp_jack,
922                 SND_JACK_HEADSET);
923
924         /* Add call mode switch control */
925         ret = snd_ctl_add(codec->card->snd_card,
926                         snd_ctl_new1(&tegra_aic326x_call_mode_control,
927                                 machine));
928         if (ret < 0)
929                 return ret;
930
931         snd_soc_dapm_nc_pin(dapm, "IN2L");
932         snd_soc_dapm_nc_pin(dapm, "IN2R");
933         snd_soc_dapm_sync(dapm);
934
935         return 0;
936 }
937
938 static struct snd_soc_dai_link tegra_aic326x_dai[] = {
939         [DAI_LINK_HIFI] = {
940                 .name = "AIC3262",
941                 .stream_name = "AIC3262 PCM HIFI",
942                 .codec_name = "aic3262-codec.4-0018",
943                 .platform_name = "tegra-pcm-audio",
944 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
945                 .cpu_dai_name = "tegra20-i2s.0",
946 #endif
947                 .codec_dai_name = "aic3262-asi1",
948                 .init = tegra_aic326x_init,
949                 .ops = &tegra_aic326x_hifi_ops,
950                 },
951         [DAI_LINK_SPDIF] = {
952                 .name = "SPDIF",
953                 .stream_name = "SPDIF PCM",
954                 .codec_name = "spdif-dit.0",
955                 .platform_name = "tegra-pcm-audio",
956 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
957                 .cpu_dai_name = "tegra20-spdif",
958 #else
959                 .cpu_dai_name = "tegra30-spdif",
960 #endif
961                 .codec_dai_name = "dit-hifi",
962                 .ops = &tegra_aic326x_spdif_ops,
963                 },
964         [DAI_LINK_BTSCO] = {
965                 .name = "BT-SCO",
966                 .stream_name = "BT SCO PCM",
967                 .codec_name = "spdif-dit.1",
968                 .platform_name = "tegra-pcm-audio",
969 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
970                 .cpu_dai_name = "tegra20-i2s.1",
971 #endif
972                 .codec_dai_name = "dit-hifi",
973                 .ops = &tegra_aic326x_bt_ops,
974                 },
975         [DAI_LINK_VOICE_CALL] = {
976                         .name = "VOICE CALL",
977                         .stream_name = "VOICE CALL PCM",
978                         .codec_name = "aic3262-codec.4-0018",
979                         .platform_name = "tegra-pcm-audio",
980                         .cpu_dai_name = "dit-hifi",
981                         .codec_dai_name = "aic3262-asi2",
982                         .ops = &tegra_aic326x_voice_call_ops,
983                 },
984         [DAI_LINK_BT_VOICE_CALL] = {
985                         .name = "BT VOICE CALL",
986                         .stream_name = "BT VOICE CALL PCM",
987                         .codec_name = "spdif-dit.2",
988                         .platform_name = "tegra-pcm-audio",
989                         .cpu_dai_name = "dit-hifi",
990                         .codec_dai_name = "dit-hifi",
991                         .ops = &tegra_aic326x_bt_voice_call_ops,
992                 },
993 };
994
995 static struct snd_soc_card snd_soc_tegra_aic326x = {
996         .name = "tegra-aic326x",
997         .dai_link = tegra_aic326x_dai,
998         .num_links = ARRAY_SIZE(tegra_aic326x_dai),
999 };
1000
1001 static __devinit int tegra_aic326x_driver_probe(struct platform_device *pdev)
1002 {
1003         struct snd_soc_card *card = &snd_soc_tegra_aic326x;
1004         struct tegra_aic326x *machine;
1005         struct tegra_aic326x_platform_data *pdata;
1006         int ret, i;
1007
1008         pdata = pdev->dev.platform_data;
1009         if (!pdata) {
1010                 dev_err(&pdev->dev, "No platform data supplied\n");
1011                 return -EINVAL;
1012         }
1013
1014         machine = kzalloc(sizeof(struct tegra_aic326x), GFP_KERNEL);
1015         if (!machine) {
1016                 dev_err(&pdev->dev, "Can't allocate tegra_aic326x struct\n");
1017                 return -ENOMEM;
1018         }
1019
1020         machine->pdata = pdata;
1021
1022         ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
1023         if (ret)
1024                 goto err_free_machine;
1025
1026         card->dev = &pdev->dev;
1027         platform_set_drvdata(pdev, card);
1028         snd_soc_card_set_drvdata(card, machine);
1029
1030 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
1031         for (i = 0; i < NUM_I2S_DEVICES ; i++)
1032                 machine->codec_info[i].i2s_id = pdata->audio_port_id[i];
1033
1034         machine->codec_info[BASEBAND].rate = pdata->baseband_param.rate;
1035         machine->codec_info[BASEBAND].channels = pdata->baseband_param.channels;
1036
1037         tegra_aic326x_dai[DAI_LINK_HIFI].cpu_dai_name =
1038         tegra_aic326x_i2s_dai_name[machine->codec_info[HIFI_CODEC].i2s_id];
1039
1040         tegra_aic326x_dai[DAI_LINK_BTSCO].cpu_dai_name =
1041         tegra_aic326x_i2s_dai_name[machine->codec_info[BT_SCO].i2s_id];
1042 #endif
1043
1044         ret = snd_soc_register_card(card);
1045         if (ret) {
1046                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
1047                         ret);
1048                 goto err_fini_utils;
1049         }
1050
1051         if (!card->instantiated) {
1052                 dev_err(&pdev->dev, "No TI AIC3262 codec\n");
1053                 goto err_unregister_card;
1054         }
1055
1056 #ifdef CONFIG_SWITCH
1057         /* Add h2w switch class support */
1058         ret = switch_dev_register(&aic326x_wired_switch_dev);
1059         if (ret < 0) {
1060                 dev_err(&pdev->dev, "not able to register switch device %d\n",
1061                         ret);
1062                 goto err_unregister_card;
1063         }
1064 #endif
1065
1066         return 0;
1067
1068 err_unregister_card:
1069         snd_soc_unregister_card(card);
1070 err_fini_utils:
1071         tegra_asoc_utils_fini(&machine->util_data);
1072 err_free_machine:
1073         kfree(machine);
1074         return ret;
1075 }
1076
1077 static int __devexit tegra_aic326x_driver_remove(struct platform_device *pdev)
1078 {
1079         struct snd_soc_card *card = platform_get_drvdata(pdev);
1080         struct tegra_aic326x *machine = snd_soc_card_get_drvdata(card);
1081         struct tegra_aic326x_platform_data *pdata = machine->pdata;
1082
1083         snd_soc_unregister_card(card);
1084
1085 #ifdef CONFIG_SWITCH
1086         switch_dev_unregister(&aic326x_wired_switch_dev);
1087 #endif
1088
1089         tegra_asoc_utils_fini(&machine->util_data);
1090
1091         if (machine->gpio_requested & GPIO_EXT_MIC_EN)
1092                 gpio_free(pdata->gpio_ext_mic_en);
1093         if (machine->gpio_requested & GPIO_INT_MIC_EN)
1094                 gpio_free(pdata->gpio_int_mic_en);
1095         if (machine->gpio_requested & GPIO_HP_MUTE)
1096                 gpio_free(pdata->gpio_hp_mute);
1097         if (machine->gpio_requested & GPIO_SPKR_EN)
1098                 gpio_free(pdata->gpio_spkr_en);
1099
1100         kfree(machine);
1101
1102         return 0;
1103 }
1104
1105 static struct platform_driver tegra_aic326x_driver = {
1106         .driver = {
1107                 .name = DRV_NAME,
1108                 .owner = THIS_MODULE,
1109                 .pm = &snd_soc_pm_ops,
1110         },
1111         .probe = tegra_aic326x_driver_probe,
1112         .remove = __devexit_p(tegra_aic326x_driver_remove),
1113 };
1114
1115 static int __init tegra_aic326x_modinit(void)
1116 {
1117         return platform_driver_register(&tegra_aic326x_driver);
1118 }
1119 module_init(tegra_aic326x_modinit);
1120
1121 static void __exit tegra_aic326x_modexit(void)
1122 {
1123         platform_driver_unregister(&tegra_aic326x_driver);
1124 }
1125 module_exit(tegra_aic326x_modexit);
1126
1127 /* Module information */
1128 MODULE_AUTHOR("Vinod G. <vinodg@nvidia.com>");
1129 MODULE_DESCRIPTION("Tegra+AIC3262 machine ASoC driver");
1130 MODULE_DESCRIPTION("Tegra ALSA SoC");
1131 MODULE_LICENSE("GPL");
1132