2 * arch/arm/mach-tegra/tegra_spdif_audio.c
4 * S/PDIF audio driver for NVIDIA Tegra SoCs
6 * Copyright (c) 2008-2009, NVIDIA Corporation.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
27 #include <linux/mutex.h>
28 #include <linux/clk.h>
29 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
32 #include <linux/spinlock.h>
33 #include <linux/uaccess.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dmapool.h>
36 #include <linux/err.h>
37 #include <linux/spi/spi.h>
38 #include <linux/kfifo.h>
39 #include <linux/debugfs.h>
40 #include <linux/completion.h>
41 #include <linux/platform_device.h>
42 #include <linux/device.h>
44 #include <linux/ktime.h>
45 #include <linux/sysfs.h>
46 #include <linux/wakelock.h>
47 #include <linux/delay.h>
48 #include <linux/tegra_audio.h>
50 #include <linux/workqueue.h>
53 #include <mach/iomap.h>
54 #include <mach/spdif.h>
55 #include <mach/audio.h>
56 #include <mach/irqs.h>
60 #define PCM_BUFFER_MAX_SIZE_ORDER (PAGE_SHIFT)
62 #define SPDIF_MAX_NUM_BUFS 4
63 /* Todo: Add IOCTL to configure the number of buffers. */
64 #define SPDIF_DEFAULT_TX_NUM_BUFS 2
65 #define SPDIF_DEFAULT_RX_NUM_BUFS 2
66 /* per stream (input/output) */
71 bool active; /* is DMA in progress? */
73 void *buffer[SPDIF_MAX_NUM_BUFS];
74 dma_addr_t buf_phy[SPDIF_MAX_NUM_BUFS];
75 struct completion comp[SPDIF_MAX_NUM_BUFS];
76 struct tegra_dma_req dma_req[SPDIF_MAX_NUM_BUFS];
79 int spdif_fifo_atn_level;
81 struct tegra_dma_channel *dma_chan;
83 struct completion stop_completion;
84 spinlock_t dma_req_lock;
86 struct work_struct allow_suspend_work;
87 struct wake_lock wake_lock;
88 char wake_lock_name[100];
91 struct audio_driver_state {
92 struct list_head next;
94 struct platform_device *pdev;
95 struct tegra_audio_platform_data *pdata;
96 phys_addr_t spdif_phys;
97 unsigned long spdif_base;
99 unsigned long dma_req_sel;
104 struct miscdevice misc_out;
105 struct miscdevice misc_out_ctl;
106 struct audio_stream out;
109 static inline bool pending_buffer_requests(struct audio_stream *stream)
112 for (i = 0; i < stream->num_bufs; i++)
113 if (!completion_done(&stream->comp[i]))
118 static inline int buf_size(struct audio_stream *s __attribute__((unused)))
120 return 1 << PCM_BUFFER_MAX_SIZE_ORDER;
123 static inline struct audio_driver_state *ads_from_misc_out(struct file *file)
125 struct miscdevice *m = file->private_data;
126 struct audio_driver_state *ads =
127 container_of(m, struct audio_driver_state, misc_out);
132 static inline struct audio_driver_state *ads_from_misc_out_ctl(
135 struct miscdevice *m = file->private_data;
136 struct audio_driver_state *ads =
137 container_of(m, struct audio_driver_state,
143 static inline struct audio_driver_state *ads_from_out(
144 struct audio_stream *aos)
146 return container_of(aos, struct audio_driver_state, out);
149 static inline void prevent_suspend(struct audio_stream *as)
151 pr_debug("%s\n", __func__);
152 cancel_work_sync(&as->allow_suspend_work);
153 wake_lock(&as->wake_lock);
156 static void allow_suspend_worker(struct work_struct *w)
158 struct audio_stream *as = container_of(w,
159 struct audio_stream, allow_suspend_work);
160 pr_debug("%s\n", __func__);
161 wake_unlock(&as->wake_lock);
164 static inline void allow_suspend(struct audio_stream *as)
166 schedule_work(&as->allow_suspend_work);
169 #define I2S_I2S_FIFO_TX_BUSY I2S_I2S_STATUS_FIFO1_BSY
170 #define I2S_I2S_FIFO_TX_QS I2S_I2S_STATUS_QS_FIFO1
171 #define I2S_I2S_FIFO_TX_ERR I2S_I2S_STATUS_FIFO1_ERR
173 #define I2S_I2S_FIFO_RX_BUSY I2S_I2S_STATUS_FIFO2_BSY
174 #define I2S_I2S_FIFO_RX_QS I2S_I2S_STATUS_QS_FIFO2
175 #define I2S_I2S_FIFO_RX_ERR I2S_I2S_STATUS_FIFO2_ERR
177 #define I2S_FIFO_ERR (I2S_I2S_STATUS_FIFO1_ERR | I2S_I2S_STATUS_FIFO2_ERR)
180 static inline void spdif_writel(unsigned long base, u32 val, u32 reg)
182 writel(val, base + reg);
185 static inline u32 spdif_readl(unsigned long base, u32 reg)
187 return readl(base + reg);
190 static inline void spdif_fifo_write(unsigned long base, u32 data)
192 spdif_writel(base, data, SPDIF_DATA_OUT_0);
195 static int spdif_fifo_set_attention_level(unsigned long base,
200 if (level > SPDIF_FIFO_ATN_LVL_TWELVE_SLOTS) {
201 pr_err("%s: invalid fifo level selector %d\n", __func__,
206 val = spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
208 val &= ~SPDIF_DATA_FIFO_CSR_0_TX_ATN_LVL_MASK;
209 val |= level << SPDIF_DATA_FIFO_CSR_0_TX_ATN_LVL_SHIFT;
212 spdif_writel(base, val, SPDIF_DATA_FIFO_CSR_0);
216 static void spdif_fifo_enable(unsigned long base, int on)
218 u32 val = spdif_readl(base, SPDIF_CTRL_0);
219 val &= ~(SPDIF_CTRL_0_TX_EN | SPDIF_CTRL_0_TC_EN | SPDIF_CTRL_0_TU_EN);
220 val |= on ? (SPDIF_CTRL_0_TX_EN) : 0;
221 val |= on ? (SPDIF_CTRL_0_TC_EN) : 0;
223 spdif_writel(base, val, SPDIF_CTRL_0);
226 static bool spdif_is_fifo_enabled(unsigned long base)
228 u32 val = spdif_readl(base, SPDIF_CTRL_0);
229 return !!(val & SPDIF_CTRL_0_TX_EN);
233 static void spdif_fifo_clear(unsigned long base)
235 u32 val = spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
236 val &= ~(SPDIF_DATA_FIFO_CSR_0_TX_CLR | SPDIF_DATA_FIFO_CSR_0_TU_CLR);
237 val |= SPDIF_DATA_FIFO_CSR_0_TX_CLR | SPDIF_DATA_FIFO_CSR_0_TU_CLR;
238 spdif_writel(base, val, SPDIF_DATA_FIFO_CSR_0);
242 static int spdif_set_bit_mode(unsigned long base, unsigned mode)
244 u32 val = spdif_readl(base, SPDIF_CTRL_0);
245 val &= ~SPDIF_CTRL_0_BIT_MODE_MASK;
247 if (mode > SPDIF_BIT_MODE_MODERAW) {
248 pr_err("%s: invalid bit_size selector %d\n", __func__,
253 val |= mode << SPDIF_CTRL_0_BIT_MODE_SHIFT;
255 spdif_writel(base, val, SPDIF_CTRL_0);
259 static int spdif_set_fifo_packed(unsigned long base, unsigned on)
261 u32 val = spdif_readl(base, SPDIF_CTRL_0);
262 val &= ~SPDIF_CTRL_0_PACK;
263 val |= on ? SPDIF_CTRL_0_PACK : 0;
264 spdif_writel(base, val, SPDIF_CTRL_0);
269 static void spdif_set_fifo_irq_on_err(unsigned long base, int on)
271 u32 val = spdif_readl(base, SPDIF_CTRL_0);
272 val &= ~SPDIF_CTRL_0_IE_TXE;
273 val |= on ? SPDIF_CTRL_0_IE_TXE : 0;
274 spdif_writel(base, val, SPDIF_CTRL_0);
279 static void spdif_enable_fifos(unsigned long base, int on)
281 u32 val = spdif_readl(base, SPDIF_CTRL_0);
283 val |= SPDIF_CTRL_0_TX_EN | SPDIF_CTRL_0_TC_EN |
286 val &= ~(SPDIF_CTRL_0_TX_EN | SPDIF_CTRL_0_TC_EN |
287 SPDIF_CTRL_0_IE_TXE);
289 spdif_writel(base, val, SPDIF_CTRL_0);
292 static inline u32 spdif_get_status(unsigned long base)
294 return spdif_readl(base, SPDIF_STATUS_0);
297 static inline u32 spdif_get_control(unsigned long base)
299 return spdif_readl(base, SPDIF_CTRL_0);
302 static inline void spdif_ack_status(unsigned long base)
304 return spdif_writel(base, spdif_readl(base, SPDIF_STATUS_0),
308 static inline u32 spdif_get_fifo_scr(unsigned long base)
310 return spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
313 static inline phys_addr_t spdif_get_fifo_phy_base(unsigned long phy_base)
315 return phy_base + SPDIF_DATA_OUT_0;
318 static inline u32 spdif_get_fifo_full_empty_count(unsigned long base)
320 u32 val = spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
321 val = val >> SPDIF_DATA_FIFO_CSR_0_TD_EMPTY_COUNT_SHIFT;
322 return val & SPDIF_DATA_FIFO_CSR_0_TD_EMPTY_COUNT_MASK;
326 static int spdif_set_sample_rate(struct audio_driver_state *state,
327 unsigned int sample_rate)
329 unsigned int clock_freq = 0;
330 struct clk *spdif_clk;
332 unsigned int ch_sta[] = {
333 0x0, /* 44.1, default values */
341 switch (sample_rate) {
343 clock_freq = 4096000; /* 4.0960 MHz */
344 ch_sta[0] = 0x3 << 24;
345 ch_sta[1] = 0xC << 4;
348 clock_freq = 5644800; /* 5.6448 MHz */
350 ch_sta[1] = 0xF << 4;
353 clock_freq = 6144000; /* 6.1440MHz */
354 ch_sta[0] = 0x2 << 24;
355 ch_sta[1] = 0xD << 4;
358 clock_freq = 11289600; /* 11.2896 MHz */
361 clock_freq = 12288000; /* 12.288 MHz */
364 clock_freq = 22579200; /* 22.5792 MHz */
367 clock_freq = 24576000; /* 24.5760 MHz */
373 spdif_clk = clk_get(&state->pdev->dev, NULL);
375 dev_err(&state->pdev->dev, "%s: could not get spdif clock\n",
380 clk_set_rate(spdif_clk, clock_freq);
381 if (clk_enable(spdif_clk)) {
382 dev_err(&state->pdev->dev,
383 "%s: failed to enable spdif_clk clock\n", __func__);
386 pr_info("%s: spdif_clk rate %ld\n", __func__, clk_get_rate(spdif_clk));
388 spdif_writel(state->spdif_base, ch_sta[0], SPDIF_CH_STA_TX_A_0);
389 spdif_writel(state->spdif_base, ch_sta[1], SPDIF_CH_STA_TX_B_0);
390 spdif_writel(state->spdif_base, ch_sta[2], SPDIF_CH_STA_TX_C_0);
391 spdif_writel(state->spdif_base, ch_sta[3], SPDIF_CH_STA_TX_D_0);
392 spdif_writel(state->spdif_base, ch_sta[4], SPDIF_CH_STA_TX_E_0);
393 spdif_writel(state->spdif_base, ch_sta[5], SPDIF_CH_STA_TX_F_0);
398 static int init_stream_buffer(struct audio_stream *, int);
400 static int setup_dma(struct audio_driver_state *);
401 static void tear_down_dma(struct audio_driver_state *);
402 static void stop_dma_playback(struct audio_stream *);
406 int (*setup)(struct audio_driver_state *);
407 void (*tear_down)(struct audio_driver_state *);
408 void (*stop_playback)(struct audio_stream *);
411 static const struct sound_ops dma_sound_ops = {
413 .tear_down = tear_down_dma,
414 .stop_playback = stop_dma_playback,
417 static const struct sound_ops *sound_ops = &dma_sound_ops;
421 static bool stop_playback_if_necessary(struct audio_stream *aos)
424 spin_lock_irqsave(&aos->dma_req_lock, flags);
425 pr_debug("%s\n", __func__);
426 if (!pending_buffer_requests(aos)) {
427 pr_debug("%s: no more data to play back\n", __func__);
428 sound_ops->stop_playback(aos);
429 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
433 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
439 static bool wait_till_stopped(struct audio_stream *as)
442 pr_debug("%s: wait for completion\n", __func__);
443 rc = wait_for_completion_timeout(
444 &as->stop_completion, HZ);
446 pr_err("%s: wait timed out", __func__);
448 pr_err("%s: wait error %d\n", __func__, rc);
450 pr_debug("%s: done: %d\n", __func__, rc);
454 /* Ask for playback to stop. The _nosync means that
455 * as->lock has to be locked by the caller.
457 static void request_stop_nosync(struct audio_stream *as)
460 pr_debug("%s\n", __func__);
463 if (pending_buffer_requests(as))
464 wait_till_stopped(as);
465 for (i = 0; i < as->num_bufs; i++) {
466 init_completion(&as->comp[i]);
467 complete(&as->comp[i]);
470 if (!tegra_dma_is_empty(as->dma_chan))
471 pr_err("%s: DMA not empty!\n", __func__);
472 /* Stop the DMA then dequeue anything that's in progress. */
473 tegra_dma_cancel(as->dma_chan);
474 as->active = false; /* applies to recording only */
475 pr_debug("%s: done\n", __func__);
478 static void setup_dma_tx_request(struct tegra_dma_req *req,
479 struct audio_stream *aos);
481 static int setup_dma(struct audio_driver_state *ads)
484 pr_info("%s\n", __func__);
486 /* setup audio playback */
487 for (i = 0; i < ads->out.num_bufs; i++) {
488 ads->out.buf_phy[i] = dma_map_single(&ads->pdev->dev,
492 BUG_ON(!ads->out.buf_phy[i]);
493 setup_dma_tx_request(&ads->out.dma_req[i], &ads->out);
494 ads->out.dma_req[i].source_addr = ads->out.buf_phy[i];
497 tegra_dma_allocate_channel(TEGRA_DMA_MODE_CONTINUOUS_SINGLE);
498 if (!ads->out.dma_chan) {
499 pr_err("%s: error alloc output DMA channel: %ld\n",
500 __func__, PTR_ERR(ads->out.dma_chan));
509 for (i = 0; i < ads->out.num_bufs; i++) {
510 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phy[i],
513 ads->out.buf_phy[i] = 0;
515 tegra_dma_free_channel(ads->out.dma_chan);
516 ads->out.dma_chan = 0;
522 static void tear_down_dma(struct audio_driver_state *ads)
525 pr_info("%s\n", __func__);
528 tegra_dma_free_channel(ads->out.dma_chan);
529 for (i = 0; i < ads->out.num_bufs; i++) {
530 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phy[i],
533 ads->out.buf_phy[i] = 0;
536 ads->out.dma_chan = NULL;
539 static void dma_tx_complete_callback(struct tegra_dma_req *req)
541 struct audio_stream *aos = req->dev;
544 req_num = req - aos->dma_req;
545 pr_debug("%s: completed buffer %d size %d\n", __func__,
546 req_num, req->bytes_transferred);
547 BUG_ON(req_num >= aos->num_bufs);
549 complete(&aos->comp[req_num]);
551 if (!pending_buffer_requests(aos)) {
552 pr_debug("%s: Playback underflow", __func__);
553 complete(&aos->stop_completion);
558 static void setup_dma_tx_request(struct tegra_dma_req *req,
559 struct audio_stream *aos)
561 struct audio_driver_state *ads = ads_from_out(aos);
563 memset(req, 0, sizeof(*req));
565 req->complete = dma_tx_complete_callback;
567 req->to_memory = false;
568 req->dest_addr = spdif_get_fifo_phy_base(ads->spdif_phys);
569 req->dest_bus_width = 32;
571 req->source_wrap = 0;
572 req->source_bus_width = 32;
573 req->req_sel = ads->dma_req_sel;
577 static int start_playback(struct audio_stream *aos,
578 struct tegra_dma_req *req)
582 struct audio_driver_state *ads = ads_from_out(aos);
584 pr_debug("%s: (writing %d)\n",
585 __func__, req->size);
587 spin_lock_irqsave(&aos->dma_req_lock, flags);
589 spdif_fifo_clear(ads->spdif_base);
592 spdif_fifo_set_attention_level(ads->spdif_base,
593 ads->out.spdif_fifo_atn_level);
595 if (ads->fifo_init) {
596 spdif_set_bit_mode(ads->spdif_base, SPDIF_BIT_MODE_MODE16BIT);
597 spdif_set_fifo_packed(ads->spdif_base, 1);
598 ads->fifo_init = false;
601 spdif_fifo_enable(ads->spdif_base, 1);
603 rc = tegra_dma_enqueue_req(aos->dma_chan, req);
604 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
607 pr_err("%s: could not enqueue TX DMA req\n", __func__);
611 /* Called with aos->dma_req_lock taken. */
612 static void stop_dma_playback(struct audio_stream *aos)
615 struct audio_driver_state *ads = ads_from_out(aos);
616 pr_debug("%s\n", __func__);
617 spdif_fifo_enable(ads->spdif_base, 0);
618 while ((spdif_get_status(ads->spdif_base) & SPDIF_STATUS_0_TX_BSY) &&
622 pr_info("%s: spin %d\n", __func__, spin);
625 pr_warn("%s: spinny\n", __func__);
626 ads->fifo_init = true;
631 static irqreturn_t spdif_interrupt(int irq, void *data)
633 struct audio_driver_state *ads = data;
634 u32 status = spdif_get_status(ads->spdif_base);
636 pr_debug("%s: %08x\n", __func__, status);
638 /* if (status & SPDIF_STATUS_0_TX_ERR) */
639 spdif_ack_status(ads->spdif_base);
641 pr_debug("%s: done %08x\n", __func__,
642 spdif_get_status(ads->spdif_base));
646 static ssize_t tegra_spdif_write(struct file *file,
647 const char __user *buf, size_t size, loff_t *off)
651 struct tegra_dma_req *req;
652 struct audio_driver_state *ads = ads_from_misc_out(file);
654 mutex_lock(&ads->out.lock);
656 if (!IS_ALIGNED(size, 4) || size < 4 || size > buf_size(&ads->out)) {
657 pr_err("%s: invalid user size %d\n", __func__, size);
662 pr_debug("%s: write %d bytes\n", __func__, size);
665 pr_debug("%s: playback has been cancelled\n", __func__);
669 /* Decide which buf is next. */
670 out_buf = (ads->out.last_queued + 1) % ads->out.num_bufs;
671 req = &ads->out.dma_req[out_buf];
673 /* Wait for the buffer to be emptied (complete). The maximum timeout
674 * value could be calculated dynamically based on buf_size(&ads->out).
675 * For a buffer size of 16k, at 44.1kHz/stereo/16-bit PCM, you would
678 pr_debug("%s: waiting for buffer %d\n", __func__, out_buf);
679 rc = wait_for_completion_interruptible_timeout(
680 &ads->out.comp[out_buf], HZ);
682 pr_err("%s: timeout", __func__);
686 pr_err("%s: wait error %d", __func__, rc);
690 /* Fill the buffer and enqueue it. */
691 pr_debug("%s: acquired buffer %d, copying data\n", __func__, out_buf);
692 rc = copy_from_user(ads->out.buffer[out_buf], buf, size);
698 prevent_suspend(&ads->out);
701 dma_sync_single_for_device(NULL,
702 req->source_addr, req->size, DMA_TO_DEVICE);
703 ads->out.last_queued = out_buf;
704 init_completion(&ads->out.stop_completion);
706 rc = start_playback(&ads->out, req);
710 allow_suspend(&ads->out);
713 mutex_unlock(&ads->out.lock);
717 static long tegra_spdif_out_ioctl(struct file *file,
718 unsigned int cmd, unsigned long arg)
721 struct audio_driver_state *ads = ads_from_misc_out_ctl(file);
722 struct audio_stream *aos = &ads->out;
724 mutex_lock(&aos->lock);
727 case TEGRA_AUDIO_OUT_FLUSH:
728 if (pending_buffer_requests(aos)) {
729 pr_debug("%s: flushing\n", __func__);
730 request_stop_nosync(aos);
731 pr_debug("%s: flushed\n", __func__);
733 if (stop_playback_if_necessary(aos))
734 pr_debug("%s: done (stopped)\n", __func__);
737 case TEGRA_AUDIO_OUT_SET_NUM_BUFS: {
739 if (copy_from_user(&num, (const void __user *)arg,
744 if (!num || num > SPDIF_MAX_NUM_BUFS) {
745 pr_err("%s: invalid buffer count %d\n", __func__, num);
749 if (pending_buffer_requests(aos)) {
750 pr_err("%s: playback in progress\n", __func__);
754 rc = init_stream_buffer(aos, num);
758 sound_ops->setup(ads);
761 case TEGRA_AUDIO_OUT_GET_NUM_BUFS:
762 if (copy_to_user((void __user *)arg,
763 &aos->num_bufs, sizeof(aos->num_bufs)))
770 mutex_unlock(&aos->lock);
775 static int tegra_spdif_out_open(struct inode *inode, struct file *file)
779 struct audio_driver_state *ads = ads_from_misc_out(file);
781 pr_debug("%s\n", __func__);
783 mutex_lock(&ads->out.lock);
785 if (ads->out.opened) {
791 ads->out.stop = false;
793 for (i = 0; i < SPDIF_MAX_NUM_BUFS; i++) {
794 init_completion(&ads->out.comp[i]);
795 /* TX buf rest state is unqueued, complete. */
796 complete(&ads->out.comp[i]);
800 mutex_unlock(&ads->out.lock);
804 static int tegra_spdif_out_release(struct inode *inode, struct file *file)
806 struct audio_driver_state *ads = ads_from_misc_out(file);
808 pr_debug("%s\n", __func__);
810 mutex_lock(&ads->out.lock);
812 request_stop_nosync(&ads->out);
813 if (stop_playback_if_necessary(&ads->out))
814 pr_debug("%s: done (stopped)\n", __func__);
815 allow_suspend(&ads->out);
816 mutex_unlock(&ads->out.lock);
817 pr_debug("%s: done\n", __func__);
822 static const struct file_operations tegra_spdif_out_fops = {
823 .owner = THIS_MODULE,
824 .open = tegra_spdif_out_open,
825 .release = tegra_spdif_out_release,
826 .write = tegra_spdif_write,
829 static int tegra_spdif_ctl_open(struct inode *inode, struct file *file)
834 static int tegra_spdif_ctl_release(struct inode *inode, struct file *file)
839 static const struct file_operations tegra_spdif_out_ctl_fops = {
840 .owner = THIS_MODULE,
841 .open = tegra_spdif_ctl_open,
842 .release = tegra_spdif_ctl_release,
843 .unlocked_ioctl = tegra_spdif_out_ioctl,
846 static int init_stream_buffer(struct audio_stream *s, int num)
849 pr_debug("%s (num %d)\n", __func__, num);
851 for (i = 0; i < num; i++) {
854 kmalloc(buf_size(s), GFP_KERNEL | GFP_DMA);
856 pr_err("%s: could not allocate buffer\n", __func__);
857 for (j = i - 1; j >= 0; j--) {
868 static int setup_misc_device(struct miscdevice *misc,
869 const struct file_operations *fops,
870 const char *fmt, ...)
878 memset(misc, 0, sizeof(*misc));
879 misc->minor = MISC_DYNAMIC_MINOR;
880 misc->name = kmalloc(sz, GFP_KERNEL);
886 vsnprintf((char *)misc->name, sz, fmt, args);
888 if (misc_register(misc)) {
889 pr_err("%s: could not register %s\n", __func__, misc->name);
900 static ssize_t dma_toggle_show(struct device *dev,
901 struct device_attribute *attr,
904 return sprintf(buf, "dma\n");
907 static ssize_t dma_toggle_store(struct device *dev,
908 struct device_attribute *attr,
909 const char *buf, size_t count)
911 pr_err("%s: Not implemented.", __func__);
915 static DEVICE_ATTR(dma_toggle, 0644, dma_toggle_show, dma_toggle_store);
917 static ssize_t __attr_fifo_atn_read(char *buf, int atn_lvl)
920 case SPDIF_FIFO_ATN_LVL_ONE_SLOT:
921 strncpy(buf, "1\n", 2);
923 case SPDIF_FIFO_ATN_LVL_FOUR_SLOTS:
924 strncpy(buf, "4\n", 2);
926 case SPDIF_FIFO_ATN_LVL_EIGHT_SLOTS:
927 strncpy(buf, "8\n", 2);
929 case SPDIF_FIFO_ATN_LVL_TWELVE_SLOTS:
930 strncpy(buf, "12\n", 3);
938 static ssize_t __attr_fifo_atn_write(struct audio_driver_state *ads,
939 struct audio_stream *as,
941 const char *buf, size_t size)
946 pr_err("%s: buffer size %d too big\n", __func__, size);
950 if (sscanf(buf, "%d", &lvl) != 1) {
951 pr_err("%s: invalid input string [%s]\n", __func__, buf);
957 lvl = SPDIF_FIFO_ATN_LVL_ONE_SLOT;
960 lvl = SPDIF_FIFO_ATN_LVL_FOUR_SLOTS;
963 lvl = SPDIF_FIFO_ATN_LVL_EIGHT_SLOTS;
966 lvl = SPDIF_FIFO_ATN_LVL_TWELVE_SLOTS;
969 pr_err("%s: invalid attention level %d\n", __func__, lvl);
974 pr_info("%s: fifo level %d\n", __func__, *fifo_lvl);
979 static ssize_t tx_fifo_atn_show(struct device *dev,
980 struct device_attribute *attr,
983 struct tegra_audio_platform_data *pdata = dev->platform_data;
984 struct audio_driver_state *ads = pdata->driver_data;
985 return __attr_fifo_atn_read(buf, ads->out.spdif_fifo_atn_level);
988 static ssize_t tx_fifo_atn_store(struct device *dev,
989 struct device_attribute *attr,
990 const char *buf, size_t count)
993 struct tegra_audio_platform_data *pdata = dev->platform_data;
994 struct audio_driver_state *ads = pdata->driver_data;
995 mutex_lock(&ads->out.lock);
996 if (pending_buffer_requests(&ads->out)) {
997 pr_err("%s: playback in progress.\n", __func__);
1001 rc = __attr_fifo_atn_write(ads, &ads->out,
1002 &ads->out.spdif_fifo_atn_level,
1005 mutex_unlock(&ads->out.lock);
1009 static DEVICE_ATTR(tx_fifo_atn, 0644, tx_fifo_atn_show, tx_fifo_atn_store);
1012 static int spdif_configure(struct platform_device *pdev)
1014 struct tegra_audio_platform_data *pdata = pdev->dev.platform_data;
1015 struct audio_driver_state *state = pdata->driver_data;
1020 /* disable interrupts from SPDIF */
1021 spdif_writel(state->spdif_base, 0x0, SPDIF_CTRL_0);
1022 spdif_fifo_clear(state->spdif_base);
1023 spdif_enable_fifos(state->spdif_base, 0);
1025 spdif_set_bit_mode(state->spdif_base, SPDIF_BIT_MODE_MODE16BIT);
1026 spdif_set_fifo_packed(state->spdif_base, 1);
1028 spdif_fifo_set_attention_level(state->spdif_base,
1029 state->out.spdif_fifo_atn_level);
1031 spdif_set_sample_rate(state, 44100);
1033 state->fifo_init = true;
1037 static int tegra_spdif_probe(struct platform_device *pdev)
1040 struct resource *res;
1041 struct audio_driver_state *state;
1043 pr_info("%s\n", __func__);
1045 state = kzalloc(sizeof(*state), GFP_KERNEL);
1050 state->pdata = pdev->dev.platform_data;
1051 state->pdata->driver_data = state;
1052 BUG_ON(!state->pdata);
1054 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1056 dev_err(&pdev->dev, "no mem resource!\n");
1060 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
1061 dev_err(&pdev->dev, "memory region already claimed!\n");
1065 state->spdif_phys = res->start;
1066 state->spdif_base = (unsigned long)ioremap(res->start,
1067 res->end - res->start + 1);
1068 if (!state->spdif_base) {
1069 dev_err(&pdev->dev, "cannot remap iomem!\n");
1073 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1075 dev_err(&pdev->dev, "no dma resource!\n");
1078 state->dma_req_sel = res->start;
1080 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1082 dev_err(&pdev->dev, "no irq resource!\n");
1085 state->irq = res->start;
1087 rc = spdif_configure(pdev);
1091 state->out.opened = 0;
1092 state->out.active = false;
1093 mutex_init(&state->out.lock);
1094 init_completion(&state->out.stop_completion);
1095 spin_lock_init(&state->out.dma_req_lock);
1096 state->out.dma_chan = NULL;
1097 state->out.num_bufs = SPDIF_DEFAULT_TX_NUM_BUFS;
1098 for (i = 0; i < SPDIF_MAX_NUM_BUFS; i++) {
1099 init_completion(&state->out.comp[i]);
1100 /* TX buf rest state is unqueued, complete. */
1101 complete(&state->out.comp[i]);
1102 state->out.buffer[i] = 0;
1103 state->out.buf_phy[i] = 0;
1105 state->out.last_queued = 0;
1106 rc = init_stream_buffer(&state->out, state->out.num_bufs);
1110 INIT_WORK(&state->out.allow_suspend_work, allow_suspend_worker);
1111 snprintf(state->out.wake_lock_name, sizeof(state->out.wake_lock_name),
1112 "tegra-audio-spdif");
1113 wake_lock_init(&state->out.wake_lock, WAKE_LOCK_SUSPEND,
1114 state->out.wake_lock_name);
1116 if (request_irq(state->irq, spdif_interrupt,
1117 IRQF_DISABLED, state->pdev->name, state) < 0) {
1119 "%s: could not register handler for irq %d\n",
1120 __func__, state->irq);
1124 rc = setup_misc_device(&state->misc_out,
1125 &tegra_spdif_out_fops,
1130 rc = setup_misc_device(&state->misc_out_ctl,
1131 &tegra_spdif_out_ctl_fops,
1136 sound_ops->setup(state);
1138 rc = device_create_file(&pdev->dev, &dev_attr_dma_toggle);
1140 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
1141 __func__, dev_attr_dma_toggle.attr.name, rc);
1145 rc = device_create_file(&pdev->dev, &dev_attr_tx_fifo_atn);
1147 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
1148 __func__, dev_attr_tx_fifo_atn.attr.name, rc);
1156 static int tegra_spdif_suspend(struct platform_device *pdev, pm_message_t mesg)
1158 /* dev_info(&pdev->dev, "%s\n", __func__); */
1162 static int tegra_spdif_resume(struct platform_device *pdev)
1164 return spdif_configure(pdev);
1166 #endif /* CONFIG_PM */
1168 static struct platform_driver tegra_spdif_driver = {
1170 .name = "spdif_out",
1171 .owner = THIS_MODULE,
1173 .probe = tegra_spdif_probe,
1175 .suspend = tegra_spdif_suspend,
1176 .resume = tegra_spdif_resume,
1180 static int __init tegra_spdif_init(void)
1182 return platform_driver_register(&tegra_spdif_driver);
1185 module_init(tegra_spdif_init);
1186 MODULE_LICENSE("GPL");