Skip to content

Commit 60b1b96

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 ddad4c2 commit 60b1b96

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

include/zephyr/pm/device.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,23 @@ bool pm_device_is_powered(const struct device *dev);
638638
*/
639639
int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb);
640640

641+
/**
642+
* @brief Deinit a device driver
643+
*
644+
* @details Ensures device is either SUSPENDED or OFF, and will revert the actions of
645+
* pm_device_driver_init() if this is the case. Must be called at the beginning of a
646+
* driver's deinit function.
647+
*
648+
* @note If CONFIG_PM_DEVICE=n, PM_DEVICE_ACTION_SUSPEND will be invoked
649+
* unconditionally.
650+
*
651+
* @param dev Device instance.
652+
* @param action_cb Device PM control callback function.
653+
* @retval 0 if success.
654+
* @retval -EBUSY Device is not SUSPENDED nor OFF
655+
* @retval -errno code if failure.
656+
*/
657+
int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb);
641658
#else
642659
static inline int pm_device_state_get(const struct device *dev,
643660
enum pm_device_state *state)
@@ -734,6 +751,11 @@ static inline int pm_device_driver_init(const struct device *dev, pm_device_acti
734751
return 0;
735752
}
736753

754+
static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
755+
{
756+
return action_cb(dev, PM_DEVICE_ACTION_SUSPEND);
757+
}
758+
737759
#endif /* CONFIG_PM_DEVICE */
738760

739761
/** @} */

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)