2 * AXP818 driver based on AXP221 driver
5 * (C) Copyright 2015 Vishnu Patekar <vishnuptekar0510@gmail.com>
8 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
9 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
11 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/gpio.h>
17 #include <asm/arch/pmic_bus.h>
20 static u8 axp818_mvolt_to_cfg(int mvolt, int min, int max, int div)
27 return (mvolt - min) / div;
30 int axp_set_dcdc1(unsigned int mvolt)
33 u8 cfg = axp818_mvolt_to_cfg(mvolt, 1600, 3400, 100);
36 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
37 AXP818_OUTPUT_CTRL1_DCDC1_EN);
39 ret = pmic_bus_write(AXP818_DCDC1_CTRL, cfg);
43 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
44 AXP818_OUTPUT_CTRL1_DCDC1_EN);
47 int axp_set_dcdc2(unsigned int mvolt)
53 cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20);
55 cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10);
58 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
59 AXP818_OUTPUT_CTRL1_DCDC2_EN);
61 ret = pmic_bus_write(AXP818_DCDC2_CTRL, cfg);
65 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
66 AXP818_OUTPUT_CTRL1_DCDC2_EN);
69 int axp_set_dcdc3(unsigned int mvolt)
75 cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20);
77 cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10);
80 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
81 AXP818_OUTPUT_CTRL1_DCDC3_EN);
83 ret = pmic_bus_write(AXP818_DCDC3_CTRL, cfg);
87 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
88 AXP818_OUTPUT_CTRL1_DCDC3_EN);
91 int axp_set_dcdc5(unsigned int mvolt)
97 cfg = 32 + axp818_mvolt_to_cfg(mvolt, 1140, 1840, 20);
99 cfg = axp818_mvolt_to_cfg(mvolt, 800, 1120, 10);
102 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
103 AXP818_OUTPUT_CTRL1_DCDC5_EN);
105 ret = pmic_bus_write(AXP818_DCDC5_CTRL, cfg);
109 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
110 AXP818_OUTPUT_CTRL1_DCDC5_EN);
113 int axp_set_aldo(int aldo_num, unsigned int mvolt)
118 if (aldo_num < 1 || aldo_num > 3)
122 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3,
123 AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1));
125 cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100);
126 ret = pmic_bus_write(AXP818_ALDO1_CTRL + (aldo_num - 1), cfg);
130 return pmic_bus_setbits(AXP818_OUTPUT_CTRL3,
131 AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1));
134 /* TODO: re-work other AXP drivers to consolidate ALDO functions. */
135 int axp_set_aldo1(unsigned int mvolt)
137 return axp_set_aldo(1, mvolt);
140 int axp_set_aldo2(unsigned int mvolt)
142 return axp_set_aldo(2, mvolt);
145 int axp_set_aldo3(unsigned int mvolt)
147 return axp_set_aldo(3, mvolt);
150 int axp_set_dldo(int dldo_num, unsigned int mvolt)
155 if (dldo_num < 1 || dldo_num > 4)
159 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
160 AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
162 cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100);
163 if (dldo_num == 2 && mvolt > 3300)
164 cfg += 1 + axp818_mvolt_to_cfg(mvolt, 3400, 4200, 200);
165 ret = pmic_bus_write(AXP818_ELDO1_CTRL + (dldo_num - 1), cfg);
169 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
170 AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
173 int axp_set_eldo(int eldo_num, unsigned int mvolt)
178 if (eldo_num < 1 || eldo_num > 3)
182 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
183 AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
185 cfg = axp818_mvolt_to_cfg(mvolt, 700, 1900, 50);
186 ret = pmic_bus_write(AXP818_ELDO1_CTRL + (eldo_num - 1), cfg);
190 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
191 AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
194 int axp_set_fldo(int fldo_num, unsigned int mvolt)
199 if (fldo_num < 1 || fldo_num > 3)
203 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3,
204 AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1));
207 cfg = axp818_mvolt_to_cfg(mvolt, 700, 1450, 50);
208 ret = pmic_bus_write(AXP818_FLDO1_CTRL + (fldo_num - 1), cfg);
211 * Special case for FLDO3, which is DCDC5 / 2 or FLDOIN / 2
212 * Since FLDOIN is unknown, test against DCDC5.
214 if (mvolt * 2 == CONFIG_AXP_DCDC5_VOLT)
215 ret = pmic_bus_clrbits(AXP818_FLDO2_3_CTRL,
216 AXP818_FLDO2_3_CTRL_FLDO3_VOL);
218 ret = pmic_bus_setbits(AXP818_FLDO2_3_CTRL,
219 AXP818_FLDO2_3_CTRL_FLDO3_VOL);
224 return pmic_bus_setbits(AXP818_OUTPUT_CTRL3,
225 AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1));
228 int axp_set_sw(bool on)
231 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
232 AXP818_OUTPUT_CTRL2_SW_EN);
234 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
235 AXP818_OUTPUT_CTRL2_SW_EN);
243 ret = pmic_bus_init();
247 ret = pmic_bus_read(AXP818_CHIP_ID, &axp_chip_id);
251 if (!(axp_chip_id == 0x51))
259 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
261 pmic_bus_write(AXP818_SHUTDOWN, AXP818_SHUTDOWN_POWEROFF);
263 /* infinite loop during shutdown */