Currently the default behavior is to manually having the devfreq
backend to register themselves as a devfreq cooling device.

Instead of adding the code in the drivers for the thermal cooling
device registering, let's provide a flag in the devfreq's profile to
tell the common devfreq code to register the newly created devfreq as
a cooling device.

Suggested-by: Chanwoo Choi <[email protected]>
Signed-off-by: Daniel Lezcano <[email protected]>
Reviewed-by: Steven Price <[email protected]>
Reviewed-by: Lukasz Luba <[email protected]>
---
 V6:
   - Did one line string instead of multiline
   - Removed thermal.h header inclusion as it is in devfreq_cooling.h
   - Added forward declaration for thermal_cooling_device pointer in devfreq.h
 V5:
   - Changed subject prefix by:  PM / devfreq
 V4:
   - Replaced thermal_cooling_device_unregister() by
     devfreq_cooling_unregister()
 V3:
   - Rebased on linux-pm branch without units.h
   - Set the cdev to NULL in case of error
   - Added description for the cdev field in the devfreq structure
 V2:
   - Added is_cooling_device boolean in profile structure
   - Register cooling device when the is_cooling_device boolean is set
   - Remove devfreq cooling device registration in the backend drivers
 V1:
   - Register devfreq as a cooling device unconditionnally
---
 drivers/devfreq/devfreq.c | 9 +++++++++
 include/linux/devfreq.h   | 9 +++++++++
 2 files changed, 18 insertions(+)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index bf3047896e41..b6d3e7db0b09 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -11,6 +11,7 @@
 #include <linux/kmod.h>
 #include <linux/sched.h>
 #include <linux/debugfs.h>
+#include <linux/devfreq_cooling.h>
 #include <linux/errno.h>
 #include <linux/err.h>
 #include <linux/init.h>
@@ -935,6 +936,12 @@ struct devfreq *devfreq_add_device(struct device *dev,
 
        mutex_unlock(&devfreq_list_lock);
 
+       if (devfreq->profile->is_cooling_device) {
+               devfreq->cdev = devfreq_cooling_em_register(devfreq, NULL);
+               if (IS_ERR(devfreq->cdev))
+                       devfreq->cdev = NULL;
+       }
+
        return devfreq;
 
 err_init:
@@ -960,6 +967,8 @@ int devfreq_remove_device(struct devfreq *devfreq)
        if (!devfreq)
                return -EINVAL;
 
+       devfreq_cooling_unregister(devfreq->cdev);
+
        if (devfreq->governor) {
                devfreq->governor->event_handler(devfreq,
                                                 DEVFREQ_GOV_STOP, NULL);
diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
index 26ea0850be9b..142474b4af96 100644
--- a/include/linux/devfreq.h
+++ b/include/linux/devfreq.h
@@ -38,6 +38,7 @@ enum devfreq_timer {
 
 struct devfreq;
 struct devfreq_governor;
+struct thermal_cooling_device;
 
 /**
  * struct devfreq_dev_status - Data given from devfreq user device to
@@ -98,11 +99,15 @@ struct devfreq_dev_status {
  * @freq_table:                Optional list of frequencies to support 
statistics
  *                     and freq_table must be generated in ascending order.
  * @max_state:         The size of freq_table.
+ *
+ * @is_cooling_device: A self-explanatory boolean giving the device a
+ *                     cooling effect property.
  */
 struct devfreq_dev_profile {
        unsigned long initial_freq;
        unsigned int polling_ms;
        enum devfreq_timer timer;
+       bool is_cooling_device;
 
        int (*target)(struct device *dev, unsigned long *freq, u32 flags);
        int (*get_dev_status)(struct device *dev,
@@ -156,6 +161,7 @@ struct devfreq_stats {
  * @suspend_count:      suspend requests counter for a device.
  * @stats:     Statistics of devfreq device behavior
  * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
+ * @cdev:      Cooling device pointer if the devfreq has cooling property
  * @nb_min:            Notifier block for DEV_PM_QOS_MIN_FREQUENCY
  * @nb_max:            Notifier block for DEV_PM_QOS_MAX_FREQUENCY
  *
@@ -198,6 +204,9 @@ struct devfreq {
 
        struct srcu_notifier_head transition_notifier_list;
 
+       /* Pointer to the cooling device if used for thermal mitigation */
+       struct thermal_cooling_device *cdev;
+
        struct notifier_block nb_min;
        struct notifier_block nb_max;
 };
-- 
2.17.1

Reply via email to