Skip to content

Commit 6bf881c

Browse files
pm: device: introduce pm_device_driver_deinit()
Introduce pm_device_driver_deinit() which is required to fully support device_deinit(). This function shall be called by drivers when device_deinit() is called. If PM_DEVICE is enabled, it will simply check whether the device is either SUSPENDED or OFF, this will prevent device_deinit() on a device which is currently in use, and ensure the device is in the correct state if device_init() is called later. If PM_DEVICE is not enabled, it will invoke the SUSPEND action which mirrors the pm_device_driver_init() behavior. Note that TURN_OFF action is not called since the device is deinitialized after this call. Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
1 parent 77829b4 commit 6bf881c

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

include/zephyr/pm/device.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,25 @@ bool pm_device_is_powered(const struct device *dev);
646646
*/
647647
int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb);
648648

649+
/**
650+
* @brief Prepare PM device for device driver deinit
651+
*
652+
* @details Ensures device is either SUSPENDED or OFF. If CONFIG_PM_DEVICE=y,
653+
* the function checks whether power management has moved the device to
654+
* either the SUSPENDED or OFF states. If CONFIG_PM_DEVICE=n, the function
655+
* uses the device driver's internal PM hook to move the device to the
656+
* SUSPENDED state.
657+
*
658+
* @note This function must be called at the beginning of a driver's deinit
659+
* function.
660+
*
661+
* @param dev Device instance.
662+
* @param action_cb Device PM control callback function.
663+
* @retval 0 if success.
664+
* @retval -EBUSY Device is not SUSPENDED nor OFF
665+
* @retval -errno code if failure.
666+
*/
667+
int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb);
649668
#else
650669
static inline int pm_device_state_get(const struct device *dev,
651670
enum pm_device_state *state)
@@ -742,6 +761,11 @@ static inline int pm_device_driver_init(const struct device *dev, pm_device_acti
742761
return 0;
743762
}
744763

764+
static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
765+
{
766+
return action_cb(dev, PM_DEVICE_ACTION_SUSPEND);
767+
}
768+
745769
#endif /* CONFIG_PM_DEVICE */
746770

747771
/** @} */

subsys/pm/device.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,14 @@ int pm_device_driver_init(const struct device *dev,
401401
/* Return the PM_DEVICE_ACTION_RESUME result */
402402
return rc;
403403
}
404+
405+
int pm_device_driver_deinit(const struct device *dev,
406+
pm_device_action_cb_t action_cb)
407+
{
408+
struct pm_device_base *pm = dev->pm_base;
409+
410+
return pm->state == PM_DEVICE_STATE_SUSPENDED ||
411+
pm->state == PM_DEVICE_STATE_OFF ?
412+
0 :
413+
-EBUSY;
414+
}

0 commit comments

Comments
 (0)