Skip to content

Commit 3198db1

Browse files
bjarki-andreasendkalowsk
authored andcommitted
drivers: sensor: nxp: p3t1755: impl pm device runtime
Implement pm device runtime for p3t1755 sensor. Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
1 parent ea8f69c commit 3198db1

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

drivers/sensor/nxp/p3t1755/p3t1755.c

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <zephyr/sys/__assert.h>
1212
#include <zephyr/logging/log.h>
1313
#include <stdlib.h>
14+
#include <zephyr/pm/device.h>
15+
#include <zephyr/pm/device_runtime.h>
1416

1517
LOG_MODULE_REGISTER(P3T1755, CONFIG_SENSOR_LOG_LEVEL);
1618

@@ -29,22 +31,52 @@ static int p3t1755_i3c_write_reg(const struct device *dev, uint8_t reg, uint8_t
2931

3032
return i3c_burst_write(data->i3c_dev, reg, byte, len);
3133
}
34+
35+
static int p3t1755_i3c_get(const struct device *dev)
36+
{
37+
const struct p3t1755_config *config = dev->config;
38+
39+
return pm_device_runtime_get(config->i3c.bus);
40+
}
41+
42+
static int p3t1755_i3c_put(const struct device *dev)
43+
{
44+
const struct p3t1755_config *config = dev->config;
45+
46+
return pm_device_runtime_put(config->i3c.bus);
47+
}
3248
#endif
3349

3450
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
35-
int p3t1755_i2c_read_reg(const struct device *dev, uint8_t reg, uint8_t *value, uint8_t len)
51+
__maybe_unused static int p3t1755_i2c_read_reg(const struct device *dev, uint8_t reg,
52+
uint8_t *value, uint8_t len)
3653
{
3754
const struct p3t1755_config *config = dev->config;
3855

3956
return i2c_burst_read_dt(&config->bus_cfg.i2c, reg, value, len);
4057
}
4158

42-
int p3t1755_i2c_write_reg(const struct device *dev, uint8_t reg, uint8_t *byte, uint8_t len)
59+
__maybe_unused static int p3t1755_i2c_write_reg(const struct device *dev, uint8_t reg,
60+
uint8_t *byte, uint8_t len)
4361
{
4462
const struct p3t1755_config *config = dev->config;
4563

4664
return i2c_burst_write_dt(&config->bus_cfg.i2c, reg, byte, len);
4765
}
66+
67+
__maybe_unused static int p3t1755_i2c_get(const struct device *dev)
68+
{
69+
const struct p3t1755_config *config = dev->config;
70+
71+
return pm_device_runtime_get(config->bus_cfg.i2c.bus);
72+
}
73+
74+
__maybe_unused static int p3t1755_i2c_put(const struct device *dev)
75+
{
76+
const struct p3t1755_config *config = dev->config;
77+
78+
return pm_device_runtime_put(config->bus_cfg.i2c.bus);
79+
}
4880
#endif
4981

5082
static int p3t1755_sample_fetch(const struct device *dev, enum sensor_channel chan)
@@ -58,6 +90,8 @@ static int p3t1755_sample_fetch(const struct device *dev, enum sensor_channel ch
5890
return -ENOTSUP;
5991
}
6092

93+
config->ops.get(dev);
94+
6195
if (config->oneshot_mode) {
6296
data->config_reg |= P3T1755_CONFIG_REG_OS;
6397
config->ops.write(dev, P3T1755_CONFIG_REG, &data->config_reg, 1);
@@ -67,6 +101,8 @@ static int p3t1755_sample_fetch(const struct device *dev, enum sensor_channel ch
67101

68102
int status = config->ops.read(dev, P3T1755_TEMPERATURE_REG, raw_temp, 2);
69103

104+
config->ops.put(dev);
105+
70106
if (status) {
71107
LOG_ERR("read return error %d", status);
72108
return -ENOTSUP;
@@ -115,25 +151,31 @@ static int p3t1755_channel_get(const struct device *dev, enum sensor_channel cha
115151
return 0;
116152
}
117153

118-
static int p3t1755_init(const struct device *dev)
154+
static int p3t1755_pm_resume(const struct device *dev)
119155
{
120156
const struct p3t1755_config *config = dev->config;
121157
struct p3t1755_data *data = dev->data;
158+
int ret = -ENODEV;
159+
160+
if (config->ops.get(dev)) {
161+
LOG_ERR("Bus device get failed");
162+
goto put_and_ret;
163+
}
122164

123165
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c)
124166
if (config->i3c.bus != NULL) {
125167
data->i3c_dev = i3c_device_find(config->i3c.bus, &config->i3c.dev_id);
126168
if (data->i3c_dev == NULL) {
127169
LOG_ERR("Cannot find I3C device descriptor");
128-
return -ENODEV;
170+
goto put_and_ret;
129171
}
130172
}
131173
#endif
132174
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
133175
if (config->inst_on_bus == P3T1755_BUS_I2C) {
134176
if (!i2c_is_ready_dt(&config->bus_cfg.i2c)) {
135177
LOG_ERR("I2C bus device not ready");
136-
return -ENODEV;
178+
goto put_and_ret;
137179
}
138180
}
139181
#endif
@@ -147,9 +189,35 @@ static int p3t1755_init(const struct device *dev)
147189
config->ops.write(dev, P3T1755_CONFIG_REG, &data->config_reg, 1);
148190
}
149191

150-
LOG_DBG("Init complete");
192+
ret = 0;
151193

152-
return 0;
194+
put_and_ret:
195+
(void)config->ops.put(dev);
196+
197+
return ret;
198+
}
199+
200+
static int p3t1755_pm_hook(const struct device *dev, enum pm_device_action action)
201+
{
202+
int ret;
203+
204+
switch (action) {
205+
case PM_DEVICE_ACTION_SUSPEND:
206+
ret = 0;
207+
break;
208+
case PM_DEVICE_ACTION_RESUME:
209+
ret = p3t1755_pm_resume(dev);
210+
break;
211+
default:
212+
ret = -ENOTSUP;
213+
}
214+
215+
return ret;
216+
}
217+
218+
static int p3t1755_init(const struct device *dev)
219+
{
220+
return pm_device_driver_init(dev, p3t1755_pm_hook);
153221
}
154222

155223
static DEVICE_API(sensor, p3t1755_driver_api) = {
@@ -165,6 +233,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = {
165233
.ops = { \
166234
.read = p3t1755_i2c_read_reg, \
167235
.write = p3t1755_i2c_write_reg, \
236+
.get = p3t1755_i2c_get, \
237+
.put = p3t1755_i2c_put, \
168238
}, \
169239
.inst_on_bus = P3T1755_BUS_I2C,
170240

@@ -176,6 +246,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = {
176246
.ops = { \
177247
.read = p3t1755_i3c_read_reg, \
178248
.write = p3t1755_i3c_write_reg, \
249+
.get = p3t1755_i3c_get, \
250+
.put = p3t1755_i3c_put, \
179251
}, \
180252
.inst_on_bus = P3T1755_BUS_I3C, \
181253
.i3c.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), .i3c.dev_id = I3C_DEVICE_ID_DT_INST(inst),
@@ -189,7 +261,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = {
189261
.oneshot_mode = DT_INST_PROP(n, oneshot_mode), \
190262
}; \
191263
\
192-
SENSOR_DEVICE_DT_INST_DEFINE(n, p3t1755_init, NULL, &p3t1755_data_##n, \
264+
PM_DEVICE_DT_INST_DEFINE(n, p3t1755_pm_hook); \
265+
SENSOR_DEVICE_DT_INST_DEFINE(n, p3t1755_init, PM_DEVICE_DT_INST_GET(n), &p3t1755_data_##n, \
193266
&p3t1755_config_##n, POST_KERNEL, \
194267
CONFIG_SENSOR_INIT_PRIORITY, &p3t1755_driver_api);
195268

drivers/sensor/nxp/p3t1755/p3t1755.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
struct p3t1755_io_ops {
3737
int (*read)(const struct device *dev, uint8_t reg, uint8_t *byte, uint8_t len);
3838
int (*write)(const struct device *dev, uint8_t reg, uint8_t *byte, uint8_t len);
39+
int (*get)(const struct device *dev);
40+
int (*put)(const struct device *dev);
3941
};
4042

4143
union p3t1755_bus_cfg {

0 commit comments

Comments
 (0)