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/bcd.h>
18 #include <linux/delay.h>
19 #include <linux/mfd/core.h>
20 #include <linux/slab.h>
21 #include <linux/irq.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>
29 #include <linux/mfd/wm831x/pmu.h>
32 /* Current settings - values are 2*2^(reg_val/4) microamps. These are
33 * exported since they are used by multiple drivers.
35 extern int reboot_cmd_get(void);
36 int wm831x_isinkv_values[WM831X_ISINK_MAX_ISEL + 1] = {
94 EXPORT_SYMBOL_GPL(wm831x_isinkv_values);
96 static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
102 case WM831X_WATCHDOG:
103 case WM831X_DC4_CONTROL:
104 case WM831X_ON_PIN_CONTROL:
105 case WM831X_BACKUP_CHARGER_CONTROL:
106 case WM831X_CHARGER_CONTROL_1:
107 case WM831X_CHARGER_CONTROL_2:
116 * wm831x_reg_unlock: Unlock user keyed registers
118 * The WM831x has a user key preventing writes to particularly
119 * critical registers. This function locks those registers,
120 * allowing writes to them.
122 void wm831x_reg_lock(struct wm831x *wm831x)
126 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
128 dev_vdbg(wm831x->dev, "Registers locked\n");
130 mutex_lock(&wm831x->io_lock);
131 WARN_ON(wm831x->locked);
133 mutex_unlock(&wm831x->io_lock);
135 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
139 EXPORT_SYMBOL_GPL(wm831x_reg_lock);
142 * wm831x_reg_unlock: Unlock user keyed registers
144 * The WM831x has a user key preventing writes to particularly
145 * critical registers. This function locks those registers,
146 * preventing spurious writes.
148 int wm831x_reg_unlock(struct wm831x *wm831x)
152 /* 0x9716 is the value required to unlock the registers */
153 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
155 dev_vdbg(wm831x->dev, "Registers unlocked\n");
157 mutex_lock(&wm831x->io_lock);
158 WARN_ON(!wm831x->locked);
160 mutex_unlock(&wm831x->io_lock);
165 EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
167 static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
168 int bytes, void *dest)
176 ret = wm831x->read_dev(wm831x, reg, bytes, dest);
180 for (i = 0; i < bytes / 2; i++) {
181 buf[i] = be16_to_cpu(buf[i]);
183 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
184 buf[i], reg + i, reg + i);
191 * wm831x_reg_read: Read a single WM831x register.
193 * @wm831x: Device to read from.
194 * @reg: Register to read.
196 int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
201 mutex_lock(&wm831x->io_lock);
203 ret = wm831x_read(wm831x, reg, 2, &val);
205 mutex_unlock(&wm831x->io_lock);
212 EXPORT_SYMBOL_GPL(wm831x_reg_read);
215 * wm831x_bulk_read: Read multiple WM831x registers
217 * @wm831x: Device to read from
218 * @reg: First register
219 * @count: Number of registers
220 * @buf: Buffer to fill.
222 int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
227 mutex_lock(&wm831x->io_lock);
229 ret = wm831x_read(wm831x, reg, count * 2, buf);
231 mutex_unlock(&wm831x->io_lock);
235 EXPORT_SYMBOL_GPL(wm831x_bulk_read);
237 static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
238 int bytes, void *src)
246 for (i = 0; i < bytes / 2; i++) {
247 if (wm831x_reg_locked(wm831x, reg))
250 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
251 buf[i], reg + i, reg + i);
253 buf[i] = cpu_to_be16(buf[i]);
256 return wm831x->write_dev(wm831x, reg, bytes, src);
260 * wm831x_reg_write: Write a single WM831x register.
262 * @wm831x: Device to write to.
263 * @reg: Register to write to.
264 * @val: Value to write.
266 int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
271 mutex_lock(&wm831x->io_lock);
273 ret = wm831x_write(wm831x, reg, 2, &val);
275 mutex_unlock(&wm831x->io_lock);
279 EXPORT_SYMBOL_GPL(wm831x_reg_write);
282 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
284 * @wm831x: Device to write to.
285 * @reg: Register to write to.
286 * @mask: Mask of bits to set.
287 * @val: Value to set (unshifted)
289 int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
290 unsigned short mask, unsigned short val)
295 mutex_lock(&wm831x->io_lock);
297 ret = wm831x_read(wm831x, reg, 2, &r);
304 ret = wm831x_write(wm831x, reg, 2, &r);
307 mutex_unlock(&wm831x->io_lock);
311 EXPORT_SYMBOL_GPL(wm831x_set_bits);
314 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
316 * @wm831x: Device to read from.
317 * @input: AUXADC input to read.
319 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
321 int ret, src, irq_masked, timeout;
323 /* Are we using the interrupt? */
324 irq_masked = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_1_MASK);
325 irq_masked &= WM831X_AUXADC_DATA_EINT;
327 mutex_lock(&wm831x->auxadc_lock);
329 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
330 WM831X_AUX_ENA, WM831X_AUX_ENA);
332 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
336 /* We force a single source at present */
338 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
341 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
345 /* Clear any notification from a very late arriving interrupt */
346 try_wait_for_completion(&wm831x->auxadc_done);
348 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
349 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
351 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
356 /* If we're not using interrupts then poll the
357 * interrupt status register */
362 ret = wm831x_reg_read(wm831x,
363 WM831X_INTERRUPT_STATUS_1);
366 "ISR 1 read failed: %d\n", ret);
370 /* Did it complete? */
371 if (ret & WM831X_AUXADC_DATA_EINT) {
372 wm831x_reg_write(wm831x,
373 WM831X_INTERRUPT_STATUS_1,
374 WM831X_AUXADC_DATA_EINT);
378 "AUXADC conversion timeout\n");
384 /* If we are using interrupts then wait for the
385 * interrupt to complete. Use an extremely long
386 * timeout to handle situations with heavy load where
387 * the notification of the interrupt may be delayed by
388 * threaded IRQ handling. */
389 if (!wait_for_completion_timeout(&wm831x->auxadc_done,
390 msecs_to_jiffies(2000))) {
391 dev_err(wm831x->dev, "Timed out waiting for AUXADC\n");
397 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
399 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
401 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
402 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
405 src = WM831X_AUX_CAL;
408 dev_err(wm831x->dev, "Data from source %d not %d\n",
412 ret &= WM831X_AUX_DATA_MASK;
417 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
419 mutex_unlock(&wm831x->auxadc_lock);
422 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
424 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
426 struct wm831x *wm831x = irq_data;
428 complete(&wm831x->auxadc_done);
434 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
436 * @wm831x: Device to read from.
437 * @input: AUXADC input to read.
439 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
443 ret = wm831x_auxadc_read(wm831x, input);
451 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
453 static struct resource wm831x_dcdc1_resources[] = {
455 .start = WM831X_DC1_CONTROL_1,
456 .end = WM831X_DC1_DVS_CONTROL,
457 .flags = IORESOURCE_IO,
461 .start = WM831X_IRQ_UV_DC1,
462 .end = WM831X_IRQ_UV_DC1,
463 .flags = IORESOURCE_IRQ,
467 .start = WM831X_IRQ_HC_DC1,
468 .end = WM831X_IRQ_HC_DC1,
469 .flags = IORESOURCE_IRQ,
474 static struct resource wm831x_dcdc2_resources[] = {
476 .start = WM831X_DC2_CONTROL_1,
477 .end = WM831X_DC2_DVS_CONTROL,
478 .flags = IORESOURCE_IO,
482 .start = WM831X_IRQ_UV_DC2,
483 .end = WM831X_IRQ_UV_DC2,
484 .flags = IORESOURCE_IRQ,
488 .start = WM831X_IRQ_HC_DC2,
489 .end = WM831X_IRQ_HC_DC2,
490 .flags = IORESOURCE_IRQ,
494 static struct resource wm831x_dcdc3_resources[] = {
496 .start = WM831X_DC3_CONTROL_1,
497 .end = WM831X_DC3_SLEEP_CONTROL,
498 .flags = IORESOURCE_IO,
502 .start = WM831X_IRQ_UV_DC3,
503 .end = WM831X_IRQ_UV_DC3,
504 .flags = IORESOURCE_IRQ,
508 static struct resource wm831x_dcdc4_resources[] = {
510 .start = WM831X_DC4_CONTROL,
511 .end = WM831X_DC4_SLEEP_CONTROL,
512 .flags = IORESOURCE_IO,
516 .start = WM831X_IRQ_UV_DC4,
517 .end = WM831X_IRQ_UV_DC4,
518 .flags = IORESOURCE_IRQ,
522 static struct resource wm8320_dcdc4_buck_resources[] = {
524 .start = WM831X_DC4_CONTROL,
525 .end = WM832X_DC4_SLEEP_CONTROL,
526 .flags = IORESOURCE_IO,
530 .start = WM831X_IRQ_UV_DC4,
531 .end = WM831X_IRQ_UV_DC4,
532 .flags = IORESOURCE_IRQ,
536 static struct resource wm831x_gpio_resources[] = {
538 .start = WM831X_IRQ_GPIO_1,
539 .end = WM831X_IRQ_GPIO_16,
540 .flags = IORESOURCE_IRQ,
544 static struct resource wm831x_isink1_resources[] = {
546 .start = WM831X_CURRENT_SINK_1,
547 .end = WM831X_CURRENT_SINK_1,
548 .flags = IORESOURCE_IO,
551 .start = WM831X_IRQ_CS1,
552 .end = WM831X_IRQ_CS1,
553 .flags = IORESOURCE_IRQ,
557 static struct resource wm831x_isink2_resources[] = {
559 .start = WM831X_CURRENT_SINK_2,
560 .end = WM831X_CURRENT_SINK_2,
561 .flags = IORESOURCE_IO,
564 .start = WM831X_IRQ_CS2,
565 .end = WM831X_IRQ_CS2,
566 .flags = IORESOURCE_IRQ,
570 static struct resource wm831x_ldo1_resources[] = {
572 .start = WM831X_LDO1_CONTROL,
573 .end = WM831X_LDO1_SLEEP_CONTROL,
574 .flags = IORESOURCE_IO,
578 .start = WM831X_IRQ_UV_LDO1,
579 .end = WM831X_IRQ_UV_LDO1,
580 .flags = IORESOURCE_IRQ,
584 static struct resource wm831x_ldo2_resources[] = {
586 .start = WM831X_LDO2_CONTROL,
587 .end = WM831X_LDO2_SLEEP_CONTROL,
588 .flags = IORESOURCE_IO,
592 .start = WM831X_IRQ_UV_LDO2,
593 .end = WM831X_IRQ_UV_LDO2,
594 .flags = IORESOURCE_IRQ,
598 static struct resource wm831x_ldo3_resources[] = {
600 .start = WM831X_LDO3_CONTROL,
601 .end = WM831X_LDO3_SLEEP_CONTROL,
602 .flags = IORESOURCE_IO,
606 .start = WM831X_IRQ_UV_LDO3,
607 .end = WM831X_IRQ_UV_LDO3,
608 .flags = IORESOURCE_IRQ,
612 static struct resource wm831x_ldo4_resources[] = {
614 .start = WM831X_LDO4_CONTROL,
615 .end = WM831X_LDO4_SLEEP_CONTROL,
616 .flags = IORESOURCE_IO,
620 .start = WM831X_IRQ_UV_LDO4,
621 .end = WM831X_IRQ_UV_LDO4,
622 .flags = IORESOURCE_IRQ,
626 static struct resource wm831x_ldo5_resources[] = {
628 .start = WM831X_LDO5_CONTROL,
629 .end = WM831X_LDO5_SLEEP_CONTROL,
630 .flags = IORESOURCE_IO,
634 .start = WM831X_IRQ_UV_LDO5,
635 .end = WM831X_IRQ_UV_LDO5,
636 .flags = IORESOURCE_IRQ,
640 static struct resource wm831x_ldo6_resources[] = {
642 .start = WM831X_LDO6_CONTROL,
643 .end = WM831X_LDO6_SLEEP_CONTROL,
644 .flags = IORESOURCE_IO,
648 .start = WM831X_IRQ_UV_LDO6,
649 .end = WM831X_IRQ_UV_LDO6,
650 .flags = IORESOURCE_IRQ,
654 static struct resource wm831x_ldo7_resources[] = {
656 .start = WM831X_LDO7_CONTROL,
657 .end = WM831X_LDO7_SLEEP_CONTROL,
658 .flags = IORESOURCE_IO,
662 .start = WM831X_IRQ_UV_LDO7,
663 .end = WM831X_IRQ_UV_LDO7,
664 .flags = IORESOURCE_IRQ,
668 static struct resource wm831x_ldo8_resources[] = {
670 .start = WM831X_LDO8_CONTROL,
671 .end = WM831X_LDO8_SLEEP_CONTROL,
672 .flags = IORESOURCE_IO,
676 .start = WM831X_IRQ_UV_LDO8,
677 .end = WM831X_IRQ_UV_LDO8,
678 .flags = IORESOURCE_IRQ,
682 static struct resource wm831x_ldo9_resources[] = {
684 .start = WM831X_LDO9_CONTROL,
685 .end = WM831X_LDO9_SLEEP_CONTROL,
686 .flags = IORESOURCE_IO,
690 .start = WM831X_IRQ_UV_LDO9,
691 .end = WM831X_IRQ_UV_LDO9,
692 .flags = IORESOURCE_IRQ,
696 static struct resource wm831x_ldo10_resources[] = {
698 .start = WM831X_LDO10_CONTROL,
699 .end = WM831X_LDO10_SLEEP_CONTROL,
700 .flags = IORESOURCE_IO,
704 .start = WM831X_IRQ_UV_LDO10,
705 .end = WM831X_IRQ_UV_LDO10,
706 .flags = IORESOURCE_IRQ,
710 static struct resource wm831x_ldo11_resources[] = {
712 .start = WM831X_LDO11_ON_CONTROL,
713 .end = WM831X_LDO11_SLEEP_CONTROL,
714 .flags = IORESOURCE_IO,
718 static struct resource wm831x_on_resources[] = {
720 .start = WM831X_IRQ_ON,
721 .end = WM831X_IRQ_ON,
722 .flags = IORESOURCE_IRQ,
727 static struct resource wm831x_power_resources[] = {
730 .start = WM831X_IRQ_PPM_SYSLO,
731 .end = WM831X_IRQ_PPM_SYSLO,
732 .flags = IORESOURCE_IRQ,
736 .start = WM831X_IRQ_PPM_PWR_SRC,
737 .end = WM831X_IRQ_PPM_PWR_SRC,
738 .flags = IORESOURCE_IRQ,
742 .start = WM831X_IRQ_PPM_USB_CURR,
743 .end = WM831X_IRQ_PPM_USB_CURR,
744 .flags = IORESOURCE_IRQ,
748 .start = WM831X_IRQ_CHG_BATT_HOT,
749 .end = WM831X_IRQ_CHG_BATT_HOT,
750 .flags = IORESOURCE_IRQ,
754 .start = WM831X_IRQ_CHG_BATT_COLD,
755 .end = WM831X_IRQ_CHG_BATT_COLD,
756 .flags = IORESOURCE_IRQ,
760 .start = WM831X_IRQ_CHG_BATT_FAIL,
761 .end = WM831X_IRQ_CHG_BATT_FAIL,
762 .flags = IORESOURCE_IRQ,
766 .start = WM831X_IRQ_CHG_OV,
767 .end = WM831X_IRQ_CHG_OV,
768 .flags = IORESOURCE_IRQ,
772 .start = WM831X_IRQ_CHG_END,
773 .end = WM831X_IRQ_CHG_END,
774 .flags = IORESOURCE_IRQ,
778 .start = WM831X_IRQ_CHG_TO,
779 .end = WM831X_IRQ_CHG_TO,
780 .flags = IORESOURCE_IRQ,
784 .start = WM831X_IRQ_CHG_MODE,
785 .end = WM831X_IRQ_CHG_MODE,
786 .flags = IORESOURCE_IRQ,
790 .start = WM831X_IRQ_CHG_START,
791 .end = WM831X_IRQ_CHG_START,
792 .flags = IORESOURCE_IRQ,
796 static struct resource wm831x_rtc_resources[] = {
799 .start = WM831X_IRQ_RTC_PER,
800 .end = WM831X_IRQ_RTC_PER,
801 .flags = IORESOURCE_IRQ,
805 .start = WM831X_IRQ_RTC_ALM,
806 .end = WM831X_IRQ_RTC_ALM,
807 .flags = IORESOURCE_IRQ,
811 static struct resource wm831x_status1_resources[] = {
813 .start = WM831X_STATUS_LED_1,
814 .end = WM831X_STATUS_LED_1,
815 .flags = IORESOURCE_IO,
819 static struct resource wm831x_status2_resources[] = {
821 .start = WM831X_STATUS_LED_2,
822 .end = WM831X_STATUS_LED_2,
823 .flags = IORESOURCE_IO,
827 static struct resource wm831x_touch_resources[] = {
830 .start = WM831X_IRQ_TCHPD,
831 .end = WM831X_IRQ_TCHPD,
832 .flags = IORESOURCE_IRQ,
836 .start = WM831X_IRQ_TCHDATA,
837 .end = WM831X_IRQ_TCHDATA,
838 .flags = IORESOURCE_IRQ,
842 static struct resource wm831x_wdt_resources[] = {
844 .start = WM831X_IRQ_WDOG_TO,
845 .end = WM831X_IRQ_WDOG_TO,
846 .flags = IORESOURCE_IRQ,
850 static struct mfd_cell wm8310_devs[] = {
852 .name = "wm831x-backup",
855 .name = "wm831x-buckv",
857 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
858 .resources = wm831x_dcdc1_resources,
861 .name = "wm831x-buckv",
863 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
864 .resources = wm831x_dcdc2_resources,
867 .name = "wm831x-buckp",
869 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
870 .resources = wm831x_dcdc3_resources,
873 .name = "wm831x-boostp",
875 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
876 .resources = wm831x_dcdc4_resources,
879 .name = "wm831x-epe",
883 .name = "wm831x-epe",
887 .name = "wm831x-gpio",
888 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
889 .resources = wm831x_gpio_resources,
892 .name = "wm831x-hwmon",
895 .name = "wm831x-isink",
897 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
898 .resources = wm831x_isink1_resources,
901 .name = "wm831x-isink",
903 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
904 .resources = wm831x_isink2_resources,
907 .name = "wm831x-ldo",
909 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
910 .resources = wm831x_ldo1_resources,
913 .name = "wm831x-ldo",
915 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
916 .resources = wm831x_ldo2_resources,
919 .name = "wm831x-ldo",
921 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
922 .resources = wm831x_ldo3_resources,
925 .name = "wm831x-ldo",
927 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
928 .resources = wm831x_ldo4_resources,
931 .name = "wm831x-ldo",
933 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
934 .resources = wm831x_ldo5_resources,
937 .name = "wm831x-ldo",
939 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
940 .resources = wm831x_ldo6_resources,
943 .name = "wm831x-aldo",
945 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
946 .resources = wm831x_ldo7_resources,
949 .name = "wm831x-aldo",
951 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
952 .resources = wm831x_ldo8_resources,
955 .name = "wm831x-aldo",
957 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
958 .resources = wm831x_ldo9_resources,
961 .name = "wm831x-aldo",
963 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
964 .resources = wm831x_ldo10_resources,
967 .name = "wm831x-alive-ldo",
969 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
970 .resources = wm831x_ldo11_resources,
974 .num_resources = ARRAY_SIZE(wm831x_on_resources),
975 .resources = wm831x_on_resources,
978 .name = "wm831x-power",
979 .num_resources = ARRAY_SIZE(wm831x_power_resources),
980 .resources = wm831x_power_resources,
983 .name = "wm831x-rtc",
984 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
985 .resources = wm831x_rtc_resources,
988 .name = "wm831x-status",
990 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
991 .resources = wm831x_status1_resources,
994 .name = "wm831x-status",
996 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
997 .resources = wm831x_status2_resources,
1000 .name = "wm831x-watchdog",
1001 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1002 .resources = wm831x_wdt_resources,
1004 #if defined(CONFIG_KEYBOARD_WM831X_GPIO)
1006 .name = "wm831x_gpio-keys",
1010 #if defined(CONFIG_WM831X_CHARGER_DISPLAY)
1012 .name = "wm831x_charger_display",
1020 static struct mfd_cell wm8311_devs[] = {
1022 .name = "wm831x-backup",
1025 .name = "wm831x-buckv",
1027 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1028 .resources = wm831x_dcdc1_resources,
1031 .name = "wm831x-buckv",
1033 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1034 .resources = wm831x_dcdc2_resources,
1037 .name = "wm831x-buckp",
1039 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1040 .resources = wm831x_dcdc3_resources,
1043 .name = "wm831x-boostp",
1045 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1046 .resources = wm831x_dcdc4_resources,
1049 .name = "wm831x-epe",
1053 .name = "wm831x-epe",
1057 .name = "wm831x-gpio",
1058 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1059 .resources = wm831x_gpio_resources,
1062 .name = "wm831x-hwmon",
1065 .name = "wm831x-isink",
1067 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1068 .resources = wm831x_isink1_resources,
1071 .name = "wm831x-isink",
1073 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1074 .resources = wm831x_isink2_resources,
1077 .name = "wm831x-ldo",
1079 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1080 .resources = wm831x_ldo1_resources,
1083 .name = "wm831x-ldo",
1085 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1086 .resources = wm831x_ldo2_resources,
1089 .name = "wm831x-ldo",
1091 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1092 .resources = wm831x_ldo3_resources,
1095 .name = "wm831x-ldo",
1097 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1098 .resources = wm831x_ldo4_resources,
1101 .name = "wm831x-ldo",
1103 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1104 .resources = wm831x_ldo5_resources,
1107 .name = "wm831x-aldo",
1109 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1110 .resources = wm831x_ldo7_resources,
1113 .name = "wm831x-alive-ldo",
1115 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1116 .resources = wm831x_ldo11_resources,
1119 .name = "wm831x-on",
1120 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1121 .resources = wm831x_on_resources,
1124 .name = "wm831x-power",
1125 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1126 .resources = wm831x_power_resources,
1129 .name = "wm831x-rtc",
1130 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1131 .resources = wm831x_rtc_resources,
1134 .name = "wm831x-status",
1136 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1137 .resources = wm831x_status1_resources,
1140 .name = "wm831x-status",
1142 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1143 .resources = wm831x_status2_resources,
1146 .name = "wm831x-touch",
1147 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1148 .resources = wm831x_touch_resources,
1151 .name = "wm831x-watchdog",
1152 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1153 .resources = wm831x_wdt_resources,
1157 static struct mfd_cell wm8312_devs[] = {
1159 .name = "wm831x-backup",
1162 .name = "wm831x-buckv",
1164 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1165 .resources = wm831x_dcdc1_resources,
1168 .name = "wm831x-buckv",
1170 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1171 .resources = wm831x_dcdc2_resources,
1174 .name = "wm831x-buckp",
1176 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1177 .resources = wm831x_dcdc3_resources,
1180 .name = "wm831x-boostp",
1182 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1183 .resources = wm831x_dcdc4_resources,
1186 .name = "wm831x-epe",
1190 .name = "wm831x-epe",
1194 .name = "wm831x-gpio",
1195 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1196 .resources = wm831x_gpio_resources,
1199 .name = "wm831x-hwmon",
1202 .name = "wm831x-isink",
1204 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1205 .resources = wm831x_isink1_resources,
1208 .name = "wm831x-isink",
1210 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1211 .resources = wm831x_isink2_resources,
1214 .name = "wm831x-ldo",
1216 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1217 .resources = wm831x_ldo1_resources,
1220 .name = "wm831x-ldo",
1222 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1223 .resources = wm831x_ldo2_resources,
1226 .name = "wm831x-ldo",
1228 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1229 .resources = wm831x_ldo3_resources,
1232 .name = "wm831x-ldo",
1234 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1235 .resources = wm831x_ldo4_resources,
1238 .name = "wm831x-ldo",
1240 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1241 .resources = wm831x_ldo5_resources,
1244 .name = "wm831x-ldo",
1246 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1247 .resources = wm831x_ldo6_resources,
1250 .name = "wm831x-aldo",
1252 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1253 .resources = wm831x_ldo7_resources,
1256 .name = "wm831x-aldo",
1258 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1259 .resources = wm831x_ldo8_resources,
1262 .name = "wm831x-aldo",
1264 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1265 .resources = wm831x_ldo9_resources,
1268 .name = "wm831x-aldo",
1270 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1271 .resources = wm831x_ldo10_resources,
1274 .name = "wm831x-alive-ldo",
1276 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1277 .resources = wm831x_ldo11_resources,
1280 .name = "wm831x-on",
1281 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1282 .resources = wm831x_on_resources,
1285 .name = "wm831x-power",
1286 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1287 .resources = wm831x_power_resources,
1290 .name = "wm831x-rtc",
1291 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1292 .resources = wm831x_rtc_resources,
1295 .name = "wm831x-status",
1297 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1298 .resources = wm831x_status1_resources,
1301 .name = "wm831x-status",
1303 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1304 .resources = wm831x_status2_resources,
1307 .name = "wm831x-touch",
1308 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1309 .resources = wm831x_touch_resources,
1312 .name = "wm831x-watchdog",
1313 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1314 .resources = wm831x_wdt_resources,
1318 static struct mfd_cell wm8320_devs[] = {
1320 .name = "wm831x-backup",
1323 .name = "wm831x-buckv",
1325 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1326 .resources = wm831x_dcdc1_resources,
1329 .name = "wm831x-buckv",
1331 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1332 .resources = wm831x_dcdc2_resources,
1335 .name = "wm831x-buckp",
1337 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1338 .resources = wm831x_dcdc3_resources,
1341 .name = "wm831x-buckp",
1343 .num_resources = ARRAY_SIZE(wm8320_dcdc4_buck_resources),
1344 .resources = wm8320_dcdc4_buck_resources,
1347 .name = "wm831x-gpio",
1348 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1349 .resources = wm831x_gpio_resources,
1352 .name = "wm831x-hwmon",
1355 .name = "wm831x-ldo",
1357 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1358 .resources = wm831x_ldo1_resources,
1361 .name = "wm831x-ldo",
1363 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1364 .resources = wm831x_ldo2_resources,
1367 .name = "wm831x-ldo",
1369 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1370 .resources = wm831x_ldo3_resources,
1373 .name = "wm831x-ldo",
1375 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1376 .resources = wm831x_ldo4_resources,
1379 .name = "wm831x-ldo",
1381 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1382 .resources = wm831x_ldo5_resources,
1385 .name = "wm831x-ldo",
1387 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1388 .resources = wm831x_ldo6_resources,
1391 .name = "wm831x-aldo",
1393 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1394 .resources = wm831x_ldo7_resources,
1397 .name = "wm831x-aldo",
1399 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1400 .resources = wm831x_ldo8_resources,
1403 .name = "wm831x-aldo",
1405 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1406 .resources = wm831x_ldo9_resources,
1409 .name = "wm831x-aldo",
1411 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1412 .resources = wm831x_ldo10_resources,
1415 .name = "wm831x-alive-ldo",
1417 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1418 .resources = wm831x_ldo11_resources,
1421 .name = "wm831x-on",
1422 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1423 .resources = wm831x_on_resources,
1426 .name = "wm831x-rtc",
1427 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1428 .resources = wm831x_rtc_resources,
1431 .name = "wm831x-status",
1433 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1434 .resources = wm831x_status1_resources,
1437 .name = "wm831x-status",
1439 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1440 .resources = wm831x_status2_resources,
1443 .name = "wm831x-watchdog",
1444 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1445 .resources = wm831x_wdt_resources,
1449 static struct mfd_cell backlight_devs[] = {
1451 .name = "wm831x-backlight",
1456 * Instantiate the generic non-control parts of the device.
1458 int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1460 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1462 enum wm831x_parent parent;
1465 mutex_init(&wm831x->io_lock);
1466 mutex_init(&wm831x->key_lock);
1467 mutex_init(&wm831x->auxadc_lock);
1468 init_completion(&wm831x->auxadc_done);
1469 dev_set_drvdata(wm831x->dev, wm831x);
1471 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1473 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1481 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1486 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1488 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1491 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1493 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1495 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1499 /* Some engineering samples do not have the ID set, rely on
1500 * the device being registered correctly.
1503 dev_info(wm831x->dev, "Device is an engineering sample\n");
1510 wm831x->num_gpio = 12;
1511 wm831x->charger_irq_wake = 1;
1513 wm831x->has_gpio_ena = 1;
1514 wm831x->has_cs_sts = 1;
1517 ret = wm831x_reg_read(wm831x, WM831X_POWER_STATE) & 0xffff;
1518 wm831x_reg_write(wm831x, WM831X_POWER_STATE, (ret&0xfff8) | 0x04);
1520 dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
1525 wm831x->num_gpio = 16;
1526 wm831x->charger_irq_wake = 1;
1528 wm831x->has_gpio_ena = 1;
1529 wm831x->has_cs_sts = 1;
1532 dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
1537 wm831x->num_gpio = 16;
1538 wm831x->charger_irq_wake = 1;
1540 wm831x->has_gpio_ena = 1;
1541 wm831x->has_cs_sts = 1;
1544 dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
1549 wm831x->num_gpio = 12;
1550 dev_info(wm831x->dev, "WM8320 revision %c\n", 'A' + rev);
1555 wm831x->num_gpio = 12;
1556 dev_info(wm831x->dev, "WM8321 revision %c\n", 'A' + rev);
1561 wm831x->num_gpio = 12;
1562 dev_info(wm831x->dev, "WM8325 revision %c\n", 'A' + rev);
1567 wm831x->num_gpio = 12;
1568 dev_info(wm831x->dev, "WM8326 revision %c\n", 'A' + rev);
1572 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1577 /* This will need revisiting in future but is OK for all
1581 dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
1584 /* Bootstrap the user key */
1585 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1587 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1591 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1593 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1597 if (pdata && pdata->pre_init) {
1598 ret = pdata->pre_init(wm831x);
1600 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1606 for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
1607 if (!pdata->gpio_defaults[i])
1610 wm831x_reg_write(wm831x,
1611 WM831X_GPIO1_CONTROL + i,
1612 pdata->gpio_defaults[i] & 0xffff);
1616 ret = wm831x_irq_init(wm831x, irq);
1620 if (wm831x->irq_base) {
1621 ret = request_threaded_irq(wm831x->irq_base +
1622 WM831X_IRQ_AUXADC_DATA,
1623 NULL, wm831x_auxadc_irq, 0,
1626 dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
1630 /* The core device is up, instantiate the subdevices. */
1633 ret = mfd_add_devices(wm831x->dev, -1,
1634 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1635 NULL, wm831x->irq_base);
1639 ret = mfd_add_devices(wm831x->dev, -1,
1640 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1641 NULL, wm831x->irq_base);
1645 ret = mfd_add_devices(wm831x->dev, -1,
1646 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1647 NULL, wm831x->irq_base);
1654 ret = mfd_add_devices(wm831x->dev, -1,
1655 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1656 NULL, wm831x->irq_base);
1660 /* If this happens the bus probe function is buggy */
1665 dev_err(wm831x->dev, "Failed to add children\n");
1669 if (pdata && pdata->backlight) {
1670 /* Treat errors as non-critical */
1671 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1672 ARRAY_SIZE(backlight_devs), NULL,
1675 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1679 wm831x_otp_init(wm831x);
1681 if (pdata && pdata->post_init) {
1682 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716); //wm831x_reg_unlock
1683 wm831x_set_bits(wm831x, WM831X_RESET_CONTROL,0x0010,0x0000);
1684 wm831x_set_bits(wm831x, WM831X_LDO_ENABLE,0Xf800,0Xf800);
1685 ret = pdata->post_init(wm831x);
1686 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x0000); //wm831x_reg_lock
1688 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1696 wm831x_irq_exit(wm831x);
1698 mfd_remove_devices(wm831x->dev);
1703 void wm831x_device_exit(struct wm831x *wm831x)
1705 wm831x_otp_exit(wm831x);
1706 mfd_remove_devices(wm831x->dev);
1707 if (wm831x->irq_base)
1708 free_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, wm831x);
1709 wm831x_irq_exit(wm831x);
1713 int wm831x_device_suspend(struct wm831x *wm831x)
1718 //mask some intterupt avoid wakeing up system while suspending
1719 for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
1720 /* If there's been a change in the mask write it back
1721 * to the hardware. */
1722 //printk("irq_masks_cur[%d]=0x%x\n",i,wm831x->irq_masks_cur[i]);
1724 if (wm831x->irq_masks_cur[i] != wm831x->irq_masks_cache[i]) {
1725 wm831x->irq_masks_cache[i] = wm831x->irq_masks_cur[i];
1726 wm831x_reg_write(wm831x,
1727 WM831X_INTERRUPT_STATUS_1_MASK + i,
1728 wm831x->irq_masks_cur[i]);
1733 /* If the charger IRQs are a wake source then make sure we ack
1734 * them even if they're not actively being used (eg, no power
1735 * driver or no IRQ line wired up) then acknowledge the
1736 * interrupts otherwise suspend won't last very long.
1738 if (wm831x->charger_irq_wake) {
1739 reg = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_2_MASK);
1741 mask = WM831X_CHG_BATT_HOT_EINT |
1742 WM831X_CHG_BATT_COLD_EINT |
1743 WM831X_CHG_BATT_FAIL_EINT |
1744 WM831X_CHG_OV_EINT | WM831X_CHG_END_EINT |
1745 WM831X_CHG_TO_EINT | WM831X_CHG_MODE_EINT |
1746 WM831X_CHG_START_EINT;
1748 /* If any of the interrupts are masked read the statuses */
1750 reg = wm831x_reg_read(wm831x,
1751 WM831X_INTERRUPT_STATUS_2);
1754 dev_info(wm831x->dev,
1755 "Acknowledging masked charger IRQs: %x\n",
1757 wm831x_reg_write(wm831x, WM831X_INTERRUPT_STATUS_2,
1765 void wm831x_enter_sleep(void){
1766 struct regulator *dcdc = regulator_get(NULL, "dcdc1");
1768 struct wm831x_dcdc *dc = regulator_get_drvdata(dcdc);
1769 struct wm831x *wm831x = dc->wm831x;
1771 wm831x_set_bits(wm831x, WM831X_POWER_STATE, 0x4000, 0x4000); // SYSTEM SLEEP MODE
1773 wm831x_reg_write(wm831x,WM831X_INTERRUPT_STATUS_1+i, 0xffff); // INTRUPT FLAG CLEAR
1775 printk("%s:complete! \n",__func__);
1778 printk("%s:error!",__func__);
1780 regulator_put(dcdc);
1782 EXPORT_SYMBOL_GPL(wm831x_enter_sleep);
1784 void wm831x_exit_sleep(void){
1785 struct regulator *dcdc = regulator_get(NULL, "dcdc1");
1786 struct wm831x_dcdc *dc = regulator_get_drvdata(dcdc);
1787 struct wm831x *wm831x = dc->wm831x;
1789 wm831x_set_bits(wm831x, WM831X_POWER_STATE, 0x4000, 0); // SYSTEM ON MODE
1790 printk("%s:complete! \n",__func__);
1793 printk("%s:error!",__func__);
1795 regulator_put(dcdc);
1797 EXPORT_SYMBOL_GPL(wm831x_exit_sleep);
1799 int wm831x_device_shutdown(struct wm831x *wm831x)
1801 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1804 printk("pre WM831X_POWER_STATE = 0x%x\n", wm831x_reg_read(wm831x, WM831X_POWER_STATE));
1806 if (pdata && pdata->last_deinit) {
1807 ret = pdata->last_deinit(wm831x);
1809 dev_info(wm831x->dev, "last_deinit() failed: %d\n", ret);
1813 //if(0 == reboot_cmd_get())
1815 if(wm831x_set_bits(wm831x, WM831X_POWER_STATE, WM831X_CHIP_ON_MASK, 0) < 0)
1816 printk("%s wm831x_set_bits err\n", __FUNCTION__);
1817 //printk("post WM831X_POWER_STATE = 0x%x\n", wm831x_reg_read(wm831x, WM831X_POWER_STATE));
1823 EXPORT_SYMBOL_GPL(wm831x_device_shutdown);
1826 int wm831x_read_usb(struct wm831x *wm831x)
1828 int ret, usb_chg = 0, wall_chg = 0;
1830 ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
1834 if (ret & WM831X_PWR_USB)
1836 if (ret & WM831X_PWR_WALL)
1839 return ((usb_chg | wall_chg) ? 1 : 0);
1844 int wm831x_device_restart(struct wm831x *wm831x)
1846 wm831x_reg_write(wm831x,WM831X_RESET_ID, 0xffff);
1852 MODULE_DESCRIPTION("Core support for the WM831X AudioPlus PMIC");
1853 MODULE_LICENSE("GPL");
1854 MODULE_AUTHOR("Mark Brown");