From 8d2010e1e179a88766dcdc87cfb655588bcb8e24 Mon Sep 17 00:00:00 2001 From: Van Petrosyan Date: Thu, 5 Jun 2025 14:51:47 +0200 Subject: [PATCH] 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 --- drivers/sensor/sensirion/sht4x/sht4x.c | 50 ++++++++++++++++++++------ drivers/sensor/sensirion/sht4x/sht4x.h | 1 + 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/drivers/sensor/sensirion/sht4x/sht4x.c b/drivers/sensor/sensirion/sht4x/sht4x.c index c271f1b9c84..6bb29896fd9 100644 --- a/drivers/sensor/sensirion/sht4x/sht4x.c +++ b/drivers/sensor/sensirion/sht4x/sht4x.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -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, \ diff --git a/drivers/sensor/sensirion/sht4x/sht4x.h b/drivers/sensor/sensirion/sht4x/sht4x.h index 6409bea2d70..384ae396762 100644 --- a/drivers/sensor/sensirion/sht4x/sht4x.h +++ b/drivers/sensor/sensirion/sht4x/sht4x.h @@ -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