Merge branch 'for-2.6.40' of git://git.kernel.org/pub/scm/linux/kernel/git/lrg/asoc...
[linux-2.6-block.git] / sound / soc / codecs / wm8958-dsp2.c
index d0e257315d975b475f55260f4c627c52b15a5bc1..0293763debe5811160fb3b7cc23284a91df2a94a 100644 (file)
@@ -99,7 +99,7 @@ static int wm8958_dsp2_fw(struct snd_soc_codec *codec, const char *name,
        len = fw->size - len;
        while (len) {
                if (len < 12) {
-                       dev_err(codec->dev, "%s short data block of %d\n",
+                       dev_err(codec->dev, "%s short data block of %zd\n",
                                name, len);
                        goto err;
                }
@@ -107,7 +107,7 @@ static int wm8958_dsp2_fw(struct snd_soc_codec *codec, const char *name,
                memcpy(&data32, data + 4, sizeof(data32));
                block_len = be32_to_cpu(data32);
                if (block_len + 8 > len) {
-                       dev_err(codec->dev, "%d byte block longer than file\n",
+                       dev_err(codec->dev, "%zd byte block longer than file\n",
                                block_len);
                        goto err;
                }
@@ -141,7 +141,7 @@ static int wm8958_dsp2_fw(struct snd_soc_codec *codec, const char *name,
                case WM_FW_BLOCK_I:
                case WM_FW_BLOCK_A:
                case WM_FW_BLOCK_C:
-                       dev_dbg(codec->dev, "%s: %d bytes of %x@%x\n", name,
+                       dev_dbg(codec->dev, "%s: %zd bytes of %x@%x\n", name,
                                block_len, (data32 >> 24) & 0xff,
                                data32 & 0xffffff);
 
@@ -294,6 +294,36 @@ static void wm8958_dsp_start_vss(struct snd_soc_codec *codec, int path)
                            path << WM8958_MBC_SEL_SHIFT | WM8958_MBC_ENA);
 }
 
+static void wm8958_dsp_start_enh_eq(struct snd_soc_codec *codec, int path)
+{
+       struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
+       struct wm8994_pdata *pdata = wm8994->pdata;
+       int i;
+
+       wm8958_dsp2_fw(codec, "ENH_EQ", wm8994->enh_eq, false);
+
+       snd_soc_update_bits(codec, WM8958_DSP2_PROGRAM,
+                           WM8958_DSP2_ENA, WM8958_DSP2_ENA);
+
+       /* If we've got user supplied settings use them */
+       if (pdata && pdata->num_enh_eq_cfgs) {
+               struct wm8958_enh_eq_cfg *cfg
+                       = &pdata->enh_eq_cfgs[wm8994->enh_eq_cfg];
+
+               for (i = 0; i < ARRAY_SIZE(cfg->regs); i++)
+                       snd_soc_write(codec, i + 0x2200,
+                                     cfg->regs[i]);
+       }
+
+       /* Run the DSP */
+       snd_soc_write(codec, WM8958_DSP2_EXECCONTROL,
+                     WM8958_DSP2_RUNR);
+
+       /* Switch the DSP into the data path */
+       snd_soc_update_bits(codec, WM8958_DSP2_CONFIG,
+                           WM8958_MBC_SEL_MASK | WM8958_MBC_ENA,
+                           path << WM8958_MBC_SEL_SHIFT | WM8958_MBC_ENA);
+}
 
 static void wm8958_dsp_apply(struct snd_soc_codec *codec, int path, int start)
 {
@@ -321,7 +351,8 @@ static void wm8958_dsp_apply(struct snd_soc_codec *codec, int path, int start)
 
        /* Do we have both an active AIF and an active algorithm? */
        ena = wm8994->mbc_ena[path] || wm8994->vss_ena[path] ||
-               wm8994->hpf1_ena[path] || wm8994->hpf2_ena[path];
+               wm8994->hpf1_ena[path] || wm8994->hpf2_ena[path] ||
+               wm8994->enh_eq_ena[path];
        if (!pwr_reg)
                ena = 0;
 
@@ -331,6 +362,10 @@ static void wm8958_dsp_apply(struct snd_soc_codec *codec, int path, int start)
                path, wm8994->dsp_active, start, pwr_reg, reg);
 
        if (start && ena) {
+               /* If the DSP is already running then noop */
+               if (reg & WM8958_DSP2_ENA)
+                       return;
+
                /* If either AIFnCLK is not yet enabled postpone */
                if (!(snd_soc_read(codec, WM8994_AIF1_CLOCKING_1)
                      & WM8994_AIF1CLK_ENA_MASK) &&
@@ -344,7 +379,9 @@ static void wm8958_dsp_apply(struct snd_soc_codec *codec, int path, int start)
                                    aif << WM8958_DSP2CLK_SRC_SHIFT |
                                    WM8958_DSP2CLK_ENA);
 
-               if (wm8994->vss_ena[path] || wm8994->hpf1_ena[path] ||
+               if (wm8994->enh_eq_ena[path])
+                       wm8958_dsp_start_enh_eq(codec, path);
+               else if (wm8994->vss_ena[path] || wm8994->hpf1_ena[path] ||
                    wm8994->hpf2_ena[path])
                        wm8958_dsp_start_vss(codec, path);
                else if (wm8994->mbc_ena[path])
@@ -475,6 +512,9 @@ static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
        struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
 
+       if (wm8994->mbc_ena[mbc] == ucontrol->value.integer.value[0])
+               return 0;
+
        if (ucontrol->value.integer.value[0] > 1)
                return -EINVAL;
 
@@ -483,6 +523,9 @@ static int wm8958_mbc_put(struct snd_kcontrol *kcontrol,
                return -EBUSY;
        }
 
+       if (wm8994->enh_eq_ena[mbc])
+               return -EBUSY;
+
        wm8994->mbc_ena[mbc] = ucontrol->value.integer.value[0];
 
        wm8958_dsp_apply(codec, mbc, wm8994->mbc_ena[mbc]);
@@ -592,6 +635,9 @@ static int wm8958_vss_put(struct snd_kcontrol *kcontrol,
        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
        struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
 
+       if (wm8994->vss_ena[vss] == ucontrol->value.integer.value[0])
+               return 0;
+
        if (ucontrol->value.integer.value[0] > 1)
                return -EINVAL;
 
@@ -603,6 +649,9 @@ static int wm8958_vss_put(struct snd_kcontrol *kcontrol,
                return -EBUSY;
        }
 
+       if (wm8994->enh_eq_ena[vss])
+               return -EBUSY;
+
        wm8994->vss_ena[vss] = ucontrol->value.integer.value[0];
 
        wm8958_dsp_apply(codec, vss, wm8994->vss_ena[vss]);
@@ -650,6 +699,16 @@ static int wm8958_hpf_put(struct snd_kcontrol *kcontrol,
        struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
        struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
 
+       if (hpf < 3) {
+               if (wm8994->hpf1_ena[hpf % 3] ==
+                   ucontrol->value.integer.value[0])
+                       return 0;
+       } else {
+               if (wm8994->hpf2_ena[hpf % 3] ==
+                   ucontrol->value.integer.value[0])
+                       return 0;
+       }
+
        if (ucontrol->value.integer.value[0] > 1)
                return -EINVAL;
 
@@ -661,7 +720,7 @@ static int wm8958_hpf_put(struct snd_kcontrol *kcontrol,
                return -EBUSY;
        }
 
-       if (wm8994->eq[hpf % 3])
+       if (wm8994->enh_eq_ena[hpf % 3])
                return -EBUSY;
 
        if (hpf < 3)
@@ -681,6 +740,100 @@ static int wm8958_hpf_put(struct snd_kcontrol *kcontrol,
        .get = wm8958_hpf_get, .put = wm8958_hpf_put, \
        .private_value = xval }
 
+static int wm8958_put_enh_eq_enum(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
+       struct wm8994_pdata *pdata = wm8994->pdata;
+       int value = ucontrol->value.integer.value[0];
+       int reg;
+
+       /* Don't allow on the fly reconfiguration */
+       reg = snd_soc_read(codec, WM8994_CLOCKING_1);
+       if (reg < 0 || reg & WM8958_DSP2CLK_ENA)
+               return -EBUSY;
+
+       if (value >= pdata->num_enh_eq_cfgs)
+               return -EINVAL;
+
+       wm8994->enh_eq_cfg = value;
+
+       return 0;
+}
+
+static int wm8958_get_enh_eq_enum(struct snd_kcontrol *kcontrol,
+                                 struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
+
+       ucontrol->value.enumerated.item[0] = wm8994->enh_eq_cfg;
+
+       return 0;
+}
+
+static int wm8958_enh_eq_info(struct snd_kcontrol *kcontrol,
+                          struct snd_ctl_elem_info *uinfo)
+{
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
+       uinfo->count = 1;
+       uinfo->value.integer.min = 0;
+       uinfo->value.integer.max = 1;
+       return 0;
+}
+
+static int wm8958_enh_eq_get(struct snd_kcontrol *kcontrol,
+                         struct snd_ctl_elem_value *ucontrol)
+{
+       int eq = kcontrol->private_value;
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
+
+       ucontrol->value.integer.value[0] = wm8994->enh_eq_ena[eq];
+
+       return 0;
+}
+
+static int wm8958_enh_eq_put(struct snd_kcontrol *kcontrol,
+                         struct snd_ctl_elem_value *ucontrol)
+{
+       int eq = kcontrol->private_value;
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
+
+       if (wm8994->enh_eq_ena[eq] == ucontrol->value.integer.value[0])
+               return 0;
+
+       if (ucontrol->value.integer.value[0] > 1)
+               return -EINVAL;
+
+       if (!wm8994->enh_eq)
+               return -ENODEV;
+
+       if (wm8958_dsp2_busy(wm8994, eq)) {
+               dev_dbg(codec->dev, "DSP2 active on %d already\n", eq);
+               return -EBUSY;
+       }
+
+       if (wm8994->mbc_ena[eq] || wm8994->vss_ena[eq] ||
+           wm8994->hpf1_ena[eq] || wm8994->hpf2_ena[eq])
+               return -EBUSY;
+
+       wm8994->enh_eq_ena[eq] = ucontrol->value.integer.value[0];
+
+       wm8958_dsp_apply(codec, eq, ucontrol->value.integer.value[0]);
+
+       return 0;
+}
+
+#define WM8958_ENH_EQ_SWITCH(xname, xval) {\
+       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname), \
+       .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,\
+       .info = wm8958_enh_eq_info, \
+       .get = wm8958_enh_eq_get, .put = wm8958_enh_eq_put, \
+       .private_value = xval }
+
 static const struct snd_kcontrol_new wm8958_mbc_snd_controls[] = {
 WM8958_MBC_SWITCH("AIF1DAC1 MBC Switch", 0),
 WM8958_MBC_SWITCH("AIF1DAC2 MBC Switch", 1),
@@ -699,6 +852,24 @@ WM8958_HPF_SWITCH("AIF1DAC2 HPF2 Switch", 4),
 WM8958_HPF_SWITCH("AIF2DAC HPF2 Switch", 5),
 };
 
+static const struct snd_kcontrol_new wm8958_enh_eq_snd_controls[] = {
+WM8958_ENH_EQ_SWITCH("AIF1DAC1 Enhanced EQ Switch", 0),
+WM8958_ENH_EQ_SWITCH("AIF1DAC2 Enhanced EQ Switch", 1),
+WM8958_ENH_EQ_SWITCH("AIF2DAC Enhanced EQ Switch", 2),
+};
+
+static void wm8958_enh_eq_loaded(const struct firmware *fw, void *context)
+{
+       struct snd_soc_codec *codec = context;
+       struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
+
+       if (fw && (wm8958_dsp2_fw(codec, "ENH_EQ", fw, true) == 0)) {
+               mutex_lock(&codec->mutex);
+               wm8994->enh_eq = fw;
+               mutex_unlock(&codec->mutex);
+       }
+}
+
 static void wm8958_mbc_vss_loaded(const struct firmware *fw, void *context)
 {
        struct snd_soc_codec *codec = context;
@@ -710,6 +881,12 @@ static void wm8958_mbc_vss_loaded(const struct firmware *fw, void *context)
                mutex_unlock(&codec->mutex);
        }
 
+       /* We can't have more than one request outstanding at once so
+        * we daisy chain.
+        */
+       request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
+                               "wm8958_enh_eq.wfw", codec->dev, GFP_KERNEL,
+                               codec, wm8958_enh_eq_loaded);
 }
 
 static void wm8958_mbc_loaded(const struct firmware *fw, void *context)
@@ -744,6 +921,8 @@ void wm8958_dsp2_init(struct snd_soc_codec *codec)
                             ARRAY_SIZE(wm8958_mbc_snd_controls));
        snd_soc_add_controls(codec, wm8958_vss_snd_controls,
                             ARRAY_SIZE(wm8958_vss_snd_controls));
+       snd_soc_add_controls(codec, wm8958_enh_eq_snd_controls,
+                            ARRAY_SIZE(wm8958_enh_eq_snd_controls));
 
 
        /* We don't *require* firmware and don't want to delay boot */
@@ -839,4 +1018,34 @@ void wm8958_dsp2_init(struct snd_soc_codec *codec)
                                "Failed to add VSS HPFmode controls: %d\n",
                                ret);
        }
+
+       if (pdata->num_enh_eq_cfgs) {
+               struct snd_kcontrol_new control[] = {
+                       SOC_ENUM_EXT("Enhanced EQ Mode", wm8994->enh_eq_enum,
+                                    wm8958_get_enh_eq_enum,
+                                    wm8958_put_enh_eq_enum),
+               };
+
+               /* We need an array of texts for the enum API */
+               wm8994->enh_eq_texts = kmalloc(sizeof(char *)
+                                               * pdata->num_enh_eq_cfgs, GFP_KERNEL);
+               if (!wm8994->enh_eq_texts) {
+                       dev_err(wm8994->codec->dev,
+                               "Failed to allocate %d enhanced EQ config texts\n",
+                               pdata->num_enh_eq_cfgs);
+                       return;
+               }
+
+               for (i = 0; i < pdata->num_enh_eq_cfgs; i++)
+                       wm8994->enh_eq_texts[i] = pdata->enh_eq_cfgs[i].name;
+
+               wm8994->enh_eq_enum.max = pdata->num_enh_eq_cfgs;
+               wm8994->enh_eq_enum.texts = wm8994->enh_eq_texts;
+
+               ret = snd_soc_add_controls(wm8994->codec, control, 1);
+               if (ret != 0)
+                       dev_err(wm8994->codec->dev,
+                               "Failed to add enhanced EQ controls: %d\n",
+                               ret);
+       }
 }