ASoC: omap-mcbsp: start_threshold hack
[pandora-kernel.git] / sound / soc / omap / omap-mcbsp.c
1 /*
2  * omap-mcbsp.c  --  OMAP ALSA SoC DAI driver using McBSP port
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
7  *          Peter Ujfalusi <peter.ujfalusi@ti.com>
8  *
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
34 #include <plat/dma.h>
35 #include <plat/mcbsp.h>
36 #include "omap-mcbsp.h"
37 #include "omap-pcm.h"
38
39 #define OMAP_MCBSP_RATES        (SNDRV_PCM_RATE_8000_96000)
40
41 #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
49 struct omap_mcbsp_data {
50         unsigned int                    bus_id;
51         struct omap_mcbsp_reg_cfg       regs;
52         unsigned int                    fmt;
53         /*
54          * Flags indicating is the bus already activated and configured by
55          * another substream
56          */
57         int                             active;
58         int                             configured;
59         unsigned int                    in_freq;
60         int                             clk_div;
61         int                             wlen;
62 };
63
64 static 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  */
70 static struct omap_pcm_dma_data omap_mcbsp_dai_dma_params[NUM_LINKS][2];
71
72 static void omap_mcbsp_set_threshold(struct snd_pcm_substream *substream)
73 {
74         struct snd_soc_pcm_runtime *rtd = substream->private_data;
75         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
76         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
77         struct omap_pcm_dma_data *dma_data;
78         int dma_op_mode = omap_mcbsp_get_dma_op_mode(mcbsp_data->bus_id);
79         int words;
80
81         dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
82
83         /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
84         if (dma_op_mode == MCBSP_DMA_MODE_THRESHOLD)
85                 /*
86                  * Configure McBSP threshold based on either:
87                  * packet_size, when the sDMA is in packet mode, or
88                  * based on the period size.
89                  */
90                 if (dma_data->packet_size)
91                         words = dma_data->packet_size;
92                 else
93                         words = snd_pcm_lib_period_bytes(substream) /
94                                                         (mcbsp_data->wlen / 8);
95         else
96                 words = 1;
97
98         /* Configure McBSP internal buffer usage */
99         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
100                 omap_mcbsp_set_tx_threshold(mcbsp_data->bus_id, words);
101         else
102                 omap_mcbsp_set_rx_threshold(mcbsp_data->bus_id, words);
103 }
104
105 static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
106                                     struct snd_pcm_hw_rule *rule)
107 {
108         struct snd_interval *buffer_size = hw_param_interval(params,
109                                         SNDRV_PCM_HW_PARAM_BUFFER_SIZE);
110         struct snd_interval *channels = hw_param_interval(params,
111                                         SNDRV_PCM_HW_PARAM_CHANNELS);
112         struct omap_mcbsp_data *mcbsp_data = rule->private;
113         struct snd_interval frames;
114         int size;
115
116         snd_interval_any(&frames);
117         size = omap_mcbsp_get_fifo_size(mcbsp_data->bus_id);
118
119         frames.min = size / channels->min;
120         frames.integer = 1;
121         return snd_interval_refine(buffer_size, &frames);
122 }
123
124 static int omap_mcbsp_dai_startup(struct snd_pcm_substream *substream,
125                                   struct snd_soc_dai *cpu_dai)
126 {
127         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
128         int bus_id = mcbsp_data->bus_id;
129         int err = 0;
130
131         if (!cpu_dai->active)
132                 err = omap_mcbsp_request(bus_id);
133
134         /*
135          * OMAP3 McBSP FIFO is word structured.
136          * McBSP2 has 1024 + 256 = 1280 word long buffer,
137          * McBSP1,3,4,5 has 128 word long buffer
138          * This means that the size of the FIFO depends on the sample format.
139          * For example on McBSP3:
140          * 16bit samples: size is 128 * 2 = 256 bytes
141          * 32bit samples: size is 128 * 4 = 512 bytes
142          * It is simpler to place constraint for buffer and period based on
143          * channels.
144          * McBSP3 as example again (16 or 32 bit samples):
145          * 1 channel (mono): size is 128 frames (128 words)
146          * 2 channels (stereo): size is 128 / 2 = 64 frames (2 * 64 words)
147          * 4 channels: size is 128 / 4 = 32 frames (4 * 32 words)
148          */
149         if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
150                 /*
151                 * Rule for the buffer size. We should not allow
152                 * smaller buffer than the FIFO size to avoid underruns
153                 */
154                 snd_pcm_hw_rule_add(substream->runtime, 0,
155                                     SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
156                                     omap_mcbsp_hwrule_min_buffersize,
157                                     mcbsp_data,
158                                     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
159
160                 /* Make sure, that the period size is always even */
161                 snd_pcm_hw_constraint_step(substream->runtime, 0,
162                                            SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
163         }
164
165         return err;
166 }
167
168 static void omap_mcbsp_dai_shutdown(struct snd_pcm_substream *substream,
169                                     struct snd_soc_dai *cpu_dai)
170 {
171         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
172
173         if (!cpu_dai->active) {
174                 omap_mcbsp_free(mcbsp_data->bus_id);
175                 mcbsp_data->configured = 0;
176         }
177 }
178
179 static int omap_mcbsp_dai_trigger(struct snd_pcm_substream *substream, int cmd,
180                                   struct snd_soc_dai *cpu_dai)
181 {
182         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
183         int err = 0, play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
184
185         switch (cmd) {
186         case SNDRV_PCM_TRIGGER_START:
187         case SNDRV_PCM_TRIGGER_RESUME:
188         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
189                 mcbsp_data->active++;
190                 omap_mcbsp_start(mcbsp_data->bus_id, play, !play);
191                 break;
192
193         case SNDRV_PCM_TRIGGER_STOP:
194         case SNDRV_PCM_TRIGGER_SUSPEND:
195         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
196                 omap_mcbsp_stop(mcbsp_data->bus_id, play, !play);
197                 mcbsp_data->active--;
198                 break;
199         default:
200                 err = -EINVAL;
201         }
202
203         return err;
204 }
205
206 static snd_pcm_sframes_t omap_mcbsp_dai_delay(
207                         struct snd_pcm_substream *substream,
208                         struct snd_soc_dai *dai)
209 {
210         struct snd_soc_pcm_runtime *rtd = substream->private_data;
211         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
212         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
213         u16 fifo_use;
214         snd_pcm_sframes_t delay;
215
216         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
217                 fifo_use = omap_mcbsp_get_tx_delay(mcbsp_data->bus_id);
218         else
219                 fifo_use = omap_mcbsp_get_rx_delay(mcbsp_data->bus_id);
220
221         /*
222          * Divide the used locations with the channel count to get the
223          * FIFO usage in samples (don't care about partial samples in the
224          * buffer).
225          */
226         delay = fifo_use / substream->runtime->channels;
227
228         return delay;
229 }
230
231 static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
232                                     struct snd_pcm_hw_params *params,
233                                     struct snd_soc_dai *cpu_dai)
234 {
235         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
236         struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
237         struct omap_pcm_dma_data *dma_data;
238         int dma, bus_id = mcbsp_data->bus_id;
239         int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
240         int pkt_size = 0;
241         unsigned long port;
242         unsigned int format, div, framesize, master;
243
244         dma_data = &omap_mcbsp_dai_dma_params[cpu_dai->id][substream->stream];
245
246         dma = omap_mcbsp_dma_ch_params(bus_id, substream->stream);
247         port = omap_mcbsp_dma_reg_params(bus_id, substream->stream);
248
249         switch (params_format(params)) {
250         case SNDRV_PCM_FORMAT_S16_LE:
251                 dma_data->data_type = OMAP_DMA_DATA_TYPE_S16;
252                 wlen = 16;
253                 break;
254         case SNDRV_PCM_FORMAT_S32_LE:
255                 dma_data->data_type = OMAP_DMA_DATA_TYPE_S32;
256                 wlen = 32;
257                 break;
258         default:
259                 return -EINVAL;
260         }
261         if (cpu_is_omap34xx()) {
262                 dma_data->set_threshold = omap_mcbsp_set_threshold;
263                 /* TODO: Currently, MODE_ELEMENT == MODE_FRAME */
264                 if (omap_mcbsp_get_dma_op_mode(bus_id) ==
265                                                 MCBSP_DMA_MODE_THRESHOLD) {
266                         int period_words, max_thrsh;
267
268                         period_words = params_period_bytes(params) / (wlen / 8);
269                         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
270                                 max_thrsh = omap_mcbsp_get_max_tx_threshold(
271                                                             mcbsp_data->bus_id);
272                         else
273                                 max_thrsh = omap_mcbsp_get_max_rx_threshold(
274                                                             mcbsp_data->bus_id);
275                         /*
276                          * If the period contains less or equal number of words,
277                          * we are using the original threshold mode setup:
278                          * McBSP threshold = sDMA frame size = period_size
279                          * Otherwise we switch to sDMA packet mode:
280                          * McBSP threshold = sDMA packet size
281                          * sDMA frame size = period size
282                          */
283                         if (period_words > max_thrsh) {
284                                 int divider = 0;
285
286                                 /*
287                                  * Look for the biggest threshold value, which
288                                  * divides the period size evenly.
289                                  */
290                                 divider = period_words / max_thrsh;
291                                 if (period_words % max_thrsh)
292                                         divider++;
293                                 while (period_words % divider &&
294                                         divider < period_words)
295                                         divider++;
296                                 if (divider == period_words)
297                                         return -EINVAL;
298
299                                 pkt_size = period_words / divider;
300                                 sync_mode = OMAP_DMA_SYNC_PACKET;
301                         } else {
302                                 sync_mode = OMAP_DMA_SYNC_FRAME;
303                         }
304                 }
305         }
306
307         dma_data->name = substream->stream ? "Audio Capture" : "Audio Playback";
308         dma_data->dma_req = dma;
309         dma_data->port_addr = port;
310         dma_data->sync_mode = sync_mode;
311         dma_data->packet_size = pkt_size;
312
313         snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
314
315 #if 0
316         if (mcbsp_data->configured) {
317                 /* McBSP already configured by another stream */
318                 return 0;
319         }
320 #endif
321
322         regs->rcr2      &= ~(RPHASE | RFRLEN2(0x7f) | RWDLEN2(7));
323         regs->xcr2      &= ~(RPHASE | XFRLEN2(0x7f) | XWDLEN2(7));
324         regs->rcr1      &= ~(RFRLEN1(0x7f) | RWDLEN1(7));
325         regs->xcr1      &= ~(XFRLEN1(0x7f) | XWDLEN1(7));
326         format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
327         wpf = channels = params_channels(params);
328         if (channels == 2 && (format == SND_SOC_DAIFMT_I2S ||
329                               format == SND_SOC_DAIFMT_LEFT_J)) {
330                 /* Use dual-phase frames */
331                 regs->rcr2      |= RPHASE;
332                 regs->xcr2      |= XPHASE;
333                 /* Set 1 word per (McBSP) frame for phase1 and phase2 */
334                 wpf--;
335                 regs->rcr2      |= RFRLEN2(wpf - 1);
336                 regs->xcr2      |= XFRLEN2(wpf - 1);
337         }
338
339         regs->rcr1      |= RFRLEN1(wpf - 1);
340         regs->xcr1      |= XFRLEN1(wpf - 1);
341
342         switch (params_format(params)) {
343         case SNDRV_PCM_FORMAT_S16_LE:
344                 /* Set word lengths */
345                 regs->rcr2      |= RWDLEN2(OMAP_MCBSP_WORD_16);
346                 regs->rcr1      |= RWDLEN1(OMAP_MCBSP_WORD_16);
347                 regs->xcr2      |= XWDLEN2(OMAP_MCBSP_WORD_16);
348                 regs->xcr1      |= XWDLEN1(OMAP_MCBSP_WORD_16);
349                 break;
350         case SNDRV_PCM_FORMAT_S32_LE:
351                 /* Set word lengths */
352                 regs->rcr2      |= RWDLEN2(OMAP_MCBSP_WORD_32);
353                 regs->rcr1      |= RWDLEN1(OMAP_MCBSP_WORD_32);
354                 regs->xcr2      |= XWDLEN2(OMAP_MCBSP_WORD_32);
355                 regs->xcr1      |= XWDLEN1(OMAP_MCBSP_WORD_32);
356                 break;
357         default:
358                 /* Unsupported PCM format */
359                 return -EINVAL;
360         }
361
362         /* In McBSP master modes, FRAME (i.e. sample rate) is generated
363          * by _counting_ BCLKs. Calculate frame size in BCLKs */
364         master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
365         if (master ==   SND_SOC_DAIFMT_CBS_CFS) {
366                 div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
367                 framesize = (mcbsp_data->in_freq / div) / params_rate(params);
368
369                 if (framesize < wlen * channels) {
370                         printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
371                                         "channels\n", __func__);
372                         return -EINVAL;
373                 }
374         } else
375                 framesize = wlen * channels;
376
377         /* Set FS period and length in terms of bit clock periods */
378         regs->srgr2     &= ~FPER(0xfff);
379         regs->srgr1     &= ~FWID(0xff);
380         switch (format) {
381         case SND_SOC_DAIFMT_I2S:
382         case SND_SOC_DAIFMT_LEFT_J:
383                 regs->srgr2     |= FPER(framesize - 1);
384                 regs->srgr1     |= FWID((framesize >> 1) - 1);
385                 break;
386         case SND_SOC_DAIFMT_DSP_A:
387         case SND_SOC_DAIFMT_DSP_B:
388                 regs->srgr2     |= FPER(framesize - 1);
389                 regs->srgr1     |= FWID(0);
390                 break;
391         }
392
393         omap_mcbsp_config(bus_id, &mcbsp_data->regs);
394         mcbsp_data->wlen = wlen;
395         mcbsp_data->configured = 1;
396
397         return 0;
398 }
399
400 /*
401  * This must be called before _set_clkdiv and _set_sysclk since McBSP register
402  * cache is initialized here
403  */
404 static int omap_mcbsp_dai_set_dai_fmt(struct snd_soc_dai *cpu_dai,
405                                       unsigned int fmt)
406 {
407         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
408         struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
409         bool inv_fs = false;
410
411         if (mcbsp_data->configured)
412                 return 0;
413
414         mcbsp_data->fmt = fmt;
415         memset(regs, 0, sizeof(*regs));
416         /* Generic McBSP register settings */
417         regs->spcr2     |= XINTM(3) | FREE;
418         regs->spcr1     |= RINTM(3);
419         /* RFIG and XFIG are not defined in 34xx */
420         if (!cpu_is_omap34xx() && !cpu_is_omap44xx()) {
421                 regs->rcr2      |= RFIG;
422                 regs->xcr2      |= XFIG;
423         }
424         if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
425                 regs->xccr = DXENDLY(1) | XDMAEN | XDISABLE;
426                 regs->rccr = RFULL_CYCLE | RDMAEN | RDISABLE;
427         }
428
429         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
430         case SND_SOC_DAIFMT_I2S:
431                 /* 1-bit data delay */
432                 regs->rcr2      |= RDATDLY(1);
433                 regs->xcr2      |= XDATDLY(1);
434                 break;
435         case SND_SOC_DAIFMT_LEFT_J:
436                 /* 0-bit data delay */
437                 regs->rcr2      |= RDATDLY(0);
438                 regs->xcr2      |= XDATDLY(0);
439                 regs->spcr1     |= RJUST(2);
440                 /* Invert FS polarity configuration */
441                 inv_fs = true;
442                 break;
443         case SND_SOC_DAIFMT_DSP_A:
444                 /* 1-bit data delay */
445                 regs->rcr2      |= RDATDLY(1);
446                 regs->xcr2      |= XDATDLY(1);
447                 /* Invert FS polarity configuration */
448                 inv_fs = true;
449                 break;
450         case SND_SOC_DAIFMT_DSP_B:
451                 /* 0-bit data delay */
452                 regs->rcr2      |= RDATDLY(0);
453                 regs->xcr2      |= XDATDLY(0);
454                 /* Invert FS polarity configuration */
455                 inv_fs = true;
456                 break;
457         default:
458                 /* Unsupported data format */
459                 return -EINVAL;
460         }
461
462         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
463         case SND_SOC_DAIFMT_CBS_CFS:
464                 /* McBSP master. Set FS and bit clocks as outputs */
465                 regs->pcr0      |= FSXM | FSRM |
466                                    CLKXM | CLKRM;
467                 /* Sample rate generator drives the FS */
468                 regs->srgr2     |= FSGM;
469                 break;
470         case SND_SOC_DAIFMT_CBM_CFM:
471                 /* McBSP slave */
472                 break;
473         default:
474                 /* Unsupported master/slave configuration */
475                 return -EINVAL;
476         }
477
478         /* Set bit clock (CLKX/CLKR) and FS polarities */
479         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
480         case SND_SOC_DAIFMT_NB_NF:
481                 /*
482                  * Normal BCLK + FS.
483                  * FS active low. TX data driven on falling edge of bit clock
484                  * and RX data sampled on rising edge of bit clock.
485                  */
486                 regs->pcr0      |= FSXP | FSRP |
487                                    CLKXP | CLKRP;
488                 break;
489         case SND_SOC_DAIFMT_NB_IF:
490                 regs->pcr0      |= CLKXP | CLKRP;
491                 break;
492         case SND_SOC_DAIFMT_IB_NF:
493                 regs->pcr0      |= FSXP | FSRP;
494                 break;
495         case SND_SOC_DAIFMT_IB_IF:
496                 break;
497         default:
498                 return -EINVAL;
499         }
500         if (inv_fs == true)
501                 regs->pcr0 ^= FSXP | FSRP;
502
503         return 0;
504 }
505
506 static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
507                                      int div_id, int div)
508 {
509         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
510         struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
511
512         if (div_id != OMAP_MCBSP_CLKGDV)
513                 return -ENODEV;
514
515         mcbsp_data->clk_div = div;
516         regs->srgr1     &= ~CLKGDV(0xff);
517         regs->srgr1     |= CLKGDV(div - 1);
518
519         return 0;
520 }
521
522 static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
523                                          int clk_id, unsigned int freq,
524                                          int dir)
525 {
526         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
527         struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
528         int err = 0;
529
530         if (mcbsp_data->active) {
531                 if (freq == mcbsp_data->in_freq)
532                         return 0;
533                 else
534                         return -EBUSY;
535         }
536
537         /* The McBSP signal muxing functions are only available on McBSP1 */
538         if (clk_id == OMAP_MCBSP_CLKR_SRC_CLKR ||
539             clk_id == OMAP_MCBSP_CLKR_SRC_CLKX ||
540             clk_id == OMAP_MCBSP_FSR_SRC_FSR ||
541             clk_id == OMAP_MCBSP_FSR_SRC_FSX)
542                 if (cpu_class_is_omap1() || mcbsp_data->bus_id != 0)
543                         return -EINVAL;
544
545         mcbsp_data->in_freq = freq;
546         regs->srgr2     &= ~CLKSM;
547         regs->pcr0      &= ~SCLKME;
548
549         switch (clk_id) {
550         case OMAP_MCBSP_SYSCLK_CLK:
551                 regs->srgr2     |= CLKSM;
552                 break;
553         case OMAP_MCBSP_SYSCLK_CLKS_FCLK:
554                 if (cpu_class_is_omap1()) {
555                         err = -EINVAL;
556                         break;
557                 }
558                 err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
559                                                MCBSP_CLKS_PRCM_SRC);
560                 break;
561         case OMAP_MCBSP_SYSCLK_CLKS_EXT:
562                 if (cpu_class_is_omap1()) {
563                         err = 0;
564                         break;
565                 }
566                 err = omap2_mcbsp_set_clks_src(mcbsp_data->bus_id,
567                                                MCBSP_CLKS_PAD_SRC);
568                 break;
569
570         case OMAP_MCBSP_SYSCLK_CLKX_EXT:
571                 regs->srgr2     |= CLKSM;
572         case OMAP_MCBSP_SYSCLK_CLKR_EXT:
573                 regs->pcr0      |= SCLKME;
574                 break;
575
576
577         case OMAP_MCBSP_CLKR_SRC_CLKR:
578                 if (cpu_class_is_omap1())
579                         break;
580                 omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKR);
581                 break;
582         case OMAP_MCBSP_CLKR_SRC_CLKX:
583                 if (cpu_class_is_omap1())
584                         break;
585                 omap2_mcbsp1_mux_clkr_src(CLKR_SRC_CLKX);
586                 break;
587         case OMAP_MCBSP_FSR_SRC_FSR:
588                 if (cpu_class_is_omap1())
589                         break;
590                 omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSR);
591                 break;
592         case OMAP_MCBSP_FSR_SRC_FSX:
593                 if (cpu_class_is_omap1())
594                         break;
595                 omap2_mcbsp1_mux_fsr_src(FSR_SRC_FSX);
596                 break;
597         default:
598                 err = -ENODEV;
599         }
600
601         return err;
602 }
603
604 /* 
605  * We have to be sure there is more than FIFO size worth of data ready
606  * before starting, or else we get underflow right after start.
607  * XXX: To make realtime streaming work, setting this to fifo+period
608  * as DMA uses period boundaries, there must be enough data at those.
609  */
610 static int omap_mcbsp_dai_prepare(struct snd_pcm_substream *substream,
611                                   struct snd_soc_dai *cpu_dai)
612 {
613         struct omap_mcbsp_data *mcbsp_data = snd_soc_dai_get_drvdata(cpu_dai);
614         struct snd_pcm_runtime *runtime = substream->runtime;
615         int size;
616
617         size = omap_mcbsp_get_fifo_size(mcbsp_data->bus_id);
618         size /= substream->runtime->channels;
619
620         size += bytes_to_frames(runtime, snd_pcm_lib_period_bytes(substream));
621
622         if (runtime->start_threshold < size) {
623                 runtime->start_threshold = size;
624                 if (runtime->start_threshold > runtime->buffer_size)
625                         runtime->start_threshold = runtime->buffer_size;
626         }
627
628         return 0;
629 }
630
631 static struct snd_soc_dai_ops mcbsp_dai_ops = {
632         .startup        = omap_mcbsp_dai_startup,
633         .shutdown       = omap_mcbsp_dai_shutdown,
634         .trigger        = omap_mcbsp_dai_trigger,
635         .delay          = omap_mcbsp_dai_delay,
636         .hw_params      = omap_mcbsp_dai_hw_params,
637         .set_fmt        = omap_mcbsp_dai_set_dai_fmt,
638         .set_clkdiv     = omap_mcbsp_dai_set_clkdiv,
639         .set_sysclk     = omap_mcbsp_dai_set_dai_sysclk,
640         .prepare        = omap_mcbsp_dai_prepare,
641 };
642
643 static int mcbsp_dai_probe(struct snd_soc_dai *dai)
644 {
645         mcbsp_data[dai->id].bus_id = dai->id;
646         snd_soc_dai_set_drvdata(dai, &mcbsp_data[dai->id].bus_id);
647         return 0;
648 }
649
650 static struct snd_soc_dai_driver omap_mcbsp_dai = {
651         .probe = mcbsp_dai_probe,
652         .playback = {
653                 .channels_min = 1,
654                 .channels_max = 16,
655                 .rates = OMAP_MCBSP_RATES,
656                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
657         },
658         .capture = {
659                 .channels_min = 1,
660                 .channels_max = 16,
661                 .rates = OMAP_MCBSP_RATES,
662                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
663         },
664         .ops = &mcbsp_dai_ops,
665 };
666
667 static int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
668                         struct snd_ctl_elem_info *uinfo)
669 {
670         struct soc_mixer_control *mc =
671                 (struct soc_mixer_control *)kcontrol->private_value;
672         int max = mc->max;
673         int min = mc->min;
674
675         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
676         uinfo->count = 1;
677         uinfo->value.integer.min = min;
678         uinfo->value.integer.max = max;
679         return 0;
680 }
681
682 #define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel)                   \
683 static int                                                              \
684 omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc,   \
685                                         struct snd_ctl_elem_value *uc)  \
686 {                                                                       \
687         struct soc_mixer_control *mc =                                  \
688                 (struct soc_mixer_control *)kc->private_value;          \
689         int max = mc->max;                                              \
690         int min = mc->min;                                              \
691         int val = uc->value.integer.value[0];                           \
692                                                                         \
693         if (val < min || val > max)                                     \
694                 return -EINVAL;                                         \
695                                                                         \
696         /* OMAP McBSP implementation uses index values 0..4 */          \
697         return omap_st_set_chgain((id)-1, channel, val);                \
698 }
699
700 #define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel)                   \
701 static int                                                              \
702 omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc,   \
703                                         struct snd_ctl_elem_value *uc)  \
704 {                                                                       \
705         s16 chgain;                                                     \
706                                                                         \
707         if (omap_st_get_chgain((id)-1, channel, &chgain))               \
708                 return -EAGAIN;                                         \
709                                                                         \
710         uc->value.integer.value[0] = chgain;                            \
711         return 0;                                                       \
712 }
713
714 OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
715 OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
716 OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
717 OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
718 OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
719 OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
720 OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
721 OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
722
723 static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
724                                 struct snd_ctl_elem_value *ucontrol)
725 {
726         struct soc_mixer_control *mc =
727                 (struct soc_mixer_control *)kcontrol->private_value;
728         u8 value = ucontrol->value.integer.value[0];
729
730         if (value == omap_st_is_enabled(mc->reg))
731                 return 0;
732
733         if (value)
734                 omap_st_enable(mc->reg);
735         else
736                 omap_st_disable(mc->reg);
737
738         return 1;
739 }
740
741 static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
742                                 struct snd_ctl_elem_value *ucontrol)
743 {
744         struct soc_mixer_control *mc =
745                 (struct soc_mixer_control *)kcontrol->private_value;
746
747         ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
748         return 0;
749 }
750
751 static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
752         SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
753                         omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
754         OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
755                                       -32768, 32767,
756                                       omap_mcbsp2_get_st_ch0_volume,
757                                       omap_mcbsp2_set_st_ch0_volume),
758         OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
759                                       -32768, 32767,
760                                       omap_mcbsp2_get_st_ch1_volume,
761                                       omap_mcbsp2_set_st_ch1_volume),
762 };
763
764 static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
765         SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
766                         omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
767         OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
768                                       -32768, 32767,
769                                       omap_mcbsp3_get_st_ch0_volume,
770                                       omap_mcbsp3_set_st_ch0_volume),
771         OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
772                                       -32768, 32767,
773                                       omap_mcbsp3_get_st_ch1_volume,
774                                       omap_mcbsp3_set_st_ch1_volume),
775 };
776
777 int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
778 {
779         if (!cpu_is_omap34xx())
780                 return -ENODEV;
781
782         switch (mcbsp_id) {
783         case 1: /* McBSP 2 */
784                 return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
785                                         ARRAY_SIZE(omap_mcbsp2_st_controls));
786         case 2: /* McBSP 3 */
787                 return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
788                                         ARRAY_SIZE(omap_mcbsp3_st_controls));
789         default:
790                 break;
791         }
792
793         return -EINVAL;
794 }
795 EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
796
797 static __devinit int asoc_mcbsp_probe(struct platform_device *pdev)
798 {
799         return snd_soc_register_dai(&pdev->dev, &omap_mcbsp_dai);
800 }
801
802 static int __devexit asoc_mcbsp_remove(struct platform_device *pdev)
803 {
804         snd_soc_unregister_dai(&pdev->dev);
805         return 0;
806 }
807
808 static struct platform_driver asoc_mcbsp_driver = {
809         .driver = {
810                         .name = "omap-mcbsp-dai",
811                         .owner = THIS_MODULE,
812         },
813
814         .probe = asoc_mcbsp_probe,
815         .remove = __devexit_p(asoc_mcbsp_remove),
816 };
817
818 static int __init snd_omap_mcbsp_init(void)
819 {
820         return platform_driver_register(&asoc_mcbsp_driver);
821 }
822 module_init(snd_omap_mcbsp_init);
823
824 static void __exit snd_omap_mcbsp_exit(void)
825 {
826         platform_driver_unregister(&asoc_mcbsp_driver);
827 }
828 module_exit(snd_omap_mcbsp_exit);
829
830 MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
831 MODULE_DESCRIPTION("OMAP I2S SoC Interface");
832 MODULE_LICENSE("GPL");