|
6 | 6 |
|
7 | 7 | #include <zephyr/types.h> |
8 | 8 | #include <zephyr/sys/ring_buffer.h> |
| 9 | +#include <zephyr/sys/atomic.h> |
9 | 10 | #include <zephyr/drivers/uart.h> |
10 | | -#include <zephyr/pm/device.h> |
| 11 | +#include <zephyr/pm/device_runtime.h> |
11 | 12 |
|
12 | 13 | #define MODULE uart_handler |
13 | 14 | #include "module_state_event.h" |
@@ -39,6 +40,10 @@ static const struct device *devices[] = { |
39 | 40 | #define UART_SET_PM_STATE false |
40 | 41 | #endif |
41 | 42 |
|
| 43 | +#if UART_SET_PM_STATE |
| 44 | +atomic_t device_pm_requested = ATOMIC_INIT(0); |
| 45 | +#endif |
| 46 | + |
42 | 47 | struct uart_rx_buf { |
43 | 48 | atomic_t ref_counter; |
44 | 49 | size_t len; |
@@ -231,14 +236,23 @@ static void set_uart_power_state(uint8_t dev_idx, bool active) |
231 | 236 | { |
232 | 237 | #if UART_SET_PM_STATE |
233 | 238 | const struct device *dev = devices[dev_idx]; |
| 239 | + const char *action; |
234 | 240 | int err; |
235 | | - enum pm_device_action action; |
236 | 241 |
|
237 | | - action = active ? PM_DEVICE_ACTION_RESUME : PM_DEVICE_ACTION_SUSPEND; |
| 242 | + if (active) { |
| 243 | + if (!atomic_test_and_set_bit(&device_pm_requested, dev_idx)) { |
| 244 | + action = "pm_device_runtime_get"; |
| 245 | + err = pm_device_runtime_get(dev); |
| 246 | + } |
| 247 | + } else { |
| 248 | + if (atomic_test_and_clear_bit(&device_pm_requested, dev_idx)) { |
| 249 | + action = "pm_device_runtime_put"; |
| 250 | + err = pm_device_runtime_put(dev); |
| 251 | + } |
| 252 | + } |
238 | 253 |
|
239 | | - err = pm_device_action_run(dev, action); |
240 | | - if ((err < 0) && (err != -EALREADY)) { |
241 | | - LOG_ERR("pm_device_action_run failed: %d", err); |
| 254 | + if (err < 0) { |
| 255 | + LOG_ERR("%s failed: %d", action, err); |
242 | 256 | } |
243 | 257 | #endif |
244 | 258 | } |
|
0 commit comments