14 #include <linux/module.h>
18 #include <linux/device.h>
20 #include <linux/i2c.h>
22 #include <linux/slab.h>
163 static bool wm9081_volatile_register(
struct device *
dev,
unsigned int reg)
174 static bool wm9081_readable_register(
struct device *dev,
unsigned int reg)
238 static int wm9081_reset(
struct regmap *
map)
246 static unsigned int drc_max_tlv[] = {
262 static const char *drc_high_text[] = {
271 static const struct soc_enum drc_high =
274 static const char *drc_low_text[] = {
282 static const struct soc_enum drc_low =
285 static const char *drc_atk_text[] = {
300 static const struct soc_enum drc_atk =
303 static const char *drc_dcy_text[] = {
315 static const struct soc_enum drc_dcy =
318 static const char *drc_qr_dcy_text[] = {
324 static const struct soc_enum drc_qr_dcy =
327 static const char *dac_deemph_text[] = {
334 static const struct soc_enum dac_deemph =
337 static const char *speaker_mode_text[] = {
342 static const struct soc_enum speaker_mode =
345 static int speaker_mode_get(
struct snd_kcontrol *kcontrol,
353 ucontrol->
value.integer.value[0] = 1;
355 ucontrol->
value.integer.value[0] = 0;
366 static int speaker_mode_put(
struct snd_kcontrol *kcontrol,
374 if (ucontrol->
value.integer.value[0] ==
382 if (ucontrol->
value.integer.value[0]) {
389 reg2 &= ~WM9081_SPK_MODE;
408 SOC_ENUM(
"DRC High Slope", drc_high),
418 SOC_ENUM(
"DRC Quick Release Decay", drc_qr_dcy),
429 SOC_ENUM(
"DAC Deemphasis", dac_deemph),
430 SOC_ENUM_EXT(
"Speaker Mode", speaker_mode, speaker_mode_get, speaker_mode_put),
457 #define FIXED_FLL_SIZE ((1 << 16) * 10)
466 { 64000, 128000, 3, 8 },
467 { 128000, 256000, 2, 4 },
468 { 256000, 1000000, 1, 2 },
469 { 1000000, 13500000, 0, 1 },
476 unsigned int K, Ndiv, Nmod,
target;
482 while ((Fref / div) > 13500000) {
486 pr_err(
"Can't scale %dMHz input down to <=13.5MHz\n",
493 pr_debug(
"Fref=%u Fout=%u\n", Fref, Fout);
501 while (target < 90000000) {
505 pr_err(
"Unable to find FLL_OUTDIV for Fout=%uHz\n",
515 for (i = 0; i <
ARRAY_SIZE(fll_fratios); i++) {
516 if (fll_fratios[i].
min <= Fref && Fref <= fll_fratios[i].
max) {
518 target /= fll_fratios[
i].ratio;
523 pr_err(
"Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
528 Ndiv = target / Fref;
531 Nmod = target % Fref;
539 K = Kpart & 0xFFFFFFFF;
547 pr_debug(
"N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
548 fll_div->
n, fll_div->
k,
555 static int wm9081_set_fll(
struct snd_soc_codec *codec,
int fll_id,
556 unsigned int Fref,
unsigned int Fout)
558 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
590 dev_err(codec->
dev,
"Unknown FLL ID %d\n", fll_id);
598 clk_sys_reg & ~WM9081_CLK_SYS_ENA);
635 if (clk_sys_reg & WM9081_CLK_SYS_ENA)
638 dev_dbg(codec->
dev,
"FLL enabled at %dHz->%dHz\n", Fref, Fout);
648 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
678 for (i = 0; i <
ARRAY_SIZE(clk_sys_rates); i++) {
679 target = wm9081->
fs * clk_sys_rates[
i].ratio;
681 if (target >= wm9081->
bclk &&
689 }
else if (wm9081->
fs) {
690 for (i = 0; i <
ARRAY_SIZE(clk_sys_rates); i++) {
691 new_sysclk = clk_sys_rates[
i].ratio
693 if (new_sysclk > 3000000)
701 new_sysclk = 12288000;
743 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
751 dev_dbg(codec->
dev,
"Using %dHz MCLK with FLL\n",
755 dev_err(codec->
dev,
"System clock not configured\n");
761 configure_clock(codec);
766 wm9081_set_fll(codec, 0, 0, 0);
800 {
"DAC",
NULL,
"CLK_SYS" },
801 {
"DAC",
NULL,
"CLK_DSP" },
802 {
"DAC",
NULL,
"AIF" },
804 {
"Mixer",
"IN1 Switch",
"IN1" },
805 {
"Mixer",
"IN2 Switch",
"IN2" },
806 {
"Mixer",
"Playback Switch",
"DAC" },
808 {
"LINEOUT PGA",
NULL,
"Mixer" },
809 {
"LINEOUT PGA",
NULL,
"TOCLK" },
810 {
"LINEOUT PGA",
NULL,
"CLK_SYS" },
812 {
"LINEOUT",
NULL,
"LINEOUT PGA" },
814 {
"Speaker PGA",
NULL,
"Mixer" },
815 {
"Speaker PGA",
NULL,
"TOCLK" },
816 {
"Speaker PGA",
NULL,
"CLK_SYS" },
818 {
"Speaker",
NULL,
"Speaker PGA" },
819 {
"Speaker",
NULL,
"TSENSE" },
821 {
"SPKN",
NULL,
"Speaker" },
822 {
"SPKP",
NULL,
"Speaker" },
825 static int wm9081_set_bias_level(
struct snd_soc_codec *codec,
828 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
915 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
959 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
977 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1007 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
1008 int ret,
i, best, best_val, cur_val;
1009 unsigned int clk_ctrl2, aif1, aif2, aif3, aif4;
1035 wm9081->
bclk = 2 * wm9081->
fs;
1060 ret = configure_clock(codec);
1068 for (i = 1; i <
ARRAY_SIZE(clk_sys_rates); i++) {
1070 clk_sys_rates[i].ratio) - wm9081->
fs);
1071 if (cur_val < best_val) {
1076 dev_dbg(codec->
dev,
"Selected CLK_SYS_RATIO of %d\n",
1077 clk_sys_rates[best].ratio);
1078 clk_ctrl2 |= (clk_sys_rates[best].clk_sys_rate
1083 best_val =
abs(wm9081->
fs - sample_rates[0].rate);
1084 for (i = 1; i <
ARRAY_SIZE(sample_rates); i++) {
1086 cur_val =
abs(wm9081->
fs - sample_rates[i].rate);
1087 if (cur_val < best_val) {
1092 dev_dbg(codec->
dev,
"Selected SAMPLE_RATE of %dHz\n",
1093 sample_rates[best].rate);
1094 clk_ctrl2 |= (sample_rates[best].sample_rate
1100 for (i = 0; i <
ARRAY_SIZE(bclk_divs); i++) {
1101 cur_val = ((wm9081->
sysclk_rate * 10) / bclk_divs[i].div)
1105 if (cur_val < best_val) {
1111 dev_dbg(codec->
dev,
"Selected BCLK_DIV of %d for %dHz BCLK\n",
1112 bclk_divs[best].div, wm9081->
bclk);
1113 aif3 |= bclk_divs[best].bclk_div;
1117 aif4 |= wm9081->
bclk / wm9081->
fs;
1120 if (wm9081->
pdata.num_retune_configs) {
1130 if (cur_val < best_val) {
1137 dev_dbg(codec->
dev,
"ReTune Mobile %s tuned for %dHz\n",
1149 eq1 |= (s->
config[0] & ~WM9081_EQ_ENA);
1161 static int wm9081_digital_mute(
struct snd_soc_dai *codec_dai,
int mute)
1178 static int wm9081_set_sysclk(
struct snd_soc_codec *codec,
int clk_id,
1181 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
1197 static int wm9081_set_tdm_slot(
struct snd_soc_dai *dai,
1201 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
1206 if (slots < 0 || slots > 4)
1237 #define WM9081_RATES SNDRV_PCM_RATE_8000_96000
1239 #define WM9081_FORMATS \
1240 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1241 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1244 .hw_params = wm9081_hw_params,
1245 .set_fmt = wm9081_set_dai_fmt,
1246 .digital_mute = wm9081_digital_mute,
1247 .set_tdm_slot = wm9081_set_tdm_slot,
1254 .name =
"wm9081-hifi",
1256 .stream_name =
"AIF",
1262 .ops = &wm9081_dai_ops,
1267 struct wm9081_priv *wm9081 = snd_soc_codec_get_drvdata(codec);
1274 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
1284 if (!wm9081->
pdata.num_retune_configs) {
1286 "No ReTune Mobile data, using normal EQ\n");
1301 .probe = wm9081_probe,
1302 .remove = wm9081_remove,
1304 .set_sysclk = wm9081_set_sysclk,
1305 .set_bias_level = wm9081_set_bias_level,
1307 .idle_bias_off =
true,
1309 .controls = wm9081_snd_controls,
1310 .num_controls =
ARRAY_SIZE(wm9081_snd_controls),
1311 .dapm_widgets = wm9081_dapm_widgets,
1312 .num_dapm_widgets =
ARRAY_SIZE(wm9081_dapm_widgets),
1313 .dapm_routes = wm9081_audio_paths,
1314 .num_dapm_routes =
ARRAY_SIZE(wm9081_audio_paths),
1317 static const struct regmap_config wm9081_regmap = {
1322 .reg_defaults = wm9081_reg,
1324 .volatile_reg = wm9081_volatile_register,
1325 .readable_reg = wm9081_readable_register,
1329 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1342 i2c_set_clientdata(i2c, wm9081);
1345 if (IS_ERR(wm9081->
regmap)) {
1346 ret = PTR_ERR(wm9081->
regmap);
1347 dev_err(&i2c->
dev,
"regmap_init() failed: %d\n", ret);
1353 dev_err(&i2c->
dev,
"Failed to read chip ID: %d\n", ret);
1356 if (reg != 0x9081) {
1357 dev_err(&i2c->
dev,
"Device is not a WM9081: ID=0x%x\n", reg);
1362 ret = wm9081_reset(wm9081->
regmap);
1364 dev_err(&i2c->
dev,
"Failed to issue reset\n");
1368 if (dev_get_platdata(&i2c->
dev))
1370 sizeof(wm9081->
pdata));
1373 if (wm9081->
pdata.irq_high)
1375 if (!wm9081->
pdata.irq_cmos)
1383 &soc_codec_dev_wm9081, &wm9081_dai, 1);
1398 struct wm9081_priv *wm9081 = i2c_get_clientdata(client);
1411 static struct i2c_driver wm9081_i2c_driver = {
1416 .probe = wm9081_i2c_probe,
1418 .id_table = wm9081_i2c_id,