Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5d421516 MB |
2 | /* |
3 | * wm8510.c -- WM8510 ALSA Soc Audio driver | |
4 | * | |
5 | * Copyright 2006 Wolfson Microelectronics PLC. | |
6 | * | |
d331124d | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
5d421516 MB |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/moduleparam.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/delay.h> | |
15 | #include <linux/pm.h> | |
16 | #include <linux/i2c.h> | |
5e357952 | 17 | #include <linux/spi/spi.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
0a422e1a | 19 | #include <linux/of_device.h> |
e643049d | 20 | #include <linux/regmap.h> |
5d421516 MB |
21 | #include <sound/core.h> |
22 | #include <sound/pcm.h> | |
23 | #include <sound/pcm_params.h> | |
24 | #include <sound/soc.h> | |
5d421516 MB |
25 | #include <sound/initval.h> |
26 | ||
27 | #include "wm8510.h" | |
28 | ||
5d421516 MB |
29 | /* |
30 | * wm8510 register cache | |
31 | * We can't read the WM8510 register space when we are | |
32 | * using 2 wire for device control, so we cache them instead. | |
33 | */ | |
e643049d MB |
34 | static const struct reg_default wm8510_reg_defaults[] = { |
35 | { 1, 0x0000 }, | |
36 | { 2, 0x0000 }, | |
37 | { 3, 0x0000 }, | |
38 | { 4, 0x0050 }, | |
39 | { 5, 0x0000 }, | |
40 | { 6, 0x0140 }, | |
41 | { 7, 0x0000 }, | |
42 | { 8, 0x0000 }, | |
43 | { 9, 0x0000 }, | |
44 | { 10, 0x0000 }, | |
45 | { 11, 0x00ff }, | |
46 | { 12, 0x0000 }, | |
47 | { 13, 0x0000 }, | |
48 | { 14, 0x0100 }, | |
49 | { 15, 0x00ff }, | |
50 | { 16, 0x0000 }, | |
51 | { 17, 0x0000 }, | |
52 | { 18, 0x012c }, | |
53 | { 19, 0x002c }, | |
54 | { 20, 0x002c }, | |
55 | { 21, 0x002c }, | |
56 | { 22, 0x002c }, | |
57 | { 23, 0x0000 }, | |
58 | { 24, 0x0032 }, | |
59 | { 25, 0x0000 }, | |
60 | { 26, 0x0000 }, | |
61 | { 27, 0x0000 }, | |
62 | { 28, 0x0000 }, | |
63 | { 29, 0x0000 }, | |
64 | { 30, 0x0000 }, | |
65 | { 31, 0x0000 }, | |
66 | { 32, 0x0038 }, | |
67 | { 33, 0x000b }, | |
68 | { 34, 0x0032 }, | |
69 | { 35, 0x0000 }, | |
70 | { 36, 0x0008 }, | |
71 | { 37, 0x000c }, | |
72 | { 38, 0x0093 }, | |
73 | { 39, 0x00e9 }, | |
74 | { 40, 0x0000 }, | |
75 | { 41, 0x0000 }, | |
76 | { 42, 0x0000 }, | |
77 | { 43, 0x0000 }, | |
78 | { 44, 0x0003 }, | |
79 | { 45, 0x0010 }, | |
80 | { 46, 0x0000 }, | |
81 | { 47, 0x0000 }, | |
82 | { 48, 0x0000 }, | |
83 | { 49, 0x0002 }, | |
84 | { 50, 0x0001 }, | |
85 | { 51, 0x0000 }, | |
86 | { 52, 0x0000 }, | |
87 | { 53, 0x0000 }, | |
88 | { 54, 0x0039 }, | |
89 | { 55, 0x0000 }, | |
90 | { 56, 0x0001 }, | |
5d421516 MB |
91 | }; |
92 | ||
e643049d MB |
93 | static bool wm8510_volatile(struct device *dev, unsigned int reg) |
94 | { | |
95 | switch (reg) { | |
96 | case WM8510_RESET: | |
97 | return true; | |
98 | default: | |
99 | return false; | |
100 | } | |
101 | } | |
102 | ||
09af98b0 MB |
103 | #define WM8510_POWER1_BIASEN 0x08 |
104 | #define WM8510_POWER1_BUFIOEN 0x10 | |
105 | ||
51bf02cc | 106 | #define wm8510_reset(c) snd_soc_component_write(c, WM8510_RESET, 0) |
5d421516 | 107 | |
f0fba2ad LG |
108 | /* codec private data */ |
109 | struct wm8510_priv { | |
e643049d | 110 | struct regmap *regmap; |
f0fba2ad LG |
111 | }; |
112 | ||
5d421516 MB |
113 | static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" }; |
114 | static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" }; | |
115 | static const char *wm8510_alc[] = { "ALC", "Limiter" }; | |
116 | ||
117 | static const struct soc_enum wm8510_enum[] = { | |
118 | SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */ | |
119 | SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */ | |
120 | SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp), | |
121 | SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc), | |
122 | }; | |
123 | ||
124 | static const struct snd_kcontrol_new wm8510_snd_controls[] = { | |
125 | ||
126 | SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0), | |
127 | ||
128 | SOC_ENUM("DAC Companding", wm8510_enum[1]), | |
129 | SOC_ENUM("ADC Companding", wm8510_enum[0]), | |
130 | ||
131 | SOC_ENUM("Playback De-emphasis", wm8510_enum[2]), | |
132 | SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0), | |
133 | ||
134 | SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0), | |
135 | ||
136 | SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0), | |
137 | SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0), | |
138 | SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0), | |
139 | ||
140 | SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0), | |
141 | ||
142 | SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0), | |
143 | SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0), | |
144 | SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0), | |
145 | ||
146 | SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0), | |
147 | SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0), | |
148 | ||
149 | SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0), | |
150 | SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0), | |
151 | SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0), | |
152 | ||
153 | SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0), | |
154 | SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0), | |
155 | SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0), | |
156 | ||
157 | SOC_ENUM("ALC Capture Mode", wm8510_enum[3]), | |
158 | SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0), | |
159 | SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0), | |
160 | ||
161 | SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0), | |
162 | SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0), | |
163 | ||
164 | SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0), | |
165 | SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0), | |
166 | ||
167 | SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0), | |
168 | SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1), | |
169 | SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0), | |
170 | SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0), | |
171 | ||
172 | SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0), | |
173 | SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1), | |
174 | }; | |
175 | ||
5d421516 MB |
176 | /* Speaker Output Mixer */ |
177 | static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = { | |
178 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0), | |
179 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0), | |
180 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0), | |
181 | }; | |
182 | ||
183 | /* Mono Output Mixer */ | |
184 | static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = { | |
185 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0), | |
186 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0), | |
187 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0), | |
188 | }; | |
189 | ||
190 | static const struct snd_kcontrol_new wm8510_boost_controls[] = { | |
8ae23ec3 | 191 | SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1), |
5d421516 MB |
192 | SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0), |
193 | SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0), | |
194 | }; | |
195 | ||
196 | static const struct snd_kcontrol_new wm8510_micpga_controls[] = { | |
197 | SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0), | |
198 | SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0), | |
199 | SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0), | |
200 | }; | |
201 | ||
202 | static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = { | |
203 | SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0, | |
204 | &wm8510_speaker_mixer_controls[0], | |
205 | ARRAY_SIZE(wm8510_speaker_mixer_controls)), | |
206 | SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0, | |
207 | &wm8510_mono_mixer_controls[0], | |
208 | ARRAY_SIZE(wm8510_mono_mixer_controls)), | |
209 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0), | |
210 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0), | |
211 | SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0), | |
212 | SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0), | |
213 | SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0), | |
214 | SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0), | |
215 | ||
2b5f34c5 MB |
216 | SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0, |
217 | &wm8510_micpga_controls[0], | |
218 | ARRAY_SIZE(wm8510_micpga_controls)), | |
5d421516 MB |
219 | SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0, |
220 | &wm8510_boost_controls[0], | |
221 | ARRAY_SIZE(wm8510_boost_controls)), | |
222 | ||
223 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0), | |
224 | ||
225 | SND_SOC_DAPM_INPUT("MICN"), | |
226 | SND_SOC_DAPM_INPUT("MICP"), | |
227 | SND_SOC_DAPM_INPUT("AUX"), | |
228 | SND_SOC_DAPM_OUTPUT("MONOOUT"), | |
229 | SND_SOC_DAPM_OUTPUT("SPKOUTP"), | |
230 | SND_SOC_DAPM_OUTPUT("SPKOUTN"), | |
231 | }; | |
232 | ||
b6709f3b | 233 | static const struct snd_soc_dapm_route wm8510_dapm_routes[] = { |
5d421516 MB |
234 | /* Mono output mixer */ |
235 | {"Mono Mixer", "PCM Playback Switch", "DAC"}, | |
236 | {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, | |
237 | {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, | |
238 | ||
239 | /* Speaker output mixer */ | |
240 | {"Speaker Mixer", "PCM Playback Switch", "DAC"}, | |
241 | {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, | |
242 | {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, | |
243 | ||
244 | /* Outputs */ | |
245 | {"Mono Out", NULL, "Mono Mixer"}, | |
246 | {"MONOOUT", NULL, "Mono Out"}, | |
247 | {"SpkN Out", NULL, "Speaker Mixer"}, | |
248 | {"SpkP Out", NULL, "Speaker Mixer"}, | |
249 | {"SPKOUTN", NULL, "SpkN Out"}, | |
250 | {"SPKOUTP", NULL, "SpkP Out"}, | |
251 | ||
252 | /* Microphone PGA */ | |
253 | {"Mic PGA", "MICN Switch", "MICN"}, | |
254 | {"Mic PGA", "MICP Switch", "MICP"}, | |
255 | { "Mic PGA", "AUX Switch", "Aux Input" }, | |
256 | ||
257 | /* Boost Mixer */ | |
258 | {"Boost Mixer", "Mic PGA Switch", "Mic PGA"}, | |
259 | {"Boost Mixer", "Mic Volume", "MICP"}, | |
260 | {"Boost Mixer", "Aux Volume", "Aux Input"}, | |
261 | ||
262 | {"ADC", NULL, "Boost Mixer"}, | |
263 | }; | |
264 | ||
5d421516 MB |
265 | struct pll_ { |
266 | unsigned int pre_div:4; /* prescale - 1 */ | |
267 | unsigned int n:4; | |
268 | unsigned int k; | |
269 | }; | |
270 | ||
271 | static struct pll_ pll_div; | |
272 | ||
273 | /* The size in bits of the pll divide multiplied by 10 | |
274 | * to allow rounding later */ | |
275 | #define FIXED_PLL_SIZE ((1 << 24) * 10) | |
276 | ||
277 | static void pll_factors(unsigned int target, unsigned int source) | |
278 | { | |
279 | unsigned long long Kpart; | |
280 | unsigned int K, Ndiv, Nmod; | |
281 | ||
282 | Ndiv = target / source; | |
283 | if (Ndiv < 6) { | |
284 | source >>= 1; | |
285 | pll_div.pre_div = 1; | |
286 | Ndiv = target / source; | |
287 | } else | |
288 | pll_div.pre_div = 0; | |
289 | ||
290 | if ((Ndiv < 6) || (Ndiv > 12)) | |
291 | printk(KERN_WARNING | |
449bd54d | 292 | "WM8510 N value %u outwith recommended range!d\n", |
5d421516 MB |
293 | Ndiv); |
294 | ||
295 | pll_div.n = Ndiv; | |
296 | Nmod = target % source; | |
297 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; | |
298 | ||
299 | do_div(Kpart, source); | |
300 | ||
301 | K = Kpart & 0xFFFFFFFF; | |
302 | ||
303 | /* Check if we need to round */ | |
304 | if ((K % 10) >= 5) | |
305 | K += 5; | |
306 | ||
307 | /* Move down to proper range now rounding is done */ | |
308 | K /= 10; | |
309 | ||
310 | pll_div.k = K; | |
311 | } | |
312 | ||
85488037 MB |
313 | static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
314 | int source, unsigned int freq_in, unsigned int freq_out) | |
5d421516 | 315 | { |
51bf02cc | 316 | struct snd_soc_component *component = codec_dai->component; |
5d421516 MB |
317 | u16 reg; |
318 | ||
319 | if (freq_in == 0 || freq_out == 0) { | |
320 | /* Clock CODEC directly from MCLK */ | |
6d75dfc3 | 321 | reg = snd_soc_component_read(component, WM8510_CLOCK); |
51bf02cc | 322 | snd_soc_component_write(component, WM8510_CLOCK, reg & 0x0ff); |
5d421516 MB |
323 | |
324 | /* Turn off PLL */ | |
6d75dfc3 | 325 | reg = snd_soc_component_read(component, WM8510_POWER1); |
51bf02cc | 326 | snd_soc_component_write(component, WM8510_POWER1, reg & 0x1df); |
5d421516 MB |
327 | return 0; |
328 | } | |
329 | ||
86027ae7 | 330 | pll_factors(freq_out*4, freq_in); |
5d421516 | 331 | |
51bf02cc KM |
332 | snd_soc_component_write(component, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n); |
333 | snd_soc_component_write(component, WM8510_PLLK1, pll_div.k >> 18); | |
334 | snd_soc_component_write(component, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff); | |
335 | snd_soc_component_write(component, WM8510_PLLK3, pll_div.k & 0x1ff); | |
6d75dfc3 | 336 | reg = snd_soc_component_read(component, WM8510_POWER1); |
51bf02cc | 337 | snd_soc_component_write(component, WM8510_POWER1, reg | 0x020); |
5d421516 MB |
338 | |
339 | /* Run CODEC from PLL instead of MCLK */ | |
6d75dfc3 | 340 | reg = snd_soc_component_read(component, WM8510_CLOCK); |
51bf02cc | 341 | snd_soc_component_write(component, WM8510_CLOCK, reg | 0x100); |
5d421516 MB |
342 | |
343 | return 0; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Configure WM8510 clock dividers. | |
348 | */ | |
e550e17f | 349 | static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
5d421516 MB |
350 | int div_id, int div) |
351 | { | |
51bf02cc | 352 | struct snd_soc_component *component = codec_dai->component; |
5d421516 MB |
353 | u16 reg; |
354 | ||
355 | switch (div_id) { | |
356 | case WM8510_OPCLKDIV: | |
6d75dfc3 | 357 | reg = snd_soc_component_read(component, WM8510_GPIO) & 0x1cf; |
51bf02cc | 358 | snd_soc_component_write(component, WM8510_GPIO, reg | div); |
5d421516 MB |
359 | break; |
360 | case WM8510_MCLKDIV: | |
6d75dfc3 | 361 | reg = snd_soc_component_read(component, WM8510_CLOCK) & 0x11f; |
51bf02cc | 362 | snd_soc_component_write(component, WM8510_CLOCK, reg | div); |
5d421516 MB |
363 | break; |
364 | case WM8510_ADCCLK: | |
6d75dfc3 | 365 | reg = snd_soc_component_read(component, WM8510_ADC) & 0x1f7; |
51bf02cc | 366 | snd_soc_component_write(component, WM8510_ADC, reg | div); |
5d421516 MB |
367 | break; |
368 | case WM8510_DACCLK: | |
6d75dfc3 | 369 | reg = snd_soc_component_read(component, WM8510_DAC) & 0x1f7; |
51bf02cc | 370 | snd_soc_component_write(component, WM8510_DAC, reg | div); |
5d421516 MB |
371 | break; |
372 | case WM8510_BCLKDIV: | |
6d75dfc3 | 373 | reg = snd_soc_component_read(component, WM8510_CLOCK) & 0x1e3; |
51bf02cc | 374 | snd_soc_component_write(component, WM8510_CLOCK, reg | div); |
5d421516 MB |
375 | break; |
376 | default: | |
377 | return -EINVAL; | |
378 | } | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
e550e17f | 383 | static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai, |
5d421516 MB |
384 | unsigned int fmt) |
385 | { | |
51bf02cc | 386 | struct snd_soc_component *component = codec_dai->component; |
5d421516 | 387 | u16 iface = 0; |
6d75dfc3 | 388 | u16 clk = snd_soc_component_read(component, WM8510_CLOCK) & 0x1fe; |
5d421516 MB |
389 | |
390 | /* set master/slave audio interface */ | |
391 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
392 | case SND_SOC_DAIFMT_CBM_CFM: | |
393 | clk |= 0x0001; | |
394 | break; | |
395 | case SND_SOC_DAIFMT_CBS_CFS: | |
396 | break; | |
397 | default: | |
398 | return -EINVAL; | |
399 | } | |
400 | ||
401 | /* interface format */ | |
402 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
403 | case SND_SOC_DAIFMT_I2S: | |
404 | iface |= 0x0010; | |
405 | break; | |
406 | case SND_SOC_DAIFMT_RIGHT_J: | |
407 | break; | |
408 | case SND_SOC_DAIFMT_LEFT_J: | |
409 | iface |= 0x0008; | |
410 | break; | |
411 | case SND_SOC_DAIFMT_DSP_A: | |
412 | iface |= 0x00018; | |
413 | break; | |
414 | default: | |
415 | return -EINVAL; | |
416 | } | |
417 | ||
418 | /* clock inversion */ | |
419 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
420 | case SND_SOC_DAIFMT_NB_NF: | |
421 | break; | |
422 | case SND_SOC_DAIFMT_IB_IF: | |
423 | iface |= 0x0180; | |
424 | break; | |
425 | case SND_SOC_DAIFMT_IB_NF: | |
426 | iface |= 0x0100; | |
427 | break; | |
428 | case SND_SOC_DAIFMT_NB_IF: | |
429 | iface |= 0x0080; | |
430 | break; | |
431 | default: | |
432 | return -EINVAL; | |
433 | } | |
434 | ||
51bf02cc KM |
435 | snd_soc_component_write(component, WM8510_IFACE, iface); |
436 | snd_soc_component_write(component, WM8510_CLOCK, clk); | |
5d421516 MB |
437 | return 0; |
438 | } | |
439 | ||
440 | static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream, | |
dee89c4d MB |
441 | struct snd_pcm_hw_params *params, |
442 | struct snd_soc_dai *dai) | |
5d421516 | 443 | { |
51bf02cc | 444 | struct snd_soc_component *component = dai->component; |
6d75dfc3 KM |
445 | u16 iface = snd_soc_component_read(component, WM8510_IFACE) & 0x19f; |
446 | u16 adn = snd_soc_component_read(component, WM8510_ADD) & 0x1f1; | |
5d421516 MB |
447 | |
448 | /* bit size */ | |
19e4b788 MB |
449 | switch (params_width(params)) { |
450 | case 16: | |
5d421516 | 451 | break; |
19e4b788 | 452 | case 20: |
5d421516 MB |
453 | iface |= 0x0020; |
454 | break; | |
19e4b788 | 455 | case 24: |
5d421516 MB |
456 | iface |= 0x0040; |
457 | break; | |
19e4b788 | 458 | case 32: |
5d421516 MB |
459 | iface |= 0x0060; |
460 | break; | |
461 | } | |
462 | ||
463 | /* filter coefficient */ | |
464 | switch (params_rate(params)) { | |
b3172f22 | 465 | case 8000: |
5d421516 MB |
466 | adn |= 0x5 << 1; |
467 | break; | |
b3172f22 | 468 | case 11025: |
5d421516 MB |
469 | adn |= 0x4 << 1; |
470 | break; | |
b3172f22 | 471 | case 16000: |
5d421516 MB |
472 | adn |= 0x3 << 1; |
473 | break; | |
b3172f22 | 474 | case 22050: |
5d421516 MB |
475 | adn |= 0x2 << 1; |
476 | break; | |
b3172f22 | 477 | case 32000: |
5d421516 MB |
478 | adn |= 0x1 << 1; |
479 | break; | |
b3172f22 GL |
480 | case 44100: |
481 | case 48000: | |
5d421516 MB |
482 | break; |
483 | } | |
484 | ||
51bf02cc KM |
485 | snd_soc_component_write(component, WM8510_IFACE, iface); |
486 | snd_soc_component_write(component, WM8510_ADD, adn); | |
5d421516 MB |
487 | return 0; |
488 | } | |
489 | ||
26d3c16e | 490 | static int wm8510_mute(struct snd_soc_dai *dai, int mute, int direction) |
5d421516 | 491 | { |
51bf02cc | 492 | struct snd_soc_component *component = dai->component; |
6d75dfc3 | 493 | u16 mute_reg = snd_soc_component_read(component, WM8510_DAC) & 0xffbf; |
5d421516 MB |
494 | |
495 | if (mute) | |
51bf02cc | 496 | snd_soc_component_write(component, WM8510_DAC, mute_reg | 0x40); |
5d421516 | 497 | else |
51bf02cc | 498 | snd_soc_component_write(component, WM8510_DAC, mute_reg); |
5d421516 MB |
499 | return 0; |
500 | } | |
501 | ||
502 | /* liam need to make this lower power with dapm */ | |
51bf02cc | 503 | static int wm8510_set_bias_level(struct snd_soc_component *component, |
5d421516 MB |
504 | enum snd_soc_bias_level level) |
505 | { | |
51bf02cc | 506 | struct wm8510_priv *wm8510 = snd_soc_component_get_drvdata(component); |
6d75dfc3 | 507 | u16 power1 = snd_soc_component_read(component, WM8510_POWER1) & ~0x3; |
5d421516 MB |
508 | |
509 | switch (level) { | |
510 | case SND_SOC_BIAS_ON: | |
5d421516 | 511 | case SND_SOC_BIAS_PREPARE: |
09af98b0 | 512 | power1 |= 0x1; /* VMID 50k */ |
51bf02cc | 513 | snd_soc_component_write(component, WM8510_POWER1, power1); |
09af98b0 MB |
514 | break; |
515 | ||
5d421516 | 516 | case SND_SOC_BIAS_STANDBY: |
09af98b0 MB |
517 | power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN; |
518 | ||
51bf02cc | 519 | if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) { |
e643049d | 520 | regcache_sync(wm8510->regmap); |
94f17e9c | 521 | |
09af98b0 | 522 | /* Initial cap charge at VMID 5k */ |
51bf02cc | 523 | snd_soc_component_write(component, WM8510_POWER1, power1 | 0x3); |
09af98b0 MB |
524 | mdelay(100); |
525 | } | |
526 | ||
527 | power1 |= 0x2; /* VMID 500k */ | |
51bf02cc | 528 | snd_soc_component_write(component, WM8510_POWER1, power1); |
5d421516 | 529 | break; |
09af98b0 | 530 | |
5d421516 | 531 | case SND_SOC_BIAS_OFF: |
51bf02cc KM |
532 | snd_soc_component_write(component, WM8510_POWER1, 0); |
533 | snd_soc_component_write(component, WM8510_POWER2, 0); | |
534 | snd_soc_component_write(component, WM8510_POWER3, 0); | |
5d421516 MB |
535 | break; |
536 | } | |
09af98b0 | 537 | |
5d421516 MB |
538 | return 0; |
539 | } | |
540 | ||
541 | #define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ | |
542 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ | |
543 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) | |
544 | ||
545 | #define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ | |
546 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) | |
547 | ||
85e7652d | 548 | static const struct snd_soc_dai_ops wm8510_dai_ops = { |
6335d055 | 549 | .hw_params = wm8510_pcm_hw_params, |
26d3c16e | 550 | .mute_stream = wm8510_mute, |
6335d055 EM |
551 | .set_fmt = wm8510_set_dai_fmt, |
552 | .set_clkdiv = wm8510_set_dai_clkdiv, | |
553 | .set_pll = wm8510_set_dai_pll, | |
26d3c16e | 554 | .no_capture_mute = 1, |
6335d055 EM |
555 | }; |
556 | ||
f0fba2ad LG |
557 | static struct snd_soc_dai_driver wm8510_dai = { |
558 | .name = "wm8510-hifi", | |
5d421516 MB |
559 | .playback = { |
560 | .stream_name = "Playback", | |
561 | .channels_min = 2, | |
562 | .channels_max = 2, | |
563 | .rates = WM8510_RATES, | |
564 | .formats = WM8510_FORMATS,}, | |
565 | .capture = { | |
566 | .stream_name = "Capture", | |
567 | .channels_min = 2, | |
568 | .channels_max = 2, | |
569 | .rates = WM8510_RATES, | |
570 | .formats = WM8510_FORMATS,}, | |
6335d055 | 571 | .ops = &wm8510_dai_ops, |
07695752 | 572 | .symmetric_rate = 1, |
5d421516 | 573 | }; |
5d421516 | 574 | |
51bf02cc | 575 | static int wm8510_probe(struct snd_soc_component *component) |
5d421516 | 576 | { |
51bf02cc | 577 | wm8510_reset(component); |
5d421516 | 578 | |
5d421516 MB |
579 | return 0; |
580 | } | |
581 | ||
51bf02cc KM |
582 | static const struct snd_soc_component_driver soc_component_dev_wm8510 = { |
583 | .probe = wm8510_probe, | |
584 | .set_bias_level = wm8510_set_bias_level, | |
585 | .controls = wm8510_snd_controls, | |
586 | .num_controls = ARRAY_SIZE(wm8510_snd_controls), | |
587 | .dapm_widgets = wm8510_dapm_widgets, | |
588 | .num_dapm_widgets = ARRAY_SIZE(wm8510_dapm_widgets), | |
589 | .dapm_routes = wm8510_dapm_routes, | |
590 | .num_dapm_routes = ARRAY_SIZE(wm8510_dapm_routes), | |
591 | .suspend_bias_off = 1, | |
592 | .idle_bias_on = 1, | |
593 | .use_pmdown_time = 1, | |
594 | .endianness = 1, | |
595 | .non_legacy_dai_naming = 1, | |
5d421516 MB |
596 | }; |
597 | ||
0a422e1a MB |
598 | static const struct of_device_id wm8510_of_match[] = { |
599 | { .compatible = "wlf,wm8510" }, | |
600 | { }, | |
601 | }; | |
bf08f39e | 602 | MODULE_DEVICE_TABLE(of, wm8510_of_match); |
0a422e1a | 603 | |
e643049d MB |
604 | static const struct regmap_config wm8510_regmap = { |
605 | .reg_bits = 7, | |
606 | .val_bits = 9, | |
607 | .max_register = WM8510_MONOMIX, | |
608 | ||
609 | .reg_defaults = wm8510_reg_defaults, | |
610 | .num_reg_defaults = ARRAY_SIZE(wm8510_reg_defaults), | |
611 | .cache_type = REGCACHE_RBTREE, | |
612 | ||
613 | .volatile_reg = wm8510_volatile, | |
614 | }; | |
615 | ||
5e357952 | 616 | #if defined(CONFIG_SPI_MASTER) |
7a79e94e | 617 | static int wm8510_spi_probe(struct spi_device *spi) |
5e357952 | 618 | { |
f0fba2ad | 619 | struct wm8510_priv *wm8510; |
5e357952 MB |
620 | int ret; |
621 | ||
3217b0f5 MB |
622 | wm8510 = devm_kzalloc(&spi->dev, sizeof(struct wm8510_priv), |
623 | GFP_KERNEL); | |
f0fba2ad LG |
624 | if (wm8510 == NULL) |
625 | return -ENOMEM; | |
626 | ||
e643049d MB |
627 | wm8510->regmap = devm_regmap_init_spi(spi, &wm8510_regmap); |
628 | if (IS_ERR(wm8510->regmap)) | |
629 | return PTR_ERR(wm8510->regmap); | |
630 | ||
f0fba2ad | 631 | spi_set_drvdata(spi, wm8510); |
5e357952 | 632 | |
51bf02cc KM |
633 | ret = devm_snd_soc_register_component(&spi->dev, |
634 | &soc_component_dev_wm8510, &wm8510_dai, 1); | |
3217b0f5 | 635 | |
5e357952 MB |
636 | return ret; |
637 | } | |
638 | ||
5e357952 MB |
639 | static struct spi_driver wm8510_spi_driver = { |
640 | .driver = { | |
641 | .name = "wm8510", | |
0a422e1a | 642 | .of_match_table = wm8510_of_match, |
5e357952 MB |
643 | }, |
644 | .probe = wm8510_spi_probe, | |
5e357952 | 645 | }; |
5e357952 MB |
646 | #endif /* CONFIG_SPI_MASTER */ |
647 | ||
8e6ad35a | 648 | #if IS_ENABLED(CONFIG_I2C) |
7a79e94e BP |
649 | static int wm8510_i2c_probe(struct i2c_client *i2c, |
650 | const struct i2c_device_id *id) | |
5d421516 | 651 | { |
f0fba2ad LG |
652 | struct wm8510_priv *wm8510; |
653 | int ret; | |
5d421516 | 654 | |
3217b0f5 MB |
655 | wm8510 = devm_kzalloc(&i2c->dev, sizeof(struct wm8510_priv), |
656 | GFP_KERNEL); | |
f0fba2ad | 657 | if (wm8510 == NULL) |
5d421516 MB |
658 | return -ENOMEM; |
659 | ||
e643049d MB |
660 | wm8510->regmap = devm_regmap_init_i2c(i2c, &wm8510_regmap); |
661 | if (IS_ERR(wm8510->regmap)) | |
662 | return PTR_ERR(wm8510->regmap); | |
663 | ||
f0fba2ad | 664 | i2c_set_clientdata(i2c, wm8510); |
5d421516 | 665 | |
51bf02cc KM |
666 | ret = devm_snd_soc_register_component(&i2c->dev, |
667 | &soc_component_dev_wm8510, &wm8510_dai, 1); | |
3217b0f5 | 668 | |
5d421516 MB |
669 | return ret; |
670 | } | |
671 | ||
f0fba2ad LG |
672 | static const struct i2c_device_id wm8510_i2c_id[] = { |
673 | { "wm8510", 0 }, | |
674 | { } | |
675 | }; | |
676 | MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id); | |
677 | ||
678 | static struct i2c_driver wm8510_i2c_driver = { | |
679 | .driver = { | |
091edccf | 680 | .name = "wm8510", |
0a422e1a | 681 | .of_match_table = wm8510_of_match, |
f0fba2ad LG |
682 | }, |
683 | .probe = wm8510_i2c_probe, | |
f0fba2ad | 684 | .id_table = wm8510_i2c_id, |
5d421516 | 685 | }; |
f0fba2ad | 686 | #endif |
5d421516 | 687 | |
c9b3a40f | 688 | static int __init wm8510_modinit(void) |
64089b84 | 689 | { |
f0fba2ad | 690 | int ret = 0; |
8e6ad35a | 691 | #if IS_ENABLED(CONFIG_I2C) |
f0fba2ad LG |
692 | ret = i2c_add_driver(&wm8510_i2c_driver); |
693 | if (ret != 0) { | |
694 | printk(KERN_ERR "Failed to register WM8510 I2C driver: %d\n", | |
695 | ret); | |
696 | } | |
697 | #endif | |
698 | #if defined(CONFIG_SPI_MASTER) | |
699 | ret = spi_register_driver(&wm8510_spi_driver); | |
700 | if (ret != 0) { | |
701 | printk(KERN_ERR "Failed to register WM8510 SPI driver: %d\n", | |
702 | ret); | |
703 | } | |
704 | #endif | |
705 | return ret; | |
64089b84 MB |
706 | } |
707 | module_init(wm8510_modinit); | |
708 | ||
709 | static void __exit wm8510_exit(void) | |
710 | { | |
8e6ad35a | 711 | #if IS_ENABLED(CONFIG_I2C) |
f0fba2ad LG |
712 | i2c_del_driver(&wm8510_i2c_driver); |
713 | #endif | |
714 | #if defined(CONFIG_SPI_MASTER) | |
715 | spi_unregister_driver(&wm8510_spi_driver); | |
716 | #endif | |
64089b84 MB |
717 | } |
718 | module_exit(wm8510_exit); | |
719 | ||
5d421516 MB |
720 | MODULE_DESCRIPTION("ASoC WM8510 driver"); |
721 | MODULE_AUTHOR("Liam Girdwood"); | |
722 | MODULE_LICENSE("GPL"); |