blob: 2c274e6f0de9230152d99204c2eb1252654fe6d0 [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));
320 syscon |= (ENAWAKEUP | SIDLEMODE(0x02) | CLOCKACTIVITY(0x02));
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300321 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
322
Eduardo Valentind9a9b3f2009-08-20 16:18:16 +0300323 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300324 }
325}
326
327static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
328{
329 /*
330 * Disable wakup behavior, smart idle and all wakeups
331 */
332 if (cpu_is_omap34xx()) {
333 u16 syscon;
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300334
335 syscon = OMAP_MCBSP_READ(mcbsp->io_base, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300336 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300337 OMAP_MCBSP_WRITE(mcbsp->io_base, SYSCON, syscon);
338
Eduardo Valentind9a9b3f2009-08-20 16:18:16 +0300339 OMAP_MCBSP_WRITE(mcbsp->io_base, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300340 }
341}
342#else
343static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
344static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300345#endif
346
Tony Lindgren120db2c2006-04-02 17:46:27 +0100347/*
348 * We can choose between IRQ based or polled IO.
349 * This needs to be called before omap_mcbsp_request().
350 */
351int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
352{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300353 struct omap_mcbsp *mcbsp;
354
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300355 if (!omap_mcbsp_check_valid_id(id)) {
356 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
357 return -ENODEV;
358 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300359 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100360
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300361 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100362
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300363 if (!mcbsp->free) {
364 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
365 mcbsp->id);
366 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100367 return -EINVAL;
368 }
369
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300370 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100371
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300372 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100373
374 return 0;
375}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300376EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100378int omap_mcbsp_request(unsigned int id)
379{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300380 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100381 int err;
382
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300383 if (!omap_mcbsp_check_valid_id(id)) {
384 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
385 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100386 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300387 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300388
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300389 spin_lock(&mcbsp->lock);
390 if (!mcbsp->free) {
391 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
392 mcbsp->id);
393 spin_unlock(&mcbsp->lock);
Russell Kingb820ce42009-01-23 10:26:46 +0000394 return -EBUSY;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100395 }
396
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300397 mcbsp->free = 0;
398 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100399
Russell Kingb820ce42009-01-23 10:26:46 +0000400 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
401 mcbsp->pdata->ops->request(id);
402
403 clk_enable(mcbsp->iclk);
404 clk_enable(mcbsp->fclk);
405
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300406 /* Do procedure specific to omap34xx arch, if applicable */
407 omap34xx_mcbsp_request(mcbsp);
408
Jarkko Nikula5a070552008-10-08 10:01:41 +0300409 /*
410 * Make sure that transmitter, receiver and sample-rate generator are
411 * not running before activating IRQs.
412 */
413 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
414 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
415
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300416 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100417 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300418 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300419 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
420 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100421 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300422 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
423 "for McBSP%d\n", mcbsp->tx_irq,
424 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100425 return err;
426 }
427
Jarkko Nikula5a070552008-10-08 10:01:41 +0300428 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300429 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
430 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100431 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300432 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
433 "for McBSP%d\n", mcbsp->rx_irq,
434 mcbsp->id);
435 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100436 return err;
437 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438 }
439
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100440 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300442EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443
444void omap_mcbsp_free(unsigned int id)
445{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300446 struct omap_mcbsp *mcbsp;
447
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300448 if (!omap_mcbsp_check_valid_id(id)) {
449 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100450 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100451 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300452 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100453
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300454 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
455 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300456
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300457 /* Do procedure specific to omap34xx arch, if applicable */
458 omap34xx_mcbsp_free(mcbsp);
459
Russell Kingb820ce42009-01-23 10:26:46 +0000460 clk_disable(mcbsp->fclk);
461 clk_disable(mcbsp->iclk);
462
463 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
464 /* Free IRQs */
465 free_irq(mcbsp->rx_irq, (void *)mcbsp);
466 free_irq(mcbsp->tx_irq, (void *)mcbsp);
467 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300469 spin_lock(&mcbsp->lock);
470 if (mcbsp->free) {
471 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
472 mcbsp->id);
473 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100474 return;
475 }
476
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300477 mcbsp->free = 1;
478 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300480EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481
482/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300483 * Here we start the McBSP, by enabling transmitter, receiver or both.
484 * If no transmitter or receiver is active prior calling, then sample-rate
485 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300487void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300489 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100490 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300491 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100492 u16 w;
493
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300494 if (!omap_mcbsp_check_valid_id(id)) {
495 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300497 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300498 mcbsp = id_to_mcbsp_ptr(id);
499 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300501 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
502 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100503
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300504 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
505 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
506
507 if (idle) {
508 /* Start the sample generator */
509 w = OMAP_MCBSP_READ(io_base, SPCR2);
510 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
511 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512
513 /* Enable transmitter and receiver */
514 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300515 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516
517 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300518 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100519
Eduardo Valentin44a63112009-08-20 16:18:09 +0300520 /*
521 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
522 * REVISIT: 100us may give enough time for two CLKSRG, however
523 * due to some unknown PM related, clock gating etc. reason it
524 * is now at 500us.
525 */
526 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100527
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300528 if (idle) {
529 /* Start frame sync */
530 w = OMAP_MCBSP_READ(io_base, SPCR2);
531 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
532 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100533
534 /* Dump McBSP Regs */
535 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100536}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300537EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300539void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100540{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300541 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100542 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300543 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100544 u16 w;
545
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300546 if (!omap_mcbsp_check_valid_id(id)) {
547 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300549 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300551 mcbsp = id_to_mcbsp_ptr(id);
552 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100553
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300554 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300556 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557
558 /* Reset receiver */
559 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300560 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300562 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
563 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
564
565 if (idle) {
566 /* Reset the sample rate generator */
567 w = OMAP_MCBSP_READ(io_base, SPCR2);
568 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
569 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100570}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300571EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100572
Eero Nurkkala9abea082009-08-20 16:18:07 +0300573void omap_mcbsp_xmit_enable(unsigned int id, u8 enable)
574{
575 struct omap_mcbsp *mcbsp;
576 void __iomem *io_base;
577 u16 w;
578
579 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
580 return;
581
582 if (!omap_mcbsp_check_valid_id(id)) {
583 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
584 return;
585 }
586
587 mcbsp = id_to_mcbsp_ptr(id);
588 io_base = mcbsp->io_base;
589
590 w = OMAP_MCBSP_READ(io_base, XCCR);
591
592 if (enable)
593 OMAP_MCBSP_WRITE(io_base, XCCR, w & ~(XDISABLE));
594 else
595 OMAP_MCBSP_WRITE(io_base, XCCR, w | XDISABLE);
596}
597EXPORT_SYMBOL(omap_mcbsp_xmit_enable);
598
599void omap_mcbsp_recv_enable(unsigned int id, u8 enable)
600{
601 struct omap_mcbsp *mcbsp;
602 void __iomem *io_base;
603 u16 w;
604
605 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
606 return;
607
608 if (!omap_mcbsp_check_valid_id(id)) {
609 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
610 return;
611 }
612
613 mcbsp = id_to_mcbsp_ptr(id);
614 io_base = mcbsp->io_base;
615
616 w = OMAP_MCBSP_READ(io_base, RCCR);
617
618 if (enable)
619 OMAP_MCBSP_WRITE(io_base, RCCR, w & ~(RDISABLE));
620 else
621 OMAP_MCBSP_WRITE(io_base, RCCR, w | RDISABLE);
622}
623EXPORT_SYMBOL(omap_mcbsp_recv_enable);
624
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100625/* polled mcbsp i/o operations */
626int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
627{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300628 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100629 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300630
631 if (!omap_mcbsp_check_valid_id(id)) {
632 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
633 return -ENODEV;
634 }
635
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300636 mcbsp = id_to_mcbsp_ptr(id);
637 base = mcbsp->io_base;
638
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100639 writew(buf, base + OMAP_MCBSP_REG_DXR1);
640 /* if frame sync error - clear the error */
641 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
642 /* clear error */
643 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
644 base + OMAP_MCBSP_REG_SPCR2);
645 /* resend */
646 return -1;
647 } else {
648 /* wait for transmit confirmation */
649 int attemps = 0;
650 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
651 if (attemps++ > 1000) {
652 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
653 (~XRST),
654 base + OMAP_MCBSP_REG_SPCR2);
655 udelay(10);
656 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
657 (XRST),
658 base + OMAP_MCBSP_REG_SPCR2);
659 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300660 dev_err(mcbsp->dev, "Could not write to"
661 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100662 return -2;
663 }
664 }
665 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300666
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100667 return 0;
668}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300669EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100670
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300671int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100672{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300673 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100674 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300675
676 if (!omap_mcbsp_check_valid_id(id)) {
677 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
678 return -ENODEV;
679 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300680 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300681
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300682 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100683 /* if frame sync error - clear the error */
684 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
685 /* clear error */
686 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
687 base + OMAP_MCBSP_REG_SPCR1);
688 /* resend */
689 return -1;
690 } else {
691 /* wait for recieve confirmation */
692 int attemps = 0;
693 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
694 if (attemps++ > 1000) {
695 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
696 (~RRST),
697 base + OMAP_MCBSP_REG_SPCR1);
698 udelay(10);
699 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
700 (RRST),
701 base + OMAP_MCBSP_REG_SPCR1);
702 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300703 dev_err(mcbsp->dev, "Could not read from"
704 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100705 return -2;
706 }
707 }
708 }
709 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300710
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100711 return 0;
712}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300713EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100714
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715/*
716 * IRQ based word transmission.
717 */
718void omap_mcbsp_xmit_word(unsigned int id, u32 word)
719{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300720 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100721 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300722 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300724 if (!omap_mcbsp_check_valid_id(id)) {
725 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300727 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300729 mcbsp = id_to_mcbsp_ptr(id);
730 io_base = mcbsp->io_base;
731 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300733 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734
735 if (word_length > OMAP_MCBSP_WORD_16)
736 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
737 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
738}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300739EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
741u32 omap_mcbsp_recv_word(unsigned int id)
742{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100744 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300746 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300748 if (!omap_mcbsp_check_valid_id(id)) {
749 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
750 return -ENODEV;
751 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300752 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300754 word_length = mcbsp->rx_word_length;
755 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300757 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758
759 if (word_length > OMAP_MCBSP_WORD_16)
760 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
761 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
762
763 return (word_lsb | (word_msb << 16));
764}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300765EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766
Tony Lindgren120db2c2006-04-02 17:46:27 +0100767int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
768{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300769 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100770 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300771 omap_mcbsp_word_length tx_word_length;
772 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100773 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
774
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300775 if (!omap_mcbsp_check_valid_id(id)) {
776 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
777 return -ENODEV;
778 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300779 mcbsp = id_to_mcbsp_ptr(id);
780 io_base = mcbsp->io_base;
781 tx_word_length = mcbsp->tx_word_length;
782 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300783
Tony Lindgren120db2c2006-04-02 17:46:27 +0100784 if (tx_word_length != rx_word_length)
785 return -EINVAL;
786
787 /* First we wait for the transmitter to be ready */
788 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
789 while (!(spcr2 & XRDY)) {
790 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
791 if (attempts++ > 1000) {
792 /* We must reset the transmitter */
793 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
794 udelay(10);
795 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
796 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300797 dev_err(mcbsp->dev, "McBSP%d transmitter not "
798 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100799 return -EAGAIN;
800 }
801 }
802
803 /* Now we can push the data */
804 if (tx_word_length > OMAP_MCBSP_WORD_16)
805 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
806 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
807
808 /* We wait for the receiver to be ready */
809 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
810 while (!(spcr1 & RRDY)) {
811 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
812 if (attempts++ > 1000) {
813 /* We must reset the receiver */
814 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
815 udelay(10);
816 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
817 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300818 dev_err(mcbsp->dev, "McBSP%d receiver not "
819 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100820 return -EAGAIN;
821 }
822 }
823
824 /* Receiver is ready, let's read the dummy data */
825 if (rx_word_length > OMAP_MCBSP_WORD_16)
826 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
827 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
828
829 return 0;
830}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300831EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100832
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300833int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100834{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300835 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100836 u32 clock_word = 0;
837 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300838 omap_mcbsp_word_length tx_word_length;
839 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100840 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
841
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300842 if (!omap_mcbsp_check_valid_id(id)) {
843 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
844 return -ENODEV;
845 }
846
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300847 mcbsp = id_to_mcbsp_ptr(id);
848 io_base = mcbsp->io_base;
849
850 tx_word_length = mcbsp->tx_word_length;
851 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300852
Tony Lindgren120db2c2006-04-02 17:46:27 +0100853 if (tx_word_length != rx_word_length)
854 return -EINVAL;
855
856 /* First we wait for the transmitter to be ready */
857 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
858 while (!(spcr2 & XRDY)) {
859 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
860 if (attempts++ > 1000) {
861 /* We must reset the transmitter */
862 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
863 udelay(10);
864 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
865 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300866 dev_err(mcbsp->dev, "McBSP%d transmitter not "
867 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100868 return -EAGAIN;
869 }
870 }
871
872 /* We first need to enable the bus clock */
873 if (tx_word_length > OMAP_MCBSP_WORD_16)
874 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
875 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
876
877 /* We wait for the receiver to be ready */
878 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
879 while (!(spcr1 & RRDY)) {
880 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
881 if (attempts++ > 1000) {
882 /* We must reset the receiver */
883 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
884 udelay(10);
885 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
886 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300887 dev_err(mcbsp->dev, "McBSP%d receiver not "
888 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100889 return -EAGAIN;
890 }
891 }
892
893 /* Receiver is ready, there is something for us */
894 if (rx_word_length > OMAP_MCBSP_WORD_16)
895 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
896 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
897
898 word[0] = (word_lsb | (word_msb << 16));
899
900 return 0;
901}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300902EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100903
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100904/*
905 * Simple DMA based buffer rx/tx routines.
906 * Nothing fancy, just a single buffer tx/rx through DMA.
907 * The DMA resources are released once the transfer is done.
908 * For anything fancier, you should use your own customized DMA
909 * routines and callbacks.
910 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300911int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
912 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300914 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100916 int src_port = 0;
917 int dest_port = 0;
918 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100919
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300920 if (!omap_mcbsp_check_valid_id(id)) {
921 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
922 return -ENODEV;
923 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300924 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100925
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300926 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300927 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300928 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300929 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300930 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300931 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300932 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933 return -EAGAIN;
934 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300935 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100936
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300937 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300938 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100939
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300940 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100941
Tony Lindgren120db2c2006-04-02 17:46:27 +0100942 if (cpu_class_is_omap1()) {
943 src_port = OMAP_DMA_PORT_TIPB;
944 dest_port = OMAP_DMA_PORT_EMIFF;
945 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300946 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300947 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100948
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300949 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100950 OMAP_DMA_DATA_TYPE_S16,
951 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000952 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100953 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100954
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300955 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100956 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300958 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000959 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300961 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100962 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100963 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000964 buffer,
965 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100966
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300967 omap_start_dma(mcbsp->dma_tx_lch);
968 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300969
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100970 return 0;
971}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300972EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100973
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300974int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
975 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100976{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300977 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100978 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100979 int src_port = 0;
980 int dest_port = 0;
981 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100982
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300983 if (!omap_mcbsp_check_valid_id(id)) {
984 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
985 return -ENODEV;
986 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300987 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100988
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300989 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300990 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300991 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300992 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300993 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300994 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300995 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100996 return -EAGAIN;
997 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300998 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100999
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001000 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001001 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001002
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001003 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001004
Tony Lindgren120db2c2006-04-02 17:46:27 +01001005 if (cpu_class_is_omap1()) {
1006 src_port = OMAP_DMA_PORT_TIPB;
1007 dest_port = OMAP_DMA_PORT_EMIFF;
1008 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001009 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001010 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001011
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001012 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001013 OMAP_DMA_DATA_TYPE_S16,
1014 length >> 1, 1,
1015 OMAP_DMA_SYNC_ELEMENT,
1016 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001017
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001018 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001019 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001020 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001021 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001022 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001023
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001024 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001025 dest_port,
1026 OMAP_DMA_AMODE_POST_INC,
1027 buffer,
1028 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001029
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001030 omap_start_dma(mcbsp->dma_rx_lch);
1031 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001032
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001033 return 0;
1034}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001035EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001036
1037/*
1038 * SPI wrapper.
1039 * Since SPI setup is much simpler than the generic McBSP one,
1040 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1041 * Once this is done, you can call omap_mcbsp_start().
1042 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001043void omap_mcbsp_set_spi_mode(unsigned int id,
1044 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001045{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001046 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001047 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1048
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001049 if (!omap_mcbsp_check_valid_id(id)) {
1050 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001051 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001052 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001053 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001054
1055 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1056
1057 /* SPI has only one frame */
1058 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1059 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1060
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001061 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001062 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1063 mcbsp_cfg.spcr1 |= (1 << 12);
1064 else
1065 mcbsp_cfg.spcr1 |= (3 << 11);
1066
1067 /* Set clock parities */
1068 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1069 mcbsp_cfg.pcr0 |= CLKRP;
1070 else
1071 mcbsp_cfg.pcr0 &= ~CLKRP;
1072
1073 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1074 mcbsp_cfg.pcr0 &= ~CLKXP;
1075 else
1076 mcbsp_cfg.pcr0 |= CLKXP;
1077
1078 /* Set SCLKME to 0 and CLKSM to 1 */
1079 mcbsp_cfg.pcr0 &= ~SCLKME;
1080 mcbsp_cfg.srgr2 |= CLKSM;
1081
1082 /* Set FSXP */
1083 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1084 mcbsp_cfg.pcr0 &= ~FSXP;
1085 else
1086 mcbsp_cfg.pcr0 |= FSXP;
1087
1088 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1089 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001090 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001091 mcbsp_cfg.pcr0 |= FSXM;
1092 mcbsp_cfg.srgr2 &= ~FSGM;
1093 mcbsp_cfg.xcr2 |= XDATDLY(1);
1094 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001095 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096 mcbsp_cfg.pcr0 &= ~CLKXM;
1097 mcbsp_cfg.srgr1 |= CLKGDV(1);
1098 mcbsp_cfg.pcr0 &= ~FSXM;
1099 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1100 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1101 }
1102
1103 mcbsp_cfg.xcr2 &= ~XPHASE;
1104 mcbsp_cfg.rcr2 &= ~RPHASE;
1105
1106 omap_mcbsp_config(id, &mcbsp_cfg);
1107}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001108EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001109
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001110#ifdef CONFIG_ARCH_OMAP34XX
1111#define max_thres(m) (mcbsp->pdata->buffer_size)
1112#define valid_threshold(m, val) ((val) <= max_thres(m))
1113#define THRESHOLD_PROP_BUILDER(prop) \
1114static ssize_t prop##_show(struct device *dev, \
1115 struct device_attribute *attr, char *buf) \
1116{ \
1117 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1118 \
1119 return sprintf(buf, "%u\n", mcbsp->prop); \
1120} \
1121 \
1122static ssize_t prop##_store(struct device *dev, \
1123 struct device_attribute *attr, \
1124 const char *buf, size_t size) \
1125{ \
1126 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1127 unsigned long val; \
1128 int status; \
1129 \
1130 status = strict_strtoul(buf, 0, &val); \
1131 if (status) \
1132 return status; \
1133 \
1134 if (!valid_threshold(mcbsp, val)) \
1135 return -EDOM; \
1136 \
1137 mcbsp->prop = val; \
1138 return size; \
1139} \
1140 \
1141static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1142
1143THRESHOLD_PROP_BUILDER(max_tx_thres);
1144THRESHOLD_PROP_BUILDER(max_rx_thres);
1145
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001146static ssize_t dma_op_mode_show(struct device *dev,
1147 struct device_attribute *attr, char *buf)
1148{
1149 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1150 int dma_op_mode;
1151
1152 spin_lock_irq(&mcbsp->lock);
1153 dma_op_mode = mcbsp->dma_op_mode;
1154 spin_unlock_irq(&mcbsp->lock);
1155
1156 return sprintf(buf, "current mode: %d\n"
1157 "possible mode values are:\n"
1158 "%d - %s\n"
1159 "%d - %s\n"
1160 "%d - %s\n",
1161 dma_op_mode,
1162 MCBSP_DMA_MODE_ELEMENT, "element mode",
1163 MCBSP_DMA_MODE_THRESHOLD, "threshold mode",
1164 MCBSP_DMA_MODE_FRAME, "frame mode");
1165}
1166
1167static ssize_t dma_op_mode_store(struct device *dev,
1168 struct device_attribute *attr,
1169 const char *buf, size_t size)
1170{
1171 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1172 unsigned long val;
1173 int status;
1174
1175 status = strict_strtoul(buf, 0, &val);
1176 if (status)
1177 return status;
1178
1179 spin_lock_irq(&mcbsp->lock);
1180
1181 if (!mcbsp->free) {
1182 size = -EBUSY;
1183 goto unlock;
1184 }
1185
1186 if (val > MCBSP_DMA_MODE_FRAME || val < MCBSP_DMA_MODE_ELEMENT) {
1187 size = -EINVAL;
1188 goto unlock;
1189 }
1190
1191 mcbsp->dma_op_mode = val;
1192
1193unlock:
1194 spin_unlock_irq(&mcbsp->lock);
1195
1196 return size;
1197}
1198
1199static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1200
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001201static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001202 &dev_attr_max_tx_thres.attr,
1203 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001204 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001205 NULL,
1206};
1207
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001208static const struct attribute_group additional_attr_group = {
1209 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001210};
1211
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001212static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001213{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001214 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001215}
1216
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001217static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001218{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001219 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001220}
1221
1222static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1223{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001224 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001225 if (cpu_is_omap34xx()) {
1226 mcbsp->max_tx_thres = max_thres(mcbsp);
1227 mcbsp->max_rx_thres = max_thres(mcbsp);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001228 /*
1229 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1230 * for mcbsp2 instances.
1231 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001232 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001233 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001234 "Unable to create additional controls\n");
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001235 } else {
1236 mcbsp->max_tx_thres = -EINVAL;
1237 mcbsp->max_rx_thres = -EINVAL;
1238 }
1239}
1240
1241static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1242{
1243 if (cpu_is_omap34xx())
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001244 omap_additional_remove(mcbsp->dev);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001245}
1246#else
1247static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1248static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
1249#endif /* CONFIG_ARCH_OMAP34XX */
1250
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001251/*
1252 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1253 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1254 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001255static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001256{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001257 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001258 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001259 int id = pdev->id - 1;
1260 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001261
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001262 if (!pdata) {
1263 dev_err(&pdev->dev, "McBSP device initialized without"
1264 "platform data\n");
1265 ret = -EINVAL;
1266 goto exit;
1267 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001268
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001269 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001270
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001271 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001272 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1273 ret = -EINVAL;
1274 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001275 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001276
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001277 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1278 if (!mcbsp) {
1279 ret = -ENOMEM;
1280 goto exit;
1281 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001282
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001283 spin_lock_init(&mcbsp->lock);
1284 mcbsp->id = id + 1;
1285 mcbsp->free = 1;
1286 mcbsp->dma_tx_lch = -1;
1287 mcbsp->dma_rx_lch = -1;
1288
1289 mcbsp->phys_base = pdata->phys_base;
1290 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1291 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001292 ret = -ENOMEM;
1293 goto err_ioremap;
1294 }
1295
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001296 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001297 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1298 mcbsp->tx_irq = pdata->tx_irq;
1299 mcbsp->rx_irq = pdata->rx_irq;
1300 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1301 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001302
Russell Kingb820ce42009-01-23 10:26:46 +00001303 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1304 if (IS_ERR(mcbsp->iclk)) {
1305 ret = PTR_ERR(mcbsp->iclk);
1306 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1307 goto err_iclk;
1308 }
Stanley.Miao06151152009-01-29 08:57:12 -08001309
Russell Kingb820ce42009-01-23 10:26:46 +00001310 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1311 if (IS_ERR(mcbsp->fclk)) {
1312 ret = PTR_ERR(mcbsp->fclk);
1313 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1314 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001315 }
1316
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001317 mcbsp->pdata = pdata;
1318 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001319 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001320 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001321
1322 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1323 omap34xx_device_init(mcbsp);
1324
Russell Kingd592dd12008-09-04 14:25:42 +01001325 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001326
Russell Kingb820ce42009-01-23 10:26:46 +00001327err_fclk:
1328 clk_put(mcbsp->iclk);
1329err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001330 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001331err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001332 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001333exit:
1334 return ret;
1335}
1336
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001337static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001338{
1339 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1340
1341 platform_set_drvdata(pdev, NULL);
1342 if (mcbsp) {
1343
1344 if (mcbsp->pdata && mcbsp->pdata->ops &&
1345 mcbsp->pdata->ops->free)
1346 mcbsp->pdata->ops->free(mcbsp->id);
1347
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001348 omap34xx_device_exit(mcbsp);
1349
Russell Kingb820ce42009-01-23 10:26:46 +00001350 clk_disable(mcbsp->fclk);
1351 clk_disable(mcbsp->iclk);
1352 clk_put(mcbsp->fclk);
1353 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001354
Russell Kingd592dd12008-09-04 14:25:42 +01001355 iounmap(mcbsp->io_base);
1356
Russell Kingb820ce42009-01-23 10:26:46 +00001357 mcbsp->fclk = NULL;
1358 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001359 mcbsp->free = 0;
1360 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001361 }
1362
1363 return 0;
1364}
1365
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001366static struct platform_driver omap_mcbsp_driver = {
1367 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001368 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001369 .driver = {
1370 .name = "omap-mcbsp",
1371 },
1372};
1373
1374int __init omap_mcbsp_init(void)
1375{
1376 /* Register the McBSP driver */
1377 return platform_driver_register(&omap_mcbsp_driver);
1378}