]>
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> | |
21 | #include <linux/platform_device.h> | |
d2a40355 | 22 | #include <linux/spi/spi.h> |
40e0aa64 RP |
23 | #include <sound/core.h> |
24 | #include <sound/pcm.h> | |
25 | #include <sound/pcm_params.h> | |
26 | #include <sound/soc.h> | |
27 | #include <sound/soc-dapm.h> | |
28 | #include <sound/initval.h> | |
d00efa64 | 29 | #include <sound/tlv.h> |
40e0aa64 RP |
30 | |
31 | #include "wm8731.h" | |
32 | ||
5998102b | 33 | static struct snd_soc_codec *wm8731_codec; |
40e0aa64 RP |
34 | struct snd_soc_codec_device soc_codec_dev_wm8731; |
35 | ||
b36d61d4 FM |
36 | /* codec private data */ |
37 | struct wm8731_priv { | |
5998102b MB |
38 | struct snd_soc_codec codec; |
39 | u16 reg_cache[WM8731_CACHEREGNUM]; | |
b36d61d4 FM |
40 | unsigned int sysclk; |
41 | }; | |
42 | ||
a8035c8f | 43 | |
40e0aa64 RP |
44 | /* |
45 | * wm8731 register cache | |
46 | * We can't read the WM8731 register space when we are | |
47 | * using 2 wire for device control, so we cache them instead. | |
48 | * There is no point in caching the reset register | |
49 | */ | |
50 | static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { | |
17a52fd6 MB |
51 | 0x0097, 0x0097, 0x0079, 0x0079, |
52 | 0x000a, 0x0008, 0x009f, 0x000a, | |
53 | 0x0000, 0x0000 | |
40e0aa64 RP |
54 | }; |
55 | ||
17a52fd6 | 56 | #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0) |
40e0aa64 RP |
57 | |
58 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; | |
59 | static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | |
60 | ||
61 | static const struct soc_enum wm8731_enum[] = { | |
62 | SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), | |
63 | SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), | |
64 | }; | |
65 | ||
d00efa64 MB |
66 | static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0); |
67 | static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0); | |
68 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); | |
69 | ||
40e0aa64 RP |
70 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { |
71 | ||
d00efa64 MB |
72 | SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
73 | 0, 127, 0, out_tlv), | |
bd903b6e LG |
74 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, |
75 | 7, 1, 0), | |
40e0aa64 | 76 | |
d00efa64 MB |
77 | SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0, |
78 | in_tlv), | |
40e0aa64 RP |
79 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), |
80 | ||
81 | SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0), | |
ef38ed88 | 82 | SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1), |
40e0aa64 | 83 | |
d00efa64 MB |
84 | SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1, |
85 | sidetone_tlv), | |
40e0aa64 RP |
86 | |
87 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), | |
88 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), | |
89 | ||
90 | SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), | |
91 | }; | |
92 | ||
40e0aa64 RP |
93 | /* Output Mixer */ |
94 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { | |
95 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), | |
96 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), | |
97 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), | |
98 | }; | |
99 | ||
100 | /* Input mux */ | |
101 | static const struct snd_kcontrol_new wm8731_input_mux_controls = | |
102 | SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); | |
103 | ||
104 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { | |
105 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, | |
106 | &wm8731_output_mixer_controls[0], | |
107 | ARRAY_SIZE(wm8731_output_mixer_controls)), | |
108 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), | |
109 | SND_SOC_DAPM_OUTPUT("LOUT"), | |
110 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | |
111 | SND_SOC_DAPM_OUTPUT("ROUT"), | |
112 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | |
113 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), | |
114 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), | |
115 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), | |
116 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), | |
117 | SND_SOC_DAPM_INPUT("MICIN"), | |
118 | SND_SOC_DAPM_INPUT("RLINEIN"), | |
119 | SND_SOC_DAPM_INPUT("LLINEIN"), | |
120 | }; | |
121 | ||
a65f0568 | 122 | static const struct snd_soc_dapm_route intercon[] = { |
40e0aa64 RP |
123 | /* output mixer */ |
124 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | |
125 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, | |
126 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, | |
127 | ||
128 | /* outputs */ | |
129 | {"RHPOUT", NULL, "Output Mixer"}, | |
130 | {"ROUT", NULL, "Output Mixer"}, | |
131 | {"LHPOUT", NULL, "Output Mixer"}, | |
132 | {"LOUT", NULL, "Output Mixer"}, | |
133 | ||
134 | /* input mux */ | |
135 | {"Input Mux", "Line In", "Line Input"}, | |
136 | {"Input Mux", "Mic", "Mic Bias"}, | |
137 | {"ADC", NULL, "Input Mux"}, | |
138 | ||
139 | /* inputs */ | |
140 | {"Line Input", NULL, "LLINEIN"}, | |
141 | {"Line Input", NULL, "RLINEIN"}, | |
142 | {"Mic Bias", NULL, "MICIN"}, | |
40e0aa64 RP |
143 | }; |
144 | ||
145 | static int wm8731_add_widgets(struct snd_soc_codec *codec) | |
146 | { | |
a65f0568 MB |
147 | snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets, |
148 | ARRAY_SIZE(wm8731_dapm_widgets)); | |
40e0aa64 | 149 | |
a65f0568 | 150 | snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon)); |
40e0aa64 RP |
151 | |
152 | snd_soc_dapm_new_widgets(codec); | |
153 | return 0; | |
154 | } | |
155 | ||
156 | struct _coeff_div { | |
157 | u32 mclk; | |
158 | u32 rate; | |
159 | u16 fs; | |
160 | u8 sr:4; | |
161 | u8 bosr:1; | |
162 | u8 usb:1; | |
163 | }; | |
164 | ||
165 | /* codec mclk clock divider coefficients */ | |
166 | static const struct _coeff_div coeff_div[] = { | |
167 | /* 48k */ | |
168 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, | |
169 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, | |
170 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, | |
171 | ||
172 | /* 32k */ | |
173 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, | |
174 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, | |
298a2c75 | 175 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
40e0aa64 RP |
176 | |
177 | /* 8k */ | |
178 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, | |
179 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, | |
180 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, | |
181 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, | |
182 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, | |
183 | ||
184 | /* 96k */ | |
185 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, | |
186 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, | |
187 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, | |
188 | ||
189 | /* 44.1k */ | |
190 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, | |
191 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, | |
192 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, | |
193 | ||
194 | /* 88.2k */ | |
195 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, | |
196 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, | |
197 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, | |
198 | }; | |
199 | ||
200 | static inline int get_coeff(int mclk, int rate) | |
201 | { | |
202 | int i; | |
203 | ||
204 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { | |
205 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) | |
206 | return i; | |
207 | } | |
208 | return 0; | |
209 | } | |
210 | ||
b36d61d4 | 211 | static int wm8731_hw_params(struct snd_pcm_substream *substream, |
dee89c4d MB |
212 | struct snd_pcm_hw_params *params, |
213 | struct snd_soc_dai *dai) | |
40e0aa64 | 214 | { |
b36d61d4 FM |
215 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
216 | struct snd_soc_device *socdev = rtd->socdev; | |
6627a653 | 217 | struct snd_soc_codec *codec = socdev->card->codec; |
b36d61d4 | 218 | struct wm8731_priv *wm8731 = codec->private_data; |
17a52fd6 | 219 | u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3; |
b36d61d4 FM |
220 | int i = get_coeff(wm8731->sysclk, params_rate(params)); |
221 | u16 srate = (coeff_div[i].sr << 2) | | |
222 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; | |
40e0aa64 | 223 | |
17a52fd6 | 224 | snd_soc_write(codec, WM8731_SRATE, srate); |
b36d61d4 FM |
225 | |
226 | /* bit size */ | |
227 | switch (params_format(params)) { | |
228 | case SNDRV_PCM_FORMAT_S16_LE: | |
229 | break; | |
230 | case SNDRV_PCM_FORMAT_S20_3LE: | |
231 | iface |= 0x0004; | |
232 | break; | |
233 | case SNDRV_PCM_FORMAT_S24_LE: | |
234 | iface |= 0x0008; | |
235 | break; | |
236 | } | |
40e0aa64 | 237 | |
17a52fd6 | 238 | snd_soc_write(codec, WM8731_IFACE, iface); |
b36d61d4 | 239 | return 0; |
40e0aa64 RP |
240 | } |
241 | ||
dee89c4d MB |
242 | static int wm8731_pcm_prepare(struct snd_pcm_substream *substream, |
243 | struct snd_soc_dai *dai) | |
40e0aa64 RP |
244 | { |
245 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
246 | struct snd_soc_device *socdev = rtd->socdev; | |
6627a653 | 247 | struct snd_soc_codec *codec = socdev->card->codec; |
b36d61d4 FM |
248 | |
249 | /* set active */ | |
17a52fd6 | 250 | snd_soc_write(codec, WM8731_ACTIVE, 0x0001); |
b36d61d4 FM |
251 | |
252 | return 0; | |
253 | } | |
254 | ||
dee89c4d MB |
255 | static void wm8731_shutdown(struct snd_pcm_substream *substream, |
256 | struct snd_soc_dai *dai) | |
b36d61d4 FM |
257 | { |
258 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
259 | struct snd_soc_device *socdev = rtd->socdev; | |
6627a653 | 260 | struct snd_soc_codec *codec = socdev->card->codec; |
b36d61d4 FM |
261 | |
262 | /* deactivate */ | |
263 | if (!codec->active) { | |
264 | udelay(50); | |
17a52fd6 | 265 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
b36d61d4 FM |
266 | } |
267 | } | |
268 | ||
e550e17f | 269 | static int wm8731_mute(struct snd_soc_dai *dai, int mute) |
b36d61d4 FM |
270 | { |
271 | struct snd_soc_codec *codec = dai->codec; | |
17a52fd6 | 272 | u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7; |
b36d61d4 FM |
273 | |
274 | if (mute) | |
17a52fd6 | 275 | snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8); |
b36d61d4 | 276 | else |
17a52fd6 | 277 | snd_soc_write(codec, WM8731_APDIGI, mute_reg); |
b36d61d4 FM |
278 | return 0; |
279 | } | |
280 | ||
e550e17f | 281 | static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
b36d61d4 FM |
282 | int clk_id, unsigned int freq, int dir) |
283 | { | |
284 | struct snd_soc_codec *codec = codec_dai->codec; | |
285 | struct wm8731_priv *wm8731 = codec->private_data; | |
286 | ||
287 | switch (freq) { | |
288 | case 11289600: | |
289 | case 12000000: | |
290 | case 12288000: | |
291 | case 16934400: | |
292 | case 18432000: | |
293 | wm8731->sysclk = freq; | |
294 | return 0; | |
295 | } | |
296 | return -EINVAL; | |
297 | } | |
298 | ||
299 | ||
e550e17f | 300 | static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai, |
b36d61d4 FM |
301 | unsigned int fmt) |
302 | { | |
303 | struct snd_soc_codec *codec = codec_dai->codec; | |
304 | u16 iface = 0; | |
40e0aa64 RP |
305 | |
306 | /* set master/slave audio interface */ | |
b36d61d4 | 307 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
40e0aa64 RP |
308 | case SND_SOC_DAIFMT_CBM_CFM: |
309 | iface |= 0x0040; | |
310 | break; | |
311 | case SND_SOC_DAIFMT_CBS_CFS: | |
312 | break; | |
b36d61d4 FM |
313 | default: |
314 | return -EINVAL; | |
40e0aa64 | 315 | } |
40e0aa64 RP |
316 | |
317 | /* interface format */ | |
b36d61d4 | 318 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
40e0aa64 RP |
319 | case SND_SOC_DAIFMT_I2S: |
320 | iface |= 0x0002; | |
321 | break; | |
322 | case SND_SOC_DAIFMT_RIGHT_J: | |
323 | break; | |
324 | case SND_SOC_DAIFMT_LEFT_J: | |
325 | iface |= 0x0001; | |
326 | break; | |
327 | case SND_SOC_DAIFMT_DSP_A: | |
328 | iface |= 0x0003; | |
329 | break; | |
330 | case SND_SOC_DAIFMT_DSP_B: | |
331 | iface |= 0x0013; | |
332 | break; | |
b36d61d4 FM |
333 | default: |
334 | return -EINVAL; | |
40e0aa64 RP |
335 | } |
336 | ||
337 | /* clock inversion */ | |
b36d61d4 | 338 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
40e0aa64 RP |
339 | case SND_SOC_DAIFMT_NB_NF: |
340 | break; | |
341 | case SND_SOC_DAIFMT_IB_IF: | |
342 | iface |= 0x0090; | |
343 | break; | |
344 | case SND_SOC_DAIFMT_IB_NF: | |
345 | iface |= 0x0080; | |
346 | break; | |
347 | case SND_SOC_DAIFMT_NB_IF: | |
348 | iface |= 0x0010; | |
349 | break; | |
b36d61d4 FM |
350 | default: |
351 | return -EINVAL; | |
40e0aa64 RP |
352 | } |
353 | ||
354 | /* set iface */ | |
17a52fd6 | 355 | snd_soc_write(codec, WM8731_IFACE, iface); |
40e0aa64 RP |
356 | return 0; |
357 | } | |
358 | ||
0be9898a MB |
359 | static int wm8731_set_bias_level(struct snd_soc_codec *codec, |
360 | enum snd_soc_bias_level level) | |
40e0aa64 | 361 | { |
22d22ee5 | 362 | u16 reg; |
40e0aa64 | 363 | |
0be9898a MB |
364 | switch (level) { |
365 | case SND_SOC_BIAS_ON: | |
40e0aa64 | 366 | break; |
0be9898a | 367 | case SND_SOC_BIAS_PREPARE: |
40e0aa64 | 368 | break; |
0be9898a | 369 | case SND_SOC_BIAS_STANDBY: |
22d22ee5 | 370 | /* Clear PWROFF, gate CLKOUT, everything else as-is */ |
17a52fd6 MB |
371 | reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f; |
372 | snd_soc_write(codec, WM8731_PWR, reg | 0x0040); | |
40e0aa64 | 373 | break; |
0be9898a | 374 | case SND_SOC_BIAS_OFF: |
17a52fd6 MB |
375 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
376 | snd_soc_write(codec, WM8731_PWR, 0xffff); | |
40e0aa64 RP |
377 | break; |
378 | } | |
0be9898a | 379 | codec->bias_level = level; |
40e0aa64 RP |
380 | return 0; |
381 | } | |
382 | ||
b36d61d4 FM |
383 | #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
384 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ | |
385 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\ | |
386 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\ | |
387 | SNDRV_PCM_RATE_96000) | |
388 | ||
389 | #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ | |
390 | SNDRV_PCM_FMTBIT_S24_LE) | |
391 | ||
6335d055 EM |
392 | static struct snd_soc_dai_ops wm8731_dai_ops = { |
393 | .prepare = wm8731_pcm_prepare, | |
394 | .hw_params = wm8731_hw_params, | |
395 | .shutdown = wm8731_shutdown, | |
396 | .digital_mute = wm8731_mute, | |
397 | .set_sysclk = wm8731_set_dai_sysclk, | |
398 | .set_fmt = wm8731_set_dai_fmt, | |
399 | }; | |
400 | ||
e550e17f | 401 | struct snd_soc_dai wm8731_dai = { |
40e0aa64 RP |
402 | .name = "WM8731", |
403 | .playback = { | |
404 | .stream_name = "Playback", | |
405 | .channels_min = 1, | |
406 | .channels_max = 2, | |
b36d61d4 FM |
407 | .rates = WM8731_RATES, |
408 | .formats = WM8731_FORMATS,}, | |
40e0aa64 RP |
409 | .capture = { |
410 | .stream_name = "Capture", | |
411 | .channels_min = 1, | |
412 | .channels_max = 2, | |
b36d61d4 FM |
413 | .rates = WM8731_RATES, |
414 | .formats = WM8731_FORMATS,}, | |
6335d055 | 415 | .ops = &wm8731_dai_ops, |
4934482d | 416 | .symmetric_rates = 1, |
40e0aa64 RP |
417 | }; |
418 | EXPORT_SYMBOL_GPL(wm8731_dai); | |
419 | ||
b3b50b3f | 420 | #ifdef CONFIG_PM |
40e0aa64 RP |
421 | static int wm8731_suspend(struct platform_device *pdev, pm_message_t state) |
422 | { | |
423 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
6627a653 | 424 | struct snd_soc_codec *codec = socdev->card->codec; |
40e0aa64 | 425 | |
17a52fd6 | 426 | snd_soc_write(codec, WM8731_ACTIVE, 0x0); |
0be9898a | 427 | wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF); |
40e0aa64 RP |
428 | return 0; |
429 | } | |
430 | ||
431 | static int wm8731_resume(struct platform_device *pdev) | |
432 | { | |
433 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
6627a653 | 434 | struct snd_soc_codec *codec = socdev->card->codec; |
40e0aa64 RP |
435 | int i; |
436 | u8 data[2]; | |
437 | u16 *cache = codec->reg_cache; | |
438 | ||
439 | /* Sync reg_cache with the hardware */ | |
440 | for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { | |
441 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); | |
442 | data[1] = cache[i] & 0x00ff; | |
443 | codec->hw_write(codec->control_data, data, 2); | |
444 | } | |
0be9898a MB |
445 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
446 | wm8731_set_bias_level(codec, codec->suspend_bias_level); | |
40e0aa64 RP |
447 | return 0; |
448 | } | |
b3b50b3f MB |
449 | #else |
450 | #define wm8731_suspend NULL | |
451 | #define wm8731_resume NULL | |
452 | #endif | |
40e0aa64 | 453 | |
5998102b | 454 | static int wm8731_probe(struct platform_device *pdev) |
40e0aa64 | 455 | { |
5998102b MB |
456 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
457 | struct snd_soc_codec *codec; | |
458 | int ret = 0; | |
40e0aa64 | 459 | |
5998102b MB |
460 | if (wm8731_codec == NULL) { |
461 | dev_err(&pdev->dev, "Codec device not registered\n"); | |
462 | return -ENODEV; | |
463 | } | |
40e0aa64 | 464 | |
5998102b MB |
465 | socdev->card->codec = wm8731_codec; |
466 | codec = wm8731_codec; | |
40e0aa64 RP |
467 | |
468 | /* register pcms */ | |
469 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | |
470 | if (ret < 0) { | |
5998102b | 471 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
e35115a5 | 472 | goto pcm_err; |
40e0aa64 RP |
473 | } |
474 | ||
3e8e1952 | 475 | snd_soc_add_controls(codec, wm8731_snd_controls, |
5998102b | 476 | ARRAY_SIZE(wm8731_snd_controls)); |
40e0aa64 | 477 | wm8731_add_widgets(codec); |
968a6025 | 478 | ret = snd_soc_init_card(socdev); |
40e0aa64 | 479 | if (ret < 0) { |
5998102b | 480 | dev_err(codec->dev, "failed to register card: %d\n", ret); |
e35115a5 | 481 | goto card_err; |
40e0aa64 RP |
482 | } |
483 | ||
484 | return ret; | |
e35115a5 LG |
485 | |
486 | card_err: | |
487 | snd_soc_free_pcms(socdev); | |
488 | snd_soc_dapm_free(socdev); | |
489 | pcm_err: | |
40e0aa64 RP |
490 | return ret; |
491 | } | |
492 | ||
493 | /* power down chip */ | |
494 | static int wm8731_remove(struct platform_device *pdev) | |
495 | { | |
496 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
40e0aa64 RP |
497 | |
498 | snd_soc_free_pcms(socdev); | |
499 | snd_soc_dapm_free(socdev); | |
40e0aa64 RP |
500 | |
501 | return 0; | |
502 | } | |
503 | ||
504 | struct snd_soc_codec_device soc_codec_dev_wm8731 = { | |
505 | .probe = wm8731_probe, | |
506 | .remove = wm8731_remove, | |
507 | .suspend = wm8731_suspend, | |
508 | .resume = wm8731_resume, | |
509 | }; | |
40e0aa64 RP |
510 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731); |
511 | ||
7084a42b MB |
512 | static int wm8731_register(struct wm8731_priv *wm8731, |
513 | enum snd_soc_control_type control) | |
a8035c8f | 514 | { |
a8035c8f | 515 | int ret; |
5998102b | 516 | struct snd_soc_codec *codec = &wm8731->codec; |
a8035c8f | 517 | |
5998102b MB |
518 | if (wm8731_codec) { |
519 | dev_err(codec->dev, "Another WM8731 is registered\n"); | |
fe5422fc MB |
520 | ret = -EINVAL; |
521 | goto err; | |
5998102b | 522 | } |
a8035c8f | 523 | |
5998102b MB |
524 | mutex_init(&codec->mutex); |
525 | INIT_LIST_HEAD(&codec->dapm_widgets); | |
526 | INIT_LIST_HEAD(&codec->dapm_paths); | |
a8035c8f | 527 | |
5998102b MB |
528 | codec->private_data = wm8731; |
529 | codec->name = "WM8731"; | |
530 | codec->owner = THIS_MODULE; | |
5998102b MB |
531 | codec->bias_level = SND_SOC_BIAS_OFF; |
532 | codec->set_bias_level = wm8731_set_bias_level; | |
533 | codec->dai = &wm8731_dai; | |
534 | codec->num_dai = 1; | |
535 | codec->reg_cache_size = WM8731_CACHEREGNUM; | |
536 | codec->reg_cache = &wm8731->reg_cache; | |
537 | ||
538 | memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg)); | |
539 | ||
7084a42b | 540 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); |
17a52fd6 MB |
541 | if (ret < 0) { |
542 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); | |
543 | goto err; | |
544 | } | |
545 | ||
519cf2df MB |
546 | ret = wm8731_reset(codec); |
547 | if (ret < 0) { | |
fe5422fc MB |
548 | dev_err(codec->dev, "Failed to issue reset: %d\n", ret); |
549 | goto err; | |
519cf2df MB |
550 | } |
551 | ||
5998102b MB |
552 | wm8731_dai.dev = codec->dev; |
553 | ||
5998102b MB |
554 | wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
555 | ||
556 | /* Latch the update bits */ | |
17a52fd6 MB |
557 | snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0); |
558 | snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0); | |
559 | snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0); | |
560 | snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0); | |
5998102b | 561 | |
ce3bdaa8 | 562 | /* Disable bypass path by default */ |
17a52fd6 | 563 | snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0); |
ce3bdaa8 | 564 | |
5998102b MB |
565 | wm8731_codec = codec; |
566 | ||
567 | ret = snd_soc_register_codec(codec); | |
568 | if (ret != 0) { | |
569 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); | |
fe5422fc | 570 | goto err; |
5998102b MB |
571 | } |
572 | ||
573 | ret = snd_soc_register_dai(&wm8731_dai); | |
574 | if (ret != 0) { | |
575 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); | |
576 | snd_soc_unregister_codec(codec); | |
fe5422fc | 577 | goto err_codec; |
5998102b | 578 | } |
a8035c8f | 579 | |
a8035c8f | 580 | return 0; |
fe5422fc MB |
581 | |
582 | err_codec: | |
583 | snd_soc_unregister_codec(codec); | |
584 | err: | |
585 | kfree(wm8731); | |
586 | return ret; | |
a8035c8f MB |
587 | } |
588 | ||
5998102b MB |
589 | static void wm8731_unregister(struct wm8731_priv *wm8731) |
590 | { | |
591 | wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF); | |
592 | snd_soc_unregister_dai(&wm8731_dai); | |
593 | snd_soc_unregister_codec(&wm8731->codec); | |
594 | kfree(wm8731); | |
595 | wm8731_codec = NULL; | |
596 | } | |
a8035c8f | 597 | |
5998102b | 598 | #if defined(CONFIG_SPI_MASTER) |
5998102b MB |
599 | static int __devinit wm8731_spi_probe(struct spi_device *spi) |
600 | { | |
601 | struct snd_soc_codec *codec; | |
602 | struct wm8731_priv *wm8731; | |
603 | ||
604 | wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); | |
605 | if (wm8731 == NULL) | |
606 | return -ENOMEM; | |
607 | ||
608 | codec = &wm8731->codec; | |
609 | codec->control_data = spi; | |
5998102b MB |
610 | codec->dev = &spi->dev; |
611 | ||
ae31c1fb | 612 | dev_set_drvdata(&spi->dev, wm8731); |
93b760b7 | 613 | |
7084a42b | 614 | return wm8731_register(wm8731, SND_SOC_SPI); |
5998102b MB |
615 | } |
616 | ||
617 | static int __devexit wm8731_spi_remove(struct spi_device *spi) | |
618 | { | |
ae31c1fb | 619 | struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev); |
93b760b7 MB |
620 | |
621 | wm8731_unregister(wm8731); | |
622 | ||
5998102b MB |
623 | return 0; |
624 | } | |
625 | ||
b3b50b3f MB |
626 | #ifdef CONFIG_PM |
627 | static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg) | |
628 | { | |
629 | return snd_soc_suspend_device(&spi->dev); | |
630 | } | |
631 | ||
632 | static int wm8731_spi_resume(struct spi_device *spi) | |
633 | { | |
634 | return snd_soc_resume_device(&spi->dev); | |
635 | } | |
636 | #else | |
637 | #define wm8731_spi_suspend NULL | |
638 | #define wm8731_spi_resume NULL | |
639 | #endif | |
640 | ||
5998102b MB |
641 | static struct spi_driver wm8731_spi_driver = { |
642 | .driver = { | |
643 | .name = "wm8731", | |
644 | .bus = &spi_bus_type, | |
645 | .owner = THIS_MODULE, | |
646 | }, | |
647 | .probe = wm8731_spi_probe, | |
b3b50b3f MB |
648 | .suspend = wm8731_spi_suspend, |
649 | .resume = wm8731_spi_resume, | |
5998102b MB |
650 | .remove = __devexit_p(wm8731_spi_remove), |
651 | }; | |
a8035c8f MB |
652 | #endif /* CONFIG_SPI_MASTER */ |
653 | ||
654 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
c6f29811 MB |
655 | static __devinit int wm8731_i2c_probe(struct i2c_client *i2c, |
656 | const struct i2c_device_id *id) | |
a8035c8f | 657 | { |
5998102b MB |
658 | struct wm8731_priv *wm8731; |
659 | struct snd_soc_codec *codec; | |
a8035c8f | 660 | |
5998102b MB |
661 | wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL); |
662 | if (wm8731 == NULL) | |
663 | return -ENOMEM; | |
664 | ||
665 | codec = &wm8731->codec; | |
5998102b MB |
666 | |
667 | i2c_set_clientdata(i2c, wm8731); | |
a8035c8f MB |
668 | codec->control_data = i2c; |
669 | ||
5998102b | 670 | codec->dev = &i2c->dev; |
a8035c8f | 671 | |
7084a42b | 672 | return wm8731_register(wm8731, SND_SOC_I2C); |
a8035c8f MB |
673 | } |
674 | ||
c6f29811 | 675 | static __devexit int wm8731_i2c_remove(struct i2c_client *client) |
a8035c8f | 676 | { |
5998102b MB |
677 | struct wm8731_priv *wm8731 = i2c_get_clientdata(client); |
678 | wm8731_unregister(wm8731); | |
a8035c8f MB |
679 | return 0; |
680 | } | |
681 | ||
b3b50b3f MB |
682 | #ifdef CONFIG_PM |
683 | static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg) | |
684 | { | |
685 | return snd_soc_suspend_device(&i2c->dev); | |
686 | } | |
687 | ||
688 | static int wm8731_i2c_resume(struct i2c_client *i2c) | |
689 | { | |
690 | return snd_soc_resume_device(&i2c->dev); | |
691 | } | |
692 | #else | |
693 | #define wm8731_i2c_suspend NULL | |
694 | #define wm8731_i2c_resume NULL | |
695 | #endif | |
696 | ||
a8035c8f MB |
697 | static const struct i2c_device_id wm8731_i2c_id[] = { |
698 | { "wm8731", 0 }, | |
699 | { } | |
700 | }; | |
701 | MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id); | |
702 | ||
703 | static struct i2c_driver wm8731_i2c_driver = { | |
704 | .driver = { | |
705 | .name = "WM8731 I2C Codec", | |
706 | .owner = THIS_MODULE, | |
707 | }, | |
708 | .probe = wm8731_i2c_probe, | |
c6f29811 | 709 | .remove = __devexit_p(wm8731_i2c_remove), |
b3b50b3f MB |
710 | .suspend = wm8731_i2c_suspend, |
711 | .resume = wm8731_i2c_resume, | |
a8035c8f MB |
712 | .id_table = wm8731_i2c_id, |
713 | }; | |
714 | #endif | |
715 | ||
c9b3a40f | 716 | static int __init wm8731_modinit(void) |
64089b84 | 717 | { |
5998102b MB |
718 | int ret; |
719 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) | |
720 | ret = i2c_add_driver(&wm8731_i2c_driver); | |
721 | if (ret != 0) { | |
722 | printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n", | |
723 | ret); | |
724 | } | |
725 | #endif | |
726 | #if defined(CONFIG_SPI_MASTER) | |
727 | ret = spi_register_driver(&wm8731_spi_driver); | |
728 | if (ret != 0) { | |
729 | printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n", | |
730 | ret); | |
731 | } | |
732 | #endif | |
733 | return 0; | |
64089b84 MB |
734 | } |
735 | module_init(wm8731_modinit); | |
736 | ||
737 | static void __exit wm8731_exit(void) | |
738 | { | |
5998102b MB |
739 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
740 | i2c_del_driver(&wm8731_i2c_driver); | |
741 | #endif | |
742 | #if defined(CONFIG_SPI_MASTER) | |
743 | spi_unregister_driver(&wm8731_spi_driver); | |
744 | #endif | |
64089b84 MB |
745 | } |
746 | module_exit(wm8731_exit); | |
747 | ||
40e0aa64 RP |
748 | MODULE_DESCRIPTION("ASoC WM8731 driver"); |
749 | MODULE_AUTHOR("Richard Purdie"); | |
750 | MODULE_LICENSE("GPL"); |