ASoC: Automatically manage WM8731 deemphasis
authorMark Brown <broonie@opensource.wolfsonmicro.com>
Thu, 2 Dec 2010 11:44:00 +0000 (11:44 +0000)
committerMark Brown <broonie@opensource.wolfsonmicro.com>
Thu, 2 Dec 2010 12:00:20 +0000 (12:00 +0000)
The deemphasis filter should be selected based on sample rate for
optimal performance.

Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Acked-by: Liam Girdwood <lrg@slimlogic.co.uk>
sound/soc/codecs/wm8731.c

index 8003761a8e02343649b22710a665e7f0a0b159e1..71122dc36826060996a00e268df41b206f23b8a7 100644 (file)
@@ -46,6 +46,8 @@ struct wm8731_priv {
        u16 reg_cache[WM8731_CACHEREGNUM];
        unsigned int sysclk;
        int sysclk_type;
+       int playback_fs;
+       bool deemph;
 };
 
 
@@ -65,14 +67,73 @@ static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
 
 static const char *wm8731_input_select[] = {"Line In", "Mic"};
 
-static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
-
 static const struct soc_enum wm8731_insel_enum =
        SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select);
 
-static const struct soc_enum wm8731_deemph_enum =
-       SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph);
+static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
+
+static int wm8731_set_deemph(struct snd_soc_codec *codec)
+{
+       struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
+       int val, i, best;
+
+       /* If we're using deemphasis select the nearest available sample
+        * rate.
+        */
+       if (wm8731->deemph) {
+               best = 1;
+               for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
+                       if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
+                           abs(wm8731_deemph[best] - wm8731->playback_fs))
+                               best = i;
+               }
+
+               val = best << 1;
+       } else {
+               best = 0;
+               val = 0;
+       }
+
+       dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
+               best, wm8731_deemph[best]);
 
+       return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val);
+}
+
+static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
+
+       ucontrol->value.enumerated.item[0] = wm8731->deemph;
+
+       return 0;
+}
+
+static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
+       int deemph = ucontrol->value.enumerated.item[0];
+       int ret = 0;
+
+       if (deemph > 1)
+               return -EINVAL;
+
+       mutex_lock(&codec->mutex);
+       if (wm8731->deemph != deemph) {
+               wm8731->deemph = deemph;
+
+               wm8731_set_deemph(codec);
+
+               ret = 1;
+       }
+       mutex_unlock(&codec->mutex);
+
+       return ret;
+}
 
 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
@@ -99,7 +160,8 @@ SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
 
-SOC_ENUM("Playback De-emphasis", wm8731_deemph_enum),
+SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
+                   wm8731_get_deemph, wm8731_put_deemph),
 };
 
 /* Output Mixer */
@@ -243,6 +305,8 @@ static int wm8731_hw_params(struct snd_pcm_substream *substream,
        u16 srate = (coeff_div[i].sr << 2) |
                (coeff_div[i].bosr << 1) | coeff_div[i].usb;
 
+       wm8731->playback_fs = params_rate(params);
+
        snd_soc_write(codec, WM8731_SRATE, srate);
 
        /* bit size */
@@ -257,6 +321,8 @@ static int wm8731_hw_params(struct snd_pcm_substream *substream,
                break;
        }
 
+       wm8731_set_deemph(codec);
+
        snd_soc_write(codec, WM8731_IFACE, iface);
        return 0;
 }