Skip to content

Commit b12717b

Browse files
petrosyan-vankartben
authored andcommitted
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>
1 parent 47e43d5 commit b12717b

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

drivers/sensor/st/lis2dh/lis2dh.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,16 @@ static DEVICE_API(sensor, lis2dh_driver_api) = {
344344
.channel_get = lis2dh_channel_get,
345345
};
346346

347-
int lis2dh_init(const struct device *dev)
347+
int lis2dh_init_chip(const struct device *dev)
348348
{
349349
struct lis2dh_data *lis2dh = dev->data;
350350
const struct lis2dh_config *cfg = dev->config;
351351
int status;
352352
uint8_t id;
353353
uint8_t raw[6];
354354

355-
status = cfg->bus_init(dev);
356-
if (status < 0) {
357-
return status;
358-
}
355+
/* AN5005: LIS2DH needs 5ms delay to boot */
356+
k_sleep(K_MSEC(LIS2DH_POR_WAIT_MS));
359357

360358
status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_WAI, &id);
361359
if (status < 0) {
@@ -446,15 +444,17 @@ int lis2dh_init(const struct device *dev)
446444
LIS2DH_ODR_BITS);
447445
}
448446

449-
#ifdef CONFIG_PM_DEVICE
450447
static int lis2dh_pm_action(const struct device *dev,
451448
enum pm_device_action action)
452449
{
453-
int status;
450+
int status = 0;
454451
struct lis2dh_data *lis2dh = dev->data;
455452
uint8_t regdata;
456453

457454
switch (action) {
455+
case PM_DEVICE_ACTION_TURN_ON:
456+
status = lis2dh_init_chip(dev);
457+
break;
458458
case PM_DEVICE_ACTION_RESUME:
459459
/* read REFERENCE register (see datasheet rev 6 section 8.9 footnote 1) */
460460
status = lis2dh->hw_tf->read_reg(dev, LIS2DH_REG_REFERENCE, &regdata);
@@ -486,13 +486,28 @@ static int lis2dh_pm_action(const struct device *dev,
486486
return status;
487487
}
488488
break;
489+
case PM_DEVICE_ACTION_TURN_OFF:
490+
break;
489491
default:
490492
return -ENOTSUP;
491493
}
492494

493-
return 0;
495+
return status;
496+
}
497+
498+
static int lis2dh_init(const struct device *dev)
499+
{
500+
const struct lis2dh_config *cfg = dev->config;
501+
int status;
502+
503+
status = cfg->bus_init(dev);
504+
if (status < 0) {
505+
LOG_ERR("Failed to initialize the bus.");
506+
return status;
507+
}
508+
509+
return pm_device_driver_init(dev, lis2dh_pm_action);
494510
}
495-
#endif /* CONFIG_PM_DEVICE */
496511

497512
#if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
498513
#warning "LIS2DH driver enabled without any devices"

drivers/sensor/st/lis2dh/lis2dh.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#define LIS2DH_REG_WAI 0x0f
1919
#define LIS2DH_CHIP_ID 0x33
20+
#define LIS2DH_POR_WAIT_MS 5
2021

2122
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
2223
#include <zephyr/drivers/spi.h>
@@ -262,9 +263,7 @@ struct lis2dh_data {
262263
struct sensor_value temperature;
263264
#endif
264265

265-
#ifdef CONFIG_PM_DEVICE
266266
uint8_t reg_ctrl1_active_val;
267-
#endif
268267

269268
#ifdef CONFIG_LIS2DH_TRIGGER
270269
const struct device *dev;

0 commit comments

Comments
 (0)