From e2b1594fd207f647b555a53e89a474b28ad59c12 Mon Sep 17 00:00:00 2001 From: Severin Maher Date: Mon, 21 Oct 2024 11:22:58 +0200 Subject: [PATCH] feat: drivers: tcs3430: add ATIME and AGAIN configs Adds DTS properties and driver code to set integration time and gain for TCS3430. Signed-off-by: Severin Maher --- drivers/sensor/tcs3430/tcs3430.c | 30 +++++++++++++++++++++---- dts/bindings/sensor/ams,tcs3430.yaml | 15 +++++++++++++ include/zephyr/drivers/sensor/tcs3430.h | 2 ++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/drivers/sensor/tcs3430/tcs3430.c b/drivers/sensor/tcs3430/tcs3430.c index b1c8ce79be6165..e2eb71ca207da5 100644 --- a/drivers/sensor/tcs3430/tcs3430.c +++ b/drivers/sensor/tcs3430/tcs3430.c @@ -29,6 +29,8 @@ LOG_MODULE_REGISTER(tcs3430, CONFIG_SENSOR_LOG_LEVEL); #define TCS3430_CONTROL_REG 0x8F +#define TCS3430_CFG1_REG 0x90 + #define TCS3430_ID_REG 0x92 #define TCS3430_ID 0xDC @@ -62,6 +64,8 @@ LOG_MODULE_REGISTER(tcs3430, CONFIG_SENSOR_LOG_LEVEL); struct tcs3430_config { struct i2c_dt_spec i2c; struct gpio_dt_spec int_gpio; + uint16_t integration_cycles; + uint8_t gain; }; struct tcs3430_data { @@ -187,17 +191,27 @@ static int tcs3430_attr_set(const struct device *dev, enum sensor_channel chan, int ret; uint8_t reg_val; - switch (attr) { + switch ((enum sensor_attribute_tcs3430)attr) { case SENSOR_ATTR_TCS3430_INTEGRATION_CYCLES: if (!IN_RANGE(val->val1, 1, 256)) { return -EINVAL; } - reg_val = UINT8_MAX - val->val1 + 1; + reg_val = (uint8_t)(val->val1-1); ret = i2c_reg_write_byte_dt(&cfg->i2c, TCS3430_ATIME_REG, reg_val); if (ret) { return ret; } break; + case SENSOR_ATTR_TCS3430_GAIN: + if (!IN_RANGE(val->val1, 0, 3)) { + return -EINVAL; + } + reg_val = (uint8_t)val->val1; + ret = i2c_reg_write_byte_dt(&cfg->i2c, TCS3430_CFG1_REG, reg_val); + if (ret) { + return ret; + } + break; default: return -ENOTSUP; } @@ -210,17 +224,23 @@ static int tcs3430_sensor_setup(const struct device *dev) const struct tcs3430_config *cfg = dev->config; uint8_t chip_id; int ret; + if (!IN_RANGE(cfg->integration_cycles, 1, 256)) { + LOG_ERR("Invalid integration cycles: %d", cfg->integration_cycles); + return -EINVAL; + } + uint8_t atime_default = (uint8_t)(cfg->integration_cycles-1); struct { uint8_t reg_addr; uint8_t value; } reset_regs[] = { {TCS3430_ENABLE_REG, TCS3430_DEFAULT_ENABLE}, {TCS3430_AICLEAR_REG, TCS3430_AICLEAR_RESET}, - {TCS3430_ATIME_REG, TCS3430_DEFAULT_ATIME}, + {TCS3430_ATIME_REG, atime_default}, {TCS3430_PERS_REG, TCS3430_DEFAULT_PERS}, {TCS3430_CONFIG_REG, TCS3430_DEFAULT_CONFIG}, {TCS3430_CONTROL_REG, TCS3430_DEFAULT_CONTROL}, {TCS3430_INTENAB_REG, TCS3430_DEFAULT_INTENAB}, + {TCS3430_CFG1_REG, cfg->gain}, {TCS3430_CFG2_REG, TCS3430_DEFAULT_CFG2}, }; @@ -270,7 +290,7 @@ static int tcs3430_init(const struct device *dev) ret = tcs3430_sensor_setup(dev); if (ret < 0) { - LOG_ERR("Failed to setup device"); + LOG_ERR("Failed to setup device: %d", ret); return ret; } @@ -301,6 +321,8 @@ static int tcs3430_init(const struct device *dev) static const struct tcs3430_config tcs3430_config_##n = { \ .i2c = I2C_DT_SPEC_INST_GET(n), \ .int_gpio = GPIO_DT_SPEC_INST_GET(n, int_gpios), \ + .integration_cycles = DT_INST_PROP(n, integration_cycles), \ + .gain = DT_INST_PROP(n, gain), \ }; \ SENSOR_DEVICE_DT_INST_DEFINE(n, &tcs3430_init, NULL, &tcs3430_data_##n, \ &tcs3430_config_##n, POST_KERNEL, \ diff --git a/dts/bindings/sensor/ams,tcs3430.yaml b/dts/bindings/sensor/ams,tcs3430.yaml index 0fc90c3f84140e..c2eec3d1578539 100644 --- a/dts/bindings/sensor/ams,tcs3430.yaml +++ b/dts/bindings/sensor/ams,tcs3430.yaml @@ -13,3 +13,18 @@ properties: required: true description: | INT pin GPIO identifier, open-drain, active low. + integration-cycles: + type: int + default: 256 + description: | + Initial ADC integration cycle setting from 1 to 256. + 1 cycle = 2.78ms. + gain: + type: int + default: 0 + description: | + ALS Gain Control: + 0 - 1x + 1 - 4x + 2 - 16x + 3 - 64x diff --git a/include/zephyr/drivers/sensor/tcs3430.h b/include/zephyr/drivers/sensor/tcs3430.h index 916dac9b91611a..9957e667211ff2 100644 --- a/include/zephyr/drivers/sensor/tcs3430.h +++ b/include/zephyr/drivers/sensor/tcs3430.h @@ -12,6 +12,8 @@ enum sensor_attribute_tcs3430 { /** XYZB Integration Cycles */ SENSOR_ATTR_TCS3430_INTEGRATION_CYCLES = SENSOR_ATTR_PRIV_START, + /** Gain setting */ + SENSOR_ATTR_TCS3430_GAIN, }; #endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_TCS3430_H_ */