2 * linux/drivers/thermal/cpu_cooling.c
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #include <linux/module.h>
24 #include <linux/thermal.h>
25 #include <linux/cpufreq.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/cpu_cooling.h>
32 * Cooling state <-> CPUFreq frequency
34 * Cooling states are translated to frequencies throughout this driver and this
35 * is the relation between them.
37 * Highest cooling state corresponds to lowest possible frequency.
40 * level 0 --> 1st Max Freq
41 * level 1 --> 2nd Max Freq
46 * struct cpufreq_cooling_device - data for cooling device with cpufreq
47 * @id: unique integer value corresponding to each cpufreq_cooling_device
49 * @cool_dev: thermal_cooling_device pointer to keep track of the
50 * registered cooling device.
51 * @cpufreq_state: integer value representing the current state of cpufreq
53 * @cpufreq_val: integer value representing the absolute value of the clipped
55 * @max_level: maximum cooling level. One less than total number of valid
56 * cpufreq frequencies.
57 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
59 * This structure is required for keeping information of each registered
60 * cpufreq_cooling_device.
62 struct cpufreq_cooling_device
{
64 struct thermal_cooling_device
*cool_dev
;
65 unsigned int cpufreq_state
;
66 unsigned int cpufreq_val
;
67 unsigned int max_level
;
68 unsigned int *freq_table
; /* In descending order */
69 struct cpumask allowed_cpus
;
70 struct list_head node
;
72 static DEFINE_IDR(cpufreq_idr
);
73 static DEFINE_MUTEX(cooling_cpufreq_lock
);
75 static LIST_HEAD(cpufreq_dev_list
);
78 * get_idr - function to get a unique id.
79 * @idr: struct idr * handle used to create a id.
80 * @id: int * value generated by this function.
82 * This function will populate @id with an unique
83 * id, using the idr API.
85 * Return: 0 on success, an error code on failure.
87 static int get_idr(struct idr
*idr
, int *id
)
91 mutex_lock(&cooling_cpufreq_lock
);
92 ret
= idr_alloc(idr
, NULL
, 0, 0, GFP_KERNEL
);
93 mutex_unlock(&cooling_cpufreq_lock
);
94 if (unlikely(ret
< 0))
102 * release_idr - function to free the unique id.
103 * @idr: struct idr * handle used for creating the id.
104 * @id: int value representing the unique id.
106 static void release_idr(struct idr
*idr
, int id
)
108 mutex_lock(&cooling_cpufreq_lock
);
110 mutex_unlock(&cooling_cpufreq_lock
);
113 /* Below code defines functions to be used for cpufreq as cooling device */
115 enum cpufreq_cooling_property
{
121 * get_property - fetch a property of interest for a given cpu.
122 * @cpufreq_dev: cpufreq_dev for which the property is required
123 * @input: query parameter
124 * @output: query return
125 * @property: type of query (frequency, level)
127 * This is the common function to
128 * 1. translate frequency to cooling state
129 * 2. translate cooling state to frequency
131 * Note that the code may be not in good shape
132 * but it is written in this way in order to:
133 * a) reduce duplicate code as most of the code can be shared.
134 * b) make sure the logic is consistent when translating between
135 * cooling states and frequencies.
137 * Return: 0 on success, -EINVAL when invalid parameters are passed.
139 static int get_property(struct cpufreq_cooling_device
*cpufreq_dev
,
140 unsigned long input
, unsigned int *output
,
141 enum cpufreq_cooling_property property
)
144 unsigned long level
= 0;
145 unsigned int freq
= CPUFREQ_ENTRY_INVALID
;
147 struct cpufreq_frequency_table
*pos
, *table
;
152 table
= cpufreq_frequency_get_table(cpumask_first(&cpufreq_dev
->allowed_cpus
));
156 cpufreq_for_each_valid_entry(pos
, table
) {
157 /* ignore duplicate entry */
158 if (freq
== pos
->frequency
)
161 /* get the frequency order */
162 if (freq
!= CPUFREQ_ENTRY_INVALID
&& descend
== -1)
163 descend
= freq
> pos
->frequency
;
165 freq
= pos
->frequency
;
168 if (property
== GET_FREQ
)
169 level
= descend
? input
: (cpufreq_dev
->max_level
- input
);
172 cpufreq_for_each_valid_entry(pos
, table
) {
173 /* ignore duplicate entry */
174 if (freq
== pos
->frequency
)
177 /* now we have a valid frequency entry */
178 freq
= pos
->frequency
;
180 if (property
== GET_LEVEL
&& (unsigned int)input
== freq
) {
181 /* get level by frequency */
182 *output
= descend
? i
: (cpufreq_dev
->max_level
- i
);
185 if (property
== GET_FREQ
&& level
== i
) {
186 /* get frequency by level */
197 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
198 * @cpu: cpu for which the level is required
199 * @freq: the frequency of interest
201 * This function will match the cooling level corresponding to the
202 * requested @freq and return it.
204 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
207 unsigned long cpufreq_cooling_get_level(unsigned int cpu
, unsigned int freq
)
209 struct cpufreq_cooling_device
*cpufreq_dev
;
211 mutex_lock(&cooling_cpufreq_lock
);
212 list_for_each_entry(cpufreq_dev
, &cpufreq_dev_list
, node
) {
213 if (cpumask_test_cpu(cpu
, &cpufreq_dev
->allowed_cpus
)) {
216 mutex_unlock(&cooling_cpufreq_lock
);
217 if (get_property(cpufreq_dev
, (unsigned long)freq
, &val
,
219 return THERMAL_CSTATE_INVALID
;
221 return (unsigned long)val
;
224 mutex_unlock(&cooling_cpufreq_lock
);
226 pr_err("%s: cpu:%d not part of any cooling device\n", __func__
, cpu
);
227 return THERMAL_CSTATE_INVALID
;
229 EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level
);
232 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
233 * @nb: struct notifier_block * with callback info.
234 * @event: value showing cpufreq event for which this function invoked.
235 * @data: callback-specific data
237 * Callback to hijack the notification on cpufreq policy transition.
238 * Every time there is a change in policy, we will intercept and
239 * update the cpufreq policy with thermal constraints.
241 * Return: 0 (success)
243 static int cpufreq_thermal_notifier(struct notifier_block
*nb
,
244 unsigned long event
, void *data
)
246 struct cpufreq_policy
*policy
= data
;
247 unsigned long max_freq
= 0;
248 struct cpufreq_cooling_device
*cpufreq_dev
;
250 if (event
!= CPUFREQ_ADJUST
)
253 mutex_lock(&cooling_cpufreq_lock
);
254 list_for_each_entry(cpufreq_dev
, &cpufreq_dev_list
, node
) {
255 if (!cpumask_test_cpu(policy
->cpu
,
256 &cpufreq_dev
->allowed_cpus
))
259 max_freq
= cpufreq_dev
->cpufreq_val
;
261 if (policy
->max
!= max_freq
)
262 cpufreq_verify_within_limits(policy
, 0, max_freq
);
264 mutex_unlock(&cooling_cpufreq_lock
);
269 /* cpufreq cooling device callback functions are defined below */
272 * cpufreq_get_max_state - callback function to get the max cooling state.
273 * @cdev: thermal cooling device pointer.
274 * @state: fill this variable with the max cooling state.
276 * Callback for the thermal cooling device to return the cpufreq
279 * Return: 0 on success, an error code otherwise.
281 static int cpufreq_get_max_state(struct thermal_cooling_device
*cdev
,
282 unsigned long *state
)
284 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
286 *state
= cpufreq_device
->max_level
;
291 * cpufreq_get_cur_state - callback function to get the current cooling state.
292 * @cdev: thermal cooling device pointer.
293 * @state: fill this variable with the current cooling state.
295 * Callback for the thermal cooling device to return the cpufreq
296 * current cooling state.
298 * Return: 0 on success, an error code otherwise.
300 static int cpufreq_get_cur_state(struct thermal_cooling_device
*cdev
,
301 unsigned long *state
)
303 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
305 *state
= cpufreq_device
->cpufreq_state
;
311 * cpufreq_set_cur_state - callback function to set the current cooling state.
312 * @cdev: thermal cooling device pointer.
313 * @state: set this variable to the current cooling state.
315 * Callback for the thermal cooling device to change the cpufreq
316 * current cooling state.
318 * Return: 0 on success, an error code otherwise.
320 static int cpufreq_set_cur_state(struct thermal_cooling_device
*cdev
,
323 struct cpufreq_cooling_device
*cpufreq_device
= cdev
->devdata
;
324 unsigned int cpu
= cpumask_any(&cpufreq_device
->allowed_cpus
);
325 unsigned int clip_freq
;
328 /* Check if the old cooling action is same as new cooling action */
329 if (cpufreq_device
->cpufreq_state
== state
)
332 ret
= get_property(cpufreq_device
, state
, &clip_freq
, GET_FREQ
);
336 cpufreq_device
->cpufreq_state
= state
;
337 cpufreq_device
->cpufreq_val
= clip_freq
;
339 cpufreq_update_policy(cpu
);
344 /* Bind cpufreq callbacks to thermal cooling device ops */
345 static struct thermal_cooling_device_ops
const cpufreq_cooling_ops
= {
346 .get_max_state
= cpufreq_get_max_state
,
347 .get_cur_state
= cpufreq_get_cur_state
,
348 .set_cur_state
= cpufreq_set_cur_state
,
351 /* Notifier for cpufreq policy change */
352 static struct notifier_block thermal_cpufreq_notifier_block
= {
353 .notifier_call
= cpufreq_thermal_notifier
,
356 static unsigned int find_next_max(struct cpufreq_frequency_table
*table
,
357 unsigned int prev_max
)
359 struct cpufreq_frequency_table
*pos
;
360 unsigned int max
= 0;
362 cpufreq_for_each_valid_entry(pos
, table
) {
363 if (pos
->frequency
> max
&& pos
->frequency
< prev_max
)
364 max
= pos
->frequency
;
371 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
372 * @np: a valid struct device_node to the cooling device device tree node
373 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
374 * Normally this should be same as cpufreq policy->related_cpus.
376 * This interface function registers the cpufreq cooling device with the name
377 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
378 * cooling devices. It also gives the opportunity to link the cooling device
379 * with a device tree node, in order to bind it via the thermal DT code.
381 * Return: a valid struct thermal_cooling_device pointer on success,
382 * on failure, it returns a corresponding ERR_PTR().
384 static struct thermal_cooling_device
*
385 __cpufreq_cooling_register(struct device_node
*np
,
386 const struct cpumask
*clip_cpus
)
388 struct thermal_cooling_device
*cool_dev
;
389 struct cpufreq_cooling_device
*cpufreq_dev
;
390 char dev_name
[THERMAL_NAME_LENGTH
];
391 struct cpufreq_frequency_table
*pos
, *table
;
392 unsigned int freq
, i
;
395 table
= cpufreq_frequency_get_table(cpumask_first(clip_cpus
));
397 pr_debug("%s: CPUFreq table not found\n", __func__
);
398 return ERR_PTR(-EPROBE_DEFER
);
401 cpufreq_dev
= kzalloc(sizeof(*cpufreq_dev
), GFP_KERNEL
);
403 return ERR_PTR(-ENOMEM
);
405 ret
= get_property(cpufreq_dev
, 0, &cpufreq_dev
->cpufreq_val
, GET_FREQ
);
407 pr_err("%s: Failed to get frequency: %d", __func__
, ret
);
408 cool_dev
= ERR_PTR(ret
);
412 /* Find max levels */
413 cpufreq_for_each_valid_entry(pos
, table
)
414 cpufreq_dev
->max_level
++;
416 cpufreq_dev
->freq_table
= kmalloc(sizeof(*cpufreq_dev
->freq_table
) *
417 cpufreq_dev
->max_level
, GFP_KERNEL
);
418 if (!cpufreq_dev
->freq_table
) {
419 return ERR_PTR(-ENOMEM
);
420 cool_dev
= ERR_PTR(-ENOMEM
);
424 /* max_level is an index, not a counter */
425 cpufreq_dev
->max_level
--;
427 cpumask_copy(&cpufreq_dev
->allowed_cpus
, clip_cpus
);
429 ret
= get_idr(&cpufreq_idr
, &cpufreq_dev
->id
);
431 cool_dev
= ERR_PTR(ret
);
435 snprintf(dev_name
, sizeof(dev_name
), "thermal-cpufreq-%d",
438 cool_dev
= thermal_of_cooling_device_register(np
, dev_name
, cpufreq_dev
,
439 &cpufreq_cooling_ops
);
440 if (IS_ERR(cool_dev
))
443 /* Fill freq-table in descending order of frequencies */
444 for (i
= 0, freq
= -1; i
<= cpufreq_dev
->max_level
; i
++) {
445 freq
= find_next_max(table
, freq
);
446 cpufreq_dev
->freq_table
[i
] = freq
;
448 /* Warn for duplicate entries */
450 pr_warn("%s: table has duplicate entries\n", __func__
);
452 pr_debug("%s: freq:%u KHz\n", __func__
, freq
);
455 cpufreq_dev
->cool_dev
= cool_dev
;
457 mutex_lock(&cooling_cpufreq_lock
);
459 /* Register the notifier for first cpufreq cooling device */
460 if (list_empty(&cpufreq_dev_list
))
461 cpufreq_register_notifier(&thermal_cpufreq_notifier_block
,
462 CPUFREQ_POLICY_NOTIFIER
);
463 list_add(&cpufreq_dev
->node
, &cpufreq_dev_list
);
465 mutex_unlock(&cooling_cpufreq_lock
);
470 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
472 kfree(cpufreq_dev
->freq_table
);
480 * cpufreq_cooling_register - function to create cpufreq cooling device.
481 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
483 * This interface function registers the cpufreq cooling device with the name
484 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
487 * Return: a valid struct thermal_cooling_device pointer on success,
488 * on failure, it returns a corresponding ERR_PTR().
490 struct thermal_cooling_device
*
491 cpufreq_cooling_register(const struct cpumask
*clip_cpus
)
493 return __cpufreq_cooling_register(NULL
, clip_cpus
);
495 EXPORT_SYMBOL_GPL(cpufreq_cooling_register
);
498 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
499 * @np: a valid struct device_node to the cooling device device tree node
500 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
502 * This interface function registers the cpufreq cooling device with the name
503 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
504 * cooling devices. Using this API, the cpufreq cooling device will be
505 * linked to the device tree node provided.
507 * Return: a valid struct thermal_cooling_device pointer on success,
508 * on failure, it returns a corresponding ERR_PTR().
510 struct thermal_cooling_device
*
511 of_cpufreq_cooling_register(struct device_node
*np
,
512 const struct cpumask
*clip_cpus
)
515 return ERR_PTR(-EINVAL
);
517 return __cpufreq_cooling_register(np
, clip_cpus
);
519 EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register
);
522 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
523 * @cdev: thermal cooling device pointer.
525 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
527 void cpufreq_cooling_unregister(struct thermal_cooling_device
*cdev
)
529 struct cpufreq_cooling_device
*cpufreq_dev
;
534 cpufreq_dev
= cdev
->devdata
;
535 mutex_lock(&cooling_cpufreq_lock
);
536 list_del(&cpufreq_dev
->node
);
538 /* Unregister the notifier for the last cpufreq cooling device */
539 if (list_empty(&cpufreq_dev_list
))
540 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block
,
541 CPUFREQ_POLICY_NOTIFIER
);
542 mutex_unlock(&cooling_cpufreq_lock
);
544 thermal_cooling_device_unregister(cpufreq_dev
->cool_dev
);
545 release_idr(&cpufreq_idr
, cpufreq_dev
->id
);
546 kfree(cpufreq_dev
->freq_table
);
549 EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister
);