diff options
author | Owen_Ou <Owen_Ou@compal.corp-partner.google.com> | 2021-12-20 14:12:05 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2022-01-25 21:13:15 +0000 |
commit | bb8ed5538b6515346fe9944d5251b961e1210862 (patch) | |
tree | deb123331ae5c0a54e9e369f9f930b5ea9082f71 /driver | |
parent | 4e542fb4a8eb37c7c92b7cebdb542c83fcff3105 (diff) | |
download | chrome-ec-bb8ed5538b6515346fe9944d5251b961e1210862.tar.gz |
driver/als_cm32183: Add ALS source
add new ALS source.
BUG=b:208721153
BRANCH=none
TEST=rework on Brya, can read data by ectool I2C command
read lux (console) : accelread 4
Current data 4: 154 0 0
Last calib. data 4: 154 0 0
als info (ectool) : ectool motionsense info 4
ectool motionsense
sensor 4: 154 0 0
Signed-off-by: Owen_Ou <Owen_Ou@compal.corp-partner.google.com>
Change-Id: I67f1184cefed257956cfe0f988c87e4f81db3fc7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3350267
Tested-by: Owen Ou <owen_ou@compal.corp-partner.google.com>
Auto-Submit: Owen Ou <owen_ou@compal.corp-partner.google.com>
Reviewed-by: caveh jalali <caveh@chromium.org>
Commit-Queue: caveh jalali <caveh@chromium.org>
Diffstat (limited to 'driver')
-rw-r--r-- | driver/als_cm32183.c | 151 | ||||
-rw-r--r-- | driver/als_cm32183.h | 103 | ||||
-rw-r--r-- | driver/build.mk | 1 |
3 files changed, 255 insertions, 0 deletions
diff --git a/driver/als_cm32183.c b/driver/als_cm32183.c new file mode 100644 index 0000000000..e5efa29f03 --- /dev/null +++ b/driver/als_cm32183.c @@ -0,0 +1,151 @@ +/* Copyright 2022 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * CAPELLA CM32183 light sensor driver + */ + +#include "common.h" +#include "driver/als_cm32183.h" +#include "i2c.h" +#include "accelgyro.h" +#include "math_util.h" + +struct cm32183_drv_data { + int rate; + int last_value; + /* the coefficient is scale.uscale */ + int16_t scale; + uint16_t uscale; + int16_t offset; +}; + +#define CM32183_GET_DATA(_s) ((struct cm32183_drv_data *)(_s)->drv_data) + +/* + * Read CM32183 light sensor data. + */ +static int cm32183_read_lux(int *lux) +{ + int ret; + int data; + + ret = i2c_read16(I2C_PORT_SENSOR, CM32183_I2C_ADDR, + CM32183_REG_ALS_RESULT, &data); + + if (ret) + return ret; + + /* + * lux = data * 0.016 + */ + *lux = (data * 16)/1000; + + return EC_SUCCESS; +} + +/* + * Read data from CM32183 light sensor, and transfer unit into lux. + */ +static int cm32183_read(const struct motion_sensor_t *s, intv3_t v) +{ + struct cm32183_drv_data *drv_data = CM32183_GET_DATA(s); + int ret; + int lux_data; + + ret = cm32183_read_lux(&lux_data); + + if (ret) + return ret; + + lux_data += drv_data->offset; + + v[0] = lux_data; + v[1] = 0; + v[2] = 0; + + /* + * Return an error when nothing change to prevent filling the + * fifo with useless data. + */ + if (v[0] == drv_data->last_value) + return EC_ERROR_UNCHANGED; + + drv_data->last_value = v[0]; + return EC_SUCCESS; +} + +static int cm32183_set_range(struct motion_sensor_t *s, int range, + int rnd) +{ + return EC_SUCCESS; +} + +static int cm32183_set_data_rate(const struct motion_sensor_t *s, + int rate, int roundup) +{ + CM32183_GET_DATA(s)->rate = rate; + return EC_SUCCESS; +} + +static int cm32183_get_data_rate(const struct motion_sensor_t *s) +{ + return CM32183_GET_DATA(s)->rate; +} + +static int cm32183_set_offset(const struct motion_sensor_t *s, + const int16_t *offset, int16_t temp) +{ + /* TODO: check calibration method */ + return EC_SUCCESS; +} + +static int cm32183_get_offset(const struct motion_sensor_t *s, + int16_t *offset, int16_t *temp) +{ + *offset = CM32183_GET_DATA(s)->offset; + return EC_SUCCESS; +} + +#ifdef CONFIG_ACCEL_INTERRUPTS +static int cm32183_irq_handler(struct motion_sensor_t *s, uint32_t *event) +{ + return cm32183_read(s, s->xyz); +} +#endif + +/** + * Initialise CM32183 light sensor. + */ +static int cm32183_init(struct motion_sensor_t *s) +{ + int ret; + int data; + + ret = i2c_write16(s->port, s->i2c_spi_addr_flags, + CM32183_REG_CONFIGURE, CM32183_REG_CONFIGURE_CH_EN); + + if (ret) + return ret; + + ret = i2c_read16(s->port, s->i2c_spi_addr_flags, + CM32183_REG_ALS_RESULT, &data); + + if (ret) + return ret; + + return sensor_init_done(s); +} + +const struct accelgyro_drv cm32183_drv = { + .init = cm32183_init, + .read = cm32183_read, + .set_range = cm32183_set_range, + .set_offset = cm32183_set_offset, + .get_offset = cm32183_get_offset, + .set_data_rate = cm32183_set_data_rate, + .get_data_rate = cm32183_get_data_rate, +#ifdef CONFIG_ACCEL_INTERRUPTS + .irq_handler = cm32183_irq_handler, +#endif +}; diff --git a/driver/als_cm32183.h b/driver/als_cm32183.h new file mode 100644 index 0000000000..457a9e02ee --- /dev/null +++ b/driver/als_cm32183.h @@ -0,0 +1,103 @@ +/* Copyright 2022 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * CAPELLA CM32183 light sensor driver + */ + +#ifndef __CROS_EC_ALS_CM32183_H +#define __CROS_EC_ALS_CM32183_H + +/* I2C interface */ +#define CM32183_I2C_ADDR (0x29 | I2C_FLAG_BIG_ENDIAN) + +/* CM32183 registers */ +#define CM32183_REG_CONFIGURE 0x00 + +#define CM32183_REG_CONFIGURE_CH_EN 0x0004 + +/* ALS Sensitivity_mode (BIT 12:11) */ +#define CM32183_REG_CONFIGURE_ALS_SENSITIVITY_MASK GENMASK(12, 11) +#define CM32183_REG_CONFIGURE_ALS_SENSITIVITY_SHIFT 11 +#define CM32183_REG_CONFIGURE_ALS_SENSITIVITY_1 0 +#define CM32183_REG_CONFIGURE_ALS_SENSITIVITY_2 1 +#define CM32183_REG_CONFIGURE_ALS_SENSITIVITY_1_DIV_8 2 +#define CM32183_REG_CONFIGURE_ALS_SENSITIVITY_1_DIV_4 3 + +/* + * Gain mode + * 0 Gain*1 + * 1 Gain*2 (bit 10) + */ +#define CM32183_REG_CONFIGURE_GAIN BIT(10) + +/* + * ALS integration time setting which represents how long + * ALS can update the readout value (BIT 9:6) + * BIT 9:6 function + * 0000 100ms + * 0001 200ms + * 0010 400ms + * 0011 800ms + */ +#define CM32183_REG_CONFIGURE_ALS_INTEGRATION_MASK GENMASK(9, 6) +#define CM32183_REG_CONFIGURE_ALS_INTEGRATION_SHIFT 6 +#define CM32183_REG_CONFIGURE_ALS_INTEGRATION_SET100MS 0 +#define CM32183_REG_CONFIGURE_ALS_INTEGRATION_SET200MS 1 +#define CM32183_REG_CONFIGURE_ALS_INTEGRATION_SET400MS 2 +#define CM32183_REG_CONFIGURE_ALS_INTEGRATION_SET800MS 3 + +/* + * ALS interrupt persistence setting.The interrupt pin is + * triggered while sensor reading is out of threshold windows + * after consecutive number of measurement cycle. (BIT 5:4) + * BIT 5:4 measurement cycle + * 00 1 + * 01 2 + * 10 4 + * 11 8 + */ +#define CM32183_REG_CONFIGURE_MEASUREMENT_MASK GENMASK(5, 4) +#define CM32183_REG_CONFIGURE_MEASUREMENT_SHIFT 4 +#define CM32183_REG_CONFIGURE_MEASUREMENT_CYCLE_1 0 +#define CM32183_REG_CONFIGURE_MEASUREMENT_CYCLE_2 1 +#define CM32183_REG_CONFIGURE_MEASUREMENT_CYCLE_4 2 +#define CM32183_REG_CONFIGURE_MEASUREMENT_CYCLE_8 3 + +/* + * channel selection of interrupt (BIT 3) + * 0 ALS CH interrupt + * 1 White CH interrupt + */ +#define CM32183_REG_CONFIGURE_CHANNEL_SELECTION BIT(3) + +/* + * Channel enable (BIT 2) + * 0 ALS CH enable only + * 1 ALS & White CH enable + */ +#define CM32183_REG_CONFIGURE_CHANNEL_ENABLE BIT(2) + +/* enable/disable interrupt function (BIT 1) */ +#define CM32183_REG_CONFIGURE_INTERRUPT_ENABLE BIT(1) + +/* + * how to power on and shutdown sensor (BIT 0) + * 0 power on + * 1 shutdown + */ +#define CM32183_REG_CONFIGURE_POWER BIT(0) + +#define CM32183_REG_INT_HSB 0x01 +#define CM32183_REG_INT_LSB 0x02 +#define CM32183_REG_ALS_RESULT 0x04 +#define CM32183_REG_WHITE_RESULT 0x05 + +#define CM32183_REG_TRIGGER 0x06 + +#define CM32183_REG_TRIGGER_LOW_THRESHOLD BIT(15) +#define CM32183_REG_TRIGGER_HIGH_THRESHOLD BIT(14) + +extern const struct accelgyro_drv cm32183_drv; + +#endif /* __CROS_EC_ALS_CM32183_H */ diff --git a/driver/build.mk b/driver/build.mk index 271404ba09..63df758b11 100644 --- a/driver/build.mk +++ b/driver/build.mk @@ -47,6 +47,7 @@ driver-$(CONFIG_ALS_OPT3001)+=als_opt3001.o driver-$(CONFIG_ALS_SI114X)+=als_si114x.o driver-$(CONFIG_ALS_BH1730)+=als_bh1730.o driver-$(CONFIG_ALS_TCS3400)+=als_tcs3400.o +driver-$(CONFIG_ALS_CM32183)+=als_cm32183.o # Barometers driver-$(CONFIG_BARO_BMP280)+=baro_bmp280.o |