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 <jeppe.odgaard@prevas.dk>
This commit is contained in:
Jeppe Odgaard 2024-08-15 16:01:13 +02:00 committed by Benjamin Cabé
commit c41570871b
5 changed files with 95 additions and 2 deletions

View file

@ -10,6 +10,7 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/sensor/tmp116.h>
#include <zephyr/dt-bindings/sensor/tmp116.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
@ -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, \

View file

@ -7,6 +7,8 @@
#ifndef ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_
#define ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_
#include <zephyr/sys/util_macro.h>
#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_ */

View file

@ -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

View file

@ -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_ */

View file

@ -24,6 +24,7 @@
#include <zephyr/dt-bindings/sensor/stts22h.h>
#include <zephyr/dt-bindings/sensor/ina226.h>
#include <zephyr/dt-bindings/sensor/apds9253.h>
#include <zephyr/dt-bindings/sensor/tmp116.h>
/****************************************
* 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 = <TMP116_DT_ODR_125_MS>;
};
test_i2c_bq274xx: bq27xx@48 {