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>
26 #include <linux/mfd/wm831x/otp.h>
27 #include <linux/mfd/wm831x/regulator.h>
29 /* Current settings - values are 2*2^(reg_val/4) microamps. These are
30 * exported since they are used by multiple drivers.
32 int wm831x_isinkv_values[WM831X_ISINK_MAX_ISEL + 1] = {
90 EXPORT_SYMBOL_GPL(wm831x_isinkv_values);
99 static int wm831x_reg_locked(struct wm831x *wm831x, unsigned short reg)
105 case WM831X_WATCHDOG:
106 case WM831X_DC4_CONTROL:
107 case WM831X_ON_PIN_CONTROL:
108 case WM831X_BACKUP_CHARGER_CONTROL:
109 case WM831X_CHARGER_CONTROL_1:
110 case WM831X_CHARGER_CONTROL_2:
119 * wm831x_reg_unlock: Unlock user keyed registers
121 * The WM831x has a user key preventing writes to particularly
122 * critical registers. This function locks those registers,
123 * allowing writes to them.
125 void wm831x_reg_lock(struct wm831x *wm831x)
129 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
131 dev_vdbg(wm831x->dev, "Registers locked\n");
133 mutex_lock(&wm831x->io_lock);
134 WARN_ON(wm831x->locked);
136 mutex_unlock(&wm831x->io_lock);
138 dev_err(wm831x->dev, "Failed to lock registers: %d\n", ret);
142 EXPORT_SYMBOL_GPL(wm831x_reg_lock);
145 * wm831x_reg_unlock: Unlock user keyed registers
147 * The WM831x has a user key preventing writes to particularly
148 * critical registers. This function locks those registers,
149 * preventing spurious writes.
151 int wm831x_reg_unlock(struct wm831x *wm831x)
155 /* 0x9716 is the value required to unlock the registers */
156 ret = wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0x9716);
158 dev_vdbg(wm831x->dev, "Registers unlocked\n");
160 mutex_lock(&wm831x->io_lock);
161 WARN_ON(!wm831x->locked);
163 mutex_unlock(&wm831x->io_lock);
168 EXPORT_SYMBOL_GPL(wm831x_reg_unlock);
170 static int wm831x_read(struct wm831x *wm831x, unsigned short reg,
171 int bytes, void *dest)
179 ret = wm831x->read_dev(wm831x, reg, bytes, dest);
183 for (i = 0; i < bytes / 2; i++) {
184 buf[i] = be16_to_cpu(buf[i]);
186 dev_vdbg(wm831x->dev, "Read %04x from R%d(0x%x)\n",
187 buf[i], reg + i, reg + i);
194 * wm831x_reg_read: Read a single WM831x register.
196 * @wm831x: Device to read from.
197 * @reg: Register to read.
199 int wm831x_reg_read(struct wm831x *wm831x, unsigned short reg)
204 mutex_lock(&wm831x->io_lock);
206 ret = wm831x_read(wm831x, reg, 2, &val);
208 mutex_unlock(&wm831x->io_lock);
215 EXPORT_SYMBOL_GPL(wm831x_reg_read);
218 * wm831x_bulk_read: Read multiple WM831x registers
220 * @wm831x: Device to read from
221 * @reg: First register
222 * @count: Number of registers
223 * @buf: Buffer to fill.
225 int wm831x_bulk_read(struct wm831x *wm831x, unsigned short reg,
230 mutex_lock(&wm831x->io_lock);
232 ret = wm831x_read(wm831x, reg, count * 2, buf);
234 mutex_unlock(&wm831x->io_lock);
238 EXPORT_SYMBOL_GPL(wm831x_bulk_read);
240 static int wm831x_write(struct wm831x *wm831x, unsigned short reg,
241 int bytes, void *src)
249 for (i = 0; i < bytes / 2; i++) {
250 if (wm831x_reg_locked(wm831x, reg))
253 dev_vdbg(wm831x->dev, "Write %04x to R%d(0x%x)\n",
254 buf[i], reg + i, reg + i);
256 buf[i] = cpu_to_be16(buf[i]);
259 return wm831x->write_dev(wm831x, reg, bytes, src);
263 * wm831x_reg_write: Write a single WM831x register.
265 * @wm831x: Device to write to.
266 * @reg: Register to write to.
267 * @val: Value to write.
269 int wm831x_reg_write(struct wm831x *wm831x, unsigned short reg,
274 mutex_lock(&wm831x->io_lock);
276 ret = wm831x_write(wm831x, reg, 2, &val);
278 mutex_unlock(&wm831x->io_lock);
282 EXPORT_SYMBOL_GPL(wm831x_reg_write);
285 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
287 * @wm831x: Device to write to.
288 * @reg: Register to write to.
289 * @mask: Mask of bits to set.
290 * @val: Value to set (unshifted)
292 int wm831x_set_bits(struct wm831x *wm831x, unsigned short reg,
293 unsigned short mask, unsigned short val)
298 mutex_lock(&wm831x->io_lock);
300 ret = wm831x_read(wm831x, reg, 2, &r);
307 ret = wm831x_write(wm831x, reg, 2, &r);
310 mutex_unlock(&wm831x->io_lock);
314 EXPORT_SYMBOL_GPL(wm831x_set_bits);
317 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
319 * @wm831x: Device to read from.
320 * @input: AUXADC input to read.
322 int wm831x_auxadc_read(struct wm831x *wm831x, enum wm831x_auxadc input)
326 mutex_lock(&wm831x->auxadc_lock);
328 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
329 WM831X_AUX_ENA, WM831X_AUX_ENA);
331 dev_err(wm831x->dev, "Failed to enable AUXADC: %d\n", ret);
335 /* We force a single source at present */
337 ret = wm831x_reg_write(wm831x, WM831X_AUXADC_SOURCE,
340 dev_err(wm831x->dev, "Failed to set AUXADC source: %d\n", ret);
344 ret = wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL,
345 WM831X_AUX_CVT_ENA, WM831X_AUX_CVT_ENA);
347 dev_err(wm831x->dev, "Failed to start AUXADC: %d\n", ret);
351 /* Ignore the result to allow us to soldier on without IRQ hookup */
352 wait_for_completion_timeout(&wm831x->auxadc_done, msecs_to_jiffies(5));
354 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_CONTROL);
356 dev_err(wm831x->dev, "AUXADC status read failed: %d\n", ret);
360 if (ret & WM831X_AUX_CVT_ENA) {
361 dev_err(wm831x->dev, "Timed out reading AUXADC\n");
366 ret = wm831x_reg_read(wm831x, WM831X_AUXADC_DATA);
368 dev_err(wm831x->dev, "Failed to read AUXADC data: %d\n", ret);
370 src = ((ret & WM831X_AUX_DATA_SRC_MASK)
371 >> WM831X_AUX_DATA_SRC_SHIFT) - 1;
374 src = WM831X_AUX_CAL;
377 dev_err(wm831x->dev, "Data from source %d not %d\n",
381 ret &= WM831X_AUX_DATA_MASK;
386 wm831x_set_bits(wm831x, WM831X_AUXADC_CONTROL, WM831X_AUX_ENA, 0);
388 mutex_unlock(&wm831x->auxadc_lock);
391 EXPORT_SYMBOL_GPL(wm831x_auxadc_read);
393 static irqreturn_t wm831x_auxadc_irq(int irq, void *irq_data)
395 struct wm831x *wm831x = irq_data;
397 complete(&wm831x->auxadc_done);
403 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
405 * @wm831x: Device to read from.
406 * @input: AUXADC input to read.
408 int wm831x_auxadc_read_uv(struct wm831x *wm831x, enum wm831x_auxadc input)
412 ret = wm831x_auxadc_read(wm831x, input);
420 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv);
422 static struct resource wm831x_dcdc1_resources[] = {
424 .start = WM831X_DC1_CONTROL_1,
425 .end = WM831X_DC1_DVS_CONTROL,
426 .flags = IORESOURCE_IO,
430 .start = WM831X_IRQ_UV_DC1,
431 .end = WM831X_IRQ_UV_DC1,
432 .flags = IORESOURCE_IRQ,
436 .start = WM831X_IRQ_HC_DC1,
437 .end = WM831X_IRQ_HC_DC1,
438 .flags = IORESOURCE_IRQ,
443 static struct resource wm831x_dcdc2_resources[] = {
445 .start = WM831X_DC2_CONTROL_1,
446 .end = WM831X_DC2_DVS_CONTROL,
447 .flags = IORESOURCE_IO,
451 .start = WM831X_IRQ_UV_DC2,
452 .end = WM831X_IRQ_UV_DC2,
453 .flags = IORESOURCE_IRQ,
457 .start = WM831X_IRQ_HC_DC2,
458 .end = WM831X_IRQ_HC_DC2,
459 .flags = IORESOURCE_IRQ,
463 static struct resource wm831x_dcdc3_resources[] = {
465 .start = WM831X_DC3_CONTROL_1,
466 .end = WM831X_DC3_SLEEP_CONTROL,
467 .flags = IORESOURCE_IO,
471 .start = WM831X_IRQ_UV_DC3,
472 .end = WM831X_IRQ_UV_DC3,
473 .flags = IORESOURCE_IRQ,
477 static struct resource wm831x_dcdc4_resources[] = {
479 .start = WM831X_DC4_CONTROL,
480 .end = WM831X_DC4_SLEEP_CONTROL,
481 .flags = IORESOURCE_IO,
485 .start = WM831X_IRQ_UV_DC4,
486 .end = WM831X_IRQ_UV_DC4,
487 .flags = IORESOURCE_IRQ,
491 static struct resource wm8320_dcdc4_buck_resources[] = {
493 .start = WM831X_DC4_CONTROL,
494 .end = WM832X_DC4_SLEEP_CONTROL,
495 .flags = IORESOURCE_IO,
499 .start = WM831X_IRQ_UV_DC4,
500 .end = WM831X_IRQ_UV_DC4,
501 .flags = IORESOURCE_IRQ,
505 static struct resource wm831x_gpio_resources[] = {
507 .start = WM831X_IRQ_GPIO_1,
508 .end = WM831X_IRQ_GPIO_16,
509 .flags = IORESOURCE_IRQ,
513 static struct resource wm831x_isink1_resources[] = {
515 .start = WM831X_CURRENT_SINK_1,
516 .end = WM831X_CURRENT_SINK_1,
517 .flags = IORESOURCE_IO,
520 .start = WM831X_IRQ_CS1,
521 .end = WM831X_IRQ_CS1,
522 .flags = IORESOURCE_IRQ,
526 static struct resource wm831x_isink2_resources[] = {
528 .start = WM831X_CURRENT_SINK_2,
529 .end = WM831X_CURRENT_SINK_2,
530 .flags = IORESOURCE_IO,
533 .start = WM831X_IRQ_CS2,
534 .end = WM831X_IRQ_CS2,
535 .flags = IORESOURCE_IRQ,
539 static struct resource wm831x_ldo1_resources[] = {
541 .start = WM831X_LDO1_CONTROL,
542 .end = WM831X_LDO1_SLEEP_CONTROL,
543 .flags = IORESOURCE_IO,
547 .start = WM831X_IRQ_UV_LDO1,
548 .end = WM831X_IRQ_UV_LDO1,
549 .flags = IORESOURCE_IRQ,
553 static struct resource wm831x_ldo2_resources[] = {
555 .start = WM831X_LDO2_CONTROL,
556 .end = WM831X_LDO2_SLEEP_CONTROL,
557 .flags = IORESOURCE_IO,
561 .start = WM831X_IRQ_UV_LDO2,
562 .end = WM831X_IRQ_UV_LDO2,
563 .flags = IORESOURCE_IRQ,
567 static struct resource wm831x_ldo3_resources[] = {
569 .start = WM831X_LDO3_CONTROL,
570 .end = WM831X_LDO3_SLEEP_CONTROL,
571 .flags = IORESOURCE_IO,
575 .start = WM831X_IRQ_UV_LDO3,
576 .end = WM831X_IRQ_UV_LDO3,
577 .flags = IORESOURCE_IRQ,
581 static struct resource wm831x_ldo4_resources[] = {
583 .start = WM831X_LDO4_CONTROL,
584 .end = WM831X_LDO4_SLEEP_CONTROL,
585 .flags = IORESOURCE_IO,
589 .start = WM831X_IRQ_UV_LDO4,
590 .end = WM831X_IRQ_UV_LDO4,
591 .flags = IORESOURCE_IRQ,
595 static struct resource wm831x_ldo5_resources[] = {
597 .start = WM831X_LDO5_CONTROL,
598 .end = WM831X_LDO5_SLEEP_CONTROL,
599 .flags = IORESOURCE_IO,
603 .start = WM831X_IRQ_UV_LDO5,
604 .end = WM831X_IRQ_UV_LDO5,
605 .flags = IORESOURCE_IRQ,
609 static struct resource wm831x_ldo6_resources[] = {
611 .start = WM831X_LDO6_CONTROL,
612 .end = WM831X_LDO6_SLEEP_CONTROL,
613 .flags = IORESOURCE_IO,
617 .start = WM831X_IRQ_UV_LDO6,
618 .end = WM831X_IRQ_UV_LDO6,
619 .flags = IORESOURCE_IRQ,
623 static struct resource wm831x_ldo7_resources[] = {
625 .start = WM831X_LDO7_CONTROL,
626 .end = WM831X_LDO7_SLEEP_CONTROL,
627 .flags = IORESOURCE_IO,
631 .start = WM831X_IRQ_UV_LDO7,
632 .end = WM831X_IRQ_UV_LDO7,
633 .flags = IORESOURCE_IRQ,
637 static struct resource wm831x_ldo8_resources[] = {
639 .start = WM831X_LDO8_CONTROL,
640 .end = WM831X_LDO8_SLEEP_CONTROL,
641 .flags = IORESOURCE_IO,
645 .start = WM831X_IRQ_UV_LDO8,
646 .end = WM831X_IRQ_UV_LDO8,
647 .flags = IORESOURCE_IRQ,
651 static struct resource wm831x_ldo9_resources[] = {
653 .start = WM831X_LDO9_CONTROL,
654 .end = WM831X_LDO9_SLEEP_CONTROL,
655 .flags = IORESOURCE_IO,
659 .start = WM831X_IRQ_UV_LDO9,
660 .end = WM831X_IRQ_UV_LDO9,
661 .flags = IORESOURCE_IRQ,
665 static struct resource wm831x_ldo10_resources[] = {
667 .start = WM831X_LDO10_CONTROL,
668 .end = WM831X_LDO10_SLEEP_CONTROL,
669 .flags = IORESOURCE_IO,
673 .start = WM831X_IRQ_UV_LDO10,
674 .end = WM831X_IRQ_UV_LDO10,
675 .flags = IORESOURCE_IRQ,
679 static struct resource wm831x_ldo11_resources[] = {
681 .start = WM831X_LDO11_ON_CONTROL,
682 .end = WM831X_LDO11_SLEEP_CONTROL,
683 .flags = IORESOURCE_IO,
687 static struct resource wm831x_on_resources[] = {
689 .start = WM831X_IRQ_ON,
690 .end = WM831X_IRQ_ON,
691 .flags = IORESOURCE_IRQ,
696 static struct resource wm831x_power_resources[] = {
699 .start = WM831X_IRQ_PPM_SYSLO,
700 .end = WM831X_IRQ_PPM_SYSLO,
701 .flags = IORESOURCE_IRQ,
705 .start = WM831X_IRQ_PPM_PWR_SRC,
706 .end = WM831X_IRQ_PPM_PWR_SRC,
707 .flags = IORESOURCE_IRQ,
711 .start = WM831X_IRQ_PPM_USB_CURR,
712 .end = WM831X_IRQ_PPM_USB_CURR,
713 .flags = IORESOURCE_IRQ,
717 .start = WM831X_IRQ_CHG_BATT_HOT,
718 .end = WM831X_IRQ_CHG_BATT_HOT,
719 .flags = IORESOURCE_IRQ,
723 .start = WM831X_IRQ_CHG_BATT_COLD,
724 .end = WM831X_IRQ_CHG_BATT_COLD,
725 .flags = IORESOURCE_IRQ,
729 .start = WM831X_IRQ_CHG_BATT_FAIL,
730 .end = WM831X_IRQ_CHG_BATT_FAIL,
731 .flags = IORESOURCE_IRQ,
735 .start = WM831X_IRQ_CHG_OV,
736 .end = WM831X_IRQ_CHG_OV,
737 .flags = IORESOURCE_IRQ,
741 .start = WM831X_IRQ_CHG_END,
742 .end = WM831X_IRQ_CHG_END,
743 .flags = IORESOURCE_IRQ,
747 .start = WM831X_IRQ_CHG_TO,
748 .end = WM831X_IRQ_CHG_TO,
749 .flags = IORESOURCE_IRQ,
753 .start = WM831X_IRQ_CHG_MODE,
754 .end = WM831X_IRQ_CHG_MODE,
755 .flags = IORESOURCE_IRQ,
759 .start = WM831X_IRQ_CHG_START,
760 .end = WM831X_IRQ_CHG_START,
761 .flags = IORESOURCE_IRQ,
765 static struct resource wm831x_rtc_resources[] = {
768 .start = WM831X_IRQ_RTC_PER,
769 .end = WM831X_IRQ_RTC_PER,
770 .flags = IORESOURCE_IRQ,
774 .start = WM831X_IRQ_RTC_ALM,
775 .end = WM831X_IRQ_RTC_ALM,
776 .flags = IORESOURCE_IRQ,
780 static struct resource wm831x_status1_resources[] = {
782 .start = WM831X_STATUS_LED_1,
783 .end = WM831X_STATUS_LED_1,
784 .flags = IORESOURCE_IO,
788 static struct resource wm831x_status2_resources[] = {
790 .start = WM831X_STATUS_LED_2,
791 .end = WM831X_STATUS_LED_2,
792 .flags = IORESOURCE_IO,
796 static struct resource wm831x_touch_resources[] = {
799 .start = WM831X_IRQ_TCHPD,
800 .end = WM831X_IRQ_TCHPD,
801 .flags = IORESOURCE_IRQ,
805 .start = WM831X_IRQ_TCHDATA,
806 .end = WM831X_IRQ_TCHDATA,
807 .flags = IORESOURCE_IRQ,
811 static struct resource wm831x_wdt_resources[] = {
813 .start = WM831X_IRQ_WDOG_TO,
814 .end = WM831X_IRQ_WDOG_TO,
815 .flags = IORESOURCE_IRQ,
819 static struct mfd_cell wm8310_devs[] = {
821 .name = "wm831x-backup",
824 .name = "wm831x-buckv",
826 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
827 .resources = wm831x_dcdc1_resources,
830 .name = "wm831x-buckv",
832 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
833 .resources = wm831x_dcdc2_resources,
836 .name = "wm831x-buckp",
838 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
839 .resources = wm831x_dcdc3_resources,
842 .name = "wm831x-boostp",
844 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
845 .resources = wm831x_dcdc4_resources,
848 .name = "wm831x-epe",
852 .name = "wm831x-epe",
856 .name = "wm831x-gpio",
857 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
858 .resources = wm831x_gpio_resources,
861 .name = "wm831x-hwmon",
864 .name = "wm831x-isink",
866 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
867 .resources = wm831x_isink1_resources,
870 .name = "wm831x-isink",
872 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
873 .resources = wm831x_isink2_resources,
876 .name = "wm831x-ldo",
878 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
879 .resources = wm831x_ldo1_resources,
882 .name = "wm831x-ldo",
884 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
885 .resources = wm831x_ldo2_resources,
888 .name = "wm831x-ldo",
890 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
891 .resources = wm831x_ldo3_resources,
894 .name = "wm831x-ldo",
896 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
897 .resources = wm831x_ldo4_resources,
900 .name = "wm831x-ldo",
902 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
903 .resources = wm831x_ldo5_resources,
906 .name = "wm831x-ldo",
908 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
909 .resources = wm831x_ldo6_resources,
912 .name = "wm831x-aldo",
914 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
915 .resources = wm831x_ldo7_resources,
918 .name = "wm831x-aldo",
920 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
921 .resources = wm831x_ldo8_resources,
924 .name = "wm831x-aldo",
926 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
927 .resources = wm831x_ldo9_resources,
930 .name = "wm831x-aldo",
932 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
933 .resources = wm831x_ldo10_resources,
936 .name = "wm831x-alive-ldo",
938 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
939 .resources = wm831x_ldo11_resources,
943 .num_resources = ARRAY_SIZE(wm831x_on_resources),
944 .resources = wm831x_on_resources,
947 .name = "wm831x-power",
948 .num_resources = ARRAY_SIZE(wm831x_power_resources),
949 .resources = wm831x_power_resources,
952 .name = "wm831x-rtc",
953 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
954 .resources = wm831x_rtc_resources,
957 .name = "wm831x-status",
959 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
960 .resources = wm831x_status1_resources,
963 .name = "wm831x-status",
965 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
966 .resources = wm831x_status2_resources,
969 .name = "wm831x-watchdog",
970 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
971 .resources = wm831x_wdt_resources,
975 static struct mfd_cell wm8311_devs[] = {
977 .name = "wm831x-backup",
980 .name = "wm831x-buckv",
982 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
983 .resources = wm831x_dcdc1_resources,
986 .name = "wm831x-buckv",
988 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
989 .resources = wm831x_dcdc2_resources,
992 .name = "wm831x-buckp",
994 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
995 .resources = wm831x_dcdc3_resources,
998 .name = "wm831x-boostp",
1000 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1001 .resources = wm831x_dcdc4_resources,
1004 .name = "wm831x-epe",
1008 .name = "wm831x-epe",
1012 .name = "wm831x-gpio",
1013 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1014 .resources = wm831x_gpio_resources,
1017 .name = "wm831x-hwmon",
1020 .name = "wm831x-isink",
1022 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1023 .resources = wm831x_isink1_resources,
1026 .name = "wm831x-isink",
1028 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1029 .resources = wm831x_isink2_resources,
1032 .name = "wm831x-ldo",
1034 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1035 .resources = wm831x_ldo1_resources,
1038 .name = "wm831x-ldo",
1040 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1041 .resources = wm831x_ldo2_resources,
1044 .name = "wm831x-ldo",
1046 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1047 .resources = wm831x_ldo3_resources,
1050 .name = "wm831x-ldo",
1052 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1053 .resources = wm831x_ldo4_resources,
1056 .name = "wm831x-ldo",
1058 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1059 .resources = wm831x_ldo5_resources,
1062 .name = "wm831x-aldo",
1064 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1065 .resources = wm831x_ldo7_resources,
1068 .name = "wm831x-alive-ldo",
1070 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1071 .resources = wm831x_ldo11_resources,
1074 .name = "wm831x-on",
1075 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1076 .resources = wm831x_on_resources,
1079 .name = "wm831x-power",
1080 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1081 .resources = wm831x_power_resources,
1084 .name = "wm831x-rtc",
1085 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1086 .resources = wm831x_rtc_resources,
1089 .name = "wm831x-status",
1091 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1092 .resources = wm831x_status1_resources,
1095 .name = "wm831x-status",
1097 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1098 .resources = wm831x_status2_resources,
1101 .name = "wm831x-touch",
1102 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1103 .resources = wm831x_touch_resources,
1106 .name = "wm831x-watchdog",
1107 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1108 .resources = wm831x_wdt_resources,
1112 static struct mfd_cell wm8312_devs[] = {
1114 .name = "wm831x-backup",
1117 .name = "wm831x-buckv",
1119 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1120 .resources = wm831x_dcdc1_resources,
1123 .name = "wm831x-buckv",
1125 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1126 .resources = wm831x_dcdc2_resources,
1129 .name = "wm831x-buckp",
1131 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1132 .resources = wm831x_dcdc3_resources,
1135 .name = "wm831x-boostp",
1137 .num_resources = ARRAY_SIZE(wm831x_dcdc4_resources),
1138 .resources = wm831x_dcdc4_resources,
1141 .name = "wm831x-epe",
1145 .name = "wm831x-epe",
1149 .name = "wm831x-gpio",
1150 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1151 .resources = wm831x_gpio_resources,
1154 .name = "wm831x-hwmon",
1157 .name = "wm831x-isink",
1159 .num_resources = ARRAY_SIZE(wm831x_isink1_resources),
1160 .resources = wm831x_isink1_resources,
1163 .name = "wm831x-isink",
1165 .num_resources = ARRAY_SIZE(wm831x_isink2_resources),
1166 .resources = wm831x_isink2_resources,
1169 .name = "wm831x-ldo",
1171 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1172 .resources = wm831x_ldo1_resources,
1175 .name = "wm831x-ldo",
1177 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1178 .resources = wm831x_ldo2_resources,
1181 .name = "wm831x-ldo",
1183 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1184 .resources = wm831x_ldo3_resources,
1187 .name = "wm831x-ldo",
1189 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1190 .resources = wm831x_ldo4_resources,
1193 .name = "wm831x-ldo",
1195 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1196 .resources = wm831x_ldo5_resources,
1199 .name = "wm831x-ldo",
1201 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1202 .resources = wm831x_ldo6_resources,
1205 .name = "wm831x-aldo",
1207 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1208 .resources = wm831x_ldo7_resources,
1211 .name = "wm831x-aldo",
1213 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1214 .resources = wm831x_ldo8_resources,
1217 .name = "wm831x-aldo",
1219 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1220 .resources = wm831x_ldo9_resources,
1223 .name = "wm831x-aldo",
1225 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1226 .resources = wm831x_ldo10_resources,
1229 .name = "wm831x-alive-ldo",
1231 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1232 .resources = wm831x_ldo11_resources,
1235 .name = "wm831x-on",
1236 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1237 .resources = wm831x_on_resources,
1240 .name = "wm831x-power",
1241 .num_resources = ARRAY_SIZE(wm831x_power_resources),
1242 .resources = wm831x_power_resources,
1245 .name = "wm831x-rtc",
1246 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1247 .resources = wm831x_rtc_resources,
1250 .name = "wm831x-status",
1252 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1253 .resources = wm831x_status1_resources,
1256 .name = "wm831x-status",
1258 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1259 .resources = wm831x_status2_resources,
1262 .name = "wm831x-touch",
1263 .num_resources = ARRAY_SIZE(wm831x_touch_resources),
1264 .resources = wm831x_touch_resources,
1267 .name = "wm831x-watchdog",
1268 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1269 .resources = wm831x_wdt_resources,
1273 static struct mfd_cell wm8320_devs[] = {
1275 .name = "wm831x-backup",
1278 .name = "wm831x-buckv",
1280 .num_resources = ARRAY_SIZE(wm831x_dcdc1_resources),
1281 .resources = wm831x_dcdc1_resources,
1284 .name = "wm831x-buckv",
1286 .num_resources = ARRAY_SIZE(wm831x_dcdc2_resources),
1287 .resources = wm831x_dcdc2_resources,
1290 .name = "wm831x-buckp",
1292 .num_resources = ARRAY_SIZE(wm831x_dcdc3_resources),
1293 .resources = wm831x_dcdc3_resources,
1296 .name = "wm831x-buckp",
1298 .num_resources = ARRAY_SIZE(wm8320_dcdc4_buck_resources),
1299 .resources = wm8320_dcdc4_buck_resources,
1302 .name = "wm831x-gpio",
1303 .num_resources = ARRAY_SIZE(wm831x_gpio_resources),
1304 .resources = wm831x_gpio_resources,
1307 .name = "wm831x-hwmon",
1310 .name = "wm831x-ldo",
1312 .num_resources = ARRAY_SIZE(wm831x_ldo1_resources),
1313 .resources = wm831x_ldo1_resources,
1316 .name = "wm831x-ldo",
1318 .num_resources = ARRAY_SIZE(wm831x_ldo2_resources),
1319 .resources = wm831x_ldo2_resources,
1322 .name = "wm831x-ldo",
1324 .num_resources = ARRAY_SIZE(wm831x_ldo3_resources),
1325 .resources = wm831x_ldo3_resources,
1328 .name = "wm831x-ldo",
1330 .num_resources = ARRAY_SIZE(wm831x_ldo4_resources),
1331 .resources = wm831x_ldo4_resources,
1334 .name = "wm831x-ldo",
1336 .num_resources = ARRAY_SIZE(wm831x_ldo5_resources),
1337 .resources = wm831x_ldo5_resources,
1340 .name = "wm831x-ldo",
1342 .num_resources = ARRAY_SIZE(wm831x_ldo6_resources),
1343 .resources = wm831x_ldo6_resources,
1346 .name = "wm831x-aldo",
1348 .num_resources = ARRAY_SIZE(wm831x_ldo7_resources),
1349 .resources = wm831x_ldo7_resources,
1352 .name = "wm831x-aldo",
1354 .num_resources = ARRAY_SIZE(wm831x_ldo8_resources),
1355 .resources = wm831x_ldo8_resources,
1358 .name = "wm831x-aldo",
1360 .num_resources = ARRAY_SIZE(wm831x_ldo9_resources),
1361 .resources = wm831x_ldo9_resources,
1364 .name = "wm831x-aldo",
1366 .num_resources = ARRAY_SIZE(wm831x_ldo10_resources),
1367 .resources = wm831x_ldo10_resources,
1370 .name = "wm831x-alive-ldo",
1372 .num_resources = ARRAY_SIZE(wm831x_ldo11_resources),
1373 .resources = wm831x_ldo11_resources,
1376 .name = "wm831x-on",
1377 .num_resources = ARRAY_SIZE(wm831x_on_resources),
1378 .resources = wm831x_on_resources,
1381 .name = "wm831x-rtc",
1382 .num_resources = ARRAY_SIZE(wm831x_rtc_resources),
1383 .resources = wm831x_rtc_resources,
1386 .name = "wm831x-status",
1388 .num_resources = ARRAY_SIZE(wm831x_status1_resources),
1389 .resources = wm831x_status1_resources,
1392 .name = "wm831x-status",
1394 .num_resources = ARRAY_SIZE(wm831x_status2_resources),
1395 .resources = wm831x_status2_resources,
1398 .name = "wm831x-watchdog",
1399 .num_resources = ARRAY_SIZE(wm831x_wdt_resources),
1400 .resources = wm831x_wdt_resources,
1404 static struct mfd_cell backlight_devs[] = {
1406 .name = "wm831x-backlight",
1411 * Instantiate the generic non-control parts of the device.
1413 static int wm831x_device_init(struct wm831x *wm831x, unsigned long id, int irq)
1415 struct wm831x_pdata *pdata = wm831x->dev->platform_data;
1417 enum wm831x_parent parent;
1420 mutex_init(&wm831x->io_lock);
1421 mutex_init(&wm831x->key_lock);
1422 mutex_init(&wm831x->auxadc_lock);
1423 init_completion(&wm831x->auxadc_done);
1424 dev_set_drvdata(wm831x->dev, wm831x);
1426 ret = wm831x_reg_read(wm831x, WM831X_PARENT_ID);
1428 dev_err(wm831x->dev, "Failed to read parent ID: %d\n", ret);
1431 if (ret != 0x6204) {
1432 dev_err(wm831x->dev, "Device is not a WM831x: ID %x\n", ret);
1437 ret = wm831x_reg_read(wm831x, WM831X_REVISION);
1439 dev_err(wm831x->dev, "Failed to read revision: %d\n", ret);
1442 rev = (ret & WM831X_PARENT_REV_MASK) >> WM831X_PARENT_REV_SHIFT;
1444 ret = wm831x_reg_read(wm831x, WM831X_RESET_ID);
1446 dev_err(wm831x->dev, "Failed to read device ID: %d\n", ret);
1450 /* Some engineering samples do not have the ID set, rely on
1451 * the device being registered correctly.
1454 dev_info(wm831x->dev, "Device is an engineering sample\n");
1461 wm831x->num_gpio = 16;
1463 wm831x->has_gpio_ena = 1;
1464 wm831x->has_cs_sts = 1;
1467 dev_info(wm831x->dev, "WM8310 revision %c\n", 'A' + rev);
1472 wm831x->num_gpio = 16;
1474 wm831x->has_gpio_ena = 1;
1475 wm831x->has_cs_sts = 1;
1478 dev_info(wm831x->dev, "WM8311 revision %c\n", 'A' + rev);
1483 wm831x->num_gpio = 16;
1485 wm831x->has_gpio_ena = 1;
1486 wm831x->has_cs_sts = 1;
1489 dev_info(wm831x->dev, "WM8312 revision %c\n", 'A' + rev);
1494 wm831x->num_gpio = 12;
1495 dev_info(wm831x->dev, "WM8320 revision %c\n", 'A' + rev);
1499 dev_err(wm831x->dev, "Unknown WM831x device %04x\n", ret);
1504 /* This will need revisiting in future but is OK for all
1508 dev_warn(wm831x->dev, "Device was registered as a WM%lx\n",
1511 /* Bootstrap the user key */
1512 ret = wm831x_reg_read(wm831x, WM831X_SECURITY_KEY);
1514 dev_err(wm831x->dev, "Failed to read security key: %d\n", ret);
1518 dev_warn(wm831x->dev, "Security key had non-zero value %x\n",
1520 wm831x_reg_write(wm831x, WM831X_SECURITY_KEY, 0);
1524 if (pdata && pdata->pre_init) {
1525 ret = pdata->pre_init(wm831x);
1527 dev_err(wm831x->dev, "pre_init() failed: %d\n", ret);
1532 ret = wm831x_irq_init(wm831x, irq);
1536 if (wm831x->irq_base) {
1537 ret = request_threaded_irq(wm831x->irq_base +
1538 WM831X_IRQ_AUXADC_DATA,
1539 NULL, wm831x_auxadc_irq, 0,
1542 dev_err(wm831x->dev, "AUXADC IRQ request failed: %d\n",
1546 /* The core device is up, instantiate the subdevices. */
1549 ret = mfd_add_devices(wm831x->dev, -1,
1550 wm8310_devs, ARRAY_SIZE(wm8310_devs),
1551 NULL, wm831x->irq_base);
1555 ret = mfd_add_devices(wm831x->dev, -1,
1556 wm8311_devs, ARRAY_SIZE(wm8311_devs),
1557 NULL, wm831x->irq_base);
1561 ret = mfd_add_devices(wm831x->dev, -1,
1562 wm8312_devs, ARRAY_SIZE(wm8312_devs),
1563 NULL, wm831x->irq_base);
1567 ret = mfd_add_devices(wm831x->dev, -1,
1568 wm8320_devs, ARRAY_SIZE(wm8320_devs),
1573 /* If this happens the bus probe function is buggy */
1578 dev_err(wm831x->dev, "Failed to add children\n");
1582 if (pdata && pdata->backlight) {
1583 /* Treat errors as non-critical */
1584 ret = mfd_add_devices(wm831x->dev, -1, backlight_devs,
1585 ARRAY_SIZE(backlight_devs), NULL,
1588 dev_err(wm831x->dev, "Failed to add backlight: %d\n",
1592 wm831x_otp_init(wm831x);
1594 if (pdata && pdata->post_init) {
1595 ret = pdata->post_init(wm831x);
1597 dev_err(wm831x->dev, "post_init() failed: %d\n", ret);
1605 wm831x_irq_exit(wm831x);
1607 mfd_remove_devices(wm831x->dev);
1612 static void wm831x_device_exit(struct wm831x *wm831x)
1614 wm831x_otp_exit(wm831x);
1615 mfd_remove_devices(wm831x->dev);
1616 if (wm831x->irq_base)
1617 free_irq(wm831x->irq_base + WM831X_IRQ_AUXADC_DATA, wm831x);
1618 wm831x_irq_exit(wm831x);
1622 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
1623 int bytes, void *dest)
1625 struct i2c_client *i2c = wm831x->control_data;
1627 u16 r = cpu_to_be16(reg);
1629 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
1635 ret = i2c_master_recv(i2c, dest, bytes);
1643 /* Currently we allocate the write buffer on the stack; this is OK for
1644 * small writes - if we need to do large writes this will need to be
1647 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
1648 int bytes, void *src)
1650 struct i2c_client *i2c = wm831x->control_data;
1651 unsigned char msg[bytes + 2];
1654 reg = cpu_to_be16(reg);
1655 memcpy(&msg[0], ®, 2);
1656 memcpy(&msg[2], src, bytes);
1658 ret = i2c_master_send(i2c, msg, bytes + 2);
1661 if (ret < bytes + 2)
1667 static int wm831x_i2c_probe(struct i2c_client *i2c,
1668 const struct i2c_device_id *id)
1670 struct wm831x *wm831x;
1672 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
1673 if (wm831x == NULL) {
1678 i2c_set_clientdata(i2c, wm831x);
1679 wm831x->dev = &i2c->dev;
1680 wm831x->control_data = i2c;
1681 wm831x->read_dev = wm831x_i2c_read_device;
1682 wm831x->write_dev = wm831x_i2c_write_device;
1684 return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
1687 static int wm831x_i2c_remove(struct i2c_client *i2c)
1689 struct wm831x *wm831x = i2c_get_clientdata(i2c);
1691 wm831x_device_exit(wm831x);
1696 static const struct i2c_device_id wm831x_i2c_id[] = {
1697 { "wm8310", WM8310 },
1698 { "wm8311", WM8311 },
1699 { "wm8312", WM8312 },
1700 { "wm8320", WM8320 },
1703 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
1706 static struct i2c_driver wm831x_i2c_driver = {
1709 .owner = THIS_MODULE,
1711 .probe = wm831x_i2c_probe,
1712 .remove = wm831x_i2c_remove,
1713 .id_table = wm831x_i2c_id,
1716 static int __init wm831x_i2c_init(void)
1720 ret = i2c_add_driver(&wm831x_i2c_driver);
1722 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
1726 subsys_initcall(wm831x_i2c_init);
1728 static void __exit wm831x_i2c_exit(void)
1730 i2c_del_driver(&wm831x_i2c_driver);
1732 module_exit(wm831x_i2c_exit);
1734 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1735 MODULE_LICENSE("GPL");
1736 MODULE_AUTHOR("Mark Brown");