]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/thermal/thermal_sys.c
Merge remote-tracking branch 'workqueues/for-next'
[karo-tx-linux.git] / drivers / thermal / thermal_sys.c
index 67789b8345d25cb7a5fad998863aab3c9d999689..d552a96f08cd1bf1abd12f2977a5ffd35eef6da5 100644 (file)
@@ -41,15 +41,25 @@ MODULE_AUTHOR("Zhang Rui");
 MODULE_DESCRIPTION("Generic thermal management sysfs support");
 MODULE_LICENSE("GPL");
 
-struct thermal_cooling_device_instance {
+#define THERMAL_NO_TARGET -1UL
+/*
+ * This structure is used to describe the behavior of
+ * a certain cooling device on a certain trip point
+ * in a certain thermal zone
+ */
+struct thermal_instance {
        int id;
        char name[THERMAL_NAME_LENGTH];
        struct thermal_zone_device *tz;
        struct thermal_cooling_device *cdev;
        int trip;
+       unsigned long upper;    /* Highest cooling state for this trip point */
+       unsigned long lower;    /* Lowest cooling state for this trip point */
+       unsigned long target;   /* expected cooling state */
        char attr_name[THERMAL_NAME_LENGTH];
        struct device_attribute attr;
-       struct list_head node;
+       struct list_head tz_node; /* node in tz->thermal_instances */
+       struct list_head cdev_node; /* node in cdev->thermal_instances */
 };
 
 static DEFINE_IDR(thermal_tz_idr);
@@ -308,8 +318,9 @@ passive_store(struct device *dev, struct device_attribute *attr,
                        if (!strncmp("Processor", cdev->type,
                                     sizeof("Processor")))
                                thermal_zone_bind_cooling_device(tz,
-                                                                THERMAL_TRIPS_NONE,
-                                                                cdev);
+                                               THERMAL_TRIPS_NONE, cdev,
+                                               THERMAL_NO_LIMIT,
+                                               THERMAL_NO_LIMIT);
                }
                mutex_unlock(&thermal_list_lock);
                if (!tz->passive_delay)
@@ -327,9 +338,6 @@ passive_store(struct device *dev, struct device_attribute *attr,
                tz->passive_delay = 0;
        }
 
-       tz->tc1 = 1;
-       tz->tc2 = 1;
-
        tz->forced_passive = state;
 
        thermal_zone_device_update(tz);
@@ -425,10 +433,10 @@ static ssize_t
 thermal_cooling_device_trip_point_show(struct device *dev,
                                       struct device_attribute *attr, char *buf)
 {
-       struct thermal_cooling_device_instance *instance;
+       struct thermal_instance *instance;
 
        instance =
-           container_of(attr, struct thermal_cooling_device_instance, attr);
+           container_of(attr, struct thermal_instance, attr);
 
        if (instance->trip == THERMAL_TRIPS_NONE)
                return sprintf(buf, "-1\n");
@@ -590,7 +598,7 @@ thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
        temp->tz = tz;
        hwmon->count++;
 
-       snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
+       snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
                 "temp%d_input", hwmon->count);
        temp->temp_input.attr.attr.name = temp->temp_input.name;
        temp->temp_input.attr.attr.mode = 0444;
@@ -603,7 +611,8 @@ thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
        if (tz->ops->get_crit_temp) {
                unsigned long temperature;
                if (!tz->ops->get_crit_temp(tz, &temperature)) {
-                       snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
+                       snprintf(temp->temp_crit.name,
+                                sizeof(temp->temp_crit.name),
                                "temp%d_crit", hwmon->count);
                        temp->temp_crit.attr.attr.name = temp->temp_crit.name;
                        temp->temp_crit.attr.attr.mode = 0444;
@@ -704,74 +713,6 @@ static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
                cancel_delayed_work(&tz->poll_queue);
 }
 
-static void thermal_zone_device_passive(struct thermal_zone_device *tz,
-                                       int temp, int trip_temp, int trip)
-{
-       int trend = 0;
-       struct thermal_cooling_device_instance *instance;
-       struct thermal_cooling_device *cdev;
-       long state, max_state;
-
-       /*
-        * Above Trip?
-        * -----------
-        * Calculate the thermal trend (using the passive cooling equation)
-        * and modify the performance limit for all passive cooling devices
-        * accordingly.  Note that we assume symmetry.
-        */
-       if (temp >= trip_temp) {
-               tz->passive = true;
-
-               trend = (tz->tc1 * (temp - tz->last_temperature)) +
-                       (tz->tc2 * (temp - trip_temp));
-
-               /* Heating up? */
-               if (trend > 0) {
-                       list_for_each_entry(instance, &tz->cooling_devices,
-                                           node) {
-                               if (instance->trip != trip)
-                                       continue;
-                               cdev = instance->cdev;
-                               cdev->ops->get_cur_state(cdev, &state);
-                               cdev->ops->get_max_state(cdev, &max_state);
-                               if (state++ < max_state)
-                                       cdev->ops->set_cur_state(cdev, state);
-                       }
-               } else if (trend < 0) { /* Cooling off? */
-                       list_for_each_entry(instance, &tz->cooling_devices,
-                                           node) {
-                               if (instance->trip != trip)
-                                       continue;
-                               cdev = instance->cdev;
-                               cdev->ops->get_cur_state(cdev, &state);
-                               cdev->ops->get_max_state(cdev, &max_state);
-                               if (state > 0)
-                                       cdev->ops->set_cur_state(cdev, --state);
-                       }
-               }
-               return;
-       }
-
-       /*
-        * Below Trip?
-        * -----------
-        * Implement passive cooling hysteresis to slowly increase performance
-        * and avoid thrashing around the passive trip point.  Note that we
-        * assume symmetry.
-        */
-       list_for_each_entry(instance, &tz->cooling_devices, node) {
-               if (instance->trip != trip)
-                       continue;
-               cdev = instance->cdev;
-               cdev->ops->get_cur_state(cdev, &state);
-               cdev->ops->get_max_state(cdev, &max_state);
-               if (state > 0)
-                       cdev->ops->set_cur_state(cdev, --state);
-               if (state == 0)
-                       tz->passive = false;
-       }
-}
-
 static void thermal_zone_device_check(struct work_struct *work)
 {
        struct thermal_zone_device *tz = container_of(work, struct
@@ -791,12 +732,14 @@ static void thermal_zone_device_check(struct work_struct *work)
  */
 int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
                                     int trip,
-                                    struct thermal_cooling_device *cdev)
+                                    struct thermal_cooling_device *cdev,
+                                    unsigned long upper, unsigned long lower)
 {
-       struct thermal_cooling_device_instance *dev;
-       struct thermal_cooling_device_instance *pos;
+       struct thermal_instance *dev;
+       struct thermal_instance *pos;
        struct thermal_zone_device *pos1;
        struct thermal_cooling_device *pos2;
+       unsigned long max_state;
        int result;
 
        if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
@@ -814,13 +757,26 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
        if (tz != pos1 || cdev != pos2)
                return -EINVAL;
 
+       cdev->ops->get_max_state(cdev, &max_state);
+
+       /* lower default 0, upper default max_state */
+       lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
+       upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
+
+       if (lower > upper || upper > max_state)
+               return -EINVAL;
+
        dev =
-           kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
+           kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
        if (!dev)
                return -ENOMEM;
        dev->tz = tz;
        dev->cdev = cdev;
        dev->trip = trip;
+       dev->upper = upper;
+       dev->lower = lower;
+       dev->target = THERMAL_NO_TARGET;
+
        result = get_idr(&tz->idr, &tz->lock, &dev->id);
        if (result)
                goto free_mem;
@@ -841,13 +797,17 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
                goto remove_symbol_link;
 
        mutex_lock(&tz->lock);
-       list_for_each_entry(pos, &tz->cooling_devices, node)
+       mutex_lock(&cdev->lock);
+       list_for_each_entry(pos, &tz->thermal_instances, tz_node)
            if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
                result = -EEXIST;
                break;
        }
-       if (!result)
-               list_add_tail(&dev->node, &tz->cooling_devices);
+       if (!result) {
+               list_add_tail(&dev->tz_node, &tz->thermal_instances);
+               list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
+       }
+       mutex_unlock(&cdev->lock);
        mutex_unlock(&tz->lock);
 
        if (!result)
@@ -877,16 +837,20 @@ int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
                                       int trip,
                                       struct thermal_cooling_device *cdev)
 {
-       struct thermal_cooling_device_instance *pos, *next;
+       struct thermal_instance *pos, *next;
 
        mutex_lock(&tz->lock);
-       list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
+       mutex_lock(&cdev->lock);
+       list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
                if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
-                       list_del(&pos->node);
+                       list_del(&pos->tz_node);
+                       list_del(&pos->cdev_node);
+                       mutex_unlock(&cdev->lock);
                        mutex_unlock(&tz->lock);
                        goto unbind;
                }
        }
+       mutex_unlock(&cdev->lock);
        mutex_unlock(&tz->lock);
 
        return -ENODEV;
@@ -934,7 +898,7 @@ thermal_cooling_device_register(char *type, void *devdata,
        struct thermal_zone_device *pos;
        int result;
 
-       if (strlen(type) >= THERMAL_NAME_LENGTH)
+       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);
 
        if (!ops || !ops->get_max_state || !ops->get_cur_state ||
@@ -951,8 +915,11 @@ thermal_cooling_device_register(char *type, void *devdata,
                return ERR_PTR(result);
        }
 
-       strcpy(cdev->type, type);
+       strcpy(cdev->type, type ? : "");
+       mutex_init(&cdev->lock);
+       INIT_LIST_HEAD(&cdev->thermal_instances);
        cdev->ops = ops;
+       cdev->updated = true;
        cdev->device.class = &thermal_class;
        cdev->devdata = devdata;
        dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
@@ -1044,6 +1011,136 @@ void thermal_cooling_device_unregister(struct
 }
 EXPORT_SYMBOL(thermal_cooling_device_unregister);
 
+static void thermal_cdev_do_update(struct thermal_cooling_device *cdev)
+{
+       struct thermal_instance *instance;
+       unsigned long target = 0;
+
+       /* cooling device is updated*/
+       if (cdev->updated)
+               return;
+
+       mutex_lock(&cdev->lock);
+       /* Make sure cdev enters the deepest cooling state */
+       list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
+               if (instance->target == THERMAL_NO_TARGET)
+                       continue;
+               if (instance->target > target)
+                       target = instance->target;
+       }
+       mutex_unlock(&cdev->lock);
+       cdev->ops->set_cur_state(cdev, target);
+       cdev->updated = true;
+}
+
+static void thermal_zone_do_update(struct thermal_zone_device *tz)
+{
+       struct thermal_instance *instance;
+
+       list_for_each_entry(instance, &tz->thermal_instances, tz_node)
+               thermal_cdev_do_update(instance->cdev);
+}
+
+/*
+ * Cooling algorithm for both active and passive cooling
+ *
+ * 1. if the temperature is higher than a trip point,
+ *    a. if the trend is THERMAL_TREND_RAISING, use higher cooling
+ *       state for this trip point
+ *    b. if the trend is THERMAL_TREND_DROPPING, use lower cooling
+ *       state for this trip point
+ *
+ * 2. if the temperature is lower than a trip point, use lower
+ *    cooling state for this trip point
+ *
+ * Note that this behaves the same as the previous passive cooling
+ * algorithm.
+ */
+
+static void thermal_zone_trip_update(struct thermal_zone_device *tz,
+                                    int trip, long temp)
+{
+       struct thermal_instance *instance;
+       struct thermal_cooling_device *cdev = NULL;
+       unsigned long cur_state, max_state;
+       long trip_temp;
+       enum thermal_trip_type trip_type;
+       enum thermal_trend trend;
+
+       if (trip == THERMAL_TRIPS_NONE) {
+               trip_temp = tz->forced_passive;
+               trip_type = THERMAL_TRIPS_NONE;
+       } else {
+               tz->ops->get_trip_temp(tz, trip, &trip_temp);
+               tz->ops->get_trip_type(tz, trip, &trip_type);
+       }
+
+       if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
+               /*
+                * compare the current temperature and previous temperature
+                * to get the thermal trend, if no special requirement
+                */
+               if (tz->temperature > tz->last_temperature)
+                       trend = THERMAL_TREND_RAISING;
+               else if (tz->temperature < tz->last_temperature)
+                       trend = THERMAL_TREND_DROPPING;
+               else
+                       trend = THERMAL_TREND_STABLE;
+       }
+
+       if (temp >= trip_temp) {
+               list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+                       if (instance->trip != trip)
+                               continue;
+
+                       cdev = instance->cdev;
+
+                       cdev->ops->get_cur_state(cdev, &cur_state);
+                       cdev->ops->get_max_state(cdev, &max_state);
+
+                       if (trend == THERMAL_TREND_RAISING) {
+                               cur_state = cur_state < instance->upper ?
+                                           (cur_state + 1) : instance->upper;
+                       } else if (trend == THERMAL_TREND_DROPPING) {
+                               cur_state = cur_state > instance->lower ?
+                                   (cur_state - 1) : instance->lower;
+                       }
+
+                       /* activate a passive thermal instance */
+                       if ((trip_type == THERMAL_TRIP_PASSIVE ||
+                            trip_type == THERMAL_TRIPS_NONE) &&
+                            instance->target == THERMAL_NO_TARGET)
+                               tz->passive++;
+
+                       instance->target = cur_state;
+                       cdev->updated = false; /* cooling device needs update */
+               }
+       } else {        /* below trip */
+               list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
+                       if (instance->trip != trip)
+                               continue;
+
+                       /* Do not use the inactive thermal instance */
+                       if (instance->target == THERMAL_NO_TARGET)
+                               continue;
+                       cdev = instance->cdev;
+                       cdev->ops->get_cur_state(cdev, &cur_state);
+
+                       cur_state = cur_state > instance->lower ?
+                                   (cur_state - 1) : THERMAL_NO_TARGET;
+
+                       /* deactivate a passive thermal instance */
+                       if ((trip_type == THERMAL_TRIP_PASSIVE ||
+                            trip_type == THERMAL_TRIPS_NONE) &&
+                            cur_state == THERMAL_NO_TARGET)
+                               tz->passive--;
+                       instance->target = cur_state;
+                       cdev->updated = false; /* cooling device needs update */
+               }
+       }
+
+       return;
+}
 /**
  * thermal_zone_device_update - force an update of a thermal zone's state
  * @ttz:       the thermal zone to update
@@ -1054,8 +1151,6 @@ void thermal_zone_device_update(struct thermal_zone_device *tz)
        int count, ret = 0;
        long temp, trip_temp;
        enum thermal_trip_type trip_type;
-       struct thermal_cooling_device_instance *instance;
-       struct thermal_cooling_device *cdev;
 
        mutex_lock(&tz->lock);
 
@@ -1065,6 +1160,9 @@ void thermal_zone_device_update(struct thermal_zone_device *tz)
                goto leave;
        }
 
+       tz->last_temperature = tz->temperature;
+       tz->temperature = temp;
+
        for (count = 0; count < tz->trips; count++) {
                tz->ops->get_trip_type(tz, count, &trip_type);
                tz->ops->get_trip_temp(tz, count, &trip_temp);
@@ -1088,32 +1186,18 @@ void thermal_zone_device_update(struct thermal_zone_device *tz)
                                        tz->ops->notify(tz, count, trip_type);
                        break;
                case THERMAL_TRIP_ACTIVE:
-                       list_for_each_entry(instance, &tz->cooling_devices,
-                                           node) {
-                               if (instance->trip != count)
-                                       continue;
-
-                               cdev = instance->cdev;
-
-                               if (temp >= trip_temp)
-                                       cdev->ops->set_cur_state(cdev, 1);
-                               else
-                                       cdev->ops->set_cur_state(cdev, 0);
-                       }
+                       thermal_zone_trip_update(tz, count, temp);
                        break;
                case THERMAL_TRIP_PASSIVE:
                        if (temp >= trip_temp || tz->passive)
-                               thermal_zone_device_passive(tz, temp,
-                                                           trip_temp, count);
+                               thermal_zone_trip_update(tz, count, temp);
                        break;
                }
        }
 
        if (tz->forced_passive)
-               thermal_zone_device_passive(tz, temp, tz->forced_passive,
-                                           THERMAL_TRIPS_NONE);
-
-       tz->last_temperature = temp;
+               thermal_zone_trip_update(tz, THERMAL_TRIPS_NONE, temp);
+       thermal_zone_do_update(tz);
 
 leave:
        if (tz->passive)
@@ -1236,8 +1320,6 @@ static void remove_trip_attrs(struct thermal_zone_device *tz)
  * @mask:      a bit string indicating the writeablility of trip points
  * @devdata:   private device data
  * @ops:       standard thermal zone device callbacks
- * @tc1:       thermal coefficient 1 for passive calculations
- * @tc2:       thermal coefficient 2 for passive calculations
  * @passive_delay: number of milliseconds to wait between polls when
  *                performing passive cooling
  * @polling_delay: number of milliseconds to wait between polls when checking
@@ -1245,13 +1327,12 @@ static void remove_trip_attrs(struct thermal_zone_device *tz)
  *                driven systems)
  *
  * thermal_zone_device_unregister() must be called when the device is no
- * longer needed. The passive cooling formula uses tc1 and tc2 as described in
- * section 11.1.5.1 of the ACPI specification 3.0.
+ * longer needed. The passive cooling depends on the .get_trend() return value.
  */
 struct thermal_zone_device *thermal_zone_device_register(const char *type,
        int trips, int mask, void *devdata,
        const struct thermal_zone_device_ops *ops,
-       int tc1, int tc2, int passive_delay, int polling_delay)
+       int passive_delay, int polling_delay)
 {
        struct thermal_zone_device *tz;
        struct thermal_cooling_device *pos;
@@ -1260,7 +1341,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
        int count;
        int passive = 0;
 
-       if (strlen(type) >= THERMAL_NAME_LENGTH)
+       if (type && strlen(type) >= THERMAL_NAME_LENGTH)
                return ERR_PTR(-EINVAL);
 
        if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
@@ -1273,7 +1354,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
        if (!tz)
                return ERR_PTR(-ENOMEM);
 
-       INIT_LIST_HEAD(&tz->cooling_devices);
+       INIT_LIST_HEAD(&tz->thermal_instances);
        idr_init(&tz->idr);
        mutex_init(&tz->lock);
        result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
@@ -1282,13 +1363,11 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
                return ERR_PTR(result);
        }
 
-       strcpy(tz->type, type);
+       strcpy(tz->type, type ? : "");
        tz->ops = ops;
        tz->device.class = &thermal_class;
        tz->devdata = devdata;
        tz->trips = trips;
-       tz->tc1 = tc1;
-       tz->tc2 = tc2;
        tz->passive_delay = passive_delay;
        tz->polling_delay = polling_delay;