blob: 081f2eace92d3b00e749cda42f08a8136cff98e4 [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);
249#endif
250
Tony Lindgren120db2c2006-04-02 17:46:27 +0100251/*
252 * We can choose between IRQ based or polled IO.
253 * This needs to be called before omap_mcbsp_request().
254 */
255int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
256{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300257 struct omap_mcbsp *mcbsp;
258
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300259 if (!omap_mcbsp_check_valid_id(id)) {
260 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
261 return -ENODEV;
262 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300263 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100264
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300265 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100266
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300267 if (!mcbsp->free) {
268 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
269 mcbsp->id);
270 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100271 return -EINVAL;
272 }
273
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300274 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100275
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300276 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100277
278 return 0;
279}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300280EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100281
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100282int omap_mcbsp_request(unsigned int id)
283{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300284 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100285 int err;
286
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300287 if (!omap_mcbsp_check_valid_id(id)) {
288 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
289 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100290 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300291 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300292
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300293 spin_lock(&mcbsp->lock);
294 if (!mcbsp->free) {
295 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
296 mcbsp->id);
297 spin_unlock(&mcbsp->lock);
Russell Kingb820ce42009-01-23 10:26:46 +0000298 return -EBUSY;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100299 }
300
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300301 mcbsp->free = 0;
302 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100303
Russell Kingb820ce42009-01-23 10:26:46 +0000304 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
305 mcbsp->pdata->ops->request(id);
306
307 clk_enable(mcbsp->iclk);
308 clk_enable(mcbsp->fclk);
309
Jarkko Nikula5a070552008-10-08 10:01:41 +0300310 /*
311 * Make sure that transmitter, receiver and sample-rate generator are
312 * not running before activating IRQs.
313 */
314 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
315 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
316
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300317 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100318 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300319 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300320 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
321 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100322 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300323 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
324 "for McBSP%d\n", mcbsp->tx_irq,
325 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100326 return err;
327 }
328
Jarkko Nikula5a070552008-10-08 10:01:41 +0300329 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300330 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
331 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100332 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300333 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
334 "for McBSP%d\n", mcbsp->rx_irq,
335 mcbsp->id);
336 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100337 return err;
338 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339 }
340
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100341 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300343EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100344
345void omap_mcbsp_free(unsigned int id)
346{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300347 struct omap_mcbsp *mcbsp;
348
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300349 if (!omap_mcbsp_check_valid_id(id)) {
350 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100351 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100352 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300353 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100354
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300355 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
356 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300357
Russell Kingb820ce42009-01-23 10:26:46 +0000358 clk_disable(mcbsp->fclk);
359 clk_disable(mcbsp->iclk);
360
361 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
362 /* Free IRQs */
363 free_irq(mcbsp->rx_irq, (void *)mcbsp);
364 free_irq(mcbsp->tx_irq, (void *)mcbsp);
365 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300367 spin_lock(&mcbsp->lock);
368 if (mcbsp->free) {
369 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
370 mcbsp->id);
371 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100372 return;
373 }
374
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300375 mcbsp->free = 1;
376 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300378EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100379
380/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300381 * Here we start the McBSP, by enabling transmitter, receiver or both.
382 * If no transmitter or receiver is active prior calling, then sample-rate
383 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300385void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300387 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100388 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300389 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100390 u16 w;
391
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300392 if (!omap_mcbsp_check_valid_id(id)) {
393 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300395 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300396 mcbsp = id_to_mcbsp_ptr(id);
397 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100398
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300399 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
400 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100401
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300402 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
403 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
404
405 if (idle) {
406 /* Start the sample generator */
407 w = OMAP_MCBSP_READ(io_base, SPCR2);
408 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
409 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100410
411 /* Enable transmitter and receiver */
412 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300413 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414
415 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300416 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100417
Eduardo Valentin44a63112009-08-20 16:18:09 +0300418 /*
419 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
420 * REVISIT: 100us may give enough time for two CLKSRG, however
421 * due to some unknown PM related, clock gating etc. reason it
422 * is now at 500us.
423 */
424 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100425
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300426 if (idle) {
427 /* Start frame sync */
428 w = OMAP_MCBSP_READ(io_base, SPCR2);
429 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
430 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431
432 /* Dump McBSP Regs */
433 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100434}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300435EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100436
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300437void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300439 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100440 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300441 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100442 u16 w;
443
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300444 if (!omap_mcbsp_check_valid_id(id)) {
445 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100446 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300447 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100448
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300449 mcbsp = id_to_mcbsp_ptr(id);
450 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100451
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300452 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300454 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455
456 /* Reset receiver */
457 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300458 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100459
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300460 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
461 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
462
463 if (idle) {
464 /* Reset the sample rate generator */
465 w = OMAP_MCBSP_READ(io_base, SPCR2);
466 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
467 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300469EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100470
Eero Nurkkala9abea082009-08-20 16:18:07 +0300471void omap_mcbsp_xmit_enable(unsigned int id, u8 enable)
472{
473 struct omap_mcbsp *mcbsp;
474 void __iomem *io_base;
475 u16 w;
476
477 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
478 return;
479
480 if (!omap_mcbsp_check_valid_id(id)) {
481 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
482 return;
483 }
484
485 mcbsp = id_to_mcbsp_ptr(id);
486 io_base = mcbsp->io_base;
487
488 w = OMAP_MCBSP_READ(io_base, XCCR);
489
490 if (enable)
491 OMAP_MCBSP_WRITE(io_base, XCCR, w & ~(XDISABLE));
492 else
493 OMAP_MCBSP_WRITE(io_base, XCCR, w | XDISABLE);
494}
495EXPORT_SYMBOL(omap_mcbsp_xmit_enable);
496
497void omap_mcbsp_recv_enable(unsigned int id, u8 enable)
498{
499 struct omap_mcbsp *mcbsp;
500 void __iomem *io_base;
501 u16 w;
502
503 if (!(cpu_is_omap2430() || cpu_is_omap34xx()))
504 return;
505
506 if (!omap_mcbsp_check_valid_id(id)) {
507 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
508 return;
509 }
510
511 mcbsp = id_to_mcbsp_ptr(id);
512 io_base = mcbsp->io_base;
513
514 w = OMAP_MCBSP_READ(io_base, RCCR);
515
516 if (enable)
517 OMAP_MCBSP_WRITE(io_base, RCCR, w & ~(RDISABLE));
518 else
519 OMAP_MCBSP_WRITE(io_base, RCCR, w | RDISABLE);
520}
521EXPORT_SYMBOL(omap_mcbsp_recv_enable);
522
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100523/* polled mcbsp i/o operations */
524int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
525{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300526 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100527 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300528
529 if (!omap_mcbsp_check_valid_id(id)) {
530 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
531 return -ENODEV;
532 }
533
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300534 mcbsp = id_to_mcbsp_ptr(id);
535 base = mcbsp->io_base;
536
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100537 writew(buf, base + OMAP_MCBSP_REG_DXR1);
538 /* if frame sync error - clear the error */
539 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
540 /* clear error */
541 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
542 base + OMAP_MCBSP_REG_SPCR2);
543 /* resend */
544 return -1;
545 } else {
546 /* wait for transmit confirmation */
547 int attemps = 0;
548 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
549 if (attemps++ > 1000) {
550 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
551 (~XRST),
552 base + OMAP_MCBSP_REG_SPCR2);
553 udelay(10);
554 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
555 (XRST),
556 base + OMAP_MCBSP_REG_SPCR2);
557 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300558 dev_err(mcbsp->dev, "Could not write to"
559 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100560 return -2;
561 }
562 }
563 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300564
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100565 return 0;
566}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300567EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100568
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300569int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100570{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300571 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100572 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300573
574 if (!omap_mcbsp_check_valid_id(id)) {
575 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
576 return -ENODEV;
577 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300578 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300579
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300580 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100581 /* if frame sync error - clear the error */
582 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
583 /* clear error */
584 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
585 base + OMAP_MCBSP_REG_SPCR1);
586 /* resend */
587 return -1;
588 } else {
589 /* wait for recieve confirmation */
590 int attemps = 0;
591 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
592 if (attemps++ > 1000) {
593 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
594 (~RRST),
595 base + OMAP_MCBSP_REG_SPCR1);
596 udelay(10);
597 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
598 (RRST),
599 base + OMAP_MCBSP_REG_SPCR1);
600 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300601 dev_err(mcbsp->dev, "Could not read from"
602 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100603 return -2;
604 }
605 }
606 }
607 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300608
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100609 return 0;
610}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300611EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100612
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613/*
614 * IRQ based word transmission.
615 */
616void omap_mcbsp_xmit_word(unsigned int id, u32 word)
617{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300618 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100619 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300620 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100621
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300622 if (!omap_mcbsp_check_valid_id(id)) {
623 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100624 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300625 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100626
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300627 mcbsp = id_to_mcbsp_ptr(id);
628 io_base = mcbsp->io_base;
629 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300631 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632
633 if (word_length > OMAP_MCBSP_WORD_16)
634 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
635 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
636}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300637EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100638
639u32 omap_mcbsp_recv_word(unsigned int id)
640{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300641 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100642 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100643 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300644 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300646 if (!omap_mcbsp_check_valid_id(id)) {
647 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
648 return -ENODEV;
649 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300650 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300652 word_length = mcbsp->rx_word_length;
653 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100654
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300655 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656
657 if (word_length > OMAP_MCBSP_WORD_16)
658 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
659 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
660
661 return (word_lsb | (word_msb << 16));
662}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300663EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664
Tony Lindgren120db2c2006-04-02 17:46:27 +0100665int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
666{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300667 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100668 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300669 omap_mcbsp_word_length tx_word_length;
670 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100671 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
672
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300673 if (!omap_mcbsp_check_valid_id(id)) {
674 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
675 return -ENODEV;
676 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300677 mcbsp = id_to_mcbsp_ptr(id);
678 io_base = mcbsp->io_base;
679 tx_word_length = mcbsp->tx_word_length;
680 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300681
Tony Lindgren120db2c2006-04-02 17:46:27 +0100682 if (tx_word_length != rx_word_length)
683 return -EINVAL;
684
685 /* First we wait for the transmitter to be ready */
686 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
687 while (!(spcr2 & XRDY)) {
688 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
689 if (attempts++ > 1000) {
690 /* We must reset the transmitter */
691 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
692 udelay(10);
693 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
694 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300695 dev_err(mcbsp->dev, "McBSP%d transmitter not "
696 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100697 return -EAGAIN;
698 }
699 }
700
701 /* Now we can push the data */
702 if (tx_word_length > OMAP_MCBSP_WORD_16)
703 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
704 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
705
706 /* We wait for the receiver to be ready */
707 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
708 while (!(spcr1 & RRDY)) {
709 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
710 if (attempts++ > 1000) {
711 /* We must reset the receiver */
712 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
713 udelay(10);
714 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
715 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300716 dev_err(mcbsp->dev, "McBSP%d receiver not "
717 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100718 return -EAGAIN;
719 }
720 }
721
722 /* Receiver is ready, let's read the dummy data */
723 if (rx_word_length > OMAP_MCBSP_WORD_16)
724 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
725 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
726
727 return 0;
728}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300729EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100730
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300731int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100732{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300733 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100734 u32 clock_word = 0;
735 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300736 omap_mcbsp_word_length tx_word_length;
737 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100738 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
739
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300740 if (!omap_mcbsp_check_valid_id(id)) {
741 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
742 return -ENODEV;
743 }
744
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300745 mcbsp = id_to_mcbsp_ptr(id);
746 io_base = mcbsp->io_base;
747
748 tx_word_length = mcbsp->tx_word_length;
749 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300750
Tony Lindgren120db2c2006-04-02 17:46:27 +0100751 if (tx_word_length != rx_word_length)
752 return -EINVAL;
753
754 /* First we wait for the transmitter to be ready */
755 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
756 while (!(spcr2 & XRDY)) {
757 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
758 if (attempts++ > 1000) {
759 /* We must reset the transmitter */
760 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
761 udelay(10);
762 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
763 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300764 dev_err(mcbsp->dev, "McBSP%d transmitter not "
765 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100766 return -EAGAIN;
767 }
768 }
769
770 /* We first need to enable the bus clock */
771 if (tx_word_length > OMAP_MCBSP_WORD_16)
772 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
773 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
774
775 /* We wait for the receiver to be ready */
776 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
777 while (!(spcr1 & RRDY)) {
778 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
779 if (attempts++ > 1000) {
780 /* We must reset the receiver */
781 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
782 udelay(10);
783 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
784 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300785 dev_err(mcbsp->dev, "McBSP%d receiver not "
786 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100787 return -EAGAIN;
788 }
789 }
790
791 /* Receiver is ready, there is something for us */
792 if (rx_word_length > OMAP_MCBSP_WORD_16)
793 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
794 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
795
796 word[0] = (word_lsb | (word_msb << 16));
797
798 return 0;
799}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300800EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100801
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802/*
803 * Simple DMA based buffer rx/tx routines.
804 * Nothing fancy, just a single buffer tx/rx through DMA.
805 * The DMA resources are released once the transfer is done.
806 * For anything fancier, you should use your own customized DMA
807 * routines and callbacks.
808 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300809int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
810 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100811{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300812 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100813 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100814 int src_port = 0;
815 int dest_port = 0;
816 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100817
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300818 if (!omap_mcbsp_check_valid_id(id)) {
819 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
820 return -ENODEV;
821 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300822 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100823
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300824 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300825 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300826 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300827 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300828 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300829 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300830 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100831 return -EAGAIN;
832 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300833 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300835 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300836 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100837
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300838 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100839
Tony Lindgren120db2c2006-04-02 17:46:27 +0100840 if (cpu_class_is_omap1()) {
841 src_port = OMAP_DMA_PORT_TIPB;
842 dest_port = OMAP_DMA_PORT_EMIFF;
843 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300844 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300845 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100846
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300847 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100848 OMAP_DMA_DATA_TYPE_S16,
849 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000850 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100851 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100852
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300853 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100854 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100855 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300856 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000857 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300859 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100860 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000862 buffer,
863 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100864
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300865 omap_start_dma(mcbsp->dma_tx_lch);
866 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300867
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100868 return 0;
869}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300870EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100871
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300872int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
873 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100874{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300875 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100877 int src_port = 0;
878 int dest_port = 0;
879 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100880
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300881 if (!omap_mcbsp_check_valid_id(id)) {
882 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
883 return -ENODEV;
884 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300885 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300887 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300888 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300889 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300890 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300891 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300892 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300893 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100894 return -EAGAIN;
895 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300896 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100897
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300898 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300899 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100900
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300901 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100902
Tony Lindgren120db2c2006-04-02 17:46:27 +0100903 if (cpu_class_is_omap1()) {
904 src_port = OMAP_DMA_PORT_TIPB;
905 dest_port = OMAP_DMA_PORT_EMIFF;
906 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300907 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300908 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100909
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300910 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300911 OMAP_DMA_DATA_TYPE_S16,
912 length >> 1, 1,
913 OMAP_DMA_SYNC_ELEMENT,
914 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300916 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100917 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100918 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300919 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000920 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300922 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300923 dest_port,
924 OMAP_DMA_AMODE_POST_INC,
925 buffer,
926 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100927
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300928 omap_start_dma(mcbsp->dma_rx_lch);
929 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300930
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100931 return 0;
932}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300933EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100934
935/*
936 * SPI wrapper.
937 * Since SPI setup is much simpler than the generic McBSP one,
938 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
939 * Once this is done, you can call omap_mcbsp_start().
940 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300941void omap_mcbsp_set_spi_mode(unsigned int id,
942 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100943{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300944 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100945 struct omap_mcbsp_reg_cfg mcbsp_cfg;
946
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300947 if (!omap_mcbsp_check_valid_id(id)) {
948 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300950 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300951 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100952
953 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
954
955 /* SPI has only one frame */
956 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
957 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
958
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300959 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
961 mcbsp_cfg.spcr1 |= (1 << 12);
962 else
963 mcbsp_cfg.spcr1 |= (3 << 11);
964
965 /* Set clock parities */
966 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
967 mcbsp_cfg.pcr0 |= CLKRP;
968 else
969 mcbsp_cfg.pcr0 &= ~CLKRP;
970
971 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
972 mcbsp_cfg.pcr0 &= ~CLKXP;
973 else
974 mcbsp_cfg.pcr0 |= CLKXP;
975
976 /* Set SCLKME to 0 and CLKSM to 1 */
977 mcbsp_cfg.pcr0 &= ~SCLKME;
978 mcbsp_cfg.srgr2 |= CLKSM;
979
980 /* Set FSXP */
981 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
982 mcbsp_cfg.pcr0 &= ~FSXP;
983 else
984 mcbsp_cfg.pcr0 |= FSXP;
985
986 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
987 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300988 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100989 mcbsp_cfg.pcr0 |= FSXM;
990 mcbsp_cfg.srgr2 &= ~FSGM;
991 mcbsp_cfg.xcr2 |= XDATDLY(1);
992 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300993 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100994 mcbsp_cfg.pcr0 &= ~CLKXM;
995 mcbsp_cfg.srgr1 |= CLKGDV(1);
996 mcbsp_cfg.pcr0 &= ~FSXM;
997 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
998 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
999 }
1000
1001 mcbsp_cfg.xcr2 &= ~XPHASE;
1002 mcbsp_cfg.rcr2 &= ~RPHASE;
1003
1004 omap_mcbsp_config(id, &mcbsp_cfg);
1005}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001006EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001007
1008/*
1009 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1010 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1011 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001012static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001013{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001014 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001015 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001016 int id = pdev->id - 1;
1017 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001018
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001019 if (!pdata) {
1020 dev_err(&pdev->dev, "McBSP device initialized without"
1021 "platform data\n");
1022 ret = -EINVAL;
1023 goto exit;
1024 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001025
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001026 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001027
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001028 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001029 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1030 ret = -EINVAL;
1031 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001032 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001033
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001034 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1035 if (!mcbsp) {
1036 ret = -ENOMEM;
1037 goto exit;
1038 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001039
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001040 spin_lock_init(&mcbsp->lock);
1041 mcbsp->id = id + 1;
1042 mcbsp->free = 1;
1043 mcbsp->dma_tx_lch = -1;
1044 mcbsp->dma_rx_lch = -1;
1045
1046 mcbsp->phys_base = pdata->phys_base;
1047 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1048 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001049 ret = -ENOMEM;
1050 goto err_ioremap;
1051 }
1052
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001053 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001054 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1055 mcbsp->tx_irq = pdata->tx_irq;
1056 mcbsp->rx_irq = pdata->rx_irq;
1057 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1058 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001059
Russell Kingb820ce42009-01-23 10:26:46 +00001060 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1061 if (IS_ERR(mcbsp->iclk)) {
1062 ret = PTR_ERR(mcbsp->iclk);
1063 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1064 goto err_iclk;
1065 }
Stanley.Miao06151152009-01-29 08:57:12 -08001066
Russell Kingb820ce42009-01-23 10:26:46 +00001067 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1068 if (IS_ERR(mcbsp->fclk)) {
1069 ret = PTR_ERR(mcbsp->fclk);
1070 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1071 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001072 }
1073
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001074 mcbsp->pdata = pdata;
1075 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001076 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001077 platform_set_drvdata(pdev, mcbsp);
Russell Kingd592dd12008-09-04 14:25:42 +01001078 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001079
Russell Kingb820ce42009-01-23 10:26:46 +00001080err_fclk:
1081 clk_put(mcbsp->iclk);
1082err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001083 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001084err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001085 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001086exit:
1087 return ret;
1088}
1089
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001090static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001091{
1092 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1093
1094 platform_set_drvdata(pdev, NULL);
1095 if (mcbsp) {
1096
1097 if (mcbsp->pdata && mcbsp->pdata->ops &&
1098 mcbsp->pdata->ops->free)
1099 mcbsp->pdata->ops->free(mcbsp->id);
1100
Russell Kingb820ce42009-01-23 10:26:46 +00001101 clk_disable(mcbsp->fclk);
1102 clk_disable(mcbsp->iclk);
1103 clk_put(mcbsp->fclk);
1104 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001105
Russell Kingd592dd12008-09-04 14:25:42 +01001106 iounmap(mcbsp->io_base);
1107
Russell Kingb820ce42009-01-23 10:26:46 +00001108 mcbsp->fclk = NULL;
1109 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001110 mcbsp->free = 0;
1111 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001112 }
1113
1114 return 0;
1115}
1116
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001117static struct platform_driver omap_mcbsp_driver = {
1118 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001119 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001120 .driver = {
1121 .name = "omap-mcbsp",
1122 },
1123};
1124
1125int __init omap_mcbsp_init(void)
1126{
1127 /* Register the McBSP driver */
1128 return platform_driver_register(&omap_mcbsp_driver);
1129}