From 46ebe63f911331c2db73b65e90f52c0a76476443 Mon Sep 17 00:00:00 2001 From: Denis Brockus Date: Wed, 7 Aug 2019 10:52:50 -0600 Subject: isl9241: add charger_get_vbus_voltage BUG=b:138600692 BRANCH=none TEST=make buildall -j Change-Id: Idd20b1833945e37a84db4e7c444d8974f6059f83 Signed-off-by: Denis Brockus Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1742532 Commit-Queue: Edward Hill Reviewed-by: Edward Hill --- driver/charger/isl9241.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ driver/charger/isl9241.h | 5 +++++ 2 files changed, 51 insertions(+) (limited to 'driver/charger') 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 */ -- cgit v1.2.1