blob: e63990fd923f2e96311f9a77b6424b643ba5aeeb [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
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030static struct omap_mcbsp mcbsp[OMAP_MAX_MCBSP_COUNT];
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030031
32#define omap_mcbsp_check_valid_id(id) (mcbsp[id].pdata && \
33 mcbsp[id].pdata->ops && \
34 mcbsp[id].pdata->ops->check && \
35 (mcbsp[id].pdata->ops->check(id) == 0))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036
37static void omap_mcbsp_dump_reg(u8 id)
38{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030039 dev_dbg(mcbsp[id].dev, "**** McBSP%d regs ****\n", mcbsp[id].id);
40 dev_dbg(mcbsp[id].dev, "DRR2: 0x%04x\n",
41 OMAP_MCBSP_READ(mcbsp[id].io_base, DRR2));
42 dev_dbg(mcbsp[id].dev, "DRR1: 0x%04x\n",
43 OMAP_MCBSP_READ(mcbsp[id].io_base, DRR1));
44 dev_dbg(mcbsp[id].dev, "DXR2: 0x%04x\n",
45 OMAP_MCBSP_READ(mcbsp[id].io_base, DXR2));
46 dev_dbg(mcbsp[id].dev, "DXR1: 0x%04x\n",
47 OMAP_MCBSP_READ(mcbsp[id].io_base, DXR1));
48 dev_dbg(mcbsp[id].dev, "SPCR2: 0x%04x\n",
49 OMAP_MCBSP_READ(mcbsp[id].io_base, SPCR2));
50 dev_dbg(mcbsp[id].dev, "SPCR1: 0x%04x\n",
51 OMAP_MCBSP_READ(mcbsp[id].io_base, SPCR1));
52 dev_dbg(mcbsp[id].dev, "RCR2: 0x%04x\n",
53 OMAP_MCBSP_READ(mcbsp[id].io_base, RCR2));
54 dev_dbg(mcbsp[id].dev, "RCR1: 0x%04x\n",
55 OMAP_MCBSP_READ(mcbsp[id].io_base, RCR1));
56 dev_dbg(mcbsp[id].dev, "XCR2: 0x%04x\n",
57 OMAP_MCBSP_READ(mcbsp[id].io_base, XCR2));
58 dev_dbg(mcbsp[id].dev, "XCR1: 0x%04x\n",
59 OMAP_MCBSP_READ(mcbsp[id].io_base, XCR1));
60 dev_dbg(mcbsp[id].dev, "SRGR2: 0x%04x\n",
61 OMAP_MCBSP_READ(mcbsp[id].io_base, SRGR2));
62 dev_dbg(mcbsp[id].dev, "SRGR1: 0x%04x\n",
63 OMAP_MCBSP_READ(mcbsp[id].io_base, SRGR1));
64 dev_dbg(mcbsp[id].dev, "PCR0: 0x%04x\n",
65 OMAP_MCBSP_READ(mcbsp[id].io_base, PCR0));
66 dev_dbg(mcbsp[id].dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010067}
68
Linus Torvalds0cd61b62006-10-06 10:53:39 -070069static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010070{
Jeff Garzike8f2af12007-10-26 05:40:25 -040071 struct omap_mcbsp *mcbsp_tx = dev_id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010072
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030073 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n",
74 OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010075
76 complete(&mcbsp_tx->tx_irq_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +030077
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010078 return IRQ_HANDLED;
79}
80
Linus Torvalds0cd61b62006-10-06 10:53:39 -070081static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010082{
Jeff Garzike8f2af12007-10-26 05:40:25 -040083 struct omap_mcbsp *mcbsp_rx = dev_id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010084
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030085 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n",
86 OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010087
88 complete(&mcbsp_rx->rx_irq_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +030089
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090 return IRQ_HANDLED;
91}
92
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010093static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
94{
Jeff Garzike8f2af12007-10-26 05:40:25 -040095 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010096
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030097 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
98 OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099
100 /* We can free the channels */
101 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
102 mcbsp_dma_tx->dma_tx_lch = -1;
103
104 complete(&mcbsp_dma_tx->tx_dma_completion);
105}
106
107static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
108{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400109 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300111 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
112 OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113
114 /* We can free the channels */
115 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
116 mcbsp_dma_rx->dma_rx_lch = -1;
117
118 complete(&mcbsp_dma_rx->rx_dma_completion);
119}
120
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121/*
122 * omap_mcbsp_config simply write a config to the
123 * appropriate McBSP.
124 * You either call this function or set the McBSP registers
125 * by yourself before calling omap_mcbsp_start().
126 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300127void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128{
Russell Kingd592dd12008-09-04 14:25:42 +0100129 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300131 if (!omap_mcbsp_check_valid_id(id)) {
132 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
133 return;
134 }
135
136 io_base = mcbsp[id].io_base;
Russell Kingd592dd12008-09-04 14:25:42 +0100137 dev_dbg(mcbsp[id].dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
138 mcbsp[id].id, mcbsp[id].phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139
140 /* We write the given config */
141 OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
142 OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
143 OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
144 OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
145 OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
146 OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
147 OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
148 OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
149 OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
150 OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
151 OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
152}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300153EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154
Tony Lindgren120db2c2006-04-02 17:46:27 +0100155/*
156 * We can choose between IRQ based or polled IO.
157 * This needs to be called before omap_mcbsp_request().
158 */
159int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
160{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300161 if (!omap_mcbsp_check_valid_id(id)) {
162 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
163 return -ENODEV;
164 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100165
166 spin_lock(&mcbsp[id].lock);
167
168 if (!mcbsp[id].free) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300169 dev_err(mcbsp[id].dev, "McBSP%d is currently in use\n",
170 mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100171 spin_unlock(&mcbsp[id].lock);
172 return -EINVAL;
173 }
174
175 mcbsp[id].io_type = io_type;
176
177 spin_unlock(&mcbsp[id].lock);
178
179 return 0;
180}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300181EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100182
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100183int omap_mcbsp_request(unsigned int id)
184{
185 int err;
186
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300187 if (!omap_mcbsp_check_valid_id(id)) {
188 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
189 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100190 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300191
192 if (mcbsp[id].pdata->ops->request)
193 mcbsp[id].pdata->ops->request(id);
194
195 clk_enable(mcbsp[id].clk);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100196
197 spin_lock(&mcbsp[id].lock);
198 if (!mcbsp[id].free) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300199 dev_err(mcbsp[id].dev, "McBSP%d is currently in use\n",
200 mcbsp[id].id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100201 spin_unlock(&mcbsp[id].lock);
202 return -1;
203 }
204
205 mcbsp[id].free = 0;
206 spin_unlock(&mcbsp[id].lock);
207
Tony Lindgren120db2c2006-04-02 17:46:27 +0100208 if (mcbsp[id].io_type == OMAP_MCBSP_IRQ_IO) {
209 /* We need to get IRQs here */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300210 err = request_irq(mcbsp[id].tx_irq, omap_mcbsp_tx_irq_handler,
211 0, "McBSP", (void *) (&mcbsp[id]));
Tony Lindgren120db2c2006-04-02 17:46:27 +0100212 if (err != 0) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300213 dev_err(mcbsp[id].dev, "Unable to request TX IRQ %d "
214 "for McBSP%d\n", mcbsp[id].tx_irq,
215 mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100216 return err;
217 }
218
219 init_completion(&(mcbsp[id].tx_irq_completion));
220
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300221 err = request_irq(mcbsp[id].rx_irq, omap_mcbsp_rx_irq_handler,
222 0, "McBSP", (void *) (&mcbsp[id]));
Tony Lindgren120db2c2006-04-02 17:46:27 +0100223 if (err != 0) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300224 dev_err(mcbsp[id].dev, "Unable to request RX IRQ %d "
225 "for McBSP%d\n", mcbsp[id].rx_irq,
226 mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100227 free_irq(mcbsp[id].tx_irq, (void *) (&mcbsp[id]));
228 return err;
229 }
230
231 init_completion(&(mcbsp[id].rx_irq_completion));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100232 }
233
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100235}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300236EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100237
238void omap_mcbsp_free(unsigned int id)
239{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300240 if (!omap_mcbsp_check_valid_id(id)) {
241 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100243 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100244
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300245 if (mcbsp[id].pdata->ops->free)
246 mcbsp[id].pdata->ops->free(id);
247
248 clk_disable(mcbsp[id].clk);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100249
250 spin_lock(&mcbsp[id].lock);
251 if (mcbsp[id].free) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300252 dev_err(mcbsp[id].dev, "McBSP%d was not reserved\n",
253 mcbsp[id].id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100254 spin_unlock(&mcbsp[id].lock);
255 return;
256 }
257
258 mcbsp[id].free = 1;
259 spin_unlock(&mcbsp[id].lock);
260
Tony Lindgren120db2c2006-04-02 17:46:27 +0100261 if (mcbsp[id].io_type == OMAP_MCBSP_IRQ_IO) {
262 /* Free IRQs */
263 free_irq(mcbsp[id].rx_irq, (void *) (&mcbsp[id]));
264 free_irq(mcbsp[id].tx_irq, (void *) (&mcbsp[id]));
265 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100266}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300267EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100268
269/*
270 * Here we start the McBSP, by enabling the sample
271 * generator, both transmitter and receivers,
272 * and the frame sync.
273 */
274void omap_mcbsp_start(unsigned int id)
275{
Russell Kingd592dd12008-09-04 14:25:42 +0100276 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100277 u16 w;
278
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300279 if (!omap_mcbsp_check_valid_id(id)) {
280 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100281 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300282 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283
284 io_base = mcbsp[id].io_base;
285
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300286 mcbsp[id].rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
287 mcbsp[id].tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100288
289 /* Start the sample generator */
290 w = OMAP_MCBSP_READ(io_base, SPCR2);
291 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
292
293 /* Enable transmitter and receiver */
294 w = OMAP_MCBSP_READ(io_base, SPCR2);
295 OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1);
296
297 w = OMAP_MCBSP_READ(io_base, SPCR1);
298 OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1);
299
300 udelay(100);
301
302 /* Start frame sync */
303 w = OMAP_MCBSP_READ(io_base, SPCR2);
304 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
305
306 /* Dump McBSP Regs */
307 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100308}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300309EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100310
311void omap_mcbsp_stop(unsigned int id)
312{
Russell Kingd592dd12008-09-04 14:25:42 +0100313 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100314 u16 w;
315
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300316 if (!omap_mcbsp_check_valid_id(id)) {
317 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100318 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300319 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320
321 io_base = mcbsp[id].io_base;
322
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300323 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100324 w = OMAP_MCBSP_READ(io_base, SPCR2);
325 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1));
326
327 /* Reset receiver */
328 w = OMAP_MCBSP_READ(io_base, SPCR1);
329 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1));
330
331 /* Reset the sample rate generator */
332 w = OMAP_MCBSP_READ(io_base, SPCR2);
333 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
334}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300335EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100336
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100337/* polled mcbsp i/o operations */
338int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
339{
Russell Kingd592dd12008-09-04 14:25:42 +0100340 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300341
342 if (!omap_mcbsp_check_valid_id(id)) {
343 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
344 return -ENODEV;
345 }
346
347 base = mcbsp[id].io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100348 writew(buf, base + OMAP_MCBSP_REG_DXR1);
349 /* if frame sync error - clear the error */
350 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
351 /* clear error */
352 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
353 base + OMAP_MCBSP_REG_SPCR2);
354 /* resend */
355 return -1;
356 } else {
357 /* wait for transmit confirmation */
358 int attemps = 0;
359 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
360 if (attemps++ > 1000) {
361 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
362 (~XRST),
363 base + OMAP_MCBSP_REG_SPCR2);
364 udelay(10);
365 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
366 (XRST),
367 base + OMAP_MCBSP_REG_SPCR2);
368 udelay(10);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300369 dev_err(mcbsp[id].dev, "Could not write to"
370 " McBSP%d Register\n", mcbsp[id].id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100371 return -2;
372 }
373 }
374 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300375
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100376 return 0;
377}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300378EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100379
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300380int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100381{
Russell Kingd592dd12008-09-04 14:25:42 +0100382 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300383
384 if (!omap_mcbsp_check_valid_id(id)) {
385 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
386 return -ENODEV;
387 }
388
389 base = mcbsp[id].io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100390 /* if frame sync error - clear the error */
391 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
392 /* clear error */
393 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
394 base + OMAP_MCBSP_REG_SPCR1);
395 /* resend */
396 return -1;
397 } else {
398 /* wait for recieve confirmation */
399 int attemps = 0;
400 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
401 if (attemps++ > 1000) {
402 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
403 (~RRST),
404 base + OMAP_MCBSP_REG_SPCR1);
405 udelay(10);
406 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
407 (RRST),
408 base + OMAP_MCBSP_REG_SPCR1);
409 udelay(10);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300410 dev_err(mcbsp[id].dev, "Could not read from"
411 " McBSP%d Register\n", mcbsp[id].id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100412 return -2;
413 }
414 }
415 }
416 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300417
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100418 return 0;
419}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300420EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100421
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100422/*
423 * IRQ based word transmission.
424 */
425void omap_mcbsp_xmit_word(unsigned int id, u32 word)
426{
Russell Kingd592dd12008-09-04 14:25:42 +0100427 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300428 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300430 if (!omap_mcbsp_check_valid_id(id)) {
431 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300433 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434
435 io_base = mcbsp[id].io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300436 word_length = mcbsp[id].tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437
438 wait_for_completion(&(mcbsp[id].tx_irq_completion));
439
440 if (word_length > OMAP_MCBSP_WORD_16)
441 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
442 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
443}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300444EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100445
446u32 omap_mcbsp_recv_word(unsigned int id)
447{
Russell Kingd592dd12008-09-04 14:25:42 +0100448 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300450 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100451
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300452 if (!omap_mcbsp_check_valid_id(id)) {
453 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
454 return -ENODEV;
455 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100456
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300457 word_length = mcbsp[id].rx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 io_base = mcbsp[id].io_base;
459
460 wait_for_completion(&(mcbsp[id].rx_irq_completion));
461
462 if (word_length > OMAP_MCBSP_WORD_16)
463 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
464 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
465
466 return (word_lsb | (word_msb << 16));
467}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300468EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100469
Tony Lindgren120db2c2006-04-02 17:46:27 +0100470int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
471{
Russell Kingd592dd12008-09-04 14:25:42 +0100472 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300473 omap_mcbsp_word_length tx_word_length;
474 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100475 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
476
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300477 if (!omap_mcbsp_check_valid_id(id)) {
478 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
479 return -ENODEV;
480 }
481
482 io_base = mcbsp[id].io_base;
483 tx_word_length = mcbsp[id].tx_word_length;
484 rx_word_length = mcbsp[id].rx_word_length;
485
Tony Lindgren120db2c2006-04-02 17:46:27 +0100486 if (tx_word_length != rx_word_length)
487 return -EINVAL;
488
489 /* First we wait for the transmitter to be ready */
490 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
491 while (!(spcr2 & XRDY)) {
492 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
493 if (attempts++ > 1000) {
494 /* We must reset the transmitter */
495 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
496 udelay(10);
497 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
498 udelay(10);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300499 dev_err(mcbsp[id].dev, "McBSP%d transmitter not "
500 "ready\n", mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100501 return -EAGAIN;
502 }
503 }
504
505 /* Now we can push the data */
506 if (tx_word_length > OMAP_MCBSP_WORD_16)
507 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
508 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
509
510 /* We wait for the receiver to be ready */
511 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
512 while (!(spcr1 & RRDY)) {
513 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
514 if (attempts++ > 1000) {
515 /* We must reset the receiver */
516 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
517 udelay(10);
518 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
519 udelay(10);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300520 dev_err(mcbsp[id].dev, "McBSP%d receiver not "
521 "ready\n", mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100522 return -EAGAIN;
523 }
524 }
525
526 /* Receiver is ready, let's read the dummy data */
527 if (rx_word_length > OMAP_MCBSP_WORD_16)
528 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
529 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
530
531 return 0;
532}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300533EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100534
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300535int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100536{
Russell Kingd592dd12008-09-04 14:25:42 +0100537 u32 clock_word = 0;
538 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300539 omap_mcbsp_word_length tx_word_length;
540 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100541 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
542
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300543 if (!omap_mcbsp_check_valid_id(id)) {
544 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
545 return -ENODEV;
546 }
547
548 io_base = mcbsp[id].io_base;
549 tx_word_length = mcbsp[id].tx_word_length;
550 rx_word_length = mcbsp[id].rx_word_length;
551
Tony Lindgren120db2c2006-04-02 17:46:27 +0100552 if (tx_word_length != rx_word_length)
553 return -EINVAL;
554
555 /* First we wait for the transmitter to be ready */
556 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
557 while (!(spcr2 & XRDY)) {
558 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
559 if (attempts++ > 1000) {
560 /* We must reset the transmitter */
561 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
562 udelay(10);
563 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
564 udelay(10);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300565 dev_err(mcbsp[id].dev, "McBSP%d transmitter not "
566 "ready\n", mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100567 return -EAGAIN;
568 }
569 }
570
571 /* We first need to enable the bus clock */
572 if (tx_word_length > OMAP_MCBSP_WORD_16)
573 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
574 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
575
576 /* We wait for the receiver to be ready */
577 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
578 while (!(spcr1 & RRDY)) {
579 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
580 if (attempts++ > 1000) {
581 /* We must reset the receiver */
582 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
583 udelay(10);
584 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
585 udelay(10);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300586 dev_err(mcbsp[id].dev, "McBSP%d receiver not "
587 "ready\n", mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100588 return -EAGAIN;
589 }
590 }
591
592 /* Receiver is ready, there is something for us */
593 if (rx_word_length > OMAP_MCBSP_WORD_16)
594 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
595 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
596
597 word[0] = (word_lsb | (word_msb << 16));
598
599 return 0;
600}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300601EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100602
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100603/*
604 * Simple DMA based buffer rx/tx routines.
605 * Nothing fancy, just a single buffer tx/rx through DMA.
606 * The DMA resources are released once the transfer is done.
607 * For anything fancier, you should use your own customized DMA
608 * routines and callbacks.
609 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300610int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
611 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612{
613 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100614 int src_port = 0;
615 int dest_port = 0;
616 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300618 if (!omap_mcbsp_check_valid_id(id)) {
619 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
620 return -ENODEV;
621 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300623 if (omap_request_dma(mcbsp[id].dma_tx_sync, "McBSP TX",
624 omap_mcbsp_tx_dma_callback,
625 &mcbsp[id],
626 &dma_tx_ch)) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300627 dev_err(mcbsp[id].dev, " Unable to request DMA channel for "
628 "McBSP%d TX. Trying IRQ based TX\n",
629 mcbsp[id].id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630 return -EAGAIN;
631 }
632 mcbsp[id].dma_tx_lch = dma_tx_ch;
633
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300634 dev_err(mcbsp[id].dev, "McBSP%d TX DMA on channel %d\n", mcbsp[id].id,
635 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100636
637 init_completion(&(mcbsp[id].tx_dma_completion));
638
Tony Lindgren120db2c2006-04-02 17:46:27 +0100639 if (cpu_class_is_omap1()) {
640 src_port = OMAP_DMA_PORT_TIPB;
641 dest_port = OMAP_DMA_PORT_EMIFF;
642 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300643 if (cpu_class_is_omap2())
Tony Lindgren120db2c2006-04-02 17:46:27 +0100644 sync_dev = mcbsp[id].dma_tx_sync;
645
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100646 omap_set_dma_transfer_params(mcbsp[id].dma_tx_lch,
647 OMAP_DMA_DATA_TYPE_S16,
648 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000649 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100650 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651
652 omap_set_dma_dest_params(mcbsp[id].dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100653 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100654 OMAP_DMA_AMODE_CONSTANT,
Russell King65846902008-09-03 23:46:18 +0100655 mcbsp[id].phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000656 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657
658 omap_set_dma_src_params(mcbsp[id].dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100659 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000661 buffer,
662 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100663
664 omap_start_dma(mcbsp[id].dma_tx_lch);
665 wait_for_completion(&(mcbsp[id].tx_dma_completion));
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300666
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667 return 0;
668}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300669EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300671int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
672 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673{
674 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100675 int src_port = 0;
676 int dest_port = 0;
677 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300679 if (!omap_mcbsp_check_valid_id(id)) {
680 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
681 return -ENODEV;
682 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100683
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300684 if (omap_request_dma(mcbsp[id].dma_rx_sync, "McBSP RX",
685 omap_mcbsp_rx_dma_callback,
686 &mcbsp[id],
687 &dma_rx_ch)) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300688 dev_err(mcbsp[id].dev, "Unable to request DMA channel for "
689 "McBSP%d RX. Trying IRQ based RX\n",
690 mcbsp[id].id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691 return -EAGAIN;
692 }
693 mcbsp[id].dma_rx_lch = dma_rx_ch;
694
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300695 dev_err(mcbsp[id].dev, "McBSP%d RX DMA on channel %d\n", mcbsp[id].id,
696 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697
698 init_completion(&(mcbsp[id].rx_dma_completion));
699
Tony Lindgren120db2c2006-04-02 17:46:27 +0100700 if (cpu_class_is_omap1()) {
701 src_port = OMAP_DMA_PORT_TIPB;
702 dest_port = OMAP_DMA_PORT_EMIFF;
703 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300704 if (cpu_class_is_omap2())
Tony Lindgren120db2c2006-04-02 17:46:27 +0100705 sync_dev = mcbsp[id].dma_rx_sync;
706
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100707 omap_set_dma_transfer_params(mcbsp[id].dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300708 OMAP_DMA_DATA_TYPE_S16,
709 length >> 1, 1,
710 OMAP_DMA_SYNC_ELEMENT,
711 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712
713 omap_set_dma_src_params(mcbsp[id].dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100714 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715 OMAP_DMA_AMODE_CONSTANT,
Russell King65846902008-09-03 23:46:18 +0100716 mcbsp[id].phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000717 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718
719 omap_set_dma_dest_params(mcbsp[id].dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300720 dest_port,
721 OMAP_DMA_AMODE_POST_INC,
722 buffer,
723 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100724
725 omap_start_dma(mcbsp[id].dma_rx_lch);
726 wait_for_completion(&(mcbsp[id].rx_dma_completion));
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300727
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728 return 0;
729}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300730EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731
732/*
733 * SPI wrapper.
734 * Since SPI setup is much simpler than the generic McBSP one,
735 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
736 * Once this is done, you can call omap_mcbsp_start().
737 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300738void omap_mcbsp_set_spi_mode(unsigned int id,
739 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740{
741 struct omap_mcbsp_reg_cfg mcbsp_cfg;
742
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300743 if (!omap_mcbsp_check_valid_id(id)) {
744 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300746 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747
748 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
749
750 /* SPI has only one frame */
751 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
752 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
753
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300754 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100755 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
756 mcbsp_cfg.spcr1 |= (1 << 12);
757 else
758 mcbsp_cfg.spcr1 |= (3 << 11);
759
760 /* Set clock parities */
761 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
762 mcbsp_cfg.pcr0 |= CLKRP;
763 else
764 mcbsp_cfg.pcr0 &= ~CLKRP;
765
766 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
767 mcbsp_cfg.pcr0 &= ~CLKXP;
768 else
769 mcbsp_cfg.pcr0 |= CLKXP;
770
771 /* Set SCLKME to 0 and CLKSM to 1 */
772 mcbsp_cfg.pcr0 &= ~SCLKME;
773 mcbsp_cfg.srgr2 |= CLKSM;
774
775 /* Set FSXP */
776 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
777 mcbsp_cfg.pcr0 &= ~FSXP;
778 else
779 mcbsp_cfg.pcr0 |= FSXP;
780
781 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
782 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300783 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100784 mcbsp_cfg.pcr0 |= FSXM;
785 mcbsp_cfg.srgr2 &= ~FSGM;
786 mcbsp_cfg.xcr2 |= XDATDLY(1);
787 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300788 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789 mcbsp_cfg.pcr0 &= ~CLKXM;
790 mcbsp_cfg.srgr1 |= CLKGDV(1);
791 mcbsp_cfg.pcr0 &= ~FSXM;
792 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
793 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
794 }
795
796 mcbsp_cfg.xcr2 &= ~XPHASE;
797 mcbsp_cfg.rcr2 &= ~RPHASE;
798
799 omap_mcbsp_config(id, &mcbsp_cfg);
800}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300801EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802
803/*
804 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
805 * 730 has only 2 McBSP, and both of them are MPU peripherals.
806 */
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300807static int __init omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100808{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300809 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
810 int id = pdev->id - 1;
811 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100812
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300813 if (!pdata) {
814 dev_err(&pdev->dev, "McBSP device initialized without"
815 "platform data\n");
816 ret = -EINVAL;
817 goto exit;
818 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100819
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300820 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100821
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300822 if (id >= OMAP_MAX_MCBSP_COUNT) {
823 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
824 ret = -EINVAL;
825 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100826 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300828 spin_lock_init(&mcbsp[id].lock);
829 mcbsp[id].id = id + 1;
830 mcbsp[id].free = 1;
831 mcbsp[id].dma_tx_lch = -1;
832 mcbsp[id].dma_rx_lch = -1;
833
Russell King65846902008-09-03 23:46:18 +0100834 mcbsp[id].phys_base = pdata->phys_base;
Russell Kingd592dd12008-09-04 14:25:42 +0100835 mcbsp[id].io_base = ioremap(pdata->phys_base, SZ_4K);
836 if (!mcbsp[id].io_base) {
837 ret = -ENOMEM;
838 goto err_ioremap;
839 }
840
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300841 /* Default I/O is IRQ based */
842 mcbsp[id].io_type = OMAP_MCBSP_IRQ_IO;
843 mcbsp[id].tx_irq = pdata->tx_irq;
844 mcbsp[id].rx_irq = pdata->rx_irq;
845 mcbsp[id].dma_rx_sync = pdata->dma_rx_sync;
846 mcbsp[id].dma_tx_sync = pdata->dma_tx_sync;
847
848 if (pdata->clk_name)
849 mcbsp[id].clk = clk_get(&pdev->dev, pdata->clk_name);
850 if (IS_ERR(mcbsp[id].clk)) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300851 dev_err(&pdev->dev,
852 "Invalid clock configuration for McBSP%d.\n",
853 mcbsp[id].id);
Russell Kingd592dd12008-09-04 14:25:42 +0100854 ret = PTR_ERR(mcbsp[id].clk);
855 goto err_clk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300856 }
857
858 mcbsp[id].pdata = pdata;
859 mcbsp[id].dev = &pdev->dev;
860 platform_set_drvdata(pdev, &mcbsp[id]);
Russell Kingd592dd12008-09-04 14:25:42 +0100861 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300862
Russell Kingd592dd12008-09-04 14:25:42 +0100863err_clk:
864 iounmap(mcbsp[id].io_base);
865err_ioremap:
866 mcbsp[id].free = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300867exit:
868 return ret;
869}
870
871static int omap_mcbsp_remove(struct platform_device *pdev)
872{
873 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
874
875 platform_set_drvdata(pdev, NULL);
876 if (mcbsp) {
877
878 if (mcbsp->pdata && mcbsp->pdata->ops &&
879 mcbsp->pdata->ops->free)
880 mcbsp->pdata->ops->free(mcbsp->id);
881
882 clk_disable(mcbsp->clk);
883 clk_put(mcbsp->clk);
884
Russell Kingd592dd12008-09-04 14:25:42 +0100885 iounmap(mcbsp->io_base);
886
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300887 mcbsp->clk = NULL;
888 mcbsp->free = 0;
889 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100890 }
891
892 return 0;
893}
894
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300895static struct platform_driver omap_mcbsp_driver = {
896 .probe = omap_mcbsp_probe,
897 .remove = omap_mcbsp_remove,
898 .driver = {
899 .name = "omap-mcbsp",
900 },
901};
902
903int __init omap_mcbsp_init(void)
904{
905 /* Register the McBSP driver */
906 return platform_driver_register(&omap_mcbsp_driver);
907}
908