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] = {
91 EXPORT_SYMBOL_GPL(wm831x_isinkv_values);
102 static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
108 case WM831X_WATCHDOG:
109 case WM831X_DC4_CONTROL:
110 case WM831X_ON_PIN_CONTROL:
111 case WM831X_BACKUP_CHARGER_CONTROL:
112 case WM831X_CHARGER_CONTROL_1:
113 case WM831X_CHARGER_CONTROL_2:
122 * wm831x_reg_unlock: Unlock user keyed registers
124 * The WM831x has a user key preventing writes to particularly
125 * critical registers. This function locks those registers,
126 * allowing writes to them.
128 void wm831x_reg_lock(struct wm831x *wm831x)
132 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
134 dev_vdbg(wm831x->dev, "Registers locked\n");
136 mutex_lock(&wm831x->io_lock);
137 WARN_ON(wm831x->locked);
139 mutex_unlock(&wm831x->io_lock);
141 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
145 EXPORT_SYMBOL_GPL(wm831x_reg_lock);
148 * wm831x_reg_unlock: Unlock user keyed registers
150 * The WM831x has a user key preventing writes to particularly
151 * critical registers. This function locks those registers,
152 * preventing spurious writes.
154 int wm831x_reg_unlock(struct wm831x *wm831x)
158 /* 0x9716 is the value required to unlock the registers */
159 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
161 dev_vdbg(wm831x->dev, "Registers unlocked\n");
163 mutex_lock(&wm831x->io_lock);
164 WARN_ON(!wm831x->locked);
166 mutex_unlock(&wm831x->io_lock);
171 EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
173 static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
174 int bytes, void *dest)
182 ret = wm831x->read_dev(wm831x, reg, bytes, dest);
186 for (i = 0; i < bytes / 2; i++) {
187 buf[i] = be16_to_cpu(buf[i]);
189 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
190 buf[i], reg + i, reg + i);
197 * wm831x_reg_read: Read a single WM831x register.
199 * @wm831x: Device to read from.
200 * @reg: Register to read.
202 int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
207 mutex_lock(&wm831x->io_lock);
209 ret = wm831x_read(wm831x, reg, 2, &val);
211 mutex_unlock(&wm831x->io_lock);
218 EXPORT_SYMBOL_GPL(wm831x_reg_read);
221 * wm831x_bulk_read: Read multiple WM831x registers
223 * @wm831x: Device to read from
224 * @reg: First register
225 * @count: Number of registers
226 * @buf: Buffer to fill.
228 int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
233 mutex_lock(&wm831x->io_lock);
235 ret = wm831x_read(wm831x, reg, count * 2, buf);
237 mutex_unlock(&wm831x->io_lock);
241 EXPORT_SYMBOL_GPL(wm831x_bulk_read);
243 static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
244 int bytes, void *src)
252 for (i = 0; i < bytes / 2; i++) {
253 if (wm831x_reg_locked(wm831x, reg))
256 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
257 buf[i], reg + i, reg + i);
259 buf[i] = cpu_to_be16(buf[i]);
262 return wm831x->write_dev(wm831x, reg, bytes, src);
266 * wm831x_reg_write: Write a single WM831x register.
268 * @wm831x: Device to write to.
269 * @reg: Register to write to.
270 * @val: Value to write.
272 int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
277 mutex_lock(&wm831x->io_lock);
279 ret = wm831x_write(wm831x, reg, 2, &val);
281 mutex_unlock(&wm831x->io_lock);
285 EXPORT_SYMBOL_GPL(wm831x_reg_write);
288 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
290 * @wm831x: Device to write to.
291 * @reg: Register to write to.
292 * @mask: Mask of bits to set.
293 * @val: Value to set (unshifted)
295 int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
296 unsigned short mask, unsigned short val)
301 mutex_lock(&wm831x->io_lock);
303 ret = wm831x_read(wm831x, reg, 2, &r);
310 ret = wm831x_write(wm831x, reg, 2, &r);
313 mutex_unlock(&wm831x->io_lock);
317 EXPORT_SYMBOL_GPL(wm831x_set_bits);
320 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
322 * @wm831x: Device to read from.
323 * @input: AUXADC input to read.
325 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
327 int ret, src, irq_masked, timeout;
329 /* Are we using the interrupt? */
330 irq_masked = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_1_MASK);
331 irq_masked &= WM831X_AUXADC_DATA_EINT;
333 mutex_lock(&wm831x->auxadc_lock);
335 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
336 WM831X_AUX_ENA, WM831X_AUX_ENA);
338 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
342 /* We force a single source at present */
344 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
347 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
351 /* Clear any notification from a very late arriving interrupt */
352 try_wait_for_completion(&wm831x->auxadc_done);
354 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
355 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
357 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
362 /* If we're not using interrupts then poll the
363 * interrupt status register */
368 ret = wm831x_reg_read(wm831x,
369 WM831X_INTERRUPT_STATUS_1);
372 "ISR 1 read failed: %d\n", ret);
376 /* Did it complete? */
377 if (ret & WM831X_AUXADC_DATA_EINT) {
378 wm831x_reg_write(wm831x,
379 WM831X_INTERRUPT_STATUS_1,
380 WM831X_AUXADC_DATA_EINT);
384 "AUXADC conversion timeout\n");
390 /* If we are using interrupts then wait for the
391 * interrupt to complete. Use an extremely long
392 * timeout to handle situations with heavy load where
393 * the notification of the interrupt may be delayed by
394 * threaded IRQ handling. */
395 if (!wait_for_completion_timeout(&wm831x->auxadc_done,
396 msecs_to_jiffies(500))) {
397 dev_err(wm831x->dev, "Timed out waiting for AUXADC\n");
403 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
405 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
407 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
408 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
411 src = WM831X_AUX_CAL;
414 dev_err(wm831x->dev, "Data from source %d not %d\n",
418 ret &= WM831X_AUX_DATA_MASK;
423 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
425 mutex_unlock(&wm831x->auxadc_lock);
428 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
430 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
432 struct wm831x *wm831x = irq_data;
434 complete(&wm831x->auxadc_done);
440 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
442 * @wm831x: Device to read from.
443 * @input: AUXADC input to read.
445 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
449 ret = wm831x_auxadc_read(wm831x, input);
457 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
459 static struct resource wm831x_dcdc1_resources[] = {
461 .start = WM831X_DC1_CONTROL_1,
462 .end = WM831X_DC1_DVS_CONTROL,
463 .flags = IORESOURCE_IO,
467 .start = WM831X_IRQ_UV_DC1,
468 .end = WM831X_IRQ_UV_DC1,
469 .flags = IORESOURCE_IRQ,
473 .start = WM831X_IRQ_HC_DC1,
474 .end = WM831X_IRQ_HC_DC1,
475 .flags = IORESOURCE_IRQ,
480 static struct resource wm831x_dcdc2_resources[] = {
482 .start = WM831X_DC2_CONTROL_1,
483 .end = WM831X_DC2_DVS_CONTROL,
484 .flags = IORESOURCE_IO,
488 .start = WM831X_IRQ_UV_DC2,
489 .end = WM831X_IRQ_UV_DC2,
490 .flags = IORESOURCE_IRQ,
494 .start = WM831X_IRQ_HC_DC2,
495 .end = WM831X_IRQ_HC_DC2,
496 .flags = IORESOURCE_IRQ,
500 static struct resource wm831x_dcdc3_resources[] = {
502 .start = WM831X_DC3_CONTROL_1,
503 .end = WM831X_DC3_SLEEP_CONTROL,
504 .flags = IORESOURCE_IO,
508 .start = WM831X_IRQ_UV_DC3,
509 .end = WM831X_IRQ_UV_DC3,
510 .flags = IORESOURCE_IRQ,
514 static struct resource wm831x_dcdc4_resources[] = {
516 .start = WM831X_DC4_CONTROL,
517 .end = WM831X_DC4_SLEEP_CONTROL,
518 .flags = IORESOURCE_IO,
522 .start = WM831X_IRQ_UV_DC4,
523 .end = WM831X_IRQ_UV_DC4,
524 .flags = IORESOURCE_IRQ,
528 static struct resource wm8320_dcdc4_buck_resources[] = {
530 .start = WM831X_DC4_CONTROL,
531 .end = WM832X_DC4_SLEEP_CONTROL,
532 .flags = IORESOURCE_IO,
536 .start = WM831X_IRQ_UV_DC4,
537 .end = WM831X_IRQ_UV_DC4,
538 .flags = IORESOURCE_IRQ,
542 static struct resource wm831x_gpio_resources[] = {
544 .start = WM831X_IRQ_GPIO_1,
545 .end = WM831X_IRQ_GPIO_16,
546 .flags = IORESOURCE_IRQ,
550 static struct resource wm831x_isink1_resources[] = {
552 .start = WM831X_CURRENT_SINK_1,
553 .end = WM831X_CURRENT_SINK_1,
554 .flags = IORESOURCE_IO,
557 .start = WM831X_IRQ_CS1,
558 .end = WM831X_IRQ_CS1,
559 .flags = IORESOURCE_IRQ,
563 static struct resource wm831x_isink2_resources[] = {
565 .start = WM831X_CURRENT_SINK_2,
566 .end = WM831X_CURRENT_SINK_2,
567 .flags = IORESOURCE_IO,
570 .start = WM831X_IRQ_CS2,
571 .end = WM831X_IRQ_CS2,
572 .flags = IORESOURCE_IRQ,
576 static struct resource wm831x_ldo1_resources[] = {
578 .start = WM831X_LDO1_CONTROL,
579 .end = WM831X_LDO1_SLEEP_CONTROL,
580 .flags = IORESOURCE_IO,
584 .start = WM831X_IRQ_UV_LDO1,
585 .end = WM831X_IRQ_UV_LDO1,
586 .flags = IORESOURCE_IRQ,
590 static struct resource wm831x_ldo2_resources[] = {
592 .start = WM831X_LDO2_CONTROL,
593 .end = WM831X_LDO2_SLEEP_CONTROL,
594 .flags = IORESOURCE_IO,
598 .start = WM831X_IRQ_UV_LDO2,
599 .end = WM831X_IRQ_UV_LDO2,
600 .flags = IORESOURCE_IRQ,
604 static struct resource wm831x_ldo3_resources[] = {
606 .start = WM831X_LDO3_CONTROL,
607 .end = WM831X_LDO3_SLEEP_CONTROL,
608 .flags = IORESOURCE_IO,
612 .start = WM831X_IRQ_UV_LDO3,
613 .end = WM831X_IRQ_UV_LDO3,
614 .flags = IORESOURCE_IRQ,
618 static struct resource wm831x_ldo4_resources[] = {
620 .start = WM831X_LDO4_CONTROL,
621 .end = WM831X_LDO4_SLEEP_CONTROL,
622 .flags = IORESOURCE_IO,
626 .start = WM831X_IRQ_UV_LDO4,
627 .end = WM831X_IRQ_UV_LDO4,
628 .flags = IORESOURCE_IRQ,
632 static struct resource wm831x_ldo5_resources[] = {
634 .start = WM831X_LDO5_CONTROL,
635 .end = WM831X_LDO5_SLEEP_CONTROL,
636 .flags = IORESOURCE_IO,
640 .start = WM831X_IRQ_UV_LDO5,
641 .end = WM831X_IRQ_UV_LDO5,
642 .flags = IORESOURCE_IRQ,
646 static struct resource wm831x_ldo6_resources[] = {
648 .start = WM831X_LDO6_CONTROL,
649 .end = WM831X_LDO6_SLEEP_CONTROL,
650 .flags = IORESOURCE_IO,
654 .start = WM831X_IRQ_UV_LDO6,
655 .end = WM831X_IRQ_UV_LDO6,
656 .flags = IORESOURCE_IRQ,
660 static struct resource wm831x_ldo7_resources[] = {
662 .start = WM831X_LDO7_CONTROL,
663 .end = WM831X_LDO7_SLEEP_CONTROL,
664 .flags = IORESOURCE_IO,
668 .start = WM831X_IRQ_UV_LDO7,
669 .end = WM831X_IRQ_UV_LDO7,
670 .flags = IORESOURCE_IRQ,
674 static struct resource wm831x_ldo8_resources[] = {
676 .start = WM831X_LDO8_CONTROL,
677 .end = WM831X_LDO8_SLEEP_CONTROL,
678 .flags = IORESOURCE_IO,
682 .start = WM831X_IRQ_UV_LDO8,
683 .end = WM831X_IRQ_UV_LDO8,
684 .flags = IORESOURCE_IRQ,
688 static struct resource wm831x_ldo9_resources[] = {
690 .start = WM831X_LDO9_CONTROL,
691 .end = WM831X_LDO9_SLEEP_CONTROL,
692 .flags = IORESOURCE_IO,
696 .start = WM831X_IRQ_UV_LDO9,
697 .end = WM831X_IRQ_UV_LDO9,
698 .flags = IORESOURCE_IRQ,
702 static struct resource wm831x_ldo10_resources[] = {
704 .start = WM831X_LDO10_CONTROL,
705 .end = WM831X_LDO10_SLEEP_CONTROL,
706 .flags = IORESOURCE_IO,
710 .start = WM831X_IRQ_UV_LDO10,
711 .end = WM831X_IRQ_UV_LDO10,
712 .flags = IORESOURCE_IRQ,
716 static struct resource wm831x_ldo11_resources[] = {
718 .start = WM831X_LDO11_ON_CONTROL,
719 .end = WM831X_LDO11_SLEEP_CONTROL,
720 .flags = IORESOURCE_IO,
724 static struct resource wm831x_on_resources[] = {
726 .start = WM831X_IRQ_ON,
727 .end = WM831X_IRQ_ON,
728 .flags = IORESOURCE_IRQ,
733 static struct resource wm831x_power_resources[] = {
736 .start = WM831X_IRQ_PPM_SYSLO,
737 .end = WM831X_IRQ_PPM_SYSLO,
738 .flags = IORESOURCE_IRQ,
742 .start = WM831X_IRQ_PPM_PWR_SRC,
743 .end = WM831X_IRQ_PPM_PWR_SRC,
744 .flags = IORESOURCE_IRQ,
748 .start = WM831X_IRQ_PPM_USB_CURR,
749 .end = WM831X_IRQ_PPM_USB_CURR,
750 .flags = IORESOURCE_IRQ,
754 .start = WM831X_IRQ_CHG_BATT_HOT,
755 .end = WM831X_IRQ_CHG_BATT_HOT,
756 .flags = IORESOURCE_IRQ,
760 .start = WM831X_IRQ_CHG_BATT_COLD,
761 .end = WM831X_IRQ_CHG_BATT_COLD,
762 .flags = IORESOURCE_IRQ,
766 .start = WM831X_IRQ_CHG_BATT_FAIL,
767 .end = WM831X_IRQ_CHG_BATT_FAIL,
768 .flags = IORESOURCE_IRQ,
772 .start = WM831X_IRQ_CHG_OV,
773 .end = WM831X_IRQ_CHG_OV,
774 .flags = IORESOURCE_IRQ,
778 .start = WM831X_IRQ_CHG_END,
779 .end = WM831X_IRQ_CHG_END,
780 .flags = IORESOURCE_IRQ,
784 .start = WM831X_IRQ_CHG_TO,
785 .end = WM831X_IRQ_CHG_TO,
786 .flags = IORESOURCE_IRQ,
790 .start = WM831X_IRQ_CHG_MODE,
791 .end = WM831X_IRQ_CHG_MODE,
792 .flags = IORESOURCE_IRQ,
796 .start = WM831X_IRQ_CHG_START,
797 .end = WM831X_IRQ_CHG_START,
798 .flags = IORESOURCE_IRQ,
802 static struct resource wm831x_rtc_resources[] = {
805 .start = WM831X_IRQ_RTC_PER,
806 .end = WM831X_IRQ_RTC_PER,
807 .flags = IORESOURCE_IRQ,
811 .start = WM831X_IRQ_RTC_ALM,
812 .end = WM831X_IRQ_RTC_ALM,
813 .flags = IORESOURCE_IRQ,
817 static struct resource wm831x_status1_resources[] = {
819 .start = WM831X_STATUS_LED_1,
820 .end = WM831X_STATUS_LED_1,
821 .flags = IORESOURCE_IO,
825 static struct resource wm831x_status2_resources[] = {
827 .start = WM831X_STATUS_LED_2,
828 .end = WM831X_STATUS_LED_2,
829 .flags = IORESOURCE_IO,
833 static struct resource wm831x_touch_resources[] = {
836 .start = WM831X_IRQ_TCHPD,
837 .end = WM831X_IRQ_TCHPD,
838 .flags = IORESOURCE_IRQ,
842 .start = WM831X_IRQ_TCHDATA,
843 .end = WM831X_IRQ_TCHDATA,
844 .flags = IORESOURCE_IRQ,
848 static struct resource wm831x_wdt_resources[] = {
850 .start = WM831X_IRQ_WDOG_TO,
851 .end = WM831X_IRQ_WDOG_TO,
852 .flags = IORESOURCE_IRQ,
856 static struct mfd_cell wm8310_devs[] = {
858 .name = "wm831x-backup",
861 .name = "wm831x-buckv",
863 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
864 .resources = wm831x_dcdc1_resources,
867 .name = "wm831x-buckv",
869 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
870 .resources = wm831x_dcdc2_resources,
873 .name = "wm831x-buckp",
875 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
876 .resources = wm831x_dcdc3_resources,
879 .name = "wm831x-boostp",
881 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
882 .resources = wm831x_dcdc4_resources,
885 .name = "wm831x-epe",
889 .name = "wm831x-epe",
893 .name = "wm831x-gpio",
894 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
895 .resources = wm831x_gpio_resources,
898 .name = "wm831x-hwmon",
901 .name = "wm831x-isink",
903 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
904 .resources = wm831x_isink1_resources,
907 .name = "wm831x-isink",
909 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
910 .resources = wm831x_isink2_resources,
913 .name = "wm831x-ldo",
915 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
916 .resources = wm831x_ldo1_resources,
919 .name = "wm831x-ldo",
921 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
922 .resources = wm831x_ldo2_resources,
925 .name = "wm831x-ldo",
927 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
928 .resources = wm831x_ldo3_resources,
931 .name = "wm831x-ldo",
933 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
934 .resources = wm831x_ldo4_resources,
937 .name = "wm831x-ldo",
939 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
940 .resources = wm831x_ldo5_resources,
943 .name = "wm831x-ldo",
945 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
946 .resources = wm831x_ldo6_resources,
949 .name = "wm831x-aldo",
951 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
952 .resources = wm831x_ldo7_resources,
955 .name = "wm831x-aldo",
957 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
958 .resources = wm831x_ldo8_resources,
961 .name = "wm831x-aldo",
963 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
964 .resources = wm831x_ldo9_resources,
967 .name = "wm831x-aldo",
969 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
970 .resources = wm831x_ldo10_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-watchdog",
1007 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1008 .resources = wm831x_wdt_resources,
1012 static struct mfd_cell wm8311_devs[] = {
1014 .name = "wm831x-backup",
1017 .name = "wm831x-buckv",
1019 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1020 .resources = wm831x_dcdc1_resources,
1023 .name = "wm831x-buckv",
1025 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1026 .resources = wm831x_dcdc2_resources,
1029 .name = "wm831x-buckp",
1031 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1032 .resources = wm831x_dcdc3_resources,
1035 .name = "wm831x-boostp",
1037 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1038 .resources = wm831x_dcdc4_resources,
1041 .name = "wm831x-epe",
1045 .name = "wm831x-epe",
1049 .name = "wm831x-gpio",
1050 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1051 .resources = wm831x_gpio_resources,
1054 .name = "wm831x-hwmon",
1057 .name = "wm831x-isink",
1059 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1060 .resources = wm831x_isink1_resources,
1063 .name = "wm831x-isink",
1065 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1066 .resources = wm831x_isink2_resources,
1069 .name = "wm831x-ldo",
1071 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1072 .resources = wm831x_ldo1_resources,
1075 .name = "wm831x-ldo",
1077 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1078 .resources = wm831x_ldo2_resources,
1081 .name = "wm831x-ldo",
1083 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1084 .resources = wm831x_ldo3_resources,
1087 .name = "wm831x-ldo",
1089 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1090 .resources = wm831x_ldo4_resources,
1093 .name = "wm831x-ldo",
1095 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1096 .resources = wm831x_ldo5_resources,
1099 .name = "wm831x-aldo",
1101 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1102 .resources = wm831x_ldo7_resources,
1105 .name = "wm831x-alive-ldo",
1107 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1108 .resources = wm831x_ldo11_resources,
1111 .name = "wm831x-on",
1112 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1113 .resources = wm831x_on_resources,
1116 .name = "wm831x-power",
1117 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1118 .resources = wm831x_power_resources,
1121 .name = "wm831x-rtc",
1122 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1123 .resources = wm831x_rtc_resources,
1126 .name = "wm831x-status",
1128 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1129 .resources = wm831x_status1_resources,
1132 .name = "wm831x-status",
1134 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1135 .resources = wm831x_status2_resources,
1138 .name = "wm831x-touch",
1139 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1140 .resources = wm831x_touch_resources,
1143 .name = "wm831x-watchdog",
1144 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1145 .resources = wm831x_wdt_resources,
1149 static struct mfd_cell wm8312_devs[] = {
1151 .name = "wm831x-backup",
1154 .name = "wm831x-buckv",
1156 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1157 .resources = wm831x_dcdc1_resources,
1160 .name = "wm831x-buckv",
1162 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1163 .resources = wm831x_dcdc2_resources,
1166 .name = "wm831x-buckp",
1168 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1169 .resources = wm831x_dcdc3_resources,
1172 .name = "wm831x-boostp",
1174 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1175 .resources = wm831x_dcdc4_resources,
1178 .name = "wm831x-epe",
1182 .name = "wm831x-epe",
1186 .name = "wm831x-gpio",
1187 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1188 .resources = wm831x_gpio_resources,
1191 .name = "wm831x-hwmon",
1194 .name = "wm831x-isink",
1196 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1197 .resources = wm831x_isink1_resources,
1200 .name = "wm831x-isink",
1202 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1203 .resources = wm831x_isink2_resources,
1206 .name = "wm831x-ldo",
1208 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1209 .resources = wm831x_ldo1_resources,
1212 .name = "wm831x-ldo",
1214 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1215 .resources = wm831x_ldo2_resources,
1218 .name = "wm831x-ldo",
1220 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1221 .resources = wm831x_ldo3_resources,
1224 .name = "wm831x-ldo",
1226 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1227 .resources = wm831x_ldo4_resources,
1230 .name = "wm831x-ldo",
1232 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1233 .resources = wm831x_ldo5_resources,
1236 .name = "wm831x-ldo",
1238 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1239 .resources = wm831x_ldo6_resources,
1242 .name = "wm831x-aldo",
1244 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1245 .resources = wm831x_ldo7_resources,
1248 .name = "wm831x-aldo",
1250 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1251 .resources = wm831x_ldo8_resources,
1254 .name = "wm831x-aldo",
1256 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1257 .resources = wm831x_ldo9_resources,
1260 .name = "wm831x-aldo",
1262 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1263 .resources = wm831x_ldo10_resources,
1266 .name = "wm831x-alive-ldo",
1268 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1269 .resources = wm831x_ldo11_resources,
1272 .name = "wm831x-on",
1273 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1274 .resources = wm831x_on_resources,
1277 .name = "wm831x-power",
1278 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1279 .resources = wm831x_power_resources,
1282 .name = "wm831x-rtc",
1283 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1284 .resources = wm831x_rtc_resources,
1287 .name = "wm831x-status",
1289 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1290 .resources = wm831x_status1_resources,
1293 .name = "wm831x-status",
1295 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1296 .resources = wm831x_status2_resources,
1299 .name = "wm831x-touch",
1300 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1301 .resources = wm831x_touch_resources,
1304 .name = "wm831x-watchdog",
1305 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1306 .resources = wm831x_wdt_resources,
1310 static struct mfd_cell wm8320_devs[] = {
1312 .name = "wm831x-backup",
1315 .name = "wm831x-buckv",
1317 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1318 .resources = wm831x_dcdc1_resources,
1321 .name = "wm831x-buckv",
1323 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1324 .resources = wm831x_dcdc2_resources,
1327 .name = "wm831x-buckp",
1329 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1330 .resources = wm831x_dcdc3_resources,
1333 .name = "wm831x-buckp",
1335 .num_resources = ARRAY_SIZE(wm8320_dcdc4_buck_resources),
1336 .resources = wm8320_dcdc4_buck_resources,
1339 .name = "wm831x-gpio",
1340 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1341 .resources = wm831x_gpio_resources,
1344 .name = "wm831x-hwmon",
1347 .name = "wm831x-ldo",
1349 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1350 .resources = wm831x_ldo1_resources,
1353 .name = "wm831x-ldo",
1355 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1356 .resources = wm831x_ldo2_resources,
1359 .name = "wm831x-ldo",
1361 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1362 .resources = wm831x_ldo3_resources,
1365 .name = "wm831x-ldo",
1367 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1368 .resources = wm831x_ldo4_resources,
1371 .name = "wm831x-ldo",
1373 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1374 .resources = wm831x_ldo5_resources,
1377 .name = "wm831x-ldo",
1379 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1380 .resources = wm831x_ldo6_resources,
1383 .name = "wm831x-aldo",
1385 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1386 .resources = wm831x_ldo7_resources,
1389 .name = "wm831x-aldo",
1391 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1392 .resources = wm831x_ldo8_resources,
1395 .name = "wm831x-aldo",
1397 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1398 .resources = wm831x_ldo9_resources,
1401 .name = "wm831x-aldo",
1403 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1404 .resources = wm831x_ldo10_resources,
1407 .name = "wm831x-alive-ldo",
1409 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1410 .resources = wm831x_ldo11_resources,
1413 .name = "wm831x-on",
1414 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1415 .resources = wm831x_on_resources,
1418 .name = "wm831x-rtc",
1419 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1420 .resources = wm831x_rtc_resources,
1423 .name = "wm831x-status",
1425 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1426 .resources = wm831x_status1_resources,
1429 .name = "wm831x-status",
1431 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1432 .resources = wm831x_status2_resources,
1435 .name = "wm831x-watchdog",
1436 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1437 .resources = wm831x_wdt_resources,
1441 static struct mfd_cell backlight_devs[] = {
1443 .name = "wm831x-backlight",
1448 * Instantiate the generic non-control parts of the device.
1450 static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1452 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1454 enum wm831x_parent parent;
1457 mutex_init(&wm831x->io_lock);
1458 mutex_init(&wm831x->key_lock);
1459 mutex_init(&wm831x->auxadc_lock);
1460 init_completion(&wm831x->auxadc_done);
1461 dev_set_drvdata(wm831x->dev, wm831x);
1463 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1465 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1468 if (ret != 0x6204) {
1469 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1474 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1476 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1479 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1481 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1483 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1487 /* Some engineering samples do not have the ID set, rely on
1488 * the device being registered correctly.
1491 dev_info(wm831x->dev, "Device is an engineering sample\n");
1498 wm831x->num_gpio = 16;
1499 wm831x->charger_irq_wake = 1;
1501 wm831x->has_gpio_ena = 1;
1502 wm831x->has_cs_sts = 1;
1505 dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
1510 wm831x->num_gpio = 16;
1511 wm831x->charger_irq_wake = 1;
1513 wm831x->has_gpio_ena = 1;
1514 wm831x->has_cs_sts = 1;
1517 dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
1522 wm831x->num_gpio = 16;
1523 wm831x->charger_irq_wake = 1;
1525 wm831x->has_gpio_ena = 1;
1526 wm831x->has_cs_sts = 1;
1529 dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
1534 wm831x->num_gpio = 12;
1535 dev_info(wm831x->dev, "WM8320 revision %c\n", 'A' + rev);
1540 wm831x->num_gpio = 12;
1541 dev_info(wm831x->dev, "WM8321 revision %c\n", 'A' + rev);
1546 wm831x->num_gpio = 12;
1547 dev_info(wm831x->dev, "WM8325 revision %c\n", 'A' + rev);
1551 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1556 /* This will need revisiting in future but is OK for all
1560 dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
1563 /* Bootstrap the user key */
1564 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1566 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1570 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1572 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1576 if (pdata && pdata->pre_init) {
1577 ret = pdata->pre_init(wm831x);
1579 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1584 ret = wm831x_irq_init(wm831x, irq);
1588 if (wm831x->irq_base) {
1589 ret = request_threaded_irq(wm831x->irq_base +
1590 WM831X_IRQ_AUXADC_DATA,
1591 NULL, wm831x_auxadc_irq, 0,
1594 dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
1598 /* The core device is up, instantiate the subdevices. */
1601 ret = mfd_add_devices(wm831x->dev, -1,
1602 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1603 NULL, wm831x->irq_base);
1607 ret = mfd_add_devices(wm831x->dev, -1,
1608 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1609 NULL, wm831x->irq_base);
1613 ret = mfd_add_devices(wm831x->dev, -1,
1614 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1615 NULL, wm831x->irq_base);
1619 ret = mfd_add_devices(wm831x->dev, -1,
1620 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1625 ret = mfd_add_devices(wm831x->dev, -1,
1626 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1631 ret = mfd_add_devices(wm831x->dev, -1,
1632 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1637 /* If this happens the bus probe function is buggy */
1642 dev_err(wm831x->dev, "Failed to add children\n");
1646 if (pdata && pdata->backlight) {
1647 /* Treat errors as non-critical */
1648 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1649 ARRAY_SIZE(backlight_devs), NULL,
1652 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1656 wm831x_otp_init(wm831x);
1658 if (pdata && pdata->post_init) {
1659 ret = pdata->post_init(wm831x);
1661 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1669 wm831x_irq_exit(wm831x);
1671 mfd_remove_devices(wm831x->dev);
1676 static void wm831x_device_exit(struct wm831x *wm831x)
1678 wm831x_otp_exit(wm831x);
1679 mfd_remove_devices(wm831x->dev);
1680 if (wm831x->irq_base)
1681 free_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, wm831x);
1682 wm831x_irq_exit(wm831x);
1686 static int wm831x_device_suspend(struct wm831x *wm831x)
1690 /* If the charger IRQs are a wake source then make sure we ack
1691 * them even if they're not actively being used (eg, no power
1692 * driver or no IRQ line wired up) then acknowledge the
1693 * interrupts otherwise suspend won't last very long.
1695 if (wm831x->charger_irq_wake) {
1696 reg = wm831x_reg_read(wm831x, WM831X_INTERRUPT_STATUS_2_MASK);
1698 mask = WM831X_CHG_BATT_HOT_EINT |
1699 WM831X_CHG_BATT_COLD_EINT |
1700 WM831X_CHG_BATT_FAIL_EINT |
1701 WM831X_CHG_OV_EINT | WM831X_CHG_END_EINT |
1702 WM831X_CHG_TO_EINT | WM831X_CHG_MODE_EINT |
1703 WM831X_CHG_START_EINT;
1705 /* If any of the interrupts are masked read the statuses */
1707 reg = wm831x_reg_read(wm831x,
1708 WM831X_INTERRUPT_STATUS_2);
1711 dev_info(wm831x->dev,
1712 "Acknowledging masked charger IRQs: %x\n",
1714 wm831x_reg_write(wm831x, WM831X_INTERRUPT_STATUS_2,
1722 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
1723 int bytes, void *dest)
1725 struct i2c_client *i2c = wm831x->control_data;
1727 u16 r = cpu_to_be16(reg);
1729 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
1735 ret = i2c_master_recv(i2c, dest, bytes);
1743 /* Currently we allocate the write buffer on the stack; this is OK for
1744 * small writes - if we need to do large writes this will need to be
1747 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
1748 int bytes, void *src)
1750 struct i2c_client *i2c = wm831x->control_data;
1751 unsigned char msg[bytes + 2];
1754 reg = cpu_to_be16(reg);
1755 memcpy(&msg[0], ®, 2);
1756 memcpy(&msg[2], src, bytes);
1758 ret = i2c_master_send(i2c, msg, bytes + 2);
1761 if (ret < bytes + 2)
1767 static int wm831x_i2c_probe(struct i2c_client *i2c,
1768 const struct i2c_device_id *id)
1770 struct wm831x *wm831x;
1772 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
1776 i2c_set_clientdata(i2c, wm831x);
1777 wm831x->dev = &i2c->dev;
1778 wm831x->control_data = i2c;
1779 wm831x->read_dev = wm831x_i2c_read_device;
1780 wm831x->write_dev = wm831x_i2c_write_device;
1782 return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
1785 static int wm831x_i2c_remove(struct i2c_client *i2c)
1787 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1789 wm831x_device_exit(wm831x);
1794 static int wm831x_i2c_suspend(struct i2c_client *i2c, pm_message_t mesg)
1796 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1798 return wm831x_device_suspend(wm831x);
1801 static const struct i2c_device_id wm831x_i2c_id[] = {
1802 { "wm8310", WM8310 },
1803 { "wm8311", WM8311 },
1804 { "wm8312", WM8312 },
1805 { "wm8320", WM8320 },
1806 { "wm8321", WM8321 },
1807 { "wm8325", WM8325 },
1810 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
1813 static struct i2c_driver wm831x_i2c_driver = {
1816 .owner = THIS_MODULE,
1818 .probe = wm831x_i2c_probe,
1819 .remove = wm831x_i2c_remove,
1820 .suspend = wm831x_i2c_suspend,
1821 .id_table = wm831x_i2c_id,
1824 static int __init wm831x_i2c_init(void)
1828 ret = i2c_add_driver(&wm831x_i2c_driver);
1830 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
1834 subsys_initcall(wm831x_i2c_init);
1836 static void __exit wm831x_i2c_exit(void)
1838 i2c_del_driver(&wm831x_i2c_driver);
1840 module_exit(wm831x_i2c_exit);
1842 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1843 MODULE_LICENSE("GPL");
1844 MODULE_AUTHOR("Mark Brown");