blob: d34cc5b270213830f293bbf207d25bbe46bd380b [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.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053055 * @max_level: maximum cooling level. One less than total number of valid
56 * cpufreq frequencies.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053057 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053058 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053059 * This structure is required for keeping information of each registered
60 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053061 */
62struct cpufreq_cooling_device {
63 int id;
64 struct thermal_cooling_device *cool_dev;
65 unsigned int cpufreq_state;
66 unsigned int cpufreq_val;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053067 unsigned int max_level;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053068 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053069 struct list_head node;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053070};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053071static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +010072static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053073
hongbo.zhang160b7d82012-10-30 17:48:59 +010074static unsigned int cpufreq_dev_count;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053075
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053076static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053077
78/**
79 * get_idr - function to get a unique id.
80 * @idr: struct idr * handle used to create a id.
81 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +000082 *
83 * This function will populate @id with an unique
84 * id, using the idr API.
85 *
86 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053087 */
88static int get_idr(struct idr *idr, int *id)
89{
Tejun Heo6deb69f2013-02-27 17:04:46 -080090 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053091
92 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080093 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053094 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080095 if (unlikely(ret < 0))
96 return ret;
97 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +000098
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053099 return 0;
100}
101
102/**
103 * release_idr - function to free the unique id.
104 * @idr: struct idr * handle used for creating the id.
105 * @id: int value representing the unique id.
106 */
107static void release_idr(struct idr *idr, int id)
108{
109 mutex_lock(&cooling_cpufreq_lock);
110 idr_remove(idr, id);
111 mutex_unlock(&cooling_cpufreq_lock);
112}
113
114/* Below code defines functions to be used for cpufreq as cooling device */
115
Zhang Ruifc35b352013-02-08 13:09:32 +0800116enum cpufreq_cooling_property {
117 GET_LEVEL,
118 GET_FREQ,
119 GET_MAXL,
120};
121
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000122/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530123 * get_property - fetch a property of interest for a given cpu.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000124 * @cpu: cpu for which the property is required
125 * @input: query parameter
126 * @output: query return
127 * @property: type of query (frequency, level, max level)
128 *
129 * This is the common function to
Zhang Ruifc35b352013-02-08 13:09:32 +0800130 * 1. get maximum cpu cooling states
131 * 2. translate frequency to cooling state
132 * 3. translate cooling state to frequency
Viresh Kumar728c03c2014-12-04 09:41:47 +0530133 *
Zhang Ruifc35b352013-02-08 13:09:32 +0800134 * 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/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530214 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000215 * @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/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530236 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
237 * @nb: struct notifier_block * with callback info.
238 * @event: value showing cpufreq event for which this function invoked.
239 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000240 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100241 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000242 * Every time there is a change in policy, we will intercept and
243 * update the cpufreq policy with thermal constraints.
244 *
245 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530246 */
247static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000248 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530249{
250 struct cpufreq_policy *policy = data;
251 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530252 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530253
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530254 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530255 return 0;
256
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530257 mutex_lock(&cooling_cpufreq_lock);
258 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
259 if (!cpumask_test_cpu(policy->cpu,
260 &cpufreq_dev->allowed_cpus))
261 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530262
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530263 max_freq = cpufreq_dev->cpufreq_val;
264
265 if (policy->max != max_freq)
266 cpufreq_verify_within_limits(policy, 0, max_freq);
267 }
268 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530269
270 return 0;
271}
272
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000273/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530274
275/**
276 * cpufreq_get_max_state - callback function to get the max cooling state.
277 * @cdev: thermal cooling device pointer.
278 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000279 *
280 * Callback for the thermal cooling device to return the cpufreq
281 * max cooling state.
282 *
283 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530284 */
285static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
286 unsigned long *state)
287{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100288 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530289
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530290 *state = cpufreq_device->max_level;
291 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530292}
293
294/**
295 * cpufreq_get_cur_state - callback function to get the current cooling state.
296 * @cdev: thermal cooling device pointer.
297 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000298 *
299 * Callback for the thermal cooling device to return the cpufreq
300 * current cooling state.
301 *
302 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530303 */
304static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
305 unsigned long *state)
306{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100307 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530308
hongbo.zhang160b7d82012-10-30 17:48:59 +0100309 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000310
hongbo.zhang160b7d82012-10-30 17:48:59 +0100311 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530312}
313
314/**
315 * cpufreq_set_cur_state - callback function to set the current cooling state.
316 * @cdev: thermal cooling device pointer.
317 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fdb2013-04-17 17:12:14 +0000318 *
319 * Callback for the thermal cooling device to change the cpufreq
320 * current cooling state.
321 *
322 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530323 */
324static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
325 unsigned long state)
326{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100327 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530328 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
329 unsigned int clip_freq;
Viresh Kumar521a2e52014-12-04 09:42:01 +0530330 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530331
Viresh Kumar5194fe42014-12-04 09:42:00 +0530332 /* Check if the old cooling action is same as new cooling action */
333 if (cpufreq_device->cpufreq_state == state)
334 return 0;
335
Viresh Kumar521a2e52014-12-04 09:42:01 +0530336 ret = get_property(cpu, state, &clip_freq, GET_FREQ);
337 if (ret)
338 return ret;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530339
340 cpufreq_device->cpufreq_state = state;
341 cpufreq_device->cpufreq_val = clip_freq;
342
343 cpufreq_update_policy(cpu);
344
345 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530346}
347
348/* Bind cpufreq callbacks to thermal cooling device ops */
349static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
350 .get_max_state = cpufreq_get_max_state,
351 .get_cur_state = cpufreq_get_cur_state,
352 .set_cur_state = cpufreq_set_cur_state,
353};
354
355/* Notifier for cpufreq policy change */
356static struct notifier_block thermal_cpufreq_notifier_block = {
357 .notifier_call = cpufreq_thermal_notifier,
358};
359
360/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400361 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
362 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530363 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530364 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000365 *
366 * This interface function registers the cpufreq cooling device with the name
367 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400368 * cooling devices. It also gives the opportunity to link the cooling device
369 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000370 *
371 * Return: a valid struct thermal_cooling_device pointer on success,
372 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530373 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400374static struct thermal_cooling_device *
375__cpufreq_cooling_register(struct device_node *np,
376 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530377{
378 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530379 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530380 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530381 struct cpufreq_frequency_table *pos, *table;
Viresh Kumar405fb822014-12-04 09:41:55 +0530382 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530383
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530384 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
385 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530386 pr_debug("%s: CPUFreq table not found\n", __func__);
387 return ERR_PTR(-EPROBE_DEFER);
388 }
389
Viresh Kumar98d522f2014-12-04 09:41:50 +0530390 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530391 if (!cpufreq_dev)
392 return ERR_PTR(-ENOMEM);
393
Viresh Kumar521a2e52014-12-04 09:42:01 +0530394 ret = get_property(cpumask_any(clip_cpus), 0, &cpufreq_dev->cpufreq_val,
395 GET_FREQ);
396 if (ret) {
397 pr_err("%s: Failed to get frequency: %d", __func__, ret);
398 cool_dev = ERR_PTR(ret);
Viresh Kumar7adb6352014-12-04 09:41:59 +0530399 goto free_cdev;
400 }
401
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530402 /* Find max levels */
403 cpufreq_for_each_valid_entry(pos, table)
404 cpufreq_dev->max_level++;
405
406 /* max_level is an index, not a counter */
407 cpufreq_dev->max_level--;
408
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530409 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
410
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530411 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
412 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530413 cool_dev = ERR_PTR(ret);
414 goto free_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530415 }
416
Eduardo Valentin99871a72013-04-17 17:12:17 +0000417 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
418 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530419
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400420 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
421 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530422 if (IS_ERR(cool_dev))
423 goto remove_idr;
424
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530425 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530426
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530427 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530428
429 /* Register the notifier for first cpufreq cooling device */
430 if (cpufreq_dev_count == 0)
431 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000432 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100433 cpufreq_dev_count++;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530434 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530435
436 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000437
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530438 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530439
440remove_idr:
441 release_idr(&cpufreq_idr, cpufreq_dev->id);
442free_cdev:
443 kfree(cpufreq_dev);
444
445 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530446}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400447
448/**
449 * cpufreq_cooling_register - function to create cpufreq cooling device.
450 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
451 *
452 * This interface function registers the cpufreq cooling device with the name
453 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
454 * cooling devices.
455 *
456 * Return: a valid struct thermal_cooling_device pointer on success,
457 * on failure, it returns a corresponding ERR_PTR().
458 */
459struct thermal_cooling_device *
460cpufreq_cooling_register(const struct cpumask *clip_cpus)
461{
462 return __cpufreq_cooling_register(NULL, clip_cpus);
463}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000464EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530465
466/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400467 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
468 * @np: a valid struct device_node to the cooling device device tree node
469 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
470 *
471 * This interface function registers the cpufreq cooling device with the name
472 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
473 * cooling devices. Using this API, the cpufreq cooling device will be
474 * linked to the device tree node provided.
475 *
476 * Return: a valid struct thermal_cooling_device pointer on success,
477 * on failure, it returns a corresponding ERR_PTR().
478 */
479struct thermal_cooling_device *
480of_cpufreq_cooling_register(struct device_node *np,
481 const struct cpumask *clip_cpus)
482{
483 if (!np)
484 return ERR_PTR(-EINVAL);
485
486 return __cpufreq_cooling_register(np, clip_cpus);
487}
488EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
489
490/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530491 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
492 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000493 *
494 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530495 */
496void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
497{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400498 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530499
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400500 if (!cdev)
501 return;
502
503 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530504 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530505 list_del(&cpufreq_dev->node);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100506 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530507
508 /* Unregister the notifier for the last cpufreq cooling device */
Eduardo Valentin2d9bf712013-04-17 17:12:18 +0000509 if (cpufreq_dev_count == 0)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530510 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000511 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530512 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100513
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530514 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
515 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530516 kfree(cpufreq_dev);
517}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000518EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);