]>
Commit | Line | Data |
---|---|---|
33cf45c8 DP |
1 | /* |
2 | * wm8804.c -- WM8804 S/PDIF transceiver driver | |
3 | * | |
656baaeb | 4 | * Copyright 2010-11 Wolfson Microelectronics plc |
33cf45c8 DP |
5 | * |
6 | * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/moduleparam.h> | |
15 | #include <linux/init.h> | |
7e5ee1c3 | 16 | #include <linux/gpio/consumer.h> |
33cf45c8 DP |
17 | #include <linux/delay.h> |
18 | #include <linux/pm.h> | |
1a60667f | 19 | #include <linux/pm_runtime.h> |
d2dd0540 | 20 | #include <linux/of_device.h> |
33cf45c8 DP |
21 | #include <linux/regulator/consumer.h> |
22 | #include <linux/slab.h> | |
23 | #include <sound/core.h> | |
24 | #include <sound/pcm.h> | |
25 | #include <sound/pcm_params.h> | |
26 | #include <sound/soc.h> | |
33cf45c8 DP |
27 | #include <sound/initval.h> |
28 | #include <sound/tlv.h> | |
5631f187 | 29 | #include <sound/soc-dapm.h> |
33cf45c8 DP |
30 | |
31 | #include "wm8804.h" | |
32 | ||
33 | #define WM8804_NUM_SUPPLIES 2 | |
34 | static const char *wm8804_supply_names[WM8804_NUM_SUPPLIES] = { | |
35 | "PVDD", | |
36 | "DVDD" | |
37 | }; | |
38 | ||
891271c2 MB |
39 | static const struct reg_default wm8804_reg_defaults[] = { |
40 | { 3, 0x21 }, /* R3 - PLL1 */ | |
41 | { 4, 0xFD }, /* R4 - PLL2 */ | |
42 | { 5, 0x36 }, /* R5 - PLL3 */ | |
43 | { 6, 0x07 }, /* R6 - PLL4 */ | |
44 | { 7, 0x16 }, /* R7 - PLL5 */ | |
45 | { 8, 0x18 }, /* R8 - PLL6 */ | |
46 | { 9, 0xFF }, /* R9 - SPDMODE */ | |
47 | { 10, 0x00 }, /* R10 - INTMASK */ | |
48 | { 18, 0x00 }, /* R18 - SPDTX1 */ | |
49 | { 19, 0x00 }, /* R19 - SPDTX2 */ | |
50 | { 20, 0x00 }, /* R20 - SPDTX3 */ | |
51 | { 21, 0x71 }, /* R21 - SPDTX4 */ | |
52 | { 22, 0x0B }, /* R22 - SPDTX5 */ | |
53 | { 23, 0x70 }, /* R23 - GPO0 */ | |
54 | { 24, 0x57 }, /* R24 - GPO1 */ | |
55 | { 26, 0x42 }, /* R26 - GPO2 */ | |
56 | { 27, 0x06 }, /* R27 - AIFTX */ | |
57 | { 28, 0x06 }, /* R28 - AIFRX */ | |
58 | { 29, 0x80 }, /* R29 - SPDRX1 */ | |
59 | { 30, 0x07 }, /* R30 - PWRDN */ | |
33cf45c8 DP |
60 | }; |
61 | ||
62 | struct wm8804_priv { | |
1a60667f | 63 | struct device *dev; |
891271c2 | 64 | struct regmap *regmap; |
33cf45c8 DP |
65 | struct regulator_bulk_data supplies[WM8804_NUM_SUPPLIES]; |
66 | struct notifier_block disable_nb[WM8804_NUM_SUPPLIES]; | |
06109f47 | 67 | int mclk_div; |
7e5ee1c3 CK |
68 | |
69 | struct gpio_desc *reset; | |
33cf45c8 | 70 | |
5631f187 SB |
71 | int aif_pwr; |
72 | }; | |
33cf45c8 DP |
73 | |
74 | static int txsrc_put(struct snd_kcontrol *kcontrol, | |
75 | struct snd_ctl_elem_value *ucontrol); | |
76 | ||
5631f187 SB |
77 | static int wm8804_aif_event(struct snd_soc_dapm_widget *w, |
78 | struct snd_kcontrol *kcontrol, int event); | |
79 | ||
33cf45c8 DP |
80 | /* |
81 | * We can't use the same notifier block for more than one supply and | |
82 | * there's no way I can see to get from a callback to the caller | |
83 | * except container_of(). | |
84 | */ | |
85 | #define WM8804_REGULATOR_EVENT(n) \ | |
86 | static int wm8804_regulator_event_##n(struct notifier_block *nb, \ | |
87 | unsigned long event, void *data) \ | |
88 | { \ | |
89 | struct wm8804_priv *wm8804 = container_of(nb, struct wm8804_priv, \ | |
90 | disable_nb[n]); \ | |
91 | if (event & REGULATOR_EVENT_DISABLE) { \ | |
891271c2 | 92 | regcache_mark_dirty(wm8804->regmap); \ |
33cf45c8 DP |
93 | } \ |
94 | return 0; \ | |
95 | } | |
96 | ||
97 | WM8804_REGULATOR_EVENT(0) | |
98 | WM8804_REGULATOR_EVENT(1) | |
99 | ||
100 | static const char *txsrc_text[] = { "S/PDIF RX", "AIF" }; | |
c8a6b92b | 101 | static SOC_ENUM_SINGLE_DECL(txsrc, WM8804_SPDTX4, 6, txsrc_text); |
33cf45c8 | 102 | |
5631f187 SB |
103 | static const struct snd_kcontrol_new wm8804_tx_source_mux[] = { |
104 | SOC_DAPM_ENUM_EXT("Input Source", txsrc, | |
105 | snd_soc_dapm_get_enum_double, txsrc_put), | |
33cf45c8 DP |
106 | }; |
107 | ||
5631f187 SB |
108 | static const struct snd_soc_dapm_widget wm8804_dapm_widgets[] = { |
109 | SND_SOC_DAPM_OUTPUT("SPDIF Out"), | |
110 | SND_SOC_DAPM_INPUT("SPDIF In"), | |
111 | ||
112 | SND_SOC_DAPM_PGA("SPDIFTX", WM8804_PWRDN, 2, 1, NULL, 0), | |
113 | SND_SOC_DAPM_PGA("SPDIFRX", WM8804_PWRDN, 1, 1, NULL, 0), | |
114 | ||
115 | SND_SOC_DAPM_MUX("Tx Source", SND_SOC_NOPM, 6, 0, wm8804_tx_source_mux), | |
116 | ||
117 | SND_SOC_DAPM_AIF_OUT_E("AIFTX", NULL, 0, SND_SOC_NOPM, 0, 0, wm8804_aif_event, | |
118 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), | |
119 | SND_SOC_DAPM_AIF_IN_E("AIFRX", NULL, 0, SND_SOC_NOPM, 0, 0, wm8804_aif_event, | |
120 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), | |
121 | }; | |
122 | ||
123 | static const struct snd_soc_dapm_route wm8804_dapm_routes[] = { | |
124 | { "AIFRX", NULL, "Playback" }, | |
125 | { "Tx Source", "AIF", "AIFRX" }, | |
126 | ||
127 | { "SPDIFRX", NULL, "SPDIF In" }, | |
128 | { "Tx Source", "S/PDIF RX", "SPDIFRX" }, | |
129 | ||
130 | { "SPDIFTX", NULL, "Tx Source" }, | |
131 | { "SPDIF Out", NULL, "SPDIFTX" }, | |
33cf45c8 | 132 | |
5631f187 SB |
133 | { "AIFTX", NULL, "SPDIFRX" }, |
134 | { "Capture", NULL, "AIFTX" }, | |
135 | }; | |
136 | ||
137 | static int wm8804_aif_event(struct snd_soc_dapm_widget *w, | |
138 | struct snd_kcontrol *kcontrol, int event) | |
139 | { | |
140 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm); | |
141 | struct wm8804_priv *wm8804 = snd_soc_codec_get_drvdata(codec); | |
142 | ||
143 | switch (event) { | |
144 | case SND_SOC_DAPM_POST_PMU: | |
145 | /* power up the aif */ | |
146 | if (!wm8804->aif_pwr) | |
147 | snd_soc_update_bits(codec, WM8804_PWRDN, 0x10, 0x0); | |
148 | wm8804->aif_pwr++; | |
149 | break; | |
150 | case SND_SOC_DAPM_POST_PMD: | |
151 | /* power down only both paths are disabled */ | |
152 | wm8804->aif_pwr--; | |
153 | if (!wm8804->aif_pwr) | |
154 | snd_soc_update_bits(codec, WM8804_PWRDN, 0x10, 0x10); | |
155 | break; | |
156 | } | |
33cf45c8 DP |
157 | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static int txsrc_put(struct snd_kcontrol *kcontrol, | |
162 | struct snd_ctl_elem_value *ucontrol) | |
163 | { | |
5631f187 | 164 | struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol); |
e7556037 | 165 | struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec); |
5631f187 SB |
166 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
167 | unsigned int val = ucontrol->value.enumerated.item[0] << e->shift_l; | |
168 | unsigned int mask = 1 << e->shift_l; | |
169 | unsigned int txpwr; | |
170 | ||
171 | if (val != 0 && val != mask) | |
172 | return -EINVAL; | |
33cf45c8 | 173 | |
5631f187 | 174 | snd_soc_dapm_mutex_lock(dapm); |
33cf45c8 | 175 | |
5631f187 SB |
176 | if (snd_soc_test_bits(codec, e->reg, mask, val)) { |
177 | /* save the current power state of the transmitter */ | |
178 | txpwr = snd_soc_read(codec, WM8804_PWRDN) & 0x4; | |
33cf45c8 | 179 | |
5631f187 SB |
180 | /* power down the transmitter */ |
181 | snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x4); | |
33cf45c8 | 182 | |
5631f187 SB |
183 | /* set the tx source */ |
184 | snd_soc_update_bits(codec, e->reg, mask, val); | |
185 | ||
186 | /* restore the transmitter's configuration */ | |
187 | snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, txpwr); | |
33cf45c8 DP |
188 | } |
189 | ||
5631f187 | 190 | snd_soc_dapm_mutex_unlock(dapm); |
33cf45c8 DP |
191 | |
192 | return 0; | |
193 | } | |
194 | ||
891271c2 | 195 | static bool wm8804_volatile(struct device *dev, unsigned int reg) |
33cf45c8 DP |
196 | { |
197 | switch (reg) { | |
198 | case WM8804_RST_DEVID1: | |
199 | case WM8804_DEVID2: | |
200 | case WM8804_DEVREV: | |
201 | case WM8804_INTSTAT: | |
202 | case WM8804_SPDSTAT: | |
203 | case WM8804_RXCHAN1: | |
204 | case WM8804_RXCHAN2: | |
205 | case WM8804_RXCHAN3: | |
206 | case WM8804_RXCHAN4: | |
207 | case WM8804_RXCHAN5: | |
891271c2 | 208 | return true; |
33cf45c8 | 209 | default: |
891271c2 | 210 | return false; |
33cf45c8 | 211 | } |
33cf45c8 DP |
212 | } |
213 | ||
7e5ee1c3 | 214 | static int wm8804_soft_reset(struct wm8804_priv *wm8804) |
33cf45c8 | 215 | { |
6f2c9348 | 216 | return regmap_write(wm8804->regmap, WM8804_RST_DEVID1, 0x0); |
33cf45c8 DP |
217 | } |
218 | ||
219 | static int wm8804_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
220 | { | |
221 | struct snd_soc_codec *codec; | |
222 | u16 format, master, bcp, lrp; | |
223 | ||
224 | codec = dai->codec; | |
225 | ||
226 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
227 | case SND_SOC_DAIFMT_I2S: | |
228 | format = 0x2; | |
229 | break; | |
230 | case SND_SOC_DAIFMT_RIGHT_J: | |
231 | format = 0x0; | |
232 | break; | |
233 | case SND_SOC_DAIFMT_LEFT_J: | |
234 | format = 0x1; | |
235 | break; | |
236 | case SND_SOC_DAIFMT_DSP_A: | |
237 | case SND_SOC_DAIFMT_DSP_B: | |
238 | format = 0x3; | |
239 | break; | |
240 | default: | |
241 | dev_err(dai->dev, "Unknown dai format\n"); | |
242 | return -EINVAL; | |
243 | } | |
244 | ||
245 | /* set data format */ | |
246 | snd_soc_update_bits(codec, WM8804_AIFTX, 0x3, format); | |
247 | snd_soc_update_bits(codec, WM8804_AIFRX, 0x3, format); | |
248 | ||
249 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
250 | case SND_SOC_DAIFMT_CBM_CFM: | |
251 | master = 1; | |
252 | break; | |
253 | case SND_SOC_DAIFMT_CBS_CFS: | |
254 | master = 0; | |
255 | break; | |
256 | default: | |
257 | dev_err(dai->dev, "Unknown master/slave configuration\n"); | |
258 | return -EINVAL; | |
259 | } | |
260 | ||
261 | /* set master/slave mode */ | |
262 | snd_soc_update_bits(codec, WM8804_AIFRX, 0x40, master << 6); | |
263 | ||
264 | bcp = lrp = 0; | |
265 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
266 | case SND_SOC_DAIFMT_NB_NF: | |
267 | break; | |
268 | case SND_SOC_DAIFMT_IB_IF: | |
269 | bcp = lrp = 1; | |
270 | break; | |
271 | case SND_SOC_DAIFMT_IB_NF: | |
272 | bcp = 1; | |
273 | break; | |
274 | case SND_SOC_DAIFMT_NB_IF: | |
275 | lrp = 1; | |
276 | break; | |
277 | default: | |
278 | dev_err(dai->dev, "Unknown polarity configuration\n"); | |
279 | return -EINVAL; | |
280 | } | |
281 | ||
282 | /* set frame inversion */ | |
283 | snd_soc_update_bits(codec, WM8804_AIFTX, 0x10 | 0x20, | |
284 | (bcp << 4) | (lrp << 5)); | |
285 | snd_soc_update_bits(codec, WM8804_AIFRX, 0x10 | 0x20, | |
286 | (bcp << 4) | (lrp << 5)); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | static int wm8804_hw_params(struct snd_pcm_substream *substream, | |
291 | struct snd_pcm_hw_params *params, | |
292 | struct snd_soc_dai *dai) | |
293 | { | |
294 | struct snd_soc_codec *codec; | |
295 | u16 blen; | |
296 | ||
297 | codec = dai->codec; | |
298 | ||
16cfd485 MB |
299 | switch (params_width(params)) { |
300 | case 16: | |
33cf45c8 DP |
301 | blen = 0x0; |
302 | break; | |
16cfd485 | 303 | case 20: |
33cf45c8 DP |
304 | blen = 0x1; |
305 | break; | |
16cfd485 | 306 | case 24: |
095e1de5 | 307 | case 32: |
33cf45c8 DP |
308 | blen = 0x2; |
309 | break; | |
310 | default: | |
311 | dev_err(dai->dev, "Unsupported word length: %u\n", | |
16cfd485 | 312 | params_width(params)); |
33cf45c8 DP |
313 | return -EINVAL; |
314 | } | |
315 | ||
316 | /* set word length */ | |
317 | snd_soc_update_bits(codec, WM8804_AIFTX, 0xc, blen << 2); | |
318 | snd_soc_update_bits(codec, WM8804_AIFRX, 0xc, blen << 2); | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
323 | struct pll_div { | |
324 | u32 prescale:1; | |
325 | u32 mclkdiv:1; | |
326 | u32 freqmode:2; | |
327 | u32 n:4; | |
328 | u32 k:22; | |
329 | }; | |
330 | ||
331 | /* PLL rate to output rate divisions */ | |
332 | static struct { | |
333 | unsigned int div; | |
334 | unsigned int freqmode; | |
335 | unsigned int mclkdiv; | |
336 | } post_table[] = { | |
337 | { 2, 0, 0 }, | |
338 | { 4, 0, 1 }, | |
339 | { 4, 1, 0 }, | |
340 | { 8, 1, 1 }, | |
341 | { 8, 2, 0 }, | |
342 | { 16, 2, 1 }, | |
343 | { 12, 3, 0 }, | |
344 | { 24, 3, 1 } | |
345 | }; | |
346 | ||
347 | #define FIXED_PLL_SIZE ((1ULL << 22) * 10) | |
348 | static int pll_factors(struct pll_div *pll_div, unsigned int target, | |
06109f47 | 349 | unsigned int source, unsigned int mclk_div) |
33cf45c8 DP |
350 | { |
351 | u64 Kpart; | |
352 | unsigned long int K, Ndiv, Nmod, tmp; | |
353 | int i; | |
354 | ||
355 | /* | |
356 | * Scale the output frequency up; the PLL should run in the | |
357 | * region of 90-100MHz. | |
358 | */ | |
359 | for (i = 0; i < ARRAY_SIZE(post_table); i++) { | |
360 | tmp = target * post_table[i].div; | |
06109f47 DM |
361 | if ((tmp >= 90000000 && tmp <= 100000000) && |
362 | (mclk_div == post_table[i].mclkdiv)) { | |
33cf45c8 DP |
363 | pll_div->freqmode = post_table[i].freqmode; |
364 | pll_div->mclkdiv = post_table[i].mclkdiv; | |
365 | target *= post_table[i].div; | |
366 | break; | |
367 | } | |
368 | } | |
369 | ||
370 | if (i == ARRAY_SIZE(post_table)) { | |
371 | pr_err("%s: Unable to scale output frequency: %uHz\n", | |
372 | __func__, target); | |
373 | return -EINVAL; | |
374 | } | |
375 | ||
376 | pll_div->prescale = 0; | |
377 | Ndiv = target / source; | |
378 | if (Ndiv < 5) { | |
379 | source >>= 1; | |
380 | pll_div->prescale = 1; | |
381 | Ndiv = target / source; | |
382 | } | |
383 | ||
384 | if (Ndiv < 5 || Ndiv > 13) { | |
385 | pr_err("%s: WM8804 N value is not within the recommended range: %lu\n", | |
386 | __func__, Ndiv); | |
387 | return -EINVAL; | |
388 | } | |
389 | pll_div->n = Ndiv; | |
390 | ||
391 | Nmod = target % source; | |
392 | Kpart = FIXED_PLL_SIZE * (u64)Nmod; | |
393 | ||
394 | do_div(Kpart, source); | |
395 | ||
396 | K = Kpart & 0xffffffff; | |
397 | if ((K % 10) >= 5) | |
398 | K += 5; | |
399 | K /= 10; | |
400 | pll_div->k = K; | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | static int wm8804_set_pll(struct snd_soc_dai *dai, int pll_id, | |
406 | int source, unsigned int freq_in, | |
407 | unsigned int freq_out) | |
408 | { | |
1a60667f CK |
409 | struct snd_soc_codec *codec = dai->codec; |
410 | struct wm8804_priv *wm8804 = snd_soc_codec_get_drvdata(codec); | |
411 | bool change; | |
33cf45c8 | 412 | |
86ce6c9a DP |
413 | if (!freq_in || !freq_out) { |
414 | /* disable the PLL */ | |
1a60667f CK |
415 | regmap_update_bits_check(wm8804->regmap, WM8804_PWRDN, |
416 | 0x1, 0x1, &change); | |
417 | if (change) | |
418 | pm_runtime_put(wm8804->dev); | |
86ce6c9a DP |
419 | } else { |
420 | int ret; | |
421 | struct pll_div pll_div; | |
06109f47 DM |
422 | |
423 | ret = pll_factors(&pll_div, freq_out, freq_in, | |
424 | wm8804->mclk_div); | |
33cf45c8 DP |
425 | if (ret) |
426 | return ret; | |
33cf45c8 | 427 | |
86ce6c9a | 428 | /* power down the PLL before reprogramming it */ |
1a60667f CK |
429 | regmap_update_bits_check(wm8804->regmap, WM8804_PWRDN, |
430 | 0x1, 0x1, &change); | |
431 | if (!change) | |
432 | pm_runtime_get_sync(wm8804->dev); | |
33cf45c8 | 433 | |
86ce6c9a DP |
434 | /* set PLLN and PRESCALE */ |
435 | snd_soc_update_bits(codec, WM8804_PLL4, 0xf | 0x10, | |
436 | pll_div.n | (pll_div.prescale << 4)); | |
437 | /* set mclkdiv and freqmode */ | |
438 | snd_soc_update_bits(codec, WM8804_PLL5, 0x3 | 0x8, | |
439 | pll_div.freqmode | (pll_div.mclkdiv << 3)); | |
440 | /* set PLLK */ | |
441 | snd_soc_write(codec, WM8804_PLL1, pll_div.k & 0xff); | |
442 | snd_soc_write(codec, WM8804_PLL2, (pll_div.k >> 8) & 0xff); | |
443 | snd_soc_write(codec, WM8804_PLL3, pll_div.k >> 16); | |
444 | ||
445 | /* power up the PLL */ | |
6c20c807 | 446 | snd_soc_update_bits(codec, WM8804_PWRDN, 0x1, 0); |
86ce6c9a | 447 | } |
33cf45c8 DP |
448 | |
449 | return 0; | |
450 | } | |
451 | ||
452 | static int wm8804_set_sysclk(struct snd_soc_dai *dai, | |
453 | int clk_id, unsigned int freq, int dir) | |
454 | { | |
455 | struct snd_soc_codec *codec; | |
456 | ||
457 | codec = dai->codec; | |
458 | ||
459 | switch (clk_id) { | |
460 | case WM8804_TX_CLKSRC_MCLK: | |
461 | if ((freq >= 10000000 && freq <= 14400000) | |
462 | || (freq >= 16280000 && freq <= 27000000)) | |
463 | snd_soc_update_bits(codec, WM8804_PLL6, 0x80, 0x80); | |
464 | else { | |
465 | dev_err(dai->dev, "OSCCLOCK is not within the " | |
466 | "recommended range: %uHz\n", freq); | |
467 | return -EINVAL; | |
468 | } | |
469 | break; | |
470 | case WM8804_TX_CLKSRC_PLL: | |
471 | snd_soc_update_bits(codec, WM8804_PLL6, 0x80, 0); | |
472 | break; | |
473 | case WM8804_CLKOUT_SRC_CLK1: | |
474 | snd_soc_update_bits(codec, WM8804_PLL6, 0x8, 0); | |
475 | break; | |
476 | case WM8804_CLKOUT_SRC_OSCCLK: | |
477 | snd_soc_update_bits(codec, WM8804_PLL6, 0x8, 0x8); | |
478 | break; | |
479 | default: | |
480 | dev_err(dai->dev, "Unknown clock source: %d\n", clk_id); | |
481 | return -EINVAL; | |
482 | } | |
483 | ||
484 | return 0; | |
485 | } | |
486 | ||
487 | static int wm8804_set_clkdiv(struct snd_soc_dai *dai, | |
488 | int div_id, int div) | |
489 | { | |
490 | struct snd_soc_codec *codec; | |
06109f47 | 491 | struct wm8804_priv *wm8804; |
33cf45c8 DP |
492 | |
493 | codec = dai->codec; | |
494 | switch (div_id) { | |
495 | case WM8804_CLKOUT_DIV: | |
496 | snd_soc_update_bits(codec, WM8804_PLL5, 0x30, | |
497 | (div & 0x3) << 4); | |
498 | break; | |
06109f47 DM |
499 | case WM8804_MCLK_DIV: |
500 | wm8804 = snd_soc_codec_get_drvdata(codec); | |
501 | wm8804->mclk_div = div; | |
502 | break; | |
33cf45c8 DP |
503 | default: |
504 | dev_err(dai->dev, "Unknown clock divider: %d\n", div_id); | |
505 | return -EINVAL; | |
506 | } | |
507 | return 0; | |
508 | } | |
509 | ||
85e7652d | 510 | static const struct snd_soc_dai_ops wm8804_dai_ops = { |
33cf45c8 DP |
511 | .hw_params = wm8804_hw_params, |
512 | .set_fmt = wm8804_set_fmt, | |
513 | .set_sysclk = wm8804_set_sysclk, | |
514 | .set_clkdiv = wm8804_set_clkdiv, | |
515 | .set_pll = wm8804_set_pll | |
516 | }; | |
517 | ||
518 | #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ | |
095e1de5 | 519 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE) |
33cf45c8 | 520 | |
3115ae17 MB |
521 | #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ |
522 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \ | |
523 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \ | |
524 | SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000) | |
525 | ||
33cf45c8 | 526 | static struct snd_soc_dai_driver wm8804_dai = { |
cb13c6b3 | 527 | .name = "wm8804-spdif", |
33cf45c8 DP |
528 | .playback = { |
529 | .stream_name = "Playback", | |
530 | .channels_min = 2, | |
531 | .channels_max = 2, | |
3115ae17 | 532 | .rates = WM8804_RATES, |
33cf45c8 DP |
533 | .formats = WM8804_FORMATS, |
534 | }, | |
535 | .capture = { | |
536 | .stream_name = "Capture", | |
537 | .channels_min = 2, | |
538 | .channels_max = 2, | |
3115ae17 | 539 | .rates = WM8804_RATES, |
33cf45c8 DP |
540 | .formats = WM8804_FORMATS, |
541 | }, | |
542 | .ops = &wm8804_dai_ops, | |
543 | .symmetric_rates = 1 | |
544 | }; | |
545 | ||
c0b36389 | 546 | static const struct snd_soc_codec_driver soc_codec_dev_wm8804 = { |
095e1de5 | 547 | .idle_bias_off = false, |
5a374524 | 548 | |
f1d13276 KM |
549 | .component_driver = { |
550 | .dapm_widgets = wm8804_dapm_widgets, | |
551 | .num_dapm_widgets = ARRAY_SIZE(wm8804_dapm_widgets), | |
552 | .dapm_routes = wm8804_dapm_routes, | |
553 | .num_dapm_routes = ARRAY_SIZE(wm8804_dapm_routes), | |
554 | }, | |
33cf45c8 DP |
555 | }; |
556 | ||
bbed297d | 557 | const struct regmap_config wm8804_regmap_config = { |
891271c2 MB |
558 | .reg_bits = 8, |
559 | .val_bits = 8, | |
560 | ||
561 | .max_register = WM8804_MAX_REGISTER, | |
562 | .volatile_reg = wm8804_volatile, | |
563 | ||
564 | .cache_type = REGCACHE_RBTREE, | |
565 | .reg_defaults = wm8804_reg_defaults, | |
566 | .num_reg_defaults = ARRAY_SIZE(wm8804_reg_defaults), | |
567 | }; | |
bbed297d | 568 | EXPORT_SYMBOL_GPL(wm8804_regmap_config); |
891271c2 | 569 | |
bbed297d | 570 | int wm8804_probe(struct device *dev, struct regmap *regmap) |
33cf45c8 DP |
571 | { |
572 | struct wm8804_priv *wm8804; | |
6f2c9348 CK |
573 | unsigned int id1, id2; |
574 | int i, ret; | |
33cf45c8 | 575 | |
bbed297d | 576 | wm8804 = devm_kzalloc(dev, sizeof(*wm8804), GFP_KERNEL); |
fe3e2e7f DC |
577 | if (!wm8804) |
578 | return -ENOMEM; | |
33cf45c8 | 579 | |
bbed297d | 580 | dev_set_drvdata(dev, wm8804); |
33cf45c8 | 581 | |
1a60667f | 582 | wm8804->dev = dev; |
bbed297d | 583 | wm8804->regmap = regmap; |
33cf45c8 | 584 | |
7e5ee1c3 CK |
585 | wm8804->reset = devm_gpiod_get_optional(dev, "wlf,reset", |
586 | GPIOD_OUT_LOW); | |
587 | if (IS_ERR(wm8804->reset)) { | |
588 | ret = PTR_ERR(wm8804->reset); | |
589 | dev_err(dev, "Failed to get reset line: %d\n", ret); | |
590 | return ret; | |
591 | } | |
592 | ||
6f2c9348 CK |
593 | for (i = 0; i < ARRAY_SIZE(wm8804->supplies); i++) |
594 | wm8804->supplies[i].supply = wm8804_supply_names[i]; | |
595 | ||
596 | ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(wm8804->supplies), | |
597 | wm8804->supplies); | |
598 | if (ret) { | |
599 | dev_err(dev, "Failed to request supplies: %d\n", ret); | |
600 | return ret; | |
601 | } | |
602 | ||
603 | wm8804->disable_nb[0].notifier_call = wm8804_regulator_event_0; | |
604 | wm8804->disable_nb[1].notifier_call = wm8804_regulator_event_1; | |
605 | ||
606 | /* This should really be moved into the regulator core */ | |
607 | for (i = 0; i < ARRAY_SIZE(wm8804->supplies); i++) { | |
0be9653a CK |
608 | struct regulator *regulator = wm8804->supplies[i].consumer; |
609 | ||
610 | ret = devm_regulator_register_notifier(regulator, | |
611 | &wm8804->disable_nb[i]); | |
6f2c9348 CK |
612 | if (ret != 0) { |
613 | dev_err(dev, | |
614 | "Failed to register regulator notifier: %d\n", | |
615 | ret); | |
fcf638f9 | 616 | return ret; |
6f2c9348 CK |
617 | } |
618 | } | |
619 | ||
620 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8804->supplies), | |
621 | wm8804->supplies); | |
622 | if (ret) { | |
623 | dev_err(dev, "Failed to enable supplies: %d\n", ret); | |
fcf638f9 | 624 | return ret; |
6f2c9348 CK |
625 | } |
626 | ||
7e5ee1c3 CK |
627 | if (wm8804->reset) |
628 | gpiod_set_value_cansleep(wm8804->reset, 1); | |
629 | ||
6f2c9348 CK |
630 | ret = regmap_read(regmap, WM8804_RST_DEVID1, &id1); |
631 | if (ret < 0) { | |
632 | dev_err(dev, "Failed to read device ID: %d\n", ret); | |
633 | goto err_reg_enable; | |
634 | } | |
635 | ||
636 | ret = regmap_read(regmap, WM8804_DEVID2, &id2); | |
637 | if (ret < 0) { | |
638 | dev_err(dev, "Failed to read device ID: %d\n", ret); | |
639 | goto err_reg_enable; | |
640 | } | |
641 | ||
642 | id2 = (id2 << 8) | id1; | |
643 | ||
644 | if (id2 != 0x8805) { | |
645 | dev_err(dev, "Invalid device ID: %#x\n", id2); | |
646 | ret = -EINVAL; | |
647 | goto err_reg_enable; | |
648 | } | |
649 | ||
650 | ret = regmap_read(regmap, WM8804_DEVREV, &id1); | |
651 | if (ret < 0) { | |
652 | dev_err(dev, "Failed to read device revision: %d\n", | |
653 | ret); | |
654 | goto err_reg_enable; | |
655 | } | |
656 | dev_info(dev, "revision %c\n", id1 + 'A'); | |
657 | ||
7e5ee1c3 CK |
658 | if (!wm8804->reset) { |
659 | ret = wm8804_soft_reset(wm8804); | |
660 | if (ret < 0) { | |
661 | dev_err(dev, "Failed to issue reset: %d\n", ret); | |
662 | goto err_reg_enable; | |
663 | } | |
6f2c9348 CK |
664 | } |
665 | ||
fcf638f9 CK |
666 | ret = snd_soc_register_codec(dev, &soc_codec_dev_wm8804, |
667 | &wm8804_dai, 1); | |
668 | if (ret < 0) { | |
669 | dev_err(dev, "Failed to register CODEC: %d\n", ret); | |
670 | goto err_reg_enable; | |
671 | } | |
672 | ||
1a60667f CK |
673 | pm_runtime_set_active(dev); |
674 | pm_runtime_enable(dev); | |
675 | pm_runtime_idle(dev); | |
676 | ||
fcf638f9 | 677 | return 0; |
6f2c9348 CK |
678 | |
679 | err_reg_enable: | |
680 | regulator_bulk_disable(ARRAY_SIZE(wm8804->supplies), wm8804->supplies); | |
681 | return ret; | |
33cf45c8 | 682 | } |
bbed297d | 683 | EXPORT_SYMBOL_GPL(wm8804_probe); |
33cf45c8 | 684 | |
bbed297d | 685 | void wm8804_remove(struct device *dev) |
33cf45c8 | 686 | { |
1a60667f | 687 | pm_runtime_disable(dev); |
bbed297d | 688 | snd_soc_unregister_codec(dev); |
33cf45c8 | 689 | } |
bbed297d | 690 | EXPORT_SYMBOL_GPL(wm8804_remove); |
33cf45c8 | 691 | |
1a60667f CK |
692 | #if IS_ENABLED(CONFIG_PM) |
693 | static int wm8804_runtime_resume(struct device *dev) | |
694 | { | |
695 | struct wm8804_priv *wm8804 = dev_get_drvdata(dev); | |
696 | int ret; | |
697 | ||
698 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8804->supplies), | |
699 | wm8804->supplies); | |
700 | if (ret) { | |
701 | dev_err(wm8804->dev, "Failed to enable supplies: %d\n", ret); | |
702 | return ret; | |
703 | } | |
704 | ||
705 | regcache_sync(wm8804->regmap); | |
706 | ||
707 | /* Power up OSCCLK */ | |
708 | regmap_update_bits(wm8804->regmap, WM8804_PWRDN, 0x8, 0x0); | |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
713 | static int wm8804_runtime_suspend(struct device *dev) | |
714 | { | |
715 | struct wm8804_priv *wm8804 = dev_get_drvdata(dev); | |
716 | ||
717 | /* Power down OSCCLK */ | |
718 | regmap_update_bits(wm8804->regmap, WM8804_PWRDN, 0x8, 0x8); | |
719 | ||
720 | regulator_bulk_disable(ARRAY_SIZE(wm8804->supplies), | |
721 | wm8804->supplies); | |
722 | ||
723 | return 0; | |
724 | } | |
725 | #endif | |
726 | ||
727 | const struct dev_pm_ops wm8804_pm = { | |
728 | SET_RUNTIME_PM_OPS(wm8804_runtime_suspend, wm8804_runtime_resume, NULL) | |
729 | }; | |
730 | EXPORT_SYMBOL_GPL(wm8804_pm); | |
731 | ||
33cf45c8 DP |
732 | MODULE_DESCRIPTION("ASoC WM8804 driver"); |
733 | MODULE_AUTHOR("Dimitris Papastamos <dp@opensource.wolfsonmicro.com>"); | |
734 | MODULE_LICENSE("GPL"); |