Commit | Line | Data |
---|---|---|
f2644a2c MB |
1 | /* |
2 | * wm8960.c -- WM8960 ALSA SoC Audio driver | |
3 | * | |
4 | * Author: Liam Girdwood | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/moduleparam.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/delay.h> | |
15 | #include <linux/pm.h> | |
16 | #include <linux/i2c.h> | |
17 | #include <linux/platform_device.h> | |
18 | #include <sound/core.h> | |
19 | #include <sound/pcm.h> | |
20 | #include <sound/pcm_params.h> | |
21 | #include <sound/soc.h> | |
22 | #include <sound/soc-dapm.h> | |
23 | #include <sound/initval.h> | |
24 | #include <sound/tlv.h> | |
b6877a47 | 25 | #include <sound/wm8960.h> |
f2644a2c MB |
26 | |
27 | #include "wm8960.h" | |
28 | ||
29 | #define AUDIO_NAME "wm8960" | |
30 | ||
31 | struct snd_soc_codec_device soc_codec_dev_wm8960; | |
32 | ||
33 | /* R25 - Power 1 */ | |
913d7b4c | 34 | #define WM8960_VMID_MASK 0x180 |
f2644a2c MB |
35 | #define WM8960_VREF 0x40 |
36 | ||
913d7b4c MB |
37 | /* R26 - Power 2 */ |
38 | #define WM8960_PWR2_LOUT1 0x40 | |
39 | #define WM8960_PWR2_ROUT1 0x20 | |
40 | #define WM8960_PWR2_OUT3 0x02 | |
41 | ||
f2644a2c MB |
42 | /* R28 - Anti-pop 1 */ |
43 | #define WM8960_POBCTRL 0x80 | |
44 | #define WM8960_BUFDCOPEN 0x10 | |
45 | #define WM8960_BUFIOEN 0x08 | |
46 | #define WM8960_SOFT_ST 0x04 | |
47 | #define WM8960_HPSTBY 0x01 | |
48 | ||
49 | /* R29 - Anti-pop 2 */ | |
50 | #define WM8960_DISOP 0x40 | |
913d7b4c | 51 | #define WM8960_DRES_MASK 0x30 |
f2644a2c MB |
52 | |
53 | /* | |
54 | * wm8960 register cache | |
55 | * We can't read the WM8960 register space when we are | |
56 | * using 2 wire for device control, so we cache them instead. | |
57 | */ | |
58 | static const u16 wm8960_reg[WM8960_CACHEREGNUM] = { | |
59 | 0x0097, 0x0097, 0x0000, 0x0000, | |
60 | 0x0000, 0x0008, 0x0000, 0x000a, | |
61 | 0x01c0, 0x0000, 0x00ff, 0x00ff, | |
62 | 0x0000, 0x0000, 0x0000, 0x0000, | |
63 | 0x0000, 0x007b, 0x0100, 0x0032, | |
64 | 0x0000, 0x00c3, 0x00c3, 0x01c0, | |
65 | 0x0000, 0x0000, 0x0000, 0x0000, | |
66 | 0x0000, 0x0000, 0x0000, 0x0000, | |
67 | 0x0100, 0x0100, 0x0050, 0x0050, | |
68 | 0x0050, 0x0050, 0x0000, 0x0000, | |
69 | 0x0000, 0x0000, 0x0040, 0x0000, | |
70 | 0x0000, 0x0050, 0x0050, 0x0000, | |
71 | 0x0002, 0x0037, 0x004d, 0x0080, | |
72 | 0x0008, 0x0031, 0x0026, 0x00e9, | |
73 | }; | |
74 | ||
75 | struct wm8960_priv { | |
76 | u16 reg_cache[WM8960_CACHEREGNUM]; | |
77 | struct snd_soc_codec codec; | |
913d7b4c MB |
78 | struct snd_soc_dapm_widget *lout1; |
79 | struct snd_soc_dapm_widget *rout1; | |
80 | struct snd_soc_dapm_widget *out3; | |
f2644a2c MB |
81 | }; |
82 | ||
17a52fd6 | 83 | #define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0) |
f2644a2c MB |
84 | |
85 | /* enumerated controls */ | |
86 | static const char *wm8960_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; | |
87 | static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted", | |
88 | "Right Inverted", "Stereo Inversion"}; | |
89 | static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"}; | |
90 | static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"}; | |
91 | static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"}; | |
92 | static const char *wm8960_alcmode[] = {"ALC", "Limiter"}; | |
93 | ||
94 | static const struct soc_enum wm8960_enum[] = { | |
95 | SOC_ENUM_SINGLE(WM8960_DACCTL1, 1, 4, wm8960_deemph), | |
96 | SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity), | |
97 | SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity), | |
98 | SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff), | |
99 | SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff), | |
100 | SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc), | |
101 | SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode), | |
102 | }; | |
103 | ||
104 | static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0); | |
105 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1); | |
106 | static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0); | |
107 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); | |
108 | ||
109 | static const struct snd_kcontrol_new wm8960_snd_controls[] = { | |
110 | SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL, | |
111 | 0, 63, 0, adc_tlv), | |
112 | SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL, | |
113 | 6, 1, 0), | |
114 | SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL, | |
115 | 7, 1, 0), | |
116 | ||
117 | SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC, | |
118 | 0, 255, 0, dac_tlv), | |
119 | ||
120 | SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1, | |
121 | 0, 127, 0, out_tlv), | |
122 | SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1, | |
123 | 7, 1, 0), | |
124 | ||
125 | SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2, | |
126 | 0, 127, 0, out_tlv), | |
127 | SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2, | |
128 | 7, 1, 0), | |
129 | SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0), | |
130 | SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0), | |
131 | ||
132 | SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0), | |
133 | SOC_ENUM("ADC Polarity", wm8960_enum[1]), | |
134 | SOC_ENUM("Playback De-emphasis", wm8960_enum[0]), | |
135 | SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0), | |
136 | ||
137 | SOC_ENUM("DAC Polarity", wm8960_enum[2]), | |
138 | ||
139 | SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[3]), | |
140 | SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[4]), | |
141 | SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0), | |
142 | SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0), | |
143 | ||
144 | SOC_ENUM("ALC Function", wm8960_enum[5]), | |
145 | SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0), | |
146 | SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1), | |
147 | SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0), | |
148 | SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0), | |
149 | SOC_ENUM("ALC Mode", wm8960_enum[6]), | |
150 | SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0), | |
151 | SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0), | |
152 | ||
153 | SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0), | |
154 | SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0), | |
155 | ||
156 | SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH, | |
157 | 0, 127, 0), | |
158 | ||
159 | SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume", | |
160 | WM8960_BYPASS1, 4, 7, 1, bypass_tlv), | |
161 | SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume", | |
162 | WM8960_LOUTMIX, 4, 7, 1, bypass_tlv), | |
163 | SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume", | |
164 | WM8960_BYPASS2, 4, 7, 1, bypass_tlv), | |
165 | SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume", | |
166 | WM8960_ROUTMIX, 4, 7, 1, bypass_tlv), | |
167 | }; | |
168 | ||
169 | static const struct snd_kcontrol_new wm8960_lin_boost[] = { | |
170 | SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0), | |
171 | SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0), | |
172 | SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0), | |
173 | }; | |
174 | ||
175 | static const struct snd_kcontrol_new wm8960_lin[] = { | |
176 | SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0), | |
177 | }; | |
178 | ||
179 | static const struct snd_kcontrol_new wm8960_rin_boost[] = { | |
180 | SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0), | |
181 | SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0), | |
182 | SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0), | |
183 | }; | |
184 | ||
185 | static const struct snd_kcontrol_new wm8960_rin[] = { | |
186 | SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0), | |
187 | }; | |
188 | ||
189 | static const struct snd_kcontrol_new wm8960_loutput_mixer[] = { | |
190 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0), | |
191 | SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0), | |
192 | SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0), | |
193 | }; | |
194 | ||
195 | static const struct snd_kcontrol_new wm8960_routput_mixer[] = { | |
196 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0), | |
197 | SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0), | |
198 | SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0), | |
199 | }; | |
200 | ||
201 | static const struct snd_kcontrol_new wm8960_mono_out[] = { | |
202 | SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0), | |
203 | SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0), | |
204 | }; | |
205 | ||
206 | static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = { | |
207 | SND_SOC_DAPM_INPUT("LINPUT1"), | |
208 | SND_SOC_DAPM_INPUT("RINPUT1"), | |
209 | SND_SOC_DAPM_INPUT("LINPUT2"), | |
210 | SND_SOC_DAPM_INPUT("RINPUT2"), | |
211 | SND_SOC_DAPM_INPUT("LINPUT3"), | |
212 | SND_SOC_DAPM_INPUT("RINPUT3"), | |
213 | ||
214 | SND_SOC_DAPM_MICBIAS("MICB", WM8960_POWER1, 1, 0), | |
215 | ||
216 | SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0, | |
217 | wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)), | |
218 | SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0, | |
219 | wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)), | |
220 | ||
221 | SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0, | |
222 | wm8960_lin, ARRAY_SIZE(wm8960_lin)), | |
223 | SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0, | |
224 | wm8960_rin, ARRAY_SIZE(wm8960_rin)), | |
225 | ||
226 | SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0), | |
227 | SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0), | |
228 | ||
229 | SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0), | |
230 | SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0), | |
231 | ||
232 | SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0, | |
233 | &wm8960_loutput_mixer[0], | |
234 | ARRAY_SIZE(wm8960_loutput_mixer)), | |
235 | SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0, | |
236 | &wm8960_routput_mixer[0], | |
237 | ARRAY_SIZE(wm8960_routput_mixer)), | |
238 | ||
f2644a2c MB |
239 | SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0), |
240 | SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0), | |
241 | ||
242 | SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0), | |
243 | SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0), | |
244 | ||
245 | SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0), | |
246 | SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0), | |
247 | ||
248 | SND_SOC_DAPM_OUTPUT("SPK_LP"), | |
249 | SND_SOC_DAPM_OUTPUT("SPK_LN"), | |
250 | SND_SOC_DAPM_OUTPUT("HP_L"), | |
251 | SND_SOC_DAPM_OUTPUT("HP_R"), | |
252 | SND_SOC_DAPM_OUTPUT("SPK_RP"), | |
253 | SND_SOC_DAPM_OUTPUT("SPK_RN"), | |
254 | SND_SOC_DAPM_OUTPUT("OUT3"), | |
255 | }; | |
256 | ||
913d7b4c MB |
257 | static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = { |
258 | SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0, | |
259 | &wm8960_mono_out[0], | |
260 | ARRAY_SIZE(wm8960_mono_out)), | |
261 | }; | |
262 | ||
263 | /* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */ | |
264 | static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = { | |
265 | SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0), | |
266 | }; | |
267 | ||
f2644a2c MB |
268 | static const struct snd_soc_dapm_route audio_paths[] = { |
269 | { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" }, | |
270 | { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" }, | |
271 | { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" }, | |
272 | ||
273 | { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", }, | |
274 | { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */ | |
275 | { "Left Input Mixer", NULL, "LINPUT2" }, | |
276 | { "Left Input Mixer", NULL, "LINPUT3" }, | |
277 | ||
278 | { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" }, | |
279 | { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" }, | |
280 | { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" }, | |
281 | ||
282 | { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", }, | |
283 | { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */ | |
284 | { "Right Input Mixer", NULL, "RINPUT2" }, | |
285 | { "Right Input Mixer", NULL, "LINPUT3" }, | |
286 | ||
287 | { "Left ADC", NULL, "Left Input Mixer" }, | |
288 | { "Right ADC", NULL, "Right Input Mixer" }, | |
289 | ||
290 | { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" }, | |
291 | { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} , | |
292 | { "Left Output Mixer", "PCM Playback Switch", "Left DAC" }, | |
293 | ||
294 | { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" }, | |
295 | { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } , | |
296 | { "Right Output Mixer", "PCM Playback Switch", "Right DAC" }, | |
297 | ||
f2644a2c MB |
298 | { "LOUT1 PGA", NULL, "Left Output Mixer" }, |
299 | { "ROUT1 PGA", NULL, "Right Output Mixer" }, | |
300 | ||
301 | { "HP_L", NULL, "LOUT1 PGA" }, | |
302 | { "HP_R", NULL, "ROUT1 PGA" }, | |
303 | ||
304 | { "Left Speaker PGA", NULL, "Left Output Mixer" }, | |
305 | { "Right Speaker PGA", NULL, "Right Output Mixer" }, | |
306 | ||
307 | { "Left Speaker Output", NULL, "Left Speaker PGA" }, | |
308 | { "Right Speaker Output", NULL, "Right Speaker PGA" }, | |
309 | ||
310 | { "SPK_LN", NULL, "Left Speaker Output" }, | |
311 | { "SPK_LP", NULL, "Left Speaker Output" }, | |
312 | { "SPK_RN", NULL, "Right Speaker Output" }, | |
313 | { "SPK_RP", NULL, "Right Speaker Output" }, | |
913d7b4c MB |
314 | }; |
315 | ||
316 | static const struct snd_soc_dapm_route audio_paths_out3[] = { | |
317 | { "Mono Output Mixer", "Left Switch", "Left Output Mixer" }, | |
318 | { "Mono Output Mixer", "Right Switch", "Right Output Mixer" }, | |
f2644a2c MB |
319 | |
320 | { "OUT3", NULL, "Mono Output Mixer", } | |
321 | }; | |
322 | ||
913d7b4c MB |
323 | static const struct snd_soc_dapm_route audio_paths_capless[] = { |
324 | { "HP_L", NULL, "OUT3 VMID" }, | |
325 | { "HP_R", NULL, "OUT3 VMID" }, | |
326 | ||
327 | { "OUT3 VMID", NULL, "Left Output Mixer" }, | |
328 | { "OUT3 VMID", NULL, "Right Output Mixer" }, | |
329 | }; | |
330 | ||
f2644a2c MB |
331 | static int wm8960_add_widgets(struct snd_soc_codec *codec) |
332 | { | |
913d7b4c MB |
333 | struct wm8960_data *pdata = codec->dev->platform_data; |
334 | struct wm8960_priv *wm8960 = codec->private_data; | |
335 | struct snd_soc_dapm_widget *w; | |
336 | ||
f2644a2c MB |
337 | snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets, |
338 | ARRAY_SIZE(wm8960_dapm_widgets)); | |
339 | ||
340 | snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths)); | |
341 | ||
913d7b4c MB |
342 | /* In capless mode OUT3 is used to provide VMID for the |
343 | * headphone outputs, otherwise it is used as a mono mixer. | |
344 | */ | |
345 | if (pdata && pdata->capless) { | |
346 | snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets_capless, | |
347 | ARRAY_SIZE(wm8960_dapm_widgets_capless)); | |
348 | ||
349 | snd_soc_dapm_add_routes(codec, audio_paths_capless, | |
350 | ARRAY_SIZE(audio_paths_capless)); | |
351 | } else { | |
352 | snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets_out3, | |
353 | ARRAY_SIZE(wm8960_dapm_widgets_out3)); | |
354 | ||
355 | snd_soc_dapm_add_routes(codec, audio_paths_out3, | |
356 | ARRAY_SIZE(audio_paths_out3)); | |
357 | } | |
358 | ||
359 | /* We need to power up the headphone output stage out of | |
360 | * sequence for capless mode. To save scanning the widget | |
361 | * list each time to find the desired power state do so now | |
362 | * and save the result. | |
363 | */ | |
364 | list_for_each_entry(w, &codec->dapm_widgets, list) { | |
365 | if (strcmp(w->name, "LOUT1 PGA") == 0) | |
366 | wm8960->lout1 = w; | |
367 | if (strcmp(w->name, "ROUT1 PGA") == 0) | |
368 | wm8960->rout1 = w; | |
369 | if (strcmp(w->name, "OUT3 VMID") == 0) | |
370 | wm8960->out3 = w; | |
371 | } | |
372 | ||
f2644a2c MB |
373 | return 0; |
374 | } | |
375 | ||
376 | static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai, | |
377 | unsigned int fmt) | |
378 | { | |
379 | struct snd_soc_codec *codec = codec_dai->codec; | |
380 | u16 iface = 0; | |
381 | ||
382 | /* set master/slave audio interface */ | |
383 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
384 | case SND_SOC_DAIFMT_CBM_CFM: | |
385 | iface |= 0x0040; | |
386 | break; | |
387 | case SND_SOC_DAIFMT_CBS_CFS: | |
388 | break; | |
389 | default: | |
390 | return -EINVAL; | |
391 | } | |
392 | ||
393 | /* interface format */ | |
394 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
395 | case SND_SOC_DAIFMT_I2S: | |
396 | iface |= 0x0002; | |
397 | break; | |
398 | case SND_SOC_DAIFMT_RIGHT_J: | |
399 | break; | |
400 | case SND_SOC_DAIFMT_LEFT_J: | |
401 | iface |= 0x0001; | |
402 | break; | |
403 | case SND_SOC_DAIFMT_DSP_A: | |
404 | iface |= 0x0003; | |
405 | break; | |
406 | case SND_SOC_DAIFMT_DSP_B: | |
407 | iface |= 0x0013; | |
408 | break; | |
409 | default: | |
410 | return -EINVAL; | |
411 | } | |
412 | ||
413 | /* clock inversion */ | |
414 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
415 | case SND_SOC_DAIFMT_NB_NF: | |
416 | break; | |
417 | case SND_SOC_DAIFMT_IB_IF: | |
418 | iface |= 0x0090; | |
419 | break; | |
420 | case SND_SOC_DAIFMT_IB_NF: | |
421 | iface |= 0x0080; | |
422 | break; | |
423 | case SND_SOC_DAIFMT_NB_IF: | |
424 | iface |= 0x0010; | |
425 | break; | |
426 | default: | |
427 | return -EINVAL; | |
428 | } | |
429 | ||
430 | /* set iface */ | |
17a52fd6 | 431 | snd_soc_write(codec, WM8960_IFACE1, iface); |
f2644a2c MB |
432 | return 0; |
433 | } | |
434 | ||
435 | static int wm8960_hw_params(struct snd_pcm_substream *substream, | |
436 | struct snd_pcm_hw_params *params, | |
437 | struct snd_soc_dai *dai) | |
438 | { | |
439 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
440 | struct snd_soc_device *socdev = rtd->socdev; | |
441 | struct snd_soc_codec *codec = socdev->card->codec; | |
17a52fd6 | 442 | u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3; |
f2644a2c MB |
443 | |
444 | /* bit size */ | |
445 | switch (params_format(params)) { | |
446 | case SNDRV_PCM_FORMAT_S16_LE: | |
447 | break; | |
448 | case SNDRV_PCM_FORMAT_S20_3LE: | |
449 | iface |= 0x0004; | |
450 | break; | |
451 | case SNDRV_PCM_FORMAT_S24_LE: | |
452 | iface |= 0x0008; | |
453 | break; | |
454 | } | |
455 | ||
456 | /* set iface */ | |
17a52fd6 | 457 | snd_soc_write(codec, WM8960_IFACE1, iface); |
f2644a2c MB |
458 | return 0; |
459 | } | |
460 | ||
461 | static int wm8960_mute(struct snd_soc_dai *dai, int mute) | |
462 | { | |
463 | struct snd_soc_codec *codec = dai->codec; | |
17a52fd6 | 464 | u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7; |
f2644a2c MB |
465 | |
466 | if (mute) | |
17a52fd6 | 467 | snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8); |
f2644a2c | 468 | else |
17a52fd6 | 469 | snd_soc_write(codec, WM8960_DACCTL1, mute_reg); |
f2644a2c MB |
470 | return 0; |
471 | } | |
472 | ||
913d7b4c MB |
473 | static int wm8960_set_bias_level_out3(struct snd_soc_codec *codec, |
474 | enum snd_soc_bias_level level) | |
f2644a2c | 475 | { |
f2644a2c MB |
476 | u16 reg; |
477 | ||
478 | switch (level) { | |
479 | case SND_SOC_BIAS_ON: | |
480 | break; | |
481 | ||
482 | case SND_SOC_BIAS_PREPARE: | |
483 | /* Set VMID to 2x50k */ | |
17a52fd6 | 484 | reg = snd_soc_read(codec, WM8960_POWER1); |
f2644a2c MB |
485 | reg &= ~0x180; |
486 | reg |= 0x80; | |
17a52fd6 | 487 | snd_soc_write(codec, WM8960_POWER1, reg); |
f2644a2c MB |
488 | break; |
489 | ||
490 | case SND_SOC_BIAS_STANDBY: | |
491 | if (codec->bias_level == SND_SOC_BIAS_OFF) { | |
492 | /* Enable anti-pop features */ | |
17a52fd6 | 493 | snd_soc_write(codec, WM8960_APOP1, |
913d7b4c MB |
494 | WM8960_POBCTRL | WM8960_SOFT_ST | |
495 | WM8960_BUFDCOPEN | WM8960_BUFIOEN); | |
f2644a2c MB |
496 | |
497 | /* Enable & ramp VMID at 2x50k */ | |
17a52fd6 | 498 | reg = snd_soc_read(codec, WM8960_POWER1); |
f2644a2c | 499 | reg |= 0x80; |
17a52fd6 | 500 | snd_soc_write(codec, WM8960_POWER1, reg); |
f2644a2c MB |
501 | msleep(100); |
502 | ||
503 | /* Enable VREF */ | |
17a52fd6 | 504 | snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF); |
f2644a2c MB |
505 | |
506 | /* Disable anti-pop features */ | |
17a52fd6 | 507 | snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN); |
f2644a2c MB |
508 | } |
509 | ||
510 | /* Set VMID to 2x250k */ | |
17a52fd6 | 511 | reg = snd_soc_read(codec, WM8960_POWER1); |
f2644a2c MB |
512 | reg &= ~0x180; |
513 | reg |= 0x100; | |
17a52fd6 | 514 | snd_soc_write(codec, WM8960_POWER1, reg); |
f2644a2c MB |
515 | break; |
516 | ||
517 | case SND_SOC_BIAS_OFF: | |
518 | /* Enable anti-pop features */ | |
17a52fd6 | 519 | snd_soc_write(codec, WM8960_APOP1, |
f2644a2c MB |
520 | WM8960_POBCTRL | WM8960_SOFT_ST | |
521 | WM8960_BUFDCOPEN | WM8960_BUFIOEN); | |
522 | ||
523 | /* Disable VMID and VREF, let them discharge */ | |
17a52fd6 | 524 | snd_soc_write(codec, WM8960_POWER1, 0); |
f2644a2c | 525 | msleep(600); |
913d7b4c MB |
526 | break; |
527 | } | |
528 | ||
529 | codec->bias_level = level; | |
530 | ||
531 | return 0; | |
532 | } | |
533 | ||
534 | static int wm8960_set_bias_level_capless(struct snd_soc_codec *codec, | |
535 | enum snd_soc_bias_level level) | |
536 | { | |
537 | struct wm8960_priv *wm8960 = codec->private_data; | |
538 | int reg; | |
539 | ||
540 | switch (level) { | |
541 | case SND_SOC_BIAS_ON: | |
542 | break; | |
543 | ||
544 | case SND_SOC_BIAS_PREPARE: | |
545 | switch (codec->bias_level) { | |
546 | case SND_SOC_BIAS_STANDBY: | |
547 | /* Enable anti pop mode */ | |
548 | snd_soc_update_bits(codec, WM8960_APOP1, | |
549 | WM8960_POBCTRL | WM8960_SOFT_ST | | |
550 | WM8960_BUFDCOPEN, | |
551 | WM8960_POBCTRL | WM8960_SOFT_ST | | |
552 | WM8960_BUFDCOPEN); | |
553 | ||
554 | /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */ | |
555 | reg = 0; | |
556 | if (wm8960->lout1 && wm8960->lout1->power) | |
557 | reg |= WM8960_PWR2_LOUT1; | |
558 | if (wm8960->rout1 && wm8960->rout1->power) | |
559 | reg |= WM8960_PWR2_ROUT1; | |
560 | if (wm8960->out3 && wm8960->out3->power) | |
561 | reg |= WM8960_PWR2_OUT3; | |
562 | snd_soc_update_bits(codec, WM8960_POWER2, | |
563 | WM8960_PWR2_LOUT1 | | |
564 | WM8960_PWR2_ROUT1 | | |
565 | WM8960_PWR2_OUT3, reg); | |
566 | ||
567 | /* Enable VMID at 2*50k */ | |
568 | snd_soc_update_bits(codec, WM8960_POWER1, | |
569 | WM8960_VMID_MASK, 0x80); | |
570 | ||
571 | /* Ramp */ | |
572 | msleep(100); | |
573 | ||
574 | /* Enable VREF */ | |
575 | snd_soc_update_bits(codec, WM8960_POWER1, | |
576 | WM8960_VREF, WM8960_VREF); | |
577 | ||
578 | msleep(100); | |
579 | break; | |
580 | ||
581 | case SND_SOC_BIAS_ON: | |
582 | /* Enable anti-pop mode */ | |
583 | snd_soc_update_bits(codec, WM8960_APOP1, | |
584 | WM8960_POBCTRL | WM8960_SOFT_ST | | |
585 | WM8960_BUFDCOPEN, | |
586 | WM8960_POBCTRL | WM8960_SOFT_ST | | |
587 | WM8960_BUFDCOPEN); | |
588 | ||
589 | /* Disable VMID and VREF */ | |
590 | snd_soc_update_bits(codec, WM8960_POWER1, | |
591 | WM8960_VREF | WM8960_VMID_MASK, 0); | |
592 | break; | |
593 | ||
594 | default: | |
595 | break; | |
596 | } | |
597 | break; | |
598 | ||
599 | case SND_SOC_BIAS_STANDBY: | |
600 | switch (codec->bias_level) { | |
601 | case SND_SOC_BIAS_PREPARE: | |
602 | /* Disable HP discharge */ | |
603 | snd_soc_update_bits(codec, WM8960_APOP2, | |
604 | WM8960_DISOP | WM8960_DRES_MASK, | |
605 | 0); | |
606 | ||
607 | /* Disable anti-pop features */ | |
608 | snd_soc_update_bits(codec, WM8960_APOP1, | |
609 | WM8960_POBCTRL | WM8960_SOFT_ST | | |
610 | WM8960_BUFDCOPEN, | |
611 | WM8960_POBCTRL | WM8960_SOFT_ST | | |
612 | WM8960_BUFDCOPEN); | |
613 | break; | |
614 | ||
615 | default: | |
616 | break; | |
617 | } | |
618 | break; | |
f2644a2c | 619 | |
913d7b4c | 620 | case SND_SOC_BIAS_OFF: |
f2644a2c MB |
621 | break; |
622 | } | |
623 | ||
624 | codec->bias_level = level; | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
629 | /* PLL divisors */ | |
630 | struct _pll_div { | |
631 | u32 pre_div:1; | |
632 | u32 n:4; | |
633 | u32 k:24; | |
634 | }; | |
635 | ||
636 | /* The size in bits of the pll divide multiplied by 10 | |
637 | * to allow rounding later */ | |
638 | #define FIXED_PLL_SIZE ((1 << 24) * 10) | |
639 | ||
640 | static int pll_factors(unsigned int source, unsigned int target, | |
641 | struct _pll_div *pll_div) | |
642 | { | |
643 | unsigned long long Kpart; | |
644 | unsigned int K, Ndiv, Nmod; | |
645 | ||
646 | pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target); | |
647 | ||
648 | /* Scale up target to PLL operating frequency */ | |
649 | target *= 4; | |
650 | ||
651 | Ndiv = target / source; | |
652 | if (Ndiv < 6) { | |
653 | source >>= 1; | |
654 | pll_div->pre_div = 1; | |
655 | Ndiv = target / source; | |
656 | } else | |
657 | pll_div->pre_div = 0; | |
658 | ||
659 | if ((Ndiv < 6) || (Ndiv > 12)) { | |
660 | pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv); | |
661 | return -EINVAL; | |
662 | } | |
663 | ||
664 | pll_div->n = Ndiv; | |
665 | Nmod = target % source; | |
666 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; | |
667 | ||
668 | do_div(Kpart, source); | |
669 | ||
670 | K = Kpart & 0xFFFFFFFF; | |
671 | ||
672 | /* Check if we need to round */ | |
673 | if ((K % 10) >= 5) | |
674 | K += 5; | |
675 | ||
676 | /* Move down to proper range now rounding is done */ | |
677 | K /= 10; | |
678 | ||
679 | pll_div->k = K; | |
680 | ||
681 | pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n", | |
682 | pll_div->n, pll_div->k, pll_div->pre_div); | |
683 | ||
684 | return 0; | |
685 | } | |
686 | ||
85488037 MB |
687 | static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
688 | int source, unsigned int freq_in, unsigned int freq_out) | |
f2644a2c MB |
689 | { |
690 | struct snd_soc_codec *codec = codec_dai->codec; | |
691 | u16 reg; | |
692 | static struct _pll_div pll_div; | |
693 | int ret; | |
694 | ||
695 | if (freq_in && freq_out) { | |
696 | ret = pll_factors(freq_in, freq_out, &pll_div); | |
697 | if (ret != 0) | |
698 | return ret; | |
699 | } | |
700 | ||
701 | /* Disable the PLL: even if we are changing the frequency the | |
702 | * PLL needs to be disabled while we do so. */ | |
17a52fd6 MB |
703 | snd_soc_write(codec, WM8960_CLOCK1, |
704 | snd_soc_read(codec, WM8960_CLOCK1) & ~1); | |
705 | snd_soc_write(codec, WM8960_POWER2, | |
706 | snd_soc_read(codec, WM8960_POWER2) & ~1); | |
f2644a2c MB |
707 | |
708 | if (!freq_in || !freq_out) | |
709 | return 0; | |
710 | ||
17a52fd6 | 711 | reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f; |
f2644a2c MB |
712 | reg |= pll_div.pre_div << 4; |
713 | reg |= pll_div.n; | |
714 | ||
715 | if (pll_div.k) { | |
716 | reg |= 0x20; | |
717 | ||
17a52fd6 MB |
718 | snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f); |
719 | snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff); | |
720 | snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff); | |
f2644a2c | 721 | } |
17a52fd6 | 722 | snd_soc_write(codec, WM8960_PLL1, reg); |
f2644a2c MB |
723 | |
724 | /* Turn it on */ | |
17a52fd6 MB |
725 | snd_soc_write(codec, WM8960_POWER2, |
726 | snd_soc_read(codec, WM8960_POWER2) | 1); | |
f2644a2c | 727 | msleep(250); |
17a52fd6 MB |
728 | snd_soc_write(codec, WM8960_CLOCK1, |
729 | snd_soc_read(codec, WM8960_CLOCK1) | 1); | |
f2644a2c MB |
730 | |
731 | return 0; | |
732 | } | |
733 | ||
734 | static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai, | |
735 | int div_id, int div) | |
736 | { | |
737 | struct snd_soc_codec *codec = codec_dai->codec; | |
738 | u16 reg; | |
739 | ||
740 | switch (div_id) { | |
741 | case WM8960_SYSCLKSEL: | |
17a52fd6 MB |
742 | reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1fe; |
743 | snd_soc_write(codec, WM8960_CLOCK1, reg | div); | |
f2644a2c MB |
744 | break; |
745 | case WM8960_SYSCLKDIV: | |
17a52fd6 MB |
746 | reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9; |
747 | snd_soc_write(codec, WM8960_CLOCK1, reg | div); | |
f2644a2c MB |
748 | break; |
749 | case WM8960_DACDIV: | |
17a52fd6 MB |
750 | reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7; |
751 | snd_soc_write(codec, WM8960_CLOCK1, reg | div); | |
f2644a2c MB |
752 | break; |
753 | case WM8960_OPCLKDIV: | |
17a52fd6 MB |
754 | reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f; |
755 | snd_soc_write(codec, WM8960_PLL1, reg | div); | |
f2644a2c MB |
756 | break; |
757 | case WM8960_DCLKDIV: | |
17a52fd6 MB |
758 | reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f; |
759 | snd_soc_write(codec, WM8960_CLOCK2, reg | div); | |
f2644a2c MB |
760 | break; |
761 | case WM8960_TOCLKSEL: | |
17a52fd6 MB |
762 | reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd; |
763 | snd_soc_write(codec, WM8960_ADDCTL1, reg | div); | |
f2644a2c MB |
764 | break; |
765 | default: | |
766 | return -EINVAL; | |
767 | } | |
768 | ||
769 | return 0; | |
770 | } | |
771 | ||
772 | #define WM8960_RATES SNDRV_PCM_RATE_8000_48000 | |
773 | ||
774 | #define WM8960_FORMATS \ | |
775 | (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ | |
776 | SNDRV_PCM_FMTBIT_S24_LE) | |
777 | ||
778 | static struct snd_soc_dai_ops wm8960_dai_ops = { | |
779 | .hw_params = wm8960_hw_params, | |
780 | .digital_mute = wm8960_mute, | |
781 | .set_fmt = wm8960_set_dai_fmt, | |
782 | .set_clkdiv = wm8960_set_dai_clkdiv, | |
783 | .set_pll = wm8960_set_dai_pll, | |
784 | }; | |
785 | ||
786 | struct snd_soc_dai wm8960_dai = { | |
787 | .name = "WM8960", | |
788 | .playback = { | |
789 | .stream_name = "Playback", | |
790 | .channels_min = 1, | |
791 | .channels_max = 2, | |
792 | .rates = WM8960_RATES, | |
793 | .formats = WM8960_FORMATS,}, | |
794 | .capture = { | |
795 | .stream_name = "Capture", | |
796 | .channels_min = 1, | |
797 | .channels_max = 2, | |
798 | .rates = WM8960_RATES, | |
799 | .formats = WM8960_FORMATS,}, | |
800 | .ops = &wm8960_dai_ops, | |
801 | .symmetric_rates = 1, | |
802 | }; | |
803 | EXPORT_SYMBOL_GPL(wm8960_dai); | |
804 | ||
805 | static int wm8960_suspend(struct platform_device *pdev, pm_message_t state) | |
806 | { | |
807 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
808 | struct snd_soc_codec *codec = socdev->card->codec; | |
809 | ||
913d7b4c | 810 | codec->set_bias_level(codec, SND_SOC_BIAS_OFF); |
f2644a2c MB |
811 | return 0; |
812 | } | |
813 | ||
814 | static int wm8960_resume(struct platform_device *pdev) | |
815 | { | |
816 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
817 | struct snd_soc_codec *codec = socdev->card->codec; | |
818 | int i; | |
819 | u8 data[2]; | |
820 | u16 *cache = codec->reg_cache; | |
821 | ||
822 | /* Sync reg_cache with the hardware */ | |
823 | for (i = 0; i < ARRAY_SIZE(wm8960_reg); i++) { | |
824 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); | |
825 | data[1] = cache[i] & 0x00ff; | |
826 | codec->hw_write(codec->control_data, data, 2); | |
827 | } | |
828 | ||
913d7b4c MB |
829 | codec->set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
830 | codec->set_bias_level(codec, codec->suspend_bias_level); | |
f2644a2c MB |
831 | return 0; |
832 | } | |
833 | ||
834 | static struct snd_soc_codec *wm8960_codec; | |
835 | ||
836 | static int wm8960_probe(struct platform_device *pdev) | |
837 | { | |
838 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
839 | struct snd_soc_codec *codec; | |
840 | int ret = 0; | |
841 | ||
842 | if (wm8960_codec == NULL) { | |
843 | dev_err(&pdev->dev, "Codec device not registered\n"); | |
844 | return -ENODEV; | |
845 | } | |
846 | ||
847 | socdev->card->codec = wm8960_codec; | |
848 | codec = wm8960_codec; | |
849 | ||
850 | /* register pcms */ | |
851 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); | |
852 | if (ret < 0) { | |
853 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); | |
854 | goto pcm_err; | |
855 | } | |
856 | ||
857 | snd_soc_add_controls(codec, wm8960_snd_controls, | |
858 | ARRAY_SIZE(wm8960_snd_controls)); | |
859 | wm8960_add_widgets(codec); | |
f2644a2c MB |
860 | |
861 | return ret; | |
862 | ||
f2644a2c MB |
863 | pcm_err: |
864 | return ret; | |
865 | } | |
866 | ||
867 | /* power down chip */ | |
868 | static int wm8960_remove(struct platform_device *pdev) | |
869 | { | |
870 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); | |
871 | ||
872 | snd_soc_free_pcms(socdev); | |
873 | snd_soc_dapm_free(socdev); | |
874 | ||
875 | return 0; | |
876 | } | |
877 | ||
878 | struct snd_soc_codec_device soc_codec_dev_wm8960 = { | |
879 | .probe = wm8960_probe, | |
880 | .remove = wm8960_remove, | |
881 | .suspend = wm8960_suspend, | |
882 | .resume = wm8960_resume, | |
883 | }; | |
884 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8960); | |
885 | ||
7084a42b MB |
886 | static int wm8960_register(struct wm8960_priv *wm8960, |
887 | enum snd_soc_control_type control) | |
f2644a2c MB |
888 | { |
889 | struct wm8960_data *pdata = wm8960->codec.dev->platform_data; | |
890 | struct snd_soc_codec *codec = &wm8960->codec; | |
891 | int ret; | |
892 | u16 reg; | |
893 | ||
894 | if (wm8960_codec) { | |
895 | dev_err(codec->dev, "Another WM8960 is registered\n"); | |
1a01417e MB |
896 | ret = -EINVAL; |
897 | goto err; | |
f2644a2c MB |
898 | } |
899 | ||
913d7b4c MB |
900 | codec->set_bias_level = wm8960_set_bias_level_out3; |
901 | ||
f2644a2c MB |
902 | if (!pdata) { |
903 | dev_warn(codec->dev, "No platform data supplied\n"); | |
904 | } else { | |
905 | if (pdata->dres > WM8960_DRES_MAX) { | |
906 | dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres); | |
907 | pdata->dres = 0; | |
908 | } | |
913d7b4c MB |
909 | |
910 | if (pdata->capless) | |
911 | codec->set_bias_level = wm8960_set_bias_level_capless; | |
f2644a2c MB |
912 | } |
913 | ||
914 | mutex_init(&codec->mutex); | |
915 | INIT_LIST_HEAD(&codec->dapm_widgets); | |
916 | INIT_LIST_HEAD(&codec->dapm_paths); | |
917 | ||
918 | codec->private_data = wm8960; | |
919 | codec->name = "WM8960"; | |
920 | codec->owner = THIS_MODULE; | |
f2644a2c | 921 | codec->bias_level = SND_SOC_BIAS_OFF; |
f2644a2c MB |
922 | codec->dai = &wm8960_dai; |
923 | codec->num_dai = 1; | |
924 | codec->reg_cache_size = WM8960_CACHEREGNUM; | |
925 | codec->reg_cache = &wm8960->reg_cache; | |
926 | ||
927 | memcpy(codec->reg_cache, wm8960_reg, sizeof(wm8960_reg)); | |
928 | ||
7084a42b | 929 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); |
17a52fd6 MB |
930 | if (ret < 0) { |
931 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); | |
932 | goto err; | |
933 | } | |
934 | ||
f2644a2c MB |
935 | ret = wm8960_reset(codec); |
936 | if (ret < 0) { | |
937 | dev_err(codec->dev, "Failed to issue reset\n"); | |
1a01417e | 938 | goto err; |
f2644a2c MB |
939 | } |
940 | ||
941 | wm8960_dai.dev = codec->dev; | |
942 | ||
913d7b4c | 943 | codec->set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
f2644a2c MB |
944 | |
945 | /* Latch the update bits */ | |
17a52fd6 MB |
946 | reg = snd_soc_read(codec, WM8960_LINVOL); |
947 | snd_soc_write(codec, WM8960_LINVOL, reg | 0x100); | |
948 | reg = snd_soc_read(codec, WM8960_RINVOL); | |
949 | snd_soc_write(codec, WM8960_RINVOL, reg | 0x100); | |
950 | reg = snd_soc_read(codec, WM8960_LADC); | |
951 | snd_soc_write(codec, WM8960_LADC, reg | 0x100); | |
952 | reg = snd_soc_read(codec, WM8960_RADC); | |
953 | snd_soc_write(codec, WM8960_RADC, reg | 0x100); | |
954 | reg = snd_soc_read(codec, WM8960_LDAC); | |
955 | snd_soc_write(codec, WM8960_LDAC, reg | 0x100); | |
956 | reg = snd_soc_read(codec, WM8960_RDAC); | |
957 | snd_soc_write(codec, WM8960_RDAC, reg | 0x100); | |
958 | reg = snd_soc_read(codec, WM8960_LOUT1); | |
959 | snd_soc_write(codec, WM8960_LOUT1, reg | 0x100); | |
960 | reg = snd_soc_read(codec, WM8960_ROUT1); | |
961 | snd_soc_write(codec, WM8960_ROUT1, reg | 0x100); | |
962 | reg = snd_soc_read(codec, WM8960_LOUT2); | |
963 | snd_soc_write(codec, WM8960_LOUT2, reg | 0x100); | |
964 | reg = snd_soc_read(codec, WM8960_ROUT2); | |
965 | snd_soc_write(codec, WM8960_ROUT2, reg | 0x100); | |
f2644a2c MB |
966 | |
967 | wm8960_codec = codec; | |
968 | ||
969 | ret = snd_soc_register_codec(codec); | |
970 | if (ret != 0) { | |
971 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); | |
1a01417e | 972 | goto err; |
f2644a2c MB |
973 | } |
974 | ||
975 | ret = snd_soc_register_dai(&wm8960_dai); | |
976 | if (ret != 0) { | |
977 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); | |
1a01417e | 978 | goto err_codec; |
f2644a2c MB |
979 | } |
980 | ||
981 | return 0; | |
1a01417e MB |
982 | |
983 | err_codec: | |
984 | snd_soc_unregister_codec(codec); | |
985 | err: | |
986 | kfree(wm8960); | |
987 | return ret; | |
f2644a2c MB |
988 | } |
989 | ||
990 | static void wm8960_unregister(struct wm8960_priv *wm8960) | |
991 | { | |
913d7b4c | 992 | wm8960->codec.set_bias_level(&wm8960->codec, SND_SOC_BIAS_OFF); |
f2644a2c MB |
993 | snd_soc_unregister_dai(&wm8960_dai); |
994 | snd_soc_unregister_codec(&wm8960->codec); | |
995 | kfree(wm8960); | |
996 | wm8960_codec = NULL; | |
997 | } | |
998 | ||
999 | static __devinit int wm8960_i2c_probe(struct i2c_client *i2c, | |
1000 | const struct i2c_device_id *id) | |
1001 | { | |
1002 | struct wm8960_priv *wm8960; | |
1003 | struct snd_soc_codec *codec; | |
1004 | ||
1005 | wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL); | |
1006 | if (wm8960 == NULL) | |
1007 | return -ENOMEM; | |
1008 | ||
1009 | codec = &wm8960->codec; | |
f2644a2c MB |
1010 | |
1011 | i2c_set_clientdata(i2c, wm8960); | |
1012 | codec->control_data = i2c; | |
1013 | ||
1014 | codec->dev = &i2c->dev; | |
1015 | ||
7084a42b | 1016 | return wm8960_register(wm8960, SND_SOC_I2C); |
f2644a2c MB |
1017 | } |
1018 | ||
1019 | static __devexit int wm8960_i2c_remove(struct i2c_client *client) | |
1020 | { | |
1021 | struct wm8960_priv *wm8960 = i2c_get_clientdata(client); | |
1022 | wm8960_unregister(wm8960); | |
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | static const struct i2c_device_id wm8960_i2c_id[] = { | |
1027 | { "wm8960", 0 }, | |
1028 | { } | |
1029 | }; | |
1030 | MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id); | |
1031 | ||
1032 | static struct i2c_driver wm8960_i2c_driver = { | |
1033 | .driver = { | |
a24d62d2 | 1034 | .name = "wm8960", |
f2644a2c MB |
1035 | .owner = THIS_MODULE, |
1036 | }, | |
1037 | .probe = wm8960_i2c_probe, | |
1038 | .remove = __devexit_p(wm8960_i2c_remove), | |
1039 | .id_table = wm8960_i2c_id, | |
1040 | }; | |
1041 | ||
1042 | static int __init wm8960_modinit(void) | |
1043 | { | |
1044 | int ret; | |
1045 | ||
1046 | ret = i2c_add_driver(&wm8960_i2c_driver); | |
1047 | if (ret != 0) { | |
1048 | printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n", | |
1049 | ret); | |
1050 | } | |
1051 | ||
1052 | return ret; | |
1053 | } | |
1054 | module_init(wm8960_modinit); | |
1055 | ||
1056 | static void __exit wm8960_exit(void) | |
1057 | { | |
1058 | i2c_del_driver(&wm8960_i2c_driver); | |
1059 | } | |
1060 | module_exit(wm8960_exit); | |
1061 | ||
1062 | ||
1063 | MODULE_DESCRIPTION("ASoC WM8960 driver"); | |
1064 | MODULE_AUTHOR("Liam Girdwood"); | |
1065 | MODULE_LICENSE("GPL"); |