pm: device_runtime: simplify error handling for get/put
In case runtime PM is not enabled (or not built-in), the get/put functions always return 0 (instead of -ENOTSUP/-ENOSYS). When runtime PM is disabled, a device is left into active state. Similarly, when device runtime PM is not built-in, it is safe to assume that a device will be active when it is called. If a user implements a custom solution, it is its responsability to make sure that a device is active when using it. For all these reasons, the -ENOTSUP/-ENOSYS are error codes that should always be ignored by devices using get/put, since in practice it means that: device is active, function is a no-op. The example below illustrates how error handling is simplified: ```c /* before: safe to ignore -ENOSYS/-ENOTSUP since device is active (we * can continue) */ ret = pm_device_runtime_get(dev); if ((ret < 0) && (ret != -ENOSYS) && (ret != -ENOTSUP)) { return ret; } /* now */ ret = pm_device_runtime_get(dev); if (ret < 0) { return ret; } ``` Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
parent
da1663ed3b
commit
59b455eb9c
3 changed files with 13 additions and 19 deletions
|
@ -65,9 +65,8 @@ int pm_device_runtime_disable(const struct device *dev);
|
|||
*
|
||||
* @param dev Device instance.
|
||||
*
|
||||
* @retval 0 If the device has been resumed successfully.
|
||||
* @retval -ENOSTUP If runtime PM is not enabled for the device.
|
||||
* @retval -ENOSYS If the functionality is not available.
|
||||
* @retval 0 If it succeeds. In case device runtime PM is not enabled or not
|
||||
* available this function will be a no-op and will also return 0.
|
||||
* @retval -errno Other negative errno, result of the PM action callback.
|
||||
*/
|
||||
int pm_device_runtime_get(const struct device *dev);
|
||||
|
@ -84,9 +83,8 @@ int pm_device_runtime_get(const struct device *dev);
|
|||
*
|
||||
* @param dev Device instance.
|
||||
*
|
||||
* @retval 0 If device has been suspended successfully.
|
||||
* @retval -ENOSTUP If runtime PM is not enabled for the device.
|
||||
* @retval -ENOSYS If the functionality is not available.
|
||||
* @retval 0 If it succeeds. In case device runtime PM is not enabled or not
|
||||
* available this function will be a no-op and will also return 0.
|
||||
* @retval -EALREADY If device is already suspended (can only happen if get/put
|
||||
* calls are unbalanced).
|
||||
* @retval -errno Other negative errno, result of the action callback.
|
||||
|
@ -110,9 +108,8 @@ int pm_device_runtime_put(const struct device *dev);
|
|||
*
|
||||
* @param dev Device instance.
|
||||
*
|
||||
* @retval 0 If device has queued for suspend.
|
||||
* @retval -ENOSTUP If runtime PM is not enabled for the device.
|
||||
* @retval -ENOSYS If the functionality is not available.
|
||||
* @retval 0 If it succeeds. In case device runtime PM is not enabled or not
|
||||
* available this function will be a no-op and will also return 0.
|
||||
* @retval -EALREADY If device is already suspended (can only happen if get/put
|
||||
* calls are unbalanced).
|
||||
*
|
||||
|
@ -137,9 +134,9 @@ bool pm_device_runtime_is_enabled(const struct device *dev);
|
|||
#else
|
||||
static inline int pm_device_runtime_enable(const struct device *dev) { return -ENOSYS; }
|
||||
static inline int pm_device_runtime_disable(const struct device *dev) { return -ENOSYS; }
|
||||
static inline int pm_device_runtime_get(const struct device *dev) { return -ENOSYS; }
|
||||
static inline int pm_device_runtime_put(const struct device *dev) { return -ENOSYS; }
|
||||
static inline int pm_device_runtime_put_async(const struct device *dev) { return -ENOSYS; }
|
||||
static inline int pm_device_runtime_get(const struct device *dev) { return 0; }
|
||||
static inline int pm_device_runtime_put(const struct device *dev) { return 0; }
|
||||
static inline int pm_device_runtime_put_async(const struct device *dev) { return 0; }
|
||||
static inline bool pm_device_runtime_is_enabled(const struct device *dev) { return false; }
|
||||
#endif
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ LOG_MODULE_DECLARE(pm_device, CONFIG_PM_DEVICE_LOG_LEVEL);
|
|||
* @param async Perform operation asynchronously.
|
||||
*
|
||||
* @retval 0 If device has been suspended or queued for suspend.
|
||||
* @retval -ENOSTUP If runtime PM is not enabled for the device.
|
||||
* @retval -EALREADY If device is already suspended (can only happen if get/put
|
||||
* calls are unbalanced).
|
||||
* @retval -errno Other negative errno, result of the action callback.
|
||||
|
@ -42,7 +41,6 @@ static int runtime_suspend(const struct device *dev, bool async)
|
|||
}
|
||||
|
||||
if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) {
|
||||
ret = -ENOTSUP;
|
||||
goto unlock;
|
||||
}
|
||||
|
||||
|
@ -109,7 +107,6 @@ int pm_device_runtime_get(const struct device *dev)
|
|||
}
|
||||
|
||||
if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) {
|
||||
ret = -ENOTSUP;
|
||||
goto unlock;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,13 +36,13 @@ static void test_api_setup(void)
|
|||
{
|
||||
int ret;
|
||||
|
||||
/* make sure API is not usable (runtime PM disabled) */
|
||||
/* check API always returns 0 when runtime PM is disabled */
|
||||
ret = pm_device_runtime_get(dev);
|
||||
zassert_equal(ret, -ENOTSUP, NULL);
|
||||
zassert_equal(ret, 0, NULL);
|
||||
ret = pm_device_runtime_put(dev);
|
||||
zassert_equal(ret, -ENOTSUP, NULL);
|
||||
zassert_equal(ret, 0, NULL);
|
||||
ret = pm_device_runtime_put_async(dev);
|
||||
zassert_equal(ret, -ENOTSUP, NULL);
|
||||
zassert_equal(ret, 0, NULL);
|
||||
|
||||
/* enable runtime PM */
|
||||
ret = pm_device_runtime_enable(dev);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue