8
8
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
9
9
#include <zephyr/drivers/clock_control.h>
10
10
#include <zephyr/pm/device.h>
11
+ #include <zephyr/pm/device_runtime.h>
11
12
#include <zephyr/sys/util.h>
12
13
#include <zephyr/kernel.h>
13
14
#include <soc.h>
@@ -66,13 +67,23 @@ int i2c_stm32_runtime_configure(const struct device *dev, uint32_t config)
66
67
data -> dev_config = config ;
67
68
68
69
k_sem_take (& data -> bus_mutex , K_FOREVER );
70
+
71
+ #ifdef CONFIG_PM_DEVICE_RUNTIME
72
+ (void )pm_device_runtime_get (dev );
73
+ #else
69
74
pm_device_busy_set (dev );
75
+ #endif
70
76
71
77
LL_I2C_Disable (i2c );
72
78
LL_I2C_SetMode (i2c , LL_I2C_MODE_I2C );
73
79
ret = stm32_i2c_configure_timing (dev , clock );
74
80
81
+ #ifdef CONFIG_PM_DEVICE_RUNTIME
82
+ (void )pm_device_runtime_put (dev );
83
+ #else
75
84
pm_device_busy_clear (dev );
85
+ #endif
86
+
76
87
k_sem_give (& data -> bus_mutex );
77
88
78
89
return ret ;
@@ -181,11 +192,20 @@ static int i2c_stm32_transfer(const struct device *dev, struct i2c_msg *msg,
181
192
return ret ;
182
193
}
183
194
195
+ ret = pm_device_runtime_get (dev );
196
+ if (ret < 0 ) {
197
+ return ret ;
198
+ }
199
+
184
200
/* Send out messages */
185
201
k_sem_take (& data -> bus_mutex , K_FOREVER );
186
202
187
203
/* Prevent driver from being suspended by PM until I2C transaction is complete */
204
+ #ifdef CONFIG_PM_DEVICE_RUNTIME
205
+ (void )pm_device_runtime_get (dev );
206
+ #else
188
207
pm_device_busy_set (dev );
208
+ #endif
189
209
190
210
current = msg ;
191
211
@@ -204,9 +224,14 @@ static int i2c_stm32_transfer(const struct device *dev, struct i2c_msg *msg,
204
224
num_msgs -- ;
205
225
}
206
226
227
+ #ifdef CONFIG_PM_DEVICE_RUNTIME
228
+ (void )pm_device_runtime_put (dev );
229
+ #else
207
230
pm_device_busy_clear (dev );
231
+ #endif
208
232
209
233
k_sem_give (& data -> bus_mutex );
234
+
210
235
return ret ;
211
236
}
212
237
@@ -328,6 +353,12 @@ static int i2c_stm32_init(const struct device *dev)
328
353
return ret ;
329
354
}
330
355
356
+ #ifdef CONFIG_PM_DEVICE_RUNTIME
357
+ i2c_stm32_suspend (dev );
358
+ pm_device_init_suspended (dev );
359
+ (void )pm_device_runtime_enable (dev );
360
+ #endif
361
+
331
362
return 0 ;
332
363
}
333
364
0 commit comments