Skip to content

Commit ef85dbe

Browse files
kaessertnashif
authored andcommitted
drivers: sensor: adxl345: add device PM support
Add support for the Zephyr device power management framework to enable runtime power control of the ADXL345 accelerometer. This implementation adds a pm_device_action callback that handles SUSPEND and RESUME actions. When suspended, the sensor enters standby mode for low power operation. On resume, it returns to measurement mode. The implementation follows the standard pattern used by other sensor drivers (lis2dh, bme280, vcnl4040) and integrates with the PM_DEVICE_DT_INST_DEFINE macro for automatic PM setup. Tested on nRF52832 with CONFIG_PM_DEVICE enabled. Signed-off-by: Tobias Kässer <t.kaesser@gmail.com>
1 parent 6e51add commit ef85dbe

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

drivers/sensor/adi/adxl345/adxl345.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <zephyr/drivers/gpio.h>
1212
#include <zephyr/drivers/i2c.h>
1313
#include <zephyr/logging/log.h>
14+
#include <zephyr/pm/device.h>
1415
#include <zephyr/sys/__assert.h>
1516

1617
#include "adxl345.h"
@@ -472,6 +473,9 @@ static int adxl345_init(const struct device *dev)
472473
return -EIO;
473474
}
474475

476+
/* Initialize op_mode to reflect current state */
477+
data->op_mode = ADXL345_MEASURE;
478+
475479
#ifdef CONFIG_ADXL345_TRIGGER
476480
rc = adxl345_init_interrupt(dev);
477481
if (rc < 0) {
@@ -500,6 +504,34 @@ static int adxl345_init(const struct device *dev)
500504
return 0;
501505
}
502506

507+
#ifdef CONFIG_PM_DEVICE
508+
static int adxl345_pm_action(const struct device *dev,
509+
enum pm_device_action action)
510+
{
511+
struct adxl345_dev_data *data = dev->data;
512+
int rc;
513+
514+
switch (action) {
515+
case PM_DEVICE_ACTION_RESUME:
516+
/* Resume to measurement mode */
517+
rc = adxl345_set_op_mode(dev, ADXL345_MEASURE);
518+
if (rc == 0) {
519+
data->op_mode = ADXL345_MEASURE;
520+
}
521+
return rc;
522+
case PM_DEVICE_ACTION_SUSPEND:
523+
/* Enter standby mode for low power */
524+
rc = adxl345_set_op_mode(dev, ADXL345_STANDBY);
525+
if (rc == 0) {
526+
data->op_mode = ADXL345_STANDBY;
527+
}
528+
return rc;
529+
default:
530+
return -ENOTSUP;
531+
}
532+
}
533+
#endif /* CONFIG_PM_DEVICE */
534+
503535
#ifdef CONFIG_ADXL345_TRIGGER
504536

505537
#define ADXL345_CFG_IRQ(inst) \
@@ -604,7 +636,8 @@ static int adxl345_init(const struct device *dev)
604636
COND_CODE_1(DT_INST_ON_BUS(inst, spi), (ADXL345_CONFIG_SPI(inst)), \
605637
(ADXL345_CONFIG_I2C(inst))); \
606638
\
607-
SENSOR_DEVICE_DT_INST_DEFINE(inst, adxl345_init, NULL, \
639+
PM_DEVICE_DT_INST_DEFINE(inst, adxl345_pm_action); \
640+
SENSOR_DEVICE_DT_INST_DEFINE(inst, adxl345_init, PM_DEVICE_DT_INST_GET(inst), \
608641
&adxl345_data_##inst, &adxl345_config_##inst, POST_KERNEL, \
609642
CONFIG_SENSOR_INIT_PRIORITY, &adxl345_api_funcs);
610643

drivers/sensor/adi/adxl345/adxl345.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ struct adxl345_dev_data {
170170
uint8_t is_full_res;
171171
uint8_t selected_range;
172172
enum adxl345_odr odr;
173+
enum adxl345_op_mode op_mode;
173174
#ifdef CONFIG_ADXL345_TRIGGER
174175
struct gpio_callback gpio_cb;
175176

0 commit comments

Comments
 (0)