blob: dfd23245f778a3193903d582e015bd27a7f50ad3 [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>
Matthew Wilcoxae606082016-12-21 09:47:05 -080029#include <linux/idr.h>
Javi Merinoc36cf072015-02-26 19:00:29 +000030#include <linux/pm_opp.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053031#include <linux/slab.h>
32#include <linux/cpu.h>
33#include <linux/cpu_cooling.h>
34
Javi Merino6828a472015-03-02 17:17:20 +000035#include <trace/events/thermal.h>
36
Viresh Kumar07d888d2014-12-04 09:41:49 +053037/*
38 * Cooling state <-> CPUFreq frequency
39 *
40 * Cooling states are translated to frequencies throughout this driver and this
41 * is the relation between them.
42 *
43 * Highest cooling state corresponds to lowest possible frequency.
44 *
45 * i.e.
46 * level 0 --> 1st Max Freq
47 * level 1 --> 2nd Max Freq
48 * ...
49 */
50
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053051/**
Viresh Kumar349d39d2017-04-25 15:57:19 +053052 * struct freq_table - frequency table along with power entries
Javi Merinoc36cf072015-02-26 19:00:29 +000053 * @frequency: frequency in KHz
54 * @power: power in mW
55 *
56 * This structure is built when the cooling device registers and helps
Viresh Kumar349d39d2017-04-25 15:57:19 +053057 * in translating frequency to power and vice versa.
Javi Merinoc36cf072015-02-26 19:00:29 +000058 */
Viresh Kumar349d39d2017-04-25 15:57:19 +053059struct freq_table {
Javi Merinoc36cf072015-02-26 19:00:29 +000060 u32 frequency;
61 u32 power;
62};
63
64/**
Viresh Kumar81ee14d2017-04-25 15:57:20 +053065 * struct time_in_idle - Idle time stats
66 * @time: previous reading of the absolute time that this cpu was idle
67 * @timestamp: wall time of the last invocation of get_cpu_idle_time_us()
68 */
69struct time_in_idle {
70 u64 time;
71 u64 timestamp;
72};
73
74/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000075 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053076 * @id: unique integer value corresponding to each cpufreq_cooling_device
77 * registered.
Viresh Kumard72b4012017-04-25 15:57:24 +053078 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053079 * @cpufreq_state: integer value representing the current state of cpufreq
80 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053081 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053082 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053083 * @max_level: maximum cooling level. One less than total number of valid
84 * cpufreq frequencies.
Viresh Kumard72b4012017-04-25 15:57:24 +053085 * @freq_table: Freq table in descending order of frequencies
86 * @cdev: thermal_cooling_device pointer to keep track of the
87 * registered cooling device.
88 * @policy: cpufreq policy.
Javi Merinofc4de352014-12-15 16:55:52 +000089 * @node: list_head to link all cpufreq_cooling_device together.
Viresh Kumar81ee14d2017-04-25 15:57:20 +053090 * @idle_time: idle time stats
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053091 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053092 * This structure is required for keeping information of each registered
93 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053094 */
95struct cpufreq_cooling_device {
96 int id;
Viresh Kumard72b4012017-04-25 15:57:24 +053097 u32 last_load;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053098 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +053099 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530100 unsigned int max_level;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530101 struct freq_table *freq_table; /* In descending order */
Viresh Kumard72b4012017-04-25 15:57:24 +0530102 struct thermal_cooling_device *cdev;
103 struct cpufreq_policy *policy;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530104 struct list_head node;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530105 struct time_in_idle *idle_time;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530106};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107
Viresh Kumarfb8ea302017-04-25 15:57:09 +0530108static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +0530109static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530110static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530111
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530112/* Below code defines functions to be used for cpufreq as cooling device */
113
114/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530115 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +0530116 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530117 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000118 *
Viresh Kumarda27f692017-04-25 15:57:21 +0530119 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530120 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530121static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530122 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530123{
Viresh Kumarda27f692017-04-25 15:57:21 +0530124 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530125 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000126
Viresh Kumarda27f692017-04-25 15:57:21 +0530127 for (level = 1; level <= cpufreq_cdev->max_level; level++)
128 if (freq > freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530129 break;
Zhang Ruia1167762014-01-02 11:57:48 +0800130
Viresh Kumarda27f692017-04-25 15:57:21 +0530131 return level - 1;
Zhang Ruifc35b352013-02-08 13:09:32 +0800132}
133
Eduardo Valentin44952d32013-04-17 17:12:05 +0000134/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530135 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
136 * @nb: struct notifier_block * with callback info.
137 * @event: value showing cpufreq event for which this function invoked.
138 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000139 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100140 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000141 * Every time there is a change in policy, we will intercept and
142 * update the cpufreq policy with thermal constraints.
143 *
144 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530145 */
146static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000147 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530148{
149 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530150 unsigned long clipped_freq;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530151 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530152
Viresh Kumara24af232015-07-30 12:40:32 +0530153 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000154 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530155
156 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530157 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530158 /*
159 * A new copy of the policy is sent to the notifier and can't
160 * compare that directly.
161 */
162 if (policy->cpu != cpufreq_cdev->policy->cpu)
Viresh Kumara24af232015-07-30 12:40:32 +0530163 continue;
164
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530165 /*
166 * policy->max is the maximum allowed frequency defined by user
167 * and clipped_freq is the maximum that thermal constraints
168 * allow.
169 *
170 * If clipped_freq is lower than policy->max, then we need to
171 * readjust policy->max.
172 *
173 * But, if clipped_freq is greater than policy->max, we don't
174 * need to do anything.
175 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530176 clipped_freq = cpufreq_cdev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530177
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530178 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530179 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530180 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530181 }
Viresh Kumara24af232015-07-30 12:40:32 +0530182 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530183
Javi Merinoc36cf072015-02-26 19:00:29 +0000184 return NOTIFY_OK;
185}
186
187/**
Viresh Kumar349d39d2017-04-25 15:57:19 +0530188 * update_freq_table() - Update the freq table with power numbers
189 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
Javi Merinoc36cf072015-02-26 19:00:29 +0000190 * @capacitance: dynamic power coefficient for these cpus
191 *
Viresh Kumar349d39d2017-04-25 15:57:19 +0530192 * Update the freq table with power numbers. This table will be used in
193 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
194 * frequency efficiently. Power is stored in mW, frequency in KHz. The
195 * resulting table is in descending order.
Javi Merinoc36cf072015-02-26 19:00:29 +0000196 *
Javi Merino459ac372015-08-17 19:21:42 +0100197 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
Viresh Kumar349d39d2017-04-25 15:57:19 +0530198 * or -ENOMEM if we run out of memory.
Javi Merinoc36cf072015-02-26 19:00:29 +0000199 */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530200static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
201 u32 capacitance)
Javi Merinoc36cf072015-02-26 19:00:29 +0000202{
Viresh Kumar349d39d2017-04-25 15:57:19 +0530203 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000204 struct dev_pm_opp *opp;
205 struct device *dev = NULL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530206 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000207
Viresh Kumar02bacb22017-04-25 15:57:17 +0530208 dev = get_cpu_device(cpu);
209 if (unlikely(!dev)) {
210 dev_warn(&cpufreq_cdev->cdev->device,
211 "No cpu device for cpu %d\n", cpu);
212 return -ENODEV;
Javi Merinoc36cf072015-02-26 19:00:29 +0000213 }
214
Viresh Kumar02bacb22017-04-25 15:57:17 +0530215 num_opps = dev_pm_opp_get_opp_count(dev);
216 if (num_opps < 0)
217 return num_opps;
218
Viresh Kumar349d39d2017-04-25 15:57:19 +0530219 /*
220 * The cpufreq table is also built from the OPP table and so the count
221 * should match.
222 */
223 if (num_opps != cpufreq_cdev->max_level + 1) {
224 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
Javi Merino459ac372015-08-17 19:21:42 +0100225 return -EINVAL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530226 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000227
Viresh Kumar349d39d2017-04-25 15:57:19 +0530228 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
229 unsigned long freq = freq_table[i].frequency * 1000;
230 u32 freq_mhz = freq_table[i].frequency / 1000;
Javi Merinoc36cf072015-02-26 19:00:29 +0000231 u64 power;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530232 u32 voltage_mv;
Javi Merinoc36cf072015-02-26 19:00:29 +0000233
Viresh Kumar349d39d2017-04-25 15:57:19 +0530234 /*
235 * Find ceil frequency as 'freq' may be slightly lower than OPP
236 * freq due to truncation while converting to kHz.
237 */
238 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
239 if (IS_ERR(opp)) {
240 dev_err(dev, "failed to get opp for %lu frequency\n",
241 freq);
242 return -EINVAL;
Javi Merino459ac372015-08-17 19:21:42 +0100243 }
244
Javi Merinoc36cf072015-02-26 19:00:29 +0000245 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530246 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000247
248 /*
249 * Do the multiplication with MHz and millivolt so as
250 * to not overflow.
251 */
252 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
253 do_div(power, 1000000000);
254
Javi Merinoc36cf072015-02-26 19:00:29 +0000255 /* power is stored in mW */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530256 freq_table[i].power = power;
Javi Merinoeba4f882015-08-17 19:21:43 +0100257 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000258
Javi Merino459ac372015-08-17 19:21:42 +0100259 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000260}
261
Viresh Kumar1dea4322017-04-25 15:57:10 +0530262static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000263 u32 freq)
264{
265 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530266 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000267
Viresh Kumar349d39d2017-04-25 15:57:19 +0530268 for (i = 1; i <= cpufreq_cdev->max_level; i++)
269 if (freq > freq_table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000270 break;
271
Viresh Kumar349d39d2017-04-25 15:57:19 +0530272 return freq_table[i - 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000273}
274
Viresh Kumar1dea4322017-04-25 15:57:10 +0530275static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000276 u32 power)
277{
278 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530279 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000280
Viresh Kumar349d39d2017-04-25 15:57:19 +0530281 for (i = 1; i <= cpufreq_cdev->max_level; i++)
282 if (power > freq_table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000283 break;
284
Viresh Kumar349d39d2017-04-25 15:57:19 +0530285 return freq_table[i - 1].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000286}
287
288/**
289 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530290 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000291 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530292 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000293 *
294 * Return: The average load of cpu @cpu in percentage since this
295 * function was last called.
296 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530297static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000298 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000299{
300 u32 load;
301 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530302 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000303
304 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530305 delta_idle = now_idle - idle_time->time;
306 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000307
308 if (delta_time <= delta_idle)
309 load = 0;
310 else
311 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
312
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530313 idle_time->time = now_idle;
314 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000315
316 return load;
317}
318
319/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000320 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530321 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000322 * @freq: current frequency
323 *
324 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530325 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000326 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530327static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000328 unsigned long freq)
329{
330 u32 raw_cpu_power;
331
Viresh Kumar1dea4322017-04-25 15:57:10 +0530332 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
333 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530334}
335
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000336/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530337
338/**
339 * cpufreq_get_max_state - callback function to get the max cooling state.
340 * @cdev: thermal cooling device pointer.
341 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000342 *
343 * Callback for the thermal cooling device to return the cpufreq
344 * max cooling state.
345 *
346 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530347 */
348static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
349 unsigned long *state)
350{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530351 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530352
Viresh Kumar1dea4322017-04-25 15:57:10 +0530353 *state = cpufreq_cdev->max_level;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530354 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530355}
356
357/**
358 * cpufreq_get_cur_state - callback function to get the current cooling state.
359 * @cdev: thermal cooling device pointer.
360 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000361 *
362 * Callback for the thermal cooling device to return the cpufreq
363 * current cooling state.
364 *
365 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530366 */
367static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
368 unsigned long *state)
369{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530370 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530371
Viresh Kumar1dea4322017-04-25 15:57:10 +0530372 *state = cpufreq_cdev->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000373
hongbo.zhang160b7d82012-10-30 17:48:59 +0100374 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530375}
376
377/**
378 * cpufreq_set_cur_state - callback function to set the current cooling state.
379 * @cdev: thermal cooling device pointer.
380 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000381 *
382 * Callback for the thermal cooling device to change the cpufreq
383 * current cooling state.
384 *
385 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530386 */
387static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
388 unsigned long state)
389{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530390 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530391 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530392
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530393 /* Request state should be less than max_level */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530394 if (WARN_ON(state > cpufreq_cdev->max_level))
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530395 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530396
Viresh Kumar5194fe42014-12-04 09:42:00 +0530397 /* Check if the old cooling action is same as new cooling action */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530398 if (cpufreq_cdev->cpufreq_state == state)
Viresh Kumar5194fe42014-12-04 09:42:00 +0530399 return 0;
400
Viresh Kumar349d39d2017-04-25 15:57:19 +0530401 clip_freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530402 cpufreq_cdev->cpufreq_state = state;
403 cpufreq_cdev->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530404
Viresh Kumarba76dd92017-04-25 15:57:18 +0530405 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
Viresh Kumar5194fe42014-12-04 09:42:00 +0530406
407 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530408}
409
Javi Merinoc36cf072015-02-26 19:00:29 +0000410/**
411 * cpufreq_get_requested_power() - get the current power
412 * @cdev: &thermal_cooling_device pointer
413 * @tz: a valid thermal zone device pointer
414 * @power: pointer in which to store the resulting power
415 *
416 * Calculate the current power consumption of the cpus in milliwatts
417 * and store it in @power. This function should actually calculate
418 * the requested power, but it's hard to get the frequency that
419 * cpufreq would have assigned if there were no thermal limits.
420 * Instead, we calculate the current power on the assumption that the
421 * immediate future will look like the immediate past.
422 *
423 * We use the current frequency and the average load since this
424 * function was last called. In reality, there could have been
425 * multiple opps since this function was last called and that affects
426 * the load calculation. While it's not perfectly accurate, this
427 * simplification is good enough and works. REVISIT this, as more
428 * complex code may be needed if experiments show that it's not
429 * accurate enough.
430 *
431 * Return: 0 on success, -E* if getting the static power failed.
432 */
433static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
434 struct thermal_zone_device *tz,
435 u32 *power)
436{
437 unsigned long freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530438 int i = 0, cpu;
439 u32 total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530440 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530441 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000442 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000443
Viresh Kumarba76dd92017-04-25 15:57:18 +0530444 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000445
Javi Merino6828a472015-03-02 17:17:20 +0000446 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530447 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000448
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530449 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000450 }
451
Viresh Kumarba76dd92017-04-25 15:57:18 +0530452 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000453 u32 load;
454
455 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530456 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000457 else
458 load = 0;
459
460 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000461 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
462 load_cpu[i] = load;
463
464 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000465 }
466
Viresh Kumar1dea4322017-04-25 15:57:10 +0530467 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000468
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530469 *power = get_dynamic_power(cpufreq_cdev, freq);
Javi Merino6828a472015-03-02 17:17:20 +0000470
471 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530472 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530473 load_cpu, i, *power);
Javi Merino6828a472015-03-02 17:17:20 +0000474
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530475 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000476 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000477
Javi Merinoc36cf072015-02-26 19:00:29 +0000478 return 0;
479}
480
481/**
482 * cpufreq_state2power() - convert a cpu cdev state to power consumed
483 * @cdev: &thermal_cooling_device pointer
484 * @tz: a valid thermal zone device pointer
485 * @state: cooling device state to be converted
486 * @power: pointer in which to store the resulting power
487 *
488 * Convert cooling device state @state into power consumption in
489 * milliwatts assuming 100% load. Store the calculated power in
490 * @power.
491 *
492 * Return: 0 on success, -EINVAL if the cooling device state could not
493 * be converted into a frequency or other -E* if there was an error
494 * when calculating the static power.
495 */
496static int cpufreq_state2power(struct thermal_cooling_device *cdev,
497 struct thermal_zone_device *tz,
498 unsigned long state, u32 *power)
499{
500 unsigned int freq, num_cpus;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530501 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000502
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530503 /* Request state should be less than max_level */
504 if (WARN_ON(state > cpufreq_cdev->max_level))
505 return -EINVAL;
506
Viresh Kumarba76dd92017-04-25 15:57:18 +0530507 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000508
Viresh Kumar349d39d2017-04-25 15:57:19 +0530509 freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530510 *power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000511
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530512 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000513}
514
515/**
516 * cpufreq_power2state() - convert power to a cooling device state
517 * @cdev: &thermal_cooling_device pointer
518 * @tz: a valid thermal zone device pointer
519 * @power: power in milliwatts to be converted
520 * @state: pointer in which to store the resulting state
521 *
522 * Calculate a cooling device state for the cpus described by @cdev
523 * that would allow them to consume at most @power mW and store it in
524 * @state. Note that this calculation depends on external factors
525 * such as the cpu load or the current static power. Calling this
526 * function with the same power as input can yield different cooling
527 * device states depending on those external factors.
528 *
529 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
530 * the calculated frequency could not be converted to a valid state.
531 * The latter should not happen unless the frequencies available to
532 * cpufreq have changed since the initialization of the cpu cooling
533 * device.
534 */
535static int cpufreq_power2state(struct thermal_cooling_device *cdev,
536 struct thermal_zone_device *tz, u32 power,
537 unsigned long *state)
538{
Viresh Kumarba76dd92017-04-25 15:57:18 +0530539 unsigned int cur_freq, target_freq;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530540 u32 last_load, normalised_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530541 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530542 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000543
Viresh Kumarba76dd92017-04-25 15:57:18 +0530544 cur_freq = cpufreq_quick_get(policy->cpu);
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530545 power = power > 0 ? power : 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530546 last_load = cpufreq_cdev->last_load ?: 1;
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530547 normalised_power = (power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530548 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000549
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530550 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530551 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
552 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000553 return 0;
554}
555
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530556/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100557
Javi Merinoc36cf072015-02-26 19:00:29 +0000558static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530559 .get_max_state = cpufreq_get_max_state,
560 .get_cur_state = cpufreq_get_cur_state,
561 .set_cur_state = cpufreq_set_cur_state,
562};
563
Brendan Jackmana305a432016-08-17 16:14:59 +0100564static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
565 .get_max_state = cpufreq_get_max_state,
566 .get_cur_state = cpufreq_get_cur_state,
567 .set_cur_state = cpufreq_set_cur_state,
568 .get_requested_power = cpufreq_get_requested_power,
569 .state2power = cpufreq_state2power,
570 .power2state = cpufreq_power2state,
571};
572
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530573/* Notifier for cpufreq policy change */
574static struct notifier_block thermal_cpufreq_notifier_block = {
575 .notifier_call = cpufreq_thermal_notifier,
576};
577
Viresh Kumarf6859012014-12-04 09:42:06 +0530578static unsigned int find_next_max(struct cpufreq_frequency_table *table,
579 unsigned int prev_max)
580{
581 struct cpufreq_frequency_table *pos;
582 unsigned int max = 0;
583
584 cpufreq_for_each_valid_entry(pos, table) {
585 if (pos->frequency > max && pos->frequency < prev_max)
586 max = pos->frequency;
587 }
588
589 return max;
590}
591
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530592/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400593 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
594 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530595 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530596 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000597 * @capacitance: dynamic power coefficient for these cpus
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000598 *
599 * This interface function registers the cpufreq cooling device with the name
600 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400601 * cooling devices. It also gives the opportunity to link the cooling device
602 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000603 *
604 * Return: a valid struct thermal_cooling_device pointer on success,
605 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530606 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400607static struct thermal_cooling_device *
608__cpufreq_cooling_register(struct device_node *np,
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530609 struct cpufreq_policy *policy, u32 capacitance)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530610{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530611 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530612 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530613 char dev_name[THERMAL_NAME_LENGTH];
Javi Merinoc36cf072015-02-26 19:00:29 +0000614 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530615 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100616 struct thermal_cooling_device_ops *cooling_ops;
Matthew Wilcox088db932017-03-10 18:33:28 +0000617 bool first;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530618
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530619 if (IS_ERR_OR_NULL(policy)) {
Arvind Yadavb2fd7082017-10-24 13:20:39 +0530620 pr_err("%s: cpufreq policy isn't valid: %p\n", __func__, policy);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530621 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530622 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530623
Viresh Kumar55d85292017-04-25 15:57:15 +0530624 i = cpufreq_table_count_valid_entries(policy);
625 if (!i) {
626 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
627 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530628 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530629 }
630
Viresh Kumar1dea4322017-04-25 15:57:10 +0530631 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530632 if (!cpufreq_cdev)
633 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530634
Viresh Kumarb12b6512017-04-25 15:57:16 +0530635 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530636 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530637 cpufreq_cdev->idle_time = kcalloc(num_cpus,
638 sizeof(*cpufreq_cdev->idle_time),
639 GFP_KERNEL);
640 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530641 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000642 goto free_cdev;
643 }
644
Viresh Kumar55d85292017-04-25 15:57:15 +0530645 /* max_level is an index, not a counter */
646 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530647
Viresh Kumarf19b1a12017-05-23 12:33:06 +0530648 cpufreq_cdev->freq_table = kmalloc_array(i,
649 sizeof(*cpufreq_cdev->freq_table),
650 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530651 if (!cpufreq_cdev->freq_table) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530652 cdev = ERR_PTR(-ENOMEM);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530653 goto free_idle_time;
Viresh Kumarf6859012014-12-04 09:42:06 +0530654 }
655
Matthew Wilcoxae606082016-12-21 09:47:05 -0800656 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
657 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530658 cdev = ERR_PTR(ret);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530659 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530660 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530661 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530662
Viresh Kumar349d39d2017-04-25 15:57:19 +0530663 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
664 cpufreq_cdev->id);
665
Viresh Kumarf6859012014-12-04 09:42:06 +0530666 /* Fill freq-table in descending order of frequencies */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530667 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
Viresh Kumar55d85292017-04-25 15:57:15 +0530668 freq = find_next_max(policy->freq_table, freq);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530669 cpufreq_cdev->freq_table[i].frequency = freq;
Viresh Kumarf6859012014-12-04 09:42:06 +0530670
671 /* Warn for duplicate entries */
672 if (!freq)
673 pr_warn("%s: table has duplicate entries\n", __func__);
674 else
675 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530676 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530677
Viresh Kumar349d39d2017-04-25 15:57:19 +0530678 if (capacitance) {
Viresh Kumar349d39d2017-04-25 15:57:19 +0530679 ret = update_freq_table(cpufreq_cdev, capacitance);
680 if (ret) {
681 cdev = ERR_PTR(ret);
682 goto remove_ida;
683 }
684
685 cooling_ops = &cpufreq_power_cooling_ops;
686 } else {
687 cooling_ops = &cpufreq_cooling_ops;
688 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100689
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530690 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
691 cooling_ops);
692 if (IS_ERR(cdev))
Matthew Wilcoxae606082016-12-21 09:47:05 -0800693 goto remove_ida;
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100694
Viresh Kumar349d39d2017-04-25 15:57:19 +0530695 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530696 cpufreq_cdev->cdev = cdev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530697
Russell King02373d72015-08-12 15:22:16 +0530698 mutex_lock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530699 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530700 first = list_empty(&cpufreq_cdev_list);
701 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000702 mutex_unlock(&cooling_list_lock);
703
704 if (first)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530705 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000706 CPUFREQ_POLICY_NOTIFIER);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000707
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530708 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530709
Matthew Wilcoxae606082016-12-21 09:47:05 -0800710remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530711 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530712free_table:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530713 kfree(cpufreq_cdev->freq_table);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530714free_idle_time:
715 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530716free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530717 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530718 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530719}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400720
721/**
722 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530723 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400724 *
725 * This interface function registers the cpufreq cooling device with the name
726 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
727 * cooling devices.
728 *
729 * Return: a valid struct thermal_cooling_device pointer on success,
730 * on failure, it returns a corresponding ERR_PTR().
731 */
732struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530733cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400734{
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530735 return __cpufreq_cooling_register(NULL, policy, 0);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400736}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000737EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530738
739/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400740 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530741 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400742 *
743 * This interface function registers the cpufreq cooling device with the name
744 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
745 * cooling devices. Using this API, the cpufreq cooling device will be
746 * linked to the device tree node provided.
747 *
Javi Merinoc36cf072015-02-26 19:00:29 +0000748 * Using this function, the cooling device will implement the power
749 * extensions by using a simple cpu power model. The cpus must have
750 * registered their OPPs using the OPP library.
751 *
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530752 * It also takes into account, if property present in policy CPU node, the
753 * static power consumed by the cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +0000754 *
755 * Return: a valid struct thermal_cooling_device pointer on success,
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530756 * and NULL on failure.
Javi Merinoc36cf072015-02-26 19:00:29 +0000757 */
758struct thermal_cooling_device *
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530759of_cpufreq_cooling_register(struct cpufreq_policy *policy)
Javi Merinoc36cf072015-02-26 19:00:29 +0000760{
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530761 struct device_node *np = of_get_cpu_node(policy->cpu, NULL);
762 struct thermal_cooling_device *cdev = NULL;
763 u32 capacitance = 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000764
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530765 if (!np) {
766 pr_err("cpu_cooling: OF node not available for cpu%d\n",
767 policy->cpu);
768 return NULL;
769 }
770
771 if (of_find_property(np, "#cooling-cells", NULL)) {
772 of_property_read_u32(np, "dynamic-power-coefficient",
773 &capacitance);
774
Viresh Kumar84fe2ca2017-12-05 11:02:46 +0530775 cdev = __cpufreq_cooling_register(np, policy, capacitance);
Viresh Kumarf5f263f2017-12-05 11:02:43 +0530776 if (IS_ERR(cdev)) {
777 pr_err("cpu_cooling: cpu%d is not running as cooling device: %ld\n",
778 policy->cpu, PTR_ERR(cdev));
779 cdev = NULL;
780 }
781 }
782
783 of_node_put(np);
784 return cdev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000785}
Viresh Kumar3ebb62f2017-12-05 11:02:45 +0530786EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
Javi Merinoc36cf072015-02-26 19:00:29 +0000787
788/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530789 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
790 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000791 *
792 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530793 */
794void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
795{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530796 struct cpufreq_cooling_device *cpufreq_cdev;
Matthew Wilcox088db932017-03-10 18:33:28 +0000797 bool last;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530798
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400799 if (!cdev)
800 return;
801
Viresh Kumar1dea4322017-04-25 15:57:10 +0530802 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530803
Matthew Wilcoxae606082016-12-21 09:47:05 -0800804 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530805 list_del(&cpufreq_cdev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530806 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530807 last = list_empty(&cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000808 mutex_unlock(&cooling_list_lock);
809
810 if (last)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530811 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000812 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +0530813
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530814 thermal_cooling_device_unregister(cpufreq_cdev->cdev);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530815 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530816 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530817 kfree(cpufreq_cdev->freq_table);
818 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530819}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000820EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);