2 * linux/arch/arm/plat-omap/mcbsp.c
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Multichannel mode not supported.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
22 #include <linux/delay.h>
24 #include <linux/slab.h>
26 #include <plat/mcbsp.h>
27 #include <linux/pm_runtime.h>
29 /* XXX These "sideways" includes are a sign that something is wrong */
30 #include "../mach-omap2/cm2xxx_3xxx.h"
31 #include "../mach-omap2/cm-regbits-34xx.h"
33 struct omap_mcbsp **mcbsp_ptr;
34 int omap_mcbsp_count, omap_mcbsp_cache_size;
36 static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
38 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
40 if (mcbsp->pdata->reg_size == 2) {
41 ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
42 __raw_writew((u16)val, addr);
44 ((u32 *)mcbsp->reg_cache)[reg] = val;
45 __raw_writel(val, addr);
49 static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
51 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
53 if (mcbsp->pdata->reg_size == 2) {
54 return !from_cache ? __raw_readw(addr) :
55 ((u16 *)mcbsp->reg_cache)[reg];
57 return !from_cache ? __raw_readl(addr) :
58 ((u32 *)mcbsp->reg_cache)[reg];
62 #ifdef CONFIG_ARCH_OMAP3
63 static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
65 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
68 static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
70 return __raw_readl(mcbsp->st_data->io_base_st + reg);
74 #define MCBSP_READ(mcbsp, reg) \
75 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
76 #define MCBSP_WRITE(mcbsp, reg, val) \
77 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
78 #define MCBSP_READ_CACHE(mcbsp, reg) \
79 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
81 #define MCBSP_ST_READ(mcbsp, reg) \
82 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
83 #define MCBSP_ST_WRITE(mcbsp, reg, val) \
84 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
86 static void omap_mcbsp_dump_reg(u8 id)
88 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
90 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
91 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
92 MCBSP_READ(mcbsp, DRR2));
93 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
94 MCBSP_READ(mcbsp, DRR1));
95 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
96 MCBSP_READ(mcbsp, DXR2));
97 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
98 MCBSP_READ(mcbsp, DXR1));
99 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
100 MCBSP_READ(mcbsp, SPCR2));
101 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
102 MCBSP_READ(mcbsp, SPCR1));
103 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
104 MCBSP_READ(mcbsp, RCR2));
105 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
106 MCBSP_READ(mcbsp, RCR1));
107 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
108 MCBSP_READ(mcbsp, XCR2));
109 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
110 MCBSP_READ(mcbsp, XCR1));
111 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
112 MCBSP_READ(mcbsp, SRGR2));
113 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
114 MCBSP_READ(mcbsp, SRGR1));
115 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
116 MCBSP_READ(mcbsp, PCR0));
117 dev_dbg(mcbsp->dev, "***********************\n");
120 static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
122 struct omap_mcbsp *mcbsp_tx = dev_id;
125 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
126 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
128 if (irqst_spcr2 & XSYNC_ERR) {
129 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
131 /* Writing zero to XSYNC_ERR clears the IRQ */
132 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
138 static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
140 struct omap_mcbsp *mcbsp_rx = dev_id;
143 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
144 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
146 if (irqst_spcr1 & RSYNC_ERR) {
147 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
149 /* Writing zero to RSYNC_ERR clears the IRQ */
150 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
157 * omap_mcbsp_config simply write a config to the
159 * You either call this function or set the McBSP registers
160 * by yourself before calling omap_mcbsp_start().
162 void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
164 struct omap_mcbsp *mcbsp;
166 if (!omap_mcbsp_check_valid_id(id)) {
167 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
170 mcbsp = id_to_mcbsp_ptr(id);
172 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
173 mcbsp->id, mcbsp->phys_base);
175 /* We write the given config */
176 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
177 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
178 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
179 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
180 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
181 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
182 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
183 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
184 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
185 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
186 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
187 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
188 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
189 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
192 EXPORT_SYMBOL(omap_mcbsp_config);
195 * omap_mcbsp_dma_params - returns the dma channel number
197 * @stream - indicates the direction of data flow (rx or tx)
199 * Returns the dma channel number for the rx channel or tx channel
200 * based on the value of @stream for the requested mcbsp given by @id
202 int omap_mcbsp_dma_ch_params(unsigned int id, unsigned int stream)
204 struct omap_mcbsp *mcbsp;
206 if (!omap_mcbsp_check_valid_id(id)) {
207 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
210 mcbsp = id_to_mcbsp_ptr(id);
213 return mcbsp->dma_rx_sync;
215 return mcbsp->dma_tx_sync;
217 EXPORT_SYMBOL(omap_mcbsp_dma_ch_params);
220 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
222 * @stream - indicates the direction of data flow (rx or tx)
224 * Returns the address of mcbsp data transmit register or data receive register
225 * to be used by DMA for transferring/receiving data based on the value of
226 * @stream for the requested mcbsp given by @id
228 int omap_mcbsp_dma_reg_params(unsigned int id, unsigned int stream)
230 struct omap_mcbsp *mcbsp;
233 if (!omap_mcbsp_check_valid_id(id)) {
234 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
237 mcbsp = id_to_mcbsp_ptr(id);
239 if (mcbsp->pdata->reg_size == 2) {
241 data_reg = OMAP_MCBSP_REG_DRR1;
243 data_reg = OMAP_MCBSP_REG_DXR1;
246 data_reg = OMAP_MCBSP_REG_DRR;
248 data_reg = OMAP_MCBSP_REG_DXR;
251 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
253 EXPORT_SYMBOL(omap_mcbsp_dma_reg_params);
255 #ifdef CONFIG_ARCH_OMAP3
256 static void omap_st_on(struct omap_mcbsp *mcbsp)
261 * Sidetone uses McBSP ICLK - which must not idle when sidetones
262 * are enabled or sidetones start sounding ugly.
264 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
265 w &= ~(1 << (mcbsp->id - 2));
266 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
268 /* Enable McBSP Sidetone */
269 w = MCBSP_READ(mcbsp, SSELCR);
270 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
272 /* Enable Sidetone from Sidetone Core */
273 w = MCBSP_ST_READ(mcbsp, SSELCR);
274 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
277 static void omap_st_off(struct omap_mcbsp *mcbsp)
281 w = MCBSP_ST_READ(mcbsp, SSELCR);
282 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
284 w = MCBSP_READ(mcbsp, SSELCR);
285 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
287 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
288 w |= 1 << (mcbsp->id - 2);
289 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
292 static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
296 val = MCBSP_ST_READ(mcbsp, SSELCR);
298 if (val & ST_COEFFWREN)
299 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
301 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
303 for (i = 0; i < 128; i++)
304 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
308 val = MCBSP_ST_READ(mcbsp, SSELCR);
309 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
310 val = MCBSP_ST_READ(mcbsp, SSELCR);
312 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
315 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
318 static void omap_st_chgain(struct omap_mcbsp *mcbsp)
321 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
323 w = MCBSP_ST_READ(mcbsp, SSELCR);
325 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
326 ST_CH1GAIN(st_data->ch1gain));
329 int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
331 struct omap_mcbsp *mcbsp;
332 struct omap_mcbsp_st_data *st_data;
335 if (!omap_mcbsp_check_valid_id(id)) {
336 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
340 mcbsp = id_to_mcbsp_ptr(id);
341 st_data = mcbsp->st_data;
346 spin_lock_irq(&mcbsp->lock);
348 st_data->ch0gain = chgain;
349 else if (channel == 1)
350 st_data->ch1gain = chgain;
354 if (st_data->enabled)
355 omap_st_chgain(mcbsp);
356 spin_unlock_irq(&mcbsp->lock);
360 EXPORT_SYMBOL(omap_st_set_chgain);
362 int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
364 struct omap_mcbsp *mcbsp;
365 struct omap_mcbsp_st_data *st_data;
368 if (!omap_mcbsp_check_valid_id(id)) {
369 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
373 mcbsp = id_to_mcbsp_ptr(id);
374 st_data = mcbsp->st_data;
379 spin_lock_irq(&mcbsp->lock);
381 *chgain = st_data->ch0gain;
382 else if (channel == 1)
383 *chgain = st_data->ch1gain;
386 spin_unlock_irq(&mcbsp->lock);
390 EXPORT_SYMBOL(omap_st_get_chgain);
392 static int omap_st_start(struct omap_mcbsp *mcbsp)
394 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
396 if (st_data && st_data->enabled && !st_data->running) {
397 omap_st_fir_write(mcbsp, st_data->taps);
398 omap_st_chgain(mcbsp);
402 st_data->running = 1;
409 int omap_st_enable(unsigned int id)
411 struct omap_mcbsp *mcbsp;
412 struct omap_mcbsp_st_data *st_data;
414 if (!omap_mcbsp_check_valid_id(id)) {
415 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
419 mcbsp = id_to_mcbsp_ptr(id);
420 st_data = mcbsp->st_data;
425 spin_lock_irq(&mcbsp->lock);
426 st_data->enabled = 1;
427 omap_st_start(mcbsp);
428 spin_unlock_irq(&mcbsp->lock);
432 EXPORT_SYMBOL(omap_st_enable);
434 static int omap_st_stop(struct omap_mcbsp *mcbsp)
436 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
438 if (st_data && st_data->running) {
441 st_data->running = 0;
448 int omap_st_disable(unsigned int id)
450 struct omap_mcbsp *mcbsp;
451 struct omap_mcbsp_st_data *st_data;
454 if (!omap_mcbsp_check_valid_id(id)) {
455 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
459 mcbsp = id_to_mcbsp_ptr(id);
460 st_data = mcbsp->st_data;
465 spin_lock_irq(&mcbsp->lock);
467 st_data->enabled = 0;
468 spin_unlock_irq(&mcbsp->lock);
472 EXPORT_SYMBOL(omap_st_disable);
474 int omap_st_is_enabled(unsigned int id)
476 struct omap_mcbsp *mcbsp;
477 struct omap_mcbsp_st_data *st_data;
479 if (!omap_mcbsp_check_valid_id(id)) {
480 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
484 mcbsp = id_to_mcbsp_ptr(id);
485 st_data = mcbsp->st_data;
491 return st_data->enabled;
493 EXPORT_SYMBOL(omap_st_is_enabled);
496 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
497 * The threshold parameter is 1 based, and it is converted (threshold - 1)
498 * for the THRSH2 register.
500 void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
502 struct omap_mcbsp *mcbsp;
504 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
507 if (!omap_mcbsp_check_valid_id(id)) {
508 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
511 mcbsp = id_to_mcbsp_ptr(id);
513 if (threshold && threshold <= mcbsp->max_tx_thres)
514 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
516 EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
519 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
520 * The threshold parameter is 1 based, and it is converted (threshold - 1)
521 * for the THRSH1 register.
523 void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
525 struct omap_mcbsp *mcbsp;
527 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
530 if (!omap_mcbsp_check_valid_id(id)) {
531 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
534 mcbsp = id_to_mcbsp_ptr(id);
536 if (threshold && threshold <= mcbsp->max_rx_thres)
537 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
539 EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
542 * omap_mcbsp_get_max_tx_thres just return the current configured
543 * maximum threshold for transmission
545 u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
547 struct omap_mcbsp *mcbsp;
549 if (!omap_mcbsp_check_valid_id(id)) {
550 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
553 mcbsp = id_to_mcbsp_ptr(id);
555 return mcbsp->max_tx_thres;
557 EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
560 * omap_mcbsp_get_max_rx_thres just return the current configured
561 * maximum threshold for reception
563 u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
565 struct omap_mcbsp *mcbsp;
567 if (!omap_mcbsp_check_valid_id(id)) {
568 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
571 mcbsp = id_to_mcbsp_ptr(id);
573 return mcbsp->max_rx_thres;
575 EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
577 u16 omap_mcbsp_get_fifo_size(unsigned int id)
579 struct omap_mcbsp *mcbsp;
581 if (!omap_mcbsp_check_valid_id(id)) {
582 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
585 mcbsp = id_to_mcbsp_ptr(id);
587 return mcbsp->pdata->buffer_size;
589 EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
592 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
594 u16 omap_mcbsp_get_tx_delay(unsigned int id)
596 struct omap_mcbsp *mcbsp;
599 if (!omap_mcbsp_check_valid_id(id)) {
600 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
603 mcbsp = id_to_mcbsp_ptr(id);
605 /* Returns the number of free locations in the buffer */
606 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
608 /* Number of slots are different in McBSP ports */
609 return mcbsp->pdata->buffer_size - buffstat;
611 EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
614 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
615 * to reach the threshold value (when the DMA will be triggered to read it)
617 u16 omap_mcbsp_get_rx_delay(unsigned int id)
619 struct omap_mcbsp *mcbsp;
620 u16 buffstat, threshold;
622 if (!omap_mcbsp_check_valid_id(id)) {
623 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
626 mcbsp = id_to_mcbsp_ptr(id);
628 /* Returns the number of used locations in the buffer */
629 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
631 threshold = MCBSP_READ(mcbsp, THRSH1);
633 /* Return the number of location till we reach the threshold limit */
634 if (threshold <= buffstat)
637 return threshold - buffstat;
639 EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
642 * omap_mcbsp_get_dma_op_mode just return the current configured
643 * operating mode for the mcbsp channel
645 int omap_mcbsp_get_dma_op_mode(unsigned int id)
647 struct omap_mcbsp *mcbsp;
650 if (!omap_mcbsp_check_valid_id(id)) {
651 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
654 mcbsp = id_to_mcbsp_ptr(id);
656 dma_op_mode = mcbsp->dma_op_mode;
660 EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
662 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
665 * Enable wakup behavior, smart idle and all wakeups
666 * REVISIT: some wakeups may be unnecessary
668 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
669 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
673 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
676 * Disable wakup behavior, smart idle and all wakeups
678 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
680 * HW bug workaround - If no_idle mode is taken, we need to
681 * go to smart_idle before going to always_idle, or the
682 * device will not hit retention anymore.
685 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
689 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
690 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
691 static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
692 static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
695 int omap_mcbsp_request(unsigned int id)
697 struct omap_mcbsp *mcbsp;
701 if (!omap_mcbsp_check_valid_id(id)) {
702 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
705 mcbsp = id_to_mcbsp_ptr(id);
707 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
712 spin_lock(&mcbsp->lock);
714 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
721 mcbsp->reg_cache = reg_cache;
722 spin_unlock(&mcbsp->lock);
724 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
725 mcbsp->pdata->ops->request(id);
727 pm_runtime_get_sync(mcbsp->dev);
729 /* Do procedure specific to omap34xx arch, if applicable */
730 omap34xx_mcbsp_request(mcbsp);
733 * Make sure that transmitter, receiver and sample-rate generator are
734 * not running before activating IRQs.
736 MCBSP_WRITE(mcbsp, SPCR1, 0);
737 MCBSP_WRITE(mcbsp, SPCR2, 0);
739 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
740 0, "McBSP", (void *)mcbsp);
742 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
743 "for McBSP%d\n", mcbsp->tx_irq,
745 goto err_clk_disable;
749 err = request_irq(mcbsp->rx_irq,
750 omap_mcbsp_rx_irq_handler,
751 0, "McBSP", (void *)mcbsp);
753 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
754 "for McBSP%d\n", mcbsp->rx_irq,
762 free_irq(mcbsp->tx_irq, (void *)mcbsp);
764 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
765 mcbsp->pdata->ops->free(id);
767 /* Do procedure specific to omap34xx arch, if applicable */
768 omap34xx_mcbsp_free(mcbsp);
770 pm_runtime_put_sync(mcbsp->dev);
772 spin_lock(&mcbsp->lock);
774 mcbsp->reg_cache = NULL;
776 spin_unlock(&mcbsp->lock);
781 EXPORT_SYMBOL(omap_mcbsp_request);
783 void omap_mcbsp_free(unsigned int id)
785 struct omap_mcbsp *mcbsp;
788 if (!omap_mcbsp_check_valid_id(id)) {
789 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
792 mcbsp = id_to_mcbsp_ptr(id);
794 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
795 mcbsp->pdata->ops->free(id);
797 /* Do procedure specific to omap34xx arch, if applicable */
798 omap34xx_mcbsp_free(mcbsp);
800 pm_runtime_put_sync(mcbsp->dev);
803 free_irq(mcbsp->rx_irq, (void *)mcbsp);
804 free_irq(mcbsp->tx_irq, (void *)mcbsp);
806 reg_cache = mcbsp->reg_cache;
808 spin_lock(&mcbsp->lock);
810 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
813 mcbsp->reg_cache = NULL;
814 spin_unlock(&mcbsp->lock);
819 EXPORT_SYMBOL(omap_mcbsp_free);
822 * Here we start the McBSP, by enabling transmitter, receiver or both.
823 * If no transmitter or receiver is active prior calling, then sample-rate
824 * generator and frame sync are started.
826 void omap_mcbsp_start(unsigned int id, int tx, int rx)
828 struct omap_mcbsp *mcbsp;
832 if (!omap_mcbsp_check_valid_id(id)) {
833 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
836 mcbsp = id_to_mcbsp_ptr(id);
838 if (cpu_is_omap34xx())
839 omap_st_start(mcbsp);
841 /* Only enable SRG, if McBSP is master */
842 w = MCBSP_READ_CACHE(mcbsp, PCR0);
843 if (w & (FSXM | FSRM | CLKXM | CLKRM))
844 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
845 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
848 /* Start the sample generator */
849 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
850 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
853 /* Enable transmitter and receiver */
855 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
856 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
859 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
860 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
863 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
864 * REVISIT: 100us may give enough time for two CLKSRG, however
865 * due to some unknown PM related, clock gating etc. reason it
871 /* Start frame sync */
872 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
873 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
876 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
877 /* Release the transmitter and receiver */
878 w = MCBSP_READ_CACHE(mcbsp, XCCR);
879 w &= ~(tx ? XDISABLE : 0);
880 MCBSP_WRITE(mcbsp, XCCR, w);
881 w = MCBSP_READ_CACHE(mcbsp, RCCR);
882 w &= ~(rx ? RDISABLE : 0);
883 MCBSP_WRITE(mcbsp, RCCR, w);
886 /* Dump McBSP Regs */
887 omap_mcbsp_dump_reg(id);
889 EXPORT_SYMBOL(omap_mcbsp_start);
891 void omap_mcbsp_stop(unsigned int id, int tx, int rx)
893 struct omap_mcbsp *mcbsp;
897 if (!omap_mcbsp_check_valid_id(id)) {
898 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
902 mcbsp = id_to_mcbsp_ptr(id);
904 /* Reset transmitter */
906 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
907 w = MCBSP_READ_CACHE(mcbsp, XCCR);
908 w |= (tx ? XDISABLE : 0);
909 MCBSP_WRITE(mcbsp, XCCR, w);
911 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
912 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
916 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
917 w = MCBSP_READ_CACHE(mcbsp, RCCR);
918 w |= (rx ? RDISABLE : 0);
919 MCBSP_WRITE(mcbsp, RCCR, w);
921 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
922 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
924 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
925 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
928 /* Reset the sample rate generator */
929 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
930 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
933 if (cpu_is_omap34xx())
936 EXPORT_SYMBOL(omap_mcbsp_stop);
939 * The following functions are only required on an OMAP1-only build.
940 * mach-omap2/mcbsp.c contains the real functions
942 #ifndef CONFIG_ARCH_OMAP2PLUS
943 int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
945 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
950 void omap2_mcbsp1_mux_clkr_src(u8 mux)
952 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
957 void omap2_mcbsp1_mux_fsr_src(u8 mux)
959 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
965 #ifdef CONFIG_ARCH_OMAP3
966 #define max_thres(m) (mcbsp->pdata->buffer_size)
967 #define valid_threshold(m, val) ((val) <= max_thres(m))
968 #define THRESHOLD_PROP_BUILDER(prop) \
969 static ssize_t prop##_show(struct device *dev, \
970 struct device_attribute *attr, char *buf) \
972 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
974 return sprintf(buf, "%u\n", mcbsp->prop); \
977 static ssize_t prop##_store(struct device *dev, \
978 struct device_attribute *attr, \
979 const char *buf, size_t size) \
981 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
985 status = strict_strtoul(buf, 0, &val); \
989 if (!valid_threshold(mcbsp, val)) \
996 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
998 THRESHOLD_PROP_BUILDER(max_tx_thres);
999 THRESHOLD_PROP_BUILDER(max_rx_thres);
1001 static const char *dma_op_modes[] = {
1002 "element", "threshold", "frame",
1005 static ssize_t dma_op_mode_show(struct device *dev,
1006 struct device_attribute *attr, char *buf)
1008 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1009 int dma_op_mode, i = 0;
1011 const char * const *s;
1013 dma_op_mode = mcbsp->dma_op_mode;
1015 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1016 if (dma_op_mode == i)
1017 len += sprintf(buf + len, "[%s] ", *s);
1019 len += sprintf(buf + len, "%s ", *s);
1021 len += sprintf(buf + len, "\n");
1026 static ssize_t dma_op_mode_store(struct device *dev,
1027 struct device_attribute *attr,
1028 const char *buf, size_t size)
1030 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1031 const char * const *s;
1034 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1035 if (sysfs_streq(buf, *s))
1038 if (i == ARRAY_SIZE(dma_op_modes))
1041 spin_lock_irq(&mcbsp->lock);
1046 mcbsp->dma_op_mode = i;
1049 spin_unlock_irq(&mcbsp->lock);
1054 static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1056 static ssize_t st_taps_show(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1059 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1060 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1064 spin_lock_irq(&mcbsp->lock);
1065 for (i = 0; i < st_data->nr_taps; i++)
1066 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1069 status += sprintf(&buf[status], "\n");
1070 spin_unlock_irq(&mcbsp->lock);
1075 static ssize_t st_taps_store(struct device *dev,
1076 struct device_attribute *attr,
1077 const char *buf, size_t size)
1079 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1080 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1081 int val, tmp, status, i = 0;
1083 spin_lock_irq(&mcbsp->lock);
1084 memset(st_data->taps, 0, sizeof(st_data->taps));
1085 st_data->nr_taps = 0;
1088 status = sscanf(buf, "%d%n", &val, &tmp);
1089 if (status < 0 || status == 0) {
1093 if (val < -32768 || val > 32767) {
1097 st_data->taps[i++] = val;
1104 st_data->nr_taps = i;
1107 spin_unlock_irq(&mcbsp->lock);
1112 static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1114 static const struct attribute *additional_attrs[] = {
1115 &dev_attr_max_tx_thres.attr,
1116 &dev_attr_max_rx_thres.attr,
1117 &dev_attr_dma_op_mode.attr,
1121 static const struct attribute_group additional_attr_group = {
1122 .attrs = (struct attribute **)additional_attrs,
1125 static inline int __devinit omap_additional_add(struct device *dev)
1127 return sysfs_create_group(&dev->kobj, &additional_attr_group);
1130 static inline void __devexit omap_additional_remove(struct device *dev)
1132 sysfs_remove_group(&dev->kobj, &additional_attr_group);
1135 static const struct attribute *sidetone_attrs[] = {
1136 &dev_attr_st_taps.attr,
1140 static const struct attribute_group sidetone_attr_group = {
1141 .attrs = (struct attribute **)sidetone_attrs,
1144 static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
1146 struct platform_device *pdev;
1147 struct resource *res;
1148 struct omap_mcbsp_st_data *st_data;
1151 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1157 pdev = container_of(mcbsp->dev, struct platform_device, dev);
1159 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1160 st_data->io_base_st = ioremap(res->start, resource_size(res));
1161 if (!st_data->io_base_st) {
1166 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1170 mcbsp->st_data = st_data;
1174 iounmap(st_data->io_base_st);
1182 static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1184 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1187 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1188 iounmap(st_data->io_base_st);
1193 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1195 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1196 if (cpu_is_omap34xx()) {
1198 * Initially configure the maximum thresholds to a safe value.
1199 * The McBSP FIFO usage with these values should not go under
1201 * If the whole FIFO without safety buffer is used, than there
1202 * is a possibility that the DMA will be not able to push the
1203 * new data on time, causing channel shifts in runtime.
1205 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1206 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1208 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1209 * for mcbsp2 instances.
1211 if (omap_additional_add(mcbsp->dev))
1212 dev_warn(mcbsp->dev,
1213 "Unable to create additional controls\n");
1215 if (mcbsp->id == 2 || mcbsp->id == 3)
1216 if (omap_st_add(mcbsp))
1217 dev_warn(mcbsp->dev,
1218 "Unable to create sidetone controls\n");
1221 mcbsp->max_tx_thres = -EINVAL;
1222 mcbsp->max_rx_thres = -EINVAL;
1226 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1228 if (cpu_is_omap34xx()) {
1229 omap_additional_remove(mcbsp->dev);
1231 if (mcbsp->id == 2 || mcbsp->id == 3)
1232 omap_st_remove(mcbsp);
1236 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1237 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1238 #endif /* CONFIG_ARCH_OMAP3 */
1241 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1242 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1244 static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
1246 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
1247 struct omap_mcbsp *mcbsp;
1248 int id = pdev->id - 1;
1249 struct resource *res;
1253 dev_err(&pdev->dev, "McBSP device initialized without"
1259 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
1261 if (id >= omap_mcbsp_count) {
1262 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1267 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1273 spin_lock_init(&mcbsp->lock);
1277 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1279 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1281 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1282 "resource\n", __func__, pdev->id);
1287 mcbsp->phys_base = res->start;
1288 omap_mcbsp_cache_size = resource_size(res);
1289 mcbsp->io_base = ioremap(res->start, resource_size(res));
1290 if (!mcbsp->io_base) {
1295 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1297 mcbsp->phys_dma_base = mcbsp->phys_base;
1299 mcbsp->phys_dma_base = res->start;
1301 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1302 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1304 /* From OMAP4 there will be a single irq line */
1305 if (mcbsp->tx_irq == -ENXIO)
1306 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1308 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1310 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1311 __func__, pdev->id);
1315 mcbsp->dma_rx_sync = res->start;
1317 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1319 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1320 __func__, pdev->id);
1324 mcbsp->dma_tx_sync = res->start;
1326 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1327 if (IS_ERR(mcbsp->fclk)) {
1328 ret = PTR_ERR(mcbsp->fclk);
1329 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1333 mcbsp->pdata = pdata;
1334 mcbsp->dev = &pdev->dev;
1335 mcbsp_ptr[id] = mcbsp;
1336 platform_set_drvdata(pdev, mcbsp);
1337 pm_runtime_enable(mcbsp->dev);
1339 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1340 omap34xx_device_init(mcbsp);
1345 iounmap(mcbsp->io_base);
1352 static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
1354 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1356 platform_set_drvdata(pdev, NULL);
1359 if (mcbsp->pdata && mcbsp->pdata->ops &&
1360 mcbsp->pdata->ops->free)
1361 mcbsp->pdata->ops->free(mcbsp->id);
1363 omap34xx_device_exit(mcbsp);
1365 clk_put(mcbsp->fclk);
1367 iounmap(mcbsp->io_base);
1374 static struct platform_driver omap_mcbsp_driver = {
1375 .probe = omap_mcbsp_probe,
1376 .remove = __devexit_p(omap_mcbsp_remove),
1378 .name = "omap-mcbsp",
1382 int __init omap_mcbsp_init(void)
1384 /* Register the McBSP driver */
1385 return platform_driver_register(&omap_mcbsp_driver);