blob: 620dcd405ff6eec9ae65b0af8e93c25daae15d1f [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
Javi Merino6828a472015-03-02 17:17:20 +000034#include <trace/events/thermal.h>
35
Viresh Kumar07d888d2014-12-04 09:41:49 +053036/*
37 * Cooling state <-> CPUFreq frequency
38 *
39 * Cooling states are translated to frequencies throughout this driver and this
40 * is the relation between them.
41 *
42 * Highest cooling state corresponds to lowest possible frequency.
43 *
44 * i.e.
45 * level 0 --> 1st Max Freq
46 * level 1 --> 2nd Max Freq
47 * ...
48 */
49
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053050/**
Javi Merinoc36cf072015-02-26 19:00:29 +000051 * struct power_table - frequency to power conversion
52 * @frequency: frequency in KHz
53 * @power: power in mW
54 *
55 * This structure is built when the cooling device registers and helps
56 * in translating frequency to power and viceversa.
57 */
58struct power_table {
59 u32 frequency;
60 u32 power;
61};
62
63/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000064 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053065 * @id: unique integer value corresponding to each cpufreq_cooling_device
66 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000067 * @cool_dev: thermal_cooling_device pointer to keep track of the
68 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053069 * @cpufreq_state: integer value representing the current state of cpufreq
70 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053071 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053072 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053073 * @max_level: maximum cooling level. One less than total number of valid
74 * cpufreq frequencies.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053075 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Javi Merinofc4de352014-12-15 16:55:52 +000076 * @node: list_head to link all cpufreq_cooling_device together.
Javi Merinoc36cf072015-02-26 19:00:29 +000077 * @last_load: load measured by the latest call to cpufreq_get_actual_power()
78 * @time_in_idle: previous reading of the absolute time that this cpu was idle
79 * @time_in_idle_timestamp: wall time of the last invocation of
80 * get_cpu_idle_time_us()
81 * @dyn_power_table: array of struct power_table for frequency to power
82 * conversion, sorted in ascending order.
83 * @dyn_power_table_entries: number of entries in the @dyn_power_table array
84 * @cpu_dev: the first cpu_device from @allowed_cpus that has OPPs registered
85 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053086 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053087 * This structure is required for keeping information of each registered
88 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053089 */
90struct cpufreq_cooling_device {
91 int id;
92 struct thermal_cooling_device *cool_dev;
93 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +053094 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053095 unsigned int max_level;
Viresh Kumarf6859012014-12-04 09:42:06 +053096 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053097 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053098 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +000099 u32 last_load;
100 u64 *time_in_idle;
101 u64 *time_in_idle_timestamp;
102 struct power_table *dyn_power_table;
103 int dyn_power_table_entries;
104 struct device *cpu_dev;
105 get_static_t plat_get_static_power;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530106};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100108static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530109
Russell King02373d72015-08-12 15:22:16 +0530110static unsigned int cpufreq_dev_count;
111
112static DEFINE_MUTEX(cooling_list_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530113static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530114
115/**
116 * get_idr - function to get a unique id.
117 * @idr: struct idr * handle used to create a id.
118 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +0000119 *
120 * This function will populate @id with an unique
121 * id, using the idr API.
122 *
123 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530124 */
125static int get_idr(struct idr *idr, int *id)
126{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800127 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530128
129 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800130 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530131 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800132 if (unlikely(ret < 0))
133 return ret;
134 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000135
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530136 return 0;
137}
138
139/**
140 * release_idr - function to free the unique id.
141 * @idr: struct idr * handle used for creating the id.
142 * @id: int value representing the unique id.
143 */
144static void release_idr(struct idr *idr, int id)
145{
146 mutex_lock(&cooling_cpufreq_lock);
147 idr_remove(idr, id);
148 mutex_unlock(&cooling_cpufreq_lock);
149}
150
151/* Below code defines functions to be used for cpufreq as cooling device */
152
153/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530154 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530155 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530156 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000157 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530158 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530159 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530160static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
161 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530162{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530163 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000164
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530165 for (level = 0; level <= cpufreq_dev->max_level; level++) {
166 if (freq == cpufreq_dev->freq_table[level])
167 return level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530168
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530169 if (freq > cpufreq_dev->freq_table[level])
170 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800171 }
Zhang Ruia1167762014-01-02 11:57:48 +0800172
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530173 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800174}
175
Eduardo Valentin44952d32013-04-17 17:12:05 +0000176/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530177 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000178 * @cpu: cpu for which the level is required
179 * @freq: the frequency of interest
180 *
181 * This function will match the cooling level corresponding to the
182 * requested @freq and return it.
183 *
184 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
185 * otherwise.
186 */
Zhang Rui57df8102013-02-08 14:52:06 +0800187unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
188{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530189 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800190
Russell King02373d72015-08-12 15:22:16 +0530191 mutex_lock(&cooling_list_lock);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530192 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
193 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Russell King02373d72015-08-12 15:22:16 +0530194 mutex_unlock(&cooling_list_lock);
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530195 return get_level(cpufreq_dev, freq);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530196 }
197 }
Russell King02373d72015-08-12 15:22:16 +0530198 mutex_unlock(&cooling_list_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000199
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530200 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
201 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800202}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000203EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800204
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530205/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530206 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
207 * @nb: struct notifier_block * with callback info.
208 * @event: value showing cpufreq event for which this function invoked.
209 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000210 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100211 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000212 * Every time there is a change in policy, we will intercept and
213 * update the cpufreq policy with thermal constraints.
214 *
215 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530216 */
217static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000218 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530219{
220 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530221 unsigned long clipped_freq;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530222 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530223
Viresh Kumara24af232015-07-30 12:40:32 +0530224 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000225 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530226
227 mutex_lock(&cooling_list_lock);
228 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
229 if (!cpumask_test_cpu(policy->cpu, &cpufreq_dev->allowed_cpus))
230 continue;
231
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530232 /*
233 * policy->max is the maximum allowed frequency defined by user
234 * and clipped_freq is the maximum that thermal constraints
235 * allow.
236 *
237 * If clipped_freq is lower than policy->max, then we need to
238 * readjust policy->max.
239 *
240 * But, if clipped_freq is greater than policy->max, we don't
241 * need to do anything.
242 */
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530243 clipped_freq = cpufreq_dev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530244
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530245 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530246 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530247 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530248 }
Viresh Kumara24af232015-07-30 12:40:32 +0530249 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530250
Javi Merinoc36cf072015-02-26 19:00:29 +0000251 return NOTIFY_OK;
252}
253
254/**
255 * build_dyn_power_table() - create a dynamic power to frequency table
256 * @cpufreq_device: the cpufreq cooling device in which to store the table
257 * @capacitance: dynamic power coefficient for these cpus
258 *
259 * Build a dynamic power to frequency table for this cpu and store it
260 * in @cpufreq_device. This table will be used in cpu_power_to_freq() and
261 * cpu_freq_to_power() to convert between power and frequency
262 * efficiently. Power is stored in mW, frequency in KHz. The
263 * resulting table is in ascending order.
264 *
265 * Return: 0 on success, -E* on error.
266 */
267static int build_dyn_power_table(struct cpufreq_cooling_device *cpufreq_device,
268 u32 capacitance)
269{
270 struct power_table *power_table;
271 struct dev_pm_opp *opp;
272 struct device *dev = NULL;
273 int num_opps = 0, cpu, i, ret = 0;
274 unsigned long freq;
275
276 rcu_read_lock();
277
278 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
279 dev = get_cpu_device(cpu);
280 if (!dev) {
281 dev_warn(&cpufreq_device->cool_dev->device,
282 "No cpu device for cpu %d\n", cpu);
283 continue;
284 }
285
286 num_opps = dev_pm_opp_get_opp_count(dev);
287 if (num_opps > 0) {
288 break;
289 } else if (num_opps < 0) {
290 ret = num_opps;
291 goto unlock;
292 }
293 }
294
295 if (num_opps == 0) {
296 ret = -EINVAL;
297 goto unlock;
298 }
299
300 power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
Javi Merino0cdf97e2015-03-20 18:20:13 +0000301 if (!power_table) {
302 ret = -ENOMEM;
303 goto unlock;
304 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000305
306 for (freq = 0, i = 0;
307 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
308 freq++, i++) {
309 u32 freq_mhz, voltage_mv;
310 u64 power;
311
312 freq_mhz = freq / 1000000;
313 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
314
315 /*
316 * Do the multiplication with MHz and millivolt so as
317 * to not overflow.
318 */
319 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
320 do_div(power, 1000000000);
321
322 /* frequency is stored in power_table in KHz */
323 power_table[i].frequency = freq / 1000;
324
325 /* power is stored in mW */
326 power_table[i].power = power;
327 }
328
329 if (i == 0) {
330 ret = PTR_ERR(opp);
331 goto unlock;
332 }
333
334 cpufreq_device->cpu_dev = dev;
335 cpufreq_device->dyn_power_table = power_table;
336 cpufreq_device->dyn_power_table_entries = i;
337
338unlock:
339 rcu_read_unlock();
340 return ret;
341}
342
343static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_device,
344 u32 freq)
345{
346 int i;
347 struct power_table *pt = cpufreq_device->dyn_power_table;
348
349 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
350 if (freq < pt[i].frequency)
351 break;
352
353 return pt[i - 1].power;
354}
355
356static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_device,
357 u32 power)
358{
359 int i;
360 struct power_table *pt = cpufreq_device->dyn_power_table;
361
362 for (i = 1; i < cpufreq_device->dyn_power_table_entries; i++)
363 if (power < pt[i].power)
364 break;
365
366 return pt[i - 1].frequency;
367}
368
369/**
370 * get_load() - get load for a cpu since last updated
371 * @cpufreq_device: &struct cpufreq_cooling_device for this cpu
372 * @cpu: cpu number
373 *
374 * Return: The average load of cpu @cpu in percentage since this
375 * function was last called.
376 */
377static u32 get_load(struct cpufreq_cooling_device *cpufreq_device, int cpu)
378{
379 u32 load;
380 u64 now, now_idle, delta_time, delta_idle;
381
382 now_idle = get_cpu_idle_time(cpu, &now, 0);
383 delta_idle = now_idle - cpufreq_device->time_in_idle[cpu];
384 delta_time = now - cpufreq_device->time_in_idle_timestamp[cpu];
385
386 if (delta_time <= delta_idle)
387 load = 0;
388 else
389 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
390
391 cpufreq_device->time_in_idle[cpu] = now_idle;
392 cpufreq_device->time_in_idle_timestamp[cpu] = now;
393
394 return load;
395}
396
397/**
398 * get_static_power() - calculate the static power consumed by the cpus
399 * @cpufreq_device: struct &cpufreq_cooling_device for this cpu cdev
400 * @tz: thermal zone device in which we're operating
401 * @freq: frequency in KHz
402 * @power: pointer in which to store the calculated static power
403 *
404 * Calculate the static power consumed by the cpus described by
405 * @cpu_actor running at frequency @freq. This function relies on a
406 * platform specific function that should have been provided when the
407 * actor was registered. If it wasn't, the static power is assumed to
408 * be negligible. The calculated static power is stored in @power.
409 *
410 * Return: 0 on success, -E* on failure.
411 */
412static int get_static_power(struct cpufreq_cooling_device *cpufreq_device,
413 struct thermal_zone_device *tz, unsigned long freq,
414 u32 *power)
415{
416 struct dev_pm_opp *opp;
417 unsigned long voltage;
418 struct cpumask *cpumask = &cpufreq_device->allowed_cpus;
419 unsigned long freq_hz = freq * 1000;
420
421 if (!cpufreq_device->plat_get_static_power ||
422 !cpufreq_device->cpu_dev) {
423 *power = 0;
424 return 0;
425 }
426
427 rcu_read_lock();
428
429 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz,
430 true);
431 voltage = dev_pm_opp_get_voltage(opp);
432
433 rcu_read_unlock();
434
435 if (voltage == 0) {
436 dev_warn_ratelimited(cpufreq_device->cpu_dev,
437 "Failed to get voltage for frequency %lu: %ld\n",
438 freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
439 return -EINVAL;
440 }
441
442 return cpufreq_device->plat_get_static_power(cpumask, tz->passive_delay,
443 voltage, power);
444}
445
446/**
447 * get_dynamic_power() - calculate the dynamic power
448 * @cpufreq_device: &cpufreq_cooling_device for this cdev
449 * @freq: current frequency
450 *
451 * Return: the dynamic power consumed by the cpus described by
452 * @cpufreq_device.
453 */
454static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_device,
455 unsigned long freq)
456{
457 u32 raw_cpu_power;
458
459 raw_cpu_power = cpu_freq_to_power(cpufreq_device, freq);
460 return (raw_cpu_power * cpufreq_device->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530461}
462
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000463/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530464
465/**
466 * cpufreq_get_max_state - callback function to get the max cooling state.
467 * @cdev: thermal cooling device pointer.
468 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000469 *
470 * Callback for the thermal cooling device to return the cpufreq
471 * max cooling state.
472 *
473 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530474 */
475static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
476 unsigned long *state)
477{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100478 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530479
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530480 *state = cpufreq_device->max_level;
481 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530482}
483
484/**
485 * cpufreq_get_cur_state - callback function to get the current cooling state.
486 * @cdev: thermal cooling device pointer.
487 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000488 *
489 * Callback for the thermal cooling device to return the cpufreq
490 * current cooling state.
491 *
492 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530493 */
494static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
495 unsigned long *state)
496{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100497 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530498
hongbo.zhang160b7d82012-10-30 17:48:59 +0100499 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000500
hongbo.zhang160b7d82012-10-30 17:48:59 +0100501 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530502}
503
504/**
505 * cpufreq_set_cur_state - callback function to set the current cooling state.
506 * @cdev: thermal cooling device pointer.
507 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000508 *
509 * Callback for the thermal cooling device to change the cpufreq
510 * current cooling state.
511 *
512 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530513 */
514static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
515 unsigned long state)
516{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100517 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530518 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
519 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530520
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530521 /* Request state should be less than max_level */
522 if (WARN_ON(state > cpufreq_device->max_level))
523 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530524
Viresh Kumar5194fe42014-12-04 09:42:00 +0530525 /* Check if the old cooling action is same as new cooling action */
526 if (cpufreq_device->cpufreq_state == state)
527 return 0;
528
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530529 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar5194fe42014-12-04 09:42:00 +0530530 cpufreq_device->cpufreq_state = state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530531 cpufreq_device->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530532
533 cpufreq_update_policy(cpu);
534
535 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530536}
537
Javi Merinoc36cf072015-02-26 19:00:29 +0000538/**
539 * cpufreq_get_requested_power() - get the current power
540 * @cdev: &thermal_cooling_device pointer
541 * @tz: a valid thermal zone device pointer
542 * @power: pointer in which to store the resulting power
543 *
544 * Calculate the current power consumption of the cpus in milliwatts
545 * and store it in @power. This function should actually calculate
546 * the requested power, but it's hard to get the frequency that
547 * cpufreq would have assigned if there were no thermal limits.
548 * Instead, we calculate the current power on the assumption that the
549 * immediate future will look like the immediate past.
550 *
551 * We use the current frequency and the average load since this
552 * function was last called. In reality, there could have been
553 * multiple opps since this function was last called and that affects
554 * the load calculation. While it's not perfectly accurate, this
555 * simplification is good enough and works. REVISIT this, as more
556 * complex code may be needed if experiments show that it's not
557 * accurate enough.
558 *
559 * Return: 0 on success, -E* if getting the static power failed.
560 */
561static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
562 struct thermal_zone_device *tz,
563 u32 *power)
564{
565 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000566 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000567 u32 static_power, dynamic_power, total_load = 0;
568 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Javi Merino6828a472015-03-02 17:17:20 +0000569 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000570
Kapileshwar Singhdd658e02015-03-16 12:00:51 +0000571 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
572
573 /*
574 * All the CPUs are offline, thus the requested power by
575 * the cdev is 0
576 */
577 if (cpu >= nr_cpu_ids) {
578 *power = 0;
579 return 0;
580 }
581
582 freq = cpufreq_quick_get(cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000583
Javi Merino6828a472015-03-02 17:17:20 +0000584 if (trace_thermal_power_cpu_get_power_enabled()) {
585 u32 ncpus = cpumask_weight(&cpufreq_device->allowed_cpus);
586
587 load_cpu = devm_kcalloc(&cdev->device, ncpus, sizeof(*load_cpu),
588 GFP_KERNEL);
589 }
590
Javi Merinoc36cf072015-02-26 19:00:29 +0000591 for_each_cpu(cpu, &cpufreq_device->allowed_cpus) {
592 u32 load;
593
594 if (cpu_online(cpu))
595 load = get_load(cpufreq_device, cpu);
596 else
597 load = 0;
598
599 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000600 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
601 load_cpu[i] = load;
602
603 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000604 }
605
606 cpufreq_device->last_load = total_load;
607
608 dynamic_power = get_dynamic_power(cpufreq_device, freq);
609 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000610 if (ret) {
611 if (load_cpu)
612 devm_kfree(&cdev->device, load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000613 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000614 }
615
616 if (load_cpu) {
617 trace_thermal_power_cpu_get_power(
618 &cpufreq_device->allowed_cpus,
619 freq, load_cpu, i, dynamic_power, static_power);
620
621 devm_kfree(&cdev->device, load_cpu);
622 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000623
624 *power = static_power + dynamic_power;
625 return 0;
626}
627
628/**
629 * cpufreq_state2power() - convert a cpu cdev state to power consumed
630 * @cdev: &thermal_cooling_device pointer
631 * @tz: a valid thermal zone device pointer
632 * @state: cooling device state to be converted
633 * @power: pointer in which to store the resulting power
634 *
635 * Convert cooling device state @state into power consumption in
636 * milliwatts assuming 100% load. Store the calculated power in
637 * @power.
638 *
639 * Return: 0 on success, -EINVAL if the cooling device state could not
640 * be converted into a frequency or other -E* if there was an error
641 * when calculating the static power.
642 */
643static int cpufreq_state2power(struct thermal_cooling_device *cdev,
644 struct thermal_zone_device *tz,
645 unsigned long state, u32 *power)
646{
647 unsigned int freq, num_cpus;
648 cpumask_t cpumask;
649 u32 static_power, dynamic_power;
650 int ret;
651 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
652
653 cpumask_and(&cpumask, &cpufreq_device->allowed_cpus, cpu_online_mask);
654 num_cpus = cpumask_weight(&cpumask);
655
656 /* None of our cpus are online, so no power */
657 if (num_cpus == 0) {
658 *power = 0;
659 return 0;
660 }
661
662 freq = cpufreq_device->freq_table[state];
663 if (!freq)
664 return -EINVAL;
665
666 dynamic_power = cpu_freq_to_power(cpufreq_device, freq) * num_cpus;
667 ret = get_static_power(cpufreq_device, tz, freq, &static_power);
668 if (ret)
669 return ret;
670
671 *power = static_power + dynamic_power;
672 return 0;
673}
674
675/**
676 * cpufreq_power2state() - convert power to a cooling device state
677 * @cdev: &thermal_cooling_device pointer
678 * @tz: a valid thermal zone device pointer
679 * @power: power in milliwatts to be converted
680 * @state: pointer in which to store the resulting state
681 *
682 * Calculate a cooling device state for the cpus described by @cdev
683 * that would allow them to consume at most @power mW and store it in
684 * @state. Note that this calculation depends on external factors
685 * such as the cpu load or the current static power. Calling this
686 * function with the same power as input can yield different cooling
687 * device states depending on those external factors.
688 *
689 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
690 * the calculated frequency could not be converted to a valid state.
691 * The latter should not happen unless the frequencies available to
692 * cpufreq have changed since the initialization of the cpu cooling
693 * device.
694 */
695static int cpufreq_power2state(struct thermal_cooling_device *cdev,
696 struct thermal_zone_device *tz, u32 power,
697 unsigned long *state)
698{
699 unsigned int cpu, cur_freq, target_freq;
700 int ret;
701 s32 dyn_power;
702 u32 last_load, normalised_power, static_power;
703 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
704
705 cpu = cpumask_any_and(&cpufreq_device->allowed_cpus, cpu_online_mask);
706
707 /* None of our cpus are online */
708 if (cpu >= nr_cpu_ids)
709 return -ENODEV;
710
711 cur_freq = cpufreq_quick_get(cpu);
712 ret = get_static_power(cpufreq_device, tz, cur_freq, &static_power);
713 if (ret)
714 return ret;
715
716 dyn_power = power - static_power;
717 dyn_power = dyn_power > 0 ? dyn_power : 0;
718 last_load = cpufreq_device->last_load ?: 1;
719 normalised_power = (dyn_power * 100) / last_load;
720 target_freq = cpu_power_to_freq(cpufreq_device, normalised_power);
721
722 *state = cpufreq_cooling_get_level(cpu, target_freq);
723 if (*state == THERMAL_CSTATE_INVALID) {
724 dev_warn_ratelimited(&cdev->device,
725 "Failed to convert %dKHz for cpu %d into a cdev state\n",
726 target_freq, cpu);
727 return -EINVAL;
728 }
729
Javi Merino6828a472015-03-02 17:17:20 +0000730 trace_thermal_power_cpu_limit(&cpufreq_device->allowed_cpus,
731 target_freq, *state, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000732 return 0;
733}
734
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530735/* Bind cpufreq callbacks to thermal cooling device ops */
Javi Merinoc36cf072015-02-26 19:00:29 +0000736static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530737 .get_max_state = cpufreq_get_max_state,
738 .get_cur_state = cpufreq_get_cur_state,
739 .set_cur_state = cpufreq_set_cur_state,
740};
741
742/* Notifier for cpufreq policy change */
743static struct notifier_block thermal_cpufreq_notifier_block = {
744 .notifier_call = cpufreq_thermal_notifier,
745};
746
Viresh Kumarf6859012014-12-04 09:42:06 +0530747static unsigned int find_next_max(struct cpufreq_frequency_table *table,
748 unsigned int prev_max)
749{
750 struct cpufreq_frequency_table *pos;
751 unsigned int max = 0;
752
753 cpufreq_for_each_valid_entry(pos, table) {
754 if (pos->frequency > max && pos->frequency < prev_max)
755 max = pos->frequency;
756 }
757
758 return max;
759}
760
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530761/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400762 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
763 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530764 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530765 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000766 * @capacitance: dynamic power coefficient for these cpus
767 * @plat_static_func: function to calculate the static power consumed by these
768 * cpus (optional)
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000769 *
770 * This interface function registers the cpufreq cooling device with the name
771 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400772 * cooling devices. It also gives the opportunity to link the cooling device
773 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000774 *
775 * Return: a valid struct thermal_cooling_device pointer on success,
776 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530777 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400778static struct thermal_cooling_device *
779__cpufreq_cooling_register(struct device_node *np,
Javi Merinoc36cf072015-02-26 19:00:29 +0000780 const struct cpumask *clip_cpus, u32 capacitance,
781 get_static_t plat_static_func)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530782{
783 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530784 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530785 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530786 struct cpufreq_frequency_table *pos, *table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000787 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530788 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530789
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530790 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
791 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530792 pr_debug("%s: CPUFreq table not found\n", __func__);
793 return ERR_PTR(-EPROBE_DEFER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530794 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530795
Viresh Kumar98d522f2014-12-04 09:41:50 +0530796 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530797 if (!cpufreq_dev)
798 return ERR_PTR(-ENOMEM);
799
Javi Merinoc36cf072015-02-26 19:00:29 +0000800 num_cpus = cpumask_weight(clip_cpus);
801 cpufreq_dev->time_in_idle = kcalloc(num_cpus,
802 sizeof(*cpufreq_dev->time_in_idle),
803 GFP_KERNEL);
804 if (!cpufreq_dev->time_in_idle) {
805 cool_dev = ERR_PTR(-ENOMEM);
806 goto free_cdev;
807 }
808
809 cpufreq_dev->time_in_idle_timestamp =
810 kcalloc(num_cpus, sizeof(*cpufreq_dev->time_in_idle_timestamp),
811 GFP_KERNEL);
812 if (!cpufreq_dev->time_in_idle_timestamp) {
813 cool_dev = ERR_PTR(-ENOMEM);
814 goto free_time_in_idle;
815 }
816
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530817 /* Find max levels */
818 cpufreq_for_each_valid_entry(pos, table)
819 cpufreq_dev->max_level++;
820
Viresh Kumarf6859012014-12-04 09:42:06 +0530821 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
822 cpufreq_dev->max_level, GFP_KERNEL);
823 if (!cpufreq_dev->freq_table) {
Viresh Kumarf6859012014-12-04 09:42:06 +0530824 cool_dev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000825 goto free_time_in_idle_timestamp;
Viresh Kumarf6859012014-12-04 09:42:06 +0530826 }
827
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530828 /* max_level is an index, not a counter */
829 cpufreq_dev->max_level--;
830
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530831 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
832
Javi Merinoc36cf072015-02-26 19:00:29 +0000833 if (capacitance) {
834 cpufreq_cooling_ops.get_requested_power =
835 cpufreq_get_requested_power;
836 cpufreq_cooling_ops.state2power = cpufreq_state2power;
837 cpufreq_cooling_ops.power2state = cpufreq_power2state;
838 cpufreq_dev->plat_get_static_power = plat_static_func;
839
840 ret = build_dyn_power_table(cpufreq_dev, capacitance);
841 if (ret) {
842 cool_dev = ERR_PTR(ret);
843 goto free_table;
844 }
845 }
846
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530847 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
848 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530849 cool_dev = ERR_PTR(ret);
Viresh Kumarf6859012014-12-04 09:42:06 +0530850 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530851 }
852
Eduardo Valentin99871a72013-04-17 17:12:17 +0000853 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
854 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530855
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400856 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
857 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530858 if (IS_ERR(cool_dev))
859 goto remove_idr;
860
Viresh Kumarf6859012014-12-04 09:42:06 +0530861 /* Fill freq-table in descending order of frequencies */
862 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
863 freq = find_next_max(table, freq);
864 cpufreq_dev->freq_table[i] = freq;
865
866 /* Warn for duplicate entries */
867 if (!freq)
868 pr_warn("%s: table has duplicate entries\n", __func__);
869 else
870 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530871 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530872
Viresh Kumar59f0d212015-07-30 12:40:33 +0530873 cpufreq_dev->clipped_freq = cpufreq_dev->freq_table[0];
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530874 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530875
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530876 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530877
Russell King02373d72015-08-12 15:22:16 +0530878 mutex_lock(&cooling_list_lock);
879 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
880 mutex_unlock(&cooling_list_lock);
881
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530882 /* Register the notifier for first cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +0530883 if (!cpufreq_dev_count++)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530884 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000885 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530886 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000887
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530888 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530889
890remove_idr:
891 release_idr(&cpufreq_idr, cpufreq_dev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530892free_table:
893 kfree(cpufreq_dev->freq_table);
Javi Merinoc36cf072015-02-26 19:00:29 +0000894free_time_in_idle_timestamp:
895 kfree(cpufreq_dev->time_in_idle_timestamp);
896free_time_in_idle:
897 kfree(cpufreq_dev->time_in_idle);
Viresh Kumar730abe02014-12-04 09:41:58 +0530898free_cdev:
899 kfree(cpufreq_dev);
900
901 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530902}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400903
904/**
905 * cpufreq_cooling_register - function to create cpufreq cooling device.
906 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
907 *
908 * This interface function registers the cpufreq cooling device with the name
909 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
910 * cooling devices.
911 *
912 * Return: a valid struct thermal_cooling_device pointer on success,
913 * on failure, it returns a corresponding ERR_PTR().
914 */
915struct thermal_cooling_device *
916cpufreq_cooling_register(const struct cpumask *clip_cpus)
917{
Javi Merinoc36cf072015-02-26 19:00:29 +0000918 return __cpufreq_cooling_register(NULL, clip_cpus, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400919}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000920EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530921
922/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400923 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
924 * @np: a valid struct device_node to the cooling device device tree node
925 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
926 *
927 * This interface function registers the cpufreq cooling device with the name
928 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
929 * cooling devices. Using this API, the cpufreq cooling device will be
930 * linked to the device tree node provided.
931 *
932 * Return: a valid struct thermal_cooling_device pointer on success,
933 * on failure, it returns a corresponding ERR_PTR().
934 */
935struct thermal_cooling_device *
936of_cpufreq_cooling_register(struct device_node *np,
937 const struct cpumask *clip_cpus)
938{
939 if (!np)
940 return ERR_PTR(-EINVAL);
941
Javi Merinoc36cf072015-02-26 19:00:29 +0000942 return __cpufreq_cooling_register(np, clip_cpus, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400943}
944EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
945
946/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000947 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
948 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
949 * @capacitance: dynamic power coefficient for these cpus
950 * @plat_static_func: function to calculate the static power consumed by these
951 * cpus (optional)
952 *
953 * This interface function registers the cpufreq cooling device with
954 * the name "thermal-cpufreq-%x". This api can support multiple
955 * instances of cpufreq cooling devices. Using this function, the
956 * cooling device will implement the power extensions by using a
957 * simple cpu power model. The cpus must have registered their OPPs
958 * using the OPP library.
959 *
960 * An optional @plat_static_func may be provided to calculate the
961 * static power consumed by these cpus. If the platform's static
962 * power consumption is unknown or negligible, make it NULL.
963 *
964 * Return: a valid struct thermal_cooling_device pointer on success,
965 * on failure, it returns a corresponding ERR_PTR().
966 */
967struct thermal_cooling_device *
968cpufreq_power_cooling_register(const struct cpumask *clip_cpus, u32 capacitance,
969 get_static_t plat_static_func)
970{
971 return __cpufreq_cooling_register(NULL, clip_cpus, capacitance,
972 plat_static_func);
973}
974EXPORT_SYMBOL(cpufreq_power_cooling_register);
975
976/**
977 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
978 * @np: a valid struct device_node to the cooling device device tree node
979 * @clip_cpus: cpumask of cpus where the frequency constraints will happen
980 * @capacitance: dynamic power coefficient for these cpus
981 * @plat_static_func: function to calculate the static power consumed by these
982 * cpus (optional)
983 *
984 * This interface function registers the cpufreq cooling device with
985 * the name "thermal-cpufreq-%x". This api can support multiple
986 * instances of cpufreq cooling devices. Using this API, the cpufreq
987 * cooling device will be linked to the device tree node provided.
988 * Using this function, the cooling device will implement the power
989 * extensions by using a simple cpu power model. The cpus must have
990 * registered their OPPs using the OPP library.
991 *
992 * An optional @plat_static_func may be provided to calculate the
993 * static power consumed by these cpus. If the platform's static
994 * power consumption is unknown or negligible, make it NULL.
995 *
996 * Return: a valid struct thermal_cooling_device pointer on success,
997 * on failure, it returns a corresponding ERR_PTR().
998 */
999struct thermal_cooling_device *
1000of_cpufreq_power_cooling_register(struct device_node *np,
1001 const struct cpumask *clip_cpus,
1002 u32 capacitance,
1003 get_static_t plat_static_func)
1004{
1005 if (!np)
1006 return ERR_PTR(-EINVAL);
1007
1008 return __cpufreq_cooling_register(np, clip_cpus, capacitance,
1009 plat_static_func);
1010}
1011EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
1012
1013/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301014 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
1015 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +00001016 *
1017 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301018 */
1019void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
1020{
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001021 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301022
Eduardo Valentin50e66c72013-08-15 10:54:46 -04001023 if (!cdev)
1024 return;
1025
1026 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301027
1028 /* Unregister the notifier for the last cpufreq cooling device */
Russell King02373d72015-08-12 15:22:16 +05301029 mutex_lock(&cooling_cpufreq_lock);
1030 if (!--cpufreq_dev_count)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301031 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +00001032 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +05301033
1034 mutex_lock(&cooling_list_lock);
1035 list_del(&cpufreq_dev->node);
1036 mutex_unlock(&cooling_list_lock);
1037
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301038 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +01001039
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301040 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
1041 release_idr(&cpufreq_idr, cpufreq_dev->id);
Javi Merinoc36cf072015-02-26 19:00:29 +00001042 kfree(cpufreq_dev->time_in_idle_timestamp);
1043 kfree(cpufreq_dev->time_in_idle);
Viresh Kumarf6859012014-12-04 09:42:06 +05301044 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05301045 kfree(cpufreq_dev);
1046}
Eduardo Valentin243dbd92013-04-17 17:11:57 +00001047EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);