Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Freescale Semiconductor, Inc. |
Meenakshi Aggarwal | ccb5d5d | 2020-10-29 19:16:16 +0530 | [diff] [blame] | 4 | * Copyright 2020 NXP |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 5 | * Copyright 2020 Stephen Carlson <stcarlso@linux.microsoft.com> |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 11 | #include <i2c.h> |
Simon Glass | 8f3f761 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 12 | #include <irq_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Shaohui Xie | dd33567 | 2015-11-11 17:58:37 +0800 | [diff] [blame] | 14 | #include <asm/io.h> |
Shaohui Xie | 085ac1c | 2016-09-07 17:56:14 +0800 | [diff] [blame] | 15 | #ifdef CONFIG_FSL_LSCH2 |
Shaohui Xie | dd33567 | 2015-11-11 17:58:37 +0800 | [diff] [blame] | 16 | #include <asm/arch/immap_lsch2.h> |
Rai Harninder | 6aa1f3b | 2016-03-23 17:04:38 +0530 | [diff] [blame] | 17 | #elif defined(CONFIG_FSL_LSCH3) |
| 18 | #include <asm/arch/immap_lsch3.h> |
Shaohui Xie | dd33567 | 2015-11-11 17:58:37 +0800 | [diff] [blame] | 19 | #else |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 20 | #include <asm/immap_85xx.h> |
Shaohui Xie | dd33567 | 2015-11-11 17:58:37 +0800 | [diff] [blame] | 21 | #endif |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 23 | #include "vid.h" |
| 24 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 25 | /* Voltages are generally handled in mV to keep them as integers */ |
| 26 | #define MV_PER_V 1000 |
| 27 | |
| 28 | /* |
| 29 | * Select the channel on the I2C mux (on some NXP boards) that contains |
| 30 | * the voltage regulator to use for VID. Return 0 for success or nonzero |
| 31 | * for failure. |
| 32 | */ |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 33 | int __weak i2c_multiplexer_select_vid_channel(u8 channel) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | /* |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 39 | * Compensate for a board specific voltage drop between regulator and SoC. |
| 40 | * Returns the voltage offset in mV. |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 41 | */ |
| 42 | int __weak board_vdd_drop_compensation(void) |
| 43 | { |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /* |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 48 | * Performs any board specific adjustments after the VID voltage has been |
| 49 | * set. Return 0 for success or nonzero for failure. |
Rajesh Bhagat | c3a662f | 2018-01-17 16:13:02 +0530 | [diff] [blame] | 50 | */ |
| 51 | int __weak board_adjust_vdd(int vdd) |
| 52 | { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Processor specific method of converting the fuse value read from VID |
| 58 | * registers into the core voltage to supply. Return the voltage in mV. |
| 59 | */ |
| 60 | u16 __weak soc_get_fuse_vid(int vid_index) |
| 61 | { |
| 62 | /* Default VDD for Layerscape Chassis 1 devices */ |
| 63 | static const u16 vdd[32] = { |
| 64 | 0, /* unused */ |
| 65 | 9875, /* 0.9875V */ |
| 66 | 9750, |
| 67 | 9625, |
| 68 | 9500, |
| 69 | 9375, |
| 70 | 9250, |
| 71 | 9125, |
| 72 | 9000, |
| 73 | 8875, |
| 74 | 8750, |
| 75 | 8625, |
| 76 | 8500, |
| 77 | 8375, |
| 78 | 8250, |
| 79 | 8125, |
| 80 | 10000, /* 1.0000V */ |
| 81 | 10125, |
| 82 | 10250, |
| 83 | 10375, |
| 84 | 10500, |
| 85 | 10625, |
| 86 | 10750, |
| 87 | 10875, |
| 88 | 11000, |
| 89 | 0, /* reserved */ |
| 90 | }; |
| 91 | return vdd[vid_index]; |
| 92 | } |
| 93 | |
| 94 | #ifndef I2C_VOL_MONITOR_ADDR |
| 95 | #define I2C_VOL_MONITOR_ADDR 0 |
| 96 | #endif |
| 97 | |
| 98 | #if CONFIG_IS_ENABLED(DM_I2C) |
| 99 | #define DEVICE_HANDLE_T struct udevice * |
| 100 | |
| 101 | #ifndef I2C_VOL_MONITOR_BUS |
| 102 | #define I2C_VOL_MONITOR_BUS 0 |
| 103 | #endif |
| 104 | |
| 105 | /* If DM is in use, retrieve the udevice chip for the specified bus number */ |
| 106 | static int vid_get_device(int address, DEVICE_HANDLE_T *dev) |
| 107 | { |
| 108 | int ret = i2c_get_chip_for_busnum(I2C_VOL_MONITOR_BUS, address, 1, dev); |
| 109 | |
| 110 | if (ret) |
| 111 | printf("VID: Bus %d has no device with address 0x%02X\n", |
| 112 | I2C_VOL_MONITOR_BUS, address); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | #define I2C_READ(dev, register, data, length) \ |
| 117 | dm_i2c_read(dev, register, data, length) |
| 118 | #define I2C_WRITE(dev, register, data, length) \ |
| 119 | dm_i2c_write(dev, register, data, length) |
| 120 | #else |
| 121 | #define DEVICE_HANDLE_T int |
| 122 | |
| 123 | /* If DM is not in use, I2C addresses are passed directly */ |
| 124 | static int vid_get_device(int address, DEVICE_HANDLE_T *dev) |
| 125 | { |
| 126 | *dev = address; |
Rajesh Bhagat | c3a662f | 2018-01-17 16:13:02 +0530 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 130 | #define I2C_READ(dev, register, data, length) \ |
| 131 | i2c_read(dev, register, 1, data, length) |
| 132 | #define I2C_WRITE(dev, register, data, length) \ |
| 133 | i2c_write(dev, register, 1, data, length) |
| 134 | #endif |
| 135 | |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 136 | #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \ |
| 137 | defined(CONFIG_VOL_MONITOR_IR36021_READ) |
Rajesh Bhagat | c3a662f | 2018-01-17 16:13:02 +0530 | [diff] [blame] | 138 | /* |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 139 | * Get the i2c address configuration for the IR regulator chip |
| 140 | * |
| 141 | * There are some variance in the RDB HW regarding the I2C address configuration |
| 142 | * for the IR regulator chip, which is likely a problem of external resistor |
| 143 | * accuracy. So we just check each address in a hopefully non-intrusive mode |
| 144 | * and use the first one that seems to work |
| 145 | * |
| 146 | * The IR chip can show up under the following addresses: |
| 147 | * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA) |
| 148 | * 0x09 (Verified on T1040RDB-PA) |
Ying Zhang | ff77905 | 2016-01-22 12:15:13 +0800 | [diff] [blame] | 149 | * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB) |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 150 | */ |
| 151 | static int find_ir_chip_on_i2c(void) |
| 152 | { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 153 | int i2caddress, ret, i; |
| 154 | u8 mfrID; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 155 | const int ir_i2c_addr[] = {0x38, 0x08, 0x09}; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 156 | DEVICE_HANDLE_T dev; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 157 | |
| 158 | /* Check all the address */ |
| 159 | for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) { |
| 160 | i2caddress = ir_i2c_addr[i]; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 161 | ret = vid_get_device(i2caddress, &dev); |
| 162 | if (!ret) { |
| 163 | ret = I2C_READ(dev, IR36021_MFR_ID_OFFSET, |
| 164 | (void *)&mfrID, sizeof(mfrID)); |
| 165 | /* If manufacturer ID matches the IR36021 */ |
| 166 | if (!ret && mfrID == IR36021_MFR_ID) |
| 167 | return i2caddress; |
| 168 | } |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 169 | } |
| 170 | return -1; |
| 171 | } |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 172 | #endif |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 173 | |
| 174 | /* Maximum loop count waiting for new voltage to take effect */ |
| 175 | #define MAX_LOOP_WAIT_NEW_VOL 100 |
| 176 | /* Maximum loop count waiting for the voltage to be stable */ |
| 177 | #define MAX_LOOP_WAIT_VOL_STABLE 100 |
| 178 | /* |
| 179 | * read_voltage from sensor on I2C bus |
| 180 | * We use average of 4 readings, waiting for WAIT_FOR_ADC before |
| 181 | * another reading |
| 182 | */ |
| 183 | #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */ |
| 184 | |
| 185 | /* If an INA220 chip is available, we can use it to read back the voltage |
| 186 | * as it may have a higher accuracy than the IR chip for the same purpose |
| 187 | */ |
| 188 | #ifdef CONFIG_VOL_MONITOR_INA220 |
| 189 | #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */ |
| 190 | #define ADC_MIN_ACCURACY 4 |
| 191 | #else |
| 192 | #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */ |
| 193 | #define ADC_MIN_ACCURACY 4 |
| 194 | #endif |
| 195 | |
| 196 | #ifdef CONFIG_VOL_MONITOR_INA220 |
| 197 | static int read_voltage_from_INA220(int i2caddress) |
| 198 | { |
| 199 | int i, ret, voltage_read = 0; |
| 200 | u16 vol_mon; |
| 201 | u8 buf[2]; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 202 | DEVICE_HANDLE_T dev; |
| 203 | |
| 204 | /* Open device handle */ |
| 205 | ret = vid_get_device(i2caddress, &dev); |
| 206 | if (ret) |
| 207 | return ret; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 208 | |
| 209 | for (i = 0; i < NUM_READINGS; i++) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 210 | ret = I2C_READ(dev, I2C_VOL_MONITOR_BUS_V_OFFSET, |
| 211 | (void *)&buf[0], sizeof(buf)); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 212 | if (ret) { |
| 213 | printf("VID: failed to read core voltage\n"); |
| 214 | return ret; |
| 215 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 216 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 217 | vol_mon = (buf[0] << 8) | buf[1]; |
| 218 | if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) { |
| 219 | printf("VID: Core voltage sensor error\n"); |
| 220 | return -1; |
| 221 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 222 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 223 | debug("VID: bus voltage reads 0x%04x\n", vol_mon); |
| 224 | /* LSB = 4mv */ |
| 225 | voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4; |
| 226 | udelay(WAIT_FOR_ADC); |
| 227 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 228 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 229 | /* calculate the average */ |
| 230 | voltage_read /= NUM_READINGS; |
| 231 | |
| 232 | return voltage_read; |
| 233 | } |
| 234 | #endif |
| 235 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 236 | #ifdef CONFIG_VOL_MONITOR_IR36021_READ |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 237 | /* read voltage from IR */ |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 238 | static int read_voltage_from_IR(int i2caddress) |
| 239 | { |
| 240 | int i, ret, voltage_read = 0; |
| 241 | u16 vol_mon; |
| 242 | u8 buf; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 243 | DEVICE_HANDLE_T dev; |
| 244 | |
| 245 | /* Open device handle */ |
| 246 | ret = vid_get_device(i2caddress, &dev); |
| 247 | if (ret) |
| 248 | return ret; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 249 | |
| 250 | for (i = 0; i < NUM_READINGS; i++) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 251 | ret = I2C_READ(dev, IR36021_LOOP1_VOUT_OFFSET, (void *)&buf, |
| 252 | sizeof(buf)); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 253 | if (ret) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 254 | printf("VID: failed to read core voltage\n"); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | vol_mon = buf; |
| 258 | if (!vol_mon) { |
| 259 | printf("VID: Core voltage sensor error\n"); |
| 260 | return -1; |
| 261 | } |
| 262 | debug("VID: bus voltage reads 0x%02x\n", vol_mon); |
| 263 | /* Resolution is 1/128V. We scale up here to get 1/128mV |
| 264 | * and divide at the end |
| 265 | */ |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 266 | voltage_read += vol_mon * MV_PER_V; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 267 | udelay(WAIT_FOR_ADC); |
| 268 | } |
| 269 | /* Scale down to the real mV as IR resolution is 1/128V, rounding up */ |
| 270 | voltage_read = DIV_ROUND_UP(voltage_read, 128); |
| 271 | |
| 272 | /* calculate the average */ |
| 273 | voltage_read /= NUM_READINGS; |
| 274 | |
| 275 | /* Compensate for a board specific voltage drop between regulator and |
| 276 | * SoC before converting into an IR VID value |
| 277 | */ |
| 278 | voltage_read -= board_vdd_drop_compensation(); |
| 279 | |
| 280 | return voltage_read; |
| 281 | } |
| 282 | #endif |
| 283 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 284 | #if defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \ |
| 285 | defined(CONFIG_VOL_MONITOR_LTC3882_READ) || \ |
| 286 | defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \ |
| 287 | defined(CONFIG_VOL_MONITOR_LTC3882_SET) |
| 288 | |
| 289 | /* |
| 290 | * The message displayed if the VOUT exponent causes a resolution |
| 291 | * worse than 1.0 V (if exponent is >= 0). |
| 292 | */ |
| 293 | #define VOUT_WARNING "VID: VOUT_MODE exponent has resolution worse than 1 V!\n" |
| 294 | |
| 295 | /* Checks the PMBus voltage monitor for the format used for voltage values */ |
| 296 | static int get_pmbus_multiplier(DEVICE_HANDLE_T dev) |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 297 | { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 298 | u8 mode; |
| 299 | int exponent, multiplier, ret; |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 300 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 301 | ret = I2C_READ(dev, PMBUS_CMD_VOUT_MODE, &mode, sizeof(mode)); |
| 302 | if (ret) { |
| 303 | printf("VID: unable to determine voltage multiplier\n"); |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | /* Upper 3 bits is mode, lower 5 bits is exponent */ |
| 308 | exponent = (int)mode & 0x1F; |
| 309 | mode >>= 5; |
| 310 | switch (mode) { |
| 311 | case 0: |
| 312 | /* Linear, 5 bit twos component exponent */ |
| 313 | if (exponent & 0x10) { |
| 314 | multiplier = 1 << (16 - (exponent & 0xF)); |
| 315 | } else { |
| 316 | /* If exponent is >= 0, then resolution is 1 V! */ |
| 317 | printf(VOUT_WARNING); |
| 318 | multiplier = 1; |
| 319 | } |
| 320 | break; |
| 321 | case 1: |
| 322 | /* VID code identifier */ |
| 323 | printf("VID: custom VID codes are not supported\n"); |
| 324 | multiplier = MV_PER_V; |
| 325 | break; |
| 326 | default: |
| 327 | /* Direct, in mV */ |
| 328 | multiplier = MV_PER_V; |
| 329 | break; |
| 330 | } |
Chuanhua Han | 37c2c5e | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 331 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 332 | debug("VID: calculated multiplier is %d\n", multiplier); |
| 333 | return multiplier; |
| 334 | } |
Chuanhua Han | 37c2c5e | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 335 | #endif |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 336 | |
| 337 | #if defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \ |
| 338 | defined(CONFIG_VOL_MONITOR_LTC3882_READ) |
| 339 | static int read_voltage_from_pmbus(int i2caddress) |
| 340 | { |
| 341 | int ret, multiplier, vout; |
| 342 | u8 channel = PWM_CHANNEL0; |
| 343 | u16 vcode; |
| 344 | DEVICE_HANDLE_T dev; |
| 345 | |
| 346 | /* Open device handle */ |
| 347 | ret = vid_get_device(i2caddress, &dev); |
| 348 | if (ret) |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 349 | return ret; |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 350 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 351 | /* Select the right page */ |
| 352 | ret = I2C_WRITE(dev, PMBUS_CMD_PAGE, &channel, sizeof(channel)); |
Chuanhua Han | 37c2c5e | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 353 | if (ret) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 354 | printf("VID: failed to select VDD page %d\n", channel); |
Chuanhua Han | 37c2c5e | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 355 | return ret; |
| 356 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 357 | |
| 358 | /* VOUT is little endian */ |
| 359 | ret = I2C_READ(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, sizeof(vcode)); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 360 | if (ret) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 361 | printf("VID: failed to read core voltage\n"); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 362 | return ret; |
| 363 | } |
| 364 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 365 | /* Scale down to the real mV */ |
| 366 | multiplier = get_pmbus_multiplier(dev); |
| 367 | vout = (int)vcode; |
| 368 | /* Multiplier 1000 (direct mode) requires no change to convert */ |
| 369 | if (multiplier != MV_PER_V) |
| 370 | vout = DIV_ROUND_UP(vout * MV_PER_V, multiplier); |
| 371 | return vout - board_vdd_drop_compensation(); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 372 | } |
| 373 | #endif |
| 374 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 375 | static int read_voltage(int i2caddress) |
| 376 | { |
| 377 | int voltage_read; |
| 378 | #ifdef CONFIG_VOL_MONITOR_INA220 |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 379 | voltage_read = read_voltage_from_INA220(I2C_VOL_MONITOR_ADDR); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 380 | #elif defined CONFIG_VOL_MONITOR_IR36021_READ |
| 381 | voltage_read = read_voltage_from_IR(i2caddress); |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 382 | #elif defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \ |
| 383 | defined(CONFIG_VOL_MONITOR_LTC3882_READ) |
| 384 | voltage_read = read_voltage_from_pmbus(i2caddress); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 385 | #else |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 386 | voltage_read = -1; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 387 | #endif |
| 388 | return voltage_read; |
| 389 | } |
| 390 | |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 391 | #ifdef CONFIG_VOL_MONITOR_IR36021_SET |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 392 | /* |
| 393 | * We need to calculate how long before the voltage stops to drop |
| 394 | * or increase. It returns with the loop count. Each loop takes |
| 395 | * several readings (WAIT_FOR_ADC) |
| 396 | */ |
| 397 | static int wait_for_new_voltage(int vdd, int i2caddress) |
| 398 | { |
| 399 | int timeout, vdd_current; |
| 400 | |
| 401 | vdd_current = read_voltage(i2caddress); |
| 402 | /* wait until voltage starts to reach the target. Voltage slew |
| 403 | * rates by typical regulators will always lead to stable readings |
| 404 | * within each fairly long ADC interval in comparison to the |
| 405 | * intended voltage delta change until the target voltage is |
| 406 | * reached. The fairly small voltage delta change to any target |
| 407 | * VID voltage also means that this function will always complete |
| 408 | * within few iterations. If the timeout was ever reached, it would |
| 409 | * point to a serious failure in the regulator system. |
| 410 | */ |
| 411 | for (timeout = 0; |
| 412 | abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) && |
| 413 | timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) { |
| 414 | vdd_current = read_voltage(i2caddress); |
| 415 | } |
| 416 | if (timeout >= MAX_LOOP_WAIT_NEW_VOL) { |
| 417 | printf("VID: Voltage adjustment timeout\n"); |
| 418 | return -1; |
| 419 | } |
| 420 | return timeout; |
| 421 | } |
| 422 | |
| 423 | /* |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 424 | * Blocks and reads the VID voltage until it stabilizes, or the |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 425 | * timeout expires |
| 426 | */ |
| 427 | static int wait_for_voltage_stable(int i2caddress) |
| 428 | { |
| 429 | int timeout, vdd_current, vdd; |
| 430 | |
| 431 | vdd = read_voltage(i2caddress); |
| 432 | udelay(NUM_READINGS * WAIT_FOR_ADC); |
| 433 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 434 | vdd_current = read_voltage(i2caddress); |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 435 | /* |
| 436 | * The maximum timeout is |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 437 | * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC |
| 438 | */ |
| 439 | for (timeout = MAX_LOOP_WAIT_VOL_STABLE; |
| 440 | abs(vdd - vdd_current) > ADC_MIN_ACCURACY && |
| 441 | timeout > 0; timeout--) { |
| 442 | vdd = vdd_current; |
| 443 | udelay(NUM_READINGS * WAIT_FOR_ADC); |
| 444 | vdd_current = read_voltage(i2caddress); |
| 445 | } |
| 446 | if (timeout == 0) |
| 447 | return -1; |
| 448 | return vdd_current; |
| 449 | } |
| 450 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 451 | /* Sets the VID voltage using the IR36021 */ |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 452 | static int set_voltage_to_IR(int i2caddress, int vdd) |
| 453 | { |
| 454 | int wait, vdd_last; |
| 455 | int ret; |
| 456 | u8 vid; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 457 | DEVICE_HANDLE_T dev; |
| 458 | |
| 459 | /* Open device handle */ |
| 460 | ret = vid_get_device(i2caddress, &dev); |
| 461 | if (ret) |
| 462 | return ret; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 463 | |
| 464 | /* Compensate for a board specific voltage drop between regulator and |
| 465 | * SoC before converting into an IR VID value |
| 466 | */ |
| 467 | vdd += board_vdd_drop_compensation(); |
Shaohui Xie | 085ac1c | 2016-09-07 17:56:14 +0800 | [diff] [blame] | 468 | #ifdef CONFIG_FSL_LSCH2 |
Shaohui Xie | dd33567 | 2015-11-11 17:58:37 +0800 | [diff] [blame] | 469 | vid = DIV_ROUND_UP(vdd - 265, 5); |
| 470 | #else |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 471 | vid = DIV_ROUND_UP(vdd - 245, 5); |
Shaohui Xie | dd33567 | 2015-11-11 17:58:37 +0800 | [diff] [blame] | 472 | #endif |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 473 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 474 | ret = I2C_WRITE(dev, IR36021_LOOP1_MANUAL_ID_OFFSET, (void *)&vid, |
| 475 | sizeof(vid)); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 476 | if (ret) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 477 | printf("VID: failed to write new voltage\n"); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 478 | return -1; |
| 479 | } |
| 480 | wait = wait_for_new_voltage(vdd, i2caddress); |
| 481 | if (wait < 0) |
| 482 | return -1; |
| 483 | debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC); |
| 484 | |
| 485 | vdd_last = wait_for_voltage_stable(i2caddress); |
| 486 | if (vdd_last < 0) |
| 487 | return -1; |
| 488 | debug("VID: Current voltage is %d mV\n", vdd_last); |
| 489 | return vdd_last; |
| 490 | } |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 491 | #endif |
| 492 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 493 | #if defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \ |
| 494 | defined(CONFIG_VOL_MONITOR_LTC3882_SET) |
| 495 | static int set_voltage_to_pmbus(int i2caddress, int vdd) |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 496 | { |
| 497 | int ret, vdd_last, vdd_target = vdd; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 498 | int count = MAX_LOOP_WAIT_NEW_VOL, temp = 0, multiplier; |
Biwen Li | 71ecd38 | 2020-10-12 20:07:35 +0800 | [diff] [blame] | 499 | unsigned char value; |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 500 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 501 | /* The data to be sent with the PMBus command PAGE_PLUS_WRITE */ |
| 502 | u8 buffer[5] = { 0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND, 0, 0 }; |
| 503 | DEVICE_HANDLE_T dev; |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 504 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 505 | /* Open device handle */ |
| 506 | ret = vid_get_device(i2caddress, &dev); |
| 507 | if (ret) |
| 508 | return ret; |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 509 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 510 | /* Scale up to the proper value for the VOUT command, little endian */ |
| 511 | multiplier = get_pmbus_multiplier(dev); |
| 512 | vdd += board_vdd_drop_compensation(); |
| 513 | if (multiplier != MV_PER_V) |
| 514 | vdd = DIV_ROUND_UP(vdd * multiplier, MV_PER_V); |
| 515 | buffer[3] = vdd & 0xFF; |
| 516 | buffer[4] = (vdd & 0xFF00) >> 8; |
| 517 | |
Biwen Li | 71ecd38 | 2020-10-12 20:07:35 +0800 | [diff] [blame] | 518 | /* Check write protect state */ |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 519 | ret = I2C_READ(dev, PMBUS_CMD_WRITE_PROTECT, (void *)&value, |
| 520 | sizeof(value)); |
Biwen Li | 71ecd38 | 2020-10-12 20:07:35 +0800 | [diff] [blame] | 521 | if (ret) |
| 522 | goto exit; |
| 523 | |
| 524 | if (value != EN_WRITE_ALL_CMD) { |
| 525 | value = EN_WRITE_ALL_CMD; |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 526 | ret = I2C_WRITE(dev, PMBUS_CMD_WRITE_PROTECT, |
Biwen Li | 71ecd38 | 2020-10-12 20:07:35 +0800 | [diff] [blame] | 527 | (void *)&value, sizeof(value)); |
| 528 | if (ret) |
| 529 | goto exit; |
| 530 | } |
| 531 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 532 | /* Write the desired voltage code to the regulator */ |
| 533 | ret = I2C_WRITE(dev, PMBUS_CMD_PAGE_PLUS_WRITE, (void *)&buffer[0], |
| 534 | sizeof(buffer)); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 535 | if (ret) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 536 | printf("VID: I2C failed to write to the voltage regulator\n"); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 537 | return -1; |
| 538 | } |
| 539 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 540 | exit: |
| 541 | /* Wait for the voltage to get to the desired value */ |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 542 | do { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 543 | vdd_last = read_voltage_from_pmbus(i2caddress); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 544 | if (vdd_last < 0) { |
| 545 | printf("VID: Couldn't read sensor abort VID adjust\n"); |
| 546 | return -1; |
| 547 | } |
Priyanka Jain | af89d24 | 2018-10-11 05:11:23 +0000 | [diff] [blame] | 548 | count--; |
| 549 | temp = vdd_last - vdd_target; |
| 550 | } while ((abs(temp) > 2) && (count > 0)); |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 551 | |
| 552 | return vdd_last; |
| 553 | } |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 554 | #endif |
| 555 | |
| 556 | static int set_voltage(int i2caddress, int vdd) |
| 557 | { |
| 558 | int vdd_last = -1; |
| 559 | |
| 560 | #ifdef CONFIG_VOL_MONITOR_IR36021_SET |
| 561 | vdd_last = set_voltage_to_IR(i2caddress, vdd); |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 562 | #elif defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \ |
| 563 | defined(CONFIG_VOL_MONITOR_LTC3882_SET) |
| 564 | vdd_last = set_voltage_to_pmbus(i2caddress, vdd); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 565 | #else |
| 566 | #error Specific voltage monitor must be defined |
| 567 | #endif |
| 568 | return vdd_last; |
| 569 | } |
| 570 | |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 571 | int adjust_vdd(ulong vdd_override) |
| 572 | { |
| 573 | int re_enable = disable_interrupts(); |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 574 | #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 575 | struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR); |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 576 | #else |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 577 | ccsr_gur_t __iomem *gur = |
| 578 | (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR); |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 579 | #endif |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 580 | u8 vid; |
| 581 | u32 fusesr; |
| 582 | int vdd_current, vdd_last, vdd_target; |
| 583 | int ret, i2caddress = I2C_VOL_MONITOR_ADDR; |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 584 | unsigned long vdd_string_override; |
| 585 | char *vdd_string; |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 586 | |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 587 | #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \ |
| 588 | defined(CONFIG_VOL_MONITOR_IR36021_READ) |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 589 | u8 buf; |
| 590 | DEVICE_HANDLE_T dev; |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 591 | #endif |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 592 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 593 | /* |
| 594 | * VID is used according to the table below |
| 595 | * --------------------------------------- |
| 596 | * | DA_V | |
| 597 | * |-------------------------------------| |
| 598 | * | 5b00000 | 5b00001-5b11110 | 5b11111 | |
| 599 | * ---------------+---------+-----------------+---------| |
| 600 | * | D | 5b00000 | NO VID | VID = DA_V | NO VID | |
| 601 | * | A |----------+---------+-----------------+---------| |
| 602 | * | _ | 5b00001 |VID = | VID = |VID = | |
| 603 | * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT| |
| 604 | * | _ | 5b11110 | | | | |
| 605 | * | A |----------+---------+-----------------+---------| |
| 606 | * | L | 5b11111 | No VID | VID = DA_V | NO VID | |
| 607 | * | T | | | | | |
| 608 | * ------------------------------------------------------ |
| 609 | */ |
| 610 | #if defined(CONFIG_FSL_LSCH3) |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 611 | fusesr = in_le32(&gur->dcfg_fusesr); |
| 612 | vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) & |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 613 | FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK; |
| 614 | if (vid == 0 || vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK) { |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 615 | vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) & |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 616 | FSL_CHASSIS3_DCFG_FUSESR_VID_MASK; |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 617 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 618 | #elif defined(CONFIG_FSL_LSCH2) |
| 619 | fusesr = in_be32(&gur->dcfg_fusesr); |
| 620 | vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) & |
| 621 | FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK; |
| 622 | if (vid == 0 || vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK) { |
| 623 | vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) & |
| 624 | FSL_CHASSIS2_DCFG_FUSESR_VID_MASK; |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 625 | } |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 626 | #else |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 627 | fusesr = in_be32(&gur->dcfg_fusesr); |
| 628 | vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) & |
| 629 | FSL_CORENET_DCFG_FUSESR_ALTVID_MASK; |
| 630 | if (vid == 0 || vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK) { |
| 631 | vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) & |
| 632 | FSL_CORENET_DCFG_FUSESR_VID_MASK; |
Priyanka Jain | f9088dd | 2017-01-19 11:12:27 +0530 | [diff] [blame] | 633 | } |
Rajesh Bhagat | 170eecf | 2018-01-17 16:13:05 +0530 | [diff] [blame] | 634 | #endif |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 635 | vdd_target = soc_get_fuse_vid((int)vid); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 636 | |
| 637 | ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR); |
| 638 | if (ret) { |
| 639 | debug("VID: I2C failed to switch channel\n"); |
| 640 | ret = -1; |
| 641 | goto exit; |
| 642 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 643 | |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 644 | #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \ |
| 645 | defined(CONFIG_VOL_MONITOR_IR36021_READ) |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 646 | ret = find_ir_chip_on_i2c(); |
| 647 | if (ret < 0) { |
| 648 | printf("VID: Could not find voltage regulator on I2C.\n"); |
| 649 | ret = -1; |
| 650 | goto exit; |
| 651 | } else { |
| 652 | i2caddress = ret; |
| 653 | debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress); |
| 654 | } |
| 655 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 656 | ret = vid_get_device(i2caddress, &dev); |
| 657 | if (ret) |
| 658 | return ret; |
Chuanhua Han | 37c2c5e | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 659 | |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 660 | /* check IR chip work on Intel mode */ |
| 661 | ret = I2C_READ(dev, IR36021_INTEL_MODE_OFFSET, (void *)&buf, |
| 662 | sizeof(buf)); |
Ying Zhang | 7ad5eff | 2016-01-22 12:15:12 +0800 | [diff] [blame] | 663 | if (ret) { |
| 664 | printf("VID: failed to read IR chip mode.\n"); |
| 665 | ret = -1; |
| 666 | goto exit; |
| 667 | } |
| 668 | if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) { |
| 669 | printf("VID: IR Chip is not used in Intel mode.\n"); |
| 670 | ret = -1; |
| 671 | goto exit; |
| 672 | } |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 673 | #endif |
Ying Zhang | 7ad5eff | 2016-01-22 12:15:12 +0800 | [diff] [blame] | 674 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 675 | /* check override variable for overriding VDD */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 676 | vdd_string = env_get(CONFIG_VID_FLS_ENV); |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 677 | debug("VID: Initial VDD value is %d mV\n", |
| 678 | DIV_ROUND_UP(vdd_target, 10)); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 679 | if (vdd_override == 0 && vdd_string && |
| 680 | !strict_strtoul(vdd_string, 10, &vdd_string_override)) |
| 681 | vdd_override = vdd_string_override; |
| 682 | if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) { |
| 683 | vdd_target = vdd_override * 10; /* convert to 1/10 mV */ |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 684 | debug("VID: VDD override is %lu\n", vdd_override); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 685 | } else if (vdd_override != 0) { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 686 | printf("VID: Invalid VDD value.\n"); |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 687 | } |
| 688 | if (vdd_target == 0) { |
| 689 | debug("VID: VID not used\n"); |
| 690 | ret = 0; |
| 691 | goto exit; |
| 692 | } else { |
| 693 | /* divide and round up by 10 to get a value in mV */ |
| 694 | vdd_target = DIV_ROUND_UP(vdd_target, 10); |
| 695 | debug("VID: vid = %d mV\n", vdd_target); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Read voltage monitor to check real voltage. |
| 700 | */ |
| 701 | vdd_last = read_voltage(i2caddress); |
| 702 | if (vdd_last < 0) { |
| 703 | printf("VID: Couldn't read sensor abort VID adjustment\n"); |
| 704 | ret = -1; |
| 705 | goto exit; |
| 706 | } |
| 707 | vdd_current = vdd_last; |
| 708 | debug("VID: Core voltage is currently at %d mV\n", vdd_last); |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 709 | |
| 710 | #if defined(CONFIG_VOL_MONITOR_LTC3882_SET) || \ |
| 711 | defined(CONFIG_VOL_MONITOR_ISL68233_SET) |
| 712 | /* Set the target voltage */ |
| 713 | vdd_current = set_voltage(i2caddress, vdd_target); |
| 714 | vdd_last = vdd_current; |
| 715 | #else |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 716 | /* |
| 717 | * Adjust voltage to at or one step above target. |
| 718 | * As measurements are less precise than setting the values |
| 719 | * we may run through dummy steps that cancel each other |
| 720 | * when stepping up and then down. |
| 721 | */ |
| 722 | while (vdd_last > 0 && |
| 723 | vdd_last < vdd_target) { |
| 724 | vdd_current += IR_VDD_STEP_UP; |
| 725 | vdd_last = set_voltage(i2caddress, vdd_current); |
| 726 | } |
| 727 | while (vdd_last > 0 && |
| 728 | vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) { |
| 729 | vdd_current -= IR_VDD_STEP_DOWN; |
| 730 | vdd_last = set_voltage(i2caddress, vdd_current); |
| 731 | } |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 732 | #endif |
| 733 | |
| 734 | /* Board specific adjustments */ |
| 735 | if (board_adjust_vdd(vdd_target) < 0) { |
| 736 | ret = -1; |
| 737 | goto exit; |
| 738 | } |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 739 | |
| 740 | if (vdd_last > 0) |
| 741 | printf("VID: Core voltage after adjustment is at %d mV\n", |
| 742 | vdd_last); |
| 743 | else |
| 744 | ret = -1; |
| 745 | exit: |
| 746 | if (re_enable) |
| 747 | enable_interrupts(); |
Wenbin Song | 44ad75c | 2016-03-09 13:38:23 +0800 | [diff] [blame] | 748 | |
| 749 | i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT); |
| 750 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | static int print_vdd(void) |
| 755 | { |
Stephen Carlson | c3301a2 | 2021-02-08 11:11:29 +0100 | [diff] [blame] | 756 | int vdd_last, ret, i2caddress = I2C_VOL_MONITOR_ADDR; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 757 | |
| 758 | ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR); |
| 759 | if (ret) { |
| 760 | debug("VID : I2c failed to switch channel\n"); |
| 761 | return -1; |
| 762 | } |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 763 | #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \ |
| 764 | defined(CONFIG_VOL_MONITOR_IR36021_READ) |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 765 | ret = find_ir_chip_on_i2c(); |
| 766 | if (ret < 0) { |
| 767 | printf("VID: Could not find voltage regulator on I2C.\n"); |
Wenbin Song | 44ad75c | 2016-03-09 13:38:23 +0800 | [diff] [blame] | 768 | goto exit; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 769 | } else { |
| 770 | i2caddress = ret; |
| 771 | debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress); |
| 772 | } |
Rajesh Bhagat | e6cd8d5 | 2018-01-17 16:13:03 +0530 | [diff] [blame] | 773 | #endif |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 774 | |
| 775 | /* |
| 776 | * Read voltage monitor to check real voltage. |
| 777 | */ |
| 778 | vdd_last = read_voltage(i2caddress); |
| 779 | if (vdd_last < 0) { |
| 780 | printf("VID: Couldn't read sensor abort VID adjustment\n"); |
Wenbin Song | 44ad75c | 2016-03-09 13:38:23 +0800 | [diff] [blame] | 781 | goto exit; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 782 | } |
| 783 | printf("VID: Core voltage is at %d mV\n", vdd_last); |
Wenbin Song | 44ad75c | 2016-03-09 13:38:23 +0800 | [diff] [blame] | 784 | exit: |
| 785 | i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT); |
| 786 | |
| 787 | return ret < 0 ? -1 : 0; |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 788 | |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 789 | } |
| 790 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 791 | static int do_vdd_override(struct cmd_tbl *cmdtp, |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 792 | int flag, int argc, |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 793 | char *const argv[]) |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 794 | { |
| 795 | ulong override; |
| 796 | |
| 797 | if (argc < 2) |
| 798 | return CMD_RET_USAGE; |
| 799 | |
| 800 | if (!strict_strtoul(argv[1], 10, &override)) |
| 801 | adjust_vdd(override); /* the value is checked by callee */ |
| 802 | else |
| 803 | return CMD_RET_USAGE; |
| 804 | return 0; |
| 805 | } |
| 806 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 807 | static int do_vdd_read(struct cmd_tbl *cmdtp, int flag, int argc, |
| 808 | char *const argv[]) |
Ying Zhang | 8876a51 | 2014-10-31 18:06:18 +0800 | [diff] [blame] | 809 | { |
| 810 | if (argc < 1) |
| 811 | return CMD_RET_USAGE; |
| 812 | print_vdd(); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | U_BOOT_CMD( |
| 818 | vdd_override, 2, 0, do_vdd_override, |
| 819 | "override VDD", |
| 820 | " - override with the voltage specified in mV, eg. 1050" |
| 821 | ); |
| 822 | |
| 823 | U_BOOT_CMD( |
| 824 | vdd_read, 1, 0, do_vdd_read, |
| 825 | "read VDD", |
| 826 | " - Read the voltage specified in mV" |
| 827 | ) |