From c41570871b4531898d42e4a7095de6377e73ff57 Mon Sep 17 00:00:00 2001 From: Jeppe Odgaard Date: Thu, 15 Aug 2024 16:01:13 +0200 Subject: [PATCH] drivers: sensor: tmp116: support set sample frequency Add support for setting the sample frequency via `attr_set` and the output data rate from device tree source. Signed-off-by: Jeppe Odgaard --- drivers/sensor/ti/tmp116/tmp116.c | 43 +++++++++++++++++++++- drivers/sensor/ti/tmp116/tmp116.h | 4 ++ dts/bindings/sensor/ti,tmp116.yaml | 17 +++++++++ include/zephyr/dt-bindings/sensor/tmp116.h | 31 ++++++++++++++++ tests/drivers/build_all/sensor/i2c.dtsi | 2 + 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 include/zephyr/dt-bindings/sensor/tmp116.h diff --git a/drivers/sensor/ti/tmp116/tmp116.c b/drivers/sensor/ti/tmp116/tmp116.c index 0505d3615ce..977d010a28f 100644 --- a/drivers/sensor/ti/tmp116/tmp116.c +++ b/drivers/sensor/ti/tmp116/tmp116.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -242,6 +243,33 @@ static int tmp116_channel_get(const struct device *dev, return 0; } +static int16_t tmp116_conv_value(const struct sensor_value *val) +{ + uint32_t freq_micro = sensor_value_to_micro(val); + + switch (freq_micro) { + case 64000000: /* 1 / 15.5 ms has been rounded down */ + return TMP116_DT_ODR_15_5_MS; + case 8000000: + return TMP116_DT_ODR_125_MS; + case 4000000: + return TMP116_DT_ODR_250_MS; + case 2000000: + return TMP116_DT_ODR_500_MS; + case 1000000: + return TMP116_DT_ODR_1000_MS; + case 250000: + return TMP116_DT_ODR_4000_MS; + case 125000: + return TMP116_DT_ODR_8000_MS; + case 62500: + return TMP116_DT_ODR_16000_MS; + default: + LOG_ERR("%" PRIu32 " uHz not supported", freq_micro); + return -EINVAL; + } +} + static int tmp116_attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr, @@ -256,6 +284,14 @@ static int tmp116_attr_set(const struct device *dev, } switch ((int)attr) { + case SENSOR_ATTR_SAMPLING_FREQUENCY: + value = tmp116_conv_value(val); + if (value < 0) { + return value; + } + + return tmp116_write_config(dev, TMP116_CFGR_CONV, value); + case SENSOR_ATTR_OFFSET: if (drv_data->id != TMP117_DEVICE_ID) { LOG_ERR("%s: Offset is only supported by TMP117", @@ -361,13 +397,16 @@ static int tmp116_init(const struct device *dev) LOG_DBG("Got device ID: %x", id); drv_data->id = id; - return 0; + rc = tmp116_write_config(dev, TMP116_CFGR_CONV, cfg->odr); + + return rc; } #define DEFINE_TMP116(_num) \ static struct tmp116_data tmp116_data_##_num; \ static const struct tmp116_dev_config tmp116_config_##_num = { \ - .bus = I2C_DT_SPEC_INST_GET(_num) \ + .bus = I2C_DT_SPEC_INST_GET(_num), \ + .odr = DT_INST_PROP(_num, odr), \ }; \ SENSOR_DEVICE_DT_INST_DEFINE(_num, tmp116_init, NULL, \ &tmp116_data_##_num, &tmp116_config_##_num, POST_KERNEL, \ diff --git a/drivers/sensor/ti/tmp116/tmp116.h b/drivers/sensor/ti/tmp116/tmp116.h index e8b5c0a3534..6211d9a71f1 100644 --- a/drivers/sensor/ti/tmp116/tmp116.h +++ b/drivers/sensor/ti/tmp116/tmp116.h @@ -7,6 +7,8 @@ #ifndef ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_ #define ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_ +#include + #define TMP116_REG_TEMP 0x0 #define TMP116_REG_CFGR 0x1 #define TMP116_REG_HIGH_LIM 0x2 @@ -26,6 +28,7 @@ #define TMP117_DEVICE_ID 0x0117 #define TMP116_CFGR_AVG (BIT(5) | BIT(6)) +#define TMP116_CFGR_CONV (BIT(7) | BIT(8) | BIT(9)) #define TMP116_CFGR_MODE (BIT(10) | BIT(11)) #define TMP116_CFGR_DATA_READY BIT(13) #define TMP116_EEPROM_UL_UNLOCK BIT(15) @@ -46,6 +49,7 @@ struct tmp116_data { struct tmp116_dev_config { struct i2c_dt_spec bus; + uint16_t odr; }; #endif /* ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_ */ diff --git a/dts/bindings/sensor/ti,tmp116.yaml b/dts/bindings/sensor/ti,tmp116.yaml index fc949ef9340..338b2595885 100644 --- a/dts/bindings/sensor/ti,tmp116.yaml +++ b/dts/bindings/sensor/ti,tmp116.yaml @@ -8,3 +8,20 @@ compatible: "ti,tmp116" bus: tmp116 include: [sensor-device.yaml, i2c-device.yaml] + +properties: + odr: + type: int + default: 0x200 + description: | + Specify the default temperature output data rate in milliseconds (ms). + Default is power-up configuration. + enum: + - 0 # TMP116_DT_ODR_15_5_MS + - 0x80 # TMP116_DT_ODR_125_MS + - 0x100 # TMP116_DT_ODR_250_MS + - 0x180 # TMP116_DT_ODR_500_MS + - 0x200 # TMP116_DT_ODR_1000_MS + - 0x280 # TMP116_DT_ODR_4000_MS + - 0x300 # TMP116_DT_ODR_8000_MS + - 0x380 # TMP116_DT_ODR_16000_MS diff --git a/include/zephyr/dt-bindings/sensor/tmp116.h b/include/zephyr/dt-bindings/sensor/tmp116.h new file mode 100644 index 00000000000..fa7c1363ec1 --- /dev/null +++ b/include/zephyr/dt-bindings/sensor/tmp116.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Vitrolife A/S + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_TI_TMP116_H_ +#define ZEPHYR_INCLUDE_DT_BINDINGS_TI_TMP116_H_ + +/** + * @defgroup TMP116 Texas Instruments (TI) TMP116 DT Options + * @ingroup sensor_interface + * @{ + */ + +/** + * @defgroup TMP116_ODR Temperature output data rate + * @{ + */ +#define TMP116_DT_ODR_15_5_MS 0 +#define TMP116_DT_ODR_125_MS 0x80 +#define TMP116_DT_ODR_250_MS 0x100 +#define TMP116_DT_ODR_500_MS 0x180 +#define TMP116_DT_ODR_1000_MS 0x200 +#define TMP116_DT_ODR_4000_MS 0x280 +#define TMP116_DT_ODR_8000_MS 0x300 +#define TMP116_DT_ODR_16000_MS 0x380 +/** @} */ + +/** @} */ + +#endif /* ZEPHYR_INCLUDE_DT_BINDINGS_TI_TMP116_H_ */ diff --git a/tests/drivers/build_all/sensor/i2c.dtsi b/tests/drivers/build_all/sensor/i2c.dtsi index 7b5ef3e9294..3b34609bc4a 100644 --- a/tests/drivers/build_all/sensor/i2c.dtsi +++ b/tests/drivers/build_all/sensor/i2c.dtsi @@ -24,6 +24,7 @@ #include #include #include +#include /**************************************** * PLEASE KEEP REG ADDRESSES SEQUENTIAL * @@ -478,6 +479,7 @@ test_i2c_tmp112: tmp112@46 { test_i2c_tmp116: tmp116@47 { compatible = "ti,tmp116"; reg = <0x47>; + odr = ; }; test_i2c_bq274xx: bq27xx@48 {