sensor: sht4x: add device power management support

Registers driver with pm_device_driver_init(). Peform
software reset on TURN_ON. Added a small delay after
power-up

Signed-off-by: Van Petrosyan <van.petrosyan@sensirion.com>
This commit is contained in:
Van Petrosyan 2025-06-05 14:51:47 +02:00 committed by Dan Kalowsky
commit 8d2010e1e1
2 changed files with 41 additions and 10 deletions

View file

@ -9,6 +9,7 @@
#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>
#include <zephyr/pm/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/logging/log.h>
@ -180,15 +181,12 @@ static int sht4x_attr_set(const struct device *dev,
return 0;
}
static int sht4x_init(const struct device *dev)
static int sht4x_init_chip(const struct device *dev)
{
const struct sht4x_config *cfg = dev->config;
int rc = 0;
int rc;
if (!device_is_ready(cfg->bus.bus)) {
LOG_ERR("Device not ready.");
return -ENODEV;
}
/* 1 ms (max) power up time according to datasheet */
k_sleep(K_MSEC(SHT4X_POR_WAIT_MS));
rc = sht4x_write_command(dev, SHT4X_CMD_RESET);
if (rc < 0) {
@ -197,10 +195,42 @@ static int sht4x_init(const struct device *dev)
}
k_sleep(K_MSEC(SHT4X_RESET_WAIT_MS));
return 0;
}
static int sht4x_pm_action(const struct device *dev, enum pm_device_action action)
{
int rc = 0;
switch (action) {
case PM_DEVICE_ACTION_TURN_ON:
rc = sht4x_init_chip(dev);
break;
case PM_DEVICE_ACTION_RESUME:
case PM_DEVICE_ACTION_SUSPEND:
case PM_DEVICE_ACTION_TURN_OFF:
break;
default:
return -ENOTSUP;
}
return rc;
}
static int sht4x_init(const struct device *dev)
{
const struct sht4x_config *cfg = dev->config;
if (!device_is_ready(cfg->bus.bus)) {
LOG_ERR("Device not ready.");
return -ENODEV;
}
return pm_device_driver_init(dev, sht4x_pm_action);
}
static DEVICE_API(sensor, sht4x_api) = {
.sample_fetch = sht4x_sample_fetch,
@ -215,10 +245,10 @@ static DEVICE_API(sensor, sht4x_api) = {
.bus = I2C_DT_SPEC_INST_GET(n), \
.repeatability = DT_INST_PROP(n, repeatability) \
}; \
\
PM_DEVICE_DT_INST_DEFINE(n, sht4x_pm_action); \
SENSOR_DEVICE_DT_INST_DEFINE(n, \
sht4x_init, \
NULL, \
PM_DEVICE_DT_INST_GET(n), \
&sht4x_data_##n, \
&sht4x_config_##n, \
POST_KERNEL, \

View file

@ -13,6 +13,7 @@
#define SHT4X_CMD_RESET 0x94
#define SHT4X_RESET_WAIT_MS 1
#define SHT4X_POR_WAIT_MS 1
#define SHT4X_HEATER_POWER_IDX_MAX 3
#define SHT4X_HEATER_DURATION_IDX_MAX 2