1 // SPDX-License-Identifier: GPL-2.0+
9 #include <asm/arch/pmic_bus.h>
11 #include <linux/delay.h>
13 #ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_08
14 # define AXP209_VRC_SLOPE AXP209_VRC_LDO3_800uV_uS
16 #ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_16
17 # define AXP209_VRC_SLOPE AXP209_VRC_LDO3_1600uV_uS
19 #if defined CONFIG_AXP_ALDO3_VOLT_SLOPE_NONE || !defined AXP209_VRC_SLOPE
20 # define AXP209_VRC_SLOPE 0x00
23 static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
30 return (mvolt - min) / div;
33 int axp_set_dcdc2(unsigned int mvolt)
39 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
40 AXP209_OUTPUT_CTRL_DCDC2);
42 rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2);
46 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
48 /* Do we really need to be this gentle? It has built-in voltage slope */
49 while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 &&
56 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
64 int axp_set_dcdc3(unsigned int mvolt)
66 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
70 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
71 AXP209_OUTPUT_CTRL_DCDC3);
73 rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
77 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3);
80 int axp_set_aldo2(unsigned int mvolt)
86 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
87 AXP209_OUTPUT_CTRL_LDO2);
89 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
91 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®);
95 reg |= AXP209_LDO24_LDO2_SET(reg, cfg);
96 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
100 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2);
103 int axp_set_aldo3(unsigned int mvolt)
109 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
110 AXP209_OUTPUT_CTRL_LDO3);
113 * Some boards have trouble reaching the target voltage without causing
114 * great inrush currents. To prevent this, boards can enable a certain
115 * slope to ramp up voltage. Note, this only works when changing an
116 * already active power rail. When toggling power on, the AXP ramps up
117 * steeply at 0.0167 V/uS.
119 rc = pmic_bus_read(AXP209_VRC_DCDC2_LDO3, &cfg);
120 cfg = AXP209_VRC_LDO3_SLOPE_SET(cfg, AXP209_VRC_SLOPE);
121 rc |= pmic_bus_write(AXP209_VRC_DCDC2_LDO3, cfg);
126 #ifdef CONFIG_AXP_ALDO3_INRUSH_QUIRK
128 * On some boards, LDO3 has a too big capacitor installed. When
129 * turning on LDO3, this causes the AXP209 to shutdown on
130 * voltages over 1.9 volt. As a workaround, we enable LDO3
131 * first with the lowest possible voltage. If this still causes
132 * high inrush currents, the voltage slope should be increased.
134 rc = pmic_bus_read(AXP209_OUTPUT_CTRL, &cfg);
138 if (!(cfg & AXP209_OUTPUT_CTRL_LDO3)) {
139 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, 0x0); /* 0.7 Volt */
141 rc |= pmic_bus_setbits(AXP209_OUTPUT_CTRL,
142 AXP209_OUTPUT_CTRL_LDO3);
150 cfg = AXP209_LDO3_VOLTAGE_FROM_LDO3IN;
152 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
153 cfg = AXP209_LDO3_VOLTAGE_SET(cfg);
156 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
160 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3);
163 int axp_set_aldo4(unsigned int mvolt)
166 static const unsigned int vindex[] = {
167 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
168 2700, 2800, 3000, 3100, 3200, 3300
173 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
174 AXP209_OUTPUT_CTRL_LDO4);
176 /* Translate mvolt to register cfg value, requested <= selected */
177 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
179 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®);
183 reg |= AXP209_LDO24_LDO4_SET(reg, cfg);
184 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
188 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4);
196 rc = pmic_bus_init();
200 rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
204 if ((ver & AXP209_CHIP_VERSION_MASK) != 0x1)
207 /* Mask all interrupts */
208 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
209 rc = pmic_bus_write(i, 0);
215 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
216 * from android these are sometimes on.
218 rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
222 rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
226 rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT);
233 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
235 pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF);
237 /* infinite loop during shutdown */