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>
28 #include <plat/mcbsp.h>
34 static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
36 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
38 if (mcbsp->pdata->reg_size == 2) {
39 ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
40 __raw_writew((u16)val, addr);
42 ((u32 *)mcbsp->reg_cache)[reg] = val;
43 __raw_writel(val, addr);
47 static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
49 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
51 if (mcbsp->pdata->reg_size == 2) {
52 return !from_cache ? __raw_readw(addr) :
53 ((u16 *)mcbsp->reg_cache)[reg];
55 return !from_cache ? __raw_readl(addr) :
56 ((u32 *)mcbsp->reg_cache)[reg];
60 static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
62 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
65 static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
67 return __raw_readl(mcbsp->st_data->io_base_st + reg);
70 #define MCBSP_READ(mcbsp, reg) \
71 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
72 #define MCBSP_WRITE(mcbsp, reg, val) \
73 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
74 #define MCBSP_READ_CACHE(mcbsp, reg) \
75 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
77 #define MCBSP_ST_READ(mcbsp, reg) \
78 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
79 #define MCBSP_ST_WRITE(mcbsp, reg, val) \
80 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
82 static void omap_mcbsp_dump_reg(struct omap_mcbsp *mcbsp)
84 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
85 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
86 MCBSP_READ(mcbsp, DRR2));
87 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
88 MCBSP_READ(mcbsp, DRR1));
89 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
90 MCBSP_READ(mcbsp, DXR2));
91 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
92 MCBSP_READ(mcbsp, DXR1));
93 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
94 MCBSP_READ(mcbsp, SPCR2));
95 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
96 MCBSP_READ(mcbsp, SPCR1));
97 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
98 MCBSP_READ(mcbsp, RCR2));
99 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
100 MCBSP_READ(mcbsp, RCR1));
101 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
102 MCBSP_READ(mcbsp, XCR2));
103 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
104 MCBSP_READ(mcbsp, XCR1));
105 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
106 MCBSP_READ(mcbsp, SRGR2));
107 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
108 MCBSP_READ(mcbsp, SRGR1));
109 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
110 MCBSP_READ(mcbsp, PCR0));
111 dev_dbg(mcbsp->dev, "***********************\n");
114 static irqreturn_t omap_mcbsp_irq_handler(int irq, void *dev_id)
116 struct omap_mcbsp *mcbsp = dev_id;
119 irqst = MCBSP_READ(mcbsp, IRQST);
120 dev_dbg(mcbsp->dev, "IRQ callback : 0x%x\n", irqst);
122 if (irqst & RSYNCERREN)
123 dev_err(mcbsp->dev, "RX Frame Sync Error!\n");
125 dev_dbg(mcbsp->dev, "RX Frame Sync\n");
127 dev_dbg(mcbsp->dev, "RX End Of Frame\n");
129 dev_dbg(mcbsp->dev, "RX Buffer Threshold Reached\n");
130 if (irqst & RUNDFLEN)
131 dev_err(mcbsp->dev, "RX Buffer Underflow!\n");
133 dev_err(mcbsp->dev, "RX Buffer Overflow!\n");
135 if (irqst & XSYNCERREN)
136 dev_err(mcbsp->dev, "TX Frame Sync Error!\n");
138 dev_dbg(mcbsp->dev, "TX Frame Sync\n");
140 dev_dbg(mcbsp->dev, "TX End Of Frame\n");
142 dev_dbg(mcbsp->dev, "TX Buffer threshold Reached\n");
143 if (irqst & XUNDFLEN)
144 dev_err(mcbsp->dev, "TX Buffer Underflow!\n");
146 dev_err(mcbsp->dev, "TX Buffer Overflow!\n");
147 if (irqst & XEMPTYEOFEN)
148 dev_dbg(mcbsp->dev, "TX Buffer empty at end of frame\n");
150 MCBSP_WRITE(mcbsp, IRQST, irqst);
155 static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
157 struct omap_mcbsp *mcbsp_tx = dev_id;
160 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
161 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
163 if (irqst_spcr2 & XSYNC_ERR) {
164 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
166 /* Writing zero to XSYNC_ERR clears the IRQ */
167 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
173 static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
175 struct omap_mcbsp *mcbsp_rx = dev_id;
178 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
179 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
181 if (irqst_spcr1 & RSYNC_ERR) {
182 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
184 /* Writing zero to RSYNC_ERR clears the IRQ */
185 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
192 * omap_mcbsp_config simply write a config to the
194 * You either call this function or set the McBSP registers
195 * by yourself before calling omap_mcbsp_start().
197 void omap_mcbsp_config(struct omap_mcbsp *mcbsp,
198 const struct omap_mcbsp_reg_cfg *config)
200 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
201 mcbsp->id, mcbsp->phys_base);
203 /* We write the given config */
204 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
205 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
206 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
207 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
208 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
209 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
210 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
211 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
212 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
213 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
214 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
215 if (mcbsp->pdata->has_ccr) {
216 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
217 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
219 /* Enable wakeup behavior */
220 if (mcbsp->pdata->has_wakeup)
221 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
223 /* Enable TX/RX sync error interrupts by default */
225 MCBSP_WRITE(mcbsp, IRQEN, RSYNCERREN | XSYNCERREN);
229 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
231 * @stream - indicates the direction of data flow (rx or tx)
233 * Returns the address of mcbsp data transmit register or data receive register
234 * to be used by DMA for transferring/receiving data based on the value of
235 * @stream for the requested mcbsp given by @id
237 static int omap_mcbsp_dma_reg_params(struct omap_mcbsp *mcbsp,
242 if (mcbsp->pdata->reg_size == 2) {
244 data_reg = OMAP_MCBSP_REG_DRR1;
246 data_reg = OMAP_MCBSP_REG_DXR1;
249 data_reg = OMAP_MCBSP_REG_DRR;
251 data_reg = OMAP_MCBSP_REG_DXR;
254 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
257 static void omap_st_on(struct omap_mcbsp *mcbsp)
261 if (mcbsp->pdata->enable_st_clock)
262 mcbsp->pdata->enable_st_clock(mcbsp->id, 1);
264 /* Enable McBSP Sidetone */
265 w = MCBSP_READ(mcbsp, SSELCR);
266 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
268 /* Enable Sidetone from Sidetone Core */
269 w = MCBSP_ST_READ(mcbsp, SSELCR);
270 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
273 static void omap_st_off(struct omap_mcbsp *mcbsp)
277 w = MCBSP_ST_READ(mcbsp, SSELCR);
278 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
280 w = MCBSP_READ(mcbsp, SSELCR);
281 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
283 if (mcbsp->pdata->enable_st_clock)
284 mcbsp->pdata->enable_st_clock(mcbsp->id, 0);
287 static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
291 val = MCBSP_ST_READ(mcbsp, SSELCR);
293 if (val & ST_COEFFWREN)
294 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
296 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
298 for (i = 0; i < 128; i++)
299 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
303 val = MCBSP_ST_READ(mcbsp, SSELCR);
304 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
305 val = MCBSP_ST_READ(mcbsp, SSELCR);
307 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
310 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
313 static void omap_st_chgain(struct omap_mcbsp *mcbsp)
316 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
318 w = MCBSP_ST_READ(mcbsp, SSELCR);
320 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
321 ST_CH1GAIN(st_data->ch1gain));
324 int omap_st_set_chgain(struct omap_mcbsp *mcbsp, int channel, s16 chgain)
326 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
332 spin_lock_irq(&mcbsp->lock);
334 st_data->ch0gain = chgain;
335 else if (channel == 1)
336 st_data->ch1gain = chgain;
340 if (st_data->enabled)
341 omap_st_chgain(mcbsp);
342 spin_unlock_irq(&mcbsp->lock);
347 int omap_st_get_chgain(struct omap_mcbsp *mcbsp, int channel, s16 *chgain)
349 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
355 spin_lock_irq(&mcbsp->lock);
357 *chgain = st_data->ch0gain;
358 else if (channel == 1)
359 *chgain = st_data->ch1gain;
362 spin_unlock_irq(&mcbsp->lock);
367 static int omap_st_start(struct omap_mcbsp *mcbsp)
369 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
371 if (st_data->enabled && !st_data->running) {
372 omap_st_fir_write(mcbsp, st_data->taps);
373 omap_st_chgain(mcbsp);
377 st_data->running = 1;
384 int omap_st_enable(struct omap_mcbsp *mcbsp)
386 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
391 spin_lock_irq(&mcbsp->lock);
392 st_data->enabled = 1;
393 omap_st_start(mcbsp);
394 spin_unlock_irq(&mcbsp->lock);
399 static int omap_st_stop(struct omap_mcbsp *mcbsp)
401 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
403 if (st_data->running) {
406 st_data->running = 0;
413 int omap_st_disable(struct omap_mcbsp *mcbsp)
415 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
421 spin_lock_irq(&mcbsp->lock);
423 st_data->enabled = 0;
424 spin_unlock_irq(&mcbsp->lock);
429 int omap_st_is_enabled(struct omap_mcbsp *mcbsp)
431 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
436 return st_data->enabled;
440 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
441 * The threshold parameter is 1 based, and it is converted (threshold - 1)
442 * for the THRSH2 register.
444 void omap_mcbsp_set_tx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
446 if (mcbsp->pdata->buffer_size == 0)
449 if (threshold && threshold <= mcbsp->max_tx_thres)
450 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
454 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
455 * The threshold parameter is 1 based, and it is converted (threshold - 1)
456 * for the THRSH1 register.
458 void omap_mcbsp_set_rx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
460 if (mcbsp->pdata->buffer_size == 0)
463 if (threshold && threshold <= mcbsp->max_rx_thres)
464 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
468 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
470 u16 omap_mcbsp_get_tx_delay(struct omap_mcbsp *mcbsp)
474 if (mcbsp->pdata->buffer_size == 0)
477 /* Returns the number of free locations in the buffer */
478 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
480 /* Number of slots are different in McBSP ports */
481 return mcbsp->pdata->buffer_size - buffstat;
485 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
486 * to reach the threshold value (when the DMA will be triggered to read it)
488 u16 omap_mcbsp_get_rx_delay(struct omap_mcbsp *mcbsp)
490 u16 buffstat, threshold;
492 if (mcbsp->pdata->buffer_size == 0)
495 /* Returns the number of used locations in the buffer */
496 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
498 threshold = MCBSP_READ(mcbsp, THRSH1);
500 /* Return the number of location till we reach the threshold limit */
501 if (threshold <= buffstat)
504 return threshold - buffstat;
507 int omap_mcbsp_request(struct omap_mcbsp *mcbsp)
512 reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
517 spin_lock(&mcbsp->lock);
519 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
526 mcbsp->reg_cache = reg_cache;
527 spin_unlock(&mcbsp->lock);
529 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
530 mcbsp->pdata->ops->request(mcbsp->id - 1);
533 * Make sure that transmitter, receiver and sample-rate generator are
534 * not running before activating IRQs.
536 MCBSP_WRITE(mcbsp, SPCR1, 0);
537 MCBSP_WRITE(mcbsp, SPCR2, 0);
540 err = request_irq(mcbsp->irq, omap_mcbsp_irq_handler, 0,
541 "McBSP", (void *)mcbsp);
543 dev_err(mcbsp->dev, "Unable to request IRQ\n");
544 goto err_clk_disable;
547 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler, 0,
548 "McBSP TX", (void *)mcbsp);
550 dev_err(mcbsp->dev, "Unable to request TX IRQ\n");
551 goto err_clk_disable;
554 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler, 0,
555 "McBSP RX", (void *)mcbsp);
557 dev_err(mcbsp->dev, "Unable to request RX IRQ\n");
564 free_irq(mcbsp->tx_irq, (void *)mcbsp);
566 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
567 mcbsp->pdata->ops->free(mcbsp->id - 1);
569 /* Disable wakeup behavior */
570 if (mcbsp->pdata->has_wakeup)
571 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
573 spin_lock(&mcbsp->lock);
575 mcbsp->reg_cache = NULL;
577 spin_unlock(&mcbsp->lock);
583 void omap_mcbsp_free(struct omap_mcbsp *mcbsp)
587 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
588 mcbsp->pdata->ops->free(mcbsp->id - 1);
590 /* Disable wakeup behavior */
591 if (mcbsp->pdata->has_wakeup)
592 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
594 /* Disable interrupt requests */
596 MCBSP_WRITE(mcbsp, IRQEN, 0);
599 free_irq(mcbsp->irq, (void *)mcbsp);
601 free_irq(mcbsp->rx_irq, (void *)mcbsp);
602 free_irq(mcbsp->tx_irq, (void *)mcbsp);
605 reg_cache = mcbsp->reg_cache;
608 * Select CLKS source from internal source unconditionally before
609 * marking the McBSP port as free.
610 * If the external clock source via MCBSP_CLKS pin has been selected the
611 * system will refuse to enter idle if the CLKS pin source is not reset
612 * back to internal source.
614 if (!cpu_class_is_omap1())
615 omap2_mcbsp_set_clks_src(mcbsp, MCBSP_CLKS_PRCM_SRC);
617 spin_lock(&mcbsp->lock);
619 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
622 mcbsp->reg_cache = NULL;
623 spin_unlock(&mcbsp->lock);
630 * Here we start the McBSP, by enabling transmitter, receiver or both.
631 * If no transmitter or receiver is active prior calling, then sample-rate
632 * generator and frame sync are started.
634 void omap_mcbsp_start(struct omap_mcbsp *mcbsp, int tx, int rx)
640 omap_st_start(mcbsp);
642 /* Only enable SRG, if McBSP is master */
643 w = MCBSP_READ_CACHE(mcbsp, PCR0);
644 if (w & (FSXM | FSRM | CLKXM | CLKRM))
645 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
646 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
649 /* Start the sample generator */
650 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
651 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
654 /* Enable transmitter and receiver */
656 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
657 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
660 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
661 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
664 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
665 * REVISIT: 100us may give enough time for two CLKSRG, however
666 * due to some unknown PM related, clock gating etc. reason it
672 /* Start frame sync */
673 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
674 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
677 if (mcbsp->pdata->has_ccr) {
678 /* Release the transmitter and receiver */
679 w = MCBSP_READ_CACHE(mcbsp, XCCR);
680 w &= ~(tx ? XDISABLE : 0);
681 MCBSP_WRITE(mcbsp, XCCR, w);
682 w = MCBSP_READ_CACHE(mcbsp, RCCR);
683 w &= ~(rx ? RDISABLE : 0);
684 MCBSP_WRITE(mcbsp, RCCR, w);
687 /* Dump McBSP Regs */
688 omap_mcbsp_dump_reg(mcbsp);
691 void omap_mcbsp_stop(struct omap_mcbsp *mcbsp, int tx, int rx)
696 /* Reset transmitter */
698 if (mcbsp->pdata->has_ccr) {
699 w = MCBSP_READ_CACHE(mcbsp, XCCR);
700 w |= (tx ? XDISABLE : 0);
701 MCBSP_WRITE(mcbsp, XCCR, w);
703 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
704 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
708 if (mcbsp->pdata->has_ccr) {
709 w = MCBSP_READ_CACHE(mcbsp, RCCR);
710 w |= (rx ? RDISABLE : 0);
711 MCBSP_WRITE(mcbsp, RCCR, w);
713 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
714 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
716 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
717 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
720 /* Reset the sample rate generator */
721 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
722 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
729 int omap2_mcbsp_set_clks_src(struct omap_mcbsp *mcbsp, u8 fck_src_id)
733 if (fck_src_id == MCBSP_CLKS_PAD_SRC)
735 else if (fck_src_id == MCBSP_CLKS_PRCM_SRC)
740 if (mcbsp->pdata->set_clk_src)
741 return mcbsp->pdata->set_clk_src(mcbsp->dev, mcbsp->fclk, src);
746 int omap_mcbsp_6pin_src_mux(struct omap_mcbsp *mcbsp, u8 mux)
748 const char *signal, *src;
750 if (!mcbsp->pdata->mux_signal)
774 return mcbsp->pdata->mux_signal(mcbsp->dev, signal, src);
777 #define max_thres(m) (mcbsp->pdata->buffer_size)
778 #define valid_threshold(m, val) ((val) <= max_thres(m))
779 #define THRESHOLD_PROP_BUILDER(prop) \
780 static ssize_t prop##_show(struct device *dev, \
781 struct device_attribute *attr, char *buf) \
783 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
785 return sprintf(buf, "%u\n", mcbsp->prop); \
788 static ssize_t prop##_store(struct device *dev, \
789 struct device_attribute *attr, \
790 const char *buf, size_t size) \
792 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
796 status = strict_strtoul(buf, 0, &val); \
800 if (!valid_threshold(mcbsp, val)) \
807 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
809 THRESHOLD_PROP_BUILDER(max_tx_thres);
810 THRESHOLD_PROP_BUILDER(max_rx_thres);
812 static const char *dma_op_modes[] = {
813 "element", "threshold",
816 static ssize_t dma_op_mode_show(struct device *dev,
817 struct device_attribute *attr, char *buf)
819 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
820 int dma_op_mode, i = 0;
822 const char * const *s;
824 dma_op_mode = mcbsp->dma_op_mode;
826 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
827 if (dma_op_mode == i)
828 len += sprintf(buf + len, "[%s] ", *s);
830 len += sprintf(buf + len, "%s ", *s);
832 len += sprintf(buf + len, "\n");
837 static ssize_t dma_op_mode_store(struct device *dev,
838 struct device_attribute *attr,
839 const char *buf, size_t size)
841 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
842 const char * const *s;
845 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
846 if (sysfs_streq(buf, *s))
849 if (i == ARRAY_SIZE(dma_op_modes))
852 spin_lock_irq(&mcbsp->lock);
857 mcbsp->dma_op_mode = i;
860 spin_unlock_irq(&mcbsp->lock);
865 static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
867 static const struct attribute *additional_attrs[] = {
868 &dev_attr_max_tx_thres.attr,
869 &dev_attr_max_rx_thres.attr,
870 &dev_attr_dma_op_mode.attr,
874 static const struct attribute_group additional_attr_group = {
875 .attrs = (struct attribute **)additional_attrs,
878 static ssize_t st_taps_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
881 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
882 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
886 spin_lock_irq(&mcbsp->lock);
887 for (i = 0; i < st_data->nr_taps; i++)
888 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
891 status += sprintf(&buf[status], "\n");
892 spin_unlock_irq(&mcbsp->lock);
897 static ssize_t st_taps_store(struct device *dev,
898 struct device_attribute *attr,
899 const char *buf, size_t size)
901 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
902 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
903 int val, tmp, status, i = 0;
905 spin_lock_irq(&mcbsp->lock);
906 memset(st_data->taps, 0, sizeof(st_data->taps));
907 st_data->nr_taps = 0;
910 status = sscanf(buf, "%d%n", &val, &tmp);
911 if (status < 0 || status == 0) {
915 if (val < -32768 || val > 32767) {
919 st_data->taps[i++] = val;
926 st_data->nr_taps = i;
929 spin_unlock_irq(&mcbsp->lock);
934 static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
936 static const struct attribute *sidetone_attrs[] = {
937 &dev_attr_st_taps.attr,
941 static const struct attribute_group sidetone_attr_group = {
942 .attrs = (struct attribute **)sidetone_attrs,
945 static int __devinit omap_st_add(struct omap_mcbsp *mcbsp,
946 struct resource *res)
948 struct omap_mcbsp_st_data *st_data;
951 st_data = devm_kzalloc(mcbsp->dev, sizeof(*mcbsp->st_data), GFP_KERNEL);
955 st_data->io_base_st = devm_ioremap(mcbsp->dev, res->start,
957 if (!st_data->io_base_st)
960 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
964 mcbsp->st_data = st_data;
969 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
970 * 730 has only 2 McBSP, and both of them are MPU peripherals.
972 int __devinit omap_mcbsp_init(struct platform_device *pdev)
974 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
975 struct resource *res;
978 spin_lock_init(&mcbsp->lock);
981 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
983 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
985 dev_err(mcbsp->dev, "invalid memory resource\n");
989 if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
990 dev_name(&pdev->dev))) {
991 dev_err(mcbsp->dev, "memory region already claimed\n");
995 mcbsp->phys_base = res->start;
996 mcbsp->reg_cache_size = resource_size(res);
997 mcbsp->io_base = devm_ioremap(&pdev->dev, res->start,
1002 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1004 mcbsp->phys_dma_base = mcbsp->phys_base;
1006 mcbsp->phys_dma_base = res->start;
1009 * OMAP1, 2 uses two interrupt lines: TX, RX
1010 * OMAP2430, OMAP3 SoC have combined IRQ line as well.
1011 * OMAP4 and newer SoC only have the combined IRQ line.
1012 * Use the combined IRQ if available since it gives better debugging
1015 mcbsp->irq = platform_get_irq_byname(pdev, "common");
1016 if (mcbsp->irq == -ENXIO) {
1017 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1019 if (mcbsp->tx_irq == -ENXIO) {
1020 mcbsp->irq = platform_get_irq(pdev, 0);
1023 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1028 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1030 dev_err(&pdev->dev, "invalid rx DMA channel\n");
1033 /* RX DMA request number, and port address configuration */
1034 mcbsp->dma_data[1].name = "Audio Capture";
1035 mcbsp->dma_data[1].dma_req = res->start;
1036 mcbsp->dma_data[1].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 1);
1038 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1040 dev_err(&pdev->dev, "invalid tx DMA channel\n");
1043 /* TX DMA request number, and port address configuration */
1044 mcbsp->dma_data[0].name = "Audio Playback";
1045 mcbsp->dma_data[0].dma_req = res->start;
1046 mcbsp->dma_data[0].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 0);
1048 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1049 if (IS_ERR(mcbsp->fclk)) {
1050 ret = PTR_ERR(mcbsp->fclk);
1051 dev_err(mcbsp->dev, "unable to get fck: %d\n", ret);
1055 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1056 if (mcbsp->pdata->buffer_size) {
1058 * Initially configure the maximum thresholds to a safe value.
1059 * The McBSP FIFO usage with these values should not go under
1061 * If the whole FIFO without safety buffer is used, than there
1062 * is a possibility that the DMA will be not able to push the
1063 * new data on time, causing channel shifts in runtime.
1065 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1066 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1068 ret = sysfs_create_group(&mcbsp->dev->kobj,
1069 &additional_attr_group);
1072 "Unable to create additional controls\n");
1076 mcbsp->max_tx_thres = -EINVAL;
1077 mcbsp->max_rx_thres = -EINVAL;
1080 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1082 ret = omap_st_add(mcbsp, res);
1085 "Unable to create sidetone controls\n");
1093 if (mcbsp->pdata->buffer_size)
1094 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
1096 clk_put(mcbsp->fclk);
1100 void __devexit omap_mcbsp_sysfs_remove(struct omap_mcbsp *mcbsp)
1102 if (mcbsp->pdata->buffer_size)
1103 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
1106 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);