389144bf88253afcc2a9b9eeca6c42e87dece1bc
[platform/kernel/u-boot.git] / arch / arm / cpu / armv7 / sunxi / pmic_bus.c
1 /*
2  * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
3  *
4  * Sunxi PMIC bus access helpers
5  *
6  * The axp152 & axp209 use an i2c bus, the axp221 uses the p2wi bus and the
7  * axp223 uses the rsb bus, these functions abstract this.
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <common.h>
13 #include <asm/arch/p2wi.h>
14 #include <asm/arch/rsb.h>
15 #include <asm/arch/pmic_bus.h>
16
17 #define AXP221_CHIP_ADDR                0x68
18 #define AXP221_CTRL_ADDR                0x3e
19 #define AXP221_INIT_DATA                0x3e
20
21 #define AXP223_DEVICE_ADDR              0x3a3
22 #define AXP223_RUNTIME_ADDR             0x2d
23
24 int pmic_bus_init(void)
25 {
26         /* This cannot be 0 because it is used in SPL before BSS is ready */
27         static int needs_init = 1;
28         int ret;
29
30         if (!needs_init)
31                 return 0;
32
33 #ifdef CONFIG_MACH_SUN6I
34         p2wi_init();
35         ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
36                                        AXP221_INIT_DATA);
37 #else
38         ret = rsb_init();
39         if (ret)
40                 return ret;
41
42         ret = rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
43 #endif
44         if (ret)
45                 return ret;
46
47         needs_init = 0;
48         return 0;
49 }
50
51 int pmic_bus_read(u8 reg, u8 *data)
52 {
53 #ifdef CONFIG_MACH_SUN6I
54         return p2wi_read(reg, data);
55 #else
56         return rsb_read(AXP223_RUNTIME_ADDR, reg, data);
57 #endif
58 }
59
60 int pmic_bus_write(u8 reg, u8 data)
61 {
62 #ifdef CONFIG_MACH_SUN6I
63         return p2wi_write(reg, data);
64 #else
65         return rsb_write(AXP223_RUNTIME_ADDR, reg, data);
66 #endif
67 }
68
69 int pmic_bus_setbits(u8 reg, u8 bits)
70 {
71         int ret;
72         u8 val;
73
74         ret = pmic_bus_read(reg, &val);
75         if (ret)
76                 return ret;
77
78         val |= bits;
79         return pmic_bus_write(reg, val);
80 }
81
82 int pmic_bus_clrbits(u8 reg, u8 bits)
83 {
84         int ret;
85         u8 val;
86
87         ret = pmic_bus_read(reg, &val);
88         if (ret)
89                 return ret;
90
91         val &= ~bits;
92         return pmic_bus_write(reg, val);
93 }