blob: 6c62af108710c89fc6f36e814bbf36beec338fb8 [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 If36d01d2011-02-24 15:16:53 +053027#include <plat/omap_device.h>
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +053028#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029
Paul Walmsley59fb6592010-12-21 15:30:55 -070030/* XXX These "sideways" includes are a sign that something is wrong */
31#include "../mach-omap2/cm2xxx_3xxx.h"
Eero Nurkkalad912fa92010-02-22 12:21:11 +000032#include "../mach-omap2/cm-regbits-34xx.h"
33
Chandra Shekharb4b58f52008-10-08 10:01:39 +030034struct omap_mcbsp **mcbsp_ptr;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080035int omap_mcbsp_count, omap_mcbsp_cache_size;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030036
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070037static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030038{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080039 if (cpu_class_is_omap1()) {
40 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080041 __raw_writew((u16)val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080042 } else if (cpu_is_omap2420()) {
43 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
44 __raw_writew((u16)val, mcbsp->io_base + reg);
45 } else {
46 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080047 __raw_writel(val, mcbsp->io_base + reg);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080048 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030049}
50
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070051static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030052{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080053 if (cpu_class_is_omap1()) {
54 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
55 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
56 } else if (cpu_is_omap2420()) {
57 return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
58 ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
59 } else {
60 return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
61 ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
62 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030063}
64
Eero Nurkkalad912fa92010-02-22 12:21:11 +000065#ifdef CONFIG_ARCH_OMAP3
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070066static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000067{
68 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
69}
70
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070071static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000072{
73 return __raw_readl(mcbsp->st_data->io_base_st + reg);
74}
75#endif
76
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080077#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080078 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080079#define MCBSP_WRITE(mcbsp, reg, val) \
80 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080081#define MCBSP_READ_CACHE(mcbsp, reg) \
82 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030083
Eero Nurkkalad912fa92010-02-22 12:21:11 +000084#define MCBSP_ST_READ(mcbsp, reg) \
85 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
86#define MCBSP_ST_WRITE(mcbsp, reg, val) \
87 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
88
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089static void omap_mcbsp_dump_reg(u8 id)
90{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030091 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
92
93 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
94 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080095 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030096 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080097 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030098 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080099 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300100 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800101 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300102 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800103 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300104 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800105 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300106 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800107 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300108 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800109 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300110 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800111 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300112 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800113 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300114 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800115 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300116 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800117 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300118 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800119 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300120 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121}
122
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700123static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400125 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700126 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800128 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700129 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700131 if (irqst_spcr2 & XSYNC_ERR) {
132 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
133 irqst_spcr2);
134 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000135 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700136 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300137
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100138 return IRQ_HANDLED;
139}
140
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700141static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400143 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700144 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800146 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700147 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700149 if (irqst_spcr1 & RSYNC_ERR) {
150 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
151 irqst_spcr1);
152 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000153 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700154 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300155
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100156 return IRQ_HANDLED;
157}
158
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159/*
160 * omap_mcbsp_config simply write a config to the
161 * appropriate McBSP.
162 * You either call this function or set the McBSP registers
163 * by yourself before calling omap_mcbsp_start().
164 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300165void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100166{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300167 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300169 if (!omap_mcbsp_check_valid_id(id)) {
170 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
171 return;
172 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300173 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300174
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300175 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
176 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100177
178 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800179 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
180 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
181 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
182 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
183 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
184 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
185 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
186 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
187 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
188 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
189 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Syed Rafiuddina5b92cc2009-07-28 18:57:10 +0530190 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800191 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
192 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200193 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100194}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300195EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100196
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530197/**
198 * omap_mcbsp_dma_params - returns the dma channel number
199 * @id - mcbsp id
200 * @stream - indicates the direction of data flow (rx or tx)
201 *
202 * Returns the dma channel number for the rx channel or tx channel
203 * based on the value of @stream for the requested mcbsp given by @id
204 */
205int omap_mcbsp_dma_ch_params(unsigned int id, unsigned int stream)
206{
207 struct omap_mcbsp *mcbsp;
208
209 if (!omap_mcbsp_check_valid_id(id)) {
210 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
211 return -ENODEV;
212 }
213 mcbsp = id_to_mcbsp_ptr(id);
214
215 if (stream)
216 return mcbsp->dma_rx_sync;
217 else
218 return mcbsp->dma_tx_sync;
219}
220EXPORT_SYMBOL(omap_mcbsp_dma_ch_params);
221
222/**
223 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
224 * @id - mcbsp id
225 * @stream - indicates the direction of data flow (rx or tx)
226 *
227 * Returns the address of mcbsp data transmit register or data receive register
228 * to be used by DMA for transferring/receiving data based on the value of
229 * @stream for the requested mcbsp given by @id
230 */
231int omap_mcbsp_dma_reg_params(unsigned int id, unsigned int stream)
232{
233 struct omap_mcbsp *mcbsp;
234 int data_reg;
235
236 if (!omap_mcbsp_check_valid_id(id)) {
237 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
238 return -ENODEV;
239 }
240 mcbsp = id_to_mcbsp_ptr(id);
241
242 data_reg = mcbsp->phys_dma_base;
243
244 if (mcbsp->mcbsp_config_type < MCBSP_CONFIG_TYPE2) {
245 if (stream)
246 data_reg += OMAP_MCBSP_REG_DRR1;
247 else
248 data_reg += OMAP_MCBSP_REG_DXR1;
249 } else {
250 if (stream)
251 data_reg += OMAP_MCBSP_REG_DRR;
252 else
253 data_reg += OMAP_MCBSP_REG_DXR;
254 }
255
256 return data_reg;
257}
258EXPORT_SYMBOL(omap_mcbsp_dma_reg_params);
259
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800260#ifdef CONFIG_ARCH_OMAP3
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530261static struct omap_device *find_omap_device_by_dev(struct device *dev)
262{
263 struct platform_device *pdev = container_of(dev,
264 struct platform_device, dev);
265 return container_of(pdev, struct omap_device, pdev);
266}
267
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000268static void omap_st_on(struct omap_mcbsp *mcbsp)
269{
270 unsigned int w;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530271 struct omap_device *od;
272
273 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000274
275 /*
276 * Sidetone uses McBSP ICLK - which must not idle when sidetones
277 * are enabled or sidetones start sounding ugly.
278 */
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700279 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000280 w &= ~(1 << (mcbsp->id - 2));
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700281 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000282
283 /* Enable McBSP Sidetone */
284 w = MCBSP_READ(mcbsp, SSELCR);
285 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
286
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000287 /* Enable Sidetone from Sidetone Core */
288 w = MCBSP_ST_READ(mcbsp, SSELCR);
289 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
290}
291
292static void omap_st_off(struct omap_mcbsp *mcbsp)
293{
294 unsigned int w;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530295 struct omap_device *od;
296
297 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000298
299 w = MCBSP_ST_READ(mcbsp, SSELCR);
300 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
301
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000302 w = MCBSP_READ(mcbsp, SSELCR);
303 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
304
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700305 w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000306 w |= 1 << (mcbsp->id - 2);
Paul Walmsleyc4d7e582010-12-21 21:05:14 -0700307 omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000308}
309
310static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
311{
312 u16 val, i;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530313 struct omap_device *od;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000314
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530315 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000316
317 val = MCBSP_ST_READ(mcbsp, SSELCR);
318
319 if (val & ST_COEFFWREN)
320 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
321
322 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
323
324 for (i = 0; i < 128; i++)
325 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
326
327 i = 0;
328
329 val = MCBSP_ST_READ(mcbsp, SSELCR);
330 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
331 val = MCBSP_ST_READ(mcbsp, SSELCR);
332
333 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
334
335 if (i == 1000)
336 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
337}
338
339static void omap_st_chgain(struct omap_mcbsp *mcbsp)
340{
341 u16 w;
342 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530343 struct omap_device *od;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000344
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530345 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000346
347 w = MCBSP_ST_READ(mcbsp, SSELCR);
348
349 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
350 ST_CH1GAIN(st_data->ch1gain));
351}
352
353int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
354{
355 struct omap_mcbsp *mcbsp;
356 struct omap_mcbsp_st_data *st_data;
357 int ret = 0;
358
359 if (!omap_mcbsp_check_valid_id(id)) {
360 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
361 return -ENODEV;
362 }
363
364 mcbsp = id_to_mcbsp_ptr(id);
365 st_data = mcbsp->st_data;
366
367 if (!st_data)
368 return -ENOENT;
369
370 spin_lock_irq(&mcbsp->lock);
371 if (channel == 0)
372 st_data->ch0gain = chgain;
373 else if (channel == 1)
374 st_data->ch1gain = chgain;
375 else
376 ret = -EINVAL;
377
378 if (st_data->enabled)
379 omap_st_chgain(mcbsp);
380 spin_unlock_irq(&mcbsp->lock);
381
382 return ret;
383}
384EXPORT_SYMBOL(omap_st_set_chgain);
385
386int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
387{
388 struct omap_mcbsp *mcbsp;
389 struct omap_mcbsp_st_data *st_data;
390 int ret = 0;
391
392 if (!omap_mcbsp_check_valid_id(id)) {
393 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
394 return -ENODEV;
395 }
396
397 mcbsp = id_to_mcbsp_ptr(id);
398 st_data = mcbsp->st_data;
399
400 if (!st_data)
401 return -ENOENT;
402
403 spin_lock_irq(&mcbsp->lock);
404 if (channel == 0)
405 *chgain = st_data->ch0gain;
406 else if (channel == 1)
407 *chgain = st_data->ch1gain;
408 else
409 ret = -EINVAL;
410 spin_unlock_irq(&mcbsp->lock);
411
412 return ret;
413}
414EXPORT_SYMBOL(omap_st_get_chgain);
415
416static int omap_st_start(struct omap_mcbsp *mcbsp)
417{
418 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
419
420 if (st_data && st_data->enabled && !st_data->running) {
421 omap_st_fir_write(mcbsp, st_data->taps);
422 omap_st_chgain(mcbsp);
423
424 if (!mcbsp->free) {
425 omap_st_on(mcbsp);
426 st_data->running = 1;
427 }
428 }
429
430 return 0;
431}
432
433int omap_st_enable(unsigned int id)
434{
435 struct omap_mcbsp *mcbsp;
436 struct omap_mcbsp_st_data *st_data;
437
438 if (!omap_mcbsp_check_valid_id(id)) {
439 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
440 return -ENODEV;
441 }
442
443 mcbsp = id_to_mcbsp_ptr(id);
444 st_data = mcbsp->st_data;
445
446 if (!st_data)
447 return -ENODEV;
448
449 spin_lock_irq(&mcbsp->lock);
450 st_data->enabled = 1;
451 omap_st_start(mcbsp);
452 spin_unlock_irq(&mcbsp->lock);
453
454 return 0;
455}
456EXPORT_SYMBOL(omap_st_enable);
457
458static int omap_st_stop(struct omap_mcbsp *mcbsp)
459{
460 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
461
462 if (st_data && st_data->running) {
463 if (!mcbsp->free) {
464 omap_st_off(mcbsp);
465 st_data->running = 0;
466 }
467 }
468
469 return 0;
470}
471
472int omap_st_disable(unsigned int id)
473{
474 struct omap_mcbsp *mcbsp;
475 struct omap_mcbsp_st_data *st_data;
476 int ret = 0;
477
478 if (!omap_mcbsp_check_valid_id(id)) {
479 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
480 return -ENODEV;
481 }
482
483 mcbsp = id_to_mcbsp_ptr(id);
484 st_data = mcbsp->st_data;
485
486 if (!st_data)
487 return -ENODEV;
488
489 spin_lock_irq(&mcbsp->lock);
490 omap_st_stop(mcbsp);
491 st_data->enabled = 0;
492 spin_unlock_irq(&mcbsp->lock);
493
494 return ret;
495}
496EXPORT_SYMBOL(omap_st_disable);
497
498int omap_st_is_enabled(unsigned int id)
499{
500 struct omap_mcbsp *mcbsp;
501 struct omap_mcbsp_st_data *st_data;
502
503 if (!omap_mcbsp_check_valid_id(id)) {
504 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
505 return -ENODEV;
506 }
507
508 mcbsp = id_to_mcbsp_ptr(id);
509 st_data = mcbsp->st_data;
510
511 if (!st_data)
512 return -ENODEV;
513
514
515 return st_data->enabled;
516}
517EXPORT_SYMBOL(omap_st_is_enabled);
518
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300519/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300520 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
521 * The threshold parameter is 1 based, and it is converted (threshold - 1)
522 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300523 */
524void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
525{
526 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300527
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500528 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300529 return;
530
531 if (!omap_mcbsp_check_valid_id(id)) {
532 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
533 return;
534 }
535 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300536
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300537 if (threshold && threshold <= mcbsp->max_tx_thres)
538 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300539}
540EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
541
542/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300543 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
544 * The threshold parameter is 1 based, and it is converted (threshold - 1)
545 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300546 */
547void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
548{
549 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300550
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500551 if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300552 return;
553
554 if (!omap_mcbsp_check_valid_id(id)) {
555 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
556 return;
557 }
558 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300559
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300560 if (threshold && threshold <= mcbsp->max_rx_thres)
561 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300562}
563EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300564
565/*
566 * omap_mcbsp_get_max_tx_thres just return the current configured
567 * maximum threshold for transmission
568 */
569u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
570{
571 struct omap_mcbsp *mcbsp;
572
573 if (!omap_mcbsp_check_valid_id(id)) {
574 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
575 return -ENODEV;
576 }
577 mcbsp = id_to_mcbsp_ptr(id);
578
579 return mcbsp->max_tx_thres;
580}
581EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
582
583/*
584 * omap_mcbsp_get_max_rx_thres just return the current configured
585 * maximum threshold for reception
586 */
587u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
588{
589 struct omap_mcbsp *mcbsp;
590
591 if (!omap_mcbsp_check_valid_id(id)) {
592 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
593 return -ENODEV;
594 }
595 mcbsp = id_to_mcbsp_ptr(id);
596
597 return mcbsp->max_rx_thres;
598}
599EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300600
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300601u16 omap_mcbsp_get_fifo_size(unsigned int id)
602{
603 struct omap_mcbsp *mcbsp;
604
605 if (!omap_mcbsp_check_valid_id(id)) {
606 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
607 return -ENODEV;
608 }
609 mcbsp = id_to_mcbsp_ptr(id);
610
611 return mcbsp->pdata->buffer_size;
612}
613EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
614
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200615/*
616 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
617 */
618u16 omap_mcbsp_get_tx_delay(unsigned int id)
619{
620 struct omap_mcbsp *mcbsp;
621 u16 buffstat;
622
623 if (!omap_mcbsp_check_valid_id(id)) {
624 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
625 return -ENODEV;
626 }
627 mcbsp = id_to_mcbsp_ptr(id);
628
629 /* Returns the number of free locations in the buffer */
630 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
631
632 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300633 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200634}
635EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
636
637/*
638 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
639 * to reach the threshold value (when the DMA will be triggered to read it)
640 */
641u16 omap_mcbsp_get_rx_delay(unsigned int id)
642{
643 struct omap_mcbsp *mcbsp;
644 u16 buffstat, threshold;
645
646 if (!omap_mcbsp_check_valid_id(id)) {
647 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
648 return -ENODEV;
649 }
650 mcbsp = id_to_mcbsp_ptr(id);
651
652 /* Returns the number of used locations in the buffer */
653 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
654 /* RX threshold */
655 threshold = MCBSP_READ(mcbsp, THRSH1);
656
657 /* Return the number of location till we reach the threshold limit */
658 if (threshold <= buffstat)
659 return 0;
660 else
661 return threshold - buffstat;
662}
663EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
664
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300665/*
666 * omap_mcbsp_get_dma_op_mode just return the current configured
667 * operating mode for the mcbsp channel
668 */
669int omap_mcbsp_get_dma_op_mode(unsigned int id)
670{
671 struct omap_mcbsp *mcbsp;
672 int dma_op_mode;
673
674 if (!omap_mcbsp_check_valid_id(id)) {
675 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
676 return -ENODEV;
677 }
678 mcbsp = id_to_mcbsp_ptr(id);
679
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300680 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300681
682 return dma_op_mode;
683}
684EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300685
686static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
687{
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530688 struct omap_device *od;
689
690 od = find_omap_device_by_dev(mcbsp->dev);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300691 /*
692 * Enable wakup behavior, smart idle and all wakeups
693 * REVISIT: some wakeups may be unnecessary
694 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500695 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530696 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300697 }
698}
699
700static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
701{
Kishon Vijay Abraham If36d01d2011-02-24 15:16:53 +0530702 struct omap_device *od;
703
704 od = find_omap_device_by_dev(mcbsp->dev);
705
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300706 /*
707 * Disable wakup behavior, smart idle and all wakeups
708 */
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500709 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Eero Nurkkala72cc6d72009-08-20 16:18:20 +0300710 /*
711 * HW bug workaround - If no_idle mode is taken, we need to
712 * go to smart_idle before going to always_idle, or the
713 * device will not hit retention anymore.
714 */
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300715
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800716 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300717 }
718}
719#else
720static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
721static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000722static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
723static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300724#endif
725
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726int omap_mcbsp_request(unsigned int id)
727{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300728 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800729 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730 int err;
731
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300732 if (!omap_mcbsp_check_valid_id(id)) {
733 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
734 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100735 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300736 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300737
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800738 reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
739 if (!reg_cache) {
740 return -ENOMEM;
741 }
742
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 spin_lock(&mcbsp->lock);
744 if (!mcbsp->free) {
745 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
746 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800747 err = -EBUSY;
748 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100749 }
750
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800751 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800752 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300753 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100754
Russell Kingb820ce42009-01-23 10:26:46 +0000755 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
756 mcbsp->pdata->ops->request(id);
757
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530758 pm_runtime_get_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000759
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300760 /* Do procedure specific to omap34xx arch, if applicable */
761 omap34xx_mcbsp_request(mcbsp);
762
Jarkko Nikula5a070552008-10-08 10:01:41 +0300763 /*
764 * Make sure that transmitter, receiver and sample-rate generator are
765 * not running before activating IRQs.
766 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800767 MCBSP_WRITE(mcbsp, SPCR1, 0);
768 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300769
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000770 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
771 0, "McBSP", (void *)mcbsp);
772 if (err != 0) {
773 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
774 "for McBSP%d\n", mcbsp->tx_irq,
775 mcbsp->id);
776 goto err_clk_disable;
777 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100778
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000779 if (mcbsp->rx_irq) {
780 err = request_irq(mcbsp->rx_irq,
781 omap_mcbsp_rx_irq_handler,
782 0, "McBSP", (void *)mcbsp);
783 if (err != 0) {
784 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
785 "for McBSP%d\n", mcbsp->rx_irq,
786 mcbsp->id);
787 goto err_free_irq;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100788 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789 }
790
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100791 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800792err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800793 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800794err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800795 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800796 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800797
798 /* Do procedure specific to omap34xx arch, if applicable */
799 omap34xx_mcbsp_free(mcbsp);
800
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530801 pm_runtime_put_sync(mcbsp->dev);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800802
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800803 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800804 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800805 mcbsp->reg_cache = NULL;
806err_kfree:
807 spin_unlock(&mcbsp->lock);
808 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800809
810 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100811}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300812EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100813
814void omap_mcbsp_free(unsigned int id)
815{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300816 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800817 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300818
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300819 if (!omap_mcbsp_check_valid_id(id)) {
820 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100821 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100822 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300823 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100824
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300825 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
826 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300827
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300828 /* Do procedure specific to omap34xx arch, if applicable */
829 omap34xx_mcbsp_free(mcbsp);
830
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530831 pm_runtime_put_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000832
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000833 if (mcbsp->rx_irq)
834 free_irq(mcbsp->rx_irq, (void *)mcbsp);
835 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800837 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100838
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800839 spin_lock(&mcbsp->lock);
840 if (mcbsp->free)
841 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
842 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800843 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800844 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300845 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800846
847 if (reg_cache)
848 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100849}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300850EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100851
852/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300853 * Here we start the McBSP, by enabling transmitter, receiver or both.
854 * If no transmitter or receiver is active prior calling, then sample-rate
855 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300857void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300859 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000860 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861 u16 w;
862
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300863 if (!omap_mcbsp_check_valid_id(id)) {
864 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100865 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300866 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300867 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100868
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000869 if (cpu_is_omap34xx())
870 omap_st_start(mcbsp);
871
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000872 /* Only enable SRG, if McBSP is master */
873 w = MCBSP_READ_CACHE(mcbsp, PCR0);
874 if (w & (FSXM | FSRM | CLKXM | CLKRM))
875 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
876 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300877
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000878 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300879 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800880 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800881 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300882 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100883
884 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300885 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800886 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800887 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300889 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800890 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800891 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100892
Eduardo Valentin44a63112009-08-20 16:18:09 +0300893 /*
894 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
895 * REVISIT: 100us may give enough time for two CLKSRG, however
896 * due to some unknown PM related, clock gating etc. reason it
897 * is now at 500us.
898 */
899 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100900
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000901 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300902 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800903 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800904 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300905 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100906
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500907 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300908 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800909 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300910 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800911 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800912 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300913 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800914 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300915 }
916
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100917 /* Dump McBSP Regs */
918 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100919}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300920EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300922void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100923{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300924 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300925 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100926 u16 w;
927
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300928 if (!omap_mcbsp_check_valid_id(id)) {
929 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100930 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300931 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100932
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300933 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100934
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300935 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300936 tx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500937 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800938 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300939 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800940 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300941 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800942 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800943 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100944
945 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300946 rx &= 1;
Jorge Eduardo Candelaria752ec2f2010-05-12 12:18:40 -0500947 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800948 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700949 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800950 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300951 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800952 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800953 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100954
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800955 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
956 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300957
958 if (idle) {
959 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800960 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800961 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300962 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000963
964 if (cpu_is_omap34xx())
965 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100966}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300967EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100968
Paul Walmsley69d042d2011-07-01 08:52:25 +0000969/*
970 * The following functions are only required on an OMAP1-only build.
971 * mach-omap2/mcbsp.c contains the real functions
972 */
973#ifndef CONFIG_ARCH_OMAP2PLUS
974int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
975{
976 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
977 __func__);
978 return -EINVAL;
979}
980
981void omap2_mcbsp1_mux_clkr_src(u8 mux)
982{
983 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
984 __func__);
985 return;
986}
987
988void omap2_mcbsp1_mux_fsr_src(u8 mux)
989{
990 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
991 __func__);
992 return;
993}
994#endif
995
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800996#ifdef CONFIG_ARCH_OMAP3
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300997#define max_thres(m) (mcbsp->pdata->buffer_size)
998#define valid_threshold(m, val) ((val) <= max_thres(m))
999#define THRESHOLD_PROP_BUILDER(prop) \
1000static ssize_t prop##_show(struct device *dev, \
1001 struct device_attribute *attr, char *buf) \
1002{ \
1003 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1004 \
1005 return sprintf(buf, "%u\n", mcbsp->prop); \
1006} \
1007 \
1008static ssize_t prop##_store(struct device *dev, \
1009 struct device_attribute *attr, \
1010 const char *buf, size_t size) \
1011{ \
1012 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
1013 unsigned long val; \
1014 int status; \
1015 \
1016 status = strict_strtoul(buf, 0, &val); \
1017 if (status) \
1018 return status; \
1019 \
1020 if (!valid_threshold(mcbsp, val)) \
1021 return -EDOM; \
1022 \
1023 mcbsp->prop = val; \
1024 return size; \
1025} \
1026 \
1027static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
1028
1029THRESHOLD_PROP_BUILDER(max_tx_thres);
1030THRESHOLD_PROP_BUILDER(max_rx_thres);
1031
Jarkko Nikula9b300502009-08-24 17:45:50 +03001032static const char *dma_op_modes[] = {
1033 "element", "threshold", "frame",
1034};
1035
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001036static ssize_t dma_op_mode_show(struct device *dev,
1037 struct device_attribute *attr, char *buf)
1038{
1039 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001040 int dma_op_mode, i = 0;
1041 ssize_t len = 0;
1042 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001043
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001044 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001045
Jarkko Nikula9b300502009-08-24 17:45:50 +03001046 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
1047 if (dma_op_mode == i)
1048 len += sprintf(buf + len, "[%s] ", *s);
1049 else
1050 len += sprintf(buf + len, "%s ", *s);
1051 }
1052 len += sprintf(buf + len, "\n");
1053
1054 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001055}
1056
1057static ssize_t dma_op_mode_store(struct device *dev,
1058 struct device_attribute *attr,
1059 const char *buf, size_t size)
1060{
1061 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001062 const char * const *s;
1063 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001064
Jarkko Nikula9b300502009-08-24 17:45:50 +03001065 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1066 if (sysfs_streq(buf, *s))
1067 break;
1068
1069 if (i == ARRAY_SIZE(dma_op_modes))
1070 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001071
1072 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001073 if (!mcbsp->free) {
1074 size = -EBUSY;
1075 goto unlock;
1076 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001077 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001078
1079unlock:
1080 spin_unlock_irq(&mcbsp->lock);
1081
1082 return size;
1083}
1084
1085static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1086
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001087static ssize_t st_taps_show(struct device *dev,
1088 struct device_attribute *attr, char *buf)
1089{
1090 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1091 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1092 ssize_t status = 0;
1093 int i;
1094
1095 spin_lock_irq(&mcbsp->lock);
1096 for (i = 0; i < st_data->nr_taps; i++)
1097 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1098 st_data->taps[i]);
1099 if (i)
1100 status += sprintf(&buf[status], "\n");
1101 spin_unlock_irq(&mcbsp->lock);
1102
1103 return status;
1104}
1105
1106static ssize_t st_taps_store(struct device *dev,
1107 struct device_attribute *attr,
1108 const char *buf, size_t size)
1109{
1110 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1111 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1112 int val, tmp, status, i = 0;
1113
1114 spin_lock_irq(&mcbsp->lock);
1115 memset(st_data->taps, 0, sizeof(st_data->taps));
1116 st_data->nr_taps = 0;
1117
1118 do {
1119 status = sscanf(buf, "%d%n", &val, &tmp);
1120 if (status < 0 || status == 0) {
1121 size = -EINVAL;
1122 goto out;
1123 }
1124 if (val < -32768 || val > 32767) {
1125 size = -EINVAL;
1126 goto out;
1127 }
1128 st_data->taps[i++] = val;
1129 buf += tmp;
1130 if (*buf != ',')
1131 break;
1132 buf++;
1133 } while (1);
1134
1135 st_data->nr_taps = i;
1136
1137out:
1138 spin_unlock_irq(&mcbsp->lock);
1139
1140 return size;
1141}
1142
1143static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1144
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001145static const struct attribute *additional_attrs[] = {
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001146 &dev_attr_max_tx_thres.attr,
1147 &dev_attr_max_rx_thres.attr,
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001148 &dev_attr_dma_op_mode.attr,
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001149 NULL,
1150};
1151
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001152static const struct attribute_group additional_attr_group = {
1153 .attrs = (struct attribute **)additional_attrs,
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001154};
1155
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001156static inline int __devinit omap_additional_add(struct device *dev)
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001157{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001158 return sysfs_create_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001159}
1160
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001161static inline void __devexit omap_additional_remove(struct device *dev)
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001162{
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001163 sysfs_remove_group(&dev->kobj, &additional_attr_group);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001164}
1165
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001166static const struct attribute *sidetone_attrs[] = {
1167 &dev_attr_st_taps.attr,
1168 NULL,
1169};
1170
1171static const struct attribute_group sidetone_attr_group = {
1172 .attrs = (struct attribute **)sidetone_attrs,
1173};
1174
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -07001175static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001176{
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001177 struct platform_device *pdev;
1178 struct resource *res;
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001179 struct omap_mcbsp_st_data *st_data;
1180 int err;
1181
1182 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1183 if (!st_data) {
1184 err = -ENOMEM;
1185 goto err1;
1186 }
1187
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001188 pdev = container_of(mcbsp->dev, struct platform_device, dev);
1189
1190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1191 st_data->io_base_st = ioremap(res->start, resource_size(res));
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001192 if (!st_data->io_base_st) {
1193 err = -ENOMEM;
1194 goto err2;
1195 }
1196
1197 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1198 if (err)
1199 goto err3;
1200
1201 mcbsp->st_data = st_data;
1202 return 0;
1203
1204err3:
1205 iounmap(st_data->io_base_st);
1206err2:
1207 kfree(st_data);
1208err1:
1209 return err;
1210
1211}
1212
1213static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1214{
1215 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1216
1217 if (st_data) {
1218 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1219 iounmap(st_data->io_base_st);
1220 kfree(st_data);
1221 }
1222}
1223
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001224static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
1225{
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001226 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001227 if (cpu_is_omap34xx()) {
Peter Ujfalusi451fd822010-06-03 07:39:33 +03001228 /*
1229 * Initially configure the maximum thresholds to a safe value.
1230 * The McBSP FIFO usage with these values should not go under
1231 * 16 locations.
1232 * If the whole FIFO without safety buffer is used, than there
1233 * is a possibility that the DMA will be not able to push the
1234 * new data on time, causing channel shifts in runtime.
1235 */
1236 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1237 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001238 /*
1239 * REVISIT: Set dmap_op_mode to THRESHOLD as default
1240 * for mcbsp2 instances.
1241 */
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001242 if (omap_additional_add(mcbsp->dev))
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001243 dev_warn(mcbsp->dev,
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001244 "Unable to create additional controls\n");
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001245
1246 if (mcbsp->id == 2 || mcbsp->id == 3)
1247 if (omap_st_add(mcbsp))
1248 dev_warn(mcbsp->dev,
1249 "Unable to create sidetone controls\n");
1250
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001251 } else {
1252 mcbsp->max_tx_thres = -EINVAL;
1253 mcbsp->max_rx_thres = -EINVAL;
1254 }
1255}
1256
1257static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
1258{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001259 if (cpu_is_omap34xx()) {
Eduardo Valentin4c8200a2009-08-20 16:18:13 +03001260 omap_additional_remove(mcbsp->dev);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001261
1262 if (mcbsp->id == 2 || mcbsp->id == 3)
1263 omap_st_remove(mcbsp);
1264 }
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001265}
1266#else
1267static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
1268static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001269#endif /* CONFIG_ARCH_OMAP3 */
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001270
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001271/*
1272 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1273 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1274 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001275static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001276{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001277 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001278 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001279 int id = pdev->id - 1;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001280 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001281 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001282
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001283 if (!pdata) {
1284 dev_err(&pdev->dev, "McBSP device initialized without"
1285 "platform data\n");
1286 ret = -EINVAL;
1287 goto exit;
1288 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001289
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001290 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001291
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001292 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001293 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1294 ret = -EINVAL;
1295 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001296 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001297
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001298 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1299 if (!mcbsp) {
1300 ret = -ENOMEM;
1301 goto exit;
1302 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001303
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001304 spin_lock_init(&mcbsp->lock);
1305 mcbsp->id = id + 1;
Shubhrajyoti D6722a722010-12-07 16:25:41 -08001306 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001307
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001308 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1309 if (!res) {
1310 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1311 if (!res) {
1312 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1313 "resource\n", __func__, pdev->id);
1314 ret = -ENOMEM;
1315 goto exit;
1316 }
1317 }
1318 mcbsp->phys_base = res->start;
1319 omap_mcbsp_cache_size = resource_size(res);
1320 mcbsp->io_base = ioremap(res->start, resource_size(res));
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001321 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001322 ret = -ENOMEM;
1323 goto err_ioremap;
1324 }
1325
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001326 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1327 if (!res)
1328 mcbsp->phys_dma_base = mcbsp->phys_base;
1329 else
1330 mcbsp->phys_dma_base = res->start;
1331
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001332 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1333 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1334
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +05301335 /* From OMAP4 there will be a single irq line */
1336 if (mcbsp->tx_irq == -ENXIO)
1337 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1338
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001339 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1340 if (!res) {
1341 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1342 __func__, pdev->id);
1343 ret = -ENODEV;
1344 goto err_res;
1345 }
1346 mcbsp->dma_rx_sync = res->start;
1347
1348 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1349 if (!res) {
1350 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1351 __func__, pdev->id);
1352 ret = -ENODEV;
1353 goto err_res;
1354 }
1355 mcbsp->dma_tx_sync = res->start;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001356
Russell Kingb820ce42009-01-23 10:26:46 +00001357 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1358 if (IS_ERR(mcbsp->fclk)) {
1359 ret = PTR_ERR(mcbsp->fclk);
1360 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301361 goto err_res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001362 }
1363
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001364 mcbsp->pdata = pdata;
1365 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001366 mcbsp_ptr[id] = mcbsp;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +05301367 mcbsp->mcbsp_config_type = pdata->mcbsp_config_type;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001368 platform_set_drvdata(pdev, mcbsp);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301369 pm_runtime_enable(mcbsp->dev);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001370
1371 /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
1372 omap34xx_device_init(mcbsp);
1373
Russell Kingd592dd12008-09-04 14:25:42 +01001374 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001375
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001376err_res:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001377 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001378err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001379 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001380exit:
1381 return ret;
1382}
1383
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001384static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001385{
1386 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1387
1388 platform_set_drvdata(pdev, NULL);
1389 if (mcbsp) {
1390
1391 if (mcbsp->pdata && mcbsp->pdata->ops &&
1392 mcbsp->pdata->ops->free)
1393 mcbsp->pdata->ops->free(mcbsp->id);
1394
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001395 omap34xx_device_exit(mcbsp);
1396
Russell Kingb820ce42009-01-23 10:26:46 +00001397 clk_put(mcbsp->fclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001398
Russell Kingd592dd12008-09-04 14:25:42 +01001399 iounmap(mcbsp->io_base);
Jarkko Nikula5f3b7282010-12-07 16:25:40 -08001400 kfree(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001401 }
1402
1403 return 0;
1404}
1405
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001406static struct platform_driver omap_mcbsp_driver = {
1407 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001408 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001409 .driver = {
1410 .name = "omap-mcbsp",
1411 },
1412};
1413
1414int __init omap_mcbsp_init(void)
1415{
1416 /* Register the McBSP driver */
1417 return platform_driver_register(&omap_mcbsp_driver);
1418}