blob: 89e8bce114af4b978f303ad2d75a5116329654fe [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
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/control.h>
35#include <mach/dma.h>
36#include <mach/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
42struct omap_mcbsp_data {
43 unsigned int bus_id;
44 struct omap_mcbsp_reg_cfg regs;
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +030045 unsigned int fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +020046 /*
47 * Flags indicating is the bus already activated and configured by
48 * another substream
49 */
50 int active;
51 int configured;
52};
53
54#define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id)
55
56static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
57
58/*
59 * Stream DMA parameters. DMA request line and port address are set runtime
60 * since they are different between OMAP1 and later OMAPs
61 */
Jarkko Nikula2e897132008-10-09 15:57:21 +030062static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
Jarkko Nikula2e747962008-04-25 13:55:19 +020063
64#if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX)
65static const int omap1_dma_reqs[][2] = {
66 { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX },
67 { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX },
68 { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX },
69};
70static const unsigned long omap1_mcbsp_port[][2] = {
71 { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
72 OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
73 { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
74 OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
75 { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1,
76 OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 },
77};
78#else
79static const int omap1_dma_reqs[][2] = {};
80static const unsigned long omap1_mcbsp_port[][2] = {};
81#endif
Jarkko Nikula406e2c42008-10-09 15:57:20 +030082
83#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
84static const int omap24xx_dma_reqs[][2] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +020085 { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
86 { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
Jarkko Nikula406e2c42008-10-09 15:57:20 +030087#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
88 { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX },
89 { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX },
90 { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX },
91#endif
Jarkko Nikula2e747962008-04-25 13:55:19 +020092};
Jarkko Nikula406e2c42008-10-09 15:57:20 +030093#else
94static const int omap24xx_dma_reqs[][2] = {};
95#endif
96
97#if defined(CONFIG_ARCH_OMAP2420)
Jarkko Nikula2e747962008-04-25 13:55:19 +020098static const unsigned long omap2420_mcbsp_port[][2] = {
99 { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
100 OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
101 { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
102 OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
103};
104#else
Jarkko Nikula2e747962008-04-25 13:55:19 +0200105static const unsigned long omap2420_mcbsp_port[][2] = {};
106#endif
107
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300108#if defined(CONFIG_ARCH_OMAP2430)
109static const unsigned long omap2430_mcbsp_port[][2] = {
110 { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
111 OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
112 { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
113 OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
114 { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
115 OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
116 { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
117 OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
118 { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
119 OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
120};
121#else
122static const unsigned long omap2430_mcbsp_port[][2] = {};
123#endif
124
125#if defined(CONFIG_ARCH_OMAP34XX)
126static const unsigned long omap34xx_mcbsp_port[][2] = {
127 { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
128 OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
129 { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
130 OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
131 { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
132 OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
133 { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
134 OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
135 { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
136 OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
137};
138#else
139static const unsigned long omap34xx_mcbsp_port[][2] = {};
140#endif
141
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300142static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
143{
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
146 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300147 int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
148 int samples;
149
150 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
151 if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
152 samples = snd_pcm_lib_period_bytes(substream) >> 1;
153 else
154 samples = 1;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300155
156 /* Configure McBSP internal buffer usage */
157 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
158 omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, samples - 1);
159 else
160 omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, samples - 1);
161}
162
Mark Browndee89c42008-11-18 22:11:38 +0000163static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
164 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200165{
166 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100167 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200168 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300169 int bus_id = mcbsp_data->bus_id;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200170 int err = 0;
171
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300172 if (!cpu_dai->active)
173 err = omap_mcbsp_request(bus_id);
174
175 if (cpu_is_omap343x()) {
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300176 int dma_op_mode = omap_mcbsp_get_dma_op_mode(bus_id);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300177 int max_period;
178
Jarkko Nikula69849922009-03-27 15:32:01 +0200179 /*
180 * McBSP2 in OMAP3 has 1024 * 32-bit internal audio buffer.
181 * Set constraint for minimum buffer size to the same than FIFO
182 * size in order to avoid underruns in playback startup because
183 * HW is keeping the DMA request active until FIFO is filled.
184 */
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300185 if (bus_id == 1)
186 snd_pcm_hw_constraint_minmax(substream->runtime,
187 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
188 4096, UINT_MAX);
Jarkko Nikula69849922009-03-27 15:32:01 +0200189
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
191 max_period = omap_mcbsp_get_max_tx_threshold(bus_id);
192 else
193 max_period = omap_mcbsp_get_max_rx_threshold(bus_id);
194
195 max_period++;
196 max_period <<= 1;
197
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300198 if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
199 snd_pcm_hw_constraint_minmax(substream->runtime,
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300200 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
201 32, max_period);
202 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200203
204 return err;
205}
206
Mark Browndee89c42008-11-18 22:11:38 +0000207static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
208 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200209{
210 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100211 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200212 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
213
214 if (!cpu_dai->active) {
215 omap_mcbsp_free(mcbsp_data->bus_id);
216 mcbsp_data->configured = 0;
217 }
218}
219
Mark Browndee89c42008-11-18 22:11:38 +0000220static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
221 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200222{
223 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100224 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200225 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300226 int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200227
228 switch (cmd) {
229 case SNDRV_PCM_TRIGGER_START:
230 case SNDRV_PCM_TRIGGER_RESUME:
231 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300232 mcbsp_data->active++;
233 omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
Eero Nurkkalaca6e2ce2009-08-20 16:18:24 +0300234 /* Make sure data transfer is frame synchronized */
235 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
236 omap_mcbsp_xmit_enable(mcbsp_data->bus_id, 1);
237 else
238 omap_mcbsp_recv_enable(mcbsp_data->bus_id, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200239 break;
240
241 case SNDRV_PCM_TRIGGER_STOP:
242 case SNDRV_PCM_TRIGGER_SUSPEND:
243 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300244 omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
245 mcbsp_data->active--;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200246 break;
247 default:
248 err = -EINVAL;
249 }
250
251 return err;
252}
253
254static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000255 struct snd_pcm_hw_params *params,
256 struct snd_soc_dai *dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200257{
258 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100259 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200260 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
261 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
262 int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300263 int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200264 unsigned long port;
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300265 unsigned int format;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200266
267 if (cpu_class_is_omap1()) {
268 dma = omap1_dma_reqs[bus_id][substream->stream];
269 port = omap1_mcbsp_port[bus_id][substream->stream];
270 } else if (cpu_is_omap2420()) {
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300271 dma = omap24xx_dma_reqs[bus_id][substream->stream];
Jarkko Nikula2e747962008-04-25 13:55:19 +0200272 port = omap2420_mcbsp_port[bus_id][substream->stream];
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300273 } else if (cpu_is_omap2430()) {
274 dma = omap24xx_dma_reqs[bus_id][substream->stream];
275 port = omap2430_mcbsp_port[bus_id][substream->stream];
276 } else if (cpu_is_omap343x()) {
277 dma = omap24xx_dma_reqs[bus_id][substream->stream];
278 port = omap34xx_mcbsp_port[bus_id][substream->stream];
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300279 omap_mcbsp_dai_dma_params[id][substream->stream].set_threshold =
280 omap_mcbsp_set_threshold;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300281 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
282 if (omap_mcbsp_get_dma_op_mode(bus_id) ==
283 MCBSP_DMA_MODE_THRESHOLD)
284 sync_mode = OMAP_DMA_SYNC_FRAME;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200285 } else {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200286 return -ENODEV;
287 }
Jarkko Nikula2e897132008-10-09 15:57:21 +0300288 omap_mcbsp_dai_dma_params[id][substream->stream].name =
289 substream->stream ? "Audio Capture" : "Audio Playback";
Jarkko Nikula2e747962008-04-25 13:55:19 +0200290 omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma;
291 omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300292 omap_mcbsp_dai_dma_params[id][substream->stream].sync_mode = sync_mode;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200293 cpu_dai->dma_data = &omap_mcbsp_dai_dma_params[id][substream->stream];
294
295 if (mcbsp_data->configured) {
296 /* McBSP already configured by another stream */
297 return 0;
298 }
299
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300300 format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
301 wpf = channels = params_channels(params);
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200302 switch (channels) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200303 case 2:
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300304 if (format == SND_SOC_DAIFMT_I2S) {
305 /* Use dual-phase frames */
306 regs->rcr2 |= RPHASE;
307 regs->xcr2 |= XPHASE;
308 /* Set 1 word per (McBSP) frame for phase1 and phase2 */
309 wpf--;
310 regs->rcr2 |= RFRLEN2(wpf - 1);
311 regs->xcr2 |= XFRLEN2(wpf - 1);
312 }
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200313 case 1:
Peter Ujfalusi31a00c62009-04-23 14:36:48 +0300314 case 4:
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300315 /* Set word per (McBSP) frame for phase1 */
316 regs->rcr1 |= RFRLEN1(wpf - 1);
317 regs->xcr1 |= XFRLEN1(wpf - 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200318 break;
319 default:
320 /* Unsupported number of channels */
321 return -EINVAL;
322 }
323
324 switch (params_format(params)) {
325 case SNDRV_PCM_FORMAT_S16_LE:
326 /* Set word lengths */
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300327 wlen = 16;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200328 regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
329 regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
330 regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
331 regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200332 break;
333 default:
334 /* Unsupported PCM format */
335 return -EINVAL;
336 }
337
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300338 /* Set FS period and length in terms of bit clock periods */
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300339 switch (format) {
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300340 case SND_SOC_DAIFMT_I2S:
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300341 regs->srgr2 |= FPER(wlen * channels - 1);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300342 regs->srgr1 |= FWID(wlen - 1);
343 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +0300344 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulabd258672008-12-22 10:21:36 +0200345 case SND_SOC_DAIFMT_DSP_B:
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200346 regs->srgr2 |= FPER(wlen * channels - 1);
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300347 regs->srgr1 |= FWID(0);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300348 break;
349 }
350
Jarkko Nikula2e747962008-04-25 13:55:19 +0200351 omap_mcbsp_config(bus_id, &mcbsp_data->regs);
352 mcbsp_data->configured = 1;
353
354 return 0;
355}
356
357/*
358 * This must be called before _set_clkdiv and _set_sysclk since McBSP register
359 * cache is initialized here
360 */
Liam Girdwood8687eb82008-07-07 16:08:07 +0100361static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200362 unsigned int fmt)
363{
364 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
365 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300366 unsigned int temp_fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200367
368 if (mcbsp_data->configured)
369 return 0;
370
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300371 mcbsp_data->fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200372 memset(regs, 0, sizeof(*regs));
373 /* Generic McBSP register settings */
374 regs->spcr2 |= XINTM(3) | FREE;
375 regs->spcr1 |= RINTM(3);
Eero Nurkkalac721bbd2009-08-20 16:18:23 +0300376 /* RFIG and XFIG are not defined in 34xx */
377 if (!cpu_is_omap34xx()) {
378 regs->rcr2 |= RFIG;
379 regs->xcr2 |= XFIG;
380 }
Misael Lopez Cruzef390c02009-01-29 13:29:46 +0200381 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
Jarkko Nikula32080af2009-08-23 12:24:26 +0300382 regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
383 regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
Misael Lopez Cruzef390c02009-01-29 13:29:46 +0200384 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200385
386 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
387 case SND_SOC_DAIFMT_I2S:
388 /* 1-bit data delay */
389 regs->rcr2 |= RDATDLY(1);
390 regs->xcr2 |= XDATDLY(1);
391 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +0300392 case SND_SOC_DAIFMT_DSP_A:
393 /* 1-bit data delay */
394 regs->rcr2 |= RDATDLY(1);
395 regs->xcr2 |= XDATDLY(1);
396 /* Invert FS polarity configuration */
397 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
398 break;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200399 case SND_SOC_DAIFMT_DSP_B:
Arun KS3336c5b2008-10-02 15:07:06 +0530400 /* 0-bit data delay */
401 regs->rcr2 |= RDATDLY(0);
402 regs->xcr2 |= XDATDLY(0);
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300403 /* Invert FS polarity configuration */
404 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
Arun KS3336c5b2008-10-02 15:07:06 +0530405 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200406 default:
407 /* Unsupported data format */
408 return -EINVAL;
409 }
410
411 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
412 case SND_SOC_DAIFMT_CBS_CFS:
413 /* McBSP master. Set FS and bit clocks as outputs */
414 regs->pcr0 |= FSXM | FSRM |
415 CLKXM | CLKRM;
416 /* Sample rate generator drives the FS */
417 regs->srgr2 |= FSGM;
418 break;
419 case SND_SOC_DAIFMT_CBM_CFM:
420 /* McBSP slave */
421 break;
422 default:
423 /* Unsupported master/slave configuration */
424 return -EINVAL;
425 }
426
427 /* Set bit clock (CLKX/CLKR) and FS polarities */
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300428 switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200429 case SND_SOC_DAIFMT_NB_NF:
430 /*
431 * Normal BCLK + FS.
432 * FS active low. TX data driven on falling edge of bit clock
433 * and RX data sampled on rising edge of bit clock.
434 */
435 regs->pcr0 |= FSXP | FSRP |
436 CLKXP | CLKRP;
437 break;
438 case SND_SOC_DAIFMT_NB_IF:
439 regs->pcr0 |= CLKXP | CLKRP;
440 break;
441 case SND_SOC_DAIFMT_IB_NF:
442 regs->pcr0 |= FSXP | FSRP;
443 break;
444 case SND_SOC_DAIFMT_IB_IF:
445 break;
446 default:
447 return -EINVAL;
448 }
449
450 return 0;
451}
452
Liam Girdwood8687eb82008-07-07 16:08:07 +0100453static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200454 int div_id, int div)
455{
456 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
457 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
458
459 if (div_id != OMAP_MCBSP_CLKGDV)
460 return -ENODEV;
461
462 regs->srgr1 |= CLKGDV(div - 1);
463
464 return 0;
465}
466
467static int omap_mcbsp_dai_set_clks_src(struct omap_mcbsp_data *mcbsp_data,
468 int clk_id)
469{
470 int sel_bit;
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300471 u16 reg, reg_devconf1 = OMAP243X_CONTROL_DEVCONF1;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200472
473 if (cpu_class_is_omap1()) {
474 /* OMAP1's can use only external source clock */
475 if (unlikely(clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK))
476 return -EINVAL;
477 else
478 return 0;
479 }
480
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300481 if (cpu_is_omap2420() && mcbsp_data->bus_id > 1)
482 return -EINVAL;
483
484 if (cpu_is_omap343x())
485 reg_devconf1 = OMAP343X_CONTROL_DEVCONF1;
486
Jarkko Nikula2e747962008-04-25 13:55:19 +0200487 switch (mcbsp_data->bus_id) {
488 case 0:
489 reg = OMAP2_CONTROL_DEVCONF0;
490 sel_bit = 2;
491 break;
492 case 1:
493 reg = OMAP2_CONTROL_DEVCONF0;
494 sel_bit = 6;
495 break;
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300496 case 2:
497 reg = reg_devconf1;
498 sel_bit = 0;
499 break;
500 case 3:
501 reg = reg_devconf1;
502 sel_bit = 2;
503 break;
504 case 4:
505 reg = reg_devconf1;
506 sel_bit = 4;
507 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200508 default:
509 return -EINVAL;
510 }
511
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300512 if (clk_id == OMAP_MCBSP_SYSCLK_CLKS_FCLK)
513 omap_ctrl_writel(omap_ctrl_readl(reg) & ~(1 << sel_bit), reg);
514 else
515 omap_ctrl_writel(omap_ctrl_readl(reg) | (1 << sel_bit), reg);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200516
517 return 0;
518}
519
Liam Girdwood8687eb82008-07-07 16:08:07 +0100520static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200521 int clk_id, unsigned int freq,
522 int dir)
523{
524 struct omap_mcbsp_data *mcbsp_data = to_mcbsp(cpu_dai->private_data);
525 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
526 int err = 0;
527
528 switch (clk_id) {
529 case OMAP_MCBSP_SYSCLK_CLK:
530 regs->srgr2 |= CLKSM;
531 break;
532 case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
533 case OMAP_MCBSP_SYSCLK_CLKS_EXT:
534 err = omap_mcbsp_dai_set_clks_src(mcbsp_data, clk_id);
535 break;
536
537 case OMAP_MCBSP_SYSCLK_CLKX_EXT:
538 regs->srgr2 |= CLKSM;
539 case OMAP_MCBSP_SYSCLK_CLKR_EXT:
540 regs->pcr0 |= SCLKME;
541 break;
542 default:
543 err = -ENODEV;
544 }
545
546 return err;
547}
548
Eric Miao6335d052009-03-03 09:41:00 +0800549static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
550 .startup = omap_mcbsp_dai_startup,
551 .shutdown = omap_mcbsp_dai_shutdown,
552 .trigger = omap_mcbsp_dai_trigger,
553 .hw_params = omap_mcbsp_dai_hw_params,
554 .set_fmt = omap_mcbsp_dai_set_dai_fmt,
555 .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
556 .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
557};
558
Jarkko Nikula8def4642008-10-09 15:57:22 +0300559#define OMAP_MCBSP_DAI_BUILDER(link_id) \
560{ \
Jarkko Nikula0c758bd2008-11-21 14:31:33 +0200561 .name = "omap-mcbsp-dai-"#link_id, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300562 .id = (link_id), \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300563 .playback = { \
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200564 .channels_min = 1, \
Peter Ujfalusi31a00c62009-04-23 14:36:48 +0300565 .channels_max = 4, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300566 .rates = OMAP_MCBSP_RATES, \
567 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
568 }, \
569 .capture = { \
Jarkko Nikula375e8a72008-11-25 12:45:09 +0200570 .channels_min = 1, \
Peter Ujfalusi31a00c62009-04-23 14:36:48 +0300571 .channels_max = 4, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300572 .rates = OMAP_MCBSP_RATES, \
573 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
574 }, \
Eric Miao6335d052009-03-03 09:41:00 +0800575 .ops = &omap_mcbsp_dai_ops, \
Jarkko Nikula8def4642008-10-09 15:57:22 +0300576 .private_data = &mcbsp_data[(link_id)].bus_id, \
577}
578
579struct snd_soc_dai omap_mcbsp_dai[] = {
580 OMAP_MCBSP_DAI_BUILDER(0),
581 OMAP_MCBSP_DAI_BUILDER(1),
582#if NUM_LINKS >= 3
583 OMAP_MCBSP_DAI_BUILDER(2),
584#endif
585#if NUM_LINKS == 5
586 OMAP_MCBSP_DAI_BUILDER(3),
587 OMAP_MCBSP_DAI_BUILDER(4),
588#endif
Jarkko Nikula2e747962008-04-25 13:55:19 +0200589};
Jarkko Nikula8def4642008-10-09 15:57:22 +0300590
Jarkko Nikula2e747962008-04-25 13:55:19 +0200591EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
592
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100593static int __init snd_omap_mcbsp_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000594{
595 return snd_soc_register_dais(omap_mcbsp_dai,
596 ARRAY_SIZE(omap_mcbsp_dai));
597}
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100598module_init(snd_omap_mcbsp_init);
Mark Brown3f4b7832008-12-03 19:26:35 +0000599
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100600static void __exit snd_omap_mcbsp_exit(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000601{
602 snd_soc_unregister_dais(omap_mcbsp_dai, ARRAY_SIZE(omap_mcbsp_dai));
603}
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100604module_exit(snd_omap_mcbsp_exit);
Mark Brown3f4b7832008-12-03 19:26:35 +0000605
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300606MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200607MODULE_DESCRIPTION("OMAP I2S SoC Interface");
608MODULE_LICENSE("GPL");