Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Henrik Nordstrom <henrik@henriknordstrom.net> |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Michael van Slingerland | 8fb31fe | 2015-12-13 13:17:31 +0100 | [diff] [blame] | 7 | #include <command.h> |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 8 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 9 | #include <axp_pmic.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 10 | #include <linux/delay.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <linux/errno.h> |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 12 | |
Olliver Schinagl | 353b4cf | 2018-11-21 20:05:30 +0200 | [diff] [blame] | 13 | #ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_08 |
| 14 | # define AXP209_VRC_SLOPE AXP209_VRC_LDO3_800uV_uS |
| 15 | #endif |
| 16 | #ifdef CONFIG_AXP_ALDO3_VOLT_SLOPE_16 |
| 17 | # define AXP209_VRC_SLOPE AXP209_VRC_LDO3_1600uV_uS |
| 18 | #endif |
| 19 | #if defined CONFIG_AXP_ALDO3_VOLT_SLOPE_NONE || !defined AXP209_VRC_SLOPE |
| 20 | # define AXP209_VRC_SLOPE 0x00 |
| 21 | #endif |
| 22 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 23 | static u8 axp209_mvolt_to_cfg(int mvolt, int min, int max, int div) |
| 24 | { |
| 25 | if (mvolt < min) |
| 26 | mvolt = min; |
| 27 | else if (mvolt > max) |
| 28 | mvolt = max; |
| 29 | |
| 30 | return (mvolt - min) / div; |
| 31 | } |
| 32 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 33 | int axp_set_dcdc2(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 34 | { |
| 35 | int rc; |
| 36 | u8 cfg, current; |
| 37 | |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 38 | if (mvolt == 0) |
| 39 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 40 | AXP209_OUTPUT_CTRL_DCDC2); |
| 41 | |
| 42 | rc = pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC2); |
| 43 | if (rc) |
| 44 | return rc; |
| 45 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 46 | cfg = axp209_mvolt_to_cfg(mvolt, 700, 2275, 25); |
| 47 | |
| 48 | /* Do we really need to be this gentle? It has built-in voltage slope */ |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 49 | while ((rc = pmic_bus_read(AXP209_DCDC2_VOLTAGE, ¤t)) == 0 && |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 50 | current != cfg) { |
| 51 | if (current < cfg) |
| 52 | current++; |
| 53 | else |
| 54 | current--; |
| 55 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 56 | rc = pmic_bus_write(AXP209_DCDC2_VOLTAGE, current); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 57 | if (rc) |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | return rc; |
| 62 | } |
| 63 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 64 | int axp_set_dcdc3(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 65 | { |
| 66 | u8 cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 67 | int rc; |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 68 | |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 69 | if (mvolt == 0) |
| 70 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 71 | AXP209_OUTPUT_CTRL_DCDC3); |
| 72 | |
| 73 | rc = pmic_bus_write(AXP209_DCDC3_VOLTAGE, cfg); |
| 74 | if (rc) |
| 75 | return rc; |
| 76 | |
| 77 | return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_DCDC3); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 80 | int axp_set_aldo2(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 81 | { |
| 82 | int rc; |
| 83 | u8 cfg, reg; |
| 84 | |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 85 | if (mvolt == 0) |
| 86 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 87 | AXP209_OUTPUT_CTRL_LDO2); |
| 88 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 89 | cfg = axp209_mvolt_to_cfg(mvolt, 1800, 3300, 100); |
| 90 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 91 | rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 92 | if (rc) |
| 93 | return rc; |
| 94 | |
Olliver Schinagl | 9238249 | 2018-11-21 20:05:29 +0200 | [diff] [blame] | 95 | reg |= AXP209_LDO24_LDO2_SET(reg, cfg); |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 96 | rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg); |
| 97 | if (rc) |
| 98 | return rc; |
| 99 | |
| 100 | return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO2); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 103 | int axp_set_aldo3(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 104 | { |
| 105 | u8 cfg; |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 106 | int rc; |
| 107 | |
| 108 | if (mvolt == 0) |
| 109 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 110 | AXP209_OUTPUT_CTRL_LDO3); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 111 | |
Olliver Schinagl | 353b4cf | 2018-11-21 20:05:30 +0200 | [diff] [blame] | 112 | /* |
| 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. |
| 118 | */ |
| 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); |
| 122 | |
| 123 | if (rc) |
| 124 | return rc; |
| 125 | |
Olliver Schinagl | 9e77d61 | 2018-11-21 20:05:31 +0200 | [diff] [blame] | 126 | #ifdef CONFIG_AXP_ALDO3_INRUSH_QUIRK |
| 127 | /* |
| 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. |
| 133 | */ |
| 134 | rc = pmic_bus_read(AXP209_OUTPUT_CTRL, &cfg); |
| 135 | if (rc) |
| 136 | return rc; |
| 137 | |
| 138 | if (!(cfg & AXP209_OUTPUT_CTRL_LDO3)) { |
| 139 | rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, 0x0); /* 0.7 Volt */ |
| 140 | mdelay(1); |
| 141 | rc |= pmic_bus_setbits(AXP209_OUTPUT_CTRL, |
| 142 | AXP209_OUTPUT_CTRL_LDO3); |
| 143 | |
| 144 | if (rc) |
| 145 | return rc; |
| 146 | } |
| 147 | #endif |
| 148 | |
Olliver Schinagl | 9238249 | 2018-11-21 20:05:29 +0200 | [diff] [blame] | 149 | if (mvolt == -1) { |
| 150 | cfg = AXP209_LDO3_VOLTAGE_FROM_LDO3IN; |
| 151 | } else { |
Iain Paton | 71a7b3b | 2015-03-25 16:03:26 +0000 | [diff] [blame] | 152 | cfg = axp209_mvolt_to_cfg(mvolt, 700, 3500, 25); |
Olliver Schinagl | 9238249 | 2018-11-21 20:05:29 +0200 | [diff] [blame] | 153 | cfg = AXP209_LDO3_VOLTAGE_SET(cfg); |
| 154 | } |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 155 | |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 156 | rc = pmic_bus_write(AXP209_LDO3_VOLTAGE, cfg); |
| 157 | if (rc) |
| 158 | return rc; |
| 159 | |
| 160 | return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO3); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 163 | int axp_set_aldo4(unsigned int mvolt) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 164 | { |
| 165 | int rc; |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 166 | static const unsigned int vindex[] = { |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 167 | 1250, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500, |
| 168 | 2700, 2800, 3000, 3100, 3200, 3300 |
| 169 | }; |
| 170 | u8 cfg, reg; |
| 171 | |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 172 | if (mvolt == 0) |
| 173 | return pmic_bus_clrbits(AXP209_OUTPUT_CTRL, |
| 174 | AXP209_OUTPUT_CTRL_LDO4); |
| 175 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 176 | /* Translate mvolt to register cfg value, requested <= selected */ |
| 177 | for (cfg = 15; vindex[cfg] > mvolt && cfg > 0; cfg--); |
| 178 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 179 | rc = pmic_bus_read(AXP209_LDO24_VOLTAGE, ®); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 180 | if (rc) |
| 181 | return rc; |
| 182 | |
Olliver Schinagl | 9238249 | 2018-11-21 20:05:29 +0200 | [diff] [blame] | 183 | reg |= AXP209_LDO24_LDO4_SET(reg, cfg); |
Hans de Goede | 84a8b79 | 2015-10-04 12:01:17 +0200 | [diff] [blame] | 184 | rc = pmic_bus_write(AXP209_LDO24_VOLTAGE, reg); |
| 185 | if (rc) |
| 186 | return rc; |
| 187 | |
| 188 | return pmic_bus_setbits(AXP209_OUTPUT_CTRL, AXP209_OUTPUT_CTRL_LDO4); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Hans de Goede | d9ee84b | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 191 | int axp_init(void) |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 192 | { |
| 193 | u8 ver; |
Hans de Goede | c004b9f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 194 | int i, rc; |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 195 | |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 196 | rc = pmic_bus_init(); |
| 197 | if (rc) |
| 198 | return rc; |
| 199 | |
| 200 | rc = pmic_bus_read(AXP209_CHIP_VERSION, &ver); |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 201 | if (rc) |
| 202 | return rc; |
| 203 | |
Olliver Schinagl | 185210d | 2018-11-21 20:05:28 +0200 | [diff] [blame] | 204 | if ((ver & AXP209_CHIP_VERSION_MASK) != 0x1) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 205 | return -EINVAL; |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 206 | |
Hans de Goede | c004b9f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 207 | /* Mask all interrupts */ |
| 208 | for (i = AXP209_IRQ_ENABLE1; i <= AXP209_IRQ_ENABLE5; i++) { |
Hans de Goede | aa1d512 | 2015-10-03 16:12:27 +0200 | [diff] [blame] | 209 | rc = pmic_bus_write(i, 0); |
Hans de Goede | c004b9f | 2015-01-17 16:39:20 +0100 | [diff] [blame] | 210 | if (rc) |
| 211 | return rc; |
| 212 | } |
| 213 | |
Hans de Goede | af7f67c | 2016-09-12 09:52:52 +0200 | [diff] [blame] | 214 | /* |
| 215 | * Turn off LDOIO regulators / tri-state GPIO pins, when rebooting |
| 216 | * from android these are sometimes on. |
| 217 | */ |
| 218 | rc = pmic_bus_write(AXP_GPIO0_CTRL, AXP_GPIO_CTRL_INPUT); |
| 219 | if (rc) |
| 220 | return rc; |
| 221 | |
| 222 | rc = pmic_bus_write(AXP_GPIO1_CTRL, AXP_GPIO_CTRL_INPUT); |
| 223 | if (rc) |
| 224 | return rc; |
| 225 | |
| 226 | rc = pmic_bus_write(AXP_GPIO2_CTRL, AXP_GPIO_CTRL_INPUT); |
| 227 | if (rc) |
| 228 | return rc; |
| 229 | |
Henrik Nordstrom | aa382ad | 2014-06-13 22:55:50 +0200 | [diff] [blame] | 230 | return 0; |
| 231 | } |
Michael van Slingerland | 8fb31fe | 2015-12-13 13:17:31 +0100 | [diff] [blame] | 232 | |
Samuel Holland | 41a545f | 2021-08-22 18:18:05 -0500 | [diff] [blame] | 233 | #if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 234 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Michael van Slingerland | 8fb31fe | 2015-12-13 13:17:31 +0100 | [diff] [blame] | 235 | { |
| 236 | pmic_bus_write(AXP209_SHUTDOWN, AXP209_POWEROFF); |
| 237 | |
| 238 | /* infinite loop during shutdown */ |
| 239 | while (1) {} |
| 240 | |
| 241 | /* not reached */ |
| 242 | return 0; |
| 243 | } |
Samuel Holland | 41a545f | 2021-08-22 18:18:05 -0500 | [diff] [blame] | 244 | #endif |