blob: eac4b978e9fd2feb4c692d8f10567b853ca53398 [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
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070036static void 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
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070050static int 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
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070065static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000066{
67 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
68}
69
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070070static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000071{
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
Eero Nurkkalad912fa92010-02-22 12:21:11 +000083#define MCBSP_ST_READ(mcbsp, reg) \
84 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
85#define MCBSP_ST_WRITE(mcbsp, reg, val) \
86 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
87
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010088static void omap_mcbsp_dump_reg(u8 id)
89{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030090 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
91
92 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
93 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080094 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030095 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080096 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030097 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080098 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030099 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800100 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300101 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800102 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300103 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800104 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300105 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800106 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300107 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800108 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300109 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800110 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300111 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800112 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300113 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800114 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300115 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800116 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300117 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800118 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300119 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100120}
121
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700122static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400124 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700125 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100126
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800127 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700128 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100129
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700130 if (irqst_spcr2 & XSYNC_ERR) {
131 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
132 irqst_spcr2);
133 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000134 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700135 } else {
136 complete(&mcbsp_tx->tx_irq_completion);
137 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300138
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139 return IRQ_HANDLED;
140}
141
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700142static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400144 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700145 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100146
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800147 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700148 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100149
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700150 if (irqst_spcr1 & RSYNC_ERR) {
151 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
152 irqst_spcr1);
153 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000154 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700155 } else {
Scott Elliscb922d22010-09-23 18:47:23 -0700156 complete(&mcbsp_rx->rx_irq_completion);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700157 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300158
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159 return IRQ_HANDLED;
160}
161
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
163{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400164 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100165
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300166 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800167 MCBSP_READ(mcbsp_dma_tx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168
169 /* We can free the channels */
170 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
171 mcbsp_dma_tx->dma_tx_lch = -1;
172
173 complete(&mcbsp_dma_tx->tx_dma_completion);
174}
175
176static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
177{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400178 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100179
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300180 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800181 MCBSP_READ(mcbsp_dma_rx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100182
183 /* We can free the channels */
184 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
185 mcbsp_dma_rx->dma_rx_lch = -1;
186
187 complete(&mcbsp_dma_rx->rx_dma_completion);
188}
189
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100190/*
191 * omap_mcbsp_config simply write a config to the
192 * appropriate McBSP.
193 * You either call this function or set the McBSP registers
194 * by yourself before calling omap_mcbsp_start().
195 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300196void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100197{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300198 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100199
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300200 if (!omap_mcbsp_check_valid_id(id)) {
201 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
202 return;
203 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300204 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300205
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300206 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
207 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208
209 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800210 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
211 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
212 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
213 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
214 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
215 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
216 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
217 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
218 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
219 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
220 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Syed Rafiuddina5b92cc2009-07-28 18:57:10 +0530221 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800222 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
223 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200224 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100225}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300226EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100227
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800228#ifdef CONFIG_ARCH_OMAP3
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000229static void omap_st_on(struct omap_mcbsp *mcbsp)
230{
231 unsigned int w;
232
233 /*
234 * Sidetone uses McBSP ICLK - which must not idle when sidetones
235 * are enabled or sidetones start sounding ugly.
236 */
237 w = cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
238 w &= ~(1 << (mcbsp->id - 2));
239 cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
240
241 /* Enable McBSP Sidetone */
242 w = MCBSP_READ(mcbsp, SSELCR);
243 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
244
245 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
246 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w & ~(ST_AUTOIDLE));
247
248 /* Enable Sidetone from Sidetone Core */
249 w = MCBSP_ST_READ(mcbsp, SSELCR);
250 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
251}
252
253static void omap_st_off(struct omap_mcbsp *mcbsp)
254{
255 unsigned int w;
256
257 w = MCBSP_ST_READ(mcbsp, SSELCR);
258 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
259
260 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
261 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w | ST_AUTOIDLE);
262
263 w = MCBSP_READ(mcbsp, SSELCR);
264 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
265
266 w = cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
267 w |= 1 << (mcbsp->id - 2);
268 cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
269}
270
271static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
272{
273 u16 val, i;
274
275 val = MCBSP_ST_READ(mcbsp, SYSCONFIG);
276 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, val & ~(ST_AUTOIDLE));
277
278 val = MCBSP_ST_READ(mcbsp, SSELCR);
279
280 if (val & ST_COEFFWREN)
281 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
282
283 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
284
285 for (i = 0; i < 128; i++)
286 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
287
288 i = 0;
289
290 val = MCBSP_ST_READ(mcbsp, SSELCR);
291 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
292 val = MCBSP_ST_READ(mcbsp, SSELCR);
293
294 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
295
296 if (i == 1000)
297 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
298}
299
300static void omap_st_chgain(struct omap_mcbsp *mcbsp)
301{
302 u16 w;
303 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
304
305 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
306 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w & ~(ST_AUTOIDLE));
307
308 w = MCBSP_ST_READ(mcbsp, SSELCR);
309
310 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
311 ST_CH1GAIN(st_data->ch1gain));
312}
313
314int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
315{
316 struct omap_mcbsp *mcbsp;
317 struct omap_mcbsp_st_data *st_data;
318 int ret = 0;
319
320 if (!omap_mcbsp_check_valid_id(id)) {
321 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
322 return -ENODEV;
323 }
324
325 mcbsp = id_to_mcbsp_ptr(id);
326 st_data = mcbsp->st_data;
327
328 if (!st_data)
329 return -ENOENT;
330
331 spin_lock_irq(&mcbsp->lock);
332 if (channel == 0)
333 st_data->ch0gain = chgain;
334 else if (channel == 1)
335 st_data->ch1gain = chgain;
336 else
337 ret = -EINVAL;
338
339 if (st_data->enabled)
340 omap_st_chgain(mcbsp);
341 spin_unlock_irq(&mcbsp->lock);
342
343 return ret;
344}
345EXPORT_SYMBOL(omap_st_set_chgain);
346
347int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
348{
349 struct omap_mcbsp *mcbsp;
350 struct omap_mcbsp_st_data *st_data;
351 int ret = 0;
352
353 if (!omap_mcbsp_check_valid_id(id)) {
354 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
355 return -ENODEV;
356 }
357
358 mcbsp = id_to_mcbsp_ptr(id);
359 st_data = mcbsp->st_data;
360
361 if (!st_data)
362 return -ENOENT;
363
364 spin_lock_irq(&mcbsp->lock);
365 if (channel == 0)
366 *chgain = st_data->ch0gain;
367 else if (channel == 1)
368 *chgain = st_data->ch1gain;
369 else
370 ret = -EINVAL;
371 spin_unlock_irq(&mcbsp->lock);
372
373 return ret;
374}
375EXPORT_SYMBOL(omap_st_get_chgain);
376
377static int omap_st_start(struct omap_mcbsp *mcbsp)
378{
379 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
380
381 if (st_data && st_data->enabled && !st_data->running) {
382 omap_st_fir_write(mcbsp, st_data->taps);
383 omap_st_chgain(mcbsp);
384
385 if (!mcbsp->free) {
386 omap_st_on(mcbsp);
387 st_data->running = 1;
388 }
389 }
390
391 return 0;
392}
393
394int omap_st_enable(unsigned int id)
395{
396 struct omap_mcbsp *mcbsp;
397 struct omap_mcbsp_st_data *st_data;
398
399 if (!omap_mcbsp_check_valid_id(id)) {
400 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
401 return -ENODEV;
402 }
403
404 mcbsp = id_to_mcbsp_ptr(id);
405 st_data = mcbsp->st_data;
406
407 if (!st_data)
408 return -ENODEV;
409
410 spin_lock_irq(&mcbsp->lock);
411 st_data->enabled = 1;
412 omap_st_start(mcbsp);
413 spin_unlock_irq(&mcbsp->lock);
414
415 return 0;
416}
417EXPORT_SYMBOL(omap_st_enable);
418
419static int omap_st_stop(struct omap_mcbsp *mcbsp)
420{
421 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
422
423 if (st_data && st_data->running) {
424 if (!mcbsp->free) {
425 omap_st_off(mcbsp);
426 st_data->running = 0;
427 }
428 }
429
430 return 0;
431}
432
433int omap_st_disable(unsigned int id)
434{
435 struct omap_mcbsp *mcbsp;
436 struct omap_mcbsp_st_data *st_data;
437 int ret = 0;
438
439 if (!omap_mcbsp_check_valid_id(id)) {
440 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
441 return -ENODEV;
442 }
443
444 mcbsp = id_to_mcbsp_ptr(id);
445 st_data = mcbsp->st_data;
446
447 if (!st_data)
448 return -ENODEV;
449
450 spin_lock_irq(&mcbsp->lock);
451 omap_st_stop(mcbsp);
452 st_data->enabled = 0;
453 spin_unlock_irq(&mcbsp->lock);
454
455 return ret;
456}
457EXPORT_SYMBOL(omap_st_disable);
458
459int omap_st_is_enabled(unsigned int id)
460{
461 struct omap_mcbsp *mcbsp;
462 struct omap_mcbsp_st_data *st_data;
463
464 if (!omap_mcbsp_check_valid_id(id)) {
465 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
466 return -ENODEV;
467 }
468
469 mcbsp = id_to_mcbsp_ptr(id);
470 st_data = mcbsp->st_data;
471
472 if (!st_data)
473 return -ENODEV;
474
475
476 return st_data->enabled;
477}
478EXPORT_SYMBOL(omap_st_is_enabled);
479
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300480/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300481 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
482 * The threshold parameter is 1 based, and it is converted (threshold - 1)
483 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300484 */
485void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
486{
487 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300488
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500489 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300490 return;
491
492 if (!omap_mcbsp_check_valid_id(id)) {
493 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
494 return;
495 }
496 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300497
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300498 if (threshold && threshold <= mcbsp->max_tx_thres)
499 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300500}
501EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
502
503/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300504 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
505 * The threshold parameter is 1 based, and it is converted (threshold - 1)
506 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300507 */
508void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
509{
510 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300511
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500512 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300513 return;
514
515 if (!omap_mcbsp_check_valid_id(id)) {
516 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
517 return;
518 }
519 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300520
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300521 if (threshold && threshold <= mcbsp->max_rx_thres)
522 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300523}
524EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +0300525
526/*
527 * omap_mcbsp_get_max_tx_thres just return the current configured
528 * maximum threshold for transmission
529 */
530u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
531{
532 struct omap_mcbsp *mcbsp;
533
534 if (!omap_mcbsp_check_valid_id(id)) {
535 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
536 return -ENODEV;
537 }
538 mcbsp = id_to_mcbsp_ptr(id);
539
540 return mcbsp->max_tx_thres;
541}
542EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
543
544/*
545 * omap_mcbsp_get_max_rx_thres just return the current configured
546 * maximum threshold for reception
547 */
548u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
549{
550 struct omap_mcbsp *mcbsp;
551
552 if (!omap_mcbsp_check_valid_id(id)) {
553 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
554 return -ENODEV;
555 }
556 mcbsp = id_to_mcbsp_ptr(id);
557
558 return mcbsp->max_rx_thres;
559}
560EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300561
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300562u16 omap_mcbsp_get_fifo_size(unsigned int id)
563{
564 struct omap_mcbsp *mcbsp;
565
566 if (!omap_mcbsp_check_valid_id(id)) {
567 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
568 return -ENODEV;
569 }
570 mcbsp = id_to_mcbsp_ptr(id);
571
572 return mcbsp->pdata->buffer_size;
573}
574EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
575
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200576/*
577 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
578 */
579u16 omap_mcbsp_get_tx_delay(unsigned int id)
580{
581 struct omap_mcbsp *mcbsp;
582 u16 buffstat;
583
584 if (!omap_mcbsp_check_valid_id(id)) {
585 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
586 return -ENODEV;
587 }
588 mcbsp = id_to_mcbsp_ptr(id);
589
590 /* Returns the number of free locations in the buffer */
591 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
592
593 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300594 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200595}
596EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
597
598/*
599 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
600 * to reach the threshold value (when the DMA will be triggered to read it)
601 */
602u16 omap_mcbsp_get_rx_delay(unsigned int id)
603{
604 struct omap_mcbsp *mcbsp;
605 u16 buffstat, threshold;
606
607 if (!omap_mcbsp_check_valid_id(id)) {
608 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
609 return -ENODEV;
610 }
611 mcbsp = id_to_mcbsp_ptr(id);
612
613 /* Returns the number of used locations in the buffer */
614 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
615 /* RX threshold */
616 threshold = MCBSP_READ(mcbsp, THRSH1);
617
618 /* Return the number of location till we reach the threshold limit */
619 if (threshold <= buffstat)
620 return 0;
621 else
622 return threshold - buffstat;
623}
624EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
625
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300626/*
627 * omap_mcbsp_get_dma_op_mode just return the current configured
628 * operating mode for the mcbsp channel
629 */
630int omap_mcbsp_get_dma_op_mode(unsigned int id)
631{
632 struct omap_mcbsp *mcbsp;
633 int dma_op_mode;
634
635 if (!omap_mcbsp_check_valid_id(id)) {
636 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
637 return -ENODEV;
638 }
639 mcbsp = id_to_mcbsp_ptr(id);
640
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300641 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300642
643 return dma_op_mode;
644}
645EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300646
647static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
648{
649 /*
650 * Enable wakup behavior, smart idle and all wakeups
651 * REVISIT: some wakeups may be unnecessary
652 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500653 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300654 u16 syscon;
655
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800656 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300657 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eduardo Valentind99a7452009-08-20 16:18:18 +0300658
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300659 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
660 syscon |= (ENAWAKEUP | SIDLEMODE(0x02) |
661 CLOCKACTIVITY(0x02));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800662 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300663 } else {
Eduardo Valentind99a7452009-08-20 16:18:18 +0300664 syscon |= SIDLEMODE(0x01);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300665 }
Eduardo Valentind99a7452009-08-20 16:18:18 +0300666
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800667 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300668 }
669}
670
671static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
672{
673 /*
674 * Disable wakup behavior, smart idle and all wakeups
675 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500676 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300677 u16 syscon;
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300678
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800679 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300680 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300681 /*
682 * HW bug workaround - If no_idle mode is taken, we need to
683 * go to smart_idle before going to always_idle, or the
684 * device will not hit retention anymore.
685 */
686 syscon |= SIDLEMODE(0x02);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800687 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300688
689 syscon &= ~(SIDLEMODE(0x03));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800690 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300691
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800692 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300693 }
694}
695#else
696static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
697static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000698static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
699static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300700#endif
701
Tony Lindgren120db2c2006-04-02 17:46:27 +0100702/*
703 * We can choose between IRQ based or polled IO.
704 * This needs to be called before omap_mcbsp_request().
705 */
706int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
707{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300708 struct omap_mcbsp *mcbsp;
709
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300710 if (!omap_mcbsp_check_valid_id(id)) {
711 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
712 return -ENODEV;
713 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300714 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100715
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300716 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100717
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300718 if (!mcbsp->free) {
719 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
720 mcbsp->id);
721 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100722 return -EINVAL;
723 }
724
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300725 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100726
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300727 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100728
729 return 0;
730}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300731EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733int omap_mcbsp_request(unsigned int id)
734{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300735 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800736 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737 int err;
738
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300739 if (!omap_mcbsp_check_valid_id(id)) {
740 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
741 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100742 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300744
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800745 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
746 if (!reg_cache) {
747 return -ENOMEM;
748 }
749
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300750 spin_lock(&mcbsp->lock);
751 if (!mcbsp->free) {
752 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
753 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800754 err = -EBUSY;
755 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756 }
757
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300758 mcbsp->free = 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800759 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300760 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761
Russell Kingb820ce42009-01-23 10:26:46 +0000762 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
763 mcbsp->pdata->ops->request(id);
764
765 clk_enable(mcbsp->iclk);
766 clk_enable(mcbsp->fclk);
767
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300768 /* Do procedure specific to omap34xx arch, if applicable */
769 omap34xx_mcbsp_request(mcbsp);
770
Jarkko Nikula5a070552008-10-08 10:01:41 +0300771 /*
772 * Make sure that transmitter, receiver and sample-rate generator are
773 * not running before activating IRQs.
774 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800775 MCBSP_WRITE(mcbsp, SPCR1, 0);
776 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300777
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300778 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100779 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300780 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300781 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
782 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100783 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300784 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
785 "for McBSP%d\n", mcbsp->tx_irq,
786 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800787 goto err_clk_disable;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100788 }
789
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500790 if (mcbsp->rx_irq) {
791 init_completion(&mcbsp->rx_irq_completion);
792 err = request_irq(mcbsp->rx_irq,
793 omap_mcbsp_rx_irq_handler,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300794 0, "McBSP", (void *)mcbsp);
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500795 if (err != 0) {
796 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
797 "for McBSP%d\n", mcbsp->rx_irq,
798 mcbsp->id);
799 goto err_free_irq;
800 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100801 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802 }
803
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100804 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800805err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800806 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800807err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800808 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800809 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800810
811 /* Do procedure specific to omap34xx arch, if applicable */
812 omap34xx_mcbsp_free(mcbsp);
813
814 clk_disable(mcbsp->fclk);
815 clk_disable(mcbsp->iclk);
816
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800817 spin_lock(&mcbsp->lock);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800818 mcbsp->free = 1;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800819 mcbsp->reg_cache = NULL;
820err_kfree:
821 spin_unlock(&mcbsp->lock);
822 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800823
824 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100825}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300826EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827
828void omap_mcbsp_free(unsigned int id)
829{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300830 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800831 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300832
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300833 if (!omap_mcbsp_check_valid_id(id)) {
834 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100835 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100836 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300837 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100838
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300839 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
840 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300841
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300842 /* Do procedure specific to omap34xx arch, if applicable */
843 omap34xx_mcbsp_free(mcbsp);
844
Russell Kingb820ce42009-01-23 10:26:46 +0000845 clk_disable(mcbsp->fclk);
846 clk_disable(mcbsp->iclk);
847
848 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
849 /* Free IRQs */
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500850 if (mcbsp->rx_irq)
851 free_irq(mcbsp->rx_irq, (void *)mcbsp);
Russell Kingb820ce42009-01-23 10:26:46 +0000852 free_irq(mcbsp->tx_irq, (void *)mcbsp);
853 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100854
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800855 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800857 spin_lock(&mcbsp->lock);
858 if (mcbsp->free)
859 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
860 else
861 mcbsp->free = 1;
862 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300863 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800864
865 if (reg_cache)
866 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100867}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300868EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869
870/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300871 * Here we start the McBSP, by enabling transmitter, receiver or both.
872 * If no transmitter or receiver is active prior calling, then sample-rate
873 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100874 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300875void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300877 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000878 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100879 u16 w;
880
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);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100883 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300884 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300885 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000887 if (cpu_is_omap34xx())
888 omap_st_start(mcbsp);
889
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800890 mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
891 mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100892
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000893 /* Only enable SRG, if McBSP is master */
894 w = MCBSP_READ_CACHE(mcbsp, PCR0);
895 if (w & (FSXM | FSRM | CLKXM | CLKRM))
896 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
897 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300898
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000899 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300900 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800901 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800902 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300903 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100904
905 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300906 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800907 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800908 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100909
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300910 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800911 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800912 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913
Eduardo Valentin44a63112009-08-20 16:18:09 +0300914 /*
915 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
916 * REVISIT: 100us may give enough time for two CLKSRG, however
917 * due to some unknown PM related, clock gating etc. reason it
918 * is now at 500us.
919 */
920 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000922 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300923 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800924 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800925 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300926 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100927
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500928 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300929 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800930 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300931 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800932 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800933 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300934 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800935 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300936 }
937
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100938 /* Dump McBSP Regs */
939 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100940}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300941EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100942
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300943void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300945 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300946 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100947 u16 w;
948
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300949 if (!omap_mcbsp_check_valid_id(id)) {
950 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100951 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300952 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100953
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300954 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100955
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300956 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300957 tx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500958 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800959 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300960 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800961 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300962 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800963 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800964 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100965
966 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300967 rx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500968 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800969 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700970 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800971 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300972 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800973 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800974 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100975
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800976 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
977 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300978
979 if (idle) {
980 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800981 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800982 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300983 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000984
985 if (cpu_is_omap34xx())
986 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100987}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300988EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100989
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100990/* polled mcbsp i/o operations */
991int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
992{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300993 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300994
995 if (!omap_mcbsp_check_valid_id(id)) {
996 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
997 return -ENODEV;
998 }
999
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001000 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001001
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001002 MCBSP_WRITE(mcbsp, DXR1, buf);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001003 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001004 if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001005 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001006 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001007 /* resend */
1008 return -1;
1009 } else {
1010 /* wait for transmit confirmation */
1011 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001012 while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001013 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001014 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001015 MCBSP_READ_CACHE(mcbsp, SPCR2) &
1016 (~XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001017 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001018 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001019 MCBSP_READ_CACHE(mcbsp, SPCR2) |
1020 (XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001021 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001022 dev_err(mcbsp->dev, "Could not write to"
1023 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001024 return -2;
1025 }
1026 }
1027 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001028
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001029 return 0;
1030}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001031EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001032
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001033int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001034{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001035 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001036
1037 if (!omap_mcbsp_check_valid_id(id)) {
1038 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1039 return -ENODEV;
1040 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001041 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001042
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001043 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001044 if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001045 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001046 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001047 /* resend */
1048 return -1;
1049 } else {
1050 /* wait for recieve confirmation */
1051 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001052 while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001053 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001054 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001055 MCBSP_READ_CACHE(mcbsp, SPCR1) &
1056 (~RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001057 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001058 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001059 MCBSP_READ_CACHE(mcbsp, SPCR1) |
1060 (RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001061 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001062 dev_err(mcbsp->dev, "Could not read from"
1063 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001064 return -2;
1065 }
1066 }
1067 }
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001068 *buf = MCBSP_READ(mcbsp, DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001069
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001070 return 0;
1071}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001072EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001073
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001074/*
1075 * IRQ based word transmission.
1076 */
1077void omap_mcbsp_xmit_word(unsigned int id, u32 word)
1078{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001079 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001080 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001081
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001082 if (!omap_mcbsp_check_valid_id(id)) {
1083 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001084 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001085 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001086
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001087 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001088 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001089
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001090 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001091
1092 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001093 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1094 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001095}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001096EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001097
1098u32 omap_mcbsp_recv_word(unsigned int id)
1099{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001100 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001101 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001102 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001103
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001104 if (!omap_mcbsp_check_valid_id(id)) {
1105 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1106 return -ENODEV;
1107 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001108 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001109
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001110 word_length = mcbsp->rx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001111
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001112 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001113
1114 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001115 word_msb = MCBSP_READ(mcbsp, DRR2);
1116 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001117
1118 return (word_lsb | (word_msb << 16));
1119}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001120EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001121
Tony Lindgren120db2c2006-04-02 17:46:27 +01001122int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
1123{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001124 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001125 omap_mcbsp_word_length tx_word_length;
1126 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001127 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1128
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001129 if (!omap_mcbsp_check_valid_id(id)) {
1130 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1131 return -ENODEV;
1132 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001133 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001134 tx_word_length = mcbsp->tx_word_length;
1135 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001136
Tony Lindgren120db2c2006-04-02 17:46:27 +01001137 if (tx_word_length != rx_word_length)
1138 return -EINVAL;
1139
1140 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001141 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001142 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001143 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001144 if (attempts++ > 1000) {
1145 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001146 MCBSP_WRITE(mcbsp, SPCR2,
1147 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001148 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001149 MCBSP_WRITE(mcbsp, SPCR2,
1150 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001151 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001152 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1153 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001154 return -EAGAIN;
1155 }
1156 }
1157
1158 /* Now we can push the data */
1159 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001160 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1161 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001162
1163 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001164 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001165 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001166 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001167 if (attempts++ > 1000) {
1168 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001169 MCBSP_WRITE(mcbsp, SPCR1,
1170 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001171 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001172 MCBSP_WRITE(mcbsp, SPCR1,
1173 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001174 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001175 dev_err(mcbsp->dev, "McBSP%d receiver not "
1176 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001177 return -EAGAIN;
1178 }
1179 }
1180
1181 /* Receiver is ready, let's read the dummy data */
1182 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001183 word_msb = MCBSP_READ(mcbsp, DRR2);
1184 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001185
1186 return 0;
1187}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001188EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001189
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001190int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +01001191{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001192 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +01001193 u32 clock_word = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001194 omap_mcbsp_word_length tx_word_length;
1195 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001196 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1197
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001198 if (!omap_mcbsp_check_valid_id(id)) {
1199 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1200 return -ENODEV;
1201 }
1202
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001203 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001204
1205 tx_word_length = mcbsp->tx_word_length;
1206 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001207
Tony Lindgren120db2c2006-04-02 17:46:27 +01001208 if (tx_word_length != rx_word_length)
1209 return -EINVAL;
1210
1211 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001212 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001213 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001214 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001215 if (attempts++ > 1000) {
1216 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001217 MCBSP_WRITE(mcbsp, SPCR2,
1218 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001219 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001220 MCBSP_WRITE(mcbsp, SPCR2,
1221 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001222 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001223 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1224 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001225 return -EAGAIN;
1226 }
1227 }
1228
1229 /* We first need to enable the bus clock */
1230 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001231 MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
1232 MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001233
1234 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001235 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001236 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001237 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001238 if (attempts++ > 1000) {
1239 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001240 MCBSP_WRITE(mcbsp, SPCR1,
1241 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001242 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001243 MCBSP_WRITE(mcbsp, SPCR1,
1244 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001245 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001246 dev_err(mcbsp->dev, "McBSP%d receiver not "
1247 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001248 return -EAGAIN;
1249 }
1250 }
1251
1252 /* Receiver is ready, there is something for us */
1253 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001254 word_msb = MCBSP_READ(mcbsp, DRR2);
1255 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001256
1257 word[0] = (word_lsb | (word_msb << 16));
1258
1259 return 0;
1260}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001261EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001262
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001263/*
1264 * Simple DMA based buffer rx/tx routines.
1265 * Nothing fancy, just a single buffer tx/rx through DMA.
1266 * The DMA resources are released once the transfer is done.
1267 * For anything fancier, you should use your own customized DMA
1268 * routines and callbacks.
1269 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001270int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
1271 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001272{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001273 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001274 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001275 int src_port = 0;
1276 int dest_port = 0;
1277 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001278
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001279 if (!omap_mcbsp_check_valid_id(id)) {
1280 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1281 return -ENODEV;
1282 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001283 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001284
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001285 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001286 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001287 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001288 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001289 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001290 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001291 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001292 return -EAGAIN;
1293 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001294 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001295
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001296 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001297 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001298
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001299 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001300
Tony Lindgren120db2c2006-04-02 17:46:27 +01001301 if (cpu_class_is_omap1()) {
1302 src_port = OMAP_DMA_PORT_TIPB;
1303 dest_port = OMAP_DMA_PORT_EMIFF;
1304 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001305 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001306 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001307
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001308 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001309 OMAP_DMA_DATA_TYPE_S16,
1310 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001311 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001312 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001313
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001314 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001315 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001316 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001317 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001318 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001319
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001320 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001321 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001322 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001323 buffer,
1324 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001325
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001326 omap_start_dma(mcbsp->dma_tx_lch);
1327 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001328
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001329 return 0;
1330}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001331EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001332
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001333int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
1334 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001335{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001336 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001337 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001338 int src_port = 0;
1339 int dest_port = 0;
1340 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001341
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001342 if (!omap_mcbsp_check_valid_id(id)) {
1343 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1344 return -ENODEV;
1345 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001346 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001347
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001348 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001349 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001350 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001351 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001352 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001353 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001354 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001355 return -EAGAIN;
1356 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001357 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001358
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001359 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001360 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001361
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001362 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001363
Tony Lindgren120db2c2006-04-02 17:46:27 +01001364 if (cpu_class_is_omap1()) {
1365 src_port = OMAP_DMA_PORT_TIPB;
1366 dest_port = OMAP_DMA_PORT_EMIFF;
1367 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001368 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001369 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001370
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001371 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001372 OMAP_DMA_DATA_TYPE_S16,
1373 length >> 1, 1,
1374 OMAP_DMA_SYNC_ELEMENT,
1375 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001376
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001377 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001378 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001379 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001380 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001381 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001382
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001383 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001384 dest_port,
1385 OMAP_DMA_AMODE_POST_INC,
1386 buffer,
1387 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001388
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001389 omap_start_dma(mcbsp->dma_rx_lch);
1390 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001391
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001392 return 0;
1393}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001394EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001395
1396/*
1397 * SPI wrapper.
1398 * Since SPI setup is much simpler than the generic McBSP one,
1399 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1400 * Once this is done, you can call omap_mcbsp_start().
1401 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001402void omap_mcbsp_set_spi_mode(unsigned int id,
1403 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001404{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001405 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001406 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1407
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001408 if (!omap_mcbsp_check_valid_id(id)) {
1409 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001410 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001411 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001412 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001413
1414 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1415
1416 /* SPI has only one frame */
1417 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1418 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1419
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001420 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001421 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1422 mcbsp_cfg.spcr1 |= (1 << 12);
1423 else
1424 mcbsp_cfg.spcr1 |= (3 << 11);
1425
1426 /* Set clock parities */
1427 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1428 mcbsp_cfg.pcr0 |= CLKRP;
1429 else
1430 mcbsp_cfg.pcr0 &= ~CLKRP;
1431
1432 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1433 mcbsp_cfg.pcr0 &= ~CLKXP;
1434 else
1435 mcbsp_cfg.pcr0 |= CLKXP;
1436
1437 /* Set SCLKME to 0 and CLKSM to 1 */
1438 mcbsp_cfg.pcr0 &= ~SCLKME;
1439 mcbsp_cfg.srgr2 |= CLKSM;
1440
1441 /* Set FSXP */
1442 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1443 mcbsp_cfg.pcr0 &= ~FSXP;
1444 else
1445 mcbsp_cfg.pcr0 |= FSXP;
1446
1447 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1448 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001449 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001450 mcbsp_cfg.pcr0 |= FSXM;
1451 mcbsp_cfg.srgr2 &= ~FSGM;
1452 mcbsp_cfg.xcr2 |= XDATDLY(1);
1453 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001454 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001455 mcbsp_cfg.pcr0 &= ~CLKXM;
1456 mcbsp_cfg.srgr1 |= CLKGDV(1);
1457 mcbsp_cfg.pcr0 &= ~FSXM;
1458 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1459 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1460 }
1461
1462 mcbsp_cfg.xcr2 &= ~XPHASE;
1463 mcbsp_cfg.rcr2 &= ~RPHASE;
1464
1465 omap_mcbsp_config(id, &mcbsp_cfg);
1466}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001467EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001468
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001469#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001470#define max_thres(m) (mcbsp->pdata->buffer_size)
1471#define valid_threshold(m, val) ((val) <= max_thres(m))
1472#define THRESHOLD_PROP_BUILDER(prop) \
1473static ssize_t prop##_show(struct device *dev, \
1474 struct device_attribute *attr, char *buf) \
1475{ \
1476 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1477 \
1478 return sprintf(buf, "%u\n", mcbsp->prop); \
1479} \
1480 \
1481static ssize_t prop##_store(struct device *dev, \
1482 struct device_attribute *attr, \
1483 const char *buf, size_t size) \
1484{ \
1485 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1486 unsigned long val; \
1487 int status; \
1488 \
1489 status = strict_strtoul(buf, 0, &val); \
1490 if (status) \
1491 return status; \
1492 \
1493 if (!valid_threshold(mcbsp, val)) \
1494 return -EDOM; \
1495 \
1496 mcbsp->prop = val; \
1497 return size; \
1498} \
1499 \
1500static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1501
1502THRESHOLD_PROP_BUILDER(max_tx_thres);
1503THRESHOLD_PROP_BUILDER(max_rx_thres);
1504
Jarkko Nikula9b300502009-08-24 17:45:50 +03001505static const char *dma_op_modes[] = {
1506 "element", "threshold", "frame",
1507};
1508
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001509static ssize_t dma_op_mode_show(struct device *dev,
1510 struct device_attribute *attr, char *buf)
1511{
1512 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001513 int dma_op_mode, i = 0;
1514 ssize_t len = 0;
1515 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001516
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001517 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001518
Jarkko Nikula9b300502009-08-24 17:45:50 +03001519 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1520 if (dma_op_mode == i)
1521 len += sprintf(buf + len, "[%s] ", *s);
1522 else
1523 len += sprintf(buf + len, "%s ", *s);
1524 }
1525 len += sprintf(buf + len, "\n");
1526
1527 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001528}
1529
1530static ssize_t dma_op_mode_store(struct device *dev,
1531 struct device_attribute *attr,
1532 const char *buf, size_t size)
1533{
1534 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001535 const char * const *s;
1536 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001537
Jarkko Nikula9b300502009-08-24 17:45:50 +03001538 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1539 if (sysfs_streq(buf, *s))
1540 break;
1541
1542 if (i == ARRAY_SIZE(dma_op_modes))
1543 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001544
1545 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001546 if (!mcbsp->free) {
1547 size = -EBUSY;
1548 goto unlock;
1549 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001550 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001551
1552unlock:
1553 spin_unlock_irq(&mcbsp->lock);
1554
1555 return size;
1556}
1557
1558static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1559
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001560static ssize_t st_taps_show(struct device *dev,
1561 struct device_attribute *attr, char *buf)
1562{
1563 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1564 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1565 ssize_t status = 0;
1566 int i;
1567
1568 spin_lock_irq(&mcbsp->lock);
1569 for (i = 0; i < st_data->nr_taps; i++)
1570 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1571 st_data->taps[i]);
1572 if (i)
1573 status += sprintf(&buf[status], "\n");
1574 spin_unlock_irq(&mcbsp->lock);
1575
1576 return status;
1577}
1578
1579static ssize_t st_taps_store(struct device *dev,
1580 struct device_attribute *attr,
1581 const char *buf, size_t size)
1582{
1583 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1584 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1585 int val, tmp, status, i = 0;
1586
1587 spin_lock_irq(&mcbsp->lock);
1588 memset(st_data->taps, 0, sizeof(st_data->taps));
1589 st_data->nr_taps = 0;
1590
1591 do {
1592 status = sscanf(buf, "%d%n", &val, &tmp);
1593 if (status < 0 || status == 0) {
1594 size = -EINVAL;
1595 goto out;
1596 }
1597 if (val < -32768 || val > 32767) {
1598 size = -EINVAL;
1599 goto out;
1600 }
1601 st_data->taps[i++] = val;
1602 buf += tmp;
1603 if (*buf != ',')
1604 break;
1605 buf++;
1606 } while (1);
1607
1608 st_data->nr_taps = i;
1609
1610out:
1611 spin_unlock_irq(&mcbsp->lock);
1612
1613 return size;
1614}
1615
1616static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1617
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001618static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001619 &dev_attr_max_tx_thres.attr,
1620 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001621 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001622 NULL,
1623};
1624
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001625static const struct attribute_group additional_attr_group = {
1626 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001627};
1628
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001629static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001630{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001631 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001632}
1633
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001634static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001635{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001636 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001637}
1638
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001639static const struct attribute *sidetone_attrs[] = {
1640 &dev_attr_st_taps.attr,
1641 NULL,
1642};
1643
1644static const struct attribute_group sidetone_attr_group = {
1645 .attrs = (struct attribute **)sidetone_attrs,
1646};
1647
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -07001648static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001649{
1650 struct omap_mcbsp_platform_data *pdata = mcbsp->pdata;
1651 struct omap_mcbsp_st_data *st_data;
1652 int err;
1653
1654 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1655 if (!st_data) {
1656 err = -ENOMEM;
1657 goto err1;
1658 }
1659
1660 st_data->io_base_st = ioremap(pdata->phys_base_st, SZ_4K);
1661 if (!st_data->io_base_st) {
1662 err = -ENOMEM;
1663 goto err2;
1664 }
1665
1666 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1667 if (err)
1668 goto err3;
1669
1670 mcbsp->st_data = st_data;
1671 return 0;
1672
1673err3:
1674 iounmap(st_data->io_base_st);
1675err2:
1676 kfree(st_data);
1677err1:
1678 return err;
1679
1680}
1681
1682static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1683{
1684 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1685
1686 if (st_data) {
1687 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1688 iounmap(st_data->io_base_st);
1689 kfree(st_data);
1690 }
1691}
1692
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001693static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1694{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001695 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001696 if (cpu_is_omap34xx()) {
Peter Ujfalusi451fd822010-06-03 07:39:33 +03001697 /*
1698 * Initially configure the maximum thresholds to a safe value.
1699 * The McBSP FIFO usage with these values should not go under
1700 * 16 locations.
1701 * If the whole FIFO without safety buffer is used, than there
1702 * is a possibility that the DMA will be not able to push the
1703 * new data on time, causing channel shifts in runtime.
1704 */
1705 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1706 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001707 /*
1708 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1709 * for mcbsp2 instances.
1710 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001711 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001712 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001713 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001714
1715 if (mcbsp->id == 2 || mcbsp->id == 3)
1716 if (omap_st_add(mcbsp))
1717 dev_warn(mcbsp->dev,
1718 "Unable to create sidetone controls\n");
1719
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001720 } else {
1721 mcbsp->max_tx_thres = -EINVAL;
1722 mcbsp->max_rx_thres = -EINVAL;
1723 }
1724}
1725
1726static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1727{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001728 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001729 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001730
1731 if (mcbsp->id == 2 || mcbsp->id == 3)
1732 omap_st_remove(mcbsp);
1733 }
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001734}
1735#else
1736static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1737static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001738#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001739
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001740/*
1741 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1742 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1743 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001744static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001745{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001746 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001747 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001748 int id = pdev->id - 1;
1749 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001750
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001751 if (!pdata) {
1752 dev_err(&pdev->dev, "McBSP device initialized without"
1753 "platform data\n");
1754 ret = -EINVAL;
1755 goto exit;
1756 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001757
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001758 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001759
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001760 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001761 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1762 ret = -EINVAL;
1763 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001764 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001765
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001766 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1767 if (!mcbsp) {
1768 ret = -ENOMEM;
1769 goto exit;
1770 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001771
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001772 spin_lock_init(&mcbsp->lock);
1773 mcbsp->id = id + 1;
1774 mcbsp->free = 1;
1775 mcbsp->dma_tx_lch = -1;
1776 mcbsp->dma_rx_lch = -1;
1777
1778 mcbsp->phys_base = pdata->phys_base;
1779 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1780 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001781 ret = -ENOMEM;
1782 goto err_ioremap;
1783 }
1784
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001785 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001786 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1787 mcbsp->tx_irq = pdata->tx_irq;
1788 mcbsp->rx_irq = pdata->rx_irq;
1789 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1790 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001791
Russell Kingb820ce42009-01-23 10:26:46 +00001792 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1793 if (IS_ERR(mcbsp->iclk)) {
1794 ret = PTR_ERR(mcbsp->iclk);
1795 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1796 goto err_iclk;
1797 }
Stanley.Miao06151152009-01-29 08:57:12 -08001798
Russell Kingb820ce42009-01-23 10:26:46 +00001799 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1800 if (IS_ERR(mcbsp->fclk)) {
1801 ret = PTR_ERR(mcbsp->fclk);
1802 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1803 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001804 }
1805
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001806 mcbsp->pdata = pdata;
1807 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001808 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001809 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001810
1811 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1812 omap34xx_device_init(mcbsp);
1813
Russell Kingd592dd12008-09-04 14:25:42 +01001814 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001815
Russell Kingb820ce42009-01-23 10:26:46 +00001816err_fclk:
1817 clk_put(mcbsp->iclk);
1818err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001819 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001820err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001821 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001822exit:
1823 return ret;
1824}
1825
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001826static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001827{
1828 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1829
1830 platform_set_drvdata(pdev, NULL);
1831 if (mcbsp) {
1832
1833 if (mcbsp->pdata && mcbsp->pdata->ops &&
1834 mcbsp->pdata->ops->free)
1835 mcbsp->pdata->ops->free(mcbsp->id);
1836
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001837 omap34xx_device_exit(mcbsp);
1838
Russell Kingb820ce42009-01-23 10:26:46 +00001839 clk_disable(mcbsp->fclk);
1840 clk_disable(mcbsp->iclk);
1841 clk_put(mcbsp->fclk);
1842 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001843
Russell Kingd592dd12008-09-04 14:25:42 +01001844 iounmap(mcbsp->io_base);
1845
Russell Kingb820ce42009-01-23 10:26:46 +00001846 mcbsp->fclk = NULL;
1847 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001848 mcbsp->free = 0;
1849 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001850 }
1851
1852 return 0;
1853}
1854
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001855static struct platform_driver omap_mcbsp_driver = {
1856 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001857 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001858 .driver = {
1859 .name = "omap-mcbsp",
1860 },
1861};
1862
1863int __init omap_mcbsp_init(void)
1864{
1865 /* Register the McBSP driver */
1866 return platform_driver_register(&omap_mcbsp_driver);
1867}