Skip to content

Commit 464abe7

Browse files
dancollinscarlescufi
authored andcommitted
sensor: st: lis3mdl: Add support for device power management
This commit adds support for device power management to the LIS3MDL magnetometer driver. Signed-off-by: Dan Collins <dan@collinsnz.com>
1 parent 6013642 commit 464abe7

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

drivers/sensor/st/lis3mdl/lis3mdl.c

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

@@ -162,6 +163,41 @@ int lis3mdl_init(const struct device *dev)
162163
return 0;
163164
}
164165

166+
#ifdef CONFIG_PM_DEVICE
167+
static int lis3mdl_pm_action(const struct device *dev, enum pm_device_action action)
168+
{
169+
const struct lis3mdl_config *config = dev->config;
170+
uint8_t ctrl_reg3;
171+
172+
switch (action) {
173+
case PM_DEVICE_ACTION_RESUME:
174+
ctrl_reg3 = LIS3MDL_MD_CONTINUOUS;
175+
176+
if (i2c_reg_write_byte_dt(&config->i2c, LIS3MDL_REG_CTRL3, ctrl_reg3) < 0) {
177+
LOG_DBG("Failed to configure chip.");
178+
return -EIO;
179+
}
180+
181+
LOG_DBG("State changed to active");
182+
break;
183+
case PM_DEVICE_ACTION_SUSPEND:
184+
ctrl_reg3 = LIS3MDL_MD_POWER_DOWN;
185+
186+
if (i2c_reg_write_byte_dt(&config->i2c, LIS3MDL_REG_CTRL3, ctrl_reg3) < 0) {
187+
LOG_DBG("Failed to configure chip.");
188+
return -EIO;
189+
}
190+
191+
LOG_DBG("State changed to inactive");
192+
break;
193+
default:
194+
return -ENOTSUP;
195+
}
196+
197+
return 0;
198+
}
199+
#endif
200+
165201
#define LIS3MDL_DEFINE(inst) \
166202
static struct lis3mdl_data lis3mdl_data_##inst; \
167203
\
@@ -171,7 +207,10 @@ int lis3mdl_init(const struct device *dev)
171207
(.irq_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, irq_gpios, { 0 }),)) \
172208
}; \
173209
\
174-
SENSOR_DEVICE_DT_INST_DEFINE(inst, lis3mdl_init, NULL, \
210+
PM_DEVICE_DT_INST_DEFINE(inst, lis3mdl_pm_action); \
211+
\
212+
SENSOR_DEVICE_DT_INST_DEFINE(inst, lis3mdl_init, \
213+
PM_DEVICE_DT_INST_GET(inst), \
175214
&lis3mdl_data_##inst, &lis3mdl_config_##inst, POST_KERNEL, \
176215
CONFIG_SENSOR_INIT_PRIORITY, &lis3mdl_driver_api); \
177216

0 commit comments

Comments
 (0)