blob: 86bcf8dc14d373b88e3b3165de1d3eaa65e258e4 [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
Zhang Ruifc35b352013-02-08 13:09:32 +0800113enum cpufreq_cooling_property {
114 GET_LEVEL,
115 GET_FREQ,
116 GET_MAXL,
117};
118
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000119/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530120 * get_property - fetch a property of interest for a given cpu.
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000121 * @cpu: cpu for which the property is required
122 * @input: query parameter
123 * @output: query return
124 * @property: type of query (frequency, level, max level)
125 *
126 * This is the common function to
Zhang Ruifc35b352013-02-08 13:09:32 +0800127 * 1. get maximum cpu cooling states
128 * 2. translate frequency to cooling state
129 * 3. translate cooling state to frequency
Viresh Kumar728c03c2014-12-04 09:41:47 +0530130 *
Zhang Ruifc35b352013-02-08 13:09:32 +0800131 * Note that the code may be not in good shape
132 * but it is written in this way in order to:
133 * a) reduce duplicate code as most of the code can be shared.
134 * b) make sure the logic is consistent when translating between
135 * cooling states and frequencies.
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000136 *
137 * Return: 0 on success, -EINVAL when invalid parameters are passed.
138 */
Zhang Ruifc35b352013-02-08 13:09:32 +0800139static int get_property(unsigned int cpu, unsigned long input,
Eduardo Valentind8892a02013-04-17 17:12:03 +0000140 unsigned int *output,
141 enum cpufreq_cooling_property property)
Zhang Ruifc35b352013-02-08 13:09:32 +0800142{
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300143 int i;
Eduardo Valentin4469b9972013-04-17 17:11:58 +0000144 unsigned long max_level = 0, level = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800145 unsigned int freq = CPUFREQ_ENTRY_INVALID;
146 int descend = -1;
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300147 struct cpufreq_frequency_table *pos, *table =
Zhang Ruifc35b352013-02-08 13:09:32 +0800148 cpufreq_frequency_get_table(cpu);
Eduardo Valentin79d26402013-04-17 17:11:55 +0000149
Zhang Ruifc35b352013-02-08 13:09:32 +0800150 if (!output)
151 return -EINVAL;
152
153 if (!table)
154 return -EINVAL;
155
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300156 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800157 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300158 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800159 continue;
160
161 /* get the frequency order */
Shawn Guo24c7a382013-05-28 06:22:32 +0000162 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300163 descend = freq > pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800164
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300165 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800166 max_level++;
167 }
Zhang Ruia1167762014-01-02 11:57:48 +0800168
169 /* No valid cpu frequency entry */
170 if (max_level == 0)
171 return -EINVAL;
172
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400173 /* max_level is an index, not a counter */
174 max_level--;
Zhang Ruifc35b352013-02-08 13:09:32 +0800175
176 /* get max level */
177 if (property == GET_MAXL) {
178 *output = (unsigned int)max_level;
179 return 0;
180 }
181
182 if (property == GET_FREQ)
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400183 level = descend ? input : (max_level - input);
Zhang Ruifc35b352013-02-08 13:09:32 +0800184
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300185 i = 0;
186 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800187 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300188 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800189 continue;
190
191 /* now we have a valid frequency entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300192 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800193
194 if (property == GET_LEVEL && (unsigned int)input == freq) {
195 /* get level by frequency */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300196 *output = descend ? i : (max_level - i);
Zhang Ruifc35b352013-02-08 13:09:32 +0800197 return 0;
198 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300199 if (property == GET_FREQ && level == i) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800200 /* get frequency by level */
201 *output = freq;
202 return 0;
203 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300204 i++;
Zhang Ruifc35b352013-02-08 13:09:32 +0800205 }
Eduardo Valentin79491e52013-04-17 17:11:59 +0000206
Zhang Ruifc35b352013-02-08 13:09:32 +0800207 return -EINVAL;
208}
209
Eduardo Valentin44952d32013-04-17 17:12:05 +0000210/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530211 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000212 * @cpu: cpu for which the level is required
213 * @freq: the frequency of interest
214 *
215 * This function will match the cooling level corresponding to the
216 * requested @freq and return it.
217 *
218 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
219 * otherwise.
220 */
Zhang Rui57df8102013-02-08 14:52:06 +0800221unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
222{
223 unsigned int val;
224
225 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
226 return THERMAL_CSTATE_INVALID;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000227
Zhang Rui57df8102013-02-08 14:52:06 +0800228 return (unsigned long)val;
229}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000230EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800231
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530232/**
233 * get_cpu_frequency - get the absolute value of frequency from level.
234 * @cpu: cpu for which frequency is fetched.
Eduardo Valentin41518c42013-04-17 17:12:07 +0000235 * @level: cooling level
236 *
237 * This function matches cooling level with frequency. Based on a cooling level
238 * of frequency, equals cooling state of cpu cooling device, it will return
239 * the corresponding frequency.
Zhang Rui475f41c2013-02-06 09:34:32 +0800240 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
Eduardo Valentin41518c42013-04-17 17:12:07 +0000241 *
242 * Return: 0 on error, the corresponding frequency otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530243 */
244static unsigned int get_cpu_frequency(unsigned int cpu, unsigned long level)
245{
Zhang Ruifc35b352013-02-08 13:09:32 +0800246 int ret = 0;
247 unsigned int freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530248
Zhang Ruifc35b352013-02-08 13:09:32 +0800249 ret = get_property(cpu, level, &freq, GET_FREQ);
250 if (ret)
251 return 0;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000252
Zhang Ruifc35b352013-02-08 13:09:32 +0800253 return freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530254}
255
256/**
257 * cpufreq_apply_cooling - function to apply frequency clipping.
258 * @cpufreq_device: cpufreq_cooling_device pointer containing frequency
259 * clipping data.
260 * @cooling_state: value of the cooling state.
Eduardo Valentin4b33deb2013-04-17 17:12:08 +0000261 *
262 * Function used to make sure the cpufreq layer is aware of current thermal
263 * limits. The limits are applied by updating the cpufreq policy.
264 *
265 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
266 * cooling state).
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530267 */
268static int cpufreq_apply_cooling(struct cpufreq_cooling_device *cpufreq_device,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000269 unsigned long cooling_state)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530270{
Viresh Kumare1fae552014-12-04 09:41:56 +0530271 unsigned int clip_freq;
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000272 struct cpumask *mask = &cpufreq_device->allowed_cpus;
273 unsigned int cpu = cpumask_any(mask);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530274
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530275 /* Check if the old cooling action is same as new cooling action */
276 if (cpufreq_device->cpufreq_state == cooling_state)
277 return 0;
278
279 clip_freq = get_cpu_frequency(cpu, cooling_state);
280 if (!clip_freq)
281 return -EINVAL;
282
283 cpufreq_device->cpufreq_state = cooling_state;
284 cpufreq_device->cpufreq_val = clip_freq;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530285
Viresh Kumarc9ca3192014-12-04 09:41:57 +0530286 cpufreq_update_policy(cpu);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530287
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530288 return 0;
289}
290
291/**
292 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
293 * @nb: struct notifier_block * with callback info.
294 * @event: value showing cpufreq event for which this function invoked.
295 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000296 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100297 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000298 * Every time there is a change in policy, we will intercept and
299 * update the cpufreq policy with thermal constraints.
300 *
301 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530302 */
303static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000304 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530305{
306 struct cpufreq_policy *policy = data;
307 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530308 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530309
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530310 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530311 return 0;
312
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530313 mutex_lock(&cooling_cpufreq_lock);
314 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
315 if (!cpumask_test_cpu(policy->cpu,
316 &cpufreq_dev->allowed_cpus))
317 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530318
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530319 max_freq = cpufreq_dev->cpufreq_val;
320
321 if (policy->max != max_freq)
322 cpufreq_verify_within_limits(policy, 0, max_freq);
323 }
324 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530325
326 return 0;
327}
328
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000329/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530330
331/**
332 * cpufreq_get_max_state - callback function to get the max cooling state.
333 * @cdev: thermal cooling device pointer.
334 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000335 *
336 * Callback for the thermal cooling device to return the cpufreq
337 * max cooling state.
338 *
339 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530340 */
341static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
342 unsigned long *state)
343{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100344 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000345 struct cpumask *mask = &cpufreq_device->allowed_cpus;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530346 unsigned int cpu;
Dan Carpenter4f890382013-04-17 07:18:28 +0000347 unsigned int count = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800348 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530349
Laurent Navet [Mali]bde00662013-03-12 10:47:50 +0000350 cpu = cpumask_any(mask);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530351
Dan Carpenter4f890382013-04-17 07:18:28 +0000352 ret = get_property(cpu, 0, &count, GET_MAXL);
hongbo.zhang9c51b052012-10-30 17:48:58 +0100353
Zhang Ruifc35b352013-02-08 13:09:32 +0800354 if (count > 0)
355 *state = count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530356
Zhang Ruifc35b352013-02-08 13:09:32 +0800357 return ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530358}
359
360/**
361 * cpufreq_get_cur_state - callback function to get the current cooling state.
362 * @cdev: thermal cooling device pointer.
363 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000364 *
365 * Callback for the thermal cooling device to return the cpufreq
366 * current cooling state.
367 *
368 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530369 */
370static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
371 unsigned long *state)
372{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100373 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530374
hongbo.zhang160b7d82012-10-30 17:48:59 +0100375 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000376
hongbo.zhang160b7d82012-10-30 17:48:59 +0100377 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530378}
379
380/**
381 * cpufreq_set_cur_state - callback function to set the current cooling state.
382 * @cdev: thermal cooling device pointer.
383 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000384 *
385 * Callback for the thermal cooling device to change the cpufreq
386 * current cooling state.
387 *
388 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530389 */
390static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
391 unsigned long state)
392{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100393 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530394
hongbo.zhang160b7d82012-10-30 17:48:59 +0100395 return cpufreq_apply_cooling(cpufreq_device, state);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530396}
397
398/* Bind cpufreq callbacks to thermal cooling device ops */
399static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
400 .get_max_state = cpufreq_get_max_state,
401 .get_cur_state = cpufreq_get_cur_state,
402 .set_cur_state = cpufreq_set_cur_state,
403};
404
405/* Notifier for cpufreq policy change */
406static struct notifier_block thermal_cpufreq_notifier_block = {
407 .notifier_call = cpufreq_thermal_notifier,
408};
409
410/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400411 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
412 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530413 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530414 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000415 *
416 * This interface function registers the cpufreq cooling device with the name
417 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400418 * cooling devices. It also gives the opportunity to link the cooling device
419 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000420 *
421 * Return: a valid struct thermal_cooling_device pointer on success,
422 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530423 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400424static struct thermal_cooling_device *
425__cpufreq_cooling_register(struct device_node *np,
426 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530427{
428 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530429 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530430 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumar405fb822014-12-04 09:41:55 +0530431 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530432
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530433 if (!cpufreq_frequency_get_table(cpumask_first(clip_cpus))) {
434 pr_debug("%s: CPUFreq table not found\n", __func__);
435 return ERR_PTR(-EPROBE_DEFER);
436 }
437
Viresh Kumar98d522f2014-12-04 09:41:50 +0530438 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530439 if (!cpufreq_dev)
440 return ERR_PTR(-ENOMEM);
441
Viresh Kumar7adb6352014-12-04 09:41:59 +0530442 cpufreq_dev->cpufreq_val = get_cpu_frequency(cpumask_any(clip_cpus), 0);
443 if (!cpufreq_dev->cpufreq_val) {
444 pr_err("%s: Failed to get frequency", __func__);
445 cool_dev = ERR_PTR(-EINVAL);
446 goto free_cdev;
447 }
448
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530449 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
450
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530451 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
452 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530453 cool_dev = ERR_PTR(ret);
454 goto free_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530455 }
456
Eduardo Valentin99871a72013-04-17 17:12:17 +0000457 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
458 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530459
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400460 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
461 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530462 if (IS_ERR(cool_dev))
463 goto remove_idr;
464
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530465 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530466
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530467 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530468
469 /* Register the notifier for first cpufreq cooling device */
470 if (cpufreq_dev_count == 0)
471 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000472 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100473 cpufreq_dev_count++;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530474 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530475
476 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000477
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530478 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530479
480remove_idr:
481 release_idr(&cpufreq_idr, cpufreq_dev->id);
482free_cdev:
483 kfree(cpufreq_dev);
484
485 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530486}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400487
488/**
489 * cpufreq_cooling_register - function to create cpufreq cooling device.
490 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
491 *
492 * This interface function registers the cpufreq cooling device with the name
493 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
494 * cooling devices.
495 *
496 * Return: a valid struct thermal_cooling_device pointer on success,
497 * on failure, it returns a corresponding ERR_PTR().
498 */
499struct thermal_cooling_device *
500cpufreq_cooling_register(const struct cpumask *clip_cpus)
501{
502 return __cpufreq_cooling_register(NULL, clip_cpus);
503}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000504EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530505
506/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400507 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
508 * @np: a valid struct device_node to the cooling device device tree node
509 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
510 *
511 * This interface function registers the cpufreq cooling device with the name
512 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
513 * cooling devices. Using this API, the cpufreq cooling device will be
514 * linked to the device tree node provided.
515 *
516 * Return: a valid struct thermal_cooling_device pointer on success,
517 * on failure, it returns a corresponding ERR_PTR().
518 */
519struct thermal_cooling_device *
520of_cpufreq_cooling_register(struct device_node *np,
521 const struct cpumask *clip_cpus)
522{
523 if (!np)
524 return ERR_PTR(-EINVAL);
525
526 return __cpufreq_cooling_register(np, clip_cpus);
527}
528EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
529
530/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530531 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
532 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000533 *
534 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530535 */
536void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
537{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400538 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530539
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400540 if (!cdev)
541 return;
542
543 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530544 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530545 list_del(&cpufreq_dev->node);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100546 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530547
548 /* Unregister the notifier for the last cpufreq cooling device */
Eduardo Valentin2d9bf712013-04-17 17:12:18 +0000549 if (cpufreq_dev_count == 0)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530550 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000551 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530552 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100553
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530554 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
555 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530556 kfree(cpufreq_dev);
557}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000558EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);