]>
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> | |
22 | #include <sound/driver.h> | |
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 | ||
32 | #define AUDIO_NAME "wm8731" | |
33 | #define WM8731_VERSION "0.12" | |
34 | ||
35 | /* | |
36 | * Debug | |
37 | */ | |
38 | ||
39 | #define WM8731_DEBUG 0 | |
40 | ||
41 | #ifdef WM8731_DEBUG | |
42 | #define dbg(format, arg...) \ | |
43 | printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg) | |
44 | #else | |
45 | #define dbg(format, arg...) do {} while (0) | |
46 | #endif | |
47 | #define err(format, arg...) \ | |
48 | printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg) | |
49 | #define info(format, arg...) \ | |
50 | printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg) | |
51 | #define warn(format, arg...) \ | |
52 | printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg) | |
53 | ||
54 | struct snd_soc_codec_device soc_codec_dev_wm8731; | |
55 | ||
56 | /* | |
57 | * wm8731 register cache | |
58 | * We can't read the WM8731 register space when we are | |
59 | * using 2 wire for device control, so we cache them instead. | |
60 | * There is no point in caching the reset register | |
61 | */ | |
62 | static const u16 wm8731_reg[WM8731_CACHEREGNUM] = { | |
63 | 0x0097, 0x0097, 0x0079, 0x0079, | |
64 | 0x000a, 0x0008, 0x009f, 0x000a, | |
65 | 0x0000, 0x0000 | |
66 | }; | |
67 | ||
68 | #define WM8731_DAIFMT \ | |
69 | (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_RIGHT_J | \ | |
70 | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_IB_NF | \ | |
71 | SND_SOC_DAIFMT_IB_IF) | |
72 | ||
73 | #define WM8731_DIR \ | |
74 | (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE) | |
75 | ||
76 | #define WM8731_RATES \ | |
77 | (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \ | |
78 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ | |
79 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) | |
80 | ||
81 | #define WM8731_HIFI_BITS \ | |
82 | (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ | |
83 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) | |
84 | ||
85 | static struct snd_soc_dai_mode wm8731_modes[] = { | |
86 | /* codec frame and clock master modes */ | |
87 | /* 8k */ | |
527541f9 LG |
88 | { |
89 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
90 | .pcmfmt = WM8731_HIFI_BITS, |
91 | .pcmrate = SNDRV_PCM_RATE_8000, | |
527541f9 | 92 | .pcmdir = WM8731_DIR, |
a71a468a | 93 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 94 | .fs = 1536, |
a71a468a | 95 | .bfs = 64, |
527541f9 LG |
96 | }, |
97 | { | |
98 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
99 | .pcmfmt = WM8731_HIFI_BITS, |
100 | .pcmrate = SNDRV_PCM_RATE_8000, | |
527541f9 | 101 | .pcmdir = WM8731_DIR, |
a71a468a | 102 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 103 | .fs = 2304, |
a71a468a | 104 | .bfs = 64, |
527541f9 LG |
105 | }, |
106 | { | |
107 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
108 | .pcmfmt = WM8731_HIFI_BITS, |
109 | .pcmrate = SNDRV_PCM_RATE_8000, | |
527541f9 | 110 | .pcmdir = WM8731_DIR, |
a71a468a | 111 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 112 | .fs = 1408, |
a71a468a | 113 | .bfs = 64, |
527541f9 LG |
114 | }, |
115 | { | |
116 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
117 | .pcmfmt = WM8731_HIFI_BITS, |
118 | .pcmrate = SNDRV_PCM_RATE_8000, | |
527541f9 | 119 | .pcmdir = WM8731_DIR, |
a71a468a | 120 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 121 | .fs = 2112, |
a71a468a | 122 | .bfs = 64, |
527541f9 | 123 | }, |
40e0aa64 RP |
124 | |
125 | /* 32k */ | |
527541f9 LG |
126 | { |
127 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb | 128 | .pcmfmt = WM8731_HIFI_BITS, |
527541f9 LG |
129 | .pcmrate = SNDRV_PCM_RATE_32000, |
130 | .pcmdir = WM8731_DIR, | |
a71a468a | 131 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 132 | .fs = 384, |
a71a468a | 133 | .bfs = 64, |
527541f9 LG |
134 | }, |
135 | { | |
136 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
137 | .pcmfmt = WM8731_HIFI_BITS, |
138 | .pcmrate = SNDRV_PCM_RATE_32000, | |
527541f9 | 139 | .pcmdir = WM8731_DIR, |
a71a468a | 140 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 141 | .fs = 576, |
a71a468a | 142 | .bfs = 64, |
527541f9 | 143 | }, |
40e0aa64 RP |
144 | |
145 | /* 44.1k & 48k */ | |
527541f9 LG |
146 | { |
147 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb | 148 | .pcmfmt = WM8731_HIFI_BITS, |
527541f9 LG |
149 | .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
150 | .pcmdir = WM8731_DIR, | |
a71a468a | 151 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 152 | .fs = 256, |
a71a468a | 153 | .bfs = 64, |
527541f9 LG |
154 | }, |
155 | { | |
156 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb | 157 | .pcmfmt = WM8731_HIFI_BITS, |
527541f9 LG |
158 | .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, |
159 | .pcmdir = WM8731_DIR, | |
a71a468a | 160 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 161 | .fs = 384, |
a71a468a | 162 | .bfs = 64, |
527541f9 | 163 | }, |
40e0aa64 RP |
164 | |
165 | /* 88.2 & 96k */ | |
527541f9 LG |
166 | { |
167 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb | 168 | .pcmfmt = WM8731_HIFI_BITS, |
527541f9 LG |
169 | .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000, |
170 | .pcmdir = WM8731_DIR, | |
a71a468a | 171 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 172 | .fs = 128, |
a71a468a | 173 | .bfs = 64, |
527541f9 LG |
174 | }, |
175 | { | |
176 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb | 177 | .pcmfmt = WM8731_HIFI_BITS, |
527541f9 LG |
178 | .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000, |
179 | .pcmdir = WM8731_DIR, | |
a71a468a | 180 | .flags = SND_SOC_DAI_BFS_RATE, |
b3b9c1cb | 181 | .fs = 192, |
a71a468a | 182 | .bfs = 64, |
527541f9 | 183 | }, |
40e0aa64 RP |
184 | |
185 | /* USB codec frame and clock master modes */ | |
186 | /* 8k */ | |
527541f9 LG |
187 | { |
188 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
189 | .pcmfmt = WM8731_HIFI_BITS, |
190 | .pcmrate = SNDRV_PCM_RATE_8000, | |
527541f9 | 191 | .pcmdir = WM8731_DIR, |
b3b9c1cb TI |
192 | .flags = SND_SOC_DAI_BFS_DIV, |
193 | .fs = 1500, | |
527541f9 LG |
194 | .bfs = SND_SOC_FSBD(1), |
195 | }, | |
40e0aa64 RP |
196 | |
197 | /* 44.1k */ | |
527541f9 LG |
198 | { |
199 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
200 | .pcmfmt = WM8731_HIFI_BITS, |
201 | .pcmrate = SNDRV_PCM_RATE_44100, | |
527541f9 LG |
202 | .pcmdir = WM8731_DIR, |
203 | .flags = SND_SOC_DAI_BFS_DIV, | |
b3b9c1cb | 204 | .fs = 272, |
527541f9 LG |
205 | .bfs = SND_SOC_FSBD(1), |
206 | }, | |
40e0aa64 RP |
207 | |
208 | /* 48k */ | |
527541f9 LG |
209 | { |
210 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
211 | .pcmfmt = WM8731_HIFI_BITS, |
212 | .pcmrate = SNDRV_PCM_RATE_48000, | |
527541f9 | 213 | .pcmdir = WM8731_DIR, |
b3b9c1cb TI |
214 | .flags = SND_SOC_DAI_BFS_DIV, |
215 | .fs = 250, | |
527541f9 LG |
216 | .bfs = SND_SOC_FSBD(1), |
217 | }, | |
40e0aa64 RP |
218 | |
219 | /* 88.2k */ | |
527541f9 | 220 | { |
b3b9c1cb TI |
221 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, |
222 | .pcmfmt = WM8731_HIFI_BITS, | |
223 | .pcmrate = SNDRV_PCM_RATE_88200, | |
527541f9 LG |
224 | .pcmdir = WM8731_DIR, |
225 | .flags = SND_SOC_DAI_BFS_DIV, | |
b3b9c1cb | 226 | .fs = 136, |
527541f9 LG |
227 | .bfs = SND_SOC_FSBD(1), |
228 | }, | |
40e0aa64 RP |
229 | |
230 | /* 96k */ | |
527541f9 LG |
231 | { |
232 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM, | |
b3b9c1cb TI |
233 | .pcmfmt = WM8731_HIFI_BITS, |
234 | .pcmrate = SNDRV_PCM_RATE_96000, | |
527541f9 | 235 | .pcmdir = WM8731_DIR, |
b3b9c1cb TI |
236 | .flags = SND_SOC_DAI_BFS_DIV, |
237 | .fs = 125, | |
527541f9 LG |
238 | .bfs = SND_SOC_FSBD(1), |
239 | }, | |
40e0aa64 RP |
240 | |
241 | /* codec frame and clock slave modes */ | |
527541f9 LG |
242 | { |
243 | .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBS_CFS, | |
b3b9c1cb TI |
244 | .pcmfmt = WM8731_HIFI_BITS, |
245 | .pcmrate = WM8731_RATES, | |
527541f9 LG |
246 | .pcmdir = WM8731_DIR, |
247 | .flags = SND_SOC_DAI_BFS_DIV, | |
b3b9c1cb | 248 | .fs = SND_SOC_FS_ALL, |
a71a468a | 249 | .bfs = SND_SOC_FSB_ALL, |
527541f9 | 250 | }, |
40e0aa64 RP |
251 | }; |
252 | ||
253 | /* | |
254 | * read wm8731 register cache | |
255 | */ | |
256 | static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec, | |
257 | unsigned int reg) | |
258 | { | |
259 | u16 *cache = codec->reg_cache; | |
260 | if (reg == WM8731_RESET) | |
261 | return 0; | |
262 | if (reg >= WM8731_CACHEREGNUM) | |
263 | return -1; | |
264 | return cache[reg]; | |
265 | } | |
266 | ||
267 | /* | |
268 | * write wm8731 register cache | |
269 | */ | |
270 | static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec, | |
271 | u16 reg, unsigned int value) | |
272 | { | |
273 | u16 *cache = codec->reg_cache; | |
274 | if (reg >= WM8731_CACHEREGNUM) | |
275 | return; | |
276 | cache[reg] = value; | |
277 | } | |
278 | ||
279 | /* | |
280 | * write to the WM8731 register space | |
281 | */ | |
282 | static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg, | |
283 | unsigned int value) | |
284 | { | |
285 | u8 data[2]; | |
286 | ||
287 | /* data is | |
288 | * D15..D9 WM8731 register offset | |
289 | * D8...D0 register data | |
290 | */ | |
291 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); | |
292 | data[1] = value & 0x00ff; | |
293 | ||
294 | wm8731_write_reg_cache (codec, reg, value); | |
295 | if (codec->hw_write(codec->control_data, data, 2) == 2) | |
296 | return 0; | |
297 | else | |
298 | return -EIO; | |
299 | } | |
300 | ||
301 | #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0) | |
302 | ||
303 | static const char *wm8731_input_select[] = {"Line In", "Mic"}; | |
304 | static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | |
305 | ||
306 | static const struct soc_enum wm8731_enum[] = { | |
307 | SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select), | |
308 | SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph), | |
309 | }; | |
310 | ||
311 | static const struct snd_kcontrol_new wm8731_snd_controls[] = { | |
312 | ||
bd903b6e LG |
313 | SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, |
314 | 0, 127, 0), | |
315 | SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, | |
316 | 7, 1, 0), | |
40e0aa64 RP |
317 | |
318 | SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0), | |
319 | SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1), | |
320 | ||
321 | SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0), | |
322 | SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1), | |
323 | ||
324 | SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1), | |
325 | ||
326 | SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1), | |
327 | SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0), | |
328 | ||
329 | SOC_ENUM("Playback De-emphasis", wm8731_enum[1]), | |
330 | }; | |
331 | ||
332 | /* add non dapm controls */ | |
333 | static int wm8731_add_controls(struct snd_soc_codec *codec) | |
334 | { | |
335 | int err, i; | |
336 | ||
337 | for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) { | |
338 | if ((err = snd_ctl_add(codec->card, | |
339 | snd_soc_cnew(&wm8731_snd_controls[i],codec, NULL))) < 0) | |
340 | return err; | |
341 | } | |
342 | ||
343 | return 0; | |
344 | } | |
345 | ||
346 | /* Output Mixer */ | |
347 | static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = { | |
348 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0), | |
349 | SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0), | |
350 | SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0), | |
351 | }; | |
352 | ||
353 | /* Input mux */ | |
354 | static const struct snd_kcontrol_new wm8731_input_mux_controls = | |
355 | SOC_DAPM_ENUM("Input Select", wm8731_enum[0]); | |
356 | ||
357 | static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = { | |
358 | SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1, | |
359 | &wm8731_output_mixer_controls[0], | |
360 | ARRAY_SIZE(wm8731_output_mixer_controls)), | |
361 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1), | |
362 | SND_SOC_DAPM_OUTPUT("LOUT"), | |
363 | SND_SOC_DAPM_OUTPUT("LHPOUT"), | |
364 | SND_SOC_DAPM_OUTPUT("ROUT"), | |
365 | SND_SOC_DAPM_OUTPUT("RHPOUT"), | |
366 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1), | |
367 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls), | |
368 | SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0), | |
369 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1), | |
370 | SND_SOC_DAPM_INPUT("MICIN"), | |
371 | SND_SOC_DAPM_INPUT("RLINEIN"), | |
372 | SND_SOC_DAPM_INPUT("LLINEIN"), | |
373 | }; | |
374 | ||
375 | static const char *intercon[][3] = { | |
376 | /* output mixer */ | |
377 | {"Output Mixer", "Line Bypass Switch", "Line Input"}, | |
378 | {"Output Mixer", "HiFi Playback Switch", "DAC"}, | |
379 | {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"}, | |
380 | ||
381 | /* outputs */ | |
382 | {"RHPOUT", NULL, "Output Mixer"}, | |
383 | {"ROUT", NULL, "Output Mixer"}, | |
384 | {"LHPOUT", NULL, "Output Mixer"}, | |
385 | {"LOUT", NULL, "Output Mixer"}, | |
386 | ||
387 | /* input mux */ | |
388 | {"Input Mux", "Line In", "Line Input"}, | |
389 | {"Input Mux", "Mic", "Mic Bias"}, | |
390 | {"ADC", NULL, "Input Mux"}, | |
391 | ||
392 | /* inputs */ | |
393 | {"Line Input", NULL, "LLINEIN"}, | |
394 | {"Line Input", NULL, "RLINEIN"}, | |
395 | {"Mic Bias", NULL, "MICIN"}, | |
396 | ||
397 | /* terminator */ | |
398 | {NULL, NULL, NULL}, | |
399 | }; | |
400 | ||
401 | static int wm8731_add_widgets(struct snd_soc_codec *codec) | |
402 | { | |
403 | int i; | |
404 | ||
405 | for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) { | |
406 | snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]); | |
407 | } | |
408 | ||
409 | /* set up audio path interconnects */ | |
410 | for(i = 0; intercon[i][0] != NULL; i++) { | |
411 | snd_soc_dapm_connect_input(codec, intercon[i][0], | |
412 | intercon[i][1], intercon[i][2]); | |
413 | } | |
414 | ||
415 | snd_soc_dapm_new_widgets(codec); | |
416 | return 0; | |
417 | } | |
418 | ||
419 | struct _coeff_div { | |
420 | u32 mclk; | |
421 | u32 rate; | |
422 | u16 fs; | |
423 | u8 sr:4; | |
424 | u8 bosr:1; | |
425 | u8 usb:1; | |
426 | }; | |
427 | ||
428 | /* codec mclk clock divider coefficients */ | |
429 | static const struct _coeff_div coeff_div[] = { | |
430 | /* 48k */ | |
431 | {12288000, 48000, 256, 0x0, 0x0, 0x0}, | |
432 | {18432000, 48000, 384, 0x0, 0x1, 0x0}, | |
433 | {12000000, 48000, 250, 0x0, 0x0, 0x1}, | |
434 | ||
435 | /* 32k */ | |
436 | {12288000, 32000, 384, 0x6, 0x0, 0x0}, | |
437 | {18432000, 32000, 576, 0x6, 0x1, 0x0}, | |
298a2c75 | 438 | {12000000, 32000, 375, 0x6, 0x0, 0x1}, |
40e0aa64 RP |
439 | |
440 | /* 8k */ | |
441 | {12288000, 8000, 1536, 0x3, 0x0, 0x0}, | |
442 | {18432000, 8000, 2304, 0x3, 0x1, 0x0}, | |
443 | {11289600, 8000, 1408, 0xb, 0x0, 0x0}, | |
444 | {16934400, 8000, 2112, 0xb, 0x1, 0x0}, | |
445 | {12000000, 8000, 1500, 0x3, 0x0, 0x1}, | |
446 | ||
447 | /* 96k */ | |
448 | {12288000, 96000, 128, 0x7, 0x0, 0x0}, | |
449 | {18432000, 96000, 192, 0x7, 0x1, 0x0}, | |
450 | {12000000, 96000, 125, 0x7, 0x0, 0x1}, | |
451 | ||
452 | /* 44.1k */ | |
453 | {11289600, 44100, 256, 0x8, 0x0, 0x0}, | |
454 | {16934400, 44100, 384, 0x8, 0x1, 0x0}, | |
455 | {12000000, 44100, 272, 0x8, 0x1, 0x1}, | |
456 | ||
457 | /* 88.2k */ | |
458 | {11289600, 88200, 128, 0xf, 0x0, 0x0}, | |
459 | {16934400, 88200, 192, 0xf, 0x1, 0x0}, | |
460 | {12000000, 88200, 136, 0xf, 0x1, 0x1}, | |
461 | }; | |
462 | ||
463 | static inline int get_coeff(int mclk, int rate) | |
464 | { | |
465 | int i; | |
466 | ||
467 | for (i = 0; i < ARRAY_SIZE(coeff_div); i++) { | |
468 | if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk) | |
469 | return i; | |
470 | } | |
471 | return 0; | |
472 | } | |
473 | ||
474 | /* WM8731 supports numerous clocks per sample rate */ | |
475 | static unsigned int wm8731_config_sysclk(struct snd_soc_codec_dai *dai, | |
476 | struct snd_soc_clock_info *info, unsigned int clk) | |
477 | { | |
478 | dai->mclk = 0; | |
479 | ||
480 | /* check that the calculated FS and rate actually match a clock from | |
481 | * the machine driver */ | |
482 | if (info->fs * info->rate == clk) | |
483 | dai->mclk = clk; | |
484 | ||
485 | return dai->mclk; | |
486 | } | |
487 | ||
488 | static int wm8731_pcm_prepare(struct snd_pcm_substream *substream) | |
489 | { | |
490 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
491 | struct snd_soc_device *socdev = rtd->socdev; | |
492 | struct snd_soc_codec *codec = socdev->codec; | |
493 | u16 iface = 0, srate; | |
494 | int i = get_coeff(rtd->codec_dai->mclk, | |
495 | snd_soc_get_rate(rtd->codec_dai->dai_runtime.pcmrate)); | |
496 | ||
497 | /* set master/slave audio interface */ | |
498 | switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) { | |
499 | case SND_SOC_DAIFMT_CBM_CFM: | |
500 | iface |= 0x0040; | |
501 | break; | |
502 | case SND_SOC_DAIFMT_CBS_CFS: | |
503 | break; | |
504 | } | |
505 | srate = (coeff_div[i].sr << 2) | | |
506 | (coeff_div[i].bosr << 1) | coeff_div[i].usb; | |
507 | wm8731_write(codec, WM8731_SRATE, srate); | |
508 | ||
509 | /* interface format */ | |
510 | switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
511 | case SND_SOC_DAIFMT_I2S: | |
512 | iface |= 0x0002; | |
513 | break; | |
514 | case SND_SOC_DAIFMT_RIGHT_J: | |
515 | break; | |
516 | case SND_SOC_DAIFMT_LEFT_J: | |
517 | iface |= 0x0001; | |
518 | break; | |
519 | case SND_SOC_DAIFMT_DSP_A: | |
520 | iface |= 0x0003; | |
521 | break; | |
522 | case SND_SOC_DAIFMT_DSP_B: | |
523 | iface |= 0x0013; | |
524 | break; | |
525 | } | |
526 | ||
527 | /* bit size */ | |
528 | switch (rtd->codec_dai->dai_runtime.pcmfmt) { | |
529 | case SNDRV_PCM_FMTBIT_S16_LE: | |
530 | break; | |
531 | case SNDRV_PCM_FMTBIT_S20_3LE: | |
532 | iface |= 0x0004; | |
533 | break; | |
534 | case SNDRV_PCM_FMTBIT_S24_LE: | |
535 | iface |= 0x0008; | |
536 | break; | |
537 | case SNDRV_PCM_FMTBIT_S32_LE: | |
538 | iface |= 0x000c; | |
539 | break; | |
540 | } | |
541 | ||
542 | /* clock inversion */ | |
543 | switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK) { | |
544 | case SND_SOC_DAIFMT_NB_NF: | |
545 | break; | |
546 | case SND_SOC_DAIFMT_IB_IF: | |
547 | iface |= 0x0090; | |
548 | break; | |
549 | case SND_SOC_DAIFMT_IB_NF: | |
550 | iface |= 0x0080; | |
551 | break; | |
552 | case SND_SOC_DAIFMT_NB_IF: | |
553 | iface |= 0x0010; | |
554 | break; | |
555 | } | |
556 | ||
557 | /* set iface */ | |
558 | wm8731_write(codec, WM8731_IFACE, iface); | |
559 | ||
560 | /* set active */ | |
561 | wm8731_write(codec, WM8731_ACTIVE, 0x0001); | |
562 | return 0; | |
563 | } | |
564 | ||
565 | static void wm8731_shutdown(struct snd_pcm_substream *substream) | |
566 | { | |
567 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
568 | struct snd_soc_device *socdev = rtd->socdev; | |
569 | struct snd_soc_codec *codec = socdev->codec; | |
570 | ||
571 | /* deactivate */ | |
572 | if (!codec->active) { | |
573 | udelay(50); | |
574 | wm8731_write(codec, WM8731_ACTIVE, 0x0); | |
575 | } | |
576 | } | |
577 | ||
578 | static int wm8731_mute(struct snd_soc_codec *codec, | |
579 | struct snd_soc_codec_dai *dai, int mute) | |
580 | { | |
581 | u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7; | |
582 | if (mute) | |
583 | wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8); | |
584 | else | |
585 | wm8731_write(codec, WM8731_APDIGI, mute_reg); | |
586 | return 0; | |
587 | } | |
588 | ||
589 | static int wm8731_dapm_event(struct snd_soc_codec *codec, int event) | |
590 | { | |
591 | u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f; | |
592 | ||
593 | switch (event) { | |
594 | case SNDRV_CTL_POWER_D0: /* full On */ | |
595 | /* vref/mid, osc on, dac unmute */ | |
596 | wm8731_write(codec, WM8731_PWR, reg); | |
597 | break; | |
598 | case SNDRV_CTL_POWER_D1: /* partial On */ | |
599 | case SNDRV_CTL_POWER_D2: /* partial On */ | |
600 | break; | |
601 | case SNDRV_CTL_POWER_D3hot: /* Off, with power */ | |
602 | /* everything off except vref/vmid, */ | |
603 | wm8731_write(codec, WM8731_PWR, reg | 0x0040); | |
604 | break; | |
605 | case SNDRV_CTL_POWER_D3cold: /* Off, without power */ | |
606 | /* everything off, dac mute, inactive */ | |
607 | wm8731_write(codec, WM8731_ACTIVE, 0x0); | |
608 | wm8731_write(codec, WM8731_PWR, 0xffff); | |
609 | break; | |
610 | } | |
611 | codec->dapm_state = event; | |
612 | return 0; | |
613 | } | |
614 | ||
615 | struct snd_soc_codec_dai wm8731_dai = { | |
616 | .name = "WM8731", | |
617 | .playback = { | |
618 | .stream_name = "Playback", | |
619 | .channels_min = 1, | |
620 | .channels_max = 2, | |
621 | }, | |
622 | .capture = { | |
623 | .stream_name = "Capture", | |
624 | .channels_min = 1, | |
625 | .channels_max = 2, | |
626 | }, | |
627 | .config_sysclk = wm8731_config_sysclk, | |
628 | .digital_mute = wm8731_mute, | |
629 | .ops = { | |
630 | .prepare = wm8731_pcm_prepare, | |
631 | .shutdown = wm8731_shutdown, | |
632 | }, | |
633 | .caps = { | |
634 | .num_modes = ARRAY_SIZE(wm8731_modes), | |
635 | .mode = wm8731_modes, | |
636 | }, | |
637 | }; | |
638 | EXPORT_SYMBOL_GPL(wm8731_dai); | |
639 | ||
640 | static int wm8731_suspend(struct platform_device *pdev, pm_message_t state) | |
641 | { | |
642 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
643 | struct snd_soc_codec *codec = socdev->codec; | |
644 | ||
645 | wm8731_write(codec, WM8731_ACTIVE, 0x0); | |
646 | wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold); | |
647 | return 0; | |
648 | } | |
649 | ||
650 | static int wm8731_resume(struct platform_device *pdev) | |
651 | { | |
652 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
653 | struct snd_soc_codec *codec = socdev->codec; | |
654 | int i; | |
655 | u8 data[2]; | |
656 | u16 *cache = codec->reg_cache; | |
657 | ||
658 | /* Sync reg_cache with the hardware */ | |
659 | for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) { | |
660 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); | |
661 | data[1] = cache[i] & 0x00ff; | |
662 | codec->hw_write(codec->control_data, data, 2); | |
663 | } | |
664 | wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot); | |
665 | wm8731_dapm_event(codec, codec->suspend_dapm_state); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | /* | |
670 | * initialise the WM8731 driver | |
671 | * register the mixer and dsp interfaces with the kernel | |
672 | */ | |
673 | static int wm8731_init(struct snd_soc_device *socdev) | |
674 | { | |
675 | struct snd_soc_codec *codec = socdev->codec; | |
676 | int reg, ret = 0; | |
677 | ||
678 | codec->name = "WM8731"; | |
679 | codec->owner = THIS_MODULE; | |
680 | codec->read = wm8731_read_reg_cache; | |
681 | codec->write = wm8731_write; | |
682 | codec->dapm_event = wm8731_dapm_event; | |
683 | codec->dai = &wm8731_dai; | |
684 | codec->num_dai = 1; | |
685 | codec->reg_cache_size = ARRAY_SIZE(wm8731_reg); | |
686 | ||
687 | codec->reg_cache = | |
688 | kzalloc(sizeof(u16) * ARRAY_SIZE(wm8731_reg), GFP_KERNEL); | |
689 | if (codec->reg_cache == NULL) | |
690 | return -ENOMEM; | |
691 | memcpy(codec->reg_cache, | |
692 | wm8731_reg, sizeof(u16) * ARRAY_SIZE(wm8731_reg)); | |
693 | codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8731_reg); | |
694 | ||
695 | wm8731_reset(codec); | |
696 | ||
697 | /* register pcms */ | |
698 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | |
699 | if (ret < 0) { | |
e35115a5 LG |
700 | printk(KERN_ERR "wm8731: failed to create pcms\n"); |
701 | goto pcm_err; | |
40e0aa64 RP |
702 | } |
703 | ||
704 | /* power on device */ | |
705 | wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot); | |
706 | ||
707 | /* set the update bits */ | |
708 | reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V); | |
709 | wm8731_write(codec, WM8731_LOUT1V, reg | 0x0100); | |
710 | reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V); | |
711 | wm8731_write(codec, WM8731_ROUT1V, reg | 0x0100); | |
712 | reg = wm8731_read_reg_cache(codec, WM8731_LINVOL); | |
713 | wm8731_write(codec, WM8731_LINVOL, reg | 0x0100); | |
714 | reg = wm8731_read_reg_cache(codec, WM8731_RINVOL); | |
715 | wm8731_write(codec, WM8731_RINVOL, reg | 0x0100); | |
716 | ||
717 | wm8731_add_controls(codec); | |
718 | wm8731_add_widgets(codec); | |
719 | ret = snd_soc_register_card(socdev); | |
720 | if (ret < 0) { | |
e35115a5 LG |
721 | printk(KERN_ERR "wm8731: failed to register card\n"); |
722 | goto card_err; | |
40e0aa64 RP |
723 | } |
724 | ||
725 | return ret; | |
e35115a5 LG |
726 | |
727 | card_err: | |
728 | snd_soc_free_pcms(socdev); | |
729 | snd_soc_dapm_free(socdev); | |
730 | pcm_err: | |
731 | kfree(codec->reg_cache); | |
732 | return ret; | |
40e0aa64 RP |
733 | } |
734 | ||
735 | static struct snd_soc_device *wm8731_socdev; | |
736 | ||
737 | #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE) | |
738 | ||
739 | /* | |
740 | * WM8731 2 wire address is determined by GPIO5 | |
741 | * state during powerup. | |
742 | * low = 0x1a | |
743 | * high = 0x1b | |
744 | */ | |
745 | static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END }; | |
746 | ||
747 | /* Magic definition of all other variables and things */ | |
748 | I2C_CLIENT_INSMOD; | |
749 | ||
750 | static struct i2c_driver wm8731_i2c_driver; | |
751 | static struct i2c_client client_template; | |
752 | ||
753 | /* If the i2c layer weren't so broken, we could pass this kind of data | |
754 | around */ | |
755 | ||
756 | static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind) | |
757 | { | |
758 | struct snd_soc_device *socdev = wm8731_socdev; | |
759 | struct wm8731_setup_data *setup = socdev->codec_data; | |
760 | struct snd_soc_codec *codec = socdev->codec; | |
761 | struct i2c_client *i2c; | |
762 | int ret; | |
763 | ||
764 | if (addr != setup->i2c_address) | |
765 | return -ENODEV; | |
766 | ||
767 | client_template.adapter = adap; | |
768 | client_template.addr = addr; | |
769 | ||
770 | i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); | |
771 | if (i2c == NULL) { | |
772 | kfree(codec); | |
773 | return -ENOMEM; | |
774 | } | |
775 | memcpy(i2c, &client_template, sizeof(struct i2c_client)); | |
776 | i2c_set_clientdata(i2c, codec); | |
777 | codec->control_data = i2c; | |
778 | ||
779 | ret = i2c_attach_client(i2c); | |
780 | if (ret < 0) { | |
781 | err("failed to attach codec at addr %x\n", addr); | |
782 | goto err; | |
783 | } | |
784 | ||
785 | ret = wm8731_init(socdev); | |
786 | if (ret < 0) { | |
787 | err("failed to initialise WM8731\n"); | |
788 | goto err; | |
789 | } | |
790 | return ret; | |
791 | ||
792 | err: | |
793 | kfree(codec); | |
794 | kfree(i2c); | |
795 | return ret; | |
796 | } | |
797 | ||
798 | static int wm8731_i2c_detach(struct i2c_client *client) | |
799 | { | |
800 | struct snd_soc_codec* codec = i2c_get_clientdata(client); | |
801 | i2c_detach_client(client); | |
802 | kfree(codec->reg_cache); | |
803 | kfree(client); | |
804 | return 0; | |
805 | } | |
806 | ||
807 | static int wm8731_i2c_attach(struct i2c_adapter *adap) | |
808 | { | |
809 | return i2c_probe(adap, &addr_data, wm8731_codec_probe); | |
810 | } | |
811 | ||
812 | /* corgi i2c codec control layer */ | |
813 | static struct i2c_driver wm8731_i2c_driver = { | |
814 | .driver = { | |
815 | .name = "WM8731 I2C Codec", | |
816 | .owner = THIS_MODULE, | |
817 | }, | |
818 | .id = I2C_DRIVERID_WM8731, | |
819 | .attach_adapter = wm8731_i2c_attach, | |
820 | .detach_client = wm8731_i2c_detach, | |
821 | .command = NULL, | |
822 | }; | |
823 | ||
824 | static struct i2c_client client_template = { | |
825 | .name = "WM8731", | |
826 | .driver = &wm8731_i2c_driver, | |
827 | }; | |
828 | #endif | |
829 | ||
830 | static int wm8731_probe(struct platform_device *pdev) | |
831 | { | |
832 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
833 | struct wm8731_setup_data *setup; | |
834 | struct snd_soc_codec *codec; | |
835 | int ret = 0; | |
836 | ||
837 | info("WM8731 Audio Codec %s", WM8731_VERSION); | |
838 | ||
839 | setup = socdev->codec_data; | |
840 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); | |
841 | if (codec == NULL) | |
842 | return -ENOMEM; | |
843 | ||
844 | socdev->codec = codec; | |
845 | mutex_init(&codec->mutex); | |
846 | INIT_LIST_HEAD(&codec->dapm_widgets); | |
847 | INIT_LIST_HEAD(&codec->dapm_paths); | |
848 | ||
849 | wm8731_socdev = socdev; | |
850 | #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE) | |
851 | if (setup->i2c_address) { | |
852 | normal_i2c[0] = setup->i2c_address; | |
853 | codec->hw_write = (hw_write_t)i2c_master_send; | |
854 | ret = i2c_add_driver(&wm8731_i2c_driver); | |
855 | if (ret != 0) | |
856 | printk(KERN_ERR "can't add i2c driver"); | |
857 | } | |
858 | #else | |
859 | /* Add other interfaces here */ | |
860 | #endif | |
861 | return ret; | |
862 | } | |
863 | ||
864 | /* power down chip */ | |
865 | static int wm8731_remove(struct platform_device *pdev) | |
866 | { | |
867 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
868 | struct snd_soc_codec *codec = socdev->codec; | |
869 | ||
870 | if (codec->control_data) | |
871 | wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold); | |
872 | ||
873 | snd_soc_free_pcms(socdev); | |
874 | snd_soc_dapm_free(socdev); | |
875 | #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE) | |
876 | i2c_del_driver(&wm8731_i2c_driver); | |
877 | #endif | |
878 | kfree(codec); | |
879 | ||
880 | return 0; | |
881 | } | |
882 | ||
883 | struct snd_soc_codec_device soc_codec_dev_wm8731 = { | |
884 | .probe = wm8731_probe, | |
885 | .remove = wm8731_remove, | |
886 | .suspend = wm8731_suspend, | |
887 | .resume = wm8731_resume, | |
888 | }; | |
889 | ||
890 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731); | |
891 | ||
892 | MODULE_DESCRIPTION("ASoC WM8731 driver"); | |
893 | MODULE_AUTHOR("Richard Purdie"); | |
894 | MODULE_LICENSE("GPL"); |