blob: 136f8c5d0dac075fb8c601e5d840958ec2aa6633 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/mcbsp.c
3 *
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
6 *
7 *
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.
11 *
12 * Multichannel mode not supported.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030018#include <linux/platform_device.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010019#include <linux/wait.h>
20#include <linux/completion.h>
21#include <linux/interrupt.h>
22#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Tony Lindgren04fbf6a2007-02-12 10:50:53 -080024#include <linux/delay.h>
Eduardo Valentinfb78d802008-07-03 12:24:39 +030025#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/dma.h>
28#include <mach/mcbsp.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029
Chandra Shekharb4b58f52008-10-08 10:01:39 +030030struct omap_mcbsp **mcbsp_ptr;
31int omap_mcbsp_count;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030032
Chandra Shekharb4b58f52008-10-08 10:01:39 +030033void omap_mcbsp_write(void __iomem *io_base, u16 reg, u32 val)
34{
35 if (cpu_class_is_omap1() || cpu_is_omap2420())
36 __raw_writew((u16)val, io_base + reg);
37 else
38 __raw_writel(val, io_base + reg);
39}
40
41int omap_mcbsp_read(void __iomem *io_base, u16 reg)
42{
43 if (cpu_class_is_omap1() || cpu_is_omap2420())
44 return __raw_readw(io_base + reg);
45 else
46 return __raw_readl(io_base + reg);
47}
48
49#define OMAP_MCBSP_READ(base, reg) \
50 omap_mcbsp_read(base, OMAP_MCBSP_REG_##reg)
51#define OMAP_MCBSP_WRITE(base, reg, val) \
52 omap_mcbsp_write(base, OMAP_MCBSP_REG_##reg, val)
53
54#define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
55#define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010056
57static void omap_mcbsp_dump_reg(u8 id)
58{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030059 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
60
61 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
62 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
63 OMAP_MCBSP_READ(mcbsp->io_base, DRR2));
64 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
65 OMAP_MCBSP_READ(mcbsp->io_base, DRR1));
66 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
67 OMAP_MCBSP_READ(mcbsp->io_base, DXR2));
68 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
69 OMAP_MCBSP_READ(mcbsp->io_base, DXR1));
70 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
71 OMAP_MCBSP_READ(mcbsp->io_base, SPCR2));
72 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
73 OMAP_MCBSP_READ(mcbsp->io_base, SPCR1));
74 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
75 OMAP_MCBSP_READ(mcbsp->io_base, RCR2));
76 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
77 OMAP_MCBSP_READ(mcbsp->io_base, RCR1));
78 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
79 OMAP_MCBSP_READ(mcbsp->io_base, XCR2));
80 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
81 OMAP_MCBSP_READ(mcbsp->io_base, XCR1));
82 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
83 OMAP_MCBSP_READ(mcbsp->io_base, SRGR2));
84 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
85 OMAP_MCBSP_READ(mcbsp->io_base, SRGR1));
86 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
87 OMAP_MCBSP_READ(mcbsp->io_base, PCR0));
88 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089}
90
Linus Torvalds0cd61b62006-10-06 10:53:39 -070091static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092{
Jeff Garzike8f2af12007-10-26 05:40:25 -040093 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -070094 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095
Eero Nurkkalad6d834b2009-05-25 11:08:42 -070096 irqst_spcr2 = OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2);
97 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010098
Eero Nurkkalad6d834b2009-05-25 11:08:42 -070099 if (irqst_spcr2 & XSYNC_ERR) {
100 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
101 irqst_spcr2);
102 /* Writing zero to XSYNC_ERR clears the IRQ */
103 OMAP_MCBSP_WRITE(mcbsp_tx->io_base, SPCR2,
104 irqst_spcr2 & ~(XSYNC_ERR));
105 } else {
106 complete(&mcbsp_tx->tx_irq_completion);
107 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300108
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109 return IRQ_HANDLED;
110}
111
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700112static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400114 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700115 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700117 irqst_spcr1 = OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR1);
118 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700120 if (irqst_spcr1 & RSYNC_ERR) {
121 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
122 irqst_spcr1);
123 /* Writing zero to RSYNC_ERR clears the IRQ */
124 OMAP_MCBSP_WRITE(mcbsp_rx->io_base, SPCR1,
125 irqst_spcr1 & ~(RSYNC_ERR));
126 } else {
127 complete(&mcbsp_rx->tx_irq_completion);
128 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300129
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130 return IRQ_HANDLED;
131}
132
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
134{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400135 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300137 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
138 OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139
140 /* We can free the channels */
141 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
142 mcbsp_dma_tx->dma_tx_lch = -1;
143
144 complete(&mcbsp_dma_tx->tx_dma_completion);
145}
146
147static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
148{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400149 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300151 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
152 OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
154 /* We can free the channels */
155 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
156 mcbsp_dma_rx->dma_rx_lch = -1;
157
158 complete(&mcbsp_dma_rx->rx_dma_completion);
159}
160
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161/*
162 * omap_mcbsp_config simply write a config to the
163 * appropriate McBSP.
164 * You either call this function or set the McBSP registers
165 * by yourself before calling omap_mcbsp_start().
166 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300167void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300169 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100170 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300172 if (!omap_mcbsp_check_valid_id(id)) {
173 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
174 return;
175 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300176 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300177
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300178 io_base = mcbsp->io_base;
179 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
180 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100181
182 /* We write the given config */
183 OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
184 OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
185 OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
186 OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
187 OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
188 OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
189 OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
190 OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
191 OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
192 OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
193 OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200194 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
195 OMAP_MCBSP_WRITE(io_base, XCCR, config->xccr);
196 OMAP_MCBSP_WRITE(io_base, RCCR, config->rccr);
197 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100198}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300199EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100200
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300201#ifdef CONFIG_ARCH_OMAP34XX
202/*
203 * omap_mcbsp_set_tx_threshold configures how to deal
204 * with transmit threshold. the threshold value and handler can be
205 * configure in here.
206 */
207void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
208{
209 struct omap_mcbsp *mcbsp;
210 void __iomem *io_base;
211
212 if (!cpu_is_omap34xx())
213 return;
214
215 if (!omap_mcbsp_check_valid_id(id)) {
216 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
217 return;
218 }
219 mcbsp = id_to_mcbsp_ptr(id);
220 io_base = mcbsp->io_base;
221
222 OMAP_MCBSP_WRITE(io_base, THRSH2, threshold);
223}
224EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
225
226/*
227 * omap_mcbsp_set_rx_threshold configures how to deal
228 * with receive threshold. the threshold value and handler can be
229 * configure in here.
230 */
231void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
232{
233 struct omap_mcbsp *mcbsp;
234 void __iomem *io_base;
235
236 if (!cpu_is_omap34xx())
237 return;
238
239 if (!omap_mcbsp_check_valid_id(id)) {
240 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
241 return;
242 }
243 mcbsp = id_to_mcbsp_ptr(id);
244 io_base = mcbsp->io_base;
245
246 OMAP_MCBSP_WRITE(io_base, THRSH1, threshold);
247}
248EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +0300249
250/*
251 * omap_mcbsp_get_max_tx_thres just return the current configured
252 * maximum threshold for transmission
253 */
254u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
255{
256 struct omap_mcbsp *mcbsp;
257
258 if (!omap_mcbsp_check_valid_id(id)) {
259 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
260 return -ENODEV;
261 }
262 mcbsp = id_to_mcbsp_ptr(id);
263
264 return mcbsp->max_tx_thres;
265}
266EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
267
268/*
269 * omap_mcbsp_get_max_rx_thres just return the current configured
270 * maximum threshold for reception
271 */
272u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
273{
274 struct omap_mcbsp *mcbsp;
275
276 if (!omap_mcbsp_check_valid_id(id)) {
277 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
278 return -ENODEV;
279 }
280 mcbsp = id_to_mcbsp_ptr(id);
281
282 return mcbsp->max_rx_thres;
283}
284EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300285
286/*
287 * omap_mcbsp_get_dma_op_mode just return the current configured
288 * operating mode for the mcbsp channel
289 */
290int omap_mcbsp_get_dma_op_mode(unsigned int id)
291{
292 struct omap_mcbsp *mcbsp;
293 int dma_op_mode;
294
295 if (!omap_mcbsp_check_valid_id(id)) {
296 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
297 return -ENODEV;
298 }
299 mcbsp = id_to_mcbsp_ptr(id);
300
301 spin_lock_irq(&mcbsp->lock);
302 dma_op_mode = mcbsp->dma_op_mode;
303 spin_unlock_irq(&mcbsp->lock);
304
305 return dma_op_mode;
306}
307EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300308
309static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
310{
311 /*
312 * Enable wakup behavior, smart idle and all wakeups
313 * REVISIT: some wakeups may be unnecessary
314 */
315 if (cpu_is_omap34xx()) {
316 u16 syscon;
317
318 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
319 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03));
320 syscon |= (ENAWAKEUP | SIDLEMODE(0x02));
321 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
322
323 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, WAKEUPEN_ALL);
324 }
325}
326
327static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
328{
329 /*
330 * Disable wakup behavior, smart idle and all wakeups
331 */
332 if (cpu_is_omap34xx()) {
333 u16 syscon;
334 u16 wakeupen;
335
336 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
337 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03));
338 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
339
340 wakeupen = OMAP_MCBSP_READ(mcbsp->io_base, WAKEUPEN);
341 wakeupen &= ~WAKEUPEN_ALL;
342 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, wakeupen);
343 }
344}
345#else
346static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
347static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300348#endif
349
Tony Lindgren120db2c2006-04-02 17:46:27 +0100350/*
351 * We can choose between IRQ based or polled IO.
352 * This needs to be called before omap_mcbsp_request().
353 */
354int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
355{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300356 struct omap_mcbsp *mcbsp;
357
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300358 if (!omap_mcbsp_check_valid_id(id)) {
359 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
360 return -ENODEV;
361 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300362 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100363
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300364 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100365
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300366 if (!mcbsp->free) {
367 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
368 mcbsp->id);
369 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100370 return -EINVAL;
371 }
372
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300373 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100374
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300375 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100376
377 return 0;
378}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300379EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100381int omap_mcbsp_request(unsigned int id)
382{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300383 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384 int err;
385
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300386 if (!omap_mcbsp_check_valid_id(id)) {
387 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
388 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100389 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300390 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300391
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300392 spin_lock(&mcbsp->lock);
393 if (!mcbsp->free) {
394 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
395 mcbsp->id);
396 spin_unlock(&mcbsp->lock);
Russell Kingb820ce42009-01-23 10:26:46 +0000397 return -EBUSY;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398 }
399
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300400 mcbsp->free = 0;
401 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402
Russell Kingb820ce42009-01-23 10:26:46 +0000403 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
404 mcbsp->pdata->ops->request(id);
405
406 clk_enable(mcbsp->iclk);
407 clk_enable(mcbsp->fclk);
408
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300409 /* Do procedure specific to omap34xx arch, if applicable */
410 omap34xx_mcbsp_request(mcbsp);
411
Jarkko Nikula5a070552008-10-08 10:01:41 +0300412 /*
413 * Make sure that transmitter, receiver and sample-rate generator are
414 * not running before activating IRQs.
415 */
416 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
417 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
418
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300419 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100420 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300421 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300422 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
423 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100424 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300425 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
426 "for McBSP%d\n", mcbsp->tx_irq,
427 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100428 return err;
429 }
430
Jarkko Nikula5a070552008-10-08 10:01:41 +0300431 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300432 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
433 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100434 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300435 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
436 "for McBSP%d\n", mcbsp->rx_irq,
437 mcbsp->id);
438 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100439 return err;
440 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441 }
442
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300445EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100446
447void omap_mcbsp_free(unsigned int id)
448{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300449 struct omap_mcbsp *mcbsp;
450
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300451 if (!omap_mcbsp_check_valid_id(id)) {
452 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100454 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300455 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100456
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300457 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
458 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300459
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300460 /* Do procedure specific to omap34xx arch, if applicable */
461 omap34xx_mcbsp_free(mcbsp);
462
Russell Kingb820ce42009-01-23 10:26:46 +0000463 clk_disable(mcbsp->fclk);
464 clk_disable(mcbsp->iclk);
465
466 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
467 /* Free IRQs */
468 free_irq(mcbsp->rx_irq, (void *)mcbsp);
469 free_irq(mcbsp->tx_irq, (void *)mcbsp);
470 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100471
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300472 spin_lock(&mcbsp->lock);
473 if (mcbsp->free) {
474 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
475 mcbsp->id);
476 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477 return;
478 }
479
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300480 mcbsp->free = 1;
481 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100482}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300483EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100484
485/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300486 * Here we start the McBSP, by enabling transmitter, receiver or both.
487 * If no transmitter or receiver is active prior calling, then sample-rate
488 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100489 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300490void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300492 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100493 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300494 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100495 u16 w;
496
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300497 if (!omap_mcbsp_check_valid_id(id)) {
498 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100499 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300500 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300501 mcbsp = id_to_mcbsp_ptr(id);
502 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100503
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300504 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
505 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300507 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
508 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
509
510 if (idle) {
511 /* Start the sample generator */
512 w = OMAP_MCBSP_READ(io_base, SPCR2);
513 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
514 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100515
516 /* Enable transmitter and receiver */
517 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300518 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100519
520 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300521 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522
Eduardo Valentin44a63112009-08-20 16:18:09 +0300523 /*
524 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
525 * REVISIT: 100us may give enough time for two CLKSRG, however
526 * due to some unknown PM related, clock gating etc. reason it
527 * is now at 500us.
528 */
529 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100530
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300531 if (idle) {
532 /* Start frame sync */
533 w = OMAP_MCBSP_READ(io_base, SPCR2);
534 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
535 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100536
537 /* Dump McBSP Regs */
538 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300540EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100541
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300542void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300544 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100545 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300546 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100547 u16 w;
548
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300549 if (!omap_mcbsp_check_valid_id(id)) {
550 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100551 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300552 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100553
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300554 mcbsp = id_to_mcbsp_ptr(id);
555 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300557 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100558 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300559 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100560
561 /* Reset receiver */
562 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300563 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300565 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
566 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
567
568 if (idle) {
569 /* Reset the sample rate generator */
570 w = OMAP_MCBSP_READ(io_base, SPCR2);
571 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
572 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300574EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575
Eero Nurkkala9abea082009-08-20 16:18:07 +0300576void omap_mcbsp_xmit_enable(unsigned int id, u8 enable)
577{
578 struct omap_mcbsp *mcbsp;
579 void __iomem *io_base;
580 u16 w;
581
582 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
583 return;
584
585 if (!omap_mcbsp_check_valid_id(id)) {
586 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
587 return;
588 }
589
590 mcbsp = id_to_mcbsp_ptr(id);
591 io_base = mcbsp->io_base;
592
593 w = OMAP_MCBSP_READ(io_base, XCCR);
594
595 if (enable)
596 OMAP_MCBSP_WRITE(io_base, XCCR, w & ~(XDISABLE));
597 else
598 OMAP_MCBSP_WRITE(io_base, XCCR, w | XDISABLE);
599}
600EXPORT_SYMBOL(omap_mcbsp_xmit_enable);
601
602void omap_mcbsp_recv_enable(unsigned int id, u8 enable)
603{
604 struct omap_mcbsp *mcbsp;
605 void __iomem *io_base;
606 u16 w;
607
608 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
609 return;
610
611 if (!omap_mcbsp_check_valid_id(id)) {
612 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
613 return;
614 }
615
616 mcbsp = id_to_mcbsp_ptr(id);
617 io_base = mcbsp->io_base;
618
619 w = OMAP_MCBSP_READ(io_base, RCCR);
620
621 if (enable)
622 OMAP_MCBSP_WRITE(io_base, RCCR, w & ~(RDISABLE));
623 else
624 OMAP_MCBSP_WRITE(io_base, RCCR, w | RDISABLE);
625}
626EXPORT_SYMBOL(omap_mcbsp_recv_enable);
627
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100628/* polled mcbsp i/o operations */
629int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
630{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300631 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100632 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300633
634 if (!omap_mcbsp_check_valid_id(id)) {
635 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
636 return -ENODEV;
637 }
638
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300639 mcbsp = id_to_mcbsp_ptr(id);
640 base = mcbsp->io_base;
641
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100642 writew(buf, base + OMAP_MCBSP_REG_DXR1);
643 /* if frame sync error - clear the error */
644 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
645 /* clear error */
646 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
647 base + OMAP_MCBSP_REG_SPCR2);
648 /* resend */
649 return -1;
650 } else {
651 /* wait for transmit confirmation */
652 int attemps = 0;
653 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
654 if (attemps++ > 1000) {
655 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
656 (~XRST),
657 base + OMAP_MCBSP_REG_SPCR2);
658 udelay(10);
659 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
660 (XRST),
661 base + OMAP_MCBSP_REG_SPCR2);
662 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300663 dev_err(mcbsp->dev, "Could not write to"
664 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100665 return -2;
666 }
667 }
668 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300669
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100670 return 0;
671}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300672EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100673
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300674int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100675{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300676 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100677 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300678
679 if (!omap_mcbsp_check_valid_id(id)) {
680 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
681 return -ENODEV;
682 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300683 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300684
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300685 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100686 /* if frame sync error - clear the error */
687 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
688 /* clear error */
689 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
690 base + OMAP_MCBSP_REG_SPCR1);
691 /* resend */
692 return -1;
693 } else {
694 /* wait for recieve confirmation */
695 int attemps = 0;
696 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
697 if (attemps++ > 1000) {
698 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
699 (~RRST),
700 base + OMAP_MCBSP_REG_SPCR1);
701 udelay(10);
702 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
703 (RRST),
704 base + OMAP_MCBSP_REG_SPCR1);
705 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300706 dev_err(mcbsp->dev, "Could not read from"
707 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100708 return -2;
709 }
710 }
711 }
712 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300713
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100714 return 0;
715}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300716EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100717
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718/*
719 * IRQ based word transmission.
720 */
721void omap_mcbsp_xmit_word(unsigned int id, u32 word)
722{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300723 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100724 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300725 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300727 if (!omap_mcbsp_check_valid_id(id)) {
728 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100729 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300730 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300732 mcbsp = id_to_mcbsp_ptr(id);
733 io_base = mcbsp->io_base;
734 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300736 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737
738 if (word_length > OMAP_MCBSP_WORD_16)
739 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
740 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
741}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300742EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743
744u32 omap_mcbsp_recv_word(unsigned int id)
745{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300746 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100747 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300749 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300751 if (!omap_mcbsp_check_valid_id(id)) {
752 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
753 return -ENODEV;
754 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300755 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300757 word_length = mcbsp->rx_word_length;
758 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300760 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761
762 if (word_length > OMAP_MCBSP_WORD_16)
763 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
764 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
765
766 return (word_lsb | (word_msb << 16));
767}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300768EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Tony Lindgren120db2c2006-04-02 17:46:27 +0100770int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
771{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300772 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100773 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300774 omap_mcbsp_word_length tx_word_length;
775 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100776 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
777
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300778 if (!omap_mcbsp_check_valid_id(id)) {
779 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
780 return -ENODEV;
781 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300782 mcbsp = id_to_mcbsp_ptr(id);
783 io_base = mcbsp->io_base;
784 tx_word_length = mcbsp->tx_word_length;
785 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300786
Tony Lindgren120db2c2006-04-02 17:46:27 +0100787 if (tx_word_length != rx_word_length)
788 return -EINVAL;
789
790 /* First we wait for the transmitter to be ready */
791 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
792 while (!(spcr2 & XRDY)) {
793 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
794 if (attempts++ > 1000) {
795 /* We must reset the transmitter */
796 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
797 udelay(10);
798 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
799 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300800 dev_err(mcbsp->dev, "McBSP%d transmitter not "
801 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100802 return -EAGAIN;
803 }
804 }
805
806 /* Now we can push the data */
807 if (tx_word_length > OMAP_MCBSP_WORD_16)
808 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
809 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
810
811 /* We wait for the receiver to be ready */
812 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
813 while (!(spcr1 & RRDY)) {
814 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
815 if (attempts++ > 1000) {
816 /* We must reset the receiver */
817 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
818 udelay(10);
819 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
820 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300821 dev_err(mcbsp->dev, "McBSP%d receiver not "
822 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100823 return -EAGAIN;
824 }
825 }
826
827 /* Receiver is ready, let's read the dummy data */
828 if (rx_word_length > OMAP_MCBSP_WORD_16)
829 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
830 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
831
832 return 0;
833}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300834EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100835
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300836int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100837{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300838 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100839 u32 clock_word = 0;
840 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300841 omap_mcbsp_word_length tx_word_length;
842 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100843 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
844
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300845 if (!omap_mcbsp_check_valid_id(id)) {
846 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
847 return -ENODEV;
848 }
849
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300850 mcbsp = id_to_mcbsp_ptr(id);
851 io_base = mcbsp->io_base;
852
853 tx_word_length = mcbsp->tx_word_length;
854 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300855
Tony Lindgren120db2c2006-04-02 17:46:27 +0100856 if (tx_word_length != rx_word_length)
857 return -EINVAL;
858
859 /* First we wait for the transmitter to be ready */
860 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
861 while (!(spcr2 & XRDY)) {
862 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
863 if (attempts++ > 1000) {
864 /* We must reset the transmitter */
865 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
866 udelay(10);
867 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
868 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300869 dev_err(mcbsp->dev, "McBSP%d transmitter not "
870 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100871 return -EAGAIN;
872 }
873 }
874
875 /* We first need to enable the bus clock */
876 if (tx_word_length > OMAP_MCBSP_WORD_16)
877 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
878 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
879
880 /* We wait for the receiver to be ready */
881 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
882 while (!(spcr1 & RRDY)) {
883 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
884 if (attempts++ > 1000) {
885 /* We must reset the receiver */
886 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
887 udelay(10);
888 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
889 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300890 dev_err(mcbsp->dev, "McBSP%d receiver not "
891 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100892 return -EAGAIN;
893 }
894 }
895
896 /* Receiver is ready, there is something for us */
897 if (rx_word_length > OMAP_MCBSP_WORD_16)
898 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
899 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
900
901 word[0] = (word_lsb | (word_msb << 16));
902
903 return 0;
904}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300905EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100906
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100907/*
908 * Simple DMA based buffer rx/tx routines.
909 * Nothing fancy, just a single buffer tx/rx through DMA.
910 * The DMA resources are released once the transfer is done.
911 * For anything fancier, you should use your own customized DMA
912 * routines and callbacks.
913 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300914int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
915 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100916{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300917 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100918 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100919 int src_port = 0;
920 int dest_port = 0;
921 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100922
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300923 if (!omap_mcbsp_check_valid_id(id)) {
924 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
925 return -ENODEV;
926 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300927 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100928
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300929 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300930 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300931 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300932 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300933 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300934 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300935 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100936 return -EAGAIN;
937 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300938 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100939
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300940 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300941 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100942
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300943 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944
Tony Lindgren120db2c2006-04-02 17:46:27 +0100945 if (cpu_class_is_omap1()) {
946 src_port = OMAP_DMA_PORT_TIPB;
947 dest_port = OMAP_DMA_PORT_EMIFF;
948 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300949 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300950 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100951
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300952 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100953 OMAP_DMA_DATA_TYPE_S16,
954 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000955 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100956 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300958 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100959 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300961 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000962 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100963
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300964 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100965 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100966 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000967 buffer,
968 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100969
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300970 omap_start_dma(mcbsp->dma_tx_lch);
971 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300972
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100973 return 0;
974}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300975EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100976
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300977int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
978 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100979{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300980 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100981 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100982 int src_port = 0;
983 int dest_port = 0;
984 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100985
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300986 if (!omap_mcbsp_check_valid_id(id)) {
987 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
988 return -ENODEV;
989 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300990 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100991
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300992 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300993 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300994 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300995 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300996 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300997 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300998 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100999 return -EAGAIN;
1000 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001001 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001002
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001003 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001004 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001005
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001006 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001007
Tony Lindgren120db2c2006-04-02 17:46:27 +01001008 if (cpu_class_is_omap1()) {
1009 src_port = OMAP_DMA_PORT_TIPB;
1010 dest_port = OMAP_DMA_PORT_EMIFF;
1011 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001012 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001013 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001014
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001015 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001016 OMAP_DMA_DATA_TYPE_S16,
1017 length >> 1, 1,
1018 OMAP_DMA_SYNC_ELEMENT,
1019 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001020
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001021 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001022 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001023 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001024 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001025 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001026
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001027 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001028 dest_port,
1029 OMAP_DMA_AMODE_POST_INC,
1030 buffer,
1031 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001032
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001033 omap_start_dma(mcbsp->dma_rx_lch);
1034 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001035
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001036 return 0;
1037}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001038EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001039
1040/*
1041 * SPI wrapper.
1042 * Since SPI setup is much simpler than the generic McBSP one,
1043 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1044 * Once this is done, you can call omap_mcbsp_start().
1045 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001046void omap_mcbsp_set_spi_mode(unsigned int id,
1047 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001048{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001049 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001050 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1051
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001052 if (!omap_mcbsp_check_valid_id(id)) {
1053 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001054 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001055 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001056 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001057
1058 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1059
1060 /* SPI has only one frame */
1061 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1062 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1063
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001064 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001065 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1066 mcbsp_cfg.spcr1 |= (1 << 12);
1067 else
1068 mcbsp_cfg.spcr1 |= (3 << 11);
1069
1070 /* Set clock parities */
1071 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1072 mcbsp_cfg.pcr0 |= CLKRP;
1073 else
1074 mcbsp_cfg.pcr0 &= ~CLKRP;
1075
1076 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1077 mcbsp_cfg.pcr0 &= ~CLKXP;
1078 else
1079 mcbsp_cfg.pcr0 |= CLKXP;
1080
1081 /* Set SCLKME to 0 and CLKSM to 1 */
1082 mcbsp_cfg.pcr0 &= ~SCLKME;
1083 mcbsp_cfg.srgr2 |= CLKSM;
1084
1085 /* Set FSXP */
1086 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1087 mcbsp_cfg.pcr0 &= ~FSXP;
1088 else
1089 mcbsp_cfg.pcr0 |= FSXP;
1090
1091 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1092 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001093 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001094 mcbsp_cfg.pcr0 |= FSXM;
1095 mcbsp_cfg.srgr2 &= ~FSGM;
1096 mcbsp_cfg.xcr2 |= XDATDLY(1);
1097 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001098 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001099 mcbsp_cfg.pcr0 &= ~CLKXM;
1100 mcbsp_cfg.srgr1 |= CLKGDV(1);
1101 mcbsp_cfg.pcr0 &= ~FSXM;
1102 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1103 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1104 }
1105
1106 mcbsp_cfg.xcr2 &= ~XPHASE;
1107 mcbsp_cfg.rcr2 &= ~RPHASE;
1108
1109 omap_mcbsp_config(id, &mcbsp_cfg);
1110}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001111EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001112
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001113#ifdef CONFIG_ARCH_OMAP34XX
1114#define max_thres(m) (mcbsp->pdata->buffer_size)
1115#define valid_threshold(m, val) ((val) <= max_thres(m))
1116#define THRESHOLD_PROP_BUILDER(prop) \
1117static ssize_t prop##_show(struct device *dev, \
1118 struct device_attribute *attr, char *buf) \
1119{ \
1120 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1121 \
1122 return sprintf(buf, "%u\n", mcbsp->prop); \
1123} \
1124 \
1125static ssize_t prop##_store(struct device *dev, \
1126 struct device_attribute *attr, \
1127 const char *buf, size_t size) \
1128{ \
1129 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1130 unsigned long val; \
1131 int status; \
1132 \
1133 status = strict_strtoul(buf, 0, &val); \
1134 if (status) \
1135 return status; \
1136 \
1137 if (!valid_threshold(mcbsp, val)) \
1138 return -EDOM; \
1139 \
1140 mcbsp->prop = val; \
1141 return size; \
1142} \
1143 \
1144static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1145
1146THRESHOLD_PROP_BUILDER(max_tx_thres);
1147THRESHOLD_PROP_BUILDER(max_rx_thres);
1148
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001149static ssize_t dma_op_mode_show(struct device *dev,
1150 struct device_attribute *attr, char *buf)
1151{
1152 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1153 int dma_op_mode;
1154
1155 spin_lock_irq(&mcbsp->lock);
1156 dma_op_mode = mcbsp->dma_op_mode;
1157 spin_unlock_irq(&mcbsp->lock);
1158
1159 return sprintf(buf, "current mode: %d\n"
1160 "possible mode values are:\n"
1161 "%d - %s\n"
1162 "%d - %s\n"
1163 "%d - %s\n",
1164 dma_op_mode,
1165 MCBSP_DMA_MODE_ELEMENT, "element mode",
1166 MCBSP_DMA_MODE_THRESHOLD, "threshold mode",
1167 MCBSP_DMA_MODE_FRAME, "frame mode");
1168}
1169
1170static ssize_t dma_op_mode_store(struct device *dev,
1171 struct device_attribute *attr,
1172 const char *buf, size_t size)
1173{
1174 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1175 unsigned long val;
1176 int status;
1177
1178 status = strict_strtoul(buf, 0, &val);
1179 if (status)
1180 return status;
1181
1182 spin_lock_irq(&mcbsp->lock);
1183
1184 if (!mcbsp->free) {
1185 size = -EBUSY;
1186 goto unlock;
1187 }
1188
1189 if (val > MCBSP_DMA_MODE_FRAME || val < MCBSP_DMA_MODE_ELEMENT) {
1190 size = -EINVAL;
1191 goto unlock;
1192 }
1193
1194 mcbsp->dma_op_mode = val;
1195
1196unlock:
1197 spin_unlock_irq(&mcbsp->lock);
1198
1199 return size;
1200}
1201
1202static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1203
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001204static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001205 &dev_attr_max_tx_thres.attr,
1206 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001207 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001208 NULL,
1209};
1210
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001211static const struct attribute_group additional_attr_group = {
1212 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001213};
1214
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001215static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001216{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001217 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001218}
1219
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001220static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001221{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001222 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001223}
1224
1225static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1226{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001227 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001228 if (cpu_is_omap34xx()) {
1229 mcbsp->max_tx_thres = max_thres(mcbsp);
1230 mcbsp->max_rx_thres = max_thres(mcbsp);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001231 /*
1232 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1233 * for mcbsp2 instances.
1234 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001235 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001236 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001237 "Unable to create additional controls\n");
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001238 } else {
1239 mcbsp->max_tx_thres = -EINVAL;
1240 mcbsp->max_rx_thres = -EINVAL;
1241 }
1242}
1243
1244static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1245{
1246 if (cpu_is_omap34xx())
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001247 omap_additional_remove(mcbsp->dev);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001248}
1249#else
1250static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1251static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1252#endif /* CONFIG_ARCH_OMAP34XX */
1253
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001254/*
1255 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1256 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1257 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001258static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001259{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001260 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001261 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001262 int id = pdev->id - 1;
1263 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001264
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001265 if (!pdata) {
1266 dev_err(&pdev->dev, "McBSP device initialized without"
1267 "platform data\n");
1268 ret = -EINVAL;
1269 goto exit;
1270 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001271
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001272 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001273
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001274 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001275 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1276 ret = -EINVAL;
1277 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001278 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001279
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001280 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1281 if (!mcbsp) {
1282 ret = -ENOMEM;
1283 goto exit;
1284 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001285
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001286 spin_lock_init(&mcbsp->lock);
1287 mcbsp->id = id + 1;
1288 mcbsp->free = 1;
1289 mcbsp->dma_tx_lch = -1;
1290 mcbsp->dma_rx_lch = -1;
1291
1292 mcbsp->phys_base = pdata->phys_base;
1293 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1294 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001295 ret = -ENOMEM;
1296 goto err_ioremap;
1297 }
1298
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001299 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001300 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1301 mcbsp->tx_irq = pdata->tx_irq;
1302 mcbsp->rx_irq = pdata->rx_irq;
1303 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1304 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001305
Russell Kingb820ce42009-01-23 10:26:46 +00001306 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1307 if (IS_ERR(mcbsp->iclk)) {
1308 ret = PTR_ERR(mcbsp->iclk);
1309 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1310 goto err_iclk;
1311 }
Stanley.Miao06151152009-01-29 08:57:12 -08001312
Russell Kingb820ce42009-01-23 10:26:46 +00001313 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1314 if (IS_ERR(mcbsp->fclk)) {
1315 ret = PTR_ERR(mcbsp->fclk);
1316 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1317 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001318 }
1319
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001320 mcbsp->pdata = pdata;
1321 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001322 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001323 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001324
1325 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1326 omap34xx_device_init(mcbsp);
1327
Russell Kingd592dd12008-09-04 14:25:42 +01001328 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001329
Russell Kingb820ce42009-01-23 10:26:46 +00001330err_fclk:
1331 clk_put(mcbsp->iclk);
1332err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001333 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001334err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001335 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001336exit:
1337 return ret;
1338}
1339
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001340static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001341{
1342 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1343
1344 platform_set_drvdata(pdev, NULL);
1345 if (mcbsp) {
1346
1347 if (mcbsp->pdata && mcbsp->pdata->ops &&
1348 mcbsp->pdata->ops->free)
1349 mcbsp->pdata->ops->free(mcbsp->id);
1350
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001351 omap34xx_device_exit(mcbsp);
1352
Russell Kingb820ce42009-01-23 10:26:46 +00001353 clk_disable(mcbsp->fclk);
1354 clk_disable(mcbsp->iclk);
1355 clk_put(mcbsp->fclk);
1356 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001357
Russell Kingd592dd12008-09-04 14:25:42 +01001358 iounmap(mcbsp->io_base);
1359
Russell Kingb820ce42009-01-23 10:26:46 +00001360 mcbsp->fclk = NULL;
1361 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001362 mcbsp->free = 0;
1363 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001364 }
1365
1366 return 0;
1367}
1368
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001369static struct platform_driver omap_mcbsp_driver = {
1370 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001371 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001372 .driver = {
1373 .name = "omap-mcbsp",
1374 },
1375};
1376
1377int __init omap_mcbsp_init(void)
1378{
1379 /* Register the McBSP driver */
1380 return platform_driver_register(&omap_mcbsp_driver);
1381}