Merge branch 'topic/asoc' into for-linus
[safe/jmp/linux-2.6] / sound / soc / codecs / wm8903.c
index 8cf571f..bf08282 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 "wm8903.h"
 
@@ -217,137 +219,68 @@ struct wm8903_priv {
        int sysclk;
 
        /* Reference counts */
-       int charge_pump_users;
        int class_w_users;
        int playback_active;
        int capture_active;
 
+       struct completion wseq;
+
+       struct snd_soc_jack *mic_jack;
+       int mic_det;
+       int mic_short;
+       int mic_last_report;
+       int mic_delay;
+
        struct snd_pcm_substream *master_substream;
        struct snd_pcm_substream *slave_substream;
 };
 
-
-static unsigned int wm8903_read_reg_cache(struct snd_soc_codec *codec,
-                                                unsigned int reg)
-{
-       u16 *cache = codec->reg_cache;
-
-       BUG_ON(reg >= ARRAY_SIZE(wm8903_reg_defaults));
-
-       return cache[reg];
-}
-
-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;
-       }
-
-       return (data >> 8) | ((data & 0xff) << 8);
-}
-
-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);
+               return 1;
 
        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;
-
-       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);
 
-       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");
 
-       /* 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;
 }
@@ -358,12 +291,12 @@ 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));
 }
@@ -373,6 +306,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
@@ -382,19 +324,20 @@ 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();
@@ -414,67 +357,52 @@ static int wm8903_output_event(struct snd_soc_dapm_widget *w,
        }
 
        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;
@@ -493,18 +421,18 @@ 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 wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
        struct i2c_client *i2c = codec->control_data;
        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 &
+                       snd_soc_write(codec, WM8903_CLASS_W_0, reg &
                                     ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
                }
                wm8903->class_w_users++;
@@ -517,7 +445,7 @@ static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
        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 |
+                       snd_soc_write(codec, WM8903_CLASS_W_0, reg |
                                     WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
                }
                wm8903->class_w_users--;
@@ -539,6 +467,7 @@ static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
 /* 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);
@@ -657,6 +586,16 @@ 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 */
@@ -678,7 +617,7 @@ SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
 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),
@@ -688,11 +627,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,
@@ -700,6 +639,9 @@ 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_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
                 WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
@@ -707,8 +649,6 @@ 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),
 
@@ -762,6 +702,12 @@ 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),
@@ -828,6 +774,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),
 
@@ -844,26 +793,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[] = {
@@ -909,7 +861,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" },
@@ -951,6 +915,11 @@ 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)
@@ -960,8 +929,6 @@ static int wm8903_add_widgets(struct snd_soc_codec *codec)
 
        snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
 
-       snd_soc_dapm_new_widgets(codec);
-
        return 0;
 }
 
@@ -974,50 +941,55 @@ static int wm8903_set_bias_level(struct snd_soc_codec *codec,
        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) {
-                       wm8903_write(codec, WM8903_CLOCK_RATES_2,
+                       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,
+                       reg = snd_soc_read(codec,
                                          WM8903_CONTROL_INTERFACE_TEST_1);
-                       wm8903_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
+                       snd_soc_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 = snd_soc_read(codec, WM8903_CHARGE_PUMP_TEST_1);
+                       snd_soc_write(codec, WM8903_CHARGE_PUMP_TEST_1,
                                     reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
-                       wm8903_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
+                       snd_soc_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 |
+                       snd_soc_write(codec, WM8903_CLASS_W_0, reg |
                                     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 = wm8903_read(codec, WM8903_CLOCK_RATES_2);
+               reg = snd_soc_read(codec, WM8903_CLOCK_RATES_2);
                reg &= ~WM8903_CLK_SYS_ENA;
-               wm8903_write(codec, WM8903_CLOCK_RATES_2, reg);
+               snd_soc_write(codec, WM8903_CLOCK_RATES_2, reg);
                break;
        }
 
@@ -1030,7 +1002,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;
 
@@ -1041,7 +1013,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);
@@ -1119,7 +1091,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;
 }
@@ -1129,14 +1101,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;
 }
@@ -1215,22 +1187,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 },
@@ -1262,7 +1230,7 @@ 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->card->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
        struct i2c_client *i2c = codec->control_data;
        struct snd_pcm_runtime *master_runtime;
 
@@ -1277,14 +1245,8 @@ static int wm8903_startup(struct snd_pcm_substream *substream,
        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);
+               dev_dbg(&i2c->dev, "Constraining to %d bits\n",
+                       master_runtime->sample_bits);
 
                snd_pcm_hw_constraint_minmax(substream->runtime,
                                             SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
@@ -1304,7 +1266,7 @@ 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->card->codec;
-       struct wm8903_priv *wm8903 = codec->private_data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
 
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
                wm8903->playback_active--;
@@ -1324,7 +1286,7 @@ static int wm8903_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;
-       struct wm8903_priv *wm8903 = codec->private_data;
+       struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
        struct i2c_client *i2c = codec->control_data;
        int fs = params_rate(params);
        int bclk;
@@ -1336,17 +1298,24 @@ 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;
        best_val = abs(sample_rates[dsp_config].rate - fs);
@@ -1466,15 +1435,126 @@ static int wm8903_hw_params(struct snd_pcm_substream *substream,
        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);
+       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 wm8903_priv *wm8903 = data;
+       struct snd_soc_codec *codec = &wm8903->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);
+
+       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 |   \
@@ -1523,6 +1603,7 @@ struct snd_soc_dai wm8903_dai = {
                 .formats = WM8903_FORMATS,
         },
        .ops = &wm8903_dai_ops,
+       .symmetric_rates = 1,
 };
 EXPORT_SYMBOL_GPL(wm8903_dai);
 
@@ -1543,18 +1624,18 @@ static int wm8903_resume(struct platform_device *pdev)
        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");
        }
@@ -1567,9 +1648,11 @@ static struct snd_soc_codec *wm8903_codec;
 static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
                                      const struct i2c_device_id *id)
 {
+       struct wm8903_platform_data *pdata = dev_get_platdata(&i2c->dev);
        struct wm8903_priv *wm8903;
        struct snd_soc_codec *codec;
-       int ret;
+       int ret, i;
+       int trigger, irq_pol;
        u16 val;
 
        wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
@@ -1585,66 +1668,118 @@ static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
        codec->dev = &i2c->dev;
        codec->name = "WM8903";
        codec->owner = THIS_MODULE;
-       codec->read = wm8903_read;
-       codec->write = wm8903_write;
-       codec->hw_write = (hw_write_t)i2c_master_send;
        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_cache);
        codec->reg_cache = &wm8903->reg_cache[0];
-       codec->private_data = wm8903;
+       snd_soc_codec_set_drvdata(codec, wm8903);
+       codec->volatile_register = wm8903_volatile_register;
+       init_completion(&wm8903->wseq);
 
        i2c_set_clientdata(i2c, codec);
        codec->control_data = i2c;
 
-       val = wm8903_hw_read(codec, WM8903_SW_RESET_AND_ID);
+       ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
+       if (ret != 0) {
+               dev_err(&i2c->dev, "Failed to set cache I/O: %d\n", ret);
+               goto err;
+       }
+
+       val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
        if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
                dev_err(&i2c->dev,
                        "Device with ID register %x is not a WM8903\n", val);
                return -ENODEV;
        }
 
-       val = wm8903_read(codec, WM8903_REVISION_NUMBER);
+       val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
        dev_info(&i2c->dev, "WM8903 revision %d\n",
                 val & WM8903_CHIP_REV_MASK);
 
        wm8903_reset(codec);
 
+       /* 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 (i2c->irq) {
+               if (pdata && pdata->irq_active_low) {
+                       trigger = IRQF_TRIGGER_LOW;
+                       irq_pol = WM8903_IRQ_POL;
+               } else {
+                       trigger = IRQF_TRIGGER_HIGH;
+                       irq_pol = 0;
+               }
+
+               snd_soc_update_bits(codec, WM8903_INTERRUPT_CONTROL,
+                                   WM8903_IRQ_POL, irq_pol);
+               
+               ret = request_threaded_irq(i2c->irq, NULL, wm8903_irq,
+                                          trigger | IRQF_ONESHOT,
+                                          "wm8903", wm8903);
+               if (ret != 0) {
+                       dev_err(&i2c->dev, "Failed to request IRQ: %d\n",
+                               ret);
+                       goto err;
+               }
+
+               /* 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_dai.dev = &i2c->dev;
        wm8903_codec = codec;
@@ -1652,7 +1787,7 @@ static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
        ret = snd_soc_register_codec(codec);
        if (ret != 0) {
                dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
-               goto err;
+               goto err_irq;
        }
 
        ret = snd_soc_register_dai(&wm8903_dai);
@@ -1665,6 +1800,9 @@ static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
 
 err_codec:
        snd_soc_unregister_codec(codec);
+err_irq:
+       if (i2c->irq)
+               free_irq(i2c->irq, wm8903);
 err:
        wm8903_codec = NULL;
        kfree(wm8903);
@@ -1674,13 +1812,17 @@ err:
 static __devexit int wm8903_i2c_remove(struct i2c_client *client)
 {
        struct snd_soc_codec *codec = i2c_get_clientdata(client);
+       struct wm8903_priv *priv = snd_soc_codec_get_drvdata(codec);
 
        snd_soc_unregister_dai(&wm8903_dai);
        snd_soc_unregister_codec(codec);
 
        wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
 
-       kfree(codec->private_data);
+       if (client->irq)
+               free_irq(client->irq, priv);
+
+       kfree(priv);
 
        wm8903_codec = NULL;
        wm8903_dai.dev = NULL;
@@ -1728,17 +1870,8 @@ static int wm8903_probe(struct platform_device *pdev)
                                ARRAY_SIZE(wm8903_snd_controls));
        wm8903_add_widgets(socdev->card->codec);
 
-       ret = snd_soc_init_card(socdev);
-       if (ret < 0) {
-               dev_err(&pdev->dev, "wm8903: failed to register card\n");
-               goto card_err;
-       }
-
        return ret;
 
-card_err:
-       snd_soc_free_pcms(socdev);
-       snd_soc_dapm_free(socdev);
 err:
        return ret;
 }