2 * sound/soc/omap/mcbsp.c
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
7 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
8 * Peter Ujfalusi <peter.ujfalusi@ti.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * Multichannel mode not supported.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.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>
27 #include <linux/pm_runtime.h>
29 #include <linux/platform_data/asoc-ti-mcbsp.h>
33 static void omap_mcbsp_write(struct omap_mcbsp
*mcbsp
, u16 reg
, u32 val
)
35 void __iomem
*addr
= mcbsp
->io_base
+ reg
* mcbsp
->pdata
->reg_step
;
37 if (mcbsp
->pdata
->reg_size
== 2) {
38 ((u16
*)mcbsp
->reg_cache
)[reg
] = (u16
)val
;
39 __raw_writew((u16
)val
, addr
);
41 ((u32
*)mcbsp
->reg_cache
)[reg
] = val
;
42 __raw_writel(val
, addr
);
46 static int omap_mcbsp_read(struct omap_mcbsp
*mcbsp
, u16 reg
, bool from_cache
)
48 void __iomem
*addr
= mcbsp
->io_base
+ reg
* mcbsp
->pdata
->reg_step
;
50 if (mcbsp
->pdata
->reg_size
== 2) {
51 return !from_cache
? __raw_readw(addr
) :
52 ((u16
*)mcbsp
->reg_cache
)[reg
];
54 return !from_cache
? __raw_readl(addr
) :
55 ((u32
*)mcbsp
->reg_cache
)[reg
];
59 static void omap_mcbsp_st_write(struct omap_mcbsp
*mcbsp
, u16 reg
, u32 val
)
61 __raw_writel(val
, mcbsp
->st_data
->io_base_st
+ reg
);
64 static int omap_mcbsp_st_read(struct omap_mcbsp
*mcbsp
, u16 reg
)
66 return __raw_readl(mcbsp
->st_data
->io_base_st
+ reg
);
69 #define MCBSP_READ(mcbsp, reg) \
70 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
71 #define MCBSP_WRITE(mcbsp, reg, val) \
72 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
73 #define MCBSP_READ_CACHE(mcbsp, reg) \
74 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
76 #define MCBSP_ST_READ(mcbsp, reg) \
77 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
78 #define MCBSP_ST_WRITE(mcbsp, reg, val) \
79 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
81 static void omap_mcbsp_dump_reg(struct omap_mcbsp
*mcbsp
)
83 dev_dbg(mcbsp
->dev
, "**** McBSP%d regs ****\n", mcbsp
->id
);
84 dev_dbg(mcbsp
->dev
, "DRR2: 0x%04x\n",
85 MCBSP_READ(mcbsp
, DRR2
));
86 dev_dbg(mcbsp
->dev
, "DRR1: 0x%04x\n",
87 MCBSP_READ(mcbsp
, DRR1
));
88 dev_dbg(mcbsp
->dev
, "DXR2: 0x%04x\n",
89 MCBSP_READ(mcbsp
, DXR2
));
90 dev_dbg(mcbsp
->dev
, "DXR1: 0x%04x\n",
91 MCBSP_READ(mcbsp
, DXR1
));
92 dev_dbg(mcbsp
->dev
, "SPCR2: 0x%04x\n",
93 MCBSP_READ(mcbsp
, SPCR2
));
94 dev_dbg(mcbsp
->dev
, "SPCR1: 0x%04x\n",
95 MCBSP_READ(mcbsp
, SPCR1
));
96 dev_dbg(mcbsp
->dev
, "RCR2: 0x%04x\n",
97 MCBSP_READ(mcbsp
, RCR2
));
98 dev_dbg(mcbsp
->dev
, "RCR1: 0x%04x\n",
99 MCBSP_READ(mcbsp
, RCR1
));
100 dev_dbg(mcbsp
->dev
, "XCR2: 0x%04x\n",
101 MCBSP_READ(mcbsp
, XCR2
));
102 dev_dbg(mcbsp
->dev
, "XCR1: 0x%04x\n",
103 MCBSP_READ(mcbsp
, XCR1
));
104 dev_dbg(mcbsp
->dev
, "SRGR2: 0x%04x\n",
105 MCBSP_READ(mcbsp
, SRGR2
));
106 dev_dbg(mcbsp
->dev
, "SRGR1: 0x%04x\n",
107 MCBSP_READ(mcbsp
, SRGR1
));
108 dev_dbg(mcbsp
->dev
, "PCR0: 0x%04x\n",
109 MCBSP_READ(mcbsp
, PCR0
));
110 dev_dbg(mcbsp
->dev
, "***********************\n");
113 static irqreturn_t
omap_mcbsp_irq_handler(int irq
, void *dev_id
)
115 struct omap_mcbsp
*mcbsp
= dev_id
;
118 irqst
= MCBSP_READ(mcbsp
, IRQST
);
119 dev_dbg(mcbsp
->dev
, "IRQ callback : 0x%x\n", irqst
);
121 if (irqst
& RSYNCERREN
)
122 dev_err(mcbsp
->dev
, "RX Frame Sync Error!\n");
124 dev_dbg(mcbsp
->dev
, "RX Frame Sync\n");
126 dev_dbg(mcbsp
->dev
, "RX End Of Frame\n");
128 dev_dbg(mcbsp
->dev
, "RX Buffer Threshold Reached\n");
129 if (irqst
& RUNDFLEN
)
130 dev_err(mcbsp
->dev
, "RX Buffer Underflow!\n");
132 dev_err(mcbsp
->dev
, "RX Buffer Overflow!\n");
134 if (irqst
& XSYNCERREN
)
135 dev_err(mcbsp
->dev
, "TX Frame Sync Error!\n");
137 dev_dbg(mcbsp
->dev
, "TX Frame Sync\n");
139 dev_dbg(mcbsp
->dev
, "TX End Of Frame\n");
141 dev_dbg(mcbsp
->dev
, "TX Buffer threshold Reached\n");
142 if (irqst
& XUNDFLEN
)
143 dev_err(mcbsp
->dev
, "TX Buffer Underflow!\n");
145 dev_err(mcbsp
->dev
, "TX Buffer Overflow!\n");
146 if (irqst
& XEMPTYEOFEN
)
147 dev_dbg(mcbsp
->dev
, "TX Buffer empty at end of frame\n");
149 MCBSP_WRITE(mcbsp
, IRQST
, irqst
);
154 static irqreturn_t
omap_mcbsp_tx_irq_handler(int irq
, void *dev_id
)
156 struct omap_mcbsp
*mcbsp_tx
= dev_id
;
159 irqst_spcr2
= MCBSP_READ(mcbsp_tx
, SPCR2
);
160 dev_dbg(mcbsp_tx
->dev
, "TX IRQ callback : 0x%x\n", irqst_spcr2
);
162 if (irqst_spcr2
& XSYNC_ERR
) {
163 dev_err(mcbsp_tx
->dev
, "TX Frame Sync Error! : 0x%x\n",
165 /* Writing zero to XSYNC_ERR clears the IRQ */
166 MCBSP_WRITE(mcbsp_tx
, SPCR2
, MCBSP_READ_CACHE(mcbsp_tx
, SPCR2
));
172 static irqreturn_t
omap_mcbsp_rx_irq_handler(int irq
, void *dev_id
)
174 struct omap_mcbsp
*mcbsp_rx
= dev_id
;
177 irqst_spcr1
= MCBSP_READ(mcbsp_rx
, SPCR1
);
178 dev_dbg(mcbsp_rx
->dev
, "RX IRQ callback : 0x%x\n", irqst_spcr1
);
180 if (irqst_spcr1
& RSYNC_ERR
) {
181 dev_err(mcbsp_rx
->dev
, "RX Frame Sync Error! : 0x%x\n",
183 /* Writing zero to RSYNC_ERR clears the IRQ */
184 MCBSP_WRITE(mcbsp_rx
, SPCR1
, MCBSP_READ_CACHE(mcbsp_rx
, SPCR1
));
191 * omap_mcbsp_config simply write a config to the
193 * You either call this function or set the McBSP registers
194 * by yourself before calling omap_mcbsp_start().
196 void omap_mcbsp_config(struct omap_mcbsp
*mcbsp
,
197 const struct omap_mcbsp_reg_cfg
*config
)
199 dev_dbg(mcbsp
->dev
, "Configuring McBSP%d phys_base: 0x%08lx\n",
200 mcbsp
->id
, mcbsp
->phys_base
);
202 /* We write the given config */
203 MCBSP_WRITE(mcbsp
, SPCR2
, config
->spcr2
);
204 MCBSP_WRITE(mcbsp
, SPCR1
, config
->spcr1
);
205 MCBSP_WRITE(mcbsp
, RCR2
, config
->rcr2
);
206 MCBSP_WRITE(mcbsp
, RCR1
, config
->rcr1
);
207 MCBSP_WRITE(mcbsp
, XCR2
, config
->xcr2
);
208 MCBSP_WRITE(mcbsp
, XCR1
, config
->xcr1
);
209 MCBSP_WRITE(mcbsp
, SRGR2
, config
->srgr2
);
210 MCBSP_WRITE(mcbsp
, SRGR1
, config
->srgr1
);
211 MCBSP_WRITE(mcbsp
, MCR2
, config
->mcr2
);
212 MCBSP_WRITE(mcbsp
, MCR1
, config
->mcr1
);
213 MCBSP_WRITE(mcbsp
, PCR0
, config
->pcr0
);
214 if (mcbsp
->pdata
->has_ccr
) {
215 MCBSP_WRITE(mcbsp
, XCCR
, config
->xccr
);
216 MCBSP_WRITE(mcbsp
, RCCR
, config
->rccr
);
218 /* Enable wakeup behavior */
219 if (mcbsp
->pdata
->has_wakeup
)
220 MCBSP_WRITE(mcbsp
, WAKEUPEN
, XRDYEN
| RRDYEN
);
222 /* Enable TX/RX sync error interrupts by default */
224 MCBSP_WRITE(mcbsp
, IRQEN
, RSYNCERREN
| XSYNCERREN
);
228 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
230 * @stream - indicates the direction of data flow (rx or tx)
232 * Returns the address of mcbsp data transmit register or data receive register
233 * to be used by DMA for transferring/receiving data based on the value of
234 * @stream for the requested mcbsp given by @id
236 static int omap_mcbsp_dma_reg_params(struct omap_mcbsp
*mcbsp
,
241 if (mcbsp
->pdata
->reg_size
== 2) {
243 data_reg
= OMAP_MCBSP_REG_DRR1
;
245 data_reg
= OMAP_MCBSP_REG_DXR1
;
248 data_reg
= OMAP_MCBSP_REG_DRR
;
250 data_reg
= OMAP_MCBSP_REG_DXR
;
253 return mcbsp
->phys_dma_base
+ data_reg
* mcbsp
->pdata
->reg_step
;
256 static void omap_st_on(struct omap_mcbsp
*mcbsp
)
260 if (mcbsp
->pdata
->enable_st_clock
)
261 mcbsp
->pdata
->enable_st_clock(mcbsp
->id
, 1);
263 /* Enable McBSP Sidetone */
264 w
= MCBSP_READ(mcbsp
, SSELCR
);
265 MCBSP_WRITE(mcbsp
, SSELCR
, w
| SIDETONEEN
);
267 /* Enable Sidetone from Sidetone Core */
268 w
= MCBSP_ST_READ(mcbsp
, SSELCR
);
269 MCBSP_ST_WRITE(mcbsp
, SSELCR
, w
| ST_SIDETONEEN
);
272 static void omap_st_off(struct omap_mcbsp
*mcbsp
)
276 w
= MCBSP_ST_READ(mcbsp
, SSELCR
);
277 MCBSP_ST_WRITE(mcbsp
, SSELCR
, w
& ~(ST_SIDETONEEN
));
279 w
= MCBSP_READ(mcbsp
, SSELCR
);
280 MCBSP_WRITE(mcbsp
, SSELCR
, w
& ~(SIDETONEEN
));
282 if (mcbsp
->pdata
->enable_st_clock
)
283 mcbsp
->pdata
->enable_st_clock(mcbsp
->id
, 0);
286 static void omap_st_fir_write(struct omap_mcbsp
*mcbsp
, s16
*fir
)
290 val
= MCBSP_ST_READ(mcbsp
, SSELCR
);
292 if (val
& ST_COEFFWREN
)
293 MCBSP_ST_WRITE(mcbsp
, SSELCR
, val
& ~(ST_COEFFWREN
));
295 MCBSP_ST_WRITE(mcbsp
, SSELCR
, val
| ST_COEFFWREN
);
297 for (i
= 0; i
< 128; i
++)
298 MCBSP_ST_WRITE(mcbsp
, SFIRCR
, fir
[i
]);
302 val
= MCBSP_ST_READ(mcbsp
, SSELCR
);
303 while (!(val
& ST_COEFFWRDONE
) && (++i
< 1000))
304 val
= MCBSP_ST_READ(mcbsp
, SSELCR
);
306 MCBSP_ST_WRITE(mcbsp
, SSELCR
, val
& ~(ST_COEFFWREN
));
309 dev_err(mcbsp
->dev
, "McBSP FIR load error!\n");
312 static void omap_st_chgain(struct omap_mcbsp
*mcbsp
)
315 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
317 w
= MCBSP_ST_READ(mcbsp
, SSELCR
);
319 MCBSP_ST_WRITE(mcbsp
, SGAINCR
, ST_CH0GAIN(st_data
->ch0gain
) | \
320 ST_CH1GAIN(st_data
->ch1gain
));
323 int omap_st_set_chgain(struct omap_mcbsp
*mcbsp
, int channel
, s16 chgain
)
325 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
331 spin_lock_irq(&mcbsp
->lock
);
333 st_data
->ch0gain
= chgain
;
334 else if (channel
== 1)
335 st_data
->ch1gain
= chgain
;
339 if (st_data
->enabled
)
340 omap_st_chgain(mcbsp
);
341 spin_unlock_irq(&mcbsp
->lock
);
346 int omap_st_get_chgain(struct omap_mcbsp
*mcbsp
, int channel
, s16
*chgain
)
348 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
354 spin_lock_irq(&mcbsp
->lock
);
356 *chgain
= st_data
->ch0gain
;
357 else if (channel
== 1)
358 *chgain
= st_data
->ch1gain
;
361 spin_unlock_irq(&mcbsp
->lock
);
366 static int omap_st_start(struct omap_mcbsp
*mcbsp
)
368 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
370 if (st_data
->enabled
&& !st_data
->running
) {
371 omap_st_fir_write(mcbsp
, st_data
->taps
);
372 omap_st_chgain(mcbsp
);
376 st_data
->running
= 1;
383 int omap_st_enable(struct omap_mcbsp
*mcbsp
)
385 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
390 spin_lock_irq(&mcbsp
->lock
);
391 st_data
->enabled
= 1;
392 omap_st_start(mcbsp
);
393 spin_unlock_irq(&mcbsp
->lock
);
398 static int omap_st_stop(struct omap_mcbsp
*mcbsp
)
400 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
402 if (st_data
->running
) {
405 st_data
->running
= 0;
412 int omap_st_disable(struct omap_mcbsp
*mcbsp
)
414 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
420 spin_lock_irq(&mcbsp
->lock
);
422 st_data
->enabled
= 0;
423 spin_unlock_irq(&mcbsp
->lock
);
428 int omap_st_is_enabled(struct omap_mcbsp
*mcbsp
)
430 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
435 return st_data
->enabled
;
439 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
440 * The threshold parameter is 1 based, and it is converted (threshold - 1)
441 * for the THRSH2 register.
443 void omap_mcbsp_set_tx_threshold(struct omap_mcbsp
*mcbsp
, u16 threshold
)
445 if (mcbsp
->pdata
->buffer_size
== 0)
448 if (threshold
&& threshold
<= mcbsp
->max_tx_thres
)
449 MCBSP_WRITE(mcbsp
, THRSH2
, threshold
- 1);
453 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
454 * The threshold parameter is 1 based, and it is converted (threshold - 1)
455 * for the THRSH1 register.
457 void omap_mcbsp_set_rx_threshold(struct omap_mcbsp
*mcbsp
, u16 threshold
)
459 if (mcbsp
->pdata
->buffer_size
== 0)
462 if (threshold
&& threshold
<= mcbsp
->max_rx_thres
)
463 MCBSP_WRITE(mcbsp
, THRSH1
, threshold
- 1);
467 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
469 u16
omap_mcbsp_get_tx_delay(struct omap_mcbsp
*mcbsp
)
473 if (mcbsp
->pdata
->buffer_size
== 0)
476 /* Returns the number of free locations in the buffer */
477 buffstat
= MCBSP_READ(mcbsp
, XBUFFSTAT
);
479 /* Number of slots are different in McBSP ports */
480 return mcbsp
->pdata
->buffer_size
- buffstat
;
484 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
485 * to reach the threshold value (when the DMA will be triggered to read it)
487 u16
omap_mcbsp_get_rx_delay(struct omap_mcbsp
*mcbsp
)
489 u16 buffstat
, threshold
;
491 if (mcbsp
->pdata
->buffer_size
== 0)
494 /* Returns the number of used locations in the buffer */
495 buffstat
= MCBSP_READ(mcbsp
, RBUFFSTAT
);
497 threshold
= MCBSP_READ(mcbsp
, THRSH1
);
499 /* Return the number of location till we reach the threshold limit */
500 if (threshold
<= buffstat
)
503 return threshold
- buffstat
;
506 int omap_mcbsp_request(struct omap_mcbsp
*mcbsp
)
511 reg_cache
= kzalloc(mcbsp
->reg_cache_size
, GFP_KERNEL
);
516 spin_lock(&mcbsp
->lock
);
518 dev_err(mcbsp
->dev
, "McBSP%d is currently in use\n",
525 mcbsp
->reg_cache
= reg_cache
;
526 spin_unlock(&mcbsp
->lock
);
528 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&& mcbsp
->pdata
->ops
->request
)
529 mcbsp
->pdata
->ops
->request(mcbsp
->id
- 1);
532 * Make sure that transmitter, receiver and sample-rate generator are
533 * not running before activating IRQs.
535 MCBSP_WRITE(mcbsp
, SPCR1
, 0);
536 MCBSP_WRITE(mcbsp
, SPCR2
, 0);
539 err
= request_irq(mcbsp
->irq
, omap_mcbsp_irq_handler
, 0,
540 "McBSP", (void *)mcbsp
);
542 dev_err(mcbsp
->dev
, "Unable to request IRQ\n");
543 goto err_clk_disable
;
546 err
= request_irq(mcbsp
->tx_irq
, omap_mcbsp_tx_irq_handler
, 0,
547 "McBSP TX", (void *)mcbsp
);
549 dev_err(mcbsp
->dev
, "Unable to request TX IRQ\n");
550 goto err_clk_disable
;
553 err
= request_irq(mcbsp
->rx_irq
, omap_mcbsp_rx_irq_handler
, 0,
554 "McBSP RX", (void *)mcbsp
);
556 dev_err(mcbsp
->dev
, "Unable to request RX IRQ\n");
563 free_irq(mcbsp
->tx_irq
, (void *)mcbsp
);
565 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&& mcbsp
->pdata
->ops
->free
)
566 mcbsp
->pdata
->ops
->free(mcbsp
->id
- 1);
568 /* Disable wakeup behavior */
569 if (mcbsp
->pdata
->has_wakeup
)
570 MCBSP_WRITE(mcbsp
, WAKEUPEN
, 0);
572 spin_lock(&mcbsp
->lock
);
574 mcbsp
->reg_cache
= NULL
;
576 spin_unlock(&mcbsp
->lock
);
582 void omap_mcbsp_free(struct omap_mcbsp
*mcbsp
)
586 if (mcbsp
->pdata
&& mcbsp
->pdata
->ops
&& mcbsp
->pdata
->ops
->free
)
587 mcbsp
->pdata
->ops
->free(mcbsp
->id
- 1);
589 /* Disable wakeup behavior */
590 if (mcbsp
->pdata
->has_wakeup
)
591 MCBSP_WRITE(mcbsp
, WAKEUPEN
, 0);
593 /* Disable interrupt requests */
595 MCBSP_WRITE(mcbsp
, IRQEN
, 0);
598 free_irq(mcbsp
->irq
, (void *)mcbsp
);
600 free_irq(mcbsp
->rx_irq
, (void *)mcbsp
);
601 free_irq(mcbsp
->tx_irq
, (void *)mcbsp
);
604 reg_cache
= mcbsp
->reg_cache
;
607 * Select CLKS source from internal source unconditionally before
608 * marking the McBSP port as free.
609 * If the external clock source via MCBSP_CLKS pin has been selected the
610 * system will refuse to enter idle if the CLKS pin source is not reset
611 * back to internal source.
614 omap2_mcbsp_set_clks_src(mcbsp
, MCBSP_CLKS_PRCM_SRC
);
616 spin_lock(&mcbsp
->lock
);
618 dev_err(mcbsp
->dev
, "McBSP%d was not reserved\n", mcbsp
->id
);
621 mcbsp
->reg_cache
= NULL
;
622 spin_unlock(&mcbsp
->lock
);
629 * Here we start the McBSP, by enabling transmitter, receiver or both.
630 * If no transmitter or receiver is active prior calling, then sample-rate
631 * generator and frame sync are started.
633 void omap_mcbsp_start(struct omap_mcbsp
*mcbsp
, int tx
, int rx
)
639 omap_st_start(mcbsp
);
641 /* Only enable SRG, if McBSP is master */
642 w
= MCBSP_READ_CACHE(mcbsp
, PCR0
);
643 if (w
& (FSXM
| FSRM
| CLKXM
| CLKRM
))
644 enable_srg
= !((MCBSP_READ_CACHE(mcbsp
, SPCR2
) |
645 MCBSP_READ_CACHE(mcbsp
, SPCR1
)) & 1);
648 /* Start the sample generator */
649 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
650 MCBSP_WRITE(mcbsp
, SPCR2
, w
| (1 << 6));
653 /* Enable transmitter and receiver */
655 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
656 MCBSP_WRITE(mcbsp
, SPCR2
, w
| tx
);
659 w
= MCBSP_READ_CACHE(mcbsp
, SPCR1
);
660 MCBSP_WRITE(mcbsp
, SPCR1
, w
| rx
);
663 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
664 * REVISIT: 100us may give enough time for two CLKSRG, however
665 * due to some unknown PM related, clock gating etc. reason it
671 /* Start frame sync */
672 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
673 MCBSP_WRITE(mcbsp
, SPCR2
, w
| (1 << 7));
676 if (mcbsp
->pdata
->has_ccr
) {
677 /* Release the transmitter and receiver */
678 w
= MCBSP_READ_CACHE(mcbsp
, XCCR
);
679 w
&= ~(tx
? XDISABLE
: 0);
680 MCBSP_WRITE(mcbsp
, XCCR
, w
);
681 w
= MCBSP_READ_CACHE(mcbsp
, RCCR
);
682 w
&= ~(rx
? RDISABLE
: 0);
683 MCBSP_WRITE(mcbsp
, RCCR
, w
);
686 /* Dump McBSP Regs */
687 omap_mcbsp_dump_reg(mcbsp
);
690 void omap_mcbsp_stop(struct omap_mcbsp
*mcbsp
, int tx
, int rx
)
695 /* Reset transmitter */
697 if (mcbsp
->pdata
->has_ccr
) {
698 w
= MCBSP_READ_CACHE(mcbsp
, XCCR
);
699 w
|= (tx
? XDISABLE
: 0);
700 MCBSP_WRITE(mcbsp
, XCCR
, w
);
702 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
703 MCBSP_WRITE(mcbsp
, SPCR2
, w
& ~tx
);
707 if (mcbsp
->pdata
->has_ccr
) {
708 w
= MCBSP_READ_CACHE(mcbsp
, RCCR
);
709 w
|= (rx
? RDISABLE
: 0);
710 MCBSP_WRITE(mcbsp
, RCCR
, w
);
712 w
= MCBSP_READ_CACHE(mcbsp
, SPCR1
);
713 MCBSP_WRITE(mcbsp
, SPCR1
, w
& ~rx
);
715 idle
= !((MCBSP_READ_CACHE(mcbsp
, SPCR2
) |
716 MCBSP_READ_CACHE(mcbsp
, SPCR1
)) & 1);
719 /* Reset the sample rate generator */
720 w
= MCBSP_READ_CACHE(mcbsp
, SPCR2
);
721 MCBSP_WRITE(mcbsp
, SPCR2
, w
& ~(1 << 6));
728 int omap2_mcbsp_set_clks_src(struct omap_mcbsp
*mcbsp
, u8 fck_src_id
)
734 if (fck_src_id
== MCBSP_CLKS_PAD_SRC
)
736 else if (fck_src_id
== MCBSP_CLKS_PRCM_SRC
)
741 fck_src
= clk_get(mcbsp
->dev
, src
);
742 if (IS_ERR(fck_src
)) {
743 dev_err(mcbsp
->dev
, "CLKS: could not clk_get() %s\n", src
);
747 pm_runtime_put_sync(mcbsp
->dev
);
749 r
= clk_set_parent(mcbsp
->fclk
, fck_src
);
751 dev_err(mcbsp
->dev
, "CLKS: could not clk_set_parent() to %s\n",
757 pm_runtime_get_sync(mcbsp
->dev
);
765 #define max_thres(m) (mcbsp->pdata->buffer_size)
766 #define valid_threshold(m, val) ((val) <= max_thres(m))
767 #define THRESHOLD_PROP_BUILDER(prop) \
768 static ssize_t prop##_show(struct device *dev, \
769 struct device_attribute *attr, char *buf) \
771 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
773 return sprintf(buf, "%u\n", mcbsp->prop); \
776 static ssize_t prop##_store(struct device *dev, \
777 struct device_attribute *attr, \
778 const char *buf, size_t size) \
780 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
784 status = strict_strtoul(buf, 0, &val); \
788 if (!valid_threshold(mcbsp, val)) \
795 static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
797 THRESHOLD_PROP_BUILDER(max_tx_thres
);
798 THRESHOLD_PROP_BUILDER(max_rx_thres
);
800 static const char *dma_op_modes
[] = {
801 "element", "threshold",
804 static ssize_t
dma_op_mode_show(struct device
*dev
,
805 struct device_attribute
*attr
, char *buf
)
807 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
808 int dma_op_mode
, i
= 0;
810 const char * const *s
;
812 dma_op_mode
= mcbsp
->dma_op_mode
;
814 for (s
= &dma_op_modes
[i
]; i
< ARRAY_SIZE(dma_op_modes
); s
++, i
++) {
815 if (dma_op_mode
== i
)
816 len
+= sprintf(buf
+ len
, "[%s] ", *s
);
818 len
+= sprintf(buf
+ len
, "%s ", *s
);
820 len
+= sprintf(buf
+ len
, "\n");
825 static ssize_t
dma_op_mode_store(struct device
*dev
,
826 struct device_attribute
*attr
,
827 const char *buf
, size_t size
)
829 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
830 const char * const *s
;
833 for (s
= &dma_op_modes
[i
]; i
< ARRAY_SIZE(dma_op_modes
); s
++, i
++)
834 if (sysfs_streq(buf
, *s
))
837 if (i
== ARRAY_SIZE(dma_op_modes
))
840 spin_lock_irq(&mcbsp
->lock
);
845 mcbsp
->dma_op_mode
= i
;
848 spin_unlock_irq(&mcbsp
->lock
);
853 static DEVICE_ATTR(dma_op_mode
, 0644, dma_op_mode_show
, dma_op_mode_store
);
855 static const struct attribute
*additional_attrs
[] = {
856 &dev_attr_max_tx_thres
.attr
,
857 &dev_attr_max_rx_thres
.attr
,
858 &dev_attr_dma_op_mode
.attr
,
862 static const struct attribute_group additional_attr_group
= {
863 .attrs
= (struct attribute
**)additional_attrs
,
866 static ssize_t
st_taps_show(struct device
*dev
,
867 struct device_attribute
*attr
, char *buf
)
869 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
870 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
874 spin_lock_irq(&mcbsp
->lock
);
875 for (i
= 0; i
< st_data
->nr_taps
; i
++)
876 status
+= sprintf(&buf
[status
], (i
? ", %d" : "%d"),
879 status
+= sprintf(&buf
[status
], "\n");
880 spin_unlock_irq(&mcbsp
->lock
);
885 static ssize_t
st_taps_store(struct device
*dev
,
886 struct device_attribute
*attr
,
887 const char *buf
, size_t size
)
889 struct omap_mcbsp
*mcbsp
= dev_get_drvdata(dev
);
890 struct omap_mcbsp_st_data
*st_data
= mcbsp
->st_data
;
891 int val
, tmp
, status
, i
= 0;
893 spin_lock_irq(&mcbsp
->lock
);
894 memset(st_data
->taps
, 0, sizeof(st_data
->taps
));
895 st_data
->nr_taps
= 0;
898 status
= sscanf(buf
, "%d%n", &val
, &tmp
);
899 if (status
< 0 || status
== 0) {
903 if (val
< -32768 || val
> 32767) {
907 st_data
->taps
[i
++] = val
;
914 st_data
->nr_taps
= i
;
917 spin_unlock_irq(&mcbsp
->lock
);
922 static DEVICE_ATTR(st_taps
, 0644, st_taps_show
, st_taps_store
);
924 static const struct attribute
*sidetone_attrs
[] = {
925 &dev_attr_st_taps
.attr
,
929 static const struct attribute_group sidetone_attr_group
= {
930 .attrs
= (struct attribute
**)sidetone_attrs
,
933 static int omap_st_add(struct omap_mcbsp
*mcbsp
, struct resource
*res
)
935 struct omap_mcbsp_st_data
*st_data
;
938 st_data
= devm_kzalloc(mcbsp
->dev
, sizeof(*mcbsp
->st_data
), GFP_KERNEL
);
942 st_data
->io_base_st
= devm_ioremap(mcbsp
->dev
, res
->start
,
944 if (!st_data
->io_base_st
)
947 err
= sysfs_create_group(&mcbsp
->dev
->kobj
, &sidetone_attr_group
);
951 mcbsp
->st_data
= st_data
;
956 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
957 * 730 has only 2 McBSP, and both of them are MPU peripherals.
959 int omap_mcbsp_init(struct platform_device
*pdev
)
961 struct omap_mcbsp
*mcbsp
= platform_get_drvdata(pdev
);
962 struct resource
*res
;
965 spin_lock_init(&mcbsp
->lock
);
968 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
970 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
972 dev_err(mcbsp
->dev
, "invalid memory resource\n");
976 if (!devm_request_mem_region(&pdev
->dev
, res
->start
, resource_size(res
),
977 dev_name(&pdev
->dev
))) {
978 dev_err(mcbsp
->dev
, "memory region already claimed\n");
982 mcbsp
->phys_base
= res
->start
;
983 mcbsp
->reg_cache_size
= resource_size(res
);
984 mcbsp
->io_base
= devm_ioremap(&pdev
->dev
, res
->start
,
989 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dma");
991 mcbsp
->phys_dma_base
= mcbsp
->phys_base
;
993 mcbsp
->phys_dma_base
= res
->start
;
996 * OMAP1, 2 uses two interrupt lines: TX, RX
997 * OMAP2430, OMAP3 SoC have combined IRQ line as well.
998 * OMAP4 and newer SoC only have the combined IRQ line.
999 * Use the combined IRQ if available since it gives better debugging
1002 mcbsp
->irq
= platform_get_irq_byname(pdev
, "common");
1003 if (mcbsp
->irq
== -ENXIO
) {
1004 mcbsp
->tx_irq
= platform_get_irq_byname(pdev
, "tx");
1006 if (mcbsp
->tx_irq
== -ENXIO
) {
1007 mcbsp
->irq
= platform_get_irq(pdev
, 0);
1010 mcbsp
->rx_irq
= platform_get_irq_byname(pdev
, "rx");
1015 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "rx");
1017 dev_err(&pdev
->dev
, "invalid rx DMA channel\n");
1020 /* RX DMA request number, and port address configuration */
1021 mcbsp
->dma_data
[1].name
= "Audio Capture";
1022 mcbsp
->dma_data
[1].dma_req
= res
->start
;
1023 mcbsp
->dma_data
[1].port_addr
= omap_mcbsp_dma_reg_params(mcbsp
, 1);
1025 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "tx");
1027 dev_err(&pdev
->dev
, "invalid tx DMA channel\n");
1030 /* TX DMA request number, and port address configuration */
1031 mcbsp
->dma_data
[0].name
= "Audio Playback";
1032 mcbsp
->dma_data
[0].dma_req
= res
->start
;
1033 mcbsp
->dma_data
[0].port_addr
= omap_mcbsp_dma_reg_params(mcbsp
, 0);
1035 mcbsp
->fclk
= clk_get(&pdev
->dev
, "fck");
1036 if (IS_ERR(mcbsp
->fclk
)) {
1037 ret
= PTR_ERR(mcbsp
->fclk
);
1038 dev_err(mcbsp
->dev
, "unable to get fck: %d\n", ret
);
1042 mcbsp
->dma_op_mode
= MCBSP_DMA_MODE_ELEMENT
;
1043 if (mcbsp
->pdata
->buffer_size
) {
1045 * Initially configure the maximum thresholds to a safe value.
1046 * The McBSP FIFO usage with these values should not go under
1048 * If the whole FIFO without safety buffer is used, than there
1049 * is a possibility that the DMA will be not able to push the
1050 * new data on time, causing channel shifts in runtime.
1052 mcbsp
->max_tx_thres
= max_thres(mcbsp
) - 0x10;
1053 mcbsp
->max_rx_thres
= max_thres(mcbsp
) - 0x10;
1055 ret
= sysfs_create_group(&mcbsp
->dev
->kobj
,
1056 &additional_attr_group
);
1059 "Unable to create additional controls\n");
1063 mcbsp
->max_tx_thres
= -EINVAL
;
1064 mcbsp
->max_rx_thres
= -EINVAL
;
1067 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "sidetone");
1069 ret
= omap_st_add(mcbsp
, res
);
1072 "Unable to create sidetone controls\n");
1080 if (mcbsp
->pdata
->buffer_size
)
1081 sysfs_remove_group(&mcbsp
->dev
->kobj
, &additional_attr_group
);
1083 clk_put(mcbsp
->fclk
);
1087 void omap_mcbsp_sysfs_remove(struct omap_mcbsp
*mcbsp
)
1089 if (mcbsp
->pdata
->buffer_size
)
1090 sysfs_remove_group(&mcbsp
->dev
->kobj
, &additional_attr_group
);
1093 sysfs_remove_group(&mcbsp
->dev
->kobj
, &sidetone_attr_group
);