Skip to content

Commit 8d2010e

Browse files
petrosyan-vandkalowsk
authored andcommitted
sensor: sht4x: add device power management support
Registers driver with pm_device_driver_init(). Peform software reset on TURN_ON. Added a small delay after power-up Signed-off-by: Van Petrosyan <[email protected]>
1 parent 936d027 commit 8d2010e

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

drivers/sensor/sensirion/sht4x/sht4x.c

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <zephyr/device.h>
1010
#include <zephyr/drivers/i2c.h>
1111
#include <zephyr/kernel.h>
12+
#include <zephyr/pm/device.h>
1213
#include <zephyr/drivers/sensor.h>
1314
#include <zephyr/sys/__assert.h>
1415
#include <zephyr/logging/log.h>
@@ -180,15 +181,12 @@ static int sht4x_attr_set(const struct device *dev,
180181
return 0;
181182
}
182183

183-
static int sht4x_init(const struct device *dev)
184+
static int sht4x_init_chip(const struct device *dev)
184185
{
185-
const struct sht4x_config *cfg = dev->config;
186-
int rc = 0;
186+
int rc;
187187

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));
192190

193191
rc = sht4x_write_command(dev, SHT4X_CMD_RESET);
194192
if (rc < 0) {
@@ -197,10 +195,42 @@ static int sht4x_init(const struct device *dev)
197195
}
198196

199197
k_sleep(K_MSEC(SHT4X_RESET_WAIT_MS));
200-
201198
return 0;
202199
}
203200

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+
204234

205235
static DEVICE_API(sensor, sht4x_api) = {
206236
.sample_fetch = sht4x_sample_fetch,
@@ -215,10 +245,10 @@ static DEVICE_API(sensor, sht4x_api) = {
215245
.bus = I2C_DT_SPEC_INST_GET(n), \
216246
.repeatability = DT_INST_PROP(n, repeatability) \
217247
}; \
218-
\
248+
PM_DEVICE_DT_INST_DEFINE(n, sht4x_pm_action); \
219249
SENSOR_DEVICE_DT_INST_DEFINE(n, \
220250
sht4x_init, \
221-
NULL, \
251+
PM_DEVICE_DT_INST_GET(n), \
222252
&sht4x_data_##n, \
223253
&sht4x_config_##n, \
224254
POST_KERNEL, \

drivers/sensor/sensirion/sht4x/sht4x.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define SHT4X_CMD_RESET 0x94
1414

1515
#define SHT4X_RESET_WAIT_MS 1
16+
#define SHT4X_POR_WAIT_MS 1
1617

1718
#define SHT4X_HEATER_POWER_IDX_MAX 3
1819
#define SHT4X_HEATER_DURATION_IDX_MAX 2

0 commit comments

Comments
 (0)