drivers: sensor: lm75: add alert threshold support
Add SENSOR_ATTR_ALERT and SENSOR_ATTR_HYSTERESIS attributes support. The code is heavily inspired by the one for lm77 sensor. Signed-off-by: Benjamin Lemouzy <blemouzy@centralp.fr>
This commit is contained in:
parent
eb2fc22967
commit
5c8937fbac
6 changed files with 342 additions and 26 deletions
|
@ -10,4 +10,43 @@ config LM75
|
|||
select I2C
|
||||
help
|
||||
Enable the driver for the LM75 digital temperature Sensor with 2-wire
|
||||
interface. Thermal Watchdog is not implemented
|
||||
interface.
|
||||
|
||||
if LM75
|
||||
|
||||
choice
|
||||
prompt "Trigger mode"
|
||||
default LM75_TRIGGER_NONE
|
||||
help
|
||||
Specify the type of triggering to be used by the driver.
|
||||
|
||||
config LM75_TRIGGER_NONE
|
||||
bool "No trigger"
|
||||
|
||||
config LM75_TRIGGER_GLOBAL_THREAD
|
||||
bool "Use global thread"
|
||||
depends on GPIO
|
||||
select LM75_TRIGGER
|
||||
|
||||
endchoice
|
||||
|
||||
config LM75_TRIGGER
|
||||
bool
|
||||
|
||||
if LM75_TRIGGER
|
||||
|
||||
config LM75_TRIGGER_THREAD_STACK_SIZE
|
||||
int "Stack size for the trigger workqueue thread"
|
||||
default 512
|
||||
help
|
||||
Size of the stack used for the internal trigger workqueue thread.
|
||||
|
||||
config LM75_TRIGGER_THREAD_PRIO
|
||||
int "Priority for the trigger workqueue thread"
|
||||
default 0
|
||||
help
|
||||
Priority level for the internal trigger workqueue thread.
|
||||
|
||||
endif # LM75_TRIGGER
|
||||
|
||||
endif # LM75
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Leica Geosystems AG
|
||||
* Copyright (c) 2023 Centralp
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -8,62 +9,267 @@
|
|||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/pm/device.h>
|
||||
#include <zephyr/pm/device_runtime.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/byteorder.h>
|
||||
|
||||
LOG_MODULE_REGISTER(LM75, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
/*
|
||||
* Only compile in trigger support if enabled in Kconfig and at least one
|
||||
* enabled lm75 devicetree node has the int-gpios property.
|
||||
*/
|
||||
#define LM75_TRIGGER_SUPPORT \
|
||||
(CONFIG_LM75_TRIGGER && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios))
|
||||
|
||||
#define LM75_REG_TEMP 0x00
|
||||
/* LM75 registers */
|
||||
#define LM75_REG_TEMP 0x00
|
||||
#define LM75_REG_CONFIG 0x01
|
||||
#define LM75_REG_T_HYST 0x02
|
||||
#define LM75_REG_T_OS 0x03
|
||||
#define LM75_REG_T_OS 0x03
|
||||
|
||||
struct lm75_data {
|
||||
int16_t temp; /*temp in 0.1°C*/
|
||||
int16_t temp;
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
const struct device *dev;
|
||||
struct k_work_q workq;
|
||||
struct k_work work;
|
||||
struct gpio_callback int_gpio_cb;
|
||||
const struct sensor_trigger *trigger;
|
||||
sensor_trigger_handler_t trigger_handler;
|
||||
|
||||
K_KERNEL_STACK_MEMBER(stack, CONFIG_LM75_TRIGGER_THREAD_STACK_SIZE);
|
||||
#endif /* LM75_TRIGGER_SUPPORT */
|
||||
};
|
||||
|
||||
/* LM75 configuration register bits */
|
||||
union lm75_reg_config {
|
||||
uint8_t reg;
|
||||
struct {
|
||||
uint8_t shutdown: 1;
|
||||
uint8_t int_mode: 1;
|
||||
uint8_t int_pol: 1;
|
||||
uint8_t fault_queue: 2;
|
||||
uint8_t reserved: 3;
|
||||
} __packed;
|
||||
};
|
||||
|
||||
struct lm75_config {
|
||||
struct i2c_dt_spec i2c;
|
||||
union lm75_reg_config config_dt;
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
struct gpio_dt_spec int_gpio;
|
||||
#endif /* LM75_TRIGGER_SUPPORT */
|
||||
};
|
||||
|
||||
static inline int lm75_reg_read(const struct lm75_config *cfg, uint8_t reg,
|
||||
uint8_t *buf, uint32_t size)
|
||||
static inline int lm75_reg_read(const struct device *dev, uint8_t reg, uint8_t *buf, uint32_t size)
|
||||
{
|
||||
const struct lm75_config *cfg = dev->config;
|
||||
|
||||
return i2c_burst_read_dt(&cfg->i2c, reg, buf, size);
|
||||
}
|
||||
|
||||
static inline int lm75_fetch_temp(const struct lm75_config *cfg, struct lm75_data *data)
|
||||
static inline int lm75_reg_write(const struct device *dev, uint8_t reg, const uint8_t *buf,
|
||||
uint32_t size)
|
||||
{
|
||||
int ret;
|
||||
uint8_t temp_read[2];
|
||||
int16_t temp;
|
||||
const struct lm75_config *cfg = dev->config;
|
||||
|
||||
ret = lm75_reg_read(cfg, LM75_REG_TEMP, temp_read, sizeof(temp_read));
|
||||
return i2c_burst_write_dt(&cfg->i2c, reg, buf, size);
|
||||
}
|
||||
|
||||
static inline int lm75_temp_read(const struct device *dev, uint8_t reg, int16_t *value)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
int ret;
|
||||
|
||||
ret = lm75_reg_read(dev, reg, buf, sizeof(buf));
|
||||
if (ret) {
|
||||
LOG_ERR("Could not fetch temperature [%d]", ret);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
/* temp is in two's complement.
|
||||
* bit 7 in the lower part corresponds to 0.5
|
||||
*/
|
||||
temp = temp_read[0] << 8 | temp_read[1];
|
||||
|
||||
/* shift right by 7, multiply by 10 to get 0.1° and divide by 2 to get °C */
|
||||
data->temp = (temp / 128) * 10 / 2;
|
||||
*value = sys_get_be16(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lm75_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel chan)
|
||||
static int lm75_temp_write(const struct device *dev, uint8_t reg, int16_t value)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
|
||||
sys_put_be16(value, buf);
|
||||
return lm75_reg_write(dev, reg, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
static void lm75_sensor_value_to_temp(const struct sensor_value *val, int16_t *temp)
|
||||
{
|
||||
*temp = val->val1 * 10;
|
||||
*temp += val->val2 / 100000U;
|
||||
|
||||
*temp = (*temp * 256) / 10;
|
||||
}
|
||||
|
||||
static void lm75_temp_to_sensor_value(int16_t temp, struct sensor_value *val)
|
||||
{
|
||||
/* shift right by 7, multiply by 10 to get 0.1° and divide by 2 to get °C */
|
||||
temp = (temp / 128) * 10 / 2;
|
||||
|
||||
/* Integer part in degrees Celsius */
|
||||
val->val1 = temp / 10;
|
||||
|
||||
/* Fractional part in micro degrees Celsius */
|
||||
val->val2 = (temp - val->val1 * 10) * 100000U;
|
||||
}
|
||||
|
||||
static int lm75_attr_set(const struct device *dev, enum sensor_channel chan,
|
||||
enum sensor_attribute attr, const struct sensor_value *val)
|
||||
{
|
||||
int16_t temp = 0;
|
||||
uint8_t reg;
|
||||
int err;
|
||||
|
||||
__ASSERT_NO_MSG(val != NULL);
|
||||
|
||||
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
switch (attr) {
|
||||
case SENSOR_ATTR_ALERT:
|
||||
reg = LM75_REG_T_OS;
|
||||
break;
|
||||
case SENSOR_ATTR_HYSTERESIS:
|
||||
reg = LM75_REG_T_HYST;
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
lm75_sensor_value_to_temp(val, &temp);
|
||||
|
||||
err = lm75_temp_write(dev, reg, temp);
|
||||
if (err < 0) {
|
||||
LOG_ERR("failed to write register 0x%02x (err %d)", reg, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lm75_attr_get(const struct device *dev, enum sensor_channel chan,
|
||||
enum sensor_attribute attr, struct sensor_value *val)
|
||||
{
|
||||
int16_t temp;
|
||||
uint8_t reg;
|
||||
int err;
|
||||
|
||||
__ASSERT_NO_MSG(val != NULL);
|
||||
|
||||
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
switch (attr) {
|
||||
case SENSOR_ATTR_ALERT:
|
||||
reg = LM75_REG_T_OS;
|
||||
break;
|
||||
case SENSOR_ATTR_HYSTERESIS:
|
||||
reg = LM75_REG_T_HYST;
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
err = lm75_temp_read(dev, reg, &temp);
|
||||
if (err < 0) {
|
||||
LOG_ERR("failed to read register 0x%02x (err %d)", reg, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
lm75_temp_to_sensor_value(temp, val);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
static int lm75_trigger_set(const struct device *dev, const struct sensor_trigger *trig,
|
||||
sensor_trigger_handler_t handler)
|
||||
{
|
||||
const struct lm75_config *config = dev->config;
|
||||
struct lm75_data *data = dev->data;
|
||||
gpio_flags_t flags;
|
||||
int err;
|
||||
|
||||
__ASSERT_NO_MSG(trig != NULL);
|
||||
|
||||
if (trig->type != SENSOR_TRIG_THRESHOLD || trig->chan != SENSOR_CHAN_AMBIENT_TEMP ||
|
||||
config->int_gpio.port == NULL) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if (handler != NULL) {
|
||||
flags = GPIO_INT_EDGE_TO_ACTIVE;
|
||||
} else {
|
||||
flags = GPIO_INT_DISABLE;
|
||||
}
|
||||
|
||||
err = gpio_pin_interrupt_configure_dt(&config->int_gpio, flags);
|
||||
if (err < 0) {
|
||||
LOG_ERR("failed to configure INT GPIO IRQ (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
data->trigger = trig;
|
||||
data->trigger_handler = handler;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void lm75_trigger_work_handler(struct k_work *item)
|
||||
{
|
||||
struct lm75_data *data = CONTAINER_OF(item, struct lm75_data, work);
|
||||
sensor_trigger_handler_t handler = data->trigger_handler;
|
||||
|
||||
if (handler != NULL) {
|
||||
handler(data->dev, (struct sensor_trigger *)data->trigger);
|
||||
}
|
||||
}
|
||||
|
||||
static void lm75_int_gpio_callback_handler(const struct device *port, struct gpio_callback *cb,
|
||||
gpio_port_pins_t pins)
|
||||
{
|
||||
struct lm75_data *data = CONTAINER_OF(cb, struct lm75_data, int_gpio_cb);
|
||||
|
||||
ARG_UNUSED(port);
|
||||
ARG_UNUSED(pins);
|
||||
|
||||
k_work_submit_to_queue(&data->workq, &data->work);
|
||||
}
|
||||
#endif /* LM75_TRIGGER_SUPPORT */
|
||||
|
||||
static inline int lm75_fetch_temp(const struct device *dev)
|
||||
{
|
||||
struct lm75_data *data = dev->data;
|
||||
const struct lm75_config *cfg = dev->config;
|
||||
int16_t temp;
|
||||
int ret;
|
||||
|
||||
ret = lm75_temp_read(dev, LM75_REG_TEMP, &temp);
|
||||
if (ret) {
|
||||
LOG_ERR("Could not fetch temperature [%d]", ret);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
data->temp = temp;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lm75_sample_fetch(const struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
enum pm_device_state pm_state;
|
||||
int ret;
|
||||
|
||||
|
@ -76,7 +282,7 @@ static int lm75_sample_fetch(const struct device *dev,
|
|||
switch (chan) {
|
||||
case SENSOR_CHAN_ALL:
|
||||
case SENSOR_CHAN_AMBIENT_TEMP:
|
||||
ret = lm75_fetch_temp(cfg, data);
|
||||
ret = lm75_fetch_temp(dev);
|
||||
break;
|
||||
default:
|
||||
ret = -ENOTSUP;
|
||||
|
@ -86,16 +292,14 @@ static int lm75_sample_fetch(const struct device *dev,
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int lm75_channel_get(const struct device *dev,
|
||||
enum sensor_channel chan,
|
||||
static int lm75_channel_get(const struct device *dev, enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct lm75_data *data = dev->data;
|
||||
|
||||
switch (chan) {
|
||||
case SENSOR_CHAN_AMBIENT_TEMP:
|
||||
val->val1 = data->temp / 10;
|
||||
val->val2 = (data->temp - val->val1 * 10) * 100000U;
|
||||
lm75_temp_to_sensor_value(data->temp, val);
|
||||
return 0;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
|
@ -103,6 +307,11 @@ static int lm75_channel_get(const struct device *dev,
|
|||
}
|
||||
|
||||
static const struct sensor_driver_api lm75_driver_api = {
|
||||
.attr_set = lm75_attr_set,
|
||||
.attr_get = lm75_attr_get,
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
.trigger_set = lm75_trigger_set,
|
||||
#endif /* LM75_TRIGGER_SUPPORT */
|
||||
.sample_fetch = lm75_sample_fetch,
|
||||
.channel_get = lm75_channel_get,
|
||||
};
|
||||
|
@ -111,6 +320,9 @@ int lm75_init(const struct device *dev)
|
|||
{
|
||||
const struct lm75_config *cfg = dev->config;
|
||||
int ret = 0;
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
struct lm75_data *data = dev->data;
|
||||
#endif /* LM75_TRIGGER_SUPPORT */
|
||||
|
||||
if (!device_is_ready(cfg->i2c.bus)) {
|
||||
LOG_ERR("I2C dev not ready");
|
||||
|
@ -127,6 +339,42 @@ int lm75_init(const struct device *dev)
|
|||
}
|
||||
#endif
|
||||
|
||||
ret = lm75_reg_write(dev, LM75_REG_CONFIG, &cfg->config_dt.reg, sizeof(cfg->config_dt.reg));
|
||||
if (ret < 0) {
|
||||
LOG_ERR("failed to write configuration (ret %d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
data->dev = dev;
|
||||
k_work_queue_start(&data->workq, data->stack, K_THREAD_STACK_SIZEOF(data->stack),
|
||||
CONFIG_LM75_TRIGGER_THREAD_PRIO, NULL);
|
||||
k_thread_name_set(&data->workq.thread, "lm75_trigger");
|
||||
k_work_init(&data->work, lm75_trigger_work_handler);
|
||||
|
||||
if (cfg->int_gpio.port != NULL) {
|
||||
if (!device_is_ready(cfg->int_gpio.port)) {
|
||||
LOG_ERR("INT GPIO not ready");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&cfg->int_gpio, GPIO_INPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("failed to configure INT GPIO (ret %d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gpio_init_callback(&data->int_gpio_cb, lm75_int_gpio_callback_handler,
|
||||
BIT(cfg->int_gpio.pin));
|
||||
|
||||
ret = gpio_add_callback(cfg->int_gpio.port, &data->int_gpio_cb);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("failed to add INT GPIO callback (ret %d)", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif /* LM75_TRIGGER_SUPPORT */
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -149,10 +397,24 @@ static int lm75_pm_action(const struct device *dev, enum pm_device_action action
|
|||
|
||||
#endif
|
||||
|
||||
#if LM75_TRIGGER_SUPPORT
|
||||
#define LM75_INT_GPIO_INIT(n) .int_gpio = GPIO_DT_SPEC_INST_GET_OR(n, int_gpios, { 0 })
|
||||
#else /* LM75_TRIGGER_SUPPORT */
|
||||
#define LM75_INT_GPIO_INIT(n)
|
||||
#endif /* ! LM75_TRIGGER_SUPPORT */
|
||||
|
||||
#define LM75_INST(inst) \
|
||||
static struct lm75_data lm75_data_##inst; \
|
||||
static const struct lm75_config lm75_config_##inst = { \
|
||||
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||
.config_dt = { \
|
||||
.shutdown = 0, \
|
||||
.int_mode = DT_INST_NODE_HAS_PROP(inst, int_gpios), \
|
||||
.int_pol = DT_INST_PROP(inst, int_inverted), \
|
||||
.fault_queue = 0, \
|
||||
.reserved = 0, \
|
||||
}, \
|
||||
LM75_INT_GPIO_INIT(inst) \
|
||||
}; \
|
||||
PM_DEVICE_DT_INST_DEFINE(inst, lm75_pm_action); \
|
||||
SENSOR_DEVICE_DT_INST_DEFINE(inst, lm75_init, PM_DEVICE_DT_INST_GET(inst), &lm75_data_##inst, \
|
||||
|
|
|
@ -6,3 +6,15 @@ description: LM75 Digital Temperature Sensor with 2-Wire Interface.
|
|||
compatible: "lm75"
|
||||
|
||||
include: [sensor-device.yaml, i2c-device.yaml]
|
||||
|
||||
properties:
|
||||
int-gpios:
|
||||
type: phandle-array
|
||||
description: |
|
||||
Identifies the INT signal, which is active-low open drain by default
|
||||
when produced by the sensor.
|
||||
|
||||
int-inverted:
|
||||
type: boolean
|
||||
description: |
|
||||
When present, the polarity on the INT signal is inverted (active-high).
|
||||
|
|
|
@ -597,6 +597,7 @@ test_i2c_bmp388: bmp388@55 {
|
|||
test_i2c_lm75: lm75@56 {
|
||||
compatible = "lm75";
|
||||
reg = <0x56>;
|
||||
int-gpios = <&test_gpio 0 0>;
|
||||
};
|
||||
|
||||
test_i2c_ina230: ina230@57 {
|
||||
|
|
|
@ -40,6 +40,7 @@ CONFIG_LIS2DUX12_TRIGGER_GLOBAL_THREAD=y
|
|||
CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD=y
|
||||
CONFIG_LIS2MDL_TRIGGER_GLOBAL_THREAD=y
|
||||
CONFIG_LIS3MDL_TRIGGER_GLOBAL_THREAD=y
|
||||
CONFIG_LM75_TRIGGER_GLOBAL_THREAD=y
|
||||
CONFIG_LPS2XDF_TRIGGER_GLOBAL_THREAD=y
|
||||
CONFIG_LPS22HH_TRIGGER_GLOBAL_THREAD=y
|
||||
CONFIG_LSM6DSL_TRIGGER_GLOBAL_THREAD=y
|
||||
|
|
|
@ -40,6 +40,7 @@ CONFIG_LIS2DUX12_TRIGGER_NONE=y
|
|||
CONFIG_LIS2DW12_TRIGGER_NONE=y
|
||||
CONFIG_LIS2MDL_TRIGGER_NONE=y
|
||||
CONFIG_LIS3MDL_TRIGGER_NONE=y
|
||||
CONFIG_LM75_TRIGGER_NONE=y
|
||||
CONFIG_LPS2XDF_TRIGGER_NONE=y
|
||||
CONFIG_LPS22HH_TRIGGER_NONE=y
|
||||
CONFIG_LSM6DSL_TRIGGER_NONE=y
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue