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>
43 #include <linux/pm_qos_params.h>
44 #include <linux/wakelock.h>
45 #include <linux/delay.h>
46 #include <linux/tegra_audio.h>
49 #include <mach/iomap.h>
51 #include <mach/audio.h>
52 #include <mach/irqs.h>
56 #define PCM_BUFFER_MAX_SIZE_ORDER (PAGE_SHIFT + 2)
57 #define PCM_BUFFER_DMA_CHUNK_SIZE_ORDER PAGE_SHIFT
58 #define PCM_BUFFER_THRESHOLD_ORDER (PCM_BUFFER_MAX_SIZE_ORDER - 1)
59 #define PCM_DMA_CHUNK_MIN_SIZE_ORDER 3
61 #define PCM_IN_BUFFER_PADDING (1<<6) /* bytes */
63 #define TEGRA_AUDIO_DSP_NONE 0
64 #define TEGRA_AUDIO_DSP_PCM 1
65 #define TEGRA_AUDIO_DSP_NETWORK 2
66 #define TEGRA_AUDIO_DSP_TDM 3
68 /* per stream (input/output) */
73 struct tegra_audio_buf_config buf_config;
74 bool active; /* is DMA or PIO in progress? */
78 struct completion fifo_completion;
79 struct scatterlist sg;
81 struct tegra_audio_error_counts errors;
83 int i2s_fifo_atn_level;
86 struct tegra_dma_channel *dma_chan;
88 struct completion stop_completion;
89 spinlock_t dma_req_lock; /* guards dma_has_it */
91 struct tegra_dma_req dma_req;
93 struct pm_qos_request_list pm_qos;
94 struct wake_lock wake_lock;
95 char wake_lock_name[100];
98 struct i2s_pio_stats {
99 u32 i2s_interrupt_count;
106 static const int divs_8000[] = { 5, 6, 6, 5 }; /* 8018.(18) Hz */
107 static const int divs_11025[] = { 4 };
108 static const int divs_22050[] = { 2 };
109 static const int divs_44100[] = { 1 };
110 static const int divs_16000[] = { 2, 3, 3, 3, 3, 3, 3, 2 }; /* 16036.(36) Hz */
112 /* per i2s controller */
113 struct audio_driver_state {
114 struct list_head next;
116 struct platform_device *pdev;
117 struct tegra_audio_platform_data *pdata;
118 phys_addr_t i2s_phys;
119 unsigned long i2s_base;
122 unsigned long dma_req_sel;
124 int irq; /* for pio mode */
125 struct i2s_pio_stats pio_stats;
126 struct tegra_audio_in_config in_config;
130 struct miscdevice misc_out;
131 struct miscdevice misc_out_ctl;
132 struct audio_stream out;
134 struct miscdevice misc_in;
135 struct miscdevice misc_in_ctl;
136 struct audio_stream in;
138 /* Control for whole I2S (Data format, etc.) */
139 struct miscdevice misc_ctl;
140 unsigned int bit_format;
143 static inline int buf_size(struct audio_stream *s)
145 return 1 << s->buf_config.size;
148 static inline int chunk_size(struct audio_stream *s)
150 return 1 << s->buf_config.chunk;
153 static inline int threshold_size(struct audio_stream *s)
155 return 1 << s->buf_config.threshold;
158 static inline struct audio_driver_state *ads_from_misc_out(struct file *file)
160 struct miscdevice *m = file->private_data;
161 struct audio_driver_state *ads =
162 container_of(m, struct audio_driver_state, misc_out);
167 static inline struct audio_driver_state *ads_from_misc_out_ctl(
170 struct miscdevice *m = file->private_data;
171 struct audio_driver_state *ads =
172 container_of(m, struct audio_driver_state,
178 static inline struct audio_driver_state *ads_from_misc_in(struct file *file)
180 struct miscdevice *m = file->private_data;
181 struct audio_driver_state *ads =
182 container_of(m, struct audio_driver_state, misc_in);
187 static inline struct audio_driver_state *ads_from_misc_in_ctl(
190 struct miscdevice *m = file->private_data;
191 struct audio_driver_state *ads =
192 container_of(m, struct audio_driver_state,
198 static inline struct audio_driver_state *ads_from_misc_ctl(
201 struct miscdevice *m = file->private_data;
202 struct audio_driver_state *ads =
203 container_of(m, struct audio_driver_state,
209 static inline struct audio_driver_state *ads_from_out(
210 struct audio_stream *aos)
212 return container_of(aos, struct audio_driver_state, out);
215 static inline struct audio_driver_state *ads_from_in(
216 struct audio_stream *ais)
218 return container_of(ais, struct audio_driver_state, in);
221 static inline void prevent_suspend(struct audio_stream *as)
223 pr_debug("%s\n", __func__);
224 wake_lock(&as->wake_lock);
225 pm_qos_update_request(&as->pm_qos, 0);
228 static inline void allow_suspend(struct audio_stream *as)
230 pr_debug("%s\n", __func__);
231 pm_qos_update_request(&as->pm_qos, PM_QOS_DEFAULT_VALUE);
232 wake_unlock(&as->wake_lock);
235 #define I2S_I2S_FIFO_TX_BUSY I2S_I2S_STATUS_FIFO1_BSY
236 #define I2S_I2S_FIFO_TX_QS I2S_I2S_STATUS_QS_FIFO1
237 #define I2S_I2S_FIFO_TX_ERR I2S_I2S_STATUS_FIFO1_ERR
239 #define I2S_I2S_FIFO_RX_BUSY I2S_I2S_STATUS_FIFO2_BSY
240 #define I2S_I2S_FIFO_RX_QS I2S_I2S_STATUS_QS_FIFO2
241 #define I2S_I2S_FIFO_RX_ERR I2S_I2S_STATUS_FIFO2_ERR
243 #define I2S_FIFO_ERR (I2S_I2S_STATUS_FIFO1_ERR | I2S_I2S_STATUS_FIFO2_ERR)
245 static inline void i2s_writel(unsigned long base, u32 val, u32 reg)
247 writel(val, base + reg);
250 static inline u32 i2s_readl(unsigned long base, u32 reg)
252 return readl(base + reg);
255 static inline void i2s_fifo_write(unsigned long base, int fifo, u32 data)
257 i2s_writel(base, data, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
260 static inline u32 i2s_fifo_read(unsigned long base, int fifo)
262 return i2s_readl(base, fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
265 static int i2s_set_channel_bit_count(unsigned long base,
266 int sampling, int bitclk)
269 int bitcnt = bitclk / (2 * sampling) - 1;
271 if (bitcnt < 0 || bitcnt >= 1<<11) {
272 pr_err("%s: bit count %d is out of bounds\n", __func__,
278 if (bitclk % (2 * sampling)) {
279 pr_info("%s: enabling non-symmetric mode\n", __func__);
280 val |= I2S_I2S_TIMING_NON_SYM_ENABLE;
283 pr_debug("%s: I2S_I2S_TIMING_0 = %08x\n", __func__, val);
284 i2s_writel(base, val, I2S_I2S_TIMING_0);
288 static void i2s_set_fifo_mode(unsigned long base, int fifo, int tx)
290 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
292 val &= ~I2S_I2S_CTRL_FIFO1_RX_ENABLE;
293 val |= (!tx) ? I2S_I2S_CTRL_FIFO1_RX_ENABLE : 0;
295 val &= ~I2S_I2S_CTRL_FIFO2_TX_ENABLE;
296 val |= tx ? I2S_I2S_CTRL_FIFO2_TX_ENABLE : 0;
298 i2s_writel(base, val, I2S_I2S_CTRL_0);
301 static int i2s_fifo_set_attention_level(unsigned long base,
302 int fifo, unsigned level)
306 if (level > I2S_FIFO_ATN_LVL_TWELVE_SLOTS) {
307 pr_err("%s: invalid fifo level selector %d\n", __func__,
312 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
315 val &= ~I2S_I2S_FIFO_SCR_FIFO1_ATN_LVL_MASK;
316 val |= level << I2S_FIFO1_ATN_LVL_SHIFT;
318 val &= ~I2S_I2S_FIFO_SCR_FIFO2_ATN_LVL_MASK;
319 val |= level << I2S_FIFO2_ATN_LVL_SHIFT;
322 i2s_writel(base, val, I2S_I2S_FIFO_SCR_0);
326 static void i2s_fifo_enable(unsigned long base, int fifo, int on)
328 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
330 val &= ~I2S_I2S_CTRL_FIFO1_ENABLE;
331 val |= on ? I2S_I2S_CTRL_FIFO1_ENABLE : 0;
333 val &= ~I2S_I2S_CTRL_FIFO2_ENABLE;
334 val |= on ? I2S_I2S_CTRL_FIFO2_ENABLE : 0;
337 i2s_writel(base, val, I2S_I2S_CTRL_0);
340 static bool i2s_is_fifo_enabled(unsigned long base, int fifo)
342 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
344 return !!(val & I2S_I2S_CTRL_FIFO1_ENABLE);
345 return !!(val & I2S_I2S_CTRL_FIFO2_ENABLE);
348 static void i2s_fifo_clear(unsigned long base, int fifo)
350 u32 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
352 val &= ~I2S_I2S_FIFO_SCR_FIFO1_CLR;
353 val |= I2S_I2S_FIFO_SCR_FIFO1_CLR;
355 /* Per Nvidia, reduces pop on the next run. */
356 if (!(val & I2S_I2S_CTRL_FIFO1_RX_ENABLE)) {
359 i2s_writel(base, 0, I2S_I2S_FIFO1_0);
363 val &= ~I2S_I2S_FIFO_SCR_FIFO2_CLR;
364 val |= I2S_I2S_FIFO_SCR_FIFO2_CLR;
367 i2s_writel(base, val, I2S_I2S_FIFO_SCR_0);
370 static void i2s_set_master(unsigned long base, int master)
372 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
373 val &= ~I2S_I2S_CTRL_MASTER_ENABLE;
374 val |= master ? I2S_I2S_CTRL_MASTER_ENABLE : 0;
375 i2s_writel(base, val, I2S_I2S_CTRL_0);
378 static int i2s_set_dsp_mode(unsigned long base, unsigned int mode)
381 if (mode > TEGRA_AUDIO_DSP_TDM) {
382 pr_err("%s: invalid mode %d.\n", __func__, mode);
385 if (mode == TEGRA_AUDIO_DSP_TDM) {
386 pr_err("TEGRA_AUDIO_DSP_TDM not implemented.\n");
390 /* Disable unused modes */
391 if (mode != TEGRA_AUDIO_DSP_PCM) {
392 /* Disable PCM mode */
393 val = i2s_readl(base, I2S_I2S_PCM_CTRL_0);
394 val &= ~(I2S_I2S_PCM_CTRL_TRM_MODE|I2S_I2S_PCM_CTRL_RCV_MODE);
395 i2s_writel(base, val, I2S_I2S_PCM_CTRL_0);
397 if (mode != TEGRA_AUDIO_DSP_NETWORK) {
398 /* Disable Network mode */
399 val = i2s_readl(base, I2S_I2S_NW_CTRL_0);
400 val &= ~(I2S_I2S_NW_CTRL_TRM_TLPHY_MODE|I2S_I2S_NW_CTRL_RCV_TLPHY_MODE);
401 i2s_writel(base, val, I2S_I2S_NW_CTRL_0);
404 /* Enable the selected mode. */
406 case TEGRA_AUDIO_DSP_NETWORK:
407 /* Set DSP Network (Telephony) Mode */
408 val = i2s_readl(base, I2S_I2S_NW_CTRL_0);
409 val |= I2S_I2S_NW_CTRL_TRM_TLPHY_MODE|I2S_I2S_NW_CTRL_RCV_TLPHY_MODE;
410 i2s_writel(base, val, I2S_I2S_NW_CTRL_0);
412 case TEGRA_AUDIO_DSP_PCM:
413 /* Set DSP PCM Mode */
414 val = i2s_readl(base, I2S_I2S_PCM_CTRL_0);
415 val |= I2S_I2S_PCM_CTRL_TRM_MODE|I2S_I2S_PCM_CTRL_RCV_MODE;
416 i2s_writel(base, val, I2S_I2S_PCM_CTRL_0);
423 static int i2s_set_bit_format(unsigned long base, unsigned fmt)
427 if (fmt > I2S_BIT_FORMAT_DSP) {
428 pr_err("%s: invalid bit-format selector %d\n", __func__, fmt);
432 val = i2s_readl(base, I2S_I2S_CTRL_0);
433 val &= ~I2S_I2S_CTRL_BIT_FORMAT_MASK;
434 val |= fmt << I2S_BIT_FORMAT_SHIFT;
435 i2s_writel(base, val, I2S_I2S_CTRL_0);
436 /* For DSP format, select DSP PCM mode. */
437 /* PCM mode and Network Mode slot 0 are effectively identical. */
438 if (fmt == I2S_BIT_FORMAT_DSP)
439 i2s_set_dsp_mode(base, TEGRA_AUDIO_DSP_PCM);
441 i2s_set_dsp_mode(base, TEGRA_AUDIO_DSP_NONE);
446 static int i2s_set_bit_size(unsigned long base, unsigned bit_size)
448 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
449 val &= ~I2S_I2S_CTRL_BIT_SIZE_MASK;
451 if (bit_size > I2S_BIT_SIZE_32) {
452 pr_err("%s: invalid bit_size selector %d\n", __func__,
457 val |= bit_size << I2S_BIT_SIZE_SHIFT;
459 i2s_writel(base, val, I2S_I2S_CTRL_0);
463 static int i2s_set_fifo_format(unsigned long base, unsigned fmt)
465 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
466 val &= ~I2S_I2S_CTRL_FIFO_FORMAT_MASK;
468 if (fmt > I2S_FIFO_32 && fmt != I2S_FIFO_PACKED) {
469 pr_err("%s: invalid fmt selector %d\n", __func__, fmt);
473 val |= fmt << I2S_FIFO_SHIFT;
475 i2s_writel(base, val, I2S_I2S_CTRL_0);
479 static void i2s_set_left_right_control_polarity(unsigned long base,
482 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
483 val &= ~I2S_I2S_CTRL_L_R_CTRL;
484 val |= high_low ? I2S_I2S_CTRL_L_R_CTRL : 0;
485 i2s_writel(base, val, I2S_I2S_CTRL_0);
488 static void i2s_set_fifo_irq_on_err(unsigned long base, int fifo, int on)
490 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
492 val &= ~I2S_I2S_IE_FIFO1_ERR;
493 val |= on ? I2S_I2S_IE_FIFO1_ERR : 0;
495 val &= ~I2S_I2S_IE_FIFO2_ERR;
496 val |= on ? I2S_I2S_IE_FIFO2_ERR : 0;
498 i2s_writel(base, val, I2S_I2S_CTRL_0);
501 static void i2s_set_fifo_irq_on_qe(unsigned long base, int fifo, int on)
503 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
505 val &= ~I2S_I2S_QE_FIFO1;
506 val |= on ? I2S_I2S_QE_FIFO1 : 0;
508 val &= ~I2S_I2S_QE_FIFO2;
509 val |= on ? I2S_I2S_QE_FIFO2 : 0;
511 i2s_writel(base, val, I2S_I2S_CTRL_0);
514 static void i2s_enable_fifos(unsigned long base, int on)
516 u32 val = i2s_readl(base, I2S_I2S_CTRL_0);
518 val |= I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
519 I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR;
521 val &= ~(I2S_I2S_QE_FIFO1 | I2S_I2S_QE_FIFO2 |
522 I2S_I2S_IE_FIFO1_ERR | I2S_I2S_IE_FIFO2_ERR);
524 i2s_writel(base, val, I2S_I2S_CTRL_0);
527 static inline u32 i2s_get_status(unsigned long base)
529 return i2s_readl(base, I2S_I2S_STATUS_0);
532 static inline u32 i2s_get_control(unsigned long base)
534 return i2s_readl(base, I2S_I2S_CTRL_0);
537 static inline void i2s_ack_status(unsigned long base)
539 return i2s_writel(base, i2s_readl(base, I2S_I2S_STATUS_0),
543 static inline u32 i2s_get_fifo_scr(unsigned long base)
545 return i2s_readl(base, I2S_I2S_FIFO_SCR_0);
548 static inline phys_addr_t i2s_get_fifo_phy_base(unsigned long phy_base,
551 return phy_base + (fifo ? I2S_I2S_FIFO2_0 : I2S_I2S_FIFO1_0);
554 static inline u32 i2s_get_fifo_full_empty_count(unsigned long base, int fifo)
556 u32 val = i2s_readl(base, I2S_I2S_FIFO_SCR_0);
559 val = val >> I2S_I2S_FIFO_SCR_FIFO1_FULL_EMPTY_COUNT_SHIFT;
561 val = val >> I2S_I2S_FIFO_SCR_FIFO2_FULL_EMPTY_COUNT_SHIFT;
563 return val & I2S_I2S_FIFO_SCR_FIFO_FULL_EMPTY_COUNT_MASK;
566 static int init_stream_buffer(struct audio_stream *,
567 struct tegra_audio_buf_config *cfg, unsigned);
569 static int setup_dma(struct audio_driver_state *);
570 static void tear_down_dma(struct audio_driver_state *);
571 static int start_dma_playback(struct audio_stream *);
572 static void stop_dma_playback(struct audio_stream *);
573 static int start_dma_recording(struct audio_stream *);
574 static int resume_dma_recording(struct audio_stream *);
575 static void stop_dma_recording(struct audio_stream *);
577 static int setup_pio(struct audio_driver_state *);
578 static void tear_down_pio(struct audio_driver_state *);
579 static int start_pio_playback(struct audio_stream *);
580 static void stop_pio_playback(struct audio_stream *);
581 static int start_pio_recording(struct audio_stream *);
582 static void stop_pio_recording(struct audio_stream *);
585 int (*setup)(struct audio_driver_state *);
586 void (*tear_down)(struct audio_driver_state *);
587 int (*start_playback)(struct audio_stream *);
588 void (*stop_playback)(struct audio_stream *);
589 int (*start_recording)(struct audio_stream *);
590 void (*stop_recording)(struct audio_stream *);
593 static const struct sound_ops dma_sound_ops = {
595 .tear_down = tear_down_dma,
596 .start_playback = start_dma_playback,
597 .stop_playback = stop_dma_playback,
598 .start_recording = start_dma_recording,
599 .stop_recording = stop_dma_recording,
602 static const struct sound_ops pio_sound_ops = {
604 .tear_down = tear_down_pio,
605 .start_playback = start_pio_playback,
606 .stop_playback = stop_pio_playback,
607 .start_recording = start_pio_recording,
608 .stop_recording = stop_pio_recording,
611 static const struct sound_ops *sound_ops = &dma_sound_ops;
613 static int start_playback(struct audio_stream *aos)
617 spin_lock_irqsave(&aos->dma_req_lock, flags);
618 pr_debug("%s: starting playback\n", __func__);
619 rc = sound_ops->start_playback(aos);
620 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
622 prevent_suspend(aos);
626 static int start_recording_if_necessary(struct audio_stream *ais)
629 bool started = false;
632 spin_lock_irqsave(&ais->dma_req_lock, flags);
633 if (!ais->stop && !kfifo_is_full(&ais->fifo)) {
634 pr_debug("%s: starting recording\n", __func__);
635 rc = sound_ops->start_recording(ais);
638 spin_unlock_irqrestore(&ais->dma_req_lock, flags);
640 prevent_suspend(ais);
644 static bool stop_playback_if_necessary(struct audio_stream *aos)
647 spin_lock_irqsave(&aos->dma_req_lock, flags);
648 if (kfifo_is_empty(&aos->fifo)) {
649 sound_ops->stop_playback(aos);
650 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
654 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
659 static bool stop_recording_if_necessary_nosync(struct audio_stream *ais)
661 if (ais->stop || kfifo_is_full(&ais->fifo)) {
662 if (kfifo_is_full(&ais->fifo))
663 ais->errors.full_empty++; /* overflow */
664 sound_ops->stop_recording(ais);
671 /* playback and recording */
672 static bool wait_till_stopped(struct audio_stream *as)
675 pr_debug("%s: wait for completion\n", __func__);
676 rc = wait_for_completion_interruptible(
677 &as->stop_completion);
679 pr_debug("%s: done: %d\n", __func__, rc);
683 /* Ask for playback and recording to stop. The _nosync means that
684 * as->lock has to be locked by the caller.
686 static void request_stop_nosync(struct audio_stream *as)
688 pr_debug("%s\n", __func__);
691 wait_till_stopped(as);
692 if (!completion_done(&as->fifo_completion)) {
693 pr_debug("%s: complete\n", __func__);
694 complete(&as->fifo_completion);
697 kfifo_reset(&as->fifo);
698 as->active = false; /* applies to recording only */
699 pr_debug("%s: done\n", __func__);
702 static void toggle_dma(struct audio_driver_state *ads)
704 pr_info("%s: %s\n", __func__, ads->using_dma ? "pio" : "dma");
705 sound_ops->tear_down(ads);
706 sound_ops = ads->using_dma ? &pio_sound_ops : &dma_sound_ops;
707 sound_ops->setup(ads);
708 ads->using_dma = !ads->using_dma;
713 static int resume_dma_playback(struct audio_stream *aos);
715 static void setup_dma_tx_request(struct tegra_dma_req *req,
716 struct audio_stream *aos);
718 static void setup_dma_rx_request(struct tegra_dma_req *req,
719 struct audio_stream *ais);
721 static int setup_dma(struct audio_driver_state *ads)
724 pr_info("%s\n", __func__);
726 if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_TX)) {
727 /* setup audio playback */
728 ads->out.buf_phys = dma_map_single(&ads->pdev->dev,
730 1 << PCM_BUFFER_MAX_SIZE_ORDER,
732 BUG_ON(!ads->out.buf_phys);
733 setup_dma_tx_request(&ads->out.dma_req, &ads->out);
735 tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
736 if (!ads->out.dma_chan) {
737 pr_err("%s: error allocating output DMA channel: %ld\n",
738 __func__, PTR_ERR(ads->out.dma_chan));
744 if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_RX)) {
745 /* setup audio recording */
746 ads->in.buf_phys = dma_map_single(&ads->pdev->dev,
748 1 << PCM_BUFFER_MAX_SIZE_ORDER,
750 BUG_ON(!ads->in.buf_phys);
751 setup_dma_rx_request(&ads->in.dma_req, &ads->in);
753 tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
754 if (!ads->in.dma_chan) {
755 pr_err("%s: error allocating input DMA channel: %ld\n",
756 __func__, PTR_ERR(ads->in.dma_chan));
765 if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_RX)) {
766 dma_unmap_single(&ads->pdev->dev, ads->in.buf_phys,
767 1 << PCM_BUFFER_MAX_SIZE_ORDER, DMA_FROM_DEVICE);
768 tegra_dma_free_channel(ads->in.dma_chan);
769 ads->in.dma_chan = 0;
772 if ((ads->pdata->mask & TEGRA_AUDIO_ENABLE_TX)) {
773 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phys,
774 1 << PCM_BUFFER_MAX_SIZE_ORDER, DMA_TO_DEVICE);
775 tegra_dma_free_channel(ads->out.dma_chan);
776 ads->out.dma_chan = 0;
782 static void tear_down_dma(struct audio_driver_state *ads)
784 pr_info("%s\n", __func__);
786 tegra_dma_free_channel(ads->out.dma_chan);
787 ads->out.dma_chan = NULL;
788 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phys,
791 ads->out.buf_phys = 0;
793 tegra_dma_free_channel(ads->in.dma_chan);
794 ads->in.dma_chan = NULL;
795 dma_unmap_single(&ads->pdev->dev, ads->in.buf_phys,
798 ads->in.buf_phys = 0;
801 static void dma_tx_complete_callback(struct tegra_dma_req *req)
804 struct audio_stream *aos = req->dev;
805 int count = req->bytes_transferred;
807 u64 max_delay_us = count * 10000 / (4 * 441);
809 pr_debug("%s bytes transferred %d\n", __func__, count);
811 aos->dma_has_it = false;
812 delta_us = ktime_to_us(ktime_sub(ktime_get_real(), aos->last_dma_ts));
814 if (delta_us > max_delay_us) {
815 pr_debug("%s: too late by %lld us\n", __func__,
816 delta_us - max_delay_us);
817 aos->errors.late_dma++;
820 kfifo_dma_out_finish(&aos->fifo, count);
821 dma_unmap_sg(NULL, &aos->sg, 1, DMA_TO_DEVICE);
823 if (!completion_done(&aos->fifo_completion)) {
824 pr_debug("%s: complete (%d avail)\n", __func__,
825 kfifo_avail(&aos->fifo));
826 complete(&aos->fifo_completion);
829 if (stop_playback_if_necessary(aos)) {
830 pr_debug("%s: done (stopped)\n", __func__);
831 if (!completion_done(&aos->stop_completion)) {
832 pr_debug("%s: signalling stop completion\n", __func__);
833 complete(&aos->stop_completion);
838 spin_lock_irqsave(&aos->dma_req_lock, flags);
839 resume_dma_playback(aos);
840 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
843 static void dma_rx_complete_threshold(struct tegra_dma_req *req)
845 pr_debug("%s\n", __func__);
848 static void dma_rx_complete_callback(struct tegra_dma_req *req)
851 struct audio_stream *ais = req->dev;
852 int count = req->bytes_transferred;
854 spin_lock_irqsave(&ais->dma_req_lock, flags);
856 ais->dma_has_it = false;
858 pr_debug("%s(%d): transferred %d bytes (%d available in fifo)\n",
861 count, kfifo_avail(&ais->fifo));
863 BUG_ON(kfifo_avail(&ais->fifo) < count);
864 kfifo_dma_in_finish(&ais->fifo, count);
865 dma_unmap_sg(NULL, &ais->sg, 1, DMA_FROM_DEVICE);
867 if (!completion_done(&ais->fifo_completion)) {
868 pr_debug("%s: signalling fifo completion\n", __func__);
869 complete(&ais->fifo_completion);
872 if (stop_recording_if_necessary_nosync(ais)) {
873 spin_unlock_irqrestore(&ais->dma_req_lock, flags);
874 pr_debug("%s: done (stopped)\n", __func__);
875 if (!completion_done(&ais->stop_completion)) {
876 pr_debug("%s: signalling stop completion\n", __func__);
877 complete(&ais->stop_completion);
882 pr_debug("%s: resuming dma recording\n", __func__);
884 /* This call will fail if we try to set up a DMA request that's
887 (void)resume_dma_recording(ais);
888 spin_unlock_irqrestore(&ais->dma_req_lock, flags);
890 pr_debug("%s: done\n", __func__);
893 static void setup_dma_tx_request(struct tegra_dma_req *req,
894 struct audio_stream *aos)
896 struct audio_driver_state *ads = ads_from_out(aos);
898 memset(req, 0, sizeof(*req));
900 req->complete = dma_tx_complete_callback;
902 req->to_memory = false;
903 req->dest_addr = i2s_get_fifo_phy_base(ads->i2s_phys, I2S_FIFO_TX);
905 if (ads->bit_format == TEGRA_AUDIO_BIT_FORMAT_DSP)
906 req->dest_bus_width = ads->pdata->dsp_bus_width;
908 req->dest_bus_width = ads->pdata->i2s_bus_width;
909 req->source_bus_width = 32;
910 req->source_wrap = 0;
911 req->req_sel = ads->dma_req_sel;
914 static void setup_dma_rx_request(struct tegra_dma_req *req,
915 struct audio_stream *ais)
917 struct audio_driver_state *ads = ads_from_in(ais);
919 memset(req, 0, sizeof(*req));
921 req->complete = dma_rx_complete_callback;
922 req->threshold = dma_rx_complete_threshold;
924 req->to_memory = true;
925 req->source_addr = i2s_get_fifo_phy_base(ads->i2s_phys, I2S_FIFO_RX);
926 req->source_wrap = 4;
927 if (ads->bit_format == TEGRA_AUDIO_BIT_FORMAT_DSP)
928 req->source_bus_width = ads->pdata->dsp_bus_width;
930 req->source_bus_width = ads->pdata->i2s_bus_width;
931 req->dest_bus_width = 32;
933 req->req_sel = ads->dma_req_sel;
936 /* Called with aos->dma_req_lock taken. */
937 static int resume_dma_playback(struct audio_stream *aos)
940 struct audio_driver_state *ads = ads_from_out(aos);
941 struct tegra_dma_req *req = &aos->dma_req;
943 if (aos->dma_has_it) {
944 pr_debug("%s: playback already in progress\n", __func__);
948 rc = kfifo_dma_out_prepare(&aos->fifo, &aos->sg,
949 1, kfifo_len(&aos->fifo));
950 /* stop_playback_if_necessary() already checks to see if the fifo is
954 rc = dma_map_sg(NULL, &aos->sg, 1, DMA_TO_DEVICE);
956 pr_err("%s: could not map dma memory: %d\n", __func__, rc);
961 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_TX);
963 i2s_fifo_set_attention_level(ads->i2s_base,
964 I2S_FIFO_TX, aos->i2s_fifo_atn_level);
967 req->source_addr = sg_dma_address(&aos->sg);
968 req->size = sg_dma_len(&aos->sg);
969 dma_sync_single_for_device(NULL,
970 req->source_addr, req->size, DMA_TO_DEVICE);
972 /* Don't send all the data yet. */
973 if (req->size > chunk_size(aos))
974 req->size = chunk_size(aos);
975 pr_debug("%s resume playback (%d in fifo, writing %d)\n",
976 __func__, kfifo_len(&aos->fifo), req->size);
978 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
980 aos->last_dma_ts = ktime_get_real();
981 rc = tegra_dma_enqueue_req(aos->dma_chan, req);
982 aos->dma_has_it = !rc;
983 if (!aos->dma_has_it)
984 pr_err("%s: could not enqueue TX DMA req\n", __func__);
988 /* Called with aos->dma_req_lock taken. */
989 static int start_dma_playback(struct audio_stream *aos)
991 return resume_dma_playback(aos);
994 /* Called with aos->dma_req_lock taken. */
995 static void stop_dma_playback(struct audio_stream *aos)
998 struct audio_driver_state *ads = ads_from_out(aos);
999 pr_debug("%s\n", __func__);
1000 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 0);
1001 while ((i2s_get_status(ads->i2s_base) & I2S_I2S_FIFO_TX_BUSY) &&
1005 pr_info("%s: spin %d\n", __func__, spin);
1008 pr_warn("%s: spinny\n", __func__);
1011 /* This function may be called from either interrupt or process context. */
1012 /* Called with ais->dma_req_lock taken. */
1013 static int resume_dma_recording(struct audio_stream *ais)
1016 struct audio_driver_state *ads = ads_from_in(ais);
1017 struct tegra_dma_req *req = &ais->dma_req;
1019 BUG_ON(kfifo_is_full(&ais->fifo));
1021 if (ais->dma_has_it) {
1022 pr_debug("%s: recording already in progress\n", __func__);
1026 /* Don't send all the data yet. */
1027 if (req->size > chunk_size(ais))
1028 req->size = chunk_size(ais);
1029 rc = kfifo_dma_in_prepare(&ais->fifo, &ais->sg, 1,
1030 kfifo_avail(&ais->fifo));
1032 rc = dma_map_sg(NULL, &ais->sg, 1, DMA_FROM_DEVICE);
1034 pr_err("%s: coult not map dma for recording: %d\n",
1039 req->dest_addr = sg_dma_address(&ais->sg);
1040 req->size = round_down(sg_dma_len(&ais->sg), 4);
1043 pr_err("%s: invalid request size %d\n", __func__, req->size);
1047 dma_sync_single_for_device(NULL,
1048 req->dest_addr, req->size, DMA_FROM_DEVICE);
1050 ais->dma_has_it = !tegra_dma_enqueue_req(ais->dma_chan, &ais->dma_req);
1051 if (!ais->dma_has_it) {
1052 pr_err("%s: could not enqueue RX DMA req\n", __func__);
1057 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1059 i2s_fifo_set_attention_level(ads->i2s_base,
1060 I2S_FIFO_RX, ais->i2s_fifo_atn_level);
1061 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1065 /* Called with ais->dma_req_lock taken. */
1066 static int start_dma_recording(struct audio_stream *ais)
1068 pr_debug("%s\n", __func__);
1069 return resume_dma_recording(ais);
1072 /* Called with ais->dma_req_lock taken. */
1073 static void stop_dma_recording(struct audio_stream *ais)
1076 struct audio_driver_state *ads = ads_from_in(ais);
1077 pr_debug("%s\n", __func__);
1078 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1079 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1080 while ((i2s_get_status(ads->i2s_base) & I2S_I2S_FIFO_RX_BUSY) &&
1084 pr_info("%s: spin %d\n", __func__, spin);
1087 pr_warn("%s: spinny\n", __func__);
1092 static int setup_pio(struct audio_driver_state *ads)
1094 pr_info("%s\n", __func__);
1095 enable_irq(ads->irq);
1099 static void tear_down_pio(struct audio_driver_state *ads)
1101 pr_info("%s\n", __func__);
1102 disable_irq(ads->irq);
1105 static int start_pio_playback(struct audio_stream *aos)
1107 struct audio_driver_state *ads = ads_from_out(aos);
1109 if (i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_TX)) {
1110 pr_debug("%s: playback is already in progress\n", __func__);
1114 pr_debug("%s\n", __func__);
1116 i2s_fifo_set_attention_level(ads->i2s_base,
1117 I2S_FIFO_TX, aos->i2s_fifo_atn_level);
1119 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_TX);
1122 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
1124 i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_TX, 1);
1125 i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_TX, 1);
1130 static void stop_pio_playback(struct audio_stream *aos)
1132 struct audio_driver_state *ads = ads_from_out(aos);
1134 i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_TX, 0);
1135 i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_TX, 0);
1136 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 0);
1137 while (i2s_get_status(ads->i2s_base) & I2S_I2S_FIFO_TX_BUSY)
1140 pr_debug("%s: interrupts %d\n", __func__,
1141 ads->pio_stats.i2s_interrupt_count);
1142 pr_info("%s: sent %d\n", __func__,
1143 ads->pio_stats.tx_fifo_written);
1144 pr_info("%s: tx errors %d\n", __func__,
1145 ads->pio_stats.tx_fifo_errors);
1147 memset(&ads->pio_stats, 0, sizeof(ads->pio_stats));
1150 static int start_pio_recording(struct audio_stream *ais)
1152 struct audio_driver_state *ads = ads_from_in(ais);
1154 if (i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_RX)) {
1155 pr_debug("%s: already started\n", __func__);
1159 pr_debug("%s: start\n", __func__);
1161 i2s_fifo_set_attention_level(ads->i2s_base,
1162 I2S_FIFO_RX, ais->i2s_fifo_atn_level);
1164 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1167 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1169 i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_RX, 1);
1170 i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_RX, 1);
1175 static void stop_pio_recording(struct audio_stream *ais)
1177 struct audio_driver_state *ads = ads_from_in(ais);
1179 pr_debug("%s\n", __func__);
1181 i2s_set_fifo_irq_on_err(ads->i2s_base, I2S_FIFO_RX, 0);
1182 i2s_set_fifo_irq_on_qe(ads->i2s_base, I2S_FIFO_RX, 0);
1183 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1184 i2s_fifo_clear(ads->i2s_base, I2S_FIFO_RX);
1186 pr_debug("%s: interrupts %d\n", __func__,
1187 ads->pio_stats.i2s_interrupt_count);
1188 pr_debug("%s: received %d\n", __func__,
1189 ads->pio_stats.rx_fifo_read);
1190 pr_debug("%s: rx errors %d\n", __func__,
1191 ads->pio_stats.rx_fifo_errors);
1193 memset(&ads->pio_stats, 0, sizeof(ads->pio_stats));
1196 static irqreturn_t i2s_interrupt(int irq, void *data)
1198 struct audio_driver_state *ads = data;
1199 u32 status = i2s_get_status(ads->i2s_base);
1201 pr_debug("%s: %08x\n", __func__, status);
1203 ads->pio_stats.i2s_interrupt_count++;
1205 if (status & I2S_I2S_FIFO_TX_ERR)
1206 ads->pio_stats.tx_fifo_errors++;
1208 if (status & I2S_I2S_FIFO_RX_ERR) {
1209 ads->pio_stats.rx_fifo_errors++;
1210 ads->in.errors.full_empty++;
1213 if (status & I2S_FIFO_ERR)
1214 i2s_ack_status(ads->i2s_base);
1216 if (status & I2S_I2S_FIFO_TX_QS) {
1220 u16 fifo_buffer[32];
1222 struct audio_stream *out = &ads->out;
1224 if (!i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_TX)) {
1225 pr_debug("%s: tx fifo not enabled, skipping\n",
1230 pr_debug("%s tx fifo is ready\n", __func__);
1232 if (!completion_done(&out->fifo_completion)) {
1233 pr_debug("%s: tx complete (%d avail)\n", __func__,
1234 kfifo_avail(&out->fifo));
1235 complete(&out->fifo_completion);
1238 if (stop_playback_if_necessary(out)) {
1239 pr_debug("%s: done (stopped)\n", __func__);
1240 if (!completion_done(&out->stop_completion)) {
1241 pr_debug("%s: signalling stop completion\n",
1243 complete(&out->stop_completion);
1248 empty = i2s_get_fifo_full_empty_count(ads->i2s_base,
1251 len = kfifo_out(&out->fifo, fifo_buffer,
1252 empty * sizeof(u16));
1256 while (empty-- && written < len) {
1257 ads->pio_stats.tx_fifo_written += written * sizeof(u16);
1258 i2s_fifo_write(ads->i2s_base,
1259 I2S_FIFO_TX, fifo_buffer[written++]);
1262 /* TODO: Should we check to see if we wrote less than the
1263 * FIFO threshold and adjust it if so?
1267 /* start the transaction */
1268 pr_debug("%s: enabling fifo (%d samples written)\n",
1270 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_TX, 1);
1275 if (status & I2S_I2S_FIFO_RX_QS) {
1279 struct audio_stream *in = &ads->in;
1281 if (!i2s_is_fifo_enabled(ads->i2s_base, I2S_FIFO_RX)) {
1282 pr_debug("%s: rx fifo not enabled, skipping\n",
1287 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 0);
1289 full = i2s_get_fifo_full_empty_count(ads->i2s_base,
1292 pr_debug("%s rx fifo is ready (%d samples)\n", __func__, full);
1296 u16 sample = i2s_fifo_read(ads->i2s_base, I2S_FIFO_RX);
1297 kfifo_in(&in->fifo, &sample, sizeof(sample));
1300 ads->pio_stats.rx_fifo_read += full * sizeof(u16);
1302 if (!completion_done(&in->fifo_completion)) {
1303 pr_debug("%s: rx complete (%d avail)\n", __func__,
1304 kfifo_avail(&in->fifo));
1305 complete(&in->fifo_completion);
1308 if (stop_recording_if_necessary_nosync(&ads->in)) {
1309 pr_debug("%s: recording cancelled or fifo full\n",
1311 if (!completion_done(&ads->in.stop_completion)) {
1312 pr_debug("%s: signalling stop completion\n",
1314 complete(&ads->in.stop_completion);
1319 i2s_fifo_enable(ads->i2s_base, I2S_FIFO_RX, 1);
1323 pr_debug("%s: done %08x\n", __func__, i2s_get_status(ads->i2s_base));
1327 static ssize_t tegra_audio_write(struct file *file,
1328 const char __user *buf, size_t size, loff_t *off)
1330 ssize_t rc = 0, total = 0;
1333 struct audio_driver_state *ads = ads_from_misc_out(file);
1335 mutex_lock(&ads->out.lock);
1337 if (!IS_ALIGNED(size, 4)) {
1338 pr_err("%s: user size request %d not aligned to 4\n",
1344 pr_debug("%s: write %d bytes, %d available\n", __func__,
1345 size, kfifo_avail(&ads->out.fifo));
1348 if (ads->out.stop) {
1349 pr_info("%s: playback has been cancelled (%d/%d bytes)\n",
1350 __func__, total, size);
1354 rc = kfifo_from_user(&ads->out.fifo, buf + total, size - total, &nw);
1356 pr_err("%s: error copying from user\n", __func__);
1360 rc = start_playback(&ads->out);
1361 if (rc < 0 && rc != -EALREADY) {
1362 pr_err("%s: could not start playback: %d\n", __func__, rc);
1368 pr_debug("%s: sleep (user %d total %d nw %d)\n", __func__,
1370 mutex_unlock(&ads->out.lock);
1371 rc = wait_for_completion_interruptible(
1372 &ads->out.fifo_completion);
1373 mutex_lock(&ads->out.lock);
1374 if (rc == -ERESTARTSYS) {
1375 pr_warn("%s: interrupted\n", __func__);
1378 pr_debug("%s: awake\n", __func__);
1386 mutex_unlock(&ads->out.lock);
1390 static long tegra_audio_out_ioctl(struct file *file,
1391 unsigned int cmd, unsigned long arg)
1394 struct audio_driver_state *ads = ads_from_misc_out_ctl(file);
1395 struct audio_stream *aos = &ads->out;
1397 mutex_lock(&aos->lock);
1400 case TEGRA_AUDIO_OUT_SET_BUF_CONFIG: {
1401 struct tegra_audio_buf_config cfg;
1402 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg))) {
1406 if (kfifo_len(&aos->fifo)) {
1407 pr_err("%s: playback in progress\n", __func__);
1411 rc = init_stream_buffer(aos, &cfg, 0);
1414 aos->buf_config = cfg;
1417 case TEGRA_AUDIO_OUT_GET_BUF_CONFIG:
1418 if (copy_to_user((void __user *)arg, &aos->buf_config,
1419 sizeof(aos->buf_config)))
1422 case TEGRA_AUDIO_OUT_GET_ERROR_COUNT:
1423 if (copy_to_user((void __user *)arg, &aos->errors,
1424 sizeof(aos->errors)))
1427 memset(&aos->errors, 0, sizeof(aos->errors));
1429 case TEGRA_AUDIO_OUT_FLUSH:
1430 if (kfifo_len(&aos->fifo)) {
1431 pr_debug("%s: flushing\n", __func__);
1432 request_stop_nosync(aos);
1433 pr_debug("%s: flushed\n", __func__);
1441 mutex_unlock(&aos->lock);
1445 static long tegra_audio_ioctl(struct file *file,
1446 unsigned int cmd, unsigned long arg)
1449 struct audio_driver_state *ads = ads_from_misc_ctl(file);
1451 bool dma_restart = false;
1453 mutex_lock(&ads->out.lock);
1454 mutex_lock(&ads->in.lock);
1457 case TEGRA_AUDIO_SET_BIT_FORMAT:
1458 if (copy_from_user(&mode, (const void __user *)arg,
1463 dma_restart = (mode != ads->bit_format);
1465 case TEGRA_AUDIO_BIT_FORMAT_DEFAULT:
1466 i2s_set_bit_format(ads->i2s_base, ads->pdata->mode);
1467 ads->bit_format = mode;
1469 case TEGRA_AUDIO_BIT_FORMAT_DSP:
1470 i2s_set_bit_format(ads->i2s_base, I2S_BIT_FORMAT_DSP);
1471 ads->bit_format = mode;
1474 pr_err("%s: Invald PCM mode %d", __func__, mode);
1479 case TEGRA_AUDIO_GET_BIT_FORMAT:
1480 if (copy_to_user((void __user *)arg, &ads->bit_format,
1486 if (dma_restart && ads->using_dma) {
1487 pr_debug("%s: Restarting DMA due to configuration change.\n",
1489 if (kfifo_len(&ads->out.fifo) || ads->in.active) {
1490 pr_err("%s: dma busy, cannot restart.\n", __func__);
1494 sound_ops->tear_down(ads);
1495 sound_ops->setup(ads);
1499 mutex_unlock(&ads->in.lock);
1500 mutex_unlock(&ads->out.lock);
1504 static long tegra_audio_in_ioctl(struct file *file,
1505 unsigned int cmd, unsigned long arg)
1508 struct audio_driver_state *ads = ads_from_misc_in_ctl(file);
1509 struct audio_stream *ais = &ads->in;
1511 mutex_lock(&ais->lock);
1514 case TEGRA_AUDIO_IN_START:
1515 pr_debug("%s: start recording\n", __func__);
1517 rc = start_recording_if_necessary(ais);
1518 ais->active = !rc || rc == -EALREADY;
1520 case TEGRA_AUDIO_IN_STOP:
1521 pr_debug("%s: start recording\n", __func__);
1523 request_stop_nosync(ais);
1525 case TEGRA_AUDIO_IN_SET_CONFIG: {
1526 struct tegra_audio_in_config cfg;
1529 pr_err("%s: recording in progress\n", __func__);
1533 if (copy_from_user(&cfg, (const void __user *)arg,
1539 #ifdef SAMPLE_RATE_CONVERTER_IN_DRIVER
1542 ads->in_divs = divs_8000;
1543 ads->in_divs_len = ARRAY_SIZE(divs_8000);
1546 ads->in_divs = divs_11025;
1547 ads->in_divs_len = ARRAY_SIZE(divs_11025);
1550 ads->in_divs = divs_16000;
1551 ads->in_divs_len = ARRAY_SIZE(divs_16000);
1554 ads->in_divs = divs_22050;
1555 ads->in_divs_len = ARRAY_SIZE(divs_22050);
1558 ads->in_divs = divs_44100;
1559 ads->in_divs_len = ARRAY_SIZE(divs_44100);
1562 pr_err("%s: invalid sampling rate %d\n", __func__,
1568 if(cfg.stereo && !ads->pdata->stereo_capture) {
1569 pr_err("%s: not capable of stereo capture.",
1574 pr_info("%s: setting input sampling rate to %d, %s\n",
1576 cfg.stereo ? "stereo" : "mono");
1577 ads->in_config = cfg;
1578 ads->in_config.stereo = !!ads->in_config.stereo;
1582 case TEGRA_AUDIO_IN_GET_CONFIG:
1583 if (copy_to_user((void __user *)arg, &ads->in_config,
1584 sizeof(ads->in_config)))
1587 case TEGRA_AUDIO_IN_SET_BUF_CONFIG: {
1588 struct tegra_audio_buf_config cfg;
1589 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg))) {
1594 pr_err("%s: recording in progress\n", __func__);
1598 rc = init_stream_buffer(ais, &cfg, PCM_IN_BUFFER_PADDING);
1601 ais->buf_config = cfg;
1604 case TEGRA_AUDIO_IN_GET_BUF_CONFIG:
1605 if (copy_to_user((void __user *)arg, &ais->buf_config,
1606 sizeof(ais->buf_config)))
1609 case TEGRA_AUDIO_IN_GET_ERROR_COUNT:
1610 if (copy_to_user((void __user *)arg, &ais->errors,
1611 sizeof(ais->errors)))
1614 memset(&ais->errors, 0, sizeof(ais->errors));
1620 mutex_unlock(&ais->lock);
1624 static ssize_t __i2s_copy_to_user(struct audio_driver_state *ads,
1625 void __user *dst, int dst_size,
1626 void *src, int src_size,
1629 int bytes_written = dst_size < src_size ? dst_size : src_size;
1630 *num_consumed = bytes_written;
1631 if (copy_to_user(dst, src, bytes_written)) {
1632 pr_err("%s: error copying %d bytes to user\n", __func__,
1636 return bytes_written;
1639 #ifdef SAMPLE_RATE_CONVERTER_IN_DRIVER
1641 /* downsample a 16-bit 44.1kHz PCM stereo stream to stereo or mono 16-bit PCM
1645 static int downsample(const s16 *in, int in_len,
1646 s16 *out, int out_len,
1647 int *consumed, /* from input */
1648 const int *divs, int divs_len,
1651 /* Todo: Handle mono source streams */
1661 while (i + div * 2 <= in_len && oi + out_stereo < out_len) {
1662 for (j = 0, lsum = 0, rsum = 0; j < div; j++) {
1663 lsum += in[i + j * 2];
1664 rsum += in[i + j * 2 + 1];
1667 out[oi] = (lsum + rsum) / (div * 2);
1669 out[oi] = lsum / div;
1670 out[oi + 1] = rsum / div;
1673 oi += out_stereo + 1;
1675 div = divs[++di % divs_len];
1680 pr_debug("%s: in_len %d out_len %d consumed %d generated %d\n",
1681 __func__, in_len, out_len, *consumed, oi);
1685 static ssize_t __downsample_to_user(struct audio_driver_state *ads,
1686 void __user *dst, int dst_size,
1687 void *src, int src_size,
1692 pr_debug("%s\n", __func__);
1694 bytes_ds = downsample(src, src_size / sizeof(s16),
1695 src, dst_size / sizeof(s16),
1697 ads->in_divs, ads->in_divs_len,
1698 ads->in_config.stereo) * sizeof(s16);
1700 if (copy_to_user(dst, src, bytes_ds)) {
1701 pr_err("%s: error copying %d bytes to user\n", __func__,
1706 *num_consumed *= sizeof(s16);
1707 BUG_ON(*num_consumed > src_size);
1709 pr_debug("%s: generated %d, skipped %d, original in fifo %d\n",
1710 __func__, bytes_ds, *num_consumed, src_size);
1714 #endif /*SAMPLE_RATE_CONVERTER_IN_DRIVER*/
1716 static ssize_t downsample_to_user(struct audio_driver_state *ads,
1718 size_t size) /* bytes to write to user buffer */
1721 int bytes_consumed_from_fifo, bc_now;
1722 int bytes_ds, ds_now;
1723 bool take_two = false;
1725 struct scatterlist sgl[PCM_BUFFER_MAX_SIZE_ORDER - PAGE_SHIFT];
1726 sg_init_table(sgl, ARRAY_SIZE(sgl));
1729 pr_debug("%s: user buffer is full\n", __func__);
1733 if (kfifo_is_empty(&ads->in.fifo)) {
1734 pr_debug("%s: input fifo is empty\n", __func__);
1738 nr_sg = kfifo_dma_out_prepare(&ads->in.fifo,
1739 sgl, ARRAY_SIZE(sgl),
1740 kfifo_len(&ads->in.fifo));
1743 pr_debug("%s (fifo size %d)\n", __func__, size);
1746 bytes_consumed_from_fifo = 0;
1747 for (bytes_ds = 0, i = 0; i < nr_sg; i++) {
1748 BUG_ON(!sgl[i].length);
1751 #ifdef SAMPLE_RATE_CONVERTER_IN_DRIVER
1752 ds_now = __downsample_to_user(
1754 ds_now = __i2s_copy_to_user(
1758 sg_virt(&sgl[i]), sgl[i].length,
1761 if (!ds_now && !sg_is_last(sgl + i)) {
1767 /* The assumption is that this sgl entry is at the end
1768 * of the fifo, and there isn't enough space till the
1769 * end of the fifo for at least one target sample to be
1770 * generated. When this happens, we copy enough bytes
1771 * from the next sgl entry to the end of the buffer of
1772 * the current one, knowing that the copied bytes will
1773 * cause the fifo to wrap around. We adjust the next
1774 * entry, and continue with the loop.
1777 BUG_ON(sg_virt(&sgl[i]) + sgl[i].length !=
1778 ads->in.buffer + kfifo_size(&ads->in.fifo));
1780 if (sgl[i + 1].length < PCM_IN_BUFFER_PADDING) {
1781 pr_debug("%s: not enough data till end of fifo\n",
1786 memcpy(sg_virt(&sgl[i]) + sgl[i].length,
1787 sg_virt(&sgl[i + 1]),
1788 PCM_IN_BUFFER_PADDING);
1789 sgl[i].length += PCM_IN_BUFFER_PADDING;
1791 sg_set_buf(&sgl[i + 1],
1792 sg_virt(&sgl[i + 1]) + PCM_IN_BUFFER_PADDING,
1793 sgl[i + 1].length - PCM_IN_BUFFER_PADDING);
1800 BUG_ON(ds_now > size);
1802 bytes_consumed_from_fifo += bc_now;
1803 pr_debug("%s: downsampled (%d req, %d actual)" \
1804 " -> total ds %d (size %d)\n", __func__,
1805 sgl[i].length, bytes_consumed_from_fifo,
1807 if (sg_is_last(sgl + i))
1811 kfifo_dma_out_finish(&ads->in.fifo, bytes_consumed_from_fifo);
1816 static ssize_t tegra_audio_read(struct file *file, char __user *buf,
1817 size_t size, loff_t *off)
1819 ssize_t rc, total = 0;
1822 struct audio_driver_state *ads = ads_from_misc_in(file);
1824 mutex_lock(&ads->in.lock);
1826 if (!IS_ALIGNED(size, 4)) {
1827 pr_err("%s: user size request %d not aligned to 4\n",
1833 pr_debug("%s:%d: read %d bytes, %d available\n", __func__,
1835 size, kfifo_len(&ads->in.fifo));
1838 /* If we want recording to stop immediately after it gets cancelled,
1839 * then we do not want to wait for the fifo to get drained.
1841 if (ads->in.stop /* && kfifo_is_empty(&ads->in.fifo) */) {
1842 pr_debug("%s: recording has been cancelled (%d/%d bytes)\n",
1843 __func__, total, size);
1847 rc = start_recording_if_necessary(&ads->in);
1848 if (rc < 0 && rc != -EALREADY) {
1849 pr_err("%s: could not start recording\n", __func__);
1853 ads->in.active = true;
1857 nr = downsample_to_user(ads, buf + total, size - total);
1865 pr_debug("%s: copied %d bytes to user, total %d/%d\n",
1866 __func__, nr, total, size);
1869 mutex_unlock(&ads->in.lock);
1870 pr_debug("%s: sleep (user %d total %d nr %d)\n", __func__,
1872 rc = wait_for_completion_interruptible(
1873 &ads->in.fifo_completion);
1874 pr_debug("%s: awake\n", __func__);
1875 mutex_lock(&ads->in.lock);
1876 if (rc == -ERESTARTSYS) {
1877 pr_warn("%s: interrupted\n", __func__);
1883 pr_debug("%s: done reading %d bytes, %d available\n", __func__,
1884 total, kfifo_avail(&ads->in.fifo));
1891 mutex_unlock(&ads->in.lock);
1895 static int tegra_audio_out_open(struct inode *inode, struct file *file)
1897 struct audio_driver_state *ads = ads_from_misc_out(file);
1899 pr_debug("%s\n", __func__);
1901 mutex_lock(&ads->out.lock);
1902 if (!ads->out.opened++) {
1903 pr_debug("%s: resetting fifo and error count\n", __func__);
1904 ads->out.stop = false;
1905 memset(&ads->out.errors, 0, sizeof(ads->out.errors));
1906 kfifo_reset(&ads->out.fifo);
1908 mutex_unlock(&ads->out.lock);
1913 static int tegra_audio_out_release(struct inode *inode, struct file *file)
1915 struct audio_driver_state *ads = ads_from_misc_out(file);
1917 pr_debug("%s\n", __func__);
1919 mutex_lock(&ads->out.lock);
1920 if (ads->out.opened)
1922 if (!ads->out.opened) {
1923 stop_playback_if_necessary(&ads->out);
1924 if (wake_lock_active(&ads->out.wake_lock))
1925 pr_err("%s: wake lock is still held!\n", __func__);
1926 if (kfifo_len(&ads->out.fifo))
1927 pr_err("%s: output fifo is not empty (%d bytes left)\n",
1928 __func__, kfifo_len(&ads->out.fifo));
1929 allow_suspend(&ads->out);
1931 mutex_unlock(&ads->out.lock);
1936 static int tegra_audio_in_open(struct inode *inode, struct file *file)
1938 struct audio_driver_state *ads = ads_from_misc_in(file);
1940 pr_debug("%s\n", __func__);
1942 mutex_lock(&ads->in.lock);
1943 if (!ads->in.opened++) {
1944 pr_debug("%s: resetting fifo\n", __func__);
1945 /* By default, do not start recording when someone reads from
1948 ads->in.stop = false;
1949 memset(&ads->in.errors, 0, sizeof(ads->in.errors));
1950 kfifo_reset(&ads->in.fifo);
1952 mutex_unlock(&ads->in.lock);
1954 pr_debug("%s: done\n", __func__);
1958 static int tegra_audio_in_release(struct inode *inode, struct file *file)
1960 struct audio_driver_state *ads = ads_from_misc_in(file);
1962 pr_debug("%s\n", __func__);
1964 mutex_lock(&ads->in.lock);
1968 if (!ads->in.opened) {
1970 request_stop_nosync(&ads->in);
1971 if (wake_lock_active(&ads->in.wake_lock))
1972 pr_err("%s: wake lock is still held!\n", __func__);
1973 if (kfifo_len(&ads->in.fifo))
1974 pr_err("%s: input fifo is not empty (%d bytes left)\n",
1975 __func__, kfifo_len(&ads->in.fifo));
1976 allow_suspend(&ads->in);
1979 mutex_unlock(&ads->in.lock);
1980 pr_debug("%s: done\n", __func__);
1984 static const struct file_operations tegra_audio_out_fops = {
1985 .owner = THIS_MODULE,
1986 .open = tegra_audio_out_open,
1987 .release = tegra_audio_out_release,
1988 .write = tegra_audio_write,
1991 static const struct file_operations tegra_audio_in_fops = {
1992 .owner = THIS_MODULE,
1993 .open = tegra_audio_in_open,
1994 .read = tegra_audio_read,
1995 .release = tegra_audio_in_release,
1998 static int tegra_audio_ctl_open(struct inode *inode, struct file *file)
2003 static int tegra_audio_ctl_release(struct inode *inode, struct file *file)
2008 static const struct file_operations tegra_audio_out_ctl_fops = {
2009 .owner = THIS_MODULE,
2010 .open = tegra_audio_ctl_open,
2011 .release = tegra_audio_ctl_release,
2012 .unlocked_ioctl = tegra_audio_out_ioctl,
2015 static const struct file_operations tegra_audio_in_ctl_fops = {
2016 .owner = THIS_MODULE,
2017 .open = tegra_audio_ctl_open,
2018 .release = tegra_audio_ctl_release,
2019 .unlocked_ioctl = tegra_audio_in_ioctl,
2022 static const struct file_operations tegra_audio_ctl_fops = {
2023 .owner = THIS_MODULE,
2024 .open = tegra_audio_ctl_open,
2025 .release = tegra_audio_ctl_release,
2026 .unlocked_ioctl = tegra_audio_ioctl,
2029 static int init_stream_buffer(struct audio_stream *s,
2030 struct tegra_audio_buf_config *cfg,
2033 pr_debug("%s (size %d threshold %d chunk %d)\n", __func__,
2034 cfg->size, cfg->threshold, cfg->chunk);
2036 if (cfg->chunk < PCM_DMA_CHUNK_MIN_SIZE_ORDER) {
2037 pr_err("%s: chunk %d too small (%d min)\n", __func__,
2038 cfg->chunk, PCM_DMA_CHUNK_MIN_SIZE_ORDER);
2042 if (cfg->chunk > cfg->size) {
2043 pr_err("%s: chunk %d > size %d\n", __func__,
2044 cfg->chunk, cfg->size);
2048 if (cfg->threshold > cfg->size) {
2049 pr_err("%s: threshold %d > size %d\n", __func__,
2050 cfg->threshold, cfg->size);
2054 if ((1 << cfg->size) < padding) {
2055 pr_err("%s: size %d < buffer padding %d (bytes)\n", __func__,
2056 cfg->size, padding);
2060 if (cfg->size > PCM_BUFFER_MAX_SIZE_ORDER) {
2061 pr_err("%s: size %d exceeds max %d\n", __func__,
2062 cfg->size, PCM_BUFFER_MAX_SIZE_ORDER);
2067 pr_debug("%s: allocating buffer (size %d, padding %d)\n",
2068 __func__, 1 << cfg->size, padding);
2069 s->buffer = kmalloc((1 << cfg->size) + padding,
2070 GFP_KERNEL | GFP_DMA);
2073 pr_err("%s: could not allocate output buffer\n", __func__);
2077 kfifo_init(&s->fifo, s->buffer, 1 << cfg->size);
2078 sg_init_table(&s->sg, 1);
2083 static int setup_misc_device(struct miscdevice *misc,
2084 const struct file_operations *fops,
2085 const char *fmt, ...)
2091 va_start(args, fmt);
2093 memset(misc, 0, sizeof(*misc));
2094 misc->minor = MISC_DYNAMIC_MINOR;
2095 misc->name = kmalloc(sz, GFP_KERNEL);
2101 vsnprintf((char *)misc->name, sz, fmt, args);
2103 if (misc_register(misc)) {
2104 pr_err("%s: could not register %s\n", __func__, misc->name);
2115 static ssize_t dma_toggle_show(struct device *dev,
2116 struct device_attribute *attr,
2119 struct tegra_audio_platform_data *pdata = dev->platform_data;
2120 struct audio_driver_state *ads = pdata->driver_data;
2121 return sprintf(buf, "%s\n", ads->using_dma ? "dma" : "pio");
2124 static ssize_t dma_toggle_store(struct device *dev,
2125 struct device_attribute *attr,
2126 const char *buf, size_t count)
2129 struct tegra_audio_platform_data *pdata = dev->platform_data;
2130 struct audio_driver_state *ads = pdata->driver_data;
2136 if (!strncmp(buf, "dma", 3))
2138 else if (strncmp(buf, "pio", 3)) {
2139 dev_err(dev, "%s: invalid string [%s]\n", __func__, buf);
2143 mutex_lock(&ads->out.lock);
2144 mutex_lock(&ads->in.lock);
2145 if (kfifo_len(&ads->out.fifo) || ads->in.active) {
2146 dev_err(dev, "%s: playback or recording in progress.\n",
2148 mutex_unlock(&ads->in.lock);
2149 mutex_unlock(&ads->out.lock);
2152 if (!!use_dma ^ !!ads->using_dma)
2155 dev_info(dev, "%s: no change\n", __func__);
2156 mutex_unlock(&ads->in.lock);
2157 mutex_unlock(&ads->out.lock);
2162 static DEVICE_ATTR(dma_toggle, 0644, dma_toggle_show, dma_toggle_store);
2164 static ssize_t __attr_fifo_atn_read(char *buf, int atn_lvl)
2167 case I2S_FIFO_ATN_LVL_ONE_SLOT:
2168 strncpy(buf, "1\n", 2);
2170 case I2S_FIFO_ATN_LVL_FOUR_SLOTS:
2171 strncpy(buf, "4\n", 2);
2173 case I2S_FIFO_ATN_LVL_EIGHT_SLOTS:
2174 strncpy(buf, "8\n", 2);
2176 case I2S_FIFO_ATN_LVL_TWELVE_SLOTS:
2177 strncpy(buf, "12\n", 3);
2185 static ssize_t __attr_fifo_atn_write(struct audio_driver_state *ads,
2186 struct audio_stream *as,
2188 const char *buf, size_t size)
2193 pr_err("%s: buffer size %d too big\n", __func__, size);
2197 if (sscanf(buf, "%d", &lvl) != 1) {
2198 pr_err("%s: invalid input string [%s]\n", __func__, buf);
2204 lvl = I2S_FIFO_ATN_LVL_ONE_SLOT;
2207 lvl = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
2210 lvl = I2S_FIFO_ATN_LVL_EIGHT_SLOTS;
2213 lvl = I2S_FIFO_ATN_LVL_TWELVE_SLOTS;
2216 pr_err("%s: invalid attention level %d\n", __func__, lvl);
2221 pr_info("%s: fifo level %d\n", __func__, *fifo_lvl);
2226 static ssize_t tx_fifo_atn_show(struct device *dev,
2227 struct device_attribute *attr,
2230 struct tegra_audio_platform_data *pdata = dev->platform_data;
2231 struct audio_driver_state *ads = pdata->driver_data;
2232 return __attr_fifo_atn_read(buf, ads->out.i2s_fifo_atn_level);
2235 static ssize_t tx_fifo_atn_store(struct device *dev,
2236 struct device_attribute *attr,
2237 const char *buf, size_t count)
2240 struct tegra_audio_platform_data *pdata = dev->platform_data;
2241 struct audio_driver_state *ads = pdata->driver_data;
2242 mutex_lock(&ads->out.lock);
2243 if (kfifo_len(&ads->out.fifo)) {
2244 pr_err("%s: playback in progress.\n", __func__);
2248 rc = __attr_fifo_atn_write(ads, &ads->out,
2249 &ads->out.i2s_fifo_atn_level,
2252 mutex_unlock(&ads->out.lock);
2256 static DEVICE_ATTR(tx_fifo_atn, 0644, tx_fifo_atn_show, tx_fifo_atn_store);
2258 static ssize_t rx_fifo_atn_show(struct device *dev,
2259 struct device_attribute *attr,
2262 struct tegra_audio_platform_data *pdata = dev->platform_data;
2263 struct audio_driver_state *ads = pdata->driver_data;
2264 return __attr_fifo_atn_read(buf, ads->in.i2s_fifo_atn_level);
2267 static ssize_t rx_fifo_atn_store(struct device *dev,
2268 struct device_attribute *attr,
2269 const char *buf, size_t count)
2272 struct tegra_audio_platform_data *pdata = dev->platform_data;
2273 struct audio_driver_state *ads = pdata->driver_data;
2274 mutex_lock(&ads->in.lock);
2275 if (ads->in.active) {
2276 pr_err("%s: recording in progress.\n", __func__);
2280 rc = __attr_fifo_atn_write(ads, &ads->in,
2281 &ads->in.i2s_fifo_atn_level,
2284 mutex_unlock(&ads->in.lock);
2288 static DEVICE_ATTR(rx_fifo_atn, 0644, rx_fifo_atn_show, rx_fifo_atn_store);
2290 static int tegra_audio_probe(struct platform_device *pdev)
2293 struct resource *res;
2294 struct clk *i2s_clk, *audio_sync_clk, *dap_mclk;
2295 struct audio_driver_state *state;
2297 pr_info("%s\n", __func__);
2299 state = kzalloc(sizeof(*state), GFP_KERNEL);
2304 state->pdata = pdev->dev.platform_data;
2305 state->pdata->driver_data = state;
2306 BUG_ON(!state->pdata);
2308 if (!(state->pdata->mask &
2309 (TEGRA_AUDIO_ENABLE_TX | TEGRA_AUDIO_ENABLE_RX))) {
2310 dev_err(&pdev->dev, "neither tx nor rx is enabled!\n");
2314 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2316 dev_err(&pdev->dev, "no mem resource!\n");
2320 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
2321 dev_err(&pdev->dev, "memory region already claimed!\n");
2325 state->i2s_phys = res->start;
2326 state->i2s_base = (unsigned long)ioremap(res->start,
2327 res->end - res->start + 1);
2328 if (!state->i2s_base) {
2329 dev_err(&pdev->dev, "cannot remap iomem!\n");
2333 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
2335 dev_err(&pdev->dev, "no dma resource!\n");
2338 state->dma_req_sel = res->start;
2340 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2342 dev_err(&pdev->dev, "no irq resource!\n");
2345 state->irq = res->start;
2347 memset(&state->pio_stats, 0, sizeof(state->pio_stats));
2349 i2s_clk = clk_get(&pdev->dev, NULL);
2351 dev_err(&pdev->dev, "%s: could not get i2s1 clock\n",
2356 clk_set_rate(i2s_clk, state->pdata->i2s_clk_rate);
2357 if (clk_enable(i2s_clk)) {
2358 dev_err(&pdev->dev, "%s: failed to enable i2s1 clock\n",
2362 pr_info("%s: i2s_clk rate %ld\n", __func__, clk_get_rate(i2s_clk));
2364 dap_mclk = tegra_get_clock_by_name(state->pdata->dap_clk);
2366 dev_err(&pdev->dev, "%s: could not get DAP clock\n",
2370 clk_enable(dap_mclk);
2372 audio_sync_clk = tegra_get_clock_by_name(state->pdata->audio_sync_clk);
2373 if (!audio_sync_clk) {
2374 dev_err(&pdev->dev, "%s: could not get audio_2x clock\n",
2378 clk_enable(audio_sync_clk);
2380 i2s_enable_fifos(state->i2s_base, 0);
2381 /* disable interrupts from I2S */
2382 i2s_fifo_clear(state->i2s_base, I2S_FIFO_TX);
2383 i2s_fifo_clear(state->i2s_base, I2S_FIFO_RX);
2384 i2s_set_left_right_control_polarity(state->i2s_base, 0); /* default */
2385 if (state->pdata->master)
2386 i2s_set_channel_bit_count(state->i2s_base, 44100,
2387 clk_get_rate(i2s_clk));
2388 i2s_set_master(state->i2s_base, state->pdata->master);
2389 i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_TX, 1);
2390 i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_RX, 0);
2391 i2s_set_bit_format(state->i2s_base, state->pdata->mode);
2392 i2s_set_bit_size(state->i2s_base, state->pdata->bit_size);
2393 i2s_set_fifo_format(state->i2s_base, state->pdata->fifo_fmt);
2395 if ((state->pdata->mask & TEGRA_AUDIO_ENABLE_TX)) {
2396 state->out.opened = 0;
2397 state->out.active = false;
2398 mutex_init(&state->out.lock);
2399 init_completion(&state->out.fifo_completion);
2400 init_completion(&state->out.stop_completion);
2401 spin_lock_init(&state->out.dma_req_lock);
2402 state->out.buf_phys = 0;
2403 state->out.dma_chan = NULL;
2404 state->out.dma_has_it = false;
2406 state->out.i2s_fifo_atn_level = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
2407 state->out.buffer = 0;
2408 state->out.buf_config.size = PCM_BUFFER_MAX_SIZE_ORDER;
2409 state->out.buf_config.threshold = PCM_BUFFER_THRESHOLD_ORDER;
2410 state->out.buf_config.chunk = PCM_BUFFER_DMA_CHUNK_SIZE_ORDER;
2411 rc = init_stream_buffer(&state->out, &state->out.buf_config, 0);
2415 pm_qos_add_request(&state->out.pm_qos, PM_QOS_CPU_DMA_LATENCY,
2416 PM_QOS_DEFAULT_VALUE);
2418 snprintf(state->out.wake_lock_name,
2419 sizeof(state->out.wake_lock_name),
2420 "i2s.%d-audio-out", state->pdev->id);
2421 wake_lock_init(&state->out.wake_lock, WAKE_LOCK_SUSPEND,
2422 state->out.wake_lock_name);
2424 rc = setup_misc_device(&state->misc_out,
2425 &tegra_audio_out_fops,
2426 "audio%d_out", state->pdev->id);
2430 rc = setup_misc_device(&state->misc_out_ctl,
2431 &tegra_audio_out_ctl_fops,
2432 "audio%d_out_ctl", state->pdev->id);
2437 if ((state->pdata->mask & TEGRA_AUDIO_ENABLE_RX)) {
2438 state->in.opened = 0;
2439 state->in.active = false;
2440 mutex_init(&state->in.lock);
2441 init_completion(&state->in.fifo_completion);
2442 init_completion(&state->in.stop_completion);
2443 spin_lock_init(&state->in.dma_req_lock);
2444 state->in.buf_phys = 0;
2445 state->in.dma_chan = NULL;
2446 state->in.dma_has_it = false;
2448 state->in.i2s_fifo_atn_level = I2S_FIFO_ATN_LVL_FOUR_SLOTS;
2449 state->in.buffer = 0;
2450 state->in.buf_config.size = PCM_BUFFER_MAX_SIZE_ORDER;
2451 state->in.buf_config.threshold = PCM_BUFFER_THRESHOLD_ORDER;
2452 state->in.buf_config.chunk = PCM_BUFFER_DMA_CHUNK_SIZE_ORDER;
2453 rc = init_stream_buffer(&state->in, &state->in.buf_config,
2454 PCM_IN_BUFFER_PADDING);
2458 pm_qos_add_request(&state->in.pm_qos, PM_QOS_CPU_DMA_LATENCY,
2459 PM_QOS_DEFAULT_VALUE);
2461 snprintf(state->in.wake_lock_name,
2462 sizeof(state->in.wake_lock_name),
2463 "i2s.%d-audio-in", state->pdev->id);
2464 wake_lock_init(&state->in.wake_lock, WAKE_LOCK_SUSPEND,
2465 state->in.wake_lock_name);
2467 rc = setup_misc_device(&state->misc_in,
2468 &tegra_audio_in_fops,
2469 "audio%d_in", state->pdev->id);
2473 rc = setup_misc_device(&state->misc_in_ctl,
2474 &tegra_audio_in_ctl_fops,
2475 "audio%d_in_ctl", state->pdev->id);
2480 if (request_irq(state->irq, i2s_interrupt,
2481 IRQF_DISABLED, state->pdev->name, state) < 0) {
2483 "%s: could not register handler for irq %d\n",
2484 __func__, state->irq);
2488 rc = setup_misc_device(&state->misc_ctl,
2489 &tegra_audio_ctl_fops,
2490 "audio%d_ctl", state->pdev->id);
2494 state->using_dma = state->pdata->dma_on;
2495 if (!state->using_dma)
2496 sound_ops = &pio_sound_ops;
2497 sound_ops->setup(state);
2499 rc = device_create_file(&pdev->dev, &dev_attr_dma_toggle);
2501 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2502 __func__, dev_attr_dma_toggle.attr.name, rc);
2506 rc = device_create_file(&pdev->dev, &dev_attr_tx_fifo_atn);
2508 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2509 __func__, dev_attr_tx_fifo_atn.attr.name, rc);
2513 rc = device_create_file(&pdev->dev, &dev_attr_rx_fifo_atn);
2515 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
2516 __func__, dev_attr_rx_fifo_atn.attr.name, rc);
2520 state->in_config.rate = 11025;
2521 state->in_config.stereo = false;
2522 state->in_divs = divs_11025;
2523 state->in_divs_len = ARRAY_SIZE(divs_11025);
2529 static int tegra_audio_suspend(struct platform_device *pdev, pm_message_t mesg)
2531 /* dev_info(&pdev->dev, "%s\n", __func__); */
2535 static int tegra_audio_resume(struct platform_device *pdev)
2537 struct tegra_audio_platform_data *pdata = pdev->dev.platform_data;
2538 struct audio_driver_state *state = pdata->driver_data;
2540 /* dev_info(&pdev->dev, "%s\n", __func__); */
2545 /* disable interrupts from I2S */
2546 i2s_fifo_clear(state->i2s_base, I2S_FIFO_TX);
2547 i2s_fifo_clear(state->i2s_base, I2S_FIFO_RX);
2548 i2s_enable_fifos(state->i2s_base, 0);
2550 i2s_set_left_right_control_polarity(state->i2s_base, 0); /* default */
2552 if (state->pdata->master)
2553 i2s_set_channel_bit_count(state->i2s_base, 44100,
2554 state->pdata->i2s_clk_rate);
2555 i2s_set_master(state->i2s_base, state->pdata->master);
2557 i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_TX, 1);
2558 i2s_set_fifo_mode(state->i2s_base, I2S_FIFO_RX, 0);
2560 if (state->bit_format == TEGRA_AUDIO_BIT_FORMAT_DSP)
2561 i2s_set_bit_format(state->i2s_base, I2S_BIT_FORMAT_DSP);
2563 i2s_set_bit_format(state->i2s_base, state->pdata->mode);
2564 i2s_set_bit_size(state->i2s_base, state->pdata->bit_size);
2565 i2s_set_fifo_format(state->i2s_base, state->pdata->fifo_fmt);
2569 #endif /* CONFIG_PM */
2571 static struct platform_driver tegra_audio_driver = {
2574 .owner = THIS_MODULE,
2576 .probe = tegra_audio_probe,
2578 .suspend = tegra_audio_suspend,
2579 .resume = tegra_audio_resume,
2583 static int __init tegra_audio_init(void)
2585 return platform_driver_register(&tegra_audio_driver);
2588 module_init(tegra_audio_init);
2589 MODULE_LICENSE("GPL");