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
);
100 static int wm831x_reg_locked(struct wm831x
*wm831x
, unsigned short reg
)
106 case WM831X_WATCHDOG
:
107 case WM831X_DC4_CONTROL
:
108 case WM831X_ON_PIN_CONTROL
:
109 case WM831X_BACKUP_CHARGER_CONTROL
:
110 case WM831X_CHARGER_CONTROL_1
:
111 case WM831X_CHARGER_CONTROL_2
:
120 * wm831x_reg_unlock: Unlock user keyed registers
122 * The WM831x has a user key preventing writes to particularly
123 * critical registers. This function locks those registers,
124 * allowing writes to them.
126 void wm831x_reg_lock(struct wm831x
*wm831x
)
130 ret
= wm831x_reg_write(wm831x
, WM831X_SECURITY_KEY
, 0);
132 dev_vdbg(wm831x
->dev
, "Registers locked\n");
134 mutex_lock(&wm831x
->io_lock
);
135 WARN_ON(wm831x
->locked
);
137 mutex_unlock(&wm831x
->io_lock
);
139 dev_err(wm831x
->dev
, "Failed to lock registers: %d\n", ret
);
143 EXPORT_SYMBOL_GPL(wm831x_reg_lock
);
146 * wm831x_reg_unlock: Unlock user keyed registers
148 * The WM831x has a user key preventing writes to particularly
149 * critical registers. This function locks those registers,
150 * preventing spurious writes.
152 int wm831x_reg_unlock(struct wm831x
*wm831x
)
156 /* 0x9716 is the value required to unlock the registers */
157 ret
= wm831x_reg_write(wm831x
, WM831X_SECURITY_KEY
, 0x9716);
159 dev_vdbg(wm831x
->dev
, "Registers unlocked\n");
161 mutex_lock(&wm831x
->io_lock
);
162 WARN_ON(!wm831x
->locked
);
164 mutex_unlock(&wm831x
->io_lock
);
169 EXPORT_SYMBOL_GPL(wm831x_reg_unlock
);
171 static int wm831x_read(struct wm831x
*wm831x
, unsigned short reg
,
172 int bytes
, void *dest
)
180 ret
= wm831x
->read_dev(wm831x
, reg
, bytes
, dest
);
184 for (i
= 0; i
< bytes
/ 2; i
++) {
185 buf
[i
] = be16_to_cpu(buf
[i
]);
187 dev_vdbg(wm831x
->dev
, "Read %04x from R%d(0x%x)\n",
188 buf
[i
], reg
+ i
, reg
+ i
);
195 * wm831x_reg_read: Read a single WM831x register.
197 * @wm831x: Device to read from.
198 * @reg: Register to read.
200 int wm831x_reg_read(struct wm831x
*wm831x
, unsigned short reg
)
205 mutex_lock(&wm831x
->io_lock
);
207 ret
= wm831x_read(wm831x
, reg
, 2, &val
);
209 mutex_unlock(&wm831x
->io_lock
);
216 EXPORT_SYMBOL_GPL(wm831x_reg_read
);
219 * wm831x_bulk_read: Read multiple WM831x registers
221 * @wm831x: Device to read from
222 * @reg: First register
223 * @count: Number of registers
224 * @buf: Buffer to fill.
226 int wm831x_bulk_read(struct wm831x
*wm831x
, unsigned short reg
,
231 mutex_lock(&wm831x
->io_lock
);
233 ret
= wm831x_read(wm831x
, reg
, count
* 2, buf
);
235 mutex_unlock(&wm831x
->io_lock
);
239 EXPORT_SYMBOL_GPL(wm831x_bulk_read
);
241 static int wm831x_write(struct wm831x
*wm831x
, unsigned short reg
,
242 int bytes
, void *src
)
250 for (i
= 0; i
< bytes
/ 2; i
++) {
251 if (wm831x_reg_locked(wm831x
, reg
))
254 dev_vdbg(wm831x
->dev
, "Write %04x to R%d(0x%x)\n",
255 buf
[i
], reg
+ i
, reg
+ i
);
257 buf
[i
] = cpu_to_be16(buf
[i
]);
260 return wm831x
->write_dev(wm831x
, reg
, bytes
, src
);
264 * wm831x_reg_write: Write a single WM831x register.
266 * @wm831x: Device to write to.
267 * @reg: Register to write to.
268 * @val: Value to write.
270 int wm831x_reg_write(struct wm831x
*wm831x
, unsigned short reg
,
275 mutex_lock(&wm831x
->io_lock
);
277 ret
= wm831x_write(wm831x
, reg
, 2, &val
);
279 mutex_unlock(&wm831x
->io_lock
);
283 EXPORT_SYMBOL_GPL(wm831x_reg_write
);
286 * wm831x_set_bits: Set the value of a bitfield in a WM831x register
288 * @wm831x: Device to write to.
289 * @reg: Register to write to.
290 * @mask: Mask of bits to set.
291 * @val: Value to set (unshifted)
293 int wm831x_set_bits(struct wm831x
*wm831x
, unsigned short reg
,
294 unsigned short mask
, unsigned short val
)
299 mutex_lock(&wm831x
->io_lock
);
301 ret
= wm831x_read(wm831x
, reg
, 2, &r
);
308 ret
= wm831x_write(wm831x
, reg
, 2, &r
);
311 mutex_unlock(&wm831x
->io_lock
);
315 EXPORT_SYMBOL_GPL(wm831x_set_bits
);
318 * wm831x_auxadc_read: Read a value from the WM831x AUXADC
320 * @wm831x: Device to read from.
321 * @input: AUXADC input to read.
323 int wm831x_auxadc_read(struct wm831x
*wm831x
, enum wm831x_auxadc input
)
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 ret
= wm831x_set_bits(wm831x
, WM831X_AUXADC_CONTROL
,
346 WM831X_AUX_CVT_ENA
, WM831X_AUX_CVT_ENA
);
348 dev_err(wm831x
->dev
, "Failed to start AUXADC: %d\n", ret
);
352 /* Ignore the result to allow us to soldier on without IRQ hookup */
353 wait_for_completion_timeout(&wm831x
->auxadc_done
, msecs_to_jiffies(5));
355 ret
= wm831x_reg_read(wm831x
, WM831X_AUXADC_CONTROL
);
357 dev_err(wm831x
->dev
, "AUXADC status read failed: %d\n", ret
);
361 if (ret
& WM831X_AUX_CVT_ENA
) {
362 dev_err(wm831x
->dev
, "Timed out reading AUXADC\n");
367 ret
= wm831x_reg_read(wm831x
, WM831X_AUXADC_DATA
);
369 dev_err(wm831x
->dev
, "Failed to read AUXADC data: %d\n", ret
);
371 src
= ((ret
& WM831X_AUX_DATA_SRC_MASK
)
372 >> WM831X_AUX_DATA_SRC_SHIFT
) - 1;
375 src
= WM831X_AUX_CAL
;
378 dev_err(wm831x
->dev
, "Data from source %d not %d\n",
382 ret
&= WM831X_AUX_DATA_MASK
;
387 wm831x_set_bits(wm831x
, WM831X_AUXADC_CONTROL
, WM831X_AUX_ENA
, 0);
389 mutex_unlock(&wm831x
->auxadc_lock
);
392 EXPORT_SYMBOL_GPL(wm831x_auxadc_read
);
394 static irqreturn_t
wm831x_auxadc_irq(int irq
, void *irq_data
)
396 struct wm831x
*wm831x
= irq_data
;
398 complete(&wm831x
->auxadc_done
);
404 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
406 * @wm831x: Device to read from.
407 * @input: AUXADC input to read.
409 int wm831x_auxadc_read_uv(struct wm831x
*wm831x
, enum wm831x_auxadc input
)
413 ret
= wm831x_auxadc_read(wm831x
, input
);
421 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv
);
423 static struct resource wm831x_dcdc1_resources
[] = {
425 .start
= WM831X_DC1_CONTROL_1
,
426 .end
= WM831X_DC1_DVS_CONTROL
,
427 .flags
= IORESOURCE_IO
,
431 .start
= WM831X_IRQ_UV_DC1
,
432 .end
= WM831X_IRQ_UV_DC1
,
433 .flags
= IORESOURCE_IRQ
,
437 .start
= WM831X_IRQ_HC_DC1
,
438 .end
= WM831X_IRQ_HC_DC1
,
439 .flags
= IORESOURCE_IRQ
,
444 static struct resource wm831x_dcdc2_resources
[] = {
446 .start
= WM831X_DC2_CONTROL_1
,
447 .end
= WM831X_DC2_DVS_CONTROL
,
448 .flags
= IORESOURCE_IO
,
452 .start
= WM831X_IRQ_UV_DC2
,
453 .end
= WM831X_IRQ_UV_DC2
,
454 .flags
= IORESOURCE_IRQ
,
458 .start
= WM831X_IRQ_HC_DC2
,
459 .end
= WM831X_IRQ_HC_DC2
,
460 .flags
= IORESOURCE_IRQ
,
464 static struct resource wm831x_dcdc3_resources
[] = {
466 .start
= WM831X_DC3_CONTROL_1
,
467 .end
= WM831X_DC3_SLEEP_CONTROL
,
468 .flags
= IORESOURCE_IO
,
472 .start
= WM831X_IRQ_UV_DC3
,
473 .end
= WM831X_IRQ_UV_DC3
,
474 .flags
= IORESOURCE_IRQ
,
478 static struct resource wm831x_dcdc4_resources
[] = {
480 .start
= WM831X_DC4_CONTROL
,
481 .end
= WM831X_DC4_SLEEP_CONTROL
,
482 .flags
= IORESOURCE_IO
,
486 .start
= WM831X_IRQ_UV_DC4
,
487 .end
= WM831X_IRQ_UV_DC4
,
488 .flags
= IORESOURCE_IRQ
,
492 static struct resource wm8320_dcdc4_buck_resources
[] = {
494 .start
= WM831X_DC4_CONTROL
,
495 .end
= WM832X_DC4_SLEEP_CONTROL
,
496 .flags
= IORESOURCE_IO
,
500 .start
= WM831X_IRQ_UV_DC4
,
501 .end
= WM831X_IRQ_UV_DC4
,
502 .flags
= IORESOURCE_IRQ
,
506 static struct resource wm831x_gpio_resources
[] = {
508 .start
= WM831X_IRQ_GPIO_1
,
509 .end
= WM831X_IRQ_GPIO_16
,
510 .flags
= IORESOURCE_IRQ
,
514 static struct resource wm831x_isink1_resources
[] = {
516 .start
= WM831X_CURRENT_SINK_1
,
517 .end
= WM831X_CURRENT_SINK_1
,
518 .flags
= IORESOURCE_IO
,
521 .start
= WM831X_IRQ_CS1
,
522 .end
= WM831X_IRQ_CS1
,
523 .flags
= IORESOURCE_IRQ
,
527 static struct resource wm831x_isink2_resources
[] = {
529 .start
= WM831X_CURRENT_SINK_2
,
530 .end
= WM831X_CURRENT_SINK_2
,
531 .flags
= IORESOURCE_IO
,
534 .start
= WM831X_IRQ_CS2
,
535 .end
= WM831X_IRQ_CS2
,
536 .flags
= IORESOURCE_IRQ
,
540 static struct resource wm831x_ldo1_resources
[] = {
542 .start
= WM831X_LDO1_CONTROL
,
543 .end
= WM831X_LDO1_SLEEP_CONTROL
,
544 .flags
= IORESOURCE_IO
,
548 .start
= WM831X_IRQ_UV_LDO1
,
549 .end
= WM831X_IRQ_UV_LDO1
,
550 .flags
= IORESOURCE_IRQ
,
554 static struct resource wm831x_ldo2_resources
[] = {
556 .start
= WM831X_LDO2_CONTROL
,
557 .end
= WM831X_LDO2_SLEEP_CONTROL
,
558 .flags
= IORESOURCE_IO
,
562 .start
= WM831X_IRQ_UV_LDO2
,
563 .end
= WM831X_IRQ_UV_LDO2
,
564 .flags
= IORESOURCE_IRQ
,
568 static struct resource wm831x_ldo3_resources
[] = {
570 .start
= WM831X_LDO3_CONTROL
,
571 .end
= WM831X_LDO3_SLEEP_CONTROL
,
572 .flags
= IORESOURCE_IO
,
576 .start
= WM831X_IRQ_UV_LDO3
,
577 .end
= WM831X_IRQ_UV_LDO3
,
578 .flags
= IORESOURCE_IRQ
,
582 static struct resource wm831x_ldo4_resources
[] = {
584 .start
= WM831X_LDO4_CONTROL
,
585 .end
= WM831X_LDO4_SLEEP_CONTROL
,
586 .flags
= IORESOURCE_IO
,
590 .start
= WM831X_IRQ_UV_LDO4
,
591 .end
= WM831X_IRQ_UV_LDO4
,
592 .flags
= IORESOURCE_IRQ
,
596 static struct resource wm831x_ldo5_resources
[] = {
598 .start
= WM831X_LDO5_CONTROL
,
599 .end
= WM831X_LDO5_SLEEP_CONTROL
,
600 .flags
= IORESOURCE_IO
,
604 .start
= WM831X_IRQ_UV_LDO5
,
605 .end
= WM831X_IRQ_UV_LDO5
,
606 .flags
= IORESOURCE_IRQ
,
610 static struct resource wm831x_ldo6_resources
[] = {
612 .start
= WM831X_LDO6_CONTROL
,
613 .end
= WM831X_LDO6_SLEEP_CONTROL
,
614 .flags
= IORESOURCE_IO
,
618 .start
= WM831X_IRQ_UV_LDO6
,
619 .end
= WM831X_IRQ_UV_LDO6
,
620 .flags
= IORESOURCE_IRQ
,
624 static struct resource wm831x_ldo7_resources
[] = {
626 .start
= WM831X_LDO7_CONTROL
,
627 .end
= WM831X_LDO7_SLEEP_CONTROL
,
628 .flags
= IORESOURCE_IO
,
632 .start
= WM831X_IRQ_UV_LDO7
,
633 .end
= WM831X_IRQ_UV_LDO7
,
634 .flags
= IORESOURCE_IRQ
,
638 static struct resource wm831x_ldo8_resources
[] = {
640 .start
= WM831X_LDO8_CONTROL
,
641 .end
= WM831X_LDO8_SLEEP_CONTROL
,
642 .flags
= IORESOURCE_IO
,
646 .start
= WM831X_IRQ_UV_LDO8
,
647 .end
= WM831X_IRQ_UV_LDO8
,
648 .flags
= IORESOURCE_IRQ
,
652 static struct resource wm831x_ldo9_resources
[] = {
654 .start
= WM831X_LDO9_CONTROL
,
655 .end
= WM831X_LDO9_SLEEP_CONTROL
,
656 .flags
= IORESOURCE_IO
,
660 .start
= WM831X_IRQ_UV_LDO9
,
661 .end
= WM831X_IRQ_UV_LDO9
,
662 .flags
= IORESOURCE_IRQ
,
666 static struct resource wm831x_ldo10_resources
[] = {
668 .start
= WM831X_LDO10_CONTROL
,
669 .end
= WM831X_LDO10_SLEEP_CONTROL
,
670 .flags
= IORESOURCE_IO
,
674 .start
= WM831X_IRQ_UV_LDO10
,
675 .end
= WM831X_IRQ_UV_LDO10
,
676 .flags
= IORESOURCE_IRQ
,
680 static struct resource wm831x_ldo11_resources
[] = {
682 .start
= WM831X_LDO11_ON_CONTROL
,
683 .end
= WM831X_LDO11_SLEEP_CONTROL
,
684 .flags
= IORESOURCE_IO
,
688 static struct resource wm831x_on_resources
[] = {
690 .start
= WM831X_IRQ_ON
,
691 .end
= WM831X_IRQ_ON
,
692 .flags
= IORESOURCE_IRQ
,
697 static struct resource wm831x_power_resources
[] = {
700 .start
= WM831X_IRQ_PPM_SYSLO
,
701 .end
= WM831X_IRQ_PPM_SYSLO
,
702 .flags
= IORESOURCE_IRQ
,
706 .start
= WM831X_IRQ_PPM_PWR_SRC
,
707 .end
= WM831X_IRQ_PPM_PWR_SRC
,
708 .flags
= IORESOURCE_IRQ
,
712 .start
= WM831X_IRQ_PPM_USB_CURR
,
713 .end
= WM831X_IRQ_PPM_USB_CURR
,
714 .flags
= IORESOURCE_IRQ
,
718 .start
= WM831X_IRQ_CHG_BATT_HOT
,
719 .end
= WM831X_IRQ_CHG_BATT_HOT
,
720 .flags
= IORESOURCE_IRQ
,
724 .start
= WM831X_IRQ_CHG_BATT_COLD
,
725 .end
= WM831X_IRQ_CHG_BATT_COLD
,
726 .flags
= IORESOURCE_IRQ
,
730 .start
= WM831X_IRQ_CHG_BATT_FAIL
,
731 .end
= WM831X_IRQ_CHG_BATT_FAIL
,
732 .flags
= IORESOURCE_IRQ
,
736 .start
= WM831X_IRQ_CHG_OV
,
737 .end
= WM831X_IRQ_CHG_OV
,
738 .flags
= IORESOURCE_IRQ
,
742 .start
= WM831X_IRQ_CHG_END
,
743 .end
= WM831X_IRQ_CHG_END
,
744 .flags
= IORESOURCE_IRQ
,
748 .start
= WM831X_IRQ_CHG_TO
,
749 .end
= WM831X_IRQ_CHG_TO
,
750 .flags
= IORESOURCE_IRQ
,
754 .start
= WM831X_IRQ_CHG_MODE
,
755 .end
= WM831X_IRQ_CHG_MODE
,
756 .flags
= IORESOURCE_IRQ
,
760 .start
= WM831X_IRQ_CHG_START
,
761 .end
= WM831X_IRQ_CHG_START
,
762 .flags
= IORESOURCE_IRQ
,
766 static struct resource wm831x_rtc_resources
[] = {
769 .start
= WM831X_IRQ_RTC_PER
,
770 .end
= WM831X_IRQ_RTC_PER
,
771 .flags
= IORESOURCE_IRQ
,
775 .start
= WM831X_IRQ_RTC_ALM
,
776 .end
= WM831X_IRQ_RTC_ALM
,
777 .flags
= IORESOURCE_IRQ
,
781 static struct resource wm831x_status1_resources
[] = {
783 .start
= WM831X_STATUS_LED_1
,
784 .end
= WM831X_STATUS_LED_1
,
785 .flags
= IORESOURCE_IO
,
789 static struct resource wm831x_status2_resources
[] = {
791 .start
= WM831X_STATUS_LED_2
,
792 .end
= WM831X_STATUS_LED_2
,
793 .flags
= IORESOURCE_IO
,
797 static struct resource wm831x_touch_resources
[] = {
800 .start
= WM831X_IRQ_TCHPD
,
801 .end
= WM831X_IRQ_TCHPD
,
802 .flags
= IORESOURCE_IRQ
,
806 .start
= WM831X_IRQ_TCHDATA
,
807 .end
= WM831X_IRQ_TCHDATA
,
808 .flags
= IORESOURCE_IRQ
,
812 static struct resource wm831x_wdt_resources
[] = {
814 .start
= WM831X_IRQ_WDOG_TO
,
815 .end
= WM831X_IRQ_WDOG_TO
,
816 .flags
= IORESOURCE_IRQ
,
820 static struct mfd_cell wm8310_devs
[] = {
822 .name
= "wm831x-backup",
825 .name
= "wm831x-buckv",
827 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
828 .resources
= wm831x_dcdc1_resources
,
831 .name
= "wm831x-buckv",
833 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
834 .resources
= wm831x_dcdc2_resources
,
837 .name
= "wm831x-buckp",
839 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
840 .resources
= wm831x_dcdc3_resources
,
843 .name
= "wm831x-boostp",
845 .num_resources
= ARRAY_SIZE(wm831x_dcdc4_resources
),
846 .resources
= wm831x_dcdc4_resources
,
849 .name
= "wm831x-epe",
853 .name
= "wm831x-epe",
857 .name
= "wm831x-gpio",
858 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
859 .resources
= wm831x_gpio_resources
,
862 .name
= "wm831x-hwmon",
865 .name
= "wm831x-isink",
867 .num_resources
= ARRAY_SIZE(wm831x_isink1_resources
),
868 .resources
= wm831x_isink1_resources
,
871 .name
= "wm831x-isink",
873 .num_resources
= ARRAY_SIZE(wm831x_isink2_resources
),
874 .resources
= wm831x_isink2_resources
,
877 .name
= "wm831x-ldo",
879 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
880 .resources
= wm831x_ldo1_resources
,
883 .name
= "wm831x-ldo",
885 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
886 .resources
= wm831x_ldo2_resources
,
889 .name
= "wm831x-ldo",
891 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
892 .resources
= wm831x_ldo3_resources
,
895 .name
= "wm831x-ldo",
897 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
898 .resources
= wm831x_ldo4_resources
,
901 .name
= "wm831x-ldo",
903 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
904 .resources
= wm831x_ldo5_resources
,
907 .name
= "wm831x-ldo",
909 .num_resources
= ARRAY_SIZE(wm831x_ldo6_resources
),
910 .resources
= wm831x_ldo6_resources
,
913 .name
= "wm831x-aldo",
915 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
916 .resources
= wm831x_ldo7_resources
,
919 .name
= "wm831x-aldo",
921 .num_resources
= ARRAY_SIZE(wm831x_ldo8_resources
),
922 .resources
= wm831x_ldo8_resources
,
925 .name
= "wm831x-aldo",
927 .num_resources
= ARRAY_SIZE(wm831x_ldo9_resources
),
928 .resources
= wm831x_ldo9_resources
,
931 .name
= "wm831x-aldo",
933 .num_resources
= ARRAY_SIZE(wm831x_ldo10_resources
),
934 .resources
= wm831x_ldo10_resources
,
937 .name
= "wm831x-alive-ldo",
939 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
940 .resources
= wm831x_ldo11_resources
,
944 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
945 .resources
= wm831x_on_resources
,
948 .name
= "wm831x-power",
949 .num_resources
= ARRAY_SIZE(wm831x_power_resources
),
950 .resources
= wm831x_power_resources
,
953 .name
= "wm831x-rtc",
954 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
955 .resources
= wm831x_rtc_resources
,
958 .name
= "wm831x-status",
960 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
961 .resources
= wm831x_status1_resources
,
964 .name
= "wm831x-status",
966 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
967 .resources
= wm831x_status2_resources
,
970 .name
= "wm831x-watchdog",
971 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
972 .resources
= wm831x_wdt_resources
,
976 static struct mfd_cell wm8311_devs
[] = {
978 .name
= "wm831x-backup",
981 .name
= "wm831x-buckv",
983 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
984 .resources
= wm831x_dcdc1_resources
,
987 .name
= "wm831x-buckv",
989 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
990 .resources
= wm831x_dcdc2_resources
,
993 .name
= "wm831x-buckp",
995 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
996 .resources
= wm831x_dcdc3_resources
,
999 .name
= "wm831x-boostp",
1001 .num_resources
= ARRAY_SIZE(wm831x_dcdc4_resources
),
1002 .resources
= wm831x_dcdc4_resources
,
1005 .name
= "wm831x-epe",
1009 .name
= "wm831x-epe",
1013 .name
= "wm831x-gpio",
1014 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
1015 .resources
= wm831x_gpio_resources
,
1018 .name
= "wm831x-hwmon",
1021 .name
= "wm831x-isink",
1023 .num_resources
= ARRAY_SIZE(wm831x_isink1_resources
),
1024 .resources
= wm831x_isink1_resources
,
1027 .name
= "wm831x-isink",
1029 .num_resources
= ARRAY_SIZE(wm831x_isink2_resources
),
1030 .resources
= wm831x_isink2_resources
,
1033 .name
= "wm831x-ldo",
1035 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
1036 .resources
= wm831x_ldo1_resources
,
1039 .name
= "wm831x-ldo",
1041 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
1042 .resources
= wm831x_ldo2_resources
,
1045 .name
= "wm831x-ldo",
1047 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
1048 .resources
= wm831x_ldo3_resources
,
1051 .name
= "wm831x-ldo",
1053 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
1054 .resources
= wm831x_ldo4_resources
,
1057 .name
= "wm831x-ldo",
1059 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
1060 .resources
= wm831x_ldo5_resources
,
1063 .name
= "wm831x-aldo",
1065 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
1066 .resources
= wm831x_ldo7_resources
,
1069 .name
= "wm831x-alive-ldo",
1071 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
1072 .resources
= wm831x_ldo11_resources
,
1075 .name
= "wm831x-on",
1076 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
1077 .resources
= wm831x_on_resources
,
1080 .name
= "wm831x-power",
1081 .num_resources
= ARRAY_SIZE(wm831x_power_resources
),
1082 .resources
= wm831x_power_resources
,
1085 .name
= "wm831x-rtc",
1086 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
1087 .resources
= wm831x_rtc_resources
,
1090 .name
= "wm831x-status",
1092 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
1093 .resources
= wm831x_status1_resources
,
1096 .name
= "wm831x-status",
1098 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
1099 .resources
= wm831x_status2_resources
,
1102 .name
= "wm831x-touch",
1103 .num_resources
= ARRAY_SIZE(wm831x_touch_resources
),
1104 .resources
= wm831x_touch_resources
,
1107 .name
= "wm831x-watchdog",
1108 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
1109 .resources
= wm831x_wdt_resources
,
1113 static struct mfd_cell wm8312_devs
[] = {
1115 .name
= "wm831x-backup",
1118 .name
= "wm831x-buckv",
1120 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
1121 .resources
= wm831x_dcdc1_resources
,
1124 .name
= "wm831x-buckv",
1126 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
1127 .resources
= wm831x_dcdc2_resources
,
1130 .name
= "wm831x-buckp",
1132 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
1133 .resources
= wm831x_dcdc3_resources
,
1136 .name
= "wm831x-boostp",
1138 .num_resources
= ARRAY_SIZE(wm831x_dcdc4_resources
),
1139 .resources
= wm831x_dcdc4_resources
,
1142 .name
= "wm831x-epe",
1146 .name
= "wm831x-epe",
1150 .name
= "wm831x-gpio",
1151 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
1152 .resources
= wm831x_gpio_resources
,
1155 .name
= "wm831x-hwmon",
1158 .name
= "wm831x-isink",
1160 .num_resources
= ARRAY_SIZE(wm831x_isink1_resources
),
1161 .resources
= wm831x_isink1_resources
,
1164 .name
= "wm831x-isink",
1166 .num_resources
= ARRAY_SIZE(wm831x_isink2_resources
),
1167 .resources
= wm831x_isink2_resources
,
1170 .name
= "wm831x-ldo",
1172 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
1173 .resources
= wm831x_ldo1_resources
,
1176 .name
= "wm831x-ldo",
1178 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
1179 .resources
= wm831x_ldo2_resources
,
1182 .name
= "wm831x-ldo",
1184 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
1185 .resources
= wm831x_ldo3_resources
,
1188 .name
= "wm831x-ldo",
1190 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
1191 .resources
= wm831x_ldo4_resources
,
1194 .name
= "wm831x-ldo",
1196 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
1197 .resources
= wm831x_ldo5_resources
,
1200 .name
= "wm831x-ldo",
1202 .num_resources
= ARRAY_SIZE(wm831x_ldo6_resources
),
1203 .resources
= wm831x_ldo6_resources
,
1206 .name
= "wm831x-aldo",
1208 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
1209 .resources
= wm831x_ldo7_resources
,
1212 .name
= "wm831x-aldo",
1214 .num_resources
= ARRAY_SIZE(wm831x_ldo8_resources
),
1215 .resources
= wm831x_ldo8_resources
,
1218 .name
= "wm831x-aldo",
1220 .num_resources
= ARRAY_SIZE(wm831x_ldo9_resources
),
1221 .resources
= wm831x_ldo9_resources
,
1224 .name
= "wm831x-aldo",
1226 .num_resources
= ARRAY_SIZE(wm831x_ldo10_resources
),
1227 .resources
= wm831x_ldo10_resources
,
1230 .name
= "wm831x-alive-ldo",
1232 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
1233 .resources
= wm831x_ldo11_resources
,
1236 .name
= "wm831x-on",
1237 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
1238 .resources
= wm831x_on_resources
,
1241 .name
= "wm831x-power",
1242 .num_resources
= ARRAY_SIZE(wm831x_power_resources
),
1243 .resources
= wm831x_power_resources
,
1246 .name
= "wm831x-rtc",
1247 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
1248 .resources
= wm831x_rtc_resources
,
1251 .name
= "wm831x-status",
1253 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
1254 .resources
= wm831x_status1_resources
,
1257 .name
= "wm831x-status",
1259 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
1260 .resources
= wm831x_status2_resources
,
1263 .name
= "wm831x-touch",
1264 .num_resources
= ARRAY_SIZE(wm831x_touch_resources
),
1265 .resources
= wm831x_touch_resources
,
1268 .name
= "wm831x-watchdog",
1269 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
1270 .resources
= wm831x_wdt_resources
,
1274 static struct mfd_cell wm8320_devs
[] = {
1276 .name
= "wm831x-backup",
1279 .name
= "wm831x-buckv",
1281 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
1282 .resources
= wm831x_dcdc1_resources
,
1285 .name
= "wm831x-buckv",
1287 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
1288 .resources
= wm831x_dcdc2_resources
,
1291 .name
= "wm831x-buckp",
1293 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
1294 .resources
= wm831x_dcdc3_resources
,
1297 .name
= "wm831x-buckp",
1299 .num_resources
= ARRAY_SIZE(wm8320_dcdc4_buck_resources
),
1300 .resources
= wm8320_dcdc4_buck_resources
,
1303 .name
= "wm831x-gpio",
1304 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
1305 .resources
= wm831x_gpio_resources
,
1308 .name
= "wm831x-hwmon",
1311 .name
= "wm831x-ldo",
1313 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
1314 .resources
= wm831x_ldo1_resources
,
1317 .name
= "wm831x-ldo",
1319 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
1320 .resources
= wm831x_ldo2_resources
,
1323 .name
= "wm831x-ldo",
1325 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
1326 .resources
= wm831x_ldo3_resources
,
1329 .name
= "wm831x-ldo",
1331 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
1332 .resources
= wm831x_ldo4_resources
,
1335 .name
= "wm831x-ldo",
1337 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
1338 .resources
= wm831x_ldo5_resources
,
1341 .name
= "wm831x-ldo",
1343 .num_resources
= ARRAY_SIZE(wm831x_ldo6_resources
),
1344 .resources
= wm831x_ldo6_resources
,
1347 .name
= "wm831x-aldo",
1349 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
1350 .resources
= wm831x_ldo7_resources
,
1353 .name
= "wm831x-aldo",
1355 .num_resources
= ARRAY_SIZE(wm831x_ldo8_resources
),
1356 .resources
= wm831x_ldo8_resources
,
1359 .name
= "wm831x-aldo",
1361 .num_resources
= ARRAY_SIZE(wm831x_ldo9_resources
),
1362 .resources
= wm831x_ldo9_resources
,
1365 .name
= "wm831x-aldo",
1367 .num_resources
= ARRAY_SIZE(wm831x_ldo10_resources
),
1368 .resources
= wm831x_ldo10_resources
,
1371 .name
= "wm831x-alive-ldo",
1373 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
1374 .resources
= wm831x_ldo11_resources
,
1377 .name
= "wm831x-on",
1378 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
1379 .resources
= wm831x_on_resources
,
1382 .name
= "wm831x-rtc",
1383 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
1384 .resources
= wm831x_rtc_resources
,
1387 .name
= "wm831x-status",
1389 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
1390 .resources
= wm831x_status1_resources
,
1393 .name
= "wm831x-status",
1395 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
1396 .resources
= wm831x_status2_resources
,
1399 .name
= "wm831x-watchdog",
1400 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
1401 .resources
= wm831x_wdt_resources
,
1405 static struct mfd_cell backlight_devs
[] = {
1407 .name
= "wm831x-backlight",
1412 * Instantiate the generic non-control parts of the device.
1414 static int wm831x_device_init(struct wm831x
*wm831x
, unsigned long id
, int irq
)
1416 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
1418 enum wm831x_parent parent
;
1421 mutex_init(&wm831x
->io_lock
);
1422 mutex_init(&wm831x
->key_lock
);
1423 mutex_init(&wm831x
->auxadc_lock
);
1424 init_completion(&wm831x
->auxadc_done
);
1425 dev_set_drvdata(wm831x
->dev
, wm831x
);
1427 ret
= wm831x_reg_read(wm831x
, WM831X_PARENT_ID
);
1429 dev_err(wm831x
->dev
, "Failed to read parent ID: %d\n", ret
);
1432 if (ret
!= 0x6204) {
1433 dev_err(wm831x
->dev
, "Device is not a WM831x: ID %x\n", ret
);
1438 ret
= wm831x_reg_read(wm831x
, WM831X_REVISION
);
1440 dev_err(wm831x
->dev
, "Failed to read revision: %d\n", ret
);
1443 rev
= (ret
& WM831X_PARENT_REV_MASK
) >> WM831X_PARENT_REV_SHIFT
;
1445 ret
= wm831x_reg_read(wm831x
, WM831X_RESET_ID
);
1447 dev_err(wm831x
->dev
, "Failed to read device ID: %d\n", ret
);
1451 /* Some engineering samples do not have the ID set, rely on
1452 * the device being registered correctly.
1455 dev_info(wm831x
->dev
, "Device is an engineering sample\n");
1462 wm831x
->num_gpio
= 16;
1464 wm831x
->has_gpio_ena
= 1;
1465 wm831x
->has_cs_sts
= 1;
1468 dev_info(wm831x
->dev
, "WM8310 revision %c\n", 'A' + rev
);
1473 wm831x
->num_gpio
= 16;
1475 wm831x
->has_gpio_ena
= 1;
1476 wm831x
->has_cs_sts
= 1;
1479 dev_info(wm831x
->dev
, "WM8311 revision %c\n", 'A' + rev
);
1484 wm831x
->num_gpio
= 16;
1486 wm831x
->has_gpio_ena
= 1;
1487 wm831x
->has_cs_sts
= 1;
1490 dev_info(wm831x
->dev
, "WM8312 revision %c\n", 'A' + rev
);
1495 wm831x
->num_gpio
= 12;
1496 dev_info(wm831x
->dev
, "WM8320 revision %c\n", 'A' + rev
);
1500 dev_err(wm831x
->dev
, "Unknown WM831x device %04x\n", ret
);
1505 /* This will need revisiting in future but is OK for all
1509 dev_warn(wm831x
->dev
, "Device was registered as a WM%lx\n",
1512 /* Bootstrap the user key */
1513 ret
= wm831x_reg_read(wm831x
, WM831X_SECURITY_KEY
);
1515 dev_err(wm831x
->dev
, "Failed to read security key: %d\n", ret
);
1519 dev_warn(wm831x
->dev
, "Security key had non-zero value %x\n",
1521 wm831x_reg_write(wm831x
, WM831X_SECURITY_KEY
, 0);
1525 if (pdata
&& pdata
->pre_init
) {
1526 ret
= pdata
->pre_init(wm831x
);
1528 dev_err(wm831x
->dev
, "pre_init() failed: %d\n", ret
);
1533 ret
= wm831x_irq_init(wm831x
, irq
);
1537 if (wm831x
->irq_base
) {
1538 ret
= request_threaded_irq(wm831x
->irq_base
+
1539 WM831X_IRQ_AUXADC_DATA
,
1540 NULL
, wm831x_auxadc_irq
, 0,
1543 dev_err(wm831x
->dev
, "AUXADC IRQ request failed: %d\n",
1547 /* The core device is up, instantiate the subdevices. */
1550 ret
= mfd_add_devices(wm831x
->dev
, -1,
1551 wm8310_devs
, ARRAY_SIZE(wm8310_devs
),
1552 NULL
, wm831x
->irq_base
);
1556 ret
= mfd_add_devices(wm831x
->dev
, -1,
1557 wm8311_devs
, ARRAY_SIZE(wm8311_devs
),
1558 NULL
, wm831x
->irq_base
);
1562 ret
= mfd_add_devices(wm831x
->dev
, -1,
1563 wm8312_devs
, ARRAY_SIZE(wm8312_devs
),
1564 NULL
, wm831x
->irq_base
);
1568 ret
= mfd_add_devices(wm831x
->dev
, -1,
1569 wm8320_devs
, ARRAY_SIZE(wm8320_devs
),
1574 /* If this happens the bus probe function is buggy */
1579 dev_err(wm831x
->dev
, "Failed to add children\n");
1583 if (pdata
&& pdata
->backlight
) {
1584 /* Treat errors as non-critical */
1585 ret
= mfd_add_devices(wm831x
->dev
, -1, backlight_devs
,
1586 ARRAY_SIZE(backlight_devs
), NULL
,
1589 dev_err(wm831x
->dev
, "Failed to add backlight: %d\n",
1593 wm831x_otp_init(wm831x
);
1595 if (pdata
&& pdata
->post_init
) {
1596 ret
= pdata
->post_init(wm831x
);
1598 dev_err(wm831x
->dev
, "post_init() failed: %d\n", ret
);
1606 wm831x_irq_exit(wm831x
);
1608 mfd_remove_devices(wm831x
->dev
);
1613 static void wm831x_device_exit(struct wm831x
*wm831x
)
1615 wm831x_otp_exit(wm831x
);
1616 mfd_remove_devices(wm831x
->dev
);
1617 if (wm831x
->irq_base
)
1618 free_irq(wm831x
->irq_base
+ WM831X_IRQ_AUXADC_DATA
, wm831x
);
1619 wm831x_irq_exit(wm831x
);
1623 static int wm831x_i2c_read_device(struct wm831x
*wm831x
, unsigned short reg
,
1624 int bytes
, void *dest
)
1626 struct i2c_client
*i2c
= wm831x
->control_data
;
1628 u16 r
= cpu_to_be16(reg
);
1630 ret
= i2c_master_send(i2c
, (unsigned char *)&r
, 2);
1636 ret
= i2c_master_recv(i2c
, dest
, bytes
);
1644 /* Currently we allocate the write buffer on the stack; this is OK for
1645 * small writes - if we need to do large writes this will need to be
1648 static int wm831x_i2c_write_device(struct wm831x
*wm831x
, unsigned short reg
,
1649 int bytes
, void *src
)
1651 struct i2c_client
*i2c
= wm831x
->control_data
;
1652 unsigned char msg
[bytes
+ 2];
1655 reg
= cpu_to_be16(reg
);
1656 memcpy(&msg
[0], ®
, 2);
1657 memcpy(&msg
[2], src
, bytes
);
1659 ret
= i2c_master_send(i2c
, msg
, bytes
+ 2);
1662 if (ret
< bytes
+ 2)
1668 static int wm831x_i2c_probe(struct i2c_client
*i2c
,
1669 const struct i2c_device_id
*id
)
1671 struct wm831x
*wm831x
;
1673 wm831x
= kzalloc(sizeof(struct wm831x
), GFP_KERNEL
);
1674 if (wm831x
== NULL
) {
1679 i2c_set_clientdata(i2c
, wm831x
);
1680 wm831x
->dev
= &i2c
->dev
;
1681 wm831x
->control_data
= i2c
;
1682 wm831x
->read_dev
= wm831x_i2c_read_device
;
1683 wm831x
->write_dev
= wm831x_i2c_write_device
;
1685 return wm831x_device_init(wm831x
, id
->driver_data
, i2c
->irq
);
1688 static int wm831x_i2c_remove(struct i2c_client
*i2c
)
1690 struct wm831x
*wm831x
= i2c_get_clientdata(i2c
);
1692 wm831x_device_exit(wm831x
);
1697 static const struct i2c_device_id wm831x_i2c_id
[] = {
1698 { "wm8310", WM8310
},
1699 { "wm8311", WM8311
},
1700 { "wm8312", WM8312
},
1701 { "wm8320", WM8320
},
1704 MODULE_DEVICE_TABLE(i2c
, wm831x_i2c_id
);
1707 static struct i2c_driver wm831x_i2c_driver
= {
1710 .owner
= THIS_MODULE
,
1712 .probe
= wm831x_i2c_probe
,
1713 .remove
= wm831x_i2c_remove
,
1714 .id_table
= wm831x_i2c_id
,
1717 static int __init
wm831x_i2c_init(void)
1721 ret
= i2c_add_driver(&wm831x_i2c_driver
);
1723 pr_err("Failed to register wm831x I2C driver: %d\n", ret
);
1727 subsys_initcall(wm831x_i2c_init
);
1729 static void __exit
wm831x_i2c_exit(void)
1731 i2c_del_driver(&wm831x_i2c_driver
);
1733 module_exit(wm831x_i2c_exit
);
1735 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1736 MODULE_LICENSE("GPL");
1737 MODULE_AUTHOR("Mark Brown");