2 * sound/soc/omap/mcbsp.c
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
7 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
8 * Peter Ujfalusi <peter.ujfalusi@ti.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * Multichannel mode not supported.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/clk.h>
24 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pm_runtime.h>
29 #include <linux/platform_data/asoc-ti-mcbsp.h>
35 static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
37 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
39 if (mcbsp->pdata->reg_size == 2) {
40 ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
41 __raw_writew((u16)val, addr);
43 ((u32 *)mcbsp->reg_cache)[reg] = val;
44 __raw_writel(val, addr);
48 static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
50 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
52 if (mcbsp->pdata->reg_size == 2) {
53 return !from_cache ? __raw_readw(addr) :
54 ((u16 *)mcbsp->reg_cache)[reg];
56 return !from_cache ? __raw_readl(addr) :
57 ((u32 *)mcbsp->reg_cache)[reg];
61 static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
63 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
66 static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
68 return __raw_readl(mcbsp->st_data->io_base_st + reg);
71 #define MCBSP_READ(mcbsp, reg) \
72 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
73 #define MCBSP_WRITE(mcbsp, reg, val) \
74 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
75 #define MCBSP_READ_CACHE(mcbsp, reg) \
76 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
78 #define MCBSP_ST_READ(mcbsp, reg) \
79 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
80 #define MCBSP_ST_WRITE(mcbsp, reg, val) \
81 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
83 static void omap_mcbsp_dump_reg(struct omap_mcbsp *mcbsp)
85 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
86 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
87 MCBSP_READ(mcbsp, DRR2));
88 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
89 MCBSP_READ(mcbsp, DRR1));
90 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
91 MCBSP_READ(mcbsp, DXR2));
92 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
93 MCBSP_READ(mcbsp, DXR1));
94 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
95 MCBSP_READ(mcbsp, SPCR2));
96 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
97 MCBSP_READ(mcbsp, SPCR1));
98 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
99 MCBSP_READ(mcbsp, RCR2));
100 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
101 MCBSP_READ(mcbsp, RCR1));
102 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
103 MCBSP_READ(mcbsp, XCR2));
104 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
105 MCBSP_READ(mcbsp, XCR1));
106 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
107 MCBSP_READ(mcbsp, SRGR2));
108 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
109 MCBSP_READ(mcbsp, SRGR1));
110 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
111 MCBSP_READ(mcbsp, PCR0));
112 dev_dbg(mcbsp->dev, "***********************\n");
115 static irqreturn_t omap_mcbsp_irq_handler(int irq, void *dev_id)
117 struct omap_mcbsp *mcbsp = dev_id;
120 irqst = MCBSP_READ(mcbsp, IRQST);
121 dev_dbg(mcbsp->dev, "IRQ callback : 0x%x\n", irqst);
123 if (irqst & RSYNCERREN)
124 dev_err(mcbsp->dev, "RX Frame Sync Error!\n");
126 dev_dbg(mcbsp->dev, "RX Frame Sync\n");
128 dev_dbg(mcbsp->dev, "RX End Of Frame\n");
130 dev_dbg(mcbsp->dev, "RX Buffer Threshold Reached\n");
131 if (irqst & RUNDFLEN)
132 dev_err(mcbsp->dev, "RX Buffer Underflow!\n");
134 dev_err(mcbsp->dev, "RX Buffer Overflow!\n");
136 if (irqst & XSYNCERREN)
137 dev_err(mcbsp->dev, "TX Frame Sync Error!\n");
139 dev_dbg(mcbsp->dev, "TX Frame Sync\n");
141 dev_dbg(mcbsp->dev, "TX End Of Frame\n");
143 dev_dbg(mcbsp->dev, "TX Buffer threshold Reached\n");
144 if (irqst & XUNDFLEN)
145 dev_err(mcbsp->dev, "TX Buffer Underflow!\n");
147 dev_err(mcbsp->dev, "TX Buffer Overflow!\n");
148 if (irqst & XEMPTYEOFEN)
149 dev_dbg(mcbsp->dev, "TX Buffer empty at end of frame\n");
151 MCBSP_WRITE(mcbsp, IRQST, irqst);
156 static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
158 struct omap_mcbsp *mcbsp_tx = dev_id;
161 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
162 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
164 if (irqst_spcr2 & XSYNC_ERR) {
165 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
167 /* Writing zero to XSYNC_ERR clears the IRQ */
168 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
174 static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
176 struct omap_mcbsp *mcbsp_rx = dev_id;
179 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
180 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
182 if (irqst_spcr1 & RSYNC_ERR) {
183 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
185 /* Writing zero to RSYNC_ERR clears the IRQ */
186 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
193 * omap_mcbsp_config simply write a config to the
195 * You either call this function or set the McBSP registers
196 * by yourself before calling omap_mcbsp_start().
198 void omap_mcbsp_config(struct omap_mcbsp *mcbsp,
199 const struct omap_mcbsp_reg_cfg *config)
201 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
202 mcbsp->id, mcbsp->phys_base);
204 /* We write the given config */
205 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
206 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
207 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
208 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
209 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
210 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
211 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
212 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
213 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
214 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
215 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
216 if (mcbsp->pdata->has_ccr) {
217 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
218 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
220 /* Enable wakeup behavior */
221 if (mcbsp->pdata->has_wakeup)
222 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
224 /* Enable TX/RX sync error interrupts by default */
226 MCBSP_WRITE(mcbsp, IRQEN, RSYNCERREN | XSYNCERREN);
230 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
232 * @stream - indicates the direction of data flow (rx or tx)
234 * Returns the address of mcbsp data transmit register or data receive register
235 * to be used by DMA for transferring/receiving data based on the value of
236 * @stream for the requested mcbsp given by @id
238 static int omap_mcbsp_dma_reg_params(struct omap_mcbsp *mcbsp,
243 if (mcbsp->pdata->reg_size == 2) {
245 data_reg = OMAP_MCBSP_REG_DRR1;
247 data_reg = OMAP_MCBSP_REG_DXR1;
250 data_reg = OMAP_MCBSP_REG_DRR;
252 data_reg = OMAP_MCBSP_REG_DXR;
255 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
258 static void omap_st_on(struct omap_mcbsp *mcbsp)
262 if (mcbsp->pdata->enable_st_clock)
263 mcbsp->pdata->enable_st_clock(mcbsp->id, 1);
265 /* Enable McBSP Sidetone */
266 w = MCBSP_READ(mcbsp, SSELCR);
267 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
269 /* Enable Sidetone from Sidetone Core */
270 w = MCBSP_ST_READ(mcbsp, SSELCR);
271 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
274 static void omap_st_off(struct omap_mcbsp *mcbsp)
278 w = MCBSP_ST_READ(mcbsp, SSELCR);
279 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
281 w = MCBSP_READ(mcbsp, SSELCR);
282 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
284 if (mcbsp->pdata->enable_st_clock)
285 mcbsp->pdata->enable_st_clock(mcbsp->id, 0);
288 static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
292 val = MCBSP_ST_READ(mcbsp, SSELCR);
294 if (val & ST_COEFFWREN)
295 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
297 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
299 for (i = 0; i < 128; i++)
300 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
304 val = MCBSP_ST_READ(mcbsp, SSELCR);
305 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
306 val = MCBSP_ST_READ(mcbsp, SSELCR);
308 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
311 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
314 static void omap_st_chgain(struct omap_mcbsp *mcbsp)
317 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
319 w = MCBSP_ST_READ(mcbsp, SSELCR);
321 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
322 ST_CH1GAIN(st_data->ch1gain));
325 int omap_st_set_chgain(struct omap_mcbsp *mcbsp, int channel, s16 chgain)
327 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
333 spin_lock_irq(&mcbsp->lock);
335 st_data->ch0gain = chgain;
336 else if (channel == 1)
337 st_data->ch1gain = chgain;
341 if (st_data->enabled)
342 omap_st_chgain(mcbsp);
343 spin_unlock_irq(&mcbsp->lock);
348 int omap_st_get_chgain(struct omap_mcbsp *mcbsp, int channel, s16 *chgain)
350 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
356 spin_lock_irq(&mcbsp->lock);
358 *chgain = st_data->ch0gain;
359 else if (channel == 1)
360 *chgain = st_data->ch1gain;
363 spin_unlock_irq(&mcbsp->lock);
368 static int omap_st_start(struct omap_mcbsp *mcbsp)
370 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
372 if (st_data->enabled && !st_data->running) {
373 omap_st_fir_write(mcbsp, st_data->taps);
374 omap_st_chgain(mcbsp);
378 st_data->running = 1;
385 int omap_st_enable(struct omap_mcbsp *mcbsp)
387 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
392 spin_lock_irq(&mcbsp->lock);
393 st_data->enabled = 1;
394 omap_st_start(mcbsp);
395 spin_unlock_irq(&mcbsp->lock);
400 static int omap_st_stop(struct omap_mcbsp *mcbsp)
402 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
404 if (st_data->running) {
407 st_data->running = 0;
414 int omap_st_disable(struct omap_mcbsp *mcbsp)
416 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
422 spin_lock_irq(&mcbsp->lock);
424 st_data->enabled = 0;
425 spin_unlock_irq(&mcbsp->lock);
430 int omap_st_is_enabled(struct omap_mcbsp *mcbsp)
432 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
437 return st_data->enabled;
441 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
442 * The threshold parameter is 1 based, and it is converted (threshold - 1)
443 * for the THRSH2 register.
445 void omap_mcbsp_set_tx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
447 if (mcbsp->pdata->buffer_size == 0)
450 if (threshold && threshold <= mcbsp->max_tx_thres)
451 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
455 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
456 * The threshold parameter is 1 based, and it is converted (threshold - 1)
457 * for the THRSH1 register.
459 void omap_mcbsp_set_rx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
461 if (mcbsp->pdata->buffer_size == 0)
464 if (threshold && threshold <= mcbsp->max_rx_thres)
465 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
469 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
471 u16 omap_mcbsp_get_tx_delay(struct omap_mcbsp *mcbsp)
475 if (mcbsp->pdata->buffer_size == 0)
478 /* Returns the number of free locations in the buffer */
479 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
481 /* Number of slots are different in McBSP ports */
482 return mcbsp->pdata->buffer_size - buffstat;
486 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
487 * to reach the threshold value (when the DMA will be triggered to read it)
489 u16 omap_mcbsp_get_rx_delay(struct omap_mcbsp *mcbsp)
491 u16 buffstat, threshold;
493 if (mcbsp->pdata->buffer_size == 0)
496 /* Returns the number of used locations in the buffer */
497 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
499 threshold = MCBSP_READ(mcbsp, THRSH1);
501 /* Return the number of location till we reach the threshold limit */
502 if (threshold <= buffstat)
505 return threshold - buffstat;
508 int omap_mcbsp_request(struct omap_mcbsp *mcbsp)
513 reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
518 spin_lock(&mcbsp->lock);
520 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
527 mcbsp->reg_cache = reg_cache;
528 spin_unlock(&mcbsp->lock);
530 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
531 mcbsp->pdata->ops->request(mcbsp->id - 1);
534 * Make sure that transmitter, receiver and sample-rate generator are
535 * not running before activating IRQs.
537 MCBSP_WRITE(mcbsp, SPCR1, 0);
538 MCBSP_WRITE(mcbsp, SPCR2, 0);
541 err = request_irq(mcbsp->irq, omap_mcbsp_irq_handler, 0,
542 "McBSP", (void *)mcbsp);
544 dev_err(mcbsp->dev, "Unable to request IRQ\n");
545 goto err_clk_disable;
548 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler, 0,
549 "McBSP TX", (void *)mcbsp);
551 dev_err(mcbsp->dev, "Unable to request TX IRQ\n");
552 goto err_clk_disable;
555 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler, 0,
556 "McBSP RX", (void *)mcbsp);
558 dev_err(mcbsp->dev, "Unable to request RX IRQ\n");
565 free_irq(mcbsp->tx_irq, (void *)mcbsp);
567 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
568 mcbsp->pdata->ops->free(mcbsp->id - 1);
570 /* Disable wakeup behavior */
571 if (mcbsp->pdata->has_wakeup)
572 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
574 spin_lock(&mcbsp->lock);
576 mcbsp->reg_cache = NULL;
578 spin_unlock(&mcbsp->lock);
584 void omap_mcbsp_free(struct omap_mcbsp *mcbsp)
588 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
589 mcbsp->pdata->ops->free(mcbsp->id - 1);
591 /* Disable wakeup behavior */
592 if (mcbsp->pdata->has_wakeup)
593 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
595 /* Disable interrupt requests */
597 MCBSP_WRITE(mcbsp, IRQEN, 0);
600 free_irq(mcbsp->irq, (void *)mcbsp);
602 free_irq(mcbsp->rx_irq, (void *)mcbsp);
603 free_irq(mcbsp->tx_irq, (void *)mcbsp);
606 reg_cache = mcbsp->reg_cache;
609 * Select CLKS source from internal source unconditionally before
610 * marking the McBSP port as free.
611 * If the external clock source via MCBSP_CLKS pin has been selected the
612 * system will refuse to enter idle if the CLKS pin source is not reset
613 * back to internal source.
615 if (!cpu_class_is_omap1())
616 omap2_mcbsp_set_clks_src(mcbsp, MCBSP_CLKS_PRCM_SRC);
618 spin_lock(&mcbsp->lock);
620 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
623 mcbsp->reg_cache = NULL;
624 spin_unlock(&mcbsp->lock);
631 * Here we start the McBSP, by enabling transmitter, receiver or both.
632 * If no transmitter or receiver is active prior calling, then sample-rate
633 * generator and frame sync are started.
635 void omap_mcbsp_start(struct omap_mcbsp *mcbsp, int tx, int rx)
641 omap_st_start(mcbsp);
643 /* Only enable SRG, if McBSP is master */
644 w = MCBSP_READ_CACHE(mcbsp, PCR0);
645 if (w & (FSXM | FSRM | CLKXM | CLKRM))
646 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
647 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
650 /* Start the sample generator */
651 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
652 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
655 /* Enable transmitter and receiver */
657 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
658 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
661 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
662 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
665 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
666 * REVISIT: 100us may give enough time for two CLKSRG, however
667 * due to some unknown PM related, clock gating etc. reason it
673 /* Start frame sync */
674 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
675 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
678 if (mcbsp->pdata->has_ccr) {
679 /* Release the transmitter and receiver */
680 w = MCBSP_READ_CACHE(mcbsp, XCCR);
681 w &= ~(tx ? XDISABLE : 0);
682 MCBSP_WRITE(mcbsp, XCCR, w);
683 w = MCBSP_READ_CACHE(mcbsp, RCCR);
684 w &= ~(rx ? RDISABLE : 0);
685 MCBSP_WRITE(mcbsp, RCCR, w);
688 /* Dump McBSP Regs */
689 omap_mcbsp_dump_reg(mcbsp);
692 void omap_mcbsp_stop(struct omap_mcbsp *mcbsp, int tx, int rx)
697 /* Reset transmitter */
699 if (mcbsp->pdata->has_ccr) {
700 w = MCBSP_READ_CACHE(mcbsp, XCCR);
701 w |= (tx ? XDISABLE : 0);
702 MCBSP_WRITE(mcbsp, XCCR, w);
704 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
705 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
709 if (mcbsp->pdata->has_ccr) {
710 w = MCBSP_READ_CACHE(mcbsp, RCCR);
711 w |= (rx ? RDISABLE : 0);
712 MCBSP_WRITE(mcbsp, RCCR, w);
714 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
715 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
717 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
718 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
721 /* Reset the sample rate generator */
722 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
723 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
730 int omap2_mcbsp_set_clks_src(struct omap_mcbsp *mcbsp, u8 fck_src_id)
736 if (fck_src_id == MCBSP_CLKS_PAD_SRC)
738 else if (fck_src_id == MCBSP_CLKS_PRCM_SRC)
743 fck_src = clk_get(mcbsp->dev, src);
744 if (IS_ERR(fck_src)) {
745 dev_err(mcbsp->dev, "CLKS: could not clk_get() %s\n", src);
749 pm_runtime_put_sync(mcbsp->dev);
751 r = clk_set_parent(mcbsp->fclk, fck_src);
753 dev_err(mcbsp->dev, "CLKS: could not clk_set_parent() to %s\n",
759 pm_runtime_get_sync(mcbsp->dev);
767 #define max_thres(m) (mcbsp->pdata->buffer_size)
768 #define valid_threshold(m, val) ((val) <= max_thres(m))
769 #define THRESHOLD_PROP_BUILDER(prop) \
770 static ssize_t prop##_show(struct device *dev, \
771 struct device_attribute *attr, char *buf) \
773 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
775 return sprintf(buf, "%u\n", mcbsp->prop); \
778 static ssize_t prop##_store(struct device *dev, \
779 struct device_attribute *attr, \
780 const char *buf, size_t size) \
782 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
786 status = strict_strtoul(buf, 0, &val); \
790 if (!valid_threshold(mcbsp, val)) \
797 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
799 THRESHOLD_PROP_BUILDER(max_tx_thres);
800 THRESHOLD_PROP_BUILDER(max_rx_thres);
802 static const char *dma_op_modes[] = {
803 "element", "threshold",
806 static ssize_t dma_op_mode_show(struct device *dev,
807 struct device_attribute *attr, char *buf)
809 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
810 int dma_op_mode, i = 0;
812 const char * const *s;
814 dma_op_mode = mcbsp->dma_op_mode;
816 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
817 if (dma_op_mode == i)
818 len += sprintf(buf + len, "[%s] ", *s);
820 len += sprintf(buf + len, "%s ", *s);
822 len += sprintf(buf + len, "\n");
827 static ssize_t dma_op_mode_store(struct device *dev,
828 struct device_attribute *attr,
829 const char *buf, size_t size)
831 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
832 const char * const *s;
835 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
836 if (sysfs_streq(buf, *s))
839 if (i == ARRAY_SIZE(dma_op_modes))
842 spin_lock_irq(&mcbsp->lock);
847 mcbsp->dma_op_mode = i;
850 spin_unlock_irq(&mcbsp->lock);
855 static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
857 static const struct attribute *additional_attrs[] = {
858 &dev_attr_max_tx_thres.attr,
859 &dev_attr_max_rx_thres.attr,
860 &dev_attr_dma_op_mode.attr,
864 static const struct attribute_group additional_attr_group = {
865 .attrs = (struct attribute **)additional_attrs,
868 static ssize_t st_taps_show(struct device *dev,
869 struct device_attribute *attr, char *buf)
871 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
872 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
876 spin_lock_irq(&mcbsp->lock);
877 for (i = 0; i < st_data->nr_taps; i++)
878 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
881 status += sprintf(&buf[status], "\n");
882 spin_unlock_irq(&mcbsp->lock);
887 static ssize_t st_taps_store(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf, size_t size)
891 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
892 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
893 int val, tmp, status, i = 0;
895 spin_lock_irq(&mcbsp->lock);
896 memset(st_data->taps, 0, sizeof(st_data->taps));
897 st_data->nr_taps = 0;
900 status = sscanf(buf, "%d%n", &val, &tmp);
901 if (status < 0 || status == 0) {
905 if (val < -32768 || val > 32767) {
909 st_data->taps[i++] = val;
916 st_data->nr_taps = i;
919 spin_unlock_irq(&mcbsp->lock);
924 static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
926 static const struct attribute *sidetone_attrs[] = {
927 &dev_attr_st_taps.attr,
931 static const struct attribute_group sidetone_attr_group = {
932 .attrs = (struct attribute **)sidetone_attrs,
935 static int __devinit omap_st_add(struct omap_mcbsp *mcbsp,
936 struct resource *res)
938 struct omap_mcbsp_st_data *st_data;
941 st_data = devm_kzalloc(mcbsp->dev, sizeof(*mcbsp->st_data), GFP_KERNEL);
945 st_data->io_base_st = devm_ioremap(mcbsp->dev, res->start,
947 if (!st_data->io_base_st)
950 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
954 mcbsp->st_data = st_data;
959 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
960 * 730 has only 2 McBSP, and both of them are MPU peripherals.
962 int __devinit omap_mcbsp_init(struct platform_device *pdev)
964 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
965 struct resource *res;
968 spin_lock_init(&mcbsp->lock);
971 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
973 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
975 dev_err(mcbsp->dev, "invalid memory resource\n");
979 if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
980 dev_name(&pdev->dev))) {
981 dev_err(mcbsp->dev, "memory region already claimed\n");
985 mcbsp->phys_base = res->start;
986 mcbsp->reg_cache_size = resource_size(res);
987 mcbsp->io_base = devm_ioremap(&pdev->dev, res->start,
992 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
994 mcbsp->phys_dma_base = mcbsp->phys_base;
996 mcbsp->phys_dma_base = res->start;
999 * OMAP1, 2 uses two interrupt lines: TX, RX
1000 * OMAP2430, OMAP3 SoC have combined IRQ line as well.
1001 * OMAP4 and newer SoC only have the combined IRQ line.
1002 * Use the combined IRQ if available since it gives better debugging
1005 mcbsp->irq = platform_get_irq_byname(pdev, "common");
1006 if (mcbsp->irq == -ENXIO) {
1007 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1009 if (mcbsp->tx_irq == -ENXIO) {
1010 mcbsp->irq = platform_get_irq(pdev, 0);
1013 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1018 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1020 dev_err(&pdev->dev, "invalid rx DMA channel\n");
1023 /* RX DMA request number, and port address configuration */
1024 mcbsp->dma_data[1].name = "Audio Capture";
1025 mcbsp->dma_data[1].dma_req = res->start;
1026 mcbsp->dma_data[1].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 1);
1028 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1030 dev_err(&pdev->dev, "invalid tx DMA channel\n");
1033 /* TX DMA request number, and port address configuration */
1034 mcbsp->dma_data[0].name = "Audio Playback";
1035 mcbsp->dma_data[0].dma_req = res->start;
1036 mcbsp->dma_data[0].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 0);
1038 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1039 if (IS_ERR(mcbsp->fclk)) {
1040 ret = PTR_ERR(mcbsp->fclk);
1041 dev_err(mcbsp->dev, "unable to get fck: %d\n", ret);
1045 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1046 if (mcbsp->pdata->buffer_size) {
1048 * Initially configure the maximum thresholds to a safe value.
1049 * The McBSP FIFO usage with these values should not go under
1051 * If the whole FIFO without safety buffer is used, than there
1052 * is a possibility that the DMA will be not able to push the
1053 * new data on time, causing channel shifts in runtime.
1055 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1056 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1058 ret = sysfs_create_group(&mcbsp->dev->kobj,
1059 &additional_attr_group);
1062 "Unable to create additional controls\n");
1066 mcbsp->max_tx_thres = -EINVAL;
1067 mcbsp->max_rx_thres = -EINVAL;
1070 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1072 ret = omap_st_add(mcbsp, res);
1075 "Unable to create sidetone controls\n");
1083 if (mcbsp->pdata->buffer_size)
1084 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
1086 clk_put(mcbsp->fclk);
1090 void __devexit omap_mcbsp_sysfs_remove(struct omap_mcbsp *mcbsp)
1092 if (mcbsp->pdata->buffer_size)
1093 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
1096 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);