blob: d2e6f845fe7de577cb566063c90208896fa7a9b2 [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,
Zhang Ruifc35b352013-02-08 13:09:32 +0800119};
120
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000121/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530122 * get_property - fetch a property of interest for a given cpu.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000123 * @cpu: cpu for which the property is required
124 * @input: query parameter
125 * @output: query return
Viresh Kumar97afa4a2014-12-04 09:42:03 +0530126 * @property: type of query (frequency, level)
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000127 *
128 * This is the common function to
Viresh Kumar97afa4a2014-12-04 09:42:03 +0530129 * 1. translate frequency to cooling state
130 * 2. translate cooling state to frequency
Viresh Kumar728c03c2014-12-04 09:41:47 +0530131 *
Zhang Ruifc35b352013-02-08 13:09:32 +0800132 * Note that the code may be not in good shape
133 * but it is written in this way in order to:
134 * a) reduce duplicate code as most of the code can be shared.
135 * b) make sure the logic is consistent when translating between
136 * cooling states and frequencies.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000137 *
138 * Return: 0 on success, -EINVAL when invalid parameters are passed.
139 */
Zhang Ruifc35b352013-02-08 13:09:32 +0800140static int get_property(unsigned int cpu, unsigned long input,
Eduardo Valentind8892a02013-04-17 17:12:03 +0000141 unsigned int *output,
142 enum cpufreq_cooling_property property)
Zhang Ruifc35b352013-02-08 13:09:32 +0800143{
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300144 int i;
Eduardo Valentin4469b992013-04-17 17:11:58 +0000145 unsigned long max_level = 0, level = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800146 unsigned int freq = CPUFREQ_ENTRY_INVALID;
147 int descend = -1;
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300148 struct cpufreq_frequency_table *pos, *table =
Zhang Ruifc35b352013-02-08 13:09:32 +0800149 cpufreq_frequency_get_table(cpu);
Eduardo Valentin79d26402013-04-17 17:11:55 +0000150
Zhang Ruifc35b352013-02-08 13:09:32 +0800151 if (!output)
152 return -EINVAL;
153
154 if (!table)
155 return -EINVAL;
156
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300157 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800158 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300159 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800160 continue;
161
162 /* get the frequency order */
Shawn Guo24c7a382013-05-28 06:22:32 +0000163 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300164 descend = freq > pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800165
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300166 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800167 max_level++;
168 }
Zhang Ruia1167762014-01-02 11:57:48 +0800169
170 /* No valid cpu frequency entry */
171 if (max_level == 0)
172 return -EINVAL;
173
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400174 /* max_level is an index, not a counter */
175 max_level--;
Zhang Ruifc35b352013-02-08 13:09:32 +0800176
Zhang Ruifc35b352013-02-08 13:09:32 +0800177 if (property == GET_FREQ)
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400178 level = descend ? input : (max_level - input);
Zhang Ruifc35b352013-02-08 13:09:32 +0800179
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300180 i = 0;
181 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800182 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300183 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800184 continue;
185
186 /* now we have a valid frequency entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300187 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800188
189 if (property == GET_LEVEL && (unsigned int)input == freq) {
190 /* get level by frequency */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300191 *output = descend ? i : (max_level - i);
Zhang Ruifc35b352013-02-08 13:09:32 +0800192 return 0;
193 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300194 if (property == GET_FREQ && level == i) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800195 /* get frequency by level */
196 *output = freq;
197 return 0;
198 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300199 i++;
Zhang Ruifc35b352013-02-08 13:09:32 +0800200 }
Eduardo Valentin79491e52013-04-17 17:11:59 +0000201
Zhang Ruifc35b352013-02-08 13:09:32 +0800202 return -EINVAL;
203}
204
Eduardo Valentin44952d32013-04-17 17:12:05 +0000205/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530206 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000207 * @cpu: cpu for which the level is required
208 * @freq: the frequency of interest
209 *
210 * This function will match the cooling level corresponding to the
211 * requested @freq and return it.
212 *
213 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
214 * otherwise.
215 */
Zhang Rui57df8102013-02-08 14:52:06 +0800216unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
217{
218 unsigned int val;
219
220 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
221 return THERMAL_CSTATE_INVALID;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000222
Zhang Rui57df8102013-02-08 14:52:06 +0800223 return (unsigned long)val;
224}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000225EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800226
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530227/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530228 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
229 * @nb: struct notifier_block * with callback info.
230 * @event: value showing cpufreq event for which this function invoked.
231 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000232 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100233 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000234 * Every time there is a change in policy, we will intercept and
235 * update the cpufreq policy with thermal constraints.
236 *
237 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530238 */
239static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000240 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530241{
242 struct cpufreq_policy *policy = data;
243 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530244 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530245
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530246 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530247 return 0;
248
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530249 mutex_lock(&cooling_cpufreq_lock);
250 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
251 if (!cpumask_test_cpu(policy->cpu,
252 &cpufreq_dev->allowed_cpus))
253 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530254
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530255 max_freq = cpufreq_dev->cpufreq_val;
256
257 if (policy->max != max_freq)
258 cpufreq_verify_within_limits(policy, 0, max_freq);
259 }
260 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530261
262 return 0;
263}
264
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000265/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530266
267/**
268 * cpufreq_get_max_state - callback function to get the max cooling state.
269 * @cdev: thermal cooling device pointer.
270 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000271 *
272 * Callback for the thermal cooling device to return the cpufreq
273 * max cooling state.
274 *
275 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530276 */
277static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
278 unsigned long *state)
279{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100280 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530281
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530282 *state = cpufreq_device->max_level;
283 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530284}
285
286/**
287 * cpufreq_get_cur_state - callback function to get the current cooling state.
288 * @cdev: thermal cooling device pointer.
289 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000290 *
291 * Callback for the thermal cooling device to return the cpufreq
292 * current cooling state.
293 *
294 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530295 */
296static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
297 unsigned long *state)
298{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100299 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530300
hongbo.zhang160b7d82012-10-30 17:48:59 +0100301 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000302
hongbo.zhang160b7d82012-10-30 17:48:59 +0100303 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530304}
305
306/**
307 * cpufreq_set_cur_state - callback function to set the current cooling state.
308 * @cdev: thermal cooling device pointer.
309 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000310 *
311 * Callback for the thermal cooling device to change the cpufreq
312 * current cooling state.
313 *
314 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530315 */
316static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
317 unsigned long state)
318{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100319 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530320 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
321 unsigned int clip_freq;
Viresh Kumar521a2e52014-12-04 09:42:01 +0530322 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530323
Viresh Kumar5194fe42014-12-04 09:42:00 +0530324 /* Check if the old cooling action is same as new cooling action */
325 if (cpufreq_device->cpufreq_state == state)
326 return 0;
327
Viresh Kumar521a2e52014-12-04 09:42:01 +0530328 ret = get_property(cpu, state, &clip_freq, GET_FREQ);
329 if (ret)
330 return ret;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530331
332 cpufreq_device->cpufreq_state = state;
333 cpufreq_device->cpufreq_val = clip_freq;
334
335 cpufreq_update_policy(cpu);
336
337 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530338}
339
340/* Bind cpufreq callbacks to thermal cooling device ops */
341static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
342 .get_max_state = cpufreq_get_max_state,
343 .get_cur_state = cpufreq_get_cur_state,
344 .set_cur_state = cpufreq_set_cur_state,
345};
346
347/* Notifier for cpufreq policy change */
348static struct notifier_block thermal_cpufreq_notifier_block = {
349 .notifier_call = cpufreq_thermal_notifier,
350};
351
352/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400353 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
354 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530355 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530356 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000357 *
358 * This interface function registers the cpufreq cooling device with the name
359 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400360 * cooling devices. It also gives the opportunity to link the cooling device
361 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000362 *
363 * Return: a valid struct thermal_cooling_device pointer on success,
364 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530365 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400366static struct thermal_cooling_device *
367__cpufreq_cooling_register(struct device_node *np,
368 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530369{
370 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530371 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530372 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530373 struct cpufreq_frequency_table *pos, *table;
Viresh Kumar405fb822014-12-04 09:41:55 +0530374 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530375
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530376 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
377 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530378 pr_debug("%s: CPUFreq table not found\n", __func__);
379 return ERR_PTR(-EPROBE_DEFER);
380 }
381
Viresh Kumar98d522f2014-12-04 09:41:50 +0530382 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530383 if (!cpufreq_dev)
384 return ERR_PTR(-ENOMEM);
385
Viresh Kumar521a2e52014-12-04 09:42:01 +0530386 ret = get_property(cpumask_any(clip_cpus), 0, &cpufreq_dev->cpufreq_val,
387 GET_FREQ);
388 if (ret) {
389 pr_err("%s: Failed to get frequency: %d", __func__, ret);
390 cool_dev = ERR_PTR(ret);
Viresh Kumar7adb6352014-12-04 09:41:59 +0530391 goto free_cdev;
392 }
393
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530394 /* Find max levels */
395 cpufreq_for_each_valid_entry(pos, table)
396 cpufreq_dev->max_level++;
397
398 /* max_level is an index, not a counter */
399 cpufreq_dev->max_level--;
400
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530401 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
402
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530403 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
404 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530405 cool_dev = ERR_PTR(ret);
406 goto free_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530407 }
408
Eduardo Valentin99871a72013-04-17 17:12:17 +0000409 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
410 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530411
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400412 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
413 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530414 if (IS_ERR(cool_dev))
415 goto remove_idr;
416
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530417 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530418
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530419 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530420
421 /* Register the notifier for first cpufreq cooling device */
422 if (cpufreq_dev_count == 0)
423 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000424 CPUFREQ_POLICY_NOTIFIER);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100425 cpufreq_dev_count++;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530426 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530427
428 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000429
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530430 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530431
432remove_idr:
433 release_idr(&cpufreq_idr, cpufreq_dev->id);
434free_cdev:
435 kfree(cpufreq_dev);
436
437 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530438}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400439
440/**
441 * cpufreq_cooling_register - function to create cpufreq cooling device.
442 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
443 *
444 * This interface function registers the cpufreq cooling device with the name
445 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
446 * cooling devices.
447 *
448 * Return: a valid struct thermal_cooling_device pointer on success,
449 * on failure, it returns a corresponding ERR_PTR().
450 */
451struct thermal_cooling_device *
452cpufreq_cooling_register(const struct cpumask *clip_cpus)
453{
454 return __cpufreq_cooling_register(NULL, clip_cpus);
455}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000456EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530457
458/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400459 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
460 * @np: a valid struct device_node to the cooling device device tree node
461 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
462 *
463 * This interface function registers the cpufreq cooling device with the name
464 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
465 * cooling devices. Using this API, the cpufreq cooling device will be
466 * linked to the device tree node provided.
467 *
468 * Return: a valid struct thermal_cooling_device pointer on success,
469 * on failure, it returns a corresponding ERR_PTR().
470 */
471struct thermal_cooling_device *
472of_cpufreq_cooling_register(struct device_node *np,
473 const struct cpumask *clip_cpus)
474{
475 if (!np)
476 return ERR_PTR(-EINVAL);
477
478 return __cpufreq_cooling_register(np, clip_cpus);
479}
480EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
481
482/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530483 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
484 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000485 *
486 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530487 */
488void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
489{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400490 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530491
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400492 if (!cdev)
493 return;
494
495 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530496 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530497 list_del(&cpufreq_dev->node);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100498 cpufreq_dev_count--;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530499
500 /* Unregister the notifier for the last cpufreq cooling device */
Eduardo Valentin2d9bf712013-04-17 17:12:18 +0000501 if (cpufreq_dev_count == 0)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530502 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000503 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530504 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100505
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530506 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
507 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530508 kfree(cpufreq_dev);
509}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000510EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);