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