blob: ba23150c7bdea5f3d7bc716725c291eb5b9f1f74 [file] [log] [blame]
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301/*
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 *
Viresh Kumar73904cb2014-12-04 09:42:08 +05307 * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org>
8 *
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05309 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053025#include <linux/module.h>
26#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053027#include <linux/cpufreq.h>
28#include <linux/err.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000029#include <linux/pm_opp.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053030#include <linux/slab.h>
31#include <linux/cpu.h>
32#include <linux/cpu_cooling.h>
33
Viresh Kumar07d888d2014-12-04 09:41:49 +053034/*
35 * Cooling state <-> CPUFreq frequency
36 *
37 * Cooling states are translated to frequencies throughout this driver and this
38 * is the relation between them.
39 *
40 * Highest cooling state corresponds to lowest possible frequency.
41 *
42 * i.e.
43 * level 0 --> 1st Max Freq
44 * level 1 --> 2nd Max Freq
45 * ...
46 */
47
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053048/**
Javi Merinoc36cf072015-02-26 19:00:29 +000049 * struct power_table - frequency to power conversion
50 * @frequency: frequency in KHz
51 * @power: power in mW
52 *
53 * This structure is built when the cooling device registers and helps
54 * in translating frequency to power and viceversa.
55 */
56struct power_table {
57 u32 frequency;
58 u32 power;
59};
60
61/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000062 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053063 * @id: unique integer value corresponding to each cpufreq_cooling_device
64 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000065 * @cool_dev: thermal_cooling_device pointer to keep track of the
66 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053067 * @cpufreq_state: integer value representing the current state of cpufreq
68 * cooling devices.
69 * @cpufreq_val: integer value representing the absolute value of the clipped
70 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053071 * @max_level: maximum cooling level. One less than total number of valid
72 * cpufreq frequencies.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053073 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Javi Merinofc4de352014-12-15 16:55:52 +000074 * @node: list_head to link all cpufreq_cooling_device together.
Javi Merinoc36cf072015-02-26 19:00:29 +000075 * @last_load: load measured by the latest call to cpufreq_get_actual_power()
76 * @time_in_idle: previous reading of the absolute time that this cpu was idle
77 * @time_in_idle_timestamp: wall time of the last invocation of
78 * get_cpu_idle_time_us()
79 * @dyn_power_table: array of struct power_table for frequency to power
80 * conversion, sorted in ascending order.
81 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
82 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
83 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053084 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053085 * This structure is required for keeping information of each registered
86 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053087 */
88struct cpufreq_cooling_device {
89 int id;
90 struct thermal_cooling_device *cool_dev;
91 unsigned int cpufreq_state;
92 unsigned int cpufreq_val;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053093 unsigned int max_level;
Viresh Kumarf6859012014-12-04 09:42:06 +053094 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053095 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053096 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +000097 u32 last_load;
98 u64 *time_in_idle;
99 u64 *time_in_idle_timestamp;
100 struct power_table *dyn_power_table;
101 int dyn_power_table_entries;
102 struct device *cpu_dev;
103 get_static_t plat_get_static_power;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530104};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530105static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100106static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530108static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530109
110/**
111 * get_idr - function to get a unique id.
112 * @idr: struct idr * handle used to create a id.
113 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +0000114 *
115 * This function will populate @id with an unique
116 * id, using the idr API.
117 *
118 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530119 */
120static int get_idr(struct idr *idr, int *id)
121{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800122 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530123
124 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800125 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530126 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800127 if (unlikely(ret < 0))
128 return ret;
129 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000130
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530131 return 0;
132}
133
134/**
135 * release_idr - function to free the unique id.
136 * @idr: struct idr * handle used for creating the id.
137 * @id: int value representing the unique id.
138 */
139static void release_idr(struct idr *idr, int id)
140{
141 mutex_lock(&cooling_cpufreq_lock);
142 idr_remove(idr, id);
143 mutex_unlock(&cooling_cpufreq_lock);
144}
145
146/* Below code defines functions to be used for cpufreq as cooling device */
147
148/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530149 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530150 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530151 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000152 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530153 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530154 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530155static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
156 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530157{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530158 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000159
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530160 for (level = 0; level <= cpufreq_dev->max_level; level++) {
161 if (freq == cpufreq_dev->freq_table[level])
162 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530163
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530164 if (freq > cpufreq_dev->freq_table[level])
165 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800166 }
Zhang Ruia1167762014-01-02 11:57:48 +0800167
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530168 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800169}
170
Eduardo Valentin44952d32013-04-17 17:12:05 +0000171/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530172 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000173 * @cpu: cpu for which the level is required
174 * @freq: the frequency of interest
175 *
176 * This function will match the cooling level corresponding to the
177 * requested @freq and return it.
178 *
179 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
180 * otherwise.
181 */
Zhang Rui57df8102013-02-08 14:52:06 +0800182unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
183{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530184 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800185
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530186 mutex_lock(&cooling_cpufreq_lock);
187 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
188 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530189 mutex_unlock(&cooling_cpufreq_lock);
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530190 return get_level(cpufreq_dev, freq);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530191 }
192 }
193 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000194
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530195 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
196 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800197}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000198EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800199
Javi Merinoc36cf072015-02-26 19:00:29 +0000200static void update_cpu_device(int cpu)
201{
202 struct cpufreq_cooling_device *cpufreq_dev;
203
204 mutex_lock(&cooling_cpufreq_lock);
205 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
206 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
207 cpufreq_dev->cpu_dev = get_cpu_device(cpu);
208 if (!cpufreq_dev->cpu_dev) {
209 dev_warn(&cpufreq_dev->cool_dev->device,
210 "No cpu device for new policy cpu %d\n",
211 cpu);
212 }
213 break;
214 }
215 }
216 mutex_unlock(&cooling_cpufreq_lock);
217}
218
219static void remove_cpu_device(int cpu)
220{
221 struct cpufreq_cooling_device *cpufreq_dev;
222
223 mutex_lock(&cooling_cpufreq_lock);
224 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
225 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
226 cpufreq_dev->cpu_dev = NULL;
227 break;
228 }
229 }
230 mutex_unlock(&cooling_cpufreq_lock);
231}
232
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530233/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530234 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
235 * @nb: struct notifier_block * with callback info.
236 * @event: value showing cpufreq event for which this function invoked.
237 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000238 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100239 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000240 * Every time there is a change in policy, we will intercept and
241 * update the cpufreq policy with thermal constraints.
242 *
243 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530244 */
245static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000246 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530247{
248 struct cpufreq_policy *policy = data;
249 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530250 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530251
Javi Merinoc36cf072015-02-26 19:00:29 +0000252 switch (event) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530253
Javi Merinoc36cf072015-02-26 19:00:29 +0000254 case CPUFREQ_ADJUST:
255 mutex_lock(&cooling_cpufreq_lock);
256 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
257 if (!cpumask_test_cpu(policy->cpu,
258 &cpufreq_dev->allowed_cpus))
259 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530260
Javi Merinoc36cf072015-02-26 19:00:29 +0000261 max_freq = cpufreq_dev->cpufreq_val;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530262
Javi Merinoc36cf072015-02-26 19:00:29 +0000263 if (policy->max != max_freq)
264 cpufreq_verify_within_limits(policy, 0,
265 max_freq);
266 }
267 mutex_unlock(&cooling_cpufreq_lock);
268 break;
269
270 case CPUFREQ_CREATE_POLICY:
271 update_cpu_device(policy->cpu);
272 break;
273 case CPUFREQ_REMOVE_POLICY:
274 remove_cpu_device(policy->cpu);
275 break;
276 default:
277 return NOTIFY_DONE;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530278 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530279
Javi Merinoc36cf072015-02-26 19:00:29 +0000280 return NOTIFY_OK;
281}
282
283/**
284 * build_dyn_power_table() - create a dynamic power to frequency table
285 * @cpufreq_device: the cpufreq cooling device in which to store the table
286 * @capacitance: dynamic power coefficient for these cpus
287 *
288 * Build a dynamic power to frequency table for this cpu and store it
289 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
290 * cpu_freq_to_power() to convert between power and frequency
291 * efficiently. Power is stored in mW, frequency in KHz. The
292 * resulting table is in ascending order.
293 *
294 * Return: 0 on success, -E* on error.
295 */
296static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
297 u32 capacitance)
298{
299 struct power_table *power_table;
300 struct dev_pm_opp *opp;
301 struct device *dev = NULL;
302 int num_opps = 0, cpu, i, ret = 0;
303 unsigned long freq;
304
305 rcu_read_lock();
306
307 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
308 dev = get_cpu_device(cpu);
309 if (!dev) {
310 dev_warn(&cpufreq_device->cool_dev->device,
311 "No cpu device for cpu %d\n", cpu);
312 continue;
313 }
314
315 num_opps = dev_pm_opp_get_opp_count(dev);
316 if (num_opps > 0) {
317 break;
318 } else if (num_opps < 0) {
319 ret = num_opps;
320 goto unlock;
321 }
322 }
323
324 if (num_opps == 0) {
325 ret = -EINVAL;
326 goto unlock;
327 }
328
329 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
330
331 for (freq = 0, i = 0;
332 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
333 freq++, i++) {
334 u32 freq_mhz, voltage_mv;
335 u64 power;
336
337 freq_mhz = freq / 1000000;
338 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
339
340 /*
341 * Do the multiplication with MHz and millivolt so as
342 * to not overflow.
343 */
344 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
345 do_div(power, 1000000000);
346
347 /* frequency is stored in power_table in KHz */
348 power_table[i].frequency = freq / 1000;
349
350 /* power is stored in mW */
351 power_table[i].power = power;
352 }
353
354 if (i == 0) {
355 ret = PTR_ERR(opp);
356 goto unlock;
357 }
358
359 cpufreq_device->cpu_dev = dev;
360 cpufreq_device->dyn_power_table = power_table;
361 cpufreq_device->dyn_power_table_entries = i;
362
363unlock:
364 rcu_read_unlock();
365 return ret;
366}
367
368static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
369 u32 freq)
370{
371 int i;
372 struct power_table *pt = cpufreq_device->dyn_power_table;
373
374 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
375 if (freq < pt[i].frequency)
376 break;
377
378 return pt[i - 1].power;
379}
380
381static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
382 u32 power)
383{
384 int i;
385 struct power_table *pt = cpufreq_device->dyn_power_table;
386
387 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
388 if (power < pt[i].power)
389 break;
390
391 return pt[i - 1].frequency;
392}
393
394/**
395 * get_load() - get load for a cpu since last updated
396 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
397 * @cpu: cpu number
398 *
399 * Return: The average load of cpu @cpu in percentage since this
400 * function was last called.
401 */
402static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu)
403{
404 u32 load;
405 u64 now, now_idle, delta_time, delta_idle;
406
407 now_idle = get_cpu_idle_time(cpu, &now, 0);
408 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu];
409 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu];
410
411 if (delta_time <= delta_idle)
412 load = 0;
413 else
414 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
415
416 cpufreq_device->time_in_idle[cpu] = now_idle;
417 cpufreq_device->time_in_idle_timestamp[cpu] = now;
418
419 return load;
420}
421
422/**
423 * get_static_power() - calculate the static power consumed by the cpus
424 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
425 * @tz: thermal zone device in which we're operating
426 * @freq: frequency in KHz
427 * @power: pointer in which to store the calculated static power
428 *
429 * Calculate the static power consumed by the cpus described by
430 * @cpu_actor running at frequency @freq. This function relies on a
431 * platform specific function that should have been provided when the
432 * actor was registered. If it wasn't, the static power is assumed to
433 * be negligible. The calculated static power is stored in @power.
434 *
435 * Return: 0 on success, -E* on failure.
436 */
437static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
438 struct thermal_zone_device *tz, unsigned long freq,
439 u32 *power)
440{
441 struct dev_pm_opp *opp;
442 unsigned long voltage;
443 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
444 unsigned long freq_hz = freq * 1000;
445
446 if (!cpufreq_device->plat_get_static_power ||
447 !cpufreq_device->cpu_dev) {
448 *power = 0;
449 return 0;
450 }
451
452 rcu_read_lock();
453
454 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
455 true);
456 voltage = dev_pm_opp_get_voltage(opp);
457
458 rcu_read_unlock();
459
460 if (voltage == 0) {
461 dev_warn_ratelimited(cpufreq_device->cpu_dev,
462 "Failed to get voltage for frequency %lu: %ld\n",
463 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
464 return -EINVAL;
465 }
466
467 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
468 voltage, power);
469}
470
471/**
472 * get_dynamic_power() - calculate the dynamic power
473 * @cpufreq_device: &cpufreq_cooling_device for this cdev
474 * @freq: current frequency
475 *
476 * Return: the dynamic power consumed by the cpus described by
477 * @cpufreq_device.
478 */
479static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
480 unsigned long freq)
481{
482 u32 raw_cpu_power;
483
484 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
485 return (raw_cpu_power * cpufreq_device->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530486}
487
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000488/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530489
490/**
491 * cpufreq_get_max_state - callback function to get the max cooling state.
492 * @cdev: thermal cooling device pointer.
493 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000494 *
495 * Callback for the thermal cooling device to return the cpufreq
496 * max cooling state.
497 *
498 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530499 */
500static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
501 unsigned long *state)
502{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100503 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530504
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530505 *state = cpufreq_device->max_level;
506 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530507}
508
509/**
510 * cpufreq_get_cur_state - callback function to get the current cooling state.
511 * @cdev: thermal cooling device pointer.
512 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000513 *
514 * Callback for the thermal cooling device to return the cpufreq
515 * current cooling state.
516 *
517 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530518 */
519static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
520 unsigned long *state)
521{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100522 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530523
hongbo.zhang160b7d82012-10-30 17:48:59 +0100524 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000525
hongbo.zhang160b7d82012-10-30 17:48:59 +0100526 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530527}
528
529/**
530 * cpufreq_set_cur_state - callback function to set the current cooling state.
531 * @cdev: thermal cooling device pointer.
532 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000533 *
534 * Callback for the thermal cooling device to change the cpufreq
535 * current cooling state.
536 *
537 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530538 */
539static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
540 unsigned long state)
541{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100542 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530543 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
544 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530545
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530546 /* Request state should be less than max_level */
547 if (WARN_ON(state > cpufreq_device->max_level))
548 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530549
Viresh Kumar5194fe42014-12-04 09:42:00 +0530550 /* Check if the old cooling action is same as new cooling action */
551 if (cpufreq_device->cpufreq_state == state)
552 return 0;
553
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530554 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar5194fe42014-12-04 09:42:00 +0530555 cpufreq_device->cpufreq_state = state;
556 cpufreq_device->cpufreq_val = clip_freq;
557
558 cpufreq_update_policy(cpu);
559
560 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530561}
562
Javi Merinoc36cf072015-02-26 19:00:29 +0000563/**
564 * cpufreq_get_requested_power() - get the current power
565 * @cdev: &thermal_cooling_device pointer
566 * @tz: a valid thermal zone device pointer
567 * @power: pointer in which to store the resulting power
568 *
569 * Calculate the current power consumption of the cpus in milliwatts
570 * and store it in @power. This function should actually calculate
571 * the requested power, but it's hard to get the frequency that
572 * cpufreq would have assigned if there were no thermal limits.
573 * Instead, we calculate the current power on the assumption that the
574 * immediate future will look like the immediate past.
575 *
576 * We use the current frequency and the average load since this
577 * function was last called. In reality, there could have been
578 * multiple opps since this function was last called and that affects
579 * the load calculation. While it's not perfectly accurate, this
580 * simplification is good enough and works. REVISIT this, as more
581 * complex code may be needed if experiments show that it's not
582 * accurate enough.
583 *
584 * Return: 0 on success, -E* if getting the static power failed.
585 */
586static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
587 struct thermal_zone_device *tz,
588 u32 *power)
589{
590 unsigned long freq;
591 int cpu, ret;
592 u32 static_power, dynamic_power, total_load = 0;
593 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
594
595 freq = cpufreq_quick_get(cpumask_any(&cpufreq_device->allowed_cpus));
596
597 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
598 u32 load;
599
600 if (cpu_online(cpu))
601 load = get_load(cpufreq_device, cpu);
602 else
603 load = 0;
604
605 total_load += load;
606 }
607
608 cpufreq_device->last_load = total_load;
609
610 dynamic_power = get_dynamic_power(cpufreq_device, freq);
611 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
612 if (ret)
613 return ret;
614
615 *power = static_power + dynamic_power;
616 return 0;
617}
618
619/**
620 * cpufreq_state2power() - convert a cpu cdev state to power consumed
621 * @cdev: &thermal_cooling_device pointer
622 * @tz: a valid thermal zone device pointer
623 * @state: cooling device state to be converted
624 * @power: pointer in which to store the resulting power
625 *
626 * Convert cooling device state @state into power consumption in
627 * milliwatts assuming 100% load. Store the calculated power in
628 * @power.
629 *
630 * Return: 0 on success, -EINVAL if the cooling device state could not
631 * be converted into a frequency or other -E* if there was an error
632 * when calculating the static power.
633 */
634static int cpufreq_state2power(struct thermal_cooling_device *cdev,
635 struct thermal_zone_device *tz,
636 unsigned long state, u32 *power)
637{
638 unsigned int freq, num_cpus;
639 cpumask_t cpumask;
640 u32 static_power, dynamic_power;
641 int ret;
642 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
643
644 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
645 num_cpus = cpumask_weight(&cpumask);
646
647 /* None of our cpus are online, so no power */
648 if (num_cpus == 0) {
649 *power = 0;
650 return 0;
651 }
652
653 freq = cpufreq_device->freq_table[state];
654 if (!freq)
655 return -EINVAL;
656
657 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
658 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
659 if (ret)
660 return ret;
661
662 *power = static_power + dynamic_power;
663 return 0;
664}
665
666/**
667 * cpufreq_power2state() - convert power to a cooling device state
668 * @cdev: &thermal_cooling_device pointer
669 * @tz: a valid thermal zone device pointer
670 * @power: power in milliwatts to be converted
671 * @state: pointer in which to store the resulting state
672 *
673 * Calculate a cooling device state for the cpus described by @cdev
674 * that would allow them to consume at most @power mW and store it in
675 * @state. Note that this calculation depends on external factors
676 * such as the cpu load or the current static power. Calling this
677 * function with the same power as input can yield different cooling
678 * device states depending on those external factors.
679 *
680 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
681 * the calculated frequency could not be converted to a valid state.
682 * The latter should not happen unless the frequencies available to
683 * cpufreq have changed since the initialization of the cpu cooling
684 * device.
685 */
686static int cpufreq_power2state(struct thermal_cooling_device *cdev,
687 struct thermal_zone_device *tz, u32 power,
688 unsigned long *state)
689{
690 unsigned int cpu, cur_freq, target_freq;
691 int ret;
692 s32 dyn_power;
693 u32 last_load, normalised_power, static_power;
694 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
695
696 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
697
698 /* None of our cpus are online */
699 if (cpu >= nr_cpu_ids)
700 return -ENODEV;
701
702 cur_freq = cpufreq_quick_get(cpu);
703 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
704 if (ret)
705 return ret;
706
707 dyn_power = power - static_power;
708 dyn_power = dyn_power > 0 ? dyn_power : 0;
709 last_load = cpufreq_device->last_load ?: 1;
710 normalised_power = (dyn_power * 100) / last_load;
711 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
712
713 *state = cpufreq_cooling_get_level(cpu, target_freq);
714 if (*state == THERMAL_CSTATE_INVALID) {
715 dev_warn_ratelimited(&cdev->device,
716 "Failed to convert %dKHz for cpu %d into a cdev state\n",
717 target_freq, cpu);
718 return -EINVAL;
719 }
720
721 return 0;
722}
723
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530724/* Bind cpufreq callbacks to thermal cooling device ops */
Javi Merinoc36cf072015-02-26 19:00:29 +0000725static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530726 .get_max_state = cpufreq_get_max_state,
727 .get_cur_state = cpufreq_get_cur_state,
728 .set_cur_state = cpufreq_set_cur_state,
729};
730
731/* Notifier for cpufreq policy change */
732static struct notifier_block thermal_cpufreq_notifier_block = {
733 .notifier_call = cpufreq_thermal_notifier,
734};
735
Viresh Kumarf6859012014-12-04 09:42:06 +0530736static unsigned int find_next_max(struct cpufreq_frequency_table *table,
737 unsigned int prev_max)
738{
739 struct cpufreq_frequency_table *pos;
740 unsigned int max = 0;
741
742 cpufreq_for_each_valid_entry(pos, table) {
743 if (pos->frequency > max && pos->frequency < prev_max)
744 max = pos->frequency;
745 }
746
747 return max;
748}
749
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530750/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400751 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
752 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530753 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530754 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000755 * @capacitance: dynamic power coefficient for these cpus
756 * @plat_static_func: function to calculate the static power consumed by these
757 * cpus (optional)
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000758 *
759 * This interface function registers the cpufreq cooling device with the name
760 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400761 * cooling devices. It also gives the opportunity to link the cooling device
762 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000763 *
764 * Return: a valid struct thermal_cooling_device pointer on success,
765 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530766 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400767static struct thermal_cooling_device *
768__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +0000769 const struct cpumask *clip_cpus, u32 capacitance,
770 get_static_t plat_static_func)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530771{
772 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530773 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530774 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530775 struct cpufreq_frequency_table *pos, *table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000776 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530777 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530778
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530779 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
780 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530781 pr_debug("%s: CPUFreq table not found\n", __func__);
782 return ERR_PTR(-EPROBE_DEFER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530783 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530784
Viresh Kumar98d522f2014-12-04 09:41:50 +0530785 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530786 if (!cpufreq_dev)
787 return ERR_PTR(-ENOMEM);
788
Javi Merinoc36cf072015-02-26 19:00:29 +0000789 num_cpus = cpumask_weight(clip_cpus);
790 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
791 sizeof(*cpufreq_dev->time_in_idle),
792 GFP_KERNEL);
793 if (!cpufreq_dev->time_in_idle) {
794 cool_dev = ERR_PTR(-ENOMEM);
795 goto free_cdev;
796 }
797
798 cpufreq_dev->time_in_idle_timestamp =
799 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
800 GFP_KERNEL);
801 if (!cpufreq_dev->time_in_idle_timestamp) {
802 cool_dev = ERR_PTR(-ENOMEM);
803 goto free_time_in_idle;
804 }
805
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530806 /* Find max levels */
807 cpufreq_for_each_valid_entry(pos, table)
808 cpufreq_dev->max_level++;
809
Viresh Kumarf6859012014-12-04 09:42:06 +0530810 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
811 cpufreq_dev->max_level, GFP_KERNEL);
812 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +0530813 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000814 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +0530815 }
816
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530817 /* max_level is an index, not a counter */
818 cpufreq_dev->max_level--;
819
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530820 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
821
Javi Merinoc36cf072015-02-26 19:00:29 +0000822 if (capacitance) {
823 cpufreq_cooling_ops.get_requested_power =
824 cpufreq_get_requested_power;
825 cpufreq_cooling_ops.state2power = cpufreq_state2power;
826 cpufreq_cooling_ops.power2state = cpufreq_power2state;
827 cpufreq_dev->plat_get_static_power = plat_static_func;
828
829 ret = build_dyn_power_table(cpufreq_dev, capacitance);
830 if (ret) {
831 cool_dev = ERR_PTR(ret);
832 goto free_table;
833 }
834 }
835
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530836 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
837 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530838 cool_dev = ERR_PTR(ret);
Viresh Kumarf6859012014-12-04 09:42:06 +0530839 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530840 }
841
Eduardo Valentin99871a72013-04-17 17:12:17 +0000842 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
843 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530844
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400845 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
846 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530847 if (IS_ERR(cool_dev))
848 goto remove_idr;
849
Viresh Kumarf6859012014-12-04 09:42:06 +0530850 /* Fill freq-table in descending order of frequencies */
851 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
852 freq = find_next_max(table, freq);
853 cpufreq_dev->freq_table[i] = freq;
854
855 /* Warn for duplicate entries */
856 if (!freq)
857 pr_warn("%s: table has duplicate entries\n", __func__);
858 else
859 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530860 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530861
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530862 cpufreq_dev->cpufreq_val = cpufreq_dev->freq_table[0];
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530863 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530864
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530865 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530866
867 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar2479bb62014-12-04 09:42:04 +0530868 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530869 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000870 CPUFREQ_POLICY_NOTIFIER);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530871 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530872
873 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000874
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530875 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530876
877remove_idr:
878 release_idr(&cpufreq_idr, cpufreq_dev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530879free_table:
880 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +0000881free_time_in_idle_timestamp:
882 kfree(cpufreq_dev->time_in_idle_timestamp);
883free_time_in_idle:
884 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +0530885free_cdev:
886 kfree(cpufreq_dev);
887
888 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530889}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400890
891/**
892 * cpufreq_cooling_register - function to create cpufreq cooling device.
893 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
894 *
895 * This interface function registers the cpufreq cooling device with the name
896 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
897 * cooling devices.
898 *
899 * Return: a valid struct thermal_cooling_device pointer on success,
900 * on failure, it returns a corresponding ERR_PTR().
901 */
902struct thermal_cooling_device *
903cpufreq_cooling_register(const struct cpumask *clip_cpus)
904{
Javi Merinoc36cf072015-02-26 19:00:29 +0000905 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400906}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000907EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530908
909/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400910 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
911 * @np: a valid struct device_node to the cooling device device tree node
912 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
913 *
914 * This interface function registers the cpufreq cooling device with the name
915 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
916 * cooling devices. Using this API, the cpufreq cooling device will be
917 * linked to the device tree node provided.
918 *
919 * Return: a valid struct thermal_cooling_device pointer on success,
920 * on failure, it returns a corresponding ERR_PTR().
921 */
922struct thermal_cooling_device *
923of_cpufreq_cooling_register(struct device_node *np,
924 const struct cpumask *clip_cpus)
925{
926 if (!np)
927 return ERR_PTR(-EINVAL);
928
Javi Merinoc36cf072015-02-26 19:00:29 +0000929 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400930}
931EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
932
933/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000934 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
935 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
936 * @capacitance: dynamic power coefficient for these cpus
937 * @plat_static_func: function to calculate the static power consumed by these
938 * cpus (optional)
939 *
940 * This interface function registers the cpufreq cooling device with
941 * the name "thermal-cpufreq-%x". This api can support multiple
942 * instances of cpufreq cooling devices. Using this function, the
943 * cooling device will implement the power extensions by using a
944 * simple cpu power model. The cpus must have registered their OPPs
945 * using the OPP library.
946 *
947 * An optional @plat_static_func may be provided to calculate the
948 * static power consumed by these cpus. If the platform's static
949 * power consumption is unknown or negligible, make it NULL.
950 *
951 * Return: a valid struct thermal_cooling_device pointer on success,
952 * on failure, it returns a corresponding ERR_PTR().
953 */
954struct thermal_cooling_device *
955cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
956 get_static_t plat_static_func)
957{
958 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
959 plat_static_func);
960}
961EXPORT_SYMBOL(cpufreq_power_cooling_register);
962
963/**
964 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
965 * @np: a valid struct device_node to the cooling device device tree node
966 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
967 * @capacitance: dynamic power coefficient for these cpus
968 * @plat_static_func: function to calculate the static power consumed by these
969 * cpus (optional)
970 *
971 * This interface function registers the cpufreq cooling device with
972 * the name "thermal-cpufreq-%x". This api can support multiple
973 * instances of cpufreq cooling devices. Using this API, the cpufreq
974 * cooling device will be linked to the device tree node provided.
975 * Using this function, the cooling device will implement the power
976 * extensions by using a simple cpu power model. The cpus must have
977 * registered their OPPs using the OPP library.
978 *
979 * An optional @plat_static_func may be provided to calculate the
980 * static power consumed by these cpus. If the platform's static
981 * power consumption is unknown or negligible, make it NULL.
982 *
983 * Return: a valid struct thermal_cooling_device pointer on success,
984 * on failure, it returns a corresponding ERR_PTR().
985 */
986struct thermal_cooling_device *
987of_cpufreq_power_cooling_register(struct device_node *np,
988 const struct cpumask *clip_cpus,
989 u32 capacitance,
990 get_static_t plat_static_func)
991{
992 if (!np)
993 return ERR_PTR(-EINVAL);
994
995 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
996 plat_static_func);
997}
998EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
999
1000/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301001 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1002 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001003 *
1004 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301005 */
1006void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1007{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001008 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301009
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001010 if (!cdev)
1011 return;
1012
1013 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301014 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +05301015 list_del(&cpufreq_dev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301016
1017 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar2479bb62014-12-04 09:42:04 +05301018 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301019 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001020 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301021 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +01001022
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301023 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1024 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoc36cf072015-02-26 19:00:29 +00001025 kfree(cpufreq_dev->time_in_idle_timestamp);
1026 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301027 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301028 kfree(cpufreq_dev);
1029}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001030EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);