sensor: lis2dh: add device runtime PM support

Registers driver with pm_device_driver_init(). Moved
chip init routine into separate function to be called
from PM_DEVICE_ACTION_TURN_ON. Added a delay after
power-up.

Signed-off-by: Van Petrosyan <van.petrosyan@sensirion.com>
This commit is contained in:
Van Petrosyan 2025-06-05 16:17:04 +02:00 committed by Benjamin Cabé
commit b12717bee1
2 changed files with 25 additions and 11 deletions

View file

@ -344,7 +344,7 @@ static DEVICE_API(sensor, lis2dh_driver_api) = {
.channel_get = lis2dh_channel_get,
};
int lis2dh_init(const struct device *dev)
int lis2dh_init_chip(const struct device *dev)
{
struct lis2dh_data *lis2dh = dev->data;
const struct lis2dh_config *cfg = dev->config;
@ -352,10 +352,8 @@ int lis2dh_init(const struct device *dev)
uint8_t id;
uint8_t raw[6];
status = cfg->bus_init(dev);
if (status < 0) {
return status;
}
/* AN5005: LIS2DH needs 5ms delay to boot */
k_sleep(K_MSEC(LIS2DH_POR_WAIT_MS));
status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_WAI, &id);
if (status < 0) {
@ -446,15 +444,17 @@ int lis2dh_init(const struct device *dev)
LIS2DH_ODR_BITS);
}
#ifdef CONFIG_PM_DEVICE
static int lis2dh_pm_action(const struct device *dev,
enum pm_device_action action)
{
int status;
int status = 0;
struct lis2dh_data *lis2dh = dev->data;
uint8_t regdata;
switch (action) {
case PM_DEVICE_ACTION_TURN_ON:
status = lis2dh_init_chip(dev);
break;
case PM_DEVICE_ACTION_RESUME:
/* read REFERENCE register (see datasheet rev 6 section 8.9 footnote 1) */
status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_REFERENCE, &regdata);
@ -486,13 +486,28 @@ static int lis2dh_pm_action(const struct device *dev,
return status;
}
break;
case PM_DEVICE_ACTION_TURN_OFF:
break;
default:
return -ENOTSUP;
}
return 0;
return status;
}
static int lis2dh_init(const struct device *dev)
{
const struct lis2dh_config *cfg = dev->config;
int status;
status = cfg->bus_init(dev);
if (status < 0) {
LOG_ERR("Failed to initialize the bus.");
return status;
}
return pm_device_driver_init(dev, lis2dh_pm_action);
}
#endif /* CONFIG_PM_DEVICE */
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
#warning "LIS2DH driver enabled without any devices"

View file

@ -17,6 +17,7 @@
#define LIS2DH_REG_WAI 0x0f
#define LIS2DH_CHIP_ID 0x33
#define LIS2DH_POR_WAIT_MS 5
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
#include <zephyr/drivers/spi.h>
@ -262,9 +263,7 @@ struct lis2dh_data {
struct sensor_value temperature;
#endif
#ifdef CONFIG_PM_DEVICE
uint8_t reg_ctrl1_active_val;
#endif
#ifdef CONFIG_LIS2DH_TRIGGER
const struct device *dev;