2 * arch/arm/mach-tegra/tegra_i2s_audio.c
4 * Copyright (C) 2010 Google, Inc.
7 * Iliyan Malchev <malchev@google.com>
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.
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.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/miscdevice.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>
41 #include <linux/ktime.h>
42 #include <linux/sysfs.h>
44 #include <linux/tegra_audio.h>
47 #include <mach/iomap.h>
49 #include <mach/audio.h>
50 #include <mach/irqs.h>
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
59 #define PCM_IN_BUFFER_PADDING (1<<6) /* bytes */
61 /* per stream (input/output) */
66 struct tegra_audio_buf_config buf_config;
67 bool active; /* is DMA or PIO in progress? */
71 struct completion fifo_completion;
72 struct scatterlist sg;
76 int i2s_fifo_atn_level;
79 struct tegra_dma_channel *dma_chan;
80 struct completion stop_completion;
81 spinlock_t dma_req_lock; /* guards dma_has_it */
83 struct tegra_dma_req dma_req;
86 struct i2s_pio_stats {
87 u32 i2s_interrupt_count;
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 */
100 /* per i2s controller */
101 struct audio_driver_state {
102 struct list_head next;
104 struct platform_device *pdev;
105 struct tegra_audio_platform_data *pdata;
106 phys_addr_t i2s_phys;
107 unsigned long i2s_base;
110 unsigned long dma_req_sel;
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;
119 struct miscdevice misc_out;
120 struct miscdevice misc_out_ctl;
121 struct audio_stream out;
123 struct miscdevice misc_in;
124 struct miscdevice misc_in_ctl;
125 struct audio_stream in;
128 static inline int buf_size(struct audio_stream *s)
130 return 1 << s->buf_config.size;
133 static inline int chunk_size(struct audio_stream *s)
135 return 1 << s->buf_config.chunk;
138 static inline int threshold_size(struct audio_stream *s)
140 return 1 << s->buf_config.threshold;
143 static inline struct audio_driver_state *ads_from_misc_out(struct file *file)
145 struct miscdevice *m = file->private_data;
146 struct audio_driver_state *ads =
147 container_of(m, struct audio_driver_state, misc_out);
152 static inline struct audio_driver_state *ads_from_misc_out_ctl(
155 struct miscdevice *m = file->private_data;
156 struct audio_driver_state *ads =
157 container_of(m, struct audio_driver_state,
163 static inline struct audio_driver_state *ads_from_misc_in(struct file *file)
165 struct miscdevice *m = file->private_data;
166 struct audio_driver_state *ads =
167 container_of(m, struct audio_driver_state, misc_in);
172 static inline struct audio_driver_state *ads_from_misc_in_ctl(
175 struct miscdevice *m = file->private_data;
176 struct audio_driver_state *ads =
177 container_of(m, struct audio_driver_state,
183 static inline struct audio_driver_state *ads_from_out(
184 struct audio_stream *aos)
186 return container_of(aos, struct audio_driver_state, out);
189 static inline struct audio_driver_state *ads_from_in(
190 struct audio_stream *ais)
192 return container_of(ais, struct audio_driver_state, in);
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
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
203 #define I2S_FIFO_ERR (I2S_I2S_STATUS_FIFO1_ERR | I2S_I2S_STATUS_FIFO2_ERR)
205 static inline void i2s_writel(unsigned long base, u32 val, u32 reg)
207 writel(val, base + reg);
210 static inline u32 i2s_readl(unsigned long base, u32 reg)
212 return readl(base + reg);
215 static inline void i2s_fifo_write(unsigned long base, int fifo, u32 data)
217 i2s_writel(base, data, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
220 static inline u32 i2s_fifo_read(unsigned long base, int fifo)
222 return i2s_readl(base, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
225 static int i2s_set_channel_bit_count(unsigned long base,
226 int sampling, int bitclk)
229 int bitcnt = bitclk / (2 * sampling) - 1;
231 if (bitcnt < 0 || bitcnt >= 1<<11) {
232 pr_err("%s: bit count %d is out of bounds\n", __func__,
238 if (bitclk % (2 * sampling)) {
239 pr_info("%s: enabling non-symmetric mode\n", __func__);
240 val |= I2S_I2S_TIMING_NON_SYM_ENABLE;
243 pr_debug("%s: I2S_I2S_TIMING_0 = %08x\n", __func__, val);
244 i2s_writel(base, val, I2S_I2S_TIMING_0);
248 static void i2s_set_fifo_mode(unsigned long base, int fifo, int tx)
250 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
252 val &= ~I2S_I2S_CTRL_FIFO1_RX_ENABLE;
253 val |= (!tx) ? I2S_I2S_CTRL_FIFO1_RX_ENABLE : 0;
255 val &= ~I2S_I2S_CTRL_FIFO2_TX_ENABLE;
256 val |= tx ? I2S_I2S_CTRL_FIFO2_TX_ENABLE : 0;
258 i2s_writel(base, val, I2S_I2S_CTRL_0);
261 static int i2s_fifo_set_attention_level(unsigned long base,
262 int fifo, unsigned level)
266 if (level > I2S_FIFO_ATN_LVL_TWELVE_SLOTS) {
267 pr_err("%s: invalid fifo level selector %d\n", __func__,
272 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
275 val &= ~I2S_I2S_FIFO_SCR_FIFO1_ATN_LVL_MASK;
276 val |= level << I2S_FIFO1_ATN_LVL_SHIFT;
278 val &= ~I2S_I2S_FIFO_SCR_FIFO2_ATN_LVL_MASK;
279 val |= level << I2S_FIFO2_ATN_LVL_SHIFT;
282 i2s_writel(base, val, I2S_I2S_FIFO_SCR_0);
286 static void i2s_fifo_enable(unsigned long base, int fifo, int on)
288 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
290 val &= ~I2S_I2S_CTRL_FIFO1_ENABLE;
291 val |= on ? I2S_I2S_CTRL_FIFO1_ENABLE : 0;
293 val &= ~I2S_I2S_CTRL_FIFO2_ENABLE;
294 val |= on ? I2S_I2S_CTRL_FIFO2_ENABLE : 0;
297 i2s_writel(base, val, I2S_I2S_CTRL_0);
300 static bool i2s_is_fifo_enabled(unsigned long base, int fifo)
302 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
304 return !!(val & I2S_I2S_CTRL_FIFO1_ENABLE);
305 return !!(val & I2S_I2S_CTRL_FIFO2_ENABLE);
308 static void i2s_fifo_clear(unsigned long base, int fifo)
310 u32 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
312 val &= ~I2S_I2S_FIFO_SCR_FIFO1_CLR;
313 val |= I2S_I2S_FIFO_SCR_FIFO1_CLR;
315 /* Per Nvidia, reduces pop on the next run. */
316 if (!(val & I2S_I2S_CTRL_FIFO1_RX_ENABLE)) {
319 i2s_writel(base, 0, I2S_I2S_FIFO1_0);
323 val &= ~I2S_I2S_FIFO_SCR_FIFO2_CLR;
324 val |= I2S_I2S_FIFO_SCR_FIFO2_CLR;
327 i2s_writel(base, val, I2S_I2S_FIFO_SCR_0);
330 static void i2s_set_master(unsigned long base, int master)
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);
338 static int i2s_set_bit_format(unsigned long base, unsigned fmt)
342 if (fmt > I2S_BIT_FORMAT_DSP) {
343 pr_err("%s: invalid bit-format selector %d\n", __func__, fmt);
347 val = i2s_readl(base, I2S_I2S_CTRL_0);
348 val &= ~I2S_I2S_CTRL_BIT_FORMAT_MASK;
349 val |= fmt << I2S_BIT_FORMAT_SHIFT;
351 i2s_writel(base, val, I2S_I2S_CTRL_0);
355 static int i2s_set_bit_size(unsigned long base, unsigned bit_size)
357 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
358 val &= ~I2S_I2S_CTRL_BIT_SIZE_MASK;
360 if (bit_size > I2S_BIT_SIZE_32) {
361 pr_err("%s: invalid bit_size selector %d\n", __func__,
366 val |= bit_size << I2S_BIT_SIZE_SHIFT;
368 i2s_writel(base, val, I2S_I2S_CTRL_0);
372 static int i2s_set_fifo_format(unsigned long base, unsigned fmt)
374 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
375 val &= ~I2S_I2S_CTRL_FIFO_FORMAT_MASK;
377 if (fmt > I2S_FIFO_32 && fmt != I2S_FIFO_PACKED) {
378 pr_err("%s: invalid fmt selector %d\n", __func__, fmt);
382 val |= fmt << I2S_FIFO_SHIFT;
384 i2s_writel(base, val, I2S_I2S_CTRL_0);
388 static void i2s_set_left_right_control_polarity(unsigned long base,
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);
397 static void i2s_set_fifo_irq_on_err(unsigned long base, int fifo, int on)
399 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
401 val &= ~I2S_I2S_IE_FIFO1_ERR;
402 val |= on ? I2S_I2S_IE_FIFO1_ERR : 0;
404 val &= ~I2S_I2S_IE_FIFO2_ERR;
405 val |= on ? I2S_I2S_IE_FIFO2_ERR : 0;
407 i2s_writel(base, val, I2S_I2S_CTRL_0);
410 static void i2s_set_fifo_irq_on_qe(unsigned long base, int fifo, int on)
412 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
414 val &= ~I2S_I2S_QE_FIFO1;
415 val |= on ? I2S_I2S_QE_FIFO1 : 0;
417 val &= ~I2S_I2S_QE_FIFO2;
418 val |= on ? I2S_I2S_QE_FIFO2 : 0;
420 i2s_writel(base, val, I2S_I2S_CTRL_0);
423 static void i2s_enable_fifos(unsigned long base, int on)
425 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
427 val |= I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
428 I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR;
430 val &= ~(I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
431 I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR);
433 i2s_writel(base, val, I2S_I2S_CTRL_0);
436 static inline u32 i2s_get_status(unsigned long base)
438 return i2s_readl(base, I2S_I2S_STATUS_0);
441 static inline u32 i2s_get_control(unsigned long base)
443 return i2s_readl(base, I2S_I2S_CTRL_0);
446 static inline void i2s_ack_status(unsigned long base)
448 return i2s_writel(base, i2s_readl(base, I2S_I2S_STATUS_0),
452 static inline u32 i2s_get_fifo_scr(unsigned long base)
454 return i2s_readl(base, I2S_I2S_FIFO_SCR_0);
457 static inline phys_addr_t i2s_get_fifo_phy_base(unsigned long phy_base,
460 return phy_base + (fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
463 static inline u32 i2s_get_fifo_full_empty_count(unsigned long base, int fifo)
465 u32 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
468 val = val >> I2S_I2S_FIFO_SCR_FIFO1_FULL_EMPTY_COUNT_SHIFT;
470 val = val >> I2S_I2S_FIFO_SCR_FIFO2_FULL_EMPTY_COUNT_SHIFT;
472 return val & I2S_I2S_FIFO_SCR_FIFO_FULL_EMPTY_COUNT_MASK;
475 static int init_stream_buffer(struct audio_stream *,
476 struct tegra_audio_buf_config *cfg, unsigned);
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 *);
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 *);
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 *);
502 static const struct sound_ops dma_sound_ops = {
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,
511 static const struct sound_ops pio_sound_ops = {
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,
520 static const struct sound_ops *sound_ops = &dma_sound_ops;
522 static int start_playback(struct audio_stream *aos)
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);
533 static int start_recording_if_necessary(struct audio_stream *ais)
537 struct audio_driver_state *ads = ads_from_in(ais);
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);
544 spin_unlock_irqrestore(&ais->dma_req_lock, flags);
548 static bool stop_playback_if_necessary(struct audio_stream *aos)
551 spin_lock_irqsave(&aos->dma_req_lock, flags);
552 if (kfifo_is_empty(&aos->fifo)) {
553 sound_ops->stop_playback(aos);
556 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
559 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
564 static bool stop_recording_if_necessary_nosync(struct audio_stream *ais)
566 struct audio_driver_state *ads = ads_from_in(ais);
568 if (ads->recording_cancelled || kfifo_is_full(&ais->fifo)) {
569 if (kfifo_is_full(&ais->fifo))
571 sound_ops->stop_recording(ais);
578 static bool stop_recording(struct audio_stream *ais)
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);
588 static void toggle_dma(struct audio_driver_state *ads)
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;
599 static int resume_dma_playback(struct audio_stream *aos);
601 static void setup_dma_tx_request(struct tegra_dma_req *req,
602 struct audio_stream *aos);
604 static void setup_dma_rx_request(struct tegra_dma_req *req,
605 struct audio_stream *ais);
607 static int setup_dma(struct audio_driver_state *ads)
610 pr_info("%s\n", __func__);
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));
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,
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));
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;
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;
656 static void tear_down_dma(struct audio_driver_state *ads)
658 pr_info("%s\n", __func__);
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,
665 ads->out.buf_phys = 0;
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,
672 ads->in.buf_phys = 0;
675 static void dma_tx_complete_callback(struct tegra_dma_req *req)
678 struct audio_stream *aos = req->dev;
679 int count = req->bytes_transferred;
681 u64 max_delay_us = count * 10000 / (4 * 441);
683 pr_debug("%s bytes transferred %d\n", __func__, count);
685 aos->dma_has_it = false;
686 delta_us = ktime_to_us(ktime_sub(ktime_get_real(), aos->last_dma_ts));
688 if (delta_us > max_delay_us) {
689 pr_debug("%s: too late by %lld us\n", __func__,
690 delta_us - max_delay_us);
694 kfifo_dma_out_finish(&aos->fifo, count);
695 dma_unmap_sg(NULL, &aos->sg, 1, DMA_TO_DEVICE);
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);
704 if (stop_playback_if_necessary(aos))
706 spin_lock_irqsave(&aos->dma_req_lock, flags);
707 resume_dma_playback(aos);
708 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
711 static void dma_rx_complete_threshold(struct tegra_dma_req *req)
713 pr_info("%s\n", __func__);
716 static void dma_rx_complete_callback(struct tegra_dma_req *req)
719 struct audio_stream *ais = req->dev;
720 int count = req->bytes_transferred;
722 spin_lock_irqsave(&ais->dma_req_lock, flags);
724 ais->dma_has_it = false;
726 pr_debug("%s(%d): transferred %d bytes (%d available in fifo)\n",
729 count, kfifo_avail(&ais->fifo));
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);
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);
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);
751 pr_debug("%s: resuming dma recording\n", __func__);
753 /* This call will fail if we try to set up a DMA request that's
756 (void)resume_dma_recording(ais);
757 spin_unlock_irqrestore(&ais->dma_req_lock, flags);
759 pr_debug("%s: done\n", __func__);
762 static void setup_dma_tx_request(struct tegra_dma_req *req,
763 struct audio_stream *aos)
765 struct audio_driver_state *ads = ads_from_out(aos);
767 memset(req, 0, sizeof(*req));
769 req->complete = dma_tx_complete_callback;
771 req->to_memory = false;
772 req->dest_addr = i2s_get_fifo_phy_base(ads->i2s_phys, I2S_FIFO_TX);
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;
780 static void setup_dma_rx_request(struct tegra_dma_req *req,
781 struct audio_stream *ais)
783 struct audio_driver_state *ads = ads_from_in(ais);
785 memset(req, 0, sizeof(*req));
787 req->complete = dma_rx_complete_callback;
788 req->threshold = dma_rx_complete_threshold;
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;
796 req->req_sel = ads->dma_req_sel;
799 /* Called with aos->dma_req_lock taken. */
800 static int resume_dma_playback(struct audio_stream *aos)
803 struct audio_driver_state *ads = ads_from_out(aos);
804 struct tegra_dma_req *req = &aos->dma_req;
806 if (aos->dma_has_it) {
807 pr_debug("%s: playback already in progress\n", __func__);
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
817 rc = dma_map_sg(NULL, &aos->sg, 1, DMA_TO_DEVICE);
819 pr_err("%s: could not map dma memory: %d\n", __func__, rc);
824 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_TX);
826 i2s_fifo_set_attention_level(ads->i2s_base,
827 I2S_FIFO_TX, aos->i2s_fifo_atn_level);
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);
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);
841 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
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__);
851 /* Called with aos->dma_req_lock taken. */
852 static int start_dma_playback(struct audio_stream *aos)
854 return resume_dma_playback(aos);
857 /* Called with aos->dma_req_lock taken. */
858 static void stop_dma_playback(struct audio_stream *aos)
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) &&
867 pr_info("%s: spin %d\n", __func__, spin);
869 pr_warn("%s: spinny\n", __func__);
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)
877 struct audio_driver_state *ads = ads_from_in(ais);
878 struct tegra_dma_req *req = &ais->dma_req;
880 BUG_ON(kfifo_is_full(&ais->fifo));
882 if (ais->dma_has_it) {
883 pr_debug("%s: recording already in progress\n", __func__);
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));
893 rc = dma_map_sg(NULL, &ais->sg, 1, DMA_FROM_DEVICE);
895 pr_err("%s: coult not map dma for recording: %d\n",
900 req->dest_addr = sg_dma_address(&ais->sg);
901 req->size = round_down(sg_dma_len(&ais->sg), 4);
904 pr_err("%s: invalid request size %d\n", __func__, req->size);
908 dma_sync_single_for_device(NULL,
909 req->dest_addr, req->size, DMA_FROM_DEVICE);
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__);
918 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
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);
926 /* Called with ais->dma_req_lock taken. */
927 static int start_dma_recording(struct audio_stream *ais)
929 pr_debug("%s\n", __func__);
930 return resume_dma_recording(ais);
933 /* Called with ais->dma_req_lock taken. */
934 static void stop_dma_recording(struct audio_stream *ais)
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) &&
944 pr_info("%s: spin %d\n", __func__, spin);
946 pr_warn("%s: spinny\n", __func__);
951 static int setup_pio(struct audio_driver_state *ads)
953 pr_info("%s\n", __func__);
954 enable_irq(ads->irq);
958 static void tear_down_pio(struct audio_driver_state *ads)
960 pr_info("%s\n", __func__);
961 disable_irq(ads->irq);
964 static int start_pio_playback(struct audio_stream *aos)
966 struct audio_driver_state *ads = ads_from_out(aos);
968 if (i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_TX)) {
969 pr_debug("%s: playback is already in progress\n", __func__);
973 pr_debug("%s\n", __func__);
975 i2s_fifo_set_attention_level(ads->i2s_base,
976 I2S_FIFO_TX, aos->i2s_fifo_atn_level);
978 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_TX);
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);
988 static void stop_pio_playback(struct audio_stream *aos)
990 struct audio_driver_state *ads = ads_from_out(aos);
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)
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);
1005 memset(&ads->pio_stats, 0, sizeof(ads->pio_stats));
1008 static int start_pio_recording(struct audio_stream *ais)
1010 struct audio_driver_state *ads = ads_from_in(ais);
1012 if (i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_RX)) {
1013 pr_debug("%s: already started\n", __func__);
1017 pr_debug("%s: start\n", __func__);
1019 i2s_fifo_set_attention_level(ads->i2s_base,
1020 I2S_FIFO_RX, ais->i2s_fifo_atn_level);
1022 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
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);
1028 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1033 static void stop_pio_recording(struct audio_stream *ais)
1035 struct audio_driver_state *ads = ads_from_in(ais);
1037 pr_debug("%s\n", __func__);
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);
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);
1051 memset(&ads->pio_stats, 0, sizeof(ads->pio_stats));
1054 static irqreturn_t i2s_interrupt(int irq, void *data)
1056 struct audio_driver_state *ads = data;
1057 u32 status = i2s_get_status(ads->i2s_base);
1059 pr_debug("%s: %08x\n", __func__, status);
1061 ads->pio_stats.i2s_interrupt_count++;
1063 if (status & I2S_I2S_FIFO_TX_ERR)
1064 ads->pio_stats.tx_fifo_errors++;
1066 if (status & I2S_I2S_FIFO_RX_ERR) {
1067 ads->pio_stats.rx_fifo_errors++;
1071 if (status & I2S_FIFO_ERR)
1072 i2s_ack_status(ads->i2s_base);
1074 if (status & I2S_I2S_FIFO_TX_QS) {
1078 u16 fifo_buffer[32];
1080 struct audio_stream *out = &ads->out;
1082 if (!i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_TX)) {
1083 pr_debug("%s: tx fifo not enabled, skipping\n",
1088 pr_debug("%s tx fifo is ready\n", __func__);
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);
1097 if (stop_playback_if_necessary(out))
1100 empty = i2s_get_fifo_full_empty_count(ads->i2s_base,
1103 len = kfifo_out(&out->fifo, fifo_buffer,
1104 empty * sizeof(u16));
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++]);
1114 /* TODO: Should we check to see if we wrote less than the
1115 * FIFO threshold and adjust it if so?
1119 /* start the transaction */
1120 pr_debug("%s: enabling fifo (%d samples written)\n",
1122 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
1127 if (status & I2S_I2S_FIFO_RX_QS) {
1131 struct audio_stream *in = &ads->in;
1133 if (!i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_RX)) {
1134 pr_debug("%s: rx fifo not enabled, skipping\n",
1139 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1141 full = i2s_get_fifo_full_empty_count(ads->i2s_base,
1144 pr_debug("%s rx fifo is ready (%d samples)\n", __func__, full);
1148 u16 sample = i2s_fifo_read(ads->i2s_base, I2S_FIFO_RX);
1149 kfifo_in(&in->fifo, &sample, sizeof(sample));
1152 ads->pio_stats.rx_fifo_read += full * sizeof(u16);
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);
1161 if (stop_recording_if_necessary_nosync(&ads->in)) {
1162 pr_debug("%s: recording cancelled or fifo full\n",
1164 if (!completion_done(&ads->in.stop_completion)) {
1165 pr_debug("%s: signalling stop completion\n",
1167 complete(&ads->in.stop_completion);
1172 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1176 pr_debug("%s: done %08x\n", __func__, i2s_get_status(ads->i2s_base));
1180 static ssize_t tegra_audio_write(struct file *file,
1181 const char __user *buf, size_t size, loff_t *off)
1183 ssize_t rc, total = 0;
1186 struct audio_driver_state *ads = ads_from_misc_out(file);
1188 mutex_lock(&ads->out.lock);
1190 ads->out.active = true;
1192 if (!IS_ALIGNED(size, 4)) {
1193 pr_err("%s: user size request %d not aligned to 4\n",
1199 pr_debug("%s: write %d bytes, %d available\n", __func__,
1200 size, kfifo_avail(&ads->out.fifo));
1203 rc = kfifo_from_user(&ads->out.fifo, buf + total, size - total, &nw);
1205 pr_err("%s: error copying from user\n", __func__);
1209 rc = start_playback(&ads->out);
1211 pr_err("%s: could not start playback: %d\n", __func__, rc);
1217 pr_debug("%s: sleep (user %d total %d nw %d)\n", __func__,
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__);
1227 pr_debug("%s: awake\n", __func__);
1235 ads->out.active = false;
1236 mutex_unlock(&ads->out.lock);
1240 static long tegra_audio_out_ioctl(struct file *file,
1241 unsigned int cmd, unsigned long arg)
1244 struct audio_driver_state *ads = ads_from_misc_out_ctl(file);
1245 struct audio_stream *aos = &ads->out;
1247 mutex_lock(&aos->lock);
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))) {
1257 pr_err("%s: playback in progress\n", __func__);
1261 rc = init_stream_buffer(aos, &cfg, 0);
1264 aos->buf_config = cfg;
1267 case TEGRA_AUDIO_OUT_GET_BUF_CONFIG:
1268 if (copy_to_user((void __user *)arg, &aos->buf_config,
1269 sizeof(aos->buf_config)))
1272 case TEGRA_AUDIO_OUT_GET_ERROR_COUNT:
1273 if (copy_to_user((void __user *)arg, &aos->errors,
1274 sizeof(aos->errors)))
1279 case TEGRA_AUDIO_OUT_PRELOAD_FIFO: {
1280 struct tegra_audio_out_preload preload;
1281 if (copy_from_user(&preload, (void __user *)arg,
1286 rc = kfifo_from_user(&ads->out.fifo,
1287 (void __user *)preload.data, preload.len,
1288 &preload.len_written);
1290 pr_err("%s: error copying from user\n", __func__);
1293 if (copy_to_user((void __user *)arg, &preload, sizeof(preload)))
1295 pr_info("%s: preloaded output fifo with %d bytes\n", __func__,
1296 preload.len_written);
1303 mutex_unlock(&aos->lock);
1307 static long tegra_audio_in_ioctl(struct file *file,
1308 unsigned int cmd, unsigned long arg)
1311 struct audio_driver_state *ads = ads_from_misc_in_ctl(file);
1312 struct audio_stream *ais = &ads->in;
1314 mutex_lock(&ais->lock);
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);
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);
1333 case TEGRA_AUDIO_IN_SET_CONFIG: {
1334 struct tegra_audio_in_config cfg;
1337 pr_err("%s: recording in progress\n", __func__);
1341 if (copy_from_user(&cfg, (const void __user *)arg,
1349 ads->in_divs = divs_8000;
1350 ads->in_divs_len = ARRAY_SIZE(divs_8000);
1353 ads->in_divs = divs_11025;
1354 ads->in_divs_len = ARRAY_SIZE(divs_11025);
1357 ads->in_divs = divs_16000;
1358 ads->in_divs_len = ARRAY_SIZE(divs_16000);
1361 ads->in_divs = divs_22050;
1362 ads->in_divs_len = ARRAY_SIZE(divs_22050);
1365 ads->in_divs = divs_44100;
1366 ads->in_divs_len = ARRAY_SIZE(divs_44100);
1369 pr_err("%s: invalid sampling rate %d\n", __func__,
1376 pr_info("%s: setting input sampling rate to %d, %s\n",
1378 cfg.stereo ? "stereo" : "mono");
1379 ads->in_config = cfg;
1380 ads->in_config.stereo = !!ads->in_config.stereo;
1384 case TEGRA_AUDIO_IN_GET_CONFIG:
1385 if (copy_to_user((void __user *)arg, &ads->in_config,
1386 sizeof(ads->in_config)))
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))) {
1396 pr_err("%s: recording in progress\n", __func__);
1400 rc = init_stream_buffer(ais, &cfg, PCM_IN_BUFFER_PADDING);
1403 ais->buf_config = cfg;
1406 case TEGRA_AUDIO_IN_GET_BUF_CONFIG:
1407 if (copy_to_user((void __user *)arg, &ais->buf_config,
1408 sizeof(ais->buf_config)))
1411 case TEGRA_AUDIO_IN_GET_ERROR_COUNT:
1412 if (copy_to_user((void __user *)arg, &ais->errors,
1413 sizeof(ais->errors)))
1422 mutex_unlock(&ais->lock);
1426 /* downsample a 16-bit 44.1kHz PCM stereo stream to stereo or mono 16-bit PCM
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,
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];
1451 out[oi] = (lsum + rsum) / (div * 2);
1453 out[oi] = lsum / div;
1454 out[oi + 1] = rsum / div;
1457 oi += out_stereo + 1;
1459 div = divs[++di % divs_len];
1464 pr_debug("%s: in_len %d out_len %d consumed %d generated %d\n",
1465 __func__, in_len, out_len, *consumed, oi);
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,
1476 pr_debug("%s\n", __func__);
1478 bytes_ds = downsample(src, src_size / sizeof(s16),
1479 src, dst_size / sizeof(s16),
1481 ads->in_divs, ads->in_divs_len,
1482 ads->in_config.stereo) * sizeof(s16);
1484 if (copy_to_user(dst, src, bytes_ds)) {
1485 pr_err("%s: error copying %d bytes to user\n", __func__,
1490 *num_consumed *= sizeof(s16);
1491 BUG_ON(*num_consumed > src_size);
1493 pr_debug("%s: generated %d, skipped %d, original in fifo %d\n",
1494 __func__, bytes_ds, *num_consumed, src_size);
1499 static ssize_t downsample_to_user(struct audio_driver_state *ads,
1501 size_t size) /* bytes to write to user buffer */
1504 int bytes_consumed_from_fifo, bc_now;
1505 int bytes_ds, ds_now;
1506 bool take_two = false;
1508 struct scatterlist sgl[PCM_BUFFER_MAX_SIZE_ORDER - PAGE_SHIFT];
1509 sg_init_table(sgl, ARRAY_SIZE(sgl));
1512 pr_debug("%s: user buffer is full\n", __func__);
1516 if (kfifo_is_empty(&ads->in.fifo)) {
1517 pr_debug("%s: input fifo is empty\n", __func__);
1521 nr_sg = kfifo_dma_out_prepare(&ads->in.fifo,
1522 sgl, ARRAY_SIZE(sgl),
1523 kfifo_len(&ads->in.fifo));
1526 pr_debug("%s (fifo size %d)\n", __func__, size);
1529 bytes_consumed_from_fifo = 0;
1530 for (bytes_ds = 0, i = 0; i < nr_sg; i++) {
1531 BUG_ON(!sgl[i].length);
1534 ds_now = __downsample_to_user(ads,
1536 sg_virt(&sgl[i]), sgl[i].length,
1539 if (!ds_now && !sg_is_last(sgl + i)) {
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.
1555 BUG_ON(sg_virt(&sgl[i]) + sgl[i].length !=
1556 ads->in.buffer + kfifo_size(&ads->in.fifo));
1558 if (sgl[i + 1].length < PCM_IN_BUFFER_PADDING) {
1559 pr_debug("%s: not enough data till end of fifo\n",
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;
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);
1578 BUG_ON(ds_now > size);
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,
1585 if (sg_is_last(sgl + i))
1589 kfifo_dma_out_finish(&ads->in.fifo, bytes_consumed_from_fifo);
1594 static ssize_t tegra_audio_read(struct file *file, char __user *buf,
1595 size_t size, loff_t *off)
1597 ssize_t rc, total = 0;
1600 struct audio_driver_state *ads = ads_from_misc_in(file);
1602 mutex_lock(&ads->in.lock);
1604 ads->in.active = true;
1606 if (!IS_ALIGNED(size, 4)) {
1607 pr_err("%s: user size request %d not aligned to 4\n",
1613 pr_debug("%s:%d: read %d bytes, %d available\n", __func__,
1615 size, kfifo_len(&ads->in.fifo));
1617 rc = start_recording_if_necessary(&ads->in);
1619 pr_err("%s: could not start recording\n", __func__);
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.
1627 if (ads->recording_cancelled /* && kfifo_is_empty(&ads->in.fifo) */) {
1628 pr_debug("%s: recording has been cancelled (read %d bytes)\n",
1635 nr = downsample_to_user(ads, buf + total, size - total);
1643 pr_debug("%s: copied %d bytes to user, total %d/%d\n",
1644 __func__, nr, total, size);
1647 /* If we lost data, recording was stopped, so we need to resume
1650 rc = start_recording_if_necessary(&ads->in);
1652 pr_err("%s: could not resume recording\n", __func__);
1656 mutex_unlock(&ads->in.lock);
1657 pr_debug("%s: sleep (user %d total %d nr %d)\n", __func__,
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__);
1670 pr_debug("%s: done reading %d bytes, %d available\n", __func__,
1671 total, kfifo_avail(&ads->in.fifo));
1678 ads->in.active = false;
1679 mutex_unlock(&ads->in.lock);
1683 static int tegra_audio_out_open(struct inode *inode, struct file *file)
1685 struct audio_driver_state *ads = ads_from_misc_out(file);
1687 pr_info("%s\n", __func__);
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);
1695 mutex_unlock(&ads->out.lock);
1700 static int tegra_audio_out_release(struct inode *inode, struct file *file)
1702 struct audio_driver_state *ads = ads_from_misc_out(file);
1704 pr_info("%s\n", __func__);
1706 mutex_lock(&ads->out.lock);
1707 if (ads->out.opened)
1709 if (!ads->out.opened)
1710 stop_playback_if_necessary(&ads->out);
1711 mutex_unlock(&ads->out.lock);
1716 static int tegra_audio_in_open(struct inode *inode, struct file *file)
1718 struct audio_driver_state *ads = ads_from_misc_in(file);
1720 pr_info("%s\n", __func__);
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
1728 ads->recording_cancelled = false;
1730 kfifo_reset(&ads->in.fifo);
1732 mutex_unlock(&ads->in.lock);
1734 pr_info("%s: done\n", __func__);
1738 static int tegra_audio_in_release(struct inode *inode, struct file *file)
1740 struct audio_driver_state *ads = ads_from_misc_in(file);
1742 pr_info("%s\n", __func__);
1743 mutex_lock(&ads->in.lock);
1746 mutex_unlock(&ads->in.lock);
1747 pr_info("%s: done\n", __func__);
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,
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,
1765 static int tegra_audio_ctl_open(struct inode *inode, struct file *file)
1770 static int tegra_audio_ctl_release(struct inode *inode, struct file *file)
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,
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,
1789 static int init_stream_buffer(struct audio_stream *s,
1790 struct tegra_audio_buf_config *cfg,
1793 pr_info("%s (size %d threshold %d chunk %d)\n", __func__,
1794 cfg->size, cfg->threshold, cfg->chunk);
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);
1802 if (cfg->chunk > cfg->size) {
1803 pr_err("%s: chunk %d > size %d\n", __func__,
1804 cfg->chunk, cfg->size);
1808 if (cfg->threshold > cfg->size) {
1809 pr_err("%s: threshold %d > size %d\n", __func__,
1810 cfg->threshold, cfg->size);
1814 if ((1 << cfg->size) < padding) {
1815 pr_err("%s: size %d < buffer padding %d (bytes)\n", __func__,
1816 cfg->size, padding);
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);
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);
1833 pr_err("%s: could not allocate output buffer\n", __func__);
1837 kfifo_init(&s->fifo, s->buffer, 1 << cfg->size);
1838 sg_init_table(&s->sg, 1);
1843 static int setup_misc_device(struct miscdevice *misc,
1844 const struct file_operations *fops,
1845 const char *fmt, ...)
1851 va_start(args, fmt);
1853 memset(misc, 0, sizeof(*misc));
1854 misc->minor = MISC_DYNAMIC_MINOR;
1855 misc->name = kmalloc(sz, GFP_KERNEL);
1861 vsnprintf((char *)misc->name, sz, fmt, args);
1863 if (misc_register(misc)) {
1864 pr_err("%s: could not register %s\n", __func__, misc->name);
1875 static ssize_t dma_toggle_show(struct device *dev,
1876 struct device_attribute *attr,
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");
1884 static ssize_t dma_toggle_store(struct device *dev,
1885 struct device_attribute *attr,
1886 const char *buf, size_t count)
1889 struct tegra_audio_platform_data *pdata = dev->platform_data;
1890 struct audio_driver_state *ads = pdata->driver_data;
1896 if (!strncmp(buf, "dma", 3))
1898 else if (strncmp(buf, "pio", 3)) {
1899 dev_err(dev, "%s: invalid string [%s]\n", __func__, buf);
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",
1908 mutex_unlock(&ads->in.lock);
1909 mutex_unlock(&ads->out.lock);
1912 if (!!use_dma ^ !!ads->using_dma)
1915 dev_info(dev, "%s: no change\n", __func__);
1916 mutex_unlock(&ads->in.lock);
1917 mutex_unlock(&ads->out.lock);
1922 static DEVICE_ATTR(dma_toggle, 0644, dma_toggle_show, dma_toggle_store);
1924 static ssize_t __attr_fifo_atn_read(char *buf, int atn_lvl)
1927 case I2S_FIFO_ATN_LVL_ONE_SLOT:
1928 strncpy(buf, "1\n", 2);
1930 case I2S_FIFO_ATN_LVL_FOUR_SLOTS:
1931 strncpy(buf, "4\n", 2);
1933 case I2S_FIFO_ATN_LVL_EIGHT_SLOTS:
1934 strncpy(buf, "8\n", 2);
1936 case I2S_FIFO_ATN_LVL_TWELVE_SLOTS:
1937 strncpy(buf, "12\n", 3);
1945 static ssize_t __attr_fifo_atn_write(struct audio_driver_state *ads,
1946 struct audio_stream *as,
1948 const char *buf, size_t size)
1953 pr_err("%s: buffer size %d too big\n", __func__, size);
1957 if (sscanf(buf, "%d", &lvl) != 1) {
1958 pr_err("%s: invalid input string [%s]\n", __func__, buf);
1964 lvl = I2S_FIFO_ATN_LVL_ONE_SLOT;
1967 lvl = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
1970 lvl = I2S_FIFO_ATN_LVL_EIGHT_SLOTS;
1973 lvl = I2S_FIFO_ATN_LVL_TWELVE_SLOTS;
1976 pr_err("%s: invalid attention level %d\n", __func__, lvl);
1980 mutex_lock(&as->lock);
1982 pr_err("%s: in progress.\n", __func__);
1983 mutex_unlock(&as->lock);
1987 pr_info("%s: fifo level %d\n", __func__, *fifo_lvl);
1988 mutex_unlock(&as->lock);
1993 static ssize_t tx_fifo_atn_show(struct device *dev,
1994 struct device_attribute *attr,
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);
2002 static ssize_t tx_fifo_atn_store(struct device *dev,
2003 struct device_attribute *attr,
2004 const char *buf, size_t count)
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);
2012 static DEVICE_ATTR(tx_fifo_atn, 0644, tx_fifo_atn_show, tx_fifo_atn_store);
2014 static ssize_t rx_fifo_atn_show(struct device *dev,
2015 struct device_attribute *attr,
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);
2023 static ssize_t rx_fifo_atn_store(struct device *dev,
2024 struct device_attribute *attr,
2025 const char *buf, size_t count)
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);
2033 static DEVICE_ATTR(rx_fifo_atn, 0644, rx_fifo_atn_show, rx_fifo_atn_store);
2035 static int tegra_audio_probe(struct platform_device *pdev)
2038 struct resource *res;
2039 struct clk *i2s_clk, *audio_sync_clk, *dap_mclk;
2040 struct audio_driver_state *state;
2042 pr_info("%s\n", __func__);
2044 state = kmalloc(sizeof(*state), GFP_KERNEL);
2049 state->pdata = pdev->dev.platform_data;
2050 state->pdata->driver_data = state;
2051 BUG_ON(!state->pdata);
2053 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2055 dev_err(&pdev->dev, "no mem resource!\n");
2059 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
2060 dev_err(&pdev->dev, "memory region already claimed!\n");
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");
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;
2075 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
2077 dev_err(&pdev->dev, "no dma resource!\n");
2080 state->dma_req_sel = res->start;
2082 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2084 dev_err(&pdev->dev, "no irq resource!\n");
2087 state->irq = res->start;
2089 memset(&state->pio_stats, 0, sizeof(state->pio_stats));
2091 i2s_clk = clk_get(&pdev->dev, NULL);
2093 dev_err(&pdev->dev, "%s: could not get i2s1 clock\n",
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",
2104 pr_info("%s: i2s_clk rate %ld\n", __func__, clk_get_rate(i2s_clk));
2106 dap_mclk = tegra_get_clock_by_name(state->pdata->dap_clk);
2108 dev_err(&pdev->dev, "%s: could not get DAP clock\n",
2112 clk_enable(dap_mclk);
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",
2120 clk_enable(audio_sync_clk);
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);
2127 i2s_set_left_right_control_polarity(state->i2s_base, 0); /* default */
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);
2134 i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_TX, 1);
2135 i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_RX, 0);
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);
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;
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;
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);
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);
2178 if (request_irq(state->irq, i2s_interrupt,
2179 IRQF_DISABLED, state->pdev->name, state) < 0) {
2181 "%s: could not register handler for irq %d\n",
2182 __func__, state->irq);
2186 rc = setup_misc_device(&state->misc_out,
2187 &tegra_audio_out_fops,
2188 "audio%d_out", state->pdev->id);
2192 rc = setup_misc_device(&state->misc_out_ctl,
2193 &tegra_audio_out_ctl_fops,
2194 "audio%d_out_ctl", state->pdev->id);
2198 rc = setup_misc_device(&state->misc_in,
2199 &tegra_audio_in_fops,
2200 "audio%d_in", state->pdev->id);
2204 rc = setup_misc_device(&state->misc_in_ctl,
2205 &tegra_audio_in_ctl_fops,
2206 "audio%d_in_ctl", state->pdev->id);
2210 state->using_dma = state->pdata->dma_on;
2211 if (!state->using_dma)
2212 sound_ops = &pio_sound_ops;
2213 sound_ops->setup(state);
2215 rc = device_create_file(&pdev->dev, &dev_attr_dma_toggle);
2217 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2218 __func__, dev_attr_dma_toggle.attr.name, rc);
2222 rc = device_create_file(&pdev->dev, &dev_attr_tx_fifo_atn);
2224 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2225 __func__, dev_attr_tx_fifo_atn.attr.name, rc);
2229 rc = device_create_file(&pdev->dev, &dev_attr_rx_fifo_atn);
2231 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2232 __func__, dev_attr_rx_fifo_atn.attr.name, rc);
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);
2244 static struct platform_driver tegra_audio_driver = {
2247 .owner = THIS_MODULE,
2249 .probe = tegra_audio_probe,
2252 static int __init tegra_audio_init(void)
2254 return platform_driver_register(&tegra_audio_driver);
2257 module_init(tegra_audio_init);
2258 MODULE_LICENSE("GPL");