diff --git a/subsys/pm/device_runtime.c b/subsys/pm/device_runtime.c index 56f0b066a91..a4b64fbe994 100644 --- a/subsys/pm/device_runtime.c +++ b/subsys/pm/device_runtime.c @@ -177,8 +177,22 @@ int pm_device_runtime_get(const struct device *dev) pm->usage++; + /* + * Check if the device has a pending suspend operation (not started + * yet) and cancel it. This way we avoid unnecessary operations because + * the device is actually active. + */ + if ((pm->state == PM_DEVICE_STATE_SUSPENDING) && + ((k_work_cancel_delayable(&pm->work) & K_WORK_RUNNING) == 0)) { + pm->state = PM_DEVICE_STATE_ACTIVE; + goto unlock; + } + if (!k_is_pre_kernel()) { - /* wait until possible async suspend is completed */ + /* + * If the device is already suspending there is + * nothing else we can do but wait until it finishes. + */ while (pm->state == PM_DEVICE_STATE_SUSPENDING) { k_sem_give(&pm->lock);