Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * wm8776.c  --  WM8776 ALSA SoC Audio driver
0004  *
0005  * Copyright 2009-12 Wolfson Microelectronics plc
0006  *
0007  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008  *
0009  * TODO: Input ALC/limiter support
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/moduleparam.h>
0014 #include <linux/init.h>
0015 #include <linux/delay.h>
0016 #include <linux/pm.h>
0017 #include <linux/i2c.h>
0018 #include <linux/of_device.h>
0019 #include <linux/regmap.h>
0020 #include <linux/spi/spi.h>
0021 #include <linux/slab.h>
0022 #include <sound/core.h>
0023 #include <sound/pcm.h>
0024 #include <sound/pcm_params.h>
0025 #include <sound/soc.h>
0026 #include <sound/initval.h>
0027 #include <sound/tlv.h>
0028 
0029 #include "wm8776.h"
0030 
0031 enum wm8776_chip_type {
0032     WM8775 = 1,
0033     WM8776,
0034 };
0035 
0036 /* codec private data */
0037 struct wm8776_priv {
0038     struct regmap *regmap;
0039     int sysclk[2];
0040 };
0041 
0042 static const struct reg_default wm8776_reg_defaults[] = {
0043     {  0, 0x79 },
0044     {  1, 0x79 },
0045     {  2, 0x79 },
0046     {  3, 0xff },
0047     {  4, 0xff },
0048     {  5, 0xff },
0049     {  6, 0x00 },
0050     {  7, 0x90 },
0051     {  8, 0x00 },
0052     {  9, 0x00 },
0053     { 10, 0x22 },
0054     { 11, 0x22 },
0055     { 12, 0x22 },
0056     { 13, 0x08 },
0057     { 14, 0xcf },
0058     { 15, 0xcf },
0059     { 16, 0x7b },
0060     { 17, 0x00 },
0061     { 18, 0x32 },
0062     { 19, 0x00 },
0063     { 20, 0xa6 },
0064     { 21, 0x01 },
0065     { 22, 0x01 },
0066 };
0067 
0068 static bool wm8776_volatile(struct device *dev, unsigned int reg)
0069 {
0070     switch (reg) {
0071     case WM8776_RESET:
0072         return true;
0073     default:
0074         return false;
0075     }
0076 }
0077 
0078 static int wm8776_reset(struct snd_soc_component *component)
0079 {
0080     return snd_soc_component_write(component, WM8776_RESET, 0);
0081 }
0082 
0083 static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
0084 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
0085 static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
0086 
0087 static const struct snd_kcontrol_new wm8776_snd_controls[] = {
0088 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
0089          0, 127, 0, hp_tlv),
0090 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
0091          0, 255, 0, dac_tlv),
0092 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
0093 
0094 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
0095 
0096 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
0097          0, 255, 0, adc_tlv),
0098 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
0099 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
0100 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
0101 };
0102 
0103 static const struct snd_kcontrol_new inmix_controls[] = {
0104 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
0105 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
0106 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
0107 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
0108 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
0109 };
0110 
0111 static const struct snd_kcontrol_new outmix_controls[] = {
0112 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
0113 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
0114 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
0115 };
0116 
0117 static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
0118 SND_SOC_DAPM_INPUT("AUX"),
0119 
0120 SND_SOC_DAPM_INPUT("AIN1"),
0121 SND_SOC_DAPM_INPUT("AIN2"),
0122 SND_SOC_DAPM_INPUT("AIN3"),
0123 SND_SOC_DAPM_INPUT("AIN4"),
0124 SND_SOC_DAPM_INPUT("AIN5"),
0125 
0126 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
0127            inmix_controls, ARRAY_SIZE(inmix_controls)),
0128 
0129 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
0130 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
0131 
0132 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
0133            outmix_controls, ARRAY_SIZE(outmix_controls)),
0134 
0135 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
0136 
0137 SND_SOC_DAPM_OUTPUT("VOUT"),
0138 
0139 SND_SOC_DAPM_OUTPUT("HPOUTL"),
0140 SND_SOC_DAPM_OUTPUT("HPOUTR"),
0141 };
0142 
0143 static const struct snd_soc_dapm_route routes[] = {
0144     { "Input Mixer", "AIN1 Switch", "AIN1" },
0145     { "Input Mixer", "AIN2 Switch", "AIN2" },
0146     { "Input Mixer", "AIN3 Switch", "AIN3" },
0147     { "Input Mixer", "AIN4 Switch", "AIN4" },
0148     { "Input Mixer", "AIN5 Switch", "AIN5" },
0149 
0150     { "ADC", NULL, "Input Mixer" },
0151 
0152     { "Output Mixer", "DAC Switch", "DAC" },
0153     { "Output Mixer", "AUX Switch", "AUX" },
0154     { "Output Mixer", "Bypass Switch", "Input Mixer" },
0155 
0156     { "VOUT", NULL, "Output Mixer" },
0157 
0158     { "Headphone PGA", NULL, "Output Mixer" },
0159 
0160     { "HPOUTL", NULL, "Headphone PGA" },
0161     { "HPOUTR", NULL, "Headphone PGA" },
0162 };
0163 
0164 static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0165 {
0166     struct snd_soc_component *component = dai->component;
0167     int reg, iface, master;
0168 
0169     switch (dai->driver->id) {
0170     case WM8776_DAI_DAC:
0171         reg = WM8776_DACIFCTRL;
0172         master = 0x80;
0173         break;
0174     case WM8776_DAI_ADC:
0175         reg = WM8776_ADCIFCTRL;
0176         master = 0x100;
0177         break;
0178     default:
0179         return -EINVAL;
0180     }
0181 
0182     iface = 0;
0183 
0184     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
0185     case SND_SOC_DAIFMT_CBM_CFM:
0186         break;
0187     case SND_SOC_DAIFMT_CBS_CFS:
0188         master = 0;
0189         break;
0190     default:
0191         return -EINVAL;
0192     }
0193 
0194     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
0195     case SND_SOC_DAIFMT_I2S:
0196         iface |= 0x0002;
0197         break;
0198     case SND_SOC_DAIFMT_RIGHT_J:
0199         break;
0200     case SND_SOC_DAIFMT_LEFT_J:
0201         iface |= 0x0001;
0202         break;
0203     default:
0204         return -EINVAL;
0205     }
0206 
0207     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
0208     case SND_SOC_DAIFMT_NB_NF:
0209         break;
0210     case SND_SOC_DAIFMT_IB_IF:
0211         iface |= 0x00c;
0212         break;
0213     case SND_SOC_DAIFMT_IB_NF:
0214         iface |= 0x008;
0215         break;
0216     case SND_SOC_DAIFMT_NB_IF:
0217         iface |= 0x004;
0218         break;
0219     default:
0220         return -EINVAL;
0221     }
0222 
0223     /* Finally, write out the values */
0224     snd_soc_component_update_bits(component, reg, 0xf, iface);
0225     snd_soc_component_update_bits(component, WM8776_MSTRCTRL, 0x180, master);
0226 
0227     return 0;
0228 }
0229 
0230 static int mclk_ratios[] = {
0231     128,
0232     192,
0233     256,
0234     384,
0235     512,
0236     768,
0237 };
0238 
0239 static int wm8776_hw_params(struct snd_pcm_substream *substream,
0240                 struct snd_pcm_hw_params *params,
0241                 struct snd_soc_dai *dai)
0242 {
0243     struct snd_soc_component *component = dai->component;
0244     struct wm8776_priv *wm8776 = snd_soc_component_get_drvdata(component);
0245     int iface_reg, iface;
0246     int ratio_shift, master;
0247     int i;
0248 
0249     switch (dai->driver->id) {
0250     case WM8776_DAI_DAC:
0251         iface_reg = WM8776_DACIFCTRL;
0252         master = 0x80;
0253         ratio_shift = 4;
0254         break;
0255     case WM8776_DAI_ADC:
0256         iface_reg = WM8776_ADCIFCTRL;
0257         master = 0x100;
0258         ratio_shift = 0;
0259         break;
0260     default:
0261         return -EINVAL;
0262     }
0263 
0264     /* Set word length */
0265     switch (params_width(params)) {
0266     case 16:
0267         iface = 0;
0268         break;
0269     case 20:
0270         iface = 0x10;
0271         break;
0272     case 24:
0273         iface = 0x20;
0274         break;
0275     case 32:
0276         iface = 0x30;
0277         break;
0278     default:
0279         dev_err(component->dev, "Unsupported sample size: %i\n",
0280             params_width(params));
0281         return -EINVAL;
0282     }
0283 
0284     /* Only need to set MCLK/LRCLK ratio if we're master */
0285     if (snd_soc_component_read(component, WM8776_MSTRCTRL) & master) {
0286         for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
0287             if (wm8776->sysclk[dai->driver->id] / params_rate(params)
0288                 == mclk_ratios[i])
0289                 break;
0290         }
0291 
0292         if (i == ARRAY_SIZE(mclk_ratios)) {
0293             dev_err(component->dev,
0294                 "Unable to configure MCLK ratio %d/%d\n",
0295                 wm8776->sysclk[dai->driver->id], params_rate(params));
0296             return -EINVAL;
0297         }
0298 
0299         dev_dbg(component->dev, "MCLK is %dfs\n", mclk_ratios[i]);
0300 
0301         snd_soc_component_update_bits(component, WM8776_MSTRCTRL,
0302                     0x7 << ratio_shift, i << ratio_shift);
0303     } else {
0304         dev_dbg(component->dev, "DAI in slave mode\n");
0305     }
0306 
0307     snd_soc_component_update_bits(component, iface_reg, 0x30, iface);
0308 
0309     return 0;
0310 }
0311 
0312 static int wm8776_mute(struct snd_soc_dai *dai, int mute, int direction)
0313 {
0314     struct snd_soc_component *component = dai->component;
0315 
0316     return snd_soc_component_write(component, WM8776_DACMUTE, !!mute);
0317 }
0318 
0319 static int wm8776_set_sysclk(struct snd_soc_dai *dai,
0320                  int clk_id, unsigned int freq, int dir)
0321 {
0322     struct snd_soc_component *component = dai->component;
0323     struct wm8776_priv *wm8776 = snd_soc_component_get_drvdata(component);
0324 
0325     if (WARN_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk)))
0326         return -EINVAL;
0327 
0328     wm8776->sysclk[dai->driver->id] = freq;
0329 
0330     return 0;
0331 }
0332 
0333 static int wm8776_set_bias_level(struct snd_soc_component *component,
0334                  enum snd_soc_bias_level level)
0335 {
0336     struct wm8776_priv *wm8776 = snd_soc_component_get_drvdata(component);
0337 
0338     switch (level) {
0339     case SND_SOC_BIAS_ON:
0340         break;
0341     case SND_SOC_BIAS_PREPARE:
0342         break;
0343     case SND_SOC_BIAS_STANDBY:
0344         if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
0345             regcache_sync(wm8776->regmap);
0346 
0347             /* Disable the global powerdown; DAPM does the rest */
0348             snd_soc_component_update_bits(component, WM8776_PWRDOWN, 1, 0);
0349         }
0350 
0351         break;
0352     case SND_SOC_BIAS_OFF:
0353         snd_soc_component_update_bits(component, WM8776_PWRDOWN, 1, 1);
0354         break;
0355     }
0356 
0357     return 0;
0358 }
0359 
0360 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
0361             SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
0362 
0363 static const struct snd_soc_dai_ops wm8776_dac_ops = {
0364     .mute_stream    = wm8776_mute,
0365     .hw_params      = wm8776_hw_params,
0366     .set_fmt        = wm8776_set_fmt,
0367     .set_sysclk     = wm8776_set_sysclk,
0368     .no_capture_mute = 1,
0369 };
0370 
0371 static const struct snd_soc_dai_ops wm8776_adc_ops = {
0372     .hw_params      = wm8776_hw_params,
0373     .set_fmt        = wm8776_set_fmt,
0374     .set_sysclk     = wm8776_set_sysclk,
0375 };
0376 
0377 static struct snd_soc_dai_driver wm8776_dai[] = {
0378     {
0379         .name = "wm8776-hifi-playback",
0380         .id = WM8776_DAI_DAC,
0381         .playback = {
0382             .stream_name = "Playback",
0383             .channels_min = 2,
0384             .channels_max = 2,
0385             .rates = SNDRV_PCM_RATE_CONTINUOUS,
0386             .rate_min = 32000,
0387             .rate_max = 192000,
0388             .formats = WM8776_FORMATS,
0389         },
0390         .ops = &wm8776_dac_ops,
0391     },
0392     {
0393         .name = "wm8776-hifi-capture",
0394         .id = WM8776_DAI_ADC,
0395         .capture = {
0396             .stream_name = "Capture",
0397             .channels_min = 2,
0398             .channels_max = 2,
0399             .rates = SNDRV_PCM_RATE_CONTINUOUS,
0400             .rate_min = 32000,
0401             .rate_max = 96000,
0402             .formats = WM8776_FORMATS,
0403         },
0404         .ops = &wm8776_adc_ops,
0405     },
0406 };
0407 
0408 static int wm8776_probe(struct snd_soc_component *component)
0409 {
0410     int ret = 0;
0411 
0412     ret = wm8776_reset(component);
0413     if (ret < 0) {
0414         dev_err(component->dev, "Failed to issue reset: %d\n", ret);
0415         return ret;
0416     }
0417 
0418     /* Latch the update bits; right channel only since we always
0419      * update both. */
0420     snd_soc_component_update_bits(component, WM8776_HPRVOL, 0x100, 0x100);
0421     snd_soc_component_update_bits(component, WM8776_DACRVOL, 0x100, 0x100);
0422 
0423     return ret;
0424 }
0425 
0426 static const struct snd_soc_component_driver soc_component_dev_wm8776 = {
0427     .probe          = wm8776_probe,
0428     .set_bias_level     = wm8776_set_bias_level,
0429     .controls       = wm8776_snd_controls,
0430     .num_controls       = ARRAY_SIZE(wm8776_snd_controls),
0431     .dapm_widgets       = wm8776_dapm_widgets,
0432     .num_dapm_widgets   = ARRAY_SIZE(wm8776_dapm_widgets),
0433     .dapm_routes        = routes,
0434     .num_dapm_routes    = ARRAY_SIZE(routes),
0435     .suspend_bias_off   = 1,
0436     .idle_bias_on       = 1,
0437     .use_pmdown_time    = 1,
0438     .endianness     = 1,
0439 };
0440 
0441 static const struct of_device_id wm8776_of_match[] = {
0442     { .compatible = "wlf,wm8776", },
0443     { }
0444 };
0445 MODULE_DEVICE_TABLE(of, wm8776_of_match);
0446 
0447 static const struct regmap_config wm8776_regmap = {
0448     .reg_bits = 7,
0449     .val_bits = 9,
0450     .max_register = WM8776_RESET,
0451 
0452     .reg_defaults = wm8776_reg_defaults,
0453     .num_reg_defaults = ARRAY_SIZE(wm8776_reg_defaults),
0454     .cache_type = REGCACHE_RBTREE,
0455 
0456     .volatile_reg = wm8776_volatile,
0457 };
0458 
0459 #if defined(CONFIG_SPI_MASTER)
0460 static int wm8776_spi_probe(struct spi_device *spi)
0461 {
0462     struct wm8776_priv *wm8776;
0463     int ret;
0464 
0465     wm8776 = devm_kzalloc(&spi->dev, sizeof(struct wm8776_priv),
0466                   GFP_KERNEL);
0467     if (wm8776 == NULL)
0468         return -ENOMEM;
0469 
0470     wm8776->regmap = devm_regmap_init_spi(spi, &wm8776_regmap);
0471     if (IS_ERR(wm8776->regmap))
0472         return PTR_ERR(wm8776->regmap);
0473 
0474     spi_set_drvdata(spi, wm8776);
0475 
0476     ret = devm_snd_soc_register_component(&spi->dev,
0477             &soc_component_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
0478 
0479     return ret;
0480 }
0481 
0482 static struct spi_driver wm8776_spi_driver = {
0483     .driver = {
0484         .name   = "wm8776",
0485         .of_match_table = wm8776_of_match,
0486     },
0487     .probe      = wm8776_spi_probe,
0488 };
0489 #endif /* CONFIG_SPI_MASTER */
0490 
0491 #if IS_ENABLED(CONFIG_I2C)
0492 static int wm8776_i2c_probe(struct i2c_client *i2c)
0493 {
0494     struct wm8776_priv *wm8776;
0495     int ret;
0496 
0497     wm8776 = devm_kzalloc(&i2c->dev, sizeof(struct wm8776_priv),
0498                   GFP_KERNEL);
0499     if (wm8776 == NULL)
0500         return -ENOMEM;
0501 
0502     wm8776->regmap = devm_regmap_init_i2c(i2c, &wm8776_regmap);
0503     if (IS_ERR(wm8776->regmap))
0504         return PTR_ERR(wm8776->regmap);
0505 
0506     i2c_set_clientdata(i2c, wm8776);
0507 
0508     ret = devm_snd_soc_register_component(&i2c->dev,
0509             &soc_component_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
0510 
0511     return ret;
0512 }
0513 
0514 static const struct i2c_device_id wm8776_i2c_id[] = {
0515     { "wm8775", WM8775 },
0516     { "wm8776", WM8776 },
0517     { }
0518 };
0519 MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
0520 
0521 static struct i2c_driver wm8776_i2c_driver = {
0522     .driver = {
0523         .name = "wm8776",
0524         .of_match_table = wm8776_of_match,
0525     },
0526     .probe_new = wm8776_i2c_probe,
0527     .id_table = wm8776_i2c_id,
0528 };
0529 #endif
0530 
0531 static int __init wm8776_modinit(void)
0532 {
0533     int ret = 0;
0534 #if IS_ENABLED(CONFIG_I2C)
0535     ret = i2c_add_driver(&wm8776_i2c_driver);
0536     if (ret != 0) {
0537         printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
0538                ret);
0539     }
0540 #endif
0541 #if defined(CONFIG_SPI_MASTER)
0542     ret = spi_register_driver(&wm8776_spi_driver);
0543     if (ret != 0) {
0544         printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
0545                ret);
0546     }
0547 #endif
0548     return ret;
0549 }
0550 module_init(wm8776_modinit);
0551 
0552 static void __exit wm8776_exit(void)
0553 {
0554 #if IS_ENABLED(CONFIG_I2C)
0555     i2c_del_driver(&wm8776_i2c_driver);
0556 #endif
0557 #if defined(CONFIG_SPI_MASTER)
0558     spi_unregister_driver(&wm8776_spi_driver);
0559 #endif
0560 }
0561 module_exit(wm8776_exit);
0562 
0563 MODULE_DESCRIPTION("ASoC WM8776 driver");
0564 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
0565 MODULE_LICENSE("GPL");