Revert "Merge commit 'main-jb-2012.08.03-B4' into t114-0806"
[linux-2.6.git] / sound / soc / tegra / tegra30_i2s.c
1 /*
2  * tegra30_i2s.c - Tegra 30 I2S driver
3  *
4  * Author: Stephen Warren <swarren@nvidia.com>
5  * Copyright (c) 2010-2011, NVIDIA Corporation.
6  *
7  * Based on code copyright/by:
8  *
9  * Copyright (c) 2009-2010, NVIDIA Corporation.
10  * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
11  * Scott Peterson <speterson@nvidia.com>
12  *
13  * Copyright (C) 2010 Google, Inc.
14  * Iliyan Malchev <malchev@google.com>
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * version 2 as published by the Free Software Foundation.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28  * 02110-1301 USA
29  *
30  */
31
32 #include <linux/clk.h>
33 #include <linux/module.h>
34 #include <linux/debugfs.h>
35 #include <linux/device.h>
36 #include <linux/platform_device.h>
37 #include <linux/seq_file.h>
38 #include <linux/slab.h>
39 #include <linux/io.h>
40 #include <linux/delay.h>
41 #include <mach/iomap.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46
47 #include "tegra30_ahub.h"
48 #include "tegra30_dam.h"
49 #include "tegra30_i2s.h"
50
51 #define DRV_NAME "tegra30-i2s"
52
53 static struct tegra30_i2s  i2scont[TEGRA30_NR_I2S_IFC];
54
55 static inline void tegra30_i2s_write(struct tegra30_i2s *i2s, u32 reg, u32 val)
56 {
57 #ifdef CONFIG_PM
58         i2s->reg_cache[reg >> 2] = val;
59 #endif
60         __raw_writel(val, i2s->regs + reg);
61 }
62
63 static inline u32 tegra30_i2s_read(struct tegra30_i2s *i2s, u32 reg)
64 {
65         return __raw_readl(i2s->regs + reg);
66 }
67
68 static void tegra30_i2s_enable_clocks(struct tegra30_i2s *i2s)
69 {
70         tegra30_ahub_enable_clocks();
71         clk_enable(i2s->clk_i2s);
72 }
73
74 static void tegra30_i2s_disable_clocks(struct tegra30_i2s *i2s)
75 {
76         clk_disable(i2s->clk_i2s);
77         tegra30_ahub_disable_clocks();
78 }
79
80 #ifdef CONFIG_DEBUG_FS
81 static int tegra30_i2s_show(struct seq_file *s, void *unused)
82 {
83 #define REG(r) { r, #r }
84         static const struct {
85                 int offset;
86                 const char *name;
87         } regs[] = {
88                 REG(TEGRA30_I2S_CTRL),
89                 REG(TEGRA30_I2S_TIMING),
90                 REG(TEGRA30_I2S_OFFSET),
91                 REG(TEGRA30_I2S_CH_CTRL),
92                 REG(TEGRA30_I2S_SLOT_CTRL),
93                 REG(TEGRA30_I2S_CIF_TX_CTRL),
94                 REG(TEGRA30_I2S_CIF_RX_CTRL),
95                 REG(TEGRA30_I2S_FLOWCTL),
96                 REG(TEGRA30_I2S_TX_STEP),
97                 REG(TEGRA30_I2S_FLOW_STATUS),
98                 REG(TEGRA30_I2S_FLOW_TOTAL),
99                 REG(TEGRA30_I2S_FLOW_OVER),
100                 REG(TEGRA30_I2S_FLOW_UNDER),
101                 REG(TEGRA30_I2S_LCOEF_1_4_0),
102                 REG(TEGRA30_I2S_LCOEF_1_4_1),
103                 REG(TEGRA30_I2S_LCOEF_1_4_2),
104                 REG(TEGRA30_I2S_LCOEF_1_4_3),
105                 REG(TEGRA30_I2S_LCOEF_1_4_4),
106                 REG(TEGRA30_I2S_LCOEF_1_4_5),
107                 REG(TEGRA30_I2S_LCOEF_2_4_0),
108                 REG(TEGRA30_I2S_LCOEF_2_4_1),
109                 REG(TEGRA30_I2S_LCOEF_2_4_2),
110         };
111 #undef REG
112
113         struct tegra30_i2s *i2s = s->private;
114         int i;
115
116         tegra30_i2s_enable_clocks(i2s);
117
118         for (i = 0; i < ARRAY_SIZE(regs); i++) {
119                 u32 val = tegra30_i2s_read(i2s, regs[i].offset);
120                 seq_printf(s, "%s = %08x\n", regs[i].name, val);
121         }
122
123         tegra30_i2s_disable_clocks(i2s);
124
125         return 0;
126 }
127
128 static int tegra30_i2s_debug_open(struct inode *inode, struct file *file)
129 {
130         return single_open(file, tegra30_i2s_show, inode->i_private);
131 }
132
133 static const struct file_operations tegra30_i2s_debug_fops = {
134         .open    = tegra30_i2s_debug_open,
135         .read    = seq_read,
136         .llseek  = seq_lseek,
137         .release = single_release,
138 };
139
140 static void tegra30_i2s_debug_add(struct tegra30_i2s *i2s)
141 {
142         char name[] = DRV_NAME ".0";
143
144         snprintf(name, sizeof(name), DRV_NAME);
145         i2s->debug = debugfs_create_file(name, S_IRUGO, snd_soc_debugfs_root,
146                                                 i2s, &tegra30_i2s_debug_fops);
147 }
148
149 static void tegra30_i2s_debug_remove(struct tegra30_i2s *i2s)
150 {
151         if (i2s->debug)
152                 debugfs_remove(i2s->debug);
153 }
154 #else
155 static inline void tegra30_i2s_debug_add(struct tegra30_i2s *i2s)
156 {
157 }
158
159 static inline void tegra30_i2s_debug_remove(struct tegra30_i2s *i2s)
160 {
161 }
162 #endif
163
164 int tegra30_i2s_startup(struct snd_pcm_substream *substream,
165                         struct snd_soc_dai *dai)
166 {
167         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
168         int ret;
169
170         tegra30_i2s_enable_clocks(i2s);
171
172         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
173                 /* increment the playback ref count */
174                 i2s->playback_ref_count++;
175
176                 ret = tegra30_ahub_allocate_tx_fifo(&i2s->txcif,
177                                         &i2s->playback_dma_data.addr,
178                                         &i2s->playback_dma_data.req_sel);
179                 i2s->playback_dma_data.wrap = 4;
180                 i2s->playback_dma_data.width = 32;
181
182                 if (!i2s->is_dam_used)
183                         tegra30_ahub_set_rx_cif_source(
184                                 TEGRA30_AHUB_RXCIF_I2S0_RX0 + i2s->id,
185                                 i2s->txcif);
186         } else {
187                 ret = tegra30_ahub_allocate_rx_fifo(&i2s->rxcif,
188                                         &i2s->capture_dma_data.addr,
189                                         &i2s->capture_dma_data.req_sel);
190                 i2s->capture_dma_data.wrap = 4;
191                 i2s->capture_dma_data.width = 32;
192                 tegra30_ahub_set_rx_cif_source(i2s->rxcif,
193                                         TEGRA30_AHUB_TXCIF_I2S0_TX0 + i2s->id);
194         }
195
196         tegra30_i2s_disable_clocks(i2s);
197
198         return ret;
199 }
200
201 void tegra30_i2s_shutdown(struct snd_pcm_substream *substream,
202                         struct snd_soc_dai *dai)
203 {
204         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
205
206         tegra30_i2s_enable_clocks(i2s);
207
208         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
209                 if (i2s->playback_ref_count == 1)
210                         tegra30_ahub_unset_rx_cif_source(
211                                 TEGRA30_AHUB_RXCIF_I2S0_RX0 + i2s->id);
212
213                 /* free the apbif dma channel*/
214                 tegra30_ahub_free_tx_fifo(i2s->txcif);
215
216                 /* decrement the playback ref count */
217                 i2s->playback_ref_count--;
218         } else {
219                 tegra30_ahub_unset_rx_cif_source(i2s->rxcif);
220                 tegra30_ahub_free_rx_fifo(i2s->rxcif);
221         }
222
223         tegra30_i2s_disable_clocks(i2s);
224 }
225
226 static int tegra30_i2s_set_fmt(struct snd_soc_dai *dai,
227                                 unsigned int fmt)
228 {
229         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
230
231         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
232         case SND_SOC_DAIFMT_NB_NF:
233                 break;
234         default:
235                 return -EINVAL;
236         }
237
238         i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_MASTER_ENABLE;
239         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
240         case SND_SOC_DAIFMT_CBS_CFS:
241                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_MASTER_ENABLE;
242                 break;
243         case SND_SOC_DAIFMT_CBM_CFM:
244                 break;
245         default:
246                 return -EINVAL;
247         }
248
249         i2s->reg_ctrl &= ~(TEGRA30_I2S_CTRL_FRAME_FORMAT_MASK |
250                                 TEGRA30_I2S_CTRL_LRCK_MASK);
251         i2s->reg_ch_ctrl &= ~TEGRA30_I2S_CH_CTRL_EGDE_CTRL_MASK;
252         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
253         case SND_SOC_DAIFMT_DSP_A:
254                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC;
255                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_R_LOW;
256                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_NEG_EDGE;
257                 break;
258         case SND_SOC_DAIFMT_DSP_B:
259                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC;
260                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_R_LOW;
261                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_POS_EDGE;
262                 break;
263         case SND_SOC_DAIFMT_I2S:
264                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
265                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_L_LOW;
266                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_POS_EDGE;
267                 break;
268         case SND_SOC_DAIFMT_RIGHT_J:
269                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
270                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_R_LOW;
271                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_POS_EDGE;
272                 break;
273         case SND_SOC_DAIFMT_LEFT_J:
274                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
275                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_R_LOW;
276                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_POS_EDGE;
277                 break;
278         default:
279                 return -EINVAL;
280         }
281
282         return 0;
283 }
284
285 static void tegra30_i2s_set_channel_bit_count(struct tegra30_i2s *i2s,
286                                 int i2sclock, int srate)
287 {
288         int sym_bitclk, bitcnt;
289         u32 val;
290
291         bitcnt = (i2sclock / (2 * srate)) - 1;
292         sym_bitclk = !(i2sclock % (2 * srate));
293
294         val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
295
296         if (!sym_bitclk)
297                 val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
298
299         tegra30_i2s_write(i2s, TEGRA30_I2S_TIMING, val);
300 }
301
302 static void tegra30_i2s_set_data_offset(struct tegra30_i2s *i2s)
303 {
304         u32 val;
305         int rx_data_offset = i2s->dsp_config.rx_data_offset;
306         int tx_data_offset = i2s->dsp_config.tx_data_offset;
307
308         val = (rx_data_offset <<
309                                 TEGRA30_I2S_OFFSET_RX_DATA_OFFSET_SHIFT) |
310                         (tx_data_offset <<
311                                 TEGRA30_I2S_OFFSET_TX_DATA_OFFSET_SHIFT);
312
313         tegra30_i2s_write(i2s, TEGRA30_I2S_OFFSET, val);
314 }
315
316 static void tegra30_i2s_set_slot_control(struct tegra30_i2s *i2s, int stream)
317 {
318         u32 val;
319         int tx_mask = i2s->dsp_config.tx_mask;
320         int rx_mask = i2s->dsp_config.rx_mask;
321
322         val = tegra30_i2s_read(i2s, TEGRA30_I2S_SLOT_CTRL);
323         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
324                 val &= ~TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_MASK;
325                 val |= (tx_mask << TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_SHIFT);
326         } else {
327                 val &= ~TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_MASK;
328                 val |= (rx_mask << TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_SHIFT);
329         }
330
331         val &= ~TEGRA30_I2S_SLOT_CTRL_TOTAL_SLOTS_MASK;
332         val |= (i2s->dsp_config.num_slots - 1)
333                         << TEGRA30_I2S_SLOT_CTRL_TOTAL_SLOTS_SHIFT;
334
335         tegra30_i2s_write(i2s, TEGRA30_I2S_SLOT_CTRL, val);
336 }
337
338 static int tegra30_i2s_tdm_setup_clocks(struct device *dev,
339                                 struct tegra30_i2s *i2s, int *i2sclock)
340 {
341         int ret;
342
343         if (i2s->reg_ctrl & TEGRA30_I2S_CTRL_MASTER_ENABLE) {
344
345                 ret = clk_set_parent(i2s->clk_i2s, i2s->clk_pll_a_out0);
346                 if (ret) {
347                         dev_err(dev, "Can't set parent of I2S clock\n");
348                         return ret;
349                 }
350
351                 ret = clk_set_rate(i2s->clk_i2s, *i2sclock);
352                 if (ret) {
353                         dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
354                         return ret;
355                 }
356         } else {
357
358                 ret = clk_set_rate(i2s->clk_i2s_sync, *i2sclock);
359                 if (ret) {
360                         dev_err(dev, "Can't set I2S sync clock rate\n");
361                         return ret;
362                 }
363
364                 ret = clk_set_parent(clk_get_parent(i2s->clk_audio_2x),
365                                                 i2s->clk_i2s_sync);
366                 if (ret) {
367                         dev_err(dev, "Can't set parent of audio2x clock\n");
368                         return ret;
369                 }
370
371                 ret = clk_set_rate(i2s->clk_audio_2x, *i2sclock);
372                 if (ret) {
373                         dev_err(dev, "Can't set audio2x clock rate\n");
374                         return ret;
375                 }
376
377                 ret = clk_set_parent(i2s->clk_i2s, i2s->clk_audio_2x);
378                 if (ret) {
379                         dev_err(dev, "Can't set parent of i2s clock\n");
380                         return ret;
381                 }
382         }
383         return ret;
384 }
385
386
387 static int tegra30_i2s_tdm_hw_params(struct snd_pcm_substream *substream,
388                                 struct snd_pcm_hw_params *params,
389                                 struct snd_soc_dai *dai)
390 {
391         struct device *dev = substream->pcm->card->dev;
392         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
393         u32 val;
394         int i2s_client_ch, i2s_audio_ch;
395         int i2s_audio_bits = 0, i2s_client_bits = 0;
396         int i2sclock, srate;
397         int ret;
398
399         srate = params_rate(params);
400
401         i2sclock = srate *
402                                 i2s->dsp_config.num_slots *
403                                 i2s->dsp_config.slot_width;
404
405         ret = tegra30_i2s_tdm_setup_clocks(dev, i2s, &i2sclock);
406         if (ret)
407                 return -EINVAL;
408
409         /* Run ahub clock greater than i2sclock */
410         tegra30_ahub_clock_set_rate(i2sclock*2);
411
412         tegra30_i2s_enable_clocks(i2s);
413
414         tegra30_i2s_set_channel_bit_count(i2s, i2sclock*2, srate);
415
416         i2s_client_ch = i2s->dsp_config.num_slots;
417         i2s_audio_ch = i2s->dsp_config.num_slots;
418
419         i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_BIT_SIZE_MASK;
420         switch (i2s->dsp_config.slot_width) {
421         case 16:
422                 i2s_audio_bits = TEGRA30_AUDIOCIF_BITS_16;
423                 i2s_client_bits = TEGRA30_AUDIOCIF_BITS_16;
424                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_BIT_SIZE_16;
425                 break;
426         case 32:
427                 i2s_audio_bits = TEGRA30_AUDIOCIF_BITS_32;
428                 i2s_client_bits = TEGRA30_AUDIOCIF_BITS_32;
429                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_BIT_SIZE_32;
430                 break;
431         default:
432                 dev_err(dev, "unknown slot_width %d\n",
433                                 i2s->dsp_config.slot_width);
434                 return -EINVAL;
435         }
436
437         val = (0 << TEGRA30_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
438                         ((i2s_audio_ch - 1) <<
439                          TEGRA30_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
440                         ((i2s_client_ch - 1) <<
441                          TEGRA30_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
442                         (i2s_audio_bits <<
443                          TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_SHIFT) |
444                         (i2s_client_bits <<
445                          TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_SHIFT);
446
447         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
448                 val |= TEGRA30_AUDIOCIF_CTRL_DIRECTION_RX;
449                 tegra30_i2s_write(i2s, TEGRA30_I2S_CIF_RX_CTRL, val);
450
451                 tegra30_ahub_set_tx_cif_channels(i2s->txcif,
452                                                 i2s_audio_ch,
453                                                 i2s_client_ch);
454                 tegra30_ahub_set_tx_cif_bits(i2s->txcif,
455                                                 i2s_audio_bits,
456                                                 i2s_client_bits);
457                 tegra30_ahub_set_tx_fifo_pack_mode(i2s->txcif, 0);
458
459         } else {
460                 val |= TEGRA30_AUDIOCIF_CTRL_DIRECTION_TX;
461                 tegra30_i2s_write(i2s, TEGRA30_I2S_CIF_TX_CTRL, val);
462
463                 tegra30_ahub_set_rx_cif_channels(i2s->rxcif,
464                                                 i2s_audio_ch,
465                                                 i2s_client_ch);
466                 tegra30_ahub_set_rx_cif_bits(i2s->rxcif,
467                                                 i2s_audio_bits,
468                                                 i2s_client_bits);
469                 tegra30_ahub_set_rx_fifo_pack_mode(i2s->rxcif, 0);
470         }
471
472         tegra30_i2s_set_slot_control(i2s, substream->stream);
473
474         tegra30_i2s_set_data_offset(i2s);
475
476         i2s->reg_ch_ctrl &= ~TEGRA30_I2S_CH_CTRL_FSYNC_WIDTH_MASK;
477         i2s->reg_ch_ctrl |= (i2s->dsp_config.slot_width - 1) <<
478                         TEGRA30_I2S_CH_CTRL_FSYNC_WIDTH_SHIFT;
479         tegra30_i2s_write(i2s, TEGRA30_I2S_CH_CTRL, i2s->reg_ch_ctrl);
480
481         tegra30_i2s_disable_clocks(i2s);
482
483         return 0;
484 }
485
486 static int tegra30_i2s_hw_params(struct snd_pcm_substream *substream,
487                                 struct snd_pcm_hw_params *params,
488                                 struct snd_soc_dai *dai)
489 {
490         struct device *dev = substream->pcm->card->dev;
491         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
492         u32 val;
493         int ret, sample_size, srate, i2sclock, bitcnt, sym_bitclk;
494         int i2s_client_ch;
495
496         i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_BIT_SIZE_MASK;
497         switch (params_format(params)) {
498         case SNDRV_PCM_FORMAT_S16_LE:
499                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_BIT_SIZE_16;
500                 sample_size = 16;
501                 break;
502         default:
503                 return -EINVAL;
504         }
505
506         /* TDM mode */
507         if ((i2s->reg_ctrl & TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC) &&
508                 (i2s->dsp_config.slot_width > 2))
509                 return tegra30_i2s_tdm_hw_params(substream, params, dai);
510
511         srate = params_rate(params);
512
513         if (i2s->reg_ctrl & TEGRA30_I2S_CTRL_MASTER_ENABLE) {
514                 i2sclock = srate * params_channels(params) * sample_size;
515
516                 /* Additional "* 4" is needed for FSYNC mode */
517                 if (i2s->reg_ctrl & TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC)
518                         i2sclock *= 4;
519
520 #if defined(CONFIG_ARCH_TEGRA_11x_SOC)
521                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
522                         i2sclock = 13000000;
523                         __raw_writel(((0x3 << 30) | (0x1 << 28) | 0x0),
524                                 IO_ADDRESS(TEGRA_CLK_RESET_BASE)+0x1d8);
525                 } else {
526                         i2sclock = 13000000/2;
527                         __raw_writel(((0x3 << 30) | (0x1 << 28) | 0x1),
528                                 IO_ADDRESS(TEGRA_CLK_RESET_BASE)+0x1d8);
529                 }
530 #else
531                 ret = clk_set_parent(i2s->clk_i2s, i2s->clk_pll_a_out0);
532                 if (ret) {
533                         dev_err(dev, "Can't set parent of I2S clock\n");
534                         return ret;
535                 }
536
537                 ret = clk_set_rate(i2s->clk_i2s, i2sclock);
538                 if (ret) {
539                         dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
540                         return ret;
541                 }
542 #endif
543                 if (i2s->reg_ctrl & TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC) {
544                         bitcnt = (i2sclock / srate) - 1;
545                         sym_bitclk = !(i2sclock % srate);
546                 } else {
547                         bitcnt = (i2sclock / (2 * srate)) - 1;
548                         sym_bitclk = !(i2sclock % (2 * srate));
549                 }
550                 val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
551
552                 if (!sym_bitclk)
553                         val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
554
555                 tegra30_i2s_enable_clocks(i2s);
556
557                 tegra30_i2s_write(i2s, TEGRA30_I2S_TIMING, val);
558         } else {
559                 i2sclock = srate * params_channels(params) * sample_size;
560
561                 /* Additional "* 2" is needed for FSYNC mode */
562                 if (i2s->reg_ctrl & TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC)
563                         i2sclock *= 2;
564
565                 ret = clk_set_rate(i2s->clk_i2s_sync, i2sclock);
566                 if (ret) {
567                         dev_err(dev, "Can't set I2S sync clock rate\n");
568                         return ret;
569                 }
570
571                 ret = clk_set_rate(i2s->clk_audio_2x, i2sclock);
572                 if (ret) {
573                         dev_err(dev, "Can't set I2S sync clock rate\n");
574                         return ret;
575                 }
576
577                 ret = clk_set_parent(i2s->clk_i2s, i2s->clk_audio_2x);
578                 if (ret) {
579                         dev_err(dev, "Can't set parent of audio2x clock\n");
580                         return ret;
581                 }
582
583                 tegra30_i2s_enable_clocks(i2s);
584         }
585
586         i2s_client_ch = (i2s->reg_ctrl & TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC) ?
587                         params_channels(params) : 2;
588
589         val = (0 << TEGRA30_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
590               ((params_channels(params) - 1) <<
591                         TEGRA30_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
592               ((i2s_client_ch - 1) <<
593                         TEGRA30_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
594               TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_16 |
595               TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_16;
596
597         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
598                 val |= TEGRA30_AUDIOCIF_CTRL_DIRECTION_RX;
599                 tegra30_i2s_write(i2s, TEGRA30_I2S_CIF_RX_CTRL, val);
600
601                 tegra30_ahub_set_tx_cif_channels(i2s->txcif,
602                                                  params_channels(params),
603                                                  params_channels(params));
604         } else {
605                 val |= TEGRA30_AUDIOCIF_CTRL_DIRECTION_TX;
606                 tegra30_i2s_write(i2s, TEGRA30_I2S_CIF_TX_CTRL, val);
607
608                 tegra30_ahub_set_rx_cif_channels(i2s->rxcif,
609                                                  params_channels(params),
610                                                  params_channels(params));
611         }
612
613         val = (1 << TEGRA30_I2S_OFFSET_RX_DATA_OFFSET_SHIFT) |
614               (1 << TEGRA30_I2S_OFFSET_TX_DATA_OFFSET_SHIFT);
615         tegra30_i2s_write(i2s, TEGRA30_I2S_OFFSET, val);
616
617         tegra30_i2s_write(i2s, TEGRA30_I2S_CH_CTRL, i2s->reg_ch_ctrl);
618
619         val = tegra30_i2s_read(i2s, TEGRA30_I2S_SLOT_CTRL);
620         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
621                 val &= ~TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_MASK;
622                 val |= (1 << TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_SHIFT);
623         } else {
624                 val &= ~TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_MASK;
625                 val |= (1 << TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_SHIFT);
626         }
627         tegra30_i2s_write(i2s, TEGRA30_I2S_SLOT_CTRL, val);
628
629         tegra30_i2s_disable_clocks(i2s);
630
631         return 0;
632 }
633
634 static void tegra30_i2s_start_playback(struct tegra30_i2s *i2s)
635 {
636         tegra30_ahub_enable_tx_fifo(i2s->txcif);
637         /* if this is the only user of i2s tx then enable it*/
638         if (i2s->playback_ref_count == 1) {
639                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_XFER_EN_TX;
640                 tegra30_i2s_write(i2s, TEGRA30_I2S_CTRL, i2s->reg_ctrl);
641         }
642 }
643
644 static void tegra30_i2s_stop_playback(struct tegra30_i2s *i2s)
645 {
646         int dcnt = 10;
647         /* if this is the only user of i2s tx then disable it*/
648         tegra30_ahub_disable_tx_fifo(i2s->txcif);
649         if (i2s->playback_ref_count == 1) {
650                 i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_XFER_EN_TX;
651                 tegra30_i2s_write(i2s, TEGRA30_I2S_CTRL, i2s->reg_ctrl);
652         }
653         while (tegra30_ahub_tx_fifo_is_enabled(i2s->id) && dcnt--)
654                 udelay(100);
655 }
656
657 static void tegra30_i2s_start_capture(struct tegra30_i2s *i2s)
658 {
659         tegra30_ahub_enable_rx_fifo(i2s->rxcif);
660         if (!i2s->is_call_mode_rec) {
661                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_XFER_EN_RX;
662                 tegra30_i2s_write(i2s, TEGRA30_I2S_CTRL, i2s->reg_ctrl);
663         }
664 }
665
666 static void tegra30_i2s_stop_capture(struct tegra30_i2s *i2s)
667 {
668         int dcnt = 10;
669         tegra30_ahub_disable_rx_fifo(i2s->rxcif);
670         if (!i2s->is_call_mode_rec) {
671                 i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_XFER_EN_RX;
672                 tegra30_i2s_write(i2s, TEGRA30_I2S_CTRL, i2s->reg_ctrl);
673         }
674         while (tegra30_ahub_rx_fifo_is_enabled(i2s->id) && dcnt--)
675                 udelay(100);
676 }
677
678 static int tegra30_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
679                                 struct snd_soc_dai *dai)
680 {
681         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
682
683         switch (cmd) {
684         case SNDRV_PCM_TRIGGER_START:
685         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
686         case SNDRV_PCM_TRIGGER_RESUME:
687                 tegra30_i2s_enable_clocks(i2s);
688                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
689                         tegra30_i2s_start_playback(i2s);
690                 else
691                         tegra30_i2s_start_capture(i2s);
692                 break;
693         case SNDRV_PCM_TRIGGER_STOP:
694         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
695         case SNDRV_PCM_TRIGGER_SUSPEND:
696                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
697                         tegra30_i2s_stop_playback(i2s);
698                 else
699                         tegra30_i2s_stop_capture(i2s);
700                 tegra30_i2s_disable_clocks(i2s);
701                 break;
702         default:
703                 return -EINVAL;
704         }
705
706         return 0;
707 }
708
709 static int tegra30_i2s_probe(struct snd_soc_dai *dai)
710 {
711         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
712 #ifdef CONFIG_PM
713         int i;
714 #endif
715
716         dai->capture_dma_data = &i2s->capture_dma_data;
717         dai->playback_dma_data = &i2s->playback_dma_data;
718
719 #ifdef CONFIG_PM
720         tegra30_i2s_enable_clocks(i2s);
721
722         /*cache the POR values of i2s regs*/
723         for (i = 0; i < ((TEGRA30_I2S_CIF_TX_CTRL>>2) + 1); i++)
724                 i2s->reg_cache[i] = tegra30_i2s_read(i2s, i<<2);
725
726         tegra30_i2s_disable_clocks(i2s);
727 #endif
728
729         /* Default values for DSP mode */
730         i2s->dsp_config.num_slots = 1;
731         i2s->dsp_config.slot_width = 2;
732         i2s->dsp_config.tx_mask = 1;
733         i2s->dsp_config.rx_mask = 1;
734         i2s->dsp_config.rx_data_offset = 1;
735         i2s->dsp_config.tx_data_offset = 1;
736
737
738         return 0;
739 }
740
741 int tegra30_i2s_set_tdm_slot(struct snd_soc_dai *cpu_dai,
742                                                         unsigned int tx_mask,
743                                                         unsigned int rx_mask,
744                                                         int slots,
745                                                         int slot_width)
746 {
747         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
748
749         i2s->dsp_config.num_slots = slots;
750         i2s->dsp_config.slot_width = slot_width;
751         i2s->dsp_config.tx_mask = tx_mask;
752         i2s->dsp_config.rx_mask = rx_mask;
753         i2s->dsp_config.rx_data_offset = 0;
754         i2s->dsp_config.tx_data_offset = 0;
755
756         return 0;
757 }
758
759 #ifdef CONFIG_PM
760 int tegra30_i2s_resume(struct snd_soc_dai *cpu_dai)
761 {
762         struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(cpu_dai);
763         int i, ret = 0;
764
765         tegra30_i2s_enable_clocks(i2s);
766
767         /*restore the i2s regs*/
768         for (i = 0; i < ((TEGRA30_I2S_CIF_TX_CTRL>>2) + 1); i++)
769                 tegra30_i2s_write(i2s, i<<2, i2s->reg_cache[i]);
770
771         tegra30_ahub_apbif_resume();
772
773         tegra30_i2s_disable_clocks(i2s);
774
775         if (i2s->dam_ch_refcount)
776                 ret = tegra30_dam_resume(i2s->dam_ifc);
777
778         return ret;
779 }
780 #else
781 #define tegra30_i2s_resume NULL
782 #endif
783
784 static struct snd_soc_dai_ops tegra30_i2s_dai_ops = {
785         .startup        = tegra30_i2s_startup,
786         .shutdown       = tegra30_i2s_shutdown,
787         .set_fmt        = tegra30_i2s_set_fmt,
788         .hw_params      = tegra30_i2s_hw_params,
789         .trigger        = tegra30_i2s_trigger,
790         .set_tdm_slot = tegra30_i2s_set_tdm_slot,
791 };
792
793 #define TEGRA30_I2S_DAI(id) \
794         { \
795                 .name = DRV_NAME "." #id, \
796                 .probe = tegra30_i2s_probe, \
797                 .resume = tegra30_i2s_resume, \
798                 .playback = { \
799                         .channels_min = 1, \
800                         .channels_max = 16, \
801                         .rates = SNDRV_PCM_RATE_8000_96000, \
802                         .formats = SNDRV_PCM_FMTBIT_S16_LE, \
803                 }, \
804                 .capture = { \
805                         .channels_min = 1, \
806                         .channels_max = 16, \
807                         .rates = SNDRV_PCM_RATE_8000_96000, \
808                         .formats = SNDRV_PCM_FMTBIT_S16_LE, \
809                 }, \
810                 .ops = &tegra30_i2s_dai_ops, \
811                 .symmetric_rates = 1, \
812         }
813
814 struct snd_soc_dai_driver tegra30_i2s_dai[] = {
815         TEGRA30_I2S_DAI(0),
816         TEGRA30_I2S_DAI(1),
817         TEGRA30_I2S_DAI(2),
818         TEGRA30_I2S_DAI(3),
819         TEGRA30_I2S_DAI(4),
820 };
821
822 static int configure_baseband_i2s(struct tegra30_i2s  *i2s, int is_i2smaster,
823                 int is_formatdsp, int channels, int rate, int bitsize)
824 {
825         u32 val;
826         int i2sclock, bitcnt, ret;
827
828         i2sclock = rate * channels * bitsize * 2;
829
830         /* additional 8 for baseband */
831         if (is_formatdsp)
832                 i2sclock *= 8;
833
834         if (is_i2smaster) {
835                 ret = clk_set_parent(i2s->clk_i2s, i2s->clk_pll_a_out0);
836                 if (ret) {
837                         pr_err("Can't set parent of I2S clock\n");
838                         return ret;
839                 }
840
841                 ret = clk_set_rate(i2s->clk_i2s, i2sclock);
842                 if (ret) {
843                         pr_err("Can't set I2S clock rate: %d\n", ret);
844                         return ret;
845                 }
846         } else {
847                 ret = clk_set_rate(i2s->clk_i2s_sync, i2sclock);
848                 if (ret) {
849                         pr_err("Can't set I2S sync clock rate\n");
850                         return ret;
851                 }
852
853                 ret = clk_set_rate(i2s->clk_audio_2x, i2sclock);
854                 if (ret) {
855                         pr_err("Can't set I2S sync clock rate\n");
856                         return ret;
857                 }
858
859                 ret = clk_set_parent(i2s->clk_i2s, i2s->clk_audio_2x);
860                 if (ret) {
861                         pr_err("Can't set parent of audio2x clock\n");
862                         return ret;
863                 }
864         }
865
866         tegra30_i2s_enable_clocks(i2s);
867
868         i2s->reg_ctrl &= ~(TEGRA30_I2S_CTRL_FRAME_FORMAT_MASK |
869                                         TEGRA30_I2S_CTRL_LRCK_MASK |
870                                         TEGRA30_I2S_CTRL_MASTER_ENABLE);
871         i2s->reg_ch_ctrl &= ~TEGRA30_I2S_CH_CTRL_EGDE_CTRL_MASK;
872
873         i2s->reg_ctrl |= TEGRA30_I2S_CTRL_BIT_SIZE_16;
874
875         if (is_i2smaster)
876                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_MASTER_ENABLE;
877
878         if (is_formatdsp) {
879                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC;
880                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_R_LOW;
881                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_NEG_EDGE;
882         } else {
883                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_FRAME_FORMAT_LRCK;
884                 i2s->reg_ctrl |= TEGRA30_I2S_CTRL_LRCK_L_LOW;
885                 i2s->reg_ch_ctrl |= TEGRA30_I2S_CH_CTRL_EGDE_CTRL_POS_EDGE;
886         }
887
888         tegra30_i2s_write(i2s, TEGRA30_I2S_CH_CTRL, i2s->reg_ch_ctrl);
889
890         val = tegra30_i2s_read(i2s, TEGRA30_I2S_SLOT_CTRL);
891         val &= ~(TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_MASK |
892                 TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_MASK);
893         val |= (1 << TEGRA30_I2S_SLOT_CTRL_TX_SLOT_ENABLES_SHIFT |
894                 1 << TEGRA30_I2S_SLOT_CTRL_RX_SLOT_ENABLES_SHIFT);
895         tegra30_i2s_write(i2s, TEGRA30_I2S_SLOT_CTRL, val);
896
897         val = (1 << TEGRA30_I2S_OFFSET_RX_DATA_OFFSET_SHIFT) |
898               (1 << TEGRA30_I2S_OFFSET_TX_DATA_OFFSET_SHIFT);
899         tegra30_i2s_write(i2s, TEGRA30_I2S_OFFSET, val);
900
901         if (is_formatdsp) {
902                 bitcnt = (i2sclock/rate) - 1;
903                 val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
904                 if (i2sclock % (rate))
905                         val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
906         } else {
907                 bitcnt = (i2sclock/(2*rate)) - 1;
908                 val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
909                 if (i2sclock % (2*rate))
910                         val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
911         }
912
913         tegra30_i2s_write(i2s, TEGRA30_I2S_TIMING, val);
914
915         /* configure the i2s cif*/
916         val = (0 << TEGRA30_AUDIOCIF_CTRL_FIFO_THRESHOLD_SHIFT) |
917               ((channels - 1) << TEGRA30_AUDIOCIF_CTRL_AUDIO_CHANNELS_SHIFT) |
918               ((channels - 1) << TEGRA30_AUDIOCIF_CTRL_CLIENT_CHANNELS_SHIFT) |
919               TEGRA30_AUDIOCIF_CTRL_AUDIO_BITS_16 |
920               TEGRA30_AUDIOCIF_CTRL_CLIENT_BITS_16;
921         val |= TEGRA30_AUDIOCIF_CTRL_DIRECTION_RX;
922         tegra30_i2s_write(i2s, TEGRA30_I2S_CIF_RX_CTRL, val);
923
924         val &= ~TEGRA30_AUDIOCIF_CTRL_DIRECTION_MASK;
925         val |= TEGRA30_AUDIOCIF_CTRL_DIRECTION_TX;
926         tegra30_i2s_write(i2s, TEGRA30_I2S_CIF_TX_CTRL, val);
927
928         return 0;
929 }
930
931 static int configure_dam(struct tegra30_i2s  *i2s, int out_channel,
932                 int out_rate, int out_bitsize, int in_channels,
933                 int in_rate, int in_bitsize)
934 {
935
936         if (!i2s->dam_ch_refcount)
937                 i2s->dam_ifc = tegra30_dam_allocate_controller();
938
939         tegra30_dam_allocate_channel(i2s->dam_ifc, TEGRA30_DAM_CHIN0_SRC);
940         i2s->dam_ch_refcount++;
941         tegra30_dam_enable_clock(i2s->dam_ifc);
942         tegra30_dam_set_samplerate(i2s->dam_ifc, TEGRA30_DAM_CHOUT, out_rate);
943         tegra30_dam_set_samplerate(i2s->dam_ifc, TEGRA30_DAM_CHIN0_SRC,
944                                 in_rate);
945         tegra30_dam_set_gain(i2s->dam_ifc, TEGRA30_DAM_CHIN0_SRC, 0x1000);
946         tegra30_dam_set_acif(i2s->dam_ifc, TEGRA30_DAM_CHIN0_SRC,
947                         in_channels, in_bitsize, 1, 16);
948         tegra30_dam_set_acif(i2s->dam_ifc, TEGRA30_DAM_CHOUT,
949                         out_channel, out_bitsize, out_channel, out_bitsize);
950
951         return 0;
952 }
953
954
955 int tegra30_make_voice_call_connections(struct codec_config *codec_info,
956                                 struct codec_config *bb_info)
957 {
958         struct tegra30_i2s  *codec_i2s;
959         struct tegra30_i2s  *bb_i2s;
960
961         codec_i2s = &i2scont[codec_info->i2s_id];
962         bb_i2s = &i2scont[bb_info->i2s_id];
963
964         /* increment the codec i2s playback ref count */
965         codec_i2s->playback_ref_count++;
966         bb_i2s->playback_ref_count++;
967
968         /*Configure codec i2s*/
969         configure_baseband_i2s(codec_i2s, codec_info->is_i2smaster,
970                 codec_info->is_format_dsp, codec_info->channels,
971                 codec_info->rate, codec_info->bitsize);
972
973         /*Configure bb i2s*/
974         configure_baseband_i2s(bb_i2s, bb_info->is_i2smaster,
975                 bb_info->is_format_dsp, bb_info->channels,
976                 bb_info->rate, bb_info->bitsize);
977
978         /*configure codec dam*/
979         configure_dam(codec_i2s, codec_info->channels,
980                 codec_info->rate, codec_info->bitsize, bb_info->channels,
981                 bb_info->rate, bb_info->bitsize);
982
983         /*configure bb dam*/
984         configure_dam(bb_i2s, bb_info->channels,
985                 bb_info->rate, bb_info->bitsize, codec_info->channels,
986                 codec_info->rate, codec_info->bitsize);
987
988         /*make ahub connections*/
989
990         /* if this is the only user of i2s tx then make ahub i2s rx connection*/
991         if (codec_i2s->playback_ref_count == 1) {
992                 tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_I2S0_RX0 +
993                                 codec_info->i2s_id, TEGRA30_AHUB_TXCIF_DAM0_TX0 +
994                                 codec_i2s->dam_ifc);
995         }
996
997         tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_I2S0_RX0 +
998                         bb_info->i2s_id, TEGRA30_AHUB_TXCIF_DAM0_TX0 +
999                         bb_i2s->dam_ifc);
1000         tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
1001                 (codec_i2s->dam_ifc*2), TEGRA30_AHUB_TXCIF_I2S0_TX0 +
1002                 bb_info->i2s_id);
1003         tegra30_ahub_set_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0 +
1004                 (bb_i2s->dam_ifc*2), TEGRA30_AHUB_TXCIF_I2S0_TX0 +
1005                 codec_info->i2s_id);
1006
1007         /*enable dam and i2s*/
1008         tegra30_dam_enable(codec_i2s->dam_ifc, TEGRA30_DAM_ENABLE,
1009                 TEGRA30_DAM_CHIN0_SRC);
1010         tegra30_dam_enable(bb_i2s->dam_ifc, TEGRA30_DAM_ENABLE,
1011                 TEGRA30_DAM_CHIN0_SRC);
1012
1013         codec_i2s->reg_ctrl |= TEGRA30_I2S_CTRL_XFER_EN_TX;
1014         codec_i2s->reg_ctrl |= TEGRA30_I2S_CTRL_XFER_EN_RX;
1015         tegra30_i2s_write(codec_i2s, TEGRA30_I2S_CTRL,
1016                 codec_i2s->reg_ctrl);
1017         bb_i2s->reg_ctrl |= TEGRA30_I2S_CTRL_XFER_EN_TX;
1018         bb_i2s->reg_ctrl |= TEGRA30_I2S_CTRL_XFER_EN_RX;
1019         tegra30_i2s_write(bb_i2s, TEGRA30_I2S_CTRL,
1020                 bb_i2s->reg_ctrl);
1021
1022         return 0;
1023 }
1024
1025 int tegra30_break_voice_call_connections(struct codec_config *codec_info,
1026                                 struct codec_config *bb_info)
1027 {
1028         struct tegra30_i2s  *codec_i2s;
1029         struct tegra30_i2s  *bb_i2s;
1030         int dcnt = 10;
1031
1032         codec_i2s = &i2scont[codec_info->i2s_id];
1033         bb_i2s = &i2scont[bb_info->i2s_id];
1034
1035         /*Disable Codec I2S RX (TX to ahub)*/
1036         codec_i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_XFER_EN_RX;
1037         tegra30_i2s_write(codec_i2s, TEGRA30_I2S_CTRL, codec_i2s->reg_ctrl);
1038
1039         while (tegra30_ahub_rx_fifo_is_enabled(codec_i2s->id) && dcnt--)
1040                 udelay(100);
1041
1042         dcnt = 10;
1043
1044         /*Disable baseband DAM*/
1045         tegra30_dam_enable(bb_i2s->dam_ifc, TEGRA30_DAM_DISABLE,
1046                         TEGRA30_DAM_CHIN0_SRC);
1047         tegra30_dam_free_channel(bb_i2s->dam_ifc, TEGRA30_DAM_CHIN0_SRC);
1048         bb_i2s->dam_ch_refcount--;
1049         if (!bb_i2s->dam_ch_refcount)
1050                 tegra30_dam_free_controller(bb_i2s->dam_ifc);
1051
1052         /*Disable baseband I2S TX (RX from ahub)*/
1053         bb_i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_XFER_EN_TX;
1054         tegra30_i2s_write(bb_i2s, TEGRA30_I2S_CTRL, bb_i2s->reg_ctrl);
1055
1056         while (tegra30_ahub_tx_fifo_is_enabled(bb_i2s->id) && dcnt--)
1057                 udelay(100);
1058
1059         dcnt = 10;
1060
1061         /*Disable baseband I2S RX (TX to ahub)*/
1062         bb_i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_XFER_EN_RX;
1063         tegra30_i2s_write(bb_i2s, TEGRA30_I2S_CTRL, bb_i2s->reg_ctrl);
1064
1065         while (tegra30_ahub_rx_fifo_is_enabled(bb_i2s->id) && dcnt--)
1066                 udelay(100);
1067
1068         dcnt = 10;
1069
1070         /*Disable Codec DAM*/
1071         tegra30_dam_enable(codec_i2s->dam_ifc,
1072                 TEGRA30_DAM_DISABLE, TEGRA30_DAM_CHIN0_SRC);
1073         tegra30_dam_free_channel(codec_i2s->dam_ifc,
1074                 TEGRA30_DAM_CHIN0_SRC);
1075         codec_i2s->dam_ch_refcount--;
1076         if (!codec_i2s->dam_ch_refcount)
1077                 tegra30_dam_free_controller(codec_i2s->dam_ifc);
1078
1079         /*Disable Codec I2S TX (RX from ahub)*/
1080         if (codec_i2s->playback_ref_count == 1)
1081                         codec_i2s->reg_ctrl &= ~TEGRA30_I2S_CTRL_XFER_EN_TX;
1082
1083         tegra30_i2s_write(codec_i2s, TEGRA30_I2S_CTRL, codec_i2s->reg_ctrl);
1084
1085         while (tegra30_ahub_tx_fifo_is_enabled(codec_i2s->id) && dcnt--)
1086                 udelay(100);
1087
1088         dcnt = 10;
1089
1090         /* Disconnect the ahub connections */
1091         /* If this is the only user of i2s tx then break ahub
1092         i2s rx connection */
1093         if (codec_i2s->playback_ref_count == 1)
1094                 tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_I2S0_RX0
1095                         + codec_info->i2s_id);
1096
1097         tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_I2S0_RX0
1098                                 + bb_info->i2s_id);
1099         tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0
1100                                 + (codec_i2s->dam_ifc*2));
1101         tegra30_ahub_unset_rx_cif_source(TEGRA30_AHUB_RXCIF_DAM0_RX0
1102                                 + (bb_i2s->dam_ifc*2));
1103
1104         /* Decrement the codec and bb i2s playback ref count */
1105         codec_i2s->playback_ref_count--;
1106         bb_i2s->playback_ref_count--;
1107
1108         /* Disable the clocks */
1109         tegra30_i2s_disable_clocks(codec_i2s);
1110         tegra30_i2s_disable_clocks(bb_i2s);
1111         tegra30_dam_disable_clock(codec_i2s->dam_ifc);
1112         tegra30_dam_disable_clock(bb_i2s->dam_ifc);
1113
1114         return 0;
1115 }
1116
1117 static __devinit int tegra30_i2s_platform_probe(struct platform_device *pdev)
1118 {
1119         struct tegra30_i2s *i2s;
1120         struct resource *mem, *memregion;
1121         int ret;
1122
1123         if ((pdev->id < 0) ||
1124                 (pdev->id >= ARRAY_SIZE(tegra30_i2s_dai))) {
1125                 dev_err(&pdev->dev, "ID %d out of range\n", pdev->id);
1126                 return -EINVAL;
1127         }
1128
1129         i2s = &i2scont[pdev->id];
1130         dev_set_drvdata(&pdev->dev, i2s);
1131         i2s->id = pdev->id;
1132
1133         i2s->clk_i2s = clk_get(&pdev->dev, "i2s");
1134         if (IS_ERR(i2s->clk_i2s)) {
1135                 dev_err(&pdev->dev, "Can't retrieve i2s clock\n");
1136                 ret = PTR_ERR(i2s->clk_i2s);
1137                 goto exit;
1138         }
1139         i2s->clk_i2s_sync = clk_get(&pdev->dev, "ext_audio_sync");
1140         if (IS_ERR(i2s->clk_i2s_sync)) {
1141                 dev_err(&pdev->dev, "Can't retrieve i2s_sync clock\n");
1142                 ret = PTR_ERR(i2s->clk_i2s_sync);
1143                 goto err_i2s_clk_put;
1144         }
1145
1146         i2s->clk_audio_2x = clk_get(&pdev->dev, "audio_sync_2x");
1147         if (IS_ERR(i2s->clk_audio_2x)) {
1148                 dev_err(&pdev->dev, "Can't retrieve audio 2x clock\n");
1149                 ret = PTR_ERR(i2s->clk_audio_2x);
1150                 goto err_i2s_sync_clk_put;
1151         }
1152
1153         i2s->clk_pll_a_out0 = clk_get_sys(NULL, "pll_a_out0");
1154         if (IS_ERR(i2s->clk_pll_a_out0)) {
1155                 dev_err(&pdev->dev, "Can't retrieve pll_a_out0 clock\n");
1156                 ret = PTR_ERR(i2s->clk_pll_a_out0);
1157                 goto err_audio_2x_clk_put;
1158         }
1159
1160         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1161         if (!mem) {
1162                 dev_err(&pdev->dev, "No memory resource\n");
1163                 ret = -ENODEV;
1164                 goto err_pll_a_out0_clk_put;
1165         }
1166
1167         memregion = request_mem_region(mem->start, resource_size(mem),
1168                                         DRV_NAME);
1169         if (!memregion) {
1170                 dev_err(&pdev->dev, "Memory region already claimed\n");
1171                 ret = -EBUSY;
1172                 goto err_pll_a_out0_clk_put;
1173         }
1174
1175         i2s->regs = ioremap(mem->start, resource_size(mem));
1176         if (!i2s->regs) {
1177                 dev_err(&pdev->dev, "ioremap failed\n");
1178                 ret = -ENOMEM;
1179                 goto err_release;
1180         }
1181
1182         ret = snd_soc_register_dai(&pdev->dev, &tegra30_i2s_dai[pdev->id]);
1183         if (ret) {
1184                 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
1185                 ret = -ENOMEM;
1186                 goto err_unmap;
1187         }
1188
1189         tegra30_i2s_debug_add(i2s);
1190
1191         return 0;
1192
1193 err_unmap:
1194         iounmap(i2s->regs);
1195 err_release:
1196         release_mem_region(mem->start, resource_size(mem));
1197 err_pll_a_out0_clk_put:
1198         clk_put(i2s->clk_pll_a_out0);
1199 err_audio_2x_clk_put:
1200         clk_put(i2s->clk_audio_2x);
1201 err_i2s_sync_clk_put:
1202         clk_put(i2s->clk_i2s_sync);
1203 err_i2s_clk_put:
1204         clk_put(i2s->clk_i2s);
1205 exit:
1206         return ret;
1207 }
1208
1209 static int __devexit tegra30_i2s_platform_remove(struct platform_device *pdev)
1210 {
1211         struct tegra30_i2s *i2s = dev_get_drvdata(&pdev->dev);
1212         struct resource *res;
1213
1214         snd_soc_unregister_dai(&pdev->dev);
1215
1216         tegra30_i2s_debug_remove(i2s);
1217
1218         iounmap(i2s->regs);
1219
1220         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1221         release_mem_region(res->start, resource_size(res));
1222
1223         clk_put(i2s->clk_pll_a_out0);
1224         clk_put(i2s->clk_audio_2x);
1225         clk_put(i2s->clk_i2s_sync);
1226         clk_put(i2s->clk_i2s);
1227
1228         kfree(i2s);
1229
1230         return 0;
1231 }
1232
1233 static struct platform_driver tegra30_i2s_driver = {
1234         .driver = {
1235                 .name = DRV_NAME,
1236                 .owner = THIS_MODULE,
1237         },
1238         .probe = tegra30_i2s_platform_probe,
1239         .remove = __devexit_p(tegra30_i2s_platform_remove),
1240 };
1241
1242 static int __init snd_tegra30_i2s_init(void)
1243 {
1244         return platform_driver_register(&tegra30_i2s_driver);
1245 }
1246 module_init(snd_tegra30_i2s_init);
1247
1248 static void __exit snd_tegra30_i2s_exit(void)
1249 {
1250         platform_driver_unregister(&tegra30_i2s_driver);
1251 }
1252 module_exit(snd_tegra30_i2s_exit);
1253
1254 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
1255 MODULE_DESCRIPTION("Tegra 30 I2S ASoC driver");
1256 MODULE_LICENSE("GPL");
1257 MODULE_ALIAS("platform:" DRV_NAME);