Skip to content

Commit 400fa70

Browse files
fougestephanosio
authored andcommitted
drivers: i2c: stm32: PM device runtime support
Lock I2C device state when used so that Power Manager doesn't suspend the device. Initial state is a suspended device. Signed-off-by: Cyril Fougeray <[email protected]>
1 parent 7710082 commit 400fa70

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

drivers/i2c/i2c_ll_stm32.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
99
#include <zephyr/drivers/clock_control.h>
1010
#include <zephyr/pm/device.h>
11+
#include <zephyr/pm/device_runtime.h>
1112
#include <zephyr/sys/util.h>
1213
#include <zephyr/kernel.h>
1314
#include <soc.h>
@@ -66,13 +67,23 @@ int i2c_stm32_runtime_configure(const struct device *dev, uint32_t config)
6667
data->dev_config = config;
6768

6869
k_sem_take(&data->bus_mutex, K_FOREVER);
70+
71+
#ifdef CONFIG_PM_DEVICE_RUNTIME
72+
(void)pm_device_runtime_get(dev);
73+
#else
6974
pm_device_busy_set(dev);
75+
#endif
7076

7177
LL_I2C_Disable(i2c);
7278
LL_I2C_SetMode(i2c, LL_I2C_MODE_I2C);
7379
ret = stm32_i2c_configure_timing(dev, clock);
7480

81+
#ifdef CONFIG_PM_DEVICE_RUNTIME
82+
(void)pm_device_runtime_put(dev);
83+
#else
7584
pm_device_busy_clear(dev);
85+
#endif
86+
7687
k_sem_give(&data->bus_mutex);
7788

7889
return ret;
@@ -181,11 +192,20 @@ static int i2c_stm32_transfer(const struct device *dev, struct i2c_msg *msg,
181192
return ret;
182193
}
183194

195+
ret = pm_device_runtime_get(dev);
196+
if (ret < 0) {
197+
return ret;
198+
}
199+
184200
/* Send out messages */
185201
k_sem_take(&data->bus_mutex, K_FOREVER);
186202

187203
/* 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
188207
pm_device_busy_set(dev);
208+
#endif
189209

190210
current = msg;
191211

@@ -204,9 +224,14 @@ static int i2c_stm32_transfer(const struct device *dev, struct i2c_msg *msg,
204224
num_msgs--;
205225
}
206226

227+
#ifdef CONFIG_PM_DEVICE_RUNTIME
228+
(void)pm_device_runtime_put(dev);
229+
#else
207230
pm_device_busy_clear(dev);
231+
#endif
208232

209233
k_sem_give(&data->bus_mutex);
234+
210235
return ret;
211236
}
212237

@@ -328,6 +353,12 @@ static int i2c_stm32_init(const struct device *dev)
328353
return ret;
329354
}
330355

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+
331362
return 0;
332363
}
333364

0 commit comments

Comments
 (0)