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
);
79 #define MCBSP_ST_READ(mcbsp, reg) \
80 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
81 #define MCBSP_ST_WRITE(mcbsp, reg, val) \
82 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
85 #define MCBSP_READ(mcbsp, reg) \
86 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
87 #define MCBSP_WRITE(mcbsp, reg, val) \
88 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
89 #define MCBSP_READ_CACHE(mcbsp, reg) \
90 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
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
);
233 * omap_mcbsp_dma_params - returns the dma channel number
235 * @stream - indicates the direction of data flow (rx or tx)
237 * Returns the dma channel number for the rx channel or tx channel
238 * based on the value of @stream for the requested mcbsp given by @id
240 int omap_mcbsp_dma_ch_params(unsigned int id
, unsigned int stream
)
242 struct omap_mcbsp
*mcbsp
;
244 if (!omap_mcbsp_check_valid_id(id
)) {
245 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
248 mcbsp
= id_to_mcbsp_ptr(id
);
251 return mcbsp
->dma_rx_sync
;
253 return mcbsp
->dma_tx_sync
;
255 EXPORT_SYMBOL(omap_mcbsp_dma_ch_params
);
258 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
260 * @stream - indicates the direction of data flow (rx or tx)
262 * Returns the address of mcbsp data transmit register or data receive register
263 * to be used by DMA for transferring/receiving data based on the value of
264 * @stream for the requested mcbsp given by @id
266 int omap_mcbsp_dma_reg_params(unsigned int id
, unsigned int stream
)
268 struct omap_mcbsp
*mcbsp
;
271 if (!omap_mcbsp_check_valid_id(id
)) {
272 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
275 mcbsp
= id_to_mcbsp_ptr(id
);
277 data_reg
= mcbsp
->phys_dma_base
;
279 if (mcbsp
->mcbsp_config_type
< MCBSP_CONFIG_TYPE2
) {
281 data_reg
+= OMAP_MCBSP_REG_DRR1
;
283 data_reg
+= OMAP_MCBSP_REG_DXR1
;
286 data_reg
+= OMAP_MCBSP_REG_DRR
;
288 data_reg
+= OMAP_MCBSP_REG_DXR
;
293 EXPORT_SYMBOL(omap_mcbsp_dma_reg_params
);
295 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
296 static struct omap_device
*find_omap_device_by_dev(struct device
*dev
)
298 struct platform_device
*pdev
= container_of(dev
,
299 struct platform_device
, dev
);
300 return container_of(pdev
, struct omap_device
, pdev
);
304 #ifdef CONFIG_ARCH_OMAP3
305 static void omap_st_on(struct omap_mcbsp
*mcbsp
)
308 struct omap_device
*od
;
310 od
= find_omap_device_by_dev(mcbsp
->dev
);
313 * Sidetone uses McBSP ICLK - which must not idle when sidetones
314 * are enabled or sidetones start sounding ugly.
316 w
= omap2_cm_read_mod_reg(OMAP3430_PER_MOD
, CM_AUTOIDLE
);
317 w
&= ~(1 << (mcbsp
->id
- 2));
318 omap2_cm_write_mod_reg(w
, OMAP3430_PER_MOD
, CM_AUTOIDLE
);
320 /* Enable McBSP Sidetone */
321 w
= MCBSP_READ(mcbsp
, SSELCR
);
322 MCBSP_WRITE(mcbsp
, SSELCR
, w
| SIDETONEEN
);
324 /* Enable Sidetone from Sidetone Core */
325 w
= MCBSP_ST_READ(mcbsp
, SSELCR
);
326 MCBSP_ST_WRITE(mcbsp
, SSELCR
, w
| ST_SIDETONEEN
);
329 static void omap_st_off(struct omap_mcbsp
*mcbsp
)
332 struct omap_device
*od
;
334 od
= find_omap_device_by_dev(mcbsp
->dev
);
336 w
= MCBSP_ST_READ(mcbsp
, SSELCR
);
337 MCBSP_ST_WRITE(mcbsp
, SSELCR
, w
& ~(ST_SIDETONEEN
));
339 w
= MCBSP_READ(mcbsp
, SSELCR
);
340 MCBSP_WRITE(mcbsp
, SSELCR
, w
& ~(SIDETONEEN
));
342 w
= omap2_cm_read_mod_reg(OMAP3430_PER_MOD
, CM_AUTOIDLE
);
343 w
|= 1 << (mcbsp
->id
- 2);
344 omap2_cm_write_mod_reg(w
, OMAP3430_PER_MOD
, CM_AUTOIDLE
);
347 static void omap_st_fir_write(struct omap_mcbsp
*mcbsp
, s16
*fir
)
350 struct omap_device
*od
;
352 od
= find_omap_device_by_dev(mcbsp
->dev
);
354 val
= MCBSP_ST_READ(mcbsp
, SSELCR
);
356 if (val
& ST_COEFFWREN
)
357 MCBSP_ST_WRITE(mcbsp
, SSELCR
, val
& ~(ST_COEFFWREN
));
359 MCBSP_ST_WRITE(mcbsp
, SSELCR
, val
| ST_COEFFWREN
);
361 for (i
= 0; i
< 128; i
++)
362 MCBSP_ST_WRITE(mcbsp
, SFIRCR
, fir
[i
]);
366 val
= MCBSP_ST_READ(mcbsp
, SSELCR
);
367 while (!(val
& ST_COEFFWRDONE
) && (++i
< 1000))
368 val
= MCBSP_ST_READ(mcbsp
, SSELCR
);
370 MCBSP_ST_WRITE(mcbsp
, SSELCR
, val
& ~(ST_COEFFWREN
));
373 dev_err(mcbsp
->dev
, "McBSP FIR load error!\n");
376 static void omap_st_chgain(struct omap_mcbsp
*mcbsp
)
379 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
380 struct omap_device
*od
;
382 od
= find_omap_device_by_dev(mcbsp
->dev
);
384 w
= MCBSP_ST_READ(mcbsp
, SSELCR
);
386 MCBSP_ST_WRITE(mcbsp
, SGAINCR
, ST_CH0GAIN(st_data
->ch0gain
) | \
387 ST_CH1GAIN(st_data
->ch1gain
));
390 int omap_st_set_chgain(unsigned int id
, int channel
, s16 chgain
)
392 struct omap_mcbsp
*mcbsp
;
393 struct omap_mcbsp_st_data
*st_data
;
396 if (!omap_mcbsp_check_valid_id(id
)) {
397 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
401 mcbsp
= id_to_mcbsp_ptr(id
);
402 st_data
= mcbsp
->st_data
;
407 spin_lock_irq(&mcbsp
->lock
);
409 st_data
->ch0gain
= chgain
;
410 else if (channel
== 1)
411 st_data
->ch1gain
= chgain
;
415 if (st_data
->enabled
)
416 omap_st_chgain(mcbsp
);
417 spin_unlock_irq(&mcbsp
->lock
);
421 EXPORT_SYMBOL(omap_st_set_chgain
);
423 int omap_st_get_chgain(unsigned int id
, int channel
, s16
*chgain
)
425 struct omap_mcbsp
*mcbsp
;
426 struct omap_mcbsp_st_data
*st_data
;
429 if (!omap_mcbsp_check_valid_id(id
)) {
430 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
434 mcbsp
= id_to_mcbsp_ptr(id
);
435 st_data
= mcbsp
->st_data
;
440 spin_lock_irq(&mcbsp
->lock
);
442 *chgain
= st_data
->ch0gain
;
443 else if (channel
== 1)
444 *chgain
= st_data
->ch1gain
;
447 spin_unlock_irq(&mcbsp
->lock
);
451 EXPORT_SYMBOL(omap_st_get_chgain
);
453 static int omap_st_start(struct omap_mcbsp
*mcbsp
)
455 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
457 if (st_data
&& st_data
->enabled
&& !st_data
->running
) {
458 omap_st_fir_write(mcbsp
, st_data
->taps
);
459 omap_st_chgain(mcbsp
);
463 st_data
->running
= 1;
470 int omap_st_enable(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
;
486 spin_lock_irq(&mcbsp
->lock
);
487 st_data
->enabled
= 1;
488 omap_st_start(mcbsp
);
489 spin_unlock_irq(&mcbsp
->lock
);
493 EXPORT_SYMBOL(omap_st_enable
);
495 static int omap_st_stop(struct omap_mcbsp
*mcbsp
)
497 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
499 if (st_data
&& st_data
->running
) {
502 st_data
->running
= 0;
509 int omap_st_disable(unsigned int id
)
511 struct omap_mcbsp
*mcbsp
;
512 struct omap_mcbsp_st_data
*st_data
;
515 if (!omap_mcbsp_check_valid_id(id
)) {
516 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
520 mcbsp
= id_to_mcbsp_ptr(id
);
521 st_data
= mcbsp
->st_data
;
526 spin_lock_irq(&mcbsp
->lock
);
528 st_data
->enabled
= 0;
529 spin_unlock_irq(&mcbsp
->lock
);
533 EXPORT_SYMBOL(omap_st_disable
);
535 int omap_st_is_enabled(unsigned int id
)
537 struct omap_mcbsp
*mcbsp
;
538 struct omap_mcbsp_st_data
*st_data
;
540 if (!omap_mcbsp_check_valid_id(id
)) {
541 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
545 mcbsp
= id_to_mcbsp_ptr(id
);
546 st_data
= mcbsp
->st_data
;
552 return st_data
->enabled
;
554 EXPORT_SYMBOL(omap_st_is_enabled
);
556 static inline void omap_st_start(struct omap_mcbsp
*mcbsp
) {}
557 static inline void omap_st_stop(struct omap_mcbsp
*mcbsp
) {}
560 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
562 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
563 * The threshold parameter is 1 based, and it is converted (threshold - 1)
564 * for the THRSH2 register.
566 void omap_mcbsp_set_tx_threshold(unsigned int id
, u16 threshold
)
568 struct omap_mcbsp
*mcbsp
;
570 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
573 if (!omap_mcbsp_check_valid_id(id
)) {
574 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
577 mcbsp
= id_to_mcbsp_ptr(id
);
579 if (threshold
&& threshold
<= mcbsp
->max_tx_thres
)
580 MCBSP_WRITE(mcbsp
, THRSH2
, threshold
- 1);
582 EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold
);
585 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
586 * The threshold parameter is 1 based, and it is converted (threshold - 1)
587 * for the THRSH1 register.
589 void omap_mcbsp_set_rx_threshold(unsigned int id
, u16 threshold
)
591 struct omap_mcbsp
*mcbsp
;
593 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
596 if (!omap_mcbsp_check_valid_id(id
)) {
597 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
600 mcbsp
= id_to_mcbsp_ptr(id
);
602 if (threshold
&& threshold
<= mcbsp
->max_rx_thres
)
603 MCBSP_WRITE(mcbsp
, THRSH1
, threshold
- 1);
605 EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold
);
608 * omap_mcbsp_get_max_tx_thres just return the current configured
609 * maximum threshold for transmission
611 u16
omap_mcbsp_get_max_tx_threshold(unsigned int id
)
613 struct omap_mcbsp
*mcbsp
;
615 if (!omap_mcbsp_check_valid_id(id
)) {
616 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
619 mcbsp
= id_to_mcbsp_ptr(id
);
621 return mcbsp
->max_tx_thres
;
623 EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold
);
626 * omap_mcbsp_get_max_rx_thres just return the current configured
627 * maximum threshold for reception
629 u16
omap_mcbsp_get_max_rx_threshold(unsigned int id
)
631 struct omap_mcbsp
*mcbsp
;
633 if (!omap_mcbsp_check_valid_id(id
)) {
634 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
637 mcbsp
= id_to_mcbsp_ptr(id
);
639 return mcbsp
->max_rx_thres
;
641 EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold
);
643 u16
omap_mcbsp_get_fifo_size(unsigned int id
)
645 struct omap_mcbsp
*mcbsp
;
647 if (!omap_mcbsp_check_valid_id(id
)) {
648 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
651 mcbsp
= id_to_mcbsp_ptr(id
);
653 return mcbsp
->pdata
->buffer_size
;
655 EXPORT_SYMBOL(omap_mcbsp_get_fifo_size
);
658 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
660 u16
omap_mcbsp_get_tx_delay(unsigned int id
)
662 struct omap_mcbsp
*mcbsp
;
665 if (!omap_mcbsp_check_valid_id(id
)) {
666 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
669 mcbsp
= id_to_mcbsp_ptr(id
);
671 /* Returns the number of free locations in the buffer */
672 buffstat
= MCBSP_READ(mcbsp
, XBUFFSTAT
);
674 /* Number of slots are different in McBSP ports */
675 return mcbsp
->pdata
->buffer_size
- buffstat
;
677 EXPORT_SYMBOL(omap_mcbsp_get_tx_delay
);
680 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
681 * to reach the threshold value (when the DMA will be triggered to read it)
683 u16
omap_mcbsp_get_rx_delay(unsigned int id
)
685 struct omap_mcbsp
*mcbsp
;
686 u16 buffstat
, threshold
;
688 if (!omap_mcbsp_check_valid_id(id
)) {
689 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
692 mcbsp
= id_to_mcbsp_ptr(id
);
694 /* Returns the number of used locations in the buffer */
695 buffstat
= MCBSP_READ(mcbsp
, RBUFFSTAT
);
697 threshold
= MCBSP_READ(mcbsp
, THRSH1
);
699 /* Return the number of location till we reach the threshold limit */
700 if (threshold
<= buffstat
)
703 return threshold
- buffstat
;
705 EXPORT_SYMBOL(omap_mcbsp_get_rx_delay
);
708 * omap_mcbsp_get_dma_op_mode just return the current configured
709 * operating mode for the mcbsp channel
711 int omap_mcbsp_get_dma_op_mode(unsigned int id
)
713 struct omap_mcbsp
*mcbsp
;
716 if (!omap_mcbsp_check_valid_id(id
)) {
717 printk(KERN_ERR
"%s: Invalid id (%u)\n", __func__
, id
+ 1);
720 mcbsp
= id_to_mcbsp_ptr(id
);
722 dma_op_mode
= mcbsp
->dma_op_mode
;
726 EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode
);
728 static inline void omap34xx_mcbsp_request(struct omap_mcbsp
*mcbsp
)
730 struct omap_device
*od
;
732 od
= find_omap_device_by_dev(mcbsp
->dev
);
734 * Enable wakup behavior, smart idle and all wakeups
735 * REVISIT: some wakeups may be unnecessary
737 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
738 MCBSP_WRITE(mcbsp
, WAKEUPEN
, XRDYEN
| RRDYEN
);
742 static inline void omap34xx_mcbsp_free(struct omap_mcbsp
*mcbsp
)
744 struct omap_device
*od
;
746 od
= find_omap_device_by_dev(mcbsp
->dev
);
749 * Disable wakup behavior, smart idle and all wakeups
751 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
753 * HW bug workaround - If no_idle mode is taken, we need to
754 * go to smart_idle before going to always_idle, or the
755 * device will not hit retention anymore.
758 MCBSP_WRITE(mcbsp
, WAKEUPEN
, 0);
762 static inline void omap34xx_mcbsp_request(struct omap_mcbsp
*mcbsp
) {}
763 static inline void omap34xx_mcbsp_free(struct omap_mcbsp
*mcbsp
) {}
767 * We can choose between IRQ based or polled IO.
768 * This needs to be called before omap_mcbsp_request().
770 int omap_mcbsp_set_io_type(unsigned int id
, omap_mcbsp_io_type_t io_type
)
772 struct omap_mcbsp
*mcbsp
;
774 if (!omap_mcbsp_check_valid_id(id
)) {
775 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
778 mcbsp
= id_to_mcbsp_ptr(id
);
780 spin_lock(&mcbsp
->lock
);
783 dev_err(mcbsp
->dev
, "McBSP%d is currently in use\n",
785 spin_unlock(&mcbsp
->lock
);
789 mcbsp
->io_type
= io_type
;
791 spin_unlock(&mcbsp
->lock
);
795 EXPORT_SYMBOL(omap_mcbsp_set_io_type
);
797 int omap_mcbsp_request(unsigned int id
)
799 struct omap_mcbsp
*mcbsp
;
803 if (!omap_mcbsp_check_valid_id(id
)) {
804 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
807 mcbsp
= id_to_mcbsp_ptr(id
);
809 reg_cache
= kzalloc(omap_mcbsp_cache_size
, GFP_KERNEL
);
814 spin_lock(&mcbsp
->lock
);
816 dev_err(mcbsp
->dev
, "McBSP%d is currently in use\n",
823 mcbsp
->reg_cache
= reg_cache
;
824 spin_unlock(&mcbsp
->lock
);
826 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&& mcbsp
->pdata
->ops
->request
)
827 mcbsp
->pdata
->ops
->request(id
);
829 pm_runtime_get_sync(mcbsp
->dev
);
831 /* Do procedure specific to omap34xx arch, if applicable */
832 omap34xx_mcbsp_request(mcbsp
);
835 * Make sure that transmitter, receiver and sample-rate generator are
836 * not running before activating IRQs.
838 MCBSP_WRITE(mcbsp
, SPCR1
, 0);
839 MCBSP_WRITE(mcbsp
, SPCR2
, 0);
841 if (mcbsp
->io_type
== OMAP_MCBSP_IRQ_IO
) {
842 /* We need to get IRQs here */
843 init_completion(&mcbsp
->tx_irq_completion
);
844 err
= request_irq(mcbsp
->tx_irq
, omap_mcbsp_tx_irq_handler
,
845 0, "McBSP", (void *)mcbsp
);
847 dev_err(mcbsp
->dev
, "Unable to request TX IRQ %d "
848 "for McBSP%d\n", mcbsp
->tx_irq
,
850 goto err_clk_disable
;
854 init_completion(&mcbsp
->rx_irq_completion
);
855 err
= request_irq(mcbsp
->rx_irq
,
856 omap_mcbsp_rx_irq_handler
,
857 0, "McBSP", (void *)mcbsp
);
859 dev_err(mcbsp
->dev
, "Unable to request RX IRQ %d "
860 "for McBSP%d\n", mcbsp
->rx_irq
,
869 free_irq(mcbsp
->tx_irq
, (void *)mcbsp
);
871 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&& mcbsp
->pdata
->ops
->free
)
872 mcbsp
->pdata
->ops
->free(id
);
874 /* Do procedure specific to omap34xx arch, if applicable */
875 omap34xx_mcbsp_free(mcbsp
);
877 pm_runtime_put_sync(mcbsp
->dev
);
879 spin_lock(&mcbsp
->lock
);
881 mcbsp
->reg_cache
= NULL
;
883 spin_unlock(&mcbsp
->lock
);
888 EXPORT_SYMBOL(omap_mcbsp_request
);
890 void omap_mcbsp_free(unsigned int id
)
892 struct omap_mcbsp
*mcbsp
;
895 if (!omap_mcbsp_check_valid_id(id
)) {
896 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
899 mcbsp
= id_to_mcbsp_ptr(id
);
901 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&& mcbsp
->pdata
->ops
->free
)
902 mcbsp
->pdata
->ops
->free(id
);
904 /* Do procedure specific to omap34xx arch, if applicable */
905 omap34xx_mcbsp_free(mcbsp
);
907 pm_runtime_put_sync(mcbsp
->dev
);
909 if (mcbsp
->io_type
== OMAP_MCBSP_IRQ_IO
) {
912 free_irq(mcbsp
->rx_irq
, (void *)mcbsp
);
913 free_irq(mcbsp
->tx_irq
, (void *)mcbsp
);
916 reg_cache
= mcbsp
->reg_cache
;
918 spin_lock(&mcbsp
->lock
);
920 dev_err(mcbsp
->dev
, "McBSP%d was not reserved\n", mcbsp
->id
);
923 mcbsp
->reg_cache
= NULL
;
924 spin_unlock(&mcbsp
->lock
);
929 EXPORT_SYMBOL(omap_mcbsp_free
);
932 * Here we start the McBSP, by enabling transmitter, receiver or both.
933 * If no transmitter or receiver is active prior calling, then sample-rate
934 * generator and frame sync are started.
936 void omap_mcbsp_start(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);
946 mcbsp
= id_to_mcbsp_ptr(id
);
948 if (cpu_is_omap34xx())
949 omap_st_start(mcbsp
);
951 mcbsp
->rx_word_length
= (MCBSP_READ_CACHE(mcbsp
, RCR1
) >> 5) & 0x7;
952 mcbsp
->tx_word_length
= (MCBSP_READ_CACHE(mcbsp
, XCR1
) >> 5) & 0x7;
954 /* Only enable SRG, if McBSP is master */
955 w
= MCBSP_READ_CACHE(mcbsp
, PCR0
);
956 if (w
& (FSXM
| FSRM
| CLKXM
| CLKRM
))
957 enable_srg
= !((MCBSP_READ_CACHE(mcbsp
, SPCR2
) |
958 MCBSP_READ_CACHE(mcbsp
, SPCR1
)) & 1);
961 /* Start the sample generator */
962 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
963 MCBSP_WRITE(mcbsp
, SPCR2
, w
| (1 << 6));
966 /* Enable transmitter and receiver */
968 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
969 MCBSP_WRITE(mcbsp
, SPCR2
, w
| tx
);
972 w
= MCBSP_READ_CACHE(mcbsp
, SPCR1
);
973 MCBSP_WRITE(mcbsp
, SPCR1
, w
| rx
);
976 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
977 * REVISIT: 100us may give enough time for two CLKSRG, however
978 * due to some unknown PM related, clock gating etc. reason it
984 /* Start frame sync */
985 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
986 MCBSP_WRITE(mcbsp
, SPCR2
, w
| (1 << 7));
989 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
990 /* Release the transmitter and receiver */
991 w
= MCBSP_READ_CACHE(mcbsp
, XCCR
);
992 w
&= ~(tx
? XDISABLE
: 0);
993 MCBSP_WRITE(mcbsp
, XCCR
, w
);
994 w
= MCBSP_READ_CACHE(mcbsp
, RCCR
);
995 w
&= ~(rx
? RDISABLE
: 0);
996 MCBSP_WRITE(mcbsp
, RCCR
, w
);
999 /*Disable and Re-enable transmitter if ready */
1000 if (tx
&& (MCBSP_READ(mcbsp
, SPCR2
) & XRDY
)) {
1001 MCBSP_WRITE(mcbsp
, SPCR2
,
1002 MCBSP_READ_CACHE(mcbsp
, SPCR2
) &
1004 MCBSP_WRITE(mcbsp
, SPCR2
,
1005 MCBSP_READ_CACHE(mcbsp
, SPCR2
) |
1008 /*Disable and Re-enable receiver if ready */
1009 if (rx
&& (MCBSP_READ(mcbsp
, SPCR1
) & RRDY
)) {
1010 MCBSP_WRITE(mcbsp
, SPCR1
,
1011 MCBSP_READ_CACHE(mcbsp
, SPCR1
) &
1013 MCBSP_WRITE(mcbsp
, SPCR1
,
1014 MCBSP_READ_CACHE(mcbsp
, SPCR1
) |
1018 /* Dump McBSP Regs */
1019 omap_mcbsp_dump_reg(id
);
1021 EXPORT_SYMBOL(omap_mcbsp_start
);
1023 void omap_mcbsp_stop(unsigned int id
, int tx
, int rx
)
1025 struct omap_mcbsp
*mcbsp
;
1029 if (!omap_mcbsp_check_valid_id(id
)) {
1030 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1034 mcbsp
= id_to_mcbsp_ptr(id
);
1036 /* Reset transmitter */
1038 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
1039 w
= MCBSP_READ_CACHE(mcbsp
, XCCR
);
1040 w
|= (tx
? XDISABLE
: 0);
1041 MCBSP_WRITE(mcbsp
, XCCR
, w
);
1043 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
1044 MCBSP_WRITE(mcbsp
, SPCR2
, w
& ~tx
);
1046 /* Reset receiver */
1048 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
1049 w
= MCBSP_READ_CACHE(mcbsp
, RCCR
);
1050 w
|= (rx
? RDISABLE
: 0);
1051 MCBSP_WRITE(mcbsp
, RCCR
, w
);
1053 w
= MCBSP_READ_CACHE(mcbsp
, SPCR1
);
1054 MCBSP_WRITE(mcbsp
, SPCR1
, w
& ~rx
);
1056 idle
= !((MCBSP_READ_CACHE(mcbsp
, SPCR2
) |
1057 MCBSP_READ_CACHE(mcbsp
, SPCR1
)) & 1);
1060 /* Reset the sample rate generator */
1061 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
1062 MCBSP_WRITE(mcbsp
, SPCR2
, w
& ~(1 << 6));
1065 if (cpu_is_omap34xx())
1066 omap_st_stop(mcbsp
);
1068 EXPORT_SYMBOL(omap_mcbsp_stop
);
1070 /* polled mcbsp i/o operations */
1071 int omap_mcbsp_pollwrite(unsigned int id
, u16 buf
)
1073 struct omap_mcbsp
*mcbsp
;
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
);
1082 MCBSP_WRITE(mcbsp
, DXR1
, buf
);
1083 /* if frame sync error - clear the error */
1084 if (MCBSP_READ(mcbsp
, SPCR2
) & XSYNC_ERR
) {
1086 MCBSP_WRITE(mcbsp
, SPCR2
, MCBSP_READ_CACHE(mcbsp
, SPCR2
));
1090 /* wait for transmit confirmation */
1092 while (!(MCBSP_READ(mcbsp
, SPCR2
) & XRDY
)) {
1093 if (attemps
++ > 1000) {
1094 MCBSP_WRITE(mcbsp
, SPCR2
,
1095 MCBSP_READ_CACHE(mcbsp
, SPCR2
) &
1098 MCBSP_WRITE(mcbsp
, SPCR2
,
1099 MCBSP_READ_CACHE(mcbsp
, SPCR2
) |
1102 dev_err(mcbsp
->dev
, "Could not write to"
1103 " McBSP%d Register\n", mcbsp
->id
);
1111 EXPORT_SYMBOL(omap_mcbsp_pollwrite
);
1113 int omap_mcbsp_pollread(unsigned int id
, u16
*buf
)
1115 struct omap_mcbsp
*mcbsp
;
1117 if (!omap_mcbsp_check_valid_id(id
)) {
1118 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1121 mcbsp
= id_to_mcbsp_ptr(id
);
1123 /* if frame sync error - clear the error */
1124 if (MCBSP_READ(mcbsp
, SPCR1
) & RSYNC_ERR
) {
1126 MCBSP_WRITE(mcbsp
, SPCR1
, MCBSP_READ_CACHE(mcbsp
, SPCR1
));
1130 /* wait for receive confirmation */
1132 while (!(MCBSP_READ(mcbsp
, SPCR1
) & RRDY
)) {
1133 if (attemps
++ > 1000) {
1134 MCBSP_WRITE(mcbsp
, SPCR1
,
1135 MCBSP_READ_CACHE(mcbsp
, SPCR1
) &
1138 MCBSP_WRITE(mcbsp
, SPCR1
,
1139 MCBSP_READ_CACHE(mcbsp
, SPCR1
) |
1142 dev_err(mcbsp
->dev
, "Could not read from"
1143 " McBSP%d Register\n", mcbsp
->id
);
1148 *buf
= MCBSP_READ(mcbsp
, DRR1
);
1152 EXPORT_SYMBOL(omap_mcbsp_pollread
);
1155 * IRQ based word transmission.
1157 void omap_mcbsp_xmit_word(unsigned int id
, u32 word
)
1159 struct omap_mcbsp
*mcbsp
;
1160 omap_mcbsp_word_length word_length
;
1162 if (!omap_mcbsp_check_valid_id(id
)) {
1163 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1167 mcbsp
= id_to_mcbsp_ptr(id
);
1168 word_length
= mcbsp
->tx_word_length
;
1170 wait_for_completion(&mcbsp
->tx_irq_completion
);
1172 if (word_length
> OMAP_MCBSP_WORD_16
)
1173 MCBSP_WRITE(mcbsp
, DXR2
, word
>> 16);
1174 MCBSP_WRITE(mcbsp
, DXR1
, word
& 0xffff);
1176 EXPORT_SYMBOL(omap_mcbsp_xmit_word
);
1178 u32
omap_mcbsp_recv_word(unsigned int id
)
1180 struct omap_mcbsp
*mcbsp
;
1181 u16 word_lsb
, word_msb
= 0;
1182 omap_mcbsp_word_length word_length
;
1184 if (!omap_mcbsp_check_valid_id(id
)) {
1185 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1188 mcbsp
= id_to_mcbsp_ptr(id
);
1190 word_length
= mcbsp
->rx_word_length
;
1192 wait_for_completion(&mcbsp
->rx_irq_completion
);
1194 if (word_length
> OMAP_MCBSP_WORD_16
)
1195 word_msb
= MCBSP_READ(mcbsp
, DRR2
);
1196 word_lsb
= MCBSP_READ(mcbsp
, DRR1
);
1198 return (word_lsb
| (word_msb
<< 16));
1200 EXPORT_SYMBOL(omap_mcbsp_recv_word
);
1202 int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id
, u32 word
)
1204 struct omap_mcbsp
*mcbsp
;
1205 omap_mcbsp_word_length tx_word_length
;
1206 omap_mcbsp_word_length rx_word_length
;
1207 u16 spcr2
, spcr1
, attempts
= 0, word_lsb
, word_msb
= 0;
1209 if (!omap_mcbsp_check_valid_id(id
)) {
1210 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1213 mcbsp
= id_to_mcbsp_ptr(id
);
1214 tx_word_length
= mcbsp
->tx_word_length
;
1215 rx_word_length
= mcbsp
->rx_word_length
;
1217 if (tx_word_length
!= rx_word_length
)
1220 /* First we wait for the transmitter to be ready */
1221 spcr2
= MCBSP_READ(mcbsp
, SPCR2
);
1222 while (!(spcr2
& XRDY
)) {
1223 spcr2
= MCBSP_READ(mcbsp
, SPCR2
);
1224 if (attempts
++ > 1000) {
1225 /* We must reset the transmitter */
1226 MCBSP_WRITE(mcbsp
, SPCR2
,
1227 MCBSP_READ_CACHE(mcbsp
, SPCR2
) & (~XRST
));
1229 MCBSP_WRITE(mcbsp
, SPCR2
,
1230 MCBSP_READ_CACHE(mcbsp
, SPCR2
) | XRST
);
1232 dev_err(mcbsp
->dev
, "McBSP%d transmitter not "
1233 "ready\n", mcbsp
->id
);
1238 /* Now we can push the data */
1239 if (tx_word_length
> OMAP_MCBSP_WORD_16
)
1240 MCBSP_WRITE(mcbsp
, DXR2
, word
>> 16);
1241 MCBSP_WRITE(mcbsp
, DXR1
, word
& 0xffff);
1243 /* We wait for the receiver to be ready */
1244 spcr1
= MCBSP_READ(mcbsp
, SPCR1
);
1245 while (!(spcr1
& RRDY
)) {
1246 spcr1
= MCBSP_READ(mcbsp
, SPCR1
);
1247 if (attempts
++ > 1000) {
1248 /* We must reset the receiver */
1249 MCBSP_WRITE(mcbsp
, SPCR1
,
1250 MCBSP_READ_CACHE(mcbsp
, SPCR1
) & (~RRST
));
1252 MCBSP_WRITE(mcbsp
, SPCR1
,
1253 MCBSP_READ_CACHE(mcbsp
, SPCR1
) | RRST
);
1255 dev_err(mcbsp
->dev
, "McBSP%d receiver not "
1256 "ready\n", mcbsp
->id
);
1261 /* Receiver is ready, let's read the dummy data */
1262 if (rx_word_length
> OMAP_MCBSP_WORD_16
)
1263 word_msb
= MCBSP_READ(mcbsp
, DRR2
);
1264 word_lsb
= MCBSP_READ(mcbsp
, DRR1
);
1268 EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll
);
1270 int omap_mcbsp_spi_master_recv_word_poll(unsigned int id
, u32
*word
)
1272 struct omap_mcbsp
*mcbsp
;
1274 omap_mcbsp_word_length tx_word_length
;
1275 omap_mcbsp_word_length rx_word_length
;
1276 u16 spcr2
, spcr1
, attempts
= 0, word_lsb
, word_msb
= 0;
1278 if (!omap_mcbsp_check_valid_id(id
)) {
1279 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1283 mcbsp
= id_to_mcbsp_ptr(id
);
1285 tx_word_length
= mcbsp
->tx_word_length
;
1286 rx_word_length
= mcbsp
->rx_word_length
;
1288 if (tx_word_length
!= rx_word_length
)
1291 /* First we wait for the transmitter to be ready */
1292 spcr2
= MCBSP_READ(mcbsp
, SPCR2
);
1293 while (!(spcr2
& XRDY
)) {
1294 spcr2
= MCBSP_READ(mcbsp
, SPCR2
);
1295 if (attempts
++ > 1000) {
1296 /* We must reset the transmitter */
1297 MCBSP_WRITE(mcbsp
, SPCR2
,
1298 MCBSP_READ_CACHE(mcbsp
, SPCR2
) & (~XRST
));
1300 MCBSP_WRITE(mcbsp
, SPCR2
,
1301 MCBSP_READ_CACHE(mcbsp
, SPCR2
) | XRST
);
1303 dev_err(mcbsp
->dev
, "McBSP%d transmitter not "
1304 "ready\n", mcbsp
->id
);
1309 /* We first need to enable the bus clock */
1310 if (tx_word_length
> OMAP_MCBSP_WORD_16
)
1311 MCBSP_WRITE(mcbsp
, DXR2
, clock_word
>> 16);
1312 MCBSP_WRITE(mcbsp
, DXR1
, clock_word
& 0xffff);
1314 /* We wait for the receiver to be ready */
1315 spcr1
= MCBSP_READ(mcbsp
, SPCR1
);
1316 while (!(spcr1
& RRDY
)) {
1317 spcr1
= MCBSP_READ(mcbsp
, SPCR1
);
1318 if (attempts
++ > 1000) {
1319 /* We must reset the receiver */
1320 MCBSP_WRITE(mcbsp
, SPCR1
,
1321 MCBSP_READ_CACHE(mcbsp
, SPCR1
) & (~RRST
));
1323 MCBSP_WRITE(mcbsp
, SPCR1
,
1324 MCBSP_READ_CACHE(mcbsp
, SPCR1
) | RRST
);
1326 dev_err(mcbsp
->dev
, "McBSP%d receiver not "
1327 "ready\n", mcbsp
->id
);
1332 /* Receiver is ready, there is something for us */
1333 if (rx_word_length
> OMAP_MCBSP_WORD_16
)
1334 word_msb
= MCBSP_READ(mcbsp
, DRR2
);
1335 word_lsb
= MCBSP_READ(mcbsp
, DRR1
);
1337 word
[0] = (word_lsb
| (word_msb
<< 16));
1341 EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll
);
1344 * Simple DMA based buffer rx/tx routines.
1345 * Nothing fancy, just a single buffer tx/rx through DMA.
1346 * The DMA resources are released once the transfer is done.
1347 * For anything fancier, you should use your own customized DMA
1348 * routines and callbacks.
1350 int omap_mcbsp_xmit_buffer(unsigned int id
, dma_addr_t buffer
,
1351 unsigned int length
)
1353 struct omap_mcbsp
*mcbsp
;
1359 if (!omap_mcbsp_check_valid_id(id
)) {
1360 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1363 mcbsp
= id_to_mcbsp_ptr(id
);
1365 if (omap_request_dma(mcbsp
->dma_tx_sync
, "McBSP TX",
1366 omap_mcbsp_tx_dma_callback
,
1369 dev_err(mcbsp
->dev
, " Unable to request DMA channel for "
1370 "McBSP%d TX. Trying IRQ based TX\n",
1374 mcbsp
->dma_tx_lch
= dma_tx_ch
;
1376 dev_err(mcbsp
->dev
, "McBSP%d TX DMA on channel %d\n", mcbsp
->id
,
1379 init_completion(&mcbsp
->tx_dma_completion
);
1381 if (cpu_class_is_omap1()) {
1382 src_port
= OMAP_DMA_PORT_TIPB
;
1383 dest_port
= OMAP_DMA_PORT_EMIFF
;
1385 if (cpu_class_is_omap2())
1386 sync_dev
= mcbsp
->dma_tx_sync
;
1388 omap_set_dma_transfer_params(mcbsp
->dma_tx_lch
,
1389 OMAP_DMA_DATA_TYPE_S16
,
1391 OMAP_DMA_SYNC_ELEMENT
,
1394 omap_set_dma_dest_params(mcbsp
->dma_tx_lch
,
1396 OMAP_DMA_AMODE_CONSTANT
,
1397 mcbsp
->phys_base
+ OMAP_MCBSP_REG_DXR1
,
1400 omap_set_dma_src_params(mcbsp
->dma_tx_lch
,
1402 OMAP_DMA_AMODE_POST_INC
,
1406 omap_start_dma(mcbsp
->dma_tx_lch
);
1407 wait_for_completion(&mcbsp
->tx_dma_completion
);
1411 EXPORT_SYMBOL(omap_mcbsp_xmit_buffer
);
1413 int omap_mcbsp_recv_buffer(unsigned int id
, dma_addr_t buffer
,
1414 unsigned int length
)
1416 struct omap_mcbsp
*mcbsp
;
1422 if (!omap_mcbsp_check_valid_id(id
)) {
1423 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1426 mcbsp
= id_to_mcbsp_ptr(id
);
1428 if (omap_request_dma(mcbsp
->dma_rx_sync
, "McBSP RX",
1429 omap_mcbsp_rx_dma_callback
,
1432 dev_err(mcbsp
->dev
, "Unable to request DMA channel for "
1433 "McBSP%d RX. Trying IRQ based RX\n",
1437 mcbsp
->dma_rx_lch
= dma_rx_ch
;
1439 dev_err(mcbsp
->dev
, "McBSP%d RX DMA on channel %d\n", mcbsp
->id
,
1442 init_completion(&mcbsp
->rx_dma_completion
);
1444 if (cpu_class_is_omap1()) {
1445 src_port
= OMAP_DMA_PORT_TIPB
;
1446 dest_port
= OMAP_DMA_PORT_EMIFF
;
1448 if (cpu_class_is_omap2())
1449 sync_dev
= mcbsp
->dma_rx_sync
;
1451 omap_set_dma_transfer_params(mcbsp
->dma_rx_lch
,
1452 OMAP_DMA_DATA_TYPE_S16
,
1454 OMAP_DMA_SYNC_ELEMENT
,
1457 omap_set_dma_src_params(mcbsp
->dma_rx_lch
,
1459 OMAP_DMA_AMODE_CONSTANT
,
1460 mcbsp
->phys_base
+ OMAP_MCBSP_REG_DRR1
,
1463 omap_set_dma_dest_params(mcbsp
->dma_rx_lch
,
1465 OMAP_DMA_AMODE_POST_INC
,
1469 omap_start_dma(mcbsp
->dma_rx_lch
);
1470 wait_for_completion(&mcbsp
->rx_dma_completion
);
1474 EXPORT_SYMBOL(omap_mcbsp_recv_buffer
);
1478 * Since SPI setup is much simpler than the generic McBSP one,
1479 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1480 * Once this is done, you can call omap_mcbsp_start().
1482 void omap_mcbsp_set_spi_mode(unsigned int id
,
1483 const struct omap_mcbsp_spi_cfg
*spi_cfg
)
1485 struct omap_mcbsp
*mcbsp
;
1486 struct omap_mcbsp_reg_cfg mcbsp_cfg
;
1488 if (!omap_mcbsp_check_valid_id(id
)) {
1489 printk(KERN_ERR
"%s: Invalid id (%d)\n", __func__
, id
+ 1);
1492 mcbsp
= id_to_mcbsp_ptr(id
);
1494 memset(&mcbsp_cfg
, 0, sizeof(struct omap_mcbsp_reg_cfg
));
1496 /* SPI has only one frame */
1497 mcbsp_cfg
.rcr1
|= (RWDLEN1(spi_cfg
->word_length
) | RFRLEN1(0));
1498 mcbsp_cfg
.xcr1
|= (XWDLEN1(spi_cfg
->word_length
) | XFRLEN1(0));
1500 /* Clock stop mode */
1501 if (spi_cfg
->clk_stp_mode
== OMAP_MCBSP_CLK_STP_MODE_NO_DELAY
)
1502 mcbsp_cfg
.spcr1
|= (1 << 12);
1504 mcbsp_cfg
.spcr1
|= (3 << 11);
1506 /* Set clock parities */
1507 if (spi_cfg
->rx_clock_polarity
== OMAP_MCBSP_CLK_RISING
)
1508 mcbsp_cfg
.pcr0
|= CLKRP
;
1510 mcbsp_cfg
.pcr0
&= ~CLKRP
;
1512 if (spi_cfg
->tx_clock_polarity
== OMAP_MCBSP_CLK_RISING
)
1513 mcbsp_cfg
.pcr0
&= ~CLKXP
;
1515 mcbsp_cfg
.pcr0
|= CLKXP
;
1517 /* Set SCLKME to 0 and CLKSM to 1 */
1518 mcbsp_cfg
.pcr0
&= ~SCLKME
;
1519 mcbsp_cfg
.srgr2
|= CLKSM
;
1522 if (spi_cfg
->fsx_polarity
== OMAP_MCBSP_FS_ACTIVE_HIGH
)
1523 mcbsp_cfg
.pcr0
&= ~FSXP
;
1525 mcbsp_cfg
.pcr0
|= FSXP
;
1527 if (spi_cfg
->spi_mode
== OMAP_MCBSP_SPI_MASTER
) {
1528 mcbsp_cfg
.pcr0
|= CLKXM
;
1529 mcbsp_cfg
.srgr1
|= CLKGDV(spi_cfg
->clk_div
- 1);
1530 mcbsp_cfg
.pcr0
|= FSXM
;
1531 mcbsp_cfg
.srgr2
&= ~FSGM
;
1532 mcbsp_cfg
.xcr2
|= XDATDLY(1);
1533 mcbsp_cfg
.rcr2
|= RDATDLY(1);
1535 mcbsp_cfg
.pcr0
&= ~CLKXM
;
1536 mcbsp_cfg
.srgr1
|= CLKGDV(1);
1537 mcbsp_cfg
.pcr0
&= ~FSXM
;
1538 mcbsp_cfg
.xcr2
&= ~XDATDLY(3);
1539 mcbsp_cfg
.rcr2
&= ~RDATDLY(3);
1542 mcbsp_cfg
.xcr2
&= ~XPHASE
;
1543 mcbsp_cfg
.rcr2
&= ~RPHASE
;
1545 omap_mcbsp_config(id
, &mcbsp_cfg
);
1547 EXPORT_SYMBOL(omap_mcbsp_set_spi_mode
);
1549 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
1550 #define max_thres(m) (mcbsp->pdata->buffer_size)
1551 #define valid_threshold(m, val) ((val) <= max_thres(m))
1552 #define THRESHOLD_PROP_BUILDER(prop) \
1553 static ssize_t prop##_show(struct device *dev, \
1554 struct device_attribute *attr, char *buf) \
1556 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1558 return sprintf(buf, "%u\n", mcbsp->prop); \
1561 static ssize_t prop##_store(struct device *dev, \
1562 struct device_attribute *attr, \
1563 const char *buf, size_t size) \
1565 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1566 unsigned long val; \
1569 status = strict_strtoul(buf, 0, &val); \
1573 if (!valid_threshold(mcbsp, val)) \
1576 mcbsp->prop = val; \
1580 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1582 THRESHOLD_PROP_BUILDER(max_tx_thres
);
1583 THRESHOLD_PROP_BUILDER(max_rx_thres
);
1585 static const char *dma_op_modes
[] = {
1586 "element", "threshold", "frame",
1589 static ssize_t
dma_op_mode_show(struct device
*dev
,
1590 struct device_attribute
*attr
, char *buf
)
1592 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
1593 int dma_op_mode
, i
= 0;
1595 const char * const *s
;
1597 dma_op_mode
= mcbsp
->dma_op_mode
;
1599 for (s
= &dma_op_modes
[i
]; i
< ARRAY_SIZE(dma_op_modes
); s
++, i
++) {
1600 if (dma_op_mode
== i
)
1601 len
+= sprintf(buf
+ len
, "[%s] ", *s
);
1603 len
+= sprintf(buf
+ len
, "%s ", *s
);
1605 len
+= sprintf(buf
+ len
, "\n");
1610 static ssize_t
dma_op_mode_store(struct device
*dev
,
1611 struct device_attribute
*attr
,
1612 const char *buf
, size_t size
)
1614 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
1615 const char * const *s
;
1618 for (s
= &dma_op_modes
[i
]; i
< ARRAY_SIZE(dma_op_modes
); s
++, i
++)
1619 if (sysfs_streq(buf
, *s
))
1622 if (i
== ARRAY_SIZE(dma_op_modes
))
1625 spin_lock_irq(&mcbsp
->lock
);
1630 mcbsp
->dma_op_mode
= i
;
1633 spin_unlock_irq(&mcbsp
->lock
);
1638 static DEVICE_ATTR(dma_op_mode
, 0644, dma_op_mode_show
, dma_op_mode_store
);
1640 static const struct attribute
*additional_attrs
[] = {
1641 &dev_attr_max_tx_thres
.attr
,
1642 &dev_attr_max_rx_thres
.attr
,
1643 &dev_attr_dma_op_mode
.attr
,
1647 static const struct attribute_group additional_attr_group
= {
1648 .attrs
= (struct attribute
**)additional_attrs
,
1651 static inline int __devinit
omap_additional_add(struct device
*dev
)
1653 return sysfs_create_group(&dev
->kobj
, &additional_attr_group
);
1656 static inline void __devexit
omap_additional_remove(struct device
*dev
)
1658 sysfs_remove_group(&dev
->kobj
, &additional_attr_group
);
1662 #ifdef CONFIG_ARCH_OMAP3
1663 static ssize_t
st_taps_show(struct device
*dev
,
1664 struct device_attribute
*attr
, char *buf
)
1666 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
1667 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
1671 spin_lock_irq(&mcbsp
->lock
);
1672 for (i
= 0; i
< st_data
->nr_taps
; i
++)
1673 status
+= sprintf(&buf
[status
], (i
? ", %d" : "%d"),
1676 status
+= sprintf(&buf
[status
], "\n");
1677 spin_unlock_irq(&mcbsp
->lock
);
1682 static ssize_t
st_taps_store(struct device
*dev
,
1683 struct device_attribute
*attr
,
1684 const char *buf
, size_t size
)
1686 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
1687 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
1688 int val
, tmp
, status
, i
= 0;
1690 spin_lock_irq(&mcbsp
->lock
);
1691 memset(st_data
->taps
, 0, sizeof(st_data
->taps
));
1692 st_data
->nr_taps
= 0;
1695 status
= sscanf(buf
, "%d%n", &val
, &tmp
);
1696 if (status
< 0 || status
== 0) {
1700 if (val
< -32768 || val
> 32767) {
1704 st_data
->taps
[i
++] = val
;
1711 st_data
->nr_taps
= i
;
1714 spin_unlock_irq(&mcbsp
->lock
);
1719 static DEVICE_ATTR(st_taps
, 0644, st_taps_show
, st_taps_store
);
1721 static const struct attribute
*sidetone_attrs
[] = {
1722 &dev_attr_st_taps
.attr
,
1726 static const struct attribute_group sidetone_attr_group
= {
1727 .attrs
= (struct attribute
**)sidetone_attrs
,
1730 static int __devinit
omap_st_add(struct omap_mcbsp
*mcbsp
)
1732 struct platform_device
*pdev
;
1733 struct resource
*res
;
1734 struct omap_mcbsp_st_data
*st_data
;
1737 st_data
= kzalloc(sizeof(*mcbsp
->st_data
), GFP_KERNEL
);
1743 pdev
= container_of(mcbsp
->dev
, struct platform_device
, dev
);
1745 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "sidetone");
1746 st_data
->io_base_st
= ioremap(res
->start
, resource_size(res
));
1747 if (!st_data
->io_base_st
) {
1752 err
= sysfs_create_group(&mcbsp
->dev
->kobj
, &sidetone_attr_group
);
1756 mcbsp
->st_data
= st_data
;
1760 iounmap(st_data
->io_base_st
);
1768 static void __devexit
omap_st_remove(struct omap_mcbsp
*mcbsp
)
1770 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
1773 sysfs_remove_group(&mcbsp
->dev
->kobj
, &sidetone_attr_group
);
1774 iounmap(st_data
->io_base_st
);
1779 static inline int __devinit
omap_st_add(struct omap_mcbsp
*mcbsp
) { return 0; }
1780 static inline void __devexit
omap_st_remove(struct omap_mcbsp
*mcbsp
) {}
1783 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
1784 static inline void __devinit
omap34xx_device_init(struct omap_mcbsp
*mcbsp
)
1786 mcbsp
->dma_op_mode
= MCBSP_DMA_MODE_ELEMENT
;
1787 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
1789 * Initially configure the maximum thresholds to a safe value.
1790 * The McBSP FIFO usage with these values should not go under
1792 * If the whole FIFO without safety buffer is used, than there
1793 * is a possibility that the DMA will be not able to push the
1794 * new data on time, causing channel shifts in runtime.
1796 mcbsp
->max_tx_thres
= max_thres(mcbsp
) - 0x10;
1797 mcbsp
->max_rx_thres
= max_thres(mcbsp
) - 0x10;
1799 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1800 * for mcbsp2 instances.
1802 if (omap_additional_add(mcbsp
->dev
))
1803 dev_warn(mcbsp
->dev
,
1804 "Unable to create additional controls\n");
1806 mcbsp
->max_tx_thres
= -EINVAL
;
1807 mcbsp
->max_rx_thres
= -EINVAL
;
1810 if (cpu_is_omap34xx()) {
1811 if (mcbsp
->id
== 2 || mcbsp
->id
== 3)
1812 if (omap_st_add(mcbsp
))
1813 dev_warn(mcbsp
->dev
,
1814 "Unable to create sidetone controls\n");
1818 static inline void __devexit
omap34xx_device_exit(struct omap_mcbsp
*mcbsp
)
1820 if (cpu_is_omap34xx() || cpu_is_omap44xx())
1821 omap_additional_remove(mcbsp
->dev
);
1822 if (cpu_is_omap34xx())
1823 if (mcbsp
->id
== 2 || mcbsp
->id
== 3)
1824 omap_st_remove(mcbsp
);
1827 static inline void __devinit
omap34xx_device_init(struct omap_mcbsp
*mcbsp
) {}
1828 static inline void __devexit
omap34xx_device_exit(struct omap_mcbsp
*mcbsp
) {}
1829 #endif /* CONFIG_ARCH_OMAP3 */
1832 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1833 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1835 static int __devinit
omap_mcbsp_probe(struct platform_device
*pdev
)
1837 struct omap_mcbsp_platform_data
*pdata
= pdev
->dev
.platform_data
;
1838 struct omap_mcbsp
*mcbsp
;
1839 int id
= pdev
->id
- 1;
1840 struct resource
*res
;
1844 dev_err(&pdev
->dev
, "McBSP device initialized without"
1850 dev_dbg(&pdev
->dev
, "Initializing OMAP McBSP (%d).\n", pdev
->id
);
1852 if (id
>= omap_mcbsp_count
) {
1853 dev_err(&pdev
->dev
, "Invalid McBSP device id (%d)\n", id
);
1858 mcbsp
= kzalloc(sizeof(struct omap_mcbsp
), GFP_KERNEL
);
1864 spin_lock_init(&mcbsp
->lock
);
1867 mcbsp
->dma_tx_lch
= -1;
1868 mcbsp
->dma_rx_lch
= -1;
1870 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
1872 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1874 dev_err(&pdev
->dev
, "%s:mcbsp%d has invalid memory"
1875 "resource\n", __func__
, pdev
->id
);
1880 mcbsp
->phys_base
= res
->start
;
1881 omap_mcbsp_cache_size
= resource_size(res
);
1882 mcbsp
->io_base
= ioremap(res
->start
, resource_size(res
));
1883 if (!mcbsp
->io_base
) {
1888 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dma");
1890 mcbsp
->phys_dma_base
= mcbsp
->phys_base
;
1892 mcbsp
->phys_dma_base
= res
->start
;
1894 /* Default I/O is IRQ based */
1895 mcbsp
->io_type
= OMAP_MCBSP_IRQ_IO
;
1897 mcbsp
->tx_irq
= platform_get_irq_byname(pdev
, "tx");
1898 mcbsp
->rx_irq
= platform_get_irq_byname(pdev
, "rx");
1900 /* From OMAP4 there will be a single irq line */
1901 if (mcbsp
->tx_irq
== -ENXIO
)
1902 mcbsp
->tx_irq
= platform_get_irq(pdev
, 0);
1904 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "rx");
1906 dev_err(&pdev
->dev
, "%s:mcbsp%d has invalid rx DMA channel\n",
1907 __func__
, pdev
->id
);
1911 mcbsp
->dma_rx_sync
= res
->start
;
1913 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "tx");
1915 dev_err(&pdev
->dev
, "%s:mcbsp%d has invalid tx DMA channel\n",
1916 __func__
, pdev
->id
);
1920 mcbsp
->dma_tx_sync
= res
->start
;
1922 mcbsp
->fclk
= clk_get(&pdev
->dev
, "fck");
1923 if (IS_ERR(mcbsp
->fclk
)) {
1924 ret
= PTR_ERR(mcbsp
->fclk
);
1925 dev_err(&pdev
->dev
, "unable to get fck: %d\n", ret
);
1929 mcbsp
->pdata
= pdata
;
1930 mcbsp
->dev
= &pdev
->dev
;
1931 mcbsp_ptr
[id
] = mcbsp
;
1932 mcbsp
->mcbsp_config_type
= pdata
->mcbsp_config_type
;
1933 platform_set_drvdata(pdev
, mcbsp
);
1934 pm_runtime_enable(mcbsp
->dev
);
1936 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1937 omap34xx_device_init(mcbsp
);
1942 iounmap(mcbsp
->io_base
);
1949 static int __devexit
omap_mcbsp_remove(struct platform_device
*pdev
)
1951 struct omap_mcbsp
*mcbsp
= platform_get_drvdata(pdev
);
1953 platform_set_drvdata(pdev
, NULL
);
1956 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&&
1957 mcbsp
->pdata
->ops
->free
)
1958 mcbsp
->pdata
->ops
->free(mcbsp
->id
);
1960 omap34xx_device_exit(mcbsp
);
1962 clk_put(mcbsp
->fclk
);
1964 iounmap(mcbsp
->io_base
);
1971 static struct platform_driver omap_mcbsp_driver
= {
1972 .probe
= omap_mcbsp_probe
,
1973 .remove
= __devexit_p(omap_mcbsp_remove
),
1975 .name
= "omap-mcbsp",
1979 int __init
omap_mcbsp_init(void)
1981 /* Register the McBSP driver */
1982 return platform_driver_register(&omap_mcbsp_driver
);