13 #include <linux/module.h>
18 #include <linux/i2c.h>
22 #include <linux/slab.h>
34 #define WM8993_NUM_SUPPLIES 6
156 } clk_sys_rates[] = {
226 static bool wm8993_volatile(
struct device *
dev,
unsigned int reg)
241 static bool wm8993_readable(
struct device *
dev,
unsigned int reg)
370 #define FIXED_FLL_SIZE ((1 << 16) * 10)
379 { 64000, 128000, 3, 8 },
380 { 128000, 256000, 2, 4 },
381 { 256000, 1000000, 1, 2 },
382 { 1000000, 13500000, 0, 1 },
389 unsigned int K, Ndiv, Nmod,
target;
396 while ((Fref / div) > 13500000) {
401 pr_err(
"Can't scale %dMHz input down to <=13.5MHz\n",
407 pr_debug(
"Fref=%u Fout=%u\n", Fref, Fout);
415 while (target < 90000000) {
419 pr_err(
"Unable to find FLL_OUTDIV for Fout=%uHz\n",
429 for (i = 0; i <
ARRAY_SIZE(fll_fratios); i++) {
430 if (fll_fratios[i].
min <= Fref && Fref <= fll_fratios[i].
max) {
432 target /= fll_fratios[
i].ratio;
437 pr_err(
"Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
442 Ndiv = target / Fref;
445 Nmod = target % Fref;
453 K = Kpart & 0xFFFFFFFF;
461 pr_debug(
"N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
462 fll_div->
n, fll_div->
k,
470 unsigned int Fref,
unsigned int Fout)
472 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
516 dev_err(codec->
dev,
"Unknown FLL ID %d\n", fll_id);
550 else if (Fref < 1000000)
561 if (i2c->
irq && !timeout)
562 dev_warn(codec->
dev,
"Timed out waiting for FLL\n");
564 dev_dbg(codec->
dev,
"FLL enabled at %dHz->%dHz\n", Fref, Fout);
573 static int wm8993_set_fll(
struct snd_soc_dai *
dai,
int fll_id,
int source,
574 unsigned int Fref,
unsigned int Fout)
576 return _wm8993_set_fll(dai->
codec, fll_id, source, Fref, Fout);
581 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
618 dev_err(codec->
dev,
"System clock not configured\n");
631 static const unsigned int drc_max_tlv[] = {
642 static const char *dac_deemph_text[] = {
649 static const struct soc_enum dac_deemph =
652 static const char *adc_hpf_text[] = {
659 static const struct soc_enum adc_hpf =
662 static const char *drc_path_text[] = {
667 static const struct soc_enum drc_path =
670 static const char *drc_r0_text[] = {
679 static const struct soc_enum drc_r0 =
682 static const char *drc_r1_text[] = {
690 static const struct soc_enum drc_r1 =
693 static const char *drc_attack_text[] = {
708 static const struct soc_enum drc_attack =
711 static const char *drc_decay_text[] = {
723 static const struct soc_enum drc_decay =
726 static const char *drc_ff_text[] = {
731 static const struct soc_enum drc_ff =
734 static const char *drc_qr_rate_text[] = {
740 static const struct soc_enum drc_qr_rate =
743 static const char *drc_smooth_text[] = {
749 static const struct soc_enum drc_smooth =
754 5, 9, 12, 0, sidetone_tlv),
759 2, 60, 1, drc_comp_threash),
761 11, 30, 1, drc_comp_amp),
768 SOC_ENUM(
"DRC Attack Rate", drc_attack),
769 SOC_ENUM(
"DRC Decay Rate", drc_decay),
775 SOC_ENUM(
"DRC Quick Release Rate", drc_qr_rate),
778 SOC_ENUM(
"DRC Smoothing Hysteresis Threshold", drc_smooth),
787 SOC_ENUM(
"ADC High Pass Filter Mode", adc_hpf),
793 SOC_ENUM(
"DAC Deemphasis", dac_deemph),
796 2, 1, 1, wm_hubs_spkmix_tlv),
799 2, 1, 1, wm_hubs_spkmix_tlv),
817 return configure_clock(codec);
840 static const char *aif_text[] = {
844 static const struct soc_enum aifoutl_enum =
850 static const struct soc_enum aifoutr_enum =
856 static const struct soc_enum aifinl_enum =
862 static const struct soc_enum aifinr_enum =
868 static const char *sidetone_text[] = {
869 "None",
"Left",
"Right"
872 static const struct soc_enum sidetonel_enum =
878 static const struct soc_enum sidetoner_enum =
916 left_speaker_mixer,
ARRAY_SIZE(left_speaker_mixer)),
918 right_speaker_mixer,
ARRAY_SIZE(right_speaker_mixer)),
923 {
"MICBIAS1",
NULL,
"VMID" },
924 {
"MICBIAS2",
NULL,
"VMID" },
926 {
"ADCL",
NULL,
"CLK_SYS" },
927 {
"ADCL",
NULL,
"CLK_DSP" },
928 {
"ADCR",
NULL,
"CLK_SYS" },
929 {
"ADCR",
NULL,
"CLK_DSP" },
931 {
"AIFOUTL Mux",
"Left",
"ADCL" },
932 {
"AIFOUTL Mux",
"Right",
"ADCR" },
933 {
"AIFOUTR Mux",
"Left",
"ADCL" },
934 {
"AIFOUTR Mux",
"Right",
"ADCR" },
936 {
"AIFOUTL",
NULL,
"AIFOUTL Mux" },
937 {
"AIFOUTR",
NULL,
"AIFOUTR Mux" },
939 {
"DACL Mux",
"Left",
"AIFINL" },
940 {
"DACL Mux",
"Right",
"AIFINR" },
941 {
"DACR Mux",
"Left",
"AIFINL" },
942 {
"DACR Mux",
"Right",
"AIFINR" },
944 {
"DACL Sidetone",
"Left",
"ADCL" },
945 {
"DACL Sidetone",
"Right",
"ADCR" },
946 {
"DACR Sidetone",
"Left",
"ADCL" },
947 {
"DACR Sidetone",
"Right",
"ADCR" },
949 {
"DACL",
NULL,
"CLK_SYS" },
950 {
"DACL",
NULL,
"CLK_DSP" },
951 {
"DACL",
NULL,
"DACL Mux" },
952 {
"DACL",
NULL,
"DACL Sidetone" },
953 {
"DACR",
NULL,
"CLK_SYS" },
954 {
"DACR",
NULL,
"CLK_DSP" },
955 {
"DACR",
NULL,
"DACR Mux" },
956 {
"DACR",
NULL,
"DACR Sidetone" },
958 {
"Left Output Mixer",
"DAC Switch",
"DACL" },
960 {
"Right Output Mixer",
"DAC Switch",
"DACR" },
962 {
"Left Output PGA",
NULL,
"CLK_SYS" },
964 {
"Right Output PGA",
NULL,
"CLK_SYS" },
966 {
"SPKL",
"DAC Switch",
"DACL" },
967 {
"SPKL",
NULL,
"CLK_SYS" },
969 {
"SPKR",
"DAC Switch",
"DACR" },
970 {
"SPKR",
NULL,
"CLK_SYS" },
972 {
"Left Headphone Mux",
"DAC",
"DACL" },
973 {
"Right Headphone Mux",
"DAC",
"DACR" },
976 static int wm8993_set_bias_level(
struct snd_soc_codec *codec,
979 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1019 if (!wm8993->
pdata.lineout1_diff ||
1020 !wm8993->
pdata.lineout2_diff)
1073 static int wm8993_set_sysclk(
struct snd_soc_dai *codec_dai,
1074 int clk_id,
unsigned int freq,
int dir)
1077 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1097 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1144 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1162 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1193 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1194 int ret,
i, best, best_val, cur_val;
1195 unsigned int clocking1, clocking3, aif1, aif4;
1211 wm8993->
bclk = 2 * wm8993->
fs;
1213 dev_dbg(codec->
dev,
"Configuring for %d %d bit TDM slots\n",
1240 ret = configure_clock(codec);
1248 for (i = 1; i <
ARRAY_SIZE(clk_sys_rates); i++) {
1250 clk_sys_rates[i].ratio) - wm8993->
fs);
1251 if (cur_val < best_val) {
1256 dev_dbg(codec->
dev,
"Selected CLK_SYS_RATIO of %d\n",
1257 clk_sys_rates[best].ratio);
1258 clocking3 |= (clk_sys_rates[best].clk_sys_rate
1263 best_val =
abs(wm8993->
fs - sample_rates[0].rate);
1264 for (i = 1; i <
ARRAY_SIZE(sample_rates); i++) {
1266 cur_val =
abs(wm8993->
fs - sample_rates[i].rate);
1267 if (cur_val < best_val) {
1272 dev_dbg(codec->
dev,
"Selected SAMPLE_RATE of %dHz\n",
1273 sample_rates[best].rate);
1274 clocking3 |= (sample_rates[best].sample_rate
1280 for (i = 0; i <
ARRAY_SIZE(bclk_divs); i++) {
1281 cur_val = ((wm8993->
sysclk_rate * 10) / bclk_divs[i].div)
1285 if (cur_val < best_val) {
1291 dev_dbg(codec->
dev,
"Selected BCLK_DIV of %d for %dHz BCLK\n",
1292 bclk_divs[best].div, wm8993->
bclk);
1297 aif4 |= wm8993->
bclk / wm8993->
fs;
1305 if (wm8993->
pdata.num_retune_configs) {
1310 best_val =
abs(wm8993->
pdata.retune_configs[0].rate
1312 for (i = 0; i < wm8993->
pdata.num_retune_configs; i++) {
1313 cur_val =
abs(wm8993->
pdata.retune_configs[i].rate
1315 if (cur_val < best_val) {
1320 s = &wm8993->
pdata.retune_configs[best];
1322 dev_dbg(codec->
dev,
"ReTune Mobile %s tuned for %dHz\n",
1337 static int wm8993_digital_mute(
struct snd_soc_dai *codec_dai,
int mute)
1358 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1415 dev_err(wm8993->
dev,
"Failed to read interrupt status: %d\n",
1422 dev_err(wm8993->
dev,
"Failed to read interrupt mask: %d\n",
1433 dev_crit(wm8993->
dev,
"Thermal warning\n");
1442 dev_err(wm8993->
dev,
"Failed to ack interrupt: %d\n", ret);
1448 .set_sysclk = wm8993_set_sysclk,
1449 .set_fmt = wm8993_set_dai_fmt,
1450 .hw_params = wm8993_hw_params,
1451 .digital_mute = wm8993_digital_mute,
1452 .set_pll = wm8993_set_fll,
1453 .set_tdm_slot = wm8993_set_tdm_slot,
1456 #define WM8993_RATES SNDRV_PCM_RATE_8000_48000
1458 #define WM8993_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
1459 SNDRV_PCM_FMTBIT_S20_3LE |\
1460 SNDRV_PCM_FMTBIT_S24_LE |\
1461 SNDRV_PCM_FMTBIT_S32_LE)
1464 .name =
"wm8993-hifi",
1466 .stream_name =
"Playback",
1474 .stream_name =
"Capture",
1482 .symmetric_rates = 1,
1487 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1499 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
1518 wm8993->
pdata.lineout2_diff,
1519 wm8993->
pdata.lineout1fb,
1520 wm8993->
pdata.lineout2fb,
1521 wm8993->
pdata.jd_scthr,
1522 wm8993->
pdata.jd_thr,
1523 wm8993->
pdata.micbias1_delay,
1524 wm8993->
pdata.micbias2_delay,
1525 wm8993->
pdata.micbias1_lvl,
1526 wm8993->
pdata.micbias2_lvl);
1534 if (wm8993->
pdata.num_retune_configs != 0) {
1535 dev_dbg(codec->
dev,
"Using ReTune Mobile\n");
1537 dev_dbg(codec->
dev,
"No ReTune Mobile, using normal EQ\n");
1548 wm8993->
pdata.lineout2_diff);
1553 if (wm8993->
pdata.lineout1_diff && wm8993->
pdata.lineout2_diff)
1554 codec->
dapm.idle_bias_off = 1;
1562 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1572 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1578 ret = _wm8993_set_fll(codec, 0, 0, 0, 0);
1594 struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
1607 ret = _wm8993_set_fll(codec, 0, wm8993->
fll_src,
1608 fll_fref, fll_fout);
1610 dev_err(codec->
dev,
"Failed to restart FLL\n");
1616 #define wm8993_suspend NULL
1617 #define wm8993_resume NULL
1621 static struct reg_default wm8993_regmap_patch[] = {
1627 static const struct regmap_config wm8993_regmap = {
1632 .volatile_reg = wm8993_volatile,
1633 .readable_reg = wm8993_readable,
1636 .reg_defaults = wm8993_reg_defaults,
1637 .num_reg_defaults =
ARRAY_SIZE(wm8993_reg_defaults),
1641 .probe = wm8993_probe,
1642 .remove = wm8993_remove,
1645 .set_bias_level = wm8993_set_bias_level,
1661 init_completion(&wm8993->
fll_lock);
1664 if (IS_ERR(wm8993->
regmap)) {
1665 ret = PTR_ERR(wm8993->
regmap);
1666 dev_err(&i2c->
dev,
"Failed to allocate regmap: %d\n", ret);
1670 i2c_set_clientdata(i2c, wm8993);
1673 wm8993->
supplies[i].supply = wm8993_supply_names[i];
1678 dev_err(&i2c->
dev,
"Failed to request supplies: %d\n", ret);
1685 dev_err(&i2c->
dev,
"Failed to enable supplies: %d\n", ret);
1691 dev_err(&i2c->
dev,
"Failed to read chip ID: %d\n", ret);
1695 if (reg != 0x8993) {
1696 dev_err(&i2c->
dev,
"Invalid ID register value %x\n", reg);
1708 dev_warn(wm8993->
dev,
"Failed to apply regmap patch: %d\n",
1732 &soc_codec_dev_wm8993, &wm8993_dai, 1);
1734 dev_err(&i2c->
dev,
"Failed to register CODEC: %d\n", ret);
1754 struct wm8993_priv *wm8993 = i2c_get_clientdata(i2c);
1772 static struct i2c_driver wm8993_i2c_driver = {
1777 .probe = wm8993_i2c_probe,
1779 .id_table = wm8993_i2c_id,