blob: 9cbf7bdbeb7a34f87b8edf6a72912c5a1b5e3512 [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 Kumar04bdbdf2017-04-25 15:57:11 +053078 * @cdev: thermal_cooling_device pointer to keep track of the
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000079 * registered cooling device.
Viresh Kumarb12b6512017-04-25 15:57:16 +053080 * @policy: cpufreq policy.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053081 * @cpufreq_state: integer value representing the current state of cpufreq
82 * cooling devices.
Viresh Kumar59f0d212015-07-30 12:40:33 +053083 * @clipped_freq: integer value representing the absolute value of the clipped
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053084 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053085 * @max_level: maximum cooling level. One less than total number of valid
86 * cpufreq frequencies.
Javi Merinofc4de352014-12-15 16:55:52 +000087 * @node: list_head to link all cpufreq_cooling_device together.
Hugh Kang0744f132016-09-07 09:35:39 +090088 * @last_load: load measured by the latest call to cpufreq_get_requested_power()
Viresh Kumar81ee14d2017-04-25 15:57:20 +053089 * @idle_time: idle time stats
Javi Merinoc36cf072015-02-26 19:00:29 +000090 * @plat_get_static_power: callback to calculate the static power
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 Kumar04bdbdf2017-04-25 15:57:11 +053097 struct thermal_cooling_device *cdev;
Viresh Kumarb12b6512017-04-25 15:57:16 +053098 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053099 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530100 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530101 unsigned int max_level;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530102 struct freq_table *freq_table; /* In descending order */
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530103 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +0000104 u32 last_load;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530105 struct time_in_idle *idle_time;
Javi Merinoc36cf072015-02-26 19:00:29 +0000106 get_static_t plat_get_static_power;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530107};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530108
Viresh Kumarfb8ea302017-04-25 15:57:09 +0530109static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +0530110static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530111static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530112
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530113/* Below code defines functions to be used for cpufreq as cooling device */
114
115/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530116 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +0530117 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530118 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000119 *
Viresh Kumarda27f692017-04-25 15:57:21 +0530120 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530121 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530122static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530123 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530124{
Viresh Kumarda27f692017-04-25 15:57:21 +0530125 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530126 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000127
Viresh Kumarda27f692017-04-25 15:57:21 +0530128 for (level = 1; level <= cpufreq_cdev->max_level; level++)
129 if (freq > freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530130 break;
Zhang Ruia1167762014-01-02 11:57:48 +0800131
Viresh Kumarda27f692017-04-25 15:57:21 +0530132 return level - 1;
Zhang Ruifc35b352013-02-08 13:09:32 +0800133}
134
Eduardo Valentin44952d32013-04-17 17:12:05 +0000135/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530136 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
137 * @nb: struct notifier_block * with callback info.
138 * @event: value showing cpufreq event for which this function invoked.
139 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000140 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100141 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000142 * Every time there is a change in policy, we will intercept and
143 * update the cpufreq policy with thermal constraints.
144 *
145 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530146 */
147static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000148 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530149{
150 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530151 unsigned long clipped_freq;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530152 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530153
Viresh Kumara24af232015-07-30 12:40:32 +0530154 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000155 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530156
157 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530158 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530159 /*
160 * A new copy of the policy is sent to the notifier and can't
161 * compare that directly.
162 */
163 if (policy->cpu != cpufreq_cdev->policy->cpu)
Viresh Kumara24af232015-07-30 12:40:32 +0530164 continue;
165
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530166 /*
167 * policy->max is the maximum allowed frequency defined by user
168 * and clipped_freq is the maximum that thermal constraints
169 * allow.
170 *
171 * If clipped_freq is lower than policy->max, then we need to
172 * readjust policy->max.
173 *
174 * But, if clipped_freq is greater than policy->max, we don't
175 * need to do anything.
176 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530177 clipped_freq = cpufreq_cdev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530178
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530179 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530180 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530181 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530182 }
Viresh Kumara24af232015-07-30 12:40:32 +0530183 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530184
Javi Merinoc36cf072015-02-26 19:00:29 +0000185 return NOTIFY_OK;
186}
187
188/**
Viresh Kumar349d39d2017-04-25 15:57:19 +0530189 * update_freq_table() - Update the freq table with power numbers
190 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
Javi Merinoc36cf072015-02-26 19:00:29 +0000191 * @capacitance: dynamic power coefficient for these cpus
192 *
Viresh Kumar349d39d2017-04-25 15:57:19 +0530193 * Update the freq table with power numbers. This table will be used in
194 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
195 * frequency efficiently. Power is stored in mW, frequency in KHz. The
196 * resulting table is in descending order.
Javi Merinoc36cf072015-02-26 19:00:29 +0000197 *
Javi Merino459ac372015-08-17 19:21:42 +0100198 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
Viresh Kumar349d39d2017-04-25 15:57:19 +0530199 * or -ENOMEM if we run out of memory.
Javi Merinoc36cf072015-02-26 19:00:29 +0000200 */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530201static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
202 u32 capacitance)
Javi Merinoc36cf072015-02-26 19:00:29 +0000203{
Viresh Kumar349d39d2017-04-25 15:57:19 +0530204 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000205 struct dev_pm_opp *opp;
206 struct device *dev = NULL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530207 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000208
Viresh Kumar02bacb22017-04-25 15:57:17 +0530209 dev = get_cpu_device(cpu);
210 if (unlikely(!dev)) {
211 dev_warn(&cpufreq_cdev->cdev->device,
212 "No cpu device for cpu %d\n", cpu);
213 return -ENODEV;
Javi Merinoc36cf072015-02-26 19:00:29 +0000214 }
215
Viresh Kumar02bacb22017-04-25 15:57:17 +0530216 num_opps = dev_pm_opp_get_opp_count(dev);
217 if (num_opps < 0)
218 return num_opps;
219
Viresh Kumar349d39d2017-04-25 15:57:19 +0530220 /*
221 * The cpufreq table is also built from the OPP table and so the count
222 * should match.
223 */
224 if (num_opps != cpufreq_cdev->max_level + 1) {
225 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
Javi Merino459ac372015-08-17 19:21:42 +0100226 return -EINVAL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530227 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000228
Viresh Kumar349d39d2017-04-25 15:57:19 +0530229 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
230 unsigned long freq = freq_table[i].frequency * 1000;
231 u32 freq_mhz = freq_table[i].frequency / 1000;
Javi Merinoc36cf072015-02-26 19:00:29 +0000232 u64 power;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530233 u32 voltage_mv;
Javi Merinoc36cf072015-02-26 19:00:29 +0000234
Viresh Kumar349d39d2017-04-25 15:57:19 +0530235 /*
236 * Find ceil frequency as 'freq' may be slightly lower than OPP
237 * freq due to truncation while converting to kHz.
238 */
239 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
240 if (IS_ERR(opp)) {
241 dev_err(dev, "failed to get opp for %lu frequency\n",
242 freq);
243 return -EINVAL;
Javi Merino459ac372015-08-17 19:21:42 +0100244 }
245
Javi Merinoc36cf072015-02-26 19:00:29 +0000246 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530247 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000248
249 /*
250 * Do the multiplication with MHz and millivolt so as
251 * to not overflow.
252 */
253 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
254 do_div(power, 1000000000);
255
Javi Merinoc36cf072015-02-26 19:00:29 +0000256 /* power is stored in mW */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530257 freq_table[i].power = power;
Javi Merinoeba4f882015-08-17 19:21:43 +0100258 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000259
Javi Merino459ac372015-08-17 19:21:42 +0100260 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000261}
262
Viresh Kumar1dea4322017-04-25 15:57:10 +0530263static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000264 u32 freq)
265{
266 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530267 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000268
Viresh Kumar349d39d2017-04-25 15:57:19 +0530269 for (i = 1; i <= cpufreq_cdev->max_level; i++)
270 if (freq > freq_table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000271 break;
272
Viresh Kumar349d39d2017-04-25 15:57:19 +0530273 return freq_table[i - 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000274}
275
Viresh Kumar1dea4322017-04-25 15:57:10 +0530276static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000277 u32 power)
278{
279 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530280 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000281
Viresh Kumar349d39d2017-04-25 15:57:19 +0530282 for (i = 1; i <= cpufreq_cdev->max_level; i++)
283 if (power > freq_table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000284 break;
285
Viresh Kumar349d39d2017-04-25 15:57:19 +0530286 return freq_table[i - 1].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000287}
288
289/**
290 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530291 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000292 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530293 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000294 *
295 * Return: The average load of cpu @cpu in percentage since this
296 * function was last called.
297 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530298static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000299 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000300{
301 u32 load;
302 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530303 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000304
305 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530306 delta_idle = now_idle - idle_time->time;
307 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000308
309 if (delta_time <= delta_idle)
310 load = 0;
311 else
312 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
313
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530314 idle_time->time = now_idle;
315 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000316
317 return load;
318}
319
320/**
321 * get_static_power() - calculate the static power consumed by the cpus
Viresh Kumar1dea4322017-04-25 15:57:10 +0530322 * @cpufreq_cdev: struct &cpufreq_cooling_device for this cpu cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000323 * @tz: thermal zone device in which we're operating
324 * @freq: frequency in KHz
325 * @power: pointer in which to store the calculated static power
326 *
327 * Calculate the static power consumed by the cpus described by
328 * @cpu_actor running at frequency @freq. This function relies on a
329 * platform specific function that should have been provided when the
330 * actor was registered. If it wasn't, the static power is assumed to
331 * be negligible. The calculated static power is stored in @power.
332 *
333 * Return: 0 on success, -E* on failure.
334 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530335static int get_static_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000336 struct thermal_zone_device *tz, unsigned long freq,
337 u32 *power)
338{
339 struct dev_pm_opp *opp;
340 unsigned long voltage;
Viresh Kumar53ca1ec2017-04-25 15:57:22 +0530341 struct cpufreq_policy *policy = cpufreq_cdev->policy;
342 struct cpumask *cpumask = policy->related_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000343 unsigned long freq_hz = freq * 1000;
Viresh Kumar53ca1ec2017-04-25 15:57:22 +0530344 struct device *dev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000345
Viresh Kumar53ca1ec2017-04-25 15:57:22 +0530346 if (!cpufreq_cdev->plat_get_static_power) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000347 *power = 0;
348 return 0;
349 }
350
Viresh Kumar53ca1ec2017-04-25 15:57:22 +0530351 dev = get_cpu_device(policy->cpu);
352 WARN_ON(!dev);
353
354 opp = dev_pm_opp_find_freq_exact(dev, freq_hz, true);
Viresh Kumar3ea32172017-02-07 09:40:05 +0530355 if (IS_ERR(opp)) {
Viresh Kumar53ca1ec2017-04-25 15:57:22 +0530356 dev_warn_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
Viresh Kumar3ea32172017-02-07 09:40:05 +0530357 freq_hz, PTR_ERR(opp));
358 return -EINVAL;
359 }
360
Javi Merinoc36cf072015-02-26 19:00:29 +0000361 voltage = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530362 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000363
364 if (voltage == 0) {
Viresh Kumar53ca1ec2017-04-25 15:57:22 +0530365 dev_err_ratelimited(dev, "Failed to get voltage for frequency %lu\n",
Viresh Kumar3ea32172017-02-07 09:40:05 +0530366 freq_hz);
Javi Merinoc36cf072015-02-26 19:00:29 +0000367 return -EINVAL;
368 }
369
Viresh Kumar1dea4322017-04-25 15:57:10 +0530370 return cpufreq_cdev->plat_get_static_power(cpumask, tz->passive_delay,
371 voltage, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000372}
373
374/**
375 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530376 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000377 * @freq: current frequency
378 *
379 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530380 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000381 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530382static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000383 unsigned long freq)
384{
385 u32 raw_cpu_power;
386
Viresh Kumar1dea4322017-04-25 15:57:10 +0530387 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
388 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530389}
390
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000391/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530392
393/**
394 * cpufreq_get_max_state - callback function to get the max cooling state.
395 * @cdev: thermal cooling device pointer.
396 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000397 *
398 * Callback for the thermal cooling device to return the cpufreq
399 * max cooling state.
400 *
401 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530402 */
403static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
404 unsigned long *state)
405{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530406 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530407
Viresh Kumar1dea4322017-04-25 15:57:10 +0530408 *state = cpufreq_cdev->max_level;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530409 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530410}
411
412/**
413 * cpufreq_get_cur_state - callback function to get the current cooling state.
414 * @cdev: thermal cooling device pointer.
415 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000416 *
417 * Callback for the thermal cooling device to return the cpufreq
418 * current cooling state.
419 *
420 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530421 */
422static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
423 unsigned long *state)
424{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530425 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530426
Viresh Kumar1dea4322017-04-25 15:57:10 +0530427 *state = cpufreq_cdev->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000428
hongbo.zhang160b7d82012-10-30 17:48:59 +0100429 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530430}
431
432/**
433 * cpufreq_set_cur_state - callback function to set the current cooling state.
434 * @cdev: thermal cooling device pointer.
435 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000436 *
437 * Callback for the thermal cooling device to change the cpufreq
438 * current cooling state.
439 *
440 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530441 */
442static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
443 unsigned long state)
444{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530445 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530446 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530447
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530448 /* Request state should be less than max_level */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530449 if (WARN_ON(state > cpufreq_cdev->max_level))
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530450 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530451
Viresh Kumar5194fe42014-12-04 09:42:00 +0530452 /* Check if the old cooling action is same as new cooling action */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530453 if (cpufreq_cdev->cpufreq_state == state)
Viresh Kumar5194fe42014-12-04 09:42:00 +0530454 return 0;
455
Viresh Kumar349d39d2017-04-25 15:57:19 +0530456 clip_freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530457 cpufreq_cdev->cpufreq_state = state;
458 cpufreq_cdev->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530459
Viresh Kumarba76dd92017-04-25 15:57:18 +0530460 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
Viresh Kumar5194fe42014-12-04 09:42:00 +0530461
462 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530463}
464
Javi Merinoc36cf072015-02-26 19:00:29 +0000465/**
466 * cpufreq_get_requested_power() - get the current power
467 * @cdev: &thermal_cooling_device pointer
468 * @tz: a valid thermal zone device pointer
469 * @power: pointer in which to store the resulting power
470 *
471 * Calculate the current power consumption of the cpus in milliwatts
472 * and store it in @power. This function should actually calculate
473 * the requested power, but it's hard to get the frequency that
474 * cpufreq would have assigned if there were no thermal limits.
475 * Instead, we calculate the current power on the assumption that the
476 * immediate future will look like the immediate past.
477 *
478 * We use the current frequency and the average load since this
479 * function was last called. In reality, there could have been
480 * multiple opps since this function was last called and that affects
481 * the load calculation. While it's not perfectly accurate, this
482 * simplification is good enough and works. REVISIT this, as more
483 * complex code may be needed if experiments show that it's not
484 * accurate enough.
485 *
486 * Return: 0 on success, -E* if getting the static power failed.
487 */
488static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
489 struct thermal_zone_device *tz,
490 u32 *power)
491{
492 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000493 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000494 u32 static_power, dynamic_power, total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530495 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530496 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000497 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000498
Viresh Kumarba76dd92017-04-25 15:57:18 +0530499 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000500
Javi Merino6828a472015-03-02 17:17:20 +0000501 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530502 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000503
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530504 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000505 }
506
Viresh Kumarba76dd92017-04-25 15:57:18 +0530507 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000508 u32 load;
509
510 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530511 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000512 else
513 load = 0;
514
515 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000516 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
517 load_cpu[i] = load;
518
519 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000520 }
521
Viresh Kumar1dea4322017-04-25 15:57:10 +0530522 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000523
Viresh Kumar1dea4322017-04-25 15:57:10 +0530524 dynamic_power = get_dynamic_power(cpufreq_cdev, freq);
525 ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000526 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530527 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000528 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000529 }
530
531 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530532 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
533 load_cpu, i, dynamic_power,
534 static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000535
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530536 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000537 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000538
539 *power = static_power + dynamic_power;
540 return 0;
541}
542
543/**
544 * cpufreq_state2power() - convert a cpu cdev state to power consumed
545 * @cdev: &thermal_cooling_device pointer
546 * @tz: a valid thermal zone device pointer
547 * @state: cooling device state to be converted
548 * @power: pointer in which to store the resulting power
549 *
550 * Convert cooling device state @state into power consumption in
551 * milliwatts assuming 100% load. Store the calculated power in
552 * @power.
553 *
554 * Return: 0 on success, -EINVAL if the cooling device state could not
555 * be converted into a frequency or other -E* if there was an error
556 * when calculating the static power.
557 */
558static int cpufreq_state2power(struct thermal_cooling_device *cdev,
559 struct thermal_zone_device *tz,
560 unsigned long state, u32 *power)
561{
562 unsigned int freq, num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000563 u32 static_power, dynamic_power;
564 int ret;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530565 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000566
Viresh Kumarcb1b6312017-04-25 15:57:23 +0530567 /* Request state should be less than max_level */
568 if (WARN_ON(state > cpufreq_cdev->max_level))
569 return -EINVAL;
570
Viresh Kumarba76dd92017-04-25 15:57:18 +0530571 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000572
Viresh Kumar349d39d2017-04-25 15:57:19 +0530573 freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530574 dynamic_power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
575 ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000576 if (ret)
Viresh Kumarba76dd92017-04-25 15:57:18 +0530577 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000578
579 *power = static_power + dynamic_power;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100580 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000581}
582
583/**
584 * cpufreq_power2state() - convert power to a cooling device state
585 * @cdev: &thermal_cooling_device pointer
586 * @tz: a valid thermal zone device pointer
587 * @power: power in milliwatts to be converted
588 * @state: pointer in which to store the resulting state
589 *
590 * Calculate a cooling device state for the cpus described by @cdev
591 * that would allow them to consume at most @power mW and store it in
592 * @state. Note that this calculation depends on external factors
593 * such as the cpu load or the current static power. Calling this
594 * function with the same power as input can yield different cooling
595 * device states depending on those external factors.
596 *
597 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
598 * the calculated frequency could not be converted to a valid state.
599 * The latter should not happen unless the frequencies available to
600 * cpufreq have changed since the initialization of the cpu cooling
601 * device.
602 */
603static int cpufreq_power2state(struct thermal_cooling_device *cdev,
604 struct thermal_zone_device *tz, u32 power,
605 unsigned long *state)
606{
Viresh Kumarba76dd92017-04-25 15:57:18 +0530607 unsigned int cur_freq, target_freq;
Javi Merinoc36cf072015-02-26 19:00:29 +0000608 int ret;
609 s32 dyn_power;
610 u32 last_load, normalised_power, static_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530611 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530612 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000613
Viresh Kumarba76dd92017-04-25 15:57:18 +0530614 cur_freq = cpufreq_quick_get(policy->cpu);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530615 ret = get_static_power(cpufreq_cdev, tz, cur_freq, &static_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000616 if (ret)
617 return ret;
618
619 dyn_power = power - static_power;
620 dyn_power = dyn_power > 0 ? dyn_power : 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530621 last_load = cpufreq_cdev->last_load ?: 1;
Javi Merinoc36cf072015-02-26 19:00:29 +0000622 normalised_power = (dyn_power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530623 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000624
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530625 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530626 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
627 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000628 return 0;
629}
630
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530631/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100632
Javi Merinoc36cf072015-02-26 19:00:29 +0000633static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530634 .get_max_state = cpufreq_get_max_state,
635 .get_cur_state = cpufreq_get_cur_state,
636 .set_cur_state = cpufreq_set_cur_state,
637};
638
Brendan Jackmana305a432016-08-17 16:14:59 +0100639static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
640 .get_max_state = cpufreq_get_max_state,
641 .get_cur_state = cpufreq_get_cur_state,
642 .set_cur_state = cpufreq_set_cur_state,
643 .get_requested_power = cpufreq_get_requested_power,
644 .state2power = cpufreq_state2power,
645 .power2state = cpufreq_power2state,
646};
647
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530648/* Notifier for cpufreq policy change */
649static struct notifier_block thermal_cpufreq_notifier_block = {
650 .notifier_call = cpufreq_thermal_notifier,
651};
652
Viresh Kumarf6859012014-12-04 09:42:06 +0530653static unsigned int find_next_max(struct cpufreq_frequency_table *table,
654 unsigned int prev_max)
655{
656 struct cpufreq_frequency_table *pos;
657 unsigned int max = 0;
658
659 cpufreq_for_each_valid_entry(pos, table) {
660 if (pos->frequency > max && pos->frequency < prev_max)
661 max = pos->frequency;
662 }
663
664 return max;
665}
666
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530667/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400668 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
669 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530670 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530671 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000672 * @capacitance: dynamic power coefficient for these cpus
673 * @plat_static_func: function to calculate the static power consumed by these
674 * cpus (optional)
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000675 *
676 * This interface function registers the cpufreq cooling device with the name
677 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400678 * cooling devices. It also gives the opportunity to link the cooling device
679 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000680 *
681 * Return: a valid struct thermal_cooling_device pointer on success,
682 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530683 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400684static struct thermal_cooling_device *
685__cpufreq_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530686 struct cpufreq_policy *policy, u32 capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000687 get_static_t plat_static_func)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530688{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530689 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530690 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530691 char dev_name[THERMAL_NAME_LENGTH];
Javi Merinoc36cf072015-02-26 19:00:29 +0000692 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530693 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100694 struct thermal_cooling_device_ops *cooling_ops;
Matthew Wilcox088db932017-03-10 18:33:28 +0000695 bool first;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530696
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530697 if (IS_ERR_OR_NULL(policy)) {
698 pr_err("%s: cpufreq policy isn't valid: %p", __func__, policy);
699 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530700 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530701
Viresh Kumar55d85292017-04-25 15:57:15 +0530702 i = cpufreq_table_count_valid_entries(policy);
703 if (!i) {
704 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
705 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530706 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530707 }
708
Viresh Kumar1dea4322017-04-25 15:57:10 +0530709 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530710 if (!cpufreq_cdev)
711 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530712
Viresh Kumarb12b6512017-04-25 15:57:16 +0530713 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530714 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530715 cpufreq_cdev->idle_time = kcalloc(num_cpus,
716 sizeof(*cpufreq_cdev->idle_time),
717 GFP_KERNEL);
718 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530719 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000720 goto free_cdev;
721 }
722
Viresh Kumar55d85292017-04-25 15:57:15 +0530723 /* max_level is an index, not a counter */
724 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530725
Viresh Kumar55d85292017-04-25 15:57:15 +0530726 cpufreq_cdev->freq_table = kmalloc(sizeof(*cpufreq_cdev->freq_table) * i,
727 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530728 if (!cpufreq_cdev->freq_table) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530729 cdev = ERR_PTR(-ENOMEM);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530730 goto free_idle_time;
Viresh Kumarf6859012014-12-04 09:42:06 +0530731 }
732
Matthew Wilcoxae606082016-12-21 09:47:05 -0800733 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
734 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530735 cdev = ERR_PTR(ret);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530736 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530737 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530738 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530739
Viresh Kumar349d39d2017-04-25 15:57:19 +0530740 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
741 cpufreq_cdev->id);
742
Viresh Kumarf6859012014-12-04 09:42:06 +0530743 /* Fill freq-table in descending order of frequencies */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530744 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
Viresh Kumar55d85292017-04-25 15:57:15 +0530745 freq = find_next_max(policy->freq_table, freq);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530746 cpufreq_cdev->freq_table[i].frequency = freq;
Viresh Kumarf6859012014-12-04 09:42:06 +0530747
748 /* Warn for duplicate entries */
749 if (!freq)
750 pr_warn("%s: table has duplicate entries\n", __func__);
751 else
752 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530753 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530754
Viresh Kumar349d39d2017-04-25 15:57:19 +0530755 if (capacitance) {
756 cpufreq_cdev->plat_get_static_power = plat_static_func;
757
758 ret = update_freq_table(cpufreq_cdev, capacitance);
759 if (ret) {
760 cdev = ERR_PTR(ret);
761 goto remove_ida;
762 }
763
764 cooling_ops = &cpufreq_power_cooling_ops;
765 } else {
766 cooling_ops = &cpufreq_cooling_ops;
767 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100768
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530769 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
770 cooling_ops);
771 if (IS_ERR(cdev))
Matthew Wilcoxae606082016-12-21 09:47:05 -0800772 goto remove_ida;
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100773
Viresh Kumar349d39d2017-04-25 15:57:19 +0530774 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530775 cpufreq_cdev->cdev = cdev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530776
Russell King02373d72015-08-12 15:22:16 +0530777 mutex_lock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530778 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530779 first = list_empty(&cpufreq_cdev_list);
780 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000781 mutex_unlock(&cooling_list_lock);
782
783 if (first)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530784 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000785 CPUFREQ_POLICY_NOTIFIER);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000786
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530787 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530788
Matthew Wilcoxae606082016-12-21 09:47:05 -0800789remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530790 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530791free_table:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530792 kfree(cpufreq_cdev->freq_table);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530793free_idle_time:
794 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530795free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530796 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530797 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530798}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400799
800/**
801 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530802 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400803 *
804 * This interface function registers the cpufreq cooling device with the name
805 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
806 * cooling devices.
807 *
808 * Return: a valid struct thermal_cooling_device pointer on success,
809 * on failure, it returns a corresponding ERR_PTR().
810 */
811struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530812cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400813{
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530814 return __cpufreq_cooling_register(NULL, policy, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400815}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000816EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530817
818/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400819 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
820 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530821 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400822 *
823 * This interface function registers the cpufreq cooling device with the name
824 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
825 * cooling devices. Using this API, the cpufreq cooling device will be
826 * linked to the device tree node provided.
827 *
828 * Return: a valid struct thermal_cooling_device pointer on success,
829 * on failure, it returns a corresponding ERR_PTR().
830 */
831struct thermal_cooling_device *
832of_cpufreq_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530833 struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400834{
835 if (!np)
836 return ERR_PTR(-EINVAL);
837
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530838 return __cpufreq_cooling_register(np, policy, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400839}
840EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
841
842/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000843 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530844 * @policy: cpufreq policy
Javi Merinoc36cf072015-02-26 19:00:29 +0000845 * @capacitance: dynamic power coefficient for these cpus
846 * @plat_static_func: function to calculate the static power consumed by these
847 * cpus (optional)
848 *
849 * This interface function registers the cpufreq cooling device with
850 * the name "thermal-cpufreq-%x". This api can support multiple
851 * instances of cpufreq cooling devices. Using this function, the
852 * cooling device will implement the power extensions by using a
853 * simple cpu power model. The cpus must have registered their OPPs
854 * using the OPP library.
855 *
856 * An optional @plat_static_func may be provided to calculate the
857 * static power consumed by these cpus. If the platform's static
858 * power consumption is unknown or negligible, make it NULL.
859 *
860 * Return: a valid struct thermal_cooling_device pointer on success,
861 * on failure, it returns a corresponding ERR_PTR().
862 */
863struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530864cpufreq_power_cooling_register(struct cpufreq_policy *policy, u32 capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000865 get_static_t plat_static_func)
866{
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530867 return __cpufreq_cooling_register(NULL, policy, capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000868 plat_static_func);
869}
870EXPORT_SYMBOL(cpufreq_power_cooling_register);
871
872/**
873 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
874 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530875 * @policy: cpufreq policy
Javi Merinoc36cf072015-02-26 19:00:29 +0000876 * @capacitance: dynamic power coefficient for these cpus
877 * @plat_static_func: function to calculate the static power consumed by these
878 * cpus (optional)
879 *
880 * This interface function registers the cpufreq cooling device with
881 * the name "thermal-cpufreq-%x". This api can support multiple
882 * instances of cpufreq cooling devices. Using this API, the cpufreq
883 * cooling device will be linked to the device tree node provided.
884 * Using this function, the cooling device will implement the power
885 * extensions by using a simple cpu power model. The cpus must have
886 * registered their OPPs using the OPP library.
887 *
888 * An optional @plat_static_func may be provided to calculate the
889 * static power consumed by these cpus. If the platform's static
890 * power consumption is unknown or negligible, make it NULL.
891 *
892 * Return: a valid struct thermal_cooling_device pointer on success,
893 * on failure, it returns a corresponding ERR_PTR().
894 */
895struct thermal_cooling_device *
896of_cpufreq_power_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530897 struct cpufreq_policy *policy,
Javi Merinoc36cf072015-02-26 19:00:29 +0000898 u32 capacitance,
899 get_static_t plat_static_func)
900{
901 if (!np)
902 return ERR_PTR(-EINVAL);
903
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530904 return __cpufreq_cooling_register(np, policy, capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000905 plat_static_func);
906}
907EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
908
909/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530910 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
911 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000912 *
913 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530914 */
915void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
916{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530917 struct cpufreq_cooling_device *cpufreq_cdev;
Matthew Wilcox088db932017-03-10 18:33:28 +0000918 bool last;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530919
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400920 if (!cdev)
921 return;
922
Viresh Kumar1dea4322017-04-25 15:57:10 +0530923 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530924
Matthew Wilcoxae606082016-12-21 09:47:05 -0800925 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530926 list_del(&cpufreq_cdev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530927 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530928 last = list_empty(&cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000929 mutex_unlock(&cooling_list_lock);
930
931 if (last)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530932 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000933 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +0530934
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530935 thermal_cooling_device_unregister(cpufreq_cdev->cdev);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530936 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530937 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530938 kfree(cpufreq_cdev->freq_table);
939 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530940}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000941EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);