From 1536bd5c62126e3dd67c92e53b239e405eddb860 Mon Sep 17 00:00:00 2001 From: Gwendal Grignou Date: Mon, 30 May 2016 18:31:33 -0700 Subject: driver: si114x: Handle overflow properly. Overflow happens when raw value from ADC is greater than 0x7FFF. When it happens, skip the result. BRANCH=ryu,jerry TEST=Without this code, the proximity sensor would show 22000in instead of staying close to 0 when thumb is near sensor. BUG=chrome-os-partner:53851 Change-Id: Id2182acbbf7b00157d9fee5d28bb61df4f166246 Signed-off-by: Gwendal Grignou Reviewed-on: https://chromium-review.googlesource.com/348300 --- driver/als_si114x.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'driver/als_si114x.c') diff --git a/driver/als_si114x.c b/driver/als_si114x.c index b9c6e5ecc5..9d9b87006c 100644 --- a/driver/als_si114x.c +++ b/driver/als_si114x.c @@ -100,21 +100,25 @@ static int si114x_read_results(struct motion_sensor_t *s, int nb) &val); if (ret) break; - /* Add offset, calibration */ - if (val + type_data->offset <= 0) { + if (val == SI114X_OVERFLOW) { + /* overflowing, try next time. */ + return EC_SUCCESS; + } else if (val + type_data->offset <= 0) { + /* No light */ val = 1; - } else if (val != SI114X_OVERFLOW) { + } else { + /* Add offset, calibration */ val += type_data->offset; - /* - * Proxmitiy sensor data is inverse of the distance. - * Return back something proportional to distance, - * we affine with the scale parmeter. - */ - if (s->type == MOTIONSENSE_TYPE_PROX) - val = SI114X_PS_INVERSION(val); - val = val * type_data->scale + - val * type_data->uscale / 10000; } + /* + * Proximity sensor data is inverse of the distance. + * Return back something proportional to distance, + * we correct later with the scale parameter. + */ + if (s->type == MOTIONSENSE_TYPE_PROX) + val = SI114X_PS_INVERSION(val); + val = val * type_data->scale + + val * type_data->uscale / 10000; s->raw_xyz[i] = val; } -- cgit v1.2.1