1 // SPDX-License-Identifier: GPL-2.0+
4 * Henrik Nordstrom <henrik@henriknordstrom.net>
9 #include <asm/arch/pmic_bus.h>
12 #ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_08
13 # define AXP209_VRC_SLOPE AXP209_VRC_LDO3_800uV_uS
15 #ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_16
16 # define AXP209_VRC_SLOPE AXP209_VRC_LDO3_1600uV_uS
18 #if defined CONFIG_AXP_ALDO3_VOLT_SLOPE_NONE || !defined AXP209_VRC_SLOPE
19 # define AXP209_VRC_SLOPE 0x00
22 static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
29 return (mvolt - min) / div;
32 int axp_set_dcdc2(unsigned int mvolt)
38 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
39 AXP209_OUTPUT_CTRL_DCDC2);
41 rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2);
45 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
47 /* Do we really need to be this gentle? It has built-in voltage slope */
48 while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 &&
55 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
63 int axp_set_dcdc3(unsigned int mvolt)
65 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
69 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
70 AXP209_OUTPUT_CTRL_DCDC3);
72 rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
76 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3);
79 int axp_set_aldo2(unsigned int mvolt)
85 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
86 AXP209_OUTPUT_CTRL_LDO2);
88 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
90 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®);
94 reg |= AXP209_LDO24_LDO2_SET(reg, cfg);
95 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
99 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2);
102 int axp_set_aldo3(unsigned int mvolt)
108 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
109 AXP209_OUTPUT_CTRL_LDO3);
112 * Some boards have trouble reaching the target voltage without causing
113 * great inrush currents. To prevent this, boards can enable a certain
114 * slope to ramp up voltage. Note, this only works when changing an
115 * already active power rail. When toggling power on, the AXP ramps up
116 * steeply at 0.0167 V/uS.
118 rc = pmic_bus_read(AXP209_VRC_DCDC2_LDO3, &cfg);
119 cfg = AXP209_VRC_LDO3_SLOPE_SET(cfg, AXP209_VRC_SLOPE);
120 rc |= pmic_bus_write(AXP209_VRC_DCDC2_LDO3, cfg);
125 #ifdef CONFIG_AXP_ALDO3_INRUSH_QUIRK
127 * On some boards, LDO3 has a too big capacitor installed. When
128 * turning on LDO3, this causes the AXP209 to shutdown on
129 * voltages over 1.9 volt. As a workaround, we enable LDO3
130 * first with the lowest possible voltage. If this still causes
131 * high inrush currents, the voltage slope should be increased.
133 rc = pmic_bus_read(AXP209_OUTPUT_CTRL, &cfg);
137 if (!(cfg & AXP209_OUTPUT_CTRL_LDO3)) {
138 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, 0x0); /* 0.7 Volt */
140 rc |= pmic_bus_setbits(AXP209_OUTPUT_CTRL,
141 AXP209_OUTPUT_CTRL_LDO3);
149 cfg = AXP209_LDO3_VOLTAGE_FROM_LDO3IN;
151 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
152 cfg = AXP209_LDO3_VOLTAGE_SET(cfg);
155 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
159 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3);
162 int axp_set_aldo4(unsigned int mvolt)
165 static const unsigned int vindex[] = {
166 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
167 2700, 2800, 3000, 3100, 3200, 3300
172 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
173 AXP209_OUTPUT_CTRL_LDO4);
175 /* Translate mvolt to register cfg value, requested <= selected */
176 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
178 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®);
182 reg |= AXP209_LDO24_LDO4_SET(reg, cfg);
183 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
187 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4);
195 rc = pmic_bus_init();
199 rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
203 if ((ver & AXP209_CHIP_VERSION_MASK) != 0x1)
206 /* Mask all interrupts */
207 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
208 rc = pmic_bus_write(i, 0);
214 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
215 * from android these are sometimes on.
217 rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
221 rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
225 rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT);
232 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
234 pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF);
236 /* infinite loop during shutdown */