thermal: cpu_cooling: remove unnecessary wrapper get_cpu_frequency()
[linux-2.6-block.git] / drivers / thermal / cpu_cooling.c
CommitLineData
02361418
ADK
1/*
2 * linux/drivers/thermal/cpu_cooling.c
3 *
4 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
5 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
6 *
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.
11 *
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.
16 *
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.
20 *
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 */
02361418
ADK
23#include <linux/module.h>
24#include <linux/thermal.h>
02361418
ADK
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>
30
07d888d8
VK
31/*
32 * Cooling state <-> CPUFreq frequency
33 *
34 * Cooling states are translated to frequencies throughout this driver and this
35 * is the relation between them.
36 *
37 * Highest cooling state corresponds to lowest possible frequency.
38 *
39 * i.e.
40 * level 0 --> 1st Max Freq
41 * level 1 --> 2nd Max Freq
42 * ...
43 */
44
02361418 45/**
3b3c0748 46 * struct cpufreq_cooling_device - data for cooling device with cpufreq
02361418
ADK
47 * @id: unique integer value corresponding to each cpufreq_cooling_device
48 * registered.
3b3c0748
EV
49 * @cool_dev: thermal_cooling_device pointer to keep track of the
50 * registered cooling device.
02361418
ADK
51 * @cpufreq_state: integer value representing the current state of cpufreq
52 * cooling devices.
53 * @cpufreq_val: integer value representing the absolute value of the clipped
54 * frequency.
55 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
02361418 56 *
beca6053
VK
57 * This structure is required for keeping information of each registered
58 * cpufreq_cooling_device.
02361418
ADK
59 */
60struct cpufreq_cooling_device {
61 int id;
62 struct thermal_cooling_device *cool_dev;
63 unsigned int cpufreq_state;
64 unsigned int cpufreq_val;
65 struct cpumask allowed_cpus;
2dcd851f 66 struct list_head node;
02361418 67};
02361418 68static DEFINE_IDR(cpufreq_idr);
160b7d80 69static DEFINE_MUTEX(cooling_cpufreq_lock);
02361418 70
160b7d80 71static unsigned int cpufreq_dev_count;
02361418 72
2dcd851f 73static LIST_HEAD(cpufreq_dev_list);
02361418
ADK
74
75/**
76 * get_idr - function to get a unique id.
77 * @idr: struct idr * handle used to create a id.
78 * @id: int * value generated by this function.
79491e53
EV
79 *
80 * This function will populate @id with an unique
81 * id, using the idr API.
82 *
83 * Return: 0 on success, an error code on failure.
02361418
ADK
84 */
85static int get_idr(struct idr *idr, int *id)
86{
6deb69fa 87 int ret;
02361418
ADK
88
89 mutex_lock(&cooling_cpufreq_lock);
6deb69fa 90 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
02361418 91 mutex_unlock(&cooling_cpufreq_lock);
6deb69fa
TH
92 if (unlikely(ret < 0))
93 return ret;
94 *id = ret;
79491e53 95
02361418
ADK
96 return 0;
97}
98
99/**
100 * release_idr - function to free the unique id.
101 * @idr: struct idr * handle used for creating the id.
102 * @id: int value representing the unique id.
103 */
104static void release_idr(struct idr *idr, int id)
105{
106 mutex_lock(&cooling_cpufreq_lock);
107 idr_remove(idr, id);
108 mutex_unlock(&cooling_cpufreq_lock);
109}
110
111/* Below code defines functions to be used for cpufreq as cooling device */
112
fc35b35c
ZR
113enum cpufreq_cooling_property {
114 GET_LEVEL,
115 GET_FREQ,
116 GET_MAXL,
117};
118
2d6f28fe 119/**
728c03c9 120 * get_property - fetch a property of interest for a given cpu.
2d6f28fe
EV
121 * @cpu: cpu for which the property is required
122 * @input: query parameter
123 * @output: query return
124 * @property: type of query (frequency, level, max level)
125 *
126 * This is the common function to
fc35b35c
ZR
127 * 1. get maximum cpu cooling states
128 * 2. translate frequency to cooling state
129 * 3. translate cooling state to frequency
728c03c9 130 *
fc35b35c
ZR
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.
2d6f28fe
EV
136 *
137 * Return: 0 on success, -EINVAL when invalid parameters are passed.
138 */
fc35b35c 139static int get_property(unsigned int cpu, unsigned long input,
d8892a01
EV
140 unsigned int *output,
141 enum cpufreq_cooling_property property)
02361418 142{
3c84ef3a 143 int i;
4469b997 144 unsigned long max_level = 0, level = 0;
fc35b35c
ZR
145 unsigned int freq = CPUFREQ_ENTRY_INVALID;
146 int descend = -1;
3c84ef3a 147 struct cpufreq_frequency_table *pos, *table =
02361418 148 cpufreq_frequency_get_table(cpu);
79d26401 149
fc35b35c
ZR
150 if (!output)
151 return -EINVAL;
152
02361418 153 if (!table)
fc35b35c 154 return -EINVAL;
02361418 155
3c84ef3a 156 cpufreq_for_each_valid_entry(pos, table) {
fc35b35c 157 /* ignore duplicate entry */
3c84ef3a 158 if (freq == pos->frequency)
fc35b35c
ZR
159 continue;
160
161 /* get the frequency order */
24c7a381 162 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
3c84ef3a 163 descend = freq > pos->frequency;
02361418 164
3c84ef3a 165 freq = pos->frequency;
fc35b35c 166 max_level++;
02361418 167 }
a116776f
ZR
168
169 /* No valid cpu frequency entry */
170 if (max_level == 0)
171 return -EINVAL;
172
1c9573a4
EV
173 /* max_level is an index, not a counter */
174 max_level--;
02361418 175
fc35b35c
ZR
176 /* get max level */
177 if (property == GET_MAXL) {
178 *output = (unsigned int)max_level;
179 return 0;
180 }
02361418 181
fc35b35c 182 if (property == GET_FREQ)
1c9573a4 183 level = descend ? input : (max_level - input);
fc35b35c 184
3c84ef3a
SK
185 i = 0;
186 cpufreq_for_each_valid_entry(pos, table) {
fc35b35c 187 /* ignore duplicate entry */
3c84ef3a 188 if (freq == pos->frequency)
fc35b35c
ZR
189 continue;
190
191 /* now we have a valid frequency entry */
3c84ef3a 192 freq = pos->frequency;
fc35b35c
ZR
193
194 if (property == GET_LEVEL && (unsigned int)input == freq) {
195 /* get level by frequency */
3c84ef3a 196 *output = descend ? i : (max_level - i);
fc35b35c
ZR
197 return 0;
198 }
3c84ef3a 199 if (property == GET_FREQ && level == i) {
fc35b35c
ZR
200 /* get frequency by level */
201 *output = freq;
202 return 0;
203 }
3c84ef3a 204 i++;
02361418 205 }
79491e53 206
fc35b35c
ZR
207 return -EINVAL;
208}
209
44952d33 210/**
728c03c9 211 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
44952d33
EV
212 * @cpu: cpu for which the level is required
213 * @freq: the frequency of interest
214 *
215 * This function will match the cooling level corresponding to the
216 * requested @freq and return it.
217 *
218 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
219 * otherwise.
220 */
57df8106
ZR
221unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
222{
223 unsigned int val;
224
225 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
226 return THERMAL_CSTATE_INVALID;
79491e53 227
57df8106
ZR
228 return (unsigned long)val;
229}
243dbd9c 230EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
57df8106 231
02361418
ADK
232/**
233 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
234 * @nb: struct notifier_block * with callback info.
235 * @event: value showing cpufreq event for which this function invoked.
236 * @data: callback-specific data
bab30554 237 *
9746b6e7 238 * Callback to hijack the notification on cpufreq policy transition.
bab30554
EV
239 * Every time there is a change in policy, we will intercept and
240 * update the cpufreq policy with thermal constraints.
241 *
242 * Return: 0 (success)
02361418
ADK
243 */
244static int cpufreq_thermal_notifier(struct notifier_block *nb,
5fda7f68 245 unsigned long event, void *data)
02361418
ADK
246{
247 struct cpufreq_policy *policy = data;
248 unsigned long max_freq = 0;
2dcd851f 249 struct cpufreq_cooling_device *cpufreq_dev;
02361418 250
2dcd851f 251 if (event != CPUFREQ_ADJUST)
02361418
ADK
252 return 0;
253
2dcd851f
YSB
254 mutex_lock(&cooling_cpufreq_lock);
255 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
256 if (!cpumask_test_cpu(policy->cpu,
257 &cpufreq_dev->allowed_cpus))
258 continue;
259
2dcd851f 260 max_freq = cpufreq_dev->cpufreq_val;
02361418 261
2dcd851f
YSB
262 if (policy->max != max_freq)
263 cpufreq_verify_within_limits(policy, 0, max_freq);
264 }
265 mutex_unlock(&cooling_cpufreq_lock);
02361418
ADK
266
267 return 0;
268}
269
1b9e3526 270/* cpufreq cooling device callback functions are defined below */
02361418
ADK
271
272/**
273 * cpufreq_get_max_state - callback function to get the max cooling state.
274 * @cdev: thermal cooling device pointer.
275 * @state: fill this variable with the max cooling state.
62c00421
EV
276 *
277 * Callback for the thermal cooling device to return the cpufreq
278 * max cooling state.
279 *
280 * Return: 0 on success, an error code otherwise.
02361418
ADK
281 */
282static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
283 unsigned long *state)
284{
160b7d80 285 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
bde00663 286 struct cpumask *mask = &cpufreq_device->allowed_cpus;
02361418 287 unsigned int cpu;
4f89038f 288 unsigned int count = 0;
fc35b35c 289 int ret;
02361418 290
bde00663 291 cpu = cpumask_any(mask);
02361418 292
4f89038f 293 ret = get_property(cpu, 0, &count, GET_MAXL);
9c51b05a 294
fc35b35c
ZR
295 if (count > 0)
296 *state = count;
02361418 297
fc35b35c 298 return ret;
02361418
ADK
299}
300
301/**
302 * cpufreq_get_cur_state - callback function to get the current cooling state.
303 * @cdev: thermal cooling device pointer.
304 * @state: fill this variable with the current cooling state.
3672552d
EV
305 *
306 * Callback for the thermal cooling device to return the cpufreq
307 * current cooling state.
308 *
309 * Return: 0 on success, an error code otherwise.
02361418
ADK
310 */
311static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
312 unsigned long *state)
313{
160b7d80 314 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
02361418 315
160b7d80 316 *state = cpufreq_device->cpufreq_state;
79491e53 317
160b7d80 318 return 0;
02361418
ADK
319}
320
321/**
322 * cpufreq_set_cur_state - callback function to set the current cooling state.
323 * @cdev: thermal cooling device pointer.
324 * @state: set this variable to the current cooling state.
56e05fdb
EV
325 *
326 * Callback for the thermal cooling device to change the cpufreq
327 * current cooling state.
328 *
329 * Return: 0 on success, an error code otherwise.
02361418
ADK
330 */
331static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
332 unsigned long state)
333{
160b7d80 334 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
5194fe46
VK
335 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
336 unsigned int clip_freq;
521a2e58 337 int ret;
5194fe46
VK
338
339 /* Check if the old cooling action is same as new cooling action */
340 if (cpufreq_device->cpufreq_state == state)
341 return 0;
02361418 342
521a2e58
VK
343 ret = get_property(cpu, state, &clip_freq, GET_FREQ);
344 if (ret)
345 return ret;
5194fe46
VK
346
347 cpufreq_device->cpufreq_state = state;
348 cpufreq_device->cpufreq_val = clip_freq;
349
350 cpufreq_update_policy(cpu);
351
352 return 0;
02361418
ADK
353}
354
355/* Bind cpufreq callbacks to thermal cooling device ops */
356static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
357 .get_max_state = cpufreq_get_max_state,
358 .get_cur_state = cpufreq_get_cur_state,
359 .set_cur_state = cpufreq_set_cur_state,
360};
361
362/* Notifier for cpufreq policy change */
363static struct notifier_block thermal_cpufreq_notifier_block = {
364 .notifier_call = cpufreq_thermal_notifier,
365};
366
367/**
39d99cff
EV
368 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
369 * @np: a valid struct device_node to the cooling device device tree node
02361418 370 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
405fb825 371 * Normally this should be same as cpufreq policy->related_cpus.
12cb08ba
EV
372 *
373 * This interface function registers the cpufreq cooling device with the name
374 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
39d99cff
EV
375 * cooling devices. It also gives the opportunity to link the cooling device
376 * with a device tree node, in order to bind it via the thermal DT code.
12cb08ba
EV
377 *
378 * Return: a valid struct thermal_cooling_device pointer on success,
379 * on failure, it returns a corresponding ERR_PTR().
02361418 380 */
39d99cff
EV
381static struct thermal_cooling_device *
382__cpufreq_cooling_register(struct device_node *np,
383 const struct cpumask *clip_cpus)
02361418
ADK
384{
385 struct thermal_cooling_device *cool_dev;
5d3bdb89 386 struct cpufreq_cooling_device *cpufreq_dev;
02361418 387 char dev_name[THERMAL_NAME_LENGTH];
405fb825 388 int ret;
02361418 389
0f1be51c
EV
390 if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) {
391 pr_debug("%s: CPUFreq table not found\n", __func__);
392 return ERR_PTR(-EPROBE_DEFER);
393 }
394
98d522f0 395 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
02361418
ADK
396 if (!cpufreq_dev)
397 return ERR_PTR(-ENOMEM);
398
521a2e58
VK
399 ret = get_property(cpumask_any(clip_cpus), 0, &cpufreq_dev->cpufreq_val,
400 GET_FREQ);
401 if (ret) {
402 pr_err("%s: Failed to get frequency: %d", __func__, ret);
403 cool_dev = ERR_PTR(ret);
7adb635b
VK
404 goto free_cdev;
405 }
406
02361418
ADK
407 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
408
02361418
ADK
409 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
410 if (ret) {
730abe06
VK
411 cool_dev = ERR_PTR(ret);
412 goto free_cdev;
02361418
ADK
413 }
414
99871a71
EV
415 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
416 cpufreq_dev->id);
02361418 417
39d99cff
EV
418 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
419 &cpufreq_cooling_ops);
730abe06
VK
420 if (IS_ERR(cool_dev))
421 goto remove_idr;
422
02361418 423 cpufreq_dev->cool_dev = cool_dev;
92e615ec 424
02361418 425 mutex_lock(&cooling_cpufreq_lock);
02361418
ADK
426
427 /* Register the notifier for first cpufreq cooling device */
428 if (cpufreq_dev_count == 0)
429 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 430 CPUFREQ_POLICY_NOTIFIER);
160b7d80 431 cpufreq_dev_count++;
2dcd851f 432 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
02361418
ADK
433
434 mutex_unlock(&cooling_cpufreq_lock);
79491e53 435
730abe06
VK
436 return cool_dev;
437
438remove_idr:
439 release_idr(&cpufreq_idr, cpufreq_dev->id);
440free_cdev:
441 kfree(cpufreq_dev);
442
02361418
ADK
443 return cool_dev;
444}
39d99cff
EV
445
446/**
447 * cpufreq_cooling_register - function to create cpufreq cooling device.
448 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
449 *
450 * This interface function registers the cpufreq cooling device with the name
451 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
452 * cooling devices.
453 *
454 * Return: a valid struct thermal_cooling_device pointer on success,
455 * on failure, it returns a corresponding ERR_PTR().
456 */
457struct thermal_cooling_device *
458cpufreq_cooling_register(const struct cpumask *clip_cpus)
459{
460 return __cpufreq_cooling_register(NULL, clip_cpus);
461}
243dbd9c 462EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
02361418 463
39d99cff
EV
464/**
465 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
466 * @np: a valid struct device_node to the cooling device device tree node
467 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
468 *
469 * This interface function registers the cpufreq cooling device with the name
470 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
471 * cooling devices. Using this API, the cpufreq cooling device will be
472 * linked to the device tree node provided.
473 *
474 * Return: a valid struct thermal_cooling_device pointer on success,
475 * on failure, it returns a corresponding ERR_PTR().
476 */
477struct thermal_cooling_device *
478of_cpufreq_cooling_register(struct device_node *np,
479 const struct cpumask *clip_cpus)
480{
481 if (!np)
482 return ERR_PTR(-EINVAL);
483
484 return __cpufreq_cooling_register(np, clip_cpus);
485}
486EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
487
02361418
ADK
488/**
489 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
490 * @cdev: thermal cooling device pointer.
135266b4
EV
491 *
492 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
02361418
ADK
493 */
494void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
495{
50e66c7e 496 struct cpufreq_cooling_device *cpufreq_dev;
02361418 497
50e66c7e
EV
498 if (!cdev)
499 return;
500
501 cpufreq_dev = cdev->devdata;
02361418 502 mutex_lock(&cooling_cpufreq_lock);
2dcd851f 503 list_del(&cpufreq_dev->node);
160b7d80 504 cpufreq_dev_count--;
02361418
ADK
505
506 /* Unregister the notifier for the last cpufreq cooling device */
2d9bf71c 507 if (cpufreq_dev_count == 0)
02361418 508 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
5fda7f68 509 CPUFREQ_POLICY_NOTIFIER);
02361418 510 mutex_unlock(&cooling_cpufreq_lock);
160b7d80 511
02361418
ADK
512 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
513 release_idr(&cpufreq_idr, cpufreq_dev->id);
02361418
ADK
514 kfree(cpufreq_dev);
515}
243dbd9c 516EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);