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