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>
26 #include <linux/slab.h>
29 #include <plat/mcbsp.h>
30 #include <plat/omap_device.h>
31 #include <linux/pm_runtime.h>
33 /* XXX These "sideways" includes are a sign that something is wrong */
34 #include "../mach-omap2/cm2xxx_3xxx.h"
35 #include "../mach-omap2/cm-regbits-34xx.h"
37 struct omap_mcbsp **mcbsp_ptr;
38 int omap_mcbsp_count, omap_mcbsp_cache_size;
40 static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
42 if (cpu_class_is_omap1()) {
43 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
44 __raw_writew((u16)val, mcbsp->io_base + reg);
45 } else if (cpu_is_omap2420()) {
46 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
47 __raw_writew((u16)val, mcbsp->io_base + reg);
49 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
50 __raw_writel(val, mcbsp->io_base + reg);
54 static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
56 if (cpu_class_is_omap1()) {
57 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
58 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
59 } else if (cpu_is_omap2420()) {
60 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
61 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
63 return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
64 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
68 #ifdef CONFIG_ARCH_OMAP3
69 static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
71 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
74 static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
76 return __raw_readl(mcbsp->st_data->io_base_st + reg);
80 #define MCBSP_READ(mcbsp, reg) \
81 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
82 #define MCBSP_WRITE(mcbsp, reg, val) \
83 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
84 #define MCBSP_READ_CACHE(mcbsp, reg) \
85 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
87 #define MCBSP_ST_READ(mcbsp, reg) \
88 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
89 #define MCBSP_ST_WRITE(mcbsp, reg, val) \
90 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
92 static void omap_mcbsp_dump_reg(u8 id)
94 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
96 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
97 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
98 MCBSP_READ(mcbsp, DRR2));
99 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
100 MCBSP_READ(mcbsp, DRR1));
101 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
102 MCBSP_READ(mcbsp, DXR2));
103 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
104 MCBSP_READ(mcbsp, DXR1));
105 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
106 MCBSP_READ(mcbsp, SPCR2));
107 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
108 MCBSP_READ(mcbsp, SPCR1));
109 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
110 MCBSP_READ(mcbsp, RCR2));
111 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
112 MCBSP_READ(mcbsp, RCR1));
113 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
114 MCBSP_READ(mcbsp, XCR2));
115 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
116 MCBSP_READ(mcbsp, XCR1));
117 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
118 MCBSP_READ(mcbsp, SRGR2));
119 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
120 MCBSP_READ(mcbsp, SRGR1));
121 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
122 MCBSP_READ(mcbsp, PCR0));
123 dev_dbg(mcbsp->dev, "***********************\n");
126 static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
128 struct omap_mcbsp *mcbsp_tx = dev_id;
131 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
132 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
134 if (irqst_spcr2 & XSYNC_ERR) {
135 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
137 /* Writing zero to XSYNC_ERR clears the IRQ */
138 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
140 complete(&mcbsp_tx->tx_irq_completion);
146 static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
148 struct omap_mcbsp *mcbsp_rx = dev_id;
151 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
152 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
154 if (irqst_spcr1 & RSYNC_ERR) {
155 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
157 /* Writing zero to RSYNC_ERR clears the IRQ */
158 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
160 complete(&mcbsp_rx->rx_irq_completion);
166 static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
168 struct omap_mcbsp *mcbsp_dma_tx = data;
170 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
171 MCBSP_READ(mcbsp_dma_tx, SPCR2));
173 /* We can free the channels */
174 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
175 mcbsp_dma_tx->dma_tx_lch = -1;
177 complete(&mcbsp_dma_tx->tx_dma_completion);
180 static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
182 struct omap_mcbsp *mcbsp_dma_rx = data;
184 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
185 MCBSP_READ(mcbsp_dma_rx, SPCR2));
187 /* We can free the channels */
188 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
189 mcbsp_dma_rx->dma_rx_lch = -1;
191 complete(&mcbsp_dma_rx->rx_dma_completion);
195 * omap_mcbsp_config simply write a config to the
197 * You either call this function or set the McBSP registers
198 * by yourself before calling omap_mcbsp_start().
200 void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
202 struct omap_mcbsp *mcbsp;
204 if (!omap_mcbsp_check_valid_id(id)) {
205 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
208 mcbsp = id_to_mcbsp_ptr(id);
210 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
211 mcbsp->id, mcbsp->phys_base);
213 /* We write the given config */
214 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
215 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
216 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
217 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
218 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
219 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
220 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
221 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
222 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
223 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
224 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
225 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
226 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
227 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
230 EXPORT_SYMBOL(omap_mcbsp_config);
232 #ifdef CONFIG_ARCH_OMAP3
233 static struct omap_device *find_omap_device_by_dev(struct device *dev)
235 struct platform_device *pdev = container_of(dev,
236 struct platform_device, dev);
237 return container_of(pdev, struct omap_device, pdev);
240 static void omap_st_on(struct omap_mcbsp *mcbsp)
243 struct omap_device *od;
245 od = find_omap_device_by_dev(mcbsp->dev);
248 * Sidetone uses McBSP ICLK - which must not idle when sidetones
249 * are enabled or sidetones start sounding ugly.
251 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
252 w &= ~(1 << (mcbsp->id - 2));
253 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
255 /* Enable McBSP Sidetone */
256 w = MCBSP_READ(mcbsp, SSELCR);
257 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
259 /* Enable Sidetone from Sidetone Core */
260 w = MCBSP_ST_READ(mcbsp, SSELCR);
261 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
264 static void omap_st_off(struct omap_mcbsp *mcbsp)
267 struct omap_device *od;
269 od = find_omap_device_by_dev(mcbsp->dev);
271 w = MCBSP_ST_READ(mcbsp, SSELCR);
272 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
274 w = MCBSP_READ(mcbsp, SSELCR);
275 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
277 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
278 w |= 1 << (mcbsp->id - 2);
279 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
282 static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
285 struct omap_device *od;
287 od = find_omap_device_by_dev(mcbsp->dev);
289 val = MCBSP_ST_READ(mcbsp, SSELCR);
291 if (val & ST_COEFFWREN)
292 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
294 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
296 for (i = 0; i < 128; i++)
297 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
301 val = MCBSP_ST_READ(mcbsp, SSELCR);
302 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
303 val = MCBSP_ST_READ(mcbsp, SSELCR);
305 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
308 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
311 static void omap_st_chgain(struct omap_mcbsp *mcbsp)
314 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
315 struct omap_device *od;
317 od = find_omap_device_by_dev(mcbsp->dev);
319 w = MCBSP_ST_READ(mcbsp, SSELCR);
321 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
322 ST_CH1GAIN(st_data->ch1gain));
325 int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
327 struct omap_mcbsp *mcbsp;
328 struct omap_mcbsp_st_data *st_data;
331 if (!omap_mcbsp_check_valid_id(id)) {
332 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
336 mcbsp = id_to_mcbsp_ptr(id);
337 st_data = mcbsp->st_data;
342 spin_lock_irq(&mcbsp->lock);
344 st_data->ch0gain = chgain;
345 else if (channel == 1)
346 st_data->ch1gain = chgain;
350 if (st_data->enabled)
351 omap_st_chgain(mcbsp);
352 spin_unlock_irq(&mcbsp->lock);
356 EXPORT_SYMBOL(omap_st_set_chgain);
358 int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
360 struct omap_mcbsp *mcbsp;
361 struct omap_mcbsp_st_data *st_data;
364 if (!omap_mcbsp_check_valid_id(id)) {
365 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
369 mcbsp = id_to_mcbsp_ptr(id);
370 st_data = mcbsp->st_data;
375 spin_lock_irq(&mcbsp->lock);
377 *chgain = st_data->ch0gain;
378 else if (channel == 1)
379 *chgain = st_data->ch1gain;
382 spin_unlock_irq(&mcbsp->lock);
386 EXPORT_SYMBOL(omap_st_get_chgain);
388 static int omap_st_start(struct omap_mcbsp *mcbsp)
390 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
392 if (st_data && st_data->enabled && !st_data->running) {
393 omap_st_fir_write(mcbsp, st_data->taps);
394 omap_st_chgain(mcbsp);
398 st_data->running = 1;
405 int omap_st_enable(unsigned int id)
407 struct omap_mcbsp *mcbsp;
408 struct omap_mcbsp_st_data *st_data;
410 if (!omap_mcbsp_check_valid_id(id)) {
411 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
415 mcbsp = id_to_mcbsp_ptr(id);
416 st_data = mcbsp->st_data;
421 spin_lock_irq(&mcbsp->lock);
422 st_data->enabled = 1;
423 omap_st_start(mcbsp);
424 spin_unlock_irq(&mcbsp->lock);
428 EXPORT_SYMBOL(omap_st_enable);
430 static int omap_st_stop(struct omap_mcbsp *mcbsp)
432 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
434 if (st_data && st_data->running) {
437 st_data->running = 0;
444 int omap_st_disable(unsigned int id)
446 struct omap_mcbsp *mcbsp;
447 struct omap_mcbsp_st_data *st_data;
450 if (!omap_mcbsp_check_valid_id(id)) {
451 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
455 mcbsp = id_to_mcbsp_ptr(id);
456 st_data = mcbsp->st_data;
461 spin_lock_irq(&mcbsp->lock);
463 st_data->enabled = 0;
464 spin_unlock_irq(&mcbsp->lock);
468 EXPORT_SYMBOL(omap_st_disable);
470 int omap_st_is_enabled(unsigned int id)
472 struct omap_mcbsp *mcbsp;
473 struct omap_mcbsp_st_data *st_data;
475 if (!omap_mcbsp_check_valid_id(id)) {
476 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
480 mcbsp = id_to_mcbsp_ptr(id);
481 st_data = mcbsp->st_data;
487 return st_data->enabled;
489 EXPORT_SYMBOL(omap_st_is_enabled);
492 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
493 * The threshold parameter is 1 based, and it is converted (threshold - 1)
494 * for the THRSH2 register.
496 void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
498 struct omap_mcbsp *mcbsp;
500 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
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);
509 if (threshold && threshold <= mcbsp->max_tx_thres)
510 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
512 EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
515 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
516 * The threshold parameter is 1 based, and it is converted (threshold - 1)
517 * for the THRSH1 register.
519 void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
521 struct omap_mcbsp *mcbsp;
523 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
526 if (!omap_mcbsp_check_valid_id(id)) {
527 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
530 mcbsp = id_to_mcbsp_ptr(id);
532 if (threshold && threshold <= mcbsp->max_rx_thres)
533 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
535 EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
538 * omap_mcbsp_get_max_tx_thres just return the current configured
539 * maximum threshold for transmission
541 u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
543 struct omap_mcbsp *mcbsp;
545 if (!omap_mcbsp_check_valid_id(id)) {
546 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
549 mcbsp = id_to_mcbsp_ptr(id);
551 return mcbsp->max_tx_thres;
553 EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
556 * omap_mcbsp_get_max_rx_thres just return the current configured
557 * maximum threshold for reception
559 u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
561 struct omap_mcbsp *mcbsp;
563 if (!omap_mcbsp_check_valid_id(id)) {
564 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
567 mcbsp = id_to_mcbsp_ptr(id);
569 return mcbsp->max_rx_thres;
571 EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
573 u16 omap_mcbsp_get_fifo_size(unsigned int id)
575 struct omap_mcbsp *mcbsp;
577 if (!omap_mcbsp_check_valid_id(id)) {
578 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
581 mcbsp = id_to_mcbsp_ptr(id);
583 return mcbsp->pdata->buffer_size;
585 EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
588 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
590 u16 omap_mcbsp_get_tx_delay(unsigned int id)
592 struct omap_mcbsp *mcbsp;
595 if (!omap_mcbsp_check_valid_id(id)) {
596 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
599 mcbsp = id_to_mcbsp_ptr(id);
601 /* Returns the number of free locations in the buffer */
602 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
604 /* Number of slots are different in McBSP ports */
605 return mcbsp->pdata->buffer_size - buffstat;
607 EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
610 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
611 * to reach the threshold value (when the DMA will be triggered to read it)
613 u16 omap_mcbsp_get_rx_delay(unsigned int id)
615 struct omap_mcbsp *mcbsp;
616 u16 buffstat, threshold;
618 if (!omap_mcbsp_check_valid_id(id)) {
619 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
622 mcbsp = id_to_mcbsp_ptr(id);
624 /* Returns the number of used locations in the buffer */
625 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
627 threshold = MCBSP_READ(mcbsp, THRSH1);
629 /* Return the number of location till we reach the threshold limit */
630 if (threshold <= buffstat)
633 return threshold - buffstat;
635 EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
638 * omap_mcbsp_get_dma_op_mode just return the current configured
639 * operating mode for the mcbsp channel
641 int omap_mcbsp_get_dma_op_mode(unsigned int id)
643 struct omap_mcbsp *mcbsp;
646 if (!omap_mcbsp_check_valid_id(id)) {
647 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
650 mcbsp = id_to_mcbsp_ptr(id);
652 dma_op_mode = mcbsp->dma_op_mode;
656 EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
658 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
660 struct omap_device *od;
662 od = find_omap_device_by_dev(mcbsp->dev);
664 * Enable wakup behavior, smart idle and all wakeups
665 * REVISIT: some wakeups may be unnecessary
667 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
668 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
672 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
674 struct omap_device *od;
676 od = find_omap_device_by_dev(mcbsp->dev);
679 * Disable wakup behavior, smart idle and all wakeups
681 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
683 * HW bug workaround - If no_idle mode is taken, we need to
684 * go to smart_idle before going to always_idle, or the
685 * device will not hit retention anymore.
688 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
692 static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
693 static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
694 static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
695 static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
699 * We can choose between IRQ based or polled IO.
700 * This needs to be called before omap_mcbsp_request().
702 int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
704 struct omap_mcbsp *mcbsp;
706 if (!omap_mcbsp_check_valid_id(id)) {
707 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
710 mcbsp = id_to_mcbsp_ptr(id);
712 spin_lock(&mcbsp->lock);
715 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
717 spin_unlock(&mcbsp->lock);
721 mcbsp->io_type = io_type;
723 spin_unlock(&mcbsp->lock);
727 EXPORT_SYMBOL(omap_mcbsp_set_io_type);
729 int omap_mcbsp_request(unsigned int id)
731 struct omap_mcbsp *mcbsp;
735 if (!omap_mcbsp_check_valid_id(id)) {
736 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
739 mcbsp = id_to_mcbsp_ptr(id);
741 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
746 spin_lock(&mcbsp->lock);
748 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
755 mcbsp->reg_cache = reg_cache;
756 spin_unlock(&mcbsp->lock);
758 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
759 mcbsp->pdata->ops->request(id);
761 pm_runtime_get_sync(mcbsp->dev);
763 /* Do procedure specific to omap34xx arch, if applicable */
764 omap34xx_mcbsp_request(mcbsp);
767 * Make sure that transmitter, receiver and sample-rate generator are
768 * not running before activating IRQs.
770 MCBSP_WRITE(mcbsp, SPCR1, 0);
771 MCBSP_WRITE(mcbsp, SPCR2, 0);
773 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
774 /* We need to get IRQs here */
775 init_completion(&mcbsp->tx_irq_completion);
776 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
777 0, "McBSP", (void *)mcbsp);
779 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
780 "for McBSP%d\n", mcbsp->tx_irq,
782 goto err_clk_disable;
786 init_completion(&mcbsp->rx_irq_completion);
787 err = request_irq(mcbsp->rx_irq,
788 omap_mcbsp_rx_irq_handler,
789 0, "McBSP", (void *)mcbsp);
791 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
792 "for McBSP%d\n", mcbsp->rx_irq,
801 free_irq(mcbsp->tx_irq, (void *)mcbsp);
803 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
804 mcbsp->pdata->ops->free(id);
806 /* Do procedure specific to omap34xx arch, if applicable */
807 omap34xx_mcbsp_free(mcbsp);
809 pm_runtime_put_sync(mcbsp->dev);
811 spin_lock(&mcbsp->lock);
813 mcbsp->reg_cache = NULL;
815 spin_unlock(&mcbsp->lock);
820 EXPORT_SYMBOL(omap_mcbsp_request);
822 void omap_mcbsp_free(unsigned int id)
824 struct omap_mcbsp *mcbsp;
827 if (!omap_mcbsp_check_valid_id(id)) {
828 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
831 mcbsp = id_to_mcbsp_ptr(id);
833 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
834 mcbsp->pdata->ops->free(id);
836 /* Do procedure specific to omap34xx arch, if applicable */
837 omap34xx_mcbsp_free(mcbsp);
839 pm_runtime_put_sync(mcbsp->dev);
841 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
844 free_irq(mcbsp->rx_irq, (void *)mcbsp);
845 free_irq(mcbsp->tx_irq, (void *)mcbsp);
848 reg_cache = mcbsp->reg_cache;
850 spin_lock(&mcbsp->lock);
852 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
855 mcbsp->reg_cache = NULL;
856 spin_unlock(&mcbsp->lock);
861 EXPORT_SYMBOL(omap_mcbsp_free);
864 * Here we start the McBSP, by enabling transmitter, receiver or both.
865 * If no transmitter or receiver is active prior calling, then sample-rate
866 * generator and frame sync are started.
868 void omap_mcbsp_start(unsigned int id, int tx, int rx)
870 struct omap_mcbsp *mcbsp;
874 if (!omap_mcbsp_check_valid_id(id)) {
875 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
878 mcbsp = id_to_mcbsp_ptr(id);
880 if (cpu_is_omap34xx())
881 omap_st_start(mcbsp);
883 mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
884 mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
886 /* Only enable SRG, if McBSP is master */
887 w = MCBSP_READ_CACHE(mcbsp, PCR0);
888 if (w & (FSXM | FSRM | CLKXM | CLKRM))
889 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
890 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
893 /* Start the sample generator */
894 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
895 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
898 /* Enable transmitter and receiver */
900 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
901 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
904 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
905 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
908 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
909 * REVISIT: 100us may give enough time for two CLKSRG, however
910 * due to some unknown PM related, clock gating etc. reason it
916 /* Start frame sync */
917 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
918 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
921 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
922 /* Release the transmitter and receiver */
923 w = MCBSP_READ_CACHE(mcbsp, XCCR);
924 w &= ~(tx ? XDISABLE : 0);
925 MCBSP_WRITE(mcbsp, XCCR, w);
926 w = MCBSP_READ_CACHE(mcbsp, RCCR);
927 w &= ~(rx ? RDISABLE : 0);
928 MCBSP_WRITE(mcbsp, RCCR, w);
931 /* Dump McBSP Regs */
932 omap_mcbsp_dump_reg(id);
934 EXPORT_SYMBOL(omap_mcbsp_start);
936 void omap_mcbsp_stop(unsigned int id, int tx, int rx)
938 struct omap_mcbsp *mcbsp;
942 if (!omap_mcbsp_check_valid_id(id)) {
943 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
947 mcbsp = id_to_mcbsp_ptr(id);
949 /* Reset transmitter */
951 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
952 w = MCBSP_READ_CACHE(mcbsp, XCCR);
953 w |= (tx ? XDISABLE : 0);
954 MCBSP_WRITE(mcbsp, XCCR, w);
956 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
957 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
961 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
962 w = MCBSP_READ_CACHE(mcbsp, RCCR);
963 w |= (rx ? RDISABLE : 0);
964 MCBSP_WRITE(mcbsp, RCCR, w);
966 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
967 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
969 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
970 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
973 /* Reset the sample rate generator */
974 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
975 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
978 if (cpu_is_omap34xx())
981 EXPORT_SYMBOL(omap_mcbsp_stop);
983 /* polled mcbsp i/o operations */
984 int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
986 struct omap_mcbsp *mcbsp;
988 if (!omap_mcbsp_check_valid_id(id)) {
989 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
993 mcbsp = id_to_mcbsp_ptr(id);
995 MCBSP_WRITE(mcbsp, DXR1, buf);
996 /* if frame sync error - clear the error */
997 if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
999 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
1003 /* wait for transmit confirmation */
1005 while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
1006 if (attemps++ > 1000) {
1007 MCBSP_WRITE(mcbsp, SPCR2,
1008 MCBSP_READ_CACHE(mcbsp, SPCR2) &
1011 MCBSP_WRITE(mcbsp, SPCR2,
1012 MCBSP_READ_CACHE(mcbsp, SPCR2) |
1015 dev_err(mcbsp->dev, "Could not write to"
1016 " McBSP%d Register\n", mcbsp->id);
1024 EXPORT_SYMBOL(omap_mcbsp_pollwrite);
1026 int omap_mcbsp_pollread(unsigned int id, u16 *buf)
1028 struct omap_mcbsp *mcbsp;
1030 if (!omap_mcbsp_check_valid_id(id)) {
1031 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1034 mcbsp = id_to_mcbsp_ptr(id);
1036 /* if frame sync error - clear the error */
1037 if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
1039 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
1043 /* wait for recieve confirmation */
1045 while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
1046 if (attemps++ > 1000) {
1047 MCBSP_WRITE(mcbsp, SPCR1,
1048 MCBSP_READ_CACHE(mcbsp, SPCR1) &
1051 MCBSP_WRITE(mcbsp, SPCR1,
1052 MCBSP_READ_CACHE(mcbsp, SPCR1) |
1055 dev_err(mcbsp->dev, "Could not read from"
1056 " McBSP%d Register\n", mcbsp->id);
1061 *buf = MCBSP_READ(mcbsp, DRR1);
1065 EXPORT_SYMBOL(omap_mcbsp_pollread);
1068 * IRQ based word transmission.
1070 void omap_mcbsp_xmit_word(unsigned int id, u32 word)
1072 struct omap_mcbsp *mcbsp;
1073 omap_mcbsp_word_length word_length;
1075 if (!omap_mcbsp_check_valid_id(id)) {
1076 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1080 mcbsp = id_to_mcbsp_ptr(id);
1081 word_length = mcbsp->tx_word_length;
1083 wait_for_completion(&mcbsp->tx_irq_completion);
1085 if (word_length > OMAP_MCBSP_WORD_16)
1086 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1087 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
1089 EXPORT_SYMBOL(omap_mcbsp_xmit_word);
1091 u32 omap_mcbsp_recv_word(unsigned int id)
1093 struct omap_mcbsp *mcbsp;
1094 u16 word_lsb, word_msb = 0;
1095 omap_mcbsp_word_length word_length;
1097 if (!omap_mcbsp_check_valid_id(id)) {
1098 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1101 mcbsp = id_to_mcbsp_ptr(id);
1103 word_length = mcbsp->rx_word_length;
1105 wait_for_completion(&mcbsp->rx_irq_completion);
1107 if (word_length > OMAP_MCBSP_WORD_16)
1108 word_msb = MCBSP_READ(mcbsp, DRR2);
1109 word_lsb = MCBSP_READ(mcbsp, DRR1);
1111 return (word_lsb | (word_msb << 16));
1113 EXPORT_SYMBOL(omap_mcbsp_recv_word);
1115 int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
1117 struct omap_mcbsp *mcbsp;
1118 omap_mcbsp_word_length tx_word_length;
1119 omap_mcbsp_word_length rx_word_length;
1120 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1122 if (!omap_mcbsp_check_valid_id(id)) {
1123 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1126 mcbsp = id_to_mcbsp_ptr(id);
1127 tx_word_length = mcbsp->tx_word_length;
1128 rx_word_length = mcbsp->rx_word_length;
1130 if (tx_word_length != rx_word_length)
1133 /* First we wait for the transmitter to be ready */
1134 spcr2 = MCBSP_READ(mcbsp, SPCR2);
1135 while (!(spcr2 & XRDY)) {
1136 spcr2 = MCBSP_READ(mcbsp, SPCR2);
1137 if (attempts++ > 1000) {
1138 /* We must reset the transmitter */
1139 MCBSP_WRITE(mcbsp, SPCR2,
1140 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
1142 MCBSP_WRITE(mcbsp, SPCR2,
1143 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
1145 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1146 "ready\n", mcbsp->id);
1151 /* Now we can push the data */
1152 if (tx_word_length > OMAP_MCBSP_WORD_16)
1153 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1154 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
1156 /* We wait for the receiver to be ready */
1157 spcr1 = MCBSP_READ(mcbsp, SPCR1);
1158 while (!(spcr1 & RRDY)) {
1159 spcr1 = MCBSP_READ(mcbsp, SPCR1);
1160 if (attempts++ > 1000) {
1161 /* We must reset the receiver */
1162 MCBSP_WRITE(mcbsp, SPCR1,
1163 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
1165 MCBSP_WRITE(mcbsp, SPCR1,
1166 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
1168 dev_err(mcbsp->dev, "McBSP%d receiver not "
1169 "ready\n", mcbsp->id);
1174 /* Receiver is ready, let's read the dummy data */
1175 if (rx_word_length > OMAP_MCBSP_WORD_16)
1176 word_msb = MCBSP_READ(mcbsp, DRR2);
1177 word_lsb = MCBSP_READ(mcbsp, DRR1);
1181 EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
1183 int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
1185 struct omap_mcbsp *mcbsp;
1187 omap_mcbsp_word_length tx_word_length;
1188 omap_mcbsp_word_length rx_word_length;
1189 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1191 if (!omap_mcbsp_check_valid_id(id)) {
1192 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1196 mcbsp = id_to_mcbsp_ptr(id);
1198 tx_word_length = mcbsp->tx_word_length;
1199 rx_word_length = mcbsp->rx_word_length;
1201 if (tx_word_length != rx_word_length)
1204 /* First we wait for the transmitter to be ready */
1205 spcr2 = MCBSP_READ(mcbsp, SPCR2);
1206 while (!(spcr2 & XRDY)) {
1207 spcr2 = MCBSP_READ(mcbsp, SPCR2);
1208 if (attempts++ > 1000) {
1209 /* We must reset the transmitter */
1210 MCBSP_WRITE(mcbsp, SPCR2,
1211 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
1213 MCBSP_WRITE(mcbsp, SPCR2,
1214 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
1216 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1217 "ready\n", mcbsp->id);
1222 /* We first need to enable the bus clock */
1223 if (tx_word_length > OMAP_MCBSP_WORD_16)
1224 MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
1225 MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
1227 /* We wait for the receiver to be ready */
1228 spcr1 = MCBSP_READ(mcbsp, SPCR1);
1229 while (!(spcr1 & RRDY)) {
1230 spcr1 = MCBSP_READ(mcbsp, SPCR1);
1231 if (attempts++ > 1000) {
1232 /* We must reset the receiver */
1233 MCBSP_WRITE(mcbsp, SPCR1,
1234 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
1236 MCBSP_WRITE(mcbsp, SPCR1,
1237 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
1239 dev_err(mcbsp->dev, "McBSP%d receiver not "
1240 "ready\n", mcbsp->id);
1245 /* Receiver is ready, there is something for us */
1246 if (rx_word_length > OMAP_MCBSP_WORD_16)
1247 word_msb = MCBSP_READ(mcbsp, DRR2);
1248 word_lsb = MCBSP_READ(mcbsp, DRR1);
1250 word[0] = (word_lsb | (word_msb << 16));
1254 EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
1257 * Simple DMA based buffer rx/tx routines.
1258 * Nothing fancy, just a single buffer tx/rx through DMA.
1259 * The DMA resources are released once the transfer is done.
1260 * For anything fancier, you should use your own customized DMA
1261 * routines and callbacks.
1263 int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
1264 unsigned int length)
1266 struct omap_mcbsp *mcbsp;
1272 if (!omap_mcbsp_check_valid_id(id)) {
1273 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1276 mcbsp = id_to_mcbsp_ptr(id);
1278 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
1279 omap_mcbsp_tx_dma_callback,
1282 dev_err(mcbsp->dev, " Unable to request DMA channel for "
1283 "McBSP%d TX. Trying IRQ based TX\n",
1287 mcbsp->dma_tx_lch = dma_tx_ch;
1289 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
1292 init_completion(&mcbsp->tx_dma_completion);
1294 if (cpu_class_is_omap1()) {
1295 src_port = OMAP_DMA_PORT_TIPB;
1296 dest_port = OMAP_DMA_PORT_EMIFF;
1298 if (cpu_class_is_omap2())
1299 sync_dev = mcbsp->dma_tx_sync;
1301 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
1302 OMAP_DMA_DATA_TYPE_S16,
1304 OMAP_DMA_SYNC_ELEMENT,
1307 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
1309 OMAP_DMA_AMODE_CONSTANT,
1310 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
1313 omap_set_dma_src_params(mcbsp->dma_tx_lch,
1315 OMAP_DMA_AMODE_POST_INC,
1319 omap_start_dma(mcbsp->dma_tx_lch);
1320 wait_for_completion(&mcbsp->tx_dma_completion);
1324 EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
1326 int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
1327 unsigned int length)
1329 struct omap_mcbsp *mcbsp;
1335 if (!omap_mcbsp_check_valid_id(id)) {
1336 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1339 mcbsp = id_to_mcbsp_ptr(id);
1341 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
1342 omap_mcbsp_rx_dma_callback,
1345 dev_err(mcbsp->dev, "Unable to request DMA channel for "
1346 "McBSP%d RX. Trying IRQ based RX\n",
1350 mcbsp->dma_rx_lch = dma_rx_ch;
1352 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
1355 init_completion(&mcbsp->rx_dma_completion);
1357 if (cpu_class_is_omap1()) {
1358 src_port = OMAP_DMA_PORT_TIPB;
1359 dest_port = OMAP_DMA_PORT_EMIFF;
1361 if (cpu_class_is_omap2())
1362 sync_dev = mcbsp->dma_rx_sync;
1364 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
1365 OMAP_DMA_DATA_TYPE_S16,
1367 OMAP_DMA_SYNC_ELEMENT,
1370 omap_set_dma_src_params(mcbsp->dma_rx_lch,
1372 OMAP_DMA_AMODE_CONSTANT,
1373 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
1376 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
1378 OMAP_DMA_AMODE_POST_INC,
1382 omap_start_dma(mcbsp->dma_rx_lch);
1383 wait_for_completion(&mcbsp->rx_dma_completion);
1387 EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
1391 * Since SPI setup is much simpler than the generic McBSP one,
1392 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1393 * Once this is done, you can call omap_mcbsp_start().
1395 void omap_mcbsp_set_spi_mode(unsigned int id,
1396 const struct omap_mcbsp_spi_cfg *spi_cfg)
1398 struct omap_mcbsp *mcbsp;
1399 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1401 if (!omap_mcbsp_check_valid_id(id)) {
1402 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1405 mcbsp = id_to_mcbsp_ptr(id);
1407 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1409 /* SPI has only one frame */
1410 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1411 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1413 /* Clock stop mode */
1414 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1415 mcbsp_cfg.spcr1 |= (1 << 12);
1417 mcbsp_cfg.spcr1 |= (3 << 11);
1419 /* Set clock parities */
1420 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1421 mcbsp_cfg.pcr0 |= CLKRP;
1423 mcbsp_cfg.pcr0 &= ~CLKRP;
1425 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1426 mcbsp_cfg.pcr0 &= ~CLKXP;
1428 mcbsp_cfg.pcr0 |= CLKXP;
1430 /* Set SCLKME to 0 and CLKSM to 1 */
1431 mcbsp_cfg.pcr0 &= ~SCLKME;
1432 mcbsp_cfg.srgr2 |= CLKSM;
1435 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1436 mcbsp_cfg.pcr0 &= ~FSXP;
1438 mcbsp_cfg.pcr0 |= FSXP;
1440 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1441 mcbsp_cfg.pcr0 |= CLKXM;
1442 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
1443 mcbsp_cfg.pcr0 |= FSXM;
1444 mcbsp_cfg.srgr2 &= ~FSGM;
1445 mcbsp_cfg.xcr2 |= XDATDLY(1);
1446 mcbsp_cfg.rcr2 |= RDATDLY(1);
1448 mcbsp_cfg.pcr0 &= ~CLKXM;
1449 mcbsp_cfg.srgr1 |= CLKGDV(1);
1450 mcbsp_cfg.pcr0 &= ~FSXM;
1451 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1452 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1455 mcbsp_cfg.xcr2 &= ~XPHASE;
1456 mcbsp_cfg.rcr2 &= ~RPHASE;
1458 omap_mcbsp_config(id, &mcbsp_cfg);
1460 EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
1462 #ifdef CONFIG_ARCH_OMAP3
1463 #define max_thres(m) (mcbsp->pdata->buffer_size)
1464 #define valid_threshold(m, val) ((val) <= max_thres(m))
1465 #define THRESHOLD_PROP_BUILDER(prop) \
1466 static ssize_t prop##_show(struct device *dev, \
1467 struct device_attribute *attr, char *buf) \
1469 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1471 return sprintf(buf, "%u\n", mcbsp->prop); \
1474 static ssize_t prop##_store(struct device *dev, \
1475 struct device_attribute *attr, \
1476 const char *buf, size_t size) \
1478 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1479 unsigned long val; \
1482 status = strict_strtoul(buf, 0, &val); \
1486 if (!valid_threshold(mcbsp, val)) \
1489 mcbsp->prop = val; \
1493 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1495 THRESHOLD_PROP_BUILDER(max_tx_thres);
1496 THRESHOLD_PROP_BUILDER(max_rx_thres);
1498 static const char *dma_op_modes[] = {
1499 "element", "threshold", "frame",
1502 static ssize_t dma_op_mode_show(struct device *dev,
1503 struct device_attribute *attr, char *buf)
1505 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1506 int dma_op_mode, i = 0;
1508 const char * const *s;
1510 dma_op_mode = mcbsp->dma_op_mode;
1512 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1513 if (dma_op_mode == i)
1514 len += sprintf(buf + len, "[%s] ", *s);
1516 len += sprintf(buf + len, "%s ", *s);
1518 len += sprintf(buf + len, "\n");
1523 static ssize_t dma_op_mode_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t size)
1527 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1528 const char * const *s;
1531 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1532 if (sysfs_streq(buf, *s))
1535 if (i == ARRAY_SIZE(dma_op_modes))
1538 spin_lock_irq(&mcbsp->lock);
1543 mcbsp->dma_op_mode = i;
1546 spin_unlock_irq(&mcbsp->lock);
1551 static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1553 static ssize_t st_taps_show(struct device *dev,
1554 struct device_attribute *attr, char *buf)
1556 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1557 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1561 spin_lock_irq(&mcbsp->lock);
1562 for (i = 0; i < st_data->nr_taps; i++)
1563 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1566 status += sprintf(&buf[status], "\n");
1567 spin_unlock_irq(&mcbsp->lock);
1572 static ssize_t st_taps_store(struct device *dev,
1573 struct device_attribute *attr,
1574 const char *buf, size_t size)
1576 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1577 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1578 int val, tmp, status, i = 0;
1580 spin_lock_irq(&mcbsp->lock);
1581 memset(st_data->taps, 0, sizeof(st_data->taps));
1582 st_data->nr_taps = 0;
1585 status = sscanf(buf, "%d%n", &val, &tmp);
1586 if (status < 0 || status == 0) {
1590 if (val < -32768 || val > 32767) {
1594 st_data->taps[i++] = val;
1601 st_data->nr_taps = i;
1604 spin_unlock_irq(&mcbsp->lock);
1609 static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1611 static const struct attribute *additional_attrs[] = {
1612 &dev_attr_max_tx_thres.attr,
1613 &dev_attr_max_rx_thres.attr,
1614 &dev_attr_dma_op_mode.attr,
1618 static const struct attribute_group additional_attr_group = {
1619 .attrs = (struct attribute **)additional_attrs,
1622 static inline int __devinit omap_additional_add(struct device *dev)
1624 return sysfs_create_group(&dev->kobj, &additional_attr_group);
1627 static inline void __devexit omap_additional_remove(struct device *dev)
1629 sysfs_remove_group(&dev->kobj, &additional_attr_group);
1632 static const struct attribute *sidetone_attrs[] = {
1633 &dev_attr_st_taps.attr,
1637 static const struct attribute_group sidetone_attr_group = {
1638 .attrs = (struct attribute **)sidetone_attrs,
1641 static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
1643 struct platform_device *pdev;
1644 struct resource *res;
1645 struct omap_mcbsp_st_data *st_data;
1648 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1654 pdev = container_of(mcbsp->dev, struct platform_device, dev);
1656 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1657 st_data->io_base_st = ioremap(res->start, resource_size(res));
1658 if (!st_data->io_base_st) {
1663 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1667 mcbsp->st_data = st_data;
1671 iounmap(st_data->io_base_st);
1679 static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1681 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1684 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1685 iounmap(st_data->io_base_st);
1690 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1692 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1693 if (cpu_is_omap34xx()) {
1695 * Initially configure the maximum thresholds to a safe value.
1696 * The McBSP FIFO usage with these values should not go under
1698 * If the whole FIFO without safety buffer is used, than there
1699 * is a possibility that the DMA will be not able to push the
1700 * new data on time, causing channel shifts in runtime.
1702 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1703 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1705 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1706 * for mcbsp2 instances.
1708 if (omap_additional_add(mcbsp->dev))
1709 dev_warn(mcbsp->dev,
1710 "Unable to create additional controls\n");
1712 if (mcbsp->id == 2 || mcbsp->id == 3)
1713 if (omap_st_add(mcbsp))
1714 dev_warn(mcbsp->dev,
1715 "Unable to create sidetone controls\n");
1718 mcbsp->max_tx_thres = -EINVAL;
1719 mcbsp->max_rx_thres = -EINVAL;
1723 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1725 if (cpu_is_omap34xx()) {
1726 omap_additional_remove(mcbsp->dev);
1728 if (mcbsp->id == 2 || mcbsp->id == 3)
1729 omap_st_remove(mcbsp);
1733 static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1734 static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1735 #endif /* CONFIG_ARCH_OMAP3 */
1738 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1739 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1741 static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
1743 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
1744 struct omap_mcbsp *mcbsp;
1745 int id = pdev->id - 1;
1746 struct resource *res;
1750 dev_err(&pdev->dev, "McBSP device initialized without"
1756 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
1758 if (id >= omap_mcbsp_count) {
1759 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1764 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1770 spin_lock_init(&mcbsp->lock);
1773 mcbsp->dma_tx_lch = -1;
1774 mcbsp->dma_rx_lch = -1;
1776 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1778 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1780 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1781 "resource\n", __func__, pdev->id);
1786 mcbsp->phys_base = res->start;
1787 omap_mcbsp_cache_size = resource_size(res);
1788 mcbsp->io_base = ioremap(res->start, resource_size(res));
1789 if (!mcbsp->io_base) {
1794 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1796 mcbsp->phys_dma_base = mcbsp->phys_base;
1798 mcbsp->phys_dma_base = res->start;
1800 /* Default I/O is IRQ based */
1801 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1803 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1804 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1806 /* From OMAP4 there will be a single irq line */
1807 if (mcbsp->tx_irq == -ENXIO)
1808 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1810 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1812 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1813 __func__, pdev->id);
1817 mcbsp->dma_rx_sync = res->start;
1819 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1821 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1822 __func__, pdev->id);
1826 mcbsp->dma_tx_sync = res->start;
1828 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1829 if (IS_ERR(mcbsp->fclk)) {
1830 ret = PTR_ERR(mcbsp->fclk);
1831 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1835 mcbsp->pdata = pdata;
1836 mcbsp->dev = &pdev->dev;
1837 mcbsp_ptr[id] = mcbsp;
1838 platform_set_drvdata(pdev, mcbsp);
1839 pm_runtime_enable(mcbsp->dev);
1841 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1842 omap34xx_device_init(mcbsp);
1847 iounmap(mcbsp->io_base);
1854 static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
1856 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1858 platform_set_drvdata(pdev, NULL);
1861 if (mcbsp->pdata && mcbsp->pdata->ops &&
1862 mcbsp->pdata->ops->free)
1863 mcbsp->pdata->ops->free(mcbsp->id);
1865 omap34xx_device_exit(mcbsp);
1867 clk_put(mcbsp->fclk);
1869 iounmap(mcbsp->io_base);
1876 static struct platform_driver omap_mcbsp_driver = {
1877 .probe = omap_mcbsp_probe,
1878 .remove = __devexit_p(omap_mcbsp_remove),
1880 .name = "omap-mcbsp",
1884 int __init omap_mcbsp_init(void)
1886 /* Register the McBSP driver */
1887 return platform_driver_register(&omap_mcbsp_driver);