Merge branch 'for-davem' of ssh://master.kernel.org/pub/scm/linux/kernel/git/linville...
[linux-2.6.git] / sound / soc / codecs / wm8903.c
index 73dfd5b..017d99c 100644 (file)
  *
  * TODO:
  *  - TDM mode configuration.
- *  - Mic detect.
  *  - Digital microphone support.
- *  - Interrupt support (mic detect and sequencer).
  */
 
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/init.h>
+#include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/pm.h>
 #include <linux/i2c.h>
 #include <linux/platform_device.h>
+#include <linux/slab.h>
 #include <sound/core.h>
+#include <sound/jack.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/tlv.h>
 #include <sound/soc.h>
-#include <sound/soc-dapm.h>
 #include <sound/initval.h>
+#include <sound/wm8903.h>
+#include <trace/events/asoc.h>
 
 #include "wm8903.h"
 
-struct wm8903_priv {
-       int sysclk;
-
-       /* Reference counts */
-       int charge_pump_users;
-       int class_w_users;
-       int playback_active;
-       int capture_active;
-
-       struct snd_pcm_substream *master_substream;
-       struct snd_pcm_substream *slave_substream;
-};
-
 /* Register defaults at reset */
 static u16 wm8903_reg_defaults[] = {
        0x8903,     /* R0   - SW Reset and ID */
@@ -223,127 +212,71 @@ static u16 wm8903_reg_defaults[] = {
        0x0000,     /* R172 - Analogue Output Bias 0 */
 };
 
-static unsigned int wm8903_read_reg_cache(struct snd_soc_codec *codec,
-                                                unsigned int reg)
-{
-       u16 *cache = codec->reg_cache;
+struct wm8903_priv {
 
-       BUG_ON(reg >= ARRAY_SIZE(wm8903_reg_defaults));
+       int sysclk;
+       int irq;
 
-       return cache[reg];
-}
+       int fs;
+       int deemph;
 
-static unsigned int wm8903_hw_read(struct snd_soc_codec *codec, u8 reg)
-{
-       struct i2c_msg xfer[2];
-       u16 data;
-       int ret;
-       struct i2c_client *client = codec->control_data;
-
-       /* Write register */
-       xfer[0].addr = client->addr;
-       xfer[0].flags = 0;
-       xfer[0].len = 1;
-       xfer[0].buf = &reg;
-
-       /* Read data */
-       xfer[1].addr = client->addr;
-       xfer[1].flags = I2C_M_RD;
-       xfer[1].len = 2;
-       xfer[1].buf = (u8 *)&data;
-
-       ret = i2c_transfer(client->adapter, xfer, 2);
-       if (ret != 2) {
-               pr_err("i2c_transfer returned %d\n", ret);
-               return 0;
-       }
+       /* Reference count */
+       int class_w_users;
 
-       return (data >> 8) | ((data & 0xff) << 8);
-}
+       struct completion wseq;
+
+       struct snd_soc_jack *mic_jack;
+       int mic_det;
+       int mic_short;
+       int mic_last_report;
+       int mic_delay;
+};
 
-static unsigned int wm8903_read(struct snd_soc_codec *codec,
-                               unsigned int reg)
+static int wm8903_volatile_register(unsigned int reg)
 {
        switch (reg) {
        case WM8903_SW_RESET_AND_ID:
        case WM8903_REVISION_NUMBER:
        case WM8903_INTERRUPT_STATUS_1:
        case WM8903_WRITE_SEQUENCER_4:
-               return wm8903_hw_read(codec, reg);
-
-       default:
-               return wm8903_read_reg_cache(codec, reg);
-       }
-}
-
-static void wm8903_write_reg_cache(struct snd_soc_codec *codec,
-                                  u16 reg, unsigned int value)
-{
-       u16 *cache = codec->reg_cache;
-
-       BUG_ON(reg >= ARRAY_SIZE(wm8903_reg_defaults));
-
-       switch (reg) {
-       case WM8903_SW_RESET_AND_ID:
-       case WM8903_REVISION_NUMBER:
-               break;
+               return 1;
 
        default:
-               cache[reg] = value;
-               break;
-       }
-}
-
-static int wm8903_write(struct snd_soc_codec *codec, unsigned int reg,
-                       unsigned int value)
-{
-       u8 data[3];
-
-       wm8903_write_reg_cache(codec, reg, value);
-
-       /* Data format is 1 byte of address followed by 2 bytes of data */
-       data[0] = reg;
-       data[1] = (value >> 8) & 0xff;
-       data[2] = value & 0xff;
-
-       if (codec->hw_write(codec->control_data, data, 3) == 2)
                return 0;
-       else
-               return -EIO;
+       }
 }
 
 static int wm8903_run_sequence(struct snd_soc_codec *codec, unsigned int start)
 {
        u16 reg[5];
-       struct i2c_client *i2c = codec->control_data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
 
        BUG_ON(start > 48);
 
-       /* Enable the sequencer */
-       reg[0] = wm8903_read(codec, WM8903_WRITE_SEQUENCER_0);
-       reg[0] |= WM8903_WSEQ_ENA;
-       wm8903_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
+       /* Enable the sequencer if it's not already on */
+       reg[0] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_0);
+       snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0,
+                     reg[0] | WM8903_WSEQ_ENA);
 
-       dev_dbg(&i2c->dev, "Starting sequence at %d\n", start);
+       dev_dbg(codec->dev, "Starting sequence at %d\n", start);
 
-       wm8903_write(codec, WM8903_WRITE_SEQUENCER_3,
+       snd_soc_write(codec, WM8903_WRITE_SEQUENCER_3,
                     start | WM8903_WSEQ_START);
 
        /* Wait for it to complete.  If we have the interrupt wired up then
-        * we could block waiting for an interrupt, though polling may still
-        * be desirable for diagnostic purposes.
+        * that will break us out of the poll early.
         */
        do {
-               msleep(10);
+               wait_for_completion_timeout(&wm8903->wseq,
+                                           msecs_to_jiffies(10));
 
-               reg[4] = wm8903_read(codec, WM8903_WRITE_SEQUENCER_4);
+               reg[4] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_4);
        } while (reg[4] & WM8903_WSEQ_BUSY);
 
-       dev_dbg(&i2c->dev, "Sequence complete\n");
+       dev_dbg(codec->dev, "Sequence complete\n");
 
-       /* Disable the sequencer again */
-       wm8903_write(codec, WM8903_WRITE_SEQUENCER_0,
-                    reg[0] & ~WM8903_WSEQ_ENA);
+       /* Disable the sequencer again if we enabled it */
+       snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
 
        return 0;
 }
@@ -354,12 +287,14 @@ static void wm8903_sync_reg_cache(struct snd_soc_codec *codec, u16 *cache)
 
        /* There really ought to be something better we can do here :/ */
        for (i = 0; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
-               cache[i] = wm8903_hw_read(codec, i);
+               cache[i] = codec->hw_read(codec, i);
 }
 
 static void wm8903_reset(struct snd_soc_codec *codec)
 {
-       wm8903_write(codec, WM8903_SW_RESET_AND_ID, 0);
+       snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
+       memcpy(codec->reg_cache, wm8903_reg_defaults,
+              sizeof(wm8903_reg_defaults));
 }
 
 #define WM8903_OUTPUT_SHORT 0x8
@@ -367,6 +302,15 @@ static void wm8903_reset(struct snd_soc_codec *codec)
 #define WM8903_OUTPUT_INT   0x2
 #define WM8903_OUTPUT_IN    0x1
 
+static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
+                          struct snd_kcontrol *kcontrol, int event)
+{
+       WARN_ON(event != SND_SOC_DAPM_POST_PMU);
+       mdelay(4);
+
+       return 0;
+}
+
 /*
  * Event for headphone and line out amplifier power changes.  Special
  * power up/down sequences are required in order to maximise pop/click
@@ -376,22 +320,24 @@ static int wm8903_output_event(struct snd_soc_dapm_widget *w,
                               struct snd_kcontrol *kcontrol, int event)
 {
        struct snd_soc_codec *codec = w->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
-       struct i2c_client *i2c = codec->control_data;
        u16 val;
        u16 reg;
+       u16 dcs_reg;
+       u16 dcs_bit;
        int shift;
-       u16 cp_reg = wm8903_read(codec, WM8903_CHARGE_PUMP_0);
 
        switch (w->reg) {
        case WM8903_POWER_MANAGEMENT_2:
                reg = WM8903_ANALOGUE_HP_0;
+               dcs_bit = 0 + w->shift;
                break;
        case WM8903_POWER_MANAGEMENT_3:
                reg = WM8903_ANALOGUE_LINEOUT_0;
+               dcs_bit = 2 + w->shift;
                break;
        default:
                BUG();
+               return -EINVAL;  /* Spurious warning from some compilers */
        }
 
        switch (w->shift) {
@@ -403,70 +349,56 @@ static int wm8903_output_event(struct snd_soc_dapm_widget *w,
                break;
        default:
                BUG();
+               return -EINVAL;  /* Spurious warning from some compilers */
        }
 
        if (event & SND_SOC_DAPM_PRE_PMU) {
-               val = wm8903_read(codec, reg);
+               val = snd_soc_read(codec, reg);
 
                /* Short the output */
                val &= ~(WM8903_OUTPUT_SHORT << shift);
-               wm8903_write(codec, reg, val);
-
-               wm8903->charge_pump_users++;
-
-               dev_dbg(&i2c->dev, "Charge pump use count now %d\n",
-                       wm8903->charge_pump_users);
-
-               if (wm8903->charge_pump_users == 1) {
-                       dev_dbg(&i2c->dev, "Enabling charge pump\n");
-                       wm8903_write(codec, WM8903_CHARGE_PUMP_0,
-                                    cp_reg | WM8903_CP_ENA);
-                       mdelay(4);
-               }
+               snd_soc_write(codec, reg, val);
        }
 
        if (event & SND_SOC_DAPM_POST_PMU) {
-               val = wm8903_read(codec, reg);
+               val = snd_soc_read(codec, reg);
 
                val |= (WM8903_OUTPUT_IN << shift);
-               wm8903_write(codec, reg, val);
+               snd_soc_write(codec, reg, val);
 
                val |= (WM8903_OUTPUT_INT << shift);
-               wm8903_write(codec, reg, val);
+               snd_soc_write(codec, reg, val);
 
                /* Turn on the output ENA_OUTP */
                val |= (WM8903_OUTPUT_OUT << shift);
-               wm8903_write(codec, reg, val);
+               snd_soc_write(codec, reg, val);
+
+               /* Enable the DC servo */
+               dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
+               dcs_reg |= dcs_bit;
+               snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
 
                /* Remove the short */
                val |= (WM8903_OUTPUT_SHORT << shift);
-               wm8903_write(codec, reg, val);
+               snd_soc_write(codec, reg, val);
        }
 
        if (event & SND_SOC_DAPM_PRE_PMD) {
-               val = wm8903_read(codec, reg);
+               val = snd_soc_read(codec, reg);
 
                /* Short the output */
                val &= ~(WM8903_OUTPUT_SHORT << shift);
-               wm8903_write(codec, reg, val);
+               snd_soc_write(codec, reg, val);
+
+               /* Disable the DC servo */
+               dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
+               dcs_reg &= ~dcs_bit;
+               snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
 
                /* Then disable the intermediate and output stages */
                val &= ~((WM8903_OUTPUT_OUT | WM8903_OUTPUT_INT |
                          WM8903_OUTPUT_IN) << shift);
-               wm8903_write(codec, reg, val);
-       }
-
-       if (event & SND_SOC_DAPM_POST_PMD) {
-               wm8903->charge_pump_users--;
-
-               dev_dbg(&i2c->dev, "Charge pump use count now %d\n",
-                       wm8903->charge_pump_users);
-
-               if (wm8903->charge_pump_users == 0) {
-                       dev_dbg(&i2c->dev, "Disabling charge pump\n");
-                       wm8903_write(codec, WM8903_CHARGE_PUMP_0,
-                                    cp_reg & ~WM8903_CP_ENA);
-               }
+               snd_soc_write(codec, reg, val);
        }
 
        return 0;
@@ -485,18 +417,17 @@ static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
 {
        struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
        struct snd_soc_codec *codec = widget->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
-       struct i2c_client *i2c = codec->control_data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
        u16 reg;
        int ret;
 
-       reg = wm8903_read(codec, WM8903_CLASS_W_0);
+       reg = snd_soc_read(codec, WM8903_CLASS_W_0);
 
        /* Turn it off if we're about to enable bypass */
        if (ucontrol->value.integer.value[0]) {
                if (wm8903->class_w_users == 0) {
-                       dev_dbg(&i2c->dev, "Disabling Class W\n");
-                       wm8903_write(codec, WM8903_CLASS_W_0, reg &
+                       dev_dbg(codec->dev, "Disabling Class W\n");
+                       snd_soc_write(codec, WM8903_CLASS_W_0, reg &
                                     ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
                }
                wm8903->class_w_users++;
@@ -508,14 +439,14 @@ static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
        /* If we've just disabled the last bypass path turn Class W on */
        if (!ucontrol->value.integer.value[0]) {
                if (wm8903->class_w_users == 1) {
-                       dev_dbg(&i2c->dev, "Enabling Class W\n");
-                       wm8903_write(codec, WM8903_CLASS_W_0, reg |
+                       dev_dbg(codec->dev, "Enabling Class W\n");
+                       snd_soc_write(codec, WM8903_CLASS_W_0, reg |
                                     WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
                }
                wm8903->class_w_users--;
        }
 
-       dev_dbg(&i2c->dev, "Bypass use count now %d\n",
+       dev_dbg(codec->dev, "Bypass use count now %d\n",
                wm8903->class_w_users);
 
        return ret;
@@ -528,9 +459,76 @@ static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
        .private_value =  SOC_SINGLE_VALUE(reg, shift, max, invert) }
 
 
+static int wm8903_deemph[] = { 0, 32000, 44100, 48000 };
+
+static int wm8903_set_deemph(struct snd_soc_codec *codec)
+{
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
+       int val, i, best;
+
+       /* If we're using deemphasis select the nearest available sample
+        * rate.
+        */
+       if (wm8903->deemph) {
+               best = 1;
+               for (i = 2; i < ARRAY_SIZE(wm8903_deemph); i++) {
+                       if (abs(wm8903_deemph[i] - wm8903->fs) <
+                           abs(wm8903_deemph[best] - wm8903->fs))
+                               best = i;
+               }
+
+               val = best << WM8903_DEEMPH_SHIFT;
+       } else {
+               best = 0;
+               val = 0;
+       }
+
+       dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
+               best, wm8903_deemph[best]);
+
+       return snd_soc_update_bits(codec, WM8903_DAC_DIGITAL_1,
+                                  WM8903_DEEMPH_MASK, val);
+}
+
+static int wm8903_get_deemph(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
+
+       ucontrol->value.enumerated.item[0] = wm8903->deemph;
+
+       return 0;
+}
+
+static int wm8903_put_deemph(struct snd_kcontrol *kcontrol,
+                            struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
+       struct wm8903_priv *wm8903 = 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 (wm8903->deemph != deemph) {
+               wm8903->deemph = deemph;
+
+               wm8903_set_deemph(codec);
+
+               ret = 1;
+       }
+       mutex_unlock(&codec->mutex);
+
+       return ret;
+}
+
 /* ALSA can only do steps of .01dB */
 static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
 
+static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
 
 static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
@@ -539,6 +537,23 @@ static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
 static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
 static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
 
+static const char *hpf_mode_text[] = {
+       "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
+};
+
+static const struct soc_enum hpf_mode =
+       SOC_ENUM_SINGLE(WM8903_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
+
+static const char *osr_text[] = {
+       "Low power", "High performance"
+};
+
+static const struct soc_enum adc_osr =
+       SOC_ENUM_SINGLE(WM8903_ANALOGUE_ADC_0, 0, 2, osr_text);
+
+static const struct soc_enum dac_osr =
+       SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 0, 2, osr_text);
+
 static const char *drc_slope_text[] = {
        "1", "1/2", "1/4", "1/8", "1/16", "0"
 };
@@ -601,13 +616,6 @@ static const char *mute_mode_text[] = {
 static const struct soc_enum mute_mode =
        SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
 
-static const char *dac_deemphasis_text[] = {
-       "Disabled", "32kHz", "44.1kHz", "48kHz"
-};
-
-static const struct soc_enum dac_deemphasis =
-       SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 1, 4, dac_deemphasis_text);
-
 static const char *companding_text[] = {
        "ulaw", "alaw"
 };
@@ -649,28 +657,41 @@ static const struct soc_enum rinput_inv_enum =
        SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
 
 
+static const char *sidetone_text[] = {
+       "None", "Left", "Right"
+};
+
+static const struct soc_enum lsidetone_enum =
+       SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 2, 3, sidetone_text);
+
+static const struct soc_enum rsidetone_enum =
+       SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 0, 3, sidetone_text);
+
 static const struct snd_kcontrol_new wm8903_snd_controls[] = {
 
 /* Input PGAs - No TLV since the scale depends on PGA mode */
 SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
-          7, 1, 0),
+          7, 1, 1),
 SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
           0, 31, 0),
 SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
           6, 1, 0),
 
 SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
-          7, 1, 0),
+          7, 1, 1),
 SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
           0, 31, 0),
 SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
           6, 1, 0),
 
 /* ADCs */
+SOC_ENUM("ADC OSR", adc_osr),
+SOC_SINGLE("HPF Switch", WM8903_ADC_DIGITAL_0, 4, 1, 0),
+SOC_ENUM("HPF Mode", hpf_mode),
 SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
 SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
 SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
-SOC_SINGLE_TLV("DRC Compressor Threashold Volume", WM8903_DRC_3, 5, 124, 1,
+SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8903_DRC_3, 5, 124, 1,
               drc_tlv_thresh),
 SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
 SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
@@ -680,11 +701,11 @@ SOC_ENUM("DRC Decay Rate", drc_decay),
 SOC_ENUM("DRC FF Delay", drc_ff_delay),
 SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
 SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
-SOC_SINGLE_TLV("DRC QR Threashold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
+SOC_SINGLE_TLV("DRC QR Threshold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
 SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
 SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
 SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
-SOC_ENUM("DRC Smoothing Threashold", drc_smoothing),
+SOC_ENUM("DRC Smoothing Threshold", drc_smoothing),
 SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
 
 SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
@@ -692,17 +713,20 @@ SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
 SOC_ENUM("ADC Companding Mode", adc_companding),
 SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
 
+SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
+              12, 0, digital_sidetone_tlv),
+
 /* DAC */
+SOC_ENUM("DAC OSR", dac_osr),
 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
                 WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
 SOC_ENUM("DAC Soft Mute Rate", soft_mute),
 SOC_ENUM("DAC Mute Mode", mute_mode),
 SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
-SOC_ENUM("DAC De-emphasis", dac_deemphasis),
-SOC_SINGLE("DAC Sloping Stopband Filter Switch",
-          WM8903_DAC_DIGITAL_1, 11, 1, 0),
 SOC_ENUM("DAC Companding Mode", dac_companding),
 SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
+SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
+                   wm8903_get_deemph, wm8903_put_deemph),
 
 /* Headphones */
 SOC_DOUBLE_R("Headphone Switch",
@@ -736,21 +760,6 @@ SOC_DOUBLE_R_TLV("Speaker Volume",
                 0, 63, 0, out_tlv),
 };
 
-static int wm8903_add_controls(struct snd_soc_codec *codec)
-{
-       int err, i;
-
-       for (i = 0; i < ARRAY_SIZE(wm8903_snd_controls); i++) {
-               err = snd_ctl_add(codec->card,
-                                 snd_soc_cnew(&wm8903_snd_controls[i],
-                                              codec, NULL));
-               if (err < 0)
-                       return err;
-       }
-
-       return 0;
-}
-
 static const struct snd_kcontrol_new linput_mode_mux =
        SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
 
@@ -769,18 +778,24 @@ static const struct snd_kcontrol_new rinput_mux =
 static const struct snd_kcontrol_new rinput_inv_mux =
        SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
 
+static const struct snd_kcontrol_new lsidetone_mux =
+       SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
+
+static const struct snd_kcontrol_new rsidetone_mux =
+       SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
+
 static const struct snd_kcontrol_new left_output_mixer[] = {
 SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
 SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
 SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
-SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
+SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
 };
 
 static const struct snd_kcontrol_new right_output_mixer[] = {
 SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
 SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
 SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
-SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
+SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
 };
 
 static const struct snd_kcontrol_new left_speaker_mixer[] = {
@@ -788,7 +803,7 @@ SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
 SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
 SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
 SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
-               1, 1, 0),
+               0, 1, 0),
 };
 
 static const struct snd_kcontrol_new right_speaker_mixer[] = {
@@ -797,7 +812,7 @@ SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
 SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
                1, 1, 0),
 SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
-               1, 1, 0),
+               0, 1, 0),
 };
 
 static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
@@ -835,6 +850,9 @@ SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
 SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8903_POWER_MANAGEMENT_6, 1, 0),
 SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8903_POWER_MANAGEMENT_6, 0, 0),
 
+SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
+SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
+
 SND_SOC_DAPM_DAC("DACL", "Left Playback", WM8903_POWER_MANAGEMENT_6, 3, 0),
 SND_SOC_DAPM_DAC("DACR", "Right Playback", WM8903_POWER_MANAGEMENT_6, 2, 0),
 
@@ -851,26 +869,29 @@ SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
 SND_SOC_DAPM_PGA_E("Left Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
                   1, 0, NULL, 0, wm8903_output_event,
                   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
-                  SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
+                  SND_SOC_DAPM_PRE_PMD),
 SND_SOC_DAPM_PGA_E("Right Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
                   0, 0, NULL, 0, wm8903_output_event,
                   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
-                  SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
+                  SND_SOC_DAPM_PRE_PMD),
 
 SND_SOC_DAPM_PGA_E("Left Line Output PGA", WM8903_POWER_MANAGEMENT_3, 1, 0,
                   NULL, 0, wm8903_output_event,
                   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
-                  SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
+                  SND_SOC_DAPM_PRE_PMD),
 SND_SOC_DAPM_PGA_E("Right Line Output PGA", WM8903_POWER_MANAGEMENT_3, 0, 0,
                   NULL, 0, wm8903_output_event,
                   SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
-                  SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
+                  SND_SOC_DAPM_PRE_PMD),
 
 SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
                 NULL, 0),
 SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
                 NULL, 0),
 
+SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
+                   wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
+SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
 };
 
 static const struct snd_soc_dapm_route intercon[] = {
@@ -916,7 +937,19 @@ static const struct snd_soc_dapm_route intercon[] = {
        { "Right Input PGA", NULL, "Right Input Mode Mux" },
 
        { "ADCL", NULL, "Left Input PGA" },
+       { "ADCL", NULL, "CLK_DSP" },
        { "ADCR", NULL, "Right Input PGA" },
+       { "ADCR", NULL, "CLK_DSP" },
+
+       { "DACL Sidetone", "Left", "ADCL" },
+       { "DACL Sidetone", "Right", "ADCR" },
+       { "DACR Sidetone", "Left", "ADCL" },
+       { "DACR Sidetone", "Right", "ADCR" },
+
+       { "DACL", NULL, "DACL Sidetone" },
+       { "DACL", NULL, "CLK_DSP" },
+       { "DACR", NULL, "DACR Sidetone" },
+       { "DACR", NULL, "CLK_DSP" },
 
        { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
        { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
@@ -958,16 +991,20 @@ static const struct snd_soc_dapm_route intercon[] = {
 
        { "ROP", NULL, "Right Speaker PGA" },
        { "RON", NULL, "Right Speaker PGA" },
+
+       { "Left Headphone Output PGA", NULL, "Charge Pump" },
+       { "Right Headphone Output PGA", NULL, "Charge Pump" },
+       { "Left Line Output PGA", NULL, "Charge Pump" },
+       { "Right Line Output PGA", NULL, "Charge Pump" },
 };
 
 static int wm8903_add_widgets(struct snd_soc_codec *codec)
 {
-       snd_soc_dapm_new_controls(codec, wm8903_dapm_widgets,
-                                 ARRAY_SIZE(wm8903_dapm_widgets));
+       struct snd_soc_dapm_context *dapm = &codec->dapm;
 
-       snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
-
-       snd_soc_dapm_new_widgets(codec);
+       snd_soc_dapm_new_controls(dapm, wm8903_dapm_widgets,
+                                 ARRAY_SIZE(wm8903_dapm_widgets));
+       snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
 
        return 0;
 }
@@ -975,54 +1012,56 @@ static int wm8903_add_widgets(struct snd_soc_codec *codec)
 static int wm8903_set_bias_level(struct snd_soc_codec *codec,
                                 enum snd_soc_bias_level level)
 {
-       struct i2c_client *i2c = codec->control_data;
-       u16 reg, reg2;
+       u16 reg;
 
        switch (level) {
        case SND_SOC_BIAS_ON:
        case SND_SOC_BIAS_PREPARE:
-               reg = wm8903_read(codec, WM8903_VMID_CONTROL_0);
+               reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
                reg &= ~(WM8903_VMID_RES_MASK);
                reg |= WM8903_VMID_RES_50K;
-               wm8903_write(codec, WM8903_VMID_CONTROL_0, reg);
+               snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
                break;
 
        case SND_SOC_BIAS_STANDBY:
-               if (codec->bias_level == SND_SOC_BIAS_OFF) {
+               if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
+                       snd_soc_write(codec, WM8903_CLOCK_RATES_2,
+                                    WM8903_CLK_SYS_ENA);
+
+                       /* Change DC servo dither level in startup sequence */
+                       snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, 0x11);
+                       snd_soc_write(codec, WM8903_WRITE_SEQUENCER_1, 0x1257);
+                       snd_soc_write(codec, WM8903_WRITE_SEQUENCER_2, 0x2);
+
                        wm8903_run_sequence(codec, 0);
                        wm8903_sync_reg_cache(codec, codec->reg_cache);
 
-                       /* Enable low impedence charge pump output */
-                       reg = wm8903_read(codec,
-                                         WM8903_CONTROL_INTERFACE_TEST_1);
-                       wm8903_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
-                                    reg | WM8903_TEST_KEY);
-                       reg2 = wm8903_read(codec, WM8903_CHARGE_PUMP_TEST_1);
-                       wm8903_write(codec, WM8903_CHARGE_PUMP_TEST_1,
-                                    reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
-                       wm8903_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
-                                    reg);
-
                        /* By default no bypass paths are enabled so
                         * enable Class W support.
                         */
-                       dev_dbg(&i2c->dev, "Enabling Class W\n");
-                       wm8903_write(codec, WM8903_CLASS_W_0, reg |
-                                    WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
+                       dev_dbg(codec->dev, "Enabling Class W\n");
+                       snd_soc_update_bits(codec, WM8903_CLASS_W_0,
+                                           WM8903_CP_DYN_FREQ |
+                                           WM8903_CP_DYN_V,
+                                           WM8903_CP_DYN_FREQ |
+                                           WM8903_CP_DYN_V);
                }
 
-               reg = wm8903_read(codec, WM8903_VMID_CONTROL_0);
+               reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
                reg &= ~(WM8903_VMID_RES_MASK);
                reg |= WM8903_VMID_RES_250K;
-               wm8903_write(codec, WM8903_VMID_CONTROL_0, reg);
+               snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
                break;
 
        case SND_SOC_BIAS_OFF:
                wm8903_run_sequence(codec, 32);
+               reg = snd_soc_read(codec, WM8903_CLOCK_RATES_2);
+               reg &= ~WM8903_CLK_SYS_ENA;
+               snd_soc_write(codec, WM8903_CLOCK_RATES_2, reg);
                break;
        }
 
-       codec->bias_level = level;
+       codec->dapm.bias_level = level;
 
        return 0;
 }
@@ -1031,7 +1070,7 @@ static int wm8903_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 wm8903_priv *wm8903 = codec->private_data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
 
        wm8903->sysclk = freq;
 
@@ -1042,7 +1081,7 @@ static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
                              unsigned int fmt)
 {
        struct snd_soc_codec *codec = codec_dai->codec;
-       u16 aif1 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_1);
+       u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
 
        aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
                  WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
@@ -1120,7 +1159,7 @@ static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
                return -EINVAL;
        }
 
-       wm8903_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
+       snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
 
        return 0;
 }
@@ -1130,14 +1169,14 @@ static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
        struct snd_soc_codec *codec = codec_dai->codec;
        u16 reg;
 
-       reg = wm8903_read(codec, WM8903_DAC_DIGITAL_1);
+       reg = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
 
        if (mute)
                reg |= WM8903_DAC_MUTE;
        else
                reg &= ~WM8903_DAC_MUTE;
 
-       wm8903_write(codec, WM8903_DAC_DIGITAL_1, reg);
+       snd_soc_write(codec, WM8903_DAC_DIGITAL_1, reg);
 
        return 0;
 }
@@ -1216,22 +1255,18 @@ static struct {
        int div;
 } bclk_divs[] = {
        {  10,  0 },
-       {  15,  1 },
        {  20,  2 },
        {  30,  3 },
        {  40,  4 },
        {  50,  5 },
-       {  55,  6 },
        {  60,  7 },
        {  80,  8 },
        { 100,  9 },
-       { 110, 10 },
        { 120, 11 },
        { 160, 12 },
        { 200, 13 },
        { 220, 14 },
        { 240, 15 },
-       { 250, 16 },
        { 300, 17 },
        { 320, 18 },
        { 440, 19 },
@@ -1257,73 +1292,13 @@ static struct {
        { 0,      0 },
 };
 
-static int wm8903_startup(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->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
-       struct i2c_client *i2c = codec->control_data;
-       struct snd_pcm_runtime *master_runtime;
-
-       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
-               wm8903->playback_active++;
-       else
-               wm8903->capture_active++;
-
-       /* The DAI has shared clocks so if we already have a playback or
-        * capture going then constrain this substream to match it.
-        */
-       if (wm8903->master_substream) {
-               master_runtime = wm8903->master_substream->runtime;
-
-               dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
-                       master_runtime->sample_bits,
-                       master_runtime->rate);
-
-               snd_pcm_hw_constraint_minmax(substream->runtime,
-                                            SNDRV_PCM_HW_PARAM_RATE,
-                                            master_runtime->rate,
-                                            master_runtime->rate);
-
-               snd_pcm_hw_constraint_minmax(substream->runtime,
-                                            SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
-                                            master_runtime->sample_bits,
-                                            master_runtime->sample_bits);
-
-               wm8903->slave_substream = substream;
-       } else
-               wm8903->master_substream = substream;
-
-       return 0;
-}
-
-static void wm8903_shutdown(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->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
-
-       if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
-               wm8903->playback_active--;
-       else
-               wm8903->capture_active--;
-
-       if (wm8903->master_substream == substream)
-               wm8903->master_substream = wm8903->slave_substream;
-
-       wm8903->slave_substream = NULL;
-}
-
 static int wm8903_hw_params(struct snd_pcm_substream *substream,
-                           struct snd_pcm_hw_params *params)
+                           struct snd_pcm_hw_params *params,
+                           struct snd_soc_dai *dai)
 {
        struct snd_soc_pcm_runtime *rtd = substream->private_data;
-       struct snd_soc_device *socdev = rtd->socdev;
-       struct snd_soc_codec *codec = socdev->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
-       struct i2c_client *i2c = codec->control_data;
+       struct snd_soc_codec *codec =rtd->codec;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
        int fs = params_rate(params);
        int bclk;
        int bclk_div;
@@ -1334,16 +1309,18 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
        int cur_val;
        int clk_sys;
 
-       u16 aif1 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_1);
-       u16 aif2 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_2);
-       u16 aif3 = wm8903_read(codec, WM8903_AUDIO_INTERFACE_3);
-       u16 clock0 = wm8903_read(codec, WM8903_CLOCK_RATES_0);
-       u16 clock1 = wm8903_read(codec, WM8903_CLOCK_RATES_1);
+       u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
+       u16 aif2 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_2);
+       u16 aif3 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_3);
+       u16 clock0 = snd_soc_read(codec, WM8903_CLOCK_RATES_0);
+       u16 clock1 = snd_soc_read(codec, WM8903_CLOCK_RATES_1);
+       u16 dac_digital1 = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
 
-       if (substream == wm8903->slave_substream) {
-               dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
-               return 0;
-       }
+       /* Enable sloping stopband filter for low sample rates */
+       if (fs <= 24000)
+               dac_digital1 |= WM8903_DAC_SB_FILT;
+       else
+               dac_digital1 &= ~WM8903_DAC_SB_FILT;
 
        /* Configure sample rate logic for DSP - choose nearest rate */
        dsp_config = 0;
@@ -1356,20 +1333,7 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
                }
        }
 
-       /* Constraints should stop us hitting this but let's make sure */
-       if (wm8903->capture_active)
-               switch (sample_rates[dsp_config].rate) {
-               case 88200:
-               case 96000:
-                       dev_err(&i2c->dev, "%dHz unsupported by ADC\n",
-                               fs);
-                       return -EINVAL;
-
-               default:
-                       break;
-               }
-
-       dev_dbg(&i2c->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
+       dev_dbg(codec->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
        clock1 &= ~WM8903_SAMPLE_RATE_MASK;
        clock1 |= sample_rates[dsp_config].value;
 
@@ -1395,7 +1359,7 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
                return -EINVAL;
        }
 
-       dev_dbg(&i2c->dev, "MCLK = %dHz, target sample rate = %dHz\n",
+       dev_dbg(codec->dev, "MCLK = %dHz, target sample rate = %dHz\n",
                wm8903->sysclk, fs);
 
        /* We may not have an MCLK which allows us to generate exactly
@@ -1430,12 +1394,12 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
        clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
        clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
 
-       dev_dbg(&i2c->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
+       dev_dbg(codec->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
                clk_sys_ratios[clk_config].rate,
                clk_sys_ratios[clk_config].mode,
                clk_sys_ratios[clk_config].div);
 
-       dev_dbg(&i2c->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
+       dev_dbg(codec->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
 
        /* We may not get quite the right frequency if using
         * approximate clocks so look for the closest match that is
@@ -1457,22 +1421,141 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
        aif2 &= ~WM8903_BCLK_DIV_MASK;
        aif3 &= ~WM8903_LRCLK_RATE_MASK;
 
-       dev_dbg(&i2c->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
+       dev_dbg(codec->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
                bclk_divs[bclk_div].ratio / 10, bclk,
                (clk_sys * 10) / bclk_divs[bclk_div].ratio);
 
        aif2 |= bclk_divs[bclk_div].div;
        aif3 |= bclk / fs;
 
-       wm8903_write(codec, WM8903_CLOCK_RATES_0, clock0);
-       wm8903_write(codec, WM8903_CLOCK_RATES_1, clock1);
-       wm8903_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
-       wm8903_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
-       wm8903_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
+       wm8903->fs = params_rate(params);
+       wm8903_set_deemph(codec);
+
+       snd_soc_write(codec, WM8903_CLOCK_RATES_0, clock0);
+       snd_soc_write(codec, WM8903_CLOCK_RATES_1, clock1);
+       snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
+       snd_soc_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
+       snd_soc_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
+       snd_soc_write(codec, WM8903_DAC_DIGITAL_1, dac_digital1);
 
        return 0;
 }
 
+/**
+ * wm8903_mic_detect - Enable microphone detection via the WM8903 IRQ
+ *
+ * @codec:  WM8903 codec
+ * @jack:   jack to report detection events on
+ * @det:    value to report for presence detection
+ * @shrt:   value to report for short detection
+ *
+ * Enable microphone detection via IRQ on the WM8903.  If GPIOs are
+ * being used to bring out signals to the processor then only platform
+ * data configuration is needed for WM8903 and processor GPIOs should
+ * be configured using snd_soc_jack_add_gpios() instead.
+ *
+ * The current threasholds for detection should be configured using
+ * micdet_cfg in the platform data.  Using this function will force on
+ * the microphone bias for the device.
+ */
+int wm8903_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
+                     int det, int shrt)
+{
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
+       int irq_mask = WM8903_MICDET_EINT | WM8903_MICSHRT_EINT;
+
+       dev_dbg(codec->dev, "Enabling microphone detection: %x %x\n",
+               det, shrt);
+
+       /* Store the configuration */
+       wm8903->mic_jack = jack;
+       wm8903->mic_det = det;
+       wm8903->mic_short = shrt;
+
+       /* Enable interrupts we've got a report configured for */
+       if (det)
+               irq_mask &= ~WM8903_MICDET_EINT;
+       if (shrt)
+               irq_mask &= ~WM8903_MICSHRT_EINT;
+
+       snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
+                           WM8903_MICDET_EINT | WM8903_MICSHRT_EINT,
+                           irq_mask);
+
+       if (det || shrt) {
+               /* Enable mic detection, this may not have been set through
+                * platform data (eg, if the defaults are OK). */
+               snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
+                                   WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
+               snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
+                                   WM8903_MICDET_ENA, WM8903_MICDET_ENA);
+       } else {
+               snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
+                                   WM8903_MICDET_ENA, 0);
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(wm8903_mic_detect);
+
+static irqreturn_t wm8903_irq(int irq, void *data)
+{
+       struct snd_soc_codec *codec = data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
+       int mic_report;
+       int int_pol;
+       int int_val = 0;
+       int mask = ~snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1_MASK);
+
+       int_val = snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1) & mask;
+
+       if (int_val & WM8903_WSEQ_BUSY_EINT) {
+               dev_dbg(codec->dev, "Write sequencer done\n");
+               complete(&wm8903->wseq);
+       }
+
+       /*
+        * The rest is microphone jack detection.  We need to manually
+        * invert the polarity of the interrupt after each event - to
+        * simplify the code keep track of the last state we reported
+        * and just invert the relevant bits in both the report and
+        * the polarity register.
+        */
+       mic_report = wm8903->mic_last_report;
+       int_pol = snd_soc_read(codec, WM8903_INTERRUPT_POLARITY_1);
+
+#ifndef CONFIG_SND_SOC_WM8903_MODULE
+       if (int_val & (WM8903_MICSHRT_EINT | WM8903_MICDET_EINT))
+               trace_snd_soc_jack_irq(dev_name(codec->dev));
+#endif
+
+       if (int_val & WM8903_MICSHRT_EINT) {
+               dev_dbg(codec->dev, "Microphone short (pol=%x)\n", int_pol);
+
+               mic_report ^= wm8903->mic_short;
+               int_pol ^= WM8903_MICSHRT_INV;
+       }
+
+       if (int_val & WM8903_MICDET_EINT) {
+               dev_dbg(codec->dev, "Microphone detect (pol=%x)\n", int_pol);
+
+               mic_report ^= wm8903->mic_det;
+               int_pol ^= WM8903_MICDET_INV;
+
+               msleep(wm8903->mic_delay);
+       }
+
+       snd_soc_update_bits(codec, WM8903_INTERRUPT_POLARITY_1,
+                           WM8903_MICSHRT_INV | WM8903_MICDET_INV, int_pol);
+
+       snd_soc_jack_report(wm8903->mic_jack, mic_report,
+                           wm8903->mic_short | wm8903->mic_det);
+
+       wm8903->mic_last_report = mic_report;
+
+       return IRQ_HANDLED;
+}
+
 #define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
                               SNDRV_PCM_RATE_11025 |   \
                               SNDRV_PCM_RATE_16000 |   \
@@ -1495,8 +1578,15 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
                        SNDRV_PCM_FMTBIT_S20_3LE |\
                        SNDRV_PCM_FMTBIT_S24_LE)
 
-struct snd_soc_dai wm8903_dai = {
-       .name = "WM8903",
+static struct snd_soc_dai_ops wm8903_dai_ops = {
+       .hw_params      = wm8903_hw_params,
+       .digital_mute   = wm8903_digital_mute,
+       .set_fmt        = wm8903_set_dai_fmt,
+       .set_sysclk     = wm8903_set_dai_sysclk,
+};
+
+static struct snd_soc_dai_driver wm8903_dai = {
+       .name = "wm8903-hifi",
        .playback = {
                .stream_name = "Playback",
                .channels_min = 2,
@@ -1511,302 +1601,242 @@ struct snd_soc_dai wm8903_dai = {
                 .rates = WM8903_CAPTURE_RATES,
                 .formats = WM8903_FORMATS,
         },
-       .ops = {
-                .startup = wm8903_startup,
-                .shutdown = wm8903_shutdown,
-                .hw_params = wm8903_hw_params,
-       },
-       .dai_ops = {
-                .digital_mute = wm8903_digital_mute,
-                .set_fmt = wm8903_set_dai_fmt,
-                .set_sysclk = wm8903_set_dai_sysclk
-       }
+       .ops = &wm8903_dai_ops,
+       .symmetric_rates = 1,
 };
-EXPORT_SYMBOL_GPL(wm8903_dai);
 
-static int wm8903_suspend(struct platform_device *pdev, pm_message_t state)
+static int wm8903_suspend(struct snd_soc_codec *codec, pm_message_t state)
 {
-       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
-       struct snd_soc_codec *codec = socdev->codec;
-
        wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
 
        return 0;
 }
 
-static int wm8903_resume(struct platform_device *pdev)
+static int wm8903_resume(struct snd_soc_codec *codec)
 {
-       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
-       struct snd_soc_codec *codec = socdev->codec;
-       struct i2c_client *i2c = codec->control_data;
        int i;
        u16 *reg_cache = codec->reg_cache;
-       u16 *tmp_cache = kmemdup(codec->reg_cache, sizeof(wm8903_reg_defaults),
+       u16 *tmp_cache = kmemdup(reg_cache, sizeof(wm8903_reg_defaults),
                                 GFP_KERNEL);
 
        /* Bring the codec back up to standby first to minimise pop/clicks */
        wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
-       wm8903_set_bias_level(codec, codec->suspend_bias_level);
 
        /* Sync back everything else */
        if (tmp_cache) {
                for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
                        if (tmp_cache[i] != reg_cache[i])
-                               wm8903_write(codec, i, tmp_cache[i]);
+                               snd_soc_write(codec, i, tmp_cache[i]);
+               kfree(tmp_cache);
        } else {
-               dev_err(&i2c->dev, "Failed to allocate temporary cache\n");
+               dev_err(codec->dev, "Failed to allocate temporary cache\n");
        }
 
        return 0;
 }
 
-/*
- * initialise the WM8903 driver
- * register the mixer and dsp interfaces with the kernel
- */
-static int wm8903_init(struct snd_soc_device *socdev)
+static int wm8903_probe(struct snd_soc_codec *codec)
 {
-       struct snd_soc_codec *codec = socdev->codec;
-       struct i2c_client *i2c = codec->control_data;
-       int ret = 0;
+       struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
+       int ret, i;
+       int trigger, irq_pol;
        u16 val;
 
-       val = wm8903_hw_read(codec, WM8903_SW_RESET_AND_ID);
+       init_completion(&wm8903->wseq);
+
+       ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
+       if (ret != 0) {
+               dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
+               return ret;
+       }
+
+       val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
        if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
-               dev_err(&i2c->dev,
+               dev_err(codec->dev,
                        "Device with ID register %x is not a WM8903\n", val);
                return -ENODEV;
        }
 
-       codec->name = "WM8903";
-       codec->owner = THIS_MODULE;
-       codec->read = wm8903_read;
-       codec->write = wm8903_write;
-       codec->bias_level = SND_SOC_BIAS_OFF;
-       codec->set_bias_level = wm8903_set_bias_level;
-       codec->dai = &wm8903_dai;
-       codec->num_dai = 1;
-       codec->reg_cache_size = ARRAY_SIZE(wm8903_reg_defaults);
-       codec->reg_cache = kmemdup(wm8903_reg_defaults,
-                                  sizeof(wm8903_reg_defaults),
-                                  GFP_KERNEL);
-       if (codec->reg_cache == NULL) {
-               dev_err(&i2c->dev, "Failed to allocate register cache\n");
-               return -ENOMEM;
-       }
-
-       val = wm8903_read(codec, WM8903_REVISION_NUMBER);
-       dev_info(&i2c->dev, "WM8903 revision %d\n",
+       val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
+       dev_info(codec->dev, "WM8903 revision %d\n",
                 val & WM8903_CHIP_REV_MASK);
 
        wm8903_reset(codec);
 
-       /* register pcms */
-       ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
-       if (ret < 0) {
-               dev_err(&i2c->dev, "failed to create pcms\n");
-               goto pcm_err;
+       /* Set up GPIOs and microphone detection */
+       if (pdata) {
+               for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
+                       if (!pdata->gpio_cfg[i])
+                               continue;
+
+                       snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
+                                     pdata->gpio_cfg[i] & 0xffff);
+               }
+
+               snd_soc_write(codec, WM8903_MIC_BIAS_CONTROL_0,
+                             pdata->micdet_cfg);
+
+               /* Microphone detection needs the WSEQ clock */
+               if (pdata->micdet_cfg)
+                       snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
+                                           WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
+
+               wm8903->mic_delay = pdata->micdet_delay;
        }
+       
+       if (wm8903->irq) {
+               if (pdata && pdata->irq_active_low) {
+                       trigger = IRQF_TRIGGER_LOW;
+                       irq_pol = WM8903_IRQ_POL;
+               } else {
+                       trigger = IRQF_TRIGGER_HIGH;
+                       irq_pol = 0;
+               }
 
-       /* SYSCLK is required for pretty much anything */
-       wm8903_write(codec, WM8903_CLOCK_RATES_2, WM8903_CLK_SYS_ENA);
+               snd_soc_update_bits(codec, WM8903_INTERRUPT_CONTROL,
+                                   WM8903_IRQ_POL, irq_pol);
+               
+               ret = request_threaded_irq(wm8903->irq, NULL, wm8903_irq,
+                                          trigger | IRQF_ONESHOT,
+                                          "wm8903", codec);
+               if (ret != 0) {
+                       dev_err(codec->dev, "Failed to request IRQ: %d\n",
+                               ret);
+                       return ret;
+               }
+
+               /* Enable write sequencer interrupts */
+               snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
+                                   WM8903_IM_WSEQ_BUSY_EINT, 0);
+       }
 
        /* power on device */
        wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 
        /* Latch volume update bits */
-       val = wm8903_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
+       val = snd_soc_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
        val |= WM8903_ADCVU;
-       wm8903_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
-       wm8903_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
+       snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
+       snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
 
-       val = wm8903_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
+       val = snd_soc_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
        val |= WM8903_DACVU;
-       wm8903_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
-       wm8903_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
+       snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
+       snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
 
-       val = wm8903_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
+       val = snd_soc_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
        val |= WM8903_HPOUTVU;
-       wm8903_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
-       wm8903_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
+       snd_soc_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
+       snd_soc_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
 
-       val = wm8903_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
+       val = snd_soc_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
        val |= WM8903_LINEOUTVU;
-       wm8903_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
-       wm8903_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
+       snd_soc_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
+       snd_soc_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
 
-       val = wm8903_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
+       val = snd_soc_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
        val |= WM8903_SPKVU;
-       wm8903_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
-       wm8903_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
+       snd_soc_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
+       snd_soc_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
 
        /* Enable DAC soft mute by default */
-       val = wm8903_read(codec, WM8903_DAC_DIGITAL_1);
+       val = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
        val |= WM8903_DAC_MUTEMODE;
-       wm8903_write(codec, WM8903_DAC_DIGITAL_1, val);
+       snd_soc_write(codec, WM8903_DAC_DIGITAL_1, val);
 
-       wm8903_add_controls(codec);
+       snd_soc_add_controls(codec, wm8903_snd_controls,
+                               ARRAY_SIZE(wm8903_snd_controls));
        wm8903_add_widgets(codec);
-       ret = snd_soc_register_card(socdev);
-       if (ret < 0) {
-               dev_err(&i2c->dev, "wm8903: failed to register card\n");
-               goto card_err;
-       }
 
        return ret;
+}
 
-card_err:
-       snd_soc_free_pcms(socdev);
-       snd_soc_dapm_free(socdev);
-pcm_err:
-       kfree(codec->reg_cache);
-       return ret;
+/* power down chip */
+static int wm8903_remove(struct snd_soc_codec *codec)
+{
+       wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
+       return 0;
 }
 
-static struct snd_soc_device *wm8903_socdev;
+static struct snd_soc_codec_driver soc_codec_dev_wm8903 = {
+       .probe =        wm8903_probe,
+       .remove =       wm8903_remove,
+       .suspend =      wm8903_suspend,
+       .resume =       wm8903_resume,
+       .set_bias_level = wm8903_set_bias_level,
+       .reg_cache_size = ARRAY_SIZE(wm8903_reg_defaults),
+       .reg_word_size = sizeof(u16),
+       .reg_cache_default = wm8903_reg_defaults,
+       .volatile_register = wm8903_volatile_register,
+};
 
-static int wm8903_i2c_probe(struct i2c_client *i2c,
-                           const struct i2c_device_id *id)
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
+static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
+                                     const struct i2c_device_id *id)
 {
-       struct snd_soc_device *socdev = wm8903_socdev;
-       struct snd_soc_codec *codec = socdev->codec;
+       struct wm8903_priv *wm8903;
        int ret;
 
-       i2c_set_clientdata(i2c, codec);
-       codec->control_data = i2c;
+       wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
+       if (wm8903 == NULL)
+               return -ENOMEM;
 
-       ret = wm8903_init(socdev);
-       if (ret < 0)
-               dev_err(&i2c->dev, "Device initialisation failed\n");
+       i2c_set_clientdata(i2c, wm8903);
+       wm8903->irq = i2c->irq;
 
+       ret = snd_soc_register_codec(&i2c->dev,
+                       &soc_codec_dev_wm8903, &wm8903_dai, 1);
+       if (ret < 0)
+               kfree(wm8903);
        return ret;
 }
 
-static int wm8903_i2c_remove(struct i2c_client *client)
+static __devexit int wm8903_i2c_remove(struct i2c_client *client)
 {
-       struct snd_soc_codec *codec = i2c_get_clientdata(client);
-       kfree(codec->reg_cache);
+       snd_soc_unregister_codec(&client->dev);
+       kfree(i2c_get_clientdata(client));
        return 0;
 }
 
-/* i2c codec control layer */
 static const struct i2c_device_id wm8903_i2c_id[] = {
-       { "wm8903", 0 },
-       { }
+       { "wm8903", 0 },
+       { }
 };
 MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
 
 static struct i2c_driver wm8903_i2c_driver = {
        .driver = {
-               .name = "WM8903",
+               .name = "wm8903-codec",
                .owner = THIS_MODULE,
        },
-       .probe    = wm8903_i2c_probe,
-       .remove   = wm8903_i2c_remove,
+       .probe =    wm8903_i2c_probe,
+       .remove =   __devexit_p(wm8903_i2c_remove),
        .id_table = wm8903_i2c_id,
 };
+#endif
 
-static struct i2c_client *wm8903_i2c_device;
-
-static int wm8903_probe(struct platform_device *pdev)
+static int __init wm8903_modinit(void)
 {
-       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
-       struct wm8903_setup_data *setup;
-       struct snd_soc_codec *codec;
-       struct wm8903_priv *wm8903;
-       struct i2c_board_info board_info;
-       struct i2c_adapter *adapter;
        int ret = 0;
-
-       setup = socdev->codec_data;
-
-       if (!setup->i2c_address) {
-               dev_err(&pdev->dev, "No codec address provided\n");
-               return -ENODEV;
-       }
-
-       codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
-       if (codec == NULL)
-               return -ENOMEM;
-
-       wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
-       if (wm8903 == NULL) {
-               ret = -ENOMEM;
-               goto err_codec;
-       }
-
-       codec->private_data = wm8903;
-       socdev->codec = codec;
-       mutex_init(&codec->mutex);
-       INIT_LIST_HEAD(&codec->dapm_widgets);
-       INIT_LIST_HEAD(&codec->dapm_paths);
-
-       wm8903_socdev = socdev;
-
-       codec->hw_write = (hw_write_t)i2c_master_send;
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
        ret = i2c_add_driver(&wm8903_i2c_driver);
        if (ret != 0) {
-               dev_err(&pdev->dev, "can't add i2c driver");
-               goto err_priv;
-       } else {
-               memset(&board_info, 0, sizeof(board_info));
-               strlcpy(board_info.type, "wm8903", I2C_NAME_SIZE);
-               board_info.addr = setup->i2c_address;
-
-               adapter = i2c_get_adapter(setup->i2c_bus);
-               if (!adapter) {
-                       dev_err(&pdev->dev, "Can't get I2C bus %d\n",
-                               setup->i2c_bus);
-                       goto err_adapter;
-               }
-
-               wm8903_i2c_device = i2c_new_device(adapter, &board_info);
-               i2c_put_adapter(adapter);
-               if (wm8903_i2c_device == NULL) {
-                       dev_err(&pdev->dev,
-                               "I2C driver registration failed\n");
-                       ret = -ENODEV;
-                       goto err_adapter;
-               }
+               printk(KERN_ERR "Failed to register wm8903 I2C driver: %d\n",
+                      ret);
        }
-
-       return ret;
-
-err_adapter:
-       i2c_del_driver(&wm8903_i2c_driver);
-err_priv:
-       kfree(codec->private_data);
-err_codec:
-       kfree(codec);
+#endif
        return ret;
 }
+module_init(wm8903_modinit);
 
-/* power down chip */
-static int wm8903_remove(struct platform_device *pdev)
+static void __exit wm8903_exit(void)
 {
-       struct snd_soc_device *socdev = platform_get_drvdata(pdev);
-       struct snd_soc_codec *codec = socdev->codec;
-
-       if (codec->control_data)
-               wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
-
-       snd_soc_free_pcms(socdev);
-       snd_soc_dapm_free(socdev);
-       i2c_unregister_device(wm8903_i2c_device);
+#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
        i2c_del_driver(&wm8903_i2c_driver);
-       kfree(codec->private_data);
-       kfree(codec);
-
-       return 0;
+#endif
 }
-
-struct snd_soc_codec_device soc_codec_dev_wm8903 = {
-       .probe =        wm8903_probe,
-       .remove =       wm8903_remove,
-       .suspend =      wm8903_suspend,
-       .resume =       wm8903_resume,
-};
-EXPORT_SYMBOL_GPL(soc_codec_dev_wm8903);
+module_exit(wm8903_exit);
 
 MODULE_DESCRIPTION("ASoC WM8903 driver");
 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");