ASoC: WM8903: Remove conditionals checking pdata != NULL
[deliverable/linux.git] / sound / soc / codecs / wm8903.c
index 0b12a5525c15ebdd03e4d8fbad32f5336216a5b4..b114468d6453bb0f06ccdbac7b4c38c227402ff0 100644 (file)
@@ -114,6 +114,7 @@ static const struct reg_default wm8903_reg_defaults[] = {
 };
 
 struct wm8903_priv {
+       struct wm8903_platform_data *pdata;
        struct snd_soc_codec *codec;
        struct regmap *regmap;
 
@@ -244,11 +245,6 @@ static bool wm8903_volatile_register(struct device *dev, unsigned int reg)
        }
 }
 
-static void wm8903_reset(struct snd_soc_codec *codec)
-{
-       snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
-}
-
 static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
                           struct snd_kcontrol *kcontrol, int event)
 {
@@ -1839,14 +1835,14 @@ static struct gpio_chip wm8903_template_chip = {
 static void wm8903_init_gpio(struct snd_soc_codec *codec)
 {
        struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
-       struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
+       struct wm8903_platform_data *pdata = wm8903->pdata;
        int ret;
 
        wm8903->gpio_chip = wm8903_template_chip;
        wm8903->gpio_chip.ngpio = WM8903_NUM_GPIO;
        wm8903->gpio_chip.dev = codec->dev;
 
-       if (pdata && pdata->gpio_base)
+       if (pdata->gpio_base)
                wm8903->gpio_chip.base = pdata->gpio_base;
        else
                wm8903->gpio_chip.base = -1;
@@ -1877,11 +1873,12 @@ static void wm8903_free_gpio(struct snd_soc_codec *codec)
 
 static int wm8903_probe(struct snd_soc_codec *codec)
 {
-       struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
        struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
+       struct wm8903_platform_data *pdata = wm8903->pdata;
        int ret, i;
        int trigger, irq_pol;
        u16 val;
+       bool mic_gpio = false;
 
        wm8903->codec = codec;
        codec->control_data = wm8903->regmap;
@@ -1892,64 +1889,49 @@ static int wm8903_probe(struct snd_soc_codec *codec)
                return ret;
        }
 
-       val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
-       if (val != 0x8903) {
-               dev_err(codec->dev,
-                       "Device with ID register %x is not a WM8903\n", val);
-               return -ENODEV;
-       }
+       /* Set up GPIOs, detect if any are MIC detect outputs */
+       for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
+               if ((!pdata->gpio_cfg[i]) ||
+                   (pdata->gpio_cfg[i] > WM8903_GPIO_CONFIG_ZERO))
+                       continue;
 
-       val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
-       dev_info(codec->dev, "WM8903 revision %c\n",
-                (val & WM8903_CHIP_REV_MASK) + 'A');
-
-       wm8903_reset(codec);
-
-       /* Set up GPIOs and microphone detection */
-       if (pdata) {
-               bool mic_gpio = false;
-
-               for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
-                       if (pdata->gpio_cfg[i] > 0x7fff)
-                               continue;
+               snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
+                               pdata->gpio_cfg[i] & 0x7fff);
 
-                       snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
-                                     pdata->gpio_cfg[i] & 0x7fff);
+               val = (pdata->gpio_cfg[i] & WM8903_GP1_FN_MASK)
+                       >> WM8903_GP1_FN_SHIFT;
 
-                       val = (pdata->gpio_cfg[i] & WM8903_GP1_FN_MASK)
-                               >> WM8903_GP1_FN_SHIFT;
-
-                       switch (val) {
-                       case WM8903_GPn_FN_MICBIAS_CURRENT_DETECT:
-                       case WM8903_GPn_FN_MICBIAS_SHORT_DETECT:
-                               mic_gpio = true;
-                               break;
-                       default:
-                               break;
-                       }
+               switch (val) {
+               case WM8903_GPn_FN_MICBIAS_CURRENT_DETECT:
+               case WM8903_GPn_FN_MICBIAS_SHORT_DETECT:
+                       mic_gpio = true;
+                       break;
+               default:
+                       break;
                }
+       }
 
-               snd_soc_write(codec, WM8903_MIC_BIAS_CONTROL_0,
-                             pdata->micdet_cfg);
+       /* Set up microphone detection */
+       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);
+       /* 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);
 
-               /* If microphone detection is enabled by pdata but
-                * detected via IRQ then interrupts can be lost before
-                * the machine driver has set up microphone detection
-                * IRQs as the IRQs are clear on read.  The detection
-                * will be enabled when the machine driver configures.
-                */
-               WARN_ON(!mic_gpio && (pdata->micdet_cfg & WM8903_MICDET_ENA));
+       /* If microphone detection is enabled by pdata but
+           * detected via IRQ then interrupts can be lost before
+           * the machine driver has set up microphone detection
+           * IRQs as the IRQs are clear on read.  The detection
+           * will be enabled when the machine driver configures.
+           */
+       WARN_ON(!mic_gpio && (pdata->micdet_cfg & WM8903_MICDET_ENA));
+
+       wm8903->mic_delay = pdata->micdet_delay;
 
-               wm8903->mic_delay = pdata->micdet_delay;
-       }
-       
        if (wm8903->irq) {
-               if (pdata && pdata->irq_active_low) {
+               if (pdata->irq_active_low) {
                        trigger = IRQF_TRIGGER_LOW;
                        irq_pol = WM8903_IRQ_POL;
                } else {
@@ -2057,7 +2039,9 @@ static const struct regmap_config wm8903_regmap = {
 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;
+       unsigned int val;
        int ret;
 
        wm8903 = devm_kzalloc(&i2c->dev,  sizeof(struct wm8903_priv),
@@ -2076,6 +2060,41 @@ static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
        i2c_set_clientdata(i2c, wm8903);
        wm8903->irq = i2c->irq;
 
+       /* If no platform data was supplied, create storage for defaults */
+       if (pdata) {
+               wm8903->pdata = pdata;
+       } else {
+               wm8903->pdata = devm_kzalloc(&i2c->dev,
+                                       sizeof(struct wm8903_platform_data),
+                                       GFP_KERNEL);
+               if (wm8903->pdata == NULL) {
+                       dev_err(&i2c->dev, "Failed to allocate pdata\n");
+                       return -ENOMEM;
+               }
+       }
+
+       ret = regmap_read(wm8903->regmap, WM8903_SW_RESET_AND_ID, &val);
+       if (ret != 0) {
+               dev_err(&i2c->dev, "Failed to read chip ID: %d\n", ret);
+               goto err;
+       }
+       if (val != 0x8903) {
+               dev_err(&i2c->dev, "Device with ID %x is not a WM8903\n", val);
+               ret = -ENODEV;
+               goto err;
+       }
+
+       ret = regmap_read(wm8903->regmap, WM8903_REVISION_NUMBER, &val);
+       if (ret != 0) {
+               dev_err(&i2c->dev, "Failed to read chip revision: %d\n", ret);
+               goto err;
+       }
+       dev_info(&i2c->dev, "WM8903 revision %c\n",
+                (val & WM8903_CHIP_REV_MASK) + 'A');
+
+       /* Reset the device */
+       regmap_write(wm8903->regmap, WM8903_SW_RESET_AND_ID, 0x8903);
+
        ret = snd_soc_register_codec(&i2c->dev,
                        &soc_codec_dev_wm8903, &wm8903_dai, 1);
        if (ret != 0)
This page took 0.038507 seconds and 5 git commands to generate.