blob: 4ef46047b787a6c363737e7c0ed4c5509651cd59 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010027
Tony Lindgrence491cf2009-10-20 09:40:47 -070028#include <plat/dma.h>
29#include <plat/mcbsp.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030
Eero Nurkkalad912fa92010-02-22 12:21:11 +000031#include "../mach-omap2/cm-regbits-34xx.h"
32
Chandra Shekharb4b58f52008-10-08 10:01:39 +030033struct omap_mcbsp **mcbsp_ptr;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080034int omap_mcbsp_count, omap_mcbsp_cache_size;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030035
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080036void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030037{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080038 if (cpu_class_is_omap1()) {
39 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080040 __raw_writew((u16)val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080041 } else if (cpu_is_omap2420()) {
42 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
43 __raw_writew((u16)val, mcbsp->io_base + reg);
44 } else {
45 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080046 __raw_writel(val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080047 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030048}
49
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080050int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030051{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080052 if (cpu_class_is_omap1()) {
53 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
54 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
55 } else if (cpu_is_omap2420()) {
56 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
57 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
58 } else {
59 return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
60 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
61 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030062}
63
Eero Nurkkalad912fa92010-02-22 12:21:11 +000064#ifdef CONFIG_ARCH_OMAP3
65void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
66{
67 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
68}
69
70int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
71{
72 return __raw_readl(mcbsp->st_data->io_base_st + reg);
73}
74#endif
75
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080076#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080077 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080078#define MCBSP_WRITE(mcbsp, reg, val) \
79 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080080#define MCBSP_READ_CACHE(mcbsp, reg) \
81 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030082
83#define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
84#define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
Eero Nurkkalad912fa92010-02-22 12:21:11 +000086#define MCBSP_ST_READ(mcbsp, reg) \
87 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
88#define MCBSP_ST_WRITE(mcbsp, reg, val) \
89 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
90
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010091static void omap_mcbsp_dump_reg(u8 id)
92{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030093 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
94
95 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
96 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080097 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030098 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080099 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300100 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800101 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300102 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800103 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300104 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800105 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300106 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800107 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300108 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800109 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300110 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800111 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300112 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800113 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300114 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800115 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300116 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800117 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300118 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800119 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300120 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800121 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300122 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123}
124
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700125static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100126{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400127 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700128 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100129
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800130 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700131 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700133 if (irqst_spcr2 & XSYNC_ERR) {
134 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
135 irqst_spcr2);
136 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000137 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700138 } else {
139 complete(&mcbsp_tx->tx_irq_completion);
140 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300141
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142 return IRQ_HANDLED;
143}
144
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700145static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100146{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400147 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700148 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100149
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800150 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700151 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700153 if (irqst_spcr1 & RSYNC_ERR) {
154 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
155 irqst_spcr1);
156 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000157 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700158 } else {
159 complete(&mcbsp_rx->tx_irq_completion);
160 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300161
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162 return IRQ_HANDLED;
163}
164
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100165static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
166{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400167 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300169 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800170 MCBSP_READ(mcbsp_dma_tx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171
172 /* We can free the channels */
173 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
174 mcbsp_dma_tx->dma_tx_lch = -1;
175
176 complete(&mcbsp_dma_tx->tx_dma_completion);
177}
178
179static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
180{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400181 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100182
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300183 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800184 MCBSP_READ(mcbsp_dma_rx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100185
186 /* We can free the channels */
187 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
188 mcbsp_dma_rx->dma_rx_lch = -1;
189
190 complete(&mcbsp_dma_rx->rx_dma_completion);
191}
192
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100193/*
194 * omap_mcbsp_config simply write a config to the
195 * appropriate McBSP.
196 * You either call this function or set the McBSP registers
197 * by yourself before calling omap_mcbsp_start().
198 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300199void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100200{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300201 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100202
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300203 if (!omap_mcbsp_check_valid_id(id)) {
204 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
205 return;
206 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300207 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300208
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300209 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
210 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100211
212 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800213 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
214 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
215 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
216 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
217 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
218 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
219 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
220 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
221 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
222 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
223 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Syed Rafiuddina5b92cc2009-07-28 18:57:10 +0530224 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800225 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
226 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200227 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100228}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300229EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100230
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800231#ifdef CONFIG_ARCH_OMAP3
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000232static void omap_st_on(struct omap_mcbsp *mcbsp)
233{
234 unsigned int w;
235
236 /*
237 * Sidetone uses McBSP ICLK - which must not idle when sidetones
238 * are enabled or sidetones start sounding ugly.
239 */
240 w = cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
241 w &= ~(1 << (mcbsp->id - 2));
242 cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
243
244 /* Enable McBSP Sidetone */
245 w = MCBSP_READ(mcbsp, SSELCR);
246 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
247
248 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
249 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w & ~(ST_AUTOIDLE));
250
251 /* Enable Sidetone from Sidetone Core */
252 w = MCBSP_ST_READ(mcbsp, SSELCR);
253 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
254}
255
256static void omap_st_off(struct omap_mcbsp *mcbsp)
257{
258 unsigned int w;
259
260 w = MCBSP_ST_READ(mcbsp, SSELCR);
261 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
262
263 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
264 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w | ST_AUTOIDLE);
265
266 w = MCBSP_READ(mcbsp, SSELCR);
267 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
268
269 w = cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
270 w |= 1 << (mcbsp->id - 2);
271 cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
272}
273
274static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
275{
276 u16 val, i;
277
278 val = MCBSP_ST_READ(mcbsp, SYSCONFIG);
279 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, val & ~(ST_AUTOIDLE));
280
281 val = MCBSP_ST_READ(mcbsp, SSELCR);
282
283 if (val & ST_COEFFWREN)
284 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
285
286 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
287
288 for (i = 0; i < 128; i++)
289 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
290
291 i = 0;
292
293 val = MCBSP_ST_READ(mcbsp, SSELCR);
294 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
295 val = MCBSP_ST_READ(mcbsp, SSELCR);
296
297 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
298
299 if (i == 1000)
300 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
301}
302
303static void omap_st_chgain(struct omap_mcbsp *mcbsp)
304{
305 u16 w;
306 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
307
308 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
309 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w & ~(ST_AUTOIDLE));
310
311 w = MCBSP_ST_READ(mcbsp, SSELCR);
312
313 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
314 ST_CH1GAIN(st_data->ch1gain));
315}
316
317int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
318{
319 struct omap_mcbsp *mcbsp;
320 struct omap_mcbsp_st_data *st_data;
321 int ret = 0;
322
323 if (!omap_mcbsp_check_valid_id(id)) {
324 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
325 return -ENODEV;
326 }
327
328 mcbsp = id_to_mcbsp_ptr(id);
329 st_data = mcbsp->st_data;
330
331 if (!st_data)
332 return -ENOENT;
333
334 spin_lock_irq(&mcbsp->lock);
335 if (channel == 0)
336 st_data->ch0gain = chgain;
337 else if (channel == 1)
338 st_data->ch1gain = chgain;
339 else
340 ret = -EINVAL;
341
342 if (st_data->enabled)
343 omap_st_chgain(mcbsp);
344 spin_unlock_irq(&mcbsp->lock);
345
346 return ret;
347}
348EXPORT_SYMBOL(omap_st_set_chgain);
349
350int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
351{
352 struct omap_mcbsp *mcbsp;
353 struct omap_mcbsp_st_data *st_data;
354 int ret = 0;
355
356 if (!omap_mcbsp_check_valid_id(id)) {
357 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
358 return -ENODEV;
359 }
360
361 mcbsp = id_to_mcbsp_ptr(id);
362 st_data = mcbsp->st_data;
363
364 if (!st_data)
365 return -ENOENT;
366
367 spin_lock_irq(&mcbsp->lock);
368 if (channel == 0)
369 *chgain = st_data->ch0gain;
370 else if (channel == 1)
371 *chgain = st_data->ch1gain;
372 else
373 ret = -EINVAL;
374 spin_unlock_irq(&mcbsp->lock);
375
376 return ret;
377}
378EXPORT_SYMBOL(omap_st_get_chgain);
379
380static int omap_st_start(struct omap_mcbsp *mcbsp)
381{
382 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
383
384 if (st_data && st_data->enabled && !st_data->running) {
385 omap_st_fir_write(mcbsp, st_data->taps);
386 omap_st_chgain(mcbsp);
387
388 if (!mcbsp->free) {
389 omap_st_on(mcbsp);
390 st_data->running = 1;
391 }
392 }
393
394 return 0;
395}
396
397int omap_st_enable(unsigned int id)
398{
399 struct omap_mcbsp *mcbsp;
400 struct omap_mcbsp_st_data *st_data;
401
402 if (!omap_mcbsp_check_valid_id(id)) {
403 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
404 return -ENODEV;
405 }
406
407 mcbsp = id_to_mcbsp_ptr(id);
408 st_data = mcbsp->st_data;
409
410 if (!st_data)
411 return -ENODEV;
412
413 spin_lock_irq(&mcbsp->lock);
414 st_data->enabled = 1;
415 omap_st_start(mcbsp);
416 spin_unlock_irq(&mcbsp->lock);
417
418 return 0;
419}
420EXPORT_SYMBOL(omap_st_enable);
421
422static int omap_st_stop(struct omap_mcbsp *mcbsp)
423{
424 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
425
426 if (st_data && st_data->running) {
427 if (!mcbsp->free) {
428 omap_st_off(mcbsp);
429 st_data->running = 0;
430 }
431 }
432
433 return 0;
434}
435
436int omap_st_disable(unsigned int id)
437{
438 struct omap_mcbsp *mcbsp;
439 struct omap_mcbsp_st_data *st_data;
440 int ret = 0;
441
442 if (!omap_mcbsp_check_valid_id(id)) {
443 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
444 return -ENODEV;
445 }
446
447 mcbsp = id_to_mcbsp_ptr(id);
448 st_data = mcbsp->st_data;
449
450 if (!st_data)
451 return -ENODEV;
452
453 spin_lock_irq(&mcbsp->lock);
454 omap_st_stop(mcbsp);
455 st_data->enabled = 0;
456 spin_unlock_irq(&mcbsp->lock);
457
458 return ret;
459}
460EXPORT_SYMBOL(omap_st_disable);
461
462int omap_st_is_enabled(unsigned int id)
463{
464 struct omap_mcbsp *mcbsp;
465 struct omap_mcbsp_st_data *st_data;
466
467 if (!omap_mcbsp_check_valid_id(id)) {
468 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
469 return -ENODEV;
470 }
471
472 mcbsp = id_to_mcbsp_ptr(id);
473 st_data = mcbsp->st_data;
474
475 if (!st_data)
476 return -ENODEV;
477
478
479 return st_data->enabled;
480}
481EXPORT_SYMBOL(omap_st_is_enabled);
482
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300483/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300484 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
485 * The threshold parameter is 1 based, and it is converted (threshold - 1)
486 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300487 */
488void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
489{
490 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300491
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500492 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300493 return;
494
495 if (!omap_mcbsp_check_valid_id(id)) {
496 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
497 return;
498 }
499 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300500
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300501 if (threshold && threshold <= mcbsp->max_tx_thres)
502 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300503}
504EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
505
506/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300507 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
508 * The threshold parameter is 1 based, and it is converted (threshold - 1)
509 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300510 */
511void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
512{
513 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300514
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500515 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300516 return;
517
518 if (!omap_mcbsp_check_valid_id(id)) {
519 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
520 return;
521 }
522 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300523
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300524 if (threshold && threshold <= mcbsp->max_rx_thres)
525 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300526}
527EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300528
529/*
530 * omap_mcbsp_get_max_tx_thres just return the current configured
531 * maximum threshold for transmission
532 */
533u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
534{
535 struct omap_mcbsp *mcbsp;
536
537 if (!omap_mcbsp_check_valid_id(id)) {
538 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
539 return -ENODEV;
540 }
541 mcbsp = id_to_mcbsp_ptr(id);
542
543 return mcbsp->max_tx_thres;
544}
545EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
546
547/*
548 * omap_mcbsp_get_max_rx_thres just return the current configured
549 * maximum threshold for reception
550 */
551u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
552{
553 struct omap_mcbsp *mcbsp;
554
555 if (!omap_mcbsp_check_valid_id(id)) {
556 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
557 return -ENODEV;
558 }
559 mcbsp = id_to_mcbsp_ptr(id);
560
561 return mcbsp->max_rx_thres;
562}
563EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300564
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300565u16 omap_mcbsp_get_fifo_size(unsigned int id)
566{
567 struct omap_mcbsp *mcbsp;
568
569 if (!omap_mcbsp_check_valid_id(id)) {
570 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
571 return -ENODEV;
572 }
573 mcbsp = id_to_mcbsp_ptr(id);
574
575 return mcbsp->pdata->buffer_size;
576}
577EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
578
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200579#define MCBSP2_FIFO_SIZE 0x500 /* 1024 + 256 locations */
580#define MCBSP1345_FIFO_SIZE 0x80 /* 128 locations */
581/*
582 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
583 */
584u16 omap_mcbsp_get_tx_delay(unsigned int id)
585{
586 struct omap_mcbsp *mcbsp;
587 u16 buffstat;
588
589 if (!omap_mcbsp_check_valid_id(id)) {
590 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
591 return -ENODEV;
592 }
593 mcbsp = id_to_mcbsp_ptr(id);
594
595 /* Returns the number of free locations in the buffer */
596 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
597
598 /* Number of slots are different in McBSP ports */
599 if (mcbsp->id == 2)
600 return MCBSP2_FIFO_SIZE - buffstat;
601 else
602 return MCBSP1345_FIFO_SIZE - buffstat;
603}
604EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
605
606/*
607 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
608 * to reach the threshold value (when the DMA will be triggered to read it)
609 */
610u16 omap_mcbsp_get_rx_delay(unsigned int id)
611{
612 struct omap_mcbsp *mcbsp;
613 u16 buffstat, threshold;
614
615 if (!omap_mcbsp_check_valid_id(id)) {
616 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
617 return -ENODEV;
618 }
619 mcbsp = id_to_mcbsp_ptr(id);
620
621 /* Returns the number of used locations in the buffer */
622 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
623 /* RX threshold */
624 threshold = MCBSP_READ(mcbsp, THRSH1);
625
626 /* Return the number of location till we reach the threshold limit */
627 if (threshold <= buffstat)
628 return 0;
629 else
630 return threshold - buffstat;
631}
632EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
633
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300634/*
635 * omap_mcbsp_get_dma_op_mode just return the current configured
636 * operating mode for the mcbsp channel
637 */
638int omap_mcbsp_get_dma_op_mode(unsigned int id)
639{
640 struct omap_mcbsp *mcbsp;
641 int dma_op_mode;
642
643 if (!omap_mcbsp_check_valid_id(id)) {
644 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
645 return -ENODEV;
646 }
647 mcbsp = id_to_mcbsp_ptr(id);
648
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300649 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300650
651 return dma_op_mode;
652}
653EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300654
655static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
656{
657 /*
658 * Enable wakup behavior, smart idle and all wakeups
659 * REVISIT: some wakeups may be unnecessary
660 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500661 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300662 u16 syscon;
663
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800664 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300665 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eduardo Valentind99a7452009-08-20 16:18:18 +0300666
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300667 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
668 syscon |= (ENAWAKEUP | SIDLEMODE(0x02) |
669 CLOCKACTIVITY(0x02));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800670 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300671 } else {
Eduardo Valentind99a7452009-08-20 16:18:18 +0300672 syscon |= SIDLEMODE(0x01);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300673 }
Eduardo Valentind99a7452009-08-20 16:18:18 +0300674
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800675 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300676 }
677}
678
679static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
680{
681 /*
682 * Disable wakup behavior, smart idle and all wakeups
683 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500684 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300685 u16 syscon;
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300686
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800687 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300688 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300689 /*
690 * HW bug workaround - If no_idle mode is taken, we need to
691 * go to smart_idle before going to always_idle, or the
692 * device will not hit retention anymore.
693 */
694 syscon |= SIDLEMODE(0x02);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800695 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300696
697 syscon &= ~(SIDLEMODE(0x03));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800698 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300699
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800700 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300701 }
702}
703#else
704static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
705static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000706static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
707static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300708#endif
709
Tony Lindgren120db2c2006-04-02 17:46:27 +0100710/*
711 * We can choose between IRQ based or polled IO.
712 * This needs to be called before omap_mcbsp_request().
713 */
714int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
715{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300716 struct omap_mcbsp *mcbsp;
717
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300718 if (!omap_mcbsp_check_valid_id(id)) {
719 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
720 return -ENODEV;
721 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300722 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100723
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300724 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100725
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300726 if (!mcbsp->free) {
727 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
728 mcbsp->id);
729 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100730 return -EINVAL;
731 }
732
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300733 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100734
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300735 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100736
737 return 0;
738}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300739EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100741int omap_mcbsp_request(unsigned int id)
742{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800744 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 int err;
746
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300747 if (!omap_mcbsp_check_valid_id(id)) {
748 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
749 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100750 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300751 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300752
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800753 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
754 if (!reg_cache) {
755 return -ENOMEM;
756 }
757
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300758 spin_lock(&mcbsp->lock);
759 if (!mcbsp->free) {
760 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
761 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800762 err = -EBUSY;
763 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764 }
765
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300766 mcbsp->free = 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800767 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300768 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Russell Kingb820ce42009-01-23 10:26:46 +0000770 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
771 mcbsp->pdata->ops->request(id);
772
773 clk_enable(mcbsp->iclk);
774 clk_enable(mcbsp->fclk);
775
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300776 /* Do procedure specific to omap34xx arch, if applicable */
777 omap34xx_mcbsp_request(mcbsp);
778
Jarkko Nikula5a070552008-10-08 10:01:41 +0300779 /*
780 * Make sure that transmitter, receiver and sample-rate generator are
781 * not running before activating IRQs.
782 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800783 MCBSP_WRITE(mcbsp, SPCR1, 0);
784 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300785
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300786 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100787 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300788 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300789 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
790 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100791 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300792 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
793 "for McBSP%d\n", mcbsp->tx_irq,
794 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800795 goto err_clk_disable;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100796 }
797
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500798 if (mcbsp->rx_irq) {
799 init_completion(&mcbsp->rx_irq_completion);
800 err = request_irq(mcbsp->rx_irq,
801 omap_mcbsp_rx_irq_handler,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300802 0, "McBSP", (void *)mcbsp);
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500803 if (err != 0) {
804 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
805 "for McBSP%d\n", mcbsp->rx_irq,
806 mcbsp->id);
807 goto err_free_irq;
808 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100809 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100810 }
811
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100812 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800813err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800814 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800815err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800816 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800817 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800818
819 /* Do procedure specific to omap34xx arch, if applicable */
820 omap34xx_mcbsp_free(mcbsp);
821
822 clk_disable(mcbsp->fclk);
823 clk_disable(mcbsp->iclk);
824
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800825 spin_lock(&mcbsp->lock);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800826 mcbsp->free = 1;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800827 mcbsp->reg_cache = NULL;
828err_kfree:
829 spin_unlock(&mcbsp->lock);
830 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800831
832 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100833}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300834EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100835
836void omap_mcbsp_free(unsigned int id)
837{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300838 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800839 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300840
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300841 if (!omap_mcbsp_check_valid_id(id)) {
842 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100843 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100844 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300845 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100846
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300847 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
848 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300849
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300850 /* Do procedure specific to omap34xx arch, if applicable */
851 omap34xx_mcbsp_free(mcbsp);
852
Russell Kingb820ce42009-01-23 10:26:46 +0000853 clk_disable(mcbsp->fclk);
854 clk_disable(mcbsp->iclk);
855
856 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
857 /* Free IRQs */
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500858 if (mcbsp->rx_irq)
859 free_irq(mcbsp->rx_irq, (void *)mcbsp);
Russell Kingb820ce42009-01-23 10:26:46 +0000860 free_irq(mcbsp->tx_irq, (void *)mcbsp);
861 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100862
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800863 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100864
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800865 spin_lock(&mcbsp->lock);
866 if (mcbsp->free)
867 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
868 else
869 mcbsp->free = 1;
870 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300871 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800872
873 if (reg_cache)
874 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300876EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100877
878/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300879 * Here we start the McBSP, by enabling transmitter, receiver or both.
880 * If no transmitter or receiver is active prior calling, then sample-rate
881 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100882 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300883void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100884{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300885 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300886 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100887 u16 w;
888
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300889 if (!omap_mcbsp_check_valid_id(id)) {
890 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100891 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300892 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300893 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100894
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000895 if (cpu_is_omap34xx())
896 omap_st_start(mcbsp);
897
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800898 mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
899 mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100900
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800901 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
902 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300903
904 if (idle) {
905 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800906 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800907 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300908 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100909
910 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300911 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800912 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800913 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100914
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300915 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800916 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800917 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100918
Eduardo Valentin44a63112009-08-20 16:18:09 +0300919 /*
920 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
921 * REVISIT: 100us may give enough time for two CLKSRG, however
922 * due to some unknown PM related, clock gating etc. reason it
923 * is now at 500us.
924 */
925 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100926
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300927 if (idle) {
928 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800929 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800930 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300931 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100932
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500933 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300934 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800935 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300936 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800937 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800938 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300939 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800940 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300941 }
942
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100943 /* Dump McBSP Regs */
944 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100945}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300946EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100947
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300948void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300950 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300951 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100952 u16 w;
953
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300954 if (!omap_mcbsp_check_valid_id(id)) {
955 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100956 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300957 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100958
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300959 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300961 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300962 tx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500963 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800964 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300965 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800966 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300967 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800968 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800969 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100970
971 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300972 rx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500973 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800974 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700975 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800976 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300977 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800978 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800979 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100980
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800981 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
982 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300983
984 if (idle) {
985 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800986 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800987 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300988 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000989
990 if (cpu_is_omap34xx())
991 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100992}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300993EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100994
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100995/* polled mcbsp i/o operations */
996int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
997{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300998 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300999
1000 if (!omap_mcbsp_check_valid_id(id)) {
1001 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1002 return -ENODEV;
1003 }
1004
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001005 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001006
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001007 MCBSP_WRITE(mcbsp, DXR1, buf);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001008 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001009 if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001010 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001011 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001012 /* resend */
1013 return -1;
1014 } else {
1015 /* wait for transmit confirmation */
1016 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001017 while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001018 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001019 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001020 MCBSP_READ_CACHE(mcbsp, SPCR2) &
1021 (~XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001022 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001023 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001024 MCBSP_READ_CACHE(mcbsp, SPCR2) |
1025 (XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001026 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001027 dev_err(mcbsp->dev, "Could not write to"
1028 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001029 return -2;
1030 }
1031 }
1032 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001033
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001034 return 0;
1035}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001036EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001037
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001038int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001039{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001040 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001041
1042 if (!omap_mcbsp_check_valid_id(id)) {
1043 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1044 return -ENODEV;
1045 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001046 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001047
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001048 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001049 if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001050 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001051 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001052 /* resend */
1053 return -1;
1054 } else {
1055 /* wait for recieve confirmation */
1056 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001057 while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001058 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001059 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001060 MCBSP_READ_CACHE(mcbsp, SPCR1) &
1061 (~RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001062 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001063 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001064 MCBSP_READ_CACHE(mcbsp, SPCR1) |
1065 (RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001066 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001067 dev_err(mcbsp->dev, "Could not read from"
1068 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001069 return -2;
1070 }
1071 }
1072 }
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001073 *buf = MCBSP_READ(mcbsp, DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001074
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001075 return 0;
1076}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001077EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001078
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001079/*
1080 * IRQ based word transmission.
1081 */
1082void omap_mcbsp_xmit_word(unsigned int id, u32 word)
1083{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001084 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001085 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001086
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001087 if (!omap_mcbsp_check_valid_id(id)) {
1088 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001089 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001090 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001091
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001092 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001093 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001094
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001095 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096
1097 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001098 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1099 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001100}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001101EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001102
1103u32 omap_mcbsp_recv_word(unsigned int id)
1104{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001105 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001106 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001107 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001108
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001109 if (!omap_mcbsp_check_valid_id(id)) {
1110 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1111 return -ENODEV;
1112 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001113 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001114
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001115 word_length = mcbsp->rx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001116
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001117 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001118
1119 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001120 word_msb = MCBSP_READ(mcbsp, DRR2);
1121 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001122
1123 return (word_lsb | (word_msb << 16));
1124}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001125EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001126
Tony Lindgren120db2c2006-04-02 17:46:27 +01001127int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
1128{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001129 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001130 omap_mcbsp_word_length tx_word_length;
1131 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001132 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1133
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001134 if (!omap_mcbsp_check_valid_id(id)) {
1135 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1136 return -ENODEV;
1137 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001138 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001139 tx_word_length = mcbsp->tx_word_length;
1140 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001141
Tony Lindgren120db2c2006-04-02 17:46:27 +01001142 if (tx_word_length != rx_word_length)
1143 return -EINVAL;
1144
1145 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001146 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001147 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001148 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001149 if (attempts++ > 1000) {
1150 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001151 MCBSP_WRITE(mcbsp, SPCR2,
1152 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001153 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001154 MCBSP_WRITE(mcbsp, SPCR2,
1155 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001156 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001157 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1158 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001159 return -EAGAIN;
1160 }
1161 }
1162
1163 /* Now we can push the data */
1164 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001165 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1166 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001167
1168 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001169 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001170 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001171 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001172 if (attempts++ > 1000) {
1173 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001174 MCBSP_WRITE(mcbsp, SPCR1,
1175 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001176 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001177 MCBSP_WRITE(mcbsp, SPCR1,
1178 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001179 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001180 dev_err(mcbsp->dev, "McBSP%d receiver not "
1181 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001182 return -EAGAIN;
1183 }
1184 }
1185
1186 /* Receiver is ready, let's read the dummy data */
1187 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001188 word_msb = MCBSP_READ(mcbsp, DRR2);
1189 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001190
1191 return 0;
1192}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001193EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001194
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001195int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +01001196{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001197 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +01001198 u32 clock_word = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001199 omap_mcbsp_word_length tx_word_length;
1200 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001201 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1202
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001203 if (!omap_mcbsp_check_valid_id(id)) {
1204 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1205 return -ENODEV;
1206 }
1207
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001208 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001209
1210 tx_word_length = mcbsp->tx_word_length;
1211 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001212
Tony Lindgren120db2c2006-04-02 17:46:27 +01001213 if (tx_word_length != rx_word_length)
1214 return -EINVAL;
1215
1216 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001217 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001218 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001219 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001220 if (attempts++ > 1000) {
1221 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001222 MCBSP_WRITE(mcbsp, SPCR2,
1223 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001224 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001225 MCBSP_WRITE(mcbsp, SPCR2,
1226 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001227 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001228 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1229 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001230 return -EAGAIN;
1231 }
1232 }
1233
1234 /* We first need to enable the bus clock */
1235 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001236 MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
1237 MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001238
1239 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001240 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001241 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001242 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001243 if (attempts++ > 1000) {
1244 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001245 MCBSP_WRITE(mcbsp, SPCR1,
1246 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001247 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001248 MCBSP_WRITE(mcbsp, SPCR1,
1249 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001250 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001251 dev_err(mcbsp->dev, "McBSP%d receiver not "
1252 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001253 return -EAGAIN;
1254 }
1255 }
1256
1257 /* Receiver is ready, there is something for us */
1258 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001259 word_msb = MCBSP_READ(mcbsp, DRR2);
1260 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001261
1262 word[0] = (word_lsb | (word_msb << 16));
1263
1264 return 0;
1265}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001266EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001267
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001268/*
1269 * Simple DMA based buffer rx/tx routines.
1270 * Nothing fancy, just a single buffer tx/rx through DMA.
1271 * The DMA resources are released once the transfer is done.
1272 * For anything fancier, you should use your own customized DMA
1273 * routines and callbacks.
1274 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001275int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
1276 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001277{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001278 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001279 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001280 int src_port = 0;
1281 int dest_port = 0;
1282 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001283
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001284 if (!omap_mcbsp_check_valid_id(id)) {
1285 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1286 return -ENODEV;
1287 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001288 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001289
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001290 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001291 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001292 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001293 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001294 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001295 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001296 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001297 return -EAGAIN;
1298 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001299 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001300
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001301 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001302 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001303
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001304 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001305
Tony Lindgren120db2c2006-04-02 17:46:27 +01001306 if (cpu_class_is_omap1()) {
1307 src_port = OMAP_DMA_PORT_TIPB;
1308 dest_port = OMAP_DMA_PORT_EMIFF;
1309 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001310 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001311 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001312
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001313 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001314 OMAP_DMA_DATA_TYPE_S16,
1315 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001316 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001317 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001318
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001319 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001320 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001321 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001322 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001323 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001324
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001325 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001326 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001327 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001328 buffer,
1329 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001330
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001331 omap_start_dma(mcbsp->dma_tx_lch);
1332 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001333
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001334 return 0;
1335}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001336EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001337
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001338int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
1339 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001340{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001341 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001342 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001343 int src_port = 0;
1344 int dest_port = 0;
1345 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001346
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001347 if (!omap_mcbsp_check_valid_id(id)) {
1348 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1349 return -ENODEV;
1350 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001351 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001352
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001353 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001354 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001355 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001356 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001357 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001358 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001359 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001360 return -EAGAIN;
1361 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001362 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001363
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001364 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001365 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001366
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001367 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001368
Tony Lindgren120db2c2006-04-02 17:46:27 +01001369 if (cpu_class_is_omap1()) {
1370 src_port = OMAP_DMA_PORT_TIPB;
1371 dest_port = OMAP_DMA_PORT_EMIFF;
1372 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001373 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001374 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001375
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001376 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001377 OMAP_DMA_DATA_TYPE_S16,
1378 length >> 1, 1,
1379 OMAP_DMA_SYNC_ELEMENT,
1380 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001381
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001382 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001383 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001384 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001385 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001386 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001387
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001388 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001389 dest_port,
1390 OMAP_DMA_AMODE_POST_INC,
1391 buffer,
1392 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001393
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001394 omap_start_dma(mcbsp->dma_rx_lch);
1395 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001396
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001397 return 0;
1398}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001399EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001400
1401/*
1402 * SPI wrapper.
1403 * Since SPI setup is much simpler than the generic McBSP one,
1404 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1405 * Once this is done, you can call omap_mcbsp_start().
1406 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001407void omap_mcbsp_set_spi_mode(unsigned int id,
1408 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001409{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001410 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001411 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1412
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001413 if (!omap_mcbsp_check_valid_id(id)) {
1414 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001415 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001416 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001417 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001418
1419 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1420
1421 /* SPI has only one frame */
1422 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1423 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1424
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001425 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001426 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1427 mcbsp_cfg.spcr1 |= (1 << 12);
1428 else
1429 mcbsp_cfg.spcr1 |= (3 << 11);
1430
1431 /* Set clock parities */
1432 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1433 mcbsp_cfg.pcr0 |= CLKRP;
1434 else
1435 mcbsp_cfg.pcr0 &= ~CLKRP;
1436
1437 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1438 mcbsp_cfg.pcr0 &= ~CLKXP;
1439 else
1440 mcbsp_cfg.pcr0 |= CLKXP;
1441
1442 /* Set SCLKME to 0 and CLKSM to 1 */
1443 mcbsp_cfg.pcr0 &= ~SCLKME;
1444 mcbsp_cfg.srgr2 |= CLKSM;
1445
1446 /* Set FSXP */
1447 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1448 mcbsp_cfg.pcr0 &= ~FSXP;
1449 else
1450 mcbsp_cfg.pcr0 |= FSXP;
1451
1452 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1453 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001454 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001455 mcbsp_cfg.pcr0 |= FSXM;
1456 mcbsp_cfg.srgr2 &= ~FSGM;
1457 mcbsp_cfg.xcr2 |= XDATDLY(1);
1458 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001459 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001460 mcbsp_cfg.pcr0 &= ~CLKXM;
1461 mcbsp_cfg.srgr1 |= CLKGDV(1);
1462 mcbsp_cfg.pcr0 &= ~FSXM;
1463 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1464 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1465 }
1466
1467 mcbsp_cfg.xcr2 &= ~XPHASE;
1468 mcbsp_cfg.rcr2 &= ~RPHASE;
1469
1470 omap_mcbsp_config(id, &mcbsp_cfg);
1471}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001472EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001473
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001474#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001475#define max_thres(m) (mcbsp->pdata->buffer_size)
1476#define valid_threshold(m, val) ((val) <= max_thres(m))
1477#define THRESHOLD_PROP_BUILDER(prop) \
1478static ssize_t prop##_show(struct device *dev, \
1479 struct device_attribute *attr, char *buf) \
1480{ \
1481 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1482 \
1483 return sprintf(buf, "%u\n", mcbsp->prop); \
1484} \
1485 \
1486static ssize_t prop##_store(struct device *dev, \
1487 struct device_attribute *attr, \
1488 const char *buf, size_t size) \
1489{ \
1490 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1491 unsigned long val; \
1492 int status; \
1493 \
1494 status = strict_strtoul(buf, 0, &val); \
1495 if (status) \
1496 return status; \
1497 \
1498 if (!valid_threshold(mcbsp, val)) \
1499 return -EDOM; \
1500 \
1501 mcbsp->prop = val; \
1502 return size; \
1503} \
1504 \
1505static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1506
1507THRESHOLD_PROP_BUILDER(max_tx_thres);
1508THRESHOLD_PROP_BUILDER(max_rx_thres);
1509
Jarkko Nikula9b300502009-08-24 17:45:50 +03001510static const char *dma_op_modes[] = {
1511 "element", "threshold", "frame",
1512};
1513
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001514static ssize_t dma_op_mode_show(struct device *dev,
1515 struct device_attribute *attr, char *buf)
1516{
1517 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001518 int dma_op_mode, i = 0;
1519 ssize_t len = 0;
1520 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001521
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001522 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001523
Jarkko Nikula9b300502009-08-24 17:45:50 +03001524 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1525 if (dma_op_mode == i)
1526 len += sprintf(buf + len, "[%s] ", *s);
1527 else
1528 len += sprintf(buf + len, "%s ", *s);
1529 }
1530 len += sprintf(buf + len, "\n");
1531
1532 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001533}
1534
1535static ssize_t dma_op_mode_store(struct device *dev,
1536 struct device_attribute *attr,
1537 const char *buf, size_t size)
1538{
1539 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001540 const char * const *s;
1541 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001542
Jarkko Nikula9b300502009-08-24 17:45:50 +03001543 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1544 if (sysfs_streq(buf, *s))
1545 break;
1546
1547 if (i == ARRAY_SIZE(dma_op_modes))
1548 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001549
1550 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001551 if (!mcbsp->free) {
1552 size = -EBUSY;
1553 goto unlock;
1554 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001555 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001556
1557unlock:
1558 spin_unlock_irq(&mcbsp->lock);
1559
1560 return size;
1561}
1562
1563static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1564
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001565static ssize_t st_taps_show(struct device *dev,
1566 struct device_attribute *attr, char *buf)
1567{
1568 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1569 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1570 ssize_t status = 0;
1571 int i;
1572
1573 spin_lock_irq(&mcbsp->lock);
1574 for (i = 0; i < st_data->nr_taps; i++)
1575 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1576 st_data->taps[i]);
1577 if (i)
1578 status += sprintf(&buf[status], "\n");
1579 spin_unlock_irq(&mcbsp->lock);
1580
1581 return status;
1582}
1583
1584static ssize_t st_taps_store(struct device *dev,
1585 struct device_attribute *attr,
1586 const char *buf, size_t size)
1587{
1588 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1589 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1590 int val, tmp, status, i = 0;
1591
1592 spin_lock_irq(&mcbsp->lock);
1593 memset(st_data->taps, 0, sizeof(st_data->taps));
1594 st_data->nr_taps = 0;
1595
1596 do {
1597 status = sscanf(buf, "%d%n", &val, &tmp);
1598 if (status < 0 || status == 0) {
1599 size = -EINVAL;
1600 goto out;
1601 }
1602 if (val < -32768 || val > 32767) {
1603 size = -EINVAL;
1604 goto out;
1605 }
1606 st_data->taps[i++] = val;
1607 buf += tmp;
1608 if (*buf != ',')
1609 break;
1610 buf++;
1611 } while (1);
1612
1613 st_data->nr_taps = i;
1614
1615out:
1616 spin_unlock_irq(&mcbsp->lock);
1617
1618 return size;
1619}
1620
1621static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1622
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001623static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001624 &dev_attr_max_tx_thres.attr,
1625 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001626 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001627 NULL,
1628};
1629
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001630static const struct attribute_group additional_attr_group = {
1631 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001632};
1633
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001634static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001635{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001636 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001637}
1638
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001639static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001640{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001641 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001642}
1643
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001644static const struct attribute *sidetone_attrs[] = {
1645 &dev_attr_st_taps.attr,
1646 NULL,
1647};
1648
1649static const struct attribute_group sidetone_attr_group = {
1650 .attrs = (struct attribute **)sidetone_attrs,
1651};
1652
1653int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
1654{
1655 struct omap_mcbsp_platform_data *pdata = mcbsp->pdata;
1656 struct omap_mcbsp_st_data *st_data;
1657 int err;
1658
1659 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1660 if (!st_data) {
1661 err = -ENOMEM;
1662 goto err1;
1663 }
1664
1665 st_data->io_base_st = ioremap(pdata->phys_base_st, SZ_4K);
1666 if (!st_data->io_base_st) {
1667 err = -ENOMEM;
1668 goto err2;
1669 }
1670
1671 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1672 if (err)
1673 goto err3;
1674
1675 mcbsp->st_data = st_data;
1676 return 0;
1677
1678err3:
1679 iounmap(st_data->io_base_st);
1680err2:
1681 kfree(st_data);
1682err1:
1683 return err;
1684
1685}
1686
1687static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1688{
1689 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1690
1691 if (st_data) {
1692 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1693 iounmap(st_data->io_base_st);
1694 kfree(st_data);
1695 }
1696}
1697
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001698static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1699{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001700 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001701 if (cpu_is_omap34xx()) {
Peter Ujfalusi451fd822010-06-03 07:39:33 +03001702 /*
1703 * Initially configure the maximum thresholds to a safe value.
1704 * The McBSP FIFO usage with these values should not go under
1705 * 16 locations.
1706 * If the whole FIFO without safety buffer is used, than there
1707 * is a possibility that the DMA will be not able to push the
1708 * new data on time, causing channel shifts in runtime.
1709 */
1710 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1711 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001712 /*
1713 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1714 * for mcbsp2 instances.
1715 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001716 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001717 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001718 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001719
1720 if (mcbsp->id == 2 || mcbsp->id == 3)
1721 if (omap_st_add(mcbsp))
1722 dev_warn(mcbsp->dev,
1723 "Unable to create sidetone controls\n");
1724
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001725 } else {
1726 mcbsp->max_tx_thres = -EINVAL;
1727 mcbsp->max_rx_thres = -EINVAL;
1728 }
1729}
1730
1731static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1732{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001733 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001734 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001735
1736 if (mcbsp->id == 2 || mcbsp->id == 3)
1737 omap_st_remove(mcbsp);
1738 }
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001739}
1740#else
1741static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1742static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001743#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001744
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001745/*
1746 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1747 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1748 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001749static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001750{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001751 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001752 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001753 int id = pdev->id - 1;
1754 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001755
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001756 if (!pdata) {
1757 dev_err(&pdev->dev, "McBSP device initialized without"
1758 "platform data\n");
1759 ret = -EINVAL;
1760 goto exit;
1761 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001762
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001763 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001764
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001765 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001766 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1767 ret = -EINVAL;
1768 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001769 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001770
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001771 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1772 if (!mcbsp) {
1773 ret = -ENOMEM;
1774 goto exit;
1775 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001776
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001777 spin_lock_init(&mcbsp->lock);
1778 mcbsp->id = id + 1;
1779 mcbsp->free = 1;
1780 mcbsp->dma_tx_lch = -1;
1781 mcbsp->dma_rx_lch = -1;
1782
1783 mcbsp->phys_base = pdata->phys_base;
1784 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1785 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001786 ret = -ENOMEM;
1787 goto err_ioremap;
1788 }
1789
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001790 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001791 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1792 mcbsp->tx_irq = pdata->tx_irq;
1793 mcbsp->rx_irq = pdata->rx_irq;
1794 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1795 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001796
Russell Kingb820ce42009-01-23 10:26:46 +00001797 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1798 if (IS_ERR(mcbsp->iclk)) {
1799 ret = PTR_ERR(mcbsp->iclk);
1800 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1801 goto err_iclk;
1802 }
Stanley.Miao06151152009-01-29 08:57:12 -08001803
Russell Kingb820ce42009-01-23 10:26:46 +00001804 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1805 if (IS_ERR(mcbsp->fclk)) {
1806 ret = PTR_ERR(mcbsp->fclk);
1807 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1808 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001809 }
1810
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001811 mcbsp->pdata = pdata;
1812 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001813 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001814 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001815
1816 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1817 omap34xx_device_init(mcbsp);
1818
Russell Kingd592dd12008-09-04 14:25:42 +01001819 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001820
Russell Kingb820ce42009-01-23 10:26:46 +00001821err_fclk:
1822 clk_put(mcbsp->iclk);
1823err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001824 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001825err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001826 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001827exit:
1828 return ret;
1829}
1830
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001831static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001832{
1833 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1834
1835 platform_set_drvdata(pdev, NULL);
1836 if (mcbsp) {
1837
1838 if (mcbsp->pdata && mcbsp->pdata->ops &&
1839 mcbsp->pdata->ops->free)
1840 mcbsp->pdata->ops->free(mcbsp->id);
1841
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001842 omap34xx_device_exit(mcbsp);
1843
Russell Kingb820ce42009-01-23 10:26:46 +00001844 clk_disable(mcbsp->fclk);
1845 clk_disable(mcbsp->iclk);
1846 clk_put(mcbsp->fclk);
1847 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001848
Russell Kingd592dd12008-09-04 14:25:42 +01001849 iounmap(mcbsp->io_base);
1850
Russell Kingb820ce42009-01-23 10:26:46 +00001851 mcbsp->fclk = NULL;
1852 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001853 mcbsp->free = 0;
1854 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001855 }
1856
1857 return 0;
1858}
1859
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001860static struct platform_driver omap_mcbsp_driver = {
1861 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001862 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001863 .driver = {
1864 .name = "omap-mcbsp",
1865 },
1866};
1867
1868int __init omap_mcbsp_init(void)
1869{
1870 /* Register the McBSP driver */
1871 return platform_driver_register(&omap_mcbsp_driver);
1872}