diff options
author | Denis Brockus <dbrockus@chromium.org> | 2019-08-07 10:52:50 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-08-08 20:32:00 +0000 |
commit | 46ebe63f911331c2db73b65e90f52c0a76476443 (patch) | |
tree | 6eca33272e452fb5ac5977c2e2a281327da5efc8 /driver | |
parent | 4f409fcad25581806c86785ce167087ca658ef0a (diff) | |
download | chrome-ec-46ebe63f911331c2db73b65e90f52c0a76476443.tar.gz |
isl9241: add charger_get_vbus_voltage
BUG=b:138600692
BRANCH=none
TEST=make buildall -j
Change-Id: Idd20b1833945e37a84db4e7c444d8974f6059f83
Signed-off-by: Denis Brockus <dbrockus@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1742532
Commit-Queue: Edward Hill <ecgh@chromium.org>
Reviewed-by: Edward Hill <ecgh@chromium.org>
Diffstat (limited to 'driver')
-rw-r--r-- | driver/charger/isl9241.c | 46 | ||||
-rw-r--r-- | driver/charger/isl9241.h | 5 |
2 files changed, 51 insertions, 0 deletions
diff --git a/driver/charger/isl9241.c b/driver/charger/isl9241.c index 4db66e0b52..1312388b5a 100644 --- a/driver/charger/isl9241.c +++ b/driver/charger/isl9241.c @@ -232,6 +232,52 @@ int charger_set_voltage(int voltage) return isl9241_write(ISL9241_REG_MAX_SYSTEM_VOLTAGE, voltage); } +int charger_get_vbus_voltage(int port) +{ + int adc_val = 0; + int ctl3_val; + int rv; + + /* Get current Control3 value */ + rv = isl9241_read(ISL9241_REG_CONTROL3, &ctl3_val); + if (rv) + goto error; + + /* Enable ADC */ + if (!(ctl3_val & ISL9241_CONTROL3_ENABLE_ADC)) { + rv = isl9241_write(ISL9241_REG_CONTROL3, + ctl3_val | ISL9241_CONTROL3_ENABLE_ADC); + if (rv) + goto error; + } + + /* Read voltage ADC value */ + rv = isl9241_read(ISL9241_REG_VIN_ADC_RESULTS, &adc_val); + if (rv) + goto error_restore_ctl3; + + /* + * Adjust adc_val + * + * raw adc_val has VIN_ADC in bits [13:6], so shift this down + * this puts adc_val in the range of 0..255, which maps to 0..24.48V + * each step in adc_val is 96mv + */ + adc_val >>= ISL9241_VIN_ADC_BIT_OFFSET; + adc_val *= ISL9241_VIN_ADC_STEP_MV; + +error_restore_ctl3: + /* Restore Control3 value */ + if (!(ctl3_val & ISL9241_CONTROL3_ENABLE_ADC)) + (void)isl9241_write(ISL9241_REG_CONTROL3, ctl3_val); + +error: + if (rv) + CPRINTF("Could not read VBUS ADC! Error: %d\n", rv); + + return adc_val; +} + int charger_post_init(void) { return EC_SUCCESS; diff --git a/driver/charger/isl9241.h b/driver/charger/isl9241.h index 7fe3f5e579..b14c15f780 100644 --- a/driver/charger/isl9241.h +++ b/driver/charger/isl9241.h @@ -86,6 +86,8 @@ #define ISL9241_CONTROL3_ACLIM_RELOAD BIT(14) /* 2: Digital Reset (0 - Idle, 1 - Reset */ #define ISL9241_CONTROL3_DIGITAL_RESET BIT(2) +/* 0: Enable ADC (0 - Active when charging, 1 - Active always) */ +#define ISL9241_CONTROL3_ENABLE_ADC BIT(0) /* Indicates various charger status */ #define ISL9241_REG_INFORMATION2 0x4D @@ -112,4 +114,7 @@ #define ISL9241_REG_MANUFACTURER_ID 0xFE #define ISL9241_REG_DEVICE_ID 0xFF +#define ISL9241_VIN_ADC_BIT_OFFSET 6 +#define ISL9241_VIN_ADC_STEP_MV 96 + #endif /* __CROS_EC_ISL9241_H */ |