9
9
#include <zephyr/device.h>
10
10
#include <zephyr/drivers/i2c.h>
11
11
#include <zephyr/kernel.h>
12
+ #include <zephyr/pm/device.h>
12
13
#include <zephyr/drivers/sensor.h>
13
14
#include <zephyr/sys/__assert.h>
14
15
#include <zephyr/logging/log.h>
@@ -180,15 +181,12 @@ static int sht4x_attr_set(const struct device *dev,
180
181
return 0 ;
181
182
}
182
183
183
- static int sht4x_init (const struct device * dev )
184
+ static int sht4x_init_chip (const struct device * dev )
184
185
{
185
- const struct sht4x_config * cfg = dev -> config ;
186
- int rc = 0 ;
186
+ int rc ;
187
187
188
- if (!device_is_ready (cfg -> bus .bus )) {
189
- LOG_ERR ("Device not ready." );
190
- return - ENODEV ;
191
- }
188
+ /* 1 ms (max) power up time according to datasheet */
189
+ k_sleep (K_MSEC (SHT4X_POR_WAIT_MS ));
192
190
193
191
rc = sht4x_write_command (dev , SHT4X_CMD_RESET );
194
192
if (rc < 0 ) {
@@ -197,10 +195,42 @@ static int sht4x_init(const struct device *dev)
197
195
}
198
196
199
197
k_sleep (K_MSEC (SHT4X_RESET_WAIT_MS ));
200
-
201
198
return 0 ;
202
199
}
203
200
201
+ static int sht4x_pm_action (const struct device * dev , enum pm_device_action action )
202
+ {
203
+ int rc = 0 ;
204
+
205
+ switch (action ) {
206
+ case PM_DEVICE_ACTION_TURN_ON :
207
+ rc = sht4x_init_chip (dev );
208
+ break ;
209
+
210
+ case PM_DEVICE_ACTION_RESUME :
211
+ case PM_DEVICE_ACTION_SUSPEND :
212
+ case PM_DEVICE_ACTION_TURN_OFF :
213
+ break ;
214
+
215
+ default :
216
+ return - ENOTSUP ;
217
+ }
218
+
219
+ return rc ;
220
+ }
221
+
222
+ static int sht4x_init (const struct device * dev )
223
+ {
224
+ const struct sht4x_config * cfg = dev -> config ;
225
+
226
+ if (!device_is_ready (cfg -> bus .bus )) {
227
+ LOG_ERR ("Device not ready." );
228
+ return - ENODEV ;
229
+ }
230
+
231
+ return pm_device_driver_init (dev , sht4x_pm_action );
232
+ }
233
+
204
234
205
235
static DEVICE_API (sensor , sht4x_api ) = {
206
236
.sample_fetch = sht4x_sample_fetch ,
@@ -215,10 +245,10 @@ static DEVICE_API(sensor, sht4x_api) = {
215
245
.bus = I2C_DT_SPEC_INST_GET(n), \
216
246
.repeatability = DT_INST_PROP(n, repeatability) \
217
247
}; \
218
- \
248
+ PM_DEVICE_DT_INST_DEFINE(n, sht4x_pm_action); \
219
249
SENSOR_DEVICE_DT_INST_DEFINE(n, \
220
250
sht4x_init, \
221
- NULL, \
251
+ PM_DEVICE_DT_INST_GET(n), \
222
252
&sht4x_data_##n, \
223
253
&sht4x_config_##n, \
224
254
POST_KERNEL, \
0 commit comments