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:
Gerard Marull-Paretas 2021-11-25 11:36:49 +01:00 committed by Carles Cufí
commit 59b455eb9c
3 changed files with 13 additions and 19 deletions

View file

@ -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