blob: f32573818db9c4005a3aa1e8eeb87a39e9672bb1 [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 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 *
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053023#include <linux/module.h>
24#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053025#include <linux/cpufreq.h>
26#include <linux/err.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/cpu_cooling.h>
30
Viresh Kumar07d888d2014-12-04 09:41:49 +053031/*
32 * Cooling state <-> CPUFreq frequency
33 *
34 * Cooling states are translated to frequencies throughout this driver and this
35 * is the relation between them.
36 *
37 * Highest cooling state corresponds to lowest possible frequency.
38 *
39 * i.e.
40 * level 0 --> 1st Max Freq
41 * level 1 --> 2nd Max Freq
42 * ...
43 */
44
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053045/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000046 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053047 * @id: unique integer value corresponding to each cpufreq_cooling_device
48 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000049 * @cool_dev: thermal_cooling_device pointer to keep track of the
50 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053051 * @cpufreq_state: integer value representing the current state of cpufreq
52 * cooling devices.
53 * @cpufreq_val: integer value representing the absolute value of the clipped
54 * frequency.
55 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053056 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053057 * This structure is required for keeping information of each registered
58 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053059 */
60struct cpufreq_cooling_device {
61 int id;
62 struct thermal_cooling_device *cool_dev;
63 unsigned int cpufreq_state;
64 unsigned int cpufreq_val;
65 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053066 struct list_head node;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053067};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053068static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +010069static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053070
hongbo.zhang160b7d82012-10-30 17:48:59 +010071static unsigned int cpufreq_dev_count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053072
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053073static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053074
75/**
76 * get_idr - function to get a unique id.
77 * @idr: struct idr * handle used to create a id.
78 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +000079 *
80 * This function will populate @id with an unique
81 * id, using the idr API.
82 *
83 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053084 */
85static int get_idr(struct idr *idr, int *id)
86{
Tejun Heo6deb69f2013-02-27 17:04:46 -080087 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053088
89 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080090 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053091 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080092 if (unlikely(ret < 0))
93 return ret;
94 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +000095
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053096 return 0;
97}
98
99/**
100 * release_idr - function to free the unique id.
101 * @idr: struct idr * handle used for creating the id.
102 * @id: int value representing the unique id.
103 */
104static void release_idr(struct idr *idr, int id)
105{
106 mutex_lock(&cooling_cpufreq_lock);
107 idr_remove(idr, id);
108 mutex_unlock(&cooling_cpufreq_lock);
109}
110
111/* Below code defines functions to be used for cpufreq as cooling device */
112
113/**
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000114 * is_cpufreq_valid - function to check frequency transitioning capability.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530115 * @cpu: cpu for which check is needed.
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000116 *
117 * This function will check the current state of the system if
118 * it is capable of changing the frequency for a given @cpu.
119 *
120 * Return: 0 if the system is not currently capable of changing
121 * the frequency of given cpu. !0 in case the frequency is changeable.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530122 */
123static int is_cpufreq_valid(int cpu)
124{
125 struct cpufreq_policy policy;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000126
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530127 return !cpufreq_get_policy(&policy, cpu);
128}
129
Zhang Ruifc35b352013-02-08 13:09:32 +0800130enum cpufreq_cooling_property {
131 GET_LEVEL,
132 GET_FREQ,
133 GET_MAXL,
134};
135
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000136/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530137 * get_property - fetch a property of interest for a given cpu.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000138 * @cpu: cpu for which the property is required
139 * @input: query parameter
140 * @output: query return
141 * @property: type of query (frequency, level, max level)
142 *
143 * This is the common function to
Zhang Ruifc35b352013-02-08 13:09:32 +0800144 * 1. get maximum cpu cooling states
145 * 2. translate frequency to cooling state
146 * 3. translate cooling state to frequency
Viresh Kumar728c03c2014-12-04 09:41:47 +0530147 *
Zhang Ruifc35b352013-02-08 13:09:32 +0800148 * Note that the code may be not in good shape
149 * but it is written in this way in order to:
150 * a) reduce duplicate code as most of the code can be shared.
151 * b) make sure the logic is consistent when translating between
152 * cooling states and frequencies.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000153 *
154 * Return: 0 on success, -EINVAL when invalid parameters are passed.
155 */
Zhang Ruifc35b352013-02-08 13:09:32 +0800156static int get_property(unsigned int cpu, unsigned long input,
Eduardo Valentind8892a02013-04-17 17:12:03 +0000157 unsigned int *output,
158 enum cpufreq_cooling_property property)
Zhang Ruifc35b352013-02-08 13:09:32 +0800159{
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300160 int i;
Eduardo Valentin4469b992013-04-17 17:11:58 +0000161 unsigned long max_level = 0, level = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800162 unsigned int freq = CPUFREQ_ENTRY_INVALID;
163 int descend = -1;
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300164 struct cpufreq_frequency_table *pos, *table =
Zhang Ruifc35b352013-02-08 13:09:32 +0800165 cpufreq_frequency_get_table(cpu);
Eduardo Valentin79d26402013-04-17 17:11:55 +0000166
Zhang Ruifc35b352013-02-08 13:09:32 +0800167 if (!output)
168 return -EINVAL;
169
170 if (!table)
171 return -EINVAL;
172
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300173 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800174 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300175 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800176 continue;
177
178 /* get the frequency order */
Shawn Guo24c7a382013-05-28 06:22:32 +0000179 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300180 descend = freq > pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800181
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300182 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800183 max_level++;
184 }
Zhang Ruia1167762014-01-02 11:57:48 +0800185
186 /* No valid cpu frequency entry */
187 if (max_level == 0)
188 return -EINVAL;
189
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400190 /* max_level is an index, not a counter */
191 max_level--;
Zhang Ruifc35b352013-02-08 13:09:32 +0800192
193 /* get max level */
194 if (property == GET_MAXL) {
195 *output = (unsigned int)max_level;
196 return 0;
197 }
198
199 if (property == GET_FREQ)
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400200 level = descend ? input : (max_level - input);
Zhang Ruifc35b352013-02-08 13:09:32 +0800201
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300202 i = 0;
203 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800204 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300205 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800206 continue;
207
208 /* now we have a valid frequency entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300209 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800210
211 if (property == GET_LEVEL && (unsigned int)input == freq) {
212 /* get level by frequency */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300213 *output = descend ? i : (max_level - i);
Zhang Ruifc35b352013-02-08 13:09:32 +0800214 return 0;
215 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300216 if (property == GET_FREQ && level == i) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800217 /* get frequency by level */
218 *output = freq;
219 return 0;
220 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300221 i++;
Zhang Ruifc35b352013-02-08 13:09:32 +0800222 }
Eduardo Valentin79491e52013-04-17 17:11:59 +0000223
Zhang Ruifc35b352013-02-08 13:09:32 +0800224 return -EINVAL;
225}
226
Eduardo Valentin44952d32013-04-17 17:12:05 +0000227/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530228 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000229 * @cpu: cpu for which the level is required
230 * @freq: the frequency of interest
231 *
232 * This function will match the cooling level corresponding to the
233 * requested @freq and return it.
234 *
235 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
236 * otherwise.
237 */
Zhang Rui57df8102013-02-08 14:52:06 +0800238unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
239{
240 unsigned int val;
241
242 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
243 return THERMAL_CSTATE_INVALID;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000244
Zhang Rui57df8102013-02-08 14:52:06 +0800245 return (unsigned long)val;
246}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000247EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800248
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530249/**
250 * get_cpu_frequency - get the absolute value of frequency from level.
251 * @cpu: cpu for which frequency is fetched.
Eduardo Valentin41518c42013-04-17 17:12:07 +0000252 * @level: cooling level
253 *
254 * This function matches cooling level with frequency. Based on a cooling level
255 * of frequency, equals cooling state of cpu cooling device, it will return
256 * the corresponding frequency.
Zhang Rui475f41c2013-02-06 09:34:32 +0800257 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
Eduardo Valentin41518c42013-04-17 17:12:07 +0000258 *
259 * Return: 0 on error, the corresponding frequency otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530260 */
261static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
262{
Zhang Ruifc35b352013-02-08 13:09:32 +0800263 int ret = 0;
264 unsigned int freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530265
Zhang Ruifc35b352013-02-08 13:09:32 +0800266 ret = get_property(cpu, level, &freq, GET_FREQ);
267 if (ret)
268 return 0;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000269
Zhang Ruifc35b352013-02-08 13:09:32 +0800270 return freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530271}
272
273/**
274 * cpufreq_apply_cooling - function to apply frequency clipping.
275 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
276 * clipping data.
277 * @cooling_state: value of the cooling state.
Eduardo Valentin4b33deb2013-04-17 17:12:08 +0000278 *
279 * Function used to make sure the cpufreq layer is aware of current thermal
280 * limits. The limits are applied by updating the cpufreq policy.
281 *
282 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
283 * cooling state).
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530284 */
285static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000286 unsigned long cooling_state)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530287{
288 unsigned int cpuid, clip_freq;
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000289 struct cpumask *mask = &cpufreq_device->allowed_cpus;
290 unsigned int cpu = cpumask_any(mask);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530291
292
293 /* Check if the old cooling action is same as new cooling action */
294 if (cpufreq_device->cpufreq_state == cooling_state)
295 return 0;
296
297 clip_freq = get_cpu_frequency(cpu, cooling_state);
298 if (!clip_freq)
299 return -EINVAL;
300
301 cpufreq_device->cpufreq_state = cooling_state;
302 cpufreq_device->cpufreq_val = clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530303
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000304 for_each_cpu(cpuid, mask) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530305 if (is_cpufreq_valid(cpuid))
306 cpufreq_update_policy(cpuid);
307 }
308
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530309 return 0;
310}
311
312/**
313 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
314 * @nb: struct notifier_block * with callback info.
315 * @event: value showing cpufreq event for which this function invoked.
316 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000317 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100318 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000319 * Every time there is a change in policy, we will intercept and
320 * update the cpufreq policy with thermal constraints.
321 *
322 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530323 */
324static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000325 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530326{
327 struct cpufreq_policy *policy = data;
328 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530329 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530330
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530331 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530332 return 0;
333
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530334 mutex_lock(&cooling_cpufreq_lock);
335 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
336 if (!cpumask_test_cpu(policy->cpu,
337 &cpufreq_dev->allowed_cpus))
338 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530339
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530340 if (!cpufreq_dev->cpufreq_val)
341 cpufreq_dev->cpufreq_val = get_cpu_frequency(
342 cpumask_any(&cpufreq_dev->allowed_cpus),
343 cpufreq_dev->cpufreq_state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530344
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530345 max_freq = cpufreq_dev->cpufreq_val;
346
347 if (policy->max != max_freq)
348 cpufreq_verify_within_limits(policy, 0, max_freq);
349 }
350 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530351
352 return 0;
353}
354
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000355/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530356
357/**
358 * cpufreq_get_max_state - callback function to get the max cooling state.
359 * @cdev: thermal cooling device pointer.
360 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000361 *
362 * Callback for the thermal cooling device to return the cpufreq
363 * max cooling state.
364 *
365 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530366 */
367static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
368 unsigned long *state)
369{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100370 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000371 struct cpumask *mask = &cpufreq_device->allowed_cpus;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530372 unsigned int cpu;
Dan Carpenter4f890382013-04-17 07:18:28 +0000373 unsigned int count = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800374 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530375
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000376 cpu = cpumask_any(mask);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530377
Dan Carpenter4f890382013-04-17 07:18:28 +0000378 ret = get_property(cpu, 0, &count, GET_MAXL);
hongbo.zhang9c51b052012-10-30 17:48:58 +0100379
Zhang Ruifc35b352013-02-08 13:09:32 +0800380 if (count > 0)
381 *state = count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530382
Zhang Ruifc35b352013-02-08 13:09:32 +0800383 return ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530384}
385
386/**
387 * cpufreq_get_cur_state - callback function to get the current cooling state.
388 * @cdev: thermal cooling device pointer.
389 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000390 *
391 * Callback for the thermal cooling device to return the cpufreq
392 * current cooling state.
393 *
394 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530395 */
396static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
397 unsigned long *state)
398{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100399 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530400
hongbo.zhang160b7d82012-10-30 17:48:59 +0100401 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000402
hongbo.zhang160b7d82012-10-30 17:48:59 +0100403 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530404}
405
406/**
407 * cpufreq_set_cur_state - callback function to set the current cooling state.
408 * @cdev: thermal cooling device pointer.
409 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000410 *
411 * Callback for the thermal cooling device to change the cpufreq
412 * current cooling state.
413 *
414 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530415 */
416static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
417 unsigned long state)
418{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100419 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530420
hongbo.zhang160b7d82012-10-30 17:48:59 +0100421 return cpufreq_apply_cooling(cpufreq_device, state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530422}
423
424/* Bind cpufreq callbacks to thermal cooling device ops */
425static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
426 .get_max_state = cpufreq_get_max_state,
427 .get_cur_state = cpufreq_get_cur_state,
428 .set_cur_state = cpufreq_set_cur_state,
429};
430
431/* Notifier for cpufreq policy change */
432static struct notifier_block thermal_cpufreq_notifier_block = {
433 .notifier_call = cpufreq_thermal_notifier,
434};
435
436/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400437 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
438 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530439 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530440 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000441 *
442 * This interface function registers the cpufreq cooling device with the name
443 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400444 * cooling devices. It also gives the opportunity to link the cooling device
445 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000446 *
447 * Return: a valid struct thermal_cooling_device pointer on success,
448 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530449 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400450static struct thermal_cooling_device *
451__cpufreq_cooling_register(struct device_node *np,
452 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530453{
454 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530455 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530456 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumar405fb822014-12-04 09:41:55 +0530457 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530458
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530459 if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) {
460 pr_debug("%s: CPUFreq table not found\n", __func__);
461 return ERR_PTR(-EPROBE_DEFER);
462 }
463
Viresh Kumar98d522f2014-12-04 09:41:50 +0530464 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530465 if (!cpufreq_dev)
466 return ERR_PTR(-ENOMEM);
467
468 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
469
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530470 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
471 if (ret) {
472 kfree(cpufreq_dev);
Viresh Kumar268ac442014-12-04 09:41:54 +0530473 return ERR_PTR(ret);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530474 }
475
Eduardo Valentin99871a72013-04-17 17:12:17 +0000476 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
477 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530478
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400479 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
480 &cpufreq_cooling_ops);
Wei Yongjun73b9bcd2013-10-25 21:55:42 +0800481 if (IS_ERR(cool_dev)) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530482 release_idr(&cpufreq_idr, cpufreq_dev->id);
483 kfree(cpufreq_dev);
Wei Yongjun73b9bcd2013-10-25 21:55:42 +0800484 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530485 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530486 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530487
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530488 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530489
490 /* Register the notifier for first cpufreq cooling device */
491 if (cpufreq_dev_count == 0)
492 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000493 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100494 cpufreq_dev_count++;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530495 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530496
497 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000498
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530499 return cool_dev;
500}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400501
502/**
503 * cpufreq_cooling_register - function to create cpufreq cooling device.
504 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
505 *
506 * This interface function registers the cpufreq cooling device with the name
507 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
508 * cooling devices.
509 *
510 * Return: a valid struct thermal_cooling_device pointer on success,
511 * on failure, it returns a corresponding ERR_PTR().
512 */
513struct thermal_cooling_device *
514cpufreq_cooling_register(const struct cpumask *clip_cpus)
515{
516 return __cpufreq_cooling_register(NULL, clip_cpus);
517}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000518EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530519
520/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400521 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
522 * @np: a valid struct device_node to the cooling device device tree node
523 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
524 *
525 * This interface function registers the cpufreq cooling device with the name
526 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
527 * cooling devices. Using this API, the cpufreq cooling device will be
528 * linked to the device tree node provided.
529 *
530 * Return: a valid struct thermal_cooling_device pointer on success,
531 * on failure, it returns a corresponding ERR_PTR().
532 */
533struct thermal_cooling_device *
534of_cpufreq_cooling_register(struct device_node *np,
535 const struct cpumask *clip_cpus)
536{
537 if (!np)
538 return ERR_PTR(-EINVAL);
539
540 return __cpufreq_cooling_register(np, clip_cpus);
541}
542EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
543
544/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530545 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
546 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000547 *
548 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530549 */
550void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
551{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400552 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530553
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400554 if (!cdev)
555 return;
556
557 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530558 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530559 list_del(&cpufreq_dev->node);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100560 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530561
562 /* Unregister the notifier for the last cpufreq cooling device */
Eduardo Valentin2d9bf712013-04-17 17:12:18 +0000563 if (cpufreq_dev_count == 0)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530564 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000565 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530566 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100567
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530568 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
569 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530570 kfree(cpufreq_dev);
571}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000572EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);