[PATCH 1/3] thermal/core: Encapsulate the set_cur_state function

From: Daniel Lezcano
Date: Wed Jun 01 2022 - 11:15:11 EST


Concentrate the actions in a single place when a cooling device state
is changed. Provide a function to do that instead of calling the
underlying ops.

Signed-off-by: Daniel Lezcano <daniel.lezcano@xxxxxxxxxx>
---
drivers/thermal/thermal_core.h | 1 +
drivers/thermal/thermal_helpers.c | 32 ++++++++++++++++++++++++-------
drivers/thermal/thermal_sysfs.c | 7 ++-----
3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 726e327b4205..4689f6cf898f 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -65,6 +65,7 @@ static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
cdev->ops->power2state;
}

+int thermal_cdev_set_state(struct thermal_cooling_device *cdev, int state);
void thermal_cdev_update(struct thermal_cooling_device *);
void __thermal_cdev_update(struct thermal_cooling_device *cdev);

diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c
index 3edd047e144f..d5f162fad1ab 100644
--- a/drivers/thermal/thermal_helpers.c
+++ b/drivers/thermal/thermal_helpers.c
@@ -182,14 +182,32 @@ void thermal_set_delay_jiffies(unsigned long *delay_jiffies, int delay_ms)
*delay_jiffies = round_jiffies(*delay_jiffies);
}

-static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
- int target)
+/**
+ * thermal_cdev_set_state - set the cooling device state
+ * @cdev: a pointer to a thermal_cooling_device
+ * @state: the target state
+ *
+ * Set the state of the cooling device passed as parameter. The
+ * cooling device lock must be held when calling this function.
+ *
+ * Return: 0 in case of success, otherwise the return value is the one
+ * returned by the backend for the ops
+ */
+int thermal_cdev_set_state(struct thermal_cooling_device *cdev, int state)
{
- if (cdev->ops->set_cur_state(cdev, target))
- return;
+ int ret;

- thermal_notify_cdev_state_update(cdev->id, target);
- thermal_cooling_device_stats_update(cdev, target);
+ /*
+ * No check is needed for the ops->set_cur_state as the
+ * registering function checked the ops are correctly set
+ */
+ ret = cdev->ops->set_cur_state(cdev, state);
+ if (!ret) {
+ thermal_notify_cdev_state_update(cdev->id, state);
+ thermal_cooling_device_stats_update(cdev, state);
+ }
+
+ return ret;
}

void __thermal_cdev_update(struct thermal_cooling_device *cdev)
@@ -207,7 +225,7 @@ void __thermal_cdev_update(struct thermal_cooling_device *cdev)
target = instance->target;
}

- thermal_cdev_set_cur_state(cdev, target);
+ thermal_cdev_set_state(cdev, target);

trace_cdev_update(cdev, target);
dev_dbg(&cdev->device, "set to state %lu\n", target);
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index 1c4aac8464a7..935e79909121 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -617,12 +617,9 @@ cur_state_store(struct device *dev, struct device_attribute *attr,
return -EINVAL;

mutex_lock(&cdev->lock);
-
- result = cdev->ops->set_cur_state(cdev, state);
- if (!result)
- thermal_cooling_device_stats_update(cdev, state);
-
+ result = thermal_cdev_set_state(cdev, state);
mutex_unlock(&cdev->lock);
+
return result ? result : count;
}

--
2.25.1