Merge with mainline to remove plat-omap/Kconfig conflict
[safe/jmp/linux-2.6] / sound / soc / omap / omap-mcbsp.c
index e8e63ba..e814a95 100644 (file)
 
 #define OMAP_MCBSP_RATES       (SNDRV_PCM_RATE_8000_96000)
 
+#define OMAP_MCBSP_SOC_SINGLE_S16_EXT(xname, xmin, xmax, \
+       xhandler_get, xhandler_put) \
+{      .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
+       .info = omap_mcbsp_st_info_volsw, \
+       .get = xhandler_get, .put = xhandler_put, \
+       .private_value = (unsigned long) &(struct soc_mixer_control) \
+       {.min = xmin, .max = xmax} }
+
 struct omap_mcbsp_data {
        unsigned int                    bus_id;
        struct omap_mcbsp_reg_cfg       regs;
@@ -49,6 +57,8 @@ struct omap_mcbsp_data {
         */
        int                             active;
        int                             configured;
+       unsigned int                    in_freq;
+       int                             clk_div;
 };
 
 #define to_mcbsp(priv) container_of((priv), struct omap_mcbsp_data, bus_id)
@@ -80,11 +90,11 @@ static const int omap1_dma_reqs[][2] = {};
 static const unsigned long omap1_mcbsp_port[][2] = {};
 #endif
 
-#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
+#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
 static const int omap24xx_dma_reqs[][2] = {
        { OMAP24XX_DMA_MCBSP1_TX, OMAP24XX_DMA_MCBSP1_RX },
        { OMAP24XX_DMA_MCBSP2_TX, OMAP24XX_DMA_MCBSP2_RX },
-#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
+#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3)
        { OMAP24XX_DMA_MCBSP3_TX, OMAP24XX_DMA_MCBSP3_RX },
        { OMAP24XX_DMA_MCBSP4_TX, OMAP24XX_DMA_MCBSP4_RX },
        { OMAP24XX_DMA_MCBSP5_TX, OMAP24XX_DMA_MCBSP5_RX },
@@ -122,7 +132,7 @@ static const unsigned long omap2430_mcbsp_port[][2] = {
 static const unsigned long omap2430_mcbsp_port[][2] = {};
 #endif
 
-#if defined(CONFIG_ARCH_OMAP34XX)
+#if defined(CONFIG_ARCH_OMAP3)
 static const unsigned long omap34xx_mcbsp_port[][2] = {
        { OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DXR,
          OMAP34XX_MCBSP1_BASE + OMAP_MCBSP_REG_DRR },
@@ -257,7 +267,7 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
        int dma, bus_id = mcbsp_data->bus_id, id = cpu_dai->id;
        int wlen, channels, wpf, sync_mode = OMAP_DMA_SYNC_ELEMENT;
        unsigned long port;
-       unsigned int format;
+       unsigned int format, div, framesize, master;
 
        if (cpu_class_is_omap1()) {
                dma = omap1_dma_reqs[bus_id][substream->stream];
@@ -285,6 +295,8 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
        omap_mcbsp_dai_dma_params[id][substream->stream].dma_req = dma;
        omap_mcbsp_dai_dma_params[id][substream->stream].port_addr = port;
        omap_mcbsp_dai_dma_params[id][substream->stream].sync_mode = sync_mode;
+       omap_mcbsp_dai_dma_params[id][substream->stream].data_type =
+                                                       OMAP_DMA_DATA_TYPE_S16;
        cpu_dai->dma_data = &omap_mcbsp_dai_dma_params[id][substream->stream];
 
        if (mcbsp_data->configured) {
@@ -294,28 +306,19 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
 
        format = mcbsp_data->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
        wpf = channels = params_channels(params);
-       switch (channels) {
-       case 2:
-               if (format == SND_SOC_DAIFMT_I2S) {
-                       /* Use dual-phase frames */
-                       regs->rcr2      |= RPHASE;
-                       regs->xcr2      |= XPHASE;
-                       /* Set 1 word per (McBSP) frame for phase1 and phase2 */
-                       wpf--;
-                       regs->rcr2      |= RFRLEN2(wpf - 1);
-                       regs->xcr2      |= XFRLEN2(wpf - 1);
-               }
-       case 1:
-       case 4:
-               /* Set word per (McBSP) frame for phase1 */
-               regs->rcr1      |= RFRLEN1(wpf - 1);
-               regs->xcr1      |= XFRLEN1(wpf - 1);
-               break;
-       default:
-               /* Unsupported number of channels */
-               return -EINVAL;
+       if (channels == 2 && format == SND_SOC_DAIFMT_I2S) {
+               /* Use dual-phase frames */
+               regs->rcr2      |= RPHASE;
+               regs->xcr2      |= XPHASE;
+               /* Set 1 word per (McBSP) frame for phase1 and phase2 */
+               wpf--;
+               regs->rcr2      |= RFRLEN2(wpf - 1);
+               regs->xcr2      |= XFRLEN2(wpf - 1);
        }
 
+       regs->rcr1      |= RFRLEN1(wpf - 1);
+       regs->xcr1      |= XFRLEN1(wpf - 1);
+
        switch (params_format(params)) {
        case SNDRV_PCM_FORMAT_S16_LE:
                /* Set word lengths */
@@ -330,15 +333,30 @@ static int omap_mcbsp_dai_hw_params(struct snd_pcm_substream *substream,
                return -EINVAL;
        }
 
+       /* In McBSP master modes, FRAME (i.e. sample rate) is generated
+        * by _counting_ BCLKs. Calculate frame size in BCLKs */
+       master = mcbsp_data->fmt & SND_SOC_DAIFMT_MASTER_MASK;
+       if (master ==   SND_SOC_DAIFMT_CBS_CFS) {
+               div = mcbsp_data->clk_div ? mcbsp_data->clk_div : 1;
+               framesize = (mcbsp_data->in_freq / div) / params_rate(params);
+
+               if (framesize < wlen * channels) {
+                       printk(KERN_ERR "%s: not enough bandwidth for desired rate and "
+                                       "channels\n", __func__);
+                       return -EINVAL;
+               }
+       } else
+               framesize = wlen * channels;
+
        /* Set FS period and length in terms of bit clock periods */
        switch (format) {
        case SND_SOC_DAIFMT_I2S:
-               regs->srgr2     |= FPER(wlen * channels - 1);
-               regs->srgr1     |= FWID(wlen - 1);
+               regs->srgr2     |= FPER(framesize - 1);
+               regs->srgr1     |= FWID((framesize >> 1) - 1);
                break;
        case SND_SOC_DAIFMT_DSP_A:
        case SND_SOC_DAIFMT_DSP_B:
-               regs->srgr2     |= FPER(wlen * channels - 1);
+               regs->srgr2     |= FPER(framesize - 1);
                regs->srgr1     |= FWID(0);
                break;
        }
@@ -454,6 +472,7 @@ static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
        if (div_id != OMAP_MCBSP_CLKGDV)
                return -ENODEV;
 
+       mcbsp_data->clk_div = div;
        regs->srgr1     |= CLKGDV(div - 1);
 
        return 0;
@@ -554,6 +573,8 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
        struct omap_mcbsp_reg_cfg *regs = &mcbsp_data->regs;
        int err = 0;
 
+       mcbsp_data->in_freq = freq;
+
        switch (clk_id) {
        case OMAP_MCBSP_SYSCLK_CLK:
                regs->srgr2     |= CLKSM;
@@ -598,13 +619,13 @@ static struct snd_soc_dai_ops omap_mcbsp_dai_ops = {
        .id = (link_id),                                        \
        .playback = {                                           \
                .channels_min = 1,                              \
-               .channels_max = 4,                              \
+               .channels_max = 16,                             \
                .rates = OMAP_MCBSP_RATES,                      \
                .formats = SNDRV_PCM_FMTBIT_S16_LE,             \
        },                                                      \
        .capture = {                                            \
                .channels_min = 1,                              \
-               .channels_max = 4,                              \
+               .channels_max = 16,                             \
                .rates = OMAP_MCBSP_RATES,                      \
                .formats = SNDRV_PCM_FMTBIT_S16_LE,             \
        },                                                      \
@@ -626,6 +647,136 @@ struct snd_soc_dai omap_mcbsp_dai[] = {
 
 EXPORT_SYMBOL_GPL(omap_mcbsp_dai);
 
+int omap_mcbsp_st_info_volsw(struct snd_kcontrol *kcontrol,
+                       struct snd_ctl_elem_info *uinfo)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+       int max = mc->max;
+       int min = mc->min;
+
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+       uinfo->count = 1;
+       uinfo->value.integer.min = min;
+       uinfo->value.integer.max = max;
+       return 0;
+}
+
+#define OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(id, channel)                  \
+static int                                                             \
+omap_mcbsp##id##_set_st_ch##channel##_volume(struct snd_kcontrol *kc,  \
+                                       struct snd_ctl_elem_value *uc)  \
+{                                                                      \
+       struct soc_mixer_control *mc =                                  \
+               (struct soc_mixer_control *)kc->private_value;          \
+       int max = mc->max;                                              \
+       int min = mc->min;                                              \
+       int val = uc->value.integer.value[0];                           \
+                                                                       \
+       if (val < min || val > max)                                     \
+               return -EINVAL;                                         \
+                                                                       \
+       /* OMAP McBSP implementation uses index values 0..4 */          \
+       return omap_st_set_chgain((id)-1, channel, val);                \
+}
+
+#define OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(id, channel)                  \
+static int                                                             \
+omap_mcbsp##id##_get_st_ch##channel##_volume(struct snd_kcontrol *kc,  \
+                                       struct snd_ctl_elem_value *uc)  \
+{                                                                      \
+       s16 chgain;                                                     \
+                                                                       \
+       if (omap_st_get_chgain((id)-1, channel, &chgain))               \
+               return -EAGAIN;                                         \
+                                                                       \
+       uc->value.integer.value[0] = chgain;                            \
+       return 0;                                                       \
+}
+
+OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 0)
+OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(2, 1)
+OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 0)
+OMAP_MCBSP_ST_SET_CHANNEL_VOLUME(3, 1)
+OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 0)
+OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(2, 1)
+OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 0)
+OMAP_MCBSP_ST_GET_CHANNEL_VOLUME(3, 1)
+
+static int omap_mcbsp_st_put_mode(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_value *ucontrol)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+       u8 value = ucontrol->value.integer.value[0];
+
+       if (value == omap_st_is_enabled(mc->reg))
+               return 0;
+
+       if (value)
+               omap_st_enable(mc->reg);
+       else
+               omap_st_disable(mc->reg);
+
+       return 1;
+}
+
+static int omap_mcbsp_st_get_mode(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_value *ucontrol)
+{
+       struct soc_mixer_control *mc =
+               (struct soc_mixer_control *)kcontrol->private_value;
+
+       ucontrol->value.integer.value[0] = omap_st_is_enabled(mc->reg);
+       return 0;
+}
+
+static const struct snd_kcontrol_new omap_mcbsp2_st_controls[] = {
+       SOC_SINGLE_EXT("McBSP2 Sidetone Switch", 1, 0, 1, 0,
+                       omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
+       OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 0 Volume",
+                                     -32768, 32767,
+                                     omap_mcbsp2_get_st_ch0_volume,
+                                     omap_mcbsp2_set_st_ch0_volume),
+       OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP2 Sidetone Channel 1 Volume",
+                                     -32768, 32767,
+                                     omap_mcbsp2_get_st_ch1_volume,
+                                     omap_mcbsp2_set_st_ch1_volume),
+};
+
+static const struct snd_kcontrol_new omap_mcbsp3_st_controls[] = {
+       SOC_SINGLE_EXT("McBSP3 Sidetone Switch", 2, 0, 1, 0,
+                       omap_mcbsp_st_get_mode, omap_mcbsp_st_put_mode),
+       OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 0 Volume",
+                                     -32768, 32767,
+                                     omap_mcbsp3_get_st_ch0_volume,
+                                     omap_mcbsp3_set_st_ch0_volume),
+       OMAP_MCBSP_SOC_SINGLE_S16_EXT("McBSP3 Sidetone Channel 1 Volume",
+                                     -32768, 32767,
+                                     omap_mcbsp3_get_st_ch1_volume,
+                                     omap_mcbsp3_set_st_ch1_volume),
+};
+
+int omap_mcbsp_st_add_controls(struct snd_soc_codec *codec, int mcbsp_id)
+{
+       if (!cpu_is_omap34xx())
+               return -ENODEV;
+
+       switch (mcbsp_id) {
+       case 1: /* McBSP 2 */
+               return snd_soc_add_controls(codec, omap_mcbsp2_st_controls,
+                                       ARRAY_SIZE(omap_mcbsp2_st_controls));
+       case 2: /* McBSP 3 */
+               return snd_soc_add_controls(codec, omap_mcbsp3_st_controls,
+                                       ARRAY_SIZE(omap_mcbsp3_st_controls));
+       default:
+               break;
+       }
+
+       return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(omap_mcbsp_st_add_controls);
+
 static int __init snd_omap_mcbsp_init(void)
 {
        return snd_soc_register_dais(omap_mcbsp_dai,