Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * wm8741.c  --  WM8741 ALSA SoC Audio driver
0004  *
0005  * Copyright 2010-1 Wolfson Microelectronics plc
0006  *
0007  * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/moduleparam.h>
0012 #include <linux/init.h>
0013 #include <linux/delay.h>
0014 #include <linux/pm.h>
0015 #include <linux/i2c.h>
0016 #include <linux/spi/spi.h>
0017 #include <linux/regmap.h>
0018 #include <linux/regulator/consumer.h>
0019 #include <linux/slab.h>
0020 #include <linux/of_device.h>
0021 #include <sound/core.h>
0022 #include <sound/pcm.h>
0023 #include <sound/pcm_params.h>
0024 #include <sound/soc.h>
0025 #include <sound/initval.h>
0026 #include <sound/tlv.h>
0027 
0028 #include "wm8741.h"
0029 
0030 #define WM8741_NUM_SUPPLIES 2
0031 static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
0032     "AVDD",
0033     "DVDD",
0034 };
0035 
0036 /* codec private data */
0037 struct wm8741_priv {
0038     struct wm8741_platform_data pdata;
0039     struct regmap *regmap;
0040     struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
0041     unsigned int sysclk;
0042     const struct snd_pcm_hw_constraint_list *sysclk_constraints;
0043 };
0044 
0045 static const struct reg_default wm8741_reg_defaults[] = {
0046     {  0, 0x0000 },     /* R0  - DACLLSB Attenuation */
0047     {  1, 0x0000 },     /* R1  - DACLMSB Attenuation */
0048     {  2, 0x0000 },     /* R2  - DACRLSB Attenuation */
0049     {  3, 0x0000 },     /* R3  - DACRMSB Attenuation */
0050     {  4, 0x0000 },     /* R4  - Volume Control */
0051     {  5, 0x000A },     /* R5  - Format Control */
0052     {  6, 0x0000 },     /* R6  - Filter Control */
0053     {  7, 0x0000 },     /* R7  - Mode Control 1 */
0054     {  8, 0x0002 },     /* R8  - Mode Control 2 */
0055     { 32, 0x0002 },     /* R32 - ADDITONAL_CONTROL_1 */
0056 };
0057 
0058 static int wm8741_reset(struct snd_soc_component *component)
0059 {
0060     return snd_soc_component_write(component, WM8741_RESET, 0);
0061 }
0062 
0063 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
0064 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
0065 
0066 static const struct snd_kcontrol_new wm8741_snd_controls_stereo[] = {
0067 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
0068          WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
0069 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
0070          WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
0071 };
0072 
0073 static const struct snd_kcontrol_new wm8741_snd_controls_mono_left[] = {
0074 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
0075          1, 255, 1, dac_tlv_fine),
0076 SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
0077          0, 511, 1, dac_tlv),
0078 };
0079 
0080 static const struct snd_kcontrol_new wm8741_snd_controls_mono_right[] = {
0081 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
0082         1, 255, 1, dac_tlv_fine),
0083 SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION,
0084         0, 511, 1, dac_tlv),
0085 };
0086 
0087 static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
0088 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
0089 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
0090 SND_SOC_DAPM_OUTPUT("VOUTLP"),
0091 SND_SOC_DAPM_OUTPUT("VOUTLN"),
0092 SND_SOC_DAPM_OUTPUT("VOUTRP"),
0093 SND_SOC_DAPM_OUTPUT("VOUTRN"),
0094 };
0095 
0096 static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
0097     { "VOUTLP", NULL, "DACL" },
0098     { "VOUTLN", NULL, "DACL" },
0099     { "VOUTRP", NULL, "DACR" },
0100     { "VOUTRN", NULL, "DACR" },
0101 };
0102 
0103 static const unsigned int rates_11289[] = {
0104     44100, 88200,
0105 };
0106 
0107 static const struct snd_pcm_hw_constraint_list constraints_11289 = {
0108     .count  = ARRAY_SIZE(rates_11289),
0109     .list   = rates_11289,
0110 };
0111 
0112 static const unsigned int rates_12288[] = {
0113     32000, 48000, 96000,
0114 };
0115 
0116 static const struct snd_pcm_hw_constraint_list constraints_12288 = {
0117     .count  = ARRAY_SIZE(rates_12288),
0118     .list   = rates_12288,
0119 };
0120 
0121 static const unsigned int rates_16384[] = {
0122     32000,
0123 };
0124 
0125 static const struct snd_pcm_hw_constraint_list constraints_16384 = {
0126     .count  = ARRAY_SIZE(rates_16384),
0127     .list   = rates_16384,
0128 };
0129 
0130 static const unsigned int rates_16934[] = {
0131     44100, 88200,
0132 };
0133 
0134 static const struct snd_pcm_hw_constraint_list constraints_16934 = {
0135     .count  = ARRAY_SIZE(rates_16934),
0136     .list   = rates_16934,
0137 };
0138 
0139 static const unsigned int rates_18432[] = {
0140     48000, 96000,
0141 };
0142 
0143 static const struct snd_pcm_hw_constraint_list constraints_18432 = {
0144     .count  = ARRAY_SIZE(rates_18432),
0145     .list   = rates_18432,
0146 };
0147 
0148 static const unsigned int rates_22579[] = {
0149     44100, 88200, 176400
0150 };
0151 
0152 static const struct snd_pcm_hw_constraint_list constraints_22579 = {
0153     .count  = ARRAY_SIZE(rates_22579),
0154     .list   = rates_22579,
0155 };
0156 
0157 static const unsigned int rates_24576[] = {
0158     32000, 48000, 96000, 192000
0159 };
0160 
0161 static const struct snd_pcm_hw_constraint_list constraints_24576 = {
0162     .count  = ARRAY_SIZE(rates_24576),
0163     .list   = rates_24576,
0164 };
0165 
0166 static const unsigned int rates_36864[] = {
0167     48000, 96000, 192000
0168 };
0169 
0170 static const struct snd_pcm_hw_constraint_list constraints_36864 = {
0171     .count  = ARRAY_SIZE(rates_36864),
0172     .list   = rates_36864,
0173 };
0174 
0175 static int wm8741_startup(struct snd_pcm_substream *substream,
0176               struct snd_soc_dai *dai)
0177 {
0178     struct snd_soc_component *component = dai->component;
0179     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0180 
0181     if (wm8741->sysclk)
0182         snd_pcm_hw_constraint_list(substream->runtime, 0,
0183                 SNDRV_PCM_HW_PARAM_RATE,
0184                 wm8741->sysclk_constraints);
0185 
0186     return 0;
0187 }
0188 
0189 static int wm8741_hw_params(struct snd_pcm_substream *substream,
0190                 struct snd_pcm_hw_params *params,
0191                 struct snd_soc_dai *dai)
0192 {
0193     struct snd_soc_component *component = dai->component;
0194     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0195     unsigned int iface, mode;
0196     int i;
0197 
0198     /* The set of sample rates that can be supported depends on the
0199      * MCLK supplied to the CODEC - enforce this.
0200      */
0201     if (!wm8741->sysclk) {
0202         dev_err(component->dev,
0203             "No MCLK configured, call set_sysclk() on init or in hw_params\n");
0204         return -EINVAL;
0205     }
0206 
0207     /* Find a supported LRCLK rate */
0208     for (i = 0; i < wm8741->sysclk_constraints->count; i++) {
0209         if (wm8741->sysclk_constraints->list[i] == params_rate(params))
0210             break;
0211     }
0212 
0213     if (i == wm8741->sysclk_constraints->count) {
0214         dev_err(component->dev, "LRCLK %d unsupported with MCLK %d\n",
0215             params_rate(params), wm8741->sysclk);
0216         return -EINVAL;
0217     }
0218 
0219     /* bit size */
0220     switch (params_width(params)) {
0221     case 16:
0222         iface = 0x0;
0223         break;
0224     case 20:
0225         iface = 0x1;
0226         break;
0227     case 24:
0228         iface = 0x2;
0229         break;
0230     case 32:
0231         iface = 0x3;
0232         break;
0233     default:
0234         dev_dbg(component->dev, "wm8741_hw_params:    Unsupported bit size param = %d",
0235             params_width(params));
0236         return -EINVAL;
0237     }
0238 
0239     /* oversampling rate */
0240     if (params_rate(params) > 96000)
0241         mode = 0x40;
0242     else if (params_rate(params) > 48000)
0243         mode = 0x20;
0244     else
0245         mode = 0x00;
0246 
0247     dev_dbg(component->dev, "wm8741_hw_params:    bit size param = %d, rate param = %d",
0248         params_width(params), params_rate(params));
0249 
0250     snd_soc_component_update_bits(component, WM8741_FORMAT_CONTROL, WM8741_IWL_MASK,
0251                 iface);
0252     snd_soc_component_update_bits(component, WM8741_MODE_CONTROL_1, WM8741_OSR_MASK,
0253                 mode);
0254 
0255     return 0;
0256 }
0257 
0258 static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
0259         int clk_id, unsigned int freq, int dir)
0260 {
0261     struct snd_soc_component *component = codec_dai->component;
0262     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0263 
0264     dev_dbg(component->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
0265 
0266     switch (freq) {
0267     case 0:
0268         wm8741->sysclk_constraints = NULL;
0269         break;
0270     case 11289600:
0271         wm8741->sysclk_constraints = &constraints_11289;
0272         break;
0273     case 12288000:
0274         wm8741->sysclk_constraints = &constraints_12288;
0275         break;
0276     case 16384000:
0277         wm8741->sysclk_constraints = &constraints_16384;
0278         break;
0279     case 16934400:
0280         wm8741->sysclk_constraints = &constraints_16934;
0281         break;
0282     case 18432000:
0283         wm8741->sysclk_constraints = &constraints_18432;
0284         break;
0285     case 22579200:
0286     case 33868800:
0287         wm8741->sysclk_constraints = &constraints_22579;
0288         break;
0289     case 24576000:
0290         wm8741->sysclk_constraints = &constraints_24576;
0291         break;
0292     case 36864000:
0293         wm8741->sysclk_constraints = &constraints_36864;
0294         break;
0295     default:
0296         return -EINVAL;
0297     }
0298 
0299     wm8741->sysclk = freq;
0300     return 0;
0301 }
0302 
0303 static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
0304         unsigned int fmt)
0305 {
0306     struct snd_soc_component *component = codec_dai->component;
0307     unsigned int iface;
0308 
0309     /* check master/slave audio interface */
0310     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
0311     case SND_SOC_DAIFMT_CBS_CFS:
0312         break;
0313     default:
0314         return -EINVAL;
0315     }
0316 
0317     /* interface format */
0318     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0319     case SND_SOC_DAIFMT_I2S:
0320         iface = 0x08;
0321         break;
0322     case SND_SOC_DAIFMT_RIGHT_J:
0323         iface = 0x00;
0324         break;
0325     case SND_SOC_DAIFMT_LEFT_J:
0326         iface = 0x04;
0327         break;
0328     case SND_SOC_DAIFMT_DSP_A:
0329         iface = 0x0C;
0330         break;
0331     case SND_SOC_DAIFMT_DSP_B:
0332         iface = 0x1C;
0333         break;
0334     default:
0335         return -EINVAL;
0336     }
0337 
0338     /* clock inversion */
0339     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0340     case SND_SOC_DAIFMT_NB_NF:
0341         break;
0342     case SND_SOC_DAIFMT_NB_IF:
0343         iface |= 0x10;
0344         break;
0345     case SND_SOC_DAIFMT_IB_NF:
0346         iface |= 0x20;
0347         break;
0348     case SND_SOC_DAIFMT_IB_IF:
0349         iface |= 0x30;
0350         break;
0351     default:
0352         return -EINVAL;
0353     }
0354 
0355 
0356     dev_dbg(component->dev, "wm8741_set_dai_fmt:    Format=%x, Clock Inv=%x\n",
0357                 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
0358                 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
0359 
0360     snd_soc_component_update_bits(component, WM8741_FORMAT_CONTROL,
0361                 WM8741_BCP_MASK | WM8741_LRP_MASK | WM8741_FMT_MASK,
0362                 iface);
0363 
0364     return 0;
0365 }
0366 
0367 static int wm8741_mute(struct snd_soc_dai *codec_dai, int mute, int direction)
0368 {
0369     struct snd_soc_component *component = codec_dai->component;
0370 
0371     snd_soc_component_update_bits(component, WM8741_VOLUME_CONTROL,
0372             WM8741_SOFT_MASK, !!mute << WM8741_SOFT_SHIFT);
0373     return 0;
0374 }
0375 
0376 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
0377             SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
0378             SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
0379             SNDRV_PCM_RATE_192000)
0380 
0381 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
0382             SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
0383 
0384 static const struct snd_soc_dai_ops wm8741_dai_ops = {
0385     .startup    = wm8741_startup,
0386     .hw_params  = wm8741_hw_params,
0387     .set_sysclk = wm8741_set_dai_sysclk,
0388     .set_fmt    = wm8741_set_dai_fmt,
0389     .mute_stream    = wm8741_mute,
0390     .no_capture_mute = 1,
0391 };
0392 
0393 static struct snd_soc_dai_driver wm8741_dai = {
0394     .name = "wm8741",
0395     .playback = {
0396         .stream_name = "Playback",
0397         .channels_min = 2,
0398         .channels_max = 2,
0399         .rates = WM8741_RATES,
0400         .formats = WM8741_FORMATS,
0401     },
0402     .ops = &wm8741_dai_ops,
0403 };
0404 
0405 #ifdef CONFIG_PM
0406 static int wm8741_resume(struct snd_soc_component *component)
0407 {
0408     snd_soc_component_cache_sync(component);
0409     return 0;
0410 }
0411 #else
0412 #define wm8741_resume NULL
0413 #endif
0414 
0415 static int wm8741_configure(struct snd_soc_component *component)
0416 {
0417     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0418 
0419     /* Configure differential mode */
0420     switch (wm8741->pdata.diff_mode) {
0421     case WM8741_DIFF_MODE_STEREO:
0422     case WM8741_DIFF_MODE_STEREO_REVERSED:
0423     case WM8741_DIFF_MODE_MONO_LEFT:
0424     case WM8741_DIFF_MODE_MONO_RIGHT:
0425         snd_soc_component_update_bits(component, WM8741_MODE_CONTROL_2,
0426                 WM8741_DIFF_MASK,
0427                 wm8741->pdata.diff_mode << WM8741_DIFF_SHIFT);
0428         break;
0429     default:
0430         return -EINVAL;
0431     }
0432 
0433     /* Change some default settings - latch VU */
0434     snd_soc_component_update_bits(component, WM8741_DACLLSB_ATTENUATION,
0435             WM8741_UPDATELL, WM8741_UPDATELL);
0436     snd_soc_component_update_bits(component, WM8741_DACLMSB_ATTENUATION,
0437             WM8741_UPDATELM, WM8741_UPDATELM);
0438     snd_soc_component_update_bits(component, WM8741_DACRLSB_ATTENUATION,
0439             WM8741_UPDATERL, WM8741_UPDATERL);
0440     snd_soc_component_update_bits(component, WM8741_DACRMSB_ATTENUATION,
0441             WM8741_UPDATERM, WM8741_UPDATERM);
0442 
0443     return 0;
0444 }
0445 
0446 static int wm8741_add_controls(struct snd_soc_component *component)
0447 {
0448     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0449 
0450     switch (wm8741->pdata.diff_mode) {
0451     case WM8741_DIFF_MODE_STEREO:
0452     case WM8741_DIFF_MODE_STEREO_REVERSED:
0453         snd_soc_add_component_controls(component,
0454                 wm8741_snd_controls_stereo,
0455                 ARRAY_SIZE(wm8741_snd_controls_stereo));
0456         break;
0457     case WM8741_DIFF_MODE_MONO_LEFT:
0458         snd_soc_add_component_controls(component,
0459                 wm8741_snd_controls_mono_left,
0460                 ARRAY_SIZE(wm8741_snd_controls_mono_left));
0461         break;
0462     case WM8741_DIFF_MODE_MONO_RIGHT:
0463         snd_soc_add_component_controls(component,
0464                 wm8741_snd_controls_mono_right,
0465                 ARRAY_SIZE(wm8741_snd_controls_mono_right));
0466         break;
0467     default:
0468         return -EINVAL;
0469     }
0470 
0471     return 0;
0472 }
0473 
0474 static int wm8741_probe(struct snd_soc_component *component)
0475 {
0476     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0477     int ret = 0;
0478 
0479     ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
0480                     wm8741->supplies);
0481     if (ret != 0) {
0482         dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
0483         goto err_get;
0484     }
0485 
0486     ret = wm8741_reset(component);
0487     if (ret < 0) {
0488         dev_err(component->dev, "Failed to issue reset\n");
0489         goto err_enable;
0490     }
0491 
0492     ret = wm8741_configure(component);
0493     if (ret < 0) {
0494         dev_err(component->dev, "Failed to change default settings\n");
0495         goto err_enable;
0496     }
0497 
0498     ret = wm8741_add_controls(component);
0499     if (ret < 0) {
0500         dev_err(component->dev, "Failed to add controls\n");
0501         goto err_enable;
0502     }
0503 
0504     dev_dbg(component->dev, "Successful registration\n");
0505     return ret;
0506 
0507 err_enable:
0508     regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
0509 err_get:
0510     return ret;
0511 }
0512 
0513 static void wm8741_remove(struct snd_soc_component *component)
0514 {
0515     struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
0516 
0517     regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
0518 }
0519 
0520 static const struct snd_soc_component_driver soc_component_dev_wm8741 = {
0521     .probe          = wm8741_probe,
0522     .remove         = wm8741_remove,
0523     .resume         = wm8741_resume,
0524     .dapm_widgets       = wm8741_dapm_widgets,
0525     .num_dapm_widgets   = ARRAY_SIZE(wm8741_dapm_widgets),
0526     .dapm_routes        = wm8741_dapm_routes,
0527     .num_dapm_routes    = ARRAY_SIZE(wm8741_dapm_routes),
0528     .idle_bias_on       = 1,
0529     .use_pmdown_time    = 1,
0530     .endianness     = 1,
0531 };
0532 
0533 static const struct of_device_id wm8741_of_match[] = {
0534     { .compatible = "wlf,wm8741", },
0535     { }
0536 };
0537 MODULE_DEVICE_TABLE(of, wm8741_of_match);
0538 
0539 static const struct regmap_config wm8741_regmap = {
0540     .reg_bits = 7,
0541     .val_bits = 9,
0542     .max_register = WM8741_MAX_REGISTER,
0543 
0544     .reg_defaults = wm8741_reg_defaults,
0545     .num_reg_defaults = ARRAY_SIZE(wm8741_reg_defaults),
0546     .cache_type = REGCACHE_RBTREE,
0547 };
0548 
0549 static int wm8741_set_pdata(struct device *dev, struct wm8741_priv *wm8741)
0550 {
0551     const struct wm8741_platform_data *pdata = dev_get_platdata(dev);
0552     u32 diff_mode;
0553 
0554     if (dev->of_node) {
0555         if (of_property_read_u32(dev->of_node, "diff-mode", &diff_mode)
0556                 >= 0)
0557             wm8741->pdata.diff_mode = diff_mode;
0558     } else {
0559         if (pdata != NULL)
0560             memcpy(&wm8741->pdata, pdata, sizeof(wm8741->pdata));
0561     }
0562 
0563     return 0;
0564 }
0565 
0566 #if IS_ENABLED(CONFIG_I2C)
0567 static int wm8741_i2c_probe(struct i2c_client *i2c)
0568 {
0569     struct wm8741_priv *wm8741;
0570     int ret, i;
0571 
0572     wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
0573                   GFP_KERNEL);
0574     if (wm8741 == NULL)
0575         return -ENOMEM;
0576 
0577     for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
0578         wm8741->supplies[i].supply = wm8741_supply_names[i];
0579 
0580     ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
0581                       wm8741->supplies);
0582     if (ret != 0) {
0583         dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
0584         return ret;
0585     }
0586 
0587     wm8741->regmap = devm_regmap_init_i2c(i2c, &wm8741_regmap);
0588     if (IS_ERR(wm8741->regmap)) {
0589         ret = PTR_ERR(wm8741->regmap);
0590         dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
0591         return ret;
0592     }
0593 
0594     ret = wm8741_set_pdata(&i2c->dev, wm8741);
0595     if (ret != 0) {
0596         dev_err(&i2c->dev, "Failed to set pdata: %d\n", ret);
0597         return ret;
0598     }
0599 
0600     i2c_set_clientdata(i2c, wm8741);
0601 
0602     ret = devm_snd_soc_register_component(&i2c->dev,
0603                      &soc_component_dev_wm8741, &wm8741_dai, 1);
0604 
0605     return ret;
0606 }
0607 
0608 static const struct i2c_device_id wm8741_i2c_id[] = {
0609     { "wm8741", 0 },
0610     { }
0611 };
0612 MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
0613 
0614 static struct i2c_driver wm8741_i2c_driver = {
0615     .driver = {
0616         .name = "wm8741",
0617         .of_match_table = wm8741_of_match,
0618     },
0619     .probe_new = wm8741_i2c_probe,
0620     .id_table = wm8741_i2c_id,
0621 };
0622 #endif
0623 
0624 #if defined(CONFIG_SPI_MASTER)
0625 static int wm8741_spi_probe(struct spi_device *spi)
0626 {
0627     struct wm8741_priv *wm8741;
0628     int ret, i;
0629 
0630     wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
0631                  GFP_KERNEL);
0632     if (wm8741 == NULL)
0633         return -ENOMEM;
0634 
0635     for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
0636         wm8741->supplies[i].supply = wm8741_supply_names[i];
0637 
0638     ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8741->supplies),
0639                       wm8741->supplies);
0640     if (ret != 0) {
0641         dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
0642         return ret;
0643     }
0644 
0645     wm8741->regmap = devm_regmap_init_spi(spi, &wm8741_regmap);
0646     if (IS_ERR(wm8741->regmap)) {
0647         ret = PTR_ERR(wm8741->regmap);
0648         dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
0649         return ret;
0650     }
0651 
0652     ret = wm8741_set_pdata(&spi->dev, wm8741);
0653     if (ret != 0) {
0654         dev_err(&spi->dev, "Failed to set pdata: %d\n", ret);
0655         return ret;
0656     }
0657 
0658     spi_set_drvdata(spi, wm8741);
0659 
0660     ret = devm_snd_soc_register_component(&spi->dev,
0661             &soc_component_dev_wm8741, &wm8741_dai, 1);
0662     return ret;
0663 }
0664 
0665 static struct spi_driver wm8741_spi_driver = {
0666     .driver = {
0667         .name   = "wm8741",
0668         .of_match_table = wm8741_of_match,
0669     },
0670     .probe      = wm8741_spi_probe,
0671 };
0672 #endif /* CONFIG_SPI_MASTER */
0673 
0674 static int __init wm8741_modinit(void)
0675 {
0676     int ret = 0;
0677 
0678 #if IS_ENABLED(CONFIG_I2C)
0679     ret = i2c_add_driver(&wm8741_i2c_driver);
0680     if (ret != 0)
0681         pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
0682 #endif
0683 #if defined(CONFIG_SPI_MASTER)
0684     ret = spi_register_driver(&wm8741_spi_driver);
0685     if (ret != 0) {
0686         printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
0687                ret);
0688     }
0689 #endif
0690 
0691     return ret;
0692 }
0693 module_init(wm8741_modinit);
0694 
0695 static void __exit wm8741_exit(void)
0696 {
0697 #if defined(CONFIG_SPI_MASTER)
0698     spi_unregister_driver(&wm8741_spi_driver);
0699 #endif
0700 #if IS_ENABLED(CONFIG_I2C)
0701     i2c_del_driver(&wm8741_i2c_driver);
0702 #endif
0703 }
0704 module_exit(wm8741_exit);
0705 
0706 MODULE_DESCRIPTION("ASoC WM8741 driver");
0707 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
0708 MODULE_LICENSE("GPL");