19 #include <linux/module.h>
21 #include <linux/kernel.h>
25 #include <linux/i2c.h>
28 #include <linux/slab.h>
37 #include <asm/div64.h>
42 #define WM8580_PLLA1 0x00
43 #define WM8580_PLLA2 0x01
44 #define WM8580_PLLA3 0x02
45 #define WM8580_PLLA4 0x03
46 #define WM8580_PLLB1 0x04
47 #define WM8580_PLLB2 0x05
48 #define WM8580_PLLB3 0x06
49 #define WM8580_PLLB4 0x07
50 #define WM8580_CLKSEL 0x08
51 #define WM8580_PAIF1 0x09
52 #define WM8580_PAIF2 0x0A
53 #define WM8580_SAIF1 0x0B
54 #define WM8580_PAIF3 0x0C
55 #define WM8580_PAIF4 0x0D
56 #define WM8580_SAIF2 0x0E
57 #define WM8580_DAC_CONTROL1 0x0F
58 #define WM8580_DAC_CONTROL2 0x10
59 #define WM8580_DAC_CONTROL3 0x11
60 #define WM8580_DAC_CONTROL4 0x12
61 #define WM8580_DAC_CONTROL5 0x13
62 #define WM8580_DIGITAL_ATTENUATION_DACL1 0x14
63 #define WM8580_DIGITAL_ATTENUATION_DACR1 0x15
64 #define WM8580_DIGITAL_ATTENUATION_DACL2 0x16
65 #define WM8580_DIGITAL_ATTENUATION_DACR2 0x17
66 #define WM8580_DIGITAL_ATTENUATION_DACL3 0x18
67 #define WM8580_DIGITAL_ATTENUATION_DACR3 0x19
68 #define WM8580_MASTER_DIGITAL_ATTENUATION 0x1C
69 #define WM8580_ADC_CONTROL1 0x1D
70 #define WM8580_SPDTXCHAN0 0x1E
71 #define WM8580_SPDTXCHAN1 0x1F
72 #define WM8580_SPDTXCHAN2 0x20
73 #define WM8580_SPDTXCHAN3 0x21
74 #define WM8580_SPDTXCHAN4 0x22
75 #define WM8580_SPDTXCHAN5 0x23
76 #define WM8580_SPDMODE 0x24
77 #define WM8580_INTMASK 0x25
78 #define WM8580_GPO1 0x26
79 #define WM8580_GPO2 0x27
80 #define WM8580_GPO3 0x28
81 #define WM8580_GPO4 0x29
82 #define WM8580_GPO5 0x2A
83 #define WM8580_INTSTAT 0x2B
84 #define WM8580_SPDRXCHAN1 0x2C
85 #define WM8580_SPDRXCHAN2 0x2D
86 #define WM8580_SPDRXCHAN3 0x2E
87 #define WM8580_SPDRXCHAN4 0x2F
88 #define WM8580_SPDRXCHAN5 0x30
89 #define WM8580_SPDSTAT 0x31
90 #define WM8580_PWRDN1 0x32
91 #define WM8580_PWRDN2 0x33
92 #define WM8580_READBACK 0x34
93 #define WM8580_RESET 0x35
95 #define WM8580_MAX_REGISTER 0x35
97 #define WM8580_DACOSR 0x40
100 #define WM8580_PLLB4_MCLKOUTSRC_MASK 0x60
101 #define WM8580_PLLB4_MCLKOUTSRC_PLLA 0x20
102 #define WM8580_PLLB4_MCLKOUTSRC_PLLB 0x40
103 #define WM8580_PLLB4_MCLKOUTSRC_OSC 0x60
105 #define WM8580_PLLB4_CLKOUTSRC_MASK 0x180
106 #define WM8580_PLLB4_CLKOUTSRC_PLLACLK 0x080
107 #define WM8580_PLLB4_CLKOUTSRC_PLLBCLK 0x100
108 #define WM8580_PLLB4_CLKOUTSRC_OSCCLK 0x180
111 #define WM8580_CLKSEL_DAC_CLKSEL_MASK 0x03
112 #define WM8580_CLKSEL_DAC_CLKSEL_PLLA 0x01
113 #define WM8580_CLKSEL_DAC_CLKSEL_PLLB 0x02
116 #define WM8580_AIF_RATE_MASK 0x7
117 #define WM8580_AIF_BCLKSEL_MASK 0x18
119 #define WM8580_AIF_MS 0x20
121 #define WM8580_AIF_CLKSRC_MASK 0xc0
122 #define WM8580_AIF_CLKSRC_PLLA 0x40
123 #define WM8580_AIF_CLKSRC_PLLB 0x40
124 #define WM8580_AIF_CLKSRC_MCLK 0xc0
127 #define WM8580_AIF_FMT_MASK 0x03
128 #define WM8580_AIF_FMT_RIGHTJ 0x00
129 #define WM8580_AIF_FMT_LEFTJ 0x01
130 #define WM8580_AIF_FMT_I2S 0x02
131 #define WM8580_AIF_FMT_DSP 0x03
133 #define WM8580_AIF_LENGTH_MASK 0x0c
134 #define WM8580_AIF_LENGTH_16 0x00
135 #define WM8580_AIF_LENGTH_20 0x04
136 #define WM8580_AIF_LENGTH_24 0x08
137 #define WM8580_AIF_LENGTH_32 0x0c
139 #define WM8580_AIF_LRP 0x10
140 #define WM8580_AIF_BCP 0x20
143 #define WM8580_PWRDN1_PWDN 0x001
144 #define WM8580_PWRDN1_ALLDACPD 0x040
147 #define WM8580_PWRDN2_OSSCPD 0x001
148 #define WM8580_PWRDN2_PLLAPD 0x002
149 #define WM8580_PWRDN2_PLLBPD 0x004
150 #define WM8580_PWRDN2_SPDIFPD 0x008
151 #define WM8580_PWRDN2_SPDIFTXD 0x010
152 #define WM8580_PWRDN2_SPDIFRXD 0x020
154 #define WM8580_DAC_CONTROL5_MUTEALL 0x10
161 static const struct reg_default wm8580_reg_defaults[] = {
217 static bool wm8580_volatile(
struct device *
dev,
unsigned int reg)
232 #define WM8580_NUM_SUPPLIES 3
257 unsigned int reg = mc->
reg;
328 {
"VOUT1L",
NULL,
"DAC1" },
329 {
"VOUT1R",
NULL,
"DAC1" },
331 {
"VOUT2L",
NULL,
"DAC2" },
332 {
"VOUT2R",
NULL,
"DAC2" },
334 {
"VOUT3L",
NULL,
"DAC3" },
335 {
"VOUT3R",
NULL,
"DAC3" },
337 {
"ADC",
NULL,
"AINL" },
338 {
"ADC",
NULL,
"AINR" },
351 #define FIXED_PLL_SIZE (1 << 22)
373 unsigned int K, Ndiv, Nmod;
376 pr_debug(
"wm8580: PLL %uHz->%uHz\n", source, target);
381 for (i = 0; i <
ARRAY_SIZE(post_table); i++) {
382 if (target * post_table[i].
div >= 90000000 &&
383 target * post_table[i].
div <= 100000000) {
384 pll_div->
freqmode = post_table[
i].freqmode;
386 target *= post_table[
i].div;
406 if ((Ndiv < 5) || (Ndiv > 13)) {
408 "WM8580 N=%u outside supported range\n", Ndiv);
418 K = Kpart & 0xFFFFFFFF;
422 pr_debug(
"PLL %x.%x prescale %d freqmode %d postscale %d\n",
429 static int wm8580_set_dai_pll(
struct snd_soc_dai *codec_dai,
int pll_id,
430 int source,
unsigned int freq_in,
unsigned int freq_out)
434 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
438 unsigned int pwr_mask;
444 memset(&pll_div, 0,
sizeof(pll_div));
461 if (freq_in && freq_out) {
468 state->
out = freq_out;
475 if (!freq_in || !freq_out)
481 (pll_div.
k >> 18 & 0xf) | (pll_div.
n << 4));
496 static const int wm8580_sysclk_ratios[] = {
497 128, 192, 256, 384, 512, 768, 1152,
509 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
537 for (i = 0; i <
ARRAY_SIZE(wm8580_sysclk_ratios); i++)
538 if (ratio == wm8580_sysclk_ratios[i])
541 dev_err(codec->
dev,
"Invalid clock ratio %d/%d\n",
546 dev_dbg(codec->
dev,
"Running at %dfs with %dHz clock\n",
547 wm8580_sysclk_ratios[i], wm8580->
sysclk[dai->
driver->id]);
573 static int wm8580_set_paif_dai_fmt(
struct snd_soc_dai *codec_dai,
579 int can_invert_lrclk;
599 can_invert_lrclk = 1;
603 can_invert_lrclk = 1;
607 can_invert_lrclk = 1;
611 can_invert_lrclk = 0;
615 can_invert_lrclk = 0;
628 if (!can_invert_lrclk)
639 if (!can_invert_lrclk)
654 static int wm8580_set_dai_clkdiv(
struct snd_soc_dai *codec_dai,
720 static int wm8580_set_sysclk(
struct snd_soc_dai *dai,
int clk_id,
721 unsigned int freq,
int dir)
724 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
725 int ret,
sel, sel_mask, sel_shift;
727 switch (dai->
driver->id) {
739 BUG_ON(
"Unknown DAI driver ID\n");
747 sel = 0 << sel_shift;
750 sel = 1 << sel_shift;
753 sel = 2 << sel_shift;
756 sel = 3 << sel_shift;
759 dev_err(codec->
dev,
"Unknown clock %d\n", clk_id);
773 static int wm8580_digital_mute(
struct snd_soc_dai *codec_dai,
int mute)
790 static int wm8580_set_bias_level(
struct snd_soc_codec *codec,
820 #define WM8580_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
821 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
824 .set_sysclk = wm8580_set_sysclk,
825 .hw_params = wm8580_paif_hw_params,
826 .set_fmt = wm8580_set_paif_dai_fmt,
827 .set_clkdiv = wm8580_set_dai_clkdiv,
828 .set_pll = wm8580_set_dai_pll,
829 .digital_mute = wm8580_digital_mute,
833 .set_sysclk = wm8580_set_sysclk,
834 .hw_params = wm8580_paif_hw_params,
835 .set_fmt = wm8580_set_paif_dai_fmt,
836 .set_clkdiv = wm8580_set_dai_clkdiv,
837 .set_pll = wm8580_set_dai_pll,
842 .name =
"wm8580-hifi-playback",
845 .stream_name =
"Playback",
851 .ops = &wm8580_dai_ops_playback,
854 .name =
"wm8580-hifi-capture",
857 .stream_name =
"Capture",
863 .ops = &wm8580_dai_ops_capture,
869 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
874 dev_err(codec->
dev,
"Failed to set cache I/O: %d\n", ret);
881 dev_err(codec->
dev,
"Failed to enable supplies: %d\n", ret);
882 goto err_regulator_get;
888 dev_err(codec->
dev,
"Failed to reset codec: %d\n", ret);
889 goto err_regulator_enable;
896 err_regulator_enable:
905 struct wm8580_priv *wm8580 = snd_soc_codec_get_drvdata(codec);
915 .probe = wm8580_probe,
916 .remove = wm8580_remove,
917 .set_bias_level = wm8580_set_bias_level,
919 .controls = wm8580_snd_controls,
920 .num_controls =
ARRAY_SIZE(wm8580_snd_controls),
921 .dapm_widgets = wm8580_dapm_widgets,
922 .num_dapm_widgets =
ARRAY_SIZE(wm8580_dapm_widgets),
923 .dapm_routes = wm8580_dapm_routes,
924 .num_dapm_routes =
ARRAY_SIZE(wm8580_dapm_routes),
928 { .compatible =
"wlf,wm8580" },
932 static const struct regmap_config wm8580_regmap = {
937 .reg_defaults = wm8580_reg_defaults,
938 .num_reg_defaults =
ARRAY_SIZE(wm8580_reg_defaults),
941 .volatile_reg = wm8580_volatile,
944 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
945 static int wm8580_i2c_probe(
struct i2c_client *i2c,
957 if (IS_ERR(wm8580->
regmap))
958 return PTR_ERR(wm8580->
regmap);
961 wm8580->
supplies[i].supply = wm8580_supply_names[i];
966 dev_err(&i2c->
dev,
"Failed to request supplies: %d\n", ret);
970 i2c_set_clientdata(i2c, wm8580);
973 &soc_codec_dev_wm8580, wm8580_dai,
ARRAY_SIZE(wm8580_dai));
990 static struct i2c_driver wm8580_i2c_driver = {
994 .of_match_table = wm8580_of_match,
996 .probe = wm8580_i2c_probe,
997 .remove = wm8580_i2c_remove,
998 .id_table = wm8580_i2c_id,
1002 static int __init wm8580_modinit(
void)
1006 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1007 ret = i2c_add_driver(&wm8580_i2c_driver);
1009 pr_err(
"Failed to register WM8580 I2C driver: %d\n", ret);
1017 static void __exit wm8580_exit(
void)
1019 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)