Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 1 | /* |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 2 | * AXP221 and AXP223 driver |
| 3 | * |
| 4 | * IMPORTANT when making changes to this file check that the registers |
| 5 | * used are the same for the axp221 and axp223. |
| 6 | * |
| 7 | * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com> |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 8 | * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl> |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Hans de Goede | fe4b71b | 2015-12-20 16:14:31 +0100 | [diff] [blame] | 14 | #include <command.h> |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 16 | #include <asm/arch/pmic_bus.h> |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 17 | #include <axp_pmic.h> |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 18 | |
| 19 | static u8 axp221_mvolt_to_cfg(int mvolt, int min, int max, int div) |
| 20 | { |
| 21 | if (mvolt < min) |
| 22 | mvolt = min; |
| 23 | else if (mvolt > max) |
| 24 | mvolt = max; |
| 25 | |
| 26 | return (mvolt - min) / div; |
| 27 | } |
| 28 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 29 | int axp_set_dcdc1(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 30 | { |
| 31 | int ret; |
| 32 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 1600, 3400, 100); |
| 33 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 34 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 35 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 36 | AXP221_OUTPUT_CTRL1_DCDC1_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 37 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 38 | ret = pmic_bus_write(AXP221_DCDC1_CTRL, cfg); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 39 | if (ret) |
| 40 | return ret; |
| 41 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 42 | ret = pmic_bus_setbits(AXP221_OUTPUT_CTRL2, |
| 43 | AXP221_OUTPUT_CTRL2_DCDC1SW_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 44 | if (ret) |
| 45 | return ret; |
| 46 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 47 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 48 | AXP221_OUTPUT_CTRL1_DCDC1_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 51 | int axp_set_dcdc2(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 52 | { |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 53 | int ret; |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 54 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20); |
| 55 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 56 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 57 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 58 | AXP221_OUTPUT_CTRL1_DCDC2_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 59 | |
| 60 | ret = pmic_bus_write(AXP221_DCDC2_CTRL, cfg); |
| 61 | if (ret) |
| 62 | return ret; |
| 63 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 64 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 65 | AXP221_OUTPUT_CTRL1_DCDC2_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 68 | int axp_set_dcdc3(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 69 | { |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 70 | int ret; |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 71 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1860, 20); |
| 72 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 73 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 74 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 75 | AXP221_OUTPUT_CTRL1_DCDC3_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 76 | |
| 77 | ret = pmic_bus_write(AXP221_DCDC3_CTRL, cfg); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 81 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 82 | AXP221_OUTPUT_CTRL1_DCDC3_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 85 | int axp_set_dcdc4(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 86 | { |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 87 | int ret; |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 88 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 600, 1540, 20); |
| 89 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 90 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 91 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 92 | AXP221_OUTPUT_CTRL1_DCDC4_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 93 | |
| 94 | ret = pmic_bus_write(AXP221_DCDC4_CTRL, cfg); |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 98 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 99 | AXP221_OUTPUT_CTRL1_DCDC4_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 102 | int axp_set_dcdc5(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 103 | { |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 104 | int ret; |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 105 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 1000, 2550, 50); |
| 106 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 107 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 108 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 109 | AXP221_OUTPUT_CTRL1_DCDC5_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 110 | |
| 111 | ret = pmic_bus_write(AXP221_DCDC5_CTRL, cfg); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 115 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 116 | AXP221_OUTPUT_CTRL1_DCDC5_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 119 | int axp_set_aldo1(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 120 | { |
| 121 | int ret; |
| 122 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); |
| 123 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 124 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 125 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 126 | AXP221_OUTPUT_CTRL1_ALDO1_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 127 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 128 | ret = pmic_bus_write(AXP221_ALDO1_CTRL, cfg); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 129 | if (ret) |
| 130 | return ret; |
| 131 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 132 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 133 | AXP221_OUTPUT_CTRL1_ALDO1_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 136 | int axp_set_aldo2(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 137 | { |
| 138 | int ret; |
| 139 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); |
| 140 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 141 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 142 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL1, |
| 143 | AXP221_OUTPUT_CTRL1_ALDO2_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 144 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 145 | ret = pmic_bus_write(AXP221_ALDO2_CTRL, cfg); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 146 | if (ret) |
| 147 | return ret; |
| 148 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 149 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL1, |
| 150 | AXP221_OUTPUT_CTRL1_ALDO2_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 153 | int axp_set_aldo3(unsigned int mvolt) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 154 | { |
| 155 | int ret; |
| 156 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); |
| 157 | |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 158 | if (mvolt == 0) |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 159 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL3, |
| 160 | AXP221_OUTPUT_CTRL3_ALDO3_EN); |
Hans de Goede | 50e0d5e | 2014-12-13 14:02:38 +0100 | [diff] [blame] | 161 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 162 | ret = pmic_bus_write(AXP221_ALDO3_CTRL, cfg); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 163 | if (ret) |
| 164 | return ret; |
| 165 | |
Hans de Goede | 1d624a4 | 2015-04-25 14:07:37 +0200 | [diff] [blame] | 166 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL3, |
| 167 | AXP221_OUTPUT_CTRL3_ALDO3_EN); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Chen-Yu Tsai | 3517a27 | 2016-01-12 14:42:37 +0800 | [diff] [blame] | 170 | int axp_set_dldo(int dldo_num, unsigned int mvolt) |
| 171 | { |
| 172 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); |
| 173 | int ret; |
| 174 | |
| 175 | if (dldo_num < 1 || dldo_num > 4) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | if (mvolt == 0) |
| 179 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2, |
| 180 | AXP221_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); |
| 181 | |
| 182 | ret = pmic_bus_write(AXP221_DLDO1_CTRL + (dldo_num - 1), cfg); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | |
| 186 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL2, |
| 187 | AXP221_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); |
| 188 | } |
| 189 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 190 | int axp_set_eldo(int eldo_num, unsigned int mvolt) |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 191 | { |
| 192 | int ret; |
| 193 | u8 cfg = axp221_mvolt_to_cfg(mvolt, 700, 3300, 100); |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 194 | |
Chen-Yu Tsai | aa23f53 | 2016-05-02 10:28:10 +0800 | [diff] [blame] | 195 | if (eldo_num < 1 || eldo_num > 3) |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 196 | return -EINVAL; |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 197 | |
| 198 | if (mvolt == 0) |
Chen-Yu Tsai | aa23f53 | 2016-05-02 10:28:10 +0800 | [diff] [blame] | 199 | return pmic_bus_clrbits(AXP221_OUTPUT_CTRL2, |
| 200 | AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1)); |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 201 | |
Chen-Yu Tsai | aa23f53 | 2016-05-02 10:28:10 +0800 | [diff] [blame] | 202 | ret = pmic_bus_write(AXP221_ELDO1_CTRL + (eldo_num - 1), cfg); |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 203 | if (ret) |
| 204 | return ret; |
| 205 | |
Chen-Yu Tsai | aa23f53 | 2016-05-02 10:28:10 +0800 | [diff] [blame] | 206 | return pmic_bus_setbits(AXP221_OUTPUT_CTRL2, |
| 207 | AXP221_OUTPUT_CTRL2_ELDO1_EN << (eldo_num - 1)); |
Siarhei Siamashka | 6906df1 | 2015-01-19 05:23:30 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 210 | int axp_init(void) |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 211 | { |
| 212 | u8 axp_chip_id; |
| 213 | int ret; |
| 214 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 215 | ret = pmic_bus_init(); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 216 | if (ret) |
| 217 | return ret; |
| 218 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 219 | ret = pmic_bus_read(AXP221_CHIP_ID, &axp_chip_id); |
Oliver Schinagl | 5c7f10f | 2013-07-26 12:56:58 +0200 | [diff] [blame] | 220 | if (ret) |
| 221 | return ret; |
| 222 | |
| 223 | if (!(axp_chip_id == 0x6 || axp_chip_id == 0x7 || axp_chip_id == 0x17)) |
| 224 | return -ENODEV; |
| 225 | |
| 226 | return 0; |
| 227 | } |
Hans de Goede | f3fba56 | 2014-11-25 16:37:52 +0100 | [diff] [blame] | 228 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 229 | int axp_get_sid(unsigned int *sid) |
Hans de Goede | f3fba56 | 2014-11-25 16:37:52 +0100 | [diff] [blame] | 230 | { |
| 231 | u8 *dest = (u8 *)sid; |
| 232 | int i, ret; |
| 233 | |
Hans de Goede | 6944aff | 2015-10-03 15:18:33 +0200 | [diff] [blame] | 234 | ret = pmic_bus_init(); |
Hans de Goede | f3fba56 | 2014-11-25 16:37:52 +0100 | [diff] [blame] | 235 | if (ret) |
| 236 | return ret; |
| 237 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 238 | ret = pmic_bus_write(AXP221_PAGE, 1); |
Hans de Goede | f3fba56 | 2014-11-25 16:37:52 +0100 | [diff] [blame] | 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | for (i = 0; i < 16; i++) { |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 243 | ret = pmic_bus_read(AXP221_SID + i, &dest[i]); |
Hans de Goede | f3fba56 | 2014-11-25 16:37:52 +0100 | [diff] [blame] | 244 | if (ret) |
| 245 | return ret; |
| 246 | } |
| 247 | |
Hans de Goede | bdcdf84 | 2014-11-29 23:54:25 +0100 | [diff] [blame] | 248 | pmic_bus_write(AXP221_PAGE, 0); |
Hans de Goede | f3fba56 | 2014-11-25 16:37:52 +0100 | [diff] [blame] | 249 | |
| 250 | for (i = 0; i < 4; i++) |
| 251 | sid[i] = be32_to_cpu(sid[i]); |
| 252 | |
| 253 | return 0; |
| 254 | } |
Hans de Goede | fe4b71b | 2015-12-20 16:14:31 +0100 | [diff] [blame] | 255 | |
| 256 | int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 257 | { |
| 258 | pmic_bus_write(AXP221_SHUTDOWN, AXP221_SHUTDOWN_POWEROFF); |
| 259 | |
| 260 | /* infinite loop during shutdown */ |
| 261 | while (1) {} |
| 262 | |
| 263 | /* not reached */ |
| 264 | return 0; |
| 265 | } |