Merge branch 'topic/asoc' into for-linus
[safe/jmp/linux-2.6] / sound / soc / codecs / wm8940.c
index 26987dc..e3c4bbf 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/i2c.h>
 #include <linux/platform_device.h>
 #include <linux/spi/spi.h>
+#include <linux/slab.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
@@ -106,50 +107,6 @@ static u16 wm8940_reg_defaults[] = {
        0x0000, /* Mono Mixer Control */
 };
 
-static inline unsigned int wm8940_read_reg_cache(struct snd_soc_codec *codec,
-                                                unsigned int reg)
-{
-       u16 *cache = codec->reg_cache;
-
-       if (reg >= ARRAY_SIZE(wm8940_reg_defaults))
-               return -1;
-
-       return cache[reg];
-}
-
-static inline int wm8940_write_reg_cache(struct snd_soc_codec *codec,
-                                         u16 reg, unsigned int value)
-{
-       u16 *cache = codec->reg_cache;
-
-       if (reg >= ARRAY_SIZE(wm8940_reg_defaults))
-               return -1;
-
-       cache[reg] = value;
-
-       return 0;
-}
-
-static int wm8940_write(struct snd_soc_codec *codec, unsigned int reg,
-                       unsigned int value)
-{
-       int ret;
-       u8 data[3] = { reg,
-                      (value & 0xff00) >> 8,
-                      (value & 0x00ff)
-       };
-
-       wm8940_write_reg_cache(codec, reg, value);
-
-       ret = codec->hw_write(codec->control_data, data, 3);
-
-       if (ret < 0)
-               return ret;
-       else if (ret != 3)
-               return -EIO;
-       return 0;
-}
-
 static const char *wm8940_companding[] = { "Off", "NC", "u-law", "A-law" };
 static const struct soc_enum wm8940_adc_companding_enum
 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 1, 4, wm8940_companding);
@@ -168,16 +125,16 @@ static const char *wm8940_filter_mode_text[] = {"Audio", "Application"};
 static const struct soc_enum wm8940_filter_mode_enum
 = SOC_ENUM_SINGLE(WM8940_ADC, 7, 2, wm8940_filter_mode_text);
 
-DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv, -5700, 100, 1);
-DECLARE_TLV_DB_SCALE(wm8940_att_tlv, -1000, 1000, 0);
-DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv, -1200, 75, 0);
-DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv, -1200, 600, 0);
-DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv, 675, 600, 0);
-DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv, -2250, 50, 0);
-DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv, 0, 100, 0);
-DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv, -600, 100, 0);
-DECLARE_TLV_DB_SCALE(wm8940_adc_tlv, -12750, 50, 1);
-DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv, 0, 2000, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv, -5700, 100, 1);
+static DECLARE_TLV_DB_SCALE(wm8940_att_tlv, -1000, 1000, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv, -1200, 75, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv, -1200, 600, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv, 675, 600, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv, -2250, 50, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv, 0, 100, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv, -600, 100, 0);
+static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv, -12750, 50, 1);
+static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv, 0, 2000, 0);
 
 static const struct snd_kcontrol_new wm8940_snd_controls[] = {
        SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL,
@@ -253,7 +210,7 @@ static const struct snd_kcontrol_new wm8940_mono_mixer_controls[] = {
        SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX, 0, 1, 0),
 };
 
-DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv, -1500, 300, 1);
+static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv, -1500, 300, 1);
 static const struct snd_kcontrol_new wm8940_input_boost_controls[] = {
        SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN, 6, 1, 1),
        SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST,
@@ -342,20 +299,19 @@ static int wm8940_add_widgets(struct snd_soc_codec *codec)
        ret = snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
        if (ret)
                goto error_ret;
-       ret = snd_soc_dapm_new_widgets(codec);
 
 error_ret:
        return ret;
 }
 
-#define wm8940_reset(c) wm8940_write(c, WM8940_SOFTRESET, 0);
+#define wm8940_reset(c) snd_soc_write(c, WM8940_SOFTRESET, 0);
 
 static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai,
                              unsigned int fmt)
 {
        struct snd_soc_codec *codec = codec_dai->codec;
-       u16 iface = wm8940_read_reg_cache(codec, WM8940_IFACE) & 0xFE67;
-       u16 clk = wm8940_read_reg_cache(codec, WM8940_CLOCK) & 0x1fe;
+       u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFE67;
+       u16 clk = snd_soc_read(codec, WM8940_CLOCK) & 0x1fe;
 
        switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
        case SND_SOC_DAIFMT_CBM_CFM:
@@ -366,7 +322,7 @@ static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai,
        default:
                return -EINVAL;
        }
-       wm8940_write(codec, WM8940_CLOCK, clk);
+       snd_soc_write(codec, WM8940_CLOCK, clk);
 
        switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
        case SND_SOC_DAIFMT_I2S:
@@ -399,7 +355,7 @@ static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai,
                break;
        }
 
-       wm8940_write(codec, WM8940_IFACE, iface);
+       snd_soc_write(codec, WM8940_IFACE, iface);
 
        return 0;
 }
@@ -411,9 +367,9 @@ static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream,
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
        struct snd_soc_device *socdev = rtd->socdev;
        struct snd_soc_codec *codec = socdev->card->codec;
-       u16 iface = wm8940_read_reg_cache(codec, WM8940_IFACE) & 0xFD9F;
-       u16 addcntrl = wm8940_read_reg_cache(codec, WM8940_ADDCNTRL) & 0xFFF1;
-       u16 companding =  wm8940_read_reg_cache(codec,
+       u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFD9F;
+       u16 addcntrl = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFF1;
+       u16 companding =  snd_soc_read(codec,
                                                WM8940_COMPANDINGCTL) & 0xFFDF;
        int ret;
 
@@ -423,26 +379,26 @@ static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream,
                iface |= (1 << 9);
 
        switch (params_rate(params)) {
-       case SNDRV_PCM_RATE_8000:
+       case 8000:
                addcntrl |= (0x5 << 1);
                break;
-       case SNDRV_PCM_RATE_11025:
+       case 11025:
                addcntrl |= (0x4 << 1);
                break;
-       case SNDRV_PCM_RATE_16000:
+       case 16000:
                addcntrl |= (0x3 << 1);
                break;
-       case SNDRV_PCM_RATE_22050:
+       case 22050:
                addcntrl |= (0x2 << 1);
                break;
-       case SNDRV_PCM_RATE_32000:
+       case 32000:
                addcntrl |= (0x1 << 1);
                break;
-       case SNDRV_PCM_RATE_44100:
-       case SNDRV_PCM_RATE_48000:
+       case 44100:
+       case 48000:
                break;
        }
-       ret = wm8940_write(codec, WM8940_ADDCNTRL, addcntrl);
+       ret = snd_soc_write(codec, WM8940_ADDCNTRL, addcntrl);
        if (ret)
                goto error_ret;
 
@@ -462,10 +418,10 @@ static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream,
                iface |= (3 << 5);
                break;
        }
-       ret = wm8940_write(codec, WM8940_COMPANDINGCTL, companding);
+       ret = snd_soc_write(codec, WM8940_COMPANDINGCTL, companding);
        if (ret)
                goto error_ret;
-       ret = wm8940_write(codec, WM8940_IFACE, iface);
+       ret = snd_soc_write(codec, WM8940_IFACE, iface);
 
 error_ret:
        return ret;
@@ -474,19 +430,19 @@ error_ret:
 static int wm8940_mute(struct snd_soc_dai *dai, int mute)
 {
        struct snd_soc_codec *codec = dai->codec;
-       u16 mute_reg = wm8940_read_reg_cache(codec, WM8940_DAC) & 0xffbf;
+       u16 mute_reg = snd_soc_read(codec, WM8940_DAC) & 0xffbf;
 
        if (mute)
                mute_reg |= 0x40;
 
-       return wm8940_write(codec, WM8940_DAC, mute_reg);
+       return snd_soc_write(codec, WM8940_DAC, mute_reg);
 }
 
 static int wm8940_set_bias_level(struct snd_soc_codec *codec,
                                 enum snd_soc_bias_level level)
 {
        u16 val;
-       u16 pwr_reg = wm8940_read_reg_cache(codec, WM8940_POWER1) & 0x1F0;
+       u16 pwr_reg = snd_soc_read(codec, WM8940_POWER1) & 0x1F0;
        int ret = 0;
 
        switch (level) {
@@ -494,26 +450,26 @@ static int wm8940_set_bias_level(struct snd_soc_codec *codec,
                /* ensure bufioen and biasen */
                pwr_reg |= (1 << 2) | (1 << 3);
                /* Enable thermal shutdown */
-               val = wm8940_read_reg_cache(codec, WM8940_OUTPUTCTL);
-               ret = wm8940_write(codec, WM8940_OUTPUTCTL, val | 0x2);
+               val = snd_soc_read(codec, WM8940_OUTPUTCTL);
+               ret = snd_soc_write(codec, WM8940_OUTPUTCTL, val | 0x2);
                if (ret)
                        break;
                /* set vmid to 75k */
-               ret = wm8940_write(codec, WM8940_POWER1, pwr_reg | 0x1);
+               ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1);
                break;
        case SND_SOC_BIAS_PREPARE:
                /* ensure bufioen and biasen */
                pwr_reg |= (1 << 2) | (1 << 3);
-               ret = wm8940_write(codec, WM8940_POWER1, pwr_reg | 0x1);
+               ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1);
                break;
        case SND_SOC_BIAS_STANDBY:
                /* ensure bufioen and biasen */
                pwr_reg |= (1 << 2) | (1 << 3);
                /* set vmid to 300k for standby */
-               ret = wm8940_write(codec, WM8940_POWER1, pwr_reg | 0x2);
+               ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x2);
                break;
        case SND_SOC_BIAS_OFF:
-               ret = wm8940_write(codec, WM8940_POWER1, pwr_reg);
+               ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg);
                break;
        }
 
@@ -580,43 +536,43 @@ static void pll_factors(unsigned int target, unsigned int source)
 }
 
 /* Untested at the moment */
-static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai,
-               int pll_id, unsigned int freq_in, unsigned int freq_out)
+static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
+               int source, unsigned int freq_in, unsigned int freq_out)
 {
        struct snd_soc_codec *codec = codec_dai->codec;
        u16 reg;
 
        /* Turn off PLL */
-       reg = wm8940_read_reg_cache(codec, WM8940_POWER1);
-       wm8940_write(codec, WM8940_POWER1, reg & 0x1df);
+       reg = snd_soc_read(codec, WM8940_POWER1);
+       snd_soc_write(codec, WM8940_POWER1, reg & 0x1df);
 
        if (freq_in == 0 || freq_out == 0) {
                /* Clock CODEC directly from MCLK */
-               reg = wm8940_read_reg_cache(codec, WM8940_CLOCK);
-               wm8940_write(codec, WM8940_CLOCK, reg & 0x0ff);
+               reg = snd_soc_read(codec, WM8940_CLOCK);
+               snd_soc_write(codec, WM8940_CLOCK, reg & 0x0ff);
                /* Pll power down */
-               wm8940_write(codec, WM8940_PLLN, (1 << 7));
+               snd_soc_write(codec, WM8940_PLLN, (1 << 7));
                return 0;
        }
 
        /* Pll is followed by a frequency divide by 4 */
        pll_factors(freq_out*4, freq_in);
        if (pll_div.k)
-               wm8940_write(codec, WM8940_PLLN,
+               snd_soc_write(codec, WM8940_PLLN,
                             (pll_div.pre_scale << 4) | pll_div.n | (1 << 6));
        else /* No factional component */
-               wm8940_write(codec, WM8940_PLLN,
+               snd_soc_write(codec, WM8940_PLLN,
                             (pll_div.pre_scale << 4) | pll_div.n);
-       wm8940_write(codec, WM8940_PLLK1, pll_div.k >> 18);
-       wm8940_write(codec, WM8940_PLLK2, (pll_div.k >> 9) & 0x1ff);
-       wm8940_write(codec, WM8940_PLLK3, pll_div.k & 0x1ff);
+       snd_soc_write(codec, WM8940_PLLK1, pll_div.k >> 18);
+       snd_soc_write(codec, WM8940_PLLK2, (pll_div.k >> 9) & 0x1ff);
+       snd_soc_write(codec, WM8940_PLLK3, pll_div.k & 0x1ff);
        /* Enable the PLL */
-       reg = wm8940_read_reg_cache(codec, WM8940_POWER1);
-       wm8940_write(codec, WM8940_POWER1, reg | 0x020);
+       reg = snd_soc_read(codec, WM8940_POWER1);
+       snd_soc_write(codec, WM8940_POWER1, reg | 0x020);
 
        /* Run CODEC from PLL instead of MCLK */
-       reg = wm8940_read_reg_cache(codec, WM8940_CLOCK);
-       wm8940_write(codec, WM8940_CLOCK, reg | 0x100);
+       reg = snd_soc_read(codec, WM8940_CLOCK);
+       snd_soc_write(codec, WM8940_CLOCK, reg | 0x100);
 
        return 0;
 }
@@ -625,7 +581,7 @@ static int wm8940_set_dai_sysclk(struct snd_soc_dai *codec_dai,
                                 int clk_id, unsigned int freq, int dir)
 {
        struct snd_soc_codec *codec = codec_dai->codec;
-       struct wm8940_priv *wm8940 = codec->private_data;
+       struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec);
 
        switch (freq) {
        case 11289600:
@@ -648,16 +604,16 @@ static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
 
        switch (div_id) {
        case WM8940_BCLKDIV:
-               reg = wm8940_read_reg_cache(codec, WM8940_CLOCK) & 0xFFEF3;
-               ret = wm8940_write(codec, WM8940_CLOCK, reg | (div << 2));
+               reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFFEF3;
+               ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 2));
                break;
        case WM8940_MCLKDIV:
-               reg = wm8940_read_reg_cache(codec, WM8940_CLOCK) & 0xFF1F;
-               ret = wm8940_write(codec, WM8940_CLOCK, reg | (div << 5));
+               reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFF1F;
+               ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 5));
                break;
        case WM8940_OPCLKDIV:
-               reg = wm8940_read_reg_cache(codec, WM8940_ADDCNTRL) & 0xFFCF;
-               ret = wm8940_write(codec, WM8940_ADDCNTRL, reg | (div << 4));
+               reg = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFCF;
+               ret = snd_soc_write(codec, WM8940_ADDCNTRL, reg | (div << 4));
                break;
        }
        return ret;
@@ -736,7 +692,6 @@ static int wm8940_resume(struct platform_device *pdev)
        ret = wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
        if (ret)
                goto error_ret;
-       ret = wm8940_set_bias_level(codec, codec->suspend_bias_level);
 
 error_ret:
        return ret;
@@ -775,12 +730,6 @@ static int wm8940_probe(struct platform_device *pdev)
        if (ret)
                goto error_free_pcms;
 
-       ret = snd_soc_init_card(socdev);
-       if (ret < 0) {
-               dev_err(codec->dev, "failed to register card: %d\n", ret);
-               goto error_free_pcms;
-       }
-
        return ret;
 
 error_free_pcms:
@@ -808,7 +757,8 @@ struct snd_soc_codec_device soc_codec_dev_wm8940 = {
 };
 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8940);
 
-static int wm8940_register(struct wm8940_priv *wm8940)
+static int wm8940_register(struct wm8940_priv *wm8940,
+                          enum snd_soc_control_type control)
 {
        struct wm8940_setup_data *pdata = wm8940->codec.dev->platform_data;
        struct snd_soc_codec *codec = &wm8940->codec;
@@ -822,11 +772,9 @@ static int wm8940_register(struct wm8940_priv *wm8940)
        INIT_LIST_HEAD(&codec->dapm_widgets);
        INIT_LIST_HEAD(&codec->dapm_paths);
 
-       codec->private_data = wm8940;
+       snd_soc_codec_set_drvdata(codec, wm8940);
        codec->name = "WM8940";
        codec->owner = THIS_MODULE;
-       codec->read = wm8940_read_reg_cache;
-       codec->write = wm8940_write;
        codec->bias_level = SND_SOC_BIAS_OFF;
        codec->set_bias_level = wm8940_set_bias_level;
        codec->dai = &wm8940_dai;
@@ -834,6 +782,12 @@ static int wm8940_register(struct wm8940_priv *wm8940)
        codec->reg_cache_size = ARRAY_SIZE(wm8940_reg_defaults);
        codec->reg_cache = &wm8940->reg_cache;
 
+       ret = snd_soc_codec_set_cache_io(codec, 8, 16, control);
+       if (ret < 0) {
+               dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
+               return ret;
+       }
+
        memcpy(codec->reg_cache, wm8940_reg_defaults,
               sizeof(wm8940_reg_defaults));
 
@@ -847,15 +801,15 @@ static int wm8940_register(struct wm8940_priv *wm8940)
 
        wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 
-       ret = wm8940_write(codec, WM8940_POWER1, 0x180);
+       ret = snd_soc_write(codec, WM8940_POWER1, 0x180);
        if (ret < 0)
                return ret;
 
        if (!pdata)
                dev_warn(codec->dev, "No platform data supplied\n");
        else {
-               reg = wm8940_read_reg_cache(codec, WM8940_OUTPUTCTL);
-               ret = wm8940_write(codec, WM8940_OUTPUTCTL, reg | pdata->vroi);
+               reg = snd_soc_read(codec, WM8940_OUTPUTCTL);
+               ret = snd_soc_write(codec, WM8940_OUTPUTCTL, reg | pdata->vroi);
                if (ret < 0)
                        return ret;
        }
@@ -904,10 +858,10 @@ static int wm8940_i2c_probe(struct i2c_client *i2c,
        codec->control_data = i2c;
        codec->dev = &i2c->dev;
 
-       return wm8940_register(wm8940);
+       return wm8940_register(wm8940, SND_SOC_I2C);
 }
 
-static int wm8940_i2c_remove(struct i2c_client *client)
+static int __devexit wm8940_i2c_remove(struct i2c_client *client)
 {
        struct wm8940_priv *wm8940 = i2c_get_clientdata(client);