Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
vishnupatekar | 91bce9c | 2015-11-29 01:07:21 +0800 | [diff] [blame] | 2 | /* |
| 3 | * AXP818 driver based on AXP221 driver |
| 4 | * |
| 5 | * |
| 6 | * (C) Copyright 2015 Vishnu Patekar <vishnuptekar0510@gmail.com> |
| 7 | * |
| 8 | * Based on axp221.c |
| 9 | * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com> |
| 10 | * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl> |
vishnupatekar | 91bce9c | 2015-11-29 01:07:21 +0800 | [diff] [blame] | 11 | */ |
| 12 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | #include <command.h> |
vishnupatekar | 91bce9c | 2015-11-29 01:07:21 +0800 | [diff] [blame] | 14 | #include <errno.h> |
vishnupatekar | 91bce9c | 2015-11-29 01:07:21 +0800 | [diff] [blame] | 15 | #include <asm/arch/pmic_bus.h> |
| 16 | #include <axp_pmic.h> |
| 17 | |
| 18 | static u8 axp818_mvolt_to_cfg(int mvolt, int min, int max, int div) |
| 19 | { |
| 20 | if (mvolt < min) |
| 21 | mvolt = min; |
| 22 | else if (mvolt > max) |
| 23 | mvolt = max; |
| 24 | |
| 25 | return (mvolt - min) / div; |
| 26 | } |
| 27 | |
| 28 | int axp_set_dcdc1(unsigned int mvolt) |
| 29 | { |
| 30 | int ret; |
| 31 | u8 cfg = axp818_mvolt_to_cfg(mvolt, 1600, 3400, 100); |
| 32 | |
| 33 | if (mvolt == 0) |
| 34 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, |
| 35 | AXP818_OUTPUT_CTRL1_DCDC1_EN); |
| 36 | |
| 37 | ret = pmic_bus_write(AXP818_DCDC1_CTRL, cfg); |
| 38 | if (ret) |
| 39 | return ret; |
| 40 | |
| 41 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, |
| 42 | AXP818_OUTPUT_CTRL1_DCDC1_EN); |
| 43 | } |
| 44 | |
| 45 | int axp_set_dcdc2(unsigned int mvolt) |
| 46 | { |
| 47 | int ret; |
| 48 | u8 cfg; |
| 49 | |
| 50 | if (mvolt >= 1220) |
| 51 | cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20); |
| 52 | else |
| 53 | cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10); |
| 54 | |
| 55 | if (mvolt == 0) |
| 56 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, |
| 57 | AXP818_OUTPUT_CTRL1_DCDC2_EN); |
| 58 | |
| 59 | ret = pmic_bus_write(AXP818_DCDC2_CTRL, cfg); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, |
| 64 | AXP818_OUTPUT_CTRL1_DCDC2_EN); |
| 65 | } |
| 66 | |
| 67 | int axp_set_dcdc3(unsigned int mvolt) |
| 68 | { |
| 69 | int ret; |
| 70 | u8 cfg; |
| 71 | |
| 72 | if (mvolt >= 1220) |
| 73 | cfg = 70 + axp818_mvolt_to_cfg(mvolt, 1220, 1300, 20); |
| 74 | else |
| 75 | cfg = axp818_mvolt_to_cfg(mvolt, 500, 1200, 10); |
| 76 | |
| 77 | if (mvolt == 0) |
| 78 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, |
| 79 | AXP818_OUTPUT_CTRL1_DCDC3_EN); |
| 80 | |
| 81 | ret = pmic_bus_write(AXP818_DCDC3_CTRL, cfg); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | |
| 85 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, |
| 86 | AXP818_OUTPUT_CTRL1_DCDC3_EN); |
| 87 | } |
| 88 | |
| 89 | int axp_set_dcdc5(unsigned int mvolt) |
| 90 | { |
| 91 | int ret; |
| 92 | u8 cfg; |
| 93 | |
| 94 | if (mvolt >= 1140) |
| 95 | cfg = 32 + axp818_mvolt_to_cfg(mvolt, 1140, 1840, 20); |
| 96 | else |
| 97 | cfg = axp818_mvolt_to_cfg(mvolt, 800, 1120, 10); |
| 98 | |
| 99 | if (mvolt == 0) |
| 100 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL1, |
| 101 | AXP818_OUTPUT_CTRL1_DCDC5_EN); |
| 102 | |
| 103 | ret = pmic_bus_write(AXP818_DCDC5_CTRL, cfg); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | |
| 107 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL1, |
| 108 | AXP818_OUTPUT_CTRL1_DCDC5_EN); |
| 109 | } |
| 110 | |
Chen-Yu Tsai | c05aa39 | 2016-01-12 14:42:40 +0800 | [diff] [blame] | 111 | int axp_set_aldo(int aldo_num, unsigned int mvolt) |
| 112 | { |
| 113 | int ret; |
| 114 | u8 cfg; |
| 115 | |
| 116 | if (aldo_num < 1 || aldo_num > 3) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | if (mvolt == 0) |
| 120 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3, |
| 121 | AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1)); |
| 122 | |
| 123 | cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100); |
| 124 | ret = pmic_bus_write(AXP818_ALDO1_CTRL + (aldo_num - 1), cfg); |
| 125 | if (ret) |
| 126 | return ret; |
| 127 | |
| 128 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL3, |
| 129 | AXP818_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1)); |
| 130 | } |
| 131 | |
| 132 | /* TODO: re-work other AXP drivers to consolidate ALDO functions. */ |
| 133 | int axp_set_aldo1(unsigned int mvolt) |
| 134 | { |
| 135 | return axp_set_aldo(1, mvolt); |
| 136 | } |
| 137 | |
| 138 | int axp_set_aldo2(unsigned int mvolt) |
| 139 | { |
| 140 | return axp_set_aldo(2, mvolt); |
| 141 | } |
| 142 | |
| 143 | int axp_set_aldo3(unsigned int mvolt) |
| 144 | { |
| 145 | return axp_set_aldo(3, mvolt); |
| 146 | } |
| 147 | |
Chen-Yu Tsai | 265015b | 2016-01-12 14:42:38 +0800 | [diff] [blame] | 148 | int axp_set_dldo(int dldo_num, unsigned int mvolt) |
| 149 | { |
| 150 | int ret; |
| 151 | u8 cfg; |
| 152 | |
| 153 | if (dldo_num < 1 || dldo_num > 4) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | if (mvolt == 0) |
| 157 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2, |
| 158 | AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); |
| 159 | |
| 160 | cfg = axp818_mvolt_to_cfg(mvolt, 700, 3300, 100); |
| 161 | if (dldo_num == 2 && mvolt > 3300) |
| 162 | cfg += 1 + axp818_mvolt_to_cfg(mvolt, 3400, 4200, 200); |
Ondrej Jirman | 0a0c5f9 | 2019-02-13 18:51:05 +0100 | [diff] [blame] | 163 | ret = pmic_bus_write(AXP818_DLDO1_CTRL + (dldo_num - 1), cfg); |
Chen-Yu Tsai | 265015b | 2016-01-12 14:42:38 +0800 | [diff] [blame] | 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL2, |
| 168 | AXP818_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); |
| 169 | } |
| 170 | |
| 171 | int axp_set_eldo(int eldo_num, unsigned int mvolt) |
| 172 | { |
| 173 | int ret; |
| 174 | u8 cfg; |
| 175 | |
| 176 | if (eldo_num < 1 || eldo_num > 3) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | if (mvolt == 0) |
| 180 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2, |
| 181 | AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1)); |
| 182 | |
| 183 | cfg = axp818_mvolt_to_cfg(mvolt, 700, 1900, 50); |
| 184 | ret = pmic_bus_write(AXP818_ELDO1_CTRL + (eldo_num - 1), cfg); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL2, |
| 189 | AXP818_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1)); |
| 190 | } |
| 191 | |
Chen-Yu Tsai | d028fba | 2016-03-30 00:26:48 +0800 | [diff] [blame] | 192 | int axp_set_fldo(int fldo_num, unsigned int mvolt) |
| 193 | { |
| 194 | int ret; |
| 195 | u8 cfg; |
| 196 | |
| 197 | if (fldo_num < 1 || fldo_num > 3) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | if (mvolt == 0) |
| 201 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL3, |
| 202 | AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1)); |
| 203 | |
| 204 | if (fldo_num < 3) { |
| 205 | cfg = axp818_mvolt_to_cfg(mvolt, 700, 1450, 50); |
| 206 | ret = pmic_bus_write(AXP818_FLDO1_CTRL + (fldo_num - 1), cfg); |
| 207 | } else { |
| 208 | /* |
| 209 | * Special case for FLDO3, which is DCDC5 / 2 or FLDOIN / 2 |
| 210 | * Since FLDOIN is unknown, test against DCDC5. |
| 211 | */ |
| 212 | if (mvolt * 2 == CONFIG_AXP_DCDC5_VOLT) |
| 213 | ret = pmic_bus_clrbits(AXP818_FLDO2_3_CTRL, |
| 214 | AXP818_FLDO2_3_CTRL_FLDO3_VOL); |
| 215 | else |
| 216 | ret = pmic_bus_setbits(AXP818_FLDO2_3_CTRL, |
| 217 | AXP818_FLDO2_3_CTRL_FLDO3_VOL); |
| 218 | } |
| 219 | if (ret) |
| 220 | return ret; |
| 221 | |
| 222 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL3, |
| 223 | AXP818_OUTPUT_CTRL3_FLDO1_EN << (fldo_num - 1)); |
| 224 | } |
| 225 | |
Chen-Yu Tsai | 0e3efd3 | 2016-05-02 10:28:12 +0800 | [diff] [blame] | 226 | int axp_set_sw(bool on) |
| 227 | { |
| 228 | if (on) |
| 229 | return pmic_bus_setbits(AXP818_OUTPUT_CTRL2, |
| 230 | AXP818_OUTPUT_CTRL2_SW_EN); |
| 231 | |
| 232 | return pmic_bus_clrbits(AXP818_OUTPUT_CTRL2, |
| 233 | AXP818_OUTPUT_CTRL2_SW_EN); |
| 234 | } |
| 235 | |
vishnupatekar | 91bce9c | 2015-11-29 01:07:21 +0800 | [diff] [blame] | 236 | int axp_init(void) |
| 237 | { |
| 238 | u8 axp_chip_id; |
| 239 | int ret; |
| 240 | |
| 241 | ret = pmic_bus_init(); |
| 242 | if (ret) |
| 243 | return ret; |
| 244 | |
| 245 | ret = pmic_bus_read(AXP818_CHIP_ID, &axp_chip_id); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | if (!(axp_chip_id == 0x51)) |
| 250 | return -ENODEV; |
| 251 | else |
| 252 | return ret; |
| 253 | |
| 254 | return 0; |
| 255 | } |
Chen-Yu Tsai | 45f192c | 2016-05-02 10:28:14 +0800 | [diff] [blame] | 256 | |
Samuel Holland | 41a545f | 2021-08-22 18:18:05 -0500 | [diff] [blame] | 257 | #if !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 258 | int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Chen-Yu Tsai | 45f192c | 2016-05-02 10:28:14 +0800 | [diff] [blame] | 259 | { |
| 260 | pmic_bus_write(AXP818_SHUTDOWN, AXP818_SHUTDOWN_POWEROFF); |
| 261 | |
| 262 | /* infinite loop during shutdown */ |
| 263 | while (1) {} |
| 264 | |
| 265 | /* not reached */ |
| 266 | return 0; |
| 267 | } |
Samuel Holland | 41a545f | 2021-08-22 18:18:05 -0500 | [diff] [blame] | 268 | #endif |