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