Commit | Line | Data |
---|---|---|
9e501086 MB |
1 | /* |
2 | * wm8994-core.c -- Device access for Wolfson WM8994 | |
3 | * | |
4 | * Copyright 2009 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 it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <linux/kernel.h> | |
16 | #include <linux/module.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
9e501086 MB |
18 | #include <linux/i2c.h> |
19 | #include <linux/delay.h> | |
20 | #include <linux/mfd/core.h> | |
d450f19e | 21 | #include <linux/pm_runtime.h> |
9e501086 MB |
22 | #include <linux/regulator/consumer.h> |
23 | #include <linux/regulator/machine.h> | |
24 | ||
25 | #include <linux/mfd/wm8994/core.h> | |
26 | #include <linux/mfd/wm8994/pdata.h> | |
27 | #include <linux/mfd/wm8994/registers.h> | |
28 | ||
29 | static int wm8994_read(struct wm8994 *wm8994, unsigned short reg, | |
30 | int bytes, void *dest) | |
31 | { | |
32 | int ret, i; | |
33 | u16 *buf = dest; | |
34 | ||
35 | BUG_ON(bytes % 2); | |
36 | BUG_ON(bytes <= 0); | |
37 | ||
38 | ret = wm8994->read_dev(wm8994, reg, bytes, dest); | |
39 | if (ret < 0) | |
40 | return ret; | |
41 | ||
42 | for (i = 0; i < bytes / 2; i++) { | |
43 | buf[i] = be16_to_cpu(buf[i]); | |
44 | ||
45 | dev_vdbg(wm8994->dev, "Read %04x from R%d(0x%x)\n", | |
46 | buf[i], reg + i, reg + i); | |
47 | } | |
48 | ||
49 | return 0; | |
50 | } | |
51 | ||
52 | /** | |
53 | * wm8994_reg_read: Read a single WM8994 register. | |
54 | * | |
55 | * @wm8994: Device to read from. | |
56 | * @reg: Register to read. | |
57 | */ | |
58 | int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg) | |
59 | { | |
60 | unsigned short val; | |
61 | int ret; | |
62 | ||
63 | mutex_lock(&wm8994->io_lock); | |
64 | ||
65 | ret = wm8994_read(wm8994, reg, 2, &val); | |
66 | ||
67 | mutex_unlock(&wm8994->io_lock); | |
68 | ||
69 | if (ret < 0) | |
70 | return ret; | |
71 | else | |
72 | return val; | |
73 | } | |
74 | EXPORT_SYMBOL_GPL(wm8994_reg_read); | |
75 | ||
76 | /** | |
77 | * wm8994_bulk_read: Read multiple WM8994 registers | |
78 | * | |
79 | * @wm8994: Device to read from | |
80 | * @reg: First register | |
81 | * @count: Number of registers | |
82 | * @buf: Buffer to fill. | |
83 | */ | |
84 | int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg, | |
85 | int count, u16 *buf) | |
86 | { | |
87 | int ret; | |
88 | ||
89 | mutex_lock(&wm8994->io_lock); | |
90 | ||
91 | ret = wm8994_read(wm8994, reg, count * 2, buf); | |
92 | ||
93 | mutex_unlock(&wm8994->io_lock); | |
94 | ||
95 | return ret; | |
96 | } | |
97 | EXPORT_SYMBOL_GPL(wm8994_bulk_read); | |
98 | ||
99 | static int wm8994_write(struct wm8994 *wm8994, unsigned short reg, | |
100 | int bytes, void *src) | |
101 | { | |
102 | u16 *buf = src; | |
103 | int i; | |
104 | ||
105 | BUG_ON(bytes % 2); | |
106 | BUG_ON(bytes <= 0); | |
107 | ||
108 | for (i = 0; i < bytes / 2; i++) { | |
109 | dev_vdbg(wm8994->dev, "Write %04x to R%d(0x%x)\n", | |
110 | buf[i], reg + i, reg + i); | |
111 | ||
112 | buf[i] = cpu_to_be16(buf[i]); | |
113 | } | |
114 | ||
115 | return wm8994->write_dev(wm8994, reg, bytes, src); | |
116 | } | |
117 | ||
118 | /** | |
119 | * wm8994_reg_write: Write a single WM8994 register. | |
120 | * | |
121 | * @wm8994: Device to write to. | |
122 | * @reg: Register to write to. | |
123 | * @val: Value to write. | |
124 | */ | |
125 | int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg, | |
126 | unsigned short val) | |
127 | { | |
128 | int ret; | |
129 | ||
130 | mutex_lock(&wm8994->io_lock); | |
131 | ||
132 | ret = wm8994_write(wm8994, reg, 2, &val); | |
133 | ||
134 | mutex_unlock(&wm8994->io_lock); | |
135 | ||
136 | return ret; | |
137 | } | |
138 | EXPORT_SYMBOL_GPL(wm8994_reg_write); | |
139 | ||
e93c5387 MB |
140 | /** |
141 | * wm8994_bulk_write: Write multiple WM8994 registers | |
142 | * | |
143 | * @wm8994: Device to write to | |
144 | * @reg: First register | |
145 | * @count: Number of registers | |
146 | * @buf: Buffer to write from. | |
147 | */ | |
148 | int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg, | |
149 | int count, u16 *buf) | |
150 | { | |
151 | int ret; | |
152 | ||
153 | mutex_lock(&wm8994->io_lock); | |
154 | ||
155 | ret = wm8994_write(wm8994, reg, count * 2, buf); | |
156 | ||
157 | mutex_unlock(&wm8994->io_lock); | |
158 | ||
159 | return ret; | |
160 | } | |
161 | EXPORT_SYMBOL_GPL(wm8994_bulk_write); | |
162 | ||
9e501086 MB |
163 | /** |
164 | * wm8994_set_bits: Set the value of a bitfield in a WM8994 register | |
165 | * | |
166 | * @wm8994: Device to write to. | |
167 | * @reg: Register to write to. | |
168 | * @mask: Mask of bits to set. | |
169 | * @val: Value to set (unshifted) | |
170 | */ | |
171 | int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg, | |
172 | unsigned short mask, unsigned short val) | |
173 | { | |
174 | int ret; | |
175 | u16 r; | |
176 | ||
177 | mutex_lock(&wm8994->io_lock); | |
178 | ||
179 | ret = wm8994_read(wm8994, reg, 2, &r); | |
180 | if (ret < 0) | |
181 | goto out; | |
182 | ||
183 | r &= ~mask; | |
184 | r |= val; | |
185 | ||
186 | ret = wm8994_write(wm8994, reg, 2, &r); | |
187 | ||
188 | out: | |
189 | mutex_unlock(&wm8994->io_lock); | |
190 | ||
191 | return ret; | |
192 | } | |
193 | EXPORT_SYMBOL_GPL(wm8994_set_bits); | |
194 | ||
195 | static struct mfd_cell wm8994_regulator_devs[] = { | |
d450f19e MB |
196 | { |
197 | .name = "wm8994-ldo", | |
198 | .id = 1, | |
199 | .pm_runtime_no_callbacks = true, | |
200 | }, | |
201 | { | |
202 | .name = "wm8994-ldo", | |
203 | .id = 2, | |
204 | .pm_runtime_no_callbacks = true, | |
205 | }, | |
9e501086 MB |
206 | }; |
207 | ||
c9fbf7e0 MB |
208 | static struct resource wm8994_codec_resources[] = { |
209 | { | |
210 | .start = WM8994_IRQ_TEMP_SHUT, | |
211 | .end = WM8994_IRQ_TEMP_WARN, | |
212 | .flags = IORESOURCE_IRQ, | |
213 | }, | |
214 | }; | |
215 | ||
216 | static struct resource wm8994_gpio_resources[] = { | |
217 | { | |
218 | .start = WM8994_IRQ_GPIO(1), | |
219 | .end = WM8994_IRQ_GPIO(11), | |
220 | .flags = IORESOURCE_IRQ, | |
221 | }, | |
222 | }; | |
223 | ||
9e501086 | 224 | static struct mfd_cell wm8994_devs[] = { |
c9fbf7e0 MB |
225 | { |
226 | .name = "wm8994-codec", | |
227 | .num_resources = ARRAY_SIZE(wm8994_codec_resources), | |
228 | .resources = wm8994_codec_resources, | |
229 | }, | |
230 | ||
231 | { | |
232 | .name = "wm8994-gpio", | |
233 | .num_resources = ARRAY_SIZE(wm8994_gpio_resources), | |
234 | .resources = wm8994_gpio_resources, | |
d450f19e | 235 | .pm_runtime_no_callbacks = true, |
c9fbf7e0 | 236 | }, |
9e501086 MB |
237 | }; |
238 | ||
239 | /* | |
240 | * Supplies for the main bulk of CODEC; the LDO supplies are ignored | |
241 | * and should be handled via the standard regulator API supply | |
242 | * management. | |
243 | */ | |
244 | static const char *wm8994_main_supplies[] = { | |
245 | "DBVDD", | |
246 | "DCVDD", | |
247 | "AVDD1", | |
248 | "AVDD2", | |
249 | "CPVDD", | |
250 | "SPKVDD1", | |
251 | "SPKVDD2", | |
252 | }; | |
253 | ||
559e0df6 MB |
254 | static const char *wm8958_main_supplies[] = { |
255 | "DBVDD1", | |
256 | "DBVDD2", | |
257 | "DBVDD3", | |
258 | "DCVDD", | |
259 | "AVDD1", | |
260 | "AVDD2", | |
261 | "CPVDD", | |
262 | "SPKVDD1", | |
263 | "SPKVDD2", | |
264 | }; | |
265 | ||
9e501086 | 266 | #ifdef CONFIG_PM |
d450f19e | 267 | static int wm8994_suspend(struct device *dev) |
9e501086 MB |
268 | { |
269 | struct wm8994 *wm8994 = dev_get_drvdata(dev); | |
270 | int ret; | |
271 | ||
77bd70e9 MB |
272 | /* Don't actually go through with the suspend if the CODEC is |
273 | * still active (eg, for audio passthrough from CP. */ | |
274 | ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_1); | |
275 | if (ret < 0) { | |
276 | dev_err(dev, "Failed to read power status: %d\n", ret); | |
277 | } else if (ret & WM8994_VMID_SEL_MASK) { | |
278 | dev_dbg(dev, "CODEC still active, ignoring suspend\n"); | |
279 | return 0; | |
280 | } | |
281 | ||
9e501086 MB |
282 | /* GPIO configuration state is saved here since we may be configuring |
283 | * the GPIO alternate functions even if we're not using the gpiolib | |
284 | * driver for them. | |
285 | */ | |
286 | ret = wm8994_read(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2, | |
287 | &wm8994->gpio_regs); | |
288 | if (ret < 0) | |
289 | dev_err(dev, "Failed to save GPIO registers: %d\n", ret); | |
290 | ||
291 | /* For similar reasons we also stash the regulator states */ | |
292 | ret = wm8994_read(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2, | |
293 | &wm8994->ldo_regs); | |
294 | if (ret < 0) | |
295 | dev_err(dev, "Failed to save LDO registers: %d\n", ret); | |
296 | ||
f40dff9e MB |
297 | /* Explicitly put the device into reset in case regulators |
298 | * don't get disabled in order to ensure consistent restart. | |
299 | */ | |
300 | wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET, 0x8994); | |
301 | ||
77bd70e9 MB |
302 | wm8994->suspended = true; |
303 | ||
559e0df6 | 304 | ret = regulator_bulk_disable(wm8994->num_supplies, |
9e501086 MB |
305 | wm8994->supplies); |
306 | if (ret != 0) { | |
307 | dev_err(dev, "Failed to disable supplies: %d\n", ret); | |
308 | return ret; | |
309 | } | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
d450f19e | 314 | static int wm8994_resume(struct device *dev) |
9e501086 MB |
315 | { |
316 | struct wm8994 *wm8994 = dev_get_drvdata(dev); | |
317 | int ret; | |
318 | ||
77bd70e9 MB |
319 | /* We may have lied to the PM core about suspending */ |
320 | if (!wm8994->suspended) | |
321 | return 0; | |
322 | ||
559e0df6 | 323 | ret = regulator_bulk_enable(wm8994->num_supplies, |
9e501086 MB |
324 | wm8994->supplies); |
325 | if (ret != 0) { | |
326 | dev_err(dev, "Failed to enable supplies: %d\n", ret); | |
327 | return ret; | |
328 | } | |
329 | ||
c9fbf7e0 MB |
330 | ret = wm8994_write(wm8994, WM8994_INTERRUPT_STATUS_1_MASK, |
331 | WM8994_NUM_IRQ_REGS * 2, &wm8994->irq_masks_cur); | |
332 | if (ret < 0) | |
333 | dev_err(dev, "Failed to restore interrupt masks: %d\n", ret); | |
334 | ||
9e501086 MB |
335 | ret = wm8994_write(wm8994, WM8994_LDO_1, WM8994_NUM_LDO_REGS * 2, |
336 | &wm8994->ldo_regs); | |
337 | if (ret < 0) | |
338 | dev_err(dev, "Failed to restore LDO registers: %d\n", ret); | |
339 | ||
340 | ret = wm8994_write(wm8994, WM8994_GPIO_1, WM8994_NUM_GPIO_REGS * 2, | |
341 | &wm8994->gpio_regs); | |
342 | if (ret < 0) | |
343 | dev_err(dev, "Failed to restore GPIO registers: %d\n", ret); | |
344 | ||
77bd70e9 MB |
345 | wm8994->suspended = false; |
346 | ||
9e501086 MB |
347 | return 0; |
348 | } | |
349 | #endif | |
350 | ||
351 | #ifdef CONFIG_REGULATOR | |
352 | static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo) | |
353 | { | |
354 | struct wm8994_ldo_pdata *ldo_pdata; | |
355 | ||
356 | if (!pdata) | |
357 | return 0; | |
358 | ||
359 | ldo_pdata = &pdata->ldo[ldo]; | |
360 | ||
361 | if (!ldo_pdata->init_data) | |
362 | return 0; | |
363 | ||
364 | return ldo_pdata->init_data->num_consumer_supplies != 0; | |
365 | } | |
366 | #else | |
367 | static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo) | |
368 | { | |
369 | return 0; | |
370 | } | |
371 | #endif | |
372 | ||
373 | /* | |
374 | * Instantiate the generic non-control parts of the device. | |
375 | */ | |
559e0df6 | 376 | static int wm8994_device_init(struct wm8994 *wm8994, int irq) |
9e501086 MB |
377 | { |
378 | struct wm8994_pdata *pdata = wm8994->dev->platform_data; | |
559e0df6 | 379 | const char *devname; |
9e501086 MB |
380 | int ret, i; |
381 | ||
382 | mutex_init(&wm8994->io_lock); | |
383 | dev_set_drvdata(wm8994->dev, wm8994); | |
384 | ||
385 | /* Add the on-chip regulators first for bootstrapping */ | |
386 | ret = mfd_add_devices(wm8994->dev, -1, | |
387 | wm8994_regulator_devs, | |
388 | ARRAY_SIZE(wm8994_regulator_devs), | |
389 | NULL, 0); | |
390 | if (ret != 0) { | |
391 | dev_err(wm8994->dev, "Failed to add children: %d\n", ret); | |
392 | goto err; | |
393 | } | |
394 | ||
559e0df6 MB |
395 | switch (wm8994->type) { |
396 | case WM8994: | |
397 | wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies); | |
398 | break; | |
399 | case WM8958: | |
400 | wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies); | |
401 | break; | |
402 | default: | |
403 | BUG(); | |
404 | return -EINVAL; | |
405 | } | |
406 | ||
9e501086 | 407 | wm8994->supplies = kzalloc(sizeof(struct regulator_bulk_data) * |
559e0df6 | 408 | wm8994->num_supplies, |
9e501086 | 409 | GFP_KERNEL); |
fccbd21f AL |
410 | if (!wm8994->supplies) { |
411 | ret = -ENOMEM; | |
9e501086 | 412 | goto err; |
fccbd21f | 413 | } |
9e501086 | 414 | |
559e0df6 MB |
415 | switch (wm8994->type) { |
416 | case WM8994: | |
417 | for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++) | |
418 | wm8994->supplies[i].supply = wm8994_main_supplies[i]; | |
419 | break; | |
420 | case WM8958: | |
421 | for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++) | |
422 | wm8994->supplies[i].supply = wm8958_main_supplies[i]; | |
423 | break; | |
424 | default: | |
425 | BUG(); | |
426 | return -EINVAL; | |
427 | } | |
428 | ||
429 | ret = regulator_bulk_get(wm8994->dev, wm8994->num_supplies, | |
9e501086 MB |
430 | wm8994->supplies); |
431 | if (ret != 0) { | |
432 | dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret); | |
7731074a | 433 | goto err_supplies; |
9e501086 MB |
434 | } |
435 | ||
559e0df6 | 436 | ret = regulator_bulk_enable(wm8994->num_supplies, |
9e501086 MB |
437 | wm8994->supplies); |
438 | if (ret != 0) { | |
439 | dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret); | |
7731074a | 440 | goto err_get; |
9e501086 MB |
441 | } |
442 | ||
443 | ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET); | |
444 | if (ret < 0) { | |
445 | dev_err(wm8994->dev, "Failed to read ID register\n"); | |
446 | goto err_enable; | |
447 | } | |
559e0df6 MB |
448 | switch (ret) { |
449 | case 0x8994: | |
450 | devname = "WM8994"; | |
451 | if (wm8994->type != WM8994) | |
452 | dev_warn(wm8994->dev, "Device registered as type %d\n", | |
453 | wm8994->type); | |
454 | wm8994->type = WM8994; | |
455 | break; | |
456 | case 0x8958: | |
457 | devname = "WM8958"; | |
458 | if (wm8994->type != WM8958) | |
459 | dev_warn(wm8994->dev, "Device registered as type %d\n", | |
460 | wm8994->type); | |
461 | wm8994->type = WM8958; | |
462 | break; | |
463 | default: | |
9e501086 MB |
464 | dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n", |
465 | ret); | |
466 | ret = -EINVAL; | |
467 | goto err_enable; | |
468 | } | |
469 | ||
470 | ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION); | |
471 | if (ret < 0) { | |
472 | dev_err(wm8994->dev, "Failed to read revision register: %d\n", | |
473 | ret); | |
474 | goto err_enable; | |
475 | } | |
476 | ||
477 | switch (ret) { | |
478 | case 0: | |
479 | case 1: | |
559e0df6 MB |
480 | if (wm8994->type == WM8994) |
481 | dev_warn(wm8994->dev, | |
482 | "revision %c not fully supported\n", | |
483 | 'A' + ret); | |
9e501086 MB |
484 | break; |
485 | default: | |
9e501086 MB |
486 | break; |
487 | } | |
488 | ||
559e0df6 | 489 | dev_info(wm8994->dev, "%s revision %c\n", devname, 'A' + ret); |
9e501086 MB |
490 | |
491 | if (pdata) { | |
c9fbf7e0 | 492 | wm8994->irq_base = pdata->irq_base; |
9e501086 MB |
493 | wm8994->gpio_base = pdata->gpio_base; |
494 | ||
495 | /* GPIO configuration is only applied if it's non-zero */ | |
496 | for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) { | |
497 | if (pdata->gpio_defaults[i]) { | |
498 | wm8994_set_bits(wm8994, WM8994_GPIO_1 + i, | |
499 | 0xffff, | |
500 | pdata->gpio_defaults[i]); | |
501 | } | |
502 | } | |
503 | } | |
504 | ||
505 | /* In some system designs where the regulators are not in use, | |
506 | * we can achieve a small reduction in leakage currents by | |
507 | * floating LDO outputs. This bit makes no difference if the | |
508 | * LDOs are enabled, it only affects cases where the LDOs were | |
509 | * in operation and are then disabled. | |
510 | */ | |
511 | for (i = 0; i < WM8994_NUM_LDO_REGS; i++) { | |
512 | if (wm8994_ldo_in_use(pdata, i)) | |
513 | wm8994_set_bits(wm8994, WM8994_LDO_1 + i, | |
514 | WM8994_LDO1_DISCH, WM8994_LDO1_DISCH); | |
515 | else | |
516 | wm8994_set_bits(wm8994, WM8994_LDO_1 + i, | |
517 | WM8994_LDO1_DISCH, 0); | |
518 | } | |
519 | ||
c9fbf7e0 MB |
520 | wm8994_irq_init(wm8994); |
521 | ||
9e501086 MB |
522 | ret = mfd_add_devices(wm8994->dev, -1, |
523 | wm8994_devs, ARRAY_SIZE(wm8994_devs), | |
524 | NULL, 0); | |
525 | if (ret != 0) { | |
526 | dev_err(wm8994->dev, "Failed to add children: %d\n", ret); | |
c9fbf7e0 | 527 | goto err_irq; |
9e501086 MB |
528 | } |
529 | ||
d450f19e MB |
530 | pm_runtime_enable(wm8994->dev); |
531 | pm_runtime_resume(wm8994->dev); | |
532 | ||
9e501086 MB |
533 | return 0; |
534 | ||
c9fbf7e0 MB |
535 | err_irq: |
536 | wm8994_irq_exit(wm8994); | |
9e501086 | 537 | err_enable: |
559e0df6 | 538 | regulator_bulk_disable(wm8994->num_supplies, |
9e501086 MB |
539 | wm8994->supplies); |
540 | err_get: | |
559e0df6 | 541 | regulator_bulk_free(wm8994->num_supplies, wm8994->supplies); |
9e501086 MB |
542 | err_supplies: |
543 | kfree(wm8994->supplies); | |
544 | err: | |
545 | mfd_remove_devices(wm8994->dev); | |
546 | kfree(wm8994); | |
547 | return ret; | |
548 | } | |
549 | ||
550 | static void wm8994_device_exit(struct wm8994 *wm8994) | |
551 | { | |
d450f19e | 552 | pm_runtime_disable(wm8994->dev); |
9e501086 | 553 | mfd_remove_devices(wm8994->dev); |
c9fbf7e0 | 554 | wm8994_irq_exit(wm8994); |
559e0df6 | 555 | regulator_bulk_disable(wm8994->num_supplies, |
9e501086 | 556 | wm8994->supplies); |
559e0df6 | 557 | regulator_bulk_free(wm8994->num_supplies, wm8994->supplies); |
9e501086 MB |
558 | kfree(wm8994->supplies); |
559 | kfree(wm8994); | |
560 | } | |
561 | ||
562 | static int wm8994_i2c_read_device(struct wm8994 *wm8994, unsigned short reg, | |
563 | int bytes, void *dest) | |
564 | { | |
565 | struct i2c_client *i2c = wm8994->control_data; | |
566 | int ret; | |
567 | u16 r = cpu_to_be16(reg); | |
568 | ||
569 | ret = i2c_master_send(i2c, (unsigned char *)&r, 2); | |
570 | if (ret < 0) | |
571 | return ret; | |
572 | if (ret != 2) | |
573 | return -EIO; | |
574 | ||
575 | ret = i2c_master_recv(i2c, dest, bytes); | |
576 | if (ret < 0) | |
577 | return ret; | |
578 | if (ret != bytes) | |
579 | return -EIO; | |
580 | return 0; | |
581 | } | |
582 | ||
583 | /* Currently we allocate the write buffer on the stack; this is OK for | |
584 | * small writes - if we need to do large writes this will need to be | |
585 | * revised. | |
586 | */ | |
587 | static int wm8994_i2c_write_device(struct wm8994 *wm8994, unsigned short reg, | |
588 | int bytes, void *src) | |
589 | { | |
590 | struct i2c_client *i2c = wm8994->control_data; | |
591 | unsigned char msg[bytes + 2]; | |
592 | int ret; | |
593 | ||
594 | reg = cpu_to_be16(reg); | |
595 | memcpy(&msg[0], ®, 2); | |
596 | memcpy(&msg[2], src, bytes); | |
597 | ||
598 | ret = i2c_master_send(i2c, msg, bytes + 2); | |
599 | if (ret < 0) | |
600 | return ret; | |
601 | if (ret < bytes + 2) | |
602 | return -EIO; | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | static int wm8994_i2c_probe(struct i2c_client *i2c, | |
608 | const struct i2c_device_id *id) | |
609 | { | |
610 | struct wm8994 *wm8994; | |
611 | ||
612 | wm8994 = kzalloc(sizeof(struct wm8994), GFP_KERNEL); | |
d0a11693 | 613 | if (wm8994 == NULL) |
9e501086 | 614 | return -ENOMEM; |
9e501086 MB |
615 | |
616 | i2c_set_clientdata(i2c, wm8994); | |
617 | wm8994->dev = &i2c->dev; | |
618 | wm8994->control_data = i2c; | |
619 | wm8994->read_dev = wm8994_i2c_read_device; | |
620 | wm8994->write_dev = wm8994_i2c_write_device; | |
c9fbf7e0 | 621 | wm8994->irq = i2c->irq; |
559e0df6 | 622 | wm8994->type = id->driver_data; |
9e501086 | 623 | |
559e0df6 | 624 | return wm8994_device_init(wm8994, i2c->irq); |
9e501086 MB |
625 | } |
626 | ||
627 | static int wm8994_i2c_remove(struct i2c_client *i2c) | |
628 | { | |
629 | struct wm8994 *wm8994 = i2c_get_clientdata(i2c); | |
630 | ||
631 | wm8994_device_exit(wm8994); | |
632 | ||
633 | return 0; | |
634 | } | |
635 | ||
9e501086 | 636 | static const struct i2c_device_id wm8994_i2c_id[] = { |
559e0df6 MB |
637 | { "wm8994", WM8994 }, |
638 | { "wm8958", WM8958 }, | |
9e501086 MB |
639 | { } |
640 | }; | |
641 | MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id); | |
642 | ||
d450f19e MB |
643 | UNIVERSAL_DEV_PM_OPS(wm8994_pm_ops, wm8994_suspend, wm8994_resume, NULL); |
644 | ||
9e501086 MB |
645 | static struct i2c_driver wm8994_i2c_driver = { |
646 | .driver = { | |
d450f19e MB |
647 | .name = "wm8994", |
648 | .owner = THIS_MODULE, | |
649 | .pm = &wm8994_pm_ops, | |
9e501086 MB |
650 | }, |
651 | .probe = wm8994_i2c_probe, | |
652 | .remove = wm8994_i2c_remove, | |
9e501086 MB |
653 | .id_table = wm8994_i2c_id, |
654 | }; | |
655 | ||
656 | static int __init wm8994_i2c_init(void) | |
657 | { | |
658 | int ret; | |
659 | ||
660 | ret = i2c_add_driver(&wm8994_i2c_driver); | |
661 | if (ret != 0) | |
662 | pr_err("Failed to register wm8994 I2C driver: %d\n", ret); | |
663 | ||
664 | return ret; | |
665 | } | |
666 | module_init(wm8994_i2c_init); | |
667 | ||
668 | static void __exit wm8994_i2c_exit(void) | |
669 | { | |
670 | i2c_del_driver(&wm8994_i2c_driver); | |
671 | } | |
672 | module_exit(wm8994_i2c_exit); | |
673 | ||
674 | MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC"); | |
675 | MODULE_LICENSE("GPL"); | |
676 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |