diff options
author | Philip Chen <philipchen@google.com> | 2017-10-21 00:55:20 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-11-01 14:06:32 -0700 |
commit | bca028445de353ae38c7956234b3d530a70762a8 (patch) | |
tree | 7835bf0cf808f5ce3ab6680aa7709a1af7a443fa /driver/charger | |
parent | 73d0ed0b794b13ceff5620b5c15ba7a3760d1f35 (diff) | |
download | chrome-ec-bca028445de353ae38c7956234b3d530a70762a8.tar.gz |
charger/rt946x: Implement VBUS measurement
BUG=b:67991345
BRANCH=none
TEST=Plug in guppy on Scarlet rev2, 'ectool usbpdpower' on console,
and see VBUS is measured as 4975mV
Change-Id: I960290745a343ef597fa32575491d936269ae628
Signed-off-by: Philip Chen <philipchen@google.com>
Reviewed-on: https://chromium-review.googlesource.com/732084
Commit-Ready: Philip Chen <philipchen@chromium.org>
Tested-by: Philip Chen <philipchen@chromium.org>
Reviewed-by: Shawn N <shawnn@chromium.org>
Diffstat (limited to 'driver/charger')
-rw-r--r-- | driver/charger/rt946x.c | 39 | ||||
-rw-r--r-- | driver/charger/rt946x.h | 9 |
2 files changed, 47 insertions, 1 deletions
diff --git a/driver/charger/rt946x.c b/driver/charger/rt946x.c index 10f1decb7c..60322f08e6 100644 --- a/driver/charger/rt946x.c +++ b/driver/charger/rt946x.c @@ -60,6 +60,11 @@ enum rt946x_ilmtsel { RT946X_ILMTSEL_LOWER_LEVEL, /* lower of above two */ }; +enum rt946x_adc_in_sel { + RT946X_ADC_VBUS_DIV5 = 1, + RT946X_ADC_VBUS_DIV2, +}; + enum rt946x_irq { RT946X_IRQ_CHGSTATC = 0, RT946X_IRQ_CHGFAULT, @@ -566,7 +571,39 @@ int charger_discharge_on_ac(int enable) int charger_get_vbus_voltage(int port) { - return EC_ERROR_UNIMPLEMENTED; + int val; + int vbus_mv; + int retries = 40; + + /* Set VBUS as ADC input */ + rt946x_update_bits(RT946X_REG_CHGADC, RT946X_MASK_ADC_IN_SEL, + RT946X_ADC_VBUS_DIV5 << RT946X_SHIFT_ADC_IN_SEL); + + /* Start ADC conversion */ + rt946x_set_bit(RT946X_REG_CHGADC, RT946X_MASK_ADC_START); + + /* + * Wait up to 200ms for the conversion to finish. + * + * TODO(chromium:780364): The 200ms delay might impact + * charge ramp algorithm. + */ + while (--retries) { + rt946x_read8(RT946X_REG_CHGSTAT, &val); + if (val & RT946X_MASK_ADC_STAT) + break; + msleep(5); + } + /* ADC timeout */ + if (!retries) + return -1; + + /* Read measured results */ + rt946x_read8(RT946X_REG_ADCDATAL, &vbus_mv); + rt946x_read8(RT946X_REG_ADCDATAH, &val); + vbus_mv |= (val << 8); + + return (vbus_mv * 25); } /* Setup sourcing current to prevent overload */ diff --git a/driver/charger/rt946x.h b/driver/charger/rt946x.h index dc25222700..7bf3a915f2 100644 --- a/driver/charger/rt946x.h +++ b/driver/charger/rt946x.h @@ -196,6 +196,13 @@ #define RT946X_MASK_JEITA_EN (1 << RT946X_SHIFT_JEITA_EN) +/* ========== CHGADC 0x11 ============ */ +#define RT946X_SHIFT_ADC_IN_SEL 4 +#define RT946X_SHIFT_ADC_START 0 + +#define RT946X_MASK_ADC_IN_SEL 0xF0 +#define RT946X_MASK_ADC_START (1 << RT946X_SHIFT_ADC_START) + /* ========== CHGDPDM1 0x12 ============ */ #define RT946X_MASK_USBCHGEN (1 << 7) @@ -212,8 +219,10 @@ /* ========== CHGSTAT 0x42 ============ */ #define RT946X_SHIFT_CHG_STAT 6 +#define RT946X_SHIFT_ADC_STAT 0 #define RT946X_MASK_CHG_STAT 0xC0 +#define RT946X_MASK_ADC_STAT (1 << RT946X_SHIFT_ADC_STAT) /* ========== CHGNTC 0x43 ============ */ #define RT946X_SHIFT_BATNTC_FAULT 4 |