blob: 95413a168087ba320f4dc6560ef757233233d755 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
Peter Ujfalusi71e822e2012-01-26 12:47:22 +02002 * sound/soc/omap/mcbsp.c
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01003 *
4 * Copyright (C) 2004 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
6 *
Peter Ujfalusi71e822e2012-01-26 12:47:22 +02007 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
8 * Peter Ujfalusi <peter.ujfalusi@ti.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * Multichannel mode not supported.
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/device.h>
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030020#include <linux/platform_device.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010021#include <linux/interrupt.h>
22#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Tony Lindgren04fbf6a2007-02-12 10:50:53 -080024#include <linux/delay.h>
Eduardo Valentinfb78d802008-07-03 12:24:39 +030025#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010027
Tony Lindgrence491cf2009-10-20 09:40:47 -070028#include <plat/mcbsp.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029
Peter Ujfalusi219f4312012-02-03 13:11:47 +020030#include "mcbsp.h"
31
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
Peter Ujfalusi45656b42012-02-14 18:20:58 +020080static void omap_mcbsp_dump_reg(struct omap_mcbsp *mcbsp)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010081{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030082 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
83 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080084 MCBSP_READ(mcbsp, DRR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030085 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080086 MCBSP_READ(mcbsp, DRR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030087 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080088 MCBSP_READ(mcbsp, DXR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030089 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080090 MCBSP_READ(mcbsp, DXR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030091 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080092 MCBSP_READ(mcbsp, SPCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030093 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080094 MCBSP_READ(mcbsp, SPCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030095 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080096 MCBSP_READ(mcbsp, RCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030097 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -080098 MCBSP_READ(mcbsp, RCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +030099 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800100 MCBSP_READ(mcbsp, XCR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300101 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800102 MCBSP_READ(mcbsp, XCR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300103 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800104 MCBSP_READ(mcbsp, SRGR2));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300105 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800106 MCBSP_READ(mcbsp, SRGR1));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300107 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800108 MCBSP_READ(mcbsp, PCR0));
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300109 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100110}
111
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700112static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400114 struct omap_mcbsp *mcbsp_tx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700115 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800117 irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700118 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700120 if (irqst_spcr2 & XSYNC_ERR) {
121 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
122 irqst_spcr2);
123 /* Writing zero to XSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000124 MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700125 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300126
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127 return IRQ_HANDLED;
128}
129
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700130static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100131{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400132 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700133 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800135 irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700136 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100137
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700138 if (irqst_spcr1 & RSYNC_ERR) {
139 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
140 irqst_spcr1);
141 /* Writing zero to RSYNC_ERR clears the IRQ */
Janusz Krzysztofik0841cb82010-02-23 15:50:38 +0000142 MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700143 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300144
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145 return IRQ_HANDLED;
146}
147
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100148/*
149 * omap_mcbsp_config simply write a config to the
150 * appropriate McBSP.
151 * You either call this function or set the McBSP registers
152 * by yourself before calling omap_mcbsp_start().
153 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200154void omap_mcbsp_config(struct omap_mcbsp *mcbsp,
155 const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100156{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300157 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
158 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100159
160 /* We write the given config */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800161 MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
162 MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
163 MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
164 MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
165 MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
166 MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
167 MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
168 MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
169 MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
170 MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
171 MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
Jarkko Nikula88408232011-09-26 10:45:41 +0300172 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800173 MCBSP_WRITE(mcbsp, XCCR, config->xccr);
174 MCBSP_WRITE(mcbsp, RCCR, config->rccr);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200175 }
Peter Ujfalusi08905d82012-03-05 11:27:40 +0200176 /* Enable wakeup behavior */
177 if (mcbsp->pdata->has_wakeup)
178 MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100179}
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100180
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530181/**
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530182 * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
183 * @id - mcbsp id
184 * @stream - indicates the direction of data flow (rx or tx)
185 *
186 * Returns the address of mcbsp data transmit register or data receive register
187 * to be used by DMA for transferring/receiving data based on the value of
188 * @stream for the requested mcbsp given by @id
189 */
Peter Ujfalusib8fb4902012-02-14 15:41:29 +0200190static int omap_mcbsp_dma_reg_params(struct omap_mcbsp *mcbsp,
191 unsigned int stream)
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530192{
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530193 int data_reg;
194
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300195 if (mcbsp->pdata->reg_size == 2) {
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530196 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300197 data_reg = OMAP_MCBSP_REG_DRR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530198 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300199 data_reg = OMAP_MCBSP_REG_DXR1;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530200 } else {
201 if (stream)
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300202 data_reg = OMAP_MCBSP_REG_DRR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530203 else
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300204 data_reg = OMAP_MCBSP_REG_DXR;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530205 }
206
Jarkko Nikulacdc715142011-09-26 10:45:39 +0300207 return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530208}
Kishon Vijay Abraham I9504ba62011-02-24 15:16:55 +0530209
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000210static void omap_st_on(struct omap_mcbsp *mcbsp)
211{
212 unsigned int w;
213
Jarkko Nikula1743d142011-09-26 10:45:44 +0300214 if (mcbsp->pdata->enable_st_clock)
215 mcbsp->pdata->enable_st_clock(mcbsp->id, 1);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000216
217 /* Enable McBSP Sidetone */
218 w = MCBSP_READ(mcbsp, SSELCR);
219 MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
220
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000221 /* Enable Sidetone from Sidetone Core */
222 w = MCBSP_ST_READ(mcbsp, SSELCR);
223 MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
224}
225
226static void omap_st_off(struct omap_mcbsp *mcbsp)
227{
228 unsigned int w;
229
230 w = MCBSP_ST_READ(mcbsp, SSELCR);
231 MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
232
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000233 w = MCBSP_READ(mcbsp, SSELCR);
234 MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
235
Jarkko Nikula1743d142011-09-26 10:45:44 +0300236 if (mcbsp->pdata->enable_st_clock)
237 mcbsp->pdata->enable_st_clock(mcbsp->id, 0);
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000238}
239
240static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
241{
242 u16 val, i;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000243
244 val = MCBSP_ST_READ(mcbsp, SSELCR);
245
246 if (val & ST_COEFFWREN)
247 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
248
249 MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
250
251 for (i = 0; i < 128; i++)
252 MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
253
254 i = 0;
255
256 val = MCBSP_ST_READ(mcbsp, SSELCR);
257 while (!(val & ST_COEFFWRDONE) && (++i < 1000))
258 val = MCBSP_ST_READ(mcbsp, SSELCR);
259
260 MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
261
262 if (i == 1000)
263 dev_err(mcbsp->dev, "McBSP FIR load error!\n");
264}
265
266static void omap_st_chgain(struct omap_mcbsp *mcbsp)
267{
268 u16 w;
269 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000270
271 w = MCBSP_ST_READ(mcbsp, SSELCR);
272
273 MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
274 ST_CH1GAIN(st_data->ch1gain));
275}
276
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200277int omap_st_set_chgain(struct omap_mcbsp *mcbsp, int channel, s16 chgain)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000278{
Peter Ujfalusie2002ab2012-02-23 15:38:37 +0200279 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000280 int ret = 0;
281
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000282 if (!st_data)
283 return -ENOENT;
284
285 spin_lock_irq(&mcbsp->lock);
286 if (channel == 0)
287 st_data->ch0gain = chgain;
288 else if (channel == 1)
289 st_data->ch1gain = chgain;
290 else
291 ret = -EINVAL;
292
293 if (st_data->enabled)
294 omap_st_chgain(mcbsp);
295 spin_unlock_irq(&mcbsp->lock);
296
297 return ret;
298}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000299
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200300int omap_st_get_chgain(struct omap_mcbsp *mcbsp, int channel, s16 *chgain)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000301{
Peter Ujfalusie2002ab2012-02-23 15:38:37 +0200302 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000303 int ret = 0;
304
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000305 if (!st_data)
306 return -ENOENT;
307
308 spin_lock_irq(&mcbsp->lock);
309 if (channel == 0)
310 *chgain = st_data->ch0gain;
311 else if (channel == 1)
312 *chgain = st_data->ch1gain;
313 else
314 ret = -EINVAL;
315 spin_unlock_irq(&mcbsp->lock);
316
317 return ret;
318}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000319
320static int omap_st_start(struct omap_mcbsp *mcbsp)
321{
322 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
323
Peter Ujfalusi58db1dc2012-02-23 15:40:55 +0200324 if (st_data->enabled && !st_data->running) {
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000325 omap_st_fir_write(mcbsp, st_data->taps);
326 omap_st_chgain(mcbsp);
327
328 if (!mcbsp->free) {
329 omap_st_on(mcbsp);
330 st_data->running = 1;
331 }
332 }
333
334 return 0;
335}
336
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200337int omap_st_enable(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000338{
Peter Ujfalusie2002ab2012-02-23 15:38:37 +0200339 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000340
341 if (!st_data)
342 return -ENODEV;
343
344 spin_lock_irq(&mcbsp->lock);
345 st_data->enabled = 1;
346 omap_st_start(mcbsp);
347 spin_unlock_irq(&mcbsp->lock);
348
349 return 0;
350}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000351
352static int omap_st_stop(struct omap_mcbsp *mcbsp)
353{
354 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
355
Peter Ujfalusi58db1dc2012-02-23 15:40:55 +0200356 if (st_data->running) {
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000357 if (!mcbsp->free) {
358 omap_st_off(mcbsp);
359 st_data->running = 0;
360 }
361 }
362
363 return 0;
364}
365
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200366int omap_st_disable(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000367{
Peter Ujfalusie2002ab2012-02-23 15:38:37 +0200368 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000369 int ret = 0;
370
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000371 if (!st_data)
372 return -ENODEV;
373
374 spin_lock_irq(&mcbsp->lock);
375 omap_st_stop(mcbsp);
376 st_data->enabled = 0;
377 spin_unlock_irq(&mcbsp->lock);
378
379 return ret;
380}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000381
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200382int omap_st_is_enabled(struct omap_mcbsp *mcbsp)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000383{
Peter Ujfalusie2002ab2012-02-23 15:38:37 +0200384 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000385
386 if (!st_data)
387 return -ENODEV;
388
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000389 return st_data->enabled;
390}
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000391
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300392/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300393 * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
394 * The threshold parameter is 1 based, and it is converted (threshold - 1)
395 * for the THRSH2 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300396 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200397void omap_mcbsp_set_tx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300398{
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300399 if (mcbsp->pdata->buffer_size == 0)
400 return;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300401
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300402 if (threshold && threshold <= mcbsp->max_tx_thres)
403 MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300404}
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300405
406/*
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300407 * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
408 * The threshold parameter is 1 based, and it is converted (threshold - 1)
409 * for the THRSH1 register.
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300410 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200411void omap_mcbsp_set_rx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300412{
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300413 if (mcbsp->pdata->buffer_size == 0)
414 return;
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300415
Peter Ujfalusi451fd822010-06-03 07:39:33 +0300416 if (threshold && threshold <= mcbsp->max_rx_thres)
417 MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
Eduardo Valentin7aa9ff52009-08-20 16:18:10 +0300418}
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300419
420/*
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200421 * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
422 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200423u16 omap_mcbsp_get_tx_delay(struct omap_mcbsp *mcbsp)
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200424{
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200425 u16 buffstat;
426
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300427 if (mcbsp->pdata->buffer_size == 0)
428 return 0;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200429
430 /* Returns the number of free locations in the buffer */
431 buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
432
433 /* Number of slots are different in McBSP ports */
Peter Ujfalusif10b8ad2010-06-03 07:39:34 +0300434 return mcbsp->pdata->buffer_size - buffstat;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200435}
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200436
437/*
438 * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
439 * to reach the threshold value (when the DMA will be triggered to read it)
440 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200441u16 omap_mcbsp_get_rx_delay(struct omap_mcbsp *mcbsp)
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200442{
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200443 u16 buffstat, threshold;
444
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300445 if (mcbsp->pdata->buffer_size == 0)
446 return 0;
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200447
448 /* Returns the number of used locations in the buffer */
449 buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
450 /* RX threshold */
451 threshold = MCBSP_READ(mcbsp, THRSH1);
452
453 /* Return the number of location till we reach the threshold limit */
454 if (threshold <= buffstat)
455 return 0;
456 else
457 return threshold - buffstat;
458}
Peter Ujfalusi7dc976e2010-03-03 15:08:08 +0200459
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200460int omap_mcbsp_request(struct omap_mcbsp *mcbsp)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100461{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800462 void *reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463 int err;
464
Jarkko Nikulaac6747c2011-09-26 10:45:43 +0300465 reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800466 if (!reg_cache) {
467 return -ENOMEM;
468 }
469
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300470 spin_lock(&mcbsp->lock);
471 if (!mcbsp->free) {
472 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
473 mcbsp->id);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800474 err = -EBUSY;
475 goto err_kfree;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100476 }
477
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800478 mcbsp->free = false;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800479 mcbsp->reg_cache = reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300480 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481
Russell Kingb820ce42009-01-23 10:26:46 +0000482 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200483 mcbsp->pdata->ops->request(mcbsp->id - 1);
Russell Kingb820ce42009-01-23 10:26:46 +0000484
Jarkko Nikula5a070552008-10-08 10:01:41 +0300485 /*
486 * Make sure that transmitter, receiver and sample-rate generator are
487 * not running before activating IRQs.
488 */
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800489 MCBSP_WRITE(mcbsp, SPCR1, 0);
490 MCBSP_WRITE(mcbsp, SPCR2, 0);
Jarkko Nikula5a070552008-10-08 10:01:41 +0300491
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000492 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
493 0, "McBSP", (void *)mcbsp);
494 if (err != 0) {
495 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
496 "for McBSP%d\n", mcbsp->tx_irq,
497 mcbsp->id);
498 goto err_clk_disable;
499 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100500
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000501 if (mcbsp->rx_irq) {
502 err = request_irq(mcbsp->rx_irq,
503 omap_mcbsp_rx_irq_handler,
504 0, "McBSP", (void *)mcbsp);
505 if (err != 0) {
506 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
507 "for McBSP%d\n", mcbsp->rx_irq,
508 mcbsp->id);
509 goto err_free_irq;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100510 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100511 }
512
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100513 return 0;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800514err_free_irq:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800515 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800516err_clk_disable:
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800517 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200518 mcbsp->pdata->ops->free(mcbsp->id - 1);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800519
Jarkko Nikula1a645882011-09-26 10:45:40 +0300520 /* Disable wakeup behavior */
521 if (mcbsp->pdata->has_wakeup)
522 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800523
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800524 spin_lock(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800525 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800526 mcbsp->reg_cache = NULL;
527err_kfree:
528 spin_unlock(&mcbsp->lock);
529 kfree(reg_cache);
Janusz Krzysztofik1866b542010-01-08 10:29:04 -0800530
531 return err;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100532}
533
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200534void omap_mcbsp_free(struct omap_mcbsp *mcbsp)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100535{
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800536 void *reg_cache;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300537
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300538 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200539 mcbsp->pdata->ops->free(mcbsp->id - 1);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300540
Jarkko Nikula1a645882011-09-26 10:45:40 +0300541 /* Disable wakeup behavior */
542 if (mcbsp->pdata->has_wakeup)
543 MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
Eero Nurkkala2122fdc2009-08-20 16:18:15 +0300544
Jarkko Nikulabafe2722011-06-14 11:23:52 +0000545 if (mcbsp->rx_irq)
546 free_irq(mcbsp->rx_irq, (void *)mcbsp);
547 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800549 reg_cache = mcbsp->reg_cache;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550
Peter Ujfalusie3866152012-03-05 11:32:27 +0200551 /*
552 * Select CLKS source from internal source unconditionally before
553 * marking the McBSP port as free.
554 * If the external clock source via MCBSP_CLKS pin has been selected the
555 * system will refuse to enter idle if the CLKS pin source is not reset
556 * back to internal source.
557 */
558 if (!cpu_class_is_omap1())
559 omap2_mcbsp_set_clks_src(mcbsp, MCBSP_CLKS_PRCM_SRC);
560
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800561 spin_lock(&mcbsp->lock);
562 if (mcbsp->free)
563 dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
564 else
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800565 mcbsp->free = true;
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800566 mcbsp->reg_cache = NULL;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300567 spin_unlock(&mcbsp->lock);
Janusz Krzysztofikc8c99692010-02-15 10:03:33 -0800568
569 if (reg_cache)
570 kfree(reg_cache);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571}
572
573/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300574 * Here we start the McBSP, by enabling transmitter, receiver or both.
575 * If no transmitter or receiver is active prior calling, then sample-rate
576 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577 */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200578void omap_mcbsp_start(struct omap_mcbsp *mcbsp, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579{
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000580 int enable_srg = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581 u16 w;
582
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300583 if (mcbsp->st_data)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000584 omap_st_start(mcbsp);
585
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000586 /* Only enable SRG, if McBSP is master */
587 w = MCBSP_READ_CACHE(mcbsp, PCR0);
588 if (w & (FSXM | FSRM | CLKXM | CLKRM))
589 enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
590 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300591
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000592 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300593 /* Start the sample generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800594 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800595 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300596 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597
598 /* Enable transmitter and receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300599 tx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800600 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800601 MCBSP_WRITE(mcbsp, SPCR2, w | tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100602
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300603 rx &= 1;
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800604 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800605 MCBSP_WRITE(mcbsp, SPCR1, w | rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100606
Eduardo Valentin44a63112009-08-20 16:18:09 +0300607 /*
608 * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
609 * REVISIT: 100us may give enough time for two CLKSRG, however
610 * due to some unknown PM related, clock gating etc. reason it
611 * is now at 500us.
612 */
613 udelay(500);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100614
Peter Ujfalusice3f0542010-08-31 08:11:44 +0000615 if (enable_srg) {
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300616 /* Start frame sync */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800617 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800618 MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300619 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620
Jarkko Nikula88408232011-09-26 10:45:41 +0300621 if (mcbsp->pdata->has_ccr) {
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300622 /* Release the transmitter and receiver */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800623 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300624 w &= ~(tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800625 MCBSP_WRITE(mcbsp, XCCR, w);
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800626 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300627 w &= ~(rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800628 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300629 }
630
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631 /* Dump McBSP Regs */
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200632 omap_mcbsp_dump_reg(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633}
634
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200635void omap_mcbsp_stop(struct omap_mcbsp *mcbsp, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100636{
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300637 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100638 u16 w;
639
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300640 /* Reset transmitter */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300641 tx &= 1;
Jarkko Nikula88408232011-09-26 10:45:41 +0300642 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800643 w = MCBSP_READ_CACHE(mcbsp, XCCR);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300644 w |= (tx ? XDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800645 MCBSP_WRITE(mcbsp, XCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300646 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800647 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800648 MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100649
650 /* Reset receiver */
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300651 rx &= 1;
Jarkko Nikula88408232011-09-26 10:45:41 +0300652 if (mcbsp->pdata->has_ccr) {
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800653 w = MCBSP_READ_CACHE(mcbsp, RCCR);
Jarkko Nikulaa93d4ed2009-10-14 09:56:35 -0700654 w |= (rx ? RDISABLE : 0);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800655 MCBSP_WRITE(mcbsp, RCCR, w);
Jarkko Nikulad09a2af2009-08-23 12:24:27 +0300656 }
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800657 w = MCBSP_READ_CACHE(mcbsp, SPCR1);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800658 MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100659
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800660 idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
661 MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300662
663 if (idle) {
664 /* Reset the sample rate generator */
Janusz Krzysztofik96fbd742010-02-15 10:03:33 -0800665 w = MCBSP_READ_CACHE(mcbsp, SPCR2);
Janusz Krzysztofik8ea32002010-02-15 10:03:32 -0800666 MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300667 }
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000668
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300669 if (mcbsp->st_data)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000670 omap_st_stop(mcbsp);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100671}
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672
Peter Ujfalusi45656b42012-02-14 18:20:58 +0200673int omap2_mcbsp_set_clks_src(struct omap_mcbsp *mcbsp, u8 fck_src_id)
Paul Walmsley69d042d2011-07-01 08:52:25 +0000674{
Jarkko Nikula09d28d22011-09-26 10:45:48 +0300675 const char *src;
Paul Walmsley69d042d2011-07-01 08:52:25 +0000676
Jarkko Nikula09d28d22011-09-26 10:45:48 +0300677 if (fck_src_id == MCBSP_CLKS_PAD_SRC)
678 src = "clks_ext";
679 else if (fck_src_id == MCBSP_CLKS_PRCM_SRC)
680 src = "clks_fclk";
681 else
682 return -EINVAL;
683
684 if (mcbsp->pdata->set_clk_src)
685 return mcbsp->pdata->set_clk_src(mcbsp->dev, mcbsp->fclk, src);
686 else
687 return -EINVAL;
688}
Jarkko Nikula09d28d22011-09-26 10:45:48 +0300689
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200690int omap_mcbsp_6pin_src_mux(struct omap_mcbsp *mcbsp, u8 mux)
Paul Walmsley69d042d2011-07-01 08:52:25 +0000691{
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200692 const char *signal, *src;
693 int ret = 0;
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300694
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200695 switch (mux) {
696 case CLKR_SRC_CLKR:
697 signal = "clkr";
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300698 src = "clkr";
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200699 break;
700 case CLKR_SRC_CLKX:
701 signal = "clkr";
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300702 src = "clkx";
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200703 break;
704 case FSR_SRC_FSR:
705 signal = "fsr";
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300706 src = "fsr";
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200707 break;
708 case FSR_SRC_FSX:
709 signal = "fsr";
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300710 src = "fsx";
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200711 break;
712 default:
713 return -EINVAL;
714 }
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300715
Jarkko Nikula7bc0c4b2011-09-26 10:45:49 +0300716 if (mcbsp->pdata->mux_signal)
Peter Ujfalusicd1f08c2012-03-08 11:01:37 +0200717 ret = mcbsp->pdata->mux_signal(mcbsp->dev, signal, src);
718
719 return ret;
Paul Walmsley69d042d2011-07-01 08:52:25 +0000720}
Paul Walmsley69d042d2011-07-01 08:52:25 +0000721
Eduardo Valentina1a56f52009-08-20 16:18:11 +0300722#define max_thres(m) (mcbsp->pdata->buffer_size)
723#define valid_threshold(m, val) ((val) <= max_thres(m))
724#define THRESHOLD_PROP_BUILDER(prop) \
725static ssize_t prop##_show(struct device *dev, \
726 struct device_attribute *attr, char *buf) \
727{ \
728 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
729 \
730 return sprintf(buf, "%u\n", mcbsp->prop); \
731} \
732 \
733static ssize_t prop##_store(struct device *dev, \
734 struct device_attribute *attr, \
735 const char *buf, size_t size) \
736{ \
737 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
738 unsigned long val; \
739 int status; \
740 \
741 status = strict_strtoul(buf, 0, &val); \
742 if (status) \
743 return status; \
744 \
745 if (!valid_threshold(mcbsp, val)) \
746 return -EDOM; \
747 \
748 mcbsp->prop = val; \
749 return size; \
750} \
751 \
752static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
753
754THRESHOLD_PROP_BUILDER(max_tx_thres);
755THRESHOLD_PROP_BUILDER(max_rx_thres);
756
Jarkko Nikula9b300502009-08-24 17:45:50 +0300757static const char *dma_op_modes[] = {
758 "element", "threshold", "frame",
759};
760
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300761static ssize_t dma_op_mode_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
763{
764 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +0300765 int dma_op_mode, i = 0;
766 ssize_t len = 0;
767 const char * const *s;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300768
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300769 dma_op_mode = mcbsp->dma_op_mode;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300770
Jarkko Nikula9b300502009-08-24 17:45:50 +0300771 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
772 if (dma_op_mode == i)
773 len += sprintf(buf + len, "[%s] ", *s);
774 else
775 len += sprintf(buf + len, "%s ", *s);
776 }
777 len += sprintf(buf + len, "\n");
778
779 return len;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300780}
781
782static ssize_t dma_op_mode_store(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t size)
785{
786 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
Jarkko Nikula9b300502009-08-24 17:45:50 +0300787 const char * const *s;
788 int i = 0;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300789
Jarkko Nikula9b300502009-08-24 17:45:50 +0300790 for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
791 if (sysfs_streq(buf, *s))
792 break;
793
794 if (i == ARRAY_SIZE(dma_op_modes))
795 return -EINVAL;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300796
797 spin_lock_irq(&mcbsp->lock);
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300798 if (!mcbsp->free) {
799 size = -EBUSY;
800 goto unlock;
801 }
Jarkko Nikula9b300502009-08-24 17:45:50 +0300802 mcbsp->dma_op_mode = i;
Peter Ujfalusi98cb20e2009-08-20 16:18:14 +0300803
804unlock:
805 spin_unlock_irq(&mcbsp->lock);
806
807 return size;
808}
809
810static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
811
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300812static const struct attribute *additional_attrs[] = {
813 &dev_attr_max_tx_thres.attr,
814 &dev_attr_max_rx_thres.attr,
815 &dev_attr_dma_op_mode.attr,
816 NULL,
817};
818
819static const struct attribute_group additional_attr_group = {
820 .attrs = (struct attribute **)additional_attrs,
821};
822
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000823static ssize_t st_taps_show(struct device *dev,
824 struct device_attribute *attr, char *buf)
825{
826 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
827 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
828 ssize_t status = 0;
829 int i;
830
831 spin_lock_irq(&mcbsp->lock);
832 for (i = 0; i < st_data->nr_taps; i++)
833 status += sprintf(&buf[status], (i ? ", %d" : "%d"),
834 st_data->taps[i]);
835 if (i)
836 status += sprintf(&buf[status], "\n");
837 spin_unlock_irq(&mcbsp->lock);
838
839 return status;
840}
841
842static ssize_t st_taps_store(struct device *dev,
843 struct device_attribute *attr,
844 const char *buf, size_t size)
845{
846 struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
847 struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
848 int val, tmp, status, i = 0;
849
850 spin_lock_irq(&mcbsp->lock);
851 memset(st_data->taps, 0, sizeof(st_data->taps));
852 st_data->nr_taps = 0;
853
854 do {
855 status = sscanf(buf, "%d%n", &val, &tmp);
856 if (status < 0 || status == 0) {
857 size = -EINVAL;
858 goto out;
859 }
860 if (val < -32768 || val > 32767) {
861 size = -EINVAL;
862 goto out;
863 }
864 st_data->taps[i++] = val;
865 buf += tmp;
866 if (*buf != ',')
867 break;
868 buf++;
869 } while (1);
870
871 st_data->nr_taps = i;
872
873out:
874 spin_unlock_irq(&mcbsp->lock);
875
876 return size;
877}
878
879static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
880
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000881static const struct attribute *sidetone_attrs[] = {
882 &dev_attr_st_taps.attr,
883 NULL,
884};
885
886static const struct attribute_group sidetone_attr_group = {
887 .attrs = (struct attribute **)sidetone_attrs,
888};
889
Jarkko Nikulaf821eec2011-09-26 10:45:45 +0300890static int __devinit omap_st_add(struct omap_mcbsp *mcbsp,
891 struct resource *res)
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000892{
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000893 struct omap_mcbsp_st_data *st_data;
894 int err;
895
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200896 st_data = devm_kzalloc(mcbsp->dev, sizeof(*mcbsp->st_data), GFP_KERNEL);
897 if (!st_data)
898 return -ENOMEM;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000899
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200900 st_data->io_base_st = devm_ioremap(mcbsp->dev, res->start,
901 resource_size(res));
902 if (!st_data->io_base_st)
903 return -ENOMEM;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000904
905 err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
906 if (err)
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200907 return err;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000908
909 mcbsp->st_data = st_data;
910 return 0;
Eero Nurkkalad912fa92010-02-22 12:21:11 +0000911}
912
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100913/*
914 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
915 * 730 has only 2 McBSP, and both of them are MPU peripherals.
916 */
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200917int __devinit omap_mcbsp_init(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100918{
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200919 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800920 struct resource *res;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300921 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100922
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300923 spin_lock_init(&mcbsp->lock);
Shubhrajyoti D6722a722010-12-07 16:25:41 -0800924 mcbsp->free = true;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300925
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800926 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
927 if (!res) {
928 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
929 if (!res) {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200930 dev_err(mcbsp->dev, "invalid memory resource\n");
931 return -ENOMEM;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800932 }
933 }
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200934 if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
935 dev_name(&pdev->dev))) {
936 dev_err(mcbsp->dev, "memory region already claimed\n");
937 return -ENODEV;
938 }
939
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800940 mcbsp->phys_base = res->start;
Jarkko Nikulaac6747c2011-09-26 10:45:43 +0300941 mcbsp->reg_cache_size = resource_size(res);
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200942 mcbsp->io_base = devm_ioremap(&pdev->dev, res->start,
943 resource_size(res));
944 if (!mcbsp->io_base)
945 return -ENOMEM;
Russell Kingd592dd12008-09-04 14:25:42 +0100946
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800947 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
948 if (!res)
949 mcbsp->phys_dma_base = mcbsp->phys_base;
950 else
951 mcbsp->phys_dma_base = res->start;
952
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800953 mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
954 mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
955
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +0530956 /* From OMAP4 there will be a single irq line */
Peter Ujfalusi73c95222012-03-07 11:15:37 +0200957 if (mcbsp->tx_irq == -ENXIO) {
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +0530958 mcbsp->tx_irq = platform_get_irq(pdev, 0);
Peter Ujfalusi73c95222012-03-07 11:15:37 +0200959 mcbsp->rx_irq = 0;
960 }
Kishon Vijay Abraham Icb7e9de2011-02-24 15:16:50 +0530961
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800962 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
963 if (!res) {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200964 dev_err(&pdev->dev, "invalid rx DMA channel\n");
965 return -ENODEV;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800966 }
Peter Ujfalusib8fb4902012-02-14 15:41:29 +0200967 /* RX DMA request number, and port address configuration */
968 mcbsp->dma_data[1].name = "Audio Capture";
969 mcbsp->dma_data[1].dma_req = res->start;
970 mcbsp->dma_data[1].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 1);
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800971
972 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
973 if (!res) {
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200974 dev_err(&pdev->dev, "invalid tx DMA channel\n");
975 return -ENODEV;
Kishon Vijay Abraham I3cf32bb2011-02-24 12:51:45 -0800976 }
Peter Ujfalusib8fb4902012-02-14 15:41:29 +0200977 /* TX DMA request number, and port address configuration */
978 mcbsp->dma_data[0].name = "Audio Playback";
979 mcbsp->dma_data[0].dma_req = res->start;
980 mcbsp->dma_data[0].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 0);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300981
Russell Kingb820ce42009-01-23 10:26:46 +0000982 mcbsp->fclk = clk_get(&pdev->dev, "fck");
983 if (IS_ERR(mcbsp->fclk)) {
984 ret = PTR_ERR(mcbsp->fclk);
Peter Ujfalusi2ee65952012-02-14 14:52:42 +0200985 dev_err(mcbsp->dev, "unable to get fck: %d\n", ret);
986 return ret;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300987 }
988
Jarkko Nikula7bba67a2011-09-26 10:45:42 +0300989 mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
990 if (mcbsp->pdata->buffer_size) {
991 /*
992 * Initially configure the maximum thresholds to a safe value.
993 * The McBSP FIFO usage with these values should not go under
994 * 16 locations.
995 * If the whole FIFO without safety buffer is used, than there
996 * is a possibility that the DMA will be not able to push the
997 * new data on time, causing channel shifts in runtime.
998 */
999 mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
1000 mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
1001
1002 ret = sysfs_create_group(&mcbsp->dev->kobj,
1003 &additional_attr_group);
1004 if (ret) {
1005 dev_err(mcbsp->dev,
1006 "Unable to create additional controls\n");
1007 goto err_thres;
1008 }
1009 } else {
1010 mcbsp->max_tx_thres = -EINVAL;
1011 mcbsp->max_rx_thres = -EINVAL;
1012 }
1013
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001014 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
1015 if (res) {
1016 ret = omap_st_add(mcbsp, res);
1017 if (ret) {
1018 dev_err(mcbsp->dev,
1019 "Unable to create sidetone controls\n");
1020 goto err_st;
1021 }
1022 }
Eduardo Valentina1a56f52009-08-20 16:18:11 +03001023
Russell Kingd592dd12008-09-04 14:25:42 +01001024 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001025
Jarkko Nikulaf821eec2011-09-26 10:45:45 +03001026err_st:
1027 if (mcbsp->pdata->buffer_size)
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001028 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
Jarkko Nikula7bba67a2011-09-26 10:45:42 +03001029err_thres:
1030 clk_put(mcbsp->fclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001031 return ret;
1032}
1033
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001034void __devexit omap_mcbsp_sysfs_remove(struct omap_mcbsp *mcbsp)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001035{
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001036 if (mcbsp->pdata->buffer_size)
1037 sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001038
Peter Ujfalusi2ee65952012-02-14 14:52:42 +02001039 if (mcbsp->st_data)
1040 sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001041}