Commit | Line | Data |
---|---|---|
3a66d387 MB |
1 | /* |
2 | * wm2000.c -- WM2000 ALSA Soc Audio driver | |
3 | * | |
656baaeb | 4 | * Copyright 2008-2011 Wolfson Microelectronics PLC. |
3a66d387 MB |
5 | * |
6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | * The download image for the WM2000 will be requested as | |
13 | * 'wm2000_anc.bin' by default (overridable via platform data) at | |
14 | * runtime and is expected to be in flat binary format. This is | |
15 | * generated by Wolfson configuration tools and includes | |
16 | * system-specific callibration information. If supplied as a | |
17 | * sequence of ASCII-encoded hexidecimal bytes this can be converted | |
18 | * into a flat binary with a command such as this on the command line: | |
19 | * | |
20 | * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }' | |
21 | * < file > wm2000_anc.bin | |
22 | */ | |
23 | ||
24 | #include <linux/module.h> | |
25 | #include <linux/moduleparam.h> | |
3a66d387 MB |
26 | #include <linux/kernel.h> |
27 | #include <linux/init.h> | |
28 | #include <linux/firmware.h> | |
29 | #include <linux/delay.h> | |
30 | #include <linux/pm.h> | |
31 | #include <linux/i2c.h> | |
8aa1fe81 | 32 | #include <linux/regmap.h> |
3a66d387 | 33 | #include <linux/debugfs.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
3a66d387 MB |
35 | #include <sound/core.h> |
36 | #include <sound/pcm.h> | |
37 | #include <sound/pcm_params.h> | |
38 | #include <sound/soc.h> | |
3a66d387 MB |
39 | #include <sound/initval.h> |
40 | #include <sound/tlv.h> | |
41 | ||
42 | #include <sound/wm2000.h> | |
43 | ||
44 | #include "wm2000.h" | |
45 | ||
46 | enum wm2000_anc_mode { | |
47 | ANC_ACTIVE = 0, | |
48 | ANC_BYPASS = 1, | |
49 | ANC_STANDBY = 2, | |
50 | ANC_OFF = 3, | |
51 | }; | |
52 | ||
53 | struct wm2000_priv { | |
54 | struct i2c_client *i2c; | |
8aa1fe81 | 55 | struct regmap *regmap; |
3a66d387 MB |
56 | |
57 | enum wm2000_anc_mode anc_mode; | |
58 | ||
59 | unsigned int anc_active:1; | |
60 | unsigned int anc_eng_ena:1; | |
61 | unsigned int spk_ena:1; | |
62 | ||
63 | unsigned int mclk_div:1; | |
64 | unsigned int speech_clarity:1; | |
65 | ||
66 | int anc_download_size; | |
67 | char *anc_download; | |
68 | }; | |
69 | ||
3a66d387 MB |
70 | static int wm2000_write(struct i2c_client *i2c, unsigned int reg, |
71 | unsigned int value) | |
72 | { | |
8aa1fe81 MB |
73 | struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c); |
74 | return regmap_write(wm2000->regmap, reg, value); | |
3a66d387 MB |
75 | } |
76 | ||
77 | static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r) | |
78 | { | |
8aa1fe81 MB |
79 | struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c); |
80 | unsigned int val; | |
3a66d387 MB |
81 | int ret; |
82 | ||
8aa1fe81 MB |
83 | ret = regmap_read(wm2000->regmap, r, &val); |
84 | if (ret < 0) | |
85 | return -1; | |
3a66d387 | 86 | |
8aa1fe81 | 87 | return val; |
3a66d387 MB |
88 | } |
89 | ||
90 | static void wm2000_reset(struct wm2000_priv *wm2000) | |
91 | { | |
92 | struct i2c_client *i2c = wm2000->i2c; | |
93 | ||
94 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR); | |
95 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); | |
96 | wm2000_write(i2c, WM2000_REG_ID1, 0); | |
97 | ||
98 | wm2000->anc_mode = ANC_OFF; | |
99 | } | |
100 | ||
101 | static int wm2000_poll_bit(struct i2c_client *i2c, | |
3ff1ec27 | 102 | unsigned int reg, u8 mask) |
3a66d387 | 103 | { |
3ff1ec27 | 104 | int timeout = 4000; |
3a66d387 MB |
105 | int val; |
106 | ||
107 | val = wm2000_read(i2c, reg); | |
108 | ||
109 | while (!(val & mask) && --timeout) { | |
110 | msleep(1); | |
111 | val = wm2000_read(i2c, reg); | |
112 | } | |
113 | ||
114 | if (timeout == 0) | |
115 | return 0; | |
116 | else | |
117 | return 1; | |
118 | } | |
119 | ||
120 | static int wm2000_power_up(struct i2c_client *i2c, int analogue) | |
121 | { | |
122 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
3ff1ec27 | 123 | int ret; |
3a66d387 MB |
124 | |
125 | BUG_ON(wm2000->anc_mode != ANC_OFF); | |
126 | ||
127 | dev_dbg(&i2c->dev, "Beginning power up\n"); | |
128 | ||
129 | if (!wm2000->mclk_div) { | |
130 | dev_dbg(&i2c->dev, "Disabling MCLK divider\n"); | |
131 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, | |
132 | WM2000_MCLK_DIV2_ENA_CLR); | |
133 | } else { | |
134 | dev_dbg(&i2c->dev, "Enabling MCLK divider\n"); | |
135 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, | |
136 | WM2000_MCLK_DIV2_ENA_SET); | |
137 | } | |
138 | ||
139 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR); | |
140 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET); | |
141 | ||
142 | /* Wait for ANC engine to become ready */ | |
143 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, | |
3ff1ec27 | 144 | WM2000_ANC_ENG_IDLE)) { |
3a66d387 MB |
145 | dev_err(&i2c->dev, "ANC engine failed to reset\n"); |
146 | return -ETIMEDOUT; | |
147 | } | |
148 | ||
149 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 | 150 | WM2000_STATUS_BOOT_COMPLETE)) { |
3a66d387 MB |
151 | dev_err(&i2c->dev, "ANC engine failed to initialise\n"); |
152 | return -ETIMEDOUT; | |
153 | } | |
154 | ||
155 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); | |
156 | ||
157 | /* Open code download of the data since it is the only bulk | |
158 | * write we do. */ | |
159 | dev_dbg(&i2c->dev, "Downloading %d bytes\n", | |
160 | wm2000->anc_download_size - 2); | |
161 | ||
162 | ret = i2c_master_send(i2c, wm2000->anc_download, | |
163 | wm2000->anc_download_size); | |
164 | if (ret < 0) { | |
165 | dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret); | |
166 | return ret; | |
167 | } | |
168 | if (ret != wm2000->anc_download_size) { | |
169 | dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n", | |
170 | ret, wm2000->anc_download_size); | |
171 | return -EIO; | |
172 | } | |
173 | ||
174 | dev_dbg(&i2c->dev, "Download complete\n"); | |
175 | ||
176 | if (analogue) { | |
3ff1ec27 | 177 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4); |
3a66d387 MB |
178 | |
179 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
180 | WM2000_MODE_ANA_SEQ_INCLUDE | | |
181 | WM2000_MODE_MOUSE_ENABLE | | |
182 | WM2000_MODE_THERMAL_ENABLE); | |
183 | } else { | |
3a66d387 MB |
184 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
185 | WM2000_MODE_MOUSE_ENABLE | | |
186 | WM2000_MODE_THERMAL_ENABLE); | |
187 | } | |
188 | ||
189 | ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY); | |
190 | if (wm2000->speech_clarity) | |
191 | ret &= ~WM2000_SPEECH_CLARITY; | |
192 | else | |
193 | ret |= WM2000_SPEECH_CLARITY; | |
194 | wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret); | |
195 | ||
196 | wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33); | |
197 | wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02); | |
198 | ||
199 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); | |
200 | ||
201 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 MB |
202 | WM2000_STATUS_MOUSE_ACTIVE)) { |
203 | dev_err(&i2c->dev, "Timed out waiting for device\n"); | |
3a66d387 MB |
204 | return -ETIMEDOUT; |
205 | } | |
206 | ||
207 | dev_dbg(&i2c->dev, "ANC active\n"); | |
208 | if (analogue) | |
209 | dev_dbg(&i2c->dev, "Analogue active\n"); | |
210 | wm2000->anc_mode = ANC_ACTIVE; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static int wm2000_power_down(struct i2c_client *i2c, int analogue) | |
216 | { | |
217 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
3a66d387 MB |
218 | |
219 | if (analogue) { | |
3ff1ec27 | 220 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4); |
3a66d387 MB |
221 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
222 | WM2000_MODE_ANA_SEQ_INCLUDE | | |
223 | WM2000_MODE_POWER_DOWN); | |
224 | } else { | |
3a66d387 MB |
225 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
226 | WM2000_MODE_POWER_DOWN); | |
227 | } | |
228 | ||
229 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 | 230 | WM2000_STATUS_POWER_DOWN_COMPLETE)) { |
3a66d387 MB |
231 | dev_err(&i2c->dev, "Timeout waiting for ANC power down\n"); |
232 | return -ETIMEDOUT; | |
233 | } | |
234 | ||
235 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, | |
3ff1ec27 | 236 | WM2000_ANC_ENG_IDLE)) { |
3a66d387 MB |
237 | dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n"); |
238 | return -ETIMEDOUT; | |
239 | } | |
240 | ||
241 | dev_dbg(&i2c->dev, "powered off\n"); | |
242 | wm2000->anc_mode = ANC_OFF; | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
247 | static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue) | |
248 | { | |
249 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
250 | ||
251 | BUG_ON(wm2000->anc_mode != ANC_ACTIVE); | |
252 | ||
253 | if (analogue) { | |
254 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
255 | WM2000_MODE_ANA_SEQ_INCLUDE | | |
256 | WM2000_MODE_THERMAL_ENABLE | | |
257 | WM2000_MODE_BYPASS_ENTRY); | |
258 | } else { | |
259 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
260 | WM2000_MODE_THERMAL_ENABLE | | |
261 | WM2000_MODE_BYPASS_ENTRY); | |
262 | } | |
263 | ||
264 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 | 265 | WM2000_STATUS_ANC_DISABLED)) { |
3a66d387 MB |
266 | dev_err(&i2c->dev, "Timeout waiting for ANC disable\n"); |
267 | return -ETIMEDOUT; | |
268 | } | |
269 | ||
270 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, | |
3ff1ec27 | 271 | WM2000_ANC_ENG_IDLE)) { |
3a66d387 MB |
272 | dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n"); |
273 | return -ETIMEDOUT; | |
274 | } | |
275 | ||
276 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY); | |
277 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); | |
278 | ||
279 | wm2000->anc_mode = ANC_BYPASS; | |
280 | dev_dbg(&i2c->dev, "bypass enabled\n"); | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue) | |
286 | { | |
287 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
288 | ||
289 | BUG_ON(wm2000->anc_mode != ANC_BYPASS); | |
290 | ||
291 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0); | |
292 | ||
293 | if (analogue) { | |
294 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
295 | WM2000_MODE_ANA_SEQ_INCLUDE | | |
296 | WM2000_MODE_MOUSE_ENABLE | | |
297 | WM2000_MODE_THERMAL_ENABLE); | |
298 | } else { | |
299 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
300 | WM2000_MODE_MOUSE_ENABLE | | |
301 | WM2000_MODE_THERMAL_ENABLE); | |
302 | } | |
303 | ||
304 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); | |
305 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); | |
306 | ||
307 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 | 308 | WM2000_STATUS_MOUSE_ACTIVE)) { |
3a66d387 MB |
309 | dev_err(&i2c->dev, "Timed out waiting for MOUSE\n"); |
310 | return -ETIMEDOUT; | |
311 | } | |
312 | ||
313 | wm2000->anc_mode = ANC_ACTIVE; | |
314 | dev_dbg(&i2c->dev, "MOUSE active\n"); | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | static int wm2000_enter_standby(struct i2c_client *i2c, int analogue) | |
320 | { | |
321 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
3a66d387 MB |
322 | |
323 | BUG_ON(wm2000->anc_mode != ANC_ACTIVE); | |
324 | ||
325 | if (analogue) { | |
3ff1ec27 | 326 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4); |
3a66d387 MB |
327 | |
328 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
329 | WM2000_MODE_ANA_SEQ_INCLUDE | | |
330 | WM2000_MODE_THERMAL_ENABLE | | |
331 | WM2000_MODE_STANDBY_ENTRY); | |
332 | } else { | |
3a66d387 MB |
333 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
334 | WM2000_MODE_THERMAL_ENABLE | | |
335 | WM2000_MODE_STANDBY_ENTRY); | |
336 | } | |
337 | ||
338 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 | 339 | WM2000_STATUS_ANC_DISABLED)) { |
3a66d387 MB |
340 | dev_err(&i2c->dev, |
341 | "Timed out waiting for ANC disable after 1ms\n"); | |
342 | return -ETIMEDOUT; | |
343 | } | |
344 | ||
3ff1ec27 | 345 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) { |
3a66d387 | 346 | dev_err(&i2c->dev, |
3ff1ec27 | 347 | "Timed out waiting for standby\n"); |
3a66d387 MB |
348 | return -ETIMEDOUT; |
349 | } | |
350 | ||
351 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY); | |
352 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); | |
353 | ||
354 | wm2000->anc_mode = ANC_STANDBY; | |
355 | dev_dbg(&i2c->dev, "standby\n"); | |
356 | if (analogue) | |
357 | dev_dbg(&i2c->dev, "Analogue disabled\n"); | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
362 | static int wm2000_exit_standby(struct i2c_client *i2c, int analogue) | |
363 | { | |
364 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
3a66d387 MB |
365 | |
366 | BUG_ON(wm2000->anc_mode != ANC_STANDBY); | |
367 | ||
368 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0); | |
369 | ||
370 | if (analogue) { | |
3ff1ec27 | 371 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4); |
3a66d387 MB |
372 | |
373 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, | |
374 | WM2000_MODE_ANA_SEQ_INCLUDE | | |
375 | WM2000_MODE_THERMAL_ENABLE | | |
376 | WM2000_MODE_MOUSE_ENABLE); | |
377 | } else { | |
3a66d387 MB |
378 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
379 | WM2000_MODE_THERMAL_ENABLE | | |
380 | WM2000_MODE_MOUSE_ENABLE); | |
381 | } | |
382 | ||
383 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); | |
384 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); | |
385 | ||
386 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, | |
3ff1ec27 MB |
387 | WM2000_STATUS_MOUSE_ACTIVE)) { |
388 | dev_err(&i2c->dev, "Timed out waiting for MOUSE\n"); | |
3a66d387 MB |
389 | return -ETIMEDOUT; |
390 | } | |
391 | ||
392 | wm2000->anc_mode = ANC_ACTIVE; | |
393 | dev_dbg(&i2c->dev, "MOUSE active\n"); | |
394 | if (analogue) | |
395 | dev_dbg(&i2c->dev, "Analogue enabled\n"); | |
396 | ||
397 | return 0; | |
398 | } | |
399 | ||
400 | typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue); | |
401 | ||
402 | static struct { | |
403 | enum wm2000_anc_mode source; | |
404 | enum wm2000_anc_mode dest; | |
405 | int analogue; | |
406 | wm2000_mode_fn step[2]; | |
407 | } anc_transitions[] = { | |
408 | { | |
409 | .source = ANC_OFF, | |
410 | .dest = ANC_ACTIVE, | |
411 | .analogue = 1, | |
412 | .step = { | |
413 | wm2000_power_up, | |
414 | }, | |
415 | }, | |
416 | { | |
417 | .source = ANC_OFF, | |
418 | .dest = ANC_STANDBY, | |
419 | .step = { | |
420 | wm2000_power_up, | |
421 | wm2000_enter_standby, | |
422 | }, | |
423 | }, | |
424 | { | |
425 | .source = ANC_OFF, | |
426 | .dest = ANC_BYPASS, | |
427 | .analogue = 1, | |
428 | .step = { | |
429 | wm2000_power_up, | |
430 | wm2000_enter_bypass, | |
431 | }, | |
432 | }, | |
433 | { | |
434 | .source = ANC_ACTIVE, | |
435 | .dest = ANC_BYPASS, | |
436 | .analogue = 1, | |
437 | .step = { | |
438 | wm2000_enter_bypass, | |
439 | }, | |
440 | }, | |
441 | { | |
442 | .source = ANC_ACTIVE, | |
443 | .dest = ANC_STANDBY, | |
444 | .analogue = 1, | |
445 | .step = { | |
446 | wm2000_enter_standby, | |
447 | }, | |
448 | }, | |
449 | { | |
450 | .source = ANC_ACTIVE, | |
451 | .dest = ANC_OFF, | |
452 | .analogue = 1, | |
453 | .step = { | |
454 | wm2000_power_down, | |
455 | }, | |
456 | }, | |
457 | { | |
458 | .source = ANC_BYPASS, | |
459 | .dest = ANC_ACTIVE, | |
460 | .analogue = 1, | |
461 | .step = { | |
462 | wm2000_exit_bypass, | |
463 | }, | |
464 | }, | |
465 | { | |
466 | .source = ANC_BYPASS, | |
467 | .dest = ANC_STANDBY, | |
468 | .analogue = 1, | |
469 | .step = { | |
470 | wm2000_exit_bypass, | |
471 | wm2000_enter_standby, | |
472 | }, | |
473 | }, | |
474 | { | |
475 | .source = ANC_BYPASS, | |
476 | .dest = ANC_OFF, | |
477 | .step = { | |
478 | wm2000_exit_bypass, | |
479 | wm2000_power_down, | |
480 | }, | |
481 | }, | |
482 | { | |
483 | .source = ANC_STANDBY, | |
484 | .dest = ANC_ACTIVE, | |
485 | .analogue = 1, | |
486 | .step = { | |
487 | wm2000_exit_standby, | |
488 | }, | |
489 | }, | |
490 | { | |
491 | .source = ANC_STANDBY, | |
492 | .dest = ANC_BYPASS, | |
493 | .analogue = 1, | |
494 | .step = { | |
495 | wm2000_exit_standby, | |
496 | wm2000_enter_bypass, | |
497 | }, | |
498 | }, | |
499 | { | |
500 | .source = ANC_STANDBY, | |
501 | .dest = ANC_OFF, | |
502 | .step = { | |
503 | wm2000_exit_standby, | |
504 | wm2000_power_down, | |
505 | }, | |
506 | }, | |
507 | }; | |
508 | ||
509 | static int wm2000_anc_transition(struct wm2000_priv *wm2000, | |
510 | enum wm2000_anc_mode mode) | |
511 | { | |
512 | struct i2c_client *i2c = wm2000->i2c; | |
513 | int i, j; | |
514 | int ret; | |
515 | ||
516 | if (wm2000->anc_mode == mode) | |
517 | return 0; | |
518 | ||
519 | for (i = 0; i < ARRAY_SIZE(anc_transitions); i++) | |
520 | if (anc_transitions[i].source == wm2000->anc_mode && | |
521 | anc_transitions[i].dest == mode) | |
522 | break; | |
523 | if (i == ARRAY_SIZE(anc_transitions)) { | |
524 | dev_err(&i2c->dev, "No transition for %d->%d\n", | |
525 | wm2000->anc_mode, mode); | |
526 | return -EINVAL; | |
527 | } | |
528 | ||
529 | for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) { | |
530 | if (!anc_transitions[i].step[j]) | |
531 | break; | |
532 | ret = anc_transitions[i].step[j](i2c, | |
533 | anc_transitions[i].analogue); | |
534 | if (ret != 0) | |
535 | return ret; | |
536 | } | |
537 | ||
538 | return 0; | |
539 | } | |
540 | ||
541 | static int wm2000_anc_set_mode(struct wm2000_priv *wm2000) | |
542 | { | |
543 | struct i2c_client *i2c = wm2000->i2c; | |
544 | enum wm2000_anc_mode mode; | |
545 | ||
546 | if (wm2000->anc_eng_ena && wm2000->spk_ena) | |
547 | if (wm2000->anc_active) | |
548 | mode = ANC_ACTIVE; | |
549 | else | |
550 | mode = ANC_BYPASS; | |
551 | else | |
552 | mode = ANC_STANDBY; | |
553 | ||
554 | dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n", | |
555 | mode, wm2000->anc_eng_ena, !wm2000->spk_ena, | |
556 | wm2000->anc_active); | |
557 | ||
558 | return wm2000_anc_transition(wm2000, mode); | |
559 | } | |
560 | ||
561 | static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol, | |
562 | struct snd_ctl_elem_value *ucontrol) | |
563 | { | |
4911ccdb MB |
564 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
565 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
3a66d387 MB |
566 | |
567 | ucontrol->value.enumerated.item[0] = wm2000->anc_active; | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
572 | static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol, | |
573 | struct snd_ctl_elem_value *ucontrol) | |
574 | { | |
4911ccdb MB |
575 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
576 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
3a66d387 MB |
577 | int anc_active = ucontrol->value.enumerated.item[0]; |
578 | ||
579 | if (anc_active > 1) | |
580 | return -EINVAL; | |
581 | ||
582 | wm2000->anc_active = anc_active; | |
583 | ||
584 | return wm2000_anc_set_mode(wm2000); | |
585 | } | |
586 | ||
587 | static int wm2000_speaker_get(struct snd_kcontrol *kcontrol, | |
588 | struct snd_ctl_elem_value *ucontrol) | |
589 | { | |
4911ccdb MB |
590 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
591 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
3a66d387 MB |
592 | |
593 | ucontrol->value.enumerated.item[0] = wm2000->spk_ena; | |
594 | ||
595 | return 0; | |
596 | } | |
597 | ||
598 | static int wm2000_speaker_put(struct snd_kcontrol *kcontrol, | |
599 | struct snd_ctl_elem_value *ucontrol) | |
600 | { | |
4911ccdb MB |
601 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
602 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
3a66d387 MB |
603 | int val = ucontrol->value.enumerated.item[0]; |
604 | ||
605 | if (val > 1) | |
606 | return -EINVAL; | |
607 | ||
608 | wm2000->spk_ena = val; | |
609 | ||
610 | return wm2000_anc_set_mode(wm2000); | |
611 | } | |
612 | ||
613 | static const struct snd_kcontrol_new wm2000_controls[] = { | |
614 | SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0, | |
615 | wm2000_anc_mode_get, | |
616 | wm2000_anc_mode_put), | |
617 | SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0, | |
618 | wm2000_speaker_get, | |
619 | wm2000_speaker_put), | |
620 | }; | |
621 | ||
622 | static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w, | |
623 | struct snd_kcontrol *kcontrol, int event) | |
624 | { | |
4911ccdb MB |
625 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
626 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
3a66d387 MB |
627 | |
628 | if (SND_SOC_DAPM_EVENT_ON(event)) | |
629 | wm2000->anc_eng_ena = 1; | |
630 | ||
631 | if (SND_SOC_DAPM_EVENT_OFF(event)) | |
632 | wm2000->anc_eng_ena = 0; | |
633 | ||
634 | return wm2000_anc_set_mode(wm2000); | |
635 | } | |
636 | ||
637 | static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = { | |
638 | /* Externally visible pins */ | |
4911ccdb MB |
639 | SND_SOC_DAPM_OUTPUT("SPKN"), |
640 | SND_SOC_DAPM_OUTPUT("SPKP"), | |
3a66d387 | 641 | |
4911ccdb MB |
642 | SND_SOC_DAPM_INPUT("LINN"), |
643 | SND_SOC_DAPM_INPUT("LINP"), | |
3a66d387 MB |
644 | |
645 | SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0, | |
646 | wm2000_anc_power_event, | |
647 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), | |
648 | }; | |
649 | ||
650 | /* Target, Path, Source */ | |
4911ccdb MB |
651 | static const struct snd_soc_dapm_route wm2000_audio_map[] = { |
652 | { "SPKN", NULL, "ANC Engine" }, | |
653 | { "SPKP", NULL, "ANC Engine" }, | |
654 | { "ANC Engine", NULL, "LINN" }, | |
655 | { "ANC Engine", NULL, "LINP" }, | |
3a66d387 MB |
656 | }; |
657 | ||
4911ccdb MB |
658 | #ifdef CONFIG_PM |
659 | static int wm2000_suspend(struct snd_soc_codec *codec) | |
3a66d387 | 660 | { |
4911ccdb | 661 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); |
3a66d387 | 662 | |
4911ccdb MB |
663 | return wm2000_anc_transition(wm2000, ANC_OFF); |
664 | } | |
3a66d387 | 665 | |
4911ccdb MB |
666 | static int wm2000_resume(struct snd_soc_codec *codec) |
667 | { | |
668 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
3a66d387 | 669 | |
4911ccdb | 670 | return wm2000_anc_set_mode(wm2000); |
3a66d387 | 671 | } |
4911ccdb MB |
672 | #else |
673 | #define wm2000_suspend NULL | |
674 | #define wm2000_resume NULL | |
675 | #endif | |
3a66d387 | 676 | |
51cc7ed3 MB |
677 | static bool wm2000_readable_reg(struct device *dev, unsigned int reg) |
678 | { | |
679 | switch (reg) { | |
680 | case WM2000_REG_SYS_START: | |
681 | case WM2000_REG_SPEECH_CLARITY: | |
682 | case WM2000_REG_SYS_WATCHDOG: | |
683 | case WM2000_REG_ANA_VMID_PD_TIME: | |
684 | case WM2000_REG_ANA_VMID_PU_TIME: | |
685 | case WM2000_REG_CAT_FLTR_INDX: | |
686 | case WM2000_REG_CAT_GAIN_0: | |
687 | case WM2000_REG_SYS_STATUS: | |
688 | case WM2000_REG_SYS_MODE_CNTRL: | |
689 | case WM2000_REG_SYS_START0: | |
690 | case WM2000_REG_SYS_START1: | |
691 | case WM2000_REG_ID1: | |
692 | case WM2000_REG_ID2: | |
693 | case WM2000_REG_REVISON: | |
694 | case WM2000_REG_SYS_CTL1: | |
695 | case WM2000_REG_SYS_CTL2: | |
696 | case WM2000_REG_ANC_STAT: | |
697 | case WM2000_REG_IF_CTL: | |
698 | return true; | |
699 | default: | |
700 | return false; | |
701 | } | |
702 | } | |
703 | ||
8aa1fe81 MB |
704 | static const struct regmap_config wm2000_regmap = { |
705 | .reg_bits = 8, | |
706 | .val_bits = 8, | |
51cc7ed3 MB |
707 | |
708 | .max_register = WM2000_REG_IF_CTL, | |
709 | .readable_reg = wm2000_readable_reg, | |
8aa1fe81 MB |
710 | }; |
711 | ||
4911ccdb MB |
712 | static int wm2000_probe(struct snd_soc_codec *codec) |
713 | { | |
714 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
715 | ||
716 | /* This will trigger a transition to standby mode by default */ | |
717 | wm2000_anc_set_mode(wm2000); | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
722 | static int wm2000_remove(struct snd_soc_codec *codec) | |
723 | { | |
724 | struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev); | |
725 | ||
726 | return wm2000_anc_transition(wm2000, ANC_OFF); | |
727 | } | |
728 | ||
729 | static struct snd_soc_codec_driver soc_codec_dev_wm2000 = { | |
730 | .probe = wm2000_probe, | |
731 | .remove = wm2000_remove, | |
732 | .suspend = wm2000_suspend, | |
733 | .resume = wm2000_resume, | |
734 | ||
735 | .dapm_widgets = wm2000_dapm_widgets, | |
736 | .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets), | |
737 | .dapm_routes = wm2000_audio_map, | |
738 | .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map), | |
739 | .controls = wm2000_controls, | |
740 | .num_controls = ARRAY_SIZE(wm2000_controls), | |
741 | }; | |
742 | ||
3a66d387 MB |
743 | static int __devinit wm2000_i2c_probe(struct i2c_client *i2c, |
744 | const struct i2c_device_id *i2c_id) | |
745 | { | |
746 | struct wm2000_priv *wm2000; | |
747 | struct wm2000_platform_data *pdata; | |
748 | const char *filename; | |
c83f1d7e JJ |
749 | const struct firmware *fw = NULL; |
750 | int ret; | |
751 | int reg; | |
3a66d387 MB |
752 | u16 id; |
753 | ||
b03e96e4 MB |
754 | wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv), |
755 | GFP_KERNEL); | |
3a66d387 MB |
756 | if (wm2000 == NULL) { |
757 | dev_err(&i2c->dev, "Unable to allocate private data\n"); | |
758 | return -ENOMEM; | |
759 | } | |
760 | ||
8aa1fe81 MB |
761 | dev_set_drvdata(&i2c->dev, wm2000); |
762 | ||
763 | wm2000->regmap = regmap_init_i2c(i2c, &wm2000_regmap); | |
764 | if (IS_ERR(wm2000->regmap)) { | |
765 | ret = PTR_ERR(wm2000->regmap); | |
766 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", | |
767 | ret); | |
c83f1d7e | 768 | goto out; |
8aa1fe81 MB |
769 | } |
770 | ||
3a66d387 MB |
771 | /* Verify that this is a WM2000 */ |
772 | reg = wm2000_read(i2c, WM2000_REG_ID1); | |
773 | id = reg << 8; | |
774 | reg = wm2000_read(i2c, WM2000_REG_ID2); | |
775 | id |= reg & 0xff; | |
776 | ||
777 | if (id != 0x2000) { | |
778 | dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id); | |
779 | ret = -ENODEV; | |
c83f1d7e | 780 | goto out_regmap_exit; |
3a66d387 MB |
781 | } |
782 | ||
783 | reg = wm2000_read(i2c, WM2000_REG_REVISON); | |
784 | dev_info(&i2c->dev, "revision %c\n", reg + 'A'); | |
785 | ||
786 | filename = "wm2000_anc.bin"; | |
787 | pdata = dev_get_platdata(&i2c->dev); | |
788 | if (pdata) { | |
789 | wm2000->mclk_div = pdata->mclkdiv2; | |
790 | wm2000->speech_clarity = !pdata->speech_enh_disable; | |
791 | ||
792 | if (pdata->download_file) | |
793 | filename = pdata->download_file; | |
794 | } | |
795 | ||
796 | ret = request_firmware(&fw, filename, &i2c->dev); | |
797 | if (ret != 0) { | |
798 | dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret); | |
c83f1d7e | 799 | goto out_regmap_exit; |
3a66d387 MB |
800 | } |
801 | ||
802 | /* Pre-cook the concatenation of the register address onto the image */ | |
803 | wm2000->anc_download_size = fw->size + 2; | |
b03e96e4 MB |
804 | wm2000->anc_download = devm_kzalloc(&i2c->dev, |
805 | wm2000->anc_download_size, | |
806 | GFP_KERNEL); | |
3a66d387 MB |
807 | if (wm2000->anc_download == NULL) { |
808 | dev_err(&i2c->dev, "Out of memory\n"); | |
809 | ret = -ENOMEM; | |
c83f1d7e | 810 | goto out_regmap_exit; |
3a66d387 MB |
811 | } |
812 | ||
813 | wm2000->anc_download[0] = 0x80; | |
814 | wm2000->anc_download[1] = 0x00; | |
815 | memcpy(wm2000->anc_download + 2, fw->data, fw->size); | |
816 | ||
3a66d387 | 817 | wm2000->anc_eng_ena = 1; |
e71fa370 MB |
818 | wm2000->anc_active = 1; |
819 | wm2000->spk_ena = 1; | |
3a66d387 MB |
820 | wm2000->i2c = i2c; |
821 | ||
822 | wm2000_reset(wm2000); | |
823 | ||
c83f1d7e JJ |
824 | ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0); |
825 | if (!ret) | |
826 | goto out; | |
3a66d387 | 827 | |
c83f1d7e | 828 | out_regmap_exit: |
8aa1fe81 | 829 | regmap_exit(wm2000->regmap); |
c83f1d7e JJ |
830 | out: |
831 | release_firmware(fw); | |
3a66d387 MB |
832 | return ret; |
833 | } | |
834 | ||
835 | static __devexit int wm2000_i2c_remove(struct i2c_client *i2c) | |
836 | { | |
837 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); | |
838 | ||
4911ccdb | 839 | snd_soc_unregister_codec(&i2c->dev); |
8aa1fe81 MB |
840 | regmap_exit(wm2000->regmap); |
841 | ||
3a66d387 MB |
842 | return 0; |
843 | } | |
844 | ||
3a66d387 MB |
845 | static const struct i2c_device_id wm2000_i2c_id[] = { |
846 | { "wm2000", 0 }, | |
847 | { } | |
848 | }; | |
849 | MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id); | |
850 | ||
851 | static struct i2c_driver wm2000_i2c_driver = { | |
852 | .driver = { | |
853 | .name = "wm2000", | |
854 | .owner = THIS_MODULE, | |
855 | }, | |
856 | .probe = wm2000_i2c_probe, | |
857 | .remove = __devexit_p(wm2000_i2c_remove), | |
3a66d387 MB |
858 | .id_table = wm2000_i2c_id, |
859 | }; | |
860 | ||
3a4bfd88 | 861 | module_i2c_driver(wm2000_i2c_driver); |
3a66d387 MB |
862 | |
863 | MODULE_DESCRIPTION("ASoC WM2000 driver"); | |
864 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>"); | |
865 | MODULE_LICENSE("GPL"); |