thermal: extend the cooling device API to include power information
authorJavi Merino <javi.merino@arm.com>
Thu, 26 Feb 2015 19:00:28 +0000 (19:00 +0000)
committerJoonyoung Shim <jy0922.shim@samsung.com>
Thu, 7 Jul 2016 05:32:02 +0000 (14:32 +0900)
Add three optional callbacks to the cooling device interface to allow
them to express power.  In addition to the callbacks, add helpers to
identify cooling devices that implement the power cooling device API.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>
Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
[jy0922.shim: apply mainline patch]
Signed-off-by: Joonyoung Shim <jy0922.shim@samsung.com>
drivers/thermal/thermal_core.c
include/linux/thermal.h

index 62cc82a02f167444c274d00e6e94eabe84ed8736..6806c82c4ec45b0e82755fc22a857893e7698535 100644 (file)
@@ -808,6 +808,58 @@ emul_temp_store(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
 #endif/*CONFIG_THERMAL_EMULATION*/
 
+/**
+ * power_actor_get_max_power() - get the maximum power that a cdev can consume
+ * @cdev:      pointer to &thermal_cooling_device
+ * @tz:                a valid thermal zone device pointer
+ * @max_power: pointer in which to store the maximum power
+ *
+ * Calculate the maximum power consumption in milliwats that the
+ * cooling device can currently consume and store it in @max_power.
+ *
+ * Return: 0 on success, -EINVAL if @cdev doesn't support the
+ * power_actor API or -E* on other error.
+ */
+int power_actor_get_max_power(struct thermal_cooling_device *cdev,
+                             struct thermal_zone_device *tz, u32 *max_power)
+{
+       if (!cdev_is_power_actor(cdev))
+               return -EINVAL;
+
+       return cdev->ops->state2power(cdev, tz, 0, max_power);
+}
+
+/**
+ * power_actor_set_power() - limit the maximum power that a cooling device can consume
+ * @cdev:      pointer to &thermal_cooling_device
+ * @instance:  thermal instance to update
+ * @power:     the power in milliwatts
+ *
+ * Set the cooling device to consume at most @power milliwatts.
+ *
+ * Return: 0 on success, -EINVAL if the cooling device does not
+ * implement the power actor API or -E* for other failures.
+ */
+int power_actor_set_power(struct thermal_cooling_device *cdev,
+                         struct thermal_instance *instance, u32 power)
+{
+       unsigned long state;
+       int ret;
+
+       if (!cdev_is_power_actor(cdev))
+               return -EINVAL;
+
+       ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
+       if (ret)
+               return ret;
+
+       instance->target = state;
+       cdev->updated = false;
+       thermal_cdev_update(cdev);
+
+       return 0;
+}
+
 static DEVICE_ATTR(type, 0444, type_show, NULL);
 static DEVICE_ATTR(temp, 0444, temp_show, NULL);
 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
index e9f28637e5aa987b2bdb589e6ccb48fb47db256e..ec5a7e37651d5d2b3918f476a6414d0132158b00 100644 (file)
@@ -60,6 +60,7 @@
 
 struct thermal_zone_device;
 struct thermal_cooling_device;
+struct thermal_instance;
 
 enum thermal_device_mode {
        THERMAL_DEVICE_DISABLED = 0,
@@ -109,6 +110,12 @@ struct thermal_cooling_device_ops {
        int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
        int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
        int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
+       int (*get_requested_power)(struct thermal_cooling_device *,
+                                  struct thermal_zone_device *, u32 *);
+       int (*state2power)(struct thermal_cooling_device *,
+                          struct thermal_zone_device *, unsigned long, u32 *);
+       int (*power2state)(struct thermal_cooling_device *,
+                          struct thermal_zone_device *, u32, unsigned long *);
 };
 
 struct thermal_cooling_device {
@@ -312,6 +319,16 @@ void thermal_zone_of_sensor_unregister(struct device *dev,
 #endif
 
 #if IS_ENABLED(CONFIG_THERMAL)
+static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
+{
+       return cdev->ops->get_requested_power && cdev->ops->state2power &&
+               cdev->ops->power2state;
+}
+
+int power_actor_get_max_power(struct thermal_cooling_device *,
+                             struct thermal_zone_device *tz, u32 *max_power);
+int power_actor_set_power(struct thermal_cooling_device *,
+                         struct thermal_instance *, u32);
 struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
                void *, struct thermal_zone_device_ops *,
                const struct thermal_zone_params *, int, int);
@@ -339,6 +356,14 @@ struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
 void thermal_cdev_update(struct thermal_cooling_device *);
 void thermal_notify_framework(struct thermal_zone_device *, int);
 #else
+static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
+{ return false; }
+static inline int power_actor_get_max_power(struct thermal_cooling_device *cdev,
+                             struct thermal_zone_device *tz, u32 *max_power)
+{ return 0; }
+static inline int power_actor_set_power(struct thermal_cooling_device *cdev,
+                         struct thermal_instance *tz, u32 power)
+{ return 0; }
 static inline struct thermal_zone_device *thermal_zone_device_register(
        const char *type, int trips, int mask, void *devdata,
        struct thermal_zone_device_ops *ops,