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/wait.h>
20 #include <linux/completion.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23 #include <linux/clk.h>
24 #include <linux/delay.h>
28 #include <mach/mcbsp.h>
30 struct omap_mcbsp **mcbsp_ptr;
33 void omap_mcbsp_write(void __iomem *io_base, u16 reg, u32 val)
35 if (cpu_class_is_omap1() || cpu_is_omap2420())
36 __raw_writew((u16)val, io_base + reg);
38 __raw_writel(val, io_base + reg);
41 int omap_mcbsp_read(void __iomem *io_base, u16 reg)
43 if (cpu_class_is_omap1() || cpu_is_omap2420())
44 return __raw_readw(io_base + reg);
46 return __raw_readl(io_base + reg);
49 #define OMAP_MCBSP_READ(base, reg) \
50 omap_mcbsp_read(base, OMAP_MCBSP_REG_##reg)
51 #define OMAP_MCBSP_WRITE(base, reg, val) \
52 omap_mcbsp_write(base, OMAP_MCBSP_REG_##reg, val)
54 #define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
55 #define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
57 static void omap_mcbsp_dump_reg(u8 id)
59 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
61 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
62 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
63 OMAP_MCBSP_READ(mcbsp->io_base, DRR2));
64 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
65 OMAP_MCBSP_READ(mcbsp->io_base, DRR1));
66 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
67 OMAP_MCBSP_READ(mcbsp->io_base, DXR2));
68 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
69 OMAP_MCBSP_READ(mcbsp->io_base, DXR1));
70 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
71 OMAP_MCBSP_READ(mcbsp->io_base, SPCR2));
72 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
73 OMAP_MCBSP_READ(mcbsp->io_base, SPCR1));
74 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
75 OMAP_MCBSP_READ(mcbsp->io_base, RCR2));
76 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
77 OMAP_MCBSP_READ(mcbsp->io_base, RCR1));
78 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
79 OMAP_MCBSP_READ(mcbsp->io_base, XCR2));
80 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
81 OMAP_MCBSP_READ(mcbsp->io_base, XCR1));
82 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
83 OMAP_MCBSP_READ(mcbsp->io_base, SRGR2));
84 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
85 OMAP_MCBSP_READ(mcbsp->io_base, SRGR1));
86 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
87 OMAP_MCBSP_READ(mcbsp->io_base, PCR0));
88 dev_dbg(mcbsp->dev, "***********************\n");
91 static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
93 struct omap_mcbsp *mcbsp_tx = dev_id;
96 irqst_spcr2 = OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2);
97 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
99 if (irqst_spcr2 & XSYNC_ERR) {
100 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
102 /* Writing zero to XSYNC_ERR clears the IRQ */
103 OMAP_MCBSP_WRITE(mcbsp_tx->io_base, SPCR2,
104 irqst_spcr2 & ~(XSYNC_ERR));
106 complete(&mcbsp_tx->tx_irq_completion);
112 static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
114 struct omap_mcbsp *mcbsp_rx = dev_id;
117 irqst_spcr1 = OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR1);
118 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
120 if (irqst_spcr1 & RSYNC_ERR) {
121 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
123 /* Writing zero to RSYNC_ERR clears the IRQ */
124 OMAP_MCBSP_WRITE(mcbsp_rx->io_base, SPCR1,
125 irqst_spcr1 & ~(RSYNC_ERR));
127 complete(&mcbsp_rx->tx_irq_completion);
133 static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
135 struct omap_mcbsp *mcbsp_dma_tx = data;
137 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
138 OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
140 /* We can free the channels */
141 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
142 mcbsp_dma_tx->dma_tx_lch = -1;
144 complete(&mcbsp_dma_tx->tx_dma_completion);
147 static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
149 struct omap_mcbsp *mcbsp_dma_rx = data;
151 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
152 OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
154 /* We can free the channels */
155 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
156 mcbsp_dma_rx->dma_rx_lch = -1;
158 complete(&mcbsp_dma_rx->rx_dma_completion);
162 * omap_mcbsp_config simply write a config to the
164 * You either call this function or set the McBSP registers
165 * by yourself before calling omap_mcbsp_start().
167 void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
169 struct omap_mcbsp *mcbsp;
170 void __iomem *io_base;
172 if (!omap_mcbsp_check_valid_id(id)) {
173 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
176 mcbsp = id_to_mcbsp_ptr(id);
178 io_base = mcbsp->io_base;
179 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
180 mcbsp->id, mcbsp->phys_base);
182 /* We write the given config */
183 OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
184 OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
185 OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
186 OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
187 OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
188 OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
189 OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
190 OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
191 OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
192 OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
193 OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
194 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
195 OMAP_MCBSP_WRITE(io_base, XCCR, config->xccr);
196 OMAP_MCBSP_WRITE(io_base, RCCR, config->rccr);
199 EXPORT_SYMBOL(omap_mcbsp_config);
201 #ifdef CONFIG_ARCH_OMAP34XX
203 * omap_mcbsp_set_tx_threshold configures how to deal
204 * with transmit threshold. the threshold value and handler can be
207 void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
209 struct omap_mcbsp *mcbsp;
210 void __iomem *io_base;
212 if (!cpu_is_omap34xx())
215 if (!omap_mcbsp_check_valid_id(id)) {
216 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
219 mcbsp = id_to_mcbsp_ptr(id);
220 io_base = mcbsp->io_base;
222 OMAP_MCBSP_WRITE(io_base, THRSH2, threshold);
224 EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
227 * omap_mcbsp_set_rx_threshold configures how to deal
228 * with receive threshold. the threshold value and handler can be
231 void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
233 struct omap_mcbsp *mcbsp;
234 void __iomem *io_base;
236 if (!cpu_is_omap34xx())
239 if (!omap_mcbsp_check_valid_id(id)) {
240 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
243 mcbsp = id_to_mcbsp_ptr(id);
244 io_base = mcbsp->io_base;
246 OMAP_MCBSP_WRITE(io_base, THRSH1, threshold);
248 EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
251 * omap_mcbsp_get_max_tx_thres just return the current configured
252 * maximum threshold for transmission
254 u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
256 struct omap_mcbsp *mcbsp;
258 if (!omap_mcbsp_check_valid_id(id)) {
259 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
262 mcbsp = id_to_mcbsp_ptr(id);
264 return mcbsp->max_tx_thres;
266 EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
269 * omap_mcbsp_get_max_rx_thres just return the current configured
270 * maximum threshold for reception
272 u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
274 struct omap_mcbsp *mcbsp;
276 if (!omap_mcbsp_check_valid_id(id)) {
277 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
280 mcbsp = id_to_mcbsp_ptr(id);
282 return mcbsp->max_rx_thres;
284 EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
287 * omap_mcbsp_get_dma_op_mode just return the current configured
288 * operating mode for the mcbsp channel
290 int omap_mcbsp_get_dma_op_mode(unsigned int id)
292 struct omap_mcbsp *mcbsp;
295 if (!omap_mcbsp_check_valid_id(id)) {
296 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
299 mcbsp = id_to_mcbsp_ptr(id);
301 spin_lock_irq(&mcbsp->lock);
302 dma_op_mode = mcbsp->dma_op_mode;
303 spin_unlock_irq(&mcbsp->lock);
307 EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
309 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
312 * Enable wakup behavior, smart idle and all wakeups
313 * REVISIT: some wakeups may be unnecessary
315 if (cpu_is_omap34xx()) {
318 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
319 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
321 spin_lock_irq(&mcbsp->lock);
322 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
323 syscon |= (ENAWAKEUP | SIDLEMODE(0x02) |
324 CLOCKACTIVITY(0x02));
325 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN,
328 syscon |= SIDLEMODE(0x01);
330 spin_unlock_irq(&mcbsp->lock);
332 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
336 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
339 * Disable wakup behavior, smart idle and all wakeups
341 if (cpu_is_omap34xx()) {
344 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
345 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
346 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
348 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, 0);
352 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
353 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
357 * We can choose between IRQ based or polled IO.
358 * This needs to be called before omap_mcbsp_request().
360 int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
362 struct omap_mcbsp *mcbsp;
364 if (!omap_mcbsp_check_valid_id(id)) {
365 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
368 mcbsp = id_to_mcbsp_ptr(id);
370 spin_lock(&mcbsp->lock);
373 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
375 spin_unlock(&mcbsp->lock);
379 mcbsp->io_type = io_type;
381 spin_unlock(&mcbsp->lock);
385 EXPORT_SYMBOL(omap_mcbsp_set_io_type);
387 int omap_mcbsp_request(unsigned int id)
389 struct omap_mcbsp *mcbsp;
392 if (!omap_mcbsp_check_valid_id(id)) {
393 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
396 mcbsp = id_to_mcbsp_ptr(id);
398 spin_lock(&mcbsp->lock);
400 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
402 spin_unlock(&mcbsp->lock);
407 spin_unlock(&mcbsp->lock);
409 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
410 mcbsp->pdata->ops->request(id);
412 clk_enable(mcbsp->iclk);
413 clk_enable(mcbsp->fclk);
415 /* Do procedure specific to omap34xx arch, if applicable */
416 omap34xx_mcbsp_request(mcbsp);
419 * Make sure that transmitter, receiver and sample-rate generator are
420 * not running before activating IRQs.
422 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
423 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
425 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
426 /* We need to get IRQs here */
427 init_completion(&mcbsp->tx_irq_completion);
428 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
429 0, "McBSP", (void *)mcbsp);
431 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
432 "for McBSP%d\n", mcbsp->tx_irq,
437 init_completion(&mcbsp->rx_irq_completion);
438 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
439 0, "McBSP", (void *)mcbsp);
441 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
442 "for McBSP%d\n", mcbsp->rx_irq,
444 free_irq(mcbsp->tx_irq, (void *)mcbsp);
451 EXPORT_SYMBOL(omap_mcbsp_request);
453 void omap_mcbsp_free(unsigned int id)
455 struct omap_mcbsp *mcbsp;
457 if (!omap_mcbsp_check_valid_id(id)) {
458 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
461 mcbsp = id_to_mcbsp_ptr(id);
463 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
464 mcbsp->pdata->ops->free(id);
466 /* Do procedure specific to omap34xx arch, if applicable */
467 omap34xx_mcbsp_free(mcbsp);
469 clk_disable(mcbsp->fclk);
470 clk_disable(mcbsp->iclk);
472 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
474 free_irq(mcbsp->rx_irq, (void *)mcbsp);
475 free_irq(mcbsp->tx_irq, (void *)mcbsp);
478 spin_lock(&mcbsp->lock);
480 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
482 spin_unlock(&mcbsp->lock);
487 spin_unlock(&mcbsp->lock);
489 EXPORT_SYMBOL(omap_mcbsp_free);
492 * Here we start the McBSP, by enabling transmitter, receiver or both.
493 * If no transmitter or receiver is active prior calling, then sample-rate
494 * generator and frame sync are started.
496 void omap_mcbsp_start(unsigned int id, int tx, int rx)
498 struct omap_mcbsp *mcbsp;
499 void __iomem *io_base;
503 if (!omap_mcbsp_check_valid_id(id)) {
504 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
507 mcbsp = id_to_mcbsp_ptr(id);
508 io_base = mcbsp->io_base;
510 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
511 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
513 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
514 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
517 /* Start the sample generator */
518 w = OMAP_MCBSP_READ(io_base, SPCR2);
519 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
522 /* Enable transmitter and receiver */
523 w = OMAP_MCBSP_READ(io_base, SPCR2);
524 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
526 w = OMAP_MCBSP_READ(io_base, SPCR1);
527 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
530 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
531 * REVISIT: 100us may give enough time for two CLKSRG, however
532 * due to some unknown PM related, clock gating etc. reason it
538 /* Start frame sync */
539 w = OMAP_MCBSP_READ(io_base, SPCR2);
540 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
543 /* Dump McBSP Regs */
544 omap_mcbsp_dump_reg(id);
546 EXPORT_SYMBOL(omap_mcbsp_start);
548 void omap_mcbsp_stop(unsigned int id, int tx, int rx)
550 struct omap_mcbsp *mcbsp;
551 void __iomem *io_base;
555 if (!omap_mcbsp_check_valid_id(id)) {
556 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
560 mcbsp = id_to_mcbsp_ptr(id);
561 io_base = mcbsp->io_base;
563 /* Reset transmitter */
564 w = OMAP_MCBSP_READ(io_base, SPCR2);
565 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
568 w = OMAP_MCBSP_READ(io_base, SPCR1);
569 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
571 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
572 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
575 /* Reset the sample rate generator */
576 w = OMAP_MCBSP_READ(io_base, SPCR2);
577 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
580 EXPORT_SYMBOL(omap_mcbsp_stop);
582 void omap_mcbsp_xmit_enable(unsigned int id, u8 enable)
584 struct omap_mcbsp *mcbsp;
585 void __iomem *io_base;
588 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
591 if (!omap_mcbsp_check_valid_id(id)) {
592 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
596 mcbsp = id_to_mcbsp_ptr(id);
597 io_base = mcbsp->io_base;
599 w = OMAP_MCBSP_READ(io_base, XCCR);
602 OMAP_MCBSP_WRITE(io_base, XCCR, w & ~(XDISABLE));
604 OMAP_MCBSP_WRITE(io_base, XCCR, w | XDISABLE);
606 EXPORT_SYMBOL(omap_mcbsp_xmit_enable);
608 void omap_mcbsp_recv_enable(unsigned int id, u8 enable)
610 struct omap_mcbsp *mcbsp;
611 void __iomem *io_base;
614 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
617 if (!omap_mcbsp_check_valid_id(id)) {
618 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
622 mcbsp = id_to_mcbsp_ptr(id);
623 io_base = mcbsp->io_base;
625 w = OMAP_MCBSP_READ(io_base, RCCR);
628 OMAP_MCBSP_WRITE(io_base, RCCR, w & ~(RDISABLE));
630 OMAP_MCBSP_WRITE(io_base, RCCR, w | RDISABLE);
632 EXPORT_SYMBOL(omap_mcbsp_recv_enable);
634 /* polled mcbsp i/o operations */
635 int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
637 struct omap_mcbsp *mcbsp;
640 if (!omap_mcbsp_check_valid_id(id)) {
641 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
645 mcbsp = id_to_mcbsp_ptr(id);
646 base = mcbsp->io_base;
648 writew(buf, base + OMAP_MCBSP_REG_DXR1);
649 /* if frame sync error - clear the error */
650 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
652 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
653 base + OMAP_MCBSP_REG_SPCR2);
657 /* wait for transmit confirmation */
659 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
660 if (attemps++ > 1000) {
661 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
663 base + OMAP_MCBSP_REG_SPCR2);
665 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
667 base + OMAP_MCBSP_REG_SPCR2);
669 dev_err(mcbsp->dev, "Could not write to"
670 " McBSP%d Register\n", mcbsp->id);
678 EXPORT_SYMBOL(omap_mcbsp_pollwrite);
680 int omap_mcbsp_pollread(unsigned int id, u16 *buf)
682 struct omap_mcbsp *mcbsp;
685 if (!omap_mcbsp_check_valid_id(id)) {
686 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
689 mcbsp = id_to_mcbsp_ptr(id);
691 base = mcbsp->io_base;
692 /* if frame sync error - clear the error */
693 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
695 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
696 base + OMAP_MCBSP_REG_SPCR1);
700 /* wait for recieve confirmation */
702 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
703 if (attemps++ > 1000) {
704 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
706 base + OMAP_MCBSP_REG_SPCR1);
708 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
710 base + OMAP_MCBSP_REG_SPCR1);
712 dev_err(mcbsp->dev, "Could not read from"
713 " McBSP%d Register\n", mcbsp->id);
718 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
722 EXPORT_SYMBOL(omap_mcbsp_pollread);
725 * IRQ based word transmission.
727 void omap_mcbsp_xmit_word(unsigned int id, u32 word)
729 struct omap_mcbsp *mcbsp;
730 void __iomem *io_base;
731 omap_mcbsp_word_length word_length;
733 if (!omap_mcbsp_check_valid_id(id)) {
734 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
738 mcbsp = id_to_mcbsp_ptr(id);
739 io_base = mcbsp->io_base;
740 word_length = mcbsp->tx_word_length;
742 wait_for_completion(&mcbsp->tx_irq_completion);
744 if (word_length > OMAP_MCBSP_WORD_16)
745 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
746 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
748 EXPORT_SYMBOL(omap_mcbsp_xmit_word);
750 u32 omap_mcbsp_recv_word(unsigned int id)
752 struct omap_mcbsp *mcbsp;
753 void __iomem *io_base;
754 u16 word_lsb, word_msb = 0;
755 omap_mcbsp_word_length word_length;
757 if (!omap_mcbsp_check_valid_id(id)) {
758 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
761 mcbsp = id_to_mcbsp_ptr(id);
763 word_length = mcbsp->rx_word_length;
764 io_base = mcbsp->io_base;
766 wait_for_completion(&mcbsp->rx_irq_completion);
768 if (word_length > OMAP_MCBSP_WORD_16)
769 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
770 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
772 return (word_lsb | (word_msb << 16));
774 EXPORT_SYMBOL(omap_mcbsp_recv_word);
776 int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
778 struct omap_mcbsp *mcbsp;
779 void __iomem *io_base;
780 omap_mcbsp_word_length tx_word_length;
781 omap_mcbsp_word_length rx_word_length;
782 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
784 if (!omap_mcbsp_check_valid_id(id)) {
785 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
788 mcbsp = id_to_mcbsp_ptr(id);
789 io_base = mcbsp->io_base;
790 tx_word_length = mcbsp->tx_word_length;
791 rx_word_length = mcbsp->rx_word_length;
793 if (tx_word_length != rx_word_length)
796 /* First we wait for the transmitter to be ready */
797 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
798 while (!(spcr2 & XRDY)) {
799 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
800 if (attempts++ > 1000) {
801 /* We must reset the transmitter */
802 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
804 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
806 dev_err(mcbsp->dev, "McBSP%d transmitter not "
807 "ready\n", mcbsp->id);
812 /* Now we can push the data */
813 if (tx_word_length > OMAP_MCBSP_WORD_16)
814 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
815 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
817 /* We wait for the receiver to be ready */
818 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
819 while (!(spcr1 & RRDY)) {
820 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
821 if (attempts++ > 1000) {
822 /* We must reset the receiver */
823 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
825 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
827 dev_err(mcbsp->dev, "McBSP%d receiver not "
828 "ready\n", mcbsp->id);
833 /* Receiver is ready, let's read the dummy data */
834 if (rx_word_length > OMAP_MCBSP_WORD_16)
835 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
836 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
840 EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
842 int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
844 struct omap_mcbsp *mcbsp;
846 void __iomem *io_base;
847 omap_mcbsp_word_length tx_word_length;
848 omap_mcbsp_word_length rx_word_length;
849 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
851 if (!omap_mcbsp_check_valid_id(id)) {
852 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
856 mcbsp = id_to_mcbsp_ptr(id);
857 io_base = mcbsp->io_base;
859 tx_word_length = mcbsp->tx_word_length;
860 rx_word_length = mcbsp->rx_word_length;
862 if (tx_word_length != rx_word_length)
865 /* First we wait for the transmitter to be ready */
866 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
867 while (!(spcr2 & XRDY)) {
868 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
869 if (attempts++ > 1000) {
870 /* We must reset the transmitter */
871 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
873 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
875 dev_err(mcbsp->dev, "McBSP%d transmitter not "
876 "ready\n", mcbsp->id);
881 /* We first need to enable the bus clock */
882 if (tx_word_length > OMAP_MCBSP_WORD_16)
883 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
884 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
886 /* We wait for the receiver to be ready */
887 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
888 while (!(spcr1 & RRDY)) {
889 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
890 if (attempts++ > 1000) {
891 /* We must reset the receiver */
892 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
894 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
896 dev_err(mcbsp->dev, "McBSP%d receiver not "
897 "ready\n", mcbsp->id);
902 /* Receiver is ready, there is something for us */
903 if (rx_word_length > OMAP_MCBSP_WORD_16)
904 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
905 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
907 word[0] = (word_lsb | (word_msb << 16));
911 EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
914 * Simple DMA based buffer rx/tx routines.
915 * Nothing fancy, just a single buffer tx/rx through DMA.
916 * The DMA resources are released once the transfer is done.
917 * For anything fancier, you should use your own customized DMA
918 * routines and callbacks.
920 int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
923 struct omap_mcbsp *mcbsp;
929 if (!omap_mcbsp_check_valid_id(id)) {
930 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
933 mcbsp = id_to_mcbsp_ptr(id);
935 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
936 omap_mcbsp_tx_dma_callback,
939 dev_err(mcbsp->dev, " Unable to request DMA channel for "
940 "McBSP%d TX. Trying IRQ based TX\n",
944 mcbsp->dma_tx_lch = dma_tx_ch;
946 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
949 init_completion(&mcbsp->tx_dma_completion);
951 if (cpu_class_is_omap1()) {
952 src_port = OMAP_DMA_PORT_TIPB;
953 dest_port = OMAP_DMA_PORT_EMIFF;
955 if (cpu_class_is_omap2())
956 sync_dev = mcbsp->dma_tx_sync;
958 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
959 OMAP_DMA_DATA_TYPE_S16,
961 OMAP_DMA_SYNC_ELEMENT,
964 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
966 OMAP_DMA_AMODE_CONSTANT,
967 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
970 omap_set_dma_src_params(mcbsp->dma_tx_lch,
972 OMAP_DMA_AMODE_POST_INC,
976 omap_start_dma(mcbsp->dma_tx_lch);
977 wait_for_completion(&mcbsp->tx_dma_completion);
981 EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
983 int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
986 struct omap_mcbsp *mcbsp;
992 if (!omap_mcbsp_check_valid_id(id)) {
993 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
996 mcbsp = id_to_mcbsp_ptr(id);
998 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
999 omap_mcbsp_rx_dma_callback,
1002 dev_err(mcbsp->dev, "Unable to request DMA channel for "
1003 "McBSP%d RX. Trying IRQ based RX\n",
1007 mcbsp->dma_rx_lch = dma_rx_ch;
1009 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
1012 init_completion(&mcbsp->rx_dma_completion);
1014 if (cpu_class_is_omap1()) {
1015 src_port = OMAP_DMA_PORT_TIPB;
1016 dest_port = OMAP_DMA_PORT_EMIFF;
1018 if (cpu_class_is_omap2())
1019 sync_dev = mcbsp->dma_rx_sync;
1021 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
1022 OMAP_DMA_DATA_TYPE_S16,
1024 OMAP_DMA_SYNC_ELEMENT,
1027 omap_set_dma_src_params(mcbsp->dma_rx_lch,
1029 OMAP_DMA_AMODE_CONSTANT,
1030 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
1033 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
1035 OMAP_DMA_AMODE_POST_INC,
1039 omap_start_dma(mcbsp->dma_rx_lch);
1040 wait_for_completion(&mcbsp->rx_dma_completion);
1044 EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
1048 * Since SPI setup is much simpler than the generic McBSP one,
1049 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1050 * Once this is done, you can call omap_mcbsp_start().
1052 void omap_mcbsp_set_spi_mode(unsigned int id,
1053 const struct omap_mcbsp_spi_cfg *spi_cfg)
1055 struct omap_mcbsp *mcbsp;
1056 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1058 if (!omap_mcbsp_check_valid_id(id)) {
1059 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1062 mcbsp = id_to_mcbsp_ptr(id);
1064 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1066 /* SPI has only one frame */
1067 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1068 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1070 /* Clock stop mode */
1071 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1072 mcbsp_cfg.spcr1 |= (1 << 12);
1074 mcbsp_cfg.spcr1 |= (3 << 11);
1076 /* Set clock parities */
1077 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1078 mcbsp_cfg.pcr0 |= CLKRP;
1080 mcbsp_cfg.pcr0 &= ~CLKRP;
1082 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1083 mcbsp_cfg.pcr0 &= ~CLKXP;
1085 mcbsp_cfg.pcr0 |= CLKXP;
1087 /* Set SCLKME to 0 and CLKSM to 1 */
1088 mcbsp_cfg.pcr0 &= ~SCLKME;
1089 mcbsp_cfg.srgr2 |= CLKSM;
1092 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1093 mcbsp_cfg.pcr0 &= ~FSXP;
1095 mcbsp_cfg.pcr0 |= FSXP;
1097 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1098 mcbsp_cfg.pcr0 |= CLKXM;
1099 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
1100 mcbsp_cfg.pcr0 |= FSXM;
1101 mcbsp_cfg.srgr2 &= ~FSGM;
1102 mcbsp_cfg.xcr2 |= XDATDLY(1);
1103 mcbsp_cfg.rcr2 |= RDATDLY(1);
1105 mcbsp_cfg.pcr0 &= ~CLKXM;
1106 mcbsp_cfg.srgr1 |= CLKGDV(1);
1107 mcbsp_cfg.pcr0 &= ~FSXM;
1108 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1109 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1112 mcbsp_cfg.xcr2 &= ~XPHASE;
1113 mcbsp_cfg.rcr2 &= ~RPHASE;
1115 omap_mcbsp_config(id, &mcbsp_cfg);
1117 EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
1119 #ifdef CONFIG_ARCH_OMAP34XX
1120 #define max_thres(m) (mcbsp->pdata->buffer_size)
1121 #define valid_threshold(m, val) ((val) <= max_thres(m))
1122 #define THRESHOLD_PROP_BUILDER(prop) \
1123 static ssize_t prop##_show(struct device *dev, \
1124 struct device_attribute *attr, char *buf) \
1126 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1128 return sprintf(buf, "%u\n", mcbsp->prop); \
1131 static ssize_t prop##_store(struct device *dev, \
1132 struct device_attribute *attr, \
1133 const char *buf, size_t size) \
1135 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1136 unsigned long val; \
1139 status = strict_strtoul(buf, 0, &val); \
1143 if (!valid_threshold(mcbsp, val)) \
1146 mcbsp->prop = val; \
1150 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1152 THRESHOLD_PROP_BUILDER(max_tx_thres);
1153 THRESHOLD_PROP_BUILDER(max_rx_thres);
1155 static ssize_t dma_op_mode_show(struct device *dev,
1156 struct device_attribute *attr, char *buf)
1158 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1161 spin_lock_irq(&mcbsp->lock);
1162 dma_op_mode = mcbsp->dma_op_mode;
1163 spin_unlock_irq(&mcbsp->lock);
1165 return sprintf(buf, "current mode: %d\n"
1166 "possible mode values are:\n"
1171 MCBSP_DMA_MODE_ELEMENT, "element mode",
1172 MCBSP_DMA_MODE_THRESHOLD, "threshold mode",
1173 MCBSP_DMA_MODE_FRAME, "frame mode");
1176 static ssize_t dma_op_mode_store(struct device *dev,
1177 struct device_attribute *attr,
1178 const char *buf, size_t size)
1180 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1184 status = strict_strtoul(buf, 0, &val);
1188 spin_lock_irq(&mcbsp->lock);
1195 if (val > MCBSP_DMA_MODE_FRAME || val < MCBSP_DMA_MODE_ELEMENT) {
1200 mcbsp->dma_op_mode = val;
1203 spin_unlock_irq(&mcbsp->lock);
1208 static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1210 static const struct attribute *additional_attrs[] = {
1211 &dev_attr_max_tx_thres.attr,
1212 &dev_attr_max_rx_thres.attr,
1213 &dev_attr_dma_op_mode.attr,
1217 static const struct attribute_group additional_attr_group = {
1218 .attrs = (struct attribute **)additional_attrs,
1221 static inline int __devinit omap_additional_add(struct device *dev)
1223 return sysfs_create_group(&dev->kobj, &additional_attr_group);
1226 static inline void __devexit omap_additional_remove(struct device *dev)
1228 sysfs_remove_group(&dev->kobj, &additional_attr_group);
1231 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1233 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1234 if (cpu_is_omap34xx()) {
1235 mcbsp->max_tx_thres = max_thres(mcbsp);
1236 mcbsp->max_rx_thres = max_thres(mcbsp);
1238 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1239 * for mcbsp2 instances.
1241 if (omap_additional_add(mcbsp->dev))
1242 dev_warn(mcbsp->dev,
1243 "Unable to create additional controls\n");
1245 mcbsp->max_tx_thres = -EINVAL;
1246 mcbsp->max_rx_thres = -EINVAL;
1250 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1252 if (cpu_is_omap34xx())
1253 omap_additional_remove(mcbsp->dev);
1256 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1257 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1258 #endif /* CONFIG_ARCH_OMAP34XX */
1261 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1262 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1264 static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
1266 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
1267 struct omap_mcbsp *mcbsp;
1268 int id = pdev->id - 1;
1272 dev_err(&pdev->dev, "McBSP device initialized without"
1278 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
1280 if (id >= omap_mcbsp_count) {
1281 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1286 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1292 spin_lock_init(&mcbsp->lock);
1295 mcbsp->dma_tx_lch = -1;
1296 mcbsp->dma_rx_lch = -1;
1298 mcbsp->phys_base = pdata->phys_base;
1299 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1300 if (!mcbsp->io_base) {
1305 /* Default I/O is IRQ based */
1306 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1307 mcbsp->tx_irq = pdata->tx_irq;
1308 mcbsp->rx_irq = pdata->rx_irq;
1309 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1310 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
1312 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1313 if (IS_ERR(mcbsp->iclk)) {
1314 ret = PTR_ERR(mcbsp->iclk);
1315 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1319 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1320 if (IS_ERR(mcbsp->fclk)) {
1321 ret = PTR_ERR(mcbsp->fclk);
1322 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1326 mcbsp->pdata = pdata;
1327 mcbsp->dev = &pdev->dev;
1328 mcbsp_ptr[id] = mcbsp;
1329 platform_set_drvdata(pdev, mcbsp);
1331 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1332 omap34xx_device_init(mcbsp);
1337 clk_put(mcbsp->iclk);
1339 iounmap(mcbsp->io_base);
1346 static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
1348 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1350 platform_set_drvdata(pdev, NULL);
1353 if (mcbsp->pdata && mcbsp->pdata->ops &&
1354 mcbsp->pdata->ops->free)
1355 mcbsp->pdata->ops->free(mcbsp->id);
1357 omap34xx_device_exit(mcbsp);
1359 clk_disable(mcbsp->fclk);
1360 clk_disable(mcbsp->iclk);
1361 clk_put(mcbsp->fclk);
1362 clk_put(mcbsp->iclk);
1364 iounmap(mcbsp->io_base);
1375 static struct platform_driver omap_mcbsp_driver = {
1376 .probe = omap_mcbsp_probe,
1377 .remove = __devexit_p(omap_mcbsp_remove),
1379 .name = "omap-mcbsp",
1383 int __init omap_mcbsp_init(void)
1385 /* Register the McBSP driver */
1386 return platform_driver_register(&omap_mcbsp_driver);