blob: 2c2d76ac04c38e1055d3f71fe4a3d653208239dd [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
Viresh Kumar02bacb22017-04-25 15:57:17 +053090 * @cpu_dev: the cpu_device of policy->cpu.
Javi Merinoc36cf072015-02-26 19:00:29 +000091 * @plat_get_static_power: callback to calculate the static power
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053092 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053093 * This structure is required for keeping information of each registered
94 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053095 */
96struct cpufreq_cooling_device {
97 int id;
Viresh Kumar04bdbdf2017-04-25 15:57:11 +053098 struct thermal_cooling_device *cdev;
Viresh Kumarb12b6512017-04-25 15:57:16 +053099 struct cpufreq_policy *policy;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530100 unsigned int cpufreq_state;
Viresh Kumar59f0d212015-07-30 12:40:33 +0530101 unsigned int clipped_freq;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530102 unsigned int max_level;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530103 struct freq_table *freq_table; /* In descending order */
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530104 struct list_head node;
Javi Merinoc36cf072015-02-26 19:00:29 +0000105 u32 last_load;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530106 struct time_in_idle *idle_time;
Javi Merinoc36cf072015-02-26 19:00:29 +0000107 struct device *cpu_dev;
108 get_static_t plat_get_static_power;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530109};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530110
Viresh Kumarfb8ea302017-04-25 15:57:09 +0530111static DEFINE_IDA(cpufreq_ida);
Russell King02373d72015-08-12 15:22:16 +0530112static DEFINE_MUTEX(cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530113static LIST_HEAD(cpufreq_cdev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530114
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530115/* Below code defines functions to be used for cpufreq as cooling device */
116
117/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530118 * get_level: Find the level for a particular frequency
Viresh Kumar1dea4322017-04-25 15:57:10 +0530119 * @cpufreq_cdev: cpufreq_cdev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530120 * @freq: Frequency
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000121 *
Viresh Kumarda27f692017-04-25 15:57:21 +0530122 * Return: level corresponding to the frequency.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530123 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530124static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_cdev,
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530125 unsigned int freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530126{
Viresh Kumarda27f692017-04-25 15:57:21 +0530127 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530128 unsigned long level;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000129
Viresh Kumarda27f692017-04-25 15:57:21 +0530130 for (level = 1; level <= cpufreq_cdev->max_level; level++)
131 if (freq > freq_table[level].frequency)
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530132 break;
Zhang Ruia1167762014-01-02 11:57:48 +0800133
Viresh Kumarda27f692017-04-25 15:57:21 +0530134 return level - 1;
Zhang Ruifc35b352013-02-08 13:09:32 +0800135}
136
Eduardo Valentin44952d32013-04-17 17:12:05 +0000137/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530138 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
139 * @nb: struct notifier_block * with callback info.
140 * @event: value showing cpufreq event for which this function invoked.
141 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000142 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100143 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000144 * Every time there is a change in policy, we will intercept and
145 * update the cpufreq policy with thermal constraints.
146 *
147 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530148 */
149static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000150 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530151{
152 struct cpufreq_policy *policy = data;
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530153 unsigned long clipped_freq;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530154 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530155
Viresh Kumara24af232015-07-30 12:40:32 +0530156 if (event != CPUFREQ_ADJUST)
Javi Merinoc36cf072015-02-26 19:00:29 +0000157 return NOTIFY_DONE;
Viresh Kumara24af232015-07-30 12:40:32 +0530158
159 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530160 list_for_each_entry(cpufreq_cdev, &cpufreq_cdev_list, node) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530161 /*
162 * A new copy of the policy is sent to the notifier and can't
163 * compare that directly.
164 */
165 if (policy->cpu != cpufreq_cdev->policy->cpu)
Viresh Kumara24af232015-07-30 12:40:32 +0530166 continue;
167
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530168 /*
169 * policy->max is the maximum allowed frequency defined by user
170 * and clipped_freq is the maximum that thermal constraints
171 * allow.
172 *
173 * If clipped_freq is lower than policy->max, then we need to
174 * readjust policy->max.
175 *
176 * But, if clipped_freq is greater than policy->max, we don't
177 * need to do anything.
178 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530179 clipped_freq = cpufreq_cdev->clipped_freq;
Viresh Kumara24af232015-07-30 12:40:32 +0530180
Viresh Kumar1afb9c52015-07-30 12:40:35 +0530181 if (policy->max > clipped_freq)
Viresh Kumarabcbcc22015-07-30 12:40:34 +0530182 cpufreq_verify_within_limits(policy, 0, clipped_freq);
Viresh Kumara24af232015-07-30 12:40:32 +0530183 break;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530184 }
Viresh Kumara24af232015-07-30 12:40:32 +0530185 mutex_unlock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530186
Javi Merinoc36cf072015-02-26 19:00:29 +0000187 return NOTIFY_OK;
188}
189
190/**
Viresh Kumar349d39d2017-04-25 15:57:19 +0530191 * update_freq_table() - Update the freq table with power numbers
192 * @cpufreq_cdev: the cpufreq cooling device in which to update the table
Javi Merinoc36cf072015-02-26 19:00:29 +0000193 * @capacitance: dynamic power coefficient for these cpus
194 *
Viresh Kumar349d39d2017-04-25 15:57:19 +0530195 * Update the freq table with power numbers. This table will be used in
196 * cpu_power_to_freq() and cpu_freq_to_power() to convert between power and
197 * frequency efficiently. Power is stored in mW, frequency in KHz. The
198 * resulting table is in descending order.
Javi Merinoc36cf072015-02-26 19:00:29 +0000199 *
Javi Merino459ac372015-08-17 19:21:42 +0100200 * Return: 0 on success, -EINVAL if there are no OPPs for any CPUs,
Viresh Kumar349d39d2017-04-25 15:57:19 +0530201 * or -ENOMEM if we run out of memory.
Javi Merinoc36cf072015-02-26 19:00:29 +0000202 */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530203static int update_freq_table(struct cpufreq_cooling_device *cpufreq_cdev,
204 u32 capacitance)
Javi Merinoc36cf072015-02-26 19:00:29 +0000205{
Viresh Kumar349d39d2017-04-25 15:57:19 +0530206 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000207 struct dev_pm_opp *opp;
208 struct device *dev = NULL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530209 int num_opps = 0, cpu = cpufreq_cdev->policy->cpu, i;
Javi Merinoc36cf072015-02-26 19:00:29 +0000210
Viresh Kumar02bacb22017-04-25 15:57:17 +0530211 dev = get_cpu_device(cpu);
212 if (unlikely(!dev)) {
213 dev_warn(&cpufreq_cdev->cdev->device,
214 "No cpu device for cpu %d\n", cpu);
215 return -ENODEV;
Javi Merinoc36cf072015-02-26 19:00:29 +0000216 }
217
Viresh Kumar02bacb22017-04-25 15:57:17 +0530218 num_opps = dev_pm_opp_get_opp_count(dev);
219 if (num_opps < 0)
220 return num_opps;
221
Viresh Kumar349d39d2017-04-25 15:57:19 +0530222 /*
223 * The cpufreq table is also built from the OPP table and so the count
224 * should match.
225 */
226 if (num_opps != cpufreq_cdev->max_level + 1) {
227 dev_warn(dev, "Number of OPPs not matching with max_levels\n");
Javi Merino459ac372015-08-17 19:21:42 +0100228 return -EINVAL;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530229 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000230
Viresh Kumar349d39d2017-04-25 15:57:19 +0530231 for (i = 0; i <= cpufreq_cdev->max_level; i++) {
232 unsigned long freq = freq_table[i].frequency * 1000;
233 u32 freq_mhz = freq_table[i].frequency / 1000;
Javi Merinoc36cf072015-02-26 19:00:29 +0000234 u64 power;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530235 u32 voltage_mv;
Javi Merinoc36cf072015-02-26 19:00:29 +0000236
Viresh Kumar349d39d2017-04-25 15:57:19 +0530237 /*
238 * Find ceil frequency as 'freq' may be slightly lower than OPP
239 * freq due to truncation while converting to kHz.
240 */
241 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
242 if (IS_ERR(opp)) {
243 dev_err(dev, "failed to get opp for %lu frequency\n",
244 freq);
245 return -EINVAL;
Javi Merino459ac372015-08-17 19:21:42 +0100246 }
247
Javi Merinoc36cf072015-02-26 19:00:29 +0000248 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530249 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000250
251 /*
252 * Do the multiplication with MHz and millivolt so as
253 * to not overflow.
254 */
255 power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
256 do_div(power, 1000000000);
257
Javi Merinoc36cf072015-02-26 19:00:29 +0000258 /* power is stored in mW */
Viresh Kumar349d39d2017-04-25 15:57:19 +0530259 freq_table[i].power = power;
Javi Merinoeba4f882015-08-17 19:21:43 +0100260 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000261
Viresh Kumar1dea4322017-04-25 15:57:10 +0530262 cpufreq_cdev->cpu_dev = dev;
Javi Merinoc36cf072015-02-26 19:00:29 +0000263
Javi Merino459ac372015-08-17 19:21:42 +0100264 return 0;
Javi Merinoc36cf072015-02-26 19:00:29 +0000265}
266
Viresh Kumar1dea4322017-04-25 15:57:10 +0530267static u32 cpu_freq_to_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000268 u32 freq)
269{
270 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530271 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000272
Viresh Kumar349d39d2017-04-25 15:57:19 +0530273 for (i = 1; i <= cpufreq_cdev->max_level; i++)
274 if (freq > freq_table[i].frequency)
Javi Merinoc36cf072015-02-26 19:00:29 +0000275 break;
276
Viresh Kumar349d39d2017-04-25 15:57:19 +0530277 return freq_table[i - 1].power;
Javi Merinoc36cf072015-02-26 19:00:29 +0000278}
279
Viresh Kumar1dea4322017-04-25 15:57:10 +0530280static u32 cpu_power_to_freq(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000281 u32 power)
282{
283 int i;
Viresh Kumar349d39d2017-04-25 15:57:19 +0530284 struct freq_table *freq_table = cpufreq_cdev->freq_table;
Javi Merinoc36cf072015-02-26 19:00:29 +0000285
Viresh Kumar349d39d2017-04-25 15:57:19 +0530286 for (i = 1; i <= cpufreq_cdev->max_level; i++)
287 if (power > freq_table[i].power)
Javi Merinoc36cf072015-02-26 19:00:29 +0000288 break;
289
Viresh Kumar349d39d2017-04-25 15:57:19 +0530290 return freq_table[i - 1].frequency;
Javi Merinoc36cf072015-02-26 19:00:29 +0000291}
292
293/**
294 * get_load() - get load for a cpu since last updated
Viresh Kumar1dea4322017-04-25 15:57:10 +0530295 * @cpufreq_cdev: &struct cpufreq_cooling_device for this cpu
Javi Merinoc36cf072015-02-26 19:00:29 +0000296 * @cpu: cpu number
Viresh Kumarba76dd92017-04-25 15:57:18 +0530297 * @cpu_idx: index of the cpu in time_in_idle*
Javi Merinoc36cf072015-02-26 19:00:29 +0000298 *
299 * Return: The average load of cpu @cpu in percentage since this
300 * function was last called.
301 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530302static u32 get_load(struct cpufreq_cooling_device *cpufreq_cdev, int cpu,
Javi Merinoa53b8392016-02-11 12:00:51 +0000303 int cpu_idx)
Javi Merinoc36cf072015-02-26 19:00:29 +0000304{
305 u32 load;
306 u64 now, now_idle, delta_time, delta_idle;
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530307 struct time_in_idle *idle_time = &cpufreq_cdev->idle_time[cpu_idx];
Javi Merinoc36cf072015-02-26 19:00:29 +0000308
309 now_idle = get_cpu_idle_time(cpu, &now, 0);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530310 delta_idle = now_idle - idle_time->time;
311 delta_time = now - idle_time->timestamp;
Javi Merinoc36cf072015-02-26 19:00:29 +0000312
313 if (delta_time <= delta_idle)
314 load = 0;
315 else
316 load = div64_u64(100 * (delta_time - delta_idle), delta_time);
317
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530318 idle_time->time = now_idle;
319 idle_time->timestamp = now;
Javi Merinoc36cf072015-02-26 19:00:29 +0000320
321 return load;
322}
323
324/**
325 * get_static_power() - calculate the static power consumed by the cpus
Viresh Kumar1dea4322017-04-25 15:57:10 +0530326 * @cpufreq_cdev: struct &cpufreq_cooling_device for this cpu cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000327 * @tz: thermal zone device in which we're operating
328 * @freq: frequency in KHz
329 * @power: pointer in which to store the calculated static power
330 *
331 * Calculate the static power consumed by the cpus described by
332 * @cpu_actor running at frequency @freq. This function relies on a
333 * platform specific function that should have been provided when the
334 * actor was registered. If it wasn't, the static power is assumed to
335 * be negligible. The calculated static power is stored in @power.
336 *
337 * Return: 0 on success, -E* on failure.
338 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530339static int get_static_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000340 struct thermal_zone_device *tz, unsigned long freq,
341 u32 *power)
342{
343 struct dev_pm_opp *opp;
344 unsigned long voltage;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530345 struct cpumask *cpumask = cpufreq_cdev->policy->related_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000346 unsigned long freq_hz = freq * 1000;
347
Viresh Kumar1dea4322017-04-25 15:57:10 +0530348 if (!cpufreq_cdev->plat_get_static_power || !cpufreq_cdev->cpu_dev) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000349 *power = 0;
350 return 0;
351 }
352
Viresh Kumar1dea4322017-04-25 15:57:10 +0530353 opp = dev_pm_opp_find_freq_exact(cpufreq_cdev->cpu_dev, freq_hz,
Javi Merinoc36cf072015-02-26 19:00:29 +0000354 true);
Viresh Kumar3ea32172017-02-07 09:40:05 +0530355 if (IS_ERR(opp)) {
Viresh Kumar1dea4322017-04-25 15:57:10 +0530356 dev_warn_ratelimited(cpufreq_cdev->cpu_dev,
Viresh Kumar3ea32172017-02-07 09:40:05 +0530357 "Failed to find OPP for frequency %lu: %ld\n",
358 freq_hz, PTR_ERR(opp));
359 return -EINVAL;
360 }
361
Javi Merinoc36cf072015-02-26 19:00:29 +0000362 voltage = dev_pm_opp_get_voltage(opp);
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530363 dev_pm_opp_put(opp);
Javi Merinoc36cf072015-02-26 19:00:29 +0000364
365 if (voltage == 0) {
Viresh Kumar1dea4322017-04-25 15:57:10 +0530366 dev_err_ratelimited(cpufreq_cdev->cpu_dev,
Viresh Kumar3ea32172017-02-07 09:40:05 +0530367 "Failed to get voltage for frequency %lu\n",
368 freq_hz);
Javi Merinoc36cf072015-02-26 19:00:29 +0000369 return -EINVAL;
370 }
371
Viresh Kumar1dea4322017-04-25 15:57:10 +0530372 return cpufreq_cdev->plat_get_static_power(cpumask, tz->passive_delay,
373 voltage, power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000374}
375
376/**
377 * get_dynamic_power() - calculate the dynamic power
Viresh Kumar1dea4322017-04-25 15:57:10 +0530378 * @cpufreq_cdev: &cpufreq_cooling_device for this cdev
Javi Merinoc36cf072015-02-26 19:00:29 +0000379 * @freq: current frequency
380 *
381 * Return: the dynamic power consumed by the cpus described by
Viresh Kumar1dea4322017-04-25 15:57:10 +0530382 * @cpufreq_cdev.
Javi Merinoc36cf072015-02-26 19:00:29 +0000383 */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530384static u32 get_dynamic_power(struct cpufreq_cooling_device *cpufreq_cdev,
Javi Merinoc36cf072015-02-26 19:00:29 +0000385 unsigned long freq)
386{
387 u32 raw_cpu_power;
388
Viresh Kumar1dea4322017-04-25 15:57:10 +0530389 raw_cpu_power = cpu_freq_to_power(cpufreq_cdev, freq);
390 return (raw_cpu_power * cpufreq_cdev->last_load) / 100;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530391}
392
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000393/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530394
395/**
396 * cpufreq_get_max_state - callback function to get the max cooling state.
397 * @cdev: thermal cooling device pointer.
398 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000399 *
400 * Callback for the thermal cooling device to return the cpufreq
401 * max cooling state.
402 *
403 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530404 */
405static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
406 unsigned long *state)
407{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530408 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530409
Viresh Kumar1dea4322017-04-25 15:57:10 +0530410 *state = cpufreq_cdev->max_level;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530411 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530412}
413
414/**
415 * cpufreq_get_cur_state - callback function to get the current cooling state.
416 * @cdev: thermal cooling device pointer.
417 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000418 *
419 * Callback for the thermal cooling device to return the cpufreq
420 * current cooling state.
421 *
422 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530423 */
424static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
425 unsigned long *state)
426{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530427 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530428
Viresh Kumar1dea4322017-04-25 15:57:10 +0530429 *state = cpufreq_cdev->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000430
hongbo.zhang160b7d82012-10-30 17:48:59 +0100431 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530432}
433
434/**
435 * cpufreq_set_cur_state - callback function to set the current cooling state.
436 * @cdev: thermal cooling device pointer.
437 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000438 *
439 * Callback for the thermal cooling device to change the cpufreq
440 * current cooling state.
441 *
442 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530443 */
444static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
445 unsigned long state)
446{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530447 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530448 unsigned int clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530449
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530450 /* Request state should be less than max_level */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530451 if (WARN_ON(state > cpufreq_cdev->max_level))
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530452 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530453
Viresh Kumar5194fe42014-12-04 09:42:00 +0530454 /* Check if the old cooling action is same as new cooling action */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530455 if (cpufreq_cdev->cpufreq_state == state)
Viresh Kumar5194fe42014-12-04 09:42:00 +0530456 return 0;
457
Viresh Kumar349d39d2017-04-25 15:57:19 +0530458 clip_freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530459 cpufreq_cdev->cpufreq_state = state;
460 cpufreq_cdev->clipped_freq = clip_freq;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530461
Viresh Kumarba76dd92017-04-25 15:57:18 +0530462 cpufreq_update_policy(cpufreq_cdev->policy->cpu);
Viresh Kumar5194fe42014-12-04 09:42:00 +0530463
464 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530465}
466
Javi Merinoc36cf072015-02-26 19:00:29 +0000467/**
468 * cpufreq_get_requested_power() - get the current power
469 * @cdev: &thermal_cooling_device pointer
470 * @tz: a valid thermal zone device pointer
471 * @power: pointer in which to store the resulting power
472 *
473 * Calculate the current power consumption of the cpus in milliwatts
474 * and store it in @power. This function should actually calculate
475 * the requested power, but it's hard to get the frequency that
476 * cpufreq would have assigned if there were no thermal limits.
477 * Instead, we calculate the current power on the assumption that the
478 * immediate future will look like the immediate past.
479 *
480 * We use the current frequency and the average load since this
481 * function was last called. In reality, there could have been
482 * multiple opps since this function was last called and that affects
483 * the load calculation. While it's not perfectly accurate, this
484 * simplification is good enough and works. REVISIT this, as more
485 * complex code may be needed if experiments show that it's not
486 * accurate enough.
487 *
488 * Return: 0 on success, -E* if getting the static power failed.
489 */
490static int cpufreq_get_requested_power(struct thermal_cooling_device *cdev,
491 struct thermal_zone_device *tz,
492 u32 *power)
493{
494 unsigned long freq;
Javi Merino6828a472015-03-02 17:17:20 +0000495 int i = 0, cpu, ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000496 u32 static_power, dynamic_power, total_load = 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530497 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530498 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merino6828a472015-03-02 17:17:20 +0000499 u32 *load_cpu = NULL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000500
Viresh Kumarba76dd92017-04-25 15:57:18 +0530501 freq = cpufreq_quick_get(policy->cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000502
Javi Merino6828a472015-03-02 17:17:20 +0000503 if (trace_thermal_power_cpu_get_power_enabled()) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530504 u32 ncpus = cpumask_weight(policy->related_cpus);
Javi Merino6828a472015-03-02 17:17:20 +0000505
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530506 load_cpu = kcalloc(ncpus, sizeof(*load_cpu), GFP_KERNEL);
Javi Merino6828a472015-03-02 17:17:20 +0000507 }
508
Viresh Kumarba76dd92017-04-25 15:57:18 +0530509 for_each_cpu(cpu, policy->related_cpus) {
Javi Merinoc36cf072015-02-26 19:00:29 +0000510 u32 load;
511
512 if (cpu_online(cpu))
Viresh Kumar1dea4322017-04-25 15:57:10 +0530513 load = get_load(cpufreq_cdev, cpu, i);
Javi Merinoc36cf072015-02-26 19:00:29 +0000514 else
515 load = 0;
516
517 total_load += load;
Javi Merino6828a472015-03-02 17:17:20 +0000518 if (trace_thermal_power_cpu_limit_enabled() && load_cpu)
519 load_cpu[i] = load;
520
521 i++;
Javi Merinoc36cf072015-02-26 19:00:29 +0000522 }
523
Viresh Kumar1dea4322017-04-25 15:57:10 +0530524 cpufreq_cdev->last_load = total_load;
Javi Merinoc36cf072015-02-26 19:00:29 +0000525
Viresh Kumar1dea4322017-04-25 15:57:10 +0530526 dynamic_power = get_dynamic_power(cpufreq_cdev, freq);
527 ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000528 if (ret) {
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530529 kfree(load_cpu);
Javi Merinoc36cf072015-02-26 19:00:29 +0000530 return ret;
Javi Merino6828a472015-03-02 17:17:20 +0000531 }
532
533 if (load_cpu) {
Viresh Kumarba76dd92017-04-25 15:57:18 +0530534 trace_thermal_power_cpu_get_power(policy->related_cpus, freq,
535 load_cpu, i, dynamic_power,
536 static_power);
Javi Merino6828a472015-03-02 17:17:20 +0000537
Vaishali Thakkara71544c2015-08-19 11:52:19 +0530538 kfree(load_cpu);
Javi Merino6828a472015-03-02 17:17:20 +0000539 }
Javi Merinoc36cf072015-02-26 19:00:29 +0000540
541 *power = static_power + dynamic_power;
542 return 0;
543}
544
545/**
546 * cpufreq_state2power() - convert a cpu cdev state to power consumed
547 * @cdev: &thermal_cooling_device pointer
548 * @tz: a valid thermal zone device pointer
549 * @state: cooling device state to be converted
550 * @power: pointer in which to store the resulting power
551 *
552 * Convert cooling device state @state into power consumption in
553 * milliwatts assuming 100% load. Store the calculated power in
554 * @power.
555 *
556 * Return: 0 on success, -EINVAL if the cooling device state could not
557 * be converted into a frequency or other -E* if there was an error
558 * when calculating the static power.
559 */
560static int cpufreq_state2power(struct thermal_cooling_device *cdev,
561 struct thermal_zone_device *tz,
562 unsigned long state, u32 *power)
563{
564 unsigned int freq, num_cpus;
Javi Merinoc36cf072015-02-26 19:00:29 +0000565 u32 static_power, dynamic_power;
566 int ret;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530567 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Javi Merinoc36cf072015-02-26 19:00:29 +0000568
Viresh Kumarba76dd92017-04-25 15:57:18 +0530569 num_cpus = cpumask_weight(cpufreq_cdev->policy->cpus);
Javi Merinoc36cf072015-02-26 19:00:29 +0000570
Viresh Kumar349d39d2017-04-25 15:57:19 +0530571 freq = cpufreq_cdev->freq_table[state].frequency;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530572 if (!freq)
573 return -EINVAL;
Javi Merinoc36cf072015-02-26 19:00:29 +0000574
Viresh Kumar1dea4322017-04-25 15:57:10 +0530575 dynamic_power = cpu_freq_to_power(cpufreq_cdev, freq) * num_cpus;
576 ret = get_static_power(cpufreq_cdev, tz, freq, &static_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000577 if (ret)
Viresh Kumarba76dd92017-04-25 15:57:18 +0530578 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000579
580 *power = static_power + dynamic_power;
Arnd Bergmannd9cc34a2017-02-02 15:46:26 +0100581 return ret;
Javi Merinoc36cf072015-02-26 19:00:29 +0000582}
583
584/**
585 * cpufreq_power2state() - convert power to a cooling device state
586 * @cdev: &thermal_cooling_device pointer
587 * @tz: a valid thermal zone device pointer
588 * @power: power in milliwatts to be converted
589 * @state: pointer in which to store the resulting state
590 *
591 * Calculate a cooling device state for the cpus described by @cdev
592 * that would allow them to consume at most @power mW and store it in
593 * @state. Note that this calculation depends on external factors
594 * such as the cpu load or the current static power. Calling this
595 * function with the same power as input can yield different cooling
596 * device states depending on those external factors.
597 *
598 * Return: 0 on success, -ENODEV if no cpus are online or -EINVAL if
599 * the calculated frequency could not be converted to a valid state.
600 * The latter should not happen unless the frequencies available to
601 * cpufreq have changed since the initialization of the cpu cooling
602 * device.
603 */
604static int cpufreq_power2state(struct thermal_cooling_device *cdev,
605 struct thermal_zone_device *tz, u32 power,
606 unsigned long *state)
607{
Viresh Kumarba76dd92017-04-25 15:57:18 +0530608 unsigned int cur_freq, target_freq;
Javi Merinoc36cf072015-02-26 19:00:29 +0000609 int ret;
610 s32 dyn_power;
611 u32 last_load, normalised_power, static_power;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530612 struct cpufreq_cooling_device *cpufreq_cdev = cdev->devdata;
Viresh Kumarba76dd92017-04-25 15:57:18 +0530613 struct cpufreq_policy *policy = cpufreq_cdev->policy;
Javi Merinoc36cf072015-02-26 19:00:29 +0000614
Viresh Kumarba76dd92017-04-25 15:57:18 +0530615 cur_freq = cpufreq_quick_get(policy->cpu);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530616 ret = get_static_power(cpufreq_cdev, tz, cur_freq, &static_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000617 if (ret)
618 return ret;
619
620 dyn_power = power - static_power;
621 dyn_power = dyn_power > 0 ? dyn_power : 0;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530622 last_load = cpufreq_cdev->last_load ?: 1;
Javi Merinoc36cf072015-02-26 19:00:29 +0000623 normalised_power = (dyn_power * 100) / last_load;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530624 target_freq = cpu_power_to_freq(cpufreq_cdev, normalised_power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000625
Viresh Kumar3e08b2d2017-04-25 15:57:12 +0530626 *state = get_level(cpufreq_cdev, target_freq);
Viresh Kumarba76dd92017-04-25 15:57:18 +0530627 trace_thermal_power_cpu_limit(policy->related_cpus, target_freq, *state,
628 power);
Javi Merinoc36cf072015-02-26 19:00:29 +0000629 return 0;
630}
631
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530632/* Bind cpufreq callbacks to thermal cooling device ops */
Brendan Jackmana305a432016-08-17 16:14:59 +0100633
Javi Merinoc36cf072015-02-26 19:00:29 +0000634static struct thermal_cooling_device_ops cpufreq_cooling_ops = {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530635 .get_max_state = cpufreq_get_max_state,
636 .get_cur_state = cpufreq_get_cur_state,
637 .set_cur_state = cpufreq_set_cur_state,
638};
639
Brendan Jackmana305a432016-08-17 16:14:59 +0100640static struct thermal_cooling_device_ops cpufreq_power_cooling_ops = {
641 .get_max_state = cpufreq_get_max_state,
642 .get_cur_state = cpufreq_get_cur_state,
643 .set_cur_state = cpufreq_set_cur_state,
644 .get_requested_power = cpufreq_get_requested_power,
645 .state2power = cpufreq_state2power,
646 .power2state = cpufreq_power2state,
647};
648
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530649/* Notifier for cpufreq policy change */
650static struct notifier_block thermal_cpufreq_notifier_block = {
651 .notifier_call = cpufreq_thermal_notifier,
652};
653
Viresh Kumarf6859012014-12-04 09:42:06 +0530654static unsigned int find_next_max(struct cpufreq_frequency_table *table,
655 unsigned int prev_max)
656{
657 struct cpufreq_frequency_table *pos;
658 unsigned int max = 0;
659
660 cpufreq_for_each_valid_entry(pos, table) {
661 if (pos->frequency > max && pos->frequency < prev_max)
662 max = pos->frequency;
663 }
664
665 return max;
666}
667
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530668/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400669 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
670 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530671 * @policy: cpufreq policy
Viresh Kumar405fb822014-12-04 09:41:55 +0530672 * Normally this should be same as cpufreq policy->related_cpus.
Javi Merinoc36cf072015-02-26 19:00:29 +0000673 * @capacitance: dynamic power coefficient for these cpus
674 * @plat_static_func: function to calculate the static power consumed by these
675 * cpus (optional)
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000676 *
677 * This interface function registers the cpufreq cooling device with the name
678 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400679 * cooling devices. It also gives the opportunity to link the cooling device
680 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000681 *
682 * Return: a valid struct thermal_cooling_device pointer on success,
683 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530684 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400685static struct thermal_cooling_device *
686__cpufreq_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530687 struct cpufreq_policy *policy, u32 capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000688 get_static_t plat_static_func)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530689{
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530690 struct thermal_cooling_device *cdev;
Viresh Kumar1dea4322017-04-25 15:57:10 +0530691 struct cpufreq_cooling_device *cpufreq_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530692 char dev_name[THERMAL_NAME_LENGTH];
Javi Merinoc36cf072015-02-26 19:00:29 +0000693 unsigned int freq, i, num_cpus;
Viresh Kumar405fb822014-12-04 09:41:55 +0530694 int ret;
Brendan Jackmana305a432016-08-17 16:14:59 +0100695 struct thermal_cooling_device_ops *cooling_ops;
Matthew Wilcox088db932017-03-10 18:33:28 +0000696 bool first;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530697
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530698 if (IS_ERR_OR_NULL(policy)) {
699 pr_err("%s: cpufreq policy isn't valid: %p", __func__, policy);
700 return ERR_PTR(-EINVAL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530701 }
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530702
Viresh Kumar55d85292017-04-25 15:57:15 +0530703 i = cpufreq_table_count_valid_entries(policy);
704 if (!i) {
705 pr_debug("%s: CPUFreq table not found or has no valid entries\n",
706 __func__);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530707 return ERR_PTR(-ENODEV);
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530708 }
709
Viresh Kumar1dea4322017-04-25 15:57:10 +0530710 cpufreq_cdev = kzalloc(sizeof(*cpufreq_cdev), GFP_KERNEL);
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530711 if (!cpufreq_cdev)
712 return ERR_PTR(-ENOMEM);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530713
Viresh Kumarb12b6512017-04-25 15:57:16 +0530714 cpufreq_cdev->policy = policy;
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530715 num_cpus = cpumask_weight(policy->related_cpus);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530716 cpufreq_cdev->idle_time = kcalloc(num_cpus,
717 sizeof(*cpufreq_cdev->idle_time),
718 GFP_KERNEL);
719 if (!cpufreq_cdev->idle_time) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530720 cdev = ERR_PTR(-ENOMEM);
Javi Merinoc36cf072015-02-26 19:00:29 +0000721 goto free_cdev;
722 }
723
Viresh Kumar55d85292017-04-25 15:57:15 +0530724 /* max_level is an index, not a counter */
725 cpufreq_cdev->max_level = i - 1;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530726
Viresh Kumar55d85292017-04-25 15:57:15 +0530727 cpufreq_cdev->freq_table = kmalloc(sizeof(*cpufreq_cdev->freq_table) * i,
728 GFP_KERNEL);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530729 if (!cpufreq_cdev->freq_table) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530730 cdev = ERR_PTR(-ENOMEM);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530731 goto free_idle_time;
Viresh Kumarf6859012014-12-04 09:42:06 +0530732 }
733
Matthew Wilcoxae606082016-12-21 09:47:05 -0800734 ret = ida_simple_get(&cpufreq_ida, 0, 0, GFP_KERNEL);
735 if (ret < 0) {
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530736 cdev = ERR_PTR(ret);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530737 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530738 }
Viresh Kumar1dea4322017-04-25 15:57:10 +0530739 cpufreq_cdev->id = ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530740
Viresh Kumar349d39d2017-04-25 15:57:19 +0530741 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
742 cpufreq_cdev->id);
743
Viresh Kumarf6859012014-12-04 09:42:06 +0530744 /* Fill freq-table in descending order of frequencies */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530745 for (i = 0, freq = -1; i <= cpufreq_cdev->max_level; i++) {
Viresh Kumar55d85292017-04-25 15:57:15 +0530746 freq = find_next_max(policy->freq_table, freq);
Viresh Kumar349d39d2017-04-25 15:57:19 +0530747 cpufreq_cdev->freq_table[i].frequency = freq;
Viresh Kumarf6859012014-12-04 09:42:06 +0530748
749 /* Warn for duplicate entries */
750 if (!freq)
751 pr_warn("%s: table has duplicate entries\n", __func__);
752 else
753 pr_debug("%s: freq:%u KHz\n", __func__, freq);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530754 }
Viresh Kumarf6859012014-12-04 09:42:06 +0530755
Viresh Kumar349d39d2017-04-25 15:57:19 +0530756 if (capacitance) {
757 cpufreq_cdev->plat_get_static_power = plat_static_func;
758
759 ret = update_freq_table(cpufreq_cdev, capacitance);
760 if (ret) {
761 cdev = ERR_PTR(ret);
762 goto remove_ida;
763 }
764
765 cooling_ops = &cpufreq_power_cooling_ops;
766 } else {
767 cooling_ops = &cpufreq_cooling_ops;
768 }
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100769
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530770 cdev = thermal_of_cooling_device_register(np, dev_name, cpufreq_cdev,
771 cooling_ops);
772 if (IS_ERR(cdev))
Matthew Wilcoxae606082016-12-21 09:47:05 -0800773 goto remove_ida;
Lukasz Lubaf840ab12016-05-31 11:32:02 +0100774
Viresh Kumar349d39d2017-04-25 15:57:19 +0530775 cpufreq_cdev->clipped_freq = cpufreq_cdev->freq_table[0].frequency;
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530776 cpufreq_cdev->cdev = cdev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530777
Russell King02373d72015-08-12 15:22:16 +0530778 mutex_lock(&cooling_list_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530779 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530780 first = list_empty(&cpufreq_cdev_list);
781 list_add(&cpufreq_cdev->node, &cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000782 mutex_unlock(&cooling_list_lock);
783
784 if (first)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530785 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000786 CPUFREQ_POLICY_NOTIFIER);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000787
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530788 return cdev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530789
Matthew Wilcoxae606082016-12-21 09:47:05 -0800790remove_ida:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530791 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530792free_table:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530793 kfree(cpufreq_cdev->freq_table);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530794free_idle_time:
795 kfree(cpufreq_cdev->idle_time);
Viresh Kumar730abe02014-12-04 09:41:58 +0530796free_cdev:
Viresh Kumar1dea4322017-04-25 15:57:10 +0530797 kfree(cpufreq_cdev);
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530798 return cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530799}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400800
801/**
802 * cpufreq_cooling_register - function to create cpufreq cooling device.
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530803 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400804 *
805 * This interface function registers the cpufreq cooling device with the name
806 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
807 * cooling devices.
808 *
809 * Return: a valid struct thermal_cooling_device pointer on success,
810 * on failure, it returns a corresponding ERR_PTR().
811 */
812struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530813cpufreq_cooling_register(struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400814{
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530815 return __cpufreq_cooling_register(NULL, policy, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400816}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000817EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530818
819/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400820 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
821 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530822 * @policy: cpufreq policy
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400823 *
824 * This interface function registers the cpufreq cooling device with the name
825 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
826 * cooling devices. Using this API, the cpufreq cooling device will be
827 * linked to the device tree node provided.
828 *
829 * Return: a valid struct thermal_cooling_device pointer on success,
830 * on failure, it returns a corresponding ERR_PTR().
831 */
832struct thermal_cooling_device *
833of_cpufreq_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530834 struct cpufreq_policy *policy)
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400835{
836 if (!np)
837 return ERR_PTR(-EINVAL);
838
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530839 return __cpufreq_cooling_register(np, policy, 0, NULL);
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400840}
841EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
842
843/**
Javi Merinoc36cf072015-02-26 19:00:29 +0000844 * cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530845 * @policy: cpufreq policy
Javi Merinoc36cf072015-02-26 19:00:29 +0000846 * @capacitance: dynamic power coefficient for these cpus
847 * @plat_static_func: function to calculate the static power consumed by these
848 * cpus (optional)
849 *
850 * This interface function registers the cpufreq cooling device with
851 * the name "thermal-cpufreq-%x". This api can support multiple
852 * instances of cpufreq cooling devices. Using this function, the
853 * cooling device will implement the power extensions by using a
854 * simple cpu power model. The cpus must have registered their OPPs
855 * using the OPP library.
856 *
857 * An optional @plat_static_func may be provided to calculate the
858 * static power consumed by these cpus. If the platform's static
859 * power consumption is unknown or negligible, make it NULL.
860 *
861 * Return: a valid struct thermal_cooling_device pointer on success,
862 * on failure, it returns a corresponding ERR_PTR().
863 */
864struct thermal_cooling_device *
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530865cpufreq_power_cooling_register(struct cpufreq_policy *policy, u32 capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000866 get_static_t plat_static_func)
867{
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530868 return __cpufreq_cooling_register(NULL, policy, capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000869 plat_static_func);
870}
871EXPORT_SYMBOL(cpufreq_power_cooling_register);
872
873/**
874 * of_cpufreq_power_cooling_register() - create cpufreq cooling device with power extensions
875 * @np: a valid struct device_node to the cooling device device tree node
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530876 * @policy: cpufreq policy
Javi Merinoc36cf072015-02-26 19:00:29 +0000877 * @capacitance: dynamic power coefficient for these cpus
878 * @plat_static_func: function to calculate the static power consumed by these
879 * cpus (optional)
880 *
881 * This interface function registers the cpufreq cooling device with
882 * the name "thermal-cpufreq-%x". This api can support multiple
883 * instances of cpufreq cooling devices. Using this API, the cpufreq
884 * cooling device will be linked to the device tree node provided.
885 * Using this function, the cooling device will implement the power
886 * extensions by using a simple cpu power model. The cpus must have
887 * registered their OPPs using the OPP library.
888 *
889 * An optional @plat_static_func may be provided to calculate the
890 * static power consumed by these cpus. If the platform's static
891 * power consumption is unknown or negligible, make it NULL.
892 *
893 * Return: a valid struct thermal_cooling_device pointer on success,
894 * on failure, it returns a corresponding ERR_PTR().
895 */
896struct thermal_cooling_device *
897of_cpufreq_power_cooling_register(struct device_node *np,
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530898 struct cpufreq_policy *policy,
Javi Merinoc36cf072015-02-26 19:00:29 +0000899 u32 capacitance,
900 get_static_t plat_static_func)
901{
902 if (!np)
903 return ERR_PTR(-EINVAL);
904
Viresh Kumar4d753aa2017-04-25 15:57:14 +0530905 return __cpufreq_cooling_register(np, policy, capacitance,
Javi Merinoc36cf072015-02-26 19:00:29 +0000906 plat_static_func);
907}
908EXPORT_SYMBOL(of_cpufreq_power_cooling_register);
909
910/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530911 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
912 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000913 *
914 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530915 */
916void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
917{
Viresh Kumar1dea4322017-04-25 15:57:10 +0530918 struct cpufreq_cooling_device *cpufreq_cdev;
Matthew Wilcox088db932017-03-10 18:33:28 +0000919 bool last;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530920
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400921 if (!cdev)
922 return;
923
Viresh Kumar1dea4322017-04-25 15:57:10 +0530924 cpufreq_cdev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530925
Matthew Wilcoxae606082016-12-21 09:47:05 -0800926 mutex_lock(&cooling_list_lock);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530927 list_del(&cpufreq_cdev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530928 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar1dea4322017-04-25 15:57:10 +0530929 last = list_empty(&cpufreq_cdev_list);
Matthew Wilcox088db932017-03-10 18:33:28 +0000930 mutex_unlock(&cooling_list_lock);
931
932 if (last)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530933 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000934 CPUFREQ_POLICY_NOTIFIER);
Russell King02373d72015-08-12 15:22:16 +0530935
Viresh Kumar04bdbdf2017-04-25 15:57:11 +0530936 thermal_cooling_device_unregister(cpufreq_cdev->cdev);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530937 ida_simple_remove(&cpufreq_ida, cpufreq_cdev->id);
Viresh Kumar81ee14d2017-04-25 15:57:20 +0530938 kfree(cpufreq_cdev->idle_time);
Viresh Kumar1dea4322017-04-25 15:57:10 +0530939 kfree(cpufreq_cdev->freq_table);
940 kfree(cpufreq_cdev);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530941}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000942EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);