From bb8ed5538b6515346fe9944d5251b961e1210862 Mon Sep 17 00:00:00 2001 From: Owen_Ou Date: Mon, 20 Dec 2021 14:12:05 +0800 Subject: 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 Change-Id: I67f1184cefed257956cfe0f988c87e4f81db3fc7 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3350267 Tested-by: Owen Ou Auto-Submit: Owen Ou Reviewed-by: caveh jalali Commit-Queue: caveh jalali --- driver/als_cm32183.c | 151 ++++++++++++++++++++++++++++++++++++++ driver/als_cm32183.h | 103 ++++++++++++++++++++++++++ driver/build.mk | 1 + include/config.h | 1 + include/ec_commands.h | 1 + util/ectool.c | 3 + zephyr/CMakeLists.txt | 2 + zephyr/Kconfig.sensor_devices | 6 ++ zephyr/shim/include/config_chip.h | 5 ++ 9 files changed, 273 insertions(+) create mode 100644 driver/als_cm32183.c create mode 100644 driver/als_cm32183.h 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 diff --git a/include/config.h b/include/config.h index 379422c2ed..15b512715b 100644 --- a/include/config.h +++ b/include/config.h @@ -349,6 +349,7 @@ #undef CONFIG_ALS_BH1730_LUXTH_PARAMS #undef CONFIG_ALS_ISL29035 #undef CONFIG_ALS_OPT3001 +#undef CONFIG_ALS_CM32183 /* Define the exact model ID present on the board: SI1141 = 41, SI1142 = 42, */ #undef CONFIG_ALS_SI114X /* Check if the device revision is supported */ diff --git a/include/ec_commands.h b/include/ec_commands.h index 652b491cd2..baec7180cb 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -2720,6 +2720,7 @@ enum motionsensor_chip { MOTIONSENSE_CHIP_BMA422 = 27, MOTIONSENSE_CHIP_BMI323 = 28, MOTIONSENSE_CHIP_BMI220 = 29, + MOTIONSENSE_CHIP_CM32183 = 30, MOTIONSENSE_CHIP_MAX, }; diff --git a/util/ectool.c b/util/ectool.c index c6cc36d97d..8ea43bfb7d 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -5309,6 +5309,9 @@ static int cmd_motionsense(int argc, char **argv) case MOTIONSENSE_CHIP_OPT3001: printf("opt3001\n"); break; + case MOTIONSENSE_CHIP_CM32183: + printf("cm32183\n"); + break; case MOTIONSENSE_CHIP_BH1730: printf("bh1730\n"); break; diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 3b8484b990..992e6d4790 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -154,6 +154,8 @@ zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ADC_CMD "${PLATFORM_EC}/common/adc.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ALS_TCS3400 "${PLATFORM_EC}/driver/als_tcs3400.c") +zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ALS_CM32183 + "${PLATFORM_EC}/driver/als_cm32183.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_ACPI "${PLATFORM_EC}/common/acpi.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_BACKLIGHT_LID diff --git a/zephyr/Kconfig.sensor_devices b/zephyr/Kconfig.sensor_devices index 2d01f41249..7087be2f0f 100644 --- a/zephyr/Kconfig.sensor_devices +++ b/zephyr/Kconfig.sensor_devices @@ -86,6 +86,12 @@ config PLATFORM_EC_ALS_TCS3400 The driver supports TCS3400 which provides color and IR (red, green, blue, clear and IR) ambient light sensing. +config PLATFORM_EC_ALS_CM32183 + bool "CM32183 Ambient Light Sensor Driver" + help + The driver supports CM32183 which provides ambient + light sensing. + config PLATFORM_EC_ACCELGYRO_ICM426XX bool "ICM426XX Accelgyro Driver" select PLATFORM_EC_ACCELGYRO_ICM diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h index 42960ae4a0..132e898741 100644 --- a/zephyr/shim/include/config_chip.h +++ b/zephyr/shim/include/config_chip.h @@ -1631,6 +1631,11 @@ #define CONFIG_ALS_TCS3400 #endif +#undef CONFIG_ALS_CM32183 +#ifdef CONFIG_PLATFORM_EC_ALS_CM32183 +#define CONFIG_ALS_CM32183 +#endif + #undef CONFIG_ACCELGYRO_ICM426XX #ifdef CONFIG_PLATFORM_EC_ACCELGYRO_ICM426XX #define CONFIG_ACCELGYRO_ICM426XX -- cgit v1.2.1