blob: 52dfcc81511e27ccf2b797517abfc7df73aa5bcb [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/mcbsp.c
3 *
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Multichannel mode not supported.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030018#include <linux/platform_device.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010019#include <linux/wait.h>
20#include <linux/completion.h>
21#include <linux/interrupt.h>
22#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Tony Lindgren04fbf6a2007-02-12 10:50:53 -080024#include <linux/delay.h>
Eduardo Valentinfb78d802008-07-03 12:24:39 +030025#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026
Tony Lindgrence491cf2009-10-20 09:40:47 -070027#include <plat/dma.h>
28#include <plat/mcbsp.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029
Eero Nurkkalad912fa92010-02-22 12:21:11 +000030#include "../mach-omap2/cm-regbits-34xx.h"
31
Chandra Shekharb4b58f52008-10-08 10:01:39 +030032struct omap_mcbsp **mcbsp_ptr;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080033int omap_mcbsp_count, omap_mcbsp_cache_size;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030034
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080035void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030036{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080037 if (cpu_class_is_omap1()) {
38 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080039 __raw_writew((u16)val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080040 } else if (cpu_is_omap2420()) {
41 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
42 __raw_writew((u16)val, mcbsp->io_base + reg);
43 } else {
44 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080045 __raw_writel(val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080046 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030047}
48
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080049int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030050{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080051 if (cpu_class_is_omap1()) {
52 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
53 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
54 } else if (cpu_is_omap2420()) {
55 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
56 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
57 } else {
58 return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
59 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
60 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030061}
62
Eero Nurkkalad912fa92010-02-22 12:21:11 +000063#ifdef CONFIG_ARCH_OMAP3
64void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
65{
66 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
67}
68
69int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
70{
71 return __raw_readl(mcbsp->st_data->io_base_st + reg);
72}
73#endif
74
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080075#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080076 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080077#define MCBSP_WRITE(mcbsp, reg, val) \
78 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080079#define MCBSP_READ_CACHE(mcbsp, reg) \
80 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030081
82#define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
83#define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010084
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 {
158 complete(&mcbsp_rx->tx_irq_completion);
159 }
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 */
239 w = cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
240 w &= ~(1 << (mcbsp->id - 2));
241 cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
242
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
268 w = cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
269 w |= 1 << (mcbsp->id - 2);
270 cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
271}
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/*
483 * omap_mcbsp_set_tx_threshold configures how to deal
484 * with transmit threshold. the threshold value and handler can be
485 * configure in here.
486 */
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
491 if (!cpu_is_omap34xx())
492 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
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800500 MCBSP_WRITE(mcbsp, THRSH2, threshold);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300501}
502EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
503
504/*
505 * omap_mcbsp_set_rx_threshold configures how to deal
506 * with receive threshold. the threshold value and handler can be
507 * configure in here.
508 */
509void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
510{
511 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300512
513 if (!cpu_is_omap34xx())
514 return;
515
516 if (!omap_mcbsp_check_valid_id(id)) {
517 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
518 return;
519 }
520 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300521
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800522 MCBSP_WRITE(mcbsp, THRSH1, threshold);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300523}
524EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +0300525
526/*
527 * omap_mcbsp_get_max_tx_thres just return the current configured
528 * maximum threshold for transmission
529 */
530u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
531{
532 struct omap_mcbsp *mcbsp;
533
534 if (!omap_mcbsp_check_valid_id(id)) {
535 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
536 return -ENODEV;
537 }
538 mcbsp = id_to_mcbsp_ptr(id);
539
540 return mcbsp->max_tx_thres;
541}
542EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
543
544/*
545 * omap_mcbsp_get_max_rx_thres just return the current configured
546 * maximum threshold for reception
547 */
548u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
549{
550 struct omap_mcbsp *mcbsp;
551
552 if (!omap_mcbsp_check_valid_id(id)) {
553 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
554 return -ENODEV;
555 }
556 mcbsp = id_to_mcbsp_ptr(id);
557
558 return mcbsp->max_rx_thres;
559}
560EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300561
562/*
563 * omap_mcbsp_get_dma_op_mode just return the current configured
564 * operating mode for the mcbsp channel
565 */
566int omap_mcbsp_get_dma_op_mode(unsigned int id)
567{
568 struct omap_mcbsp *mcbsp;
569 int dma_op_mode;
570
571 if (!omap_mcbsp_check_valid_id(id)) {
572 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
573 return -ENODEV;
574 }
575 mcbsp = id_to_mcbsp_ptr(id);
576
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300577 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300578
579 return dma_op_mode;
580}
581EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300582
583static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
584{
585 /*
586 * Enable wakup behavior, smart idle and all wakeups
587 * REVISIT: some wakeups may be unnecessary
588 */
589 if (cpu_is_omap34xx()) {
590 u16 syscon;
591
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800592 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300593 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eduardo Valentind99a7452009-08-20 16:18:18 +0300594
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300595 if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
596 syscon |= (ENAWAKEUP | SIDLEMODE(0x02) |
597 CLOCKACTIVITY(0x02));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800598 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300599 } else {
Eduardo Valentind99a7452009-08-20 16:18:18 +0300600 syscon |= SIDLEMODE(0x01);
Eero Nurkkalafa3935b2009-08-20 16:18:19 +0300601 }
Eduardo Valentind99a7452009-08-20 16:18:18 +0300602
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800603 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300604 }
605}
606
607static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
608{
609 /*
610 * Disable wakup behavior, smart idle and all wakeups
611 */
612 if (cpu_is_omap34xx()) {
613 u16 syscon;
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300614
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800615 syscon = MCBSP_READ(mcbsp, SYSCON);
Eero Nurkkala2ba93f82009-08-20 16:18:17 +0300616 syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300617 /*
618 * HW bug workaround - If no_idle mode is taken, we need to
619 * go to smart_idle before going to always_idle, or the
620 * device will not hit retention anymore.
621 */
622 syscon |= SIDLEMODE(0x02);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800623 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300624
625 syscon &= ~(SIDLEMODE(0x03));
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800626 MCBSP_WRITE(mcbsp, SYSCON, syscon);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300627
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800628 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300629 }
630}
631#else
632static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
633static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000634static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
635static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300636#endif
637
Tony Lindgren120db2c2006-04-02 17:46:27 +0100638/*
639 * We can choose between IRQ based or polled IO.
640 * This needs to be called before omap_mcbsp_request().
641 */
642int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
643{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300644 struct omap_mcbsp *mcbsp;
645
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300646 if (!omap_mcbsp_check_valid_id(id)) {
647 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
648 return -ENODEV;
649 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300650 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100651
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300652 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100653
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300654 if (!mcbsp->free) {
655 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
656 mcbsp->id);
657 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100658 return -EINVAL;
659 }
660
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300661 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100662
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300663 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100664
665 return 0;
666}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300667EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100668
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100669int omap_mcbsp_request(unsigned int id)
670{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300671 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800672 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673 int err;
674
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300675 if (!omap_mcbsp_check_valid_id(id)) {
676 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
677 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100678 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300679 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300680
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800681 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
682 if (!reg_cache) {
683 return -ENOMEM;
684 }
685
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300686 spin_lock(&mcbsp->lock);
687 if (!mcbsp->free) {
688 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
689 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800690 err = -EBUSY;
691 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100692 }
693
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300694 mcbsp->free = 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800695 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300696 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697
Russell Kingb820ce42009-01-23 10:26:46 +0000698 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
699 mcbsp->pdata->ops->request(id);
700
701 clk_enable(mcbsp->iclk);
702 clk_enable(mcbsp->fclk);
703
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300704 /* Do procedure specific to omap34xx arch, if applicable */
705 omap34xx_mcbsp_request(mcbsp);
706
Jarkko Nikula5a070552008-10-08 10:01:41 +0300707 /*
708 * Make sure that transmitter, receiver and sample-rate generator are
709 * not running before activating IRQs.
710 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800711 MCBSP_WRITE(mcbsp, SPCR1, 0);
712 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300713
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300714 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100715 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300716 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300717 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
718 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100719 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300720 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
721 "for McBSP%d\n", mcbsp->tx_irq,
722 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800723 goto err_clk_disable;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100724 }
725
Jarkko Nikula5a070552008-10-08 10:01:41 +0300726 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300727 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
728 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100729 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300730 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
731 "for McBSP%d\n", mcbsp->rx_irq,
732 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800733 goto err_free_irq;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100734 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735 }
736
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800738err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800739 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800740err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800741 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800742 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800743
744 /* Do procedure specific to omap34xx arch, if applicable */
745 omap34xx_mcbsp_free(mcbsp);
746
747 clk_disable(mcbsp->fclk);
748 clk_disable(mcbsp->iclk);
749
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800750 spin_lock(&mcbsp->lock);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800751 mcbsp->free = 1;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800752 mcbsp->reg_cache = NULL;
753err_kfree:
754 spin_unlock(&mcbsp->lock);
755 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800756
757 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300759EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760
761void omap_mcbsp_free(unsigned int id)
762{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300763 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800764 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300765
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300766 if (!omap_mcbsp_check_valid_id(id)) {
767 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100769 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300770 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100771
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300772 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
773 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300774
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300775 /* Do procedure specific to omap34xx arch, if applicable */
776 omap34xx_mcbsp_free(mcbsp);
777
Russell Kingb820ce42009-01-23 10:26:46 +0000778 clk_disable(mcbsp->fclk);
779 clk_disable(mcbsp->iclk);
780
781 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
782 /* Free IRQs */
783 free_irq(mcbsp->rx_irq, (void *)mcbsp);
784 free_irq(mcbsp->tx_irq, (void *)mcbsp);
785 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100786
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800787 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800789 spin_lock(&mcbsp->lock);
790 if (mcbsp->free)
791 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
792 else
793 mcbsp->free = 1;
794 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300795 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800796
797 if (reg_cache)
798 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300800EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801
802/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300803 * Here we start the McBSP, by enabling transmitter, receiver or both.
804 * If no transmitter or receiver is active prior calling, then sample-rate
805 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100806 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300807void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100808{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300809 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300810 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100811 u16 w;
812
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300813 if (!omap_mcbsp_check_valid_id(id)) {
814 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100815 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300816 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300817 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100818
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000819 if (cpu_is_omap34xx())
820 omap_st_start(mcbsp);
821
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800822 mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
823 mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100824
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800825 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
826 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300827
828 if (idle) {
829 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800830 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800831 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300832 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100833
834 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300835 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800836 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800837 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100838
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300839 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800840 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800841 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100842
Eduardo Valentin44a63112009-08-20 16:18:09 +0300843 /*
844 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
845 * REVISIT: 100us may give enough time for two CLKSRG, however
846 * due to some unknown PM related, clock gating etc. reason it
847 * is now at 500us.
848 */
849 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300851 if (idle) {
852 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800853 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800854 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300855 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300857 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
858 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800859 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300860 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800861 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800862 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300863 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800864 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300865 }
866
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100867 /* Dump McBSP Regs */
868 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300870EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100871
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300872void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100873{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300874 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300875 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876 u16 w;
877
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300878 if (!omap_mcbsp_check_valid_id(id)) {
879 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100880 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300881 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100882
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300883 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100884
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300885 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300886 tx &= 1;
887 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800888 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300889 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800890 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300891 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800892 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800893 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100894
895 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300896 rx &= 1;
897 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800898 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700899 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800900 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300901 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800902 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800903 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100904
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800905 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
906 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300907
908 if (idle) {
909 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800910 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800911 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300912 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000913
914 if (cpu_is_omap34xx())
915 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100916}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300917EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100918
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100919/* polled mcbsp i/o operations */
920int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
921{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300922 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300923
924 if (!omap_mcbsp_check_valid_id(id)) {
925 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
926 return -ENODEV;
927 }
928
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300929 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300930
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800931 MCBSP_WRITE(mcbsp, DXR1, buf);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100932 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800933 if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100934 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000935 MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100936 /* resend */
937 return -1;
938 } else {
939 /* wait for transmit confirmation */
940 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800941 while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100942 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800943 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800944 MCBSP_READ_CACHE(mcbsp, SPCR2) &
945 (~XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100946 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800947 MCBSP_WRITE(mcbsp, SPCR2,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800948 MCBSP_READ_CACHE(mcbsp, SPCR2) |
949 (XRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100950 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300951 dev_err(mcbsp->dev, "Could not write to"
952 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100953 return -2;
954 }
955 }
956 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300957
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100958 return 0;
959}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300960EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100961
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300962int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100963{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300964 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300965
966 if (!omap_mcbsp_check_valid_id(id)) {
967 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
968 return -ENODEV;
969 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300970 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300971
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100972 /* if frame sync error - clear the error */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800973 if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100974 /* clear error */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000975 MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100976 /* resend */
977 return -1;
978 } else {
979 /* wait for recieve confirmation */
980 int attemps = 0;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800981 while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100982 if (attemps++ > 1000) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800983 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800984 MCBSP_READ_CACHE(mcbsp, SPCR1) &
985 (~RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100986 udelay(10);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800987 MCBSP_WRITE(mcbsp, SPCR1,
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800988 MCBSP_READ_CACHE(mcbsp, SPCR1) |
989 (RRST));
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100990 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300991 dev_err(mcbsp->dev, "Could not read from"
992 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100993 return -2;
994 }
995 }
996 }
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800997 *buf = MCBSP_READ(mcbsp, DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300998
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100999 return 0;
1000}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001001EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001002
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001003/*
1004 * IRQ based word transmission.
1005 */
1006void omap_mcbsp_xmit_word(unsigned int id, u32 word)
1007{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001008 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001009 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001010
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001011 if (!omap_mcbsp_check_valid_id(id)) {
1012 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001013 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001014 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001015
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001016 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001017 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001018
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001019 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001020
1021 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001022 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1023 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001024}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001025EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001026
1027u32 omap_mcbsp_recv_word(unsigned int id)
1028{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001029 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001030 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001031 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001032
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001033 if (!omap_mcbsp_check_valid_id(id)) {
1034 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1035 return -ENODEV;
1036 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001037 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001038
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001039 word_length = mcbsp->rx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001040
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001041 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001042
1043 if (word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001044 word_msb = MCBSP_READ(mcbsp, DRR2);
1045 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001046
1047 return (word_lsb | (word_msb << 16));
1048}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001049EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001050
Tony Lindgren120db2c2006-04-02 17:46:27 +01001051int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
1052{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001053 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001054 omap_mcbsp_word_length tx_word_length;
1055 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001056 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1057
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001058 if (!omap_mcbsp_check_valid_id(id)) {
1059 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1060 return -ENODEV;
1061 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001062 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001063 tx_word_length = mcbsp->tx_word_length;
1064 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001065
Tony Lindgren120db2c2006-04-02 17:46:27 +01001066 if (tx_word_length != rx_word_length)
1067 return -EINVAL;
1068
1069 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001070 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001071 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001072 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001073 if (attempts++ > 1000) {
1074 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001075 MCBSP_WRITE(mcbsp, SPCR2,
1076 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001077 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001078 MCBSP_WRITE(mcbsp, SPCR2,
1079 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001080 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001081 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1082 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001083 return -EAGAIN;
1084 }
1085 }
1086
1087 /* Now we can push the data */
1088 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001089 MCBSP_WRITE(mcbsp, DXR2, word >> 16);
1090 MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001091
1092 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001093 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001094 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001095 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001096 if (attempts++ > 1000) {
1097 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001098 MCBSP_WRITE(mcbsp, SPCR1,
1099 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001100 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001101 MCBSP_WRITE(mcbsp, SPCR1,
1102 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001103 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001104 dev_err(mcbsp->dev, "McBSP%d receiver not "
1105 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001106 return -EAGAIN;
1107 }
1108 }
1109
1110 /* Receiver is ready, let's read the dummy data */
1111 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001112 word_msb = MCBSP_READ(mcbsp, DRR2);
1113 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001114
1115 return 0;
1116}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001117EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001118
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001119int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +01001120{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001121 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +01001122 u32 clock_word = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001123 omap_mcbsp_word_length tx_word_length;
1124 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001125 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
1126
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001127 if (!omap_mcbsp_check_valid_id(id)) {
1128 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1129 return -ENODEV;
1130 }
1131
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001132 mcbsp = id_to_mcbsp_ptr(id);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001133
1134 tx_word_length = mcbsp->tx_word_length;
1135 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001136
Tony Lindgren120db2c2006-04-02 17:46:27 +01001137 if (tx_word_length != rx_word_length)
1138 return -EINVAL;
1139
1140 /* First we wait for the transmitter to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001141 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001142 while (!(spcr2 & XRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001143 spcr2 = MCBSP_READ(mcbsp, SPCR2);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001144 if (attempts++ > 1000) {
1145 /* We must reset the transmitter */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001146 MCBSP_WRITE(mcbsp, SPCR2,
1147 MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001148 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001149 MCBSP_WRITE(mcbsp, SPCR2,
1150 MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001151 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001152 dev_err(mcbsp->dev, "McBSP%d transmitter not "
1153 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001154 return -EAGAIN;
1155 }
1156 }
1157
1158 /* We first need to enable the bus clock */
1159 if (tx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001160 MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
1161 MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001162
1163 /* We wait for the receiver to be ready */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001164 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001165 while (!(spcr1 & RRDY)) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001166 spcr1 = MCBSP_READ(mcbsp, SPCR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001167 if (attempts++ > 1000) {
1168 /* We must reset the receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001169 MCBSP_WRITE(mcbsp, SPCR1,
1170 MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
Tony Lindgren120db2c2006-04-02 17:46:27 +01001171 udelay(10);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -08001172 MCBSP_WRITE(mcbsp, SPCR1,
1173 MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001174 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001175 dev_err(mcbsp->dev, "McBSP%d receiver not "
1176 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001177 return -EAGAIN;
1178 }
1179 }
1180
1181 /* Receiver is ready, there is something for us */
1182 if (rx_word_length > OMAP_MCBSP_WORD_16)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -08001183 word_msb = MCBSP_READ(mcbsp, DRR2);
1184 word_lsb = MCBSP_READ(mcbsp, DRR1);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001185
1186 word[0] = (word_lsb | (word_msb << 16));
1187
1188 return 0;
1189}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001190EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001191
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001192/*
1193 * Simple DMA based buffer rx/tx routines.
1194 * Nothing fancy, just a single buffer tx/rx through DMA.
1195 * The DMA resources are released once the transfer is done.
1196 * For anything fancier, you should use your own customized DMA
1197 * routines and callbacks.
1198 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001199int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
1200 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001201{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001202 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001203 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001204 int src_port = 0;
1205 int dest_port = 0;
1206 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001207
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001208 if (!omap_mcbsp_check_valid_id(id)) {
1209 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1210 return -ENODEV;
1211 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001212 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001213
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001214 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001215 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001216 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001217 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001218 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001219 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001220 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001221 return -EAGAIN;
1222 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001223 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001224
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001225 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001226 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001227
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001228 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001229
Tony Lindgren120db2c2006-04-02 17:46:27 +01001230 if (cpu_class_is_omap1()) {
1231 src_port = OMAP_DMA_PORT_TIPB;
1232 dest_port = OMAP_DMA_PORT_EMIFF;
1233 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001234 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001235 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001236
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001237 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001238 OMAP_DMA_DATA_TYPE_S16,
1239 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001240 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001241 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001242
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001243 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001244 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001245 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001246 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001247 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001248
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001249 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001250 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001251 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001252 buffer,
1253 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001254
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001255 omap_start_dma(mcbsp->dma_tx_lch);
1256 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001257
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001258 return 0;
1259}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001260EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001261
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001262int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
1263 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001264{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001265 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001266 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001267 int src_port = 0;
1268 int dest_port = 0;
1269 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001270
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001271 if (!omap_mcbsp_check_valid_id(id)) {
1272 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
1273 return -ENODEV;
1274 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001275 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001276
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001277 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001278 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001279 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001280 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001281 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001282 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001283 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001284 return -EAGAIN;
1285 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001286 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001287
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001288 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001289 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001290
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001291 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001292
Tony Lindgren120db2c2006-04-02 17:46:27 +01001293 if (cpu_class_is_omap1()) {
1294 src_port = OMAP_DMA_PORT_TIPB;
1295 dest_port = OMAP_DMA_PORT_EMIFF;
1296 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001297 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001298 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +01001299
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001300 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001301 OMAP_DMA_DATA_TYPE_S16,
1302 length >> 1, 1,
1303 OMAP_DMA_SYNC_ELEMENT,
1304 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001305
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001306 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +01001307 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001308 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001309 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001310 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001311
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001312 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001313 dest_port,
1314 OMAP_DMA_AMODE_POST_INC,
1315 buffer,
1316 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001317
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001318 omap_start_dma(mcbsp->dma_rx_lch);
1319 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001320
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001321 return 0;
1322}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001323EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001324
1325/*
1326 * SPI wrapper.
1327 * Since SPI setup is much simpler than the generic McBSP one,
1328 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
1329 * Once this is done, you can call omap_mcbsp_start().
1330 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001331void omap_mcbsp_set_spi_mode(unsigned int id,
1332 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001333{
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001334 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001335 struct omap_mcbsp_reg_cfg mcbsp_cfg;
1336
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001337 if (!omap_mcbsp_check_valid_id(id)) {
1338 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001339 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001340 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001341 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001342
1343 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
1344
1345 /* SPI has only one frame */
1346 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
1347 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
1348
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001349 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001350 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
1351 mcbsp_cfg.spcr1 |= (1 << 12);
1352 else
1353 mcbsp_cfg.spcr1 |= (3 << 11);
1354
1355 /* Set clock parities */
1356 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1357 mcbsp_cfg.pcr0 |= CLKRP;
1358 else
1359 mcbsp_cfg.pcr0 &= ~CLKRP;
1360
1361 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
1362 mcbsp_cfg.pcr0 &= ~CLKXP;
1363 else
1364 mcbsp_cfg.pcr0 |= CLKXP;
1365
1366 /* Set SCLKME to 0 and CLKSM to 1 */
1367 mcbsp_cfg.pcr0 &= ~SCLKME;
1368 mcbsp_cfg.srgr2 |= CLKSM;
1369
1370 /* Set FSXP */
1371 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
1372 mcbsp_cfg.pcr0 &= ~FSXP;
1373 else
1374 mcbsp_cfg.pcr0 |= FSXP;
1375
1376 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
1377 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001378 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001379 mcbsp_cfg.pcr0 |= FSXM;
1380 mcbsp_cfg.srgr2 &= ~FSGM;
1381 mcbsp_cfg.xcr2 |= XDATDLY(1);
1382 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001383 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001384 mcbsp_cfg.pcr0 &= ~CLKXM;
1385 mcbsp_cfg.srgr1 |= CLKGDV(1);
1386 mcbsp_cfg.pcr0 &= ~FSXM;
1387 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
1388 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
1389 }
1390
1391 mcbsp_cfg.xcr2 &= ~XPHASE;
1392 mcbsp_cfg.rcr2 &= ~RPHASE;
1393
1394 omap_mcbsp_config(id, &mcbsp_cfg);
1395}
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001396EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001397
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001398#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001399#define max_thres(m) (mcbsp->pdata->buffer_size)
1400#define valid_threshold(m, val) ((val) <= max_thres(m))
1401#define THRESHOLD_PROP_BUILDER(prop) \
1402static ssize_t prop##_show(struct device *dev, \
1403 struct device_attribute *attr, char *buf) \
1404{ \
1405 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1406 \
1407 return sprintf(buf, "%u\n", mcbsp->prop); \
1408} \
1409 \
1410static ssize_t prop##_store(struct device *dev, \
1411 struct device_attribute *attr, \
1412 const char *buf, size_t size) \
1413{ \
1414 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1415 unsigned long val; \
1416 int status; \
1417 \
1418 status = strict_strtoul(buf, 0, &val); \
1419 if (status) \
1420 return status; \
1421 \
1422 if (!valid_threshold(mcbsp, val)) \
1423 return -EDOM; \
1424 \
1425 mcbsp->prop = val; \
1426 return size; \
1427} \
1428 \
1429static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1430
1431THRESHOLD_PROP_BUILDER(max_tx_thres);
1432THRESHOLD_PROP_BUILDER(max_rx_thres);
1433
Jarkko Nikula9b300502009-08-24 17:45:50 +03001434static const char *dma_op_modes[] = {
1435 "element", "threshold", "frame",
1436};
1437
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001438static ssize_t dma_op_mode_show(struct device *dev,
1439 struct device_attribute *attr, char *buf)
1440{
1441 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001442 int dma_op_mode, i = 0;
1443 ssize_t len = 0;
1444 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001445
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001446 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001447
Jarkko Nikula9b300502009-08-24 17:45:50 +03001448 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1449 if (dma_op_mode == i)
1450 len += sprintf(buf + len, "[%s] ", *s);
1451 else
1452 len += sprintf(buf + len, "%s ", *s);
1453 }
1454 len += sprintf(buf + len, "\n");
1455
1456 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001457}
1458
1459static ssize_t dma_op_mode_store(struct device *dev,
1460 struct device_attribute *attr,
1461 const char *buf, size_t size)
1462{
1463 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001464 const char * const *s;
1465 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001466
Jarkko Nikula9b300502009-08-24 17:45:50 +03001467 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1468 if (sysfs_streq(buf, *s))
1469 break;
1470
1471 if (i == ARRAY_SIZE(dma_op_modes))
1472 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001473
1474 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001475 if (!mcbsp->free) {
1476 size = -EBUSY;
1477 goto unlock;
1478 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001479 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001480
1481unlock:
1482 spin_unlock_irq(&mcbsp->lock);
1483
1484 return size;
1485}
1486
1487static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1488
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001489static ssize_t st_taps_show(struct device *dev,
1490 struct device_attribute *attr, char *buf)
1491{
1492 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1493 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1494 ssize_t status = 0;
1495 int i;
1496
1497 spin_lock_irq(&mcbsp->lock);
1498 for (i = 0; i < st_data->nr_taps; i++)
1499 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1500 st_data->taps[i]);
1501 if (i)
1502 status += sprintf(&buf[status], "\n");
1503 spin_unlock_irq(&mcbsp->lock);
1504
1505 return status;
1506}
1507
1508static ssize_t st_taps_store(struct device *dev,
1509 struct device_attribute *attr,
1510 const char *buf, size_t size)
1511{
1512 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1513 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1514 int val, tmp, status, i = 0;
1515
1516 spin_lock_irq(&mcbsp->lock);
1517 memset(st_data->taps, 0, sizeof(st_data->taps));
1518 st_data->nr_taps = 0;
1519
1520 do {
1521 status = sscanf(buf, "%d%n", &val, &tmp);
1522 if (status < 0 || status == 0) {
1523 size = -EINVAL;
1524 goto out;
1525 }
1526 if (val < -32768 || val > 32767) {
1527 size = -EINVAL;
1528 goto out;
1529 }
1530 st_data->taps[i++] = val;
1531 buf += tmp;
1532 if (*buf != ',')
1533 break;
1534 buf++;
1535 } while (1);
1536
1537 st_data->nr_taps = i;
1538
1539out:
1540 spin_unlock_irq(&mcbsp->lock);
1541
1542 return size;
1543}
1544
1545static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1546
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001547static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001548 &dev_attr_max_tx_thres.attr,
1549 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001550 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001551 NULL,
1552};
1553
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001554static const struct attribute_group additional_attr_group = {
1555 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001556};
1557
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001558static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001559{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001560 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001561}
1562
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001563static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001564{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001565 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001566}
1567
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001568static const struct attribute *sidetone_attrs[] = {
1569 &dev_attr_st_taps.attr,
1570 NULL,
1571};
1572
1573static const struct attribute_group sidetone_attr_group = {
1574 .attrs = (struct attribute **)sidetone_attrs,
1575};
1576
1577int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
1578{
1579 struct omap_mcbsp_platform_data *pdata = mcbsp->pdata;
1580 struct omap_mcbsp_st_data *st_data;
1581 int err;
1582
1583 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1584 if (!st_data) {
1585 err = -ENOMEM;
1586 goto err1;
1587 }
1588
1589 st_data->io_base_st = ioremap(pdata->phys_base_st, SZ_4K);
1590 if (!st_data->io_base_st) {
1591 err = -ENOMEM;
1592 goto err2;
1593 }
1594
1595 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1596 if (err)
1597 goto err3;
1598
1599 mcbsp->st_data = st_data;
1600 return 0;
1601
1602err3:
1603 iounmap(st_data->io_base_st);
1604err2:
1605 kfree(st_data);
1606err1:
1607 return err;
1608
1609}
1610
1611static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1612{
1613 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1614
1615 if (st_data) {
1616 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1617 iounmap(st_data->io_base_st);
1618 kfree(st_data);
1619 }
1620}
1621
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001622static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1623{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001624 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001625 if (cpu_is_omap34xx()) {
1626 mcbsp->max_tx_thres = max_thres(mcbsp);
1627 mcbsp->max_rx_thres = max_thres(mcbsp);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001628 /*
1629 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1630 * for mcbsp2 instances.
1631 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001632 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001633 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001634 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001635
1636 if (mcbsp->id == 2 || mcbsp->id == 3)
1637 if (omap_st_add(mcbsp))
1638 dev_warn(mcbsp->dev,
1639 "Unable to create sidetone controls\n");
1640
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001641 } else {
1642 mcbsp->max_tx_thres = -EINVAL;
1643 mcbsp->max_rx_thres = -EINVAL;
1644 }
1645}
1646
1647static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1648{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001649 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001650 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001651
1652 if (mcbsp->id == 2 || mcbsp->id == 3)
1653 omap_st_remove(mcbsp);
1654 }
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001655}
1656#else
1657static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1658static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001659#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001660
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001661/*
1662 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1663 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1664 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001665static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001666{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001667 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001668 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001669 int id = pdev->id - 1;
1670 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001671
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001672 if (!pdata) {
1673 dev_err(&pdev->dev, "McBSP device initialized without"
1674 "platform data\n");
1675 ret = -EINVAL;
1676 goto exit;
1677 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001678
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001679 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001680
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001681 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001682 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1683 ret = -EINVAL;
1684 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001685 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001686
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001687 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1688 if (!mcbsp) {
1689 ret = -ENOMEM;
1690 goto exit;
1691 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001692
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001693 spin_lock_init(&mcbsp->lock);
1694 mcbsp->id = id + 1;
1695 mcbsp->free = 1;
1696 mcbsp->dma_tx_lch = -1;
1697 mcbsp->dma_rx_lch = -1;
1698
1699 mcbsp->phys_base = pdata->phys_base;
1700 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
1701 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001702 ret = -ENOMEM;
1703 goto err_ioremap;
1704 }
1705
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001706 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001707 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
1708 mcbsp->tx_irq = pdata->tx_irq;
1709 mcbsp->rx_irq = pdata->rx_irq;
1710 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
1711 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001712
Russell Kingb820ce42009-01-23 10:26:46 +00001713 mcbsp->iclk = clk_get(&pdev->dev, "ick");
1714 if (IS_ERR(mcbsp->iclk)) {
1715 ret = PTR_ERR(mcbsp->iclk);
1716 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
1717 goto err_iclk;
1718 }
Stanley.Miao06151152009-01-29 08:57:12 -08001719
Russell Kingb820ce42009-01-23 10:26:46 +00001720 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1721 if (IS_ERR(mcbsp->fclk)) {
1722 ret = PTR_ERR(mcbsp->fclk);
1723 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
1724 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001725 }
1726
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001727 mcbsp->pdata = pdata;
1728 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001729 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001730 platform_set_drvdata(pdev, mcbsp);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001731
1732 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1733 omap34xx_device_init(mcbsp);
1734
Russell Kingd592dd12008-09-04 14:25:42 +01001735 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001736
Russell Kingb820ce42009-01-23 10:26:46 +00001737err_fclk:
1738 clk_put(mcbsp->iclk);
1739err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001740 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001741err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001742 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001743exit:
1744 return ret;
1745}
1746
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001747static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001748{
1749 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1750
1751 platform_set_drvdata(pdev, NULL);
1752 if (mcbsp) {
1753
1754 if (mcbsp->pdata && mcbsp->pdata->ops &&
1755 mcbsp->pdata->ops->free)
1756 mcbsp->pdata->ops->free(mcbsp->id);
1757
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001758 omap34xx_device_exit(mcbsp);
1759
Russell Kingb820ce42009-01-23 10:26:46 +00001760 clk_disable(mcbsp->fclk);
1761 clk_disable(mcbsp->iclk);
1762 clk_put(mcbsp->fclk);
1763 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001764
Russell Kingd592dd12008-09-04 14:25:42 +01001765 iounmap(mcbsp->io_base);
1766
Russell Kingb820ce42009-01-23 10:26:46 +00001767 mcbsp->fclk = NULL;
1768 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001769 mcbsp->free = 0;
1770 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001771 }
1772
1773 return 0;
1774}
1775
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001776static struct platform_driver omap_mcbsp_driver = {
1777 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001778 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001779 .driver = {
1780 .name = "omap-mcbsp",
1781 },
1782};
1783
1784int __init omap_mcbsp_init(void)
1785{
1786 /* Register the McBSP driver */
1787 return platform_driver_register(&omap_mcbsp_driver);
1788}