1 // SPDX-License-Identifier: GPL-2.0+
3 * AXP818 driver based on AXP221 driver
6 * (C) Copyright 2015 Vishnu Patekar <vishnuptekar0510@gmail.com>
9 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
10 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
15 #include <asm/arch/gpio.h>
16 #include <asm/arch/pmic_bus.h>
19 static u8 axp818_mvolt_to_cfg(int mvolt, int min, int max, int div)
26 return (mvolt - min) / div;
29 int axp_set_dcdc1(unsigned int mvolt)
32 u8 cfg = axp818_mvolt_to_cfg(mvolt, 1600, 3400, 100);
35 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
36 AXP818_OUTPUT_CTRL1_DCDC1_EN);
38 ret = pmic_bus_write(AXP818_DCDC1_CTRL, cfg);
42 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
43 AXP818_OUTPUT_CTRL1_DCDC1_EN);
46 int axp_set_dcdc2(unsigned int mvolt)
52 cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20);
54 cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10);
57 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
58 AXP818_OUTPUT_CTRL1_DCDC2_EN);
60 ret = pmic_bus_write(AXP818_DCDC2_CTRL, cfg);
64 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
65 AXP818_OUTPUT_CTRL1_DCDC2_EN);
68 int axp_set_dcdc3(unsigned int mvolt)
74 cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20);
76 cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10);
79 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
80 AXP818_OUTPUT_CTRL1_DCDC3_EN);
82 ret = pmic_bus_write(AXP818_DCDC3_CTRL, cfg);
86 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
87 AXP818_OUTPUT_CTRL1_DCDC3_EN);
90 int axp_set_dcdc5(unsigned int mvolt)
96 cfg = 32 + axp818_mvolt_to_cfg(mvolt, 1140, 1840, 20);
98 cfg = axp818_mvolt_to_cfg(mvolt, 800, 1120, 10);
101 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1,
102 AXP818_OUTPUT_CTRL1_DCDC5_EN);
104 ret = pmic_bus_write(AXP818_DCDC5_CTRL, cfg);
108 return pmic_bus_setbits(AXP818_OUTPUT_CTRL1,
109 AXP818_OUTPUT_CTRL1_DCDC5_EN);
112 int axp_set_aldo(int aldo_num, unsigned int mvolt)
117 if (aldo_num < 1 || aldo_num > 3)
121 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3,
122 AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1));
124 cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100);
125 ret = pmic_bus_write(AXP818_ALDO1_CTRL + (aldo_num - 1), cfg);
129 return pmic_bus_setbits(AXP818_OUTPUT_CTRL3,
130 AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1));
133 /* TODO: re-work other AXP drivers to consolidate ALDO functions. */
134 int axp_set_aldo1(unsigned int mvolt)
136 return axp_set_aldo(1, mvolt);
139 int axp_set_aldo2(unsigned int mvolt)
141 return axp_set_aldo(2, mvolt);
144 int axp_set_aldo3(unsigned int mvolt)
146 return axp_set_aldo(3, mvolt);
149 int axp_set_dldo(int dldo_num, unsigned int mvolt)
154 if (dldo_num < 1 || dldo_num > 4)
158 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
159 AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
161 cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100);
162 if (dldo_num == 2 && mvolt > 3300)
163 cfg += 1 + axp818_mvolt_to_cfg(mvolt, 3400, 4200, 200);
164 ret = pmic_bus_write(AXP818_ELDO1_CTRL + (dldo_num - 1), cfg);
168 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
169 AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1));
172 int axp_set_eldo(int eldo_num, unsigned int mvolt)
177 if (eldo_num < 1 || eldo_num > 3)
181 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
182 AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
184 cfg = axp818_mvolt_to_cfg(mvolt, 700, 1900, 50);
185 ret = pmic_bus_write(AXP818_ELDO1_CTRL + (eldo_num - 1), cfg);
189 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
190 AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1));
193 int axp_set_fldo(int fldo_num, unsigned int mvolt)
198 if (fldo_num < 1 || fldo_num > 3)
202 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3,
203 AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1));
206 cfg = axp818_mvolt_to_cfg(mvolt, 700, 1450, 50);
207 ret = pmic_bus_write(AXP818_FLDO1_CTRL + (fldo_num - 1), cfg);
210 * Special case for FLDO3, which is DCDC5 / 2 or FLDOIN / 2
211 * Since FLDOIN is unknown, test against DCDC5.
213 if (mvolt * 2 == CONFIG_AXP_DCDC5_VOLT)
214 ret = pmic_bus_clrbits(AXP818_FLDO2_3_CTRL,
215 AXP818_FLDO2_3_CTRL_FLDO3_VOL);
217 ret = pmic_bus_setbits(AXP818_FLDO2_3_CTRL,
218 AXP818_FLDO2_3_CTRL_FLDO3_VOL);
223 return pmic_bus_setbits(AXP818_OUTPUT_CTRL3,
224 AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1));
227 int axp_set_sw(bool on)
230 return pmic_bus_setbits(AXP818_OUTPUT_CTRL2,
231 AXP818_OUTPUT_CTRL2_SW_EN);
233 return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2,
234 AXP818_OUTPUT_CTRL2_SW_EN);
242 ret = pmic_bus_init();
246 ret = pmic_bus_read(AXP818_CHIP_ID, &axp_chip_id);
250 if (!(axp_chip_id == 0x51))
258 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
260 pmic_bus_write(AXP818_SHUTDOWN, AXP818_SHUTDOWN_POWEROFF);
262 /* infinite loop during shutdown */