blob: 38b67d9be5b3118ac54144a6606e7fe373b05988 [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
Chandra Shekharb4b58f52008-10-08 10:01:39 +030029struct omap_mcbsp **mcbsp_ptr;
Jarkko Nikulaac6747c2011-09-26 10:45:43 +030030int omap_mcbsp_count;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030031
Jarkko Nikula09d28d22011-09-26 10:45:48 +030032#define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
33#define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
34
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070035static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030036{
Jarkko Nikulacdc715142011-09-26 10:45:39 +030037 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
38
39 if (mcbsp->pdata->reg_size == 2) {
40 ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
41 __raw_writew((u16)val, addr);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080042 } else {
Jarkko Nikulacdc715142011-09-26 10:45:39 +030043 ((u32 *)mcbsp->reg_cache)[reg] = val;
44 __raw_writel(val, addr);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080045 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030046}
47
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070048static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030049{
Jarkko Nikulacdc715142011-09-26 10:45:39 +030050 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
51
52 if (mcbsp->pdata->reg_size == 2) {
53 return !from_cache ? __raw_readw(addr) :
54 ((u16 *)mcbsp->reg_cache)[reg];
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080055 } else {
Jarkko Nikulacdc715142011-09-26 10:45:39 +030056 return !from_cache ? __raw_readl(addr) :
57 ((u32 *)mcbsp->reg_cache)[reg];
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080058 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030059}
60
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070061static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000062{
63 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
64}
65
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070066static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000067{
68 return __raw_readl(mcbsp->st_data->io_base_st + reg);
69}
Eero Nurkkalad912fa92010-02-22 12:21:11 +000070
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080071#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080072 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080073#define MCBSP_WRITE(mcbsp, reg, val) \
74 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080075#define MCBSP_READ_CACHE(mcbsp, reg) \
76 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030077
Eero Nurkkalad912fa92010-02-22 12:21:11 +000078#define MCBSP_ST_READ(mcbsp, reg) \
79 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
80#define MCBSP_ST_WRITE(mcbsp, reg, val) \
81 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
82
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010083static void omap_mcbsp_dump_reg(u8 id)
84{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030085 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
86
87 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
88 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080089 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030090 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080091 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030092 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080093 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030094 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080095 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030096 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080097 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030098 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080099 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300100 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800101 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300102 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800103 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300104 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800105 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300106 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800107 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300108 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800109 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300110 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800111 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300112 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800113 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300114 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115}
116
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700117static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400119 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700120 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800122 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700123 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700125 if (irqst_spcr2 & XSYNC_ERR) {
126 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
127 irqst_spcr2);
128 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000129 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700130 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300131
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132 return IRQ_HANDLED;
133}
134
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700135static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400137 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700138 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800140 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700141 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700143 if (irqst_spcr1 & RSYNC_ERR) {
144 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
145 irqst_spcr1);
146 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000147 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700148 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300149
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150 return IRQ_HANDLED;
151}
152
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153/*
154 * omap_mcbsp_config simply write a config to the
155 * appropriate McBSP.
156 * You either call this function or set the McBSP registers
157 * by yourself before calling omap_mcbsp_start().
158 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300159void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100160{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300161 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300163 if (!omap_mcbsp_check_valid_id(id)) {
164 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
165 return;
166 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300167 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300168
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300169 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
170 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171
172 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800173 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
174 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
175 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
176 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
177 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
178 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
179 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
180 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
181 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
182 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
183 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Jarkko Nikula88408232011-09-26 10:45:41 +0300184 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800185 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
186 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200187 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100188}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300189EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100190
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530191/**
192 * omap_mcbsp_dma_params - returns the dma channel number
193 * @id - mcbsp id
194 * @stream - indicates the direction of data flow (rx or tx)
195 *
196 * Returns the dma channel number for the rx channel or tx channel
197 * based on the value of @stream for the requested mcbsp given by @id
198 */
199int omap_mcbsp_dma_ch_params(unsigned int id, unsigned int stream)
200{
201 struct omap_mcbsp *mcbsp;
202
203 if (!omap_mcbsp_check_valid_id(id)) {
204 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
205 return -ENODEV;
206 }
207 mcbsp = id_to_mcbsp_ptr(id);
208
209 if (stream)
210 return mcbsp->dma_rx_sync;
211 else
212 return mcbsp->dma_tx_sync;
213}
214EXPORT_SYMBOL(omap_mcbsp_dma_ch_params);
215
216/**
217 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
218 * @id - mcbsp id
219 * @stream - indicates the direction of data flow (rx or tx)
220 *
221 * Returns the address of mcbsp data transmit register or data receive register
222 * to be used by DMA for transferring/receiving data based on the value of
223 * @stream for the requested mcbsp given by @id
224 */
225int omap_mcbsp_dma_reg_params(unsigned int id, unsigned int stream)
226{
227 struct omap_mcbsp *mcbsp;
228 int data_reg;
229
230 if (!omap_mcbsp_check_valid_id(id)) {
231 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
232 return -ENODEV;
233 }
234 mcbsp = id_to_mcbsp_ptr(id);
235
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300236 if (mcbsp->pdata->reg_size == 2) {
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530237 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300238 data_reg = OMAP_MCBSP_REG_DRR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530239 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300240 data_reg = OMAP_MCBSP_REG_DXR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530241 } else {
242 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300243 data_reg = OMAP_MCBSP_REG_DRR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530244 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300245 data_reg = OMAP_MCBSP_REG_DXR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530246 }
247
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300248 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530249}
250EXPORT_SYMBOL(omap_mcbsp_dma_reg_params);
251
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000252static void omap_st_on(struct omap_mcbsp *mcbsp)
253{
254 unsigned int w;
255
Jarkko Nikula1743d142011-09-26 10:45:44 +0300256 if (mcbsp->pdata->enable_st_clock)
257 mcbsp->pdata->enable_st_clock(mcbsp->id, 1);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000258
259 /* Enable McBSP Sidetone */
260 w = MCBSP_READ(mcbsp, SSELCR);
261 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
262
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000263 /* Enable Sidetone from Sidetone Core */
264 w = MCBSP_ST_READ(mcbsp, SSELCR);
265 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
266}
267
268static void omap_st_off(struct omap_mcbsp *mcbsp)
269{
270 unsigned int w;
271
272 w = MCBSP_ST_READ(mcbsp, SSELCR);
273 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
274
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000275 w = MCBSP_READ(mcbsp, SSELCR);
276 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
277
Jarkko Nikula1743d142011-09-26 10:45:44 +0300278 if (mcbsp->pdata->enable_st_clock)
279 mcbsp->pdata->enable_st_clock(mcbsp->id, 0);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000280}
281
282static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
283{
284 u16 val, i;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000285
286 val = MCBSP_ST_READ(mcbsp, SSELCR);
287
288 if (val & ST_COEFFWREN)
289 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
290
291 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
292
293 for (i = 0; i < 128; i++)
294 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
295
296 i = 0;
297
298 val = MCBSP_ST_READ(mcbsp, SSELCR);
299 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
300 val = MCBSP_ST_READ(mcbsp, SSELCR);
301
302 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
303
304 if (i == 1000)
305 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
306}
307
308static void omap_st_chgain(struct omap_mcbsp *mcbsp)
309{
310 u16 w;
311 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000312
313 w = MCBSP_ST_READ(mcbsp, SSELCR);
314
315 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
316 ST_CH1GAIN(st_data->ch1gain));
317}
318
319int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
320{
321 struct omap_mcbsp *mcbsp;
322 struct omap_mcbsp_st_data *st_data;
323 int ret = 0;
324
325 if (!omap_mcbsp_check_valid_id(id)) {
326 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
327 return -ENODEV;
328 }
329
330 mcbsp = id_to_mcbsp_ptr(id);
331 st_data = mcbsp->st_data;
332
333 if (!st_data)
334 return -ENOENT;
335
336 spin_lock_irq(&mcbsp->lock);
337 if (channel == 0)
338 st_data->ch0gain = chgain;
339 else if (channel == 1)
340 st_data->ch1gain = chgain;
341 else
342 ret = -EINVAL;
343
344 if (st_data->enabled)
345 omap_st_chgain(mcbsp);
346 spin_unlock_irq(&mcbsp->lock);
347
348 return ret;
349}
350EXPORT_SYMBOL(omap_st_set_chgain);
351
352int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
353{
354 struct omap_mcbsp *mcbsp;
355 struct omap_mcbsp_st_data *st_data;
356 int ret = 0;
357
358 if (!omap_mcbsp_check_valid_id(id)) {
359 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
360 return -ENODEV;
361 }
362
363 mcbsp = id_to_mcbsp_ptr(id);
364 st_data = mcbsp->st_data;
365
366 if (!st_data)
367 return -ENOENT;
368
369 spin_lock_irq(&mcbsp->lock);
370 if (channel == 0)
371 *chgain = st_data->ch0gain;
372 else if (channel == 1)
373 *chgain = st_data->ch1gain;
374 else
375 ret = -EINVAL;
376 spin_unlock_irq(&mcbsp->lock);
377
378 return ret;
379}
380EXPORT_SYMBOL(omap_st_get_chgain);
381
382static int omap_st_start(struct omap_mcbsp *mcbsp)
383{
384 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
385
386 if (st_data && st_data->enabled && !st_data->running) {
387 omap_st_fir_write(mcbsp, st_data->taps);
388 omap_st_chgain(mcbsp);
389
390 if (!mcbsp->free) {
391 omap_st_on(mcbsp);
392 st_data->running = 1;
393 }
394 }
395
396 return 0;
397}
398
399int omap_st_enable(unsigned int id)
400{
401 struct omap_mcbsp *mcbsp;
402 struct omap_mcbsp_st_data *st_data;
403
404 if (!omap_mcbsp_check_valid_id(id)) {
405 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
406 return -ENODEV;
407 }
408
409 mcbsp = id_to_mcbsp_ptr(id);
410 st_data = mcbsp->st_data;
411
412 if (!st_data)
413 return -ENODEV;
414
415 spin_lock_irq(&mcbsp->lock);
416 st_data->enabled = 1;
417 omap_st_start(mcbsp);
418 spin_unlock_irq(&mcbsp->lock);
419
420 return 0;
421}
422EXPORT_SYMBOL(omap_st_enable);
423
424static int omap_st_stop(struct omap_mcbsp *mcbsp)
425{
426 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
427
428 if (st_data && st_data->running) {
429 if (!mcbsp->free) {
430 omap_st_off(mcbsp);
431 st_data->running = 0;
432 }
433 }
434
435 return 0;
436}
437
438int omap_st_disable(unsigned int id)
439{
440 struct omap_mcbsp *mcbsp;
441 struct omap_mcbsp_st_data *st_data;
442 int ret = 0;
443
444 if (!omap_mcbsp_check_valid_id(id)) {
445 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
446 return -ENODEV;
447 }
448
449 mcbsp = id_to_mcbsp_ptr(id);
450 st_data = mcbsp->st_data;
451
452 if (!st_data)
453 return -ENODEV;
454
455 spin_lock_irq(&mcbsp->lock);
456 omap_st_stop(mcbsp);
457 st_data->enabled = 0;
458 spin_unlock_irq(&mcbsp->lock);
459
460 return ret;
461}
462EXPORT_SYMBOL(omap_st_disable);
463
464int omap_st_is_enabled(unsigned int id)
465{
466 struct omap_mcbsp *mcbsp;
467 struct omap_mcbsp_st_data *st_data;
468
469 if (!omap_mcbsp_check_valid_id(id)) {
470 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
471 return -ENODEV;
472 }
473
474 mcbsp = id_to_mcbsp_ptr(id);
475 st_data = mcbsp->st_data;
476
477 if (!st_data)
478 return -ENODEV;
479
480
481 return st_data->enabled;
482}
483EXPORT_SYMBOL(omap_st_is_enabled);
484
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300485/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300486 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
487 * The threshold parameter is 1 based, and it is converted (threshold - 1)
488 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300489 */
490void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
491{
492 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300493
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300494 if (!omap_mcbsp_check_valid_id(id)) {
495 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
496 return;
497 }
498 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300499 if (mcbsp->pdata->buffer_size == 0)
500 return;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300501
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300502 if (threshold && threshold <= mcbsp->max_tx_thres)
503 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300504}
505EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
506
507/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300508 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
509 * The threshold parameter is 1 based, and it is converted (threshold - 1)
510 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300511 */
512void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
513{
514 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300515
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300516 if (!omap_mcbsp_check_valid_id(id)) {
517 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
518 return;
519 }
520 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300521 if (mcbsp->pdata->buffer_size == 0)
522 return;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300523
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300524 if (threshold && threshold <= mcbsp->max_rx_thres)
525 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300526}
527EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300528
529/*
530 * omap_mcbsp_get_max_tx_thres just return the current configured
531 * maximum threshold for transmission
532 */
533u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
534{
535 struct omap_mcbsp *mcbsp;
536
537 if (!omap_mcbsp_check_valid_id(id)) {
538 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
539 return -ENODEV;
540 }
541 mcbsp = id_to_mcbsp_ptr(id);
542
543 return mcbsp->max_tx_thres;
544}
545EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
546
547/*
548 * omap_mcbsp_get_max_rx_thres just return the current configured
549 * maximum threshold for reception
550 */
551u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
552{
553 struct omap_mcbsp *mcbsp;
554
555 if (!omap_mcbsp_check_valid_id(id)) {
556 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
557 return -ENODEV;
558 }
559 mcbsp = id_to_mcbsp_ptr(id);
560
561 return mcbsp->max_rx_thres;
562}
563EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300564
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300565u16 omap_mcbsp_get_fifo_size(unsigned int id)
566{
567 struct omap_mcbsp *mcbsp;
568
569 if (!omap_mcbsp_check_valid_id(id)) {
570 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
571 return -ENODEV;
572 }
573 mcbsp = id_to_mcbsp_ptr(id);
574
575 return mcbsp->pdata->buffer_size;
576}
577EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
578
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200579/*
580 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
581 */
582u16 omap_mcbsp_get_tx_delay(unsigned int id)
583{
584 struct omap_mcbsp *mcbsp;
585 u16 buffstat;
586
587 if (!omap_mcbsp_check_valid_id(id)) {
588 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
589 return -ENODEV;
590 }
591 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300592 if (mcbsp->pdata->buffer_size == 0)
593 return 0;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200594
595 /* Returns the number of free locations in the buffer */
596 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
597
598 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300599 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200600}
601EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
602
603/*
604 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
605 * to reach the threshold value (when the DMA will be triggered to read it)
606 */
607u16 omap_mcbsp_get_rx_delay(unsigned int id)
608{
609 struct omap_mcbsp *mcbsp;
610 u16 buffstat, threshold;
611
612 if (!omap_mcbsp_check_valid_id(id)) {
613 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
614 return -ENODEV;
615 }
616 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300617 if (mcbsp->pdata->buffer_size == 0)
618 return 0;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200619
620 /* Returns the number of used locations in the buffer */
621 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
622 /* RX threshold */
623 threshold = MCBSP_READ(mcbsp, THRSH1);
624
625 /* Return the number of location till we reach the threshold limit */
626 if (threshold <= buffstat)
627 return 0;
628 else
629 return threshold - buffstat;
630}
631EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
632
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300633/*
634 * omap_mcbsp_get_dma_op_mode just return the current configured
635 * operating mode for the mcbsp channel
636 */
637int omap_mcbsp_get_dma_op_mode(unsigned int id)
638{
639 struct omap_mcbsp *mcbsp;
640 int dma_op_mode;
641
642 if (!omap_mcbsp_check_valid_id(id)) {
643 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
644 return -ENODEV;
645 }
646 mcbsp = id_to_mcbsp_ptr(id);
647
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300648 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300649
650 return dma_op_mode;
651}
652EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300653
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100654int omap_mcbsp_request(unsigned int id)
655{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300656 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800657 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100658 int err;
659
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300660 if (!omap_mcbsp_check_valid_id(id)) {
661 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
662 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100663 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300664 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300665
Jarkko Nikulaac6747c2011-09-26 10:45:43 +0300666 reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800667 if (!reg_cache) {
668 return -ENOMEM;
669 }
670
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300671 spin_lock(&mcbsp->lock);
672 if (!mcbsp->free) {
673 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
674 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800675 err = -EBUSY;
676 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677 }
678
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800679 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800680 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300681 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100682
Russell Kingb820ce42009-01-23 10:26:46 +0000683 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
684 mcbsp->pdata->ops->request(id);
685
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530686 pm_runtime_get_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000687
Jarkko Nikula1a645882011-09-26 10:45:40 +0300688 /* Enable wakeup behavior */
689 if (mcbsp->pdata->has_wakeup)
690 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300691
Jarkko Nikula5a070552008-10-08 10:01:41 +0300692 /*
693 * Make sure that transmitter, receiver and sample-rate generator are
694 * not running before activating IRQs.
695 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800696 MCBSP_WRITE(mcbsp, SPCR1, 0);
697 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300698
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000699 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
700 0, "McBSP", (void *)mcbsp);
701 if (err != 0) {
702 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
703 "for McBSP%d\n", mcbsp->tx_irq,
704 mcbsp->id);
705 goto err_clk_disable;
706 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100707
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000708 if (mcbsp->rx_irq) {
709 err = request_irq(mcbsp->rx_irq,
710 omap_mcbsp_rx_irq_handler,
711 0, "McBSP", (void *)mcbsp);
712 if (err != 0) {
713 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
714 "for McBSP%d\n", mcbsp->rx_irq,
715 mcbsp->id);
716 goto err_free_irq;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100717 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718 }
719
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800721err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800722 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800723err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800724 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800725 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800726
Jarkko Nikula1a645882011-09-26 10:45:40 +0300727 /* Disable wakeup behavior */
728 if (mcbsp->pdata->has_wakeup)
729 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800730
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530731 pm_runtime_put_sync(mcbsp->dev);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800732
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800733 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800734 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800735 mcbsp->reg_cache = NULL;
736err_kfree:
737 spin_unlock(&mcbsp->lock);
738 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800739
740 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100741}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300742EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743
744void omap_mcbsp_free(unsigned int id)
745{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300746 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800747 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300748
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300749 if (!omap_mcbsp_check_valid_id(id)) {
750 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100751 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100752 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300753 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100754
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300755 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
756 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300757
Jarkko Nikula1a645882011-09-26 10:45:40 +0300758 /* Disable wakeup behavior */
759 if (mcbsp->pdata->has_wakeup)
760 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300761
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530762 pm_runtime_put_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000763
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000764 if (mcbsp->rx_irq)
765 free_irq(mcbsp->rx_irq, (void *)mcbsp);
766 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100767
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800768 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800770 spin_lock(&mcbsp->lock);
771 if (mcbsp->free)
772 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
773 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800774 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800775 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300776 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800777
778 if (reg_cache)
779 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300781EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100782
783/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300784 * Here we start the McBSP, by enabling transmitter, receiver or both.
785 * If no transmitter or receiver is active prior calling, then sample-rate
786 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100787 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300788void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300790 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000791 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792 u16 w;
793
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300794 if (!omap_mcbsp_check_valid_id(id)) {
795 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300797 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300798 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300800 if (mcbsp->st_data)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000801 omap_st_start(mcbsp);
802
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000803 /* Only enable SRG, if McBSP is master */
804 w = MCBSP_READ_CACHE(mcbsp, PCR0);
805 if (w & (FSXM | FSRM | CLKXM | CLKRM))
806 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
807 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300808
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000809 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300810 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800811 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800812 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300813 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814
815 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300816 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800817 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800818 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100819
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300820 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800821 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800822 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100823
Eduardo Valentin44a63112009-08-20 16:18:09 +0300824 /*
825 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
826 * REVISIT: 100us may give enough time for two CLKSRG, however
827 * due to some unknown PM related, clock gating etc. reason it
828 * is now at 500us.
829 */
830 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100831
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000832 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300833 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800834 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800835 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300836 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100837
Jarkko Nikula88408232011-09-26 10:45:41 +0300838 if (mcbsp->pdata->has_ccr) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300839 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800840 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300841 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800842 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800843 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300844 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800845 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300846 }
847
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100848 /* Dump McBSP Regs */
849 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300851EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100852
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300853void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100854{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300855 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300856 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100857 u16 w;
858
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300859 if (!omap_mcbsp_check_valid_id(id)) {
860 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300862 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100863
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300864 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100865
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300866 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300867 tx &= 1;
Jarkko Nikula88408232011-09-26 10:45:41 +0300868 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800869 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300870 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800871 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300872 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800873 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800874 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875
876 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300877 rx &= 1;
Jarkko Nikula88408232011-09-26 10:45:41 +0300878 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800879 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700880 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800881 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300882 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800883 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800884 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100885
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800886 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
887 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300888
889 if (idle) {
890 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800891 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800892 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300893 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000894
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300895 if (mcbsp->st_data)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000896 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100897}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300898EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100899
Paul Walmsley69d042d2011-07-01 08:52:25 +0000900int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
901{
Jarkko Nikula09d28d22011-09-26 10:45:48 +0300902 struct omap_mcbsp *mcbsp;
903 const char *src;
Paul Walmsley69d042d2011-07-01 08:52:25 +0000904
Jarkko Nikula09d28d22011-09-26 10:45:48 +0300905 if (!omap_mcbsp_check_valid_id(id)) {
906 pr_err("%s: Invalid id (%d)\n", __func__, id + 1);
907 return -EINVAL;
908 }
909 mcbsp = id_to_mcbsp_ptr(id);
910
911 if (fck_src_id == MCBSP_CLKS_PAD_SRC)
912 src = "clks_ext";
913 else if (fck_src_id == MCBSP_CLKS_PRCM_SRC)
914 src = "clks_fclk";
915 else
916 return -EINVAL;
917
918 if (mcbsp->pdata->set_clk_src)
919 return mcbsp->pdata->set_clk_src(mcbsp->dev, mcbsp->fclk, src);
920 else
921 return -EINVAL;
922}
923EXPORT_SYMBOL(omap2_mcbsp_set_clks_src);
924
925#ifndef CONFIG_ARCH_OMAP2PLUS
Paul Walmsley69d042d2011-07-01 08:52:25 +0000926void omap2_mcbsp1_mux_clkr_src(u8 mux)
927{
928 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
929 __func__);
930 return;
931}
932
933void omap2_mcbsp1_mux_fsr_src(u8 mux)
934{
935 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
936 __func__);
937 return;
938}
939#endif
940
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300941#define max_thres(m) (mcbsp->pdata->buffer_size)
942#define valid_threshold(m, val) ((val) <= max_thres(m))
943#define THRESHOLD_PROP_BUILDER(prop) \
944static ssize_t prop##_show(struct device *dev, \
945 struct device_attribute *attr, char *buf) \
946{ \
947 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
948 \
949 return sprintf(buf, "%u\n", mcbsp->prop); \
950} \
951 \
952static ssize_t prop##_store(struct device *dev, \
953 struct device_attribute *attr, \
954 const char *buf, size_t size) \
955{ \
956 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
957 unsigned long val; \
958 int status; \
959 \
960 status = strict_strtoul(buf, 0, &val); \
961 if (status) \
962 return status; \
963 \
964 if (!valid_threshold(mcbsp, val)) \
965 return -EDOM; \
966 \
967 mcbsp->prop = val; \
968 return size; \
969} \
970 \
971static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
972
973THRESHOLD_PROP_BUILDER(max_tx_thres);
974THRESHOLD_PROP_BUILDER(max_rx_thres);
975
Jarkko Nikula9b300502009-08-24 17:45:50 +0300976static const char *dma_op_modes[] = {
977 "element", "threshold", "frame",
978};
979
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300980static ssize_t dma_op_mode_show(struct device *dev,
981 struct device_attribute *attr, char *buf)
982{
983 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +0300984 int dma_op_mode, i = 0;
985 ssize_t len = 0;
986 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300987
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300988 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300989
Jarkko Nikula9b300502009-08-24 17:45:50 +0300990 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
991 if (dma_op_mode == i)
992 len += sprintf(buf + len, "[%s] ", *s);
993 else
994 len += sprintf(buf + len, "%s ", *s);
995 }
996 len += sprintf(buf + len, "\n");
997
998 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300999}
1000
1001static ssize_t dma_op_mode_store(struct device *dev,
1002 struct device_attribute *attr,
1003 const char *buf, size_t size)
1004{
1005 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +03001006 const char * const *s;
1007 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001008
Jarkko Nikula9b300502009-08-24 17:45:50 +03001009 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
1010 if (sysfs_streq(buf, *s))
1011 break;
1012
1013 if (i == ARRAY_SIZE(dma_op_modes))
1014 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001015
1016 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001017 if (!mcbsp->free) {
1018 size = -EBUSY;
1019 goto unlock;
1020 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001021 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001022
1023unlock:
1024 spin_unlock_irq(&mcbsp->lock);
1025
1026 return size;
1027}
1028
1029static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1030
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001031static const struct attribute *additional_attrs[] = {
1032 &dev_attr_max_tx_thres.attr,
1033 &dev_attr_max_rx_thres.attr,
1034 &dev_attr_dma_op_mode.attr,
1035 NULL,
1036};
1037
1038static const struct attribute_group additional_attr_group = {
1039 .attrs = (struct attribute **)additional_attrs,
1040};
1041
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001042static ssize_t st_taps_show(struct device *dev,
1043 struct device_attribute *attr, char *buf)
1044{
1045 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1046 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1047 ssize_t status = 0;
1048 int i;
1049
1050 spin_lock_irq(&mcbsp->lock);
1051 for (i = 0; i < st_data->nr_taps; i++)
1052 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1053 st_data->taps[i]);
1054 if (i)
1055 status += sprintf(&buf[status], "\n");
1056 spin_unlock_irq(&mcbsp->lock);
1057
1058 return status;
1059}
1060
1061static ssize_t st_taps_store(struct device *dev,
1062 struct device_attribute *attr,
1063 const char *buf, size_t size)
1064{
1065 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1066 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1067 int val, tmp, status, i = 0;
1068
1069 spin_lock_irq(&mcbsp->lock);
1070 memset(st_data->taps, 0, sizeof(st_data->taps));
1071 st_data->nr_taps = 0;
1072
1073 do {
1074 status = sscanf(buf, "%d%n", &val, &tmp);
1075 if (status < 0 || status == 0) {
1076 size = -EINVAL;
1077 goto out;
1078 }
1079 if (val < -32768 || val > 32767) {
1080 size = -EINVAL;
1081 goto out;
1082 }
1083 st_data->taps[i++] = val;
1084 buf += tmp;
1085 if (*buf != ',')
1086 break;
1087 buf++;
1088 } while (1);
1089
1090 st_data->nr_taps = i;
1091
1092out:
1093 spin_unlock_irq(&mcbsp->lock);
1094
1095 return size;
1096}
1097
1098static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1099
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001100static const struct attribute *sidetone_attrs[] = {
1101 &dev_attr_st_taps.attr,
1102 NULL,
1103};
1104
1105static const struct attribute_group sidetone_attr_group = {
1106 .attrs = (struct attribute **)sidetone_attrs,
1107};
1108
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001109static int __devinit omap_st_add(struct omap_mcbsp *mcbsp,
1110 struct resource *res)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001111{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001112 struct omap_mcbsp_st_data *st_data;
1113 int err;
1114
1115 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1116 if (!st_data) {
1117 err = -ENOMEM;
1118 goto err1;
1119 }
1120
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001121 st_data->io_base_st = ioremap(res->start, resource_size(res));
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001122 if (!st_data->io_base_st) {
1123 err = -ENOMEM;
1124 goto err2;
1125 }
1126
1127 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1128 if (err)
1129 goto err3;
1130
1131 mcbsp->st_data = st_data;
1132 return 0;
1133
1134err3:
1135 iounmap(st_data->io_base_st);
1136err2:
1137 kfree(st_data);
1138err1:
1139 return err;
1140
1141}
1142
1143static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1144{
1145 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1146
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001147 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1148 iounmap(st_data->io_base_st);
1149 kfree(st_data);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001150}
1151
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152/*
1153 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1154 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1155 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001156static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001158 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001159 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001160 int id = pdev->id - 1;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001161 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001162 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001163
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001164 if (!pdata) {
1165 dev_err(&pdev->dev, "McBSP device initialized without"
1166 "platform data\n");
1167 ret = -EINVAL;
1168 goto exit;
1169 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001170
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001171 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001172
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001173 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001174 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1175 ret = -EINVAL;
1176 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001177 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001179 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1180 if (!mcbsp) {
1181 ret = -ENOMEM;
1182 goto exit;
1183 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001184
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001185 spin_lock_init(&mcbsp->lock);
1186 mcbsp->id = id + 1;
Shubhrajyoti D6722a722010-12-07 16:25:41 -08001187 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001188
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001189 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1190 if (!res) {
1191 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1192 if (!res) {
1193 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1194 "resource\n", __func__, pdev->id);
1195 ret = -ENOMEM;
1196 goto exit;
1197 }
1198 }
1199 mcbsp->phys_base = res->start;
Jarkko Nikulaac6747c2011-09-26 10:45:43 +03001200 mcbsp->reg_cache_size = resource_size(res);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001201 mcbsp->io_base = ioremap(res->start, resource_size(res));
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001202 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001203 ret = -ENOMEM;
1204 goto err_ioremap;
1205 }
1206
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001207 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1208 if (!res)
1209 mcbsp->phys_dma_base = mcbsp->phys_base;
1210 else
1211 mcbsp->phys_dma_base = res->start;
1212
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001213 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1214 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1215
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +05301216 /* From OMAP4 there will be a single irq line */
1217 if (mcbsp->tx_irq == -ENXIO)
1218 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1219
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001220 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1221 if (!res) {
1222 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1223 __func__, pdev->id);
1224 ret = -ENODEV;
1225 goto err_res;
1226 }
1227 mcbsp->dma_rx_sync = res->start;
1228
1229 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1230 if (!res) {
1231 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1232 __func__, pdev->id);
1233 ret = -ENODEV;
1234 goto err_res;
1235 }
1236 mcbsp->dma_tx_sync = res->start;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001237
Russell Kingb820ce42009-01-23 10:26:46 +00001238 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1239 if (IS_ERR(mcbsp->fclk)) {
1240 ret = PTR_ERR(mcbsp->fclk);
1241 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301242 goto err_res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001243 }
1244
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001245 mcbsp->pdata = pdata;
1246 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001247 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001248 platform_set_drvdata(pdev, mcbsp);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301249 pm_runtime_enable(mcbsp->dev);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001250
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001251 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1252 if (mcbsp->pdata->buffer_size) {
1253 /*
1254 * Initially configure the maximum thresholds to a safe value.
1255 * The McBSP FIFO usage with these values should not go under
1256 * 16 locations.
1257 * If the whole FIFO without safety buffer is used, than there
1258 * is a possibility that the DMA will be not able to push the
1259 * new data on time, causing channel shifts in runtime.
1260 */
1261 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1262 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1263
1264 ret = sysfs_create_group(&mcbsp->dev->kobj,
1265 &additional_attr_group);
1266 if (ret) {
1267 dev_err(mcbsp->dev,
1268 "Unable to create additional controls\n");
1269 goto err_thres;
1270 }
1271 } else {
1272 mcbsp->max_tx_thres = -EINVAL;
1273 mcbsp->max_rx_thres = -EINVAL;
1274 }
1275
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001276 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1277 if (res) {
1278 ret = omap_st_add(mcbsp, res);
1279 if (ret) {
1280 dev_err(mcbsp->dev,
1281 "Unable to create sidetone controls\n");
1282 goto err_st;
1283 }
1284 }
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001285
Russell Kingd592dd12008-09-04 14:25:42 +01001286 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001287
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001288err_st:
1289 if (mcbsp->pdata->buffer_size)
1290 sysfs_remove_group(&mcbsp->dev->kobj,
1291 &additional_attr_group);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001292err_thres:
1293 clk_put(mcbsp->fclk);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001294err_res:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001295 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001296err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001297 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001298exit:
1299 return ret;
1300}
1301
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001302static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001303{
1304 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1305
1306 platform_set_drvdata(pdev, NULL);
1307 if (mcbsp) {
1308
1309 if (mcbsp->pdata && mcbsp->pdata->ops &&
1310 mcbsp->pdata->ops->free)
1311 mcbsp->pdata->ops->free(mcbsp->id);
1312
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001313 if (mcbsp->pdata->buffer_size)
1314 sysfs_remove_group(&mcbsp->dev->kobj,
1315 &additional_attr_group);
1316
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001317 if (mcbsp->st_data)
1318 omap_st_remove(mcbsp);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001319
Russell Kingb820ce42009-01-23 10:26:46 +00001320 clk_put(mcbsp->fclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001321
Russell Kingd592dd12008-09-04 14:25:42 +01001322 iounmap(mcbsp->io_base);
Jarkko Nikula5f3b7282010-12-07 16:25:40 -08001323 kfree(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001324 }
1325
1326 return 0;
1327}
1328
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001329static struct platform_driver omap_mcbsp_driver = {
1330 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001331 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001332 .driver = {
1333 .name = "omap-mcbsp",
1334 },
1335};
1336
1337int __init omap_mcbsp_init(void)
1338{
1339 /* Register the McBSP driver */
1340 return platform_driver_register(&omap_mcbsp_driver);
1341}