blob: 623f2c1e9d4a1b435ad9e7812c24755a26455b89 [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/interrupt.h>
20#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000021#include <linux/clk.h>
Tony Lindgren04fbf6a2007-02-12 10:50:53 -080022#include <linux/delay.h>
Eduardo Valentinfb78d802008-07-03 12:24:39 +030023#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010025
Tony Lindgrence491cf2009-10-20 09:40:47 -070026#include <plat/mcbsp.h>
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +053027#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010028
Paul Walmsley59fb6592010-12-21 15:30:55 -070029/* XXX These "sideways" includes are a sign that something is wrong */
30#include "../mach-omap2/cm2xxx_3xxx.h"
Eero Nurkkalad912fa92010-02-22 12:21:11 +000031#include "../mach-omap2/cm-regbits-34xx.h"
32
Chandra Shekharb4b58f52008-10-08 10:01:39 +030033struct omap_mcbsp **mcbsp_ptr;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080034int omap_mcbsp_count, omap_mcbsp_cache_size;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030035
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070036static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030037{
Jarkko Nikulacdc715142011-09-26 10:45:39 +030038 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
39
40 if (mcbsp->pdata->reg_size == 2) {
41 ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
42 __raw_writew((u16)val, addr);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080043 } else {
Jarkko Nikulacdc715142011-09-26 10:45:39 +030044 ((u32 *)mcbsp->reg_cache)[reg] = val;
45 __raw_writel(val, addr);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080046 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030047}
48
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070049static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030050{
Jarkko Nikulacdc715142011-09-26 10:45:39 +030051 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
52
53 if (mcbsp->pdata->reg_size == 2) {
54 return !from_cache ? __raw_readw(addr) :
55 ((u16 *)mcbsp->reg_cache)[reg];
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080056 } else {
Jarkko Nikulacdc715142011-09-26 10:45:39 +030057 return !from_cache ? __raw_readl(addr) :
58 ((u32 *)mcbsp->reg_cache)[reg];
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080059 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030060}
61
Eero Nurkkalad912fa92010-02-22 12:21:11 +000062#ifdef CONFIG_ARCH_OMAP3
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070063static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000064{
65 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
66}
67
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070068static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000069{
70 return __raw_readl(mcbsp->st_data->io_base_st + reg);
71}
72#endif
73
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080074#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080075 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080076#define MCBSP_WRITE(mcbsp, reg, val) \
77 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080078#define MCBSP_READ_CACHE(mcbsp, reg) \
79 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030080
Eero Nurkkalad912fa92010-02-22 12:21:11 +000081#define MCBSP_ST_READ(mcbsp, reg) \
82 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
83#define MCBSP_ST_WRITE(mcbsp, reg, val) \
84 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
85
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086static void omap_mcbsp_dump_reg(u8 id)
87{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030088 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
89
90 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
91 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080092 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030093 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080094 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030095 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080096 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030097 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080098 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030099 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800100 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300101 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800102 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300103 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800104 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300105 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800106 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300107 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800108 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300109 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800110 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300111 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800112 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300113 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800114 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300115 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800116 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300117 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118}
119
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700120static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400122 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700123 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800125 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700126 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700128 if (irqst_spcr2 & XSYNC_ERR) {
129 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
130 irqst_spcr2);
131 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000132 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700133 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300134
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135 return IRQ_HANDLED;
136}
137
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700138static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400140 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700141 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800143 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700144 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700146 if (irqst_spcr1 & RSYNC_ERR) {
147 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
148 irqst_spcr1);
149 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000150 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700151 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300152
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153 return IRQ_HANDLED;
154}
155
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100156/*
157 * omap_mcbsp_config simply write a config to the
158 * appropriate McBSP.
159 * You either call this function or set the McBSP registers
160 * by yourself before calling omap_mcbsp_start().
161 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300162void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300164 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100165
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300166 if (!omap_mcbsp_check_valid_id(id)) {
167 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
168 return;
169 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300170 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300171
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300172 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
173 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100174
175 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800176 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
177 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
178 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
179 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
180 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
181 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
182 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
183 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
184 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
185 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
186 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Syed Rafiuddina5b92cc2009-07-28 18:57:10 +0530187 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800188 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
189 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200190 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100191}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300192EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100193
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530194/**
195 * omap_mcbsp_dma_params - returns the dma channel number
196 * @id - mcbsp id
197 * @stream - indicates the direction of data flow (rx or tx)
198 *
199 * Returns the dma channel number for the rx channel or tx channel
200 * based on the value of @stream for the requested mcbsp given by @id
201 */
202int omap_mcbsp_dma_ch_params(unsigned int id, unsigned int stream)
203{
204 struct omap_mcbsp *mcbsp;
205
206 if (!omap_mcbsp_check_valid_id(id)) {
207 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
208 return -ENODEV;
209 }
210 mcbsp = id_to_mcbsp_ptr(id);
211
212 if (stream)
213 return mcbsp->dma_rx_sync;
214 else
215 return mcbsp->dma_tx_sync;
216}
217EXPORT_SYMBOL(omap_mcbsp_dma_ch_params);
218
219/**
220 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
221 * @id - mcbsp id
222 * @stream - indicates the direction of data flow (rx or tx)
223 *
224 * Returns the address of mcbsp data transmit register or data receive register
225 * to be used by DMA for transferring/receiving data based on the value of
226 * @stream for the requested mcbsp given by @id
227 */
228int omap_mcbsp_dma_reg_params(unsigned int id, unsigned int stream)
229{
230 struct omap_mcbsp *mcbsp;
231 int data_reg;
232
233 if (!omap_mcbsp_check_valid_id(id)) {
234 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
235 return -ENODEV;
236 }
237 mcbsp = id_to_mcbsp_ptr(id);
238
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300239 if (mcbsp->pdata->reg_size == 2) {
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530240 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300241 data_reg = OMAP_MCBSP_REG_DRR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530242 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300243 data_reg = OMAP_MCBSP_REG_DXR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530244 } else {
245 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300246 data_reg = OMAP_MCBSP_REG_DRR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530247 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300248 data_reg = OMAP_MCBSP_REG_DXR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530249 }
250
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300251 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530252}
253EXPORT_SYMBOL(omap_mcbsp_dma_reg_params);
254
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800255#ifdef CONFIG_ARCH_OMAP3
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000256static void omap_st_on(struct omap_mcbsp *mcbsp)
257{
258 unsigned int w;
259
260 /*
261 * Sidetone uses McBSP ICLK - which must not idle when sidetones
262 * are enabled or sidetones start sounding ugly.
263 */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700264 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000265 w &= ~(1 << (mcbsp->id - 2));
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700266 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000267
268 /* Enable McBSP Sidetone */
269 w = MCBSP_READ(mcbsp, SSELCR);
270 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
271
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000272 /* Enable Sidetone from Sidetone Core */
273 w = MCBSP_ST_READ(mcbsp, SSELCR);
274 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
275}
276
277static void omap_st_off(struct omap_mcbsp *mcbsp)
278{
279 unsigned int w;
280
281 w = MCBSP_ST_READ(mcbsp, SSELCR);
282 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
283
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000284 w = MCBSP_READ(mcbsp, SSELCR);
285 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
286
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700287 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000288 w |= 1 << (mcbsp->id - 2);
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700289 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000290}
291
292static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
293{
294 u16 val, i;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000295
296 val = MCBSP_ST_READ(mcbsp, SSELCR);
297
298 if (val & ST_COEFFWREN)
299 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
300
301 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
302
303 for (i = 0; i < 128; i++)
304 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
305
306 i = 0;
307
308 val = MCBSP_ST_READ(mcbsp, SSELCR);
309 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
310 val = MCBSP_ST_READ(mcbsp, SSELCR);
311
312 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
313
314 if (i == 1000)
315 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
316}
317
318static void omap_st_chgain(struct omap_mcbsp *mcbsp)
319{
320 u16 w;
321 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000322
323 w = MCBSP_ST_READ(mcbsp, SSELCR);
324
325 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
326 ST_CH1GAIN(st_data->ch1gain));
327}
328
329int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
330{
331 struct omap_mcbsp *mcbsp;
332 struct omap_mcbsp_st_data *st_data;
333 int ret = 0;
334
335 if (!omap_mcbsp_check_valid_id(id)) {
336 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
337 return -ENODEV;
338 }
339
340 mcbsp = id_to_mcbsp_ptr(id);
341 st_data = mcbsp->st_data;
342
343 if (!st_data)
344 return -ENOENT;
345
346 spin_lock_irq(&mcbsp->lock);
347 if (channel == 0)
348 st_data->ch0gain = chgain;
349 else if (channel == 1)
350 st_data->ch1gain = chgain;
351 else
352 ret = -EINVAL;
353
354 if (st_data->enabled)
355 omap_st_chgain(mcbsp);
356 spin_unlock_irq(&mcbsp->lock);
357
358 return ret;
359}
360EXPORT_SYMBOL(omap_st_set_chgain);
361
362int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
363{
364 struct omap_mcbsp *mcbsp;
365 struct omap_mcbsp_st_data *st_data;
366 int ret = 0;
367
368 if (!omap_mcbsp_check_valid_id(id)) {
369 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
370 return -ENODEV;
371 }
372
373 mcbsp = id_to_mcbsp_ptr(id);
374 st_data = mcbsp->st_data;
375
376 if (!st_data)
377 return -ENOENT;
378
379 spin_lock_irq(&mcbsp->lock);
380 if (channel == 0)
381 *chgain = st_data->ch0gain;
382 else if (channel == 1)
383 *chgain = st_data->ch1gain;
384 else
385 ret = -EINVAL;
386 spin_unlock_irq(&mcbsp->lock);
387
388 return ret;
389}
390EXPORT_SYMBOL(omap_st_get_chgain);
391
392static int omap_st_start(struct omap_mcbsp *mcbsp)
393{
394 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
395
396 if (st_data && st_data->enabled && !st_data->running) {
397 omap_st_fir_write(mcbsp, st_data->taps);
398 omap_st_chgain(mcbsp);
399
400 if (!mcbsp->free) {
401 omap_st_on(mcbsp);
402 st_data->running = 1;
403 }
404 }
405
406 return 0;
407}
408
409int omap_st_enable(unsigned int id)
410{
411 struct omap_mcbsp *mcbsp;
412 struct omap_mcbsp_st_data *st_data;
413
414 if (!omap_mcbsp_check_valid_id(id)) {
415 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
416 return -ENODEV;
417 }
418
419 mcbsp = id_to_mcbsp_ptr(id);
420 st_data = mcbsp->st_data;
421
422 if (!st_data)
423 return -ENODEV;
424
425 spin_lock_irq(&mcbsp->lock);
426 st_data->enabled = 1;
427 omap_st_start(mcbsp);
428 spin_unlock_irq(&mcbsp->lock);
429
430 return 0;
431}
432EXPORT_SYMBOL(omap_st_enable);
433
434static int omap_st_stop(struct omap_mcbsp *mcbsp)
435{
436 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
437
438 if (st_data && st_data->running) {
439 if (!mcbsp->free) {
440 omap_st_off(mcbsp);
441 st_data->running = 0;
442 }
443 }
444
445 return 0;
446}
447
448int omap_st_disable(unsigned int id)
449{
450 struct omap_mcbsp *mcbsp;
451 struct omap_mcbsp_st_data *st_data;
452 int ret = 0;
453
454 if (!omap_mcbsp_check_valid_id(id)) {
455 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
456 return -ENODEV;
457 }
458
459 mcbsp = id_to_mcbsp_ptr(id);
460 st_data = mcbsp->st_data;
461
462 if (!st_data)
463 return -ENODEV;
464
465 spin_lock_irq(&mcbsp->lock);
466 omap_st_stop(mcbsp);
467 st_data->enabled = 0;
468 spin_unlock_irq(&mcbsp->lock);
469
470 return ret;
471}
472EXPORT_SYMBOL(omap_st_disable);
473
474int omap_st_is_enabled(unsigned int id)
475{
476 struct omap_mcbsp *mcbsp;
477 struct omap_mcbsp_st_data *st_data;
478
479 if (!omap_mcbsp_check_valid_id(id)) {
480 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
481 return -ENODEV;
482 }
483
484 mcbsp = id_to_mcbsp_ptr(id);
485 st_data = mcbsp->st_data;
486
487 if (!st_data)
488 return -ENODEV;
489
490
491 return st_data->enabled;
492}
493EXPORT_SYMBOL(omap_st_is_enabled);
494
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300495/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300496 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
497 * The threshold parameter is 1 based, and it is converted (threshold - 1)
498 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300499 */
500void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
501{
502 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300503
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500504 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300505 return;
506
507 if (!omap_mcbsp_check_valid_id(id)) {
508 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
509 return;
510 }
511 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300512
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300513 if (threshold && threshold <= mcbsp->max_tx_thres)
514 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300515}
516EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
517
518/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300519 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
520 * The threshold parameter is 1 based, and it is converted (threshold - 1)
521 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300522 */
523void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
524{
525 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300526
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500527 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300528 return;
529
530 if (!omap_mcbsp_check_valid_id(id)) {
531 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
532 return;
533 }
534 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300535
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300536 if (threshold && threshold <= mcbsp->max_rx_thres)
537 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300538}
539EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +0300540
541/*
542 * omap_mcbsp_get_max_tx_thres just return the current configured
543 * maximum threshold for transmission
544 */
545u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
546{
547 struct omap_mcbsp *mcbsp;
548
549 if (!omap_mcbsp_check_valid_id(id)) {
550 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
551 return -ENODEV;
552 }
553 mcbsp = id_to_mcbsp_ptr(id);
554
555 return mcbsp->max_tx_thres;
556}
557EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
558
559/*
560 * omap_mcbsp_get_max_rx_thres just return the current configured
561 * maximum threshold for reception
562 */
563u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
564{
565 struct omap_mcbsp *mcbsp;
566
567 if (!omap_mcbsp_check_valid_id(id)) {
568 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
569 return -ENODEV;
570 }
571 mcbsp = id_to_mcbsp_ptr(id);
572
573 return mcbsp->max_rx_thres;
574}
575EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300576
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300577u16 omap_mcbsp_get_fifo_size(unsigned int id)
578{
579 struct omap_mcbsp *mcbsp;
580
581 if (!omap_mcbsp_check_valid_id(id)) {
582 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
583 return -ENODEV;
584 }
585 mcbsp = id_to_mcbsp_ptr(id);
586
587 return mcbsp->pdata->buffer_size;
588}
589EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
590
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200591/*
592 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
593 */
594u16 omap_mcbsp_get_tx_delay(unsigned int id)
595{
596 struct omap_mcbsp *mcbsp;
597 u16 buffstat;
598
599 if (!omap_mcbsp_check_valid_id(id)) {
600 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
601 return -ENODEV;
602 }
603 mcbsp = id_to_mcbsp_ptr(id);
604
605 /* Returns the number of free locations in the buffer */
606 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
607
608 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300609 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200610}
611EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
612
613/*
614 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
615 * to reach the threshold value (when the DMA will be triggered to read it)
616 */
617u16 omap_mcbsp_get_rx_delay(unsigned int id)
618{
619 struct omap_mcbsp *mcbsp;
620 u16 buffstat, threshold;
621
622 if (!omap_mcbsp_check_valid_id(id)) {
623 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
624 return -ENODEV;
625 }
626 mcbsp = id_to_mcbsp_ptr(id);
627
628 /* Returns the number of used locations in the buffer */
629 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
630 /* RX threshold */
631 threshold = MCBSP_READ(mcbsp, THRSH1);
632
633 /* Return the number of location till we reach the threshold limit */
634 if (threshold <= buffstat)
635 return 0;
636 else
637 return threshold - buffstat;
638}
639EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
640
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300641/*
642 * omap_mcbsp_get_dma_op_mode just return the current configured
643 * operating mode for the mcbsp channel
644 */
645int omap_mcbsp_get_dma_op_mode(unsigned int id)
646{
647 struct omap_mcbsp *mcbsp;
648 int dma_op_mode;
649
650 if (!omap_mcbsp_check_valid_id(id)) {
651 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
652 return -ENODEV;
653 }
654 mcbsp = id_to_mcbsp_ptr(id);
655
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300656 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300657
658 return dma_op_mode;
659}
660EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300661
662static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
663{
664 /*
665 * Enable wakup behavior, smart idle and all wakeups
666 * REVISIT: some wakeups may be unnecessary
667 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500668 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530669 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300670 }
671}
672
673static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
674{
675 /*
676 * Disable wakup behavior, smart idle and all wakeups
677 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500678 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300679 /*
680 * HW bug workaround - If no_idle mode is taken, we need to
681 * go to smart_idle before going to always_idle, or the
682 * device will not hit retention anymore.
683 */
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300684
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800685 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300686 }
687}
688#else
689static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
690static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000691static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
692static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300693#endif
694
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695int omap_mcbsp_request(unsigned int id)
696{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300697 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800698 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100699 int err;
700
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300701 if (!omap_mcbsp_check_valid_id(id)) {
702 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
703 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100704 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300705 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300706
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800707 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
708 if (!reg_cache) {
709 return -ENOMEM;
710 }
711
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300712 spin_lock(&mcbsp->lock);
713 if (!mcbsp->free) {
714 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
715 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800716 err = -EBUSY;
717 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718 }
719
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800720 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800721 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300722 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723
Russell Kingb820ce42009-01-23 10:26:46 +0000724 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
725 mcbsp->pdata->ops->request(id);
726
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530727 pm_runtime_get_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000728
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300729 /* Do procedure specific to omap34xx arch, if applicable */
730 omap34xx_mcbsp_request(mcbsp);
731
Jarkko Nikula5a070552008-10-08 10:01:41 +0300732 /*
733 * Make sure that transmitter, receiver and sample-rate generator are
734 * not running before activating IRQs.
735 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800736 MCBSP_WRITE(mcbsp, SPCR1, 0);
737 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300738
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000739 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
740 0, "McBSP", (void *)mcbsp);
741 if (err != 0) {
742 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
743 "for McBSP%d\n", mcbsp->tx_irq,
744 mcbsp->id);
745 goto err_clk_disable;
746 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100747
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000748 if (mcbsp->rx_irq) {
749 err = request_irq(mcbsp->rx_irq,
750 omap_mcbsp_rx_irq_handler,
751 0, "McBSP", (void *)mcbsp);
752 if (err != 0) {
753 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
754 "for McBSP%d\n", mcbsp->rx_irq,
755 mcbsp->id);
756 goto err_free_irq;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100757 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758 }
759
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800761err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800762 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800763err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800764 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800765 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800766
767 /* Do procedure specific to omap34xx arch, if applicable */
768 omap34xx_mcbsp_free(mcbsp);
769
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530770 pm_runtime_put_sync(mcbsp->dev);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800771
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800772 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800773 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800774 mcbsp->reg_cache = NULL;
775err_kfree:
776 spin_unlock(&mcbsp->lock);
777 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800778
779 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300781EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100782
783void omap_mcbsp_free(unsigned int id)
784{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300785 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800786 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300787
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300788 if (!omap_mcbsp_check_valid_id(id)) {
789 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100791 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300792 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100793
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300794 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
795 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300796
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300797 /* Do procedure specific to omap34xx arch, if applicable */
798 omap34xx_mcbsp_free(mcbsp);
799
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530800 pm_runtime_put_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000801
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000802 if (mcbsp->rx_irq)
803 free_irq(mcbsp->rx_irq, (void *)mcbsp);
804 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100805
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800806 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100807
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800808 spin_lock(&mcbsp->lock);
809 if (mcbsp->free)
810 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
811 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800812 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800813 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300814 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800815
816 if (reg_cache)
817 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100818}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300819EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100820
821/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300822 * Here we start the McBSP, by enabling transmitter, receiver or both.
823 * If no transmitter or receiver is active prior calling, then sample-rate
824 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100825 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300826void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300828 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000829 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100830 u16 w;
831
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300832 if (!omap_mcbsp_check_valid_id(id)) {
833 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300835 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300836 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100837
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000838 if (cpu_is_omap34xx())
839 omap_st_start(mcbsp);
840
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000841 /* Only enable SRG, if McBSP is master */
842 w = MCBSP_READ_CACHE(mcbsp, PCR0);
843 if (w & (FSXM | FSRM | CLKXM | CLKRM))
844 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
845 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300846
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000847 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300848 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800849 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800850 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300851 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100852
853 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300854 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800855 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800856 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100857
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300858 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800859 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800860 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861
Eduardo Valentin44a63112009-08-20 16:18:09 +0300862 /*
863 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
864 * REVISIT: 100us may give enough time for two CLKSRG, however
865 * due to some unknown PM related, clock gating etc. reason it
866 * is now at 500us.
867 */
868 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000870 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300871 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800872 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800873 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300874 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500876 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300877 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800878 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300879 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800880 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800881 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300882 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800883 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300884 }
885
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886 /* Dump McBSP Regs */
887 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300889EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100890
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300891void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100892{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300893 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300894 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100895 u16 w;
896
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300897 if (!omap_mcbsp_check_valid_id(id)) {
898 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100899 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300900 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100901
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300902 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100903
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300904 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300905 tx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500906 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800907 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300908 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800909 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300910 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800911 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800912 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913
914 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300915 rx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500916 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800917 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700918 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800919 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300920 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800921 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800922 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100923
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800924 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
925 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300926
927 if (idle) {
928 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800929 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800930 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300931 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000932
933 if (cpu_is_omap34xx())
934 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100935}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300936EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100937
Paul Walmsley69d042d2011-07-01 08:52:25 +0000938/*
939 * The following functions are only required on an OMAP1-only build.
940 * mach-omap2/mcbsp.c contains the real functions
941 */
942#ifndef CONFIG_ARCH_OMAP2PLUS
943int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
944{
945 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
946 __func__);
947 return -EINVAL;
948}
949
950void omap2_mcbsp1_mux_clkr_src(u8 mux)
951{
952 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
953 __func__);
954 return;
955}
956
957void omap2_mcbsp1_mux_fsr_src(u8 mux)
958{
959 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
960 __func__);
961 return;
962}
963#endif
964
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800965#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +0300966#define max_thres(m) (mcbsp->pdata->buffer_size)
967#define valid_threshold(m, val) ((val) <= max_thres(m))
968#define THRESHOLD_PROP_BUILDER(prop) \
969static ssize_t prop##_show(struct device *dev, \
970 struct device_attribute *attr, char *buf) \
971{ \
972 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
973 \
974 return sprintf(buf, "%u\n", mcbsp->prop); \
975} \
976 \
977static ssize_t prop##_store(struct device *dev, \
978 struct device_attribute *attr, \
979 const char *buf, size_t size) \
980{ \
981 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
982 unsigned long val; \
983 int status; \
984 \
985 status = strict_strtoul(buf, 0, &val); \
986 if (status) \
987 return status; \
988 \
989 if (!valid_threshold(mcbsp, val)) \
990 return -EDOM; \
991 \
992 mcbsp->prop = val; \
993 return size; \
994} \
995 \
996static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
997
998THRESHOLD_PROP_BUILDER(max_tx_thres);
999THRESHOLD_PROP_BUILDER(max_rx_thres);
1000
Jarkko Nikula9b300502009-08-24 17:45:50 +03001001static const char *dma_op_modes[] = {
1002 "element", "threshold", "frame",
1003};
1004
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001005static ssize_t dma_op_mode_show(struct device *dev,
1006 struct device_attribute *attr, char *buf)
1007{
1008 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001009 int dma_op_mode, i = 0;
1010 ssize_t len = 0;
1011 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001012
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001013 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001014
Jarkko Nikula9b300502009-08-24 17:45:50 +03001015 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1016 if (dma_op_mode == i)
1017 len += sprintf(buf + len, "[%s] ", *s);
1018 else
1019 len += sprintf(buf + len, "%s ", *s);
1020 }
1021 len += sprintf(buf + len, "\n");
1022
1023 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001024}
1025
1026static ssize_t dma_op_mode_store(struct device *dev,
1027 struct device_attribute *attr,
1028 const char *buf, size_t size)
1029{
1030 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001031 const char * const *s;
1032 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001033
Jarkko Nikula9b300502009-08-24 17:45:50 +03001034 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1035 if (sysfs_streq(buf, *s))
1036 break;
1037
1038 if (i == ARRAY_SIZE(dma_op_modes))
1039 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001040
1041 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001042 if (!mcbsp->free) {
1043 size = -EBUSY;
1044 goto unlock;
1045 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001046 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001047
1048unlock:
1049 spin_unlock_irq(&mcbsp->lock);
1050
1051 return size;
1052}
1053
1054static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1055
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001056static ssize_t st_taps_show(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1058{
1059 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1060 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1061 ssize_t status = 0;
1062 int i;
1063
1064 spin_lock_irq(&mcbsp->lock);
1065 for (i = 0; i < st_data->nr_taps; i++)
1066 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1067 st_data->taps[i]);
1068 if (i)
1069 status += sprintf(&buf[status], "\n");
1070 spin_unlock_irq(&mcbsp->lock);
1071
1072 return status;
1073}
1074
1075static ssize_t st_taps_store(struct device *dev,
1076 struct device_attribute *attr,
1077 const char *buf, size_t size)
1078{
1079 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1080 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1081 int val, tmp, status, i = 0;
1082
1083 spin_lock_irq(&mcbsp->lock);
1084 memset(st_data->taps, 0, sizeof(st_data->taps));
1085 st_data->nr_taps = 0;
1086
1087 do {
1088 status = sscanf(buf, "%d%n", &val, &tmp);
1089 if (status < 0 || status == 0) {
1090 size = -EINVAL;
1091 goto out;
1092 }
1093 if (val < -32768 || val > 32767) {
1094 size = -EINVAL;
1095 goto out;
1096 }
1097 st_data->taps[i++] = val;
1098 buf += tmp;
1099 if (*buf != ',')
1100 break;
1101 buf++;
1102 } while (1);
1103
1104 st_data->nr_taps = i;
1105
1106out:
1107 spin_unlock_irq(&mcbsp->lock);
1108
1109 return size;
1110}
1111
1112static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1113
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001114static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001115 &dev_attr_max_tx_thres.attr,
1116 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001117 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001118 NULL,
1119};
1120
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001121static const struct attribute_group additional_attr_group = {
1122 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001123};
1124
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001125static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001126{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001127 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001128}
1129
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001130static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001131{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001132 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001133}
1134
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001135static const struct attribute *sidetone_attrs[] = {
1136 &dev_attr_st_taps.attr,
1137 NULL,
1138};
1139
1140static const struct attribute_group sidetone_attr_group = {
1141 .attrs = (struct attribute **)sidetone_attrs,
1142};
1143
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -07001144static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001145{
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001146 struct platform_device *pdev;
1147 struct resource *res;
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001148 struct omap_mcbsp_st_data *st_data;
1149 int err;
1150
1151 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1152 if (!st_data) {
1153 err = -ENOMEM;
1154 goto err1;
1155 }
1156
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001157 pdev = container_of(mcbsp->dev, struct platform_device, dev);
1158
1159 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1160 st_data->io_base_st = ioremap(res->start, resource_size(res));
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001161 if (!st_data->io_base_st) {
1162 err = -ENOMEM;
1163 goto err2;
1164 }
1165
1166 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1167 if (err)
1168 goto err3;
1169
1170 mcbsp->st_data = st_data;
1171 return 0;
1172
1173err3:
1174 iounmap(st_data->io_base_st);
1175err2:
1176 kfree(st_data);
1177err1:
1178 return err;
1179
1180}
1181
1182static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1183{
1184 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1185
1186 if (st_data) {
1187 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1188 iounmap(st_data->io_base_st);
1189 kfree(st_data);
1190 }
1191}
1192
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001193static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1194{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001195 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001196 if (cpu_is_omap34xx()) {
Peter Ujfalusi451fd822010-06-03 07:39:33 +03001197 /*
1198 * Initially configure the maximum thresholds to a safe value.
1199 * The McBSP FIFO usage with these values should not go under
1200 * 16 locations.
1201 * If the whole FIFO without safety buffer is used, than there
1202 * is a possibility that the DMA will be not able to push the
1203 * new data on time, causing channel shifts in runtime.
1204 */
1205 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1206 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001207 /*
1208 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1209 * for mcbsp2 instances.
1210 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001211 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001212 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001213 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001214
1215 if (mcbsp->id == 2 || mcbsp->id == 3)
1216 if (omap_st_add(mcbsp))
1217 dev_warn(mcbsp->dev,
1218 "Unable to create sidetone controls\n");
1219
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001220 } else {
1221 mcbsp->max_tx_thres = -EINVAL;
1222 mcbsp->max_rx_thres = -EINVAL;
1223 }
1224}
1225
1226static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1227{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001228 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001229 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001230
1231 if (mcbsp->id == 2 || mcbsp->id == 3)
1232 omap_st_remove(mcbsp);
1233 }
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001234}
1235#else
1236static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1237static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001238#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001239
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001240/*
1241 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1242 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1243 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001244static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001245{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001246 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001247 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001248 int id = pdev->id - 1;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001249 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001250 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001251
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001252 if (!pdata) {
1253 dev_err(&pdev->dev, "McBSP device initialized without"
1254 "platform data\n");
1255 ret = -EINVAL;
1256 goto exit;
1257 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001258
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001259 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001260
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001261 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001262 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1263 ret = -EINVAL;
1264 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001265 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001266
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001267 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1268 if (!mcbsp) {
1269 ret = -ENOMEM;
1270 goto exit;
1271 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001272
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001273 spin_lock_init(&mcbsp->lock);
1274 mcbsp->id = id + 1;
Shubhrajyoti D6722a722010-12-07 16:25:41 -08001275 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001276
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001277 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1278 if (!res) {
1279 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1280 if (!res) {
1281 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1282 "resource\n", __func__, pdev->id);
1283 ret = -ENOMEM;
1284 goto exit;
1285 }
1286 }
1287 mcbsp->phys_base = res->start;
1288 omap_mcbsp_cache_size = resource_size(res);
1289 mcbsp->io_base = ioremap(res->start, resource_size(res));
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001290 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001291 ret = -ENOMEM;
1292 goto err_ioremap;
1293 }
1294
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001295 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1296 if (!res)
1297 mcbsp->phys_dma_base = mcbsp->phys_base;
1298 else
1299 mcbsp->phys_dma_base = res->start;
1300
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001301 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1302 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1303
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +05301304 /* From OMAP4 there will be a single irq line */
1305 if (mcbsp->tx_irq == -ENXIO)
1306 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1307
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001308 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1309 if (!res) {
1310 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1311 __func__, pdev->id);
1312 ret = -ENODEV;
1313 goto err_res;
1314 }
1315 mcbsp->dma_rx_sync = res->start;
1316
1317 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1318 if (!res) {
1319 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1320 __func__, pdev->id);
1321 ret = -ENODEV;
1322 goto err_res;
1323 }
1324 mcbsp->dma_tx_sync = res->start;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001325
Russell Kingb820ce42009-01-23 10:26:46 +00001326 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1327 if (IS_ERR(mcbsp->fclk)) {
1328 ret = PTR_ERR(mcbsp->fclk);
1329 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301330 goto err_res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001331 }
1332
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001333 mcbsp->pdata = pdata;
1334 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001335 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001336 platform_set_drvdata(pdev, mcbsp);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301337 pm_runtime_enable(mcbsp->dev);
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001338
1339 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1340 omap34xx_device_init(mcbsp);
1341
Russell Kingd592dd12008-09-04 14:25:42 +01001342 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001343
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001344err_res:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001345 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001346err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001347 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001348exit:
1349 return ret;
1350}
1351
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001352static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001353{
1354 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1355
1356 platform_set_drvdata(pdev, NULL);
1357 if (mcbsp) {
1358
1359 if (mcbsp->pdata && mcbsp->pdata->ops &&
1360 mcbsp->pdata->ops->free)
1361 mcbsp->pdata->ops->free(mcbsp->id);
1362
Eduardo Valentina1a56f5f2009-08-20 16:18:11 +03001363 omap34xx_device_exit(mcbsp);
1364
Russell Kingb820ce42009-01-23 10:26:46 +00001365 clk_put(mcbsp->fclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001366
Russell Kingd592dd12008-09-04 14:25:42 +01001367 iounmap(mcbsp->io_base);
Jarkko Nikula5f3b7282010-12-07 16:25:40 -08001368 kfree(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001369 }
1370
1371 return 0;
1372}
1373
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001374static struct platform_driver omap_mcbsp_driver = {
1375 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001376 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001377 .driver = {
1378 .name = "omap-mcbsp",
1379 },
1380};
1381
1382int __init omap_mcbsp_init(void)
1383{
1384 /* Register the McBSP driver */
1385 return platform_driver_register(&omap_mcbsp_driver);
1386}