thinkpad-acpi: untangle ACPI/vendor backlight selection
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mfd / wm831x-core.c
blobf2ab025ad97a3cad7532ad95b21684cf4561ff5e
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 /* If an interrupt arrived late clean up after it */
353 try_wait_for_completion(&wm831x->auxadc_done);
355 /* Ignore the result to allow us to soldier on without IRQ hookup */
356 wait_for_completion_timeout(&wm831x->auxadc_done, msecs_to_jiffies(5));
358 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_CONTROL);
359 if (ret < 0) {
360 dev_err(wm831x->dev, "AUXADC status read failed: %d\n", ret);
361 goto disable;
364 if (ret & WM831X_AUX_CVT_ENA) {
365 dev_err(wm831x->dev, "Timed out reading AUXADC\n");
366 ret = -EBUSY;
367 goto disable;
370 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
371 if (ret < 0) {
372 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
373 } else {
374 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
375 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
377 if (src == 14)
378 src = WM831X_AUX_CAL;
380 if (src != input) {
381 dev_err(wm831x->dev, "Data from source %d not %d\n",
382 src, input);
383 ret = -EINVAL;
384 } else {
385 ret &= WM831X_AUX_DATA_MASK;
389 disable:
390 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
391 out:
392 mutex_unlock(&wm831x->auxadc_lock);
393 return ret;
395 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
397 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
399 struct wm831x *wm831x = irq_data;
401 complete(&wm831x->auxadc_done);
403 return IRQ_HANDLED;
407 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
409 * @wm831x: Device to read from.
410 * @input: AUXADC input to read.
412 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
414 int ret;
416 ret = wm831x_auxadc_read(wm831x, input);
417 if (ret < 0)
418 return ret;
420 ret *= 1465;
422 return ret;
424 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
426 static struct resource wm831x_dcdc1_resources[] = {
428 .start = WM831X_DC1_CONTROL_1,
429 .end = WM831X_DC1_DVS_CONTROL,
430 .flags = IORESOURCE_IO,
433 .name = "UV",
434 .start = WM831X_IRQ_UV_DC1,
435 .end = WM831X_IRQ_UV_DC1,
436 .flags = IORESOURCE_IRQ,
439 .name = "HC",
440 .start = WM831X_IRQ_HC_DC1,
441 .end = WM831X_IRQ_HC_DC1,
442 .flags = IORESOURCE_IRQ,
447 static struct resource wm831x_dcdc2_resources[] = {
449 .start = WM831X_DC2_CONTROL_1,
450 .end = WM831X_DC2_DVS_CONTROL,
451 .flags = IORESOURCE_IO,
454 .name = "UV",
455 .start = WM831X_IRQ_UV_DC2,
456 .end = WM831X_IRQ_UV_DC2,
457 .flags = IORESOURCE_IRQ,
460 .name = "HC",
461 .start = WM831X_IRQ_HC_DC2,
462 .end = WM831X_IRQ_HC_DC2,
463 .flags = IORESOURCE_IRQ,
467 static struct resource wm831x_dcdc3_resources[] = {
469 .start = WM831X_DC3_CONTROL_1,
470 .end = WM831X_DC3_SLEEP_CONTROL,
471 .flags = IORESOURCE_IO,
474 .name = "UV",
475 .start = WM831X_IRQ_UV_DC3,
476 .end = WM831X_IRQ_UV_DC3,
477 .flags = IORESOURCE_IRQ,
481 static struct resource wm831x_dcdc4_resources[] = {
483 .start = WM831X_DC4_CONTROL,
484 .end = WM831X_DC4_SLEEP_CONTROL,
485 .flags = IORESOURCE_IO,
488 .name = "UV",
489 .start = WM831X_IRQ_UV_DC4,
490 .end = WM831X_IRQ_UV_DC4,
491 .flags = IORESOURCE_IRQ,
495 static struct resource wm8320_dcdc4_buck_resources[] = {
497 .start = WM831X_DC4_CONTROL,
498 .end = WM832X_DC4_SLEEP_CONTROL,
499 .flags = IORESOURCE_IO,
502 .name = "UV",
503 .start = WM831X_IRQ_UV_DC4,
504 .end = WM831X_IRQ_UV_DC4,
505 .flags = IORESOURCE_IRQ,
509 static struct resource wm831x_gpio_resources[] = {
511 .start = WM831X_IRQ_GPIO_1,
512 .end = WM831X_IRQ_GPIO_16,
513 .flags = IORESOURCE_IRQ,
517 static struct resource wm831x_isink1_resources[] = {
519 .start = WM831X_CURRENT_SINK_1,
520 .end = WM831X_CURRENT_SINK_1,
521 .flags = IORESOURCE_IO,
524 .start = WM831X_IRQ_CS1,
525 .end = WM831X_IRQ_CS1,
526 .flags = IORESOURCE_IRQ,
530 static struct resource wm831x_isink2_resources[] = {
532 .start = WM831X_CURRENT_SINK_2,
533 .end = WM831X_CURRENT_SINK_2,
534 .flags = IORESOURCE_IO,
537 .start = WM831X_IRQ_CS2,
538 .end = WM831X_IRQ_CS2,
539 .flags = IORESOURCE_IRQ,
543 static struct resource wm831x_ldo1_resources[] = {
545 .start = WM831X_LDO1_CONTROL,
546 .end = WM831X_LDO1_SLEEP_CONTROL,
547 .flags = IORESOURCE_IO,
550 .name = "UV",
551 .start = WM831X_IRQ_UV_LDO1,
552 .end = WM831X_IRQ_UV_LDO1,
553 .flags = IORESOURCE_IRQ,
557 static struct resource wm831x_ldo2_resources[] = {
559 .start = WM831X_LDO2_CONTROL,
560 .end = WM831X_LDO2_SLEEP_CONTROL,
561 .flags = IORESOURCE_IO,
564 .name = "UV",
565 .start = WM831X_IRQ_UV_LDO2,
566 .end = WM831X_IRQ_UV_LDO2,
567 .flags = IORESOURCE_IRQ,
571 static struct resource wm831x_ldo3_resources[] = {
573 .start = WM831X_LDO3_CONTROL,
574 .end = WM831X_LDO3_SLEEP_CONTROL,
575 .flags = IORESOURCE_IO,
578 .name = "UV",
579 .start = WM831X_IRQ_UV_LDO3,
580 .end = WM831X_IRQ_UV_LDO3,
581 .flags = IORESOURCE_IRQ,
585 static struct resource wm831x_ldo4_resources[] = {
587 .start = WM831X_LDO4_CONTROL,
588 .end = WM831X_LDO4_SLEEP_CONTROL,
589 .flags = IORESOURCE_IO,
592 .name = "UV",
593 .start = WM831X_IRQ_UV_LDO4,
594 .end = WM831X_IRQ_UV_LDO4,
595 .flags = IORESOURCE_IRQ,
599 static struct resource wm831x_ldo5_resources[] = {
601 .start = WM831X_LDO5_CONTROL,
602 .end = WM831X_LDO5_SLEEP_CONTROL,
603 .flags = IORESOURCE_IO,
606 .name = "UV",
607 .start = WM831X_IRQ_UV_LDO5,
608 .end = WM831X_IRQ_UV_LDO5,
609 .flags = IORESOURCE_IRQ,
613 static struct resource wm831x_ldo6_resources[] = {
615 .start = WM831X_LDO6_CONTROL,
616 .end = WM831X_LDO6_SLEEP_CONTROL,
617 .flags = IORESOURCE_IO,
620 .name = "UV",
621 .start = WM831X_IRQ_UV_LDO6,
622 .end = WM831X_IRQ_UV_LDO6,
623 .flags = IORESOURCE_IRQ,
627 static struct resource wm831x_ldo7_resources[] = {
629 .start = WM831X_LDO7_CONTROL,
630 .end = WM831X_LDO7_SLEEP_CONTROL,
631 .flags = IORESOURCE_IO,
634 .name = "UV",
635 .start = WM831X_IRQ_UV_LDO7,
636 .end = WM831X_IRQ_UV_LDO7,
637 .flags = IORESOURCE_IRQ,
641 static struct resource wm831x_ldo8_resources[] = {
643 .start = WM831X_LDO8_CONTROL,
644 .end = WM831X_LDO8_SLEEP_CONTROL,
645 .flags = IORESOURCE_IO,
648 .name = "UV",
649 .start = WM831X_IRQ_UV_LDO8,
650 .end = WM831X_IRQ_UV_LDO8,
651 .flags = IORESOURCE_IRQ,
655 static struct resource wm831x_ldo9_resources[] = {
657 .start = WM831X_LDO9_CONTROL,
658 .end = WM831X_LDO9_SLEEP_CONTROL,
659 .flags = IORESOURCE_IO,
662 .name = "UV",
663 .start = WM831X_IRQ_UV_LDO9,
664 .end = WM831X_IRQ_UV_LDO9,
665 .flags = IORESOURCE_IRQ,
669 static struct resource wm831x_ldo10_resources[] = {
671 .start = WM831X_LDO10_CONTROL,
672 .end = WM831X_LDO10_SLEEP_CONTROL,
673 .flags = IORESOURCE_IO,
676 .name = "UV",
677 .start = WM831X_IRQ_UV_LDO10,
678 .end = WM831X_IRQ_UV_LDO10,
679 .flags = IORESOURCE_IRQ,
683 static struct resource wm831x_ldo11_resources[] = {
685 .start = WM831X_LDO11_ON_CONTROL,
686 .end = WM831X_LDO11_SLEEP_CONTROL,
687 .flags = IORESOURCE_IO,
691 static struct resource wm831x_on_resources[] = {
693 .start = WM831X_IRQ_ON,
694 .end = WM831X_IRQ_ON,
695 .flags = IORESOURCE_IRQ,
700 static struct resource wm831x_power_resources[] = {
702 .name = "SYSLO",
703 .start = WM831X_IRQ_PPM_SYSLO,
704 .end = WM831X_IRQ_PPM_SYSLO,
705 .flags = IORESOURCE_IRQ,
708 .name = "PWR SRC",
709 .start = WM831X_IRQ_PPM_PWR_SRC,
710 .end = WM831X_IRQ_PPM_PWR_SRC,
711 .flags = IORESOURCE_IRQ,
714 .name = "USB CURR",
715 .start = WM831X_IRQ_PPM_USB_CURR,
716 .end = WM831X_IRQ_PPM_USB_CURR,
717 .flags = IORESOURCE_IRQ,
720 .name = "BATT HOT",
721 .start = WM831X_IRQ_CHG_BATT_HOT,
722 .end = WM831X_IRQ_CHG_BATT_HOT,
723 .flags = IORESOURCE_IRQ,
726 .name = "BATT COLD",
727 .start = WM831X_IRQ_CHG_BATT_COLD,
728 .end = WM831X_IRQ_CHG_BATT_COLD,
729 .flags = IORESOURCE_IRQ,
732 .name = "BATT FAIL",
733 .start = WM831X_IRQ_CHG_BATT_FAIL,
734 .end = WM831X_IRQ_CHG_BATT_FAIL,
735 .flags = IORESOURCE_IRQ,
738 .name = "OV",
739 .start = WM831X_IRQ_CHG_OV,
740 .end = WM831X_IRQ_CHG_OV,
741 .flags = IORESOURCE_IRQ,
744 .name = "END",
745 .start = WM831X_IRQ_CHG_END,
746 .end = WM831X_IRQ_CHG_END,
747 .flags = IORESOURCE_IRQ,
750 .name = "TO",
751 .start = WM831X_IRQ_CHG_TO,
752 .end = WM831X_IRQ_CHG_TO,
753 .flags = IORESOURCE_IRQ,
756 .name = "MODE",
757 .start = WM831X_IRQ_CHG_MODE,
758 .end = WM831X_IRQ_CHG_MODE,
759 .flags = IORESOURCE_IRQ,
762 .name = "START",
763 .start = WM831X_IRQ_CHG_START,
764 .end = WM831X_IRQ_CHG_START,
765 .flags = IORESOURCE_IRQ,
769 static struct resource wm831x_rtc_resources[] = {
771 .name = "PER",
772 .start = WM831X_IRQ_RTC_PER,
773 .end = WM831X_IRQ_RTC_PER,
774 .flags = IORESOURCE_IRQ,
777 .name = "ALM",
778 .start = WM831X_IRQ_RTC_ALM,
779 .end = WM831X_IRQ_RTC_ALM,
780 .flags = IORESOURCE_IRQ,
784 static struct resource wm831x_status1_resources[] = {
786 .start = WM831X_STATUS_LED_1,
787 .end = WM831X_STATUS_LED_1,
788 .flags = IORESOURCE_IO,
792 static struct resource wm831x_status2_resources[] = {
794 .start = WM831X_STATUS_LED_2,
795 .end = WM831X_STATUS_LED_2,
796 .flags = IORESOURCE_IO,
800 static struct resource wm831x_touch_resources[] = {
802 .name = "TCHPD",
803 .start = WM831X_IRQ_TCHPD,
804 .end = WM831X_IRQ_TCHPD,
805 .flags = IORESOURCE_IRQ,
808 .name = "TCHDATA",
809 .start = WM831X_IRQ_TCHDATA,
810 .end = WM831X_IRQ_TCHDATA,
811 .flags = IORESOURCE_IRQ,
815 static struct resource wm831x_wdt_resources[] = {
817 .start = WM831X_IRQ_WDOG_TO,
818 .end = WM831X_IRQ_WDOG_TO,
819 .flags = IORESOURCE_IRQ,
823 static struct mfd_cell wm8310_devs[] = {
825 .name = "wm831x-backup",
828 .name = "wm831x-buckv",
829 .id = 1,
830 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
831 .resources = wm831x_dcdc1_resources,
834 .name = "wm831x-buckv",
835 .id = 2,
836 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
837 .resources = wm831x_dcdc2_resources,
840 .name = "wm831x-buckp",
841 .id = 3,
842 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
843 .resources = wm831x_dcdc3_resources,
846 .name = "wm831x-boostp",
847 .id = 4,
848 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
849 .resources = wm831x_dcdc4_resources,
852 .name = "wm831x-epe",
853 .id = 1,
856 .name = "wm831x-epe",
857 .id = 2,
860 .name = "wm831x-gpio",
861 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
862 .resources = wm831x_gpio_resources,
865 .name = "wm831x-hwmon",
868 .name = "wm831x-isink",
869 .id = 1,
870 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
871 .resources = wm831x_isink1_resources,
874 .name = "wm831x-isink",
875 .id = 2,
876 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
877 .resources = wm831x_isink2_resources,
880 .name = "wm831x-ldo",
881 .id = 1,
882 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
883 .resources = wm831x_ldo1_resources,
886 .name = "wm831x-ldo",
887 .id = 2,
888 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
889 .resources = wm831x_ldo2_resources,
892 .name = "wm831x-ldo",
893 .id = 3,
894 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
895 .resources = wm831x_ldo3_resources,
898 .name = "wm831x-ldo",
899 .id = 4,
900 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
901 .resources = wm831x_ldo4_resources,
904 .name = "wm831x-ldo",
905 .id = 5,
906 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
907 .resources = wm831x_ldo5_resources,
910 .name = "wm831x-ldo",
911 .id = 6,
912 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
913 .resources = wm831x_ldo6_resources,
916 .name = "wm831x-aldo",
917 .id = 7,
918 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
919 .resources = wm831x_ldo7_resources,
922 .name = "wm831x-aldo",
923 .id = 8,
924 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
925 .resources = wm831x_ldo8_resources,
928 .name = "wm831x-aldo",
929 .id = 9,
930 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
931 .resources = wm831x_ldo9_resources,
934 .name = "wm831x-aldo",
935 .id = 10,
936 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
937 .resources = wm831x_ldo10_resources,
940 .name = "wm831x-alive-ldo",
941 .id = 11,
942 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
943 .resources = wm831x_ldo11_resources,
946 .name = "wm831x-on",
947 .num_resources = ARRAY_SIZE(wm831x_on_resources),
948 .resources = wm831x_on_resources,
951 .name = "wm831x-power",
952 .num_resources = ARRAY_SIZE(wm831x_power_resources),
953 .resources = wm831x_power_resources,
956 .name = "wm831x-rtc",
957 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
958 .resources = wm831x_rtc_resources,
961 .name = "wm831x-status",
962 .id = 1,
963 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
964 .resources = wm831x_status1_resources,
967 .name = "wm831x-status",
968 .id = 2,
969 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
970 .resources = wm831x_status2_resources,
973 .name = "wm831x-watchdog",
974 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
975 .resources = wm831x_wdt_resources,
979 static struct mfd_cell wm8311_devs[] = {
981 .name = "wm831x-backup",
984 .name = "wm831x-buckv",
985 .id = 1,
986 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
987 .resources = wm831x_dcdc1_resources,
990 .name = "wm831x-buckv",
991 .id = 2,
992 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
993 .resources = wm831x_dcdc2_resources,
996 .name = "wm831x-buckp",
997 .id = 3,
998 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
999 .resources = wm831x_dcdc3_resources,
1002 .name = "wm831x-boostp",
1003 .id = 4,
1004 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1005 .resources = wm831x_dcdc4_resources,
1008 .name = "wm831x-epe",
1009 .id = 1,
1012 .name = "wm831x-epe",
1013 .id = 2,
1016 .name = "wm831x-gpio",
1017 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1018 .resources = wm831x_gpio_resources,
1021 .name = "wm831x-hwmon",
1024 .name = "wm831x-isink",
1025 .id = 1,
1026 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1027 .resources = wm831x_isink1_resources,
1030 .name = "wm831x-isink",
1031 .id = 2,
1032 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1033 .resources = wm831x_isink2_resources,
1036 .name = "wm831x-ldo",
1037 .id = 1,
1038 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1039 .resources = wm831x_ldo1_resources,
1042 .name = "wm831x-ldo",
1043 .id = 2,
1044 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1045 .resources = wm831x_ldo2_resources,
1048 .name = "wm831x-ldo",
1049 .id = 3,
1050 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1051 .resources = wm831x_ldo3_resources,
1054 .name = "wm831x-ldo",
1055 .id = 4,
1056 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1057 .resources = wm831x_ldo4_resources,
1060 .name = "wm831x-ldo",
1061 .id = 5,
1062 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1063 .resources = wm831x_ldo5_resources,
1066 .name = "wm831x-aldo",
1067 .id = 7,
1068 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1069 .resources = wm831x_ldo7_resources,
1072 .name = "wm831x-alive-ldo",
1073 .id = 11,
1074 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1075 .resources = wm831x_ldo11_resources,
1078 .name = "wm831x-on",
1079 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1080 .resources = wm831x_on_resources,
1083 .name = "wm831x-power",
1084 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1085 .resources = wm831x_power_resources,
1088 .name = "wm831x-rtc",
1089 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1090 .resources = wm831x_rtc_resources,
1093 .name = "wm831x-status",
1094 .id = 1,
1095 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1096 .resources = wm831x_status1_resources,
1099 .name = "wm831x-status",
1100 .id = 2,
1101 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1102 .resources = wm831x_status2_resources,
1105 .name = "wm831x-touch",
1106 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1107 .resources = wm831x_touch_resources,
1110 .name = "wm831x-watchdog",
1111 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1112 .resources = wm831x_wdt_resources,
1116 static struct mfd_cell wm8312_devs[] = {
1118 .name = "wm831x-backup",
1121 .name = "wm831x-buckv",
1122 .id = 1,
1123 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1124 .resources = wm831x_dcdc1_resources,
1127 .name = "wm831x-buckv",
1128 .id = 2,
1129 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1130 .resources = wm831x_dcdc2_resources,
1133 .name = "wm831x-buckp",
1134 .id = 3,
1135 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1136 .resources = wm831x_dcdc3_resources,
1139 .name = "wm831x-boostp",
1140 .id = 4,
1141 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1142 .resources = wm831x_dcdc4_resources,
1145 .name = "wm831x-epe",
1146 .id = 1,
1149 .name = "wm831x-epe",
1150 .id = 2,
1153 .name = "wm831x-gpio",
1154 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1155 .resources = wm831x_gpio_resources,
1158 .name = "wm831x-hwmon",
1161 .name = "wm831x-isink",
1162 .id = 1,
1163 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1164 .resources = wm831x_isink1_resources,
1167 .name = "wm831x-isink",
1168 .id = 2,
1169 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1170 .resources = wm831x_isink2_resources,
1173 .name = "wm831x-ldo",
1174 .id = 1,
1175 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1176 .resources = wm831x_ldo1_resources,
1179 .name = "wm831x-ldo",
1180 .id = 2,
1181 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1182 .resources = wm831x_ldo2_resources,
1185 .name = "wm831x-ldo",
1186 .id = 3,
1187 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1188 .resources = wm831x_ldo3_resources,
1191 .name = "wm831x-ldo",
1192 .id = 4,
1193 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1194 .resources = wm831x_ldo4_resources,
1197 .name = "wm831x-ldo",
1198 .id = 5,
1199 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1200 .resources = wm831x_ldo5_resources,
1203 .name = "wm831x-ldo",
1204 .id = 6,
1205 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1206 .resources = wm831x_ldo6_resources,
1209 .name = "wm831x-aldo",
1210 .id = 7,
1211 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1212 .resources = wm831x_ldo7_resources,
1215 .name = "wm831x-aldo",
1216 .id = 8,
1217 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1218 .resources = wm831x_ldo8_resources,
1221 .name = "wm831x-aldo",
1222 .id = 9,
1223 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1224 .resources = wm831x_ldo9_resources,
1227 .name = "wm831x-aldo",
1228 .id = 10,
1229 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1230 .resources = wm831x_ldo10_resources,
1233 .name = "wm831x-alive-ldo",
1234 .id = 11,
1235 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1236 .resources = wm831x_ldo11_resources,
1239 .name = "wm831x-on",
1240 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1241 .resources = wm831x_on_resources,
1244 .name = "wm831x-power",
1245 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1246 .resources = wm831x_power_resources,
1249 .name = "wm831x-rtc",
1250 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1251 .resources = wm831x_rtc_resources,
1254 .name = "wm831x-status",
1255 .id = 1,
1256 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1257 .resources = wm831x_status1_resources,
1260 .name = "wm831x-status",
1261 .id = 2,
1262 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1263 .resources = wm831x_status2_resources,
1266 .name = "wm831x-touch",
1267 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1268 .resources = wm831x_touch_resources,
1271 .name = "wm831x-watchdog",
1272 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1273 .resources = wm831x_wdt_resources,
1277 static struct mfd_cell wm8320_devs[] = {
1279 .name = "wm831x-backup",
1282 .name = "wm831x-buckv",
1283 .id = 1,
1284 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1285 .resources = wm831x_dcdc1_resources,
1288 .name = "wm831x-buckv",
1289 .id = 2,
1290 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1291 .resources = wm831x_dcdc2_resources,
1294 .name = "wm831x-buckp",
1295 .id = 3,
1296 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1297 .resources = wm831x_dcdc3_resources,
1300 .name = "wm831x-buckp",
1301 .id = 4,
1302 .num_resources = ARRAY_SIZE(wm8320_dcdc4_buck_resources),
1303 .resources = wm8320_dcdc4_buck_resources,
1306 .name = "wm831x-gpio",
1307 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1308 .resources = wm831x_gpio_resources,
1311 .name = "wm831x-hwmon",
1314 .name = "wm831x-ldo",
1315 .id = 1,
1316 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1317 .resources = wm831x_ldo1_resources,
1320 .name = "wm831x-ldo",
1321 .id = 2,
1322 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1323 .resources = wm831x_ldo2_resources,
1326 .name = "wm831x-ldo",
1327 .id = 3,
1328 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1329 .resources = wm831x_ldo3_resources,
1332 .name = "wm831x-ldo",
1333 .id = 4,
1334 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1335 .resources = wm831x_ldo4_resources,
1338 .name = "wm831x-ldo",
1339 .id = 5,
1340 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1341 .resources = wm831x_ldo5_resources,
1344 .name = "wm831x-ldo",
1345 .id = 6,
1346 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1347 .resources = wm831x_ldo6_resources,
1350 .name = "wm831x-aldo",
1351 .id = 7,
1352 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1353 .resources = wm831x_ldo7_resources,
1356 .name = "wm831x-aldo",
1357 .id = 8,
1358 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1359 .resources = wm831x_ldo8_resources,
1362 .name = "wm831x-aldo",
1363 .id = 9,
1364 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1365 .resources = wm831x_ldo9_resources,
1368 .name = "wm831x-aldo",
1369 .id = 10,
1370 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1371 .resources = wm831x_ldo10_resources,
1374 .name = "wm831x-alive-ldo",
1375 .id = 11,
1376 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1377 .resources = wm831x_ldo11_resources,
1380 .name = "wm831x-on",
1381 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1382 .resources = wm831x_on_resources,
1385 .name = "wm831x-rtc",
1386 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1387 .resources = wm831x_rtc_resources,
1390 .name = "wm831x-status",
1391 .id = 1,
1392 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1393 .resources = wm831x_status1_resources,
1396 .name = "wm831x-status",
1397 .id = 2,
1398 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1399 .resources = wm831x_status2_resources,
1402 .name = "wm831x-watchdog",
1403 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1404 .resources = wm831x_wdt_resources,
1408 static struct mfd_cell backlight_devs[] = {
1410 .name = "wm831x-backlight",
1415 * Instantiate the generic non-control parts of the device.
1417 static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1419 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1420 int rev;
1421 enum wm831x_parent parent;
1422 int ret;
1424 mutex_init(&wm831x->io_lock);
1425 mutex_init(&wm831x->key_lock);
1426 mutex_init(&wm831x->auxadc_lock);
1427 init_completion(&wm831x->auxadc_done);
1428 dev_set_drvdata(wm831x->dev, wm831x);
1430 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1431 if (ret < 0) {
1432 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1433 goto err;
1435 if (ret != 0x6204) {
1436 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1437 ret = -EINVAL;
1438 goto err;
1441 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1442 if (ret < 0) {
1443 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1444 goto err;
1446 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1448 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1449 if (ret < 0) {
1450 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1451 goto err;
1454 /* Some engineering samples do not have the ID set, rely on
1455 * the device being registered correctly.
1457 if (ret == 0) {
1458 dev_info(wm831x->dev, "Device is an engineering sample\n");
1459 ret = id;
1462 switch (ret) {
1463 case WM8310:
1464 parent = WM8310;
1465 wm831x->num_gpio = 16;
1466 if (rev > 0) {
1467 wm831x->has_gpio_ena = 1;
1468 wm831x->has_cs_sts = 1;
1471 dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
1472 break;
1474 case WM8311:
1475 parent = WM8311;
1476 wm831x->num_gpio = 16;
1477 if (rev > 0) {
1478 wm831x->has_gpio_ena = 1;
1479 wm831x->has_cs_sts = 1;
1482 dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
1483 break;
1485 case WM8312:
1486 parent = WM8312;
1487 wm831x->num_gpio = 16;
1488 if (rev > 0) {
1489 wm831x->has_gpio_ena = 1;
1490 wm831x->has_cs_sts = 1;
1493 dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
1494 break;
1496 case WM8320:
1497 parent = WM8320;
1498 wm831x->num_gpio = 12;
1499 dev_info(wm831x->dev, "WM8320 revision %c\n", 'A' + rev);
1500 break;
1502 default:
1503 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1504 ret = -EINVAL;
1505 goto err;
1508 /* This will need revisiting in future but is OK for all
1509 * current parts.
1511 if (parent != id)
1512 dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
1513 id);
1515 /* Bootstrap the user key */
1516 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1517 if (ret < 0) {
1518 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1519 goto err;
1521 if (ret != 0) {
1522 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1523 ret);
1524 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1526 wm831x->locked = 1;
1528 if (pdata && pdata->pre_init) {
1529 ret = pdata->pre_init(wm831x);
1530 if (ret != 0) {
1531 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1532 goto err;
1536 ret = wm831x_irq_init(wm831x, irq);
1537 if (ret != 0)
1538 goto err;
1540 if (wm831x->irq_base) {
1541 ret = request_threaded_irq(wm831x->irq_base +
1542 WM831X_IRQ_AUXADC_DATA,
1543 NULL, wm831x_auxadc_irq, 0,
1544 "auxadc", wm831x);
1545 if (ret < 0)
1546 dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
1547 ret);
1550 /* The core device is up, instantiate the subdevices. */
1551 switch (parent) {
1552 case WM8310:
1553 ret = mfd_add_devices(wm831x->dev, -1,
1554 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1555 NULL, wm831x->irq_base);
1556 break;
1558 case WM8311:
1559 ret = mfd_add_devices(wm831x->dev, -1,
1560 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1561 NULL, wm831x->irq_base);
1562 break;
1564 case WM8312:
1565 ret = mfd_add_devices(wm831x->dev, -1,
1566 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1567 NULL, wm831x->irq_base);
1568 break;
1570 case WM8320:
1571 ret = mfd_add_devices(wm831x->dev, -1,
1572 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1573 NULL, 0);
1574 break;
1576 default:
1577 /* If this happens the bus probe function is buggy */
1578 BUG();
1581 if (ret != 0) {
1582 dev_err(wm831x->dev, "Failed to add children\n");
1583 goto err_irq;
1586 if (pdata && pdata->backlight) {
1587 /* Treat errors as non-critical */
1588 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1589 ARRAY_SIZE(backlight_devs), NULL,
1590 wm831x->irq_base);
1591 if (ret < 0)
1592 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1593 ret);
1596 wm831x_otp_init(wm831x);
1598 if (pdata && pdata->post_init) {
1599 ret = pdata->post_init(wm831x);
1600 if (ret != 0) {
1601 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1602 goto err_irq;
1606 return 0;
1608 err_irq:
1609 wm831x_irq_exit(wm831x);
1610 err:
1611 mfd_remove_devices(wm831x->dev);
1612 kfree(wm831x);
1613 return ret;
1616 static void wm831x_device_exit(struct wm831x *wm831x)
1618 wm831x_otp_exit(wm831x);
1619 mfd_remove_devices(wm831x->dev);
1620 if (wm831x->irq_base)
1621 free_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, wm831x);
1622 wm831x_irq_exit(wm831x);
1623 kfree(wm831x);
1626 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
1627 int bytes, void *dest)
1629 struct i2c_client *i2c = wm831x->control_data;
1630 int ret;
1631 u16 r = cpu_to_be16(reg);
1633 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
1634 if (ret < 0)
1635 return ret;
1636 if (ret != 2)
1637 return -EIO;
1639 ret = i2c_master_recv(i2c, dest, bytes);
1640 if (ret < 0)
1641 return ret;
1642 if (ret != bytes)
1643 return -EIO;
1644 return 0;
1647 /* Currently we allocate the write buffer on the stack; this is OK for
1648 * small writes - if we need to do large writes this will need to be
1649 * revised.
1651 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
1652 int bytes, void *src)
1654 struct i2c_client *i2c = wm831x->control_data;
1655 unsigned char msg[bytes + 2];
1656 int ret;
1658 reg = cpu_to_be16(reg);
1659 memcpy(&msg[0], &reg, 2);
1660 memcpy(&msg[2], src, bytes);
1662 ret = i2c_master_send(i2c, msg, bytes + 2);
1663 if (ret < 0)
1664 return ret;
1665 if (ret < bytes + 2)
1666 return -EIO;
1668 return 0;
1671 static int wm831x_i2c_probe(struct i2c_client *i2c,
1672 const struct i2c_device_id *id)
1674 struct wm831x *wm831x;
1676 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
1677 if (wm831x == NULL) {
1678 kfree(i2c);
1679 return -ENOMEM;
1682 i2c_set_clientdata(i2c, wm831x);
1683 wm831x->dev = &i2c->dev;
1684 wm831x->control_data = i2c;
1685 wm831x->read_dev = wm831x_i2c_read_device;
1686 wm831x->write_dev = wm831x_i2c_write_device;
1688 return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
1691 static int wm831x_i2c_remove(struct i2c_client *i2c)
1693 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1695 wm831x_device_exit(wm831x);
1697 return 0;
1700 static const struct i2c_device_id wm831x_i2c_id[] = {
1701 { "wm8310", WM8310 },
1702 { "wm8311", WM8311 },
1703 { "wm8312", WM8312 },
1704 { "wm8320", WM8320 },
1707 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
1710 static struct i2c_driver wm831x_i2c_driver = {
1711 .driver = {
1712 .name = "wm831x",
1713 .owner = THIS_MODULE,
1715 .probe = wm831x_i2c_probe,
1716 .remove = wm831x_i2c_remove,
1717 .id_table = wm831x_i2c_id,
1720 static int __init wm831x_i2c_init(void)
1722 int ret;
1724 ret = i2c_add_driver(&wm831x_i2c_driver);
1725 if (ret != 0)
1726 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
1728 return ret;
1730 subsys_initcall(wm831x_i2c_init);
1732 static void __exit wm831x_i2c_exit(void)
1734 i2c_del_driver(&wm831x_i2c_driver);
1736 module_exit(wm831x_i2c_exit);
1738 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1739 MODULE_LICENSE("GPL");
1740 MODULE_AUTHOR("Mark Brown");