13 #include <linux/module.h>
20 #include <linux/i2c.h>
24 #include <linux/slab.h>
37 #define WM2200_NUM_CORE_SUPPLIES 2
408 static bool wm2200_volatile_register(
struct device *
dev,
unsigned int reg)
424 static bool wm2200_readable_register(
struct device *dev,
unsigned int reg)
778 static const struct reg_default wm2200_reva_patch[] = {
863 static int wm2200_reset(
struct wm2200_priv *wm2200)
865 if (wm2200->
pdata.reset) {
880 static const char *wm2200_mixer_texts[] = {
916 static int wm2200_mixer_values[] = {
951 #define WM2200_MIXER_CONTROLS(name, base) \
952 SOC_SINGLE_TLV(name " Input 1 Volume", base + 1 , \
953 WM2200_MIXER_VOL_SHIFT, 80, 0, mixer_tlv), \
954 SOC_SINGLE_TLV(name " Input 2 Volume", base + 3 , \
955 WM2200_MIXER_VOL_SHIFT, 80, 0, mixer_tlv), \
956 SOC_SINGLE_TLV(name " Input 3 Volume", base + 5 , \
957 WM2200_MIXER_VOL_SHIFT, 80, 0, mixer_tlv), \
958 SOC_SINGLE_TLV(name " Input 4 Volume", base + 7 , \
959 WM2200_MIXER_VOL_SHIFT, 80, 0, mixer_tlv)
961 #define WM2200_MUX_ENUM_DECL(name, reg) \
962 SOC_VALUE_ENUM_SINGLE_DECL(name, reg, 0, 0xff, \
963 wm2200_mixer_texts, wm2200_mixer_values)
965 #define WM2200_MUX_CTL_DECL(name) \
966 const struct snd_kcontrol_new name##_mux = \
967 SOC_DAPM_VALUE_ENUM("Route", name##_enum)
969 #define WM2200_MIXER_ENUMS(name, base_reg) \
970 static WM2200_MUX_ENUM_DECL(name##_in1_enum, base_reg); \
971 static WM2200_MUX_ENUM_DECL(name##_in2_enum, base_reg + 2); \
972 static WM2200_MUX_ENUM_DECL(name##_in3_enum, base_reg + 4); \
973 static WM2200_MUX_ENUM_DECL(name##_in4_enum, base_reg + 6); \
974 static WM2200_MUX_CTL_DECL(name##_in1); \
975 static WM2200_MUX_CTL_DECL(name##_in2); \
976 static WM2200_MUX_CTL_DECL(name##_in3); \
977 static WM2200_MUX_CTL_DECL(name##_in4)
1003 0xbf, 0, digital_tlv),
1006 0xbf, 0, digital_tlv),
1009 0xbf, 0, digital_tlv),
1057 #define WM2200_MUX(name, ctrl) \
1058 SND_SOC_DAPM_VALUE_MUX(name, SND_SOC_NOPM, 0, 0, ctrl)
1060 #define WM2200_MIXER_WIDGETS(name, name_str) \
1061 WM2200_MUX(name_str " Input 1", &name##_in1_mux), \
1062 WM2200_MUX(name_str " Input 2", &name##_in2_mux), \
1063 WM2200_MUX(name_str " Input 3", &name##_in3_mux), \
1064 WM2200_MUX(name_str " Input 4", &name##_in4_mux), \
1065 SND_SOC_DAPM_MIXER(name_str " Mixer", SND_SOC_NOPM, 0, 0, NULL, 0)
1067 #define WM2200_MIXER_INPUT_ROUTES(name) \
1068 { name, "Tone Generator", "Tone Generator" }, \
1069 { name, "IN1L", "IN1L PGA" }, \
1070 { name, "IN1R", "IN1R PGA" }, \
1071 { name, "IN2L", "IN2L PGA" }, \
1072 { name, "IN2R", "IN2R PGA" }, \
1073 { name, "IN3L", "IN3L PGA" }, \
1074 { name, "IN3R", "IN3R PGA" }, \
1075 { name, "DSP1.1", "DSP1" }, \
1076 { name, "DSP1.2", "DSP1" }, \
1077 { name, "DSP1.3", "DSP1" }, \
1078 { name, "DSP1.4", "DSP1" }, \
1079 { name, "DSP1.5", "DSP1" }, \
1080 { name, "DSP1.6", "DSP1" }, \
1081 { name, "DSP2.1", "DSP2" }, \
1082 { name, "DSP2.2", "DSP2" }, \
1083 { name, "DSP2.3", "DSP2" }, \
1084 { name, "DSP2.4", "DSP2" }, \
1085 { name, "DSP2.5", "DSP2" }, \
1086 { name, "DSP2.6", "DSP2" }, \
1087 { name, "AIF1RX1", "AIF1RX1" }, \
1088 { name, "AIF1RX2", "AIF1RX2" }, \
1089 { name, "AIF1RX3", "AIF1RX3" }, \
1090 { name, "AIF1RX4", "AIF1RX4" }, \
1091 { name, "AIF1RX5", "AIF1RX5" }, \
1092 { name, "AIF1RX6", "AIF1RX6" }, \
1093 { name, "EQL", "EQL" }, \
1094 { name, "EQR", "EQR" }, \
1095 { name, "LHPF1", "LHPF1" }, \
1096 { name, "LHPF2", "LHPF2" }
1098 #define WM2200_MIXER_ROUTES(widget, name) \
1099 { widget, NULL, name " Mixer" }, \
1100 { name " Mixer", NULL, name " Input 1" }, \
1101 { name " Mixer", NULL, name " Input 2" }, \
1102 { name " Mixer", NULL, name " Input 3" }, \
1103 { name " Mixer", NULL, name " Input 4" }, \
1104 WM2200_MIXER_INPUT_ROUTES(name " Input 1"), \
1105 WM2200_MIXER_INPUT_ROUTES(name " Input 2"), \
1106 WM2200_MIXER_INPUT_ROUTES(name " Input 3"), \
1107 WM2200_MIXER_INPUT_ROUTES(name " Input 4")
1255 {
"IN1L",
NULL,
"SYSCLK" },
1256 {
"IN1R",
NULL,
"SYSCLK" },
1257 {
"IN2L",
NULL,
"SYSCLK" },
1258 {
"IN2R",
NULL,
"SYSCLK" },
1259 {
"IN3L",
NULL,
"SYSCLK" },
1260 {
"IN3R",
NULL,
"SYSCLK" },
1261 {
"OUT1L",
NULL,
"SYSCLK" },
1262 {
"OUT1R",
NULL,
"SYSCLK" },
1263 {
"OUT2L",
NULL,
"SYSCLK" },
1264 {
"OUT2R",
NULL,
"SYSCLK" },
1265 {
"AIF1RX1",
NULL,
"SYSCLK" },
1266 {
"AIF1RX2",
NULL,
"SYSCLK" },
1267 {
"AIF1RX3",
NULL,
"SYSCLK" },
1268 {
"AIF1RX4",
NULL,
"SYSCLK" },
1269 {
"AIF1RX5",
NULL,
"SYSCLK" },
1270 {
"AIF1RX6",
NULL,
"SYSCLK" },
1271 {
"AIF1TX1",
NULL,
"SYSCLK" },
1272 {
"AIF1TX2",
NULL,
"SYSCLK" },
1273 {
"AIF1TX3",
NULL,
"SYSCLK" },
1274 {
"AIF1TX4",
NULL,
"SYSCLK" },
1275 {
"AIF1TX5",
NULL,
"SYSCLK" },
1276 {
"AIF1TX6",
NULL,
"SYSCLK" },
1278 {
"IN1L",
NULL,
"AVDD" },
1279 {
"IN1R",
NULL,
"AVDD" },
1280 {
"IN2L",
NULL,
"AVDD" },
1281 {
"IN2R",
NULL,
"AVDD" },
1282 {
"IN3L",
NULL,
"AVDD" },
1283 {
"IN3R",
NULL,
"AVDD" },
1284 {
"OUT1L",
NULL,
"AVDD" },
1285 {
"OUT1R",
NULL,
"AVDD" },
1287 {
"IN1L PGA",
NULL,
"IN1L" },
1288 {
"IN1R PGA",
NULL,
"IN1R" },
1289 {
"IN2L PGA",
NULL,
"IN2L" },
1290 {
"IN2R PGA",
NULL,
"IN2R" },
1291 {
"IN3L PGA",
NULL,
"IN3L" },
1292 {
"IN3R PGA",
NULL,
"IN3R" },
1294 {
"Tone Generator",
NULL,
"TONE" },
1296 {
"CP2",
NULL,
"CPVDD" },
1297 {
"MICBIAS1",
NULL,
"CP2" },
1298 {
"MICBIAS2",
NULL,
"CP2" },
1300 {
"CP1",
NULL,
"CPVDD" },
1301 {
"EPD_LN",
NULL,
"CP1" },
1302 {
"EPD_LP",
NULL,
"CP1" },
1303 {
"EPD_RN",
NULL,
"CP1" },
1304 {
"EPD_RP",
NULL,
"CP1" },
1306 {
"EPD_LP",
NULL,
"OUT1L" },
1307 {
"EPD_OUTP_LP",
NULL,
"EPD_LP" },
1308 {
"EPD_RMV_SHRT_LP",
NULL,
"EPD_OUTP_LP" },
1309 {
"EPOUTLP",
NULL,
"EPD_RMV_SHRT_LP" },
1311 {
"EPD_LN",
NULL,
"OUT1L" },
1312 {
"EPD_OUTP_LN",
NULL,
"EPD_LN" },
1313 {
"EPD_RMV_SHRT_LN",
NULL,
"EPD_OUTP_LN" },
1314 {
"EPOUTLN",
NULL,
"EPD_RMV_SHRT_LN" },
1316 {
"EPD_RP",
NULL,
"OUT1R" },
1317 {
"EPD_OUTP_RP",
NULL,
"EPD_RP" },
1318 {
"EPD_RMV_SHRT_RP",
NULL,
"EPD_OUTP_RP" },
1319 {
"EPOUTRP",
NULL,
"EPD_RMV_SHRT_RP" },
1321 {
"EPD_RN",
NULL,
"OUT1R" },
1322 {
"EPD_OUTP_RN",
NULL,
"EPD_RN" },
1323 {
"EPD_RMV_SHRT_RN",
NULL,
"EPD_OUTP_RN" },
1324 {
"EPOUTRN",
NULL,
"EPD_RMV_SHRT_RN" },
1326 {
"SPK",
NULL,
"OUT2L" },
1327 {
"SPK",
NULL,
"OUT2R" },
1364 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
1374 int lrclk,
bclk, fmt_val;
1393 dev_err(codec->
dev,
"Unsupported DAI format %d\n",
1394 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1412 dev_err(codec->
dev,
"Unsupported master mode %d\n",
1413 fmt & SND_SOC_DAIFMT_MASTER_MASK);
1448 static int wm2200_sr_code[] = {
1475 #define WM2200_NUM_BCLK_RATES 12
1512 struct wm2200_priv *wm2200 = snd_soc_codec_get_drvdata(codec);
1513 int i,
bclk, lrclk, wl,
fl, sr_code;
1524 dev_dbg(codec->
dev,
"Word length %d bits, frame length %d bits\n",
1533 dev_err(codec->
dev,
"SYSCLK has no rate set\n");
1537 for (i = 0; i <
ARRAY_SIZE(wm2200_sr_code); i++)
1541 dev_err(codec->
dev,
"Unsupported sample rate: %dHz\n",
1547 dev_dbg(codec->
dev,
"Target BCLK is %dHz, using %dHz SYSCLK\n",
1550 if (wm2200->
sysclk % 4000)
1551 bclk_rates = wm2200_bclk_rates_cd;
1553 bclk_rates = wm2200_bclk_rates_dat;
1556 if (bclk_rates[i] >= bclk && (bclk_rates[i] % bclk == 0))
1558 if (i == WM2200_NUM_BCLK_RATES) {
1560 "No valid BCLK for %dHz found from %dHz SYSCLK\n",
1566 dev_dbg(codec->
dev,
"Setting %dHz BCLK\n", bclk_rates[bclk]);
1571 dev_dbg(codec->
dev,
"Setting %dHz LRCLK\n", bclk_rates[bclk] / lrclk);
1597 .set_fmt = wm2200_set_fmt,
1598 .hw_params = wm2200_hw_params,
1601 static int wm2200_set_sysclk(
struct snd_soc_codec *codec,
int clk_id,
1604 struct wm2200_priv *wm2200 = snd_soc_codec_get_drvdata(codec);
1612 dev_err(codec->
dev,
"Unknown clock %d\n", clk_id);
1623 dev_err(codec->
dev,
"Invalid source %d\n", source);
1633 dev_err(codec->
dev,
"Invalid clock rate: %d\n", freq);
1665 { 0, 64000, 4, 16 },
1666 { 64000, 128000, 3, 8 },
1667 { 128000, 256000, 2, 4 },
1668 { 256000, 1000000, 1, 2 },
1669 { 1000000, 13500000, 0, 1 },
1677 unsigned int fratio, gcd_fll;
1683 while ((Fref / div) > 13500000) {
1688 pr_err(
"Can't scale %dMHz input down to <=13.5MHz\n",
1694 pr_debug(
"FLL Fref=%u Fout=%u\n", Fref, Fout);
1701 while (Fout * div < 90000000) {
1704 pr_err(
"Unable to find FLL_OUTDIV for Fout=%uHz\n",
1709 target = Fout *
div;
1712 pr_debug(
"FLL Fvco=%dHz\n", target);
1715 for (i = 0; i <
ARRAY_SIZE(fll_fratios); i++) {
1716 if (fll_fratios[i].
min <= Fref && Fref <= fll_fratios[i].
max) {
1718 fratio = fll_fratios[
i].ratio;
1723 pr_err(
"Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
1727 fll_div->
n = target / (fratio * Fref);
1729 if (target % Fref == 0) {
1733 gcd_fll =
gcd(target, fratio * Fref);
1735 fll_div->
theta = (target - (fll_div->
n * fratio * Fref))
1737 fll_div->
lambda = (fratio * Fref) / gcd_fll;
1740 pr_debug(
"FLL N=%x THETA=%x LAMBDA=%x\n",
1742 pr_debug(
"FLL_FRATIO=%x(%d) FLL_OUTDIV=%x FLL_REFCLK_DIV=%x\n",
1749 static int wm2200_set_fll(
struct snd_soc_codec *codec,
int fll_id,
int source,
1750 unsigned int Fref,
unsigned int Fout)
1753 struct wm2200_priv *wm2200 = snd_soc_codec_get_drvdata(codec);
1761 pm_runtime_put(codec->
dev);
1775 dev_err(codec->
dev,
"Invalid FLL source %d\n", source);
1789 factors.fll_fratio);
1790 if (factors.theta) {
1811 (factors.fll_refclk_div
1819 pm_runtime_get_sync(codec->
dev);
1833 for (i = 0; i < timeout; i++) {
1847 "Failed to read FLL status: %d\n",
1856 pm_runtime_put(codec->
dev);
1864 dev_dbg(codec->
dev,
"FLL running %dHz->%dHz\n", Fref, Fout);
1869 static int wm2200_dai_probe(
struct snd_soc_dai *dai)
1872 unsigned int val = 0;
1882 dev_err(codec->
dev,
"Failed to read GPIO 1 config: %d\n", ret);
1891 #define WM2200_RATES SNDRV_PCM_RATE_8000_48000
1893 #define WM2200_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1894 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1898 .probe = wm2200_dai_probe,
1900 .stream_name =
"Playback",
1907 .stream_name =
"Capture",
1913 .ops = &wm2200_dai_ops,
1917 .probe = wm2200_probe,
1919 .idle_bias_off =
true,
1920 .ignore_pmdown_time =
true,
1921 .set_sysclk = wm2200_set_sysclk,
1922 .set_pll = wm2200_set_fll,
1924 .controls = wm2200_snd_controls,
1925 .num_controls =
ARRAY_SIZE(wm2200_snd_controls),
1926 .dapm_widgets = wm2200_dapm_widgets,
1927 .num_dapm_widgets =
ARRAY_SIZE(wm2200_dapm_widgets),
1928 .dapm_routes = wm2200_dapm_routes,
1929 .num_dapm_routes =
ARRAY_SIZE(wm2200_dapm_routes),
1940 dev_err(wm2200->
dev,
"Failed to read IRQ status: %d\n", ret);
1947 dev_warn(wm2200->
dev,
"Failed to read IRQ mask: %d\n", ret);
1967 static const struct regmap_config wm2200_regmap = {
1972 .reg_defaults = wm2200_reg_defaults,
1973 .num_reg_defaults =
ARRAY_SIZE(wm2200_reg_defaults),
1974 .volatile_reg = wm2200_volatile_register,
1975 .readable_reg = wm2200_readable_register,
1979 static const unsigned int wm2200_dig_vu[] = {
1992 static const unsigned int wm2200_mic_ctrl_reg[] = {
2012 init_completion(&wm2200->
fll_lock);
2015 if (IS_ERR(wm2200->
regmap)) {
2016 ret = PTR_ERR(wm2200->
regmap);
2017 dev_err(&i2c->
dev,
"Failed to allocate register map: %d\n",
2025 i2c_set_clientdata(i2c, wm2200);
2028 wm2200->
core_supplies[i].supply = wm2200_core_supply_names[i];
2033 dev_err(&i2c->
dev,
"Failed to request core supplies: %d\n",
2041 dev_err(&i2c->
dev,
"Failed to enable core supplies: %d\n",
2046 if (wm2200->
pdata.ldo_ena) {
2050 dev_err(&i2c->
dev,
"Failed to request LDOENA %d: %d\n",
2051 wm2200->
pdata.ldo_ena, ret);
2057 if (wm2200->
pdata.reset) {
2061 dev_err(&i2c->
dev,
"Failed to request /RESET %d: %d\n",
2062 wm2200->
pdata.reset, ret);
2069 dev_err(&i2c->
dev,
"Failed to read ID register: %d\n", ret);
2077 dev_err(&i2c->
dev,
"Device is not a WM2200, ID is %x\n", reg);
2084 dev_err(&i2c->
dev,
"Failed to read revision register\n");
2092 switch (wm2200->
rev) {
2098 dev_err(&i2c->
dev,
"Failed to register patch: %d\n",
2106 ret = wm2200_reset(wm2200);
2108 dev_err(&i2c->
dev,
"Failed to issue reset\n");
2113 if (!wm2200->
pdata.gpio_defaults[i])
2117 wm2200->
pdata.gpio_defaults[i]);
2120 for (i = 0; i <
ARRAY_SIZE(wm2200_dig_vu); i++)
2125 for (i = 0; i < 6; i++) {
2134 (wm2200->
pdata.in_mode[i] <<
2136 (wm2200->
pdata.dmic_sup[i] <<
2147 WM2200_FLL_LOCK_EINT, 0);
2149 dev_err(&i2c->
dev,
"Failed to request IRQ %d: %d\n",
2153 pm_runtime_set_active(&i2c->
dev);
2155 pm_request_idle(&i2c->
dev);
2160 dev_err(&i2c->
dev,
"Failed to register CODEC: %d\n", ret);
2161 goto err_pm_runtime;
2167 pm_runtime_disable(&i2c->
dev);
2169 if (wm2200->
pdata.reset) {
2174 if (wm2200->
pdata.ldo_ena) {
2192 struct wm2200_priv *wm2200 = i2c_get_clientdata(i2c);
2197 if (wm2200->
pdata.reset) {
2201 if (wm2200->
pdata.ldo_ena) {
2212 #ifdef CONFIG_PM_RUNTIME
2213 static int wm2200_runtime_suspend(
struct device *dev)
2219 if (wm2200->
pdata.ldo_ena)
2227 static int wm2200_runtime_resume(
struct device *dev)
2235 dev_err(dev,
"Failed to enable supplies: %d\n",
2240 if (wm2200->
pdata.ldo_ena) {
2263 static struct i2c_driver wm2200_i2c_driver = {
2269 .probe = wm2200_i2c_probe,
2271 .id_table = wm2200_i2c_id,