Merge tag 'omap-fixes-a-for-3.10-rc' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / thermal / cpu_cooling.c
index 084ef00..c94bf2e 100644 (file)
  * @cpufreq_val: integer value representing the absolute value of the clipped
  *     frequency.
  * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
- * @node: list_head to link all cpufreq_cooling_device together.
  *
  * This structure is required for keeping information of each
- * cpufreq_cooling_device registered as a list whose head is represented by
- * cooling_cpufreq_list. In order to prevent corruption of this list a
+ * cpufreq_cooling_device registered. In order to prevent corruption of this a
  * mutex lock cooling_cpufreq_lock is used.
  */
 struct cpufreq_cooling_device {
@@ -52,9 +50,7 @@ struct cpufreq_cooling_device {
        unsigned int cpufreq_state;
        unsigned int cpufreq_val;
        struct cpumask allowed_cpus;
-       struct list_head node;
 };
-static LIST_HEAD(cooling_cpufreq_list);
 static DEFINE_IDR(cpufreq_idr);
 static DEFINE_MUTEX(cooling_cpufreq_lock);
 
@@ -68,6 +64,11 @@ static struct cpufreq_cooling_device *notify_device;
  * get_idr - function to get a unique id.
  * @idr: struct idr * handle used to create a id.
  * @id: int * value generated by this function.
+ *
+ * This function will populate @id with an unique
+ * id, using the idr API.
+ *
+ * Return: 0 on success, an error code on failure.
  */
 static int get_idr(struct idr *idr, int *id)
 {
@@ -79,6 +80,7 @@ static int get_idr(struct idr *idr, int *id)
        if (unlikely(ret < 0))
                return ret;
        *id = ret;
+
        return 0;
 }
 
@@ -109,6 +111,7 @@ static void release_idr(struct idr *idr, int id)
 static int is_cpufreq_valid(int cpu)
 {
        struct cpufreq_policy policy;
+
        return !cpufreq_get_policy(&policy, cpu);
 }
 
@@ -138,7 +141,8 @@ enum cpufreq_cooling_property {
  * Return: 0 on success, -EINVAL when invalid parameters are passed.
  */
 static int get_property(unsigned int cpu, unsigned long input,
-       unsigned int* output, enum cpufreq_cooling_property property)
+                       unsigned int *output,
+                       enum cpufreq_cooling_property property)
 {
        int i, j;
        unsigned long max_level = 0, level = 0;
@@ -146,14 +150,13 @@ static int get_property(unsigned int cpu, unsigned long input,
        int descend = -1;
        struct cpufreq_frequency_table *table =
                                        cpufreq_frequency_get_table(cpu);
-       
+
        if (!output)
                return -EINVAL;
 
        if (!table)
                return -EINVAL;
 
-       
        for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
                /* ignore invalid entries */
                if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
@@ -178,8 +181,7 @@ static int get_property(unsigned int cpu, unsigned long input,
        }
 
        if (property == GET_FREQ)
-               level = descend ? input : (max_level - input -1);
-
+               level = descend ? input : (max_level - input - 1);
 
        for (i = 0, j = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
                /* ignore invalid entry */
@@ -205,15 +207,28 @@ static int get_property(unsigned int cpu, unsigned long input,
                }
                j++;
        }
+
        return -EINVAL;
 }
 
+/**
+ * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
+ * @cpu: cpu for which the level is required
+ * @freq: the frequency of interest
+ *
+ * This function will match the cooling level corresponding to the
+ * requested @freq and return it.
+ *
+ * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
+ * otherwise.
+ */
 unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
 {
        unsigned int val;
 
        if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
                return THERMAL_CSTATE_INVALID;
+
        return (unsigned long)val;
 }
 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
@@ -221,8 +236,14 @@ EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
 /**
  * get_cpu_frequency - get the absolute value of frequency from level.
  * @cpu: cpu for which frequency is fetched.
- * @level: level of frequency, equals cooling state of cpu cooling device
+ * @level: cooling level
+ *
+ * This function matches cooling level with frequency. Based on a cooling level
+ * of frequency, equals cooling state of cpu cooling device, it will return
+ * the corresponding frequency.
  *     e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
+ *
+ * Return: 0 on error, the corresponding frequency otherwise.
  */
 static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
 {
@@ -232,6 +253,7 @@ static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
        ret = get_property(cpu, level, &freq, GET_FREQ);
        if (ret)
                return 0;
+
        return freq;
 }
 
@@ -240,9 +262,15 @@ static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
  * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
  *     clipping data.
  * @cooling_state: value of the cooling state.
+ *
+ * Function used to make sure the cpufreq layer is aware of current thermal
+ * limits. The limits are applied by updating the cpufreq policy.
+ *
+ * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
+ * cooling state).
  */
 static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
-                               unsigned long cooling_state)
+                                unsigned long cooling_state)
 {
        unsigned int cpuid, clip_freq;
        struct cpumask *mask = &cpufreq_device->allowed_cpus;
@@ -276,9 +304,15 @@ static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
  * @nb:        struct notifier_block * with callback info.
  * @event: value showing cpufreq event for which this function invoked.
  * @data: callback-specific data
+ *
+ * Callback to highjack the notification on cpufreq policy transition.
+ * Every time there is a change in policy, we will intercept and
+ * update the cpufreq policy with thermal constraints.
+ *
+ * Return: 0 (success)
  */
 static int cpufreq_thermal_notifier(struct notifier_block *nb,
-                                       unsigned long event, void *data)
+                                   unsigned long event, void *data)
 {
        struct cpufreq_policy *policy = data;
        unsigned long max_freq = 0;
@@ -289,7 +323,7 @@ static int cpufreq_thermal_notifier(struct notifier_block *nb,
        if (cpumask_test_cpu(policy->cpu, &notify_device->allowed_cpus))
                max_freq = notify_device->cpufreq_val;
 
-       /* Never exceed user_policy.max*/
+       /* Never exceed user_policy.max */
        if (max_freq > policy->user_policy.max)
                max_freq = policy->user_policy.max;
 
@@ -299,14 +333,17 @@ static int cpufreq_thermal_notifier(struct notifier_block *nb,
        return 0;
 }
 
-/*
- * cpufreq cooling device callback functions are defined below
- */
+/* cpufreq cooling device callback functions are defined below */
 
 /**
  * cpufreq_get_max_state - callback function to get the max cooling state.
  * @cdev: thermal cooling device pointer.
  * @state: fill this variable with the max cooling state.
+ *
+ * Callback for the thermal cooling device to return the cpufreq
+ * max cooling state.
+ *
+ * Return: 0 on success, an error code otherwise.
  */
 static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
                                 unsigned long *state)
@@ -331,6 +368,11 @@ static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
  * cpufreq_get_cur_state - callback function to get the current cooling state.
  * @cdev: thermal cooling device pointer.
  * @state: fill this variable with the current cooling state.
+ *
+ * Callback for the thermal cooling device to return the cpufreq
+ * current cooling state.
+ *
+ * Return: 0 on success, an error code otherwise.
  */
 static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
                                 unsigned long *state)
@@ -338,6 +380,7 @@ static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
        struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
 
        *state = cpufreq_device->cpufreq_state;
+
        return 0;
 }
 
@@ -345,6 +388,11 @@ static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
  * cpufreq_set_cur_state - callback function to set the current cooling state.
  * @cdev: thermal cooling device pointer.
  * @state: set this variable to the current cooling state.
+ *
+ * Callback for the thermal cooling device to change the cpufreq
+ * current cooling state.
+ *
+ * Return: 0 on success, an error code otherwise.
  */
 static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
                                 unsigned long state)
@@ -369,9 +417,16 @@ static struct notifier_block thermal_cpufreq_notifier_block = {
 /**
  * cpufreq_cooling_register - function to create cpufreq cooling device.
  * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
+ *
+ * This interface function registers the cpufreq cooling device with the name
+ * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
+ * cooling devices.
+ *
+ * Return: a valid struct thermal_cooling_device pointer on success,
+ * on failure, it returns a corresponding ERR_PTR().
  */
-struct thermal_cooling_device *cpufreq_cooling_register(
-       const struct cpumask *clip_cpus)
+struct thermal_cooling_device *
+cpufreq_cooling_register(const struct cpumask *clip_cpus)
 {
        struct thermal_cooling_device *cool_dev;
        struct cpufreq_cooling_device *cpufreq_dev = NULL;
@@ -380,9 +435,9 @@ struct thermal_cooling_device *cpufreq_cooling_register(
        int ret = 0, i;
        struct cpufreq_policy policy;
 
-       /*Verify that all the clip cpus have same freq_min, freq_max limit*/
+       /* Verify that all the clip cpus have same freq_min, freq_max limit */
        for_each_cpu(i, clip_cpus) {
-               /*continue if cpufreq policy not found and not return error*/
+               /* continue if cpufreq policy not found and not return error */
                if (!cpufreq_get_policy(&policy, i))
                        continue;
                if (min == 0 && max == 0) {
@@ -390,12 +445,12 @@ struct thermal_cooling_device *cpufreq_cooling_register(
                        max = policy.cpuinfo.max_freq;
                } else {
                        if (min != policy.cpuinfo.min_freq ||
-                               max != policy.cpuinfo.max_freq)
+                           max != policy.cpuinfo.max_freq)
                                return ERR_PTR(-EINVAL);
                }
        }
        cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
-                       GFP_KERNEL);
+                             GFP_KERNEL);
        if (!cpufreq_dev)
                return ERR_PTR(-ENOMEM);
 
@@ -407,10 +462,11 @@ struct thermal_cooling_device *cpufreq_cooling_register(
                return ERR_PTR(-EINVAL);
        }
 
-       sprintf(dev_name, "thermal-cpufreq-%d", cpufreq_dev->id);
+       snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
+                cpufreq_dev->id);
 
        cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev,
-                                               &cpufreq_cooling_ops);
+                                                  &cpufreq_cooling_ops);
        if (!cool_dev) {
                release_idr(&cpufreq_idr, cpufreq_dev->id);
                kfree(cpufreq_dev);
@@ -423,10 +479,11 @@ struct thermal_cooling_device *cpufreq_cooling_register(
        /* Register the notifier for first cpufreq cooling device */
        if (cpufreq_dev_count == 0)
                cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
-                                               CPUFREQ_POLICY_NOTIFIER);
+                                         CPUFREQ_POLICY_NOTIFIER);
        cpufreq_dev_count++;
 
        mutex_unlock(&cooling_cpufreq_lock);
+
        return cool_dev;
 }
 EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
@@ -434,6 +491,8 @@ EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
 /**
  * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
  * @cdev: thermal cooling device pointer.
+ *
+ * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
  */
 void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
 {
@@ -443,10 +502,9 @@ void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
        cpufreq_dev_count--;
 
        /* Unregister the notifier for the last cpufreq cooling device */
-       if (cpufreq_dev_count == 0) {
+       if (cpufreq_dev_count == 0)
                cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
-                                       CPUFREQ_POLICY_NOTIFIER);
-       }
+                                           CPUFREQ_POLICY_NOTIFIER);
        mutex_unlock(&cooling_cpufreq_lock);
 
        thermal_cooling_device_unregister(cpufreq_dev->cool_dev);