15 #include <linux/module.h>
20 #include <linux/i2c.h>
24 #include <linux/slab.h>
35 #define WM8995_NUM_SUPPLIES 8
396 #define WM8995_REGULATOR_EVENT(n) \
397 static int wm8995_regulator_event_##n(struct notifier_block *nb, \
398 unsigned long event, void *data) \
400 struct wm8995_priv *wm8995 = container_of(nb, struct wm8995_priv, \
402 if (event & REGULATOR_EVENT_DISABLE) { \
403 regcache_mark_dirty(wm8995->regmap); \
422 static
const char *in1l_text[] = {
423 "Differential",
"Single-ended IN1LN",
"Single-ended IN1LP"
429 static const char *in1r_text[] = {
430 "Differential",
"Single-ended IN1RN",
"Single-ended IN1RP"
436 static const char *dmic_src_text[] = {
437 "DMICDAT1",
"DMICDAT2",
"DMICDAT3"
467 4, 3, 0, in1l_boost_tlv),
472 SOC_ENUM(
"DMIC1 SRC", dmic_src1_enum),
473 SOC_ENUM(
"DMIC2 SRC", dmic_src2_enum),
476 24, 0, sidetone_tlv),
478 24, 0, sidetone_tlv),
498 dev_dbg(codec->
dev,
"Class W source AIF2DAC\n");
502 dev_dbg(codec->
dev,
"Class W source AIF1DAC2\n");
506 dev_dbg(codec->
dev,
"Class W source AIF1DAC1\n");
510 dev_dbg(codec->
dev,
"DAC mixer setting: %x\n", reg);
517 dev_dbg(codec->
dev,
"Left and right DAC mixers different\n");
549 static int wm8995_put_class_w(
struct snd_kcontrol *kcontrol,
559 wm8995_update_class_w(codec);
570 wm8995 = snd_soc_codec_get_drvdata(codec);
598 unsigned int reg,
unsigned int val,
unsigned int mask)
602 dev_dbg(codec->
dev,
"%s: reg = %#x, val = %#x, mask = %#x\n",
603 __func__, reg, val, mask);
609 if ((val & mask) == mask)
613 dev_err(codec->
dev,
"Timed out waiting for DC Servo\n");
679 static int configure_aif_clock(
struct snd_soc_codec *codec,
int aif)
686 wm8995 = snd_soc_codec_get_drvdata(codec);
693 switch (wm8995->
sysclk[aif]) {
695 rate = wm8995->
mclk[0];
699 rate = wm8995->
mclk[1];
703 rate = wm8995->
fll[0].out;
707 rate = wm8995->
fll[1].out;
713 if (rate >= 13500000) {
717 dev_dbg(codec->
dev,
"Dividing AIF%d clock to %dHz\n",
734 wm8995 = snd_soc_codec_get_drvdata(codec);
737 configure_aif_clock(codec, 0);
738 configure_aif_clock(codec, 1);
775 return configure_clock(codec);
778 configure_clock(codec);
785 static const char *sidetone_text[] = {
786 "ADC/DMIC1",
"DMIC2",
789 static const struct soc_enum sidetone1_enum =
795 static const struct soc_enum sidetone2_enum =
887 static const char *adc_mux_text[] = {
892 static const struct soc_enum adc_enum =
901 static const char *spk_src_text[] = {
902 "DAC1L",
"DAC1R",
"DAC2L",
"DAC2R"
1037 {
"CLK_SYS",
NULL,
"AIF1CLK", check_clk_sys },
1038 {
"CLK_SYS",
NULL,
"AIF2CLK", check_clk_sys },
1040 {
"DSP1CLK",
NULL,
"CLK_SYS" },
1041 {
"DSP2CLK",
NULL,
"CLK_SYS" },
1042 {
"SYSDSPCLK",
NULL,
"CLK_SYS" },
1044 {
"AIF1ADC1L",
NULL,
"AIF1CLK" },
1045 {
"AIF1ADC1L",
NULL,
"DSP1CLK" },
1046 {
"AIF1ADC1R",
NULL,
"AIF1CLK" },
1047 {
"AIF1ADC1R",
NULL,
"DSP1CLK" },
1048 {
"AIF1ADC1R",
NULL,
"SYSDSPCLK" },
1050 {
"AIF1ADC2L",
NULL,
"AIF1CLK" },
1051 {
"AIF1ADC2L",
NULL,
"DSP1CLK" },
1052 {
"AIF1ADC2R",
NULL,
"AIF1CLK" },
1053 {
"AIF1ADC2R",
NULL,
"DSP1CLK" },
1054 {
"AIF1ADC2R",
NULL,
"SYSDSPCLK" },
1056 {
"DMIC1L",
NULL,
"DMIC1DAT" },
1057 {
"DMIC1L",
NULL,
"CLK_SYS" },
1058 {
"DMIC1R",
NULL,
"DMIC1DAT" },
1059 {
"DMIC1R",
NULL,
"CLK_SYS" },
1060 {
"DMIC2L",
NULL,
"DMIC2DAT" },
1061 {
"DMIC2L",
NULL,
"CLK_SYS" },
1062 {
"DMIC2R",
NULL,
"DMIC2DAT" },
1063 {
"DMIC2R",
NULL,
"CLK_SYS" },
1065 {
"ADCL",
NULL,
"AIF1CLK" },
1066 {
"ADCL",
NULL,
"DSP1CLK" },
1067 {
"ADCL",
NULL,
"SYSDSPCLK" },
1069 {
"ADCR",
NULL,
"AIF1CLK" },
1070 {
"ADCR",
NULL,
"DSP1CLK" },
1071 {
"ADCR",
NULL,
"SYSDSPCLK" },
1073 {
"IN1L PGA",
"IN1L Switch",
"IN1L" },
1074 {
"IN1R PGA",
"IN1R Switch",
"IN1R" },
1075 {
"IN1L PGA",
NULL,
"LDO2" },
1076 {
"IN1R PGA",
NULL,
"LDO2" },
1078 {
"ADCL",
NULL,
"IN1L PGA" },
1079 {
"ADCR",
NULL,
"IN1R PGA" },
1081 {
"ADCL Mux",
"ADC",
"ADCL" },
1082 {
"ADCL Mux",
"DMIC",
"DMIC1L" },
1083 {
"ADCR Mux",
"ADC",
"ADCR" },
1084 {
"ADCR Mux",
"DMIC",
"DMIC1R" },
1087 {
"AIF1ADC1L",
NULL,
"AIF1ADC1L Mixer" },
1088 {
"AIF1ADC1L Mixer",
"ADC/DMIC Switch",
"ADCL Mux" },
1090 {
"AIF1ADC1R",
NULL,
"AIF1ADC1R Mixer" },
1091 {
"AIF1ADC1R Mixer",
"ADC/DMIC Switch",
"ADCR Mux" },
1093 {
"AIF1ADC2L",
NULL,
"AIF1ADC2L Mixer" },
1094 {
"AIF1ADC2L Mixer",
"DMIC Switch",
"DMIC2L" },
1096 {
"AIF1ADC2R",
NULL,
"AIF1ADC2R Mixer" },
1097 {
"AIF1ADC2R Mixer",
"DMIC Switch",
"DMIC2R" },
1100 {
"Left Sidetone",
"ADC/DMIC1",
"AIF1ADC1L" },
1101 {
"Left Sidetone",
"DMIC2",
"AIF1ADC2L" },
1102 {
"Right Sidetone",
"ADC/DMIC1",
"AIF1ADC1R" },
1103 {
"Right Sidetone",
"DMIC2",
"AIF1ADC2R" },
1105 {
"AIF1DAC1L",
NULL,
"AIF1CLK" },
1106 {
"AIF1DAC1L",
NULL,
"DSP1CLK" },
1107 {
"AIF1DAC1R",
NULL,
"AIF1CLK" },
1108 {
"AIF1DAC1R",
NULL,
"DSP1CLK" },
1109 {
"AIF1DAC1R",
NULL,
"SYSDSPCLK" },
1111 {
"AIF1DAC2L",
NULL,
"AIF1CLK" },
1112 {
"AIF1DAC2L",
NULL,
"DSP1CLK" },
1113 {
"AIF1DAC2R",
NULL,
"AIF1CLK" },
1114 {
"AIF1DAC2R",
NULL,
"DSP1CLK" },
1115 {
"AIF1DAC2R",
NULL,
"SYSDSPCLK" },
1117 {
"DAC1L",
NULL,
"AIF1CLK" },
1118 {
"DAC1L",
NULL,
"DSP1CLK" },
1119 {
"DAC1L",
NULL,
"SYSDSPCLK" },
1121 {
"DAC1R",
NULL,
"AIF1CLK" },
1122 {
"DAC1R",
NULL,
"DSP1CLK" },
1123 {
"DAC1R",
NULL,
"SYSDSPCLK" },
1125 {
"AIF1DAC1L",
NULL,
"AIF1DACDAT" },
1126 {
"AIF1DAC1R",
NULL,
"AIF1DACDAT" },
1127 {
"AIF1DAC2L",
NULL,
"AIF1DACDAT" },
1128 {
"AIF1DAC2R",
NULL,
"AIF1DACDAT" },
1131 {
"DAC1L",
NULL,
"DAC1L Mixer" },
1132 {
"DAC1L Mixer",
"AIF1.1 Switch",
"AIF1DAC1L" },
1133 {
"DAC1L Mixer",
"AIF1.2 Switch",
"AIF1DAC2L" },
1134 {
"DAC1L Mixer",
"Left Sidetone Switch",
"Left Sidetone" },
1135 {
"DAC1L Mixer",
"Right Sidetone Switch",
"Right Sidetone" },
1137 {
"DAC1R",
NULL,
"DAC1R Mixer" },
1138 {
"DAC1R Mixer",
"AIF1.1 Switch",
"AIF1DAC1R" },
1139 {
"DAC1R Mixer",
"AIF1.2 Switch",
"AIF1DAC2R" },
1140 {
"DAC1R Mixer",
"Left Sidetone Switch",
"Left Sidetone" },
1141 {
"DAC1R Mixer",
"Right Sidetone Switch",
"Right Sidetone" },
1144 {
"DAC2L",
NULL,
"AIF2DAC2L Mixer" },
1145 {
"AIF2DAC2L Mixer",
"AIF1.2 Switch",
"AIF1DAC2L" },
1146 {
"AIF2DAC2L Mixer",
"AIF1.1 Switch",
"AIF1DAC1L" },
1148 {
"DAC2R",
NULL,
"AIF2DAC2R Mixer" },
1149 {
"AIF2DAC2R Mixer",
"AIF1.2 Switch",
"AIF1DAC2R" },
1150 {
"AIF2DAC2R Mixer",
"AIF1.1 Switch",
"AIF1DAC1R" },
1153 {
"Headphone PGA",
NULL,
"DAC1L" },
1154 {
"Headphone PGA",
NULL,
"DAC1R" },
1156 {
"Headphone PGA",
NULL,
"DAC2L" },
1157 {
"Headphone PGA",
NULL,
"DAC2R" },
1159 {
"Headphone PGA",
NULL,
"Headphone Supply" },
1160 {
"Headphone PGA",
NULL,
"CLK_SYS" },
1161 {
"Headphone PGA",
NULL,
"LDO2" },
1163 {
"HP1L",
NULL,
"Headphone PGA" },
1164 {
"HP1R",
NULL,
"Headphone PGA" },
1166 {
"SPK1L Driver",
"DAC1L",
"DAC1L" },
1167 {
"SPK1L Driver",
"DAC1R",
"DAC1R" },
1168 {
"SPK1L Driver",
"DAC2L",
"DAC2L" },
1169 {
"SPK1L Driver",
"DAC2R",
"DAC2R" },
1170 {
"SPK1L Driver",
NULL,
"CLK_SYS" },
1172 {
"SPK1R Driver",
"DAC1L",
"DAC1L" },
1173 {
"SPK1R Driver",
"DAC1R",
"DAC1R" },
1174 {
"SPK1R Driver",
"DAC2L",
"DAC2L" },
1175 {
"SPK1R Driver",
"DAC2R",
"DAC2R" },
1176 {
"SPK1R Driver",
NULL,
"CLK_SYS" },
1178 {
"SPK2L Driver",
"DAC1L",
"DAC1L" },
1179 {
"SPK2L Driver",
"DAC1R",
"DAC1R" },
1180 {
"SPK2L Driver",
"DAC2L",
"DAC2L" },
1181 {
"SPK2L Driver",
"DAC2R",
"DAC2R" },
1182 {
"SPK2L Driver",
NULL,
"CLK_SYS" },
1184 {
"SPK2R Driver",
"DAC1L",
"DAC1L" },
1185 {
"SPK2R Driver",
"DAC1R",
"DAC1R" },
1186 {
"SPK2R Driver",
"DAC2L",
"DAC2L" },
1187 {
"SPK2R Driver",
"DAC2R",
"DAC2R" },
1188 {
"SPK2R Driver",
NULL,
"CLK_SYS" },
1190 {
"SPK1L",
NULL,
"SPK1L Driver" },
1191 {
"SPK1R",
NULL,
"SPK1R Driver" },
1192 {
"SPK2L",
NULL,
"SPK2L Driver" },
1193 {
"SPK2R",
NULL,
"SPK2R Driver" }
1196 static bool wm8995_readable(
struct device *
dev,
unsigned int reg)
1417 static bool wm8995_volatile(
struct device *dev,
unsigned int reg)
1457 static int wm8995_set_dai_fmt(
struct snd_soc_dai *dai,
unsigned int fmt)
1473 dev_err(dai->
dev,
"Unknown master/slave configuration\n");
1497 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1515 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1544 static const int srs[] = {
1545 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
1549 static const int fs_ratios[] = {
1551 128, 192, 256, 384, 512, 768, 1024, 1408, 1536
1554 static const int bclk_divs[] = {
1555 10, 15, 20, 30, 40, 55, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480
1571 int i, rate_val, best, best_val, cur_val;
1574 wm8995 = snd_soc_codec_get_drvdata(codec);
1586 dev_dbg(codec->
dev,
"AIF1 using split LRCLK\n");
1598 dev_dbg(codec->
dev,
"AIF2 using split LRCLK\n");
1623 dev_err(dai->
dev,
"Unsupported word length %u\n",
1633 dev_err(dai->
dev,
"Sample rate %d is not supported\n",
1639 dev_dbg(dai->
dev,
"Sample rate is %dHz\n", srs[i]);
1640 dev_dbg(dai->
dev,
"AIF%dCLK is %dHz, target BCLK %dHz\n",
1641 dai->
id + 1, wm8995->
aifclk[dai->
id], bclk_rate);
1647 for (i = 2; i <
ARRAY_SIZE(fs_ratios); i++) {
1650 if (cur_val >= best_val)
1657 dev_dbg(dai->
dev,
"Selected AIF%dCLK/fs = %d\n",
1658 dai->
id + 1, fs_ratios[best]);
1668 for (i = 0; i <
ARRAY_SIZE(bclk_divs); i++) {
1669 cur_val = (wm8995->
aifclk[dai->
id] * 10 / bclk_divs[
i]) - bclk_rate;
1676 bclk_rate = wm8995->
aifclk[dai->
id] * 10 / bclk_divs[best];
1677 dev_dbg(dai->
dev,
"Using BCLK_DIV %d for actual BCLK %dHz\n",
1678 bclk_divs[best], bclk_rate);
1681 dev_dbg(dai->
dev,
"Using LRCLK rate %d for actual LRCLK %dHz\n",
1682 lrclk, bclk_rate / lrclk);
1701 switch (codec_dai->
id) {
1728 #define FIXED_FLL_SIZE ((1 << 16) * 10)
1738 static int wm8995_get_fll_config(
struct fll_div *fll,
1739 int freq_in,
int freq_out)
1742 unsigned int K, Ndiv, Nmod;
1744 pr_debug(
"FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
1748 while (freq_in > 13500000) {
1759 while (freq_out * (fll->
outdiv + 1) < 90000000) {
1764 freq_out *= fll->
outdiv + 1;
1767 if (freq_in > 1000000) {
1769 }
else if (freq_in > 256000) {
1772 }
else if (freq_in > 128000) {
1775 }
else if (freq_in > 64000) {
1785 Ndiv = freq_out / freq_in;
1788 Nmod = freq_out % freq_in;
1796 K = Kpart & 0xFFFFFFFF;
1809 static int wm8995_set_fll(
struct snd_soc_dai *dai,
int id,
1810 int src,
unsigned int freq_in,
1811 unsigned int freq_out)
1815 int reg_offset,
ret;
1820 wm8995 = snd_soc_codec_get_drvdata(codec);
1857 if (wm8995->
fll[
id].src == src &&
1858 wm8995->
fll[
id].in == freq_in && wm8995->
fll[
id].out == freq_out)
1866 ret = wm8995_get_fll_config(&fll, freq_in, freq_out);
1868 ret = wm8995_get_fll_config(&fll, wm8995->
fll[
id].in,
1869 wm8995->
fll[
id].out);
1905 wm8995->
fll[
id].in = freq_in;
1906 wm8995->
fll[
id].out = freq_out;
1915 configure_clock(codec);
1920 static int wm8995_set_dai_sysclk(
struct snd_soc_dai *dai,
1921 int clk_id,
unsigned int freq,
int dir)
1927 wm8995 = snd_soc_codec_get_drvdata(codec);
1942 dev_dbg(dai->
dev,
"AIF%d using MCLK1 at %uHz\n",
1948 dev_dbg(dai->
dev,
"AIF%d using MCLK2 at %uHz\n",
1961 dev_err(dai->
dev,
"Unknown clock source %d\n", clk_id);
1965 configure_clock(codec);
1970 static int wm8995_set_bias_level(
struct snd_soc_codec *codec,
1976 wm8995 = snd_soc_codec_get_drvdata(codec);
1991 "Failed to sync cache: %d\n", ret);
2024 #define wm8995_suspend NULL
2025 #define wm8995_resume NULL
2033 wm8995 = snd_soc_codec_get_drvdata(codec);
2050 wm8995 = snd_soc_codec_get_drvdata(codec);
2056 dev_err(codec->
dev,
"Failed to set cache i/o: %d\n", ret);
2061 wm8995->
supplies[i].supply = wm8995_supply_names[i];
2066 dev_err(codec->
dev,
"Failed to request supplies: %d\n", ret);
2070 wm8995->
disable_nb[0].notifier_call = wm8995_regulator_event_0;
2071 wm8995->
disable_nb[1].notifier_call = wm8995_regulator_event_1;
2072 wm8995->
disable_nb[2].notifier_call = wm8995_regulator_event_2;
2073 wm8995->
disable_nb[3].notifier_call = wm8995_regulator_event_3;
2074 wm8995->
disable_nb[4].notifier_call = wm8995_regulator_event_4;
2075 wm8995->
disable_nb[5].notifier_call = wm8995_regulator_event_5;
2076 wm8995->
disable_nb[6].notifier_call = wm8995_regulator_event_6;
2077 wm8995->
disable_nb[7].notifier_call = wm8995_regulator_event_7;
2085 "Failed to register regulator notifier: %d\n",
2093 dev_err(codec->
dev,
"Failed to enable supplies: %d\n", ret);
2099 dev_err(codec->
dev,
"Failed to read device ID: %d\n", ret);
2100 goto err_reg_enable;
2103 if (ret != 0x8995) {
2104 dev_err(codec->
dev,
"Invalid device ID: %#x\n", ret);
2106 goto err_reg_enable;
2111 dev_err(codec->
dev,
"Failed to issue reset: %d\n", ret);
2112 goto err_reg_enable;
2137 wm8995_update_class_w(codec);
2155 #define WM8995_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
2156 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
2159 .set_sysclk = wm8995_set_dai_sysclk,
2160 .set_fmt = wm8995_set_dai_fmt,
2161 .hw_params = wm8995_hw_params,
2162 .digital_mute = wm8995_aif_mute,
2163 .set_pll = wm8995_set_fll,
2164 .set_tristate = wm8995_set_tristate,
2168 .set_sysclk = wm8995_set_dai_sysclk,
2169 .set_fmt = wm8995_set_dai_fmt,
2170 .hw_params = wm8995_hw_params,
2171 .digital_mute = wm8995_aif_mute,
2172 .set_pll = wm8995_set_fll,
2173 .set_tristate = wm8995_set_tristate,
2177 .set_tristate = wm8995_set_tristate,
2182 .name =
"wm8995-aif1",
2184 .stream_name =
"AIF1 Playback",
2191 .stream_name =
"AIF1 Capture",
2197 .ops = &wm8995_aif1_dai_ops
2200 .name =
"wm8995-aif2",
2202 .stream_name =
"AIF2 Playback",
2209 .stream_name =
"AIF2 Capture",
2215 .ops = &wm8995_aif2_dai_ops
2218 .name =
"wm8995-aif3",
2220 .stream_name =
"AIF3 Playback",
2227 .stream_name =
"AIF3 Capture",
2233 .ops = &wm8995_aif3_dai_ops
2238 .probe = wm8995_probe,
2239 .remove = wm8995_remove,
2242 .set_bias_level = wm8995_set_bias_level,
2243 .idle_bias_off =
true,
2246 static struct regmap_config wm8995_regmap = {
2251 .reg_defaults = wm8995_reg_defaults,
2252 .num_reg_defaults =
ARRAY_SIZE(wm8995_reg_defaults),
2253 .volatile_reg = wm8995_volatile,
2254 .readable_reg = wm8995_readable,
2258 #if defined(CONFIG_SPI_MASTER)
2264 wm8995 = kzalloc(
sizeof *wm8995,
GFP_KERNEL);
2268 spi_set_drvdata(spi, wm8995);
2271 if (IS_ERR(wm8995->
regmap)) {
2272 ret = PTR_ERR(wm8995->
regmap);
2273 dev_err(&spi->
dev,
"Failed to register regmap: %d\n", ret);
2278 &soc_codec_dev_wm8995, wm8995_dai,
2295 struct wm8995_priv *wm8995 = spi_get_drvdata(spi);
2302 static struct spi_driver wm8995_spi_driver = {
2307 .probe = wm8995_spi_probe,
2312 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2319 wm8995 = kzalloc(
sizeof *wm8995,
GFP_KERNEL);
2323 i2c_set_clientdata(i2c, wm8995);
2326 if (IS_ERR(wm8995->
regmap)) {
2327 ret = PTR_ERR(wm8995->
regmap);
2328 dev_err(&i2c->
dev,
"Failed to register regmap: %d\n", ret);
2333 &soc_codec_dev_wm8995, wm8995_dai,
2336 dev_err(&i2c->
dev,
"Failed to register CODEC: %d\n", ret);
2352 struct wm8995_priv *wm8995 = i2c_get_clientdata(client);
2367 static struct i2c_driver wm8995_i2c_driver = {
2372 .probe = wm8995_i2c_probe,
2374 .id_table = wm8995_i2c_id
2378 static int __init wm8995_modinit(
void)
2382 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2383 ret = i2c_add_driver(&wm8995_i2c_driver);
2389 #if defined(CONFIG_SPI_MASTER)
2401 static void __exit wm8995_exit(
void)
2403 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2406 #if defined(CONFIG_SPI_MASTER)
2407 spi_unregister_driver(&wm8995_spi_driver);