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 |= SIDLEMODE(0x02);
325 syscon |= SIDLEMODE(0x01);
326 spin_unlock_irq(&mcbsp->lock);
328 syscon |= (ENAWAKEUP | CLOCKACTIVITY(0x02));
329 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
331 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, XRDYEN | RRDYEN);
335 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
338 * Disable wakup behavior, smart idle and all wakeups
340 if (cpu_is_omap34xx()) {
343 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
344 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
345 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
347 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, 0);
351 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
352 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
356 * We can choose between IRQ based or polled IO.
357 * This needs to be called before omap_mcbsp_request().
359 int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
361 struct omap_mcbsp *mcbsp;
363 if (!omap_mcbsp_check_valid_id(id)) {
364 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
367 mcbsp = id_to_mcbsp_ptr(id);
369 spin_lock(&mcbsp->lock);
372 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
374 spin_unlock(&mcbsp->lock);
378 mcbsp->io_type = io_type;
380 spin_unlock(&mcbsp->lock);
384 EXPORT_SYMBOL(omap_mcbsp_set_io_type);
386 int omap_mcbsp_request(unsigned int id)
388 struct omap_mcbsp *mcbsp;
391 if (!omap_mcbsp_check_valid_id(id)) {
392 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
395 mcbsp = id_to_mcbsp_ptr(id);
397 spin_lock(&mcbsp->lock);
399 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
401 spin_unlock(&mcbsp->lock);
406 spin_unlock(&mcbsp->lock);
408 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
409 mcbsp->pdata->ops->request(id);
411 clk_enable(mcbsp->iclk);
412 clk_enable(mcbsp->fclk);
414 /* Do procedure specific to omap34xx arch, if applicable */
415 omap34xx_mcbsp_request(mcbsp);
418 * Make sure that transmitter, receiver and sample-rate generator are
419 * not running before activating IRQs.
421 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
422 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
424 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
425 /* We need to get IRQs here */
426 init_completion(&mcbsp->tx_irq_completion);
427 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
428 0, "McBSP", (void *)mcbsp);
430 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
431 "for McBSP%d\n", mcbsp->tx_irq,
436 init_completion(&mcbsp->rx_irq_completion);
437 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
438 0, "McBSP", (void *)mcbsp);
440 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
441 "for McBSP%d\n", mcbsp->rx_irq,
443 free_irq(mcbsp->tx_irq, (void *)mcbsp);
450 EXPORT_SYMBOL(omap_mcbsp_request);
452 void omap_mcbsp_free(unsigned int id)
454 struct omap_mcbsp *mcbsp;
456 if (!omap_mcbsp_check_valid_id(id)) {
457 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
460 mcbsp = id_to_mcbsp_ptr(id);
462 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
463 mcbsp->pdata->ops->free(id);
465 /* Do procedure specific to omap34xx arch, if applicable */
466 omap34xx_mcbsp_free(mcbsp);
468 clk_disable(mcbsp->fclk);
469 clk_disable(mcbsp->iclk);
471 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
473 free_irq(mcbsp->rx_irq, (void *)mcbsp);
474 free_irq(mcbsp->tx_irq, (void *)mcbsp);
477 spin_lock(&mcbsp->lock);
479 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
481 spin_unlock(&mcbsp->lock);
486 spin_unlock(&mcbsp->lock);
488 EXPORT_SYMBOL(omap_mcbsp_free);
491 * Here we start the McBSP, by enabling transmitter, receiver or both.
492 * If no transmitter or receiver is active prior calling, then sample-rate
493 * generator and frame sync are started.
495 void omap_mcbsp_start(unsigned int id, int tx, int rx)
497 struct omap_mcbsp *mcbsp;
498 void __iomem *io_base;
502 if (!omap_mcbsp_check_valid_id(id)) {
503 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
506 mcbsp = id_to_mcbsp_ptr(id);
507 io_base = mcbsp->io_base;
509 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
510 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
512 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
513 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
516 /* Start the sample generator */
517 w = OMAP_MCBSP_READ(io_base, SPCR2);
518 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
521 /* Enable transmitter and receiver */
522 w = OMAP_MCBSP_READ(io_base, SPCR2);
523 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
525 w = OMAP_MCBSP_READ(io_base, SPCR1);
526 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
529 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
530 * REVISIT: 100us may give enough time for two CLKSRG, however
531 * due to some unknown PM related, clock gating etc. reason it
537 /* Start frame sync */
538 w = OMAP_MCBSP_READ(io_base, SPCR2);
539 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
542 /* Dump McBSP Regs */
543 omap_mcbsp_dump_reg(id);
545 EXPORT_SYMBOL(omap_mcbsp_start);
547 void omap_mcbsp_stop(unsigned int id, int tx, int rx)
549 struct omap_mcbsp *mcbsp;
550 void __iomem *io_base;
554 if (!omap_mcbsp_check_valid_id(id)) {
555 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
559 mcbsp = id_to_mcbsp_ptr(id);
560 io_base = mcbsp->io_base;
562 /* Reset transmitter */
563 w = OMAP_MCBSP_READ(io_base, SPCR2);
564 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
567 w = OMAP_MCBSP_READ(io_base, SPCR1);
568 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
570 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
571 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
574 /* Reset the sample rate generator */
575 w = OMAP_MCBSP_READ(io_base, SPCR2);
576 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
579 EXPORT_SYMBOL(omap_mcbsp_stop);
581 void omap_mcbsp_xmit_enable(unsigned int id, u8 enable)
583 struct omap_mcbsp *mcbsp;
584 void __iomem *io_base;
587 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
590 if (!omap_mcbsp_check_valid_id(id)) {
591 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
595 mcbsp = id_to_mcbsp_ptr(id);
596 io_base = mcbsp->io_base;
598 w = OMAP_MCBSP_READ(io_base, XCCR);
601 OMAP_MCBSP_WRITE(io_base, XCCR, w & ~(XDISABLE));
603 OMAP_MCBSP_WRITE(io_base, XCCR, w | XDISABLE);
605 EXPORT_SYMBOL(omap_mcbsp_xmit_enable);
607 void omap_mcbsp_recv_enable(unsigned int id, u8 enable)
609 struct omap_mcbsp *mcbsp;
610 void __iomem *io_base;
613 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
616 if (!omap_mcbsp_check_valid_id(id)) {
617 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
621 mcbsp = id_to_mcbsp_ptr(id);
622 io_base = mcbsp->io_base;
624 w = OMAP_MCBSP_READ(io_base, RCCR);
627 OMAP_MCBSP_WRITE(io_base, RCCR, w & ~(RDISABLE));
629 OMAP_MCBSP_WRITE(io_base, RCCR, w | RDISABLE);
631 EXPORT_SYMBOL(omap_mcbsp_recv_enable);
633 /* polled mcbsp i/o operations */
634 int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
636 struct omap_mcbsp *mcbsp;
639 if (!omap_mcbsp_check_valid_id(id)) {
640 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
644 mcbsp = id_to_mcbsp_ptr(id);
645 base = mcbsp->io_base;
647 writew(buf, base + OMAP_MCBSP_REG_DXR1);
648 /* if frame sync error - clear the error */
649 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
651 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
652 base + OMAP_MCBSP_REG_SPCR2);
656 /* wait for transmit confirmation */
658 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
659 if (attemps++ > 1000) {
660 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
662 base + OMAP_MCBSP_REG_SPCR2);
664 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
666 base + OMAP_MCBSP_REG_SPCR2);
668 dev_err(mcbsp->dev, "Could not write to"
669 " McBSP%d Register\n", mcbsp->id);
677 EXPORT_SYMBOL(omap_mcbsp_pollwrite);
679 int omap_mcbsp_pollread(unsigned int id, u16 *buf)
681 struct omap_mcbsp *mcbsp;
684 if (!omap_mcbsp_check_valid_id(id)) {
685 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
688 mcbsp = id_to_mcbsp_ptr(id);
690 base = mcbsp->io_base;
691 /* if frame sync error - clear the error */
692 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
694 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
695 base + OMAP_MCBSP_REG_SPCR1);
699 /* wait for recieve confirmation */
701 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
702 if (attemps++ > 1000) {
703 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
705 base + OMAP_MCBSP_REG_SPCR1);
707 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
709 base + OMAP_MCBSP_REG_SPCR1);
711 dev_err(mcbsp->dev, "Could not read from"
712 " McBSP%d Register\n", mcbsp->id);
717 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
721 EXPORT_SYMBOL(omap_mcbsp_pollread);
724 * IRQ based word transmission.
726 void omap_mcbsp_xmit_word(unsigned int id, u32 word)
728 struct omap_mcbsp *mcbsp;
729 void __iomem *io_base;
730 omap_mcbsp_word_length word_length;
732 if (!omap_mcbsp_check_valid_id(id)) {
733 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
737 mcbsp = id_to_mcbsp_ptr(id);
738 io_base = mcbsp->io_base;
739 word_length = mcbsp->tx_word_length;
741 wait_for_completion(&mcbsp->tx_irq_completion);
743 if (word_length > OMAP_MCBSP_WORD_16)
744 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
745 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
747 EXPORT_SYMBOL(omap_mcbsp_xmit_word);
749 u32 omap_mcbsp_recv_word(unsigned int id)
751 struct omap_mcbsp *mcbsp;
752 void __iomem *io_base;
753 u16 word_lsb, word_msb = 0;
754 omap_mcbsp_word_length word_length;
756 if (!omap_mcbsp_check_valid_id(id)) {
757 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
760 mcbsp = id_to_mcbsp_ptr(id);
762 word_length = mcbsp->rx_word_length;
763 io_base = mcbsp->io_base;
765 wait_for_completion(&mcbsp->rx_irq_completion);
767 if (word_length > OMAP_MCBSP_WORD_16)
768 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
769 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
771 return (word_lsb | (word_msb << 16));
773 EXPORT_SYMBOL(omap_mcbsp_recv_word);
775 int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
777 struct omap_mcbsp *mcbsp;
778 void __iomem *io_base;
779 omap_mcbsp_word_length tx_word_length;
780 omap_mcbsp_word_length rx_word_length;
781 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
783 if (!omap_mcbsp_check_valid_id(id)) {
784 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
787 mcbsp = id_to_mcbsp_ptr(id);
788 io_base = mcbsp->io_base;
789 tx_word_length = mcbsp->tx_word_length;
790 rx_word_length = mcbsp->rx_word_length;
792 if (tx_word_length != rx_word_length)
795 /* First we wait for the transmitter to be ready */
796 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
797 while (!(spcr2 & XRDY)) {
798 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
799 if (attempts++ > 1000) {
800 /* We must reset the transmitter */
801 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
803 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
805 dev_err(mcbsp->dev, "McBSP%d transmitter not "
806 "ready\n", mcbsp->id);
811 /* Now we can push the data */
812 if (tx_word_length > OMAP_MCBSP_WORD_16)
813 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
814 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
816 /* We wait for the receiver to be ready */
817 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
818 while (!(spcr1 & RRDY)) {
819 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
820 if (attempts++ > 1000) {
821 /* We must reset the receiver */
822 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
824 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
826 dev_err(mcbsp->dev, "McBSP%d receiver not "
827 "ready\n", mcbsp->id);
832 /* Receiver is ready, let's read the dummy data */
833 if (rx_word_length > OMAP_MCBSP_WORD_16)
834 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
835 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
839 EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
841 int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
843 struct omap_mcbsp *mcbsp;
845 void __iomem *io_base;
846 omap_mcbsp_word_length tx_word_length;
847 omap_mcbsp_word_length rx_word_length;
848 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
850 if (!omap_mcbsp_check_valid_id(id)) {
851 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
855 mcbsp = id_to_mcbsp_ptr(id);
856 io_base = mcbsp->io_base;
858 tx_word_length = mcbsp->tx_word_length;
859 rx_word_length = mcbsp->rx_word_length;
861 if (tx_word_length != rx_word_length)
864 /* First we wait for the transmitter to be ready */
865 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
866 while (!(spcr2 & XRDY)) {
867 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
868 if (attempts++ > 1000) {
869 /* We must reset the transmitter */
870 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
872 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
874 dev_err(mcbsp->dev, "McBSP%d transmitter not "
875 "ready\n", mcbsp->id);
880 /* We first need to enable the bus clock */
881 if (tx_word_length > OMAP_MCBSP_WORD_16)
882 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
883 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
885 /* We wait for the receiver to be ready */
886 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
887 while (!(spcr1 & RRDY)) {
888 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
889 if (attempts++ > 1000) {
890 /* We must reset the receiver */
891 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
893 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
895 dev_err(mcbsp->dev, "McBSP%d receiver not "
896 "ready\n", mcbsp->id);
901 /* Receiver is ready, there is something for us */
902 if (rx_word_length > OMAP_MCBSP_WORD_16)
903 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
904 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
906 word[0] = (word_lsb | (word_msb << 16));
910 EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
913 * Simple DMA based buffer rx/tx routines.
914 * Nothing fancy, just a single buffer tx/rx through DMA.
915 * The DMA resources are released once the transfer is done.
916 * For anything fancier, you should use your own customized DMA
917 * routines and callbacks.
919 int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
922 struct omap_mcbsp *mcbsp;
928 if (!omap_mcbsp_check_valid_id(id)) {
929 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
932 mcbsp = id_to_mcbsp_ptr(id);
934 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
935 omap_mcbsp_tx_dma_callback,
938 dev_err(mcbsp->dev, " Unable to request DMA channel for "
939 "McBSP%d TX. Trying IRQ based TX\n",
943 mcbsp->dma_tx_lch = dma_tx_ch;
945 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
948 init_completion(&mcbsp->tx_dma_completion);
950 if (cpu_class_is_omap1()) {
951 src_port = OMAP_DMA_PORT_TIPB;
952 dest_port = OMAP_DMA_PORT_EMIFF;
954 if (cpu_class_is_omap2())
955 sync_dev = mcbsp->dma_tx_sync;
957 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
958 OMAP_DMA_DATA_TYPE_S16,
960 OMAP_DMA_SYNC_ELEMENT,
963 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
965 OMAP_DMA_AMODE_CONSTANT,
966 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
969 omap_set_dma_src_params(mcbsp->dma_tx_lch,
971 OMAP_DMA_AMODE_POST_INC,
975 omap_start_dma(mcbsp->dma_tx_lch);
976 wait_for_completion(&mcbsp->tx_dma_completion);
980 EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
982 int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
985 struct omap_mcbsp *mcbsp;
991 if (!omap_mcbsp_check_valid_id(id)) {
992 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
995 mcbsp = id_to_mcbsp_ptr(id);
997 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
998 omap_mcbsp_rx_dma_callback,
1001 dev_err(mcbsp->dev, "Unable to request DMA channel for "
1002 "McBSP%d RX. Trying IRQ based RX\n",
1006 mcbsp->dma_rx_lch = dma_rx_ch;
1008 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
1011 init_completion(&mcbsp->rx_dma_completion);
1013 if (cpu_class_is_omap1()) {
1014 src_port = OMAP_DMA_PORT_TIPB;
1015 dest_port = OMAP_DMA_PORT_EMIFF;
1017 if (cpu_class_is_omap2())
1018 sync_dev = mcbsp->dma_rx_sync;
1020 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
1021 OMAP_DMA_DATA_TYPE_S16,
1023 OMAP_DMA_SYNC_ELEMENT,
1026 omap_set_dma_src_params(mcbsp->dma_rx_lch,
1028 OMAP_DMA_AMODE_CONSTANT,
1029 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
1032 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
1034 OMAP_DMA_AMODE_POST_INC,
1038 omap_start_dma(mcbsp->dma_rx_lch);
1039 wait_for_completion(&mcbsp->rx_dma_completion);
1043 EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
1047 * Since SPI setup is much simpler than the generic McBSP one,
1048 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1049 * Once this is done, you can call omap_mcbsp_start().
1051 void omap_mcbsp_set_spi_mode(unsigned int id,
1052 const struct omap_mcbsp_spi_cfg *spi_cfg)
1054 struct omap_mcbsp *mcbsp;
1055 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1057 if (!omap_mcbsp_check_valid_id(id)) {
1058 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1061 mcbsp = id_to_mcbsp_ptr(id);
1063 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1065 /* SPI has only one frame */
1066 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1067 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1069 /* Clock stop mode */
1070 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1071 mcbsp_cfg.spcr1 |= (1 << 12);
1073 mcbsp_cfg.spcr1 |= (3 << 11);
1075 /* Set clock parities */
1076 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1077 mcbsp_cfg.pcr0 |= CLKRP;
1079 mcbsp_cfg.pcr0 &= ~CLKRP;
1081 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1082 mcbsp_cfg.pcr0 &= ~CLKXP;
1084 mcbsp_cfg.pcr0 |= CLKXP;
1086 /* Set SCLKME to 0 and CLKSM to 1 */
1087 mcbsp_cfg.pcr0 &= ~SCLKME;
1088 mcbsp_cfg.srgr2 |= CLKSM;
1091 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1092 mcbsp_cfg.pcr0 &= ~FSXP;
1094 mcbsp_cfg.pcr0 |= FSXP;
1096 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1097 mcbsp_cfg.pcr0 |= CLKXM;
1098 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
1099 mcbsp_cfg.pcr0 |= FSXM;
1100 mcbsp_cfg.srgr2 &= ~FSGM;
1101 mcbsp_cfg.xcr2 |= XDATDLY(1);
1102 mcbsp_cfg.rcr2 |= RDATDLY(1);
1104 mcbsp_cfg.pcr0 &= ~CLKXM;
1105 mcbsp_cfg.srgr1 |= CLKGDV(1);
1106 mcbsp_cfg.pcr0 &= ~FSXM;
1107 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1108 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1111 mcbsp_cfg.xcr2 &= ~XPHASE;
1112 mcbsp_cfg.rcr2 &= ~RPHASE;
1114 omap_mcbsp_config(id, &mcbsp_cfg);
1116 EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
1118 #ifdef CONFIG_ARCH_OMAP34XX
1119 #define max_thres(m) (mcbsp->pdata->buffer_size)
1120 #define valid_threshold(m, val) ((val) <= max_thres(m))
1121 #define THRESHOLD_PROP_BUILDER(prop) \
1122 static ssize_t prop##_show(struct device *dev, \
1123 struct device_attribute *attr, char *buf) \
1125 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1127 return sprintf(buf, "%u\n", mcbsp->prop); \
1130 static ssize_t prop##_store(struct device *dev, \
1131 struct device_attribute *attr, \
1132 const char *buf, size_t size) \
1134 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1135 unsigned long val; \
1138 status = strict_strtoul(buf, 0, &val); \
1142 if (!valid_threshold(mcbsp, val)) \
1145 mcbsp->prop = val; \
1149 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1151 THRESHOLD_PROP_BUILDER(max_tx_thres);
1152 THRESHOLD_PROP_BUILDER(max_rx_thres);
1154 static ssize_t dma_op_mode_show(struct device *dev,
1155 struct device_attribute *attr, char *buf)
1157 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1160 spin_lock_irq(&mcbsp->lock);
1161 dma_op_mode = mcbsp->dma_op_mode;
1162 spin_unlock_irq(&mcbsp->lock);
1164 return sprintf(buf, "current mode: %d\n"
1165 "possible mode values are:\n"
1170 MCBSP_DMA_MODE_ELEMENT, "element mode",
1171 MCBSP_DMA_MODE_THRESHOLD, "threshold mode",
1172 MCBSP_DMA_MODE_FRAME, "frame mode");
1175 static ssize_t dma_op_mode_store(struct device *dev,
1176 struct device_attribute *attr,
1177 const char *buf, size_t size)
1179 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1183 status = strict_strtoul(buf, 0, &val);
1187 spin_lock_irq(&mcbsp->lock);
1194 if (val > MCBSP_DMA_MODE_FRAME || val < MCBSP_DMA_MODE_ELEMENT) {
1199 mcbsp->dma_op_mode = val;
1202 spin_unlock_irq(&mcbsp->lock);
1207 static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1209 static const struct attribute *additional_attrs[] = {
1210 &dev_attr_max_tx_thres.attr,
1211 &dev_attr_max_rx_thres.attr,
1212 &dev_attr_dma_op_mode.attr,
1216 static const struct attribute_group additional_attr_group = {
1217 .attrs = (struct attribute **)additional_attrs,
1220 static inline int __devinit omap_additional_add(struct device *dev)
1222 return sysfs_create_group(&dev->kobj, &additional_attr_group);
1225 static inline void __devexit omap_additional_remove(struct device *dev)
1227 sysfs_remove_group(&dev->kobj, &additional_attr_group);
1230 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1232 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1233 if (cpu_is_omap34xx()) {
1234 mcbsp->max_tx_thres = max_thres(mcbsp);
1235 mcbsp->max_rx_thres = max_thres(mcbsp);
1237 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1238 * for mcbsp2 instances.
1240 if (omap_additional_add(mcbsp->dev))
1241 dev_warn(mcbsp->dev,
1242 "Unable to create additional controls\n");
1244 mcbsp->max_tx_thres = -EINVAL;
1245 mcbsp->max_rx_thres = -EINVAL;
1249 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1251 if (cpu_is_omap34xx())
1252 omap_additional_remove(mcbsp->dev);
1255 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1256 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1257 #endif /* CONFIG_ARCH_OMAP34XX */
1260 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1261 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1263 static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
1265 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
1266 struct omap_mcbsp *mcbsp;
1267 int id = pdev->id - 1;
1271 dev_err(&pdev->dev, "McBSP device initialized without"
1277 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
1279 if (id >= omap_mcbsp_count) {
1280 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1285 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1291 spin_lock_init(&mcbsp->lock);
1294 mcbsp->dma_tx_lch = -1;
1295 mcbsp->dma_rx_lch = -1;
1297 mcbsp->phys_base = pdata->phys_base;
1298 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1299 if (!mcbsp->io_base) {
1304 /* Default I/O is IRQ based */
1305 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1306 mcbsp->tx_irq = pdata->tx_irq;
1307 mcbsp->rx_irq = pdata->rx_irq;
1308 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1309 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
1311 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1312 if (IS_ERR(mcbsp->iclk)) {
1313 ret = PTR_ERR(mcbsp->iclk);
1314 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1318 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1319 if (IS_ERR(mcbsp->fclk)) {
1320 ret = PTR_ERR(mcbsp->fclk);
1321 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1325 mcbsp->pdata = pdata;
1326 mcbsp->dev = &pdev->dev;
1327 mcbsp_ptr[id] = mcbsp;
1328 platform_set_drvdata(pdev, mcbsp);
1330 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1331 omap34xx_device_init(mcbsp);
1336 clk_put(mcbsp->iclk);
1338 iounmap(mcbsp->io_base);
1345 static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
1347 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1349 platform_set_drvdata(pdev, NULL);
1352 if (mcbsp->pdata && mcbsp->pdata->ops &&
1353 mcbsp->pdata->ops->free)
1354 mcbsp->pdata->ops->free(mcbsp->id);
1356 omap34xx_device_exit(mcbsp);
1358 clk_disable(mcbsp->fclk);
1359 clk_disable(mcbsp->iclk);
1360 clk_put(mcbsp->fclk);
1361 clk_put(mcbsp->iclk);
1363 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);