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>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 #include <linux/mfd/wm831x/irq.h>
25 #include <linux/mfd/wm831x/auxadc.h>
33 static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
40 case WM831X_DC4_CONTROL:
41 case WM831X_ON_PIN_CONTROL:
42 case WM831X_BACKUP_CHARGER_CONTROL:
43 case WM831X_CHARGER_CONTROL_1:
44 case WM831X_CHARGER_CONTROL_2:
53 * wm831x_reg_unlock: Unlock user keyed registers
55 * The WM831x has a user key preventing writes to particularly
56 * critical registers. This function locks those registers,
57 * allowing writes to them.
59 void wm831x_reg_lock(struct wm831x *wm831x)
63 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
65 dev_vdbg(wm831x->dev, "Registers locked\n");
67 mutex_lock(&wm831x->io_lock);
68 WARN_ON(wm831x->locked);
70 mutex_unlock(&wm831x->io_lock);
72 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
76 EXPORT_SYMBOL_GPL(wm831x_reg_lock);
79 * wm831x_reg_unlock: Unlock user keyed registers
81 * The WM831x has a user key preventing writes to particularly
82 * critical registers. This function locks those registers,
83 * preventing spurious writes.
85 int wm831x_reg_unlock(struct wm831x *wm831x)
89 /* 0x9716 is the value required to unlock the registers */
90 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
92 dev_vdbg(wm831x->dev, "Registers unlocked\n");
94 mutex_lock(&wm831x->io_lock);
95 WARN_ON(!wm831x->locked);
97 mutex_unlock(&wm831x->io_lock);
102 EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
104 static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
105 int bytes, void *dest)
113 ret = wm831x->read_dev(wm831x, reg, bytes, dest);
117 for (i = 0; i < bytes / 2; i++) {
118 buf[i] = be16_to_cpu(buf[i]);
120 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
121 buf[i], reg + i, reg + i);
128 * wm831x_reg_read: Read a single WM831x register.
130 * @wm831x: Device to read from.
131 * @reg: Register to read.
133 int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
138 mutex_lock(&wm831x->io_lock);
140 ret = wm831x_read(wm831x, reg, 2, &val);
142 mutex_unlock(&wm831x->io_lock);
149 EXPORT_SYMBOL_GPL(wm831x_reg_read);
152 * wm831x_bulk_read: Read multiple WM831x registers
154 * @wm831x: Device to read from
155 * @reg: First register
156 * @count: Number of registers
157 * @buf: Buffer to fill.
159 int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
164 mutex_lock(&wm831x->io_lock);
166 ret = wm831x_read(wm831x, reg, count * 2, buf);
168 mutex_unlock(&wm831x->io_lock);
172 EXPORT_SYMBOL_GPL(wm831x_bulk_read);
174 static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
175 int bytes, void *src)
183 for (i = 0; i < bytes / 2; i++) {
184 if (wm831x_reg_locked(wm831x, reg))
187 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
188 buf[i], reg + i, reg + i);
190 buf[i] = cpu_to_be16(buf[i]);
193 return wm831x->write_dev(wm831x, reg, bytes, src);
197 * wm831x_reg_write: Write a single WM831x register.
199 * @wm831x: Device to write to.
200 * @reg: Register to write to.
201 * @val: Value to write.
203 int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
208 mutex_lock(&wm831x->io_lock);
210 ret = wm831x_write(wm831x, reg, 2, &val);
212 mutex_unlock(&wm831x->io_lock);
216 EXPORT_SYMBOL_GPL(wm831x_reg_write);
219 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
221 * @wm831x: Device to write to.
222 * @reg: Register to write to.
223 * @mask: Mask of bits to set.
224 * @val: Value to set (unshifted)
226 int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
227 unsigned short mask, unsigned short val)
232 mutex_lock(&wm831x->io_lock);
234 ret = wm831x_read(wm831x, reg, 2, &r);
241 ret = wm831x_write(wm831x, reg, 2, &r);
244 mutex_unlock(&wm831x->io_lock);
248 EXPORT_SYMBOL_GPL(wm831x_set_bits);
251 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
253 * @wm831x: Device to read from.
254 * @input: AUXADC input to read.
256 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
261 mutex_lock(&wm831x->auxadc_lock);
263 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
264 WM831X_AUX_ENA, WM831X_AUX_ENA);
266 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
270 /* We force a single source at present */
272 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
275 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
279 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
280 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
282 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
289 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_CONTROL);
291 ret = WM831X_AUX_CVT_ENA;
292 } while ((ret & WM831X_AUX_CVT_ENA) && --tries);
294 if (ret & WM831X_AUX_CVT_ENA) {
295 dev_err(wm831x->dev, "Timed out reading AUXADC\n");
300 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
302 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
304 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
305 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
308 src = WM831X_AUX_CAL;
311 dev_err(wm831x->dev, "Data from source %d not %d\n",
315 ret &= WM831X_AUX_DATA_MASK;
320 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
322 mutex_unlock(&wm831x->auxadc_lock);
325 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
328 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
330 * @wm831x: Device to read from.
331 * @input: AUXADC input to read.
333 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
337 ret = wm831x_auxadc_read(wm831x, input);
345 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
347 static struct resource wm831x_dcdc1_resources[] = {
349 .start = WM831X_DC1_CONTROL_1,
350 .end = WM831X_DC1_DVS_CONTROL,
351 .flags = IORESOURCE_IO,
355 .start = WM831X_IRQ_UV_DC1,
356 .end = WM831X_IRQ_UV_DC1,
357 .flags = IORESOURCE_IRQ,
361 .start = WM831X_IRQ_HC_DC1,
362 .end = WM831X_IRQ_HC_DC1,
363 .flags = IORESOURCE_IRQ,
368 static struct resource wm831x_dcdc2_resources[] = {
370 .start = WM831X_DC2_CONTROL_1,
371 .end = WM831X_DC2_DVS_CONTROL,
372 .flags = IORESOURCE_IO,
376 .start = WM831X_IRQ_UV_DC2,
377 .end = WM831X_IRQ_UV_DC2,
378 .flags = IORESOURCE_IRQ,
382 .start = WM831X_IRQ_HC_DC2,
383 .end = WM831X_IRQ_HC_DC2,
384 .flags = IORESOURCE_IRQ,
388 static struct resource wm831x_dcdc3_resources[] = {
390 .start = WM831X_DC3_CONTROL_1,
391 .end = WM831X_DC3_SLEEP_CONTROL,
392 .flags = IORESOURCE_IO,
396 .start = WM831X_IRQ_UV_DC3,
397 .end = WM831X_IRQ_UV_DC3,
398 .flags = IORESOURCE_IRQ,
402 static struct resource wm831x_dcdc4_resources[] = {
404 .start = WM831X_DC4_CONTROL,
405 .end = WM831X_DC4_SLEEP_CONTROL,
406 .flags = IORESOURCE_IO,
410 .start = WM831X_IRQ_UV_DC4,
411 .end = WM831X_IRQ_UV_DC4,
412 .flags = IORESOURCE_IRQ,
416 static struct resource wm831x_gpio_resources[] = {
418 .start = WM831X_IRQ_GPIO_1,
419 .end = WM831X_IRQ_GPIO_16,
420 .flags = IORESOURCE_IRQ,
424 static struct resource wm831x_isink1_resources[] = {
426 .start = WM831X_CURRENT_SINK_1,
427 .end = WM831X_CURRENT_SINK_1,
428 .flags = IORESOURCE_IO,
431 .start = WM831X_IRQ_CS1,
432 .end = WM831X_IRQ_CS1,
433 .flags = IORESOURCE_IRQ,
437 static struct resource wm831x_isink2_resources[] = {
439 .start = WM831X_CURRENT_SINK_2,
440 .end = WM831X_CURRENT_SINK_2,
441 .flags = IORESOURCE_IO,
444 .start = WM831X_IRQ_CS2,
445 .end = WM831X_IRQ_CS2,
446 .flags = IORESOURCE_IRQ,
450 static struct resource wm831x_ldo1_resources[] = {
452 .start = WM831X_LDO1_CONTROL,
453 .end = WM831X_LDO1_SLEEP_CONTROL,
454 .flags = IORESOURCE_IO,
458 .start = WM831X_IRQ_UV_LDO1,
459 .end = WM831X_IRQ_UV_LDO1,
460 .flags = IORESOURCE_IRQ,
464 static struct resource wm831x_ldo2_resources[] = {
466 .start = WM831X_LDO2_CONTROL,
467 .end = WM831X_LDO2_SLEEP_CONTROL,
468 .flags = IORESOURCE_IO,
472 .start = WM831X_IRQ_UV_LDO2,
473 .end = WM831X_IRQ_UV_LDO2,
474 .flags = IORESOURCE_IRQ,
478 static struct resource wm831x_ldo3_resources[] = {
480 .start = WM831X_LDO3_CONTROL,
481 .end = WM831X_LDO3_SLEEP_CONTROL,
482 .flags = IORESOURCE_IO,
486 .start = WM831X_IRQ_UV_LDO3,
487 .end = WM831X_IRQ_UV_LDO3,
488 .flags = IORESOURCE_IRQ,
492 static struct resource wm831x_ldo4_resources[] = {
494 .start = WM831X_LDO4_CONTROL,
495 .end = WM831X_LDO4_SLEEP_CONTROL,
496 .flags = IORESOURCE_IO,
500 .start = WM831X_IRQ_UV_LDO4,
501 .end = WM831X_IRQ_UV_LDO4,
502 .flags = IORESOURCE_IRQ,
506 static struct resource wm831x_ldo5_resources[] = {
508 .start = WM831X_LDO5_CONTROL,
509 .end = WM831X_LDO5_SLEEP_CONTROL,
510 .flags = IORESOURCE_IO,
514 .start = WM831X_IRQ_UV_LDO5,
515 .end = WM831X_IRQ_UV_LDO5,
516 .flags = IORESOURCE_IRQ,
520 static struct resource wm831x_ldo6_resources[] = {
522 .start = WM831X_LDO6_CONTROL,
523 .end = WM831X_LDO6_SLEEP_CONTROL,
524 .flags = IORESOURCE_IO,
528 .start = WM831X_IRQ_UV_LDO6,
529 .end = WM831X_IRQ_UV_LDO6,
530 .flags = IORESOURCE_IRQ,
534 static struct resource wm831x_ldo7_resources[] = {
536 .start = WM831X_LDO7_CONTROL,
537 .end = WM831X_LDO7_SLEEP_CONTROL,
538 .flags = IORESOURCE_IO,
542 .start = WM831X_IRQ_UV_LDO7,
543 .end = WM831X_IRQ_UV_LDO7,
544 .flags = IORESOURCE_IRQ,
548 static struct resource wm831x_ldo8_resources[] = {
550 .start = WM831X_LDO8_CONTROL,
551 .end = WM831X_LDO8_SLEEP_CONTROL,
552 .flags = IORESOURCE_IO,
556 .start = WM831X_IRQ_UV_LDO8,
557 .end = WM831X_IRQ_UV_LDO8,
558 .flags = IORESOURCE_IRQ,
562 static struct resource wm831x_ldo9_resources[] = {
564 .start = WM831X_LDO9_CONTROL,
565 .end = WM831X_LDO9_SLEEP_CONTROL,
566 .flags = IORESOURCE_IO,
570 .start = WM831X_IRQ_UV_LDO9,
571 .end = WM831X_IRQ_UV_LDO9,
572 .flags = IORESOURCE_IRQ,
576 static struct resource wm831x_ldo10_resources[] = {
578 .start = WM831X_LDO10_CONTROL,
579 .end = WM831X_LDO10_SLEEP_CONTROL,
580 .flags = IORESOURCE_IO,
584 .start = WM831X_IRQ_UV_LDO10,
585 .end = WM831X_IRQ_UV_LDO10,
586 .flags = IORESOURCE_IRQ,
590 static struct resource wm831x_ldo11_resources[] = {
592 .start = WM831X_LDO11_ON_CONTROL,
593 .end = WM831X_LDO11_SLEEP_CONTROL,
594 .flags = IORESOURCE_IO,
598 static struct resource wm831x_on_resources[] = {
600 .start = WM831X_IRQ_ON,
601 .end = WM831X_IRQ_ON,
602 .flags = IORESOURCE_IRQ,
607 static struct resource wm831x_power_resources[] = {
610 .start = WM831X_IRQ_PPM_SYSLO,
611 .end = WM831X_IRQ_PPM_SYSLO,
612 .flags = IORESOURCE_IRQ,
616 .start = WM831X_IRQ_PPM_PWR_SRC,
617 .end = WM831X_IRQ_PPM_PWR_SRC,
618 .flags = IORESOURCE_IRQ,
622 .start = WM831X_IRQ_PPM_USB_CURR,
623 .end = WM831X_IRQ_PPM_USB_CURR,
624 .flags = IORESOURCE_IRQ,
628 .start = WM831X_IRQ_CHG_BATT_HOT,
629 .end = WM831X_IRQ_CHG_BATT_HOT,
630 .flags = IORESOURCE_IRQ,
634 .start = WM831X_IRQ_CHG_BATT_COLD,
635 .end = WM831X_IRQ_CHG_BATT_COLD,
636 .flags = IORESOURCE_IRQ,
640 .start = WM831X_IRQ_CHG_BATT_FAIL,
641 .end = WM831X_IRQ_CHG_BATT_FAIL,
642 .flags = IORESOURCE_IRQ,
646 .start = WM831X_IRQ_CHG_OV,
647 .end = WM831X_IRQ_CHG_OV,
648 .flags = IORESOURCE_IRQ,
652 .start = WM831X_IRQ_CHG_END,
653 .end = WM831X_IRQ_CHG_END,
654 .flags = IORESOURCE_IRQ,
658 .start = WM831X_IRQ_CHG_TO,
659 .end = WM831X_IRQ_CHG_TO,
660 .flags = IORESOURCE_IRQ,
664 .start = WM831X_IRQ_CHG_MODE,
665 .end = WM831X_IRQ_CHG_MODE,
666 .flags = IORESOURCE_IRQ,
670 .start = WM831X_IRQ_CHG_START,
671 .end = WM831X_IRQ_CHG_START,
672 .flags = IORESOURCE_IRQ,
676 static struct resource wm831x_rtc_resources[] = {
679 .start = WM831X_IRQ_RTC_PER,
680 .end = WM831X_IRQ_RTC_PER,
681 .flags = IORESOURCE_IRQ,
685 .start = WM831X_IRQ_RTC_ALM,
686 .end = WM831X_IRQ_RTC_ALM,
687 .flags = IORESOURCE_IRQ,
691 static struct resource wm831x_status1_resources[] = {
693 .start = WM831X_STATUS_LED_1,
694 .end = WM831X_STATUS_LED_1,
695 .flags = IORESOURCE_IO,
699 static struct resource wm831x_status2_resources[] = {
701 .start = WM831X_STATUS_LED_2,
702 .end = WM831X_STATUS_LED_2,
703 .flags = IORESOURCE_IO,
707 static struct resource wm831x_touch_resources[] = {
710 .start = WM831X_IRQ_TCHPD,
711 .end = WM831X_IRQ_TCHPD,
712 .flags = IORESOURCE_IRQ,
716 .start = WM831X_IRQ_TCHDATA,
717 .end = WM831X_IRQ_TCHDATA,
718 .flags = IORESOURCE_IRQ,
722 static struct resource wm831x_wdt_resources[] = {
724 .start = WM831X_IRQ_WDOG_TO,
725 .end = WM831X_IRQ_WDOG_TO,
726 .flags = IORESOURCE_IRQ,
730 static struct mfd_cell wm8310_devs[] = {
732 .name = "wm831x-buckv",
734 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
735 .resources = wm831x_dcdc1_resources,
738 .name = "wm831x-buckv",
740 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
741 .resources = wm831x_dcdc2_resources,
744 .name = "wm831x-buckp",
746 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
747 .resources = wm831x_dcdc3_resources,
750 .name = "wm831x-boostp",
752 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
753 .resources = wm831x_dcdc4_resources,
756 .name = "wm831x-epe",
760 .name = "wm831x-epe",
764 .name = "wm831x-gpio",
765 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
766 .resources = wm831x_gpio_resources,
769 .name = "wm831x-hwmon",
772 .name = "wm831x-isink",
774 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
775 .resources = wm831x_isink1_resources,
778 .name = "wm831x-isink",
780 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
781 .resources = wm831x_isink2_resources,
784 .name = "wm831x-ldo",
786 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
787 .resources = wm831x_ldo1_resources,
790 .name = "wm831x-ldo",
792 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
793 .resources = wm831x_ldo2_resources,
796 .name = "wm831x-ldo",
798 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
799 .resources = wm831x_ldo3_resources,
802 .name = "wm831x-ldo",
804 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
805 .resources = wm831x_ldo4_resources,
808 .name = "wm831x-ldo",
810 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
811 .resources = wm831x_ldo5_resources,
814 .name = "wm831x-ldo",
816 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
817 .resources = wm831x_ldo6_resources,
820 .name = "wm831x-aldo",
822 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
823 .resources = wm831x_ldo7_resources,
826 .name = "wm831x-aldo",
828 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
829 .resources = wm831x_ldo8_resources,
832 .name = "wm831x-aldo",
834 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
835 .resources = wm831x_ldo9_resources,
838 .name = "wm831x-aldo",
840 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
841 .resources = wm831x_ldo10_resources,
844 .name = "wm831x-alive-ldo",
846 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
847 .resources = wm831x_ldo11_resources,
851 .num_resources = ARRAY_SIZE(wm831x_on_resources),
852 .resources = wm831x_on_resources,
855 .name = "wm831x-power",
856 .num_resources = ARRAY_SIZE(wm831x_power_resources),
857 .resources = wm831x_power_resources,
860 .name = "wm831x-rtc",
861 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
862 .resources = wm831x_rtc_resources,
865 .name = "wm831x-status",
867 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
868 .resources = wm831x_status1_resources,
871 .name = "wm831x-status",
873 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
874 .resources = wm831x_status2_resources,
877 .name = "wm831x-watchdog",
878 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
879 .resources = wm831x_wdt_resources,
883 static struct mfd_cell wm8311_devs[] = {
885 .name = "wm831x-buckv",
887 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
888 .resources = wm831x_dcdc1_resources,
891 .name = "wm831x-buckv",
893 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
894 .resources = wm831x_dcdc2_resources,
897 .name = "wm831x-buckp",
899 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
900 .resources = wm831x_dcdc3_resources,
903 .name = "wm831x-boostp",
905 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
906 .resources = wm831x_dcdc4_resources,
909 .name = "wm831x-epe",
913 .name = "wm831x-epe",
917 .name = "wm831x-gpio",
918 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
919 .resources = wm831x_gpio_resources,
922 .name = "wm831x-hwmon",
925 .name = "wm831x-isink",
927 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
928 .resources = wm831x_isink1_resources,
931 .name = "wm831x-isink",
933 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
934 .resources = wm831x_isink2_resources,
937 .name = "wm831x-ldo",
939 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
940 .resources = wm831x_ldo1_resources,
943 .name = "wm831x-ldo",
945 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
946 .resources = wm831x_ldo2_resources,
949 .name = "wm831x-ldo",
951 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
952 .resources = wm831x_ldo3_resources,
955 .name = "wm831x-ldo",
957 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
958 .resources = wm831x_ldo4_resources,
961 .name = "wm831x-ldo",
963 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
964 .resources = wm831x_ldo5_resources,
967 .name = "wm831x-aldo",
969 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
970 .resources = wm831x_ldo7_resources,
973 .name = "wm831x-alive-ldo",
975 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
976 .resources = wm831x_ldo11_resources,
980 .num_resources = ARRAY_SIZE(wm831x_on_resources),
981 .resources = wm831x_on_resources,
984 .name = "wm831x-power",
985 .num_resources = ARRAY_SIZE(wm831x_power_resources),
986 .resources = wm831x_power_resources,
989 .name = "wm831x-rtc",
990 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
991 .resources = wm831x_rtc_resources,
994 .name = "wm831x-status",
996 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
997 .resources = wm831x_status1_resources,
1000 .name = "wm831x-status",
1002 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1003 .resources = wm831x_status2_resources,
1006 .name = "wm831x-touch",
1007 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1008 .resources = wm831x_touch_resources,
1011 .name = "wm831x-watchdog",
1012 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1013 .resources = wm831x_wdt_resources,
1017 static struct mfd_cell wm8312_devs[] = {
1019 .name = "wm831x-buckv",
1021 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1022 .resources = wm831x_dcdc1_resources,
1025 .name = "wm831x-buckv",
1027 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1028 .resources = wm831x_dcdc2_resources,
1031 .name = "wm831x-buckp",
1033 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1034 .resources = wm831x_dcdc3_resources,
1037 .name = "wm831x-boostp",
1039 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1040 .resources = wm831x_dcdc4_resources,
1043 .name = "wm831x-epe",
1047 .name = "wm831x-epe",
1051 .name = "wm831x-gpio",
1052 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1053 .resources = wm831x_gpio_resources,
1056 .name = "wm831x-hwmon",
1059 .name = "wm831x-isink",
1061 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1062 .resources = wm831x_isink1_resources,
1065 .name = "wm831x-isink",
1067 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1068 .resources = wm831x_isink2_resources,
1071 .name = "wm831x-ldo",
1073 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1074 .resources = wm831x_ldo1_resources,
1077 .name = "wm831x-ldo",
1079 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1080 .resources = wm831x_ldo2_resources,
1083 .name = "wm831x-ldo",
1085 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1086 .resources = wm831x_ldo3_resources,
1089 .name = "wm831x-ldo",
1091 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1092 .resources = wm831x_ldo4_resources,
1095 .name = "wm831x-ldo",
1097 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1098 .resources = wm831x_ldo5_resources,
1101 .name = "wm831x-ldo",
1103 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1104 .resources = wm831x_ldo6_resources,
1107 .name = "wm831x-aldo",
1109 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1110 .resources = wm831x_ldo7_resources,
1113 .name = "wm831x-aldo",
1115 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1116 .resources = wm831x_ldo8_resources,
1119 .name = "wm831x-aldo",
1121 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1122 .resources = wm831x_ldo9_resources,
1125 .name = "wm831x-aldo",
1127 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1128 .resources = wm831x_ldo10_resources,
1131 .name = "wm831x-alive-ldo",
1133 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1134 .resources = wm831x_ldo11_resources,
1137 .name = "wm831x-on",
1138 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1139 .resources = wm831x_on_resources,
1142 .name = "wm831x-power",
1143 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1144 .resources = wm831x_power_resources,
1147 .name = "wm831x-rtc",
1148 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1149 .resources = wm831x_rtc_resources,
1152 .name = "wm831x-status",
1154 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1155 .resources = wm831x_status1_resources,
1158 .name = "wm831x-status",
1160 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1161 .resources = wm831x_status2_resources,
1164 .name = "wm831x-touch",
1165 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1166 .resources = wm831x_touch_resources,
1169 .name = "wm831x-watchdog",
1170 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1171 .resources = wm831x_wdt_resources,
1175 static struct mfd_cell backlight_devs[] = {
1177 .name = "wm831x-backlight",
1182 * Instantiate the generic non-control parts of the device.
1184 static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1186 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1188 enum wm831x_parent parent;
1191 mutex_init(&wm831x->io_lock);
1192 mutex_init(&wm831x->key_lock);
1193 mutex_init(&wm831x->auxadc_lock);
1194 dev_set_drvdata(wm831x->dev, wm831x);
1196 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1198 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1201 if (ret != 0x6204) {
1202 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1207 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1209 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1212 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1214 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1216 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1225 dev_info(wm831x->dev, "WM8310 revision %c\n",
1235 dev_info(wm831x->dev, "WM8311 revision %c\n",
1245 dev_info(wm831x->dev, "WM8312 revision %c\n",
1252 /* Some engineering samples do not have the ID set,
1253 * rely on the device being registered correctly.
1254 * This will need revisiting for future devices with
1260 dev_info(wm831x->dev, "WM831%d ES revision %c\n",
1267 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1272 /* This will need revisiting in future but is OK for all
1276 dev_warn(wm831x->dev, "Device was registered as a WM831%lu\n",
1279 /* Bootstrap the user key */
1280 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1282 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1286 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1288 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1292 if (pdata && pdata->pre_init) {
1293 ret = pdata->pre_init(wm831x);
1295 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1300 ret = wm831x_irq_init(wm831x, irq);
1304 /* The core device is up, instantiate the subdevices. */
1307 ret = mfd_add_devices(wm831x->dev, -1,
1308 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1313 ret = mfd_add_devices(wm831x->dev, -1,
1314 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1319 ret = mfd_add_devices(wm831x->dev, -1,
1320 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1325 /* If this happens the bus probe function is buggy */
1330 dev_err(wm831x->dev, "Failed to add children\n");
1334 if (pdata && pdata->backlight) {
1335 /* Treat errors as non-critical */
1336 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1337 ARRAY_SIZE(backlight_devs), NULL, 0);
1339 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1343 if (pdata && pdata->post_init) {
1344 ret = pdata->post_init(wm831x);
1346 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1354 wm831x_irq_exit(wm831x);
1356 mfd_remove_devices(wm831x->dev);
1361 static void wm831x_device_exit(struct wm831x *wm831x)
1363 mfd_remove_devices(wm831x->dev);
1364 wm831x_irq_exit(wm831x);
1368 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
1369 int bytes, void *dest)
1371 struct i2c_client *i2c = wm831x->control_data;
1373 u16 r = cpu_to_be16(reg);
1375 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
1381 ret = i2c_master_recv(i2c, dest, bytes);
1389 /* Currently we allocate the write buffer on the stack; this is OK for
1390 * small writes - if we need to do large writes this will need to be
1393 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
1394 int bytes, void *src)
1396 struct i2c_client *i2c = wm831x->control_data;
1397 unsigned char msg[bytes + 2];
1400 reg = cpu_to_be16(reg);
1401 memcpy(&msg[0], ®, 2);
1402 memcpy(&msg[2], src, bytes);
1404 ret = i2c_master_send(i2c, msg, bytes + 2);
1407 if (ret < bytes + 2)
1413 static int wm831x_i2c_probe(struct i2c_client *i2c,
1414 const struct i2c_device_id *id)
1416 struct wm831x *wm831x;
1418 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
1419 if (wm831x == NULL) {
1424 i2c_set_clientdata(i2c, wm831x);
1425 wm831x->dev = &i2c->dev;
1426 wm831x->control_data = i2c;
1427 wm831x->read_dev = wm831x_i2c_read_device;
1428 wm831x->write_dev = wm831x_i2c_write_device;
1430 return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
1433 static int wm831x_i2c_remove(struct i2c_client *i2c)
1435 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1437 wm831x_device_exit(wm831x);
1442 static const struct i2c_device_id wm831x_i2c_id[] = {
1443 { "wm8310", WM8310 },
1444 { "wm8311", WM8311 },
1445 { "wm8312", WM8312 },
1448 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
1451 static struct i2c_driver wm831x_i2c_driver = {
1454 .owner = THIS_MODULE,
1456 .probe = wm831x_i2c_probe,
1457 .remove = wm831x_i2c_remove,
1458 .id_table = wm831x_i2c_id,
1461 static int __init wm831x_i2c_init(void)
1465 ret = i2c_add_driver(&wm831x_i2c_driver);
1467 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
1471 subsys_initcall(wm831x_i2c_init);
1473 static void __exit wm831x_i2c_exit(void)
1475 i2c_del_driver(&wm831x_i2c_driver);
1477 module_exit(wm831x_i2c_exit);
1479 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1480 MODULE_LICENSE("GPL");
1481 MODULE_AUTHOR("Mark Brown");