11
11
#include <zephyr/sys/__assert.h>
12
12
#include <zephyr/logging/log.h>
13
13
#include <stdlib.h>
14
+ #include <zephyr/pm/device.h>
15
+ #include <zephyr/pm/device_runtime.h>
14
16
15
17
LOG_MODULE_REGISTER (P3T1755 , CONFIG_SENSOR_LOG_LEVEL );
16
18
@@ -29,22 +31,52 @@ static int p3t1755_i3c_write_reg(const struct device *dev, uint8_t reg, uint8_t
29
31
30
32
return i3c_burst_write (data -> i3c_dev , reg , byte , len );
31
33
}
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
+ }
32
48
#endif
33
49
34
50
#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 )
36
53
{
37
54
const struct p3t1755_config * config = dev -> config ;
38
55
39
56
return i2c_burst_read_dt (& config -> bus_cfg .i2c , reg , value , len );
40
57
}
41
58
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 )
43
61
{
44
62
const struct p3t1755_config * config = dev -> config ;
45
63
46
64
return i2c_burst_write_dt (& config -> bus_cfg .i2c , reg , byte , len );
47
65
}
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
+ }
48
80
#endif
49
81
50
82
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
58
90
return - ENOTSUP ;
59
91
}
60
92
93
+ config -> ops .get (dev );
94
+
61
95
if (config -> oneshot_mode ) {
62
96
data -> config_reg |= P3T1755_CONFIG_REG_OS ;
63
97
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
67
101
68
102
int status = config -> ops .read (dev , P3T1755_TEMPERATURE_REG , raw_temp , 2 );
69
103
104
+ config -> ops .put (dev );
105
+
70
106
if (status ) {
71
107
LOG_ERR ("read return error %d" , status );
72
108
return - ENOTSUP ;
@@ -115,25 +151,31 @@ static int p3t1755_channel_get(const struct device *dev, enum sensor_channel cha
115
151
return 0 ;
116
152
}
117
153
118
- static int p3t1755_init (const struct device * dev )
154
+ static int p3t1755_pm_resume (const struct device * dev )
119
155
{
120
156
const struct p3t1755_config * config = dev -> config ;
121
157
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
+ }
122
164
123
165
#if DT_ANY_INST_ON_BUS_STATUS_OKAY (i3c )
124
166
if (config -> i3c .bus != NULL ) {
125
167
data -> i3c_dev = i3c_device_find (config -> i3c .bus , & config -> i3c .dev_id );
126
168
if (data -> i3c_dev == NULL ) {
127
169
LOG_ERR ("Cannot find I3C device descriptor" );
128
- return - ENODEV ;
170
+ goto put_and_ret ;
129
171
}
130
172
}
131
173
#endif
132
174
#if DT_ANY_INST_ON_BUS_STATUS_OKAY (i2c )
133
175
if (config -> inst_on_bus == P3T1755_BUS_I2C ) {
134
176
if (!i2c_is_ready_dt (& config -> bus_cfg .i2c )) {
135
177
LOG_ERR ("I2C bus device not ready" );
136
- return - ENODEV ;
178
+ goto put_and_ret ;
137
179
}
138
180
}
139
181
#endif
@@ -147,9 +189,35 @@ static int p3t1755_init(const struct device *dev)
147
189
config -> ops .write (dev , P3T1755_CONFIG_REG , & data -> config_reg , 1 );
148
190
}
149
191
150
- LOG_DBG ( "Init complete" ) ;
192
+ ret = 0 ;
151
193
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 );
153
221
}
154
222
155
223
static DEVICE_API (sensor , p3t1755_driver_api ) = {
@@ -165,6 +233,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = {
165
233
.ops = { \
166
234
.read = p3t1755_i2c_read_reg, \
167
235
.write = p3t1755_i2c_write_reg, \
236
+ .get = p3t1755_i2c_get, \
237
+ .put = p3t1755_i2c_put, \
168
238
}, \
169
239
.inst_on_bus = P3T1755_BUS_I2C,
170
240
@@ -176,6 +246,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = {
176
246
.ops = { \
177
247
.read = p3t1755_i3c_read_reg, \
178
248
.write = p3t1755_i3c_write_reg, \
249
+ .get = p3t1755_i3c_get, \
250
+ .put = p3t1755_i3c_put, \
179
251
}, \
180
252
.inst_on_bus = P3T1755_BUS_I3C, \
181
253
.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) = {
189
261
.oneshot_mode = DT_INST_PROP(n, oneshot_mode), \
190
262
}; \
191
263
\
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, \
193
266
&p3t1755_config_##n, POST_KERNEL, \
194
267
CONFIG_SENSOR_INIT_PRIORITY, &p3t1755_driver_api);
195
268
0 commit comments