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