diff options
author | Marco Chen <marcochen@chromium.org> | 2017-11-16 14:12:43 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-11-26 05:21:39 -0800 |
commit | a2e2be193a46826fa6af7fbaab0e567e248e40b4 (patch) | |
tree | 45081aa3e8896b9cbd1764fbf7ca831b31c499d0 | |
parent | f326fb05b564b0975a9870b2831f9372539202db (diff) | |
download | chrome-ec-a2e2be193a46826fa6af7fbaab0e567e248e40b4.tar.gz |
OPT3001: Support MOTIONSENSE_CMD_SENSOR_[OFFSET|RANGE] for calibration.
1. The original driver of OPT3001
a. didn't support to process the command of offset.
b. implemented command of range to setter/getter of Range Number Field
of chip.
But reffering to cros_ec_light_prox.c from linux kernel side, these two
commands are actually leveraged for in_illuminance_calib[bias|scale]
and these calibration factors should be applied into raw lux value read
from the chip.
2. Move ALS in Poppy / Soraka boards from ALS_TASK to MOTIONSENSE_TASK.
3. Mofify parameters of ALS in Reef board in order to adapt changes
here.
BUG=b:69236269
BRANCH=none
TEST=Manually test on the DUT.
Change-Id: Ic3b593feb3e4bc6da0bada6b5d614975f0cf2280
Signed-off-by: Marco Chen <marcochen@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/774341
Reviewed-by: Shawn N <shawnn@chromium.org>
-rw-r--r-- | board/poppy/board.c | 58 | ||||
-rw-r--r-- | board/poppy/board.h | 7 | ||||
-rw-r--r-- | board/poppy/ec.tasklist | 1 | ||||
-rw-r--r-- | board/reef/board.c | 6 | ||||
-rw-r--r-- | driver/als_opt3001.c | 62 | ||||
-rw-r--r-- | driver/als_opt3001.h | 23 |
6 files changed, 87 insertions, 70 deletions
diff --git a/board/poppy/board.c b/board/poppy/board.c index 6d87057ee2..09327c9801 100644 --- a/board/poppy/board.c +++ b/board/poppy/board.c @@ -534,17 +534,6 @@ const struct temp_sensor_t temp_sensors[] = { }; BUILD_ASSERT(ARRAY_SIZE(temp_sensors) == TEMP_SENSOR_COUNT); -/* ALS instances. Must be in same order as enum als_id. */ -struct als_t als[] = { - /* - * attenuation_factor is set to 1 because there would be a calibration - * in iio framework of kernel which would be configured in factory - * flow. - */ - {"TI", opt3001_init, opt3001_read_lux, 1}, -}; -BUILD_ASSERT(ARRAY_SIZE(als) == ALS_COUNT); - const struct button_config *recovery_buttons[] = { &buttons[BUTTON_VOLUME_DOWN], &buttons[BUTTON_VOLUME_UP], @@ -853,6 +842,11 @@ int board_get_version(void) static struct mutex g_lid_mutex; static struct bmi160_drv_data_t g_bmi160_data; +static struct opt3001_drv_data_t g_opt3001_data = { + .scale = 1, + .uscale = 0, + .offset = 0, +}; /* Matrix to rotate accelrator into standard reference frame */ const matrix_3x3_t mag_standard_ref = { @@ -999,9 +993,51 @@ struct motion_sensor_t motion_sensors[] = { }, }, }, + [LID_ALS] = { + .name = "Light", + .active_mask = SENSOR_ACTIVE_S0, + .chip = MOTIONSENSE_CHIP_OPT3001, + .type = MOTIONSENSE_TYPE_LIGHT, + .location = MOTIONSENSE_LOC_LID, + .drv = &opt3001_drv, + .drv_data = &g_opt3001_data, + .port = I2C_PORT_ALS, + .addr = OPT3001_I2C_ADDR, + .rot_standard_ref = NULL, + .default_range = 0x10000, /* scale = 1; uscale = 0 */ + .min_frequency = OPT3001_LIGHT_MIN_FREQ, + .max_frequency = OPT3001_LIGHT_MAX_FREQ, + .config = { + /* AP: by default shutdown all sensors */ + [SENSOR_CONFIG_AP] = { + .odr = 0, + .ec_rate = 0, + }, + [SENSOR_CONFIG_EC_S0] = { + .odr = 1000, + .ec_rate = 0, + }, + /* Sensor off in S3/S5 */ + [SENSOR_CONFIG_EC_S3] = { + .odr = 0, + .ec_rate = 0, + }, + /* Sensor off in S3/S5 */ + [SENSOR_CONFIG_EC_S5] = { + .odr = 0, + .ec_rate = 0, + }, + }, + }, }; const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors); +/* ALS instances when LPC mapping is needed. Each entry directs to a sensor. */ +const struct motion_sensor_t *motion_als_sensors[] = { + &motion_sensors[LID_ALS], +}; +BUILD_ASSERT(ARRAY_SIZE(motion_als_sensors) == ALS_COUNT); + #ifdef BOARD_SORAKA static void board_sensor_init(void) { diff --git a/board/poppy/board.h b/board/poppy/board.h index 245f8027d0..95050f8700 100644 --- a/board/poppy/board.h +++ b/board/poppy/board.h @@ -102,6 +102,7 @@ /* Sensor */ #define CONFIG_ALS #define CONFIG_ALS_OPT3001 +#define ALS_COUNT 1 #define OPT3001_I2C_ADDR OPT3001_I2C_ADDR1 #define CONFIG_TEMP_SENSOR #define CONFIG_TEMP_SENSOR_BD99992GW @@ -208,11 +209,6 @@ enum temp_sensor_id { TEMP_SENSOR_COUNT }; -enum als_id { - ALS_OPT3001, - ALS_COUNT -}; - /* * Motion sensors: * When reading through IO memory is set up for sensors (LPC is used), @@ -223,6 +219,7 @@ enum sensor_id { LID_ACCEL = 0, LID_GYRO, LID_MAG, + LID_ALS, }; enum adc_channel { diff --git a/board/poppy/ec.tasklist b/board/poppy/ec.tasklist index 376ac1965e..53c97a0cb5 100644 --- a/board/poppy/ec.tasklist +++ b/board/poppy/ec.tasklist @@ -22,7 +22,6 @@ #define CONFIG_TASK_LIST \ TASK_ALWAYS(HOOKS, hook_task, NULL, LARGER_TASK_STACK_SIZE) \ - TASK_ALWAYS(ALS, als_task, NULL, TASK_STACK_SIZE) \ TASK_ALWAYS(USB_CHG_P0, usb_charger_task, NULL, TASK_STACK_SIZE) \ TASK_ALWAYS(USB_CHG_P1, usb_charger_task, NULL, TASK_STACK_SIZE) \ TASK_ALWAYS(CHARGER, charger_task, NULL, LARGER_TASK_STACK_SIZE) \ diff --git a/board/reef/board.c b/board/reef/board.c index 43bee25318..fa16408e21 100644 --- a/board/reef/board.c +++ b/board/reef/board.c @@ -783,7 +783,9 @@ static struct kionix_accel_data g_kx022_data; static struct bmi160_drv_data_t g_bmi160_data; static struct bmp280_drv_data_t bmp280_drv_data; static struct opt3001_drv_data_t g_opt3001_data = { - .attenuation = 5, + .scale = 1, + .uscale = 0, + .offset = 0, }; /* FIXME(dhendrix): Copied from Amenia, probably need to tweak for Reef */ @@ -990,7 +992,7 @@ struct motion_sensor_t motion_sensors[] = { .port = I2C_PORT_ALS, .addr = OPT3001_I2C_ADDR1, .rot_standard_ref = NULL, - .default_range = OPT3001_RANGE_AUTOMATIC_FULL_SCALE, + .default_range = 0x10000, /* scale = 1; uscale = 0 */ .min_frequency = OPT3001_LIGHT_MIN_FREQ, .max_frequency = OPT3001_LIGHT_MAX_FREQ, .config = { diff --git a/driver/als_opt3001.c b/driver/als_opt3001.c index 7e080e8c89..b3ab198496 100644 --- a/driver/als_opt3001.c +++ b/driver/als_opt3001.c @@ -5,6 +5,7 @@ * TI OPT3001 light sensor driver */ +#include "common.h" #include "driver/als_opt3001.h" #include "i2c.h" @@ -144,17 +145,14 @@ int opt3001_read_lux(const struct motion_sensor_t *s, vector_3_t v) return ret; /* - * The default power-on values will give 12 bits of precision: - * 0x0000-0x0fff indicates 0 to 1310.40 lux. We multiply the sensor - * value by a scaling factor to account for attenuation by glass, - * tinting, etc. - */ - - /* * lux = 2EXP[3:0] × R[11:0] / 100 */ - v[0] = ((1 << ((data & 0xF000) >> 12)) * (data & 0x0FFF) * - drv_data->attenuation) / 100; + data = (1 << (data >> 12)) * (data & 0x0FFF) / 100; + data += drv_data->offset; + if (data < 0) + data = 1; + + v[0] = data * drv_data->scale + data * drv_data->uscale / 10000; v[1] = 0; v[2] = 0; @@ -164,32 +162,19 @@ int opt3001_read_lux(const struct motion_sensor_t *s, vector_3_t v) */ if (v[0] == drv_data->last_value) return EC_ERROR_UNCHANGED; - else + else { + drv_data->last_value = v[0]; return EC_SUCCESS; + } } static int opt3001_set_range(const struct motion_sensor_t *s, int range, int rnd) { - int rv; - int reg; struct opt3001_drv_data_t *drv_data = OPT3001_GET_DATA(s); - if (range < 0 || range > OPT3001_RANGE_AUTOMATIC_FULL_SCALE) - return EC_ERROR_INVAL; - - rv = opt3001_i2c_read(s->port, s->addr, OPT3001_REG_CONFIGURE, ®); - if (rv) - return rv; - - rv = opt3001_i2c_write(s->port, s->addr, OPT3001_REG_CONFIGURE, - (reg & OPT3001_RANGE_MASK) | - (range << OPT3001_RANGE_OFFSET)); - if (rv) - return rv; - - drv_data->range = range; - + drv_data->scale = range >> 16; + drv_data->uscale = range & 0xffff; return EC_SUCCESS; } @@ -197,7 +182,7 @@ static int opt3001_get_range(const struct motion_sensor_t *s) { struct opt3001_drv_data_t *drv_data = OPT3001_GET_DATA(s); - return drv_data->range; + return (drv_data->scale << 16) | (drv_data->uscale); } static int opt3001_set_data_rate(const struct motion_sensor_t *s, @@ -248,14 +233,23 @@ static int opt3001_set_offset(const struct motion_sensor_t *s, const int16_t *offset, int16_t temp) { - return EC_RES_INVALID_COMMAND; + struct opt3001_drv_data_t *drv_data = OPT3001_GET_DATA(s); + + drv_data->offset = offset[X]; + return EC_SUCCESS; } static int opt3001_get_offset(const struct motion_sensor_t *s, int16_t *offset, int16_t *temp) { - return EC_RES_INVALID_COMMAND; + struct opt3001_drv_data_t *drv_data = OPT3001_GET_DATA(s); + + offset[X] = drv_data->offset; + offset[Y] = 0; + offset[Z] = 0; + *temp = EC_MOTION_SENSE_INVALID_CALIB_TEMP; + return EC_SUCCESS; } /** * Initialise OPT3001 light sensor. @@ -278,11 +272,15 @@ static int opt3001_init(const struct motion_sensor_t *s) return EC_ERROR_ACCESS_DENIED; /* + * [15-12]: 1100b Automatic full-scale setting mode * [11] : 1b Conversion time 800ms * [4] : 1b Latched window-style comparison operation */ - opt3001_i2c_write(s->port, s->addr, OPT3001_REG_CONFIGURE, 0x810); - return opt3001_set_range(s, s->default_range, 0); + opt3001_i2c_write(s->port, s->addr, OPT3001_REG_CONFIGURE, 0xC810); + + opt3001_set_range(s, s->default_range, 0); + + return EC_SUCCESS; } const struct accelgyro_drv opt3001_drv = { diff --git a/driver/als_opt3001.h b/driver/als_opt3001.h index 9850dfe723..eeb05f3a10 100644 --- a/driver/als_opt3001.h +++ b/driver/als_opt3001.h @@ -44,30 +44,15 @@ enum opt3001_mode { int opt3001_init(void); int opt3001_read_lux(int *lux, int af); #else -/* OPT3001 Full-Scale Range */ -enum opt3001_range { - OPT3001_RANGE_40P95_LUX, - OPT3001_RANGE_81P90_LUX, - OPT3001_RANGE_163P80_LUX, - OPT3001_RANGE_327P60_LUX, - OPT3001_RANGE_655P20_LUX, - OPT3001_RANGE_1310P40_LUX, - OPT3001_RANGE_2620P80_LUX, - OPT3001_RANGE_5241P60_LUX, - OPT3001_RANGE_10483P20_LUX, - OPT3001_RANGE_20966P40_LUX, - OPT3001_RANGE_41932P80_LUX, - OPT3001_RANGE_83865P60_LUX, - OPT3001_RANGE_AUTOMATIC_FULL_SCALE, -}; - #define OPT3001_GET_DATA(_s) ((struct opt3001_drv_data_t *)(_s)->drv_data) struct opt3001_drv_data_t { - enum opt3001_range range; int rate; int last_value; - int attenuation; + /* the coef is scale.uscale */ + int16_t scale; + uint16_t uscale; + int16_t offset; }; extern const struct accelgyro_drv opt3001_drv; |