2 * AXP221 and AXP223 driver
4 * IMPORTANT when making changes to this file check that the registers
5 * used are the same for the axp221 and axp223.
7 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
8 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
10 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/arch/p2wi.h>
16 #include <asm/arch/rsb.h>
20 * The axp221 uses the p2wi bus, the axp223 is identical (for all registers
21 * used sofar) but uses the rsb bus. These functions abstract this.
23 static int pmic_bus_init(void)
25 #ifdef CONFIG_MACH_SUN6I
27 return p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
34 ret = rsb_set_device_mode(AXP223_DEVICE_MODE_DATA);
38 return rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
42 static int pmic_bus_read(const u8 addr, u8 *data)
44 #ifdef CONFIG_MACH_SUN6I
45 return p2wi_read(addr, data);
47 return rsb_read(AXP223_RUNTIME_ADDR, addr, data);
51 static int pmic_bus_write(const u8 addr, u8 data)
53 #ifdef CONFIG_MACH_SUN6I
54 return p2wi_write(addr, data);
56 return rsb_write(AXP223_RUNTIME_ADDR, addr, data);
60 static u8 axp221_mvolt_to_cfg(int mvolt, int min, int max, int div)
67 return (mvolt - min) / div;
70 static int axp221_setbits(u8 reg, u8 bits)
75 ret = pmic_bus_read(reg, &val);
80 return pmic_bus_write(reg, val);
83 int axp221_set_dcdc1(unsigned int mvolt)
86 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
88 ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
92 return axp221_setbits(AXP221_OUTPUT_CTRL2,
93 AXP221_OUTPUT_CTRL2_DCDC1_EN);
96 int axp221_set_dcdc2(unsigned int mvolt)
98 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
100 return pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
103 int axp221_set_dcdc3(unsigned int mvolt)
105 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
107 return pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
110 int axp221_set_dcdc4(unsigned int mvolt)
112 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
114 return pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
117 int axp221_set_dcdc5(unsigned int mvolt)
119 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
121 return pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
124 int axp221_set_dldo1(unsigned int mvolt)
127 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
129 ret = pmic_bus_write(AXP221_DLDO1_CTRL, cfg);
133 return axp221_setbits(AXP221_OUTPUT_CTRL2,
134 AXP221_OUTPUT_CTRL2_DLDO1_EN);
137 int axp221_set_dldo2(unsigned int mvolt)
140 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
142 ret = pmic_bus_write(AXP221_DLDO2_CTRL, cfg);
146 return axp221_setbits(AXP221_OUTPUT_CTRL2,
147 AXP221_OUTPUT_CTRL2_DLDO2_EN);
150 int axp221_set_dldo3(unsigned int mvolt)
153 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
155 ret = pmic_bus_write(AXP221_DLDO3_CTRL, cfg);
159 return axp221_setbits(AXP221_OUTPUT_CTRL2,
160 AXP221_OUTPUT_CTRL2_DLDO3_EN);
163 int axp221_set_dldo4(unsigned int mvolt)
166 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
168 ret = pmic_bus_write(AXP221_DLDO4_CTRL, cfg);
172 return axp221_setbits(AXP221_OUTPUT_CTRL2,
173 AXP221_OUTPUT_CTRL2_DLDO4_EN);
176 int axp221_set_aldo1(unsigned int mvolt)
179 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
181 ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
185 return axp221_setbits(AXP221_OUTPUT_CTRL1,
186 AXP221_OUTPUT_CTRL1_ALDO1_EN);
189 int axp221_set_aldo2(unsigned int mvolt)
192 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
194 ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
198 return axp221_setbits(AXP221_OUTPUT_CTRL1,
199 AXP221_OUTPUT_CTRL1_ALDO2_EN);
202 int axp221_set_aldo3(unsigned int mvolt)
205 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
207 ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
211 return axp221_setbits(AXP221_OUTPUT_CTRL3,
212 AXP221_OUTPUT_CTRL3_ALDO3_EN);
215 int axp221_init(void)
220 ret = pmic_bus_init();
224 ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
228 if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
234 int axp221_get_sid(unsigned int *sid)
236 u8 *dest = (u8 *)sid;
243 ret = pmic_bus_write(AXP221_PAGE, 1);
247 for (i = 0; i < 16; i++) {
248 ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
253 pmic_bus_write(AXP221_PAGE, 0);
255 for (i = 0; i < 4; i++)
256 sid[i] = be32_to_cpu(sid[i]);