14 #include <linux/module.h>
19 #include <linux/i2c.h>
22 #include <linux/slab.h>
38 #define WM8904_NUM_DCS_CHANNELS 4
40 #define WM8904_NUM_SUPPLIES 5
91 static const struct reg_default wm8904_reg_defaults[] = {
187 static bool wm8904_volatile_register(
struct device *
dev,
unsigned int reg)
205 static bool wm8904_readable_register(
struct device *dev,
unsigned int reg)
319 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
320 unsigned int clock0, clock2,
rate;
349 dev_err(codec->
dev,
"System clock not configured\n");
354 if (rate > 13500000) {
375 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
391 static int wm8904_put_drc_enum(
struct snd_kcontrol *kcontrol,
395 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
404 wm8904_set_drc(codec);
409 static int wm8904_get_drc_enum(
struct snd_kcontrol *kcontrol,
413 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
420 static void wm8904_set_retune_mobile(
struct snd_soc_codec *codec)
422 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
424 int best, best_val, save,
i,
cfg;
438 - wm8904->
fs) < best_val) {
445 dev_dbg(codec->
dev,
"ReTune Mobile %s/%dHz for %dHz sample rate\n",
462 static int wm8904_put_retune_mobile_enum(
struct snd_kcontrol *kcontrol,
466 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
468 int value = ucontrol->
value.integer.value[0];
475 wm8904_set_retune_mobile(codec);
480 static int wm8904_get_retune_mobile_enum(
struct snd_kcontrol *kcontrol,
484 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
491 static int deemph_settings[] = { 0, 32000, 44100, 48000 };
495 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
503 for (i = 2; i <
ARRAY_SIZE(deemph_settings); i++) {
504 if (
abs(deemph_settings[i] - wm8904->
fs) <
505 abs(deemph_settings[best] - wm8904->
fs))
514 dev_dbg(codec->
dev,
"Set deemphasis %d\n", val);
520 static int wm8904_get_deemph(
struct snd_kcontrol *kcontrol,
524 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
526 ucontrol->
value.enumerated.item[0] = wm8904->
deemph;
530 static int wm8904_put_deemph(
struct snd_kcontrol *kcontrol,
534 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
542 return wm8904_set_deemph(codec);
551 static const char *input_mode_text[] = {
552 "Single-Ended",
"Differential Line",
"Differential Mic"
555 static const struct soc_enum lin_mode =
558 static const struct soc_enum rin_mode =
561 static const char *hpf_mode_text[] = {
562 "Hi-fi",
"Voice 1",
"Voice 2",
"Voice 3"
565 static const struct soc_enum hpf_mode =
568 static int wm8904_adc_osr_put(
struct snd_kcontrol *kcontrol,
579 if (ucontrol->
value.integer.value[0])
595 SOC_ENUM(
"Left Caputure Mode", lin_mode),
596 SOC_ENUM(
"Right Capture Mode", rin_mode),
605 SOC_ENUM(
"High Pass Filter Mode", hpf_mode),
608 .name =
"ADC 128x OSR Switch",
610 .put = wm8904_adc_osr_put,
615 static const char *drc_path_text[] = {
619 static const struct soc_enum drc_path =
647 wm8904_get_deemph, wm8904_put_deemph),
678 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
716 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
720 int dcs_l_reg, dcs_r_reg;
778 dev_dbg(codec->
dev,
"Restoring DC servo state\n");
789 dev_dbg(codec->
dev,
"Calibrating DC servo\n");
801 if ((val & dcs_mask) == dcs_mask)
807 if ((val & dcs_mask) != dcs_mask)
861 static const char *lin_text[] = {
862 "IN1L",
"IN2L",
"IN3L"
865 static const struct soc_enum lin_enum =
871 static const struct soc_enum lin_inv_enum =
877 static const char *rin_text[] = {
878 "IN1R",
"IN2R",
"IN3R"
881 static const struct soc_enum rin_enum =
887 static const struct soc_enum rin_inv_enum =
893 static const char *aif_text[] = {
897 static const struct soc_enum aifoutl_enum =
903 static const struct soc_enum aifoutr_enum =
909 static const struct soc_enum aifinl_enum =
915 static const struct soc_enum aifinr_enum =
980 0,
NULL, 0, out_pga_event,
984 0,
NULL, 0, out_pga_event,
994 static const char *out_mux_text[] = {
998 static const struct soc_enum hpl_enum =
1004 static const struct soc_enum hpr_enum =
1010 static const struct soc_enum linel_enum =
1016 static const struct soc_enum liner_enum =
1022 static const char *sidetone_text[] = {
1023 "None",
"Left",
"Right"
1026 static const struct soc_enum dacl_sidetone_enum =
1032 static const struct soc_enum dacr_sidetone_enum =
1053 {
"CLK_DSP",
NULL,
"SYSCLK" },
1054 {
"TOCLK",
NULL,
"SYSCLK" },
1058 {
"Left Capture Mux",
"IN1L",
"IN1L" },
1059 {
"Left Capture Mux",
"IN2L",
"IN2L" },
1060 {
"Left Capture Mux",
"IN3L",
"IN3L" },
1062 {
"Left Capture Inverting Mux",
"IN1L",
"IN1L" },
1063 {
"Left Capture Inverting Mux",
"IN2L",
"IN2L" },
1064 {
"Left Capture Inverting Mux",
"IN3L",
"IN3L" },
1066 {
"Right Capture Mux",
"IN1R",
"IN1R" },
1067 {
"Right Capture Mux",
"IN2R",
"IN2R" },
1068 {
"Right Capture Mux",
"IN3R",
"IN3R" },
1070 {
"Right Capture Inverting Mux",
"IN1R",
"IN1R" },
1071 {
"Right Capture Inverting Mux",
"IN2R",
"IN2R" },
1072 {
"Right Capture Inverting Mux",
"IN3R",
"IN3R" },
1074 {
"Left Capture PGA",
NULL,
"Left Capture Mux" },
1075 {
"Left Capture PGA",
NULL,
"Left Capture Inverting Mux" },
1077 {
"Right Capture PGA",
NULL,
"Right Capture Mux" },
1078 {
"Right Capture PGA",
NULL,
"Right Capture Inverting Mux" },
1080 {
"AIFOUTL",
"Left",
"ADCL" },
1081 {
"AIFOUTL",
"Right",
"ADCR" },
1082 {
"AIFOUTR",
"Left",
"ADCL" },
1083 {
"AIFOUTR",
"Right",
"ADCR" },
1085 {
"ADCL",
NULL,
"CLK_DSP" },
1086 {
"ADCL",
NULL,
"Left Capture PGA" },
1088 {
"ADCR",
NULL,
"CLK_DSP" },
1089 {
"ADCR",
NULL,
"Right Capture PGA" },
1093 {
"DACL",
"Right",
"AIFINR" },
1094 {
"DACL",
"Left",
"AIFINL" },
1095 {
"DACL",
NULL,
"CLK_DSP" },
1097 {
"DACR",
"Right",
"AIFINR" },
1098 {
"DACR",
"Left",
"AIFINL" },
1099 {
"DACR",
NULL,
"CLK_DSP" },
1101 {
"Charge pump",
NULL,
"SYSCLK" },
1103 {
"Headphone Output",
NULL,
"HPL PGA" },
1104 {
"Headphone Output",
NULL,
"HPR PGA" },
1105 {
"Headphone Output",
NULL,
"Charge pump" },
1106 {
"Headphone Output",
NULL,
"TOCLK" },
1108 {
"Line Output",
NULL,
"LINEL PGA" },
1109 {
"Line Output",
NULL,
"LINER PGA" },
1110 {
"Line Output",
NULL,
"Charge pump" },
1111 {
"Line Output",
NULL,
"TOCLK" },
1113 {
"HPOUTL",
NULL,
"Headphone Output" },
1114 {
"HPOUTR",
NULL,
"Headphone Output" },
1116 {
"LINEOUTL",
NULL,
"Line Output" },
1117 {
"LINEOUTR",
NULL,
"Line Output" },
1121 {
"Left Sidetone",
"Left",
"ADCL" },
1122 {
"Left Sidetone",
"Right",
"ADCR" },
1123 {
"DACL",
NULL,
"Left Sidetone" },
1125 {
"Right Sidetone",
"Left",
"ADCL" },
1126 {
"Right Sidetone",
"Right",
"ADCR" },
1127 {
"DACR",
NULL,
"Right Sidetone" },
1129 {
"Left Bypass",
NULL,
"Class G" },
1130 {
"Left Bypass",
NULL,
"Left Capture PGA" },
1132 {
"Right Bypass",
NULL,
"Class G" },
1133 {
"Right Bypass",
NULL,
"Right Capture PGA" },
1135 {
"HPL Mux",
"DAC",
"DACL" },
1136 {
"HPL Mux",
"Bypass",
"Left Bypass" },
1138 {
"HPR Mux",
"DAC",
"DACR" },
1139 {
"HPR Mux",
"Bypass",
"Right Bypass" },
1141 {
"LINEL Mux",
"DAC",
"DACL" },
1142 {
"LINEL Mux",
"Bypass",
"Left Bypass" },
1144 {
"LINER Mux",
"DAC",
"DACR" },
1145 {
"LINER Mux",
"Bypass",
"Right Bypass" },
1147 {
"HPL PGA",
NULL,
"HPL Mux" },
1148 {
"HPR PGA",
NULL,
"HPR Mux" },
1150 {
"LINEL PGA",
NULL,
"LINEL Mux" },
1151 {
"LINER PGA",
NULL,
"LINER Mux" },
1155 {
"HPL PGA",
NULL,
"DACL" },
1156 {
"HPR PGA",
NULL,
"DACR" },
1158 {
"LINEL PGA",
NULL,
"DACL" },
1159 {
"LINER PGA",
NULL,
"DACR" },
1164 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
1217 } clk_sys_rates[] = {
1233 } sample_rates[] = {
1277 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
1278 int ret,
i, best, best_val, cur_val;
1279 unsigned int aif1 = 0;
1280 unsigned int aif2 = 0;
1281 unsigned int aif3 = 0;
1282 unsigned int clock1 = 0;
1283 unsigned int dac_digital1 = 0;
1288 dev_dbg(codec->
dev,
"Configuring for %d %d bit TDM slots\n",
1316 ret = wm8904_configure_clocking(codec);
1324 for (i = 1; i <
ARRAY_SIZE(clk_sys_rates); i++) {
1326 clk_sys_rates[i].ratio) - wm8904->
fs);
1327 if (cur_val < best_val) {
1332 dev_dbg(codec->
dev,
"Selected CLK_SYS_RATIO of %d\n",
1333 clk_sys_rates[best].ratio);
1334 clock1 |= (clk_sys_rates[best].clk_sys_rate
1339 best_val =
abs(wm8904->
fs - sample_rates[0].rate);
1340 for (i = 1; i <
ARRAY_SIZE(sample_rates); i++) {
1342 cur_val =
abs(wm8904->
fs - sample_rates[i].rate);
1343 if (cur_val < best_val) {
1348 dev_dbg(codec->
dev,
"Selected SAMPLE_RATE of %dHz\n",
1349 sample_rates[best].rate);
1350 clock1 |= (sample_rates[best].sample_rate
1354 if (wm8904->
fs <= 24000)
1360 for (i = 0; i <
ARRAY_SIZE(bclk_divs); i++) {
1365 if (cur_val < best_val) {
1371 dev_dbg(codec->
dev,
"Selected BCLK_DIV of %d for %dHz BCLK\n",
1372 bclk_divs[best].div, wm8904->
bclk);
1373 aif2 |= bclk_divs[best].bclk_div;
1377 aif3 |= wm8904->
bclk / wm8904->
fs;
1393 wm8904_set_retune_mobile(codec);
1394 wm8904_set_deemph(codec);
1400 static int wm8904_set_sysclk(
struct snd_soc_dai *dai,
int clk_id,
1401 unsigned int freq,
int dir)
1420 dev_dbg(dai->
dev,
"Clock source is %d at %uHz\n", clk_id, freq);
1422 wm8904_configure_clocking(codec);
1427 static int wm8904_set_fmt(
struct snd_soc_dai *dai,
unsigned int fmt)
1430 unsigned int aif1 = 0;
1431 unsigned int aif3 = 0;
1468 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1486 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1520 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
1575 #define FIXED_FLL_SIZE ((1 << 16) * 10)
1583 { 0, 64000, 4, 16 },
1584 { 64000, 128000, 3, 8 },
1585 { 128000, 256000, 2, 4 },
1586 { 256000, 1000000, 1, 2 },
1587 { 1000000, 13500000, 0, 1 },
1594 unsigned int K, Ndiv, Nmod,
target;
1601 while ((Fref / div) > 13500000) {
1606 pr_err(
"Can't scale %dMHz input down to <=13.5MHz\n",
1612 pr_debug(
"Fref=%u Fout=%u\n", Fref, Fout);
1619 while (Fout * div < 90000000) {
1622 pr_err(
"Unable to find FLL_OUTDIV for Fout=%uHz\n",
1627 target = Fout *
div;
1633 for (i = 0; i <
ARRAY_SIZE(fll_fratios); i++) {
1634 if (fll_fratios[i].
min <= Fref && Fref <= fll_fratios[i].
max) {
1636 target /= fll_fratios[
i].ratio;
1641 pr_err(
"Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
1646 Ndiv = target / Fref;
1649 Nmod = target % Fref;
1657 K = Kpart & 0xFFFFFFFF;
1663 fll_div->
k = K / 10;
1665 pr_debug(
"N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
1666 fll_div->
n, fll_div->
k,
1674 unsigned int Fref,
unsigned int Fout)
1677 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
1716 dev_dbg(codec->
dev,
"Using free running FLL\n");
1721 memset(&fll_div, 0,
sizeof(fll_div));
1726 dev_err(codec->
dev,
"Unknown FLL ID %d\n", fll_id);
1792 dev_dbg(codec->
dev,
"FLL configured for %dHz->%dHz\n", Fref, Fout);
1812 static int wm8904_digital_mute(
struct snd_soc_dai *codec_dai,
int mute)
1827 static int wm8904_set_bias_level(
struct snd_soc_codec *codec,
1830 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
1854 "Failed to enable supplies: %d\n",
1907 #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
1909 #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1910 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1913 .set_sysclk = wm8904_set_sysclk,
1914 .set_fmt = wm8904_set_fmt,
1915 .set_tdm_slot = wm8904_set_tdm_slot,
1916 .set_pll = wm8904_set_fll,
1917 .hw_params = wm8904_hw_params,
1918 .digital_mute = wm8904_digital_mute,
1922 .name =
"wm8904-hifi",
1924 .stream_name =
"Playback",
1931 .stream_name =
"Capture",
1937 .ops = &wm8904_dai_ops,
1938 .symmetric_rates = 1,
1941 static void wm8904_handle_retune_mobile_pdata(
struct snd_soc_codec *codec)
1943 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
1948 wm8904_get_retune_mobile_enum,
1949 wm8904_put_retune_mobile_enum);
1986 dev_dbg(codec->
dev,
"Allocated %d unique ReTune Mobile names\n",
1995 "Failed to add ReTune Mobile control: %d\n", ret);
1998 static void wm8904_handle_pdata(
struct snd_soc_codec *codec)
2000 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
2015 wm8904_get_drc_enum, wm8904_put_drc_enum);
2022 "Failed to allocate %d DRC config texts\n",
2036 "Failed to add DRC mode control: %d\n", ret);
2038 wm8904_set_drc(codec);
2041 dev_dbg(codec->
dev,
"%d ReTune Mobile configurations\n",
2045 wm8904_handle_retune_mobile_pdata(codec);
2054 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
2066 dev_err(codec->
dev,
"Unknown device type %d\n",
2073 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
2077 wm8904_handle_pdata(codec);
2079 wm8904_add_widgets(codec);
2086 struct wm8904_priv *wm8904 = snd_soc_codec_get_drvdata(codec);
2095 .probe = wm8904_probe,
2096 .remove = wm8904_remove,
2097 .set_bias_level = wm8904_set_bias_level,
2098 .idle_bias_off =
true,
2101 static const struct regmap_config wm8904_regmap = {
2106 .volatile_reg = wm8904_volatile_register,
2107 .readable_reg = wm8904_readable_register,
2110 .reg_defaults = wm8904_reg_defaults,
2111 .num_reg_defaults =
ARRAY_SIZE(wm8904_reg_defaults),
2127 if (IS_ERR(wm8904->
regmap)) {
2128 ret = PTR_ERR(wm8904->
regmap);
2129 dev_err(&i2c->
dev,
"Failed to allocate register map: %d\n",
2134 wm8904->
devtype =
id->driver_data;
2135 i2c_set_clientdata(i2c, wm8904);
2136 wm8904->
pdata = i2c->
dev.platform_data;
2139 wm8904->
supplies[i].supply = wm8904_supply_names[i];
2144 dev_err(&i2c->
dev,
"Failed to request supplies: %d\n", ret);
2151 dev_err(&i2c->
dev,
"Failed to enable supplies: %d\n", ret);
2157 dev_err(&i2c->
dev,
"Failed to read ID register: %d\n", ret);
2160 if (val != 0x8904) {
2161 dev_err(&i2c->
dev,
"Device is not a WM8904, ID is %x\n", val);
2168 dev_err(&i2c->
dev,
"Failed to read device revision: %d\n",
2176 dev_err(&i2c->
dev,
"Failed to issue reset: %d\n", ret);
2205 if (wm8904->
pdata) {
2207 if (!wm8904->
pdata->gpio_cfg[i])
2213 wm8904->
pdata->gpio_cfg[i]);
2221 wm8904->
pdata->mic_cfg[i]);
2239 &soc_codec_dev_wm8904, &wm8904_dai, 1);
2264 static struct i2c_driver wm8904_i2c_driver = {
2269 .probe = wm8904_i2c_probe,
2271 .id_table = wm8904_i2c_id,