blob: 2d33a89f147a3aebccbd11048e8e4cb707fb9c79 [file] [log] [blame]
Jarkko Nikula2e747962008-04-25 13:55:19 +02001/*
2 * omap-mcbsp.c -- OMAP ALSA SoC DAI driver using McBSP port
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Jarkko Nikulab08f7a62009-04-17 14:42:26 +03006 * Contact: Jarkko Nikula <jhnikula@gmail.com>
7 * Peter Ujfalusi <peter.ujfalusi@nokia.com>
Jarkko Nikula2e747962008-04-25 13:55:19 +02008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/initval.h>
32#include <sound/soc.h>
33
Tony Lindgrence491cf2009-10-20 09:40:47 -070034#include <plat/control.h>
35#include <plat/dma.h>
36#include <plat/mcbsp.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020037#include "omap-mcbsp.h"
38#include "omap-pcm.h"
39
Jarkko Nikula0b604852008-11-12 17:05:51 +020040#define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
Jarkko Nikula2e747962008-04-25 13:55:19 +020041
Ilkka Koskinen83905c12010-02-22 12:21:12 +000042#define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \
43 xhandler_get, xhandler_put) \
44{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
45 .info = omap_mcbsp_st_info_volsw, \
46 .get = xhandler_get, .put = xhandler_put, \
47 .private_value = (unsigned long) &(struct soc_mixer_control) \
48 {.min = xmin, .max = xmax} }
49
Jarkko Nikula2e747962008-04-25 13:55:19 +020050struct omap_mcbsp_data {
51 unsigned int bus_id;
52 struct omap_mcbsp_reg_cfg regs;
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +030053 unsigned int fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +020054 /*
55 * Flags indicating is the bus already activated and configured by
56 * another substream
57 */
58 int active;
59 int configured;
Graeme Gregory5f63ef92009-11-09 19:02:15 +000060 unsigned int in_freq;
61 int clk_div;
Jarkko Nikula2e747962008-04-25 13:55:19 +020062};
63
64#define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id)
65
66static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
67
68/*
69 * Stream DMA parameters. DMA request line and port address are set runtime
70 * since they are different between OMAP1 and later OMAPs
71 */
Jarkko Nikula2e897132008-10-09 15:57:21 +030072static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
Jarkko Nikula2e747962008-04-25 13:55:19 +020073
74#if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX)
75static const int omap1_dma_reqs[][2] = {
76 { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX },
77 { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX },
78 { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX },
79};
80static const unsigned long omap1_mcbsp_port[][2] = {
81 { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
82 OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
83 { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
84 OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
85 { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1,
86 OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 },
87};
88#else
89static const int omap1_dma_reqs[][2] = {};
90static const unsigned long omap1_mcbsp_port[][2] = {};
91#endif
Jarkko Nikula406e2c42008-10-09 15:57:20 +030092
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080093#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Jarkko Nikula406e2c42008-10-09 15:57:20 +030094static const int omap24xx_dma_reqs[][2] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +020095 { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
96 { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080097#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
Jarkko Nikula406e2c42008-10-09 15:57:20 +030098 { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX },
99 { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX },
100 { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX },
101#endif
Jarkko Nikula2e747962008-04-25 13:55:19 +0200102};
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300103#else
104static const int omap24xx_dma_reqs[][2] = {};
105#endif
106
107#if defined(CONFIG_ARCH_OMAP2420)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200108static const unsigned long omap2420_mcbsp_port[][2] = {
109 { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
110 OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
111 { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
112 OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
113};
114#else
Jarkko Nikula2e747962008-04-25 13:55:19 +0200115static const unsigned long omap2420_mcbsp_port[][2] = {};
116#endif
117
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300118#if defined(CONFIG_ARCH_OMAP2430)
119static const unsigned long omap2430_mcbsp_port[][2] = {
120 { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
121 OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
122 { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
123 OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
124 { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
125 OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
126 { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
127 OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
128 { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
129 OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
130};
131#else
132static const unsigned long omap2430_mcbsp_port[][2] = {};
133#endif
134
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800135#if defined(CONFIG_ARCH_OMAP3)
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300136static const unsigned long omap34xx_mcbsp_port[][2] = {
137 { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
138 OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
139 { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
140 OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
141 { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
142 OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
143 { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
144 OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
145 { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
146 OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
147};
148#else
149static const unsigned long omap34xx_mcbsp_port[][2] = {};
150#endif
151
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300152static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
153{
154 struct snd_soc_pcm_runtime *rtd = substream->private_data;
155 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
156 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300157 int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
158 int samples;
159
160 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
161 if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
162 samples = snd_pcm_lib_period_bytes(substream) >> 1;
163 else
164 samples = 1;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300165
166 /* Configure McBSP internal buffer usage */
167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
168 omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, samples - 1);
169 else
170 omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, samples - 1);
171}
172
Mark Browndee89c42008-11-18 22:11:38 +0000173static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
174 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200175{
176 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100177 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200178 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300179 int bus_id = mcbsp_data->bus_id;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200180 int err = 0;
181
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300182 if (!cpu_dai->active)
183 err = omap_mcbsp_request(bus_id);
184
185 if (cpu_is_omap343x()) {
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300186 int dma_op_mode = omap_mcbsp_get_dma_op_mode(bus_id);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300187 int max_period;
188
Jarkko Nikula69849922009-03-27 15:32:01 +0200189 /*
190 * McBSP2 in OMAP3 has 1024 * 32-bit internal audio buffer.
191 * Set constraint for minimum buffer size to the same than FIFO
192 * size in order to avoid underruns in playback startup because
193 * HW is keeping the DMA request active until FIFO is filled.
194 */
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300195 if (bus_id == 1)
196 snd_pcm_hw_constraint_minmax(substream->runtime,
197 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
198 4096, UINT_MAX);
Jarkko Nikula69849922009-03-27 15:32:01 +0200199
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300200 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
201 max_period = omap_mcbsp_get_max_tx_threshold(bus_id);
202 else
203 max_period = omap_mcbsp_get_max_rx_threshold(bus_id);
204
205 max_period++;
206 max_period <<= 1;
207
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300208 if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
209 snd_pcm_hw_constraint_minmax(substream->runtime,
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300210 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
211 32, max_period);
212 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200213
214 return err;
215}
216
Mark Browndee89c42008-11-18 22:11:38 +0000217static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
218 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200219{
220 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100221 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200222 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
223
224 if (!cpu_dai->active) {
225 omap_mcbsp_free(mcbsp_data->bus_id);
226 mcbsp_data->configured = 0;
227 }
228}
229
Mark Browndee89c42008-11-18 22:11:38 +0000230static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
231 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200232{
233 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100234 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200235 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300236 int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200237
238 switch (cmd) {
239 case SNDRV_PCM_TRIGGER_START:
240 case SNDRV_PCM_TRIGGER_RESUME:
241 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300242 mcbsp_data->active++;
243 omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200244 break;
245
246 case SNDRV_PCM_TRIGGER_STOP:
247 case SNDRV_PCM_TRIGGER_SUSPEND:
248 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300249 omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
250 mcbsp_data->active--;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200251 break;
252 default:
253 err = -EINVAL;
254 }
255
256 return err;
257}
258
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200259static snd_pcm_sframes_t omap_mcbsp_dai_delay(
260 struct snd_pcm_substream *substream,
261 struct snd_soc_dai *dai)
262{
263 struct snd_soc_pcm_runtime *rtd = substream->private_data;
264 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
265 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
266 u16 fifo_use;
267 snd_pcm_sframes_t delay;
268
269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
270 fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id);
271 else
272 fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id);
273
274 /*
275 * Divide the used locations with the channel count to get the
276 * FIFO usage in samples (don't care about partial samples in the
277 * buffer).
278 */
279 delay = fifo_use / substream->runtime->channels;
280
281 return delay;
282}
283
Jarkko Nikula2e747962008-04-25 13:55:19 +0200284static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000285 struct snd_pcm_hw_params *params,
286 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200287{
288 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100289 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200290 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
291 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
292 int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300293 int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200294 unsigned long port;
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000295 unsigned int format, div, framesize, master;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296
297 if (cpu_class_is_omap1()) {
298 dma = omap1_dma_reqs[bus_id][substream->stream];
299 port = omap1_mcbsp_port[bus_id][substream->stream];
300 } else if (cpu_is_omap2420()) {
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300301 dma = omap24xx_dma_reqs[bus_id][substream->stream];
Jarkko Nikula2e747962008-04-25 13:55:19 +0200302 port = omap2420_mcbsp_port[bus_id][substream->stream];
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300303 } else if (cpu_is_omap2430()) {
304 dma = omap24xx_dma_reqs[bus_id][substream->stream];
305 port = omap2430_mcbsp_port[bus_id][substream->stream];
306 } else if (cpu_is_omap343x()) {
307 dma = omap24xx_dma_reqs[bus_id][substream->stream];
308 port = omap34xx_mcbsp_port[bus_id][substream->stream];
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300309 omap_mcbsp_dai_dma_params[id][substream->stream].set_threshold =
310 omap_mcbsp_set_threshold;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300311 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
312 if (omap_mcbsp_get_dma_op_mode(bus_id) ==
313 MCBSP_DMA_MODE_THRESHOLD)
314 sync_mode = OMAP_DMA_SYNC_FRAME;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200315 } else {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200316 return -ENODEV;
317 }
Jarkko Nikula2e897132008-10-09 15:57:21 +0300318 omap_mcbsp_dai_dma_params[id][substream->stream].name =
319 substream->stream ? "Audio Capture" : "Audio Playback";
Jarkko Nikula2e747962008-04-25 13:55:19 +0200320 omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma;
321 omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300322 omap_mcbsp_dai_dma_params[id][substream->stream].sync_mode = sync_mode;
Misael Lopez Cruze17dd322010-02-22 15:09:19 -0600323 omap_mcbsp_dai_dma_params[id][substream->stream].data_type =
324 OMAP_DMA_DATA_TYPE_S16;
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000325
326 snd_soc_dai_set_dma_data(cpu_dai, substream,
327 &omap_mcbsp_dai_dma_params[id][substream->stream]);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200328
329 if (mcbsp_data->configured) {
330 /* McBSP already configured by another stream */
331 return 0;
332 }
333
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300334 format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
335 wpf = channels = params_channels(params);
Peter Ujfalusi299a1512010-03-19 12:27:31 +0200336 if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
337 format == SND_SOC_DAIFMT_LEFT_J)) {
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000338 /* Use dual-phase frames */
339 regs->rcr2 |= RPHASE;
340 regs->xcr2 |= XPHASE;
341 /* Set 1 word per (McBSP) frame for phase1 and phase2 */
342 wpf--;
343 regs->rcr2 |= RFRLEN2(wpf - 1);
344 regs->xcr2 |= XFRLEN2(wpf - 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200345 }
346
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000347 regs->rcr1 |= RFRLEN1(wpf - 1);
348 regs->xcr1 |= XFRLEN1(wpf - 1);
349
Jarkko Nikula2e747962008-04-25 13:55:19 +0200350 switch (params_format(params)) {
351 case SNDRV_PCM_FORMAT_S16_LE:
352 /* Set word lengths */
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300353 wlen = 16;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200354 regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
355 regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
356 regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
357 regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200358 break;
359 default:
360 /* Unsupported PCM format */
361 return -EINVAL;
362 }
363
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000364 /* In McBSP master modes, FRAME (i.e. sample rate) is generated
365 * by _counting_ BCLKs. Calculate frame size in BCLKs */
366 master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
367 if (master == SND_SOC_DAIFMT_CBS_CFS) {
368 div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
369 framesize = (mcbsp_data->in_freq / div) / params_rate(params);
370
371 if (framesize < wlen * channels) {
372 printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
373 "channels\n", __func__);
374 return -EINVAL;
375 }
376 } else
377 framesize = wlen * channels;
378
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300379 /* Set FS period and length in terms of bit clock periods */
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300380 switch (format) {
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300381 case SND_SOC_DAIFMT_I2S:
Peter Ujfalusi299a1512010-03-19 12:27:31 +0200382 case SND_SOC_DAIFMT_LEFT_J:
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000383 regs->srgr2 |= FPER(framesize - 1);
384 regs->srgr1 |= FWID((framesize >> 1) - 1);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300385 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +0300386 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulabd258672008-12-22 10:21:36 +0200387 case SND_SOC_DAIFMT_DSP_B:
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000388 regs->srgr2 |= FPER(framesize - 1);
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300389 regs->srgr1 |= FWID(0);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300390 break;
391 }
392
Jarkko Nikula2e747962008-04-25 13:55:19 +0200393 omap_mcbsp_config(bus_id, &mcbsp_data->regs);
394 mcbsp_data->configured = 1;
395
396 return 0;
397}
398
399/*
400 * This must be called before _set_clkdiv and _set_sysclk since McBSP register
401 * cache is initialized here
402 */
Liam Girdwood8687eb82008-07-07 16:08:07 +0100403static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200404 unsigned int fmt)
405{
406 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
407 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300408 unsigned int temp_fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200409
410 if (mcbsp_data->configured)
411 return 0;
412
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300413 mcbsp_data->fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200414 memset(regs, 0, sizeof(*regs));
415 /* Generic McBSP register settings */
416 regs->spcr2 |= XINTM(3) | FREE;
417 regs->spcr1 |= RINTM(3);
Eero Nurkkalac721bbd2009-08-20 16:18:23 +0300418 /* RFIG and XFIG are not defined in 34xx */
419 if (!cpu_is_omap34xx()) {
420 regs->rcr2 |= RFIG;
421 regs->xcr2 |= XFIG;
422 }
Misael Lopez Cruzef390c02009-01-29 13:29:46 +0200423 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
Jarkko Nikula32080af2009-08-23 12:24:26 +0300424 regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
425 regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
Misael Lopez Cruzef390c02009-01-29 13:29:46 +0200426 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200427
428 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
429 case SND_SOC_DAIFMT_I2S:
430 /* 1-bit data delay */
431 regs->rcr2 |= RDATDLY(1);
432 regs->xcr2 |= XDATDLY(1);
433 break;
Peter Ujfalusi299a1512010-03-19 12:27:31 +0200434 case SND_SOC_DAIFMT_LEFT_J:
435 /* 0-bit data delay */
436 regs->rcr2 |= RDATDLY(0);
437 regs->xcr2 |= XDATDLY(0);
438 regs->spcr1 |= RJUST(2);
439 /* Invert FS polarity configuration */
440 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
441 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +0300442 case SND_SOC_DAIFMT_DSP_A:
443 /* 1-bit data delay */
444 regs->rcr2 |= RDATDLY(1);
445 regs->xcr2 |= XDATDLY(1);
446 /* Invert FS polarity configuration */
447 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
448 break;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200449 case SND_SOC_DAIFMT_DSP_B:
Arun KS3336c5b2008-10-02 15:07:06 +0530450 /* 0-bit data delay */
451 regs->rcr2 |= RDATDLY(0);
452 regs->xcr2 |= XDATDLY(0);
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300453 /* Invert FS polarity configuration */
454 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
Arun KS3336c5b2008-10-02 15:07:06 +0530455 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200456 default:
457 /* Unsupported data format */
458 return -EINVAL;
459 }
460
461 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
462 case SND_SOC_DAIFMT_CBS_CFS:
463 /* McBSP master. Set FS and bit clocks as outputs */
464 regs->pcr0 |= FSXM | FSRM |
465 CLKXM | CLKRM;
466 /* Sample rate generator drives the FS */
467 regs->srgr2 |= FSGM;
468 break;
469 case SND_SOC_DAIFMT_CBM_CFM:
470 /* McBSP slave */
471 break;
472 default:
473 /* Unsupported master/slave configuration */
474 return -EINVAL;
475 }
476
477 /* Set bit clock (CLKX/CLKR) and FS polarities */
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300478 switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200479 case SND_SOC_DAIFMT_NB_NF:
480 /*
481 * Normal BCLK + FS.
482 * FS active low. TX data driven on falling edge of bit clock
483 * and RX data sampled on rising edge of bit clock.
484 */
485 regs->pcr0 |= FSXP | FSRP |
486 CLKXP | CLKRP;
487 break;
488 case SND_SOC_DAIFMT_NB_IF:
489 regs->pcr0 |= CLKXP | CLKRP;
490 break;
491 case SND_SOC_DAIFMT_IB_NF:
492 regs->pcr0 |= FSXP | FSRP;
493 break;
494 case SND_SOC_DAIFMT_IB_IF:
495 break;
496 default:
497 return -EINVAL;
498 }
499
500 return 0;
501}
502
Liam Girdwood8687eb82008-07-07 16:08:07 +0100503static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200504 int div_id, int div)
505{
506 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
507 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
508
509 if (div_id != OMAP_MCBSP_CLKGDV)
510 return -ENODEV;
511
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000512 mcbsp_data->clk_div = div;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200513 regs->srgr1 |= CLKGDV(div - 1);
514
515 return 0;
516}
517
518static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
519 int clk_id)
520{
521 int sel_bit;
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300522 u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200523
524 if (cpu_class_is_omap1()) {
525 /* OMAP1's can use only external source clock */
526 if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
527 return -EINVAL;
528 else
529 return 0;
530 }
531
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300532 if (cpu_is_omap2420() && mcbsp_data->bus_id > 1)
533 return -EINVAL;
534
535 if (cpu_is_omap343x())
536 reg_devconf1 = OMAP343X_CONTROL_DEVCONF1;
537
Jarkko Nikula2e747962008-04-25 13:55:19 +0200538 switch (mcbsp_data->bus_id) {
539 case 0:
540 reg = OMAP2_CONTROL_DEVCONF0;
541 sel_bit = 2;
542 break;
543 case 1:
544 reg = OMAP2_CONTROL_DEVCONF0;
545 sel_bit = 6;
546 break;
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300547 case 2:
548 reg = reg_devconf1;
549 sel_bit = 0;
550 break;
551 case 3:
552 reg = reg_devconf1;
553 sel_bit = 2;
554 break;
555 case 4:
556 reg = reg_devconf1;
557 sel_bit = 4;
558 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200559 default:
560 return -EINVAL;
561 }
562
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300563 if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)
564 omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
565 else
566 omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200567
568 return 0;
569}
570
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300571static int omap_mcbsp_dai_set_rcvr_src(struct omap_mcbsp_data *mcbsp_data,
572 int clk_id)
573{
574 int sel_bit, set = 0;
575 u16 reg = OMAP2_CONTROL_DEVCONF0;
576
577 if (cpu_class_is_omap1())
578 return -EINVAL; /* TODO: Can this be implemented for OMAP1? */
579 if (mcbsp_data->bus_id != 0)
580 return -EINVAL;
581
582 switch (clk_id) {
583 case OMAP_MCBSP_CLKR_SRC_CLKX:
584 set = 1;
585 case OMAP_MCBSP_CLKR_SRC_CLKR:
586 sel_bit = 3;
587 break;
588 case OMAP_MCBSP_FSR_SRC_FSX:
589 set = 1;
590 case OMAP_MCBSP_FSR_SRC_FSR:
591 sel_bit = 4;
592 break;
593 default:
594 return -EINVAL;
595 }
596
597 if (set)
598 omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
599 else
600 omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
601
602 return 0;
603}
604
Liam Girdwood8687eb82008-07-07 16:08:07 +0100605static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200606 int clk_id, unsigned int freq,
607 int dir)
608{
609 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
610 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
611 int err = 0;
612
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000613 mcbsp_data->in_freq = freq;
614
Jarkko Nikula2e747962008-04-25 13:55:19 +0200615 switch (clk_id) {
616 case OMAP_MCBSP_SYSCLK_CLK:
617 regs->srgr2 |= CLKSM;
618 break;
619 case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
620 case OMAP_MCBSP_SYSCLK_CLKS_EXT:
621 err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
622 break;
623
624 case OMAP_MCBSP_SYSCLK_CLKX_EXT:
625 regs->srgr2 |= CLKSM;
626 case OMAP_MCBSP_SYSCLK_CLKR_EXT:
627 regs->pcr0 |= SCLKME;
628 break;
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300629
630 case OMAP_MCBSP_CLKR_SRC_CLKR:
631 case OMAP_MCBSP_CLKR_SRC_CLKX:
632 case OMAP_MCBSP_FSR_SRC_FSR:
633 case OMAP_MCBSP_FSR_SRC_FSX:
634 err = omap_mcbsp_dai_set_rcvr_src(mcbsp_data, clk_id);
635 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200636 default:
637 err = -ENODEV;
638 }
639
640 return err;
641}
642
Eric Miao6335d052009-03-03 09:41:00 +0800643static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
644 .startup = omap_mcbsp_dai_startup,
645 .shutdown = omap_mcbsp_dai_shutdown,
646 .trigger = omap_mcbsp_dai_trigger,
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200647 .delay = omap_mcbsp_dai_delay,
Eric Miao6335d052009-03-03 09:41:00 +0800648 .hw_params = omap_mcbsp_dai_hw_params,
649 .set_fmt = omap_mcbsp_dai_set_dai_fmt,
650 .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
651 .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
652};
653
Jarkko Nikula8def4642008-10-09 15:57:22 +0300654#define OMAP_MCBSP_DAI_BUILDER(link_id) \
655{ \
Jarkko Nikula0c758bd2008-11-21 14:31:33 +0200656 .name = "omap-mcbsp-dai-"#link_id, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300657 .id = (link_id), \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300658 .playback = { \
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200659 .channels_min = 1, \
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000660 .channels_max = 16, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300661 .rates = OMAP_MCBSP_RATES, \
662 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
663 }, \
664 .capture = { \
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200665 .channels_min = 1, \
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000666 .channels_max = 16, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300667 .rates = OMAP_MCBSP_RATES, \
668 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
669 }, \
Eric Miao6335d052009-03-03 09:41:00 +0800670 .ops = &omap_mcbsp_dai_ops, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300671 .private_data = &mcbsp_data[(link_id)].bus_id, \
672}
673
674struct snd_soc_dai omap_mcbsp_dai[] = {
675 OMAP_MCBSP_DAI_BUILDER(0),
676 OMAP_MCBSP_DAI_BUILDER(1),
677#if NUM_LINKS >= 3
678 OMAP_MCBSP_DAI_BUILDER(2),
679#endif
680#if NUM_LINKS == 5
681 OMAP_MCBSP_DAI_BUILDER(3),
682 OMAP_MCBSP_DAI_BUILDER(4),
683#endif
Jarkko Nikula2e747962008-04-25 13:55:19 +0200684};
Jarkko Nikula8def4642008-10-09 15:57:22 +0300685
Jarkko Nikula2e747962008-04-25 13:55:19 +0200686EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
687
Ilkka Koskinen83905c12010-02-22 12:21:12 +0000688int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
689 struct snd_ctl_elem_info *uinfo)
690{
691 struct soc_mixer_control *mc =
692 (struct soc_mixer_control *)kcontrol->private_value;
693 int max = mc->max;
694 int min = mc->min;
695
696 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
697 uinfo->count = 1;
698 uinfo->value.integer.min = min;
699 uinfo->value.integer.max = max;
700 return 0;
701}
702
703#define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \
704static int \
705omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
706 struct snd_ctl_elem_value *uc) \
707{ \
708 struct soc_mixer_control *mc = \
709 (struct soc_mixer_control *)kc->private_value; \
710 int max = mc->max; \
711 int min = mc->min; \
712 int val = uc->value.integer.value[0]; \
713 \
714 if (val < min || val > max) \
715 return -EINVAL; \
716 \
717 /* OMAP McBSP implementation uses index values 0..4 */ \
718 return omap_st_set_chgain((id)-1, channel, val); \
719}
720
721#define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \
722static int \
723omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \
724 struct snd_ctl_elem_value *uc) \
725{ \
726 s16 chgain; \
727 \
728 if (omap_st_get_chgain((id)-1, channel, &chgain)) \
729 return -EAGAIN; \
730 \
731 uc->value.integer.value[0] = chgain; \
732 return 0; \
733}
734
735OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
736OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
737OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
738OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
739OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
740OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
741OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
742OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
743
744static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
745 struct snd_ctl_elem_value *ucontrol)
746{
747 struct soc_mixer_control *mc =
748 (struct soc_mixer_control *)kcontrol->private_value;
749 u8 value = ucontrol->value.integer.value[0];
750
751 if (value == omap_st_is_enabled(mc->reg))
752 return 0;
753
754 if (value)
755 omap_st_enable(mc->reg);
756 else
757 omap_st_disable(mc->reg);
758
759 return 1;
760}
761
762static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
763 struct snd_ctl_elem_value *ucontrol)
764{
765 struct soc_mixer_control *mc =
766 (struct soc_mixer_control *)kcontrol->private_value;
767
768 ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
769 return 0;
770}
771
772static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
773 SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
774 omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
775 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
776 -32768, 32767,
777 omap_mcbsp2_get_st_ch0_volume,
778 omap_mcbsp2_set_st_ch0_volume),
779 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
780 -32768, 32767,
781 omap_mcbsp2_get_st_ch1_volume,
782 omap_mcbsp2_set_st_ch1_volume),
783};
784
785static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
786 SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
787 omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
788 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
789 -32768, 32767,
790 omap_mcbsp3_get_st_ch0_volume,
791 omap_mcbsp3_set_st_ch0_volume),
792 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
793 -32768, 32767,
794 omap_mcbsp3_get_st_ch1_volume,
795 omap_mcbsp3_set_st_ch1_volume),
796};
797
798int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
799{
800 if (!cpu_is_omap34xx())
801 return -ENODEV;
802
803 switch (mcbsp_id) {
804 case 1: /* McBSP 2 */
805 return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
806 ARRAY_SIZE(omap_mcbsp2_st_controls));
807 case 2: /* McBSP 3 */
808 return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
809 ARRAY_SIZE(omap_mcbsp3_st_controls));
810 default:
811 break;
812 }
813
814 return -EINVAL;
815}
816EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
817
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100818static int __init snd_omap_mcbsp_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000819{
820 return snd_soc_register_dais(omap_mcbsp_dai,
821 ARRAY_SIZE(omap_mcbsp_dai));
822}
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100823module_init(snd_omap_mcbsp_init);
Mark Brown3f4b7832008-12-03 19:26:35 +0000824
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100825static void __exit snd_omap_mcbsp_exit(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000826{
827 snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai));
828}
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100829module_exit(snd_omap_mcbsp_exit);
Mark Brown3f4b7832008-12-03 19:26:35 +0000830
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300831MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200832MODULE_DESCRIPTION("OMAP I2S SoC Interface");
833MODULE_LICENSE("GPL");