blob: 85176a5f414880fbfb02ede77bc6c45284390603 [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);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300319 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eduardo Valentind99a7452009-08-20 16:18:18 +0300320
321 spin_lock_irq(&mcbsp->lock);
322 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
323 syscon |= SIDLEMODE(0x02);
324 else
325 syscon |= SIDLEMODE(0x01);
326 spin_unlock_irq(&mcbsp->lock);
327
328 syscon |= (ENAWAKEUP | CLOCKACTIVITY(0x02));
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300329 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
330
Eduardo Valentind9a9b3f2009-08-20 16:18:16 +0300331 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300332 }
333}
334
335static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
336{
337 /*
338 * Disable wakup behavior, smart idle and all wakeups
339 */
340 if (cpu_is_omap34xx()) {
341 u16 syscon;
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300342
343 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300344 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300345 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
346
Eduardo Valentind9a9b3f2009-08-20 16:18:16 +0300347 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300348 }
349}
350#else
351static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
352static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300353#endif
354
Tony Lindgren120db2c2006-04-02 17:46:27 +0100355/*
356 * We can choose between IRQ based or polled IO.
357 * This needs to be called before omap_mcbsp_request().
358 */
359int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
360{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300361 struct omap_mcbsp *mcbsp;
362
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300363 if (!omap_mcbsp_check_valid_id(id)) {
364 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
365 return -ENODEV;
366 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300367 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100368
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300369 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100370
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300371 if (!mcbsp->free) {
372 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
373 mcbsp->id);
374 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100375 return -EINVAL;
376 }
377
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300378 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100379
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300380 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100381
382 return 0;
383}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300384EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100385
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386int omap_mcbsp_request(unsigned int id)
387{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300388 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 int err;
390
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300391 if (!omap_mcbsp_check_valid_id(id)) {
392 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
393 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100394 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300395 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300396
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300397 spin_lock(&mcbsp->lock);
398 if (!mcbsp->free) {
399 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
400 mcbsp->id);
401 spin_unlock(&mcbsp->lock);
Russell Kingb820ce42009-01-23 10:26:46 +0000402 return -EBUSY;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 }
404
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300405 mcbsp->free = 0;
406 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407
Russell Kingb820ce42009-01-23 10:26:46 +0000408 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
409 mcbsp->pdata->ops->request(id);
410
411 clk_enable(mcbsp->iclk);
412 clk_enable(mcbsp->fclk);
413
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300414 /* Do procedure specific to omap34xx arch, if applicable */
415 omap34xx_mcbsp_request(mcbsp);
416
Jarkko Nikula5a070552008-10-08 10:01:41 +0300417 /*
418 * Make sure that transmitter, receiver and sample-rate generator are
419 * not running before activating IRQs.
420 */
421 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
422 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
423
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300424 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100425 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300426 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300427 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
428 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100429 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300430 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
431 "for McBSP%d\n", mcbsp->tx_irq,
432 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100433 return err;
434 }
435
Jarkko Nikula5a070552008-10-08 10:01:41 +0300436 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300437 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
438 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100439 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300440 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
441 "for McBSP%d\n", mcbsp->rx_irq,
442 mcbsp->id);
443 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100444 return err;
445 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100446 }
447
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100448 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300450EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100451
452void omap_mcbsp_free(unsigned int id)
453{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300454 struct omap_mcbsp *mcbsp;
455
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300456 if (!omap_mcbsp_check_valid_id(id)) {
457 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100459 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300460 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100461
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300462 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
463 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300464
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300465 /* Do procedure specific to omap34xx arch, if applicable */
466 omap34xx_mcbsp_free(mcbsp);
467
Russell Kingb820ce42009-01-23 10:26:46 +0000468 clk_disable(mcbsp->fclk);
469 clk_disable(mcbsp->iclk);
470
471 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
472 /* Free IRQs */
473 free_irq(mcbsp->rx_irq, (void *)mcbsp);
474 free_irq(mcbsp->tx_irq, (void *)mcbsp);
475 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100476
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300477 spin_lock(&mcbsp->lock);
478 if (mcbsp->free) {
479 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
480 mcbsp->id);
481 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100482 return;
483 }
484
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300485 mcbsp->free = 1;
486 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300488EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100489
490/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300491 * Here we start the McBSP, by enabling transmitter, receiver or both.
492 * If no transmitter or receiver is active prior calling, then sample-rate
493 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100494 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300495void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300497 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100498 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300499 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500 u16 w;
501
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300502 if (!omap_mcbsp_check_valid_id(id)) {
503 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100504 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300505 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300506 mcbsp = id_to_mcbsp_ptr(id);
507 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100508
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300509 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
510 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100511
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300512 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
513 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
514
515 if (idle) {
516 /* Start the sample generator */
517 w = OMAP_MCBSP_READ(io_base, SPCR2);
518 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
519 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520
521 /* Enable transmitter and receiver */
522 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300523 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524
525 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300526 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100527
Eduardo Valentin44a63112009-08-20 16:18:09 +0300528 /*
529 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
530 * REVISIT: 100us may give enough time for two CLKSRG, however
531 * due to some unknown PM related, clock gating etc. reason it
532 * is now at 500us.
533 */
534 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100535
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300536 if (idle) {
537 /* Start frame sync */
538 w = OMAP_MCBSP_READ(io_base, SPCR2);
539 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
540 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100541
542 /* Dump McBSP Regs */
543 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100544}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300545EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100546
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300547void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300549 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100550 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300551 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100552 u16 w;
553
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300554 if (!omap_mcbsp_check_valid_id(id)) {
555 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300557 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100558
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300559 mcbsp = id_to_mcbsp_ptr(id);
560 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300562 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300564 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565
566 /* Reset receiver */
567 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300568 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100569
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300570 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
571 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
572
573 if (idle) {
574 /* Reset the sample rate generator */
575 w = OMAP_MCBSP_READ(io_base, SPCR2);
576 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
577 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300579EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580
Eero Nurkkala9abea082009-08-20 16:18:07 +0300581void omap_mcbsp_xmit_enable(unsigned int id, u8 enable)
582{
583 struct omap_mcbsp *mcbsp;
584 void __iomem *io_base;
585 u16 w;
586
587 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
588 return;
589
590 if (!omap_mcbsp_check_valid_id(id)) {
591 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
592 return;
593 }
594
595 mcbsp = id_to_mcbsp_ptr(id);
596 io_base = mcbsp->io_base;
597
598 w = OMAP_MCBSP_READ(io_base, XCCR);
599
600 if (enable)
601 OMAP_MCBSP_WRITE(io_base, XCCR, w & ~(XDISABLE));
602 else
603 OMAP_MCBSP_WRITE(io_base, XCCR, w | XDISABLE);
604}
605EXPORT_SYMBOL(omap_mcbsp_xmit_enable);
606
607void omap_mcbsp_recv_enable(unsigned int id, u8 enable)
608{
609 struct omap_mcbsp *mcbsp;
610 void __iomem *io_base;
611 u16 w;
612
613 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
614 return;
615
616 if (!omap_mcbsp_check_valid_id(id)) {
617 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
618 return;
619 }
620
621 mcbsp = id_to_mcbsp_ptr(id);
622 io_base = mcbsp->io_base;
623
624 w = OMAP_MCBSP_READ(io_base, RCCR);
625
626 if (enable)
627 OMAP_MCBSP_WRITE(io_base, RCCR, w & ~(RDISABLE));
628 else
629 OMAP_MCBSP_WRITE(io_base, RCCR, w | RDISABLE);
630}
631EXPORT_SYMBOL(omap_mcbsp_recv_enable);
632
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100633/* polled mcbsp i/o operations */
634int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
635{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300636 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100637 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300638
639 if (!omap_mcbsp_check_valid_id(id)) {
640 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
641 return -ENODEV;
642 }
643
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300644 mcbsp = id_to_mcbsp_ptr(id);
645 base = mcbsp->io_base;
646
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100647 writew(buf, base + OMAP_MCBSP_REG_DXR1);
648 /* if frame sync error - clear the error */
649 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
650 /* clear error */
651 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
652 base + OMAP_MCBSP_REG_SPCR2);
653 /* resend */
654 return -1;
655 } else {
656 /* wait for transmit confirmation */
657 int attemps = 0;
658 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
659 if (attemps++ > 1000) {
660 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
661 (~XRST),
662 base + OMAP_MCBSP_REG_SPCR2);
663 udelay(10);
664 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
665 (XRST),
666 base + OMAP_MCBSP_REG_SPCR2);
667 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300668 dev_err(mcbsp->dev, "Could not write to"
669 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100670 return -2;
671 }
672 }
673 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300674
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100675 return 0;
676}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300677EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100678
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300679int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100680{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300681 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100682 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300683
684 if (!omap_mcbsp_check_valid_id(id)) {
685 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
686 return -ENODEV;
687 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300688 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300689
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300690 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100691 /* if frame sync error - clear the error */
692 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
693 /* clear error */
694 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
695 base + OMAP_MCBSP_REG_SPCR1);
696 /* resend */
697 return -1;
698 } else {
699 /* wait for recieve confirmation */
700 int attemps = 0;
701 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
702 if (attemps++ > 1000) {
703 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
704 (~RRST),
705 base + OMAP_MCBSP_REG_SPCR1);
706 udelay(10);
707 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
708 (RRST),
709 base + OMAP_MCBSP_REG_SPCR1);
710 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300711 dev_err(mcbsp->dev, "Could not read from"
712 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100713 return -2;
714 }
715 }
716 }
717 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300718
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100719 return 0;
720}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300721EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100722
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723/*
724 * IRQ based word transmission.
725 */
726void omap_mcbsp_xmit_word(unsigned int id, u32 word)
727{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300728 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100729 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300730 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300732 if (!omap_mcbsp_check_valid_id(id)) {
733 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300735 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100736
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300737 mcbsp = id_to_mcbsp_ptr(id);
738 io_base = mcbsp->io_base;
739 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300741 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742
743 if (word_length > OMAP_MCBSP_WORD_16)
744 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
745 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
746}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300747EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748
749u32 omap_mcbsp_recv_word(unsigned int id)
750{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300751 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100752 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300754 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100755
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300756 if (!omap_mcbsp_check_valid_id(id)) {
757 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
758 return -ENODEV;
759 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300760 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300762 word_length = mcbsp->rx_word_length;
763 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300765 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766
767 if (word_length > OMAP_MCBSP_WORD_16)
768 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
769 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
770
771 return (word_lsb | (word_msb << 16));
772}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300773EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774
Tony Lindgren120db2c2006-04-02 17:46:27 +0100775int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
776{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300777 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100778 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300779 omap_mcbsp_word_length tx_word_length;
780 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100781 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
782
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300783 if (!omap_mcbsp_check_valid_id(id)) {
784 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
785 return -ENODEV;
786 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300787 mcbsp = id_to_mcbsp_ptr(id);
788 io_base = mcbsp->io_base;
789 tx_word_length = mcbsp->tx_word_length;
790 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300791
Tony Lindgren120db2c2006-04-02 17:46:27 +0100792 if (tx_word_length != rx_word_length)
793 return -EINVAL;
794
795 /* First we wait for the transmitter to be ready */
796 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
797 while (!(spcr2 & XRDY)) {
798 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
799 if (attempts++ > 1000) {
800 /* We must reset the transmitter */
801 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
802 udelay(10);
803 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
804 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300805 dev_err(mcbsp->dev, "McBSP%d transmitter not "
806 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100807 return -EAGAIN;
808 }
809 }
810
811 /* Now we can push the data */
812 if (tx_word_length > OMAP_MCBSP_WORD_16)
813 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
814 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
815
816 /* We wait for the receiver to be ready */
817 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
818 while (!(spcr1 & RRDY)) {
819 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
820 if (attempts++ > 1000) {
821 /* We must reset the receiver */
822 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
823 udelay(10);
824 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
825 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300826 dev_err(mcbsp->dev, "McBSP%d receiver not "
827 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100828 return -EAGAIN;
829 }
830 }
831
832 /* Receiver is ready, let's read the dummy data */
833 if (rx_word_length > OMAP_MCBSP_WORD_16)
834 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
835 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
836
837 return 0;
838}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300839EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100840
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300841int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100842{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300843 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100844 u32 clock_word = 0;
845 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300846 omap_mcbsp_word_length tx_word_length;
847 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100848 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
849
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300850 if (!omap_mcbsp_check_valid_id(id)) {
851 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
852 return -ENODEV;
853 }
854
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300855 mcbsp = id_to_mcbsp_ptr(id);
856 io_base = mcbsp->io_base;
857
858 tx_word_length = mcbsp->tx_word_length;
859 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300860
Tony Lindgren120db2c2006-04-02 17:46:27 +0100861 if (tx_word_length != rx_word_length)
862 return -EINVAL;
863
864 /* First we wait for the transmitter to be ready */
865 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
866 while (!(spcr2 & XRDY)) {
867 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
868 if (attempts++ > 1000) {
869 /* We must reset the transmitter */
870 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
871 udelay(10);
872 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
873 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300874 dev_err(mcbsp->dev, "McBSP%d transmitter not "
875 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100876 return -EAGAIN;
877 }
878 }
879
880 /* We first need to enable the bus clock */
881 if (tx_word_length > OMAP_MCBSP_WORD_16)
882 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
883 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
884
885 /* We wait for the receiver to be ready */
886 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
887 while (!(spcr1 & RRDY)) {
888 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
889 if (attempts++ > 1000) {
890 /* We must reset the receiver */
891 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
892 udelay(10);
893 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
894 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300895 dev_err(mcbsp->dev, "McBSP%d receiver not "
896 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100897 return -EAGAIN;
898 }
899 }
900
901 /* Receiver is ready, there is something for us */
902 if (rx_word_length > OMAP_MCBSP_WORD_16)
903 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
904 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
905
906 word[0] = (word_lsb | (word_msb << 16));
907
908 return 0;
909}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300910EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100911
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100912/*
913 * Simple DMA based buffer rx/tx routines.
914 * Nothing fancy, just a single buffer tx/rx through DMA.
915 * The DMA resources are released once the transfer is done.
916 * For anything fancier, you should use your own customized DMA
917 * routines and callbacks.
918 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300919int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
920 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300922 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100923 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100924 int src_port = 0;
925 int dest_port = 0;
926 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100927
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300928 if (!omap_mcbsp_check_valid_id(id)) {
929 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
930 return -ENODEV;
931 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300932 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300934 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300935 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300936 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300937 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300938 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300939 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300940 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100941 return -EAGAIN;
942 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300943 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300945 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300946 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100947
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300948 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949
Tony Lindgren120db2c2006-04-02 17:46:27 +0100950 if (cpu_class_is_omap1()) {
951 src_port = OMAP_DMA_PORT_TIPB;
952 dest_port = OMAP_DMA_PORT_EMIFF;
953 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300954 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300955 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100956
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300957 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100958 OMAP_DMA_DATA_TYPE_S16,
959 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000960 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100961 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100962
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300963 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100964 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100965 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300966 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000967 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100968
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300969 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100970 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100971 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000972 buffer,
973 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100974
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300975 omap_start_dma(mcbsp->dma_tx_lch);
976 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300977
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100978 return 0;
979}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300980EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100981
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300982int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
983 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100984{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300985 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100986 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100987 int src_port = 0;
988 int dest_port = 0;
989 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100990
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300991 if (!omap_mcbsp_check_valid_id(id)) {
992 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
993 return -ENODEV;
994 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300995 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100996
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300997 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300998 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300999 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001000 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001001 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001002 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001003 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001004 return -EAGAIN;
1005 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001006 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001007
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001008 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001009 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001010
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001011 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001012
Tony Lindgren120db2c2006-04-02 17:46:27 +01001013 if (cpu_class_is_omap1()) {
1014 src_port = OMAP_DMA_PORT_TIPB;
1015 dest_port = OMAP_DMA_PORT_EMIFF;
1016 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001017 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001018 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001019
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001020 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001021 OMAP_DMA_DATA_TYPE_S16,
1022 length >> 1, 1,
1023 OMAP_DMA_SYNC_ELEMENT,
1024 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001025
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001026 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001027 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001028 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001029 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001030 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001031
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001032 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001033 dest_port,
1034 OMAP_DMA_AMODE_POST_INC,
1035 buffer,
1036 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001037
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001038 omap_start_dma(mcbsp->dma_rx_lch);
1039 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001040
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001041 return 0;
1042}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001043EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001044
1045/*
1046 * SPI wrapper.
1047 * Since SPI setup is much simpler than the generic McBSP one,
1048 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1049 * Once this is done, you can call omap_mcbsp_start().
1050 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001051void omap_mcbsp_set_spi_mode(unsigned int id,
1052 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001053{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001054 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001055 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1056
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001057 if (!omap_mcbsp_check_valid_id(id)) {
1058 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001059 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001060 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001061 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001062
1063 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1064
1065 /* SPI has only one frame */
1066 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1067 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1068
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001069 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001070 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1071 mcbsp_cfg.spcr1 |= (1 << 12);
1072 else
1073 mcbsp_cfg.spcr1 |= (3 << 11);
1074
1075 /* Set clock parities */
1076 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1077 mcbsp_cfg.pcr0 |= CLKRP;
1078 else
1079 mcbsp_cfg.pcr0 &= ~CLKRP;
1080
1081 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1082 mcbsp_cfg.pcr0 &= ~CLKXP;
1083 else
1084 mcbsp_cfg.pcr0 |= CLKXP;
1085
1086 /* Set SCLKME to 0 and CLKSM to 1 */
1087 mcbsp_cfg.pcr0 &= ~SCLKME;
1088 mcbsp_cfg.srgr2 |= CLKSM;
1089
1090 /* Set FSXP */
1091 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1092 mcbsp_cfg.pcr0 &= ~FSXP;
1093 else
1094 mcbsp_cfg.pcr0 |= FSXP;
1095
1096 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1097 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001098 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001099 mcbsp_cfg.pcr0 |= FSXM;
1100 mcbsp_cfg.srgr2 &= ~FSGM;
1101 mcbsp_cfg.xcr2 |= XDATDLY(1);
1102 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001103 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001104 mcbsp_cfg.pcr0 &= ~CLKXM;
1105 mcbsp_cfg.srgr1 |= CLKGDV(1);
1106 mcbsp_cfg.pcr0 &= ~FSXM;
1107 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1108 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1109 }
1110
1111 mcbsp_cfg.xcr2 &= ~XPHASE;
1112 mcbsp_cfg.rcr2 &= ~RPHASE;
1113
1114 omap_mcbsp_config(id, &mcbsp_cfg);
1115}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001116EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001117
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001118#ifdef CONFIG_ARCH_OMAP34XX
1119#define max_thres(m) (mcbsp->pdata->buffer_size)
1120#define valid_threshold(m, val) ((val) <= max_thres(m))
1121#define THRESHOLD_PROP_BUILDER(prop) \
1122static ssize_t prop##_show(struct device *dev, \
1123 struct device_attribute *attr, char *buf) \
1124{ \
1125 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1126 \
1127 return sprintf(buf, "%u\n", mcbsp->prop); \
1128} \
1129 \
1130static ssize_t prop##_store(struct device *dev, \
1131 struct device_attribute *attr, \
1132 const char *buf, size_t size) \
1133{ \
1134 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1135 unsigned long val; \
1136 int status; \
1137 \
1138 status = strict_strtoul(buf, 0, &val); \
1139 if (status) \
1140 return status; \
1141 \
1142 if (!valid_threshold(mcbsp, val)) \
1143 return -EDOM; \
1144 \
1145 mcbsp->prop = val; \
1146 return size; \
1147} \
1148 \
1149static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1150
1151THRESHOLD_PROP_BUILDER(max_tx_thres);
1152THRESHOLD_PROP_BUILDER(max_rx_thres);
1153
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001154static ssize_t dma_op_mode_show(struct device *dev,
1155 struct device_attribute *attr, char *buf)
1156{
1157 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1158 int dma_op_mode;
1159
1160 spin_lock_irq(&mcbsp->lock);
1161 dma_op_mode = mcbsp->dma_op_mode;
1162 spin_unlock_irq(&mcbsp->lock);
1163
1164 return sprintf(buf, "current mode: %d\n"
1165 "possible mode values are:\n"
1166 "%d - %s\n"
1167 "%d - %s\n"
1168 "%d - %s\n",
1169 dma_op_mode,
1170 MCBSP_DMA_MODE_ELEMENT, "element mode",
1171 MCBSP_DMA_MODE_THRESHOLD, "threshold mode",
1172 MCBSP_DMA_MODE_FRAME, "frame mode");
1173}
1174
1175static ssize_t dma_op_mode_store(struct device *dev,
1176 struct device_attribute *attr,
1177 const char *buf, size_t size)
1178{
1179 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1180 unsigned long val;
1181 int status;
1182
1183 status = strict_strtoul(buf, 0, &val);
1184 if (status)
1185 return status;
1186
1187 spin_lock_irq(&mcbsp->lock);
1188
1189 if (!mcbsp->free) {
1190 size = -EBUSY;
1191 goto unlock;
1192 }
1193
1194 if (val > MCBSP_DMA_MODE_FRAME || val < MCBSP_DMA_MODE_ELEMENT) {
1195 size = -EINVAL;
1196 goto unlock;
1197 }
1198
1199 mcbsp->dma_op_mode = val;
1200
1201unlock:
1202 spin_unlock_irq(&mcbsp->lock);
1203
1204 return size;
1205}
1206
1207static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1208
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001209static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001210 &dev_attr_max_tx_thres.attr,
1211 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001212 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001213 NULL,
1214};
1215
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001216static const struct attribute_group additional_attr_group = {
1217 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001218};
1219
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001220static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001221{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001222 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001223}
1224
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001225static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001226{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001227 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001228}
1229
1230static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1231{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001232 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001233 if (cpu_is_omap34xx()) {
1234 mcbsp->max_tx_thres = max_thres(mcbsp);
1235 mcbsp->max_rx_thres = max_thres(mcbsp);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001236 /*
1237 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1238 * for mcbsp2 instances.
1239 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001240 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001241 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001242 "Unable to create additional controls\n");
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001243 } else {
1244 mcbsp->max_tx_thres = -EINVAL;
1245 mcbsp->max_rx_thres = -EINVAL;
1246 }
1247}
1248
1249static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1250{
1251 if (cpu_is_omap34xx())
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001252 omap_additional_remove(mcbsp->dev);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001253}
1254#else
1255static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1256static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1257#endif /* CONFIG_ARCH_OMAP34XX */
1258
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001259/*
1260 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1261 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1262 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001263static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001264{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001265 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001266 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001267 int id = pdev->id - 1;
1268 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001269
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001270 if (!pdata) {
1271 dev_err(&pdev->dev, "McBSP device initialized without"
1272 "platform data\n");
1273 ret = -EINVAL;
1274 goto exit;
1275 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001276
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001277 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001278
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001279 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001280 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1281 ret = -EINVAL;
1282 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001283 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001284
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001285 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1286 if (!mcbsp) {
1287 ret = -ENOMEM;
1288 goto exit;
1289 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001290
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001291 spin_lock_init(&mcbsp->lock);
1292 mcbsp->id = id + 1;
1293 mcbsp->free = 1;
1294 mcbsp->dma_tx_lch = -1;
1295 mcbsp->dma_rx_lch = -1;
1296
1297 mcbsp->phys_base = pdata->phys_base;
1298 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1299 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001300 ret = -ENOMEM;
1301 goto err_ioremap;
1302 }
1303
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001304 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001305 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1306 mcbsp->tx_irq = pdata->tx_irq;
1307 mcbsp->rx_irq = pdata->rx_irq;
1308 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1309 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001310
Russell Kingb820ce42009-01-23 10:26:46 +00001311 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1312 if (IS_ERR(mcbsp->iclk)) {
1313 ret = PTR_ERR(mcbsp->iclk);
1314 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1315 goto err_iclk;
1316 }
Stanley.Miao06151152009-01-29 08:57:12 -08001317
Russell Kingb820ce42009-01-23 10:26:46 +00001318 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1319 if (IS_ERR(mcbsp->fclk)) {
1320 ret = PTR_ERR(mcbsp->fclk);
1321 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1322 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001323 }
1324
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001325 mcbsp->pdata = pdata;
1326 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001327 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001328 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001329
1330 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1331 omap34xx_device_init(mcbsp);
1332
Russell Kingd592dd12008-09-04 14:25:42 +01001333 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001334
Russell Kingb820ce42009-01-23 10:26:46 +00001335err_fclk:
1336 clk_put(mcbsp->iclk);
1337err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001338 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001339err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001340 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001341exit:
1342 return ret;
1343}
1344
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001345static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001346{
1347 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1348
1349 platform_set_drvdata(pdev, NULL);
1350 if (mcbsp) {
1351
1352 if (mcbsp->pdata && mcbsp->pdata->ops &&
1353 mcbsp->pdata->ops->free)
1354 mcbsp->pdata->ops->free(mcbsp->id);
1355
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001356 omap34xx_device_exit(mcbsp);
1357
Russell Kingb820ce42009-01-23 10:26:46 +00001358 clk_disable(mcbsp->fclk);
1359 clk_disable(mcbsp->iclk);
1360 clk_put(mcbsp->fclk);
1361 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001362
Russell Kingd592dd12008-09-04 14:25:42 +01001363 iounmap(mcbsp->io_base);
1364
Russell Kingb820ce42009-01-23 10:26:46 +00001365 mcbsp->fclk = NULL;
1366 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001367 mcbsp->free = 0;
1368 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001369 }
1370
1371 return 0;
1372}
1373
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001374static struct platform_driver omap_mcbsp_driver = {
1375 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001376 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001377 .driver = {
1378 .name = "omap-mcbsp",
1379 },
1380};
1381
1382int __init omap_mcbsp_init(void)
1383{
1384 /* Register the McBSP driver */
1385 return platform_driver_register(&omap_mcbsp_driver);
1386}