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 static int axp221_clrbits(u8 reg, u8 bits)
88 ret = pmic_bus_read(reg, &val);
93 return pmic_bus_write(reg, val);
96 int axp221_set_dcdc1(unsigned int mvolt)
99 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
102 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
103 AXP221_OUTPUT_CTRL1_DCDC1_EN);
105 ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
109 ret = axp221_setbits(AXP221_OUTPUT_CTRL2,
110 AXP221_OUTPUT_CTRL2_DCDC1SW_EN);
114 return axp221_setbits(AXP221_OUTPUT_CTRL1,
115 AXP221_OUTPUT_CTRL1_DCDC1_EN);
118 int axp221_set_dcdc2(unsigned int mvolt)
121 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
124 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
125 AXP221_OUTPUT_CTRL1_DCDC2_EN);
127 ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
131 return axp221_setbits(AXP221_OUTPUT_CTRL1,
132 AXP221_OUTPUT_CTRL1_DCDC2_EN);
135 int axp221_set_dcdc3(unsigned int mvolt)
138 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
141 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
142 AXP221_OUTPUT_CTRL1_DCDC3_EN);
144 ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
148 return axp221_setbits(AXP221_OUTPUT_CTRL1,
149 AXP221_OUTPUT_CTRL1_DCDC3_EN);
152 int axp221_set_dcdc4(unsigned int mvolt)
155 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
158 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
159 AXP221_OUTPUT_CTRL1_DCDC4_EN);
161 ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
165 return axp221_setbits(AXP221_OUTPUT_CTRL1,
166 AXP221_OUTPUT_CTRL1_DCDC4_EN);
169 int axp221_set_dcdc5(unsigned int mvolt)
172 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
175 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
176 AXP221_OUTPUT_CTRL1_DCDC5_EN);
178 ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
182 return axp221_setbits(AXP221_OUTPUT_CTRL1,
183 AXP221_OUTPUT_CTRL1_DCDC5_EN);
186 int axp221_set_dldo1(unsigned int mvolt)
189 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
192 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
193 AXP221_OUTPUT_CTRL2_DLDO1_EN);
195 ret = pmic_bus_write(AXP221_DLDO1_CTRL, cfg);
199 return axp221_setbits(AXP221_OUTPUT_CTRL2,
200 AXP221_OUTPUT_CTRL2_DLDO1_EN);
203 int axp221_set_dldo2(unsigned int mvolt)
206 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
209 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
210 AXP221_OUTPUT_CTRL2_DLDO2_EN);
212 ret = pmic_bus_write(AXP221_DLDO2_CTRL, cfg);
216 return axp221_setbits(AXP221_OUTPUT_CTRL2,
217 AXP221_OUTPUT_CTRL2_DLDO2_EN);
220 int axp221_set_dldo3(unsigned int mvolt)
223 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
226 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
227 AXP221_OUTPUT_CTRL2_DLDO3_EN);
229 ret = pmic_bus_write(AXP221_DLDO3_CTRL, cfg);
233 return axp221_setbits(AXP221_OUTPUT_CTRL2,
234 AXP221_OUTPUT_CTRL2_DLDO3_EN);
237 int axp221_set_dldo4(unsigned int mvolt)
240 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
243 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
244 AXP221_OUTPUT_CTRL2_DLDO4_EN);
246 ret = pmic_bus_write(AXP221_DLDO4_CTRL, cfg);
250 return axp221_setbits(AXP221_OUTPUT_CTRL2,
251 AXP221_OUTPUT_CTRL2_DLDO4_EN);
254 int axp221_set_aldo1(unsigned int mvolt)
257 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
260 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
261 AXP221_OUTPUT_CTRL1_ALDO1_EN);
263 ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
267 return axp221_setbits(AXP221_OUTPUT_CTRL1,
268 AXP221_OUTPUT_CTRL1_ALDO1_EN);
271 int axp221_set_aldo2(unsigned int mvolt)
274 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
277 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
278 AXP221_OUTPUT_CTRL1_ALDO2_EN);
280 ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
284 return axp221_setbits(AXP221_OUTPUT_CTRL1,
285 AXP221_OUTPUT_CTRL1_ALDO2_EN);
288 int axp221_set_aldo3(unsigned int mvolt)
291 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
294 return axp221_clrbits(AXP221_OUTPUT_CTRL3,
295 AXP221_OUTPUT_CTRL3_ALDO3_EN);
297 ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
301 return axp221_setbits(AXP221_OUTPUT_CTRL3,
302 AXP221_OUTPUT_CTRL3_ALDO3_EN);
305 int axp221_init(void)
307 /* This cannot be 0 because it is used in SPL before BSS is ready */
308 static int needs_init = 1;
315 ret = pmic_bus_init();
319 ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
323 if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
330 int axp221_get_sid(unsigned int *sid)
332 u8 *dest = (u8 *)sid;
339 ret = pmic_bus_write(AXP221_PAGE, 1);
343 for (i = 0; i < 16; i++) {
344 ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
349 pmic_bus_write(AXP221_PAGE, 0);
351 for (i = 0; i < 4; i++)
352 sid[i] = be32_to_cpu(sid[i]);
357 static int axp_drivebus_setup(void)
365 /* Set N_VBUSEN pin to output / DRIVEBUS function */
366 return axp221_clrbits(AXP221_MISC_CTRL, AXP221_MISC_CTRL_N_VBUSEN_FUNC);
369 int axp_drivebus_enable(void)
373 ret = axp_drivebus_setup();
377 /* Set DRIVEBUS high */
378 return axp221_setbits(AXP221_VBUS_IPSOUT, AXP221_VBUS_IPSOUT_DRIVEBUS);
381 int axp_drivebus_disable(void)
385 ret = axp_drivebus_setup();
389 /* Set DRIVEBUS low */
390 return axp221_clrbits(AXP221_VBUS_IPSOUT, AXP221_VBUS_IPSOUT_DRIVEBUS);