Merge branch 'linus' into irq/core
[linux-2.6/kvm.git] / drivers / mfd / wm831x-core.c
bloba3d5728b64492e6ac3a0fabc1b8fa2fc1862aeb9
1 /*
2 * wm831x-core.c -- Device access for Wolfson WM831x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/bcd.h>
19 #include <linux/delay.h>
20 #include <linux/mfd/core.h>
21 #include <linux/slab.h>
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
25 #include <linux/mfd/wm831x/irq.h>
26 #include <linux/mfd/wm831x/auxadc.h>
27 #include <linux/mfd/wm831x/otp.h>
28 #include <linux/mfd/wm831x/regulator.h>
30 /* Current settings - values are 2*2^(reg_val/4) microamps. These are
31 * exported since they are used by multiple drivers.
33 int wm831x_isinkv_values[WM831X_ISINK_MAX_ISEL + 1] = {
43 10,
44 11,
45 13,
46 16,
47 19,
48 23,
49 27,
50 32,
51 38,
52 45,
53 54,
54 64,
55 76,
56 91,
57 108,
58 128,
59 152,
60 181,
61 215,
62 256,
63 304,
64 362,
65 431,
66 512,
67 609,
68 724,
69 861,
70 1024,
71 1218,
72 1448,
73 1722,
74 2048,
75 2435,
76 2896,
77 3444,
78 4096,
79 4871,
80 5793,
81 6889,
82 8192,
83 9742,
84 11585,
85 13777,
86 16384,
87 19484,
88 23170,
89 27554,
91 EXPORT_SYMBOL_GPL(wm831x_isinkv_values);
93 enum wm831x_parent {
94 WM8310 = 0x8310,
95 WM8311 = 0x8311,
96 WM8312 = 0x8312,
97 WM8320 = 0x8320,
100 static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
102 if (!wm831x->locked)
103 return 0;
105 switch (reg) {
106 case WM831X_WATCHDOG:
107 case WM831X_DC4_CONTROL:
108 case WM831X_ON_PIN_CONTROL:
109 case WM831X_BACKUP_CHARGER_CONTROL:
110 case WM831X_CHARGER_CONTROL_1:
111 case WM831X_CHARGER_CONTROL_2:
112 return 1;
114 default:
115 return 0;
120 * wm831x_reg_unlock: Unlock user keyed registers
122 * The WM831x has a user key preventing writes to particularly
123 * critical registers. This function locks those registers,
124 * allowing writes to them.
126 void wm831x_reg_lock(struct wm831x *wm831x)
128 int ret;
130 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
131 if (ret == 0) {
132 dev_vdbg(wm831x->dev, "Registers locked\n");
134 mutex_lock(&wm831x->io_lock);
135 WARN_ON(wm831x->locked);
136 wm831x->locked = 1;
137 mutex_unlock(&wm831x->io_lock);
138 } else {
139 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
143 EXPORT_SYMBOL_GPL(wm831x_reg_lock);
146 * wm831x_reg_unlock: Unlock user keyed registers
148 * The WM831x has a user key preventing writes to particularly
149 * critical registers. This function locks those registers,
150 * preventing spurious writes.
152 int wm831x_reg_unlock(struct wm831x *wm831x)
154 int ret;
156 /* 0x9716 is the value required to unlock the registers */
157 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
158 if (ret == 0) {
159 dev_vdbg(wm831x->dev, "Registers unlocked\n");
161 mutex_lock(&wm831x->io_lock);
162 WARN_ON(!wm831x->locked);
163 wm831x->locked = 0;
164 mutex_unlock(&wm831x->io_lock);
167 return ret;
169 EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
171 static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
172 int bytes, void *dest)
174 int ret, i;
175 u16 *buf = dest;
177 BUG_ON(bytes % 2);
178 BUG_ON(bytes <= 0);
180 ret = wm831x->read_dev(wm831x, reg, bytes, dest);
181 if (ret < 0)
182 return ret;
184 for (i = 0; i < bytes / 2; i++) {
185 buf[i] = be16_to_cpu(buf[i]);
187 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
188 buf[i], reg + i, reg + i);
191 return 0;
195 * wm831x_reg_read: Read a single WM831x register.
197 * @wm831x: Device to read from.
198 * @reg: Register to read.
200 int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
202 unsigned short val;
203 int ret;
205 mutex_lock(&wm831x->io_lock);
207 ret = wm831x_read(wm831x, reg, 2, &val);
209 mutex_unlock(&wm831x->io_lock);
211 if (ret < 0)
212 return ret;
213 else
214 return val;
216 EXPORT_SYMBOL_GPL(wm831x_reg_read);
219 * wm831x_bulk_read: Read multiple WM831x registers
221 * @wm831x: Device to read from
222 * @reg: First register
223 * @count: Number of registers
224 * @buf: Buffer to fill.
226 int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
227 int count, u16 *buf)
229 int ret;
231 mutex_lock(&wm831x->io_lock);
233 ret = wm831x_read(wm831x, reg, count * 2, buf);
235 mutex_unlock(&wm831x->io_lock);
237 return ret;
239 EXPORT_SYMBOL_GPL(wm831x_bulk_read);
241 static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
242 int bytes, void *src)
244 u16 *buf = src;
245 int i;
247 BUG_ON(bytes % 2);
248 BUG_ON(bytes <= 0);
250 for (i = 0; i < bytes / 2; i++) {
251 if (wm831x_reg_locked(wm831x, reg))
252 return -EPERM;
254 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
255 buf[i], reg + i, reg + i);
257 buf[i] = cpu_to_be16(buf[i]);
260 return wm831x->write_dev(wm831x, reg, bytes, src);
264 * wm831x_reg_write: Write a single WM831x register.
266 * @wm831x: Device to write to.
267 * @reg: Register to write to.
268 * @val: Value to write.
270 int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
271 unsigned short val)
273 int ret;
275 mutex_lock(&wm831x->io_lock);
277 ret = wm831x_write(wm831x, reg, 2, &val);
279 mutex_unlock(&wm831x->io_lock);
281 return ret;
283 EXPORT_SYMBOL_GPL(wm831x_reg_write);
286 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
288 * @wm831x: Device to write to.
289 * @reg: Register to write to.
290 * @mask: Mask of bits to set.
291 * @val: Value to set (unshifted)
293 int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
294 unsigned short mask, unsigned short val)
296 int ret;
297 u16 r;
299 mutex_lock(&wm831x->io_lock);
301 ret = wm831x_read(wm831x, reg, 2, &r);
302 if (ret < 0)
303 goto out;
305 r &= ~mask;
306 r |= val;
308 ret = wm831x_write(wm831x, reg, 2, &r);
310 out:
311 mutex_unlock(&wm831x->io_lock);
313 return ret;
315 EXPORT_SYMBOL_GPL(wm831x_set_bits);
318 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
320 * @wm831x: Device to read from.
321 * @input: AUXADC input to read.
323 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
325 int ret, src;
327 mutex_lock(&wm831x->auxadc_lock);
329 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
330 WM831X_AUX_ENA, WM831X_AUX_ENA);
331 if (ret < 0) {
332 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
333 goto out;
336 /* We force a single source at present */
337 src = input;
338 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
339 1 << src);
340 if (ret < 0) {
341 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
342 goto out;
345 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
346 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
347 if (ret < 0) {
348 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
349 goto disable;
352 /* Ignore the result to allow us to soldier on without IRQ hookup */
353 wait_for_completion_timeout(&wm831x->auxadc_done, msecs_to_jiffies(5));
355 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_CONTROL);
356 if (ret < 0) {
357 dev_err(wm831x->dev, "AUXADC status read failed: %d\n", ret);
358 goto disable;
361 if (ret & WM831X_AUX_CVT_ENA) {
362 dev_err(wm831x->dev, "Timed out reading AUXADC\n");
363 ret = -EBUSY;
364 goto disable;
367 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
368 if (ret < 0) {
369 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
370 } else {
371 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
372 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
374 if (src == 14)
375 src = WM831X_AUX_CAL;
377 if (src != input) {
378 dev_err(wm831x->dev, "Data from source %d not %d\n",
379 src, input);
380 ret = -EINVAL;
381 } else {
382 ret &= WM831X_AUX_DATA_MASK;
386 disable:
387 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
388 out:
389 mutex_unlock(&wm831x->auxadc_lock);
390 return ret;
392 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
394 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
396 struct wm831x *wm831x = irq_data;
398 complete(&wm831x->auxadc_done);
400 return IRQ_HANDLED;
404 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
406 * @wm831x: Device to read from.
407 * @input: AUXADC input to read.
409 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
411 int ret;
413 ret = wm831x_auxadc_read(wm831x, input);
414 if (ret < 0)
415 return ret;
417 ret *= 1465;
419 return ret;
421 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
423 static struct resource wm831x_dcdc1_resources[] = {
425 .start = WM831X_DC1_CONTROL_1,
426 .end = WM831X_DC1_DVS_CONTROL,
427 .flags = IORESOURCE_IO,
430 .name = "UV",
431 .start = WM831X_IRQ_UV_DC1,
432 .end = WM831X_IRQ_UV_DC1,
433 .flags = IORESOURCE_IRQ,
436 .name = "HC",
437 .start = WM831X_IRQ_HC_DC1,
438 .end = WM831X_IRQ_HC_DC1,
439 .flags = IORESOURCE_IRQ,
444 static struct resource wm831x_dcdc2_resources[] = {
446 .start = WM831X_DC2_CONTROL_1,
447 .end = WM831X_DC2_DVS_CONTROL,
448 .flags = IORESOURCE_IO,
451 .name = "UV",
452 .start = WM831X_IRQ_UV_DC2,
453 .end = WM831X_IRQ_UV_DC2,
454 .flags = IORESOURCE_IRQ,
457 .name = "HC",
458 .start = WM831X_IRQ_HC_DC2,
459 .end = WM831X_IRQ_HC_DC2,
460 .flags = IORESOURCE_IRQ,
464 static struct resource wm831x_dcdc3_resources[] = {
466 .start = WM831X_DC3_CONTROL_1,
467 .end = WM831X_DC3_SLEEP_CONTROL,
468 .flags = IORESOURCE_IO,
471 .name = "UV",
472 .start = WM831X_IRQ_UV_DC3,
473 .end = WM831X_IRQ_UV_DC3,
474 .flags = IORESOURCE_IRQ,
478 static struct resource wm831x_dcdc4_resources[] = {
480 .start = WM831X_DC4_CONTROL,
481 .end = WM831X_DC4_SLEEP_CONTROL,
482 .flags = IORESOURCE_IO,
485 .name = "UV",
486 .start = WM831X_IRQ_UV_DC4,
487 .end = WM831X_IRQ_UV_DC4,
488 .flags = IORESOURCE_IRQ,
492 static struct resource wm8320_dcdc4_buck_resources[] = {
494 .start = WM831X_DC4_CONTROL,
495 .end = WM832X_DC4_SLEEP_CONTROL,
496 .flags = IORESOURCE_IO,
499 .name = "UV",
500 .start = WM831X_IRQ_UV_DC4,
501 .end = WM831X_IRQ_UV_DC4,
502 .flags = IORESOURCE_IRQ,
506 static struct resource wm831x_gpio_resources[] = {
508 .start = WM831X_IRQ_GPIO_1,
509 .end = WM831X_IRQ_GPIO_16,
510 .flags = IORESOURCE_IRQ,
514 static struct resource wm831x_isink1_resources[] = {
516 .start = WM831X_CURRENT_SINK_1,
517 .end = WM831X_CURRENT_SINK_1,
518 .flags = IORESOURCE_IO,
521 .start = WM831X_IRQ_CS1,
522 .end = WM831X_IRQ_CS1,
523 .flags = IORESOURCE_IRQ,
527 static struct resource wm831x_isink2_resources[] = {
529 .start = WM831X_CURRENT_SINK_2,
530 .end = WM831X_CURRENT_SINK_2,
531 .flags = IORESOURCE_IO,
534 .start = WM831X_IRQ_CS2,
535 .end = WM831X_IRQ_CS2,
536 .flags = IORESOURCE_IRQ,
540 static struct resource wm831x_ldo1_resources[] = {
542 .start = WM831X_LDO1_CONTROL,
543 .end = WM831X_LDO1_SLEEP_CONTROL,
544 .flags = IORESOURCE_IO,
547 .name = "UV",
548 .start = WM831X_IRQ_UV_LDO1,
549 .end = WM831X_IRQ_UV_LDO1,
550 .flags = IORESOURCE_IRQ,
554 static struct resource wm831x_ldo2_resources[] = {
556 .start = WM831X_LDO2_CONTROL,
557 .end = WM831X_LDO2_SLEEP_CONTROL,
558 .flags = IORESOURCE_IO,
561 .name = "UV",
562 .start = WM831X_IRQ_UV_LDO2,
563 .end = WM831X_IRQ_UV_LDO2,
564 .flags = IORESOURCE_IRQ,
568 static struct resource wm831x_ldo3_resources[] = {
570 .start = WM831X_LDO3_CONTROL,
571 .end = WM831X_LDO3_SLEEP_CONTROL,
572 .flags = IORESOURCE_IO,
575 .name = "UV",
576 .start = WM831X_IRQ_UV_LDO3,
577 .end = WM831X_IRQ_UV_LDO3,
578 .flags = IORESOURCE_IRQ,
582 static struct resource wm831x_ldo4_resources[] = {
584 .start = WM831X_LDO4_CONTROL,
585 .end = WM831X_LDO4_SLEEP_CONTROL,
586 .flags = IORESOURCE_IO,
589 .name = "UV",
590 .start = WM831X_IRQ_UV_LDO4,
591 .end = WM831X_IRQ_UV_LDO4,
592 .flags = IORESOURCE_IRQ,
596 static struct resource wm831x_ldo5_resources[] = {
598 .start = WM831X_LDO5_CONTROL,
599 .end = WM831X_LDO5_SLEEP_CONTROL,
600 .flags = IORESOURCE_IO,
603 .name = "UV",
604 .start = WM831X_IRQ_UV_LDO5,
605 .end = WM831X_IRQ_UV_LDO5,
606 .flags = IORESOURCE_IRQ,
610 static struct resource wm831x_ldo6_resources[] = {
612 .start = WM831X_LDO6_CONTROL,
613 .end = WM831X_LDO6_SLEEP_CONTROL,
614 .flags = IORESOURCE_IO,
617 .name = "UV",
618 .start = WM831X_IRQ_UV_LDO6,
619 .end = WM831X_IRQ_UV_LDO6,
620 .flags = IORESOURCE_IRQ,
624 static struct resource wm831x_ldo7_resources[] = {
626 .start = WM831X_LDO7_CONTROL,
627 .end = WM831X_LDO7_SLEEP_CONTROL,
628 .flags = IORESOURCE_IO,
631 .name = "UV",
632 .start = WM831X_IRQ_UV_LDO7,
633 .end = WM831X_IRQ_UV_LDO7,
634 .flags = IORESOURCE_IRQ,
638 static struct resource wm831x_ldo8_resources[] = {
640 .start = WM831X_LDO8_CONTROL,
641 .end = WM831X_LDO8_SLEEP_CONTROL,
642 .flags = IORESOURCE_IO,
645 .name = "UV",
646 .start = WM831X_IRQ_UV_LDO8,
647 .end = WM831X_IRQ_UV_LDO8,
648 .flags = IORESOURCE_IRQ,
652 static struct resource wm831x_ldo9_resources[] = {
654 .start = WM831X_LDO9_CONTROL,
655 .end = WM831X_LDO9_SLEEP_CONTROL,
656 .flags = IORESOURCE_IO,
659 .name = "UV",
660 .start = WM831X_IRQ_UV_LDO9,
661 .end = WM831X_IRQ_UV_LDO9,
662 .flags = IORESOURCE_IRQ,
666 static struct resource wm831x_ldo10_resources[] = {
668 .start = WM831X_LDO10_CONTROL,
669 .end = WM831X_LDO10_SLEEP_CONTROL,
670 .flags = IORESOURCE_IO,
673 .name = "UV",
674 .start = WM831X_IRQ_UV_LDO10,
675 .end = WM831X_IRQ_UV_LDO10,
676 .flags = IORESOURCE_IRQ,
680 static struct resource wm831x_ldo11_resources[] = {
682 .start = WM831X_LDO11_ON_CONTROL,
683 .end = WM831X_LDO11_SLEEP_CONTROL,
684 .flags = IORESOURCE_IO,
688 static struct resource wm831x_on_resources[] = {
690 .start = WM831X_IRQ_ON,
691 .end = WM831X_IRQ_ON,
692 .flags = IORESOURCE_IRQ,
697 static struct resource wm831x_power_resources[] = {
699 .name = "SYSLO",
700 .start = WM831X_IRQ_PPM_SYSLO,
701 .end = WM831X_IRQ_PPM_SYSLO,
702 .flags = IORESOURCE_IRQ,
705 .name = "PWR SRC",
706 .start = WM831X_IRQ_PPM_PWR_SRC,
707 .end = WM831X_IRQ_PPM_PWR_SRC,
708 .flags = IORESOURCE_IRQ,
711 .name = "USB CURR",
712 .start = WM831X_IRQ_PPM_USB_CURR,
713 .end = WM831X_IRQ_PPM_USB_CURR,
714 .flags = IORESOURCE_IRQ,
717 .name = "BATT HOT",
718 .start = WM831X_IRQ_CHG_BATT_HOT,
719 .end = WM831X_IRQ_CHG_BATT_HOT,
720 .flags = IORESOURCE_IRQ,
723 .name = "BATT COLD",
724 .start = WM831X_IRQ_CHG_BATT_COLD,
725 .end = WM831X_IRQ_CHG_BATT_COLD,
726 .flags = IORESOURCE_IRQ,
729 .name = "BATT FAIL",
730 .start = WM831X_IRQ_CHG_BATT_FAIL,
731 .end = WM831X_IRQ_CHG_BATT_FAIL,
732 .flags = IORESOURCE_IRQ,
735 .name = "OV",
736 .start = WM831X_IRQ_CHG_OV,
737 .end = WM831X_IRQ_CHG_OV,
738 .flags = IORESOURCE_IRQ,
741 .name = "END",
742 .start = WM831X_IRQ_CHG_END,
743 .end = WM831X_IRQ_CHG_END,
744 .flags = IORESOURCE_IRQ,
747 .name = "TO",
748 .start = WM831X_IRQ_CHG_TO,
749 .end = WM831X_IRQ_CHG_TO,
750 .flags = IORESOURCE_IRQ,
753 .name = "MODE",
754 .start = WM831X_IRQ_CHG_MODE,
755 .end = WM831X_IRQ_CHG_MODE,
756 .flags = IORESOURCE_IRQ,
759 .name = "START",
760 .start = WM831X_IRQ_CHG_START,
761 .end = WM831X_IRQ_CHG_START,
762 .flags = IORESOURCE_IRQ,
766 static struct resource wm831x_rtc_resources[] = {
768 .name = "PER",
769 .start = WM831X_IRQ_RTC_PER,
770 .end = WM831X_IRQ_RTC_PER,
771 .flags = IORESOURCE_IRQ,
774 .name = "ALM",
775 .start = WM831X_IRQ_RTC_ALM,
776 .end = WM831X_IRQ_RTC_ALM,
777 .flags = IORESOURCE_IRQ,
781 static struct resource wm831x_status1_resources[] = {
783 .start = WM831X_STATUS_LED_1,
784 .end = WM831X_STATUS_LED_1,
785 .flags = IORESOURCE_IO,
789 static struct resource wm831x_status2_resources[] = {
791 .start = WM831X_STATUS_LED_2,
792 .end = WM831X_STATUS_LED_2,
793 .flags = IORESOURCE_IO,
797 static struct resource wm831x_touch_resources[] = {
799 .name = "TCHPD",
800 .start = WM831X_IRQ_TCHPD,
801 .end = WM831X_IRQ_TCHPD,
802 .flags = IORESOURCE_IRQ,
805 .name = "TCHDATA",
806 .start = WM831X_IRQ_TCHDATA,
807 .end = WM831X_IRQ_TCHDATA,
808 .flags = IORESOURCE_IRQ,
812 static struct resource wm831x_wdt_resources[] = {
814 .start = WM831X_IRQ_WDOG_TO,
815 .end = WM831X_IRQ_WDOG_TO,
816 .flags = IORESOURCE_IRQ,
820 static struct mfd_cell wm8310_devs[] = {
822 .name = "wm831x-backup",
825 .name = "wm831x-buckv",
826 .id = 1,
827 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
828 .resources = wm831x_dcdc1_resources,
831 .name = "wm831x-buckv",
832 .id = 2,
833 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
834 .resources = wm831x_dcdc2_resources,
837 .name = "wm831x-buckp",
838 .id = 3,
839 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
840 .resources = wm831x_dcdc3_resources,
843 .name = "wm831x-boostp",
844 .id = 4,
845 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
846 .resources = wm831x_dcdc4_resources,
849 .name = "wm831x-epe",
850 .id = 1,
853 .name = "wm831x-epe",
854 .id = 2,
857 .name = "wm831x-gpio",
858 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
859 .resources = wm831x_gpio_resources,
862 .name = "wm831x-hwmon",
865 .name = "wm831x-isink",
866 .id = 1,
867 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
868 .resources = wm831x_isink1_resources,
871 .name = "wm831x-isink",
872 .id = 2,
873 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
874 .resources = wm831x_isink2_resources,
877 .name = "wm831x-ldo",
878 .id = 1,
879 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
880 .resources = wm831x_ldo1_resources,
883 .name = "wm831x-ldo",
884 .id = 2,
885 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
886 .resources = wm831x_ldo2_resources,
889 .name = "wm831x-ldo",
890 .id = 3,
891 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
892 .resources = wm831x_ldo3_resources,
895 .name = "wm831x-ldo",
896 .id = 4,
897 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
898 .resources = wm831x_ldo4_resources,
901 .name = "wm831x-ldo",
902 .id = 5,
903 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
904 .resources = wm831x_ldo5_resources,
907 .name = "wm831x-ldo",
908 .id = 6,
909 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
910 .resources = wm831x_ldo6_resources,
913 .name = "wm831x-aldo",
914 .id = 7,
915 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
916 .resources = wm831x_ldo7_resources,
919 .name = "wm831x-aldo",
920 .id = 8,
921 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
922 .resources = wm831x_ldo8_resources,
925 .name = "wm831x-aldo",
926 .id = 9,
927 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
928 .resources = wm831x_ldo9_resources,
931 .name = "wm831x-aldo",
932 .id = 10,
933 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
934 .resources = wm831x_ldo10_resources,
937 .name = "wm831x-alive-ldo",
938 .id = 11,
939 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
940 .resources = wm831x_ldo11_resources,
943 .name = "wm831x-on",
944 .num_resources = ARRAY_SIZE(wm831x_on_resources),
945 .resources = wm831x_on_resources,
948 .name = "wm831x-power",
949 .num_resources = ARRAY_SIZE(wm831x_power_resources),
950 .resources = wm831x_power_resources,
953 .name = "wm831x-rtc",
954 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
955 .resources = wm831x_rtc_resources,
958 .name = "wm831x-status",
959 .id = 1,
960 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
961 .resources = wm831x_status1_resources,
964 .name = "wm831x-status",
965 .id = 2,
966 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
967 .resources = wm831x_status2_resources,
970 .name = "wm831x-watchdog",
971 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
972 .resources = wm831x_wdt_resources,
976 static struct mfd_cell wm8311_devs[] = {
978 .name = "wm831x-backup",
981 .name = "wm831x-buckv",
982 .id = 1,
983 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
984 .resources = wm831x_dcdc1_resources,
987 .name = "wm831x-buckv",
988 .id = 2,
989 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
990 .resources = wm831x_dcdc2_resources,
993 .name = "wm831x-buckp",
994 .id = 3,
995 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
996 .resources = wm831x_dcdc3_resources,
999 .name = "wm831x-boostp",
1000 .id = 4,
1001 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1002 .resources = wm831x_dcdc4_resources,
1005 .name = "wm831x-epe",
1006 .id = 1,
1009 .name = "wm831x-epe",
1010 .id = 2,
1013 .name = "wm831x-gpio",
1014 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1015 .resources = wm831x_gpio_resources,
1018 .name = "wm831x-hwmon",
1021 .name = "wm831x-isink",
1022 .id = 1,
1023 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1024 .resources = wm831x_isink1_resources,
1027 .name = "wm831x-isink",
1028 .id = 2,
1029 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1030 .resources = wm831x_isink2_resources,
1033 .name = "wm831x-ldo",
1034 .id = 1,
1035 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1036 .resources = wm831x_ldo1_resources,
1039 .name = "wm831x-ldo",
1040 .id = 2,
1041 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1042 .resources = wm831x_ldo2_resources,
1045 .name = "wm831x-ldo",
1046 .id = 3,
1047 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1048 .resources = wm831x_ldo3_resources,
1051 .name = "wm831x-ldo",
1052 .id = 4,
1053 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1054 .resources = wm831x_ldo4_resources,
1057 .name = "wm831x-ldo",
1058 .id = 5,
1059 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1060 .resources = wm831x_ldo5_resources,
1063 .name = "wm831x-aldo",
1064 .id = 7,
1065 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1066 .resources = wm831x_ldo7_resources,
1069 .name = "wm831x-alive-ldo",
1070 .id = 11,
1071 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1072 .resources = wm831x_ldo11_resources,
1075 .name = "wm831x-on",
1076 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1077 .resources = wm831x_on_resources,
1080 .name = "wm831x-power",
1081 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1082 .resources = wm831x_power_resources,
1085 .name = "wm831x-rtc",
1086 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1087 .resources = wm831x_rtc_resources,
1090 .name = "wm831x-status",
1091 .id = 1,
1092 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1093 .resources = wm831x_status1_resources,
1096 .name = "wm831x-status",
1097 .id = 2,
1098 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1099 .resources = wm831x_status2_resources,
1102 .name = "wm831x-touch",
1103 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1104 .resources = wm831x_touch_resources,
1107 .name = "wm831x-watchdog",
1108 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1109 .resources = wm831x_wdt_resources,
1113 static struct mfd_cell wm8312_devs[] = {
1115 .name = "wm831x-backup",
1118 .name = "wm831x-buckv",
1119 .id = 1,
1120 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1121 .resources = wm831x_dcdc1_resources,
1124 .name = "wm831x-buckv",
1125 .id = 2,
1126 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1127 .resources = wm831x_dcdc2_resources,
1130 .name = "wm831x-buckp",
1131 .id = 3,
1132 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1133 .resources = wm831x_dcdc3_resources,
1136 .name = "wm831x-boostp",
1137 .id = 4,
1138 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1139 .resources = wm831x_dcdc4_resources,
1142 .name = "wm831x-epe",
1143 .id = 1,
1146 .name = "wm831x-epe",
1147 .id = 2,
1150 .name = "wm831x-gpio",
1151 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1152 .resources = wm831x_gpio_resources,
1155 .name = "wm831x-hwmon",
1158 .name = "wm831x-isink",
1159 .id = 1,
1160 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1161 .resources = wm831x_isink1_resources,
1164 .name = "wm831x-isink",
1165 .id = 2,
1166 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1167 .resources = wm831x_isink2_resources,
1170 .name = "wm831x-ldo",
1171 .id = 1,
1172 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1173 .resources = wm831x_ldo1_resources,
1176 .name = "wm831x-ldo",
1177 .id = 2,
1178 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1179 .resources = wm831x_ldo2_resources,
1182 .name = "wm831x-ldo",
1183 .id = 3,
1184 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1185 .resources = wm831x_ldo3_resources,
1188 .name = "wm831x-ldo",
1189 .id = 4,
1190 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1191 .resources = wm831x_ldo4_resources,
1194 .name = "wm831x-ldo",
1195 .id = 5,
1196 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1197 .resources = wm831x_ldo5_resources,
1200 .name = "wm831x-ldo",
1201 .id = 6,
1202 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1203 .resources = wm831x_ldo6_resources,
1206 .name = "wm831x-aldo",
1207 .id = 7,
1208 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1209 .resources = wm831x_ldo7_resources,
1212 .name = "wm831x-aldo",
1213 .id = 8,
1214 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1215 .resources = wm831x_ldo8_resources,
1218 .name = "wm831x-aldo",
1219 .id = 9,
1220 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1221 .resources = wm831x_ldo9_resources,
1224 .name = "wm831x-aldo",
1225 .id = 10,
1226 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1227 .resources = wm831x_ldo10_resources,
1230 .name = "wm831x-alive-ldo",
1231 .id = 11,
1232 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1233 .resources = wm831x_ldo11_resources,
1236 .name = "wm831x-on",
1237 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1238 .resources = wm831x_on_resources,
1241 .name = "wm831x-power",
1242 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1243 .resources = wm831x_power_resources,
1246 .name = "wm831x-rtc",
1247 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1248 .resources = wm831x_rtc_resources,
1251 .name = "wm831x-status",
1252 .id = 1,
1253 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1254 .resources = wm831x_status1_resources,
1257 .name = "wm831x-status",
1258 .id = 2,
1259 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1260 .resources = wm831x_status2_resources,
1263 .name = "wm831x-touch",
1264 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1265 .resources = wm831x_touch_resources,
1268 .name = "wm831x-watchdog",
1269 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1270 .resources = wm831x_wdt_resources,
1274 static struct mfd_cell wm8320_devs[] = {
1276 .name = "wm831x-backup",
1279 .name = "wm831x-buckv",
1280 .id = 1,
1281 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1282 .resources = wm831x_dcdc1_resources,
1285 .name = "wm831x-buckv",
1286 .id = 2,
1287 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1288 .resources = wm831x_dcdc2_resources,
1291 .name = "wm831x-buckp",
1292 .id = 3,
1293 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1294 .resources = wm831x_dcdc3_resources,
1297 .name = "wm831x-buckp",
1298 .id = 4,
1299 .num_resources = ARRAY_SIZE(wm8320_dcdc4_buck_resources),
1300 .resources = wm8320_dcdc4_buck_resources,
1303 .name = "wm831x-gpio",
1304 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1305 .resources = wm831x_gpio_resources,
1308 .name = "wm831x-hwmon",
1311 .name = "wm831x-ldo",
1312 .id = 1,
1313 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1314 .resources = wm831x_ldo1_resources,
1317 .name = "wm831x-ldo",
1318 .id = 2,
1319 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1320 .resources = wm831x_ldo2_resources,
1323 .name = "wm831x-ldo",
1324 .id = 3,
1325 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1326 .resources = wm831x_ldo3_resources,
1329 .name = "wm831x-ldo",
1330 .id = 4,
1331 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1332 .resources = wm831x_ldo4_resources,
1335 .name = "wm831x-ldo",
1336 .id = 5,
1337 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1338 .resources = wm831x_ldo5_resources,
1341 .name = "wm831x-ldo",
1342 .id = 6,
1343 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1344 .resources = wm831x_ldo6_resources,
1347 .name = "wm831x-aldo",
1348 .id = 7,
1349 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1350 .resources = wm831x_ldo7_resources,
1353 .name = "wm831x-aldo",
1354 .id = 8,
1355 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1356 .resources = wm831x_ldo8_resources,
1359 .name = "wm831x-aldo",
1360 .id = 9,
1361 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1362 .resources = wm831x_ldo9_resources,
1365 .name = "wm831x-aldo",
1366 .id = 10,
1367 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1368 .resources = wm831x_ldo10_resources,
1371 .name = "wm831x-alive-ldo",
1372 .id = 11,
1373 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1374 .resources = wm831x_ldo11_resources,
1377 .name = "wm831x-on",
1378 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1379 .resources = wm831x_on_resources,
1382 .name = "wm831x-rtc",
1383 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1384 .resources = wm831x_rtc_resources,
1387 .name = "wm831x-status",
1388 .id = 1,
1389 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1390 .resources = wm831x_status1_resources,
1393 .name = "wm831x-status",
1394 .id = 2,
1395 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1396 .resources = wm831x_status2_resources,
1399 .name = "wm831x-watchdog",
1400 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1401 .resources = wm831x_wdt_resources,
1405 static struct mfd_cell backlight_devs[] = {
1407 .name = "wm831x-backlight",
1412 * Instantiate the generic non-control parts of the device.
1414 static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1416 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1417 int rev;
1418 enum wm831x_parent parent;
1419 int ret;
1421 mutex_init(&wm831x->io_lock);
1422 mutex_init(&wm831x->key_lock);
1423 mutex_init(&wm831x->auxadc_lock);
1424 init_completion(&wm831x->auxadc_done);
1425 dev_set_drvdata(wm831x->dev, wm831x);
1427 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1428 if (ret < 0) {
1429 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1430 goto err;
1432 if (ret != 0x6204) {
1433 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1434 ret = -EINVAL;
1435 goto err;
1438 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1439 if (ret < 0) {
1440 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1441 goto err;
1443 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1445 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1446 if (ret < 0) {
1447 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1448 goto err;
1451 /* Some engineering samples do not have the ID set, rely on
1452 * the device being registered correctly.
1454 if (ret == 0) {
1455 dev_info(wm831x->dev, "Device is an engineering sample\n");
1456 ret = id;
1459 switch (ret) {
1460 case WM8310:
1461 parent = WM8310;
1462 wm831x->num_gpio = 16;
1463 if (rev > 0) {
1464 wm831x->has_gpio_ena = 1;
1465 wm831x->has_cs_sts = 1;
1468 dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
1469 break;
1471 case WM8311:
1472 parent = WM8311;
1473 wm831x->num_gpio = 16;
1474 if (rev > 0) {
1475 wm831x->has_gpio_ena = 1;
1476 wm831x->has_cs_sts = 1;
1479 dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
1480 break;
1482 case WM8312:
1483 parent = WM8312;
1484 wm831x->num_gpio = 16;
1485 if (rev > 0) {
1486 wm831x->has_gpio_ena = 1;
1487 wm831x->has_cs_sts = 1;
1490 dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
1491 break;
1493 case WM8320:
1494 parent = WM8320;
1495 wm831x->num_gpio = 12;
1496 dev_info(wm831x->dev, "WM8320 revision %c\n", 'A' + rev);
1497 break;
1499 default:
1500 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1501 ret = -EINVAL;
1502 goto err;
1505 /* This will need revisiting in future but is OK for all
1506 * current parts.
1508 if (parent != id)
1509 dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
1510 id);
1512 /* Bootstrap the user key */
1513 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1514 if (ret < 0) {
1515 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1516 goto err;
1518 if (ret != 0) {
1519 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1520 ret);
1521 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1523 wm831x->locked = 1;
1525 if (pdata && pdata->pre_init) {
1526 ret = pdata->pre_init(wm831x);
1527 if (ret != 0) {
1528 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1529 goto err;
1533 ret = wm831x_irq_init(wm831x, irq);
1534 if (ret != 0)
1535 goto err;
1537 if (wm831x->irq_base) {
1538 ret = request_threaded_irq(wm831x->irq_base +
1539 WM831X_IRQ_AUXADC_DATA,
1540 NULL, wm831x_auxadc_irq, 0,
1541 "auxadc", wm831x);
1542 if (ret < 0)
1543 dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
1544 ret);
1547 /* The core device is up, instantiate the subdevices. */
1548 switch (parent) {
1549 case WM8310:
1550 ret = mfd_add_devices(wm831x->dev, -1,
1551 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1552 NULL, wm831x->irq_base);
1553 break;
1555 case WM8311:
1556 ret = mfd_add_devices(wm831x->dev, -1,
1557 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1558 NULL, wm831x->irq_base);
1559 break;
1561 case WM8312:
1562 ret = mfd_add_devices(wm831x->dev, -1,
1563 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1564 NULL, wm831x->irq_base);
1565 break;
1567 case WM8320:
1568 ret = mfd_add_devices(wm831x->dev, -1,
1569 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1570 NULL, 0);
1571 break;
1573 default:
1574 /* If this happens the bus probe function is buggy */
1575 BUG();
1578 if (ret != 0) {
1579 dev_err(wm831x->dev, "Failed to add children\n");
1580 goto err_irq;
1583 if (pdata && pdata->backlight) {
1584 /* Treat errors as non-critical */
1585 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1586 ARRAY_SIZE(backlight_devs), NULL,
1587 wm831x->irq_base);
1588 if (ret < 0)
1589 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1590 ret);
1593 wm831x_otp_init(wm831x);
1595 if (pdata && pdata->post_init) {
1596 ret = pdata->post_init(wm831x);
1597 if (ret != 0) {
1598 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1599 goto err_irq;
1603 return 0;
1605 err_irq:
1606 wm831x_irq_exit(wm831x);
1607 err:
1608 mfd_remove_devices(wm831x->dev);
1609 kfree(wm831x);
1610 return ret;
1613 static void wm831x_device_exit(struct wm831x *wm831x)
1615 wm831x_otp_exit(wm831x);
1616 mfd_remove_devices(wm831x->dev);
1617 if (wm831x->irq_base)
1618 free_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, wm831x);
1619 wm831x_irq_exit(wm831x);
1620 kfree(wm831x);
1623 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
1624 int bytes, void *dest)
1626 struct i2c_client *i2c = wm831x->control_data;
1627 int ret;
1628 u16 r = cpu_to_be16(reg);
1630 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
1631 if (ret < 0)
1632 return ret;
1633 if (ret != 2)
1634 return -EIO;
1636 ret = i2c_master_recv(i2c, dest, bytes);
1637 if (ret < 0)
1638 return ret;
1639 if (ret != bytes)
1640 return -EIO;
1641 return 0;
1644 /* Currently we allocate the write buffer on the stack; this is OK for
1645 * small writes - if we need to do large writes this will need to be
1646 * revised.
1648 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
1649 int bytes, void *src)
1651 struct i2c_client *i2c = wm831x->control_data;
1652 unsigned char msg[bytes + 2];
1653 int ret;
1655 reg = cpu_to_be16(reg);
1656 memcpy(&msg[0], &reg, 2);
1657 memcpy(&msg[2], src, bytes);
1659 ret = i2c_master_send(i2c, msg, bytes + 2);
1660 if (ret < 0)
1661 return ret;
1662 if (ret < bytes + 2)
1663 return -EIO;
1665 return 0;
1668 static int wm831x_i2c_probe(struct i2c_client *i2c,
1669 const struct i2c_device_id *id)
1671 struct wm831x *wm831x;
1673 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
1674 if (wm831x == NULL) {
1675 kfree(i2c);
1676 return -ENOMEM;
1679 i2c_set_clientdata(i2c, wm831x);
1680 wm831x->dev = &i2c->dev;
1681 wm831x->control_data = i2c;
1682 wm831x->read_dev = wm831x_i2c_read_device;
1683 wm831x->write_dev = wm831x_i2c_write_device;
1685 return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
1688 static int wm831x_i2c_remove(struct i2c_client *i2c)
1690 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1692 wm831x_device_exit(wm831x);
1694 return 0;
1697 static const struct i2c_device_id wm831x_i2c_id[] = {
1698 { "wm8310", WM8310 },
1699 { "wm8311", WM8311 },
1700 { "wm8312", WM8312 },
1701 { "wm8320", WM8320 },
1704 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
1707 static struct i2c_driver wm831x_i2c_driver = {
1708 .driver = {
1709 .name = "wm831x",
1710 .owner = THIS_MODULE,
1712 .probe = wm831x_i2c_probe,
1713 .remove = wm831x_i2c_remove,
1714 .id_table = wm831x_i2c_id,
1717 static int __init wm831x_i2c_init(void)
1719 int ret;
1721 ret = i2c_add_driver(&wm831x_i2c_driver);
1722 if (ret != 0)
1723 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
1725 return ret;
1727 subsys_initcall(wm831x_i2c_init);
1729 static void __exit wm831x_i2c_exit(void)
1731 i2c_del_driver(&wm831x_i2c_driver);
1733 module_exit(wm831x_i2c_exit);
1735 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1736 MODULE_LICENSE("GPL");
1737 MODULE_AUTHOR("Mark Brown");