Merge branch 'linux-tegra-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / tegra_i2s_audio.c
1 /*
2  * arch/arm/mach-tegra/tegra_i2s_audio.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
6  * Author:
7  *      Iliyan Malchev <malchev@google.com>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/miscdevice.h>
23 #include <linux/fs.h>
24 #include <linux/mutex.h>
25 #include <linux/clk.h>
26 #include <linux/interrupt.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/spinlock.h>
30 #include <linux/uaccess.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/dmapool.h>
33 #include <linux/err.h>
34 #include <linux/spi/spi.h>
35 #include <linux/kfifo.h>
36 #include <linux/debugfs.h>
37 #include <linux/completion.h>
38 #include <linux/platform_device.h>
39 #include <linux/device.h>
40 #include <linux/io.h>
41 #include <linux/ktime.h>
42 #include <linux/sysfs.h>
43 #include <linux/pm_qos_params.h>
44 #include <linux/wakelock.h>
45 #include <linux/delay.h>
46 #include <linux/tegra_audio.h>
47 #include <linux/pm.h>
48 #include <mach/dma.h>
49 #include <mach/iomap.h>
50 #include <mach/i2s.h>
51 #include <mach/audio.h>
52 #include <mach/irqs.h>
53
54 #include "clock.h"
55
56 #define PCM_BUFFER_MAX_SIZE_ORDER       (PAGE_SHIFT + 2)
57 #define PCM_BUFFER_DMA_CHUNK_SIZE_ORDER PAGE_SHIFT
58 #define PCM_BUFFER_THRESHOLD_ORDER      (PCM_BUFFER_MAX_SIZE_ORDER - 1)
59 #define PCM_DMA_CHUNK_MIN_SIZE_ORDER    3
60
61 #define PCM_IN_BUFFER_PADDING           (1<<6) /* bytes */
62
63 #define TEGRA_AUDIO_DSP_NONE            0
64 #define TEGRA_AUDIO_DSP_PCM             1
65 #define TEGRA_AUDIO_DSP_NETWORK         2
66 #define TEGRA_AUDIO_DSP_TDM             3
67
68 /* per stream (input/output) */
69 struct audio_stream {
70         int opened;
71         struct mutex lock;
72
73         struct tegra_audio_buf_config buf_config;
74         bool active; /* is DMA or PIO in progress? */
75         void *buffer;
76         dma_addr_t buf_phys;
77         struct kfifo fifo;
78         struct completion fifo_completion;
79         struct scatterlist sg;
80
81         struct tegra_audio_error_counts errors;
82
83         int i2s_fifo_atn_level;
84
85         ktime_t last_dma_ts;
86         struct tegra_dma_channel *dma_chan;
87         bool stop;
88         struct completion stop_completion;
89         spinlock_t dma_req_lock; /* guards dma_has_it */
90         int dma_has_it;
91         struct tegra_dma_req dma_req;
92
93         struct pm_qos_request_list pm_qos;
94         struct wake_lock wake_lock;
95         char wake_lock_name[100];
96 };
97
98 struct i2s_pio_stats {
99         u32 i2s_interrupt_count;
100         u32 tx_fifo_errors;
101         u32 rx_fifo_errors;
102         u32 tx_fifo_written;
103         u32 rx_fifo_read;
104 };
105
106 static const int divs_8000[] = { 5, 6, 6, 5 }; /* 8018.(18) Hz */
107 static const int divs_11025[] = { 4 };
108 static const int divs_22050[] = { 2 };
109 static const int divs_44100[] = { 1 };
110 static const int divs_16000[] = { 2, 3, 3, 3, 3, 3, 3, 2 }; /* 16036.(36) Hz */
111
112 /* per i2s controller */
113 struct audio_driver_state {
114         struct list_head next;
115
116         struct platform_device *pdev;
117         struct tegra_audio_platform_data *pdata;
118         phys_addr_t i2s_phys;
119         unsigned long i2s_base;
120
121         bool using_dma;
122         unsigned long dma_req_sel;
123
124         int irq; /* for pio mode */
125         struct i2s_pio_stats pio_stats;
126         struct tegra_audio_in_config in_config;
127         const int *in_divs;
128         int in_divs_len;
129
130         struct miscdevice misc_out;
131         struct miscdevice misc_out_ctl;
132         struct audio_stream out;
133
134         struct miscdevice misc_in;
135         struct miscdevice misc_in_ctl;
136         struct audio_stream in;
137
138         /* Control for whole I2S (Data format, etc.) */
139         struct miscdevice misc_ctl;
140         unsigned int bit_format;
141 };
142
143 static inline int buf_size(struct audio_stream *s)
144 {
145         return 1 << s->buf_config.size;
146 }
147
148 static inline int chunk_size(struct audio_stream *s)
149 {
150         return 1 << s->buf_config.chunk;
151 }
152
153 static inline int threshold_size(struct audio_stream *s)
154 {
155         return 1 << s->buf_config.threshold;
156 }
157
158 static inline struct audio_driver_state *ads_from_misc_out(struct file *file)
159 {
160         struct miscdevice *m = file->private_data;
161         struct audio_driver_state *ads =
162                         container_of(m, struct audio_driver_state, misc_out);
163         BUG_ON(!ads);
164         return ads;
165 }
166
167 static inline struct audio_driver_state *ads_from_misc_out_ctl(
168                 struct file *file)
169 {
170         struct miscdevice *m = file->private_data;
171         struct audio_driver_state *ads =
172                         container_of(m, struct audio_driver_state,
173                                         misc_out_ctl);
174         BUG_ON(!ads);
175         return ads;
176 }
177
178 static inline struct audio_driver_state *ads_from_misc_in(struct file *file)
179 {
180         struct miscdevice *m = file->private_data;
181         struct audio_driver_state *ads =
182                         container_of(m, struct audio_driver_state, misc_in);
183         BUG_ON(!ads);
184         return ads;
185 }
186
187 static inline struct audio_driver_state *ads_from_misc_in_ctl(
188                 struct file *file)
189 {
190         struct miscdevice *m = file->private_data;
191         struct audio_driver_state *ads =
192                         container_of(m, struct audio_driver_state,
193                                         misc_in_ctl);
194         BUG_ON(!ads);
195         return ads;
196 }
197
198 static inline struct audio_driver_state *ads_from_misc_ctl(
199                 struct file *file)
200 {
201         struct miscdevice *m = file->private_data;
202         struct audio_driver_state *ads =
203                         container_of(m, struct audio_driver_state,
204                                         misc_ctl);
205         BUG_ON(!ads);
206         return ads;
207 }
208
209 static inline struct audio_driver_state *ads_from_out(
210                         struct audio_stream *aos)
211 {
212         return container_of(aos, struct audio_driver_state, out);
213 }
214
215 static inline struct audio_driver_state *ads_from_in(
216                         struct audio_stream *ais)
217 {
218         return container_of(ais, struct audio_driver_state, in);
219 }
220
221 static inline void prevent_suspend(struct audio_stream *as)
222 {
223         pr_debug("%s\n", __func__);
224         wake_lock(&as->wake_lock);
225         pm_qos_update_request(&as->pm_qos, 0);
226 }
227
228 static inline void allow_suspend(struct audio_stream *as)
229 {
230         pr_debug("%s\n", __func__);
231         pm_qos_update_request(&as->pm_qos, PM_QOS_DEFAULT_VALUE);
232         wake_unlock(&as->wake_lock);
233 }
234
235 #define I2S_I2S_FIFO_TX_BUSY    I2S_I2S_STATUS_FIFO1_BSY
236 #define I2S_I2S_FIFO_TX_QS      I2S_I2S_STATUS_QS_FIFO1
237 #define I2S_I2S_FIFO_TX_ERR     I2S_I2S_STATUS_FIFO1_ERR
238
239 #define I2S_I2S_FIFO_RX_BUSY    I2S_I2S_STATUS_FIFO2_BSY
240 #define I2S_I2S_FIFO_RX_QS      I2S_I2S_STATUS_QS_FIFO2
241 #define I2S_I2S_FIFO_RX_ERR     I2S_I2S_STATUS_FIFO2_ERR
242
243 #define I2S_FIFO_ERR (I2S_I2S_STATUS_FIFO1_ERR | I2S_I2S_STATUS_FIFO2_ERR)
244
245 static inline void i2s_writel(unsigned long base, u32 val, u32 reg)
246 {
247         writel(val, base + reg);
248 }
249
250 static inline u32 i2s_readl(unsigned long base, u32 reg)
251 {
252         return readl(base + reg);
253 }
254
255 static inline void i2s_fifo_write(unsigned long base, int fifo, u32 data)
256 {
257         i2s_writel(base, data, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
258 }
259
260 static inline u32 i2s_fifo_read(unsigned long base, int fifo)
261 {
262         return i2s_readl(base, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
263 }
264
265 static int i2s_set_channel_bit_count(unsigned long base,
266                         int sampling, int bitclk)
267 {
268         u32 val;
269         int bitcnt = bitclk / (2 * sampling) - 1;
270
271         if (bitcnt < 0 || bitcnt >= 1<<11) {
272                 pr_err("%s: bit count %d is out of bounds\n", __func__,
273                         bitcnt);
274                 return -EINVAL;
275         }
276
277         val = bitcnt;
278         if (bitclk % (2 * sampling)) {
279                 pr_info("%s: enabling non-symmetric mode\n", __func__);
280                 val |= I2S_I2S_TIMING_NON_SYM_ENABLE;
281         }
282
283         pr_debug("%s: I2S_I2S_TIMING_0 = %08x\n", __func__, val);
284         i2s_writel(base, val, I2S_I2S_TIMING_0);
285         return 0;
286 }
287
288 static void i2s_set_fifo_mode(unsigned long base, int fifo, int tx)
289 {
290         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
291         if (fifo == 0) {
292                 val &= ~I2S_I2S_CTRL_FIFO1_RX_ENABLE;
293                 val |= (!tx) ? I2S_I2S_CTRL_FIFO1_RX_ENABLE : 0;
294         } else {
295                 val &= ~I2S_I2S_CTRL_FIFO2_TX_ENABLE;
296                 val |= tx ? I2S_I2S_CTRL_FIFO2_TX_ENABLE : 0;
297         }
298         i2s_writel(base, val, I2S_I2S_CTRL_0);
299 }
300
301 static int i2s_fifo_set_attention_level(unsigned long base,
302                         int fifo, unsigned level)
303 {
304         u32 val;
305
306         if (level > I2S_FIFO_ATN_LVL_TWELVE_SLOTS) {
307                 pr_err("%s: invalid fifo level selector %d\n", __func__,
308                         level);
309                 return -EINVAL;
310         }
311
312         val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
313
314         if (!fifo) {
315                 val &= ~I2S_I2S_FIFO_SCR_FIFO1_ATN_LVL_MASK;
316                 val |= level << I2S_FIFO1_ATN_LVL_SHIFT;
317         } else {
318                 val &= ~I2S_I2S_FIFO_SCR_FIFO2_ATN_LVL_MASK;
319                 val |= level << I2S_FIFO2_ATN_LVL_SHIFT;
320         }
321
322         i2s_writel(base, val, I2S_I2S_FIFO_SCR_0);
323         return 0;
324 }
325
326 static void i2s_fifo_enable(unsigned long base, int fifo, int on)
327 {
328         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
329         if (!fifo) {
330                 val &= ~I2S_I2S_CTRL_FIFO1_ENABLE;
331                 val |= on ? I2S_I2S_CTRL_FIFO1_ENABLE : 0;
332         } else {
333                 val &= ~I2S_I2S_CTRL_FIFO2_ENABLE;
334                 val |= on ? I2S_I2S_CTRL_FIFO2_ENABLE : 0;
335         }
336
337         i2s_writel(base, val, I2S_I2S_CTRL_0);
338 }
339
340 static bool i2s_is_fifo_enabled(unsigned long base, int fifo)
341 {
342         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
343         if (!fifo)
344                 return !!(val & I2S_I2S_CTRL_FIFO1_ENABLE);
345         return !!(val & I2S_I2S_CTRL_FIFO2_ENABLE);
346 }
347
348 static void i2s_fifo_clear(unsigned long base, int fifo)
349 {
350         u32 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
351         if (!fifo) {
352                 val &= ~I2S_I2S_FIFO_SCR_FIFO1_CLR;
353                 val |= I2S_I2S_FIFO_SCR_FIFO1_CLR;
354 #if 0
355                 /* Per Nvidia, reduces pop on the next run. */
356                 if (!(val & I2S_I2S_CTRL_FIFO1_RX_ENABLE)) {
357                         int cnt = 16;
358                         while (cnt--)
359                                 i2s_writel(base, 0, I2S_I2S_FIFO1_0);
360                 }
361 #endif
362         } else {
363                 val &= ~I2S_I2S_FIFO_SCR_FIFO2_CLR;
364                 val |= I2S_I2S_FIFO_SCR_FIFO2_CLR;
365         }
366
367         i2s_writel(base, val, I2S_I2S_FIFO_SCR_0);
368 }
369
370 static void i2s_set_master(unsigned long base, int master)
371 {
372         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
373         val &= ~I2S_I2S_CTRL_MASTER_ENABLE;
374         val |= master ? I2S_I2S_CTRL_MASTER_ENABLE : 0;
375         i2s_writel(base, val, I2S_I2S_CTRL_0);
376 }
377
378 static int i2s_set_dsp_mode(unsigned long base, unsigned int mode)
379 {
380         u32 val;
381         if (mode > TEGRA_AUDIO_DSP_TDM) {
382                 pr_err("%s: invalid mode %d.\n", __func__, mode);
383                 return -EINVAL;
384         }
385         if (mode == TEGRA_AUDIO_DSP_TDM) {
386                 pr_err("TEGRA_AUDIO_DSP_TDM not implemented.\n");
387                 return -EINVAL;
388         }
389
390         /* Disable unused modes */
391         if (mode != TEGRA_AUDIO_DSP_PCM) {
392                 /* Disable PCM mode */
393                 val = i2s_readl(base, I2S_I2S_PCM_CTRL_0);
394                 val &= ~(I2S_I2S_PCM_CTRL_TRM_MODE|I2S_I2S_PCM_CTRL_RCV_MODE);
395                 i2s_writel(base, val, I2S_I2S_PCM_CTRL_0);
396         }
397         if (mode != TEGRA_AUDIO_DSP_NETWORK) {
398                 /* Disable Network mode */
399                 val = i2s_readl(base, I2S_I2S_NW_CTRL_0);
400                 val &= ~(I2S_I2S_NW_CTRL_TRM_TLPHY_MODE|I2S_I2S_NW_CTRL_RCV_TLPHY_MODE);
401                 i2s_writel(base, val, I2S_I2S_NW_CTRL_0);
402         }
403
404         /* Enable the selected mode. */
405         switch (mode) {
406         case TEGRA_AUDIO_DSP_NETWORK:
407                 /* Set DSP Network (Telephony) Mode */
408                 val = i2s_readl(base, I2S_I2S_NW_CTRL_0);
409                 val |= I2S_I2S_NW_CTRL_TRM_TLPHY_MODE|I2S_I2S_NW_CTRL_RCV_TLPHY_MODE;
410                 i2s_writel(base, val, I2S_I2S_NW_CTRL_0);
411                 break;
412         case TEGRA_AUDIO_DSP_PCM:
413                 /* Set DSP PCM Mode */
414                 val = i2s_readl(base, I2S_I2S_PCM_CTRL_0);
415                 val |= I2S_I2S_PCM_CTRL_TRM_MODE|I2S_I2S_PCM_CTRL_RCV_MODE;
416                 i2s_writel(base, val, I2S_I2S_PCM_CTRL_0);
417                 break;
418         }
419
420         return 0;
421 }
422
423 static int i2s_set_bit_format(unsigned long base, unsigned fmt)
424 {
425         u32 val;
426
427         if (fmt > I2S_BIT_FORMAT_DSP) {
428                 pr_err("%s: invalid bit-format selector %d\n", __func__, fmt);
429                 return -EINVAL;
430         }
431
432         val = i2s_readl(base, I2S_I2S_CTRL_0);
433         val &= ~I2S_I2S_CTRL_BIT_FORMAT_MASK;
434         val |= fmt << I2S_BIT_FORMAT_SHIFT;
435         i2s_writel(base, val, I2S_I2S_CTRL_0);
436         /* For DSP format, select DSP PCM mode. */
437         /* PCM mode and Network Mode slot 0 are effectively identical. */
438         if (fmt == I2S_BIT_FORMAT_DSP)
439                 i2s_set_dsp_mode(base, TEGRA_AUDIO_DSP_PCM);
440         else
441                 i2s_set_dsp_mode(base, TEGRA_AUDIO_DSP_NONE);
442
443         return 0;
444 }
445
446 static int i2s_set_bit_size(unsigned long base, unsigned bit_size)
447 {
448         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
449         val &= ~I2S_I2S_CTRL_BIT_SIZE_MASK;
450
451         if (bit_size > I2S_BIT_SIZE_32) {
452                 pr_err("%s: invalid bit_size selector %d\n", __func__,
453                         bit_size);
454                 return -EINVAL;
455         }
456
457         val |= bit_size << I2S_BIT_SIZE_SHIFT;
458
459         i2s_writel(base, val, I2S_I2S_CTRL_0);
460         return 0;
461 }
462
463 static int i2s_set_fifo_format(unsigned long base, unsigned fmt)
464 {
465         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
466         val &= ~I2S_I2S_CTRL_FIFO_FORMAT_MASK;
467
468         if (fmt > I2S_FIFO_32 && fmt != I2S_FIFO_PACKED) {
469                 pr_err("%s: invalid fmt selector %d\n", __func__, fmt);
470                 return -EINVAL;
471         }
472
473         val |= fmt << I2S_FIFO_SHIFT;
474
475         i2s_writel(base, val, I2S_I2S_CTRL_0);
476         return 0;
477 }
478
479 static void i2s_set_left_right_control_polarity(unsigned long base,
480                 int high_low)
481 {
482         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
483         val &= ~I2S_I2S_CTRL_L_R_CTRL;
484         val |= high_low ? I2S_I2S_CTRL_L_R_CTRL : 0;
485         i2s_writel(base, val, I2S_I2S_CTRL_0);
486 }
487
488 static void i2s_set_fifo_irq_on_err(unsigned long base, int fifo, int on)
489 {
490         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
491         if (!fifo) {
492                 val &= ~I2S_I2S_IE_FIFO1_ERR;
493                 val |= on ? I2S_I2S_IE_FIFO1_ERR : 0;
494         } else {
495                 val &= ~I2S_I2S_IE_FIFO2_ERR;
496                 val |= on ? I2S_I2S_IE_FIFO2_ERR : 0;
497         }
498         i2s_writel(base, val, I2S_I2S_CTRL_0);
499 }
500
501 static void i2s_set_fifo_irq_on_qe(unsigned long base, int fifo, int on)
502 {
503         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
504         if (!fifo) {
505                 val &= ~I2S_I2S_QE_FIFO1;
506                 val |= on ? I2S_I2S_QE_FIFO1 : 0;
507         } else {
508                 val &= ~I2S_I2S_QE_FIFO2;
509                 val |= on ? I2S_I2S_QE_FIFO2 : 0;
510         }
511         i2s_writel(base, val, I2S_I2S_CTRL_0);
512 }
513
514 static void i2s_enable_fifos(unsigned long base, int on)
515 {
516         u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
517         if (on)
518                 val |= I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
519                        I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR;
520         else
521                 val &= ~(I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
522                          I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR);
523
524         i2s_writel(base, val, I2S_I2S_CTRL_0);
525 }
526
527 static inline u32 i2s_get_status(unsigned long base)
528 {
529         return i2s_readl(base, I2S_I2S_STATUS_0);
530 }
531
532 static inline u32 i2s_get_control(unsigned long base)
533 {
534         return i2s_readl(base, I2S_I2S_CTRL_0);
535 }
536
537 static inline void i2s_ack_status(unsigned long base)
538 {
539         return i2s_writel(base, i2s_readl(base, I2S_I2S_STATUS_0),
540                                 I2S_I2S_STATUS_0);
541 }
542
543 static inline u32 i2s_get_fifo_scr(unsigned long base)
544 {
545         return i2s_readl(base, I2S_I2S_FIFO_SCR_0);
546 }
547
548 static inline phys_addr_t i2s_get_fifo_phy_base(unsigned long phy_base,
549                 int fifo)
550 {
551         return phy_base + (fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
552 }
553
554 static inline u32 i2s_get_fifo_full_empty_count(unsigned long base, int fifo)
555 {
556         u32 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
557
558         if (!fifo)
559                 val = val >> I2S_I2S_FIFO_SCR_FIFO1_FULL_EMPTY_COUNT_SHIFT;
560         else
561                 val = val >> I2S_I2S_FIFO_SCR_FIFO2_FULL_EMPTY_COUNT_SHIFT;
562
563         return val & I2S_I2S_FIFO_SCR_FIFO_FULL_EMPTY_COUNT_MASK;
564 }
565
566 static int init_stream_buffer(struct audio_stream *,
567                 struct tegra_audio_buf_config *cfg, unsigned);
568
569 static int setup_dma(struct audio_driver_state *);
570 static void tear_down_dma(struct audio_driver_state *);
571 static int start_dma_playback(struct audio_stream *);
572 static void stop_dma_playback(struct audio_stream *);
573 static int start_dma_recording(struct audio_stream *);
574 static int resume_dma_recording(struct audio_stream *);
575 static void stop_dma_recording(struct audio_stream *);
576
577 static int setup_pio(struct audio_driver_state *);
578 static void tear_down_pio(struct audio_driver_state *);
579 static int start_pio_playback(struct audio_stream *);
580 static void stop_pio_playback(struct audio_stream *);
581 static int start_pio_recording(struct audio_stream *);
582 static void stop_pio_recording(struct audio_stream *);
583
584 struct sound_ops {
585         int (*setup)(struct audio_driver_state *);
586         void (*tear_down)(struct audio_driver_state *);
587         int (*start_playback)(struct audio_stream *);
588         void (*stop_playback)(struct audio_stream *);
589         int (*start_recording)(struct audio_stream *);
590         void (*stop_recording)(struct audio_stream *);
591 };
592
593 static const struct sound_ops dma_sound_ops = {
594         .setup = setup_dma,
595         .tear_down = tear_down_dma,
596         .start_playback = start_dma_playback,
597         .stop_playback = stop_dma_playback,
598         .start_recording = start_dma_recording,
599         .stop_recording = stop_dma_recording,
600 };
601
602 static const struct sound_ops pio_sound_ops = {
603         .setup = setup_pio,
604         .tear_down = tear_down_pio,
605         .start_playback = start_pio_playback,
606         .stop_playback = stop_pio_playback,
607         .start_recording = start_pio_recording,
608         .stop_recording = stop_pio_recording,
609 };
610
611 static const struct sound_ops *sound_ops = &dma_sound_ops;
612
613 static int start_playback(struct audio_stream *aos)
614 {
615         int rc;
616         unsigned long flags;
617         spin_lock_irqsave(&aos->dma_req_lock, flags);
618         pr_debug("%s: starting playback\n", __func__);
619         rc = sound_ops->start_playback(aos);
620         spin_unlock_irqrestore(&aos->dma_req_lock, flags);
621         if (!rc)
622                 prevent_suspend(aos);
623         return rc;
624 }
625
626 static int start_recording_if_necessary(struct audio_stream *ais)
627 {
628         int rc = 0;
629         bool started = false;
630         unsigned long flags;
631
632         spin_lock_irqsave(&ais->dma_req_lock, flags);
633         if (!ais->stop && !kfifo_is_full(&ais->fifo)) {
634                 pr_debug("%s: starting recording\n", __func__);
635                 rc = sound_ops->start_recording(ais);
636                 started = !rc;
637         }
638         spin_unlock_irqrestore(&ais->dma_req_lock, flags);
639         if (started)
640                 prevent_suspend(ais);
641         return rc;
642 }
643
644 static bool stop_playback_if_necessary(struct audio_stream *aos)
645 {
646         unsigned long flags;
647         spin_lock_irqsave(&aos->dma_req_lock, flags);
648         if (kfifo_is_empty(&aos->fifo)) {
649                 sound_ops->stop_playback(aos);
650                 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
651                 allow_suspend(aos);
652                 return true;
653         }
654         spin_unlock_irqrestore(&aos->dma_req_lock, flags);
655
656         return false;
657 }
658
659 static bool stop_recording_if_necessary_nosync(struct audio_stream *ais)
660 {
661         if (ais->stop || kfifo_is_full(&ais->fifo)) {
662                 if (kfifo_is_full(&ais->fifo))
663                         ais->errors.full_empty++;  /* overflow */
664                 sound_ops->stop_recording(ais);
665                 return true;
666         }
667
668         return false;
669 }
670
671 /* playback and recording */
672 static bool wait_till_stopped(struct audio_stream *as)
673 {
674         int rc;
675         pr_debug("%s: wait for completion\n", __func__);
676         rc = wait_for_completion_interruptible(
677                         &as->stop_completion);
678         allow_suspend(as);
679         pr_debug("%s: done: %d\n", __func__, rc);
680         return true;
681 }
682
683 /* Ask for playback and recording to stop.  The _nosync means that
684  * as->lock has to be locked by the caller.
685  */
686 static void request_stop_nosync(struct audio_stream *as)
687 {
688         pr_debug("%s\n", __func__);
689         if (!as->stop) {
690                 as->stop = true;
691                 wait_till_stopped(as);
692                 if (!completion_done(&as->fifo_completion)) {
693                         pr_debug("%s: complete\n", __func__);
694                         complete(&as->fifo_completion);
695                 }
696         }
697         kfifo_reset(&as->fifo);
698         as->active = false; /* applies to recording only */
699         pr_debug("%s: done\n", __func__);
700 }
701
702 static void toggle_dma(struct audio_driver_state *ads)
703 {
704         pr_info("%s: %s\n", __func__, ads->using_dma ? "pio" : "dma");
705         sound_ops->tear_down(ads);
706         sound_ops = ads->using_dma ? &pio_sound_ops : &dma_sound_ops;
707         sound_ops->setup(ads);
708         ads->using_dma = !ads->using_dma;
709 }
710
711 /* DMA */
712
713 static int resume_dma_playback(struct audio_stream *aos);
714
715 static void setup_dma_tx_request(struct tegra_dma_req *req,
716                 struct audio_stream *aos);
717
718 static void setup_dma_rx_request(struct tegra_dma_req *req,
719                 struct audio_stream *ais);
720
721 static int setup_dma(struct audio_driver_state *ads)
722 {
723         int rc;
724         pr_info("%s\n", __func__);
725
726         if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_TX)) {
727                 /* setup audio playback */
728                 ads->out.buf_phys = dma_map_single(&ads->pdev->dev,
729                                         ads->out.buffer,
730                                         1 << PCM_BUFFER_MAX_SIZE_ORDER,
731                                         DMA_TO_DEVICE);
732                 BUG_ON(!ads->out.buf_phys);
733                 setup_dma_tx_request(&ads->out.dma_req, &ads->out);
734                 ads->out.dma_chan =
735                         tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
736                 if (!ads->out.dma_chan) {
737                         pr_err("%s: error allocating output DMA channel: %ld\n",
738                                 __func__, PTR_ERR(ads->out.dma_chan));
739                         rc = -ENODEV;
740                         goto fail_tx;
741                 }
742         }
743
744         if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_RX)) {
745                 /* setup audio recording */
746                 ads->in.buf_phys = dma_map_single(&ads->pdev->dev,
747                                         ads->in.buffer,
748                                         1 << PCM_BUFFER_MAX_SIZE_ORDER,
749                                         DMA_FROM_DEVICE);
750                 BUG_ON(!ads->in.buf_phys);
751                 setup_dma_rx_request(&ads->in.dma_req, &ads->in);
752                 ads->in.dma_chan =
753                         tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
754                 if (!ads->in.dma_chan) {
755                         pr_err("%s: error allocating input DMA channel: %ld\n",
756                                 __func__, PTR_ERR(ads->in.dma_chan));
757                         rc = -ENODEV;
758                         goto fail_rx;
759                 }
760         }
761
762         return 0;
763
764 fail_rx:
765         if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_RX)) {
766                 dma_unmap_single(&ads->pdev->dev, ads->in.buf_phys,
767                         1 << PCM_BUFFER_MAX_SIZE_ORDER, DMA_FROM_DEVICE);
768                 tegra_dma_free_channel(ads->in.dma_chan);
769                 ads->in.dma_chan = 0;
770         }
771 fail_tx:
772         if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_TX)) {
773                 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phys,
774                         1 << PCM_BUFFER_MAX_SIZE_ORDER, DMA_TO_DEVICE);
775                 tegra_dma_free_channel(ads->out.dma_chan);
776                 ads->out.dma_chan = 0;
777         }
778
779         return rc;
780 }
781
782 static void tear_down_dma(struct audio_driver_state *ads)
783 {
784         pr_info("%s\n", __func__);
785
786         tegra_dma_free_channel(ads->out.dma_chan);
787         ads->out.dma_chan = NULL;
788         dma_unmap_single(&ads->pdev->dev, ads->out.buf_phys,
789                                 buf_size(&ads->out),
790                                 DMA_TO_DEVICE);
791         ads->out.buf_phys = 0;
792
793         tegra_dma_free_channel(ads->in.dma_chan);
794         ads->in.dma_chan = NULL;
795         dma_unmap_single(&ads->pdev->dev, ads->in.buf_phys,
796                                 buf_size(&ads->in),
797                                 DMA_FROM_DEVICE);
798         ads->in.buf_phys = 0;
799 }
800
801 static void dma_tx_complete_callback(struct tegra_dma_req *req)
802 {
803         unsigned long flags;
804         struct audio_stream *aos = req->dev;
805         int count = req->bytes_transferred;
806         u64 delta_us;
807         u64 max_delay_us = count * 10000 / (4 * 441);
808
809         pr_debug("%s bytes transferred %d\n", __func__, count);
810
811         aos->dma_has_it = false;
812         delta_us = ktime_to_us(ktime_sub(ktime_get_real(), aos->last_dma_ts));
813
814         if (delta_us > max_delay_us) {
815                 pr_debug("%s: too late by %lld us\n", __func__,
816                         delta_us - max_delay_us);
817                 aos->errors.late_dma++;
818         }
819
820         kfifo_dma_out_finish(&aos->fifo, count);
821         dma_unmap_sg(NULL, &aos->sg, 1, DMA_TO_DEVICE);
822
823         if (!completion_done(&aos->fifo_completion)) {
824                 pr_debug("%s: complete (%d avail)\n", __func__,
825                                 kfifo_avail(&aos->fifo));
826                 complete(&aos->fifo_completion);
827         }
828
829         if (stop_playback_if_necessary(aos)) {
830                 pr_debug("%s: done (stopped)\n", __func__);
831                 if (!completion_done(&aos->stop_completion)) {
832                         pr_debug("%s: signalling stop completion\n", __func__);
833                         complete(&aos->stop_completion);
834                 }
835                 return;
836         }
837
838         spin_lock_irqsave(&aos->dma_req_lock, flags);
839         resume_dma_playback(aos);
840         spin_unlock_irqrestore(&aos->dma_req_lock, flags);
841 }
842
843 static void dma_rx_complete_threshold(struct tegra_dma_req *req)
844 {
845         pr_debug("%s\n", __func__);
846 }
847
848 static void dma_rx_complete_callback(struct tegra_dma_req *req)
849 {
850         unsigned long flags;
851         struct audio_stream *ais = req->dev;
852         int count = req->bytes_transferred;
853
854         spin_lock_irqsave(&ais->dma_req_lock, flags);
855
856         ais->dma_has_it = false;
857
858         pr_debug("%s(%d): transferred %d bytes (%d available in fifo)\n",
859                         __func__,
860                         smp_processor_id(),
861                         count, kfifo_avail(&ais->fifo));
862
863         BUG_ON(kfifo_avail(&ais->fifo) < count);
864         kfifo_dma_in_finish(&ais->fifo, count);
865         dma_unmap_sg(NULL, &ais->sg, 1, DMA_FROM_DEVICE);
866
867         if (!completion_done(&ais->fifo_completion)) {
868                 pr_debug("%s: signalling fifo completion\n", __func__);
869                 complete(&ais->fifo_completion);
870         }
871
872         if (stop_recording_if_necessary_nosync(ais)) {
873                 spin_unlock_irqrestore(&ais->dma_req_lock, flags);
874                 pr_debug("%s: done (stopped)\n", __func__);
875                 if (!completion_done(&ais->stop_completion)) {
876                         pr_debug("%s: signalling stop completion\n", __func__);
877                         complete(&ais->stop_completion);
878                 }
879                 return;
880         }
881
882         pr_debug("%s: resuming dma recording\n", __func__);
883
884         /* This call will fail if we try to set up a DMA request that's
885          * too small.
886          */
887         (void)resume_dma_recording(ais);
888         spin_unlock_irqrestore(&ais->dma_req_lock, flags);
889
890         pr_debug("%s: done\n", __func__);
891 }
892
893 static void setup_dma_tx_request(struct tegra_dma_req *req,
894                 struct audio_stream *aos)
895 {
896         struct audio_driver_state *ads = ads_from_out(aos);
897
898         memset(req, 0, sizeof(*req));
899
900         req->complete = dma_tx_complete_callback;
901         req->dev = aos;
902         req->to_memory = false;
903         req->dest_addr = i2s_get_fifo_phy_base(ads->i2s_phys, I2S_FIFO_TX);
904         req->dest_wrap = 4;
905         if (ads->bit_format == TEGRA_AUDIO_BIT_FORMAT_DSP)
906                 req->dest_bus_width = ads->pdata->dsp_bus_width;
907         else
908                 req->dest_bus_width = ads->pdata->i2s_bus_width;
909         req->source_bus_width = 32;
910         req->source_wrap = 0;
911         req->req_sel = ads->dma_req_sel;
912 }
913
914 static void setup_dma_rx_request(struct tegra_dma_req *req,
915                 struct audio_stream *ais)
916 {
917         struct audio_driver_state *ads = ads_from_in(ais);
918
919         memset(req, 0, sizeof(*req));
920
921         req->complete = dma_rx_complete_callback;
922         req->threshold = dma_rx_complete_threshold;
923         req->dev = ais;
924         req->to_memory = true;
925         req->source_addr = i2s_get_fifo_phy_base(ads->i2s_phys, I2S_FIFO_RX);
926         req->source_wrap = 4;
927         if (ads->bit_format == TEGRA_AUDIO_BIT_FORMAT_DSP)
928                 req->source_bus_width = ads->pdata->dsp_bus_width;
929         else
930                 req->source_bus_width = ads->pdata->i2s_bus_width;
931         req->dest_bus_width = 32;
932         req->dest_wrap = 0;
933         req->req_sel = ads->dma_req_sel;
934 }
935
936 /* Called with aos->dma_req_lock taken. */
937 static int resume_dma_playback(struct audio_stream *aos)
938 {
939         int rc;
940         struct audio_driver_state *ads = ads_from_out(aos);
941         struct tegra_dma_req *req = &aos->dma_req;
942
943         if (aos->dma_has_it) {
944                 pr_debug("%s: playback already in progress\n", __func__);
945                 return -EALREADY;
946         }
947
948         rc = kfifo_dma_out_prepare(&aos->fifo, &aos->sg,
949                         1, kfifo_len(&aos->fifo));
950         /* stop_playback_if_necessary() already checks to see if the fifo is
951          * empty.
952          */
953         BUG_ON(!rc);
954         rc = dma_map_sg(NULL, &aos->sg, 1, DMA_TO_DEVICE);
955         if (rc < 0) {
956                 pr_err("%s: could not map dma memory: %d\n", __func__, rc);
957                 return rc;
958         }
959
960 #if 0
961         i2s_fifo_clear(ads->i2s_base, I2S_FIFO_TX);
962 #endif
963         i2s_fifo_set_attention_level(ads->i2s_base,
964                         I2S_FIFO_TX, aos->i2s_fifo_atn_level);
965
966
967         req->source_addr = sg_dma_address(&aos->sg);
968         req->size = sg_dma_len(&aos->sg);
969         dma_sync_single_for_device(NULL,
970                         req->source_addr, req->size, DMA_TO_DEVICE);
971
972         /* Don't send all the data yet. */
973         if (req->size > chunk_size(aos))
974                 req->size = chunk_size(aos);
975         pr_debug("%s resume playback (%d in fifo, writing %d)\n",
976                         __func__, kfifo_len(&aos->fifo), req->size);
977
978         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
979
980         aos->last_dma_ts = ktime_get_real();
981         rc = tegra_dma_enqueue_req(aos->dma_chan, req);
982         aos->dma_has_it = !rc;
983         if (!aos->dma_has_it)
984                 pr_err("%s: could not enqueue TX DMA req\n", __func__);
985         return rc;
986 }
987
988 /* Called with aos->dma_req_lock taken. */
989 static int start_dma_playback(struct audio_stream *aos)
990 {
991         return resume_dma_playback(aos);
992 }
993
994 /* Called with aos->dma_req_lock taken. */
995 static void stop_dma_playback(struct audio_stream *aos)
996 {
997         int spin = 0;
998         struct audio_driver_state *ads = ads_from_out(aos);
999         pr_debug("%s\n", __func__);
1000         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 0);
1001         while ((i2s_get_status(ads->i2s_base) & I2S_I2S_FIFO_TX_BUSY) &&
1002                         spin < 100) {
1003                 udelay(10);
1004                 if (spin++ > 50)
1005                         pr_info("%s: spin %d\n", __func__, spin);
1006         }
1007         if (spin == 100)
1008                 pr_warn("%s: spinny\n", __func__);
1009 }
1010
1011 /* This function may be called from either interrupt or process context. */
1012 /* Called with ais->dma_req_lock taken. */
1013 static int resume_dma_recording(struct audio_stream *ais)
1014 {
1015         int rc;
1016         struct audio_driver_state *ads = ads_from_in(ais);
1017         struct tegra_dma_req *req = &ais->dma_req;
1018
1019         BUG_ON(kfifo_is_full(&ais->fifo));
1020
1021         if (ais->dma_has_it) {
1022                 pr_debug("%s: recording already in progress\n", __func__);
1023                 return -EALREADY;
1024         }
1025
1026         /* Don't send all the data yet. */
1027         if (req->size > chunk_size(ais))
1028                 req->size = chunk_size(ais);
1029         rc = kfifo_dma_in_prepare(&ais->fifo, &ais->sg, 1,
1030                         kfifo_avail(&ais->fifo));
1031         BUG_ON(!rc);
1032         rc = dma_map_sg(NULL, &ais->sg, 1, DMA_FROM_DEVICE);
1033         if (rc < 0) {
1034                 pr_err("%s: coult not map dma for recording: %d\n",
1035                                 __func__, rc);
1036                 return rc;
1037         }
1038
1039         req->dest_addr = sg_dma_address(&ais->sg);
1040         req->size = round_down(sg_dma_len(&ais->sg), 4);
1041
1042         if (!req->size) {
1043                 pr_err("%s: invalid request size %d\n", __func__, req->size);
1044                 return -EIO;
1045         }
1046
1047         dma_sync_single_for_device(NULL,
1048                         req->dest_addr, req->size, DMA_FROM_DEVICE);
1049
1050         ais->dma_has_it = !tegra_dma_enqueue_req(ais->dma_chan, &ais->dma_req);
1051         if (!ais->dma_has_it) {
1052                 pr_err("%s: could not enqueue RX DMA req\n", __func__);
1053                 return -EINVAL;
1054         }
1055
1056 #if 0
1057         i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1058 #endif
1059         i2s_fifo_set_attention_level(ads->i2s_base,
1060                         I2S_FIFO_RX, ais->i2s_fifo_atn_level);
1061         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1062         return 0;
1063 }
1064
1065 /* Called with ais->dma_req_lock taken. */
1066 static int start_dma_recording(struct audio_stream *ais)
1067 {
1068         pr_debug("%s\n", __func__);
1069         return resume_dma_recording(ais);
1070 }
1071
1072 /* Called with ais->dma_req_lock taken. */
1073 static void stop_dma_recording(struct audio_stream *ais)
1074 {
1075         int spin = 0;
1076         struct audio_driver_state *ads = ads_from_in(ais);
1077         pr_debug("%s\n", __func__);
1078         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1079         i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1080         while ((i2s_get_status(ads->i2s_base) & I2S_I2S_FIFO_RX_BUSY) &&
1081                         spin < 100) {
1082                 udelay(10);
1083                 if (spin++ > 50)
1084                         pr_info("%s: spin %d\n", __func__, spin);
1085         }
1086         if (spin == 100)
1087                 pr_warn("%s: spinny\n", __func__);
1088 }
1089
1090 /* PIO (non-DMA) */
1091
1092 static int setup_pio(struct audio_driver_state *ads)
1093 {
1094         pr_info("%s\n", __func__);
1095         enable_irq(ads->irq);
1096         return 0;
1097 }
1098
1099 static void tear_down_pio(struct audio_driver_state *ads)
1100 {
1101         pr_info("%s\n", __func__);
1102         disable_irq(ads->irq);
1103 }
1104
1105 static int start_pio_playback(struct audio_stream *aos)
1106 {
1107         struct audio_driver_state *ads = ads_from_out(aos);
1108
1109         if (i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_TX)) {
1110                 pr_debug("%s: playback is already in progress\n", __func__);
1111                 return -EALREADY;
1112         }
1113
1114         pr_debug("%s\n", __func__);
1115
1116         i2s_fifo_set_attention_level(ads->i2s_base,
1117                         I2S_FIFO_TX, aos->i2s_fifo_atn_level);
1118 #if 0
1119         i2s_fifo_clear(ads->i2s_base, I2S_FIFO_TX);
1120 #endif
1121
1122         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
1123
1124         i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_TX, 1);
1125         i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_TX, 1);
1126
1127         return 0;
1128 }
1129
1130 static void stop_pio_playback(struct audio_stream *aos)
1131 {
1132         struct audio_driver_state *ads = ads_from_out(aos);
1133
1134         i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_TX, 0);
1135         i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_TX, 0);
1136         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 0);
1137         while (i2s_get_status(ads->i2s_base) & I2S_I2S_FIFO_TX_BUSY)
1138                 /* spin */;
1139
1140         pr_debug("%s: interrupts %d\n", __func__,
1141                         ads->pio_stats.i2s_interrupt_count);
1142         pr_info("%s: sent       %d\n", __func__,
1143                         ads->pio_stats.tx_fifo_written);
1144         pr_info("%s: tx errors  %d\n", __func__,
1145                         ads->pio_stats.tx_fifo_errors);
1146
1147         memset(&ads->pio_stats, 0, sizeof(ads->pio_stats));
1148 }
1149
1150 static int start_pio_recording(struct audio_stream *ais)
1151 {
1152         struct audio_driver_state *ads = ads_from_in(ais);
1153
1154         if (i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_RX)) {
1155                 pr_debug("%s: already started\n", __func__);
1156                 return -EALREADY;
1157         }
1158
1159         pr_debug("%s: start\n", __func__);
1160
1161         i2s_fifo_set_attention_level(ads->i2s_base,
1162                         I2S_FIFO_RX, ais->i2s_fifo_atn_level);
1163 #if 0
1164         i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1165 #endif
1166
1167         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1168
1169         i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_RX, 1);
1170         i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_RX, 1);
1171
1172         return 0;
1173 }
1174
1175 static void stop_pio_recording(struct audio_stream *ais)
1176 {
1177         struct audio_driver_state *ads = ads_from_in(ais);
1178
1179         pr_debug("%s\n", __func__);
1180
1181         i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_RX, 0);
1182         i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_RX, 0);
1183         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1184         i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1185
1186         pr_debug("%s: interrupts %d\n", __func__,
1187                         ads->pio_stats.i2s_interrupt_count);
1188         pr_debug("%s: received   %d\n", __func__,
1189                         ads->pio_stats.rx_fifo_read);
1190         pr_debug("%s: rx errors  %d\n", __func__,
1191                         ads->pio_stats.rx_fifo_errors);
1192
1193         memset(&ads->pio_stats, 0, sizeof(ads->pio_stats));
1194 }
1195
1196 static irqreturn_t i2s_interrupt(int irq, void *data)
1197 {
1198         struct audio_driver_state *ads = data;
1199         u32 status = i2s_get_status(ads->i2s_base);
1200
1201         pr_debug("%s: %08x\n", __func__, status);
1202
1203         ads->pio_stats.i2s_interrupt_count++;
1204
1205         if (status & I2S_I2S_FIFO_TX_ERR)
1206                 ads->pio_stats.tx_fifo_errors++;
1207
1208         if (status & I2S_I2S_FIFO_RX_ERR) {
1209                 ads->pio_stats.rx_fifo_errors++;
1210                 ads->in.errors.full_empty++;
1211         }
1212
1213         if (status & I2S_FIFO_ERR)
1214                 i2s_ack_status(ads->i2s_base);
1215
1216         if (status & I2S_I2S_FIFO_TX_QS) {
1217                 int written;
1218                 int empty;
1219                 int len;
1220                 u16 fifo_buffer[32];
1221
1222                 struct audio_stream *out = &ads->out;
1223
1224                 if (!i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_TX)) {
1225                         pr_debug("%s: tx fifo not enabled, skipping\n",
1226                                 __func__);
1227                         goto check_rx;
1228                 }
1229
1230                 pr_debug("%s tx fifo is ready\n", __func__);
1231
1232                 if (!completion_done(&out->fifo_completion)) {
1233                         pr_debug("%s: tx complete (%d avail)\n", __func__,
1234                                         kfifo_avail(&out->fifo));
1235                         complete(&out->fifo_completion);
1236                 }
1237
1238                 if (stop_playback_if_necessary(out)) {
1239                         pr_debug("%s: done (stopped)\n", __func__);
1240                         if (!completion_done(&out->stop_completion)) {
1241                                 pr_debug("%s: signalling stop completion\n",
1242                                         __func__);
1243                                 complete(&out->stop_completion);
1244                         }
1245                         goto check_rx;
1246                 }
1247
1248                 empty = i2s_get_fifo_full_empty_count(ads->i2s_base,
1249                                 I2S_FIFO_TX);
1250
1251                 len = kfifo_out(&out->fifo, fifo_buffer,
1252                                 empty * sizeof(u16));
1253                 len /= sizeof(u16);
1254
1255                 written = 0;
1256                 while (empty-- && written < len) {
1257                         ads->pio_stats.tx_fifo_written += written * sizeof(u16);
1258                         i2s_fifo_write(ads->i2s_base,
1259                                         I2S_FIFO_TX, fifo_buffer[written++]);
1260                 }
1261
1262                 /* TODO: Should we check to see if we wrote less than the
1263                  * FIFO threshold and adjust it if so?
1264                  */
1265
1266                 if (written) {
1267                         /* start the transaction */
1268                         pr_debug("%s: enabling fifo (%d samples written)\n",
1269                                         __func__, written);
1270                         i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
1271                 }
1272         }
1273
1274 check_rx:
1275         if (status & I2S_I2S_FIFO_RX_QS) {
1276                 int nr;
1277                 int full;
1278
1279                 struct audio_stream *in = &ads->in;
1280
1281                 if (!i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_RX)) {
1282                         pr_debug("%s: rx fifo not enabled, skipping\n",
1283                                 __func__);
1284                         goto done;
1285                 }
1286
1287                 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1288
1289                 full = i2s_get_fifo_full_empty_count(ads->i2s_base,
1290                                 I2S_FIFO_RX);
1291
1292                 pr_debug("%s rx fifo is ready (%d samples)\n", __func__, full);
1293
1294                 nr = full;
1295                 while (nr--) {
1296                         u16 sample = i2s_fifo_read(ads->i2s_base, I2S_FIFO_RX);
1297                         kfifo_in(&in->fifo, &sample, sizeof(sample));
1298                 }
1299
1300                 ads->pio_stats.rx_fifo_read += full * sizeof(u16);
1301
1302                 if (!completion_done(&in->fifo_completion)) {
1303                         pr_debug("%s: rx complete (%d avail)\n", __func__,
1304                                         kfifo_avail(&in->fifo));
1305                         complete(&in->fifo_completion);
1306                 }
1307
1308                 if (stop_recording_if_necessary_nosync(&ads->in)) {
1309                         pr_debug("%s: recording cancelled or fifo full\n",
1310                                 __func__);
1311                         if (!completion_done(&ads->in.stop_completion)) {
1312                                 pr_debug("%s: signalling stop completion\n",
1313                                         __func__);
1314                                 complete(&ads->in.stop_completion);
1315                         }
1316                         goto done;
1317                 }
1318
1319                 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1320         }
1321
1322 done:
1323         pr_debug("%s: done %08x\n", __func__, i2s_get_status(ads->i2s_base));
1324         return IRQ_HANDLED;
1325 }
1326
1327 static ssize_t tegra_audio_write(struct file *file,
1328                 const char __user *buf, size_t size, loff_t *off)
1329 {
1330         ssize_t rc = 0, total = 0;
1331         unsigned nw = 0;
1332
1333         struct audio_driver_state *ads = ads_from_misc_out(file);
1334
1335         mutex_lock(&ads->out.lock);
1336
1337         if (!IS_ALIGNED(size, 4)) {
1338                 pr_err("%s: user size request %d not aligned to 4\n",
1339                         __func__, size);
1340                 rc = -EINVAL;
1341                 goto done;
1342         }
1343
1344         pr_debug("%s: write %d bytes, %d available\n", __func__,
1345                         size, kfifo_avail(&ads->out.fifo));
1346
1347 again:
1348         if (ads->out.stop) {
1349                 pr_info("%s: playback has been cancelled (%d/%d bytes)\n",
1350                                 __func__, total, size);
1351                 goto done;
1352         }
1353
1354         rc = kfifo_from_user(&ads->out.fifo, buf + total, size - total, &nw);
1355         if (rc < 0) {
1356                 pr_err("%s: error copying from user\n", __func__);
1357                 goto done;
1358         }
1359
1360         rc = start_playback(&ads->out);
1361         if (rc < 0 && rc != -EALREADY) {
1362                 pr_err("%s: could not start playback: %d\n", __func__, rc);
1363                 goto done;
1364         }
1365
1366         total += nw;
1367         if (total < size) {
1368                 pr_debug("%s: sleep (user %d total %d nw %d)\n", __func__,
1369                                 size, total, nw);
1370                 mutex_unlock(&ads->out.lock);
1371                 rc = wait_for_completion_interruptible(
1372                                 &ads->out.fifo_completion);
1373                 mutex_lock(&ads->out.lock);
1374                 if (rc == -ERESTARTSYS) {
1375                         pr_warn("%s: interrupted\n", __func__);
1376                         goto done;
1377                 }
1378                 pr_debug("%s: awake\n", __func__);
1379                 goto again;
1380         }
1381
1382         rc = total;
1383         *off += total;
1384
1385 done:
1386         mutex_unlock(&ads->out.lock);
1387         return rc;
1388 }
1389
1390 static long tegra_audio_out_ioctl(struct file *file,
1391                         unsigned int cmd, unsigned long arg)
1392 {
1393         int rc = 0;
1394         struct audio_driver_state *ads = ads_from_misc_out_ctl(file);
1395         struct audio_stream *aos = &ads->out;
1396
1397         mutex_lock(&aos->lock);
1398
1399         switch (cmd) {
1400         case TEGRA_AUDIO_OUT_SET_BUF_CONFIG: {
1401                 struct tegra_audio_buf_config cfg;
1402                 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg))) {
1403                         rc = -EFAULT;
1404                         break;
1405                 }
1406                 if (kfifo_len(&aos->fifo)) {
1407                         pr_err("%s: playback in progress\n", __func__);
1408                         rc = -EBUSY;
1409                         break;
1410                 }
1411                 rc = init_stream_buffer(aos, &cfg, 0);
1412                 if (rc < 0)
1413                         break;
1414                 aos->buf_config = cfg;
1415         }
1416                 break;
1417         case TEGRA_AUDIO_OUT_GET_BUF_CONFIG:
1418                 if (copy_to_user((void __user *)arg, &aos->buf_config,
1419                                 sizeof(aos->buf_config)))
1420                         rc = -EFAULT;
1421                 break;
1422         case TEGRA_AUDIO_OUT_GET_ERROR_COUNT:
1423                 if (copy_to_user((void __user *)arg, &aos->errors,
1424                                 sizeof(aos->errors)))
1425                         rc = -EFAULT;
1426                 if (!rc)
1427                         memset(&aos->errors, 0, sizeof(aos->errors));
1428                 break;
1429         case TEGRA_AUDIO_OUT_FLUSH:
1430                 if (kfifo_len(&aos->fifo)) {
1431                         pr_debug("%s: flushing\n", __func__);
1432                         request_stop_nosync(aos);
1433                         pr_debug("%s: flushed\n", __func__);
1434                 }
1435                 aos->stop = false;
1436                 break;
1437         default:
1438                 rc = -EINVAL;
1439         }
1440
1441         mutex_unlock(&aos->lock);
1442         return rc;
1443 }
1444
1445 static long tegra_audio_ioctl(struct file *file,
1446                         unsigned int cmd, unsigned long arg)
1447 {
1448         int rc = 0;
1449         struct audio_driver_state *ads = ads_from_misc_ctl(file);
1450         unsigned int mode;
1451         bool dma_restart = false;
1452
1453         mutex_lock(&ads->out.lock);
1454         mutex_lock(&ads->in.lock);
1455
1456         switch (cmd) {
1457         case TEGRA_AUDIO_SET_BIT_FORMAT:
1458                 if (copy_from_user(&mode, (const void __user *)arg,
1459                                         sizeof(mode))) {
1460                         rc = -EFAULT;
1461                         goto done;
1462                 }
1463                 dma_restart = (mode != ads->bit_format);
1464                 switch (mode) {
1465                 case TEGRA_AUDIO_BIT_FORMAT_DEFAULT:
1466                         i2s_set_bit_format(ads->i2s_base, ads->pdata->mode);
1467                         ads->bit_format = mode;
1468                         break;
1469                 case TEGRA_AUDIO_BIT_FORMAT_DSP:
1470                         i2s_set_bit_format(ads->i2s_base, I2S_BIT_FORMAT_DSP);
1471                         ads->bit_format = mode;
1472                         break;
1473                 default:
1474                         pr_err("%s: Invald PCM mode %d", __func__, mode);
1475                         rc = -EINVAL;
1476                         goto done;
1477                 }
1478                 break;
1479         case TEGRA_AUDIO_GET_BIT_FORMAT:
1480                 if (copy_to_user((void __user *)arg, &ads->bit_format,
1481                                 sizeof(mode)))
1482                         rc = -EFAULT;
1483                 goto done;
1484         }
1485
1486         if (dma_restart && ads->using_dma) {
1487                 pr_debug("%s: Restarting DMA due to configuration change.\n",
1488                         __func__);
1489                 if (kfifo_len(&ads->out.fifo) || ads->in.active) {
1490                         pr_err("%s: dma busy, cannot restart.\n", __func__);
1491                         rc = -EBUSY;
1492                         goto done;
1493                 }
1494                 sound_ops->tear_down(ads);
1495                 sound_ops->setup(ads);
1496         }
1497
1498 done:
1499         mutex_unlock(&ads->in.lock);
1500         mutex_unlock(&ads->out.lock);
1501         return rc;
1502 }
1503
1504 static long tegra_audio_in_ioctl(struct file *file,
1505                         unsigned int cmd, unsigned long arg)
1506 {
1507         int rc = 0;
1508         struct audio_driver_state *ads = ads_from_misc_in_ctl(file);
1509         struct audio_stream *ais = &ads->in;
1510
1511         mutex_lock(&ais->lock);
1512
1513         switch (cmd) {
1514         case TEGRA_AUDIO_IN_START:
1515                 pr_debug("%s: start recording\n", __func__);
1516                 ais->stop = false;
1517                 rc = start_recording_if_necessary(ais);
1518                 ais->active = !rc || rc == -EALREADY;
1519                 break;
1520         case TEGRA_AUDIO_IN_STOP:
1521                 pr_debug("%s: start recording\n", __func__);
1522                 if (ais->active)
1523                         request_stop_nosync(ais);
1524                 break;
1525         case TEGRA_AUDIO_IN_SET_CONFIG: {
1526                 struct tegra_audio_in_config cfg;
1527
1528                 if (ais->active) {
1529                         pr_err("%s: recording in progress\n", __func__);
1530                         rc = -EBUSY;
1531                         break;
1532                 }
1533                 if (copy_from_user(&cfg, (const void __user *)arg,
1534                                         sizeof(cfg))) {
1535                         rc = -EFAULT;
1536                         break;
1537                 }
1538
1539 #ifdef SAMPLE_RATE_CONVERTER_IN_DRIVER
1540                 switch (cfg.rate) {
1541                 case 8000:
1542                         ads->in_divs = divs_8000;
1543                         ads->in_divs_len = ARRAY_SIZE(divs_8000);
1544                         break;
1545                 case 11025:
1546                         ads->in_divs = divs_11025;
1547                         ads->in_divs_len = ARRAY_SIZE(divs_11025);
1548                         break;
1549                 case 16000:
1550                         ads->in_divs = divs_16000;
1551                         ads->in_divs_len = ARRAY_SIZE(divs_16000);
1552                         break;
1553                 case 22050:
1554                         ads->in_divs = divs_22050;
1555                         ads->in_divs_len = ARRAY_SIZE(divs_22050);
1556                         break;
1557                 case 44100:
1558                         ads->in_divs = divs_44100;
1559                         ads->in_divs_len = ARRAY_SIZE(divs_44100);
1560                         break;
1561                 default:
1562                         pr_err("%s: invalid sampling rate %d\n", __func__,
1563                                 cfg.rate);
1564                         rc = -EINVAL;
1565                         break;
1566                 }
1567 #endif
1568                 if(cfg.stereo && !ads->pdata->stereo_capture) {
1569                         pr_err("%s: not capable of stereo capture.",
1570                                 __func__);
1571                         rc = -EINVAL;
1572                 }
1573                 if (!rc) {
1574                         pr_info("%s: setting input sampling rate to %d, %s\n",
1575                                 __func__, cfg.rate,
1576                                 cfg.stereo ? "stereo" : "mono");
1577                         ads->in_config = cfg;
1578                         ads->in_config.stereo = !!ads->in_config.stereo;
1579                 }
1580         }
1581                 break;
1582         case TEGRA_AUDIO_IN_GET_CONFIG:
1583                 if (copy_to_user((void __user *)arg, &ads->in_config,
1584                                 sizeof(ads->in_config)))
1585                         rc = -EFAULT;
1586                 break;
1587         case TEGRA_AUDIO_IN_SET_BUF_CONFIG: {
1588                 struct tegra_audio_buf_config cfg;
1589                 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg))) {
1590                         rc = -EFAULT;
1591                         break;
1592                 }
1593                 if (ais->active) {
1594                         pr_err("%s: recording in progress\n", __func__);
1595                         rc = -EBUSY;
1596                         break;
1597                 }
1598                 rc = init_stream_buffer(ais, &cfg, PCM_IN_BUFFER_PADDING);
1599                 if (rc < 0)
1600                         break;
1601                 ais->buf_config = cfg;
1602         }
1603                 break;
1604         case TEGRA_AUDIO_IN_GET_BUF_CONFIG:
1605                 if (copy_to_user((void __user *)arg, &ais->buf_config,
1606                                 sizeof(ais->buf_config)))
1607                         rc = -EFAULT;
1608                 break;
1609         case TEGRA_AUDIO_IN_GET_ERROR_COUNT:
1610                 if (copy_to_user((void __user *)arg, &ais->errors,
1611                                 sizeof(ais->errors)))
1612                         rc = -EFAULT;
1613                 if (!rc)
1614                         memset(&ais->errors, 0, sizeof(ais->errors));
1615                 break;
1616         default:
1617                 rc = -EINVAL;
1618         }
1619
1620         mutex_unlock(&ais->lock);
1621         return rc;
1622 }
1623
1624 static ssize_t __i2s_copy_to_user(struct audio_driver_state *ads,
1625                                 void __user *dst, int dst_size,
1626                                 void *src, int src_size,
1627                                 int *num_consumed)
1628 {
1629         int bytes_written = dst_size < src_size ? dst_size : src_size;
1630         *num_consumed = bytes_written;
1631         if (copy_to_user(dst, src, bytes_written)) {
1632                 pr_err("%s: error copying %d bytes to user\n", __func__,
1633                         bytes_written);
1634                 return -EFAULT;
1635         }
1636         return bytes_written;
1637 }
1638
1639 #ifdef SAMPLE_RATE_CONVERTER_IN_DRIVER
1640
1641 /* downsample a 16-bit 44.1kHz PCM stereo stream to stereo or mono 16-bit PCM
1642  * stream.
1643  */
1644
1645 static int downsample(const s16 *in, int in_len,
1646                 s16 *out, int out_len,
1647                 int *consumed, /* from input */
1648                 const int *divs, int divs_len,
1649                 bool out_stereo)
1650 {
1651         /* Todo: Handle mono source streams */
1652         int i, j;
1653         int lsum, rsum;
1654         int di, div;
1655         int oi;
1656
1657         i = 0;
1658         oi = 0;
1659         di = 0;
1660         div = divs[0];
1661         while (i + div * 2 <= in_len && oi + out_stereo < out_len) {
1662                 for (j = 0, lsum = 0, rsum = 0; j < div; j++) {
1663                         lsum += in[i + j * 2];
1664                         rsum += in[i + j * 2 + 1];
1665                 }
1666                 if (!out_stereo)
1667                         out[oi] = (lsum + rsum) / (div * 2);
1668                 else {
1669                         out[oi] = lsum / div;
1670                         out[oi + 1] = rsum / div;
1671                 }
1672
1673                 oi += out_stereo + 1;
1674                 i += div * 2;
1675                 div = divs[++di % divs_len];
1676         }
1677
1678         *consumed = i;
1679
1680         pr_debug("%s: in_len %d out_len %d consumed %d generated %d\n",
1681                 __func__, in_len, out_len, *consumed, oi);
1682         return oi;
1683 }
1684
1685 static ssize_t __downsample_to_user(struct audio_driver_state *ads,
1686                                 void __user *dst, int dst_size,
1687                                 void *src, int src_size,
1688                                 int *num_consumed)
1689 {
1690         int bytes_ds;
1691
1692         pr_debug("%s\n", __func__);
1693
1694         bytes_ds = downsample(src, src_size / sizeof(s16),
1695                         src, dst_size / sizeof(s16),
1696                         num_consumed,
1697                         ads->in_divs, ads->in_divs_len,
1698                         ads->in_config.stereo) * sizeof(s16);
1699
1700         if (copy_to_user(dst, src, bytes_ds)) {
1701                 pr_err("%s: error copying %d bytes to user\n", __func__,
1702                         bytes_ds);
1703                 return -EFAULT;
1704         }
1705
1706         *num_consumed *= sizeof(s16);
1707         BUG_ON(*num_consumed > src_size);
1708
1709         pr_debug("%s: generated %d, skipped %d, original in fifo %d\n",
1710                         __func__, bytes_ds, *num_consumed, src_size);
1711
1712         return bytes_ds;
1713 }
1714 #endif /*SAMPLE_RATE_CONVERTER_IN_DRIVER*/
1715
1716 static ssize_t downsample_to_user(struct audio_driver_state *ads,
1717                         void __user *buf,
1718                         size_t size) /* bytes to write to user buffer */
1719 {
1720         int i, nr_sg;
1721         int bytes_consumed_from_fifo, bc_now;
1722         int bytes_ds, ds_now;
1723         bool take_two = false;
1724
1725         struct scatterlist sgl[PCM_BUFFER_MAX_SIZE_ORDER - PAGE_SHIFT];
1726         sg_init_table(sgl, ARRAY_SIZE(sgl));
1727
1728         if (size == 0) {
1729                 pr_debug("%s: user buffer is full\n", __func__);
1730                 return 0;
1731         }
1732
1733         if (kfifo_is_empty(&ads->in.fifo)) {
1734                 pr_debug("%s: input fifo is empty\n", __func__);
1735                 return 0;
1736         }
1737
1738         nr_sg = kfifo_dma_out_prepare(&ads->in.fifo,
1739                                 sgl, ARRAY_SIZE(sgl),
1740                                 kfifo_len(&ads->in.fifo));
1741         BUG_ON(!nr_sg);
1742
1743         pr_debug("%s (fifo size %d)\n", __func__, size);
1744
1745         bytes_ds = 0;
1746         bytes_consumed_from_fifo = 0;
1747         for (bytes_ds = 0, i = 0; i < nr_sg; i++) {
1748                 BUG_ON(!sgl[i].length);
1749
1750 again:
1751 #ifdef SAMPLE_RATE_CONVERTER_IN_DRIVER
1752                 ds_now = __downsample_to_user(
1753 #else
1754                 ds_now = __i2s_copy_to_user(
1755 #endif
1756                                         ads,
1757                                         buf, size,
1758                                         sg_virt(&sgl[i]), sgl[i].length,
1759                                         &bc_now);
1760
1761                 if (!ds_now && !sg_is_last(sgl + i)) {
1762
1763                         BUG_ON(bc_now);
1764                         BUG_ON(take_two);
1765                         take_two = true;
1766
1767                         /* The assumption is that this sgl entry is at the end
1768                          * of the fifo, and there isn't enough space till the
1769                          * end of the fifo for at least one target sample to be
1770                          * generated.  When this happens, we copy enough bytes
1771                          * from the next sgl entry to the end of the buffer of
1772                          * the current one, knowing that the copied bytes will
1773                          * cause the fifo to wrap around.  We adjust the next
1774                          * entry, and continue with the loop.
1775                          */
1776
1777                         BUG_ON(sg_virt(&sgl[i]) + sgl[i].length !=
1778                                 ads->in.buffer + kfifo_size(&ads->in.fifo));
1779
1780                         if (sgl[i + 1].length < PCM_IN_BUFFER_PADDING) {
1781                                 pr_debug("%s: not enough data till end of fifo\n",
1782                                                 __func__);
1783                                 return 0;
1784                         }
1785
1786                         memcpy(sg_virt(&sgl[i]) + sgl[i].length,
1787                                 sg_virt(&sgl[i + 1]),
1788                                 PCM_IN_BUFFER_PADDING);
1789                         sgl[i].length += PCM_IN_BUFFER_PADDING;
1790
1791                         sg_set_buf(&sgl[i + 1],
1792                                 sg_virt(&sgl[i + 1]) + PCM_IN_BUFFER_PADDING,
1793                                 sgl[i + 1].length - PCM_IN_BUFFER_PADDING);
1794
1795                         goto again;
1796                 }
1797
1798                 bytes_ds += ds_now;
1799                 buf += ds_now;
1800                 BUG_ON(ds_now > size);
1801                 size -= ds_now;
1802                 bytes_consumed_from_fifo += bc_now;
1803                 pr_debug("%s: downsampled (%d req, %d actual)" \
1804                         " -> total ds %d (size %d)\n", __func__,
1805                         sgl[i].length, bytes_consumed_from_fifo,
1806                         bytes_ds, size);
1807                 if (sg_is_last(sgl + i))
1808                         break;
1809         }
1810
1811         kfifo_dma_out_finish(&ads->in.fifo, bytes_consumed_from_fifo);
1812
1813         return bytes_ds;
1814 }
1815
1816 static ssize_t tegra_audio_read(struct file *file, char __user *buf,
1817                         size_t size, loff_t *off)
1818 {
1819         ssize_t rc, total = 0;
1820         ssize_t nr;
1821
1822         struct audio_driver_state *ads = ads_from_misc_in(file);
1823
1824         mutex_lock(&ads->in.lock);
1825
1826         if (!IS_ALIGNED(size, 4)) {
1827                 pr_err("%s: user size request %d not aligned to 4\n",
1828                         __func__, size);
1829                 rc = -EINVAL;
1830                 goto done_err;
1831         }
1832
1833         pr_debug("%s:%d: read %d bytes, %d available\n", __func__,
1834                         smp_processor_id(),
1835                         size, kfifo_len(&ads->in.fifo));
1836
1837 again:
1838         /* If we want recording to stop immediately after it gets cancelled,
1839          * then we do not want to wait for the fifo to get drained.
1840          */
1841         if (ads->in.stop /* && kfifo_is_empty(&ads->in.fifo) */) {
1842                 pr_debug("%s: recording has been cancelled (%d/%d bytes)\n",
1843                                 __func__, total, size);
1844                 goto done_ok;
1845         }
1846
1847         rc = start_recording_if_necessary(&ads->in);
1848         if (rc < 0 && rc != -EALREADY) {
1849                 pr_err("%s: could not start recording\n", __func__);
1850                 goto done_err;
1851         }
1852
1853         ads->in.active = true;
1854
1855         nr = 0;
1856         do {
1857                 nr = downsample_to_user(ads, buf + total, size - total);
1858                 if (nr < 0) {
1859                         rc = nr;
1860                         goto done_err;
1861                 }
1862                 total += nr;
1863         } while (nr);
1864
1865         pr_debug("%s: copied %d bytes to user, total %d/%d\n",
1866                         __func__, nr, total, size);
1867
1868         if (total < size) {
1869                 mutex_unlock(&ads->in.lock);
1870                 pr_debug("%s: sleep (user %d total %d nr %d)\n", __func__,
1871                                 size, total, nr);
1872                 rc = wait_for_completion_interruptible(
1873                                 &ads->in.fifo_completion);
1874                 pr_debug("%s: awake\n", __func__);
1875                 mutex_lock(&ads->in.lock);
1876                 if (rc == -ERESTARTSYS) {
1877                         pr_warn("%s: interrupted\n", __func__);
1878                         goto done_err;
1879                 }
1880                 goto again;
1881         }
1882
1883         pr_debug("%s: done reading %d bytes, %d available\n", __func__,
1884                         total, kfifo_avail(&ads->in.fifo));
1885
1886 done_ok:
1887         rc = total;
1888         *off += total;
1889
1890 done_err:
1891         mutex_unlock(&ads->in.lock);
1892         return rc;
1893 }
1894
1895 static int tegra_audio_out_open(struct inode *inode, struct file *file)
1896 {
1897         struct audio_driver_state *ads = ads_from_misc_out(file);
1898
1899         pr_debug("%s\n", __func__);
1900
1901         mutex_lock(&ads->out.lock);
1902         if (!ads->out.opened++) {
1903                 pr_debug("%s: resetting fifo and error count\n", __func__);
1904                 ads->out.stop = false;
1905                 memset(&ads->out.errors, 0, sizeof(ads->out.errors));
1906                 kfifo_reset(&ads->out.fifo);
1907         }
1908         mutex_unlock(&ads->out.lock);
1909
1910         return 0;
1911 }
1912
1913 static int tegra_audio_out_release(struct inode *inode, struct file *file)
1914 {
1915         struct audio_driver_state *ads = ads_from_misc_out(file);
1916
1917         pr_debug("%s\n", __func__);
1918
1919         mutex_lock(&ads->out.lock);
1920         if (ads->out.opened)
1921                 ads->out.opened--;
1922         if (!ads->out.opened) {
1923                 stop_playback_if_necessary(&ads->out);
1924                 if (wake_lock_active(&ads->out.wake_lock))
1925                         pr_err("%s: wake lock is still held!\n", __func__);
1926                 if (kfifo_len(&ads->out.fifo))
1927                         pr_err("%s: output fifo is not empty (%d bytes left)\n",
1928                                 __func__, kfifo_len(&ads->out.fifo));
1929                 allow_suspend(&ads->out);
1930         }
1931         mutex_unlock(&ads->out.lock);
1932
1933         return 0;
1934 }
1935
1936 static int tegra_audio_in_open(struct inode *inode, struct file *file)
1937 {
1938         struct audio_driver_state *ads = ads_from_misc_in(file);
1939
1940         pr_debug("%s\n", __func__);
1941
1942         mutex_lock(&ads->in.lock);
1943         if (!ads->in.opened++) {
1944                 pr_debug("%s: resetting fifo\n", __func__);
1945                 /* By default, do not start recording when someone reads from
1946                  * input device.
1947                  */
1948                 ads->in.stop = false;
1949                 memset(&ads->in.errors, 0, sizeof(ads->in.errors));
1950                 kfifo_reset(&ads->in.fifo);
1951         }
1952         mutex_unlock(&ads->in.lock);
1953
1954         pr_debug("%s: done\n", __func__);
1955         return 0;
1956 }
1957
1958 static int tegra_audio_in_release(struct inode *inode, struct file *file)
1959 {
1960         struct audio_driver_state *ads = ads_from_misc_in(file);
1961
1962         pr_debug("%s\n", __func__);
1963
1964         mutex_lock(&ads->in.lock);
1965         if (ads->in.opened)
1966                 ads->in.opened--;
1967
1968         if (!ads->in.opened) {
1969                 if (ads->in.active)
1970                         request_stop_nosync(&ads->in);
1971                 if (wake_lock_active(&ads->in.wake_lock))
1972                         pr_err("%s: wake lock is still held!\n", __func__);
1973                 if (kfifo_len(&ads->in.fifo))
1974                         pr_err("%s: input fifo is not empty (%d bytes left)\n",
1975                                 __func__, kfifo_len(&ads->in.fifo));
1976                 allow_suspend(&ads->in);
1977         }
1978
1979         mutex_unlock(&ads->in.lock);
1980         pr_debug("%s: done\n", __func__);
1981         return 0;
1982 }
1983
1984 static const struct file_operations tegra_audio_out_fops = {
1985         .owner = THIS_MODULE,
1986         .open = tegra_audio_out_open,
1987         .release = tegra_audio_out_release,
1988         .write = tegra_audio_write,
1989 };
1990
1991 static const struct file_operations tegra_audio_in_fops = {
1992         .owner = THIS_MODULE,
1993         .open = tegra_audio_in_open,
1994         .read = tegra_audio_read,
1995         .release = tegra_audio_in_release,
1996 };
1997
1998 static int tegra_audio_ctl_open(struct inode *inode, struct file *file)
1999 {
2000         return 0;
2001 }
2002
2003 static int tegra_audio_ctl_release(struct inode *inode, struct file *file)
2004 {
2005         return 0;
2006 }
2007
2008 static const struct file_operations tegra_audio_out_ctl_fops = {
2009         .owner = THIS_MODULE,
2010         .open = tegra_audio_ctl_open,
2011         .release = tegra_audio_ctl_release,
2012         .unlocked_ioctl = tegra_audio_out_ioctl,
2013 };
2014
2015 static const struct file_operations tegra_audio_in_ctl_fops = {
2016         .owner = THIS_MODULE,
2017         .open = tegra_audio_ctl_open,
2018         .release = tegra_audio_ctl_release,
2019         .unlocked_ioctl = tegra_audio_in_ioctl,
2020 };
2021
2022 static const struct file_operations tegra_audio_ctl_fops = {
2023         .owner = THIS_MODULE,
2024         .open = tegra_audio_ctl_open,
2025         .release = tegra_audio_ctl_release,
2026         .unlocked_ioctl = tegra_audio_ioctl,
2027 };
2028
2029 static int init_stream_buffer(struct audio_stream *s,
2030                                 struct tegra_audio_buf_config *cfg,
2031                                 unsigned padding)
2032 {
2033         pr_debug("%s (size %d threshold %d chunk %d)\n", __func__,
2034                 cfg->size, cfg->threshold, cfg->chunk);
2035
2036         if (cfg->chunk < PCM_DMA_CHUNK_MIN_SIZE_ORDER) {
2037                 pr_err("%s: chunk %d too small (%d min)\n", __func__,
2038                         cfg->chunk, PCM_DMA_CHUNK_MIN_SIZE_ORDER);
2039                 return -EINVAL;
2040         }
2041
2042         if (cfg->chunk > cfg->size) {
2043                 pr_err("%s: chunk %d > size %d\n", __func__,
2044                                 cfg->chunk, cfg->size);
2045                 return -EINVAL;
2046         }
2047
2048         if (cfg->threshold > cfg->size) {
2049                 pr_err("%s: threshold %d > size %d\n", __func__,
2050                                 cfg->threshold, cfg->size);
2051                 return -EINVAL;
2052         }
2053
2054         if ((1 << cfg->size) < padding) {
2055                 pr_err("%s: size %d < buffer padding %d (bytes)\n", __func__,
2056                         cfg->size, padding);
2057                 return -EINVAL;
2058         }
2059
2060         if (cfg->size > PCM_BUFFER_MAX_SIZE_ORDER) {
2061                 pr_err("%s: size %d exceeds max %d\n", __func__,
2062                         cfg->size, PCM_BUFFER_MAX_SIZE_ORDER);
2063                 return -EINVAL;
2064         }
2065
2066         if (!s->buffer) {
2067                 pr_debug("%s: allocating buffer (size %d, padding %d)\n",
2068                                 __func__, 1 << cfg->size, padding);
2069                 s->buffer = kmalloc((1 << cfg->size) + padding,
2070                                 GFP_KERNEL | GFP_DMA);
2071         }
2072         if (!s->buffer) {
2073                 pr_err("%s: could not allocate output buffer\n", __func__);
2074                 return -ENOMEM;
2075         }
2076
2077         kfifo_init(&s->fifo, s->buffer, 1 << cfg->size);
2078         sg_init_table(&s->sg, 1);
2079         return 0;
2080 }
2081
2082
2083 static int setup_misc_device(struct miscdevice *misc,
2084                         const struct file_operations  *fops,
2085                         const char *fmt, ...)
2086 {
2087         int rc = 0;
2088         va_list args;
2089         const int sz = 64;
2090
2091         va_start(args, fmt);
2092
2093         memset(misc, 0, sizeof(*misc));
2094         misc->minor = MISC_DYNAMIC_MINOR;
2095         misc->name  = kmalloc(sz, GFP_KERNEL);
2096         if (!misc->name) {
2097                 rc = -ENOMEM;
2098                 goto done;
2099         }
2100
2101         vsnprintf((char *)misc->name, sz, fmt, args);
2102         misc->fops = fops;
2103         if (misc_register(misc)) {
2104                 pr_err("%s: could not register %s\n", __func__, misc->name);
2105                 kfree(misc->name);
2106                 rc = -EIO;
2107                 goto done;
2108         }
2109
2110 done:
2111         va_end(args);
2112         return rc;
2113 }
2114
2115 static ssize_t dma_toggle_show(struct device *dev,
2116                                 struct device_attribute *attr,
2117                                 char *buf)
2118 {
2119         struct tegra_audio_platform_data *pdata = dev->platform_data;
2120         struct audio_driver_state *ads = pdata->driver_data;
2121         return sprintf(buf, "%s\n", ads->using_dma ? "dma" : "pio");
2122 }
2123
2124 static ssize_t dma_toggle_store(struct device *dev,
2125                         struct device_attribute *attr,
2126                         const char *buf, size_t count)
2127 {
2128         int use_dma;
2129         struct tegra_audio_platform_data *pdata = dev->platform_data;
2130         struct audio_driver_state *ads = pdata->driver_data;
2131
2132         if (count < 4)
2133                 return -EINVAL;
2134
2135         use_dma = 0;
2136         if (!strncmp(buf, "dma", 3))
2137                 use_dma = 1;
2138         else if (strncmp(buf, "pio", 3)) {
2139                 dev_err(dev, "%s: invalid string [%s]\n", __func__, buf);
2140                 return -EINVAL;
2141         }
2142
2143         mutex_lock(&ads->out.lock);
2144         mutex_lock(&ads->in.lock);
2145         if (kfifo_len(&ads->out.fifo) || ads->in.active) {
2146                 dev_err(dev, "%s: playback or recording in progress.\n",
2147                         __func__);
2148                 mutex_unlock(&ads->in.lock);
2149                 mutex_unlock(&ads->out.lock);
2150                 return -EBUSY;
2151         }
2152         if (!!use_dma ^ !!ads->using_dma)
2153                 toggle_dma(ads);
2154         else
2155                 dev_info(dev, "%s: no change\n", __func__);
2156         mutex_unlock(&ads->in.lock);
2157         mutex_unlock(&ads->out.lock);
2158
2159         return count;
2160 }
2161
2162 static DEVICE_ATTR(dma_toggle, 0644, dma_toggle_show, dma_toggle_store);
2163
2164 static ssize_t __attr_fifo_atn_read(char *buf, int atn_lvl)
2165 {
2166         switch (atn_lvl) {
2167         case I2S_FIFO_ATN_LVL_ONE_SLOT:
2168                 strncpy(buf, "1\n", 2);
2169                 return 2;
2170         case I2S_FIFO_ATN_LVL_FOUR_SLOTS:
2171                 strncpy(buf, "4\n", 2);
2172                 return 2;
2173         case I2S_FIFO_ATN_LVL_EIGHT_SLOTS:
2174                 strncpy(buf, "8\n", 2);
2175                 return 2;
2176         case I2S_FIFO_ATN_LVL_TWELVE_SLOTS:
2177                 strncpy(buf, "12\n", 3);
2178                 return 3;
2179         default:
2180                 BUG_ON(1);
2181                 return -EIO;
2182         }
2183 }
2184
2185 static ssize_t __attr_fifo_atn_write(struct audio_driver_state *ads,
2186                 struct audio_stream *as,
2187                 int *fifo_lvl,
2188                 const char *buf, size_t size)
2189 {
2190         int lvl;
2191
2192         if (size > 3) {
2193                 pr_err("%s: buffer size %d too big\n", __func__, size);
2194                 return -EINVAL;
2195         }
2196
2197         if (sscanf(buf, "%d", &lvl) != 1) {
2198                 pr_err("%s: invalid input string [%s]\n", __func__, buf);
2199                 return -EINVAL;
2200         }
2201
2202         switch (lvl) {
2203         case 1:
2204                 lvl = I2S_FIFO_ATN_LVL_ONE_SLOT;
2205                 break;
2206         case 4:
2207                 lvl = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
2208                 break;
2209         case 8:
2210                 lvl = I2S_FIFO_ATN_LVL_EIGHT_SLOTS;
2211                 break;
2212         case 12:
2213                 lvl = I2S_FIFO_ATN_LVL_TWELVE_SLOTS;
2214                 break;
2215         default:
2216                 pr_err("%s: invalid attention level %d\n", __func__, lvl);
2217                 return -EINVAL;
2218         }
2219
2220         *fifo_lvl = lvl;
2221         pr_info("%s: fifo level %d\n", __func__, *fifo_lvl);
2222
2223         return size;
2224 }
2225
2226 static ssize_t tx_fifo_atn_show(struct device *dev,
2227                                 struct device_attribute *attr,
2228                                 char *buf)
2229 {
2230         struct tegra_audio_platform_data *pdata = dev->platform_data;
2231         struct audio_driver_state *ads = pdata->driver_data;
2232         return __attr_fifo_atn_read(buf, ads->out.i2s_fifo_atn_level);
2233 }
2234
2235 static ssize_t tx_fifo_atn_store(struct device *dev,
2236                         struct device_attribute *attr,
2237                         const char *buf, size_t count)
2238 {
2239         ssize_t rc;
2240         struct tegra_audio_platform_data *pdata = dev->platform_data;
2241         struct audio_driver_state *ads = pdata->driver_data;
2242         mutex_lock(&ads->out.lock);
2243         if (kfifo_len(&ads->out.fifo)) {
2244                 pr_err("%s: playback in progress.\n", __func__);
2245                 rc = -EBUSY;
2246                 goto done;
2247         }
2248         rc = __attr_fifo_atn_write(ads, &ads->out,
2249                         &ads->out.i2s_fifo_atn_level,
2250                         buf, count);
2251 done:
2252         mutex_unlock(&ads->out.lock);
2253         return rc;
2254 }
2255
2256 static DEVICE_ATTR(tx_fifo_atn, 0644, tx_fifo_atn_show, tx_fifo_atn_store);
2257
2258 static ssize_t rx_fifo_atn_show(struct device *dev,
2259                                 struct device_attribute *attr,
2260                                 char *buf)
2261 {
2262         struct tegra_audio_platform_data *pdata = dev->platform_data;
2263         struct audio_driver_state *ads = pdata->driver_data;
2264         return __attr_fifo_atn_read(buf, ads->in.i2s_fifo_atn_level);
2265 }
2266
2267 static ssize_t rx_fifo_atn_store(struct device *dev,
2268                         struct device_attribute *attr,
2269                         const char *buf, size_t count)
2270 {
2271         ssize_t rc;
2272         struct tegra_audio_platform_data *pdata = dev->platform_data;
2273         struct audio_driver_state *ads = pdata->driver_data;
2274         mutex_lock(&ads->in.lock);
2275         if (ads->in.active) {
2276                 pr_err("%s: recording in progress.\n", __func__);
2277                 rc = -EBUSY;
2278                 goto done;
2279         }
2280         rc = __attr_fifo_atn_write(ads, &ads->in,
2281                         &ads->in.i2s_fifo_atn_level,
2282                         buf, count);
2283 done:
2284         mutex_unlock(&ads->in.lock);
2285         return rc;
2286 }
2287
2288 static DEVICE_ATTR(rx_fifo_atn, 0644, rx_fifo_atn_show, rx_fifo_atn_store);
2289
2290 static int tegra_audio_probe(struct platform_device *pdev)
2291 {
2292         int rc;
2293         struct resource *res;
2294         struct clk *i2s_clk, *audio_sync_clk, *dap_mclk;
2295         struct audio_driver_state *state;
2296
2297         pr_info("%s\n", __func__);
2298
2299         state = kzalloc(sizeof(*state), GFP_KERNEL);
2300         if (!state)
2301                 return -ENOMEM;
2302
2303         state->pdev = pdev;
2304         state->pdata = pdev->dev.platform_data;
2305         state->pdata->driver_data = state;
2306         BUG_ON(!state->pdata);
2307
2308         if (!(state->pdata->mask &
2309                         (TEGRA_AUDIO_ENABLE_TX | TEGRA_AUDIO_ENABLE_RX))) {
2310                 dev_err(&pdev->dev, "neither tx nor rx is enabled!\n");
2311                 return -EIO;
2312         }
2313
2314         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2315         if (!res) {
2316                 dev_err(&pdev->dev, "no mem resource!\n");
2317                 return -ENODEV;
2318         }
2319
2320         if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
2321                 dev_err(&pdev->dev, "memory region already claimed!\n");
2322                 return -ENOMEM;
2323         }
2324
2325         state->i2s_phys = res->start;
2326         state->i2s_base = (unsigned long)ioremap(res->start,
2327                         res->end - res->start + 1);
2328         if (!state->i2s_base) {
2329                 dev_err(&pdev->dev, "cannot remap iomem!\n");
2330                 return -EIO;
2331         }
2332
2333         res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
2334         if (!res) {
2335                 dev_err(&pdev->dev, "no dma resource!\n");
2336                 return -ENODEV;
2337         }
2338         state->dma_req_sel = res->start;
2339
2340         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2341         if (!res) {
2342                 dev_err(&pdev->dev, "no irq resource!\n");
2343                 return -ENODEV;
2344         }
2345         state->irq = res->start;
2346
2347         memset(&state->pio_stats, 0, sizeof(state->pio_stats));
2348
2349         i2s_clk = clk_get(&pdev->dev, NULL);
2350         if (!i2s_clk) {
2351                 dev_err(&pdev->dev, "%s: could not get i2s1 clock\n",
2352                         __func__);
2353                 return -EIO;
2354         }
2355
2356         clk_set_rate(i2s_clk, state->pdata->i2s_clk_rate);
2357         if (clk_enable(i2s_clk)) {
2358                 dev_err(&pdev->dev, "%s: failed to enable i2s1 clock\n",
2359                         __func__);
2360                 return -EIO;
2361         }
2362         pr_info("%s: i2s_clk rate %ld\n", __func__, clk_get_rate(i2s_clk));
2363
2364         dap_mclk = tegra_get_clock_by_name(state->pdata->dap_clk);
2365         if (!dap_mclk) {
2366                 dev_err(&pdev->dev, "%s: could not get DAP clock\n",
2367                         __func__);
2368                 return -EIO;
2369         }
2370         clk_enable(dap_mclk);
2371
2372         audio_sync_clk = tegra_get_clock_by_name(state->pdata->audio_sync_clk);
2373         if (!audio_sync_clk) {
2374                 dev_err(&pdev->dev, "%s: could not get audio_2x clock\n",
2375                         __func__);
2376                 return -EIO;
2377         }
2378         clk_enable(audio_sync_clk);
2379
2380         i2s_enable_fifos(state->i2s_base, 0);
2381         /* disable interrupts from I2S */
2382         i2s_fifo_clear(state->i2s_base, I2S_FIFO_TX);
2383         i2s_fifo_clear(state->i2s_base, I2S_FIFO_RX);
2384         i2s_set_left_right_control_polarity(state->i2s_base, 0); /* default */
2385         if (state->pdata->master)
2386                 i2s_set_channel_bit_count(state->i2s_base, 44100,
2387                                 clk_get_rate(i2s_clk));
2388         i2s_set_master(state->i2s_base, state->pdata->master);
2389         i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_TX, 1);
2390         i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_RX, 0);
2391         i2s_set_bit_format(state->i2s_base, state->pdata->mode);
2392         i2s_set_bit_size(state->i2s_base, state->pdata->bit_size);
2393         i2s_set_fifo_format(state->i2s_base, state->pdata->fifo_fmt);
2394
2395         if ((state->pdata->mask & TEGRA_AUDIO_ENABLE_TX)) {
2396                 state->out.opened = 0;
2397                 state->out.active = false;
2398                 mutex_init(&state->out.lock);
2399                 init_completion(&state->out.fifo_completion);
2400                 init_completion(&state->out.stop_completion);
2401                 spin_lock_init(&state->out.dma_req_lock);
2402                 state->out.buf_phys = 0;
2403                 state->out.dma_chan = NULL;
2404                 state->out.dma_has_it = false;
2405
2406                 state->out.i2s_fifo_atn_level = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
2407                 state->out.buffer = 0;
2408                 state->out.buf_config.size = PCM_BUFFER_MAX_SIZE_ORDER;
2409                 state->out.buf_config.threshold = PCM_BUFFER_THRESHOLD_ORDER;
2410                 state->out.buf_config.chunk = PCM_BUFFER_DMA_CHUNK_SIZE_ORDER;
2411                 rc = init_stream_buffer(&state->out, &state->out.buf_config, 0);
2412                 if (rc < 0)
2413                         return rc;
2414
2415                 pm_qos_add_request(&state->out.pm_qos, PM_QOS_CPU_DMA_LATENCY,
2416                                 PM_QOS_DEFAULT_VALUE);
2417
2418                 snprintf(state->out.wake_lock_name,
2419                         sizeof(state->out.wake_lock_name),
2420                         "i2s.%d-audio-out", state->pdev->id);
2421                 wake_lock_init(&state->out.wake_lock, WAKE_LOCK_SUSPEND,
2422                         state->out.wake_lock_name);
2423
2424                 rc = setup_misc_device(&state->misc_out,
2425                         &tegra_audio_out_fops,
2426                         "audio%d_out", state->pdev->id);
2427                 if (rc < 0)
2428                         return rc;
2429
2430                 rc = setup_misc_device(&state->misc_out_ctl,
2431                                 &tegra_audio_out_ctl_fops,
2432                                 "audio%d_out_ctl", state->pdev->id);
2433                 if (rc < 0)
2434                         return rc;
2435         }
2436
2437         if ((state->pdata->mask & TEGRA_AUDIO_ENABLE_RX)) {
2438                 state->in.opened = 0;
2439                 state->in.active = false;
2440                 mutex_init(&state->in.lock);
2441                 init_completion(&state->in.fifo_completion);
2442                 init_completion(&state->in.stop_completion);
2443                 spin_lock_init(&state->in.dma_req_lock);
2444                 state->in.buf_phys = 0;
2445                 state->in.dma_chan = NULL;
2446                 state->in.dma_has_it = false;
2447
2448                 state->in.i2s_fifo_atn_level = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
2449                 state->in.buffer = 0;
2450                 state->in.buf_config.size = PCM_BUFFER_MAX_SIZE_ORDER;
2451                 state->in.buf_config.threshold = PCM_BUFFER_THRESHOLD_ORDER;
2452                 state->in.buf_config.chunk = PCM_BUFFER_DMA_CHUNK_SIZE_ORDER;
2453                 rc = init_stream_buffer(&state->in, &state->in.buf_config,
2454                                 PCM_IN_BUFFER_PADDING);
2455                 if (rc < 0)
2456                         return rc;
2457
2458                 pm_qos_add_request(&state->in.pm_qos, PM_QOS_CPU_DMA_LATENCY,
2459                                         PM_QOS_DEFAULT_VALUE);
2460
2461                 snprintf(state->in.wake_lock_name,
2462                         sizeof(state->in.wake_lock_name),
2463                         "i2s.%d-audio-in", state->pdev->id);
2464                 wake_lock_init(&state->in.wake_lock, WAKE_LOCK_SUSPEND,
2465                         state->in.wake_lock_name);
2466
2467                 rc = setup_misc_device(&state->misc_in,
2468                         &tegra_audio_in_fops,
2469                         "audio%d_in", state->pdev->id);
2470                 if (rc < 0)
2471                         return rc;
2472
2473                 rc = setup_misc_device(&state->misc_in_ctl,
2474                         &tegra_audio_in_ctl_fops,
2475                         "audio%d_in_ctl", state->pdev->id);
2476                 if (rc < 0)
2477                         return rc;
2478         }
2479
2480         if (request_irq(state->irq, i2s_interrupt,
2481                         IRQF_DISABLED, state->pdev->name, state) < 0) {
2482                 dev_err(&pdev->dev,
2483                         "%s: could not register handler for irq %d\n",
2484                         __func__, state->irq);
2485                 return -EIO;
2486         }
2487
2488         rc = setup_misc_device(&state->misc_ctl,
2489                         &tegra_audio_ctl_fops,
2490                         "audio%d_ctl", state->pdev->id);
2491         if (rc < 0)
2492                 return rc;
2493
2494         state->using_dma = state->pdata->dma_on;
2495         if (!state->using_dma)
2496                 sound_ops = &pio_sound_ops;
2497         sound_ops->setup(state);
2498
2499         rc = device_create_file(&pdev->dev, &dev_attr_dma_toggle);
2500         if (rc < 0) {
2501                 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2502                         __func__, dev_attr_dma_toggle.attr.name, rc);
2503                 return rc;
2504         }
2505
2506         rc = device_create_file(&pdev->dev, &dev_attr_tx_fifo_atn);
2507         if (rc < 0) {
2508                 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2509                         __func__, dev_attr_tx_fifo_atn.attr.name, rc);
2510                 return rc;
2511         }
2512
2513         rc = device_create_file(&pdev->dev, &dev_attr_rx_fifo_atn);
2514         if (rc < 0) {
2515                 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2516                         __func__, dev_attr_rx_fifo_atn.attr.name, rc);
2517                 return rc;
2518         }
2519
2520         state->in_config.rate = 11025;
2521         state->in_config.stereo = false;
2522         state->in_divs = divs_11025;
2523         state->in_divs_len = ARRAY_SIZE(divs_11025);
2524
2525         return 0;
2526 }
2527
2528 #ifdef CONFIG_PM
2529 static int tegra_audio_suspend(struct platform_device *pdev, pm_message_t mesg)
2530 {
2531         /* dev_info(&pdev->dev, "%s\n", __func__); */
2532         return 0;
2533 }
2534
2535 static int tegra_audio_resume(struct platform_device *pdev)
2536 {
2537         struct tegra_audio_platform_data *pdata = pdev->dev.platform_data;
2538         struct audio_driver_state *state = pdata->driver_data;
2539
2540         /* dev_info(&pdev->dev, "%s\n", __func__); */
2541
2542         if (!state)
2543                 return -ENOMEM;
2544
2545         /* disable interrupts from I2S */
2546         i2s_fifo_clear(state->i2s_base, I2S_FIFO_TX);
2547         i2s_fifo_clear(state->i2s_base, I2S_FIFO_RX);
2548         i2s_enable_fifos(state->i2s_base, 0);
2549
2550         i2s_set_left_right_control_polarity(state->i2s_base, 0); /* default */
2551
2552         if (state->pdata->master)
2553                 i2s_set_channel_bit_count(state->i2s_base, 44100,
2554                                 state->pdata->i2s_clk_rate);
2555         i2s_set_master(state->i2s_base, state->pdata->master);
2556
2557         i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_TX, 1);
2558         i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_RX, 0);
2559
2560         if (state->bit_format == TEGRA_AUDIO_BIT_FORMAT_DSP)
2561                 i2s_set_bit_format(state->i2s_base, I2S_BIT_FORMAT_DSP);
2562         else
2563                 i2s_set_bit_format(state->i2s_base, state->pdata->mode);
2564         i2s_set_bit_size(state->i2s_base, state->pdata->bit_size);
2565         i2s_set_fifo_format(state->i2s_base, state->pdata->fifo_fmt);
2566
2567         return 0;
2568 }
2569 #endif /* CONFIG_PM */
2570
2571 static struct platform_driver tegra_audio_driver = {
2572         .driver = {
2573                 .name = "i2s",
2574                 .owner = THIS_MODULE,
2575         },
2576         .probe = tegra_audio_probe,
2577 #ifdef CONFIG_PM
2578         .suspend = tegra_audio_suspend,
2579         .resume = tegra_audio_resume,
2580 #endif
2581 };
2582
2583 static int __init tegra_audio_init(void)
2584 {
2585         return platform_driver_register(&tegra_audio_driver);
2586 }
2587
2588 module_init(tegra_audio_init);
2589 MODULE_LICENSE("GPL");