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>
17 #include <asm/arch/gpio.h>
21 * The axp221 uses the p2wi bus, the axp223 is identical (for all registers
22 * used sofar) but uses the rsb bus. These functions abstract this.
24 static int pmic_bus_init(void)
26 #ifdef CONFIG_MACH_SUN6I
28 return p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
37 return rsb_set_device_address(AXP223_DEVICE_ADDR, AXP223_RUNTIME_ADDR);
41 static int pmic_bus_read(const u8 addr, u8 *data)
43 #ifdef CONFIG_MACH_SUN6I
44 return p2wi_read(addr, data);
46 return rsb_read(AXP223_RUNTIME_ADDR, addr, data);
50 static int pmic_bus_write(const u8 addr, u8 data)
52 #ifdef CONFIG_MACH_SUN6I
53 return p2wi_write(addr, data);
55 return rsb_write(AXP223_RUNTIME_ADDR, addr, data);
59 static u8 axp221_mvolt_to_cfg(int mvolt, int min, int max, int div)
66 return (mvolt - min) / div;
69 static int axp221_setbits(u8 reg, u8 bits)
74 ret = pmic_bus_read(reg, &val);
79 return pmic_bus_write(reg, val);
82 static int axp221_clrbits(u8 reg, u8 bits)
87 ret = pmic_bus_read(reg, &val);
92 return pmic_bus_write(reg, val);
95 int axp221_set_dcdc1(unsigned int mvolt)
98 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100);
101 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
102 AXP221_OUTPUT_CTRL1_DCDC1_EN);
104 ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg);
108 ret = axp221_setbits(AXP221_OUTPUT_CTRL2,
109 AXP221_OUTPUT_CTRL2_DCDC1SW_EN);
113 return axp221_setbits(AXP221_OUTPUT_CTRL1,
114 AXP221_OUTPUT_CTRL1_DCDC1_EN);
117 int axp221_set_dcdc2(unsigned int mvolt)
120 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
123 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
124 AXP221_OUTPUT_CTRL1_DCDC2_EN);
126 ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg);
130 return axp221_setbits(AXP221_OUTPUT_CTRL1,
131 AXP221_OUTPUT_CTRL1_DCDC2_EN);
134 int axp221_set_dcdc3(unsigned int mvolt)
137 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20);
140 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
141 AXP221_OUTPUT_CTRL1_DCDC3_EN);
143 ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg);
147 return axp221_setbits(AXP221_OUTPUT_CTRL1,
148 AXP221_OUTPUT_CTRL1_DCDC3_EN);
151 int axp221_set_dcdc4(unsigned int mvolt)
154 u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20);
157 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
158 AXP221_OUTPUT_CTRL1_DCDC4_EN);
160 ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg);
164 return axp221_setbits(AXP221_OUTPUT_CTRL1,
165 AXP221_OUTPUT_CTRL1_DCDC4_EN);
168 int axp221_set_dcdc5(unsigned int mvolt)
171 u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50);
174 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
175 AXP221_OUTPUT_CTRL1_DCDC5_EN);
177 ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg);
181 return axp221_setbits(AXP221_OUTPUT_CTRL1,
182 AXP221_OUTPUT_CTRL1_DCDC5_EN);
185 int axp221_set_dldo1(unsigned int mvolt)
188 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
191 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
192 AXP221_OUTPUT_CTRL2_DLDO1_EN);
194 ret = pmic_bus_write(AXP221_DLDO1_CTRL, cfg);
198 return axp221_setbits(AXP221_OUTPUT_CTRL2,
199 AXP221_OUTPUT_CTRL2_DLDO1_EN);
202 int axp221_set_dldo2(unsigned int mvolt)
205 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
208 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
209 AXP221_OUTPUT_CTRL2_DLDO2_EN);
211 ret = pmic_bus_write(AXP221_DLDO2_CTRL, cfg);
215 return axp221_setbits(AXP221_OUTPUT_CTRL2,
216 AXP221_OUTPUT_CTRL2_DLDO2_EN);
219 int axp221_set_dldo3(unsigned int mvolt)
222 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
225 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
226 AXP221_OUTPUT_CTRL2_DLDO3_EN);
228 ret = pmic_bus_write(AXP221_DLDO3_CTRL, cfg);
232 return axp221_setbits(AXP221_OUTPUT_CTRL2,
233 AXP221_OUTPUT_CTRL2_DLDO3_EN);
236 int axp221_set_dldo4(unsigned int mvolt)
239 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
242 return axp221_clrbits(AXP221_OUTPUT_CTRL2,
243 AXP221_OUTPUT_CTRL2_DLDO4_EN);
245 ret = pmic_bus_write(AXP221_DLDO4_CTRL, cfg);
249 return axp221_setbits(AXP221_OUTPUT_CTRL2,
250 AXP221_OUTPUT_CTRL2_DLDO4_EN);
253 int axp221_set_aldo1(unsigned int mvolt)
256 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
259 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
260 AXP221_OUTPUT_CTRL1_ALDO1_EN);
262 ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg);
266 return axp221_setbits(AXP221_OUTPUT_CTRL1,
267 AXP221_OUTPUT_CTRL1_ALDO1_EN);
270 int axp221_set_aldo2(unsigned int mvolt)
273 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
276 return axp221_clrbits(AXP221_OUTPUT_CTRL1,
277 AXP221_OUTPUT_CTRL1_ALDO2_EN);
279 ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg);
283 return axp221_setbits(AXP221_OUTPUT_CTRL1,
284 AXP221_OUTPUT_CTRL1_ALDO2_EN);
287 int axp221_set_aldo3(unsigned int mvolt)
290 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
293 return axp221_clrbits(AXP221_OUTPUT_CTRL3,
294 AXP221_OUTPUT_CTRL3_ALDO3_EN);
296 ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg);
300 return axp221_setbits(AXP221_OUTPUT_CTRL3,
301 AXP221_OUTPUT_CTRL3_ALDO3_EN);
304 int axp221_set_eldo(int eldo_num, unsigned int mvolt)
307 u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100);
312 addr = AXP221_ELDO3_CTRL;
313 bits = AXP221_OUTPUT_CTRL2_ELDO3_EN;
316 addr = AXP221_ELDO2_CTRL;
317 bits = AXP221_OUTPUT_CTRL2_ELDO2_EN;
320 addr = AXP221_ELDO1_CTRL;
321 bits = AXP221_OUTPUT_CTRL2_ELDO1_EN;
328 return axp221_clrbits(AXP221_OUTPUT_CTRL2, bits);
330 ret = pmic_bus_write(addr, cfg);
334 return axp221_setbits(AXP221_OUTPUT_CTRL2, bits);
337 int axp221_init(void)
339 /* This cannot be 0 because it is used in SPL before BSS is ready */
340 static int needs_init = 1;
347 ret = pmic_bus_init();
351 ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id);
355 if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17))
362 int axp221_get_sid(unsigned int *sid)
364 u8 *dest = (u8 *)sid;
371 ret = pmic_bus_write(AXP221_PAGE, 1);
375 for (i = 0; i < 16; i++) {
376 ret = pmic_bus_read(AXP221_SID + i, &dest[i]);
381 pmic_bus_write(AXP221_PAGE, 0);
383 for (i = 0; i < 4; i++)
384 sid[i] = be32_to_cpu(sid[i]);
389 int axp_gpio_direction_input(struct udevice *dev, unsigned pin)
392 case SUNXI_GPIO_AXP0_VBUS_DETECT:
399 int axp_gpio_direction_output(struct udevice *dev, unsigned pin, int val)
404 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
405 ret = axp221_clrbits(AXP221_MISC_CTRL,
406 AXP221_MISC_CTRL_N_VBUSEN_FUNC);
410 return axp_gpio_set_value(dev, pin, val);
416 int axp_gpio_get_value(struct udevice *dev, unsigned pin)
422 case SUNXI_GPIO_AXP0_VBUS_DETECT:
423 ret = pmic_bus_read(AXP221_POWER_STATUS, &val);
427 return !!(val & AXP221_POWER_STATUS_VBUS_AVAIL);
433 int axp_gpio_set_value(struct udevice *dev, unsigned pin, int val)
438 case SUNXI_GPIO_AXP0_VBUS_ENABLE:
440 ret = axp221_setbits(AXP221_VBUS_IPSOUT,
441 AXP221_VBUS_IPSOUT_DRIVEBUS);
443 ret = axp221_clrbits(AXP221_VBUS_IPSOUT,
444 AXP221_VBUS_IPSOUT_DRIVEBUS);