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