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