blob: f92227f99f86f6e082309670af89e4e15ecb1529 [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
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070032static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030033{
Jarkko Nikulacdc715142011-09-26 10:45:39 +030034 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
35
36 if (mcbsp->pdata->reg_size == 2) {
37 ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
38 __raw_writew((u16)val, addr);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080039 } else {
Jarkko Nikulacdc715142011-09-26 10:45:39 +030040 ((u32 *)mcbsp->reg_cache)[reg] = val;
41 __raw_writel(val, addr);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080042 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030043}
44
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070045static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030046{
Jarkko Nikulacdc715142011-09-26 10:45:39 +030047 void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
48
49 if (mcbsp->pdata->reg_size == 2) {
50 return !from_cache ? __raw_readw(addr) :
51 ((u16 *)mcbsp->reg_cache)[reg];
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080052 } else {
Jarkko Nikulacdc715142011-09-26 10:45:39 +030053 return !from_cache ? __raw_readl(addr) :
54 ((u32 *)mcbsp->reg_cache)[reg];
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080055 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +030056}
57
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070058static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000059{
60 __raw_writel(val, mcbsp->st_data->io_base_st + reg);
61}
62
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070063static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
Eero Nurkkalad912fa92010-02-22 12:21:11 +000064{
65 return __raw_readl(mcbsp->st_data->io_base_st + reg);
66}
Eero Nurkkalad912fa92010-02-22 12:21:11 +000067
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080068#define MCBSP_READ(mcbsp, reg) \
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080069 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080070#define MCBSP_WRITE(mcbsp, reg, val) \
71 omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -080072#define MCBSP_READ_CACHE(mcbsp, reg) \
73 omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
Chandra Shekharb4b58f52008-10-08 10:01:39 +030074
Eero Nurkkalad912fa92010-02-22 12:21:11 +000075#define MCBSP_ST_READ(mcbsp, reg) \
76 omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
77#define MCBSP_ST_WRITE(mcbsp, reg, val) \
78 omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
79
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010080static void omap_mcbsp_dump_reg(u8 id)
81{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030082 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
83
84 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
85 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080086 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030087 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080088 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030089 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080090 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030091 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080092 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030093 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080094 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030095 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080096 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030097 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080098 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030099 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800100 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300101 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800102 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300103 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800104 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300105 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800106 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300107 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800108 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300109 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800110 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300111 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112}
113
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700114static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400116 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700117 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800119 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700120 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700122 if (irqst_spcr2 & XSYNC_ERR) {
123 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
124 irqst_spcr2);
125 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000126 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700127 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300128
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100129 return IRQ_HANDLED;
130}
131
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700132static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400134 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700135 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800137 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700138 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700140 if (irqst_spcr1 & RSYNC_ERR) {
141 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
142 irqst_spcr1);
143 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000144 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700145 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300146
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100147 return IRQ_HANDLED;
148}
149
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150/*
151 * omap_mcbsp_config simply write a config to the
152 * appropriate McBSP.
153 * You either call this function or set the McBSP registers
154 * by yourself before calling omap_mcbsp_start().
155 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300156void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300158 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300160 if (!omap_mcbsp_check_valid_id(id)) {
161 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
162 return;
163 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300164 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300165
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300166 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
167 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168
169 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800170 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
171 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
172 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
173 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
174 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
175 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
176 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
177 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
178 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
179 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
180 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Jarkko Nikula88408232011-09-26 10:45:41 +0300181 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800182 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
183 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200184 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100185}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300186EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100187
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530188/**
189 * omap_mcbsp_dma_params - returns the dma channel number
190 * @id - mcbsp id
191 * @stream - indicates the direction of data flow (rx or tx)
192 *
193 * Returns the dma channel number for the rx channel or tx channel
194 * based on the value of @stream for the requested mcbsp given by @id
195 */
196int omap_mcbsp_dma_ch_params(unsigned int id, unsigned int stream)
197{
198 struct omap_mcbsp *mcbsp;
199
200 if (!omap_mcbsp_check_valid_id(id)) {
201 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
202 return -ENODEV;
203 }
204 mcbsp = id_to_mcbsp_ptr(id);
205
206 if (stream)
207 return mcbsp->dma_rx_sync;
208 else
209 return mcbsp->dma_tx_sync;
210}
211EXPORT_SYMBOL(omap_mcbsp_dma_ch_params);
212
213/**
214 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
215 * @id - mcbsp id
216 * @stream - indicates the direction of data flow (rx or tx)
217 *
218 * Returns the address of mcbsp data transmit register or data receive register
219 * to be used by DMA for transferring/receiving data based on the value of
220 * @stream for the requested mcbsp given by @id
221 */
222int omap_mcbsp_dma_reg_params(unsigned int id, unsigned int stream)
223{
224 struct omap_mcbsp *mcbsp;
225 int data_reg;
226
227 if (!omap_mcbsp_check_valid_id(id)) {
228 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
229 return -ENODEV;
230 }
231 mcbsp = id_to_mcbsp_ptr(id);
232
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300233 if (mcbsp->pdata->reg_size == 2) {
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530234 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300235 data_reg = OMAP_MCBSP_REG_DRR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530236 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300237 data_reg = OMAP_MCBSP_REG_DXR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530238 } else {
239 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300240 data_reg = OMAP_MCBSP_REG_DRR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530241 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300242 data_reg = OMAP_MCBSP_REG_DXR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530243 }
244
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300245 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530246}
247EXPORT_SYMBOL(omap_mcbsp_dma_reg_params);
248
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000249static void omap_st_on(struct omap_mcbsp *mcbsp)
250{
251 unsigned int w;
252
Jarkko Nikula1743d142011-09-26 10:45:44 +0300253 if (mcbsp->pdata->enable_st_clock)
254 mcbsp->pdata->enable_st_clock(mcbsp->id, 1);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000255
256 /* Enable McBSP Sidetone */
257 w = MCBSP_READ(mcbsp, SSELCR);
258 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
259
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000260 /* Enable Sidetone from Sidetone Core */
261 w = MCBSP_ST_READ(mcbsp, SSELCR);
262 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
263}
264
265static void omap_st_off(struct omap_mcbsp *mcbsp)
266{
267 unsigned int w;
268
269 w = MCBSP_ST_READ(mcbsp, SSELCR);
270 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
271
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000272 w = MCBSP_READ(mcbsp, SSELCR);
273 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
274
Jarkko Nikula1743d142011-09-26 10:45:44 +0300275 if (mcbsp->pdata->enable_st_clock)
276 mcbsp->pdata->enable_st_clock(mcbsp->id, 0);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000277}
278
279static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
280{
281 u16 val, i;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000282
283 val = MCBSP_ST_READ(mcbsp, SSELCR);
284
285 if (val & ST_COEFFWREN)
286 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
287
288 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
289
290 for (i = 0; i < 128; i++)
291 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
292
293 i = 0;
294
295 val = MCBSP_ST_READ(mcbsp, SSELCR);
296 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
297 val = MCBSP_ST_READ(mcbsp, SSELCR);
298
299 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
300
301 if (i == 1000)
302 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
303}
304
305static void omap_st_chgain(struct omap_mcbsp *mcbsp)
306{
307 u16 w;
308 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000309
310 w = MCBSP_ST_READ(mcbsp, SSELCR);
311
312 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
313 ST_CH1GAIN(st_data->ch1gain));
314}
315
316int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
317{
318 struct omap_mcbsp *mcbsp;
319 struct omap_mcbsp_st_data *st_data;
320 int ret = 0;
321
322 if (!omap_mcbsp_check_valid_id(id)) {
323 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
324 return -ENODEV;
325 }
326
327 mcbsp = id_to_mcbsp_ptr(id);
328 st_data = mcbsp->st_data;
329
330 if (!st_data)
331 return -ENOENT;
332
333 spin_lock_irq(&mcbsp->lock);
334 if (channel == 0)
335 st_data->ch0gain = chgain;
336 else if (channel == 1)
337 st_data->ch1gain = chgain;
338 else
339 ret = -EINVAL;
340
341 if (st_data->enabled)
342 omap_st_chgain(mcbsp);
343 spin_unlock_irq(&mcbsp->lock);
344
345 return ret;
346}
347EXPORT_SYMBOL(omap_st_set_chgain);
348
349int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
350{
351 struct omap_mcbsp *mcbsp;
352 struct omap_mcbsp_st_data *st_data;
353 int ret = 0;
354
355 if (!omap_mcbsp_check_valid_id(id)) {
356 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
357 return -ENODEV;
358 }
359
360 mcbsp = id_to_mcbsp_ptr(id);
361 st_data = mcbsp->st_data;
362
363 if (!st_data)
364 return -ENOENT;
365
366 spin_lock_irq(&mcbsp->lock);
367 if (channel == 0)
368 *chgain = st_data->ch0gain;
369 else if (channel == 1)
370 *chgain = st_data->ch1gain;
371 else
372 ret = -EINVAL;
373 spin_unlock_irq(&mcbsp->lock);
374
375 return ret;
376}
377EXPORT_SYMBOL(omap_st_get_chgain);
378
379static int omap_st_start(struct omap_mcbsp *mcbsp)
380{
381 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
382
383 if (st_data && st_data->enabled && !st_data->running) {
384 omap_st_fir_write(mcbsp, st_data->taps);
385 omap_st_chgain(mcbsp);
386
387 if (!mcbsp->free) {
388 omap_st_on(mcbsp);
389 st_data->running = 1;
390 }
391 }
392
393 return 0;
394}
395
396int omap_st_enable(unsigned int id)
397{
398 struct omap_mcbsp *mcbsp;
399 struct omap_mcbsp_st_data *st_data;
400
401 if (!omap_mcbsp_check_valid_id(id)) {
402 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
403 return -ENODEV;
404 }
405
406 mcbsp = id_to_mcbsp_ptr(id);
407 st_data = mcbsp->st_data;
408
409 if (!st_data)
410 return -ENODEV;
411
412 spin_lock_irq(&mcbsp->lock);
413 st_data->enabled = 1;
414 omap_st_start(mcbsp);
415 spin_unlock_irq(&mcbsp->lock);
416
417 return 0;
418}
419EXPORT_SYMBOL(omap_st_enable);
420
421static int omap_st_stop(struct omap_mcbsp *mcbsp)
422{
423 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
424
425 if (st_data && st_data->running) {
426 if (!mcbsp->free) {
427 omap_st_off(mcbsp);
428 st_data->running = 0;
429 }
430 }
431
432 return 0;
433}
434
435int omap_st_disable(unsigned int id)
436{
437 struct omap_mcbsp *mcbsp;
438 struct omap_mcbsp_st_data *st_data;
439 int ret = 0;
440
441 if (!omap_mcbsp_check_valid_id(id)) {
442 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
443 return -ENODEV;
444 }
445
446 mcbsp = id_to_mcbsp_ptr(id);
447 st_data = mcbsp->st_data;
448
449 if (!st_data)
450 return -ENODEV;
451
452 spin_lock_irq(&mcbsp->lock);
453 omap_st_stop(mcbsp);
454 st_data->enabled = 0;
455 spin_unlock_irq(&mcbsp->lock);
456
457 return ret;
458}
459EXPORT_SYMBOL(omap_st_disable);
460
461int omap_st_is_enabled(unsigned int id)
462{
463 struct omap_mcbsp *mcbsp;
464 struct omap_mcbsp_st_data *st_data;
465
466 if (!omap_mcbsp_check_valid_id(id)) {
467 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
468 return -ENODEV;
469 }
470
471 mcbsp = id_to_mcbsp_ptr(id);
472 st_data = mcbsp->st_data;
473
474 if (!st_data)
475 return -ENODEV;
476
477
478 return st_data->enabled;
479}
480EXPORT_SYMBOL(omap_st_is_enabled);
481
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300482/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300483 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
484 * The threshold parameter is 1 based, and it is converted (threshold - 1)
485 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300486 */
487void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
488{
489 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300490
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300491 if (!omap_mcbsp_check_valid_id(id)) {
492 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
493 return;
494 }
495 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300496 if (mcbsp->pdata->buffer_size == 0)
497 return;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300498
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300499 if (threshold && threshold <= mcbsp->max_tx_thres)
500 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300501}
502EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
503
504/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300505 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
506 * The threshold parameter is 1 based, and it is converted (threshold - 1)
507 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300508 */
509void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
510{
511 struct omap_mcbsp *mcbsp;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300512
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300513 if (!omap_mcbsp_check_valid_id(id)) {
514 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
515 return;
516 }
517 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300518 if (mcbsp->pdata->buffer_size == 0)
519 return;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300520
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300521 if (threshold && threshold <= mcbsp->max_rx_thres)
522 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300523}
524EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300525
526/*
527 * omap_mcbsp_get_max_tx_thres just return the current configured
528 * maximum threshold for transmission
529 */
530u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
531{
532 struct omap_mcbsp *mcbsp;
533
534 if (!omap_mcbsp_check_valid_id(id)) {
535 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
536 return -ENODEV;
537 }
538 mcbsp = id_to_mcbsp_ptr(id);
539
540 return mcbsp->max_tx_thres;
541}
542EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
543
544/*
545 * omap_mcbsp_get_max_rx_thres just return the current configured
546 * maximum threshold for reception
547 */
548u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
549{
550 struct omap_mcbsp *mcbsp;
551
552 if (!omap_mcbsp_check_valid_id(id)) {
553 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
554 return -ENODEV;
555 }
556 mcbsp = id_to_mcbsp_ptr(id);
557
558 return mcbsp->max_rx_thres;
559}
560EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300561
Peter Ujfalusi0acce822010-06-03 07:39:32 +0300562u16 omap_mcbsp_get_fifo_size(unsigned int id)
563{
564 struct omap_mcbsp *mcbsp;
565
566 if (!omap_mcbsp_check_valid_id(id)) {
567 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
568 return -ENODEV;
569 }
570 mcbsp = id_to_mcbsp_ptr(id);
571
572 return mcbsp->pdata->buffer_size;
573}
574EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
575
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200576/*
577 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
578 */
579u16 omap_mcbsp_get_tx_delay(unsigned int id)
580{
581 struct omap_mcbsp *mcbsp;
582 u16 buffstat;
583
584 if (!omap_mcbsp_check_valid_id(id)) {
585 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
586 return -ENODEV;
587 }
588 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300589 if (mcbsp->pdata->buffer_size == 0)
590 return 0;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200591
592 /* Returns the number of free locations in the buffer */
593 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
594
595 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300596 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200597}
598EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
599
600/*
601 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
602 * to reach the threshold value (when the DMA will be triggered to read it)
603 */
604u16 omap_mcbsp_get_rx_delay(unsigned int id)
605{
606 struct omap_mcbsp *mcbsp;
607 u16 buffstat, threshold;
608
609 if (!omap_mcbsp_check_valid_id(id)) {
610 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
611 return -ENODEV;
612 }
613 mcbsp = id_to_mcbsp_ptr(id);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300614 if (mcbsp->pdata->buffer_size == 0)
615 return 0;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200616
617 /* Returns the number of used locations in the buffer */
618 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
619 /* RX threshold */
620 threshold = MCBSP_READ(mcbsp, THRSH1);
621
622 /* Return the number of location till we reach the threshold limit */
623 if (threshold <= buffstat)
624 return 0;
625 else
626 return threshold - buffstat;
627}
628EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
629
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300630/*
631 * omap_mcbsp_get_dma_op_mode just return the current configured
632 * operating mode for the mcbsp channel
633 */
634int omap_mcbsp_get_dma_op_mode(unsigned int id)
635{
636 struct omap_mcbsp *mcbsp;
637 int dma_op_mode;
638
639 if (!omap_mcbsp_check_valid_id(id)) {
640 printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
641 return -ENODEV;
642 }
643 mcbsp = id_to_mcbsp_ptr(id);
644
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300645 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300646
647 return dma_op_mode;
648}
649EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300650
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651int omap_mcbsp_request(unsigned int id)
652{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300653 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800654 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100655 int err;
656
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300657 if (!omap_mcbsp_check_valid_id(id)) {
658 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
659 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100660 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300661 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300662
Jarkko Nikulaac6747c2011-09-26 10:45:43 +0300663 reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800664 if (!reg_cache) {
665 return -ENOMEM;
666 }
667
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300668 spin_lock(&mcbsp->lock);
669 if (!mcbsp->free) {
670 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
671 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800672 err = -EBUSY;
673 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674 }
675
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800676 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800677 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300678 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100679
Russell Kingb820ce42009-01-23 10:26:46 +0000680 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
681 mcbsp->pdata->ops->request(id);
682
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530683 pm_runtime_get_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000684
Jarkko Nikula1a645882011-09-26 10:45:40 +0300685 /* Enable wakeup behavior */
686 if (mcbsp->pdata->has_wakeup)
687 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300688
Jarkko Nikula5a070552008-10-08 10:01:41 +0300689 /*
690 * Make sure that transmitter, receiver and sample-rate generator are
691 * not running before activating IRQs.
692 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800693 MCBSP_WRITE(mcbsp, SPCR1, 0);
694 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300695
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000696 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
697 0, "McBSP", (void *)mcbsp);
698 if (err != 0) {
699 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
700 "for McBSP%d\n", mcbsp->tx_irq,
701 mcbsp->id);
702 goto err_clk_disable;
703 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100704
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000705 if (mcbsp->rx_irq) {
706 err = request_irq(mcbsp->rx_irq,
707 omap_mcbsp_rx_irq_handler,
708 0, "McBSP", (void *)mcbsp);
709 if (err != 0) {
710 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
711 "for McBSP%d\n", mcbsp->rx_irq,
712 mcbsp->id);
713 goto err_free_irq;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100714 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715 }
716
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100717 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800718err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800719 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800720err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800721 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800722 mcbsp->pdata->ops->free(id);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800723
Jarkko Nikula1a645882011-09-26 10:45:40 +0300724 /* Disable wakeup behavior */
725 if (mcbsp->pdata->has_wakeup)
726 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800727
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530728 pm_runtime_put_sync(mcbsp->dev);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800729
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800730 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800731 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800732 mcbsp->reg_cache = NULL;
733err_kfree:
734 spin_unlock(&mcbsp->lock);
735 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800736
737 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300739EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
741void omap_mcbsp_free(unsigned int id)
742{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 struct omap_mcbsp *mcbsp;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800744 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300745
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300746 if (!omap_mcbsp_check_valid_id(id)) {
747 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100749 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300750 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100751
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300752 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
753 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300754
Jarkko Nikula1a645882011-09-26 10:45:40 +0300755 /* Disable wakeup behavior */
756 if (mcbsp->pdata->has_wakeup)
757 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300758
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +0530759 pm_runtime_put_sync(mcbsp->dev);
Russell Kingb820ce42009-01-23 10:26:46 +0000760
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000761 if (mcbsp->rx_irq)
762 free_irq(mcbsp->rx_irq, (void *)mcbsp);
763 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800765 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800767 spin_lock(&mcbsp->lock);
768 if (mcbsp->free)
769 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
770 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800771 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800772 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300773 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800774
775 if (reg_cache)
776 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300778EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100779
780/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300781 * Here we start the McBSP, by enabling transmitter, receiver or both.
782 * If no transmitter or receiver is active prior calling, then sample-rate
783 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100784 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300785void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100786{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300787 struct omap_mcbsp *mcbsp;
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000788 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789 u16 w;
790
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300791 if (!omap_mcbsp_check_valid_id(id)) {
792 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300794 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300795 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300797 if (mcbsp->st_data)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000798 omap_st_start(mcbsp);
799
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000800 /* Only enable SRG, if McBSP is master */
801 w = MCBSP_READ_CACHE(mcbsp, PCR0);
802 if (w & (FSXM | FSRM | CLKXM | CLKRM))
803 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
804 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300805
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000806 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300807 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800808 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800809 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300810 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100811
812 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300813 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800814 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800815 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100816
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300817 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800818 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800819 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100820
Eduardo Valentin44a63112009-08-20 16:18:09 +0300821 /*
822 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
823 * REVISIT: 100us may give enough time for two CLKSRG, however
824 * due to some unknown PM related, clock gating etc. reason it
825 * is now at 500us.
826 */
827 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100828
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000829 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300830 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800831 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800832 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300833 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100834
Jarkko Nikula88408232011-09-26 10:45:41 +0300835 if (mcbsp->pdata->has_ccr) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300836 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800837 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300838 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800839 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800840 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300841 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800842 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300843 }
844
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100845 /* Dump McBSP Regs */
846 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100847}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300848EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100849
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300850void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100851{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300852 struct omap_mcbsp *mcbsp;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300853 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100854 u16 w;
855
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300856 if (!omap_mcbsp_check_valid_id(id)) {
857 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100858 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300859 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100860
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300861 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100862
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300863 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300864 tx &= 1;
Jarkko Nikula88408232011-09-26 10:45:41 +0300865 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800866 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300867 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800868 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300869 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800870 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800871 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100872
873 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300874 rx &= 1;
Jarkko Nikula88408232011-09-26 10:45:41 +0300875 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800876 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700877 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800878 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300879 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800880 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800881 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100882
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800883 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
884 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300885
886 if (idle) {
887 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800888 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800889 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300890 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000891
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300892 if (mcbsp->st_data)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000893 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100894}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300895EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100896
Paul Walmsley69d042d2011-07-01 08:52:25 +0000897/*
898 * The following functions are only required on an OMAP1-only build.
899 * mach-omap2/mcbsp.c contains the real functions
900 */
901#ifndef CONFIG_ARCH_OMAP2PLUS
902int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
903{
904 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
905 __func__);
906 return -EINVAL;
907}
908
909void omap2_mcbsp1_mux_clkr_src(u8 mux)
910{
911 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
912 __func__);
913 return;
914}
915
916void omap2_mcbsp1_mux_fsr_src(u8 mux)
917{
918 WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
919 __func__);
920 return;
921}
922#endif
923
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300924#define max_thres(m) (mcbsp->pdata->buffer_size)
925#define valid_threshold(m, val) ((val) <= max_thres(m))
926#define THRESHOLD_PROP_BUILDER(prop) \
927static ssize_t prop##_show(struct device *dev, \
928 struct device_attribute *attr, char *buf) \
929{ \
930 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
931 \
932 return sprintf(buf, "%u\n", mcbsp->prop); \
933} \
934 \
935static ssize_t prop##_store(struct device *dev, \
936 struct device_attribute *attr, \
937 const char *buf, size_t size) \
938{ \
939 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
940 unsigned long val; \
941 int status; \
942 \
943 status = strict_strtoul(buf, 0, &val); \
944 if (status) \
945 return status; \
946 \
947 if (!valid_threshold(mcbsp, val)) \
948 return -EDOM; \
949 \
950 mcbsp->prop = val; \
951 return size; \
952} \
953 \
954static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
955
956THRESHOLD_PROP_BUILDER(max_tx_thres);
957THRESHOLD_PROP_BUILDER(max_rx_thres);
958
Jarkko Nikula9b300502009-08-24 17:45:50 +0300959static const char *dma_op_modes[] = {
960 "element", "threshold", "frame",
961};
962
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300963static ssize_t dma_op_mode_show(struct device *dev,
964 struct device_attribute *attr, char *buf)
965{
966 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +0300967 int dma_op_mode, i = 0;
968 ssize_t len = 0;
969 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300970
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300971 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300972
Jarkko Nikula9b300502009-08-24 17:45:50 +0300973 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
974 if (dma_op_mode == i)
975 len += sprintf(buf + len, "[%s] ", *s);
976 else
977 len += sprintf(buf + len, "%s ", *s);
978 }
979 len += sprintf(buf + len, "\n");
980
981 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300982}
983
984static ssize_t dma_op_mode_store(struct device *dev,
985 struct device_attribute *attr,
986 const char *buf, size_t size)
987{
988 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +0300989 const char * const *s;
990 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300991
Jarkko Nikula9b300502009-08-24 17:45:50 +0300992 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
993 if (sysfs_streq(buf, *s))
994 break;
995
996 if (i == ARRAY_SIZE(dma_op_modes))
997 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300998
999 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001000 if (!mcbsp->free) {
1001 size = -EBUSY;
1002 goto unlock;
1003 }
Jarkko Nikula9b300502009-08-24 17:45:50 +03001004 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +03001005
1006unlock:
1007 spin_unlock_irq(&mcbsp->lock);
1008
1009 return size;
1010}
1011
1012static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
1013
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001014static const struct attribute *additional_attrs[] = {
1015 &dev_attr_max_tx_thres.attr,
1016 &dev_attr_max_rx_thres.attr,
1017 &dev_attr_dma_op_mode.attr,
1018 NULL,
1019};
1020
1021static const struct attribute_group additional_attr_group = {
1022 .attrs = (struct attribute **)additional_attrs,
1023};
1024
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001025static ssize_t st_taps_show(struct device *dev,
1026 struct device_attribute *attr, char *buf)
1027{
1028 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1029 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1030 ssize_t status = 0;
1031 int i;
1032
1033 spin_lock_irq(&mcbsp->lock);
1034 for (i = 0; i < st_data->nr_taps; i++)
1035 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
1036 st_data->taps[i]);
1037 if (i)
1038 status += sprintf(&buf[status], "\n");
1039 spin_unlock_irq(&mcbsp->lock);
1040
1041 return status;
1042}
1043
1044static ssize_t st_taps_store(struct device *dev,
1045 struct device_attribute *attr,
1046 const char *buf, size_t size)
1047{
1048 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
1049 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1050 int val, tmp, status, i = 0;
1051
1052 spin_lock_irq(&mcbsp->lock);
1053 memset(st_data->taps, 0, sizeof(st_data->taps));
1054 st_data->nr_taps = 0;
1055
1056 do {
1057 status = sscanf(buf, "%d%n", &val, &tmp);
1058 if (status < 0 || status == 0) {
1059 size = -EINVAL;
1060 goto out;
1061 }
1062 if (val < -32768 || val > 32767) {
1063 size = -EINVAL;
1064 goto out;
1065 }
1066 st_data->taps[i++] = val;
1067 buf += tmp;
1068 if (*buf != ',')
1069 break;
1070 buf++;
1071 } while (1);
1072
1073 st_data->nr_taps = i;
1074
1075out:
1076 spin_unlock_irq(&mcbsp->lock);
1077
1078 return size;
1079}
1080
1081static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
1082
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001083static const struct attribute *sidetone_attrs[] = {
1084 &dev_attr_st_taps.attr,
1085 NULL,
1086};
1087
1088static const struct attribute_group sidetone_attr_group = {
1089 .attrs = (struct attribute **)sidetone_attrs,
1090};
1091
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001092static int __devinit omap_st_add(struct omap_mcbsp *mcbsp,
1093 struct resource *res)
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001094{
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001095 struct omap_mcbsp_st_data *st_data;
1096 int err;
1097
1098 st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
1099 if (!st_data) {
1100 err = -ENOMEM;
1101 goto err1;
1102 }
1103
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001104 st_data->io_base_st = ioremap(res->start, resource_size(res));
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001105 if (!st_data->io_base_st) {
1106 err = -ENOMEM;
1107 goto err2;
1108 }
1109
1110 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1111 if (err)
1112 goto err3;
1113
1114 mcbsp->st_data = st_data;
1115 return 0;
1116
1117err3:
1118 iounmap(st_data->io_base_st);
1119err2:
1120 kfree(st_data);
1121err1:
1122 return err;
1123
1124}
1125
1126static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
1127{
1128 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
1129
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001130 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
1131 iounmap(st_data->io_base_st);
1132 kfree(st_data);
Eero Nurkkalad912fa92010-02-22 12:21:11 +00001133}
1134
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001135/*
1136 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
1137 * 730 has only 2 McBSP, and both of them are MPU peripherals.
1138 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001139static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001141 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001142 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001143 int id = pdev->id - 1;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001144 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001145 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001146
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001147 if (!pdata) {
1148 dev_err(&pdev->dev, "McBSP device initialized without"
1149 "platform data\n");
1150 ret = -EINVAL;
1151 goto exit;
1152 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001153
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001154 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001155
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001156 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001157 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
1158 ret = -EINVAL;
1159 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001160 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001161
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001162 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
1163 if (!mcbsp) {
1164 ret = -ENOMEM;
1165 goto exit;
1166 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001167
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001168 spin_lock_init(&mcbsp->lock);
1169 mcbsp->id = id + 1;
Shubhrajyoti D6722a722010-12-07 16:25:41 -08001170 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001171
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001172 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
1173 if (!res) {
1174 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1175 if (!res) {
1176 dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
1177 "resource\n", __func__, pdev->id);
1178 ret = -ENOMEM;
1179 goto exit;
1180 }
1181 }
1182 mcbsp->phys_base = res->start;
Jarkko Nikulaac6747c2011-09-26 10:45:43 +03001183 mcbsp->reg_cache_size = resource_size(res);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001184 mcbsp->io_base = ioremap(res->start, resource_size(res));
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001185 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +01001186 ret = -ENOMEM;
1187 goto err_ioremap;
1188 }
1189
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
1191 if (!res)
1192 mcbsp->phys_dma_base = mcbsp->phys_base;
1193 else
1194 mcbsp->phys_dma_base = res->start;
1195
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001196 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
1197 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
1198
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +05301199 /* From OMAP4 there will be a single irq line */
1200 if (mcbsp->tx_irq == -ENXIO)
1201 mcbsp->tx_irq = platform_get_irq(pdev, 0);
1202
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001203 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1204 if (!res) {
1205 dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
1206 __func__, pdev->id);
1207 ret = -ENODEV;
1208 goto err_res;
1209 }
1210 mcbsp->dma_rx_sync = res->start;
1211
1212 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1213 if (!res) {
1214 dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
1215 __func__, pdev->id);
1216 ret = -ENODEV;
1217 goto err_res;
1218 }
1219 mcbsp->dma_tx_sync = res->start;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001220
Russell Kingb820ce42009-01-23 10:26:46 +00001221 mcbsp->fclk = clk_get(&pdev->dev, "fck");
1222 if (IS_ERR(mcbsp->fclk)) {
1223 ret = PTR_ERR(mcbsp->fclk);
1224 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301225 goto err_res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001226 }
1227
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001228 mcbsp->pdata = pdata;
1229 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +00001230 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001231 platform_set_drvdata(pdev, mcbsp);
Kishon Vijay Abraham Ie95496d2011-02-24 15:16:54 +05301232 pm_runtime_enable(mcbsp->dev);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001233
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001234 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
1235 if (mcbsp->pdata->buffer_size) {
1236 /*
1237 * Initially configure the maximum thresholds to a safe value.
1238 * The McBSP FIFO usage with these values should not go under
1239 * 16 locations.
1240 * If the whole FIFO without safety buffer is used, than there
1241 * is a possibility that the DMA will be not able to push the
1242 * new data on time, causing channel shifts in runtime.
1243 */
1244 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1245 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1246
1247 ret = sysfs_create_group(&mcbsp->dev->kobj,
1248 &additional_attr_group);
1249 if (ret) {
1250 dev_err(mcbsp->dev,
1251 "Unable to create additional controls\n");
1252 goto err_thres;
1253 }
1254 } else {
1255 mcbsp->max_tx_thres = -EINVAL;
1256 mcbsp->max_rx_thres = -EINVAL;
1257 }
1258
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001259 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1260 if (res) {
1261 ret = omap_st_add(mcbsp, res);
1262 if (ret) {
1263 dev_err(mcbsp->dev,
1264 "Unable to create sidetone controls\n");
1265 goto err_st;
1266 }
1267 }
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001268
Russell Kingd592dd12008-09-04 14:25:42 +01001269 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001270
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001271err_st:
1272 if (mcbsp->pdata->buffer_size)
1273 sysfs_remove_group(&mcbsp->dev->kobj,
1274 &additional_attr_group);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001275err_thres:
1276 clk_put(mcbsp->fclk);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -08001277err_res:
Chandra Shekharb4b58f52008-10-08 10:01:39 +03001278 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +01001279err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +00001280 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001281exit:
1282 return ret;
1283}
1284
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001285static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001286{
1287 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
1288
1289 platform_set_drvdata(pdev, NULL);
1290 if (mcbsp) {
1291
1292 if (mcbsp->pdata && mcbsp->pdata->ops &&
1293 mcbsp->pdata->ops->free)
1294 mcbsp->pdata->ops->free(mcbsp->id);
1295
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001296 if (mcbsp->pdata->buffer_size)
1297 sysfs_remove_group(&mcbsp->dev->kobj,
1298 &additional_attr_group);
1299
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001300 if (mcbsp->st_data)
1301 omap_st_remove(mcbsp);
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001302
Russell Kingb820ce42009-01-23 10:26:46 +00001303 clk_put(mcbsp->fclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001304
Russell Kingd592dd12008-09-04 14:25:42 +01001305 iounmap(mcbsp->io_base);
Jarkko Nikula5f3b7282010-12-07 16:25:40 -08001306 kfree(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001307 }
1308
1309 return 0;
1310}
1311
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001312static struct platform_driver omap_mcbsp_driver = {
1313 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001314 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001315 .driver = {
1316 .name = "omap-mcbsp",
1317 },
1318};
1319
1320int __init omap_mcbsp_init(void)
1321{
1322 /* Register the McBSP driver */
1323 return platform_driver_register(&omap_mcbsp_driver);
1324}