blob: 5f25ae5f9c1d22ffa863b2f2bb81c36c071cff19 [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
Paul Walmsley59fb6592010-12-21 15:30:55 -070031/* XXX These "sideways" includes are a sign that something is wrong */
32#include "../mach-omap2/cm2xxx_3xxx.h"
Eero Nurkkalad912fa92010-02-22 12:21:11 +000033#include "../mach-omap2/cm-regbits-34xx.h"
34
Chandra Shekharb4b58f52008-10-08 10:01:39 +030035struct omap_mcbsp **mcbsp_ptr;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080036int omap_mcbsp_count, omap_mcbsp_cache_size;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030037
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070038static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030039{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080040 if (cpu_class_is_omap1()) {
41 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080042 __raw_writew((u16)val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080043 } else if (cpu_is_omap2420()) {
44 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
45 __raw_writew((u16)val, mcbsp->io_base + reg);
46 } else {
47 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080048 __raw_writel(val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080049 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030050}
51
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070052static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030053{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080054 if (cpu_class_is_omap1()) {
55 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
56 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
57 } else if (cpu_is_omap2420()) {
58 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
59 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
60 } else {
61 return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
62 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
63 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030064}
65
Eero Nurkkalad912fa92010-02-22 12:21:11 +000066#ifdef CONFIG_ARCH_OMAP3
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070067static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000068{
69 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
70}
71
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070072static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000073{
74 return __raw_readl(mcbsp->st_data->io_base_st + reg);
75}
76#endif
77
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080078#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080079 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080080#define MCBSP_WRITE(mcbsp, reg, val) \
81 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080082#define MCBSP_READ_CACHE(mcbsp, reg) \
83 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030084
Eero Nurkkalad912fa92010-02-22 12:21:11 +000085#define MCBSP_ST_READ(mcbsp, reg) \
86 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
87#define MCBSP_ST_WRITE(mcbsp, reg, val) \
88 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
89
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090static void omap_mcbsp_dump_reg(u8 id)
91{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030092 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
93
94 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
95 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080096 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030097 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080098 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030099 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800100 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300101 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800102 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300103 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800104 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300105 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800106 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300107 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800108 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300109 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800110 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300111 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800112 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300113 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800114 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300115 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800116 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300117 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800118 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300119 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800120 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300121 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122}
123
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700124static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100125{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400126 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700127 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800129 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700130 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100131
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700132 if (irqst_spcr2 & XSYNC_ERR) {
133 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
134 irqst_spcr2);
135 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000136 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700137 } else {
138 complete(&mcbsp_tx->tx_irq_completion);
139 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300140
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100141 return IRQ_HANDLED;
142}
143
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700144static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400146 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700147 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800149 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700150 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700152 if (irqst_spcr1 & RSYNC_ERR) {
153 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
154 irqst_spcr1);
155 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000156 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700157 } else {
Scott Elliscb922d22010-09-23 18:47:23 -0700158 complete(&mcbsp_rx->rx_irq_completion);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700159 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300160
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161 return IRQ_HANDLED;
162}
163
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100164static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
165{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400166 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100167
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300168 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800169 MCBSP_READ(mcbsp_dma_tx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100170
171 /* We can free the channels */
172 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
173 mcbsp_dma_tx->dma_tx_lch = -1;
174
175 complete(&mcbsp_dma_tx->tx_dma_completion);
176}
177
178static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
179{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400180 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100181
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300182 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800183 MCBSP_READ(mcbsp_dma_rx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100184
185 /* We can free the channels */
186 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
187 mcbsp_dma_rx->dma_rx_lch = -1;
188
189 complete(&mcbsp_dma_rx->rx_dma_completion);
190}
191
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100192/*
193 * omap_mcbsp_config simply write a config to the
194 * appropriate McBSP.
195 * You either call this function or set the McBSP registers
196 * by yourself before calling omap_mcbsp_start().
197 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300198void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100199{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300200 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100201
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300202 if (!omap_mcbsp_check_valid_id(id)) {
203 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
204 return;
205 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300206 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300207
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300208 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
209 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100210
211 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800212 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
213 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
214 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
215 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
216 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
217 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
218 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
219 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
220 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
221 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
222 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Syed Rafiuddina5b92cc2009-07-28 18:57:10 +0530223 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800224 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
225 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200226 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100227}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300228EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100229
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800230#ifdef CONFIG_ARCH_OMAP3
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000231static void omap_st_on(struct omap_mcbsp *mcbsp)
232{
233 unsigned int w;
234
235 /*
236 * Sidetone uses McBSP ICLK - which must not idle when sidetones
237 * are enabled or sidetones start sounding ugly.
238 */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700239 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000240 w &= ~(1 << (mcbsp->id - 2));
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700241 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000242
243 /* Enable McBSP Sidetone */
244 w = MCBSP_READ(mcbsp, SSELCR);
245 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
246
247 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
248 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w & ~(ST_AUTOIDLE));
249
250 /* Enable Sidetone from Sidetone Core */
251 w = MCBSP_ST_READ(mcbsp, SSELCR);
252 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
253}
254
255static void omap_st_off(struct omap_mcbsp *mcbsp)
256{
257 unsigned int w;
258
259 w = MCBSP_ST_READ(mcbsp, SSELCR);
260 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
261
262 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
263 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w | ST_AUTOIDLE);
264
265 w = MCBSP_READ(mcbsp, SSELCR);
266 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
267
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700268 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000269 w |= 1 << (mcbsp->id - 2);
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700270 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000271}
272
273static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
274{
275 u16 val, i;
276
277 val = MCBSP_ST_READ(mcbsp, SYSCONFIG);
278 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, val & ~(ST_AUTOIDLE));
279
280 val = MCBSP_ST_READ(mcbsp, SSELCR);
281
282 if (val & ST_COEFFWREN)
283 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
284
285 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
286
287 for (i = 0; i < 128; i++)
288 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
289
290 i = 0;
291
292 val = MCBSP_ST_READ(mcbsp, SSELCR);
293 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
294 val = MCBSP_ST_READ(mcbsp, SSELCR);
295
296 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
297
298 if (i == 1000)
299 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
300}
301
302static void omap_st_chgain(struct omap_mcbsp *mcbsp)
303{
304 u16 w;
305 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
306
307 w = MCBSP_ST_READ(mcbsp, SYSCONFIG);
308 MCBSP_ST_WRITE(mcbsp, SYSCONFIG, w & ~(ST_AUTOIDLE));
309
310 w = MCBSP_ST_READ(mcbsp, SSELCR);
311
312 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
313 ST_CH1GAIN(st_data->ch1gain));
314}
315
316int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
317{
318 struct omap_mcbsp *mcbsp;
319 struct omap_mcbsp_st_data *st_data;
320 int ret = 0;
321
322 if (!omap_mcbsp_check_valid_id(id)) {
323 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
324 return -ENODEV;
325 }
326
327 mcbsp = id_to_mcbsp_ptr(id);
328 st_data = mcbsp->st_data;
329
330 if (!st_data)
331 return -ENOENT;
332
333 spin_lock_irq(&mcbsp->lock);
334 if (channel == 0)
335 st_data->ch0gain = chgain;
336 else if (channel == 1)
337 st_data->ch1gain = chgain;
338 else
339 ret = -EINVAL;
340
341 if (st_data->enabled)
342 omap_st_chgain(mcbsp);
343 spin_unlock_irq(&mcbsp->lock);
344
345 return ret;
346}
347EXPORT_SYMBOL(omap_st_set_chgain);
348
349int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
350{
351 struct omap_mcbsp *mcbsp;
352 struct omap_mcbsp_st_data *st_data;
353 int ret = 0;
354
355 if (!omap_mcbsp_check_valid_id(id)) {
356 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
357 return -ENODEV;
358 }
359
360 mcbsp = id_to_mcbsp_ptr(id);
361 st_data = mcbsp->st_data;
362
363 if (!st_data)
364 return -ENOENT;
365
366 spin_lock_irq(&mcbsp->lock);
367 if (channel == 0)
368 *chgain = st_data->ch0gain;
369 else if (channel == 1)
370 *chgain = st_data->ch1gain;
371 else
372 ret = -EINVAL;
373 spin_unlock_irq(&mcbsp->lock);
374
375 return ret;
376}
377EXPORT_SYMBOL(omap_st_get_chgain);
378
379static int omap_st_start(struct omap_mcbsp *mcbsp)
380{
381 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
382
383 if (st_data && st_data->enabled && !st_data->running) {
384 omap_st_fir_write(mcbsp, st_data->taps);
385 omap_st_chgain(mcbsp);
386
387 if (!mcbsp->free) {
388 omap_st_on(mcbsp);
389 st_data->running = 1;
390 }
391 }
392
393 return 0;
394}
395
396int omap_st_enable(unsigned int id)
397{
398 struct omap_mcbsp *mcbsp;
399 struct omap_mcbsp_st_data *st_data;
400
401 if (!omap_mcbsp_check_valid_id(id)) {
402 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
403 return -ENODEV;
404 }
405
406 mcbsp = id_to_mcbsp_ptr(id);
407 st_data = mcbsp->st_data;
408
409 if (!st_data)
410 return -ENODEV;
411
412 spin_lock_irq(&mcbsp->lock);
413 st_data->enabled = 1;
414 omap_st_start(mcbsp);
415 spin_unlock_irq(&mcbsp->lock);
416
417 return 0;
418}
419EXPORT_SYMBOL(omap_st_enable);
420
421static int omap_st_stop(struct omap_mcbsp *mcbsp)
422{
423 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
424
425 if (st_data && st_data->running) {
426 if (!mcbsp->free) {
427 omap_st_off(mcbsp);
428 st_data->running = 0;
429 }
430 }
431
432 return 0;
433}
434
435int omap_st_disable(unsigned int id)
436{
437 struct omap_mcbsp *mcbsp;
438 struct omap_mcbsp_st_data *st_data;
439 int ret = 0;
440
441 if (!omap_mcbsp_check_valid_id(id)) {
442 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
443 return -ENODEV;
444 }
445
446 mcbsp = id_to_mcbsp_ptr(id);
447 st_data = mcbsp->st_data;
448
449 if (!st_data)
450 return -ENODEV;
451
452 spin_lock_irq(&mcbsp->lock);
453 omap_st_stop(mcbsp);
454 st_data->enabled = 0;
455 spin_unlock_irq(&mcbsp->lock);
456
457 return ret;
458}
459EXPORT_SYMBOL(omap_st_disable);
460
461int omap_st_is_enabled(unsigned int id)
462{
463 struct omap_mcbsp *mcbsp;
464 struct omap_mcbsp_st_data *st_data;
465
466 if (!omap_mcbsp_check_valid_id(id)) {
467 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
468 return -ENODEV;
469 }
470
471 mcbsp = id_to_mcbsp_ptr(id);
472 st_data = mcbsp->st_data;
473
474 if (!st_data)
475 return -ENODEV;
476
477
478 return st_data->enabled;
479}
480EXPORT_SYMBOL(omap_st_is_enabled);
481
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300482/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300483 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
484 * The threshold parameter is 1 based, and it is converted (threshold - 1)
485 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300486 */
487void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
488{
489 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300490
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500491 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300492 return;
493
494 if (!omap_mcbsp_check_valid_id(id)) {
495 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
496 return;
497 }
498 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300499
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300500 if (threshold && threshold <= mcbsp->max_tx_thres)
501 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300502}
503EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
504
505/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300506 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
507 * The threshold parameter is 1 based, and it is converted (threshold - 1)
508 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300509 */
510void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
511{
512 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300513
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500514 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300515 return;
516
517 if (!omap_mcbsp_check_valid_id(id)) {
518 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
519 return;
520 }
521 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300522
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300523 if (threshold && threshold <= mcbsp->max_rx_thres)
524 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300525}
526EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300527
528/*
529 * omap_mcbsp_get_max_tx_thres just return the current configured
530 * maximum threshold for transmission
531 */
532u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
533{
534 struct omap_mcbsp *mcbsp;
535
536 if (!omap_mcbsp_check_valid_id(id)) {
537 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
538 return -ENODEV;
539 }
540 mcbsp = id_to_mcbsp_ptr(id);
541
542 return mcbsp->max_tx_thres;
543}
544EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
545
546/*
547 * omap_mcbsp_get_max_rx_thres just return the current configured
548 * maximum threshold for reception
549 */
550u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
551{
552 struct omap_mcbsp *mcbsp;
553
554 if (!omap_mcbsp_check_valid_id(id)) {
555 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
556 return -ENODEV;
557 }
558 mcbsp = id_to_mcbsp_ptr(id);
559
560 return mcbsp->max_rx_thres;
561}
562EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300563
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300564u16 omap_mcbsp_get_fifo_size(unsigned int id)
565{
566 struct omap_mcbsp *mcbsp;
567
568 if (!omap_mcbsp_check_valid_id(id)) {
569 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
570 return -ENODEV;
571 }
572 mcbsp = id_to_mcbsp_ptr(id);
573
574 return mcbsp->pdata->buffer_size;
575}
576EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
577
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200578/*
579 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
580 */
581u16 omap_mcbsp_get_tx_delay(unsigned int id)
582{
583 struct omap_mcbsp *mcbsp;
584 u16 buffstat;
585
586 if (!omap_mcbsp_check_valid_id(id)) {
587 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
588 return -ENODEV;
589 }
590 mcbsp = id_to_mcbsp_ptr(id);
591
592 /* Returns the number of free locations in the buffer */
593 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
594
595 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300596 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200597}
598EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
599
600/*
601 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
602 * to reach the threshold value (when the DMA will be triggered to read it)
603 */
604u16 omap_mcbsp_get_rx_delay(unsigned int id)
605{
606 struct omap_mcbsp *mcbsp;
607 u16 buffstat, threshold;
608
609 if (!omap_mcbsp_check_valid_id(id)) {
610 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
611 return -ENODEV;
612 }
613 mcbsp = id_to_mcbsp_ptr(id);
614
615 /* Returns the number of used locations in the buffer */
616 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
617 /* RX threshold */
618 threshold = MCBSP_READ(mcbsp, THRSH1);
619
620 /* Return the number of location till we reach the threshold limit */
621 if (threshold <= buffstat)
622 return 0;
623 else
624 return threshold - buffstat;
625}
626EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
627
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300628/*
629 * omap_mcbsp_get_dma_op_mode just return the current configured
630 * operating mode for the mcbsp channel
631 */
632int omap_mcbsp_get_dma_op_mode(unsigned int id)
633{
634 struct omap_mcbsp *mcbsp;
635 int dma_op_mode;
636
637 if (!omap_mcbsp_check_valid_id(id)) {
638 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
639 return -ENODEV;
640 }
641 mcbsp = id_to_mcbsp_ptr(id);
642
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300643 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300644
645 return dma_op_mode;
646}
647EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300648
649static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
650{
651 /*
652 * Enable wakup behavior, smart idle and all wakeups
653 * REVISIT: some wakeups may be unnecessary
654 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500655 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300656 u16 syscon;
657
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800658 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300659 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eduardo Valentind99a7452009-08-20 16:18:18 +0300660
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300661 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
662 syscon |= (ENAWAKEUP | SIDLEMODE(0x02) |
663 CLOCKACTIVITY(0x02));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800664 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300665 } else {
Eduardo Valentind99a7452009-08-20 16:18:18 +0300666 syscon |= SIDLEMODE(0x01);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300667 }
Eduardo Valentind99a7452009-08-20 16:18:18 +0300668
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800669 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300670 }
671}
672
673static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
674{
675 /*
676 * Disable wakup behavior, smart idle and all wakeups
677 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500678 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300679 u16 syscon;
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300680
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800681 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300682 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300683 /*
684 * HW bug workaround - If no_idle mode is taken, we need to
685 * go to smart_idle before going to always_idle, or the
686 * device will not hit retention anymore.
687 */
688 syscon |= SIDLEMODE(0x02);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800689 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300690
691 syscon &= ~(SIDLEMODE(0x03));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800692 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300693
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800694 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300695 }
696}
697#else
698static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
699static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000700static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
701static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300702#endif
703
Tony Lindgren120db2c2006-04-02 17:46:27 +0100704/*
705 * We can choose between IRQ based or polled IO.
706 * This needs to be called before omap_mcbsp_request().
707 */
708int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
709{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300710 struct omap_mcbsp *mcbsp;
711
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300712 if (!omap_mcbsp_check_valid_id(id)) {
713 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
714 return -ENODEV;
715 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300716 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100717
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300718 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100719
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300720 if (!mcbsp->free) {
721 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
722 mcbsp->id);
723 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100724 return -EINVAL;
725 }
726
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300727 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100728
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300729 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100730
731 return 0;
732}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300733EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735int omap_mcbsp_request(unsigned int id)
736{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300737 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800738 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739 int err;
740
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300741 if (!omap_mcbsp_check_valid_id(id)) {
742 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
743 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100744 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300745 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300746
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800747 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
748 if (!reg_cache) {
749 return -ENOMEM;
750 }
751
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300752 spin_lock(&mcbsp->lock);
753 if (!mcbsp->free) {
754 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
755 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800756 err = -EBUSY;
757 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758 }
759
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800760 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800761 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300762 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763
Russell Kingb820ce42009-01-23 10:26:46 +0000764 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
765 mcbsp->pdata->ops->request(id);
766
767 clk_enable(mcbsp->iclk);
768 clk_enable(mcbsp->fclk);
769
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300770 /* Do procedure specific to omap34xx arch, if applicable */
771 omap34xx_mcbsp_request(mcbsp);
772
Jarkko Nikula5a070552008-10-08 10:01:41 +0300773 /*
774 * Make sure that transmitter, receiver and sample-rate generator are
775 * not running before activating IRQs.
776 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800777 MCBSP_WRITE(mcbsp, SPCR1, 0);
778 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300779
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300780 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100781 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300782 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300783 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
784 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100785 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300786 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
787 "for McBSP%d\n", mcbsp->tx_irq,
788 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800789 goto err_clk_disable;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100790 }
791
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500792 if (mcbsp->rx_irq) {
793 init_completion(&mcbsp->rx_irq_completion);
794 err = request_irq(mcbsp->rx_irq,
795 omap_mcbsp_rx_irq_handler,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300796 0, "McBSP", (void *)mcbsp);
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500797 if (err != 0) {
798 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
799 "for McBSP%d\n", mcbsp->rx_irq,
800 mcbsp->id);
801 goto err_free_irq;
802 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100803 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100804 }
805
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100806 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800807err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800808 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800809err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800810 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800811 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800812
813 /* Do procedure specific to omap34xx arch, if applicable */
814 omap34xx_mcbsp_free(mcbsp);
815
816 clk_disable(mcbsp->fclk);
817 clk_disable(mcbsp->iclk);
818
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800819 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800820 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800821 mcbsp->reg_cache = NULL;
822err_kfree:
823 spin_unlock(&mcbsp->lock);
824 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800825
826 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300828EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100829
830void omap_mcbsp_free(unsigned int id)
831{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300832 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800833 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300834
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300835 if (!omap_mcbsp_check_valid_id(id)) {
836 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100837 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100838 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300839 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100840
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300841 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
842 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300843
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300844 /* Do procedure specific to omap34xx arch, if applicable */
845 omap34xx_mcbsp_free(mcbsp);
846
Russell Kingb820ce42009-01-23 10:26:46 +0000847 clk_disable(mcbsp->fclk);
848 clk_disable(mcbsp->iclk);
849
850 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
851 /* Free IRQs */
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500852 if (mcbsp->rx_irq)
853 free_irq(mcbsp->rx_irq, (void *)mcbsp);
Russell Kingb820ce42009-01-23 10:26:46 +0000854 free_irq(mcbsp->tx_irq, (void *)mcbsp);
855 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800857 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800859 spin_lock(&mcbsp->lock);
860 if (mcbsp->free)
861 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
862 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800863 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800864 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300865 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800866
867 if (reg_cache)
868 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300870EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100871
872/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300873 * Here we start the McBSP, by enabling transmitter, receiver or both.
874 * If no transmitter or receiver is active prior calling, then sample-rate
875 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300877void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100878{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300879 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000880 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100881 u16 w;
882
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300883 if (!omap_mcbsp_check_valid_id(id)) {
884 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100885 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300886 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300887 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000889 if (cpu_is_omap34xx())
890 omap_st_start(mcbsp);
891
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800892 mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
893 mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100894
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000895 /* Only enable SRG, if McBSP is master */
896 w = MCBSP_READ_CACHE(mcbsp, PCR0);
897 if (w & (FSXM | FSRM | CLKXM | CLKRM))
898 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
899 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300900
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000901 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300902 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800903 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800904 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300905 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100906
907 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300908 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800909 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800910 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100911
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300912 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800913 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800914 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915
Eduardo Valentin44a63112009-08-20 16:18:09 +0300916 /*
917 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
918 * REVISIT: 100us may give enough time for two CLKSRG, however
919 * due to some unknown PM related, clock gating etc. reason it
920 * is now at 500us.
921 */
922 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100923
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000924 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300925 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800926 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800927 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300928 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100929
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500930 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300931 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800932 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300933 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800934 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800935 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300936 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800937 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300938 }
939
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100940 /* Dump McBSP Regs */
941 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100942}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300943EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300945void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100946{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300947 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300948 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100949 u16 w;
950
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300951 if (!omap_mcbsp_check_valid_id(id)) {
952 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100953 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300954 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100955
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300956 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300958 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300959 tx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500960 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800961 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300962 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800963 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300964 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800965 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800966 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100967
968 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300969 rx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500970 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800971 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700972 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800973 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300974 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800975 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800976 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100977
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800978 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
979 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300980
981 if (idle) {
982 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800983 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800984 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300985 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000986
987 if (cpu_is_omap34xx())
988 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100989}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300990EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100991
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100992/* polled mcbsp i/o operations */
993int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
994{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300995 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300996
997 if (!omap_mcbsp_check_valid_id(id)) {
998 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
999 return -ENODEV;
1000 }
1001
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001002 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001003
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001004 MCBSP_WRITE(mcbsp, DXR1, buf);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001005 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001006 if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001007 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001008 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001009 /* resend */
1010 return -1;
1011 } else {
1012 /* wait for transmit confirmation */
1013 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001014 while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001015 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001016 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001017 MCBSP_READ_CACHE(mcbsp, SPCR2) &
1018 (~XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001019 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001020 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001021 MCBSP_READ_CACHE(mcbsp, SPCR2) |
1022 (XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001023 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001024 dev_err(mcbsp->dev, "Could not write to"
1025 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001026 return -2;
1027 }
1028 }
1029 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001030
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001031 return 0;
1032}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001033EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001034
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001035int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001036{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001037 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001038
1039 if (!omap_mcbsp_check_valid_id(id)) {
1040 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1041 return -ENODEV;
1042 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001043 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001044
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001045 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001046 if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001047 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001048 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001049 /* resend */
1050 return -1;
1051 } else {
1052 /* wait for recieve confirmation */
1053 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001054 while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001055 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001056 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001057 MCBSP_READ_CACHE(mcbsp, SPCR1) &
1058 (~RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001059 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001060 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001061 MCBSP_READ_CACHE(mcbsp, SPCR1) |
1062 (RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001063 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001064 dev_err(mcbsp->dev, "Could not read from"
1065 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001066 return -2;
1067 }
1068 }
1069 }
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001070 *buf = MCBSP_READ(mcbsp, DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001071
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001072 return 0;
1073}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001074EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001075
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001076/*
1077 * IRQ based word transmission.
1078 */
1079void omap_mcbsp_xmit_word(unsigned int id, u32 word)
1080{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001081 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001082 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001083
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001084 if (!omap_mcbsp_check_valid_id(id)) {
1085 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001086 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001087 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001088
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001089 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001090 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001091
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001092 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001093
1094 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001095 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1096 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001097}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001098EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001099
1100u32 omap_mcbsp_recv_word(unsigned int id)
1101{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001102 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001103 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001104 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001105
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001106 if (!omap_mcbsp_check_valid_id(id)) {
1107 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1108 return -ENODEV;
1109 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001110 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001111
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001112 word_length = mcbsp->rx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001113
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001114 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001115
1116 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001117 word_msb = MCBSP_READ(mcbsp, DRR2);
1118 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001119
1120 return (word_lsb | (word_msb << 16));
1121}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001122EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001123
Tony Lindgren120db2c2006-04-02 17:46:27 +01001124int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
1125{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001126 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001127 omap_mcbsp_word_length tx_word_length;
1128 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001129 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1130
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001131 if (!omap_mcbsp_check_valid_id(id)) {
1132 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1133 return -ENODEV;
1134 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001135 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001136 tx_word_length = mcbsp->tx_word_length;
1137 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001138
Tony Lindgren120db2c2006-04-02 17:46:27 +01001139 if (tx_word_length != rx_word_length)
1140 return -EINVAL;
1141
1142 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001143 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001144 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001145 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001146 if (attempts++ > 1000) {
1147 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001148 MCBSP_WRITE(mcbsp, SPCR2,
1149 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001150 udelay(10);
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);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001154 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1155 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001156 return -EAGAIN;
1157 }
1158 }
1159
1160 /* Now we can push the data */
1161 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001162 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1163 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001164
1165 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001166 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001167 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001168 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001169 if (attempts++ > 1000) {
1170 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001171 MCBSP_WRITE(mcbsp, SPCR1,
1172 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001173 udelay(10);
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);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001177 dev_err(mcbsp->dev, "McBSP%d receiver not "
1178 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001179 return -EAGAIN;
1180 }
1181 }
1182
1183 /* Receiver is ready, let's read the dummy data */
1184 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001185 word_msb = MCBSP_READ(mcbsp, DRR2);
1186 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001187
1188 return 0;
1189}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001190EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001191
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001192int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +01001193{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001194 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +01001195 u32 clock_word = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001196 omap_mcbsp_word_length tx_word_length;
1197 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001198 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1199
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001200 if (!omap_mcbsp_check_valid_id(id)) {
1201 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1202 return -ENODEV;
1203 }
1204
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001205 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001206
1207 tx_word_length = mcbsp->tx_word_length;
1208 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001209
Tony Lindgren120db2c2006-04-02 17:46:27 +01001210 if (tx_word_length != rx_word_length)
1211 return -EINVAL;
1212
1213 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001214 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001215 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001216 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001217 if (attempts++ > 1000) {
1218 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001219 MCBSP_WRITE(mcbsp, SPCR2,
1220 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001221 udelay(10);
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);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001225 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1226 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001227 return -EAGAIN;
1228 }
1229 }
1230
1231 /* We first need to enable the bus clock */
1232 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001233 MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
1234 MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001235
1236 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001237 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001238 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001239 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001240 if (attempts++ > 1000) {
1241 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001242 MCBSP_WRITE(mcbsp, SPCR1,
1243 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001244 udelay(10);
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);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001248 dev_err(mcbsp->dev, "McBSP%d receiver not "
1249 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001250 return -EAGAIN;
1251 }
1252 }
1253
1254 /* Receiver is ready, there is something for us */
1255 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001256 word_msb = MCBSP_READ(mcbsp, DRR2);
1257 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001258
1259 word[0] = (word_lsb | (word_msb << 16));
1260
1261 return 0;
1262}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001263EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001264
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001265/*
1266 * Simple DMA based buffer rx/tx routines.
1267 * Nothing fancy, just a single buffer tx/rx through DMA.
1268 * The DMA resources are released once the transfer is done.
1269 * For anything fancier, you should use your own customized DMA
1270 * routines and callbacks.
1271 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001272int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
1273 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001274{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001275 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001276 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001277 int src_port = 0;
1278 int dest_port = 0;
1279 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001280
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001281 if (!omap_mcbsp_check_valid_id(id)) {
1282 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1283 return -ENODEV;
1284 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001285 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001286
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001287 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001288 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001289 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001290 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001291 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001292 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001293 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001294 return -EAGAIN;
1295 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001296 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001297
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001298 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001299 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001300
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001301 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001302
Tony Lindgren120db2c2006-04-02 17:46:27 +01001303 if (cpu_class_is_omap1()) {
1304 src_port = OMAP_DMA_PORT_TIPB;
1305 dest_port = OMAP_DMA_PORT_EMIFF;
1306 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001307 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001308 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001309
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001310 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001311 OMAP_DMA_DATA_TYPE_S16,
1312 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001313 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001314 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001315
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001316 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001317 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001318 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001319 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001320 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001321
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001322 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001323 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001324 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001325 buffer,
1326 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001327
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001328 omap_start_dma(mcbsp->dma_tx_lch);
1329 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001330
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001331 return 0;
1332}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001333EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001334
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001335int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
1336 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001337{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001338 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001339 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001340 int src_port = 0;
1341 int dest_port = 0;
1342 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001343
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001344 if (!omap_mcbsp_check_valid_id(id)) {
1345 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1346 return -ENODEV;
1347 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001348 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001349
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001350 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001351 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001352 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001353 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001354 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001355 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001356 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001357 return -EAGAIN;
1358 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001359 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001360
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001361 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001362 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001363
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001364 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001365
Tony Lindgren120db2c2006-04-02 17:46:27 +01001366 if (cpu_class_is_omap1()) {
1367 src_port = OMAP_DMA_PORT_TIPB;
1368 dest_port = OMAP_DMA_PORT_EMIFF;
1369 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001370 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001371 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001372
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001373 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001374 OMAP_DMA_DATA_TYPE_S16,
1375 length >> 1, 1,
1376 OMAP_DMA_SYNC_ELEMENT,
1377 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001378
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001379 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001380 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001381 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001382 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001383 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001384
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001385 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001386 dest_port,
1387 OMAP_DMA_AMODE_POST_INC,
1388 buffer,
1389 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001390
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001391 omap_start_dma(mcbsp->dma_rx_lch);
1392 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001393
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001394 return 0;
1395}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001396EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001397
1398/*
1399 * SPI wrapper.
1400 * Since SPI setup is much simpler than the generic McBSP one,
1401 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1402 * Once this is done, you can call omap_mcbsp_start().
1403 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001404void omap_mcbsp_set_spi_mode(unsigned int id,
1405 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001406{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001407 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001408 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1409
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001410 if (!omap_mcbsp_check_valid_id(id)) {
1411 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001412 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001413 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001414 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001415
1416 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1417
1418 /* SPI has only one frame */
1419 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1420 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1421
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001422 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001423 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1424 mcbsp_cfg.spcr1 |= (1 << 12);
1425 else
1426 mcbsp_cfg.spcr1 |= (3 << 11);
1427
1428 /* Set clock parities */
1429 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1430 mcbsp_cfg.pcr0 |= CLKRP;
1431 else
1432 mcbsp_cfg.pcr0 &= ~CLKRP;
1433
1434 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1435 mcbsp_cfg.pcr0 &= ~CLKXP;
1436 else
1437 mcbsp_cfg.pcr0 |= CLKXP;
1438
1439 /* Set SCLKME to 0 and CLKSM to 1 */
1440 mcbsp_cfg.pcr0 &= ~SCLKME;
1441 mcbsp_cfg.srgr2 |= CLKSM;
1442
1443 /* Set FSXP */
1444 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1445 mcbsp_cfg.pcr0 &= ~FSXP;
1446 else
1447 mcbsp_cfg.pcr0 |= FSXP;
1448
1449 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1450 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001451 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001452 mcbsp_cfg.pcr0 |= FSXM;
1453 mcbsp_cfg.srgr2 &= ~FSGM;
1454 mcbsp_cfg.xcr2 |= XDATDLY(1);
1455 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001456 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001457 mcbsp_cfg.pcr0 &= ~CLKXM;
1458 mcbsp_cfg.srgr1 |= CLKGDV(1);
1459 mcbsp_cfg.pcr0 &= ~FSXM;
1460 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1461 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1462 }
1463
1464 mcbsp_cfg.xcr2 &= ~XPHASE;
1465 mcbsp_cfg.rcr2 &= ~RPHASE;
1466
1467 omap_mcbsp_config(id, &mcbsp_cfg);
1468}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001469EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001470
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001471#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001472#define max_thres(m) (mcbsp->pdata->buffer_size)
1473#define valid_threshold(m, val) ((val) <= max_thres(m))
1474#define THRESHOLD_PROP_BUILDER(prop) \
1475static ssize_t prop##_show(struct device *dev, \
1476 struct device_attribute *attr, char *buf) \
1477{ \
1478 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1479 \
1480 return sprintf(buf, "%u\n", mcbsp->prop); \
1481} \
1482 \
1483static ssize_t prop##_store(struct device *dev, \
1484 struct device_attribute *attr, \
1485 const char *buf, size_t size) \
1486{ \
1487 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1488 unsigned long val; \
1489 int status; \
1490 \
1491 status = strict_strtoul(buf, 0, &val); \
1492 if (status) \
1493 return status; \
1494 \
1495 if (!valid_threshold(mcbsp, val)) \
1496 return -EDOM; \
1497 \
1498 mcbsp->prop = val; \
1499 return size; \
1500} \
1501 \
1502static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1503
1504THRESHOLD_PROP_BUILDER(max_tx_thres);
1505THRESHOLD_PROP_BUILDER(max_rx_thres);
1506
Jarkko Nikula9b300502009-08-24 17:45:50 +03001507static const char *dma_op_modes[] = {
1508 "element", "threshold", "frame",
1509};
1510
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001511static ssize_t dma_op_mode_show(struct device *dev,
1512 struct device_attribute *attr, char *buf)
1513{
1514 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001515 int dma_op_mode, i = 0;
1516 ssize_t len = 0;
1517 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001518
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001519 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001520
Jarkko Nikula9b300502009-08-24 17:45:50 +03001521 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1522 if (dma_op_mode == i)
1523 len += sprintf(buf + len, "[%s] ", *s);
1524 else
1525 len += sprintf(buf + len, "%s ", *s);
1526 }
1527 len += sprintf(buf + len, "\n");
1528
1529 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001530}
1531
1532static ssize_t dma_op_mode_store(struct device *dev,
1533 struct device_attribute *attr,
1534 const char *buf, size_t size)
1535{
1536 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001537 const char * const *s;
1538 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001539
Jarkko Nikula9b300502009-08-24 17:45:50 +03001540 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1541 if (sysfs_streq(buf, *s))
1542 break;
1543
1544 if (i == ARRAY_SIZE(dma_op_modes))
1545 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001546
1547 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001548 if (!mcbsp->free) {
1549 size = -EBUSY;
1550 goto unlock;
1551 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001552 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001553
1554unlock:
1555 spin_unlock_irq(&mcbsp->lock);
1556
1557 return size;
1558}
1559
1560static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1561
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001562static ssize_t st_taps_show(struct device *dev,
1563 struct device_attribute *attr, char *buf)
1564{
1565 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1566 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1567 ssize_t status = 0;
1568 int i;
1569
1570 spin_lock_irq(&mcbsp->lock);
1571 for (i = 0; i < st_data->nr_taps; i++)
1572 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1573 st_data->taps[i]);
1574 if (i)
1575 status += sprintf(&buf[status], "\n");
1576 spin_unlock_irq(&mcbsp->lock);
1577
1578 return status;
1579}
1580
1581static ssize_t st_taps_store(struct device *dev,
1582 struct device_attribute *attr,
1583 const char *buf, size_t size)
1584{
1585 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1586 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1587 int val, tmp, status, i = 0;
1588
1589 spin_lock_irq(&mcbsp->lock);
1590 memset(st_data->taps, 0, sizeof(st_data->taps));
1591 st_data->nr_taps = 0;
1592
1593 do {
1594 status = sscanf(buf, "%d%n", &val, &tmp);
1595 if (status < 0 || status == 0) {
1596 size = -EINVAL;
1597 goto out;
1598 }
1599 if (val < -32768 || val > 32767) {
1600 size = -EINVAL;
1601 goto out;
1602 }
1603 st_data->taps[i++] = val;
1604 buf += tmp;
1605 if (*buf != ',')
1606 break;
1607 buf++;
1608 } while (1);
1609
1610 st_data->nr_taps = i;
1611
1612out:
1613 spin_unlock_irq(&mcbsp->lock);
1614
1615 return size;
1616}
1617
1618static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1619
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001620static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001621 &dev_attr_max_tx_thres.attr,
1622 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001623 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001624 NULL,
1625};
1626
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001627static const struct attribute_group additional_attr_group = {
1628 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001629};
1630
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001631static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001632{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001633 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001634}
1635
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001636static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001637{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001638 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001639}
1640
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001641static const struct attribute *sidetone_attrs[] = {
1642 &dev_attr_st_taps.attr,
1643 NULL,
1644};
1645
1646static const struct attribute_group sidetone_attr_group = {
1647 .attrs = (struct attribute **)sidetone_attrs,
1648};
1649
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -07001650static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001651{
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001652 struct platform_device *pdev;
1653 struct resource *res;
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001654 struct omap_mcbsp_st_data *st_data;
1655 int err;
1656
1657 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1658 if (!st_data) {
1659 err = -ENOMEM;
1660 goto err1;
1661 }
1662
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001663 pdev = container_of(mcbsp->dev, struct platform_device, dev);
1664
1665 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1666 st_data->io_base_st = ioremap(res->start, resource_size(res));
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001667 if (!st_data->io_base_st) {
1668 err = -ENOMEM;
1669 goto err2;
1670 }
1671
1672 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1673 if (err)
1674 goto err3;
1675
1676 mcbsp->st_data = st_data;
1677 return 0;
1678
1679err3:
1680 iounmap(st_data->io_base_st);
1681err2:
1682 kfree(st_data);
1683err1:
1684 return err;
1685
1686}
1687
1688static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1689{
1690 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1691
1692 if (st_data) {
1693 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1694 iounmap(st_data->io_base_st);
1695 kfree(st_data);
1696 }
1697}
1698
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001699static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1700{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001701 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001702 if (cpu_is_omap34xx()) {
Peter Ujfalusi451fd822010-06-03 07:39:33 +03001703 /*
1704 * Initially configure the maximum thresholds to a safe value.
1705 * The McBSP FIFO usage with these values should not go under
1706 * 16 locations.
1707 * If the whole FIFO without safety buffer is used, than there
1708 * is a possibility that the DMA will be not able to push the
1709 * new data on time, causing channel shifts in runtime.
1710 */
1711 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1712 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001713 /*
1714 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1715 * for mcbsp2 instances.
1716 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001717 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001718 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001719 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001720
1721 if (mcbsp->id == 2 || mcbsp->id == 3)
1722 if (omap_st_add(mcbsp))
1723 dev_warn(mcbsp->dev,
1724 "Unable to create sidetone controls\n");
1725
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001726 } else {
1727 mcbsp->max_tx_thres = -EINVAL;
1728 mcbsp->max_rx_thres = -EINVAL;
1729 }
1730}
1731
1732static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1733{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001734 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001735 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001736
1737 if (mcbsp->id == 2 || mcbsp->id == 3)
1738 omap_st_remove(mcbsp);
1739 }
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001740}
1741#else
1742static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1743static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001744#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001745
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001746/*
1747 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1748 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1749 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001750static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001751{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001752 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001753 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001754 int id = pdev->id - 1;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001755 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001756 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001757
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001758 if (!pdata) {
1759 dev_err(&pdev->dev, "McBSP device initialized without"
1760 "platform data\n");
1761 ret = -EINVAL;
1762 goto exit;
1763 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001764
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001765 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001766
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001767 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001768 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1769 ret = -EINVAL;
1770 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001771 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001772
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001773 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1774 if (!mcbsp) {
1775 ret = -ENOMEM;
1776 goto exit;
1777 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001778
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001779 spin_lock_init(&mcbsp->lock);
1780 mcbsp->id = id + 1;
Shubhrajyoti D6722a722010-12-07 16:25:41 -08001781 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001782 mcbsp->dma_tx_lch = -1;
1783 mcbsp->dma_rx_lch = -1;
1784
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001785 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1786 if (!res) {
1787 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1788 if (!res) {
1789 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1790 "resource\n", __func__, pdev->id);
1791 ret = -ENOMEM;
1792 goto exit;
1793 }
1794 }
1795 mcbsp->phys_base = res->start;
1796 omap_mcbsp_cache_size = resource_size(res);
1797 mcbsp->io_base = ioremap(res->start, resource_size(res));
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001798 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001799 ret = -ENOMEM;
1800 goto err_ioremap;
1801 }
1802
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001803 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1804 if (!res)
1805 mcbsp->phys_dma_base = mcbsp->phys_base;
1806 else
1807 mcbsp->phys_dma_base = res->start;
1808
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001809 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001810 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001811
1812 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1813 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1814
1815 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1816 if (!res) {
1817 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1818 __func__, pdev->id);
1819 ret = -ENODEV;
1820 goto err_res;
1821 }
1822 mcbsp->dma_rx_sync = res->start;
1823
1824 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1825 if (!res) {
1826 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1827 __func__, pdev->id);
1828 ret = -ENODEV;
1829 goto err_res;
1830 }
1831 mcbsp->dma_tx_sync = res->start;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001832
Russell Kingb820ce42009-01-23 10:26:46 +00001833 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1834 if (IS_ERR(mcbsp->iclk)) {
1835 ret = PTR_ERR(mcbsp->iclk);
1836 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001837 goto err_res;
Russell Kingb820ce42009-01-23 10:26:46 +00001838 }
Stanley.Miao06151152009-01-29 08:57:12 -08001839
Russell Kingb820ce42009-01-23 10:26:46 +00001840 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1841 if (IS_ERR(mcbsp->fclk)) {
1842 ret = PTR_ERR(mcbsp->fclk);
1843 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1844 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001845 }
1846
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001847 mcbsp->pdata = pdata;
1848 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001849 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001850 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001851
1852 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1853 omap34xx_device_init(mcbsp);
1854
Russell Kingd592dd12008-09-04 14:25:42 +01001855 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001856
Russell Kingb820ce42009-01-23 10:26:46 +00001857err_fclk:
1858 clk_put(mcbsp->iclk);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001859err_res:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001860 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001861err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001862 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001863exit:
1864 return ret;
1865}
1866
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001867static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001868{
1869 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1870
1871 platform_set_drvdata(pdev, NULL);
1872 if (mcbsp) {
1873
1874 if (mcbsp->pdata && mcbsp->pdata->ops &&
1875 mcbsp->pdata->ops->free)
1876 mcbsp->pdata->ops->free(mcbsp->id);
1877
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001878 omap34xx_device_exit(mcbsp);
1879
Russell Kingb820ce42009-01-23 10:26:46 +00001880 clk_put(mcbsp->fclk);
1881 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001882
Russell Kingd592dd12008-09-04 14:25:42 +01001883 iounmap(mcbsp->io_base);
Jarkko Nikula5f3b7282010-12-07 16:25:40 -08001884 kfree(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001885 }
1886
1887 return 0;
1888}
1889
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001890static struct platform_driver omap_mcbsp_driver = {
1891 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001892 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001893 .driver = {
1894 .name = "omap-mcbsp",
1895 },
1896};
1897
1898int __init omap_mcbsp_init(void)
1899{
1900 /* Register the McBSP driver */
1901 return platform_driver_register(&omap_mcbsp_driver);
1902}