11
11
#include <zephyr/sys/__assert.h>
12
12
#include <zephyr/sys/byteorder.h>
13
13
#include <zephyr/drivers/sensor.h>
14
+ #include <zephyr/pm/device.h>
14
15
#include <string.h>
15
16
#include <zephyr/logging/log.h>
16
17
@@ -162,6 +163,41 @@ int lis3mdl_init(const struct device *dev)
162
163
return 0 ;
163
164
}
164
165
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
+
165
201
#define LIS3MDL_DEFINE (inst ) \
166
202
static struct lis3mdl_data lis3mdl_data_##inst; \
167
203
\
@@ -171,7 +207,10 @@ int lis3mdl_init(const struct device *dev)
171
207
(.irq_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, irq_gpios, { 0 }),)) \
172
208
}; \
173
209
\
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), \
175
214
&lis3mdl_data_##inst, &lis3mdl_config_##inst, POST_KERNEL, \
176
215
CONFIG_SENSOR_INIT_PRIORITY, &lis3mdl_driver_api); \
177
216
0 commit comments