Commit | Line | Data |
---|---|---|
40e0aa64 RP |
1 | /* |
2 | * wm8731.c -- WM8731 ALSA SoC Audio driver | |
3 | * | |
4 | * Copyright 2005 Openedhand Ltd. | |
656baaeb | 5 | * Copyright 2006-12 Wolfson Microelectronics, plc |
40e0aa64 RP |
6 | * |
7 | * Author: Richard Purdie <richard@openedhand.com> | |
8 | * | |
9 | * Based on wm8753.c by Liam Girdwood | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | */ | |
15 | ||
16 | #include <linux/module.h> | |
17 | #include <linux/moduleparam.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/delay.h> | |
20 | #include <linux/pm.h> | |
21 | #include <linux/i2c.h> | |
5a0e3ad6 | 22 | #include <linux/slab.h> |
05d448e2 | 23 | #include <linux/regmap.h> |
7dea7c01 | 24 | #include <linux/regulator/consumer.h> |
d2a40355 | 25 | #include <linux/spi/spi.h> |
a7f96e4d | 26 | #include <linux/of_device.h> |
a51ff30f | 27 | #include <linux/mutex.h> |
40e0aa64 RP |
28 | #include <sound/core.h> |
29 | #include <sound/pcm.h> | |
30 | #include <sound/pcm_params.h> | |
31 | #include <sound/soc.h> | |
40e0aa64 | 32 | #include <sound/initval.h> |
d00efa64 | 33 | #include <sound/tlv.h> |
40e0aa64 RP |
34 | |
35 | #include "wm8731.h" | |
36 | ||
7dea7c01 MB |
37 | #define WM8731_NUM_SUPPLIES 4 |
38 | static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = { | |
39 | "AVDD", | |
40 | "HPVDD", | |
41 | "DCVDD", | |
42 | "DBVDD", | |
43 | }; | |
44 | ||
b36d61d4 FM |
45 | /* codec private data */ |
46 | struct wm8731_priv { | |
05d448e2 | 47 | struct regmap *regmap; |
7dea7c01 | 48 | struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES]; |
0890c2b7 | 49 | const struct snd_pcm_hw_constraint_list *constraints; |
b36d61d4 | 50 | unsigned int sysclk; |
9745e824 | 51 | int sysclk_type; |
dd31b310 MB |
52 | int playback_fs; |
53 | bool deemph; | |
a51ff30f LPC |
54 | |
55 | struct mutex lock; | |
b36d61d4 FM |
56 | }; |
57 | ||
a8035c8f | 58 | |
40e0aa64 RP |
59 | /* |
60 | * wm8731 register cache | |
40e0aa64 | 61 | */ |
05d448e2 MB |
62 | static const struct reg_default wm8731_reg_defaults[] = { |
63 | { 0, 0x0097 }, | |
64 | { 1, 0x0097 }, | |
65 | { 2, 0x0079 }, | |
66 | { 3, 0x0079 }, | |
67 | { 4, 0x000a }, | |
68 | { 5, 0x0008 }, | |
69 | { 6, 0x009f }, | |
70 | { 7, 0x000a }, | |
71 | { 8, 0x0000 }, | |
72 | { 9, 0x0000 }, | |
40e0aa64 RP |
73 | }; |
74 | ||
05d448e2 MB |
75 | static bool wm8731_volatile(struct device *dev, unsigned int reg) |
76 | { | |
77 | return reg == WM8731_RESET; | |
78 | } | |
79 | ||
80 | static bool wm8731_writeable(struct device *dev, unsigned int reg) | |
81 | { | |
82 | return reg <= WM8731_RESET; | |
83 | } | |
84 | ||
17a52fd6 | 85 | #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) |
40e0aa64 RP |
86 | |
87 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; | |
59f72970 | 88 | |
9e74b14a TI |
89 | static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum, |
90 | WM8731_APANA, 2, wm8731_input_select); | |
59f72970 | 91 | |
dd31b310 MB |
92 | static int wm8731_deemph[] = { 0, 32000, 44100, 48000 }; |
93 | ||
94 | static int wm8731_set_deemph(struct snd_soc_codec *codec) | |
95 | { | |
96 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); | |
97 | int val, i, best; | |
98 | ||
99 | /* If we're using deemphasis select the nearest available sample | |
100 | * rate. | |
101 | */ | |
102 | if (wm8731->deemph) { | |
103 | best = 1; | |
104 | for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) { | |
105 | if (abs(wm8731_deemph[i] - wm8731->playback_fs) < | |
106 | abs(wm8731_deemph[best] - wm8731->playback_fs)) | |
107 | best = i; | |
108 | } | |
109 | ||
110 | val = best << 1; | |
111 | } else { | |
112 | best = 0; | |
113 | val = 0; | |
114 | } | |
115 | ||
116 | dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n", | |
117 | best, wm8731_deemph[best]); | |
118 | ||
119 | return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val); | |
120 | } | |
121 | ||
122 | static int wm8731_get_deemph(struct snd_kcontrol *kcontrol, | |
123 | struct snd_ctl_elem_value *ucontrol) | |
124 | { | |
ea53bf77 | 125 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
dd31b310 MB |
126 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
127 | ||
bd14016f | 128 | ucontrol->value.integer.value[0] = wm8731->deemph; |
dd31b310 MB |
129 | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static int wm8731_put_deemph(struct snd_kcontrol *kcontrol, | |
134 | struct snd_ctl_elem_value *ucontrol) | |
135 | { | |
ea53bf77 | 136 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
dd31b310 | 137 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
bd14016f | 138 | int deemph = ucontrol->value.integer.value[0]; |
dd31b310 MB |
139 | int ret = 0; |
140 | ||
141 | if (deemph > 1) | |
142 | return -EINVAL; | |
143 | ||
a51ff30f | 144 | mutex_lock(&wm8731->lock); |
dd31b310 MB |
145 | if (wm8731->deemph != deemph) { |
146 | wm8731->deemph = deemph; | |
59f72970 | 147 | |
dd31b310 MB |
148 | wm8731_set_deemph(codec); |
149 | ||
150 | ret = 1; | |
151 | } | |
a51ff30f | 152 | mutex_unlock(&wm8731->lock); |
dd31b310 MB |
153 | |
154 | return ret; | |
155 | } | |
40e0aa64 | 156 | |
d00efa64 MB |
157 | static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); |
158 | static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); | |
159 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); | |
d921184e | 160 | static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0); |
d00efa64 | 161 | |
40e0aa64 RP |
162 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { |
163 | ||
d00efa64 MB |
164 | SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
165 | 0, 127, 0, out_tlv), | |
bd903b6e LG |
166 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, |
167 | 7, 1, 0), | |
40e0aa64 | 168 | |
d00efa64 MB |
169 | SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, |
170 | in_tlv), | |
40e0aa64 RP |
171 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), |
172 | ||
d921184e | 173 | SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv), |
ef38ed88 | 174 | SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), |
40e0aa64 | 175 | |
d00efa64 MB |
176 | SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, |
177 | sidetone_tlv), | |
40e0aa64 RP |
178 | |
179 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), | |
180 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), | |
181 | ||
dd31b310 MB |
182 | SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0, |
183 | wm8731_get_deemph, wm8731_put_deemph), | |
40e0aa64 RP |
184 | }; |
185 | ||
40e0aa64 RP |
186 | /* Output Mixer */ |
187 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { | |
188 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), | |
189 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), | |
190 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), | |
191 | }; | |
192 | ||
193 | /* Input mux */ | |
194 | static const struct snd_kcontrol_new wm8731_input_mux_controls = | |
59f72970 | 195 | SOC_DAPM_ENUM("Input Select", wm8731_insel_enum); |
40e0aa64 RP |
196 | |
197 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { | |
8a27bd9a | 198 | SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0), |
9745e824 | 199 | SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0), |
40e0aa64 RP |
200 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, |
201 | &wm8731_output_mixer_controls[0], | |
202 | ARRAY_SIZE(wm8731_output_mixer_controls)), | |
203 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), | |
204 | SND_SOC_DAPM_OUTPUT("LOUT"), | |
205 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | |
206 | SND_SOC_DAPM_OUTPUT("ROUT"), | |
207 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | |
208 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), | |
209 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), | |
210 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), | |
211 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), | |
212 | SND_SOC_DAPM_INPUT("MICIN"), | |
213 | SND_SOC_DAPM_INPUT("RLINEIN"), | |
214 | SND_SOC_DAPM_INPUT("LLINEIN"), | |
215 | }; | |
216 | ||
9745e824 MB |
217 | static int wm8731_check_osc(struct snd_soc_dapm_widget *source, |
218 | struct snd_soc_dapm_widget *sink) | |
219 | { | |
d286b805 LPC |
220 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm); |
221 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); | |
9745e824 | 222 | |
5a195b44 | 223 | return wm8731->sysclk_type == WM8731_SYSCLK_XTAL; |
9745e824 MB |
224 | } |
225 | ||
5e251aec | 226 | static const struct snd_soc_dapm_route wm8731_intercon[] = { |
9745e824 MB |
227 | {"DAC", NULL, "OSC", wm8731_check_osc}, |
228 | {"ADC", NULL, "OSC", wm8731_check_osc}, | |
8a27bd9a MB |
229 | {"DAC", NULL, "ACTIVE"}, |
230 | {"ADC", NULL, "ACTIVE"}, | |
9745e824 | 231 | |
40e0aa64 RP |
232 | /* output mixer */ |
233 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | |
234 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, | |
235 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, | |
236 | ||
237 | /* outputs */ | |
238 | {"RHPOUT", NULL, "Output Mixer"}, | |
239 | {"ROUT", NULL, "Output Mixer"}, | |
240 | {"LHPOUT", NULL, "Output Mixer"}, | |
241 | {"LOUT", NULL, "Output Mixer"}, | |
242 | ||
243 | /* input mux */ | |
244 | {"Input Mux", "Line In", "Line Input"}, | |
245 | {"Input Mux", "Mic", "Mic Bias"}, | |
246 | {"ADC", NULL, "Input Mux"}, | |
247 | ||
248 | /* inputs */ | |
249 | {"Line Input", NULL, "LLINEIN"}, | |
250 | {"Line Input", NULL, "RLINEIN"}, | |
251 | {"Mic Bias", NULL, "MICIN"}, | |
40e0aa64 RP |
252 | }; |
253 | ||
40e0aa64 RP |
254 | struct _coeff_div { |
255 | u32 mclk; | |
256 | u32 rate; | |
257 | u16 fs; | |
258 | u8 sr:4; | |
259 | u8 bosr:1; | |
260 | u8 usb:1; | |
261 | }; | |
262 | ||
263 | /* codec mclk clock divider coefficients */ | |
264 | static const struct _coeff_div coeff_div[] = { | |
265 | /* 48k */ | |
266 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, | |
267 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, | |
268 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, | |
269 | ||
270 | /* 32k */ | |
271 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, | |
272 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, | |
298a2c75 | 273 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
40e0aa64 RP |
274 | |
275 | /* 8k */ | |
276 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, | |
277 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, | |
278 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, | |
279 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, | |
280 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, | |
281 | ||
282 | /* 96k */ | |
283 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, | |
284 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, | |
285 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, | |
286 | ||
287 | /* 44.1k */ | |
288 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, | |
289 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, | |
290 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, | |
291 | ||
292 | /* 88.2k */ | |
293 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, | |
294 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, | |
295 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, | |
296 | }; | |
297 | ||
0890c2b7 RG |
298 | /* rates constraints */ |
299 | static const unsigned int wm8731_rates_12000000[] = { | |
300 | 8000, 32000, 44100, 48000, 96000, 88200, | |
301 | }; | |
302 | ||
303 | static const unsigned int wm8731_rates_12288000_18432000[] = { | |
304 | 8000, 32000, 48000, 96000, | |
305 | }; | |
306 | ||
307 | static const unsigned int wm8731_rates_11289600_16934400[] = { | |
308 | 8000, 44100, 88200, | |
309 | }; | |
310 | ||
311 | static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = { | |
312 | .list = wm8731_rates_12000000, | |
313 | .count = ARRAY_SIZE(wm8731_rates_12000000), | |
314 | }; | |
315 | ||
316 | static const | |
317 | struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = { | |
318 | .list = wm8731_rates_12288000_18432000, | |
319 | .count = ARRAY_SIZE(wm8731_rates_12288000_18432000), | |
320 | }; | |
321 | ||
322 | static const | |
323 | struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = { | |
324 | .list = wm8731_rates_11289600_16934400, | |
325 | .count = ARRAY_SIZE(wm8731_rates_11289600_16934400), | |
326 | }; | |
327 | ||
40e0aa64 RP |
328 | static inline int get_coeff(int mclk, int rate) |
329 | { | |
330 | int i; | |
331 | ||
332 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { | |
333 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) | |
334 | return i; | |
335 | } | |
336 | return 0; | |
337 | } | |
338 | ||
b36d61d4 | 339 | static int wm8731_hw_params(struct snd_pcm_substream *substream, |
dee89c4d MB |
340 | struct snd_pcm_hw_params *params, |
341 | struct snd_soc_dai *dai) | |
40e0aa64 | 342 | { |
f0fba2ad | 343 | struct snd_soc_codec *codec = dai->codec; |
b2c812e2 | 344 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
17a52fd6 | 345 | u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; |
b36d61d4 FM |
346 | int i = get_coeff(wm8731->sysclk, params_rate(params)); |
347 | u16 srate = (coeff_div[i].sr << 2) | | |
348 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; | |
40e0aa64 | 349 | |
dd31b310 MB |
350 | wm8731->playback_fs = params_rate(params); |
351 | ||
17a52fd6 | 352 | snd_soc_write(codec, WM8731_SRATE, srate); |
b36d61d4 FM |
353 | |
354 | /* bit size */ | |
dfb6778e MB |
355 | switch (params_width(params)) { |
356 | case 16: | |
b36d61d4 | 357 | break; |
dfb6778e | 358 | case 20: |
b36d61d4 FM |
359 | iface |= 0x0004; |
360 | break; | |
dfb6778e | 361 | case 24: |
b36d61d4 FM |
362 | iface |= 0x0008; |
363 | break; | |
364 | } | |
40e0aa64 | 365 | |
dd31b310 MB |
366 | wm8731_set_deemph(codec); |
367 | ||
17a52fd6 | 368 | snd_soc_write(codec, WM8731_IFACE, iface); |
b36d61d4 | 369 | return 0; |
40e0aa64 RP |
370 | } |
371 | ||
e550e17f | 372 | static int wm8731_mute(struct snd_soc_dai *dai, int mute) |
b36d61d4 FM |
373 | { |
374 | struct snd_soc_codec *codec = dai->codec; | |
17a52fd6 | 375 | u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; |
b36d61d4 FM |
376 | |
377 | if (mute) | |
17a52fd6 | 378 | snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); |
b36d61d4 | 379 | else |
17a52fd6 | 380 | snd_soc_write(codec, WM8731_APDIGI, mute_reg); |
b36d61d4 FM |
381 | return 0; |
382 | } | |
383 | ||
e550e17f | 384 | static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
b36d61d4 FM |
385 | int clk_id, unsigned int freq, int dir) |
386 | { | |
387 | struct snd_soc_codec *codec = codec_dai->codec; | |
b2c812e2 | 388 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
b36d61d4 | 389 | |
9745e824 MB |
390 | switch (clk_id) { |
391 | case WM8731_SYSCLK_XTAL: | |
392 | case WM8731_SYSCLK_MCLK: | |
393 | wm8731->sysclk_type = clk_id; | |
394 | break; | |
395 | default: | |
396 | return -EINVAL; | |
397 | } | |
398 | ||
b36d61d4 | 399 | switch (freq) { |
0890c2b7 RG |
400 | case 0: |
401 | wm8731->constraints = NULL; | |
402 | break; | |
b36d61d4 | 403 | case 12000000: |
0890c2b7 RG |
404 | wm8731->constraints = &wm8731_constraints_12000000; |
405 | break; | |
b36d61d4 | 406 | case 12288000: |
b36d61d4 | 407 | case 18432000: |
0890c2b7 RG |
408 | wm8731->constraints = &wm8731_constraints_12288000_18432000; |
409 | break; | |
410 | case 16934400: | |
411 | case 11289600: | |
412 | wm8731->constraints = &wm8731_constraints_11289600_16934400; | |
9745e824 MB |
413 | break; |
414 | default: | |
415 | return -EINVAL; | |
b36d61d4 | 416 | } |
9745e824 | 417 | |
0890c2b7 RG |
418 | wm8731->sysclk = freq; |
419 | ||
ce6120cc | 420 | snd_soc_dapm_sync(&codec->dapm); |
9745e824 MB |
421 | |
422 | return 0; | |
b36d61d4 FM |
423 | } |
424 | ||
425 | ||
e550e17f | 426 | static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, |
b36d61d4 FM |
427 | unsigned int fmt) |
428 | { | |
429 | struct snd_soc_codec *codec = codec_dai->codec; | |
430 | u16 iface = 0; | |
40e0aa64 RP |
431 | |
432 | /* set master/slave audio interface */ | |
b36d61d4 | 433 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
40e0aa64 RP |
434 | case SND_SOC_DAIFMT_CBM_CFM: |
435 | iface |= 0x0040; | |
436 | break; | |
437 | case SND_SOC_DAIFMT_CBS_CFS: | |
438 | break; | |
b36d61d4 FM |
439 | default: |
440 | return -EINVAL; | |
40e0aa64 | 441 | } |
40e0aa64 RP |
442 | |
443 | /* interface format */ | |
b36d61d4 | 444 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
40e0aa64 RP |
445 | case SND_SOC_DAIFMT_I2S: |
446 | iface |= 0x0002; | |
447 | break; | |
448 | case SND_SOC_DAIFMT_RIGHT_J: | |
449 | break; | |
450 | case SND_SOC_DAIFMT_LEFT_J: | |
451 | iface |= 0x0001; | |
452 | break; | |
453 | case SND_SOC_DAIFMT_DSP_A: | |
b4af6ef9 | 454 | iface |= 0x0013; |
40e0aa64 RP |
455 | break; |
456 | case SND_SOC_DAIFMT_DSP_B: | |
b4af6ef9 | 457 | iface |= 0x0003; |
40e0aa64 | 458 | break; |
b36d61d4 FM |
459 | default: |
460 | return -EINVAL; | |
40e0aa64 RP |
461 | } |
462 | ||
463 | /* clock inversion */ | |
b36d61d4 | 464 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
40e0aa64 RP |
465 | case SND_SOC_DAIFMT_NB_NF: |
466 | break; | |
467 | case SND_SOC_DAIFMT_IB_IF: | |
468 | iface |= 0x0090; | |
469 | break; | |
470 | case SND_SOC_DAIFMT_IB_NF: | |
471 | iface |= 0x0080; | |
472 | break; | |
473 | case SND_SOC_DAIFMT_NB_IF: | |
474 | iface |= 0x0010; | |
475 | break; | |
b36d61d4 FM |
476 | default: |
477 | return -EINVAL; | |
40e0aa64 RP |
478 | } |
479 | ||
480 | /* set iface */ | |
17a52fd6 | 481 | snd_soc_write(codec, WM8731_IFACE, iface); |
40e0aa64 RP |
482 | return 0; |
483 | } | |
484 | ||
0be9898a MB |
485 | static int wm8731_set_bias_level(struct snd_soc_codec *codec, |
486 | enum snd_soc_bias_level level) | |
40e0aa64 | 487 | { |
06ae9988 | 488 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
9bf311fe | 489 | int ret; |
22d22ee5 | 490 | u16 reg; |
40e0aa64 | 491 | |
0be9898a MB |
492 | switch (level) { |
493 | case SND_SOC_BIAS_ON: | |
40e0aa64 | 494 | break; |
0be9898a | 495 | case SND_SOC_BIAS_PREPARE: |
40e0aa64 | 496 | break; |
0be9898a | 497 | case SND_SOC_BIAS_STANDBY: |
ce6120cc | 498 | if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) { |
06ae9988 MB |
499 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), |
500 | wm8731->supplies); | |
501 | if (ret != 0) | |
502 | return ret; | |
503 | ||
05d448e2 | 504 | regcache_sync(wm8731->regmap); |
06ae9988 MB |
505 | } |
506 | ||
22d22ee5 | 507 | /* Clear PWROFF, gate CLKOUT, everything else as-is */ |
17a52fd6 MB |
508 | reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; |
509 | snd_soc_write(codec, WM8731_PWR, reg | 0x0040); | |
40e0aa64 | 510 | break; |
0be9898a | 511 | case SND_SOC_BIAS_OFF: |
17a52fd6 | 512 | snd_soc_write(codec, WM8731_PWR, 0xffff); |
06ae9988 MB |
513 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), |
514 | wm8731->supplies); | |
05d448e2 | 515 | regcache_mark_dirty(wm8731->regmap); |
40e0aa64 RP |
516 | break; |
517 | } | |
ce6120cc | 518 | codec->dapm.bias_level = level; |
40e0aa64 RP |
519 | return 0; |
520 | } | |
521 | ||
0890c2b7 RG |
522 | static int wm8731_startup(struct snd_pcm_substream *substream, |
523 | struct snd_soc_dai *dai) | |
524 | { | |
525 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec); | |
526 | ||
527 | if (wm8731->constraints) | |
528 | snd_pcm_hw_constraint_list(substream->runtime, 0, | |
529 | SNDRV_PCM_HW_PARAM_RATE, | |
530 | wm8731->constraints); | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
e135443e | 535 | #define WM8731_RATES SNDRV_PCM_RATE_8000_96000 |
b36d61d4 FM |
536 | |
537 | #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ | |
538 | SNDRV_PCM_FMTBIT_S24_LE) | |
539 | ||
85e7652d | 540 | static const struct snd_soc_dai_ops wm8731_dai_ops = { |
0890c2b7 | 541 | .startup = wm8731_startup, |
6335d055 | 542 | .hw_params = wm8731_hw_params, |
6335d055 EM |
543 | .digital_mute = wm8731_mute, |
544 | .set_sysclk = wm8731_set_dai_sysclk, | |
545 | .set_fmt = wm8731_set_dai_fmt, | |
546 | }; | |
547 | ||
f0fba2ad LG |
548 | static struct snd_soc_dai_driver wm8731_dai = { |
549 | .name = "wm8731-hifi", | |
40e0aa64 RP |
550 | .playback = { |
551 | .stream_name = "Playback", | |
552 | .channels_min = 1, | |
553 | .channels_max = 2, | |
b36d61d4 FM |
554 | .rates = WM8731_RATES, |
555 | .formats = WM8731_FORMATS,}, | |
40e0aa64 RP |
556 | .capture = { |
557 | .stream_name = "Capture", | |
558 | .channels_min = 1, | |
559 | .channels_max = 2, | |
b36d61d4 FM |
560 | .rates = WM8731_RATES, |
561 | .formats = WM8731_FORMATS,}, | |
6335d055 | 562 | .ops = &wm8731_dai_ops, |
4934482d | 563 | .symmetric_rates = 1, |
40e0aa64 | 564 | }; |
40e0aa64 | 565 | |
f0fba2ad | 566 | static int wm8731_probe(struct snd_soc_codec *codec) |
40e0aa64 | 567 | { |
f0fba2ad LG |
568 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
569 | int ret = 0, i; | |
5998102b | 570 | |
7dea7c01 MB |
571 | for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++) |
572 | wm8731->supplies[i].supply = wm8731_supply_names[i]; | |
573 | ||
3598aad5 | 574 | ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies), |
7dea7c01 MB |
575 | wm8731->supplies); |
576 | if (ret != 0) { | |
577 | dev_err(codec->dev, "Failed to request supplies: %d\n", ret); | |
f0fba2ad | 578 | return ret; |
7dea7c01 MB |
579 | } |
580 | ||
581 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies), | |
582 | wm8731->supplies); | |
583 | if (ret != 0) { | |
584 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); | |
3598aad5 | 585 | return ret; |
7dea7c01 MB |
586 | } |
587 | ||
519cf2df MB |
588 | ret = wm8731_reset(codec); |
589 | if (ret < 0) { | |
fe5422fc | 590 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); |
7dea7c01 | 591 | goto err_regulator_enable; |
519cf2df MB |
592 | } |
593 | ||
5998102b MB |
594 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
595 | ||
596 | /* Latch the update bits */ | |
17a52fd6 MB |
597 | snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); |
598 | snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); | |
599 | snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); | |
600 | snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); | |
5998102b | 601 | |
ce3bdaa8 | 602 | /* Disable bypass path by default */ |
2062ea52 | 603 | snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0); |
ce3bdaa8 | 604 | |
06ae9988 MB |
605 | /* Regulators will have been enabled by bias management */ |
606 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); | |
607 | ||
a8035c8f | 608 | return 0; |
fe5422fc | 609 | |
7dea7c01 MB |
610 | err_regulator_enable: |
611 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); | |
f0fba2ad | 612 | |
fe5422fc | 613 | return ret; |
a8035c8f MB |
614 | } |
615 | ||
f0fba2ad LG |
616 | /* power down chip */ |
617 | static int wm8731_remove(struct snd_soc_codec *codec) | |
5998102b | 618 | { |
f0fba2ad LG |
619 | struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec); |
620 | ||
f0fba2ad | 621 | regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies); |
f0fba2ad LG |
622 | |
623 | return 0; | |
5998102b | 624 | } |
a8035c8f | 625 | |
f0fba2ad LG |
626 | static struct snd_soc_codec_driver soc_codec_dev_wm8731 = { |
627 | .probe = wm8731_probe, | |
628 | .remove = wm8731_remove, | |
f0fba2ad | 629 | .set_bias_level = wm8731_set_bias_level, |
2081b2cf LPC |
630 | .suspend_bias_off = true, |
631 | ||
5e251aec MB |
632 | .dapm_widgets = wm8731_dapm_widgets, |
633 | .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets), | |
634 | .dapm_routes = wm8731_intercon, | |
635 | .num_dapm_routes = ARRAY_SIZE(wm8731_intercon), | |
cb555318 MB |
636 | .controls = wm8731_snd_controls, |
637 | .num_controls = ARRAY_SIZE(wm8731_snd_controls), | |
f0fba2ad LG |
638 | }; |
639 | ||
a7f96e4d MB |
640 | static const struct of_device_id wm8731_of_match[] = { |
641 | { .compatible = "wlf,wm8731", }, | |
642 | { } | |
643 | }; | |
644 | ||
645 | MODULE_DEVICE_TABLE(of, wm8731_of_match); | |
646 | ||
05d448e2 MB |
647 | static const struct regmap_config wm8731_regmap = { |
648 | .reg_bits = 7, | |
649 | .val_bits = 9, | |
650 | ||
651 | .max_register = WM8731_RESET, | |
652 | .volatile_reg = wm8731_volatile, | |
653 | .writeable_reg = wm8731_writeable, | |
654 | ||
655 | .cache_type = REGCACHE_RBTREE, | |
656 | .reg_defaults = wm8731_reg_defaults, | |
657 | .num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults), | |
658 | }; | |
659 | ||
5998102b | 660 | #if defined(CONFIG_SPI_MASTER) |
7a79e94e | 661 | static int wm8731_spi_probe(struct spi_device *spi) |
5998102b | 662 | { |
5998102b | 663 | struct wm8731_priv *wm8731; |
f0fba2ad | 664 | int ret; |
5998102b | 665 | |
cea82d8a | 666 | wm8731 = devm_kzalloc(&spi->dev, sizeof(*wm8731), GFP_KERNEL); |
5998102b MB |
667 | if (wm8731 == NULL) |
668 | return -ENOMEM; | |
669 | ||
a51ff30f LPC |
670 | mutex_init(&wm8731->lock); |
671 | ||
f1992dde | 672 | wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap); |
05d448e2 MB |
673 | if (IS_ERR(wm8731->regmap)) { |
674 | ret = PTR_ERR(wm8731->regmap); | |
675 | dev_err(&spi->dev, "Failed to allocate register map: %d\n", | |
676 | ret); | |
f1992dde | 677 | return ret; |
05d448e2 MB |
678 | } |
679 | ||
f0fba2ad | 680 | spi_set_drvdata(spi, wm8731); |
93b760b7 | 681 | |
f0fba2ad LG |
682 | ret = snd_soc_register_codec(&spi->dev, |
683 | &soc_codec_dev_wm8731, &wm8731_dai, 1); | |
05d448e2 MB |
684 | if (ret != 0) { |
685 | dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret); | |
f1992dde | 686 | return ret; |
05d448e2 MB |
687 | } |
688 | ||
689 | return 0; | |
5998102b MB |
690 | } |
691 | ||
7a79e94e | 692 | static int wm8731_spi_remove(struct spi_device *spi) |
5998102b | 693 | { |
f0fba2ad | 694 | snd_soc_unregister_codec(&spi->dev); |
5998102b MB |
695 | return 0; |
696 | } | |
697 | ||
698 | static struct spi_driver wm8731_spi_driver = { | |
699 | .driver = { | |
99b59f3c | 700 | .name = "wm8731", |
5998102b | 701 | .owner = THIS_MODULE, |
a7f96e4d | 702 | .of_match_table = wm8731_of_match, |
5998102b MB |
703 | }, |
704 | .probe = wm8731_spi_probe, | |
7a79e94e | 705 | .remove = wm8731_spi_remove, |
5998102b | 706 | }; |
a8035c8f MB |
707 | #endif /* CONFIG_SPI_MASTER */ |
708 | ||
b65ab73e | 709 | #if IS_ENABLED(CONFIG_I2C) |
7a79e94e BP |
710 | static int wm8731_i2c_probe(struct i2c_client *i2c, |
711 | const struct i2c_device_id *id) | |
a8035c8f | 712 | { |
5998102b | 713 | struct wm8731_priv *wm8731; |
f0fba2ad | 714 | int ret; |
a8035c8f | 715 | |
f1992dde MB |
716 | wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv), |
717 | GFP_KERNEL); | |
5998102b MB |
718 | if (wm8731 == NULL) |
719 | return -ENOMEM; | |
720 | ||
8a6cf30b ML |
721 | mutex_init(&wm8731->lock); |
722 | ||
f1992dde | 723 | wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap); |
05d448e2 MB |
724 | if (IS_ERR(wm8731->regmap)) { |
725 | ret = PTR_ERR(wm8731->regmap); | |
726 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", | |
727 | ret); | |
f1992dde | 728 | return ret; |
05d448e2 MB |
729 | } |
730 | ||
5998102b | 731 | i2c_set_clientdata(i2c, wm8731); |
a8035c8f | 732 | |
05d448e2 | 733 | ret = snd_soc_register_codec(&i2c->dev, |
f0fba2ad | 734 | &soc_codec_dev_wm8731, &wm8731_dai, 1); |
05d448e2 MB |
735 | if (ret != 0) { |
736 | dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret); | |
f1992dde | 737 | return ret; |
05d448e2 MB |
738 | } |
739 | ||
740 | return 0; | |
a8035c8f MB |
741 | } |
742 | ||
7a79e94e | 743 | static int wm8731_i2c_remove(struct i2c_client *client) |
a8035c8f | 744 | { |
f0fba2ad | 745 | snd_soc_unregister_codec(&client->dev); |
a8035c8f MB |
746 | return 0; |
747 | } | |
748 | ||
749 | static const struct i2c_device_id wm8731_i2c_id[] = { | |
750 | { "wm8731", 0 }, | |
751 | { } | |
752 | }; | |
753 | MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); | |
754 | ||
755 | static struct i2c_driver wm8731_i2c_driver = { | |
756 | .driver = { | |
99b59f3c | 757 | .name = "wm8731", |
a8035c8f | 758 | .owner = THIS_MODULE, |
a7f96e4d | 759 | .of_match_table = wm8731_of_match, |
a8035c8f MB |
760 | }, |
761 | .probe = wm8731_i2c_probe, | |
7a79e94e | 762 | .remove = wm8731_i2c_remove, |
a8035c8f MB |
763 | .id_table = wm8731_i2c_id, |
764 | }; | |
765 | #endif | |
766 | ||
c9b3a40f | 767 | static int __init wm8731_modinit(void) |
64089b84 | 768 | { |
f0fba2ad | 769 | int ret = 0; |
b65ab73e | 770 | #if IS_ENABLED(CONFIG_I2C) |
5998102b MB |
771 | ret = i2c_add_driver(&wm8731_i2c_driver); |
772 | if (ret != 0) { | |
773 | printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", | |
774 | ret); | |
775 | } | |
776 | #endif | |
777 | #if defined(CONFIG_SPI_MASTER) | |
778 | ret = spi_register_driver(&wm8731_spi_driver); | |
779 | if (ret != 0) { | |
780 | printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", | |
781 | ret); | |
782 | } | |
783 | #endif | |
f0fba2ad | 784 | return ret; |
64089b84 MB |
785 | } |
786 | module_init(wm8731_modinit); | |
787 | ||
788 | static void __exit wm8731_exit(void) | |
789 | { | |
b65ab73e | 790 | #if IS_ENABLED(CONFIG_I2C) |
5998102b MB |
791 | i2c_del_driver(&wm8731_i2c_driver); |
792 | #endif | |
793 | #if defined(CONFIG_SPI_MASTER) | |
794 | spi_unregister_driver(&wm8731_spi_driver); | |
795 | #endif | |
64089b84 MB |
796 | } |
797 | module_exit(wm8731_exit); | |
798 | ||
40e0aa64 RP |
799 | MODULE_DESCRIPTION("ASoC WM8731 driver"); |
800 | MODULE_AUTHOR("Richard Purdie"); | |
801 | MODULE_LICENSE("GPL"); |