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 /* If an interrupt arrived late clean up after it */
353 try_wait_for_completion(&wm831x
->auxadc_done
);
355 /* Ignore the result to allow us to soldier on without IRQ hookup */
356 wait_for_completion_timeout(&wm831x
->auxadc_done
, msecs_to_jiffies(5));
358 ret
= wm831x_reg_read(wm831x
, WM831X_AUXADC_CONTROL
);
360 dev_err(wm831x
->dev
, "AUXADC status read failed: %d\n", ret
);
364 if (ret
& WM831X_AUX_CVT_ENA
) {
365 dev_err(wm831x
->dev
, "Timed out reading AUXADC\n");
370 ret
= wm831x_reg_read(wm831x
, WM831X_AUXADC_DATA
);
372 dev_err(wm831x
->dev
, "Failed to read AUXADC data: %d\n", ret
);
374 src
= ((ret
& WM831X_AUX_DATA_SRC_MASK
)
375 >> WM831X_AUX_DATA_SRC_SHIFT
) - 1;
378 src
= WM831X_AUX_CAL
;
381 dev_err(wm831x
->dev
, "Data from source %d not %d\n",
385 ret
&= WM831X_AUX_DATA_MASK
;
390 wm831x_set_bits(wm831x
, WM831X_AUXADC_CONTROL
, WM831X_AUX_ENA
, 0);
392 mutex_unlock(&wm831x
->auxadc_lock
);
395 EXPORT_SYMBOL_GPL(wm831x_auxadc_read
);
397 static irqreturn_t
wm831x_auxadc_irq(int irq
, void *irq_data
)
399 struct wm831x
*wm831x
= irq_data
;
401 complete(&wm831x
->auxadc_done
);
407 * wm831x_auxadc_read_uv: Read a voltage from the WM831x AUXADC
409 * @wm831x: Device to read from.
410 * @input: AUXADC input to read.
412 int wm831x_auxadc_read_uv(struct wm831x
*wm831x
, enum wm831x_auxadc input
)
416 ret
= wm831x_auxadc_read(wm831x
, input
);
424 EXPORT_SYMBOL_GPL(wm831x_auxadc_read_uv
);
426 static struct resource wm831x_dcdc1_resources
[] = {
428 .start
= WM831X_DC1_CONTROL_1
,
429 .end
= WM831X_DC1_DVS_CONTROL
,
430 .flags
= IORESOURCE_IO
,
434 .start
= WM831X_IRQ_UV_DC1
,
435 .end
= WM831X_IRQ_UV_DC1
,
436 .flags
= IORESOURCE_IRQ
,
440 .start
= WM831X_IRQ_HC_DC1
,
441 .end
= WM831X_IRQ_HC_DC1
,
442 .flags
= IORESOURCE_IRQ
,
447 static struct resource wm831x_dcdc2_resources
[] = {
449 .start
= WM831X_DC2_CONTROL_1
,
450 .end
= WM831X_DC2_DVS_CONTROL
,
451 .flags
= IORESOURCE_IO
,
455 .start
= WM831X_IRQ_UV_DC2
,
456 .end
= WM831X_IRQ_UV_DC2
,
457 .flags
= IORESOURCE_IRQ
,
461 .start
= WM831X_IRQ_HC_DC2
,
462 .end
= WM831X_IRQ_HC_DC2
,
463 .flags
= IORESOURCE_IRQ
,
467 static struct resource wm831x_dcdc3_resources
[] = {
469 .start
= WM831X_DC3_CONTROL_1
,
470 .end
= WM831X_DC3_SLEEP_CONTROL
,
471 .flags
= IORESOURCE_IO
,
475 .start
= WM831X_IRQ_UV_DC3
,
476 .end
= WM831X_IRQ_UV_DC3
,
477 .flags
= IORESOURCE_IRQ
,
481 static struct resource wm831x_dcdc4_resources
[] = {
483 .start
= WM831X_DC4_CONTROL
,
484 .end
= WM831X_DC4_SLEEP_CONTROL
,
485 .flags
= IORESOURCE_IO
,
489 .start
= WM831X_IRQ_UV_DC4
,
490 .end
= WM831X_IRQ_UV_DC4
,
491 .flags
= IORESOURCE_IRQ
,
495 static struct resource wm8320_dcdc4_buck_resources
[] = {
497 .start
= WM831X_DC4_CONTROL
,
498 .end
= WM832X_DC4_SLEEP_CONTROL
,
499 .flags
= IORESOURCE_IO
,
503 .start
= WM831X_IRQ_UV_DC4
,
504 .end
= WM831X_IRQ_UV_DC4
,
505 .flags
= IORESOURCE_IRQ
,
509 static struct resource wm831x_gpio_resources
[] = {
511 .start
= WM831X_IRQ_GPIO_1
,
512 .end
= WM831X_IRQ_GPIO_16
,
513 .flags
= IORESOURCE_IRQ
,
517 static struct resource wm831x_isink1_resources
[] = {
519 .start
= WM831X_CURRENT_SINK_1
,
520 .end
= WM831X_CURRENT_SINK_1
,
521 .flags
= IORESOURCE_IO
,
524 .start
= WM831X_IRQ_CS1
,
525 .end
= WM831X_IRQ_CS1
,
526 .flags
= IORESOURCE_IRQ
,
530 static struct resource wm831x_isink2_resources
[] = {
532 .start
= WM831X_CURRENT_SINK_2
,
533 .end
= WM831X_CURRENT_SINK_2
,
534 .flags
= IORESOURCE_IO
,
537 .start
= WM831X_IRQ_CS2
,
538 .end
= WM831X_IRQ_CS2
,
539 .flags
= IORESOURCE_IRQ
,
543 static struct resource wm831x_ldo1_resources
[] = {
545 .start
= WM831X_LDO1_CONTROL
,
546 .end
= WM831X_LDO1_SLEEP_CONTROL
,
547 .flags
= IORESOURCE_IO
,
551 .start
= WM831X_IRQ_UV_LDO1
,
552 .end
= WM831X_IRQ_UV_LDO1
,
553 .flags
= IORESOURCE_IRQ
,
557 static struct resource wm831x_ldo2_resources
[] = {
559 .start
= WM831X_LDO2_CONTROL
,
560 .end
= WM831X_LDO2_SLEEP_CONTROL
,
561 .flags
= IORESOURCE_IO
,
565 .start
= WM831X_IRQ_UV_LDO2
,
566 .end
= WM831X_IRQ_UV_LDO2
,
567 .flags
= IORESOURCE_IRQ
,
571 static struct resource wm831x_ldo3_resources
[] = {
573 .start
= WM831X_LDO3_CONTROL
,
574 .end
= WM831X_LDO3_SLEEP_CONTROL
,
575 .flags
= IORESOURCE_IO
,
579 .start
= WM831X_IRQ_UV_LDO3
,
580 .end
= WM831X_IRQ_UV_LDO3
,
581 .flags
= IORESOURCE_IRQ
,
585 static struct resource wm831x_ldo4_resources
[] = {
587 .start
= WM831X_LDO4_CONTROL
,
588 .end
= WM831X_LDO4_SLEEP_CONTROL
,
589 .flags
= IORESOURCE_IO
,
593 .start
= WM831X_IRQ_UV_LDO4
,
594 .end
= WM831X_IRQ_UV_LDO4
,
595 .flags
= IORESOURCE_IRQ
,
599 static struct resource wm831x_ldo5_resources
[] = {
601 .start
= WM831X_LDO5_CONTROL
,
602 .end
= WM831X_LDO5_SLEEP_CONTROL
,
603 .flags
= IORESOURCE_IO
,
607 .start
= WM831X_IRQ_UV_LDO5
,
608 .end
= WM831X_IRQ_UV_LDO5
,
609 .flags
= IORESOURCE_IRQ
,
613 static struct resource wm831x_ldo6_resources
[] = {
615 .start
= WM831X_LDO6_CONTROL
,
616 .end
= WM831X_LDO6_SLEEP_CONTROL
,
617 .flags
= IORESOURCE_IO
,
621 .start
= WM831X_IRQ_UV_LDO6
,
622 .end
= WM831X_IRQ_UV_LDO6
,
623 .flags
= IORESOURCE_IRQ
,
627 static struct resource wm831x_ldo7_resources
[] = {
629 .start
= WM831X_LDO7_CONTROL
,
630 .end
= WM831X_LDO7_SLEEP_CONTROL
,
631 .flags
= IORESOURCE_IO
,
635 .start
= WM831X_IRQ_UV_LDO7
,
636 .end
= WM831X_IRQ_UV_LDO7
,
637 .flags
= IORESOURCE_IRQ
,
641 static struct resource wm831x_ldo8_resources
[] = {
643 .start
= WM831X_LDO8_CONTROL
,
644 .end
= WM831X_LDO8_SLEEP_CONTROL
,
645 .flags
= IORESOURCE_IO
,
649 .start
= WM831X_IRQ_UV_LDO8
,
650 .end
= WM831X_IRQ_UV_LDO8
,
651 .flags
= IORESOURCE_IRQ
,
655 static struct resource wm831x_ldo9_resources
[] = {
657 .start
= WM831X_LDO9_CONTROL
,
658 .end
= WM831X_LDO9_SLEEP_CONTROL
,
659 .flags
= IORESOURCE_IO
,
663 .start
= WM831X_IRQ_UV_LDO9
,
664 .end
= WM831X_IRQ_UV_LDO9
,
665 .flags
= IORESOURCE_IRQ
,
669 static struct resource wm831x_ldo10_resources
[] = {
671 .start
= WM831X_LDO10_CONTROL
,
672 .end
= WM831X_LDO10_SLEEP_CONTROL
,
673 .flags
= IORESOURCE_IO
,
677 .start
= WM831X_IRQ_UV_LDO10
,
678 .end
= WM831X_IRQ_UV_LDO10
,
679 .flags
= IORESOURCE_IRQ
,
683 static struct resource wm831x_ldo11_resources
[] = {
685 .start
= WM831X_LDO11_ON_CONTROL
,
686 .end
= WM831X_LDO11_SLEEP_CONTROL
,
687 .flags
= IORESOURCE_IO
,
691 static struct resource wm831x_on_resources
[] = {
693 .start
= WM831X_IRQ_ON
,
694 .end
= WM831X_IRQ_ON
,
695 .flags
= IORESOURCE_IRQ
,
700 static struct resource wm831x_power_resources
[] = {
703 .start
= WM831X_IRQ_PPM_SYSLO
,
704 .end
= WM831X_IRQ_PPM_SYSLO
,
705 .flags
= IORESOURCE_IRQ
,
709 .start
= WM831X_IRQ_PPM_PWR_SRC
,
710 .end
= WM831X_IRQ_PPM_PWR_SRC
,
711 .flags
= IORESOURCE_IRQ
,
715 .start
= WM831X_IRQ_PPM_USB_CURR
,
716 .end
= WM831X_IRQ_PPM_USB_CURR
,
717 .flags
= IORESOURCE_IRQ
,
721 .start
= WM831X_IRQ_CHG_BATT_HOT
,
722 .end
= WM831X_IRQ_CHG_BATT_HOT
,
723 .flags
= IORESOURCE_IRQ
,
727 .start
= WM831X_IRQ_CHG_BATT_COLD
,
728 .end
= WM831X_IRQ_CHG_BATT_COLD
,
729 .flags
= IORESOURCE_IRQ
,
733 .start
= WM831X_IRQ_CHG_BATT_FAIL
,
734 .end
= WM831X_IRQ_CHG_BATT_FAIL
,
735 .flags
= IORESOURCE_IRQ
,
739 .start
= WM831X_IRQ_CHG_OV
,
740 .end
= WM831X_IRQ_CHG_OV
,
741 .flags
= IORESOURCE_IRQ
,
745 .start
= WM831X_IRQ_CHG_END
,
746 .end
= WM831X_IRQ_CHG_END
,
747 .flags
= IORESOURCE_IRQ
,
751 .start
= WM831X_IRQ_CHG_TO
,
752 .end
= WM831X_IRQ_CHG_TO
,
753 .flags
= IORESOURCE_IRQ
,
757 .start
= WM831X_IRQ_CHG_MODE
,
758 .end
= WM831X_IRQ_CHG_MODE
,
759 .flags
= IORESOURCE_IRQ
,
763 .start
= WM831X_IRQ_CHG_START
,
764 .end
= WM831X_IRQ_CHG_START
,
765 .flags
= IORESOURCE_IRQ
,
769 static struct resource wm831x_rtc_resources
[] = {
772 .start
= WM831X_IRQ_RTC_PER
,
773 .end
= WM831X_IRQ_RTC_PER
,
774 .flags
= IORESOURCE_IRQ
,
778 .start
= WM831X_IRQ_RTC_ALM
,
779 .end
= WM831X_IRQ_RTC_ALM
,
780 .flags
= IORESOURCE_IRQ
,
784 static struct resource wm831x_status1_resources
[] = {
786 .start
= WM831X_STATUS_LED_1
,
787 .end
= WM831X_STATUS_LED_1
,
788 .flags
= IORESOURCE_IO
,
792 static struct resource wm831x_status2_resources
[] = {
794 .start
= WM831X_STATUS_LED_2
,
795 .end
= WM831X_STATUS_LED_2
,
796 .flags
= IORESOURCE_IO
,
800 static struct resource wm831x_touch_resources
[] = {
803 .start
= WM831X_IRQ_TCHPD
,
804 .end
= WM831X_IRQ_TCHPD
,
805 .flags
= IORESOURCE_IRQ
,
809 .start
= WM831X_IRQ_TCHDATA
,
810 .end
= WM831X_IRQ_TCHDATA
,
811 .flags
= IORESOURCE_IRQ
,
815 static struct resource wm831x_wdt_resources
[] = {
817 .start
= WM831X_IRQ_WDOG_TO
,
818 .end
= WM831X_IRQ_WDOG_TO
,
819 .flags
= IORESOURCE_IRQ
,
823 static struct mfd_cell wm8310_devs
[] = {
825 .name
= "wm831x-backup",
828 .name
= "wm831x-buckv",
830 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
831 .resources
= wm831x_dcdc1_resources
,
834 .name
= "wm831x-buckv",
836 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
837 .resources
= wm831x_dcdc2_resources
,
840 .name
= "wm831x-buckp",
842 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
843 .resources
= wm831x_dcdc3_resources
,
846 .name
= "wm831x-boostp",
848 .num_resources
= ARRAY_SIZE(wm831x_dcdc4_resources
),
849 .resources
= wm831x_dcdc4_resources
,
852 .name
= "wm831x-epe",
856 .name
= "wm831x-epe",
860 .name
= "wm831x-gpio",
861 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
862 .resources
= wm831x_gpio_resources
,
865 .name
= "wm831x-hwmon",
868 .name
= "wm831x-isink",
870 .num_resources
= ARRAY_SIZE(wm831x_isink1_resources
),
871 .resources
= wm831x_isink1_resources
,
874 .name
= "wm831x-isink",
876 .num_resources
= ARRAY_SIZE(wm831x_isink2_resources
),
877 .resources
= wm831x_isink2_resources
,
880 .name
= "wm831x-ldo",
882 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
883 .resources
= wm831x_ldo1_resources
,
886 .name
= "wm831x-ldo",
888 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
889 .resources
= wm831x_ldo2_resources
,
892 .name
= "wm831x-ldo",
894 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
895 .resources
= wm831x_ldo3_resources
,
898 .name
= "wm831x-ldo",
900 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
901 .resources
= wm831x_ldo4_resources
,
904 .name
= "wm831x-ldo",
906 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
907 .resources
= wm831x_ldo5_resources
,
910 .name
= "wm831x-ldo",
912 .num_resources
= ARRAY_SIZE(wm831x_ldo6_resources
),
913 .resources
= wm831x_ldo6_resources
,
916 .name
= "wm831x-aldo",
918 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
919 .resources
= wm831x_ldo7_resources
,
922 .name
= "wm831x-aldo",
924 .num_resources
= ARRAY_SIZE(wm831x_ldo8_resources
),
925 .resources
= wm831x_ldo8_resources
,
928 .name
= "wm831x-aldo",
930 .num_resources
= ARRAY_SIZE(wm831x_ldo9_resources
),
931 .resources
= wm831x_ldo9_resources
,
934 .name
= "wm831x-aldo",
936 .num_resources
= ARRAY_SIZE(wm831x_ldo10_resources
),
937 .resources
= wm831x_ldo10_resources
,
940 .name
= "wm831x-alive-ldo",
942 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
943 .resources
= wm831x_ldo11_resources
,
947 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
948 .resources
= wm831x_on_resources
,
951 .name
= "wm831x-power",
952 .num_resources
= ARRAY_SIZE(wm831x_power_resources
),
953 .resources
= wm831x_power_resources
,
956 .name
= "wm831x-rtc",
957 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
958 .resources
= wm831x_rtc_resources
,
961 .name
= "wm831x-status",
963 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
964 .resources
= wm831x_status1_resources
,
967 .name
= "wm831x-status",
969 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
970 .resources
= wm831x_status2_resources
,
973 .name
= "wm831x-watchdog",
974 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
975 .resources
= wm831x_wdt_resources
,
979 static struct mfd_cell wm8311_devs
[] = {
981 .name
= "wm831x-backup",
984 .name
= "wm831x-buckv",
986 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
987 .resources
= wm831x_dcdc1_resources
,
990 .name
= "wm831x-buckv",
992 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
993 .resources
= wm831x_dcdc2_resources
,
996 .name
= "wm831x-buckp",
998 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
999 .resources
= wm831x_dcdc3_resources
,
1002 .name
= "wm831x-boostp",
1004 .num_resources
= ARRAY_SIZE(wm831x_dcdc4_resources
),
1005 .resources
= wm831x_dcdc4_resources
,
1008 .name
= "wm831x-epe",
1012 .name
= "wm831x-epe",
1016 .name
= "wm831x-gpio",
1017 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
1018 .resources
= wm831x_gpio_resources
,
1021 .name
= "wm831x-hwmon",
1024 .name
= "wm831x-isink",
1026 .num_resources
= ARRAY_SIZE(wm831x_isink1_resources
),
1027 .resources
= wm831x_isink1_resources
,
1030 .name
= "wm831x-isink",
1032 .num_resources
= ARRAY_SIZE(wm831x_isink2_resources
),
1033 .resources
= wm831x_isink2_resources
,
1036 .name
= "wm831x-ldo",
1038 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
1039 .resources
= wm831x_ldo1_resources
,
1042 .name
= "wm831x-ldo",
1044 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
1045 .resources
= wm831x_ldo2_resources
,
1048 .name
= "wm831x-ldo",
1050 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
1051 .resources
= wm831x_ldo3_resources
,
1054 .name
= "wm831x-ldo",
1056 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
1057 .resources
= wm831x_ldo4_resources
,
1060 .name
= "wm831x-ldo",
1062 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
1063 .resources
= wm831x_ldo5_resources
,
1066 .name
= "wm831x-aldo",
1068 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
1069 .resources
= wm831x_ldo7_resources
,
1072 .name
= "wm831x-alive-ldo",
1074 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
1075 .resources
= wm831x_ldo11_resources
,
1078 .name
= "wm831x-on",
1079 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
1080 .resources
= wm831x_on_resources
,
1083 .name
= "wm831x-power",
1084 .num_resources
= ARRAY_SIZE(wm831x_power_resources
),
1085 .resources
= wm831x_power_resources
,
1088 .name
= "wm831x-rtc",
1089 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
1090 .resources
= wm831x_rtc_resources
,
1093 .name
= "wm831x-status",
1095 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
1096 .resources
= wm831x_status1_resources
,
1099 .name
= "wm831x-status",
1101 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
1102 .resources
= wm831x_status2_resources
,
1105 .name
= "wm831x-touch",
1106 .num_resources
= ARRAY_SIZE(wm831x_touch_resources
),
1107 .resources
= wm831x_touch_resources
,
1110 .name
= "wm831x-watchdog",
1111 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
1112 .resources
= wm831x_wdt_resources
,
1116 static struct mfd_cell wm8312_devs
[] = {
1118 .name
= "wm831x-backup",
1121 .name
= "wm831x-buckv",
1123 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
1124 .resources
= wm831x_dcdc1_resources
,
1127 .name
= "wm831x-buckv",
1129 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
1130 .resources
= wm831x_dcdc2_resources
,
1133 .name
= "wm831x-buckp",
1135 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
1136 .resources
= wm831x_dcdc3_resources
,
1139 .name
= "wm831x-boostp",
1141 .num_resources
= ARRAY_SIZE(wm831x_dcdc4_resources
),
1142 .resources
= wm831x_dcdc4_resources
,
1145 .name
= "wm831x-epe",
1149 .name
= "wm831x-epe",
1153 .name
= "wm831x-gpio",
1154 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
1155 .resources
= wm831x_gpio_resources
,
1158 .name
= "wm831x-hwmon",
1161 .name
= "wm831x-isink",
1163 .num_resources
= ARRAY_SIZE(wm831x_isink1_resources
),
1164 .resources
= wm831x_isink1_resources
,
1167 .name
= "wm831x-isink",
1169 .num_resources
= ARRAY_SIZE(wm831x_isink2_resources
),
1170 .resources
= wm831x_isink2_resources
,
1173 .name
= "wm831x-ldo",
1175 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
1176 .resources
= wm831x_ldo1_resources
,
1179 .name
= "wm831x-ldo",
1181 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
1182 .resources
= wm831x_ldo2_resources
,
1185 .name
= "wm831x-ldo",
1187 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
1188 .resources
= wm831x_ldo3_resources
,
1191 .name
= "wm831x-ldo",
1193 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
1194 .resources
= wm831x_ldo4_resources
,
1197 .name
= "wm831x-ldo",
1199 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
1200 .resources
= wm831x_ldo5_resources
,
1203 .name
= "wm831x-ldo",
1205 .num_resources
= ARRAY_SIZE(wm831x_ldo6_resources
),
1206 .resources
= wm831x_ldo6_resources
,
1209 .name
= "wm831x-aldo",
1211 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
1212 .resources
= wm831x_ldo7_resources
,
1215 .name
= "wm831x-aldo",
1217 .num_resources
= ARRAY_SIZE(wm831x_ldo8_resources
),
1218 .resources
= wm831x_ldo8_resources
,
1221 .name
= "wm831x-aldo",
1223 .num_resources
= ARRAY_SIZE(wm831x_ldo9_resources
),
1224 .resources
= wm831x_ldo9_resources
,
1227 .name
= "wm831x-aldo",
1229 .num_resources
= ARRAY_SIZE(wm831x_ldo10_resources
),
1230 .resources
= wm831x_ldo10_resources
,
1233 .name
= "wm831x-alive-ldo",
1235 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
1236 .resources
= wm831x_ldo11_resources
,
1239 .name
= "wm831x-on",
1240 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
1241 .resources
= wm831x_on_resources
,
1244 .name
= "wm831x-power",
1245 .num_resources
= ARRAY_SIZE(wm831x_power_resources
),
1246 .resources
= wm831x_power_resources
,
1249 .name
= "wm831x-rtc",
1250 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
1251 .resources
= wm831x_rtc_resources
,
1254 .name
= "wm831x-status",
1256 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
1257 .resources
= wm831x_status1_resources
,
1260 .name
= "wm831x-status",
1262 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
1263 .resources
= wm831x_status2_resources
,
1266 .name
= "wm831x-touch",
1267 .num_resources
= ARRAY_SIZE(wm831x_touch_resources
),
1268 .resources
= wm831x_touch_resources
,
1271 .name
= "wm831x-watchdog",
1272 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
1273 .resources
= wm831x_wdt_resources
,
1277 static struct mfd_cell wm8320_devs
[] = {
1279 .name
= "wm831x-backup",
1282 .name
= "wm831x-buckv",
1284 .num_resources
= ARRAY_SIZE(wm831x_dcdc1_resources
),
1285 .resources
= wm831x_dcdc1_resources
,
1288 .name
= "wm831x-buckv",
1290 .num_resources
= ARRAY_SIZE(wm831x_dcdc2_resources
),
1291 .resources
= wm831x_dcdc2_resources
,
1294 .name
= "wm831x-buckp",
1296 .num_resources
= ARRAY_SIZE(wm831x_dcdc3_resources
),
1297 .resources
= wm831x_dcdc3_resources
,
1300 .name
= "wm831x-buckp",
1302 .num_resources
= ARRAY_SIZE(wm8320_dcdc4_buck_resources
),
1303 .resources
= wm8320_dcdc4_buck_resources
,
1306 .name
= "wm831x-gpio",
1307 .num_resources
= ARRAY_SIZE(wm831x_gpio_resources
),
1308 .resources
= wm831x_gpio_resources
,
1311 .name
= "wm831x-hwmon",
1314 .name
= "wm831x-ldo",
1316 .num_resources
= ARRAY_SIZE(wm831x_ldo1_resources
),
1317 .resources
= wm831x_ldo1_resources
,
1320 .name
= "wm831x-ldo",
1322 .num_resources
= ARRAY_SIZE(wm831x_ldo2_resources
),
1323 .resources
= wm831x_ldo2_resources
,
1326 .name
= "wm831x-ldo",
1328 .num_resources
= ARRAY_SIZE(wm831x_ldo3_resources
),
1329 .resources
= wm831x_ldo3_resources
,
1332 .name
= "wm831x-ldo",
1334 .num_resources
= ARRAY_SIZE(wm831x_ldo4_resources
),
1335 .resources
= wm831x_ldo4_resources
,
1338 .name
= "wm831x-ldo",
1340 .num_resources
= ARRAY_SIZE(wm831x_ldo5_resources
),
1341 .resources
= wm831x_ldo5_resources
,
1344 .name
= "wm831x-ldo",
1346 .num_resources
= ARRAY_SIZE(wm831x_ldo6_resources
),
1347 .resources
= wm831x_ldo6_resources
,
1350 .name
= "wm831x-aldo",
1352 .num_resources
= ARRAY_SIZE(wm831x_ldo7_resources
),
1353 .resources
= wm831x_ldo7_resources
,
1356 .name
= "wm831x-aldo",
1358 .num_resources
= ARRAY_SIZE(wm831x_ldo8_resources
),
1359 .resources
= wm831x_ldo8_resources
,
1362 .name
= "wm831x-aldo",
1364 .num_resources
= ARRAY_SIZE(wm831x_ldo9_resources
),
1365 .resources
= wm831x_ldo9_resources
,
1368 .name
= "wm831x-aldo",
1370 .num_resources
= ARRAY_SIZE(wm831x_ldo10_resources
),
1371 .resources
= wm831x_ldo10_resources
,
1374 .name
= "wm831x-alive-ldo",
1376 .num_resources
= ARRAY_SIZE(wm831x_ldo11_resources
),
1377 .resources
= wm831x_ldo11_resources
,
1380 .name
= "wm831x-on",
1381 .num_resources
= ARRAY_SIZE(wm831x_on_resources
),
1382 .resources
= wm831x_on_resources
,
1385 .name
= "wm831x-rtc",
1386 .num_resources
= ARRAY_SIZE(wm831x_rtc_resources
),
1387 .resources
= wm831x_rtc_resources
,
1390 .name
= "wm831x-status",
1392 .num_resources
= ARRAY_SIZE(wm831x_status1_resources
),
1393 .resources
= wm831x_status1_resources
,
1396 .name
= "wm831x-status",
1398 .num_resources
= ARRAY_SIZE(wm831x_status2_resources
),
1399 .resources
= wm831x_status2_resources
,
1402 .name
= "wm831x-watchdog",
1403 .num_resources
= ARRAY_SIZE(wm831x_wdt_resources
),
1404 .resources
= wm831x_wdt_resources
,
1408 static struct mfd_cell backlight_devs
[] = {
1410 .name
= "wm831x-backlight",
1415 * Instantiate the generic non-control parts of the device.
1417 static int wm831x_device_init(struct wm831x
*wm831x
, unsigned long id
, int irq
)
1419 struct wm831x_pdata
*pdata
= wm831x
->dev
->platform_data
;
1421 enum wm831x_parent parent
;
1424 mutex_init(&wm831x
->io_lock
);
1425 mutex_init(&wm831x
->key_lock
);
1426 mutex_init(&wm831x
->auxadc_lock
);
1427 init_completion(&wm831x
->auxadc_done
);
1428 dev_set_drvdata(wm831x
->dev
, wm831x
);
1430 ret
= wm831x_reg_read(wm831x
, WM831X_PARENT_ID
);
1432 dev_err(wm831x
->dev
, "Failed to read parent ID: %d\n", ret
);
1435 if (ret
!= 0x6204) {
1436 dev_err(wm831x
->dev
, "Device is not a WM831x: ID %x\n", ret
);
1441 ret
= wm831x_reg_read(wm831x
, WM831X_REVISION
);
1443 dev_err(wm831x
->dev
, "Failed to read revision: %d\n", ret
);
1446 rev
= (ret
& WM831X_PARENT_REV_MASK
) >> WM831X_PARENT_REV_SHIFT
;
1448 ret
= wm831x_reg_read(wm831x
, WM831X_RESET_ID
);
1450 dev_err(wm831x
->dev
, "Failed to read device ID: %d\n", ret
);
1454 /* Some engineering samples do not have the ID set, rely on
1455 * the device being registered correctly.
1458 dev_info(wm831x
->dev
, "Device is an engineering sample\n");
1465 wm831x
->num_gpio
= 16;
1467 wm831x
->has_gpio_ena
= 1;
1468 wm831x
->has_cs_sts
= 1;
1471 dev_info(wm831x
->dev
, "WM8310 revision %c\n", 'A' + rev
);
1476 wm831x
->num_gpio
= 16;
1478 wm831x
->has_gpio_ena
= 1;
1479 wm831x
->has_cs_sts
= 1;
1482 dev_info(wm831x
->dev
, "WM8311 revision %c\n", 'A' + rev
);
1487 wm831x
->num_gpio
= 16;
1489 wm831x
->has_gpio_ena
= 1;
1490 wm831x
->has_cs_sts
= 1;
1493 dev_info(wm831x
->dev
, "WM8312 revision %c\n", 'A' + rev
);
1498 wm831x
->num_gpio
= 12;
1499 dev_info(wm831x
->dev
, "WM8320 revision %c\n", 'A' + rev
);
1503 dev_err(wm831x
->dev
, "Unknown WM831x device %04x\n", ret
);
1508 /* This will need revisiting in future but is OK for all
1512 dev_warn(wm831x
->dev
, "Device was registered as a WM%lx\n",
1515 /* Bootstrap the user key */
1516 ret
= wm831x_reg_read(wm831x
, WM831X_SECURITY_KEY
);
1518 dev_err(wm831x
->dev
, "Failed to read security key: %d\n", ret
);
1522 dev_warn(wm831x
->dev
, "Security key had non-zero value %x\n",
1524 wm831x_reg_write(wm831x
, WM831X_SECURITY_KEY
, 0);
1528 if (pdata
&& pdata
->pre_init
) {
1529 ret
= pdata
->pre_init(wm831x
);
1531 dev_err(wm831x
->dev
, "pre_init() failed: %d\n", ret
);
1536 ret
= wm831x_irq_init(wm831x
, irq
);
1540 if (wm831x
->irq_base
) {
1541 ret
= request_threaded_irq(wm831x
->irq_base
+
1542 WM831X_IRQ_AUXADC_DATA
,
1543 NULL
, wm831x_auxadc_irq
, 0,
1546 dev_err(wm831x
->dev
, "AUXADC IRQ request failed: %d\n",
1550 /* The core device is up, instantiate the subdevices. */
1553 ret
= mfd_add_devices(wm831x
->dev
, -1,
1554 wm8310_devs
, ARRAY_SIZE(wm8310_devs
),
1555 NULL
, wm831x
->irq_base
);
1559 ret
= mfd_add_devices(wm831x
->dev
, -1,
1560 wm8311_devs
, ARRAY_SIZE(wm8311_devs
),
1561 NULL
, wm831x
->irq_base
);
1565 ret
= mfd_add_devices(wm831x
->dev
, -1,
1566 wm8312_devs
, ARRAY_SIZE(wm8312_devs
),
1567 NULL
, wm831x
->irq_base
);
1571 ret
= mfd_add_devices(wm831x
->dev
, -1,
1572 wm8320_devs
, ARRAY_SIZE(wm8320_devs
),
1577 /* If this happens the bus probe function is buggy */
1582 dev_err(wm831x
->dev
, "Failed to add children\n");
1586 if (pdata
&& pdata
->backlight
) {
1587 /* Treat errors as non-critical */
1588 ret
= mfd_add_devices(wm831x
->dev
, -1, backlight_devs
,
1589 ARRAY_SIZE(backlight_devs
), NULL
,
1592 dev_err(wm831x
->dev
, "Failed to add backlight: %d\n",
1596 wm831x_otp_init(wm831x
);
1598 if (pdata
&& pdata
->post_init
) {
1599 ret
= pdata
->post_init(wm831x
);
1601 dev_err(wm831x
->dev
, "post_init() failed: %d\n", ret
);
1609 wm831x_irq_exit(wm831x
);
1611 mfd_remove_devices(wm831x
->dev
);
1616 static void wm831x_device_exit(struct wm831x
*wm831x
)
1618 wm831x_otp_exit(wm831x
);
1619 mfd_remove_devices(wm831x
->dev
);
1620 if (wm831x
->irq_base
)
1621 free_irq(wm831x
->irq_base
+ WM831X_IRQ_AUXADC_DATA
, wm831x
);
1622 wm831x_irq_exit(wm831x
);
1626 static int wm831x_i2c_read_device(struct wm831x
*wm831x
, unsigned short reg
,
1627 int bytes
, void *dest
)
1629 struct i2c_client
*i2c
= wm831x
->control_data
;
1631 u16 r
= cpu_to_be16(reg
);
1633 ret
= i2c_master_send(i2c
, (unsigned char *)&r
, 2);
1639 ret
= i2c_master_recv(i2c
, dest
, bytes
);
1647 /* Currently we allocate the write buffer on the stack; this is OK for
1648 * small writes - if we need to do large writes this will need to be
1651 static int wm831x_i2c_write_device(struct wm831x
*wm831x
, unsigned short reg
,
1652 int bytes
, void *src
)
1654 struct i2c_client
*i2c
= wm831x
->control_data
;
1655 unsigned char msg
[bytes
+ 2];
1658 reg
= cpu_to_be16(reg
);
1659 memcpy(&msg
[0], ®
, 2);
1660 memcpy(&msg
[2], src
, bytes
);
1662 ret
= i2c_master_send(i2c
, msg
, bytes
+ 2);
1665 if (ret
< bytes
+ 2)
1671 static int wm831x_i2c_probe(struct i2c_client
*i2c
,
1672 const struct i2c_device_id
*id
)
1674 struct wm831x
*wm831x
;
1676 wm831x
= kzalloc(sizeof(struct wm831x
), GFP_KERNEL
);
1677 if (wm831x
== NULL
) {
1682 i2c_set_clientdata(i2c
, wm831x
);
1683 wm831x
->dev
= &i2c
->dev
;
1684 wm831x
->control_data
= i2c
;
1685 wm831x
->read_dev
= wm831x_i2c_read_device
;
1686 wm831x
->write_dev
= wm831x_i2c_write_device
;
1688 return wm831x_device_init(wm831x
, id
->driver_data
, i2c
->irq
);
1691 static int wm831x_i2c_remove(struct i2c_client
*i2c
)
1693 struct wm831x
*wm831x
= i2c_get_clientdata(i2c
);
1695 wm831x_device_exit(wm831x
);
1700 static const struct i2c_device_id wm831x_i2c_id
[] = {
1701 { "wm8310", WM8310
},
1702 { "wm8311", WM8311
},
1703 { "wm8312", WM8312
},
1704 { "wm8320", WM8320
},
1707 MODULE_DEVICE_TABLE(i2c
, wm831x_i2c_id
);
1710 static struct i2c_driver wm831x_i2c_driver
= {
1713 .owner
= THIS_MODULE
,
1715 .probe
= wm831x_i2c_probe
,
1716 .remove
= wm831x_i2c_remove
,
1717 .id_table
= wm831x_i2c_id
,
1720 static int __init
wm831x_i2c_init(void)
1724 ret
= i2c_add_driver(&wm831x_i2c_driver
);
1726 pr_err("Failed to register wm831x I2C driver: %d\n", ret
);
1730 subsys_initcall(wm831x_i2c_init
);
1732 static void __exit
wm831x_i2c_exit(void)
1734 i2c_del_driver(&wm831x_i2c_driver
);
1736 module_exit(wm831x_i2c_exit
);
1738 MODULE_DESCRIPTION("I2C support for the WM831X AudioPlus PMIC");
1739 MODULE_LICENSE("GPL");
1740 MODULE_AUTHOR("Mark Brown");