blob: f98a763af2f58cc9b1893c42243f1dc941f06b44 [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
31/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000032 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053033 * @id: unique integer value corresponding to each cpufreq_cooling_device
34 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000035 * @cool_dev: thermal_cooling_device pointer to keep track of the
36 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053037 * @cpufreq_state: integer value representing the current state of cpufreq
38 * cooling devices.
39 * @cpufreq_val: integer value representing the absolute value of the clipped
40 * frequency.
41 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053042 *
43 * This structure is required for keeping information of each
Eduardo Valentin67d0b2a2013-04-17 17:12:19 +000044 * cpufreq_cooling_device registered. In order to prevent corruption of this a
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053045 * mutex lock cooling_cpufreq_lock is used.
46 */
47struct cpufreq_cooling_device {
48 int id;
49 struct thermal_cooling_device *cool_dev;
50 unsigned int cpufreq_state;
51 unsigned int cpufreq_val;
52 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053053 struct list_head node;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053054};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053055static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +010056static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053057
hongbo.zhang160b7d82012-10-30 17:48:59 +010058static unsigned int cpufreq_dev_count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053059
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053060static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053061
62/**
63 * get_idr - function to get a unique id.
64 * @idr: struct idr * handle used to create a id.
65 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +000066 *
67 * This function will populate @id with an unique
68 * id, using the idr API.
69 *
70 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053071 */
72static int get_idr(struct idr *idr, int *id)
73{
Tejun Heo6deb69f2013-02-27 17:04:46 -080074 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053075
76 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080077 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053078 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080079 if (unlikely(ret < 0))
80 return ret;
81 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +000082
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053083 return 0;
84}
85
86/**
87 * release_idr - function to free the unique id.
88 * @idr: struct idr * handle used for creating the id.
89 * @id: int value representing the unique id.
90 */
91static void release_idr(struct idr *idr, int id)
92{
93 mutex_lock(&cooling_cpufreq_lock);
94 idr_remove(idr, id);
95 mutex_unlock(&cooling_cpufreq_lock);
96}
97
98/* Below code defines functions to be used for cpufreq as cooling device */
99
100/**
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000101 * is_cpufreq_valid - function to check frequency transitioning capability.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530102 * @cpu: cpu for which check is needed.
Eduardo Valentin82b9ee42013-04-17 17:12:00 +0000103 *
104 * This function will check the current state of the system if
105 * it is capable of changing the frequency for a given @cpu.
106 *
107 * Return: 0 if the system is not currently capable of changing
108 * the frequency of given cpu. !0 in case the frequency is changeable.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530109 */
110static int is_cpufreq_valid(int cpu)
111{
112 struct cpufreq_policy policy;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000113
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530114 return !cpufreq_get_policy(&policy, cpu);
115}
116
Zhang Ruifc35b352013-02-08 13:09:32 +0800117enum cpufreq_cooling_property {
118 GET_LEVEL,
119 GET_FREQ,
120 GET_MAXL,
121};
122
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000123/**
124 * get_property - fetch a property of interest for a give cpu.
125 * @cpu: cpu for which the property is required
126 * @input: query parameter
127 * @output: query return
128 * @property: type of query (frequency, level, max level)
129 *
130 * This is the common function to
Zhang Ruifc35b352013-02-08 13:09:32 +0800131 * 1. get maximum cpu cooling states
132 * 2. translate frequency to cooling state
133 * 3. translate cooling state to frequency
134 * Note that the code may be not in good shape
135 * but it is written in this way in order to:
136 * a) reduce duplicate code as most of the code can be shared.
137 * b) make sure the logic is consistent when translating between
138 * cooling states and frequencies.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000139 *
140 * Return: 0 on success, -EINVAL when invalid parameters are passed.
141 */
Zhang Ruifc35b352013-02-08 13:09:32 +0800142static int get_property(unsigned int cpu, unsigned long input,
Eduardo Valentind8892a02013-04-17 17:12:03 +0000143 unsigned int *output,
144 enum cpufreq_cooling_property property)
Zhang Ruifc35b352013-02-08 13:09:32 +0800145{
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300146 int i;
Eduardo Valentin4469b992013-04-17 17:11:58 +0000147 unsigned long max_level = 0, level = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800148 unsigned int freq = CPUFREQ_ENTRY_INVALID;
149 int descend = -1;
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300150 struct cpufreq_frequency_table *pos, *table =
Zhang Ruifc35b352013-02-08 13:09:32 +0800151 cpufreq_frequency_get_table(cpu);
Eduardo Valentin79d26402013-04-17 17:11:55 +0000152
Zhang Ruifc35b352013-02-08 13:09:32 +0800153 if (!output)
154 return -EINVAL;
155
156 if (!table)
157 return -EINVAL;
158
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300159 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800160 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300161 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800162 continue;
163
164 /* get the frequency order */
Shawn Guo24c7a382013-05-28 06:22:32 +0000165 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300166 descend = freq > pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800167
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300168 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800169 max_level++;
170 }
Zhang Ruia1167762014-01-02 11:57:48 +0800171
172 /* No valid cpu frequency entry */
173 if (max_level == 0)
174 return -EINVAL;
175
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400176 /* max_level is an index, not a counter */
177 max_level--;
Zhang Ruifc35b352013-02-08 13:09:32 +0800178
179 /* get max level */
180 if (property == GET_MAXL) {
181 *output = (unsigned int)max_level;
182 return 0;
183 }
184
185 if (property == GET_FREQ)
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400186 level = descend ? input : (max_level - input);
Zhang Ruifc35b352013-02-08 13:09:32 +0800187
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300188 i = 0;
189 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800190 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300191 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800192 continue;
193
194 /* now we have a valid frequency entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300195 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800196
197 if (property == GET_LEVEL && (unsigned int)input == freq) {
198 /* get level by frequency */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300199 *output = descend ? i : (max_level - i);
Zhang Ruifc35b352013-02-08 13:09:32 +0800200 return 0;
201 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300202 if (property == GET_FREQ && level == i) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800203 /* get frequency by level */
204 *output = freq;
205 return 0;
206 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300207 i++;
Zhang Ruifc35b352013-02-08 13:09:32 +0800208 }
Eduardo Valentin79491e52013-04-17 17:11:59 +0000209
Zhang Ruifc35b352013-02-08 13:09:32 +0800210 return -EINVAL;
211}
212
Eduardo Valentin44952d32013-04-17 17:12:05 +0000213/**
214 * cpufreq_cooling_get_level - for a give cpu, return the cooling level.
215 * @cpu: cpu for which the level is required
216 * @freq: the frequency of interest
217 *
218 * This function will match the cooling level corresponding to the
219 * requested @freq and return it.
220 *
221 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
222 * otherwise.
223 */
Zhang Rui57df8102013-02-08 14:52:06 +0800224unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
225{
226 unsigned int val;
227
228 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
229 return THERMAL_CSTATE_INVALID;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000230
Zhang Rui57df8102013-02-08 14:52:06 +0800231 return (unsigned long)val;
232}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000233EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800234
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530235/**
236 * get_cpu_frequency - get the absolute value of frequency from level.
237 * @cpu: cpu for which frequency is fetched.
Eduardo Valentin41518c42013-04-17 17:12:07 +0000238 * @level: cooling level
239 *
240 * This function matches cooling level with frequency. Based on a cooling level
241 * of frequency, equals cooling state of cpu cooling device, it will return
242 * the corresponding frequency.
Zhang Rui475f41c2013-02-06 09:34:32 +0800243 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
Eduardo Valentin41518c42013-04-17 17:12:07 +0000244 *
245 * Return: 0 on error, the corresponding frequency otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530246 */
247static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
248{
Zhang Ruifc35b352013-02-08 13:09:32 +0800249 int ret = 0;
250 unsigned int freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530251
Zhang Ruifc35b352013-02-08 13:09:32 +0800252 ret = get_property(cpu, level, &freq, GET_FREQ);
253 if (ret)
254 return 0;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000255
Zhang Ruifc35b352013-02-08 13:09:32 +0800256 return freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530257}
258
259/**
260 * cpufreq_apply_cooling - function to apply frequency clipping.
261 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
262 * clipping data.
263 * @cooling_state: value of the cooling state.
Eduardo Valentin4b33deb2013-04-17 17:12:08 +0000264 *
265 * Function used to make sure the cpufreq layer is aware of current thermal
266 * limits. The limits are applied by updating the cpufreq policy.
267 *
268 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
269 * cooling state).
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530270 */
271static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000272 unsigned long cooling_state)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530273{
274 unsigned int cpuid, clip_freq;
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000275 struct cpumask *mask = &cpufreq_device->allowed_cpus;
276 unsigned int cpu = cpumask_any(mask);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530277
278
279 /* Check if the old cooling action is same as new cooling action */
280 if (cpufreq_device->cpufreq_state == cooling_state)
281 return 0;
282
283 clip_freq = get_cpu_frequency(cpu, cooling_state);
284 if (!clip_freq)
285 return -EINVAL;
286
287 cpufreq_device->cpufreq_state = cooling_state;
288 cpufreq_device->cpufreq_val = clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530289
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000290 for_each_cpu(cpuid, mask) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530291 if (is_cpufreq_valid(cpuid))
292 cpufreq_update_policy(cpuid);
293 }
294
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530295 return 0;
296}
297
298/**
299 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
300 * @nb: struct notifier_block * with callback info.
301 * @event: value showing cpufreq event for which this function invoked.
302 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000303 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100304 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000305 * Every time there is a change in policy, we will intercept and
306 * update the cpufreq policy with thermal constraints.
307 *
308 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530309 */
310static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000311 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530312{
313 struct cpufreq_policy *policy = data;
314 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530315 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530316
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530317 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530318 return 0;
319
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530320 mutex_lock(&cooling_cpufreq_lock);
321 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
322 if (!cpumask_test_cpu(policy->cpu,
323 &cpufreq_dev->allowed_cpus))
324 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530325
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530326 if (!cpufreq_dev->cpufreq_val)
327 cpufreq_dev->cpufreq_val = get_cpu_frequency(
328 cpumask_any(&cpufreq_dev->allowed_cpus),
329 cpufreq_dev->cpufreq_state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530330
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530331 max_freq = cpufreq_dev->cpufreq_val;
332
333 if (policy->max != max_freq)
334 cpufreq_verify_within_limits(policy, 0, max_freq);
335 }
336 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530337
338 return 0;
339}
340
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000341/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530342
343/**
344 * cpufreq_get_max_state - callback function to get the max cooling state.
345 * @cdev: thermal cooling device pointer.
346 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000347 *
348 * Callback for the thermal cooling device to return the cpufreq
349 * max cooling state.
350 *
351 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530352 */
353static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
354 unsigned long *state)
355{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100356 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000357 struct cpumask *mask = &cpufreq_device->allowed_cpus;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530358 unsigned int cpu;
Dan Carpenter4f890382013-04-17 07:18:28 +0000359 unsigned int count = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800360 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530361
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000362 cpu = cpumask_any(mask);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530363
Dan Carpenter4f890382013-04-17 07:18:28 +0000364 ret = get_property(cpu, 0, &count, GET_MAXL);
hongbo.zhang9c51b052012-10-30 17:48:58 +0100365
Zhang Ruifc35b352013-02-08 13:09:32 +0800366 if (count > 0)
367 *state = count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530368
Zhang Ruifc35b352013-02-08 13:09:32 +0800369 return ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530370}
371
372/**
373 * cpufreq_get_cur_state - callback function to get the current cooling state.
374 * @cdev: thermal cooling device pointer.
375 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000376 *
377 * Callback for the thermal cooling device to return the cpufreq
378 * current cooling state.
379 *
380 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530381 */
382static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
383 unsigned long *state)
384{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100385 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530386
hongbo.zhang160b7d82012-10-30 17:48:59 +0100387 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000388
hongbo.zhang160b7d82012-10-30 17:48:59 +0100389 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530390}
391
392/**
393 * cpufreq_set_cur_state - callback function to set the current cooling state.
394 * @cdev: thermal cooling device pointer.
395 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000396 *
397 * Callback for the thermal cooling device to change the cpufreq
398 * current cooling state.
399 *
400 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530401 */
402static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
403 unsigned long state)
404{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100405 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530406
hongbo.zhang160b7d82012-10-30 17:48:59 +0100407 return cpufreq_apply_cooling(cpufreq_device, state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530408}
409
410/* Bind cpufreq callbacks to thermal cooling device ops */
411static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
412 .get_max_state = cpufreq_get_max_state,
413 .get_cur_state = cpufreq_get_cur_state,
414 .set_cur_state = cpufreq_set_cur_state,
415};
416
417/* Notifier for cpufreq policy change */
418static struct notifier_block thermal_cpufreq_notifier_block = {
419 .notifier_call = cpufreq_thermal_notifier,
420};
421
422/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400423 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
424 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530425 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000426 *
427 * This interface function registers the cpufreq cooling device with the name
428 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400429 * cooling devices. It also gives the opportunity to link the cooling device
430 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000431 *
432 * Return: a valid struct thermal_cooling_device pointer on success,
433 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530434 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400435static struct thermal_cooling_device *
436__cpufreq_cooling_register(struct device_node *np,
437 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530438{
439 struct thermal_cooling_device *cool_dev;
440 struct cpufreq_cooling_device *cpufreq_dev = NULL;
hongbo.zhang160b7d82012-10-30 17:48:59 +0100441 unsigned int min = 0, max = 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530442 char dev_name[THERMAL_NAME_LENGTH];
Jonghwa Leea4b6fec2012-09-26 09:43:31 +0900443 int ret = 0, i;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530444 struct cpufreq_policy policy;
445
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530446 if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) {
447 pr_debug("%s: CPUFreq table not found\n", __func__);
448 return ERR_PTR(-EPROBE_DEFER);
449 }
450
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000451 /* Verify that all the clip cpus have same freq_min, freq_max limit */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530452 for_each_cpu(i, clip_cpus) {
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000453 /* continue if cpufreq policy not found and not return error */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530454 if (!cpufreq_get_policy(&policy, i))
455 continue;
456 if (min == 0 && max == 0) {
457 min = policy.cpuinfo.min_freq;
458 max = policy.cpuinfo.max_freq;
459 } else {
460 if (min != policy.cpuinfo.min_freq ||
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000461 max != policy.cpuinfo.max_freq)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530462 return ERR_PTR(-EINVAL);
hongbo.zhang6b6519d2012-10-30 17:48:57 +0100463 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530464 }
465 cpufreq_dev = kzalloc(sizeof(struct cpufreq_cooling_device),
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000466 GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530467 if (!cpufreq_dev)
468 return ERR_PTR(-ENOMEM);
469
470 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
471
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530472 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
473 if (ret) {
474 kfree(cpufreq_dev);
475 return ERR_PTR(-EINVAL);
476 }
477
Eduardo Valentin99871a72013-04-17 17:12:17 +0000478 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
479 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530480
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400481 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
482 &cpufreq_cooling_ops);
Wei Yongjun73b9bcd2013-10-25 21:55:42 +0800483 if (IS_ERR(cool_dev)) {
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530484 release_idr(&cpufreq_idr, cpufreq_dev->id);
485 kfree(cpufreq_dev);
Wei Yongjun73b9bcd2013-10-25 21:55:42 +0800486 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530487 }
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530488 cpufreq_dev->cool_dev = cool_dev;
489 cpufreq_dev->cpufreq_state = 0;
490 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530491
492 /* Register the notifier for first cpufreq cooling device */
493 if (cpufreq_dev_count == 0)
494 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000495 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100496 cpufreq_dev_count++;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530497 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530498
499 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000500
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530501 return cool_dev;
502}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400503
504/**
505 * cpufreq_cooling_register - function to create cpufreq cooling device.
506 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
507 *
508 * This interface function registers the cpufreq cooling device with the name
509 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
510 * cooling devices.
511 *
512 * Return: a valid struct thermal_cooling_device pointer on success,
513 * on failure, it returns a corresponding ERR_PTR().
514 */
515struct thermal_cooling_device *
516cpufreq_cooling_register(const struct cpumask *clip_cpus)
517{
518 return __cpufreq_cooling_register(NULL, clip_cpus);
519}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000520EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530521
522/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400523 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
524 * @np: a valid struct device_node to the cooling device device tree node
525 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
526 *
527 * This interface function registers the cpufreq cooling device with the name
528 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
529 * cooling devices. Using this API, the cpufreq cooling device will be
530 * linked to the device tree node provided.
531 *
532 * Return: a valid struct thermal_cooling_device pointer on success,
533 * on failure, it returns a corresponding ERR_PTR().
534 */
535struct thermal_cooling_device *
536of_cpufreq_cooling_register(struct device_node *np,
537 const struct cpumask *clip_cpus)
538{
539 if (!np)
540 return ERR_PTR(-EINVAL);
541
542 return __cpufreq_cooling_register(np, clip_cpus);
543}
544EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
545
546/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530547 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
548 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000549 *
550 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530551 */
552void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
553{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400554 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530555
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400556 if (!cdev)
557 return;
558
559 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530560 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530561 list_del(&cpufreq_dev->node);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100562 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530563
564 /* Unregister the notifier for the last cpufreq cooling device */
Eduardo Valentin2d9bf712013-04-17 17:12:18 +0000565 if (cpufreq_dev_count == 0)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530566 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000567 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530568 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100569
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530570 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
571 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530572 kfree(cpufreq_dev);
573}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000574EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);