Skip to content

Commit 495450d

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 followed by the OFF action, which mirrors the pm_device_driver_init() behavior. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 0c82c35 commit 495450d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

include/zephyr/pm/device.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,17 @@ bool pm_device_is_powered(const struct device *dev);
635635
*/
636636
int pm_device_driver_init(const struct device *dev, pm_device_action_cb_t action_cb);
637637

638+
/**
639+
* @brief Deinit a device driver
640+
*
641+
* This function must be called at the end of a driver deinit function.
642+
*
643+
* @param dev Device instance.
644+
* @param action_cb Device PM control callback function.
645+
* @retval 0 if success.
646+
* @retval -errno code if failure.
647+
*/
648+
int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb);
638649
#else
639650
static inline int pm_device_state_get(const struct device *dev,
640651
enum pm_device_state *state)
@@ -731,6 +742,23 @@ static inline int pm_device_driver_init(const struct device *dev, pm_device_acti
731742
return 0;
732743
}
733744

745+
static inline int pm_device_driver_deinit(const struct device *dev, pm_device_action_cb_t action_cb)
746+
{
747+
int rc;
748+
749+
rc = action_cb(dev, PM_DEVICE_ACTION_SUSPEND);
750+
if (rc < 0) {
751+
return rc;
752+
}
753+
754+
rc = action_cb(dev, PM_DEVICE_ACTION_TURN_OFF);
755+
if (rc < 0 && rc != -ENOTSUP) {
756+
return rc;
757+
}
758+
759+
return 0;
760+
}
761+
734762
#endif /* CONFIG_PM_DEVICE */
735763

736764
/** @} */

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)