3 * Henrik Nordstrom <henrik@henriknordstrom.net>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch/pmic_bus.h>
13 static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div)
20 return (mvolt - min) / div;
23 int axp_set_dcdc2(unsigned int mvolt)
29 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
30 AXP209_OUTPUT_CTRL_DCDC2);
32 rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2);
36 cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25);
38 /* Do we really need to be this gentle? It has built-in voltage slope */
39 while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 &&
46 rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current);
54 int axp_set_dcdc3(unsigned int mvolt)
56 u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
60 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
61 AXP209_OUTPUT_CTRL_DCDC3);
63 rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg);
67 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3);
70 int axp_set_aldo2(unsigned int mvolt)
76 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
77 AXP209_OUTPUT_CTRL_LDO2);
79 cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100);
81 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®);
85 /* LDO2 configuration is in upper 4 bits */
86 reg = (reg & 0x0f) | (cfg << 4);
87 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
91 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2);
94 int axp_set_aldo3(unsigned int mvolt)
100 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
101 AXP209_OUTPUT_CTRL_LDO3);
104 cfg = 0x80; /* determined by LDO3IN pin */
106 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25);
108 rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg);
112 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3);
115 int axp_set_aldo4(unsigned int mvolt)
118 static const unsigned int vindex[] = {
119 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500,
120 2700, 2800, 3000, 3100, 3200, 3300
125 return pmic_bus_clrbits(AXP209_OUTPUT_CTRL,
126 AXP209_OUTPUT_CTRL_LDO4);
128 /* Translate mvolt to register cfg value, requested <= selected */
129 for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--);
131 rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®);
135 /* LDO4 configuration is in lower 4 bits */
136 reg = (reg & 0xf0) | (cfg << 0);
137 rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg);
141 return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4);
149 rc = pmic_bus_init();
153 rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver);
157 /* Low 4 bits is chip version */
163 /* Mask all interrupts */
164 for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) {
165 rc = pmic_bus_write(i, 0);
171 * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting
172 * from android these are sometimes on.
174 rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT);
178 rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT);
182 rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT);
189 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
191 pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF);
193 /* infinite loop during shutdown */