summaryrefslogtreecommitdiff
path: root/driver/charger/rt946x.c
diff options
context:
space:
mode:
authorPhilip Chen <philipchen@google.com>2018-03-08 20:29:25 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-03-09 20:05:18 -0800
commita4621c5e6489042a606cc0d084c62e75e3667410 (patch)
tree04e1c258e0a30ffcf3cfaaca96a828280bae0b86 /driver/charger/rt946x.c
parentef4e70174ac2797f0c02753685b35d038a317a6a (diff)
downloadchrome-ec-a4621c5e6489042a606cc0d084c62e75e3667410.tar.gz
charger/rt946x: Limit ADC timeout to 50ms
In practice, ADC conversion rarely takes more than 35ms. However, according to the datasheet, ADC conversion may take up to 200ms. But we can't wait for that long, otherwise host command would time out. So here we set ADC timeout as 50ms. If ADC times out, we just return the last read vbus_mv. BUG=b:70641844, chromium:780364 BRANCH=scarlet TEST=test 'ectool usbpdpower' for 50k cycles w/o seeing host command timeout Change-Id: I09c3abf729e96b113f7a0f64a67cd35906da9e3e Signed-off-by: Philip Chen <philipchen@google.com> Reviewed-on: https://chromium-review.googlesource.com/956900 Commit-Ready: Philip Chen <philipchen@chromium.org> Tested-by: Philip Chen <philipchen@chromium.org> Reviewed-by: Alexandru M Stan <amstan@chromium.org> Reviewed-by: Brian Norris <briannorris@chromium.org>
Diffstat (limited to 'driver/charger/rt946x.c')
-rw-r--r--driver/charger/rt946x.c30
1 files changed, 17 insertions, 13 deletions
diff --git a/driver/charger/rt946x.c b/driver/charger/rt946x.c
index ebc48cec8a..5c1acfac79 100644
--- a/driver/charger/rt946x.c
+++ b/driver/charger/rt946x.c
@@ -615,8 +615,8 @@ int charger_discharge_on_ac(int enable)
int charger_get_vbus_voltage(int port)
{
int val;
- int vbus_mv;
- int retries = 40;
+ static int vbus_mv;
+ int retries = 10;
/* Set VBUS as ADC input */
rt946x_update_bits(RT946X_REG_CHGADC, RT946X_MASK_ADC_IN_SEL,
@@ -626,10 +626,14 @@ int charger_get_vbus_voltage(int port)
rt946x_set_bit(RT946X_REG_CHGADC, RT946X_MASK_ADC_START);
/*
- * Wait up to 200ms for the conversion to finish.
+ * In practice, ADC conversion rarely takes more than 35ms.
+ * However, according to the datasheet, ADC conversion may take
+ * up to 200ms. But we can't wait for that long, otherwise
+ * host command would time out. So here we set ADC timeout as 50ms.
+ * If ADC times out, we just return the last read vbus_mv.
*
- * TODO(chromium:780364): The 200ms delay might impact
- * charge ramp algorithm.
+ * TODO(chromium:820335): We may handle this more gracefully with
+ * EC_RES_IN_PROGRESS.
*/
while (--retries) {
rt946x_read8(RT946X_REG_CHGSTAT, &val);
@@ -637,16 +641,16 @@ int charger_get_vbus_voltage(int port)
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);
+ if (retries) {
+ /* Read measured results if ADC finishes in time. */
+ rt946x_read8(RT946X_REG_ADCDATAL, &vbus_mv);
+ rt946x_read8(RT946X_REG_ADCDATAH, &val);
+ vbus_mv |= (val << 8);
+ vbus_mv *= 25;
+ }
- return (vbus_mv * 25);
+ return vbus_mv;
}
/* Setup sourcing current to prevent overload */