blob: 6d230164b4f99a301aef6c907af29cc65fc61bb7 [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>
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +053030#include <plat/omap_device.h>
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +053031#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010032
Paul Walmsley59fb6592010-12-21 15:30:55 -070033/* XXX These "sideways" includes are a sign that something is wrong */
34#include "../mach-omap2/cm2xxx_3xxx.h"
Eero Nurkkalad912fa92010-02-22 12:21:11 +000035#include "../mach-omap2/cm-regbits-34xx.h"
36
Chandra Shekharb4b58f52008-10-08 10:01:39 +030037struct omap_mcbsp **mcbsp_ptr;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080038int omap_mcbsp_count, omap_mcbsp_cache_size;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030039
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070040static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030041{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080042 if (cpu_class_is_omap1()) {
43 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080044 __raw_writew((u16)val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080045 } else if (cpu_is_omap2420()) {
46 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
47 __raw_writew((u16)val, mcbsp->io_base + reg);
48 } else {
49 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080050 __raw_writel(val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080051 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030052}
53
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070054static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030055{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080056 if (cpu_class_is_omap1()) {
57 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
58 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
59 } else if (cpu_is_omap2420()) {
60 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
61 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
62 } else {
63 return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
64 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
65 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030066}
67
Eero Nurkkalad912fa92010-02-22 12:21:11 +000068#ifdef CONFIG_ARCH_OMAP3
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070069static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000070{
71 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
72}
73
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070074static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000075{
76 return __raw_readl(mcbsp->st_data->io_base_st + reg);
77}
78#endif
79
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080080#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080081 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080082#define MCBSP_WRITE(mcbsp, reg, val) \
83 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080084#define MCBSP_READ_CACHE(mcbsp, reg) \
85 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030086
Eero Nurkkalad912fa92010-02-22 12:21:11 +000087#define MCBSP_ST_READ(mcbsp, reg) \
88 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
89#define MCBSP_ST_WRITE(mcbsp, reg, val) \
90 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
91
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092static void omap_mcbsp_dump_reg(u8 id)
93{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030094 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
95
96 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
97 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080098 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030099 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800100 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300101 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800102 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300103 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800104 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300105 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800106 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300107 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800108 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300109 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800110 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300111 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800112 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300113 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800114 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300115 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800116 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300117 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800118 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300119 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800120 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300121 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800122 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300123 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124}
125
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700126static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400128 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700129 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800131 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700132 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700134 if (irqst_spcr2 & XSYNC_ERR) {
135 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
136 irqst_spcr2);
137 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000138 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700139 } else {
140 complete(&mcbsp_tx->tx_irq_completion);
141 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300142
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143 return IRQ_HANDLED;
144}
145
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700146static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100147{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400148 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700149 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800151 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700152 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700154 if (irqst_spcr1 & RSYNC_ERR) {
155 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
156 irqst_spcr1);
157 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000158 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700159 } else {
Scott Elliscb922d22010-09-23 18:47:23 -0700160 complete(&mcbsp_rx->rx_irq_completion);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700161 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300162
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163 return IRQ_HANDLED;
164}
165
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100166static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
167{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400168 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100169
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300170 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800171 MCBSP_READ(mcbsp_dma_tx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100172
173 /* We can free the channels */
174 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
175 mcbsp_dma_tx->dma_tx_lch = -1;
176
177 complete(&mcbsp_dma_tx->tx_dma_completion);
178}
179
180static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
181{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400182 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100183
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300184 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800185 MCBSP_READ(mcbsp_dma_rx, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100186
187 /* We can free the channels */
188 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
189 mcbsp_dma_rx->dma_rx_lch = -1;
190
191 complete(&mcbsp_dma_rx->rx_dma_completion);
192}
193
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100194/*
195 * omap_mcbsp_config simply write a config to the
196 * appropriate McBSP.
197 * You either call this function or set the McBSP registers
198 * by yourself before calling omap_mcbsp_start().
199 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300200void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100201{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300202 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100203
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300204 if (!omap_mcbsp_check_valid_id(id)) {
205 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
206 return;
207 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300208 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300209
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300210 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
211 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100212
213 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800214 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
215 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
216 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
217 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
218 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
219 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
220 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
221 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
222 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
223 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
224 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Syed Rafiuddina5b92cc2009-07-28 18:57:10 +0530225 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800226 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
227 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200228 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100229}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300230EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800232#ifdef CONFIG_ARCH_OMAP3
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530233static struct omap_device *find_omap_device_by_dev(struct device *dev)
234{
235 struct platform_device *pdev = container_of(dev,
236 struct platform_device, dev);
237 return container_of(pdev, struct omap_device, pdev);
238}
239
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000240static void omap_st_on(struct omap_mcbsp *mcbsp)
241{
242 unsigned int w;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530243 struct omap_device *od;
244
245 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000246
247 /*
248 * Sidetone uses McBSP ICLK - which must not idle when sidetones
249 * are enabled or sidetones start sounding ugly.
250 */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700251 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000252 w &= ~(1 << (mcbsp->id - 2));
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700253 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000254
255 /* Enable McBSP Sidetone */
256 w = MCBSP_READ(mcbsp, SSELCR);
257 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
258
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000259 /* Enable Sidetone from Sidetone Core */
260 w = MCBSP_ST_READ(mcbsp, SSELCR);
261 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
262}
263
264static void omap_st_off(struct omap_mcbsp *mcbsp)
265{
266 unsigned int w;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530267 struct omap_device *od;
268
269 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000270
271 w = MCBSP_ST_READ(mcbsp, SSELCR);
272 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
273
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000274 w = MCBSP_READ(mcbsp, SSELCR);
275 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
276
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700277 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000278 w |= 1 << (mcbsp->id - 2);
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700279 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000280}
281
282static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
283{
284 u16 val, i;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530285 struct omap_device *od;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000286
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530287 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000288
289 val = MCBSP_ST_READ(mcbsp, SSELCR);
290
291 if (val & ST_COEFFWREN)
292 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
293
294 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
295
296 for (i = 0; i < 128; i++)
297 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
298
299 i = 0;
300
301 val = MCBSP_ST_READ(mcbsp, SSELCR);
302 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
303 val = MCBSP_ST_READ(mcbsp, SSELCR);
304
305 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
306
307 if (i == 1000)
308 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
309}
310
311static void omap_st_chgain(struct omap_mcbsp *mcbsp)
312{
313 u16 w;
314 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530315 struct omap_device *od;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000316
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530317 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000318
319 w = MCBSP_ST_READ(mcbsp, SSELCR);
320
321 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
322 ST_CH1GAIN(st_data->ch1gain));
323}
324
325int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
326{
327 struct omap_mcbsp *mcbsp;
328 struct omap_mcbsp_st_data *st_data;
329 int ret = 0;
330
331 if (!omap_mcbsp_check_valid_id(id)) {
332 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
333 return -ENODEV;
334 }
335
336 mcbsp = id_to_mcbsp_ptr(id);
337 st_data = mcbsp->st_data;
338
339 if (!st_data)
340 return -ENOENT;
341
342 spin_lock_irq(&mcbsp->lock);
343 if (channel == 0)
344 st_data->ch0gain = chgain;
345 else if (channel == 1)
346 st_data->ch1gain = chgain;
347 else
348 ret = -EINVAL;
349
350 if (st_data->enabled)
351 omap_st_chgain(mcbsp);
352 spin_unlock_irq(&mcbsp->lock);
353
354 return ret;
355}
356EXPORT_SYMBOL(omap_st_set_chgain);
357
358int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
359{
360 struct omap_mcbsp *mcbsp;
361 struct omap_mcbsp_st_data *st_data;
362 int ret = 0;
363
364 if (!omap_mcbsp_check_valid_id(id)) {
365 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
366 return -ENODEV;
367 }
368
369 mcbsp = id_to_mcbsp_ptr(id);
370 st_data = mcbsp->st_data;
371
372 if (!st_data)
373 return -ENOENT;
374
375 spin_lock_irq(&mcbsp->lock);
376 if (channel == 0)
377 *chgain = st_data->ch0gain;
378 else if (channel == 1)
379 *chgain = st_data->ch1gain;
380 else
381 ret = -EINVAL;
382 spin_unlock_irq(&mcbsp->lock);
383
384 return ret;
385}
386EXPORT_SYMBOL(omap_st_get_chgain);
387
388static int omap_st_start(struct omap_mcbsp *mcbsp)
389{
390 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
391
392 if (st_data && st_data->enabled && !st_data->running) {
393 omap_st_fir_write(mcbsp, st_data->taps);
394 omap_st_chgain(mcbsp);
395
396 if (!mcbsp->free) {
397 omap_st_on(mcbsp);
398 st_data->running = 1;
399 }
400 }
401
402 return 0;
403}
404
405int omap_st_enable(unsigned int id)
406{
407 struct omap_mcbsp *mcbsp;
408 struct omap_mcbsp_st_data *st_data;
409
410 if (!omap_mcbsp_check_valid_id(id)) {
411 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
412 return -ENODEV;
413 }
414
415 mcbsp = id_to_mcbsp_ptr(id);
416 st_data = mcbsp->st_data;
417
418 if (!st_data)
419 return -ENODEV;
420
421 spin_lock_irq(&mcbsp->lock);
422 st_data->enabled = 1;
423 omap_st_start(mcbsp);
424 spin_unlock_irq(&mcbsp->lock);
425
426 return 0;
427}
428EXPORT_SYMBOL(omap_st_enable);
429
430static int omap_st_stop(struct omap_mcbsp *mcbsp)
431{
432 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
433
434 if (st_data && st_data->running) {
435 if (!mcbsp->free) {
436 omap_st_off(mcbsp);
437 st_data->running = 0;
438 }
439 }
440
441 return 0;
442}
443
444int omap_st_disable(unsigned int id)
445{
446 struct omap_mcbsp *mcbsp;
447 struct omap_mcbsp_st_data *st_data;
448 int ret = 0;
449
450 if (!omap_mcbsp_check_valid_id(id)) {
451 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
452 return -ENODEV;
453 }
454
455 mcbsp = id_to_mcbsp_ptr(id);
456 st_data = mcbsp->st_data;
457
458 if (!st_data)
459 return -ENODEV;
460
461 spin_lock_irq(&mcbsp->lock);
462 omap_st_stop(mcbsp);
463 st_data->enabled = 0;
464 spin_unlock_irq(&mcbsp->lock);
465
466 return ret;
467}
468EXPORT_SYMBOL(omap_st_disable);
469
470int omap_st_is_enabled(unsigned int id)
471{
472 struct omap_mcbsp *mcbsp;
473 struct omap_mcbsp_st_data *st_data;
474
475 if (!omap_mcbsp_check_valid_id(id)) {
476 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
477 return -ENODEV;
478 }
479
480 mcbsp = id_to_mcbsp_ptr(id);
481 st_data = mcbsp->st_data;
482
483 if (!st_data)
484 return -ENODEV;
485
486
487 return st_data->enabled;
488}
489EXPORT_SYMBOL(omap_st_is_enabled);
490
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300491/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300492 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
493 * The threshold parameter is 1 based, and it is converted (threshold - 1)
494 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300495 */
496void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
497{
498 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300499
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500500 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300501 return;
502
503 if (!omap_mcbsp_check_valid_id(id)) {
504 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
505 return;
506 }
507 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300508
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300509 if (threshold && threshold <= mcbsp->max_tx_thres)
510 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300511}
512EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
513
514/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300515 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
516 * The threshold parameter is 1 based, and it is converted (threshold - 1)
517 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300518 */
519void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
520{
521 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300522
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500523 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300524 return;
525
526 if (!omap_mcbsp_check_valid_id(id)) {
527 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
528 return;
529 }
530 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300531
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300532 if (threshold && threshold <= mcbsp->max_rx_thres)
533 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300534}
535EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +0300536
537/*
538 * omap_mcbsp_get_max_tx_thres just return the current configured
539 * maximum threshold for transmission
540 */
541u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
542{
543 struct omap_mcbsp *mcbsp;
544
545 if (!omap_mcbsp_check_valid_id(id)) {
546 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
547 return -ENODEV;
548 }
549 mcbsp = id_to_mcbsp_ptr(id);
550
551 return mcbsp->max_tx_thres;
552}
553EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
554
555/*
556 * omap_mcbsp_get_max_rx_thres just return the current configured
557 * maximum threshold for reception
558 */
559u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
560{
561 struct omap_mcbsp *mcbsp;
562
563 if (!omap_mcbsp_check_valid_id(id)) {
564 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
565 return -ENODEV;
566 }
567 mcbsp = id_to_mcbsp_ptr(id);
568
569 return mcbsp->max_rx_thres;
570}
571EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300572
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300573u16 omap_mcbsp_get_fifo_size(unsigned int id)
574{
575 struct omap_mcbsp *mcbsp;
576
577 if (!omap_mcbsp_check_valid_id(id)) {
578 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
579 return -ENODEV;
580 }
581 mcbsp = id_to_mcbsp_ptr(id);
582
583 return mcbsp->pdata->buffer_size;
584}
585EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
586
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200587/*
588 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
589 */
590u16 omap_mcbsp_get_tx_delay(unsigned int id)
591{
592 struct omap_mcbsp *mcbsp;
593 u16 buffstat;
594
595 if (!omap_mcbsp_check_valid_id(id)) {
596 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
597 return -ENODEV;
598 }
599 mcbsp = id_to_mcbsp_ptr(id);
600
601 /* Returns the number of free locations in the buffer */
602 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
603
604 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300605 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200606}
607EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
608
609/*
610 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
611 * to reach the threshold value (when the DMA will be triggered to read it)
612 */
613u16 omap_mcbsp_get_rx_delay(unsigned int id)
614{
615 struct omap_mcbsp *mcbsp;
616 u16 buffstat, threshold;
617
618 if (!omap_mcbsp_check_valid_id(id)) {
619 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
620 return -ENODEV;
621 }
622 mcbsp = id_to_mcbsp_ptr(id);
623
624 /* Returns the number of used locations in the buffer */
625 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
626 /* RX threshold */
627 threshold = MCBSP_READ(mcbsp, THRSH1);
628
629 /* Return the number of location till we reach the threshold limit */
630 if (threshold <= buffstat)
631 return 0;
632 else
633 return threshold - buffstat;
634}
635EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
636
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300637/*
638 * omap_mcbsp_get_dma_op_mode just return the current configured
639 * operating mode for the mcbsp channel
640 */
641int omap_mcbsp_get_dma_op_mode(unsigned int id)
642{
643 struct omap_mcbsp *mcbsp;
644 int dma_op_mode;
645
646 if (!omap_mcbsp_check_valid_id(id)) {
647 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
648 return -ENODEV;
649 }
650 mcbsp = id_to_mcbsp_ptr(id);
651
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300652 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300653
654 return dma_op_mode;
655}
656EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300657
658static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
659{
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530660 struct omap_device *od;
661
662 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300663 /*
664 * Enable wakup behavior, smart idle and all wakeups
665 * REVISIT: some wakeups may be unnecessary
666 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500667 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530668 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300669 }
670}
671
672static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
673{
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530674 struct omap_device *od;
675
676 od = find_omap_device_by_dev(mcbsp->dev);
677
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300678 /*
679 * Disable wakup behavior, smart idle and all wakeups
680 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500681 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300682 /*
683 * HW bug workaround - If no_idle mode is taken, we need to
684 * go to smart_idle before going to always_idle, or the
685 * device will not hit retention anymore.
686 */
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300687
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800688 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300689 }
690}
691#else
692static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
693static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000694static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
695static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300696#endif
697
Tony Lindgren120db2c2006-04-02 17:46:27 +0100698/*
699 * We can choose between IRQ based or polled IO.
700 * This needs to be called before omap_mcbsp_request().
701 */
702int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
703{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300704 struct omap_mcbsp *mcbsp;
705
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300706 if (!omap_mcbsp_check_valid_id(id)) {
707 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
708 return -ENODEV;
709 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300710 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100711
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300712 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100713
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300714 if (!mcbsp->free) {
715 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
716 mcbsp->id);
717 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100718 return -EINVAL;
719 }
720
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300721 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100722
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300723 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100724
725 return 0;
726}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300727EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100729int omap_mcbsp_request(unsigned int id)
730{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300731 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800732 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733 int err;
734
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300735 if (!omap_mcbsp_check_valid_id(id)) {
736 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
737 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100738 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300739 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300740
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800741 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
742 if (!reg_cache) {
743 return -ENOMEM;
744 }
745
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300746 spin_lock(&mcbsp->lock);
747 if (!mcbsp->free) {
748 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
749 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800750 err = -EBUSY;
751 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752 }
753
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800754 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800755 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300756 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100757
Russell Kingb820ce42009-01-23 10:26:46 +0000758 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
759 mcbsp->pdata->ops->request(id);
760
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530761 pm_runtime_get_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000762
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300763 /* Do procedure specific to omap34xx arch, if applicable */
764 omap34xx_mcbsp_request(mcbsp);
765
Jarkko Nikula5a070552008-10-08 10:01:41 +0300766 /*
767 * Make sure that transmitter, receiver and sample-rate generator are
768 * not running before activating IRQs.
769 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800770 MCBSP_WRITE(mcbsp, SPCR1, 0);
771 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300772
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300773 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100774 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300775 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300776 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
777 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100778 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300779 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
780 "for McBSP%d\n", mcbsp->tx_irq,
781 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800782 goto err_clk_disable;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100783 }
784
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500785 if (mcbsp->rx_irq) {
786 init_completion(&mcbsp->rx_irq_completion);
787 err = request_irq(mcbsp->rx_irq,
788 omap_mcbsp_rx_irq_handler,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300789 0, "McBSP", (void *)mcbsp);
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500790 if (err != 0) {
791 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
792 "for McBSP%d\n", mcbsp->rx_irq,
793 mcbsp->id);
794 goto err_free_irq;
795 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100796 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100797 }
798
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800800err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800801 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800802err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800803 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800804 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800805
806 /* Do procedure specific to omap34xx arch, if applicable */
807 omap34xx_mcbsp_free(mcbsp);
808
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530809 pm_runtime_put_sync(mcbsp->dev);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800810
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800811 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800812 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800813 mcbsp->reg_cache = NULL;
814err_kfree:
815 spin_unlock(&mcbsp->lock);
816 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800817
818 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100819}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300820EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100821
822void omap_mcbsp_free(unsigned int id)
823{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300824 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800825 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300826
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300827 if (!omap_mcbsp_check_valid_id(id)) {
828 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100829 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100830 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300831 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100832
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300833 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
834 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300835
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300836 /* Do procedure specific to omap34xx arch, if applicable */
837 omap34xx_mcbsp_free(mcbsp);
838
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530839 pm_runtime_put_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000840
841 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
842 /* Free IRQs */
Jorge Eduardo Candelaria9319b9d2010-05-12 12:18:39 -0500843 if (mcbsp->rx_irq)
844 free_irq(mcbsp->rx_irq, (void *)mcbsp);
Russell Kingb820ce42009-01-23 10:26:46 +0000845 free_irq(mcbsp->tx_irq, (void *)mcbsp);
846 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100847
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800848 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100849
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800850 spin_lock(&mcbsp->lock);
851 if (mcbsp->free)
852 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
853 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800854 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800855 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300856 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800857
858 if (reg_cache)
859 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100860}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300861EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100862
863/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300864 * Here we start the McBSP, by enabling transmitter, receiver or both.
865 * If no transmitter or receiver is active prior calling, then sample-rate
866 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100867 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300868void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300870 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000871 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100872 u16 w;
873
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300874 if (!omap_mcbsp_check_valid_id(id)) {
875 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300877 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300878 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100879
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000880 if (cpu_is_omap34xx())
881 omap_st_start(mcbsp);
882
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800883 mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
884 mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100885
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000886 /* Only enable SRG, if McBSP is master */
887 w = MCBSP_READ_CACHE(mcbsp, PCR0);
888 if (w & (FSXM | FSRM | CLKXM | CLKRM))
889 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
890 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300891
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000892 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300893 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800894 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800895 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300896 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100897
898 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300899 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800900 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800901 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100902
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300903 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800904 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800905 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100906
Eduardo Valentin44a63112009-08-20 16:18:09 +0300907 /*
908 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
909 * REVISIT: 100us may give enough time for two CLKSRG, however
910 * due to some unknown PM related, clock gating etc. reason it
911 * is now at 500us.
912 */
913 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100914
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000915 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300916 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800917 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800918 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300919 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100920
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500921 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300922 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800923 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300924 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800925 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800926 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300927 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800928 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300929 }
930
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100931 /* Dump McBSP Regs */
932 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300934EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100935
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300936void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100937{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300938 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300939 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100940 u16 w;
941
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300942 if (!omap_mcbsp_check_valid_id(id)) {
943 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300945 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100946
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300947 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100948
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300949 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300950 tx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500951 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800952 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300953 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800954 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300955 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800956 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800957 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100958
959 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300960 rx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500961 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800962 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700963 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800964 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300965 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800966 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800967 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100968
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800969 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
970 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300971
972 if (idle) {
973 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800974 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800975 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300976 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000977
978 if (cpu_is_omap34xx())
979 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100980}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300981EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100982
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100983/* polled mcbsp i/o operations */
984int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
985{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300986 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300987
988 if (!omap_mcbsp_check_valid_id(id)) {
989 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
990 return -ENODEV;
991 }
992
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300993 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300994
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800995 MCBSP_WRITE(mcbsp, DXR1, buf);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100996 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800997 if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100998 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000999 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001000 /* resend */
1001 return -1;
1002 } else {
1003 /* wait for transmit confirmation */
1004 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001005 while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001006 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001007 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001008 MCBSP_READ_CACHE(mcbsp, SPCR2) &
1009 (~XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001010 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001011 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001012 MCBSP_READ_CACHE(mcbsp, SPCR2) |
1013 (XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001014 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001015 dev_err(mcbsp->dev, "Could not write to"
1016 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001017 return -2;
1018 }
1019 }
1020 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001021
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001022 return 0;
1023}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001024EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001025
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001026int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001027{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001028 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001029
1030 if (!omap_mcbsp_check_valid_id(id)) {
1031 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1032 return -ENODEV;
1033 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001034 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001035
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001036 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001037 if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001038 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +00001039 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001040 /* resend */
1041 return -1;
1042 } else {
1043 /* wait for recieve confirmation */
1044 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001045 while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001046 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001047 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001048 MCBSP_READ_CACHE(mcbsp, SPCR1) &
1049 (~RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001050 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001051 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001052 MCBSP_READ_CACHE(mcbsp, SPCR1) |
1053 (RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001054 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001055 dev_err(mcbsp->dev, "Could not read from"
1056 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001057 return -2;
1058 }
1059 }
1060 }
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001061 *buf = MCBSP_READ(mcbsp, DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001062
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001063 return 0;
1064}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001065EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001066
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001067/*
1068 * IRQ based word transmission.
1069 */
1070void omap_mcbsp_xmit_word(unsigned int id, u32 word)
1071{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001072 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001073 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001074
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001075 if (!omap_mcbsp_check_valid_id(id)) {
1076 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001077 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001078 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001079
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001080 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001081 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001082
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001083 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001084
1085 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001086 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1087 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001088}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001089EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001090
1091u32 omap_mcbsp_recv_word(unsigned int id)
1092{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001093 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001094 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001095 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001097 if (!omap_mcbsp_check_valid_id(id)) {
1098 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1099 return -ENODEV;
1100 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001101 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001102
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001103 word_length = mcbsp->rx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001104
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001105 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001106
1107 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001108 word_msb = MCBSP_READ(mcbsp, DRR2);
1109 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001110
1111 return (word_lsb | (word_msb << 16));
1112}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001113EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001114
Tony Lindgren120db2c2006-04-02 17:46:27 +01001115int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
1116{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001117 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001118 omap_mcbsp_word_length tx_word_length;
1119 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001120 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1121
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001122 if (!omap_mcbsp_check_valid_id(id)) {
1123 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1124 return -ENODEV;
1125 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001126 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001127 tx_word_length = mcbsp->tx_word_length;
1128 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001129
Tony Lindgren120db2c2006-04-02 17:46:27 +01001130 if (tx_word_length != rx_word_length)
1131 return -EINVAL;
1132
1133 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001134 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001135 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001136 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001137 if (attempts++ > 1000) {
1138 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001139 MCBSP_WRITE(mcbsp, SPCR2,
1140 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001141 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001142 MCBSP_WRITE(mcbsp, SPCR2,
1143 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001144 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001145 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1146 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001147 return -EAGAIN;
1148 }
1149 }
1150
1151 /* Now we can push the data */
1152 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001153 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1154 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001155
1156 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001157 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001158 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001159 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001160 if (attempts++ > 1000) {
1161 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001162 MCBSP_WRITE(mcbsp, SPCR1,
1163 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001164 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001165 MCBSP_WRITE(mcbsp, SPCR1,
1166 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001167 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001168 dev_err(mcbsp->dev, "McBSP%d receiver not "
1169 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001170 return -EAGAIN;
1171 }
1172 }
1173
1174 /* Receiver is ready, let's read the dummy data */
1175 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001176 word_msb = MCBSP_READ(mcbsp, DRR2);
1177 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001178
1179 return 0;
1180}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001181EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001182
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001183int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +01001184{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001185 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +01001186 u32 clock_word = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001187 omap_mcbsp_word_length tx_word_length;
1188 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001189 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1190
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001191 if (!omap_mcbsp_check_valid_id(id)) {
1192 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1193 return -ENODEV;
1194 }
1195
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001196 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001197
1198 tx_word_length = mcbsp->tx_word_length;
1199 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001200
Tony Lindgren120db2c2006-04-02 17:46:27 +01001201 if (tx_word_length != rx_word_length)
1202 return -EINVAL;
1203
1204 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001205 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001206 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001207 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001208 if (attempts++ > 1000) {
1209 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001210 MCBSP_WRITE(mcbsp, SPCR2,
1211 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001212 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001213 MCBSP_WRITE(mcbsp, SPCR2,
1214 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001215 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001216 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1217 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001218 return -EAGAIN;
1219 }
1220 }
1221
1222 /* We first need to enable the bus clock */
1223 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001224 MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
1225 MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001226
1227 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001228 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001229 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001230 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001231 if (attempts++ > 1000) {
1232 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001233 MCBSP_WRITE(mcbsp, SPCR1,
1234 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001235 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001236 MCBSP_WRITE(mcbsp, SPCR1,
1237 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001238 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001239 dev_err(mcbsp->dev, "McBSP%d receiver not "
1240 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001241 return -EAGAIN;
1242 }
1243 }
1244
1245 /* Receiver is ready, there is something for us */
1246 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001247 word_msb = MCBSP_READ(mcbsp, DRR2);
1248 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001249
1250 word[0] = (word_lsb | (word_msb << 16));
1251
1252 return 0;
1253}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001254EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001255
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001256/*
1257 * Simple DMA based buffer rx/tx routines.
1258 * Nothing fancy, just a single buffer tx/rx through DMA.
1259 * The DMA resources are released once the transfer is done.
1260 * For anything fancier, you should use your own customized DMA
1261 * routines and callbacks.
1262 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001263int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
1264 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001265{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001266 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001267 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001268 int src_port = 0;
1269 int dest_port = 0;
1270 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001271
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001272 if (!omap_mcbsp_check_valid_id(id)) {
1273 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1274 return -ENODEV;
1275 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001276 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001277
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001278 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001279 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001280 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001281 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001282 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001283 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001284 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001285 return -EAGAIN;
1286 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001287 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001288
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001289 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001290 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001291
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001292 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001293
Tony Lindgren120db2c2006-04-02 17:46:27 +01001294 if (cpu_class_is_omap1()) {
1295 src_port = OMAP_DMA_PORT_TIPB;
1296 dest_port = OMAP_DMA_PORT_EMIFF;
1297 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001298 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001299 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001300
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001301 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001302 OMAP_DMA_DATA_TYPE_S16,
1303 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001304 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001305 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001306
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001307 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001308 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001309 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001310 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001311 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001312
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001313 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001314 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001315 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001316 buffer,
1317 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001318
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001319 omap_start_dma(mcbsp->dma_tx_lch);
1320 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001321
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001322 return 0;
1323}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001324EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001325
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001326int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
1327 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001328{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001329 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001330 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001331 int src_port = 0;
1332 int dest_port = 0;
1333 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001334
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001335 if (!omap_mcbsp_check_valid_id(id)) {
1336 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1337 return -ENODEV;
1338 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001339 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001340
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001341 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001342 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001343 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001344 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001345 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001346 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001347 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001348 return -EAGAIN;
1349 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001350 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001351
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001352 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001353 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001354
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001355 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001356
Tony Lindgren120db2c2006-04-02 17:46:27 +01001357 if (cpu_class_is_omap1()) {
1358 src_port = OMAP_DMA_PORT_TIPB;
1359 dest_port = OMAP_DMA_PORT_EMIFF;
1360 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001361 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001362 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001363
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001364 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001365 OMAP_DMA_DATA_TYPE_S16,
1366 length >> 1, 1,
1367 OMAP_DMA_SYNC_ELEMENT,
1368 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001369
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001370 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001371 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001372 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001373 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001374 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001375
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001376 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001377 dest_port,
1378 OMAP_DMA_AMODE_POST_INC,
1379 buffer,
1380 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001381
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001382 omap_start_dma(mcbsp->dma_rx_lch);
1383 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001384
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001385 return 0;
1386}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001387EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001388
1389/*
1390 * SPI wrapper.
1391 * Since SPI setup is much simpler than the generic McBSP one,
1392 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1393 * Once this is done, you can call omap_mcbsp_start().
1394 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001395void omap_mcbsp_set_spi_mode(unsigned int id,
1396 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001397{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001398 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001399 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1400
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001401 if (!omap_mcbsp_check_valid_id(id)) {
1402 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001403 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001404 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001405 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001406
1407 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1408
1409 /* SPI has only one frame */
1410 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1411 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1412
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001413 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001414 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1415 mcbsp_cfg.spcr1 |= (1 << 12);
1416 else
1417 mcbsp_cfg.spcr1 |= (3 << 11);
1418
1419 /* Set clock parities */
1420 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1421 mcbsp_cfg.pcr0 |= CLKRP;
1422 else
1423 mcbsp_cfg.pcr0 &= ~CLKRP;
1424
1425 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1426 mcbsp_cfg.pcr0 &= ~CLKXP;
1427 else
1428 mcbsp_cfg.pcr0 |= CLKXP;
1429
1430 /* Set SCLKME to 0 and CLKSM to 1 */
1431 mcbsp_cfg.pcr0 &= ~SCLKME;
1432 mcbsp_cfg.srgr2 |= CLKSM;
1433
1434 /* Set FSXP */
1435 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1436 mcbsp_cfg.pcr0 &= ~FSXP;
1437 else
1438 mcbsp_cfg.pcr0 |= FSXP;
1439
1440 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1441 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001442 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001443 mcbsp_cfg.pcr0 |= FSXM;
1444 mcbsp_cfg.srgr2 &= ~FSGM;
1445 mcbsp_cfg.xcr2 |= XDATDLY(1);
1446 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001447 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001448 mcbsp_cfg.pcr0 &= ~CLKXM;
1449 mcbsp_cfg.srgr1 |= CLKGDV(1);
1450 mcbsp_cfg.pcr0 &= ~FSXM;
1451 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1452 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1453 }
1454
1455 mcbsp_cfg.xcr2 &= ~XPHASE;
1456 mcbsp_cfg.rcr2 &= ~RPHASE;
1457
1458 omap_mcbsp_config(id, &mcbsp_cfg);
1459}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001460EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001461
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001462#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001463#define max_thres(m) (mcbsp->pdata->buffer_size)
1464#define valid_threshold(m, val) ((val) <= max_thres(m))
1465#define THRESHOLD_PROP_BUILDER(prop) \
1466static ssize_t prop##_show(struct device *dev, \
1467 struct device_attribute *attr, char *buf) \
1468{ \
1469 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1470 \
1471 return sprintf(buf, "%u\n", mcbsp->prop); \
1472} \
1473 \
1474static ssize_t prop##_store(struct device *dev, \
1475 struct device_attribute *attr, \
1476 const char *buf, size_t size) \
1477{ \
1478 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1479 unsigned long val; \
1480 int status; \
1481 \
1482 status = strict_strtoul(buf, 0, &val); \
1483 if (status) \
1484 return status; \
1485 \
1486 if (!valid_threshold(mcbsp, val)) \
1487 return -EDOM; \
1488 \
1489 mcbsp->prop = val; \
1490 return size; \
1491} \
1492 \
1493static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1494
1495THRESHOLD_PROP_BUILDER(max_tx_thres);
1496THRESHOLD_PROP_BUILDER(max_rx_thres);
1497
Jarkko Nikula9b300502009-08-24 17:45:50 +03001498static const char *dma_op_modes[] = {
1499 "element", "threshold", "frame",
1500};
1501
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001502static ssize_t dma_op_mode_show(struct device *dev,
1503 struct device_attribute *attr, char *buf)
1504{
1505 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001506 int dma_op_mode, i = 0;
1507 ssize_t len = 0;
1508 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001509
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001510 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001511
Jarkko Nikula9b300502009-08-24 17:45:50 +03001512 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1513 if (dma_op_mode == i)
1514 len += sprintf(buf + len, "[%s] ", *s);
1515 else
1516 len += sprintf(buf + len, "%s ", *s);
1517 }
1518 len += sprintf(buf + len, "\n");
1519
1520 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001521}
1522
1523static ssize_t dma_op_mode_store(struct device *dev,
1524 struct device_attribute *attr,
1525 const char *buf, size_t size)
1526{
1527 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001528 const char * const *s;
1529 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001530
Jarkko Nikula9b300502009-08-24 17:45:50 +03001531 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1532 if (sysfs_streq(buf, *s))
1533 break;
1534
1535 if (i == ARRAY_SIZE(dma_op_modes))
1536 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001537
1538 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001539 if (!mcbsp->free) {
1540 size = -EBUSY;
1541 goto unlock;
1542 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001543 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001544
1545unlock:
1546 spin_unlock_irq(&mcbsp->lock);
1547
1548 return size;
1549}
1550
1551static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1552
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001553static ssize_t st_taps_show(struct device *dev,
1554 struct device_attribute *attr, char *buf)
1555{
1556 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1557 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1558 ssize_t status = 0;
1559 int i;
1560
1561 spin_lock_irq(&mcbsp->lock);
1562 for (i = 0; i < st_data->nr_taps; i++)
1563 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1564 st_data->taps[i]);
1565 if (i)
1566 status += sprintf(&buf[status], "\n");
1567 spin_unlock_irq(&mcbsp->lock);
1568
1569 return status;
1570}
1571
1572static ssize_t st_taps_store(struct device *dev,
1573 struct device_attribute *attr,
1574 const char *buf, size_t size)
1575{
1576 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1577 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1578 int val, tmp, status, i = 0;
1579
1580 spin_lock_irq(&mcbsp->lock);
1581 memset(st_data->taps, 0, sizeof(st_data->taps));
1582 st_data->nr_taps = 0;
1583
1584 do {
1585 status = sscanf(buf, "%d%n", &val, &tmp);
1586 if (status < 0 || status == 0) {
1587 size = -EINVAL;
1588 goto out;
1589 }
1590 if (val < -32768 || val > 32767) {
1591 size = -EINVAL;
1592 goto out;
1593 }
1594 st_data->taps[i++] = val;
1595 buf += tmp;
1596 if (*buf != ',')
1597 break;
1598 buf++;
1599 } while (1);
1600
1601 st_data->nr_taps = i;
1602
1603out:
1604 spin_unlock_irq(&mcbsp->lock);
1605
1606 return size;
1607}
1608
1609static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1610
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001611static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001612 &dev_attr_max_tx_thres.attr,
1613 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001614 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001615 NULL,
1616};
1617
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001618static const struct attribute_group additional_attr_group = {
1619 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001620};
1621
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001622static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001623{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001624 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001625}
1626
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001627static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001628{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001629 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001630}
1631
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001632static const struct attribute *sidetone_attrs[] = {
1633 &dev_attr_st_taps.attr,
1634 NULL,
1635};
1636
1637static const struct attribute_group sidetone_attr_group = {
1638 .attrs = (struct attribute **)sidetone_attrs,
1639};
1640
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -07001641static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001642{
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001643 struct platform_device *pdev;
1644 struct resource *res;
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001645 struct omap_mcbsp_st_data *st_data;
1646 int err;
1647
1648 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1649 if (!st_data) {
1650 err = -ENOMEM;
1651 goto err1;
1652 }
1653
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001654 pdev = container_of(mcbsp->dev, struct platform_device, dev);
1655
1656 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1657 st_data->io_base_st = ioremap(res->start, resource_size(res));
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001658 if (!st_data->io_base_st) {
1659 err = -ENOMEM;
1660 goto err2;
1661 }
1662
1663 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1664 if (err)
1665 goto err3;
1666
1667 mcbsp->st_data = st_data;
1668 return 0;
1669
1670err3:
1671 iounmap(st_data->io_base_st);
1672err2:
1673 kfree(st_data);
1674err1:
1675 return err;
1676
1677}
1678
1679static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1680{
1681 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1682
1683 if (st_data) {
1684 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1685 iounmap(st_data->io_base_st);
1686 kfree(st_data);
1687 }
1688}
1689
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001690static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1691{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001692 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001693 if (cpu_is_omap34xx()) {
Peter Ujfalusi451fd822010-06-03 07:39:33 +03001694 /*
1695 * Initially configure the maximum thresholds to a safe value.
1696 * The McBSP FIFO usage with these values should not go under
1697 * 16 locations.
1698 * If the whole FIFO without safety buffer is used, than there
1699 * is a possibility that the DMA will be not able to push the
1700 * new data on time, causing channel shifts in runtime.
1701 */
1702 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1703 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001704 /*
1705 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1706 * for mcbsp2 instances.
1707 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001708 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001709 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001710 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001711
1712 if (mcbsp->id == 2 || mcbsp->id == 3)
1713 if (omap_st_add(mcbsp))
1714 dev_warn(mcbsp->dev,
1715 "Unable to create sidetone controls\n");
1716
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001717 } else {
1718 mcbsp->max_tx_thres = -EINVAL;
1719 mcbsp->max_rx_thres = -EINVAL;
1720 }
1721}
1722
1723static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1724{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001725 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001726 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001727
1728 if (mcbsp->id == 2 || mcbsp->id == 3)
1729 omap_st_remove(mcbsp);
1730 }
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001731}
1732#else
1733static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1734static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001735#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001736
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001737/*
1738 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1739 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1740 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001741static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001742{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001743 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001744 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001745 int id = pdev->id - 1;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001746 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001747 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001748
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001749 if (!pdata) {
1750 dev_err(&pdev->dev, "McBSP device initialized without"
1751 "platform data\n");
1752 ret = -EINVAL;
1753 goto exit;
1754 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001755
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001756 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001757
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001758 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001759 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1760 ret = -EINVAL;
1761 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001762 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001763
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001764 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1765 if (!mcbsp) {
1766 ret = -ENOMEM;
1767 goto exit;
1768 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001769
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001770 spin_lock_init(&mcbsp->lock);
1771 mcbsp->id = id + 1;
Shubhrajyoti D6722a722010-12-07 16:25:41 -08001772 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001773 mcbsp->dma_tx_lch = -1;
1774 mcbsp->dma_rx_lch = -1;
1775
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001776 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1777 if (!res) {
1778 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1779 if (!res) {
1780 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1781 "resource\n", __func__, pdev->id);
1782 ret = -ENOMEM;
1783 goto exit;
1784 }
1785 }
1786 mcbsp->phys_base = res->start;
1787 omap_mcbsp_cache_size = resource_size(res);
1788 mcbsp->io_base = ioremap(res->start, resource_size(res));
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001789 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001790 ret = -ENOMEM;
1791 goto err_ioremap;
1792 }
1793
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001794 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1795 if (!res)
1796 mcbsp->phys_dma_base = mcbsp->phys_base;
1797 else
1798 mcbsp->phys_dma_base = res->start;
1799
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001800 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001801 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001802
1803 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1804 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1805
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +05301806 /* From OMAP4 there will be a single irq line */
1807 if (mcbsp->tx_irq == -ENXIO)
1808 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1809
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001810 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1811 if (!res) {
1812 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1813 __func__, pdev->id);
1814 ret = -ENODEV;
1815 goto err_res;
1816 }
1817 mcbsp->dma_rx_sync = res->start;
1818
1819 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1820 if (!res) {
1821 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1822 __func__, pdev->id);
1823 ret = -ENODEV;
1824 goto err_res;
1825 }
1826 mcbsp->dma_tx_sync = res->start;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001827
Russell Kingb820ce42009-01-23 10:26:46 +00001828 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1829 if (IS_ERR(mcbsp->fclk)) {
1830 ret = PTR_ERR(mcbsp->fclk);
1831 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301832 goto err_res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001833 }
1834
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001835 mcbsp->pdata = pdata;
1836 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001837 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001838 platform_set_drvdata(pdev, mcbsp);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301839 pm_runtime_enable(mcbsp->dev);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001840
1841 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1842 omap34xx_device_init(mcbsp);
1843
Russell Kingd592dd12008-09-04 14:25:42 +01001844 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001845
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001846err_res:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001847 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001848err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001849 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001850exit:
1851 return ret;
1852}
1853
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001854static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001855{
1856 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1857
1858 platform_set_drvdata(pdev, NULL);
1859 if (mcbsp) {
1860
1861 if (mcbsp->pdata && mcbsp->pdata->ops &&
1862 mcbsp->pdata->ops->free)
1863 mcbsp->pdata->ops->free(mcbsp->id);
1864
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001865 omap34xx_device_exit(mcbsp);
1866
Russell Kingb820ce42009-01-23 10:26:46 +00001867 clk_put(mcbsp->fclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001868
Russell Kingd592dd12008-09-04 14:25:42 +01001869 iounmap(mcbsp->io_base);
Jarkko Nikula5f3b7282010-12-07 16:25:40 -08001870 kfree(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001871 }
1872
1873 return 0;
1874}
1875
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001876static struct platform_driver omap_mcbsp_driver = {
1877 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001878 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001879 .driver = {
1880 .name = "omap-mcbsp",
1881 },
1882};
1883
1884int __init omap_mcbsp_init(void)
1885{
1886 /* Register the McBSP driver */
1887 return platform_driver_register(&omap_mcbsp_driver);
1888}