blob: ede6afde7d2fbbf35b4efa1e061ae495310079ed [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/dma.h>
35#include <plat/mcbsp.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020036#include "omap-mcbsp.h"
37#include "omap-pcm.h"
38
Jarkko Nikula0b604852008-11-12 17:05:51 +020039#define OMAP_MCBSP_RATES (SNDRV_PCM_RATE_8000_96000)
Jarkko Nikula2e747962008-04-25 13:55:19 +020040
Ilkka Koskinen83905c12010-02-22 12:21:12 +000041#define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \
42 xhandler_get, xhandler_put) \
43{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
44 .info = omap_mcbsp_st_info_volsw, \
45 .get = xhandler_get, .put = xhandler_put, \
46 .private_value = (unsigned long) &(struct soc_mixer_control) \
47 {.min = xmin, .max = xmax} }
48
Jarkko Nikula2e747962008-04-25 13:55:19 +020049struct omap_mcbsp_data {
50 unsigned int bus_id;
51 struct omap_mcbsp_reg_cfg regs;
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +030052 unsigned int fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +020053 /*
54 * Flags indicating is the bus already activated and configured by
55 * another substream
56 */
57 int active;
58 int configured;
Graeme Gregory5f63ef92009-11-09 19:02:15 +000059 unsigned int in_freq;
60 int clk_div;
Peter Ujfalusi3f024032010-06-03 07:39:35 +030061 int wlen;
Jarkko Nikula2e747962008-04-25 13:55:19 +020062};
63
Jarkko Nikula2e747962008-04-25 13:55:19 +020064static struct omap_mcbsp_data mcbsp_data[NUM_LINKS];
65
66/*
67 * Stream DMA parameters. DMA request line and port address are set runtime
68 * since they are different between OMAP1 and later OMAPs
69 */
Jarkko Nikula2e897132008-10-09 15:57:21 +030070static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
Jarkko Nikula2e747962008-04-25 13:55:19 +020071
72#if defined(CONFIG_ARCH_OMAP15XX) || defined(CONFIG_ARCH_OMAP16XX)
73static const int omap1_dma_reqs[][2] = {
74 { OMAP_DMA_MCBSP1_TX, OMAP_DMA_MCBSP1_RX },
75 { OMAP_DMA_MCBSP2_TX, OMAP_DMA_MCBSP2_RX },
76 { OMAP_DMA_MCBSP3_TX, OMAP_DMA_MCBSP3_RX },
77};
78static const unsigned long omap1_mcbsp_port[][2] = {
79 { OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
80 OMAP1510_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
81 { OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
82 OMAP1510_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
83 { OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DXR1,
84 OMAP1510_MCBSP3_BASE + OMAP_MCBSP_REG_DRR1 },
85};
86#else
87static const int omap1_dma_reqs[][2] = {};
88static const unsigned long omap1_mcbsp_port[][2] = {};
89#endif
Jarkko Nikula406e2c42008-10-09 15:57:20 +030090
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -080091#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Jarkko Nikula406e2c42008-10-09 15:57:20 +030092static const int omap24xx_dma_reqs[][2] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +020093 { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
94 { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
Tony Lindgren59b479e2011-01-27 16:39:40 -080095#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
Jarkko Nikula406e2c42008-10-09 15:57:20 +030096 { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX },
97 { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX },
98 { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX },
99#endif
Jarkko Nikula2e747962008-04-25 13:55:19 +0200100};
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300101#else
102static const int omap24xx_dma_reqs[][2] = {};
103#endif
104
Jorge Eduardo Candelariad4686c62010-12-20 11:32:47 -0600105#if defined(CONFIG_ARCH_OMAP4)
106static const int omap44xx_dma_reqs[][2] = {
107 { OMAP44XX_DMA_MCBSP1_TX, OMAP44XX_DMA_MCBSP1_RX },
108 { OMAP44XX_DMA_MCBSP2_TX, OMAP44XX_DMA_MCBSP2_RX },
109 { OMAP44XX_DMA_MCBSP3_TX, OMAP44XX_DMA_MCBSP3_RX },
110 { OMAP44XX_DMA_MCBSP4_TX, OMAP44XX_DMA_MCBSP4_RX },
111};
112#else
113static const int omap44xx_dma_reqs[][2] = {};
114#endif
115
Tony Lindgren59b479e2011-01-27 16:39:40 -0800116#if defined(CONFIG_SOC_OMAP2420)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200117static const unsigned long omap2420_mcbsp_port[][2] = {
118 { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR1,
119 OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR1 },
120 { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR1,
121 OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR1 },
122};
123#else
Jarkko Nikula2e747962008-04-25 13:55:19 +0200124static const unsigned long omap2420_mcbsp_port[][2] = {};
125#endif
126
Tony Lindgren59b479e2011-01-27 16:39:40 -0800127#if defined(CONFIG_SOC_OMAP2430)
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300128static const unsigned long omap2430_mcbsp_port[][2] = {
129 { OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
130 OMAP24XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
131 { OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
132 OMAP24XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
133 { OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
134 OMAP2430_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
135 { OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
136 OMAP2430_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
137 { OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
138 OMAP2430_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
139};
140#else
141static const unsigned long omap2430_mcbsp_port[][2] = {};
142#endif
143
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800144#if defined(CONFIG_ARCH_OMAP3)
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300145static const unsigned long omap34xx_mcbsp_port[][2] = {
146 { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
147 OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
148 { OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
149 OMAP34XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
150 { OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
151 OMAP34XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
152 { OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
153 OMAP34XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
154 { OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DXR,
155 OMAP34XX_MCBSP5_BASE + OMAP_MCBSP_REG_DRR },
156};
157#else
158static const unsigned long omap34xx_mcbsp_port[][2] = {};
159#endif
160
Jorge Eduardo Candelariad4686c62010-12-20 11:32:47 -0600161#if defined(CONFIG_ARCH_OMAP4)
162static const unsigned long omap44xx_mcbsp_port[][2] = {
163 { OMAP44XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
164 OMAP44XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
165 { OMAP44XX_MCBSP2_BASE + OMAP_MCBSP_REG_DXR,
166 OMAP44XX_MCBSP2_BASE + OMAP_MCBSP_REG_DRR },
167 { OMAP44XX_MCBSP3_BASE + OMAP_MCBSP_REG_DXR,
168 OMAP44XX_MCBSP3_BASE + OMAP_MCBSP_REG_DRR },
169 { OMAP44XX_MCBSP4_BASE + OMAP_MCBSP_REG_DXR,
170 OMAP44XX_MCBSP4_BASE + OMAP_MCBSP_REG_DRR },
171};
172#else
173static const unsigned long omap44xx_mcbsp_port[][2] = {};
174#endif
175
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300176static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
177{
178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000179 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
180 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300181 struct omap_pcm_dma_data *dma_data;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300182 int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300183 int words;
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300184
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000185 dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300186
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300187 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
188 if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300189 /*
190 * Configure McBSP threshold based on either:
191 * packet_size, when the sDMA is in packet mode, or
192 * based on the period size.
193 */
194 if (dma_data->packet_size)
195 words = dma_data->packet_size;
196 else
197 words = snd_pcm_lib_period_bytes(substream) /
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300198 (mcbsp_data->wlen / 8);
Eduardo Valentina0a499c2009-08-20 16:18:26 +0300199 else
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300200 words = 1;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300201
202 /* Configure McBSP internal buffer usage */
203 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300204 omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, words);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300205 else
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300206 omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, words);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300207}
208
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300209static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
210 struct snd_pcm_hw_rule *rule)
211{
212 struct snd_interval *buffer_size = hw_param_interval(params,
213 SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
214 struct snd_interval *channels = hw_param_interval(params,
215 SNDRV_PCM_HW_PARAM_CHANNELS);
216 struct omap_mcbsp_data *mcbsp_data = rule->private;
217 struct snd_interval frames;
218 int size;
219
220 snd_interval_any(&frames);
221 size = omap_mcbsp_get_fifo_size(mcbsp_data->bus_id);
222
223 frames.min = size / channels->min;
224 frames.integer = 1;
225 return snd_interval_refine(buffer_size, &frames);
226}
227
Mark Browndee89c42008-11-18 22:11:38 +0000228static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000229 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200230{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000231 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300232 int bus_id = mcbsp_data->bus_id;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200233 int err = 0;
234
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300235 if (!cpu_dai->active)
236 err = omap_mcbsp_request(bus_id);
237
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300238 /*
239 * OMAP3 McBSP FIFO is word structured.
240 * McBSP2 has 1024 + 256 = 1280 word long buffer,
241 * McBSP1,3,4,5 has 128 word long buffer
242 * This means that the size of the FIFO depends on the sample format.
243 * For example on McBSP3:
244 * 16bit samples: size is 128 * 2 = 256 bytes
245 * 32bit samples: size is 128 * 4 = 512 bytes
246 * It is simpler to place constraint for buffer and period based on
247 * channels.
248 * McBSP3 as example again (16 or 32 bit samples):
249 * 1 channel (mono): size is 128 frames (128 words)
250 * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words)
251 * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words)
252 */
Jorge Eduardo Candelariad4686c62010-12-20 11:32:47 -0600253 if (cpu_is_omap343x() || cpu_is_omap44xx()) {
Jarkko Nikula69849922009-03-27 15:32:01 +0200254 /*
Peter Ujfalusi998a8a62010-07-29 09:51:28 +0300255 * Rule for the buffer size. We should not allow
Peter Ujfalusiddc29b02010-06-03 07:39:36 +0300256 * smaller buffer than the FIFO size to avoid underruns
257 */
258 snd_pcm_hw_rule_add(substream->runtime, 0,
259 SNDRV_PCM_HW_PARAM_CHANNELS,
260 omap_mcbsp_hwrule_min_buffersize,
261 mcbsp_data,
262 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
263
Peter Ujfalusi998a8a62010-07-29 09:51:28 +0300264 /* Make sure, that the period size is always even */
265 snd_pcm_hw_constraint_step(substream->runtime, 0,
266 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300267 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200268
269 return err;
270}
271
Mark Browndee89c42008-11-18 22:11:38 +0000272static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000273 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200274{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200276
277 if (!cpu_dai->active) {
278 omap_mcbsp_free(mcbsp_data->bus_id);
279 mcbsp_data->configured = 0;
280 }
281}
282
Mark Browndee89c42008-11-18 22:11:38 +0000283static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200285{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000286 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300287 int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200288
289 switch (cmd) {
290 case SNDRV_PCM_TRIGGER_START:
291 case SNDRV_PCM_TRIGGER_RESUME:
292 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300293 mcbsp_data->active++;
294 omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200295 break;
296
297 case SNDRV_PCM_TRIGGER_STOP:
298 case SNDRV_PCM_TRIGGER_SUSPEND:
299 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300300 omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
301 mcbsp_data->active--;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200302 break;
303 default:
304 err = -EINVAL;
305 }
306
307 return err;
308}
309
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200310static snd_pcm_sframes_t omap_mcbsp_dai_delay(
311 struct snd_pcm_substream *substream,
312 struct snd_soc_dai *dai)
313{
314 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000315 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
316 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200317 u16 fifo_use;
318 snd_pcm_sframes_t delay;
319
320 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
321 fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id);
322 else
323 fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id);
324
325 /*
326 * Divide the used locations with the channel count to get the
327 * FIFO usage in samples (don't care about partial samples in the
328 * buffer).
329 */
330 delay = fifo_use / substream->runtime->channels;
331
332 return delay;
333}
334
Jarkko Nikula2e747962008-04-25 13:55:19 +0200335static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000336 struct snd_pcm_hw_params *params,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000337 struct snd_soc_dai *cpu_dai)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200338{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000339 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200340 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
Peter Ujfalusi81ec0272010-07-29 09:51:26 +0300341 struct omap_pcm_dma_data *dma_data;
342 int dma, bus_id = mcbsp_data->bus_id;
Eduardo Valentincaebc0c2009-08-20 16:18:25 +0300343 int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300344 int pkt_size = 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200345 unsigned long port;
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000346 unsigned int format, div, framesize, master;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200347
Peter Ujfalusi81ec0272010-07-29 09:51:26 +0300348 dma_data = &omap_mcbsp_dai_dma_params[cpu_dai->id][substream->stream];
Jarkko Nikula2e747962008-04-25 13:55:19 +0200349 if (cpu_class_is_omap1()) {
350 dma = omap1_dma_reqs[bus_id][substream->stream];
351 port = omap1_mcbsp_port[bus_id][substream->stream];
352 } else if (cpu_is_omap2420()) {
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300353 dma = omap24xx_dma_reqs[bus_id][substream->stream];
Jarkko Nikula2e747962008-04-25 13:55:19 +0200354 port = omap2420_mcbsp_port[bus_id][substream->stream];
Jarkko Nikula406e2c42008-10-09 15:57:20 +0300355 } else if (cpu_is_omap2430()) {
356 dma = omap24xx_dma_reqs[bus_id][substream->stream];
357 port = omap2430_mcbsp_port[bus_id][substream->stream];
358 } else if (cpu_is_omap343x()) {
359 dma = omap24xx_dma_reqs[bus_id][substream->stream];
360 port = omap34xx_mcbsp_port[bus_id][substream->stream];
Jorge Eduardo Candelariad4686c62010-12-20 11:32:47 -0600361 } else if (cpu_is_omap44xx()) {
362 dma = omap44xx_dma_reqs[bus_id][substream->stream];
363 port = omap44xx_mcbsp_port[bus_id][substream->stream];
Jarkko Nikula2e747962008-04-25 13:55:19 +0200364 } else {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200365 return -ENODEV;
366 }
Sergey Lapind98508a2010-05-13 19:48:16 +0400367 switch (params_format(params)) {
368 case SNDRV_PCM_FORMAT_S16_LE:
Peter Ujfalusi81ec0272010-07-29 09:51:26 +0300369 dma_data->data_type = OMAP_DMA_DATA_TYPE_S16;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300370 wlen = 16;
Sergey Lapind98508a2010-05-13 19:48:16 +0400371 break;
372 case SNDRV_PCM_FORMAT_S32_LE:
Peter Ujfalusi81ec0272010-07-29 09:51:26 +0300373 dma_data->data_type = OMAP_DMA_DATA_TYPE_S32;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300374 wlen = 32;
Sergey Lapind98508a2010-05-13 19:48:16 +0400375 break;
376 default:
377 return -EINVAL;
378 }
Peter Ujfalusi15d01432010-07-29 09:51:25 +0300379 if (cpu_is_omap343x()) {
380 dma_data->set_threshold = omap_mcbsp_set_threshold;
381 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
382 if (omap_mcbsp_get_dma_op_mode(bus_id) ==
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300383 MCBSP_DMA_MODE_THRESHOLD) {
384 int period_words, max_thrsh;
385
386 period_words = params_period_bytes(params) / (wlen / 8);
387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 max_thrsh = omap_mcbsp_get_max_tx_threshold(
389 mcbsp_data->bus_id);
390 else
391 max_thrsh = omap_mcbsp_get_max_rx_threshold(
392 mcbsp_data->bus_id);
393 /*
394 * If the period contains less or equal number of words,
395 * we are using the original threshold mode setup:
396 * McBSP threshold = sDMA frame size = period_size
397 * Otherwise we switch to sDMA packet mode:
398 * McBSP threshold = sDMA packet size
399 * sDMA frame size = period size
400 */
401 if (period_words > max_thrsh) {
402 int divider = 0;
403
404 /*
405 * Look for the biggest threshold value, which
406 * divides the period size evenly.
407 */
408 divider = period_words / max_thrsh;
409 if (period_words % max_thrsh)
410 divider++;
411 while (period_words % divider &&
412 divider < period_words)
413 divider++;
414 if (divider == period_words)
415 return -EINVAL;
416
417 pkt_size = period_words / divider;
418 sync_mode = OMAP_DMA_SYNC_PACKET;
419 } else {
420 sync_mode = OMAP_DMA_SYNC_FRAME;
421 }
422 }
Peter Ujfalusi15d01432010-07-29 09:51:25 +0300423 }
424
425 dma_data->name = substream->stream ? "Audio Capture" : "Audio Playback";
426 dma_data->dma_req = dma;
427 dma_data->port_addr = port;
428 dma_data->sync_mode = sync_mode;
Peter Ujfalusicf80e152010-07-29 09:51:27 +0300429 dma_data->packet_size = pkt_size;
Daniel Mackfd23b7d2010-03-19 14:52:55 +0000430
Peter Ujfalusi81ec0272010-07-29 09:51:26 +0300431 snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200432
433 if (mcbsp_data->configured) {
434 /* McBSP already configured by another stream */
435 return 0;
436 }
437
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300438 format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
439 wpf = channels = params_channels(params);
Peter Ujfalusi299a1512010-03-19 12:27:31 +0200440 if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
441 format == SND_SOC_DAIFMT_LEFT_J)) {
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000442 /* Use dual-phase frames */
443 regs->rcr2 |= RPHASE;
444 regs->xcr2 |= XPHASE;
445 /* Set 1 word per (McBSP) frame for phase1 and phase2 */
446 wpf--;
447 regs->rcr2 |= RFRLEN2(wpf - 1);
448 regs->xcr2 |= XFRLEN2(wpf - 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200449 }
450
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000451 regs->rcr1 |= RFRLEN1(wpf - 1);
452 regs->xcr1 |= XFRLEN1(wpf - 1);
453
Jarkko Nikula2e747962008-04-25 13:55:19 +0200454 switch (params_format(params)) {
455 case SNDRV_PCM_FORMAT_S16_LE:
456 /* Set word lengths */
457 regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_16);
458 regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_16);
459 regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_16);
460 regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_16);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200461 break;
Sergey Lapind98508a2010-05-13 19:48:16 +0400462 case SNDRV_PCM_FORMAT_S32_LE:
463 /* Set word lengths */
Sergey Lapind98508a2010-05-13 19:48:16 +0400464 regs->rcr2 |= RWDLEN2(OMAP_MCBSP_WORD_32);
465 regs->rcr1 |= RWDLEN1(OMAP_MCBSP_WORD_32);
466 regs->xcr2 |= XWDLEN2(OMAP_MCBSP_WORD_32);
467 regs->xcr1 |= XWDLEN1(OMAP_MCBSP_WORD_32);
468 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200469 default:
470 /* Unsupported PCM format */
471 return -EINVAL;
472 }
473
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000474 /* In McBSP master modes, FRAME (i.e. sample rate) is generated
475 * by _counting_ BCLKs. Calculate frame size in BCLKs */
476 master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
477 if (master == SND_SOC_DAIFMT_CBS_CFS) {
478 div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
479 framesize = (mcbsp_data->in_freq / div) / params_rate(params);
480
481 if (framesize < wlen * channels) {
482 printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
483 "channels\n", __func__);
484 return -EINVAL;
485 }
486 } else
487 framesize = wlen * channels;
488
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300489 /* Set FS period and length in terms of bit clock periods */
Peter Ujfalusic29b2062009-04-15 15:38:55 +0300490 switch (format) {
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300491 case SND_SOC_DAIFMT_I2S:
Peter Ujfalusi299a1512010-03-19 12:27:31 +0200492 case SND_SOC_DAIFMT_LEFT_J:
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000493 regs->srgr2 |= FPER(framesize - 1);
494 regs->srgr1 |= FWID((framesize >> 1) - 1);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300495 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +0300496 case SND_SOC_DAIFMT_DSP_A:
Jarkko Nikulabd258672008-12-22 10:21:36 +0200497 case SND_SOC_DAIFMT_DSP_B:
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000498 regs->srgr2 |= FPER(framesize - 1);
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300499 regs->srgr1 |= FWID(0);
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300500 break;
501 }
502
Jarkko Nikula2e747962008-04-25 13:55:19 +0200503 omap_mcbsp_config(bus_id, &mcbsp_data->regs);
Peter Ujfalusi3f024032010-06-03 07:39:35 +0300504 mcbsp_data->wlen = wlen;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200505 mcbsp_data->configured = 1;
506
507 return 0;
508}
509
510/*
511 * This must be called before _set_clkdiv and _set_sysclk since McBSP register
512 * cache is initialized here
513 */
Liam Girdwood8687eb82008-07-07 16:08:07 +0100514static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200515 unsigned int fmt)
516{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200518 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300519 unsigned int temp_fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200520
521 if (mcbsp_data->configured)
522 return 0;
523
Jarkko Nikulaba9d0fd2008-10-20 15:29:59 +0300524 mcbsp_data->fmt = fmt;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200525 memset(regs, 0, sizeof(*regs));
526 /* Generic McBSP register settings */
527 regs->spcr2 |= XINTM(3) | FREE;
528 regs->spcr1 |= RINTM(3);
Eero Nurkkalac721bbd2009-08-20 16:18:23 +0300529 /* RFIG and XFIG are not defined in 34xx */
Jorge Eduardo Candelariad4686c62010-12-20 11:32:47 -0600530 if (!cpu_is_omap34xx() && !cpu_is_omap44xx()) {
Eero Nurkkalac721bbd2009-08-20 16:18:23 +0300531 regs->rcr2 |= RFIG;
532 regs->xcr2 |= XFIG;
533 }
Jorge Eduardo Candelariad4686c62010-12-20 11:32:47 -0600534 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jarkko Nikula32080af2009-08-23 12:24:26 +0300535 regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
536 regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
Misael Lopez Cruzef390c02009-01-29 13:29:46 +0200537 }
Jarkko Nikula2e747962008-04-25 13:55:19 +0200538
539 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
540 case SND_SOC_DAIFMT_I2S:
541 /* 1-bit data delay */
542 regs->rcr2 |= RDATDLY(1);
543 regs->xcr2 |= XDATDLY(1);
544 break;
Peter Ujfalusi299a1512010-03-19 12:27:31 +0200545 case SND_SOC_DAIFMT_LEFT_J:
546 /* 0-bit data delay */
547 regs->rcr2 |= RDATDLY(0);
548 regs->xcr2 |= XDATDLY(0);
549 regs->spcr1 |= RJUST(2);
550 /* Invert FS polarity configuration */
551 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
552 break;
Peter Ujfalusi3ba191c2009-04-15 15:38:56 +0300553 case SND_SOC_DAIFMT_DSP_A:
554 /* 1-bit data delay */
555 regs->rcr2 |= RDATDLY(1);
556 regs->xcr2 |= XDATDLY(1);
557 /* Invert FS polarity configuration */
558 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
559 break;
Jarkko Nikulabd258672008-12-22 10:21:36 +0200560 case SND_SOC_DAIFMT_DSP_B:
Arun KS3336c5b2008-10-02 15:07:06 +0530561 /* 0-bit data delay */
562 regs->rcr2 |= RDATDLY(0);
563 regs->xcr2 |= XDATDLY(0);
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300564 /* Invert FS polarity configuration */
565 temp_fmt ^= SND_SOC_DAIFMT_NB_IF;
Arun KS3336c5b2008-10-02 15:07:06 +0530566 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200567 default:
568 /* Unsupported data format */
569 return -EINVAL;
570 }
571
572 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
573 case SND_SOC_DAIFMT_CBS_CFS:
574 /* McBSP master. Set FS and bit clocks as outputs */
575 regs->pcr0 |= FSXM | FSRM |
576 CLKXM | CLKRM;
577 /* Sample rate generator drives the FS */
578 regs->srgr2 |= FSGM;
579 break;
580 case SND_SOC_DAIFMT_CBM_CFM:
581 /* McBSP slave */
582 break;
583 default:
584 /* Unsupported master/slave configuration */
585 return -EINVAL;
586 }
587
588 /* Set bit clock (CLKX/CLKR) and FS polarities */
Jarkko Nikula36ce8582009-04-15 13:48:16 +0300589 switch (temp_fmt & SND_SOC_DAIFMT_INV_MASK) {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200590 case SND_SOC_DAIFMT_NB_NF:
591 /*
592 * Normal BCLK + FS.
593 * FS active low. TX data driven on falling edge of bit clock
594 * and RX data sampled on rising edge of bit clock.
595 */
596 regs->pcr0 |= FSXP | FSRP |
597 CLKXP | CLKRP;
598 break;
599 case SND_SOC_DAIFMT_NB_IF:
600 regs->pcr0 |= CLKXP | CLKRP;
601 break;
602 case SND_SOC_DAIFMT_IB_NF:
603 regs->pcr0 |= FSXP | FSRP;
604 break;
605 case SND_SOC_DAIFMT_IB_IF:
606 break;
607 default:
608 return -EINVAL;
609 }
610
611 return 0;
612}
613
Liam Girdwood8687eb82008-07-07 16:08:07 +0100614static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200615 int div_id, int div)
616{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200618 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
619
620 if (div_id != OMAP_MCBSP_CLKGDV)
621 return -ENODEV;
622
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000623 mcbsp_data->clk_div = div;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200624 regs->srgr1 |= CLKGDV(div - 1);
625
626 return 0;
627}
628
Liam Girdwood8687eb82008-07-07 16:08:07 +0100629static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200630 int clk_id, unsigned int freq,
631 int dir)
632{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000633 struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200634 struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
635 int err = 0;
636
Paul Walmsleycf4c87a2010-10-08 11:40:19 -0600637 /* The McBSP signal muxing functions are only available on McBSP1 */
638 if (clk_id == OMAP_MCBSP_CLKR_SRC_CLKR ||
639 clk_id == OMAP_MCBSP_CLKR_SRC_CLKX ||
640 clk_id == OMAP_MCBSP_FSR_SRC_FSR ||
641 clk_id == OMAP_MCBSP_FSR_SRC_FSX)
642 if (cpu_class_is_omap1() || mcbsp_data->bus_id != 0)
643 return -EINVAL;
644
Graeme Gregory5f63ef92009-11-09 19:02:15 +0000645 mcbsp_data->in_freq = freq;
646
Jarkko Nikula2e747962008-04-25 13:55:19 +0200647 switch (clk_id) {
648 case OMAP_MCBSP_SYSCLK_CLK:
649 regs->srgr2 |= CLKSM;
650 break;
651 case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
Paul Walmsleyd1358652010-10-08 11:40:19 -0600652 if (cpu_class_is_omap1()) {
653 err = -EINVAL;
654 break;
655 }
656 err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
657 MCBSP_CLKS_PRCM_SRC);
658 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200659 case OMAP_MCBSP_SYSCLK_CLKS_EXT:
Paul Walmsleyd1358652010-10-08 11:40:19 -0600660 if (cpu_class_is_omap1()) {
661 err = 0;
662 break;
663 }
664 err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
665 MCBSP_CLKS_PAD_SRC);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200666 break;
667
668 case OMAP_MCBSP_SYSCLK_CLKX_EXT:
669 regs->srgr2 |= CLKSM;
670 case OMAP_MCBSP_SYSCLK_CLKR_EXT:
671 regs->pcr0 |= SCLKME;
672 break;
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300673
Paul Walmsleycf4c87a2010-10-08 11:40:19 -0600674
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300675 case OMAP_MCBSP_CLKR_SRC_CLKR:
Janusz Krzysztofik23353852010-11-02 15:50:32 +0100676 if (cpu_class_is_omap1())
677 break;
Paul Walmsleycf4c87a2010-10-08 11:40:19 -0600678 omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKR);
679 break;
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300680 case OMAP_MCBSP_CLKR_SRC_CLKX:
Janusz Krzysztofik23353852010-11-02 15:50:32 +0100681 if (cpu_class_is_omap1())
682 break;
Paul Walmsleycf4c87a2010-10-08 11:40:19 -0600683 omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKX);
684 break;
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300685 case OMAP_MCBSP_FSR_SRC_FSR:
Janusz Krzysztofik23353852010-11-02 15:50:32 +0100686 if (cpu_class_is_omap1())
687 break;
Paul Walmsleycf4c87a2010-10-08 11:40:19 -0600688 omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSR);
689 break;
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300690 case OMAP_MCBSP_FSR_SRC_FSX:
Janusz Krzysztofik23353852010-11-02 15:50:32 +0100691 if (cpu_class_is_omap1())
692 break;
Paul Walmsleycf4c87a2010-10-08 11:40:19 -0600693 omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSX);
Jarkko Nikulad2c0bda2009-08-28 15:35:35 +0300694 break;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200695 default:
696 err = -ENODEV;
697 }
698
699 return err;
700}
701
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000702static struct snd_soc_dai_ops mcbsp_dai_ops = {
Eric Miao6335d052009-03-03 09:41:00 +0800703 .startup = omap_mcbsp_dai_startup,
704 .shutdown = omap_mcbsp_dai_shutdown,
705 .trigger = omap_mcbsp_dai_trigger,
Peter Ujfalusi75581d22010-03-03 15:08:09 +0200706 .delay = omap_mcbsp_dai_delay,
Eric Miao6335d052009-03-03 09:41:00 +0800707 .hw_params = omap_mcbsp_dai_hw_params,
708 .set_fmt = omap_mcbsp_dai_set_dai_fmt,
709 .set_clkdiv = omap_mcbsp_dai_set_clkdiv,
710 .set_sysclk = omap_mcbsp_dai_set_dai_sysclk,
711};
712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713static int mcbsp_dai_probe(struct snd_soc_dai *dai)
714{
715 mcbsp_data[dai->id].bus_id = dai->id;
716 snd_soc_dai_set_drvdata(dai, &mcbsp_data[dai->id].bus_id);
717 return 0;
Jarkko Nikula8def4642008-10-09 15:57:22 +0300718}
719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720static struct snd_soc_dai_driver omap_mcbsp_dai =
721{
722 .probe = mcbsp_dai_probe,
723 .playback = {
724 .channels_min = 1,
725 .channels_max = 16,
726 .rates = OMAP_MCBSP_RATES,
727 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
728 },
729 .capture = {
730 .channels_min = 1,
731 .channels_max = 16,
732 .rates = OMAP_MCBSP_RATES,
733 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
734 },
735 .ops = &mcbsp_dai_ops,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200736};
Jarkko Nikula8def4642008-10-09 15:57:22 +0300737
G, Manjunath Kondaiah34844572010-09-08 08:53:43 +0530738static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
Ilkka Koskinen83905c12010-02-22 12:21:12 +0000739 struct snd_ctl_elem_info *uinfo)
740{
741 struct soc_mixer_control *mc =
742 (struct soc_mixer_control *)kcontrol->private_value;
743 int max = mc->max;
744 int min = mc->min;
745
746 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
747 uinfo->count = 1;
748 uinfo->value.integer.min = min;
749 uinfo->value.integer.max = max;
750 return 0;
751}
752
753#define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel) \
754static int \
755omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc, \
756 struct snd_ctl_elem_value *uc) \
757{ \
758 struct soc_mixer_control *mc = \
759 (struct soc_mixer_control *)kc->private_value; \
760 int max = mc->max; \
761 int min = mc->min; \
762 int val = uc->value.integer.value[0]; \
763 \
764 if (val < min || val > max) \
765 return -EINVAL; \
766 \
767 /* OMAP McBSP implementation uses index values 0..4 */ \
768 return omap_st_set_chgain((id)-1, channel, val); \
769}
770
771#define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel) \
772static int \
773omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc, \
774 struct snd_ctl_elem_value *uc) \
775{ \
776 s16 chgain; \
777 \
778 if (omap_st_get_chgain((id)-1, channel, &chgain)) \
779 return -EAGAIN; \
780 \
781 uc->value.integer.value[0] = chgain; \
782 return 0; \
783}
784
785OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
786OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
787OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
788OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
789OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
790OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
791OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
792OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
793
794static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
795 struct snd_ctl_elem_value *ucontrol)
796{
797 struct soc_mixer_control *mc =
798 (struct soc_mixer_control *)kcontrol->private_value;
799 u8 value = ucontrol->value.integer.value[0];
800
801 if (value == omap_st_is_enabled(mc->reg))
802 return 0;
803
804 if (value)
805 omap_st_enable(mc->reg);
806 else
807 omap_st_disable(mc->reg);
808
809 return 1;
810}
811
812static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_value *ucontrol)
814{
815 struct soc_mixer_control *mc =
816 (struct soc_mixer_control *)kcontrol->private_value;
817
818 ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
819 return 0;
820}
821
822static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
823 SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
824 omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
825 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
826 -32768, 32767,
827 omap_mcbsp2_get_st_ch0_volume,
828 omap_mcbsp2_set_st_ch0_volume),
829 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
830 -32768, 32767,
831 omap_mcbsp2_get_st_ch1_volume,
832 omap_mcbsp2_set_st_ch1_volume),
833};
834
835static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
836 SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
837 omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
838 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
839 -32768, 32767,
840 omap_mcbsp3_get_st_ch0_volume,
841 omap_mcbsp3_set_st_ch0_volume),
842 OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
843 -32768, 32767,
844 omap_mcbsp3_get_st_ch1_volume,
845 omap_mcbsp3_set_st_ch1_volume),
846};
847
848int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
849{
850 if (!cpu_is_omap34xx())
851 return -ENODEV;
852
853 switch (mcbsp_id) {
854 case 1: /* McBSP 2 */
855 return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
856 ARRAY_SIZE(omap_mcbsp2_st_controls));
857 case 2: /* McBSP 3 */
858 return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
859 ARRAY_SIZE(omap_mcbsp3_st_controls));
860 default:
861 break;
862 }
863
864 return -EINVAL;
865}
866EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
867
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000868static __devinit int asoc_mcbsp_probe(struct platform_device *pdev)
869{
870 return snd_soc_register_dai(&pdev->dev, &omap_mcbsp_dai);
871}
872
873static int __devexit asoc_mcbsp_remove(struct platform_device *pdev)
874{
875 snd_soc_unregister_dai(&pdev->dev);
876 return 0;
877}
878
879static struct platform_driver asoc_mcbsp_driver = {
880 .driver = {
881 .name = "omap-mcbsp-dai",
882 .owner = THIS_MODULE,
883 },
884
885 .probe = asoc_mcbsp_probe,
886 .remove = __devexit_p(asoc_mcbsp_remove),
887};
888
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100889static int __init snd_omap_mcbsp_init(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000890{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891 return platform_driver_register(&asoc_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000892}
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100893module_init(snd_omap_mcbsp_init);
Mark Brown3f4b7832008-12-03 19:26:35 +0000894
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100895static void __exit snd_omap_mcbsp_exit(void)
Mark Brown3f4b7832008-12-03 19:26:35 +0000896{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897 platform_driver_unregister(&asoc_mcbsp_driver);
Mark Brown3f4b7832008-12-03 19:26:35 +0000898}
Takashi Iwaif73f2a62008-12-10 07:59:33 +0100899module_exit(snd_omap_mcbsp_exit);
Mark Brown3f4b7832008-12-03 19:26:35 +0000900
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300901MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200902MODULE_DESCRIPTION("OMAP I2S SoC Interface");
903MODULE_LICENSE("GPL");