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