blob: 32ff6dc5efee848fe07dea9100d50affda40704a [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
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053074static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053075
76/**
77 * get_idr - function to get a unique id.
78 * @idr: struct idr * handle used to create a id.
79 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +000080 *
81 * This function will populate @id with an unique
82 * id, using the idr API.
83 *
84 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053085 */
86static int get_idr(struct idr *idr, int *id)
87{
Tejun Heo6deb69f2013-02-27 17:04:46 -080088 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053089
90 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080091 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053092 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080093 if (unlikely(ret < 0))
94 return ret;
95 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +000096
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053097 return 0;
98}
99
100/**
101 * release_idr - function to free the unique id.
102 * @idr: struct idr * handle used for creating the id.
103 * @id: int value representing the unique id.
104 */
105static void release_idr(struct idr *idr, int id)
106{
107 mutex_lock(&cooling_cpufreq_lock);
108 idr_remove(idr, id);
109 mutex_unlock(&cooling_cpufreq_lock);
110}
111
112/* Below code defines functions to be used for cpufreq as cooling device */
113
Zhang Ruifc35b352013-02-08 13:09:32 +0800114enum cpufreq_cooling_property {
115 GET_LEVEL,
116 GET_FREQ,
Zhang Ruifc35b352013-02-08 13:09:32 +0800117};
118
Eduardo Valentin2d6f28f2013-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 Valentin2d6f28f2013-04-17 17:12:01 +0000121 * @cpu: cpu for which the property is required
122 * @input: query parameter
123 * @output: query return
Viresh Kumar97afa4a2014-12-04 09:42:03 +0530124 * @property: type of query (frequency, level)
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000125 *
126 * This is the common function to
Viresh Kumar97afa4a2014-12-04 09:42:03 +0530127 * 1. translate frequency to cooling state
128 * 2. translate cooling state to frequency
Viresh Kumar728c03c2014-12-04 09:41:47 +0530129 *
Zhang Ruifc35b352013-02-08 13:09:32 +0800130 * Note that the code may be not in good shape
131 * but it is written in this way in order to:
132 * a) reduce duplicate code as most of the code can be shared.
133 * b) make sure the logic is consistent when translating between
134 * cooling states and frequencies.
Eduardo Valentin2d6f28f2013-04-17 17:12:01 +0000135 *
136 * Return: 0 on success, -EINVAL when invalid parameters are passed.
137 */
Zhang Ruifc35b352013-02-08 13:09:32 +0800138static int get_property(unsigned int cpu, unsigned long input,
Eduardo Valentind8892a02013-04-17 17:12:03 +0000139 unsigned int *output,
140 enum cpufreq_cooling_property property)
Zhang Ruifc35b352013-02-08 13:09:32 +0800141{
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300142 int i;
Eduardo Valentin4469b992013-04-17 17:11:58 +0000143 unsigned long max_level = 0, level = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800144 unsigned int freq = CPUFREQ_ENTRY_INVALID;
145 int descend = -1;
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300146 struct cpufreq_frequency_table *pos, *table =
Zhang Ruifc35b352013-02-08 13:09:32 +0800147 cpufreq_frequency_get_table(cpu);
Eduardo Valentin79d26402013-04-17 17:11:55 +0000148
Zhang Ruifc35b352013-02-08 13:09:32 +0800149 if (!output)
150 return -EINVAL;
151
152 if (!table)
153 return -EINVAL;
154
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300155 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800156 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300157 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800158 continue;
159
160 /* get the frequency order */
Shawn Guo24c7a382013-05-28 06:22:32 +0000161 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300162 descend = freq > pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800163
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300164 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800165 max_level++;
166 }
Zhang Ruia1167762014-01-02 11:57:48 +0800167
168 /* No valid cpu frequency entry */
169 if (max_level == 0)
170 return -EINVAL;
171
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400172 /* max_level is an index, not a counter */
173 max_level--;
Zhang Ruifc35b352013-02-08 13:09:32 +0800174
Zhang Ruifc35b352013-02-08 13:09:32 +0800175 if (property == GET_FREQ)
Eduardo Valentin1c9573a2013-11-13 14:11:09 -0400176 level = descend ? input : (max_level - input);
Zhang Ruifc35b352013-02-08 13:09:32 +0800177
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300178 i = 0;
179 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800180 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300181 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800182 continue;
183
184 /* now we have a valid frequency entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300185 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800186
187 if (property == GET_LEVEL && (unsigned int)input == freq) {
188 /* get level by frequency */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300189 *output = descend ? i : (max_level - i);
Zhang Ruifc35b352013-02-08 13:09:32 +0800190 return 0;
191 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300192 if (property == GET_FREQ && level == i) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800193 /* get frequency by level */
194 *output = freq;
195 return 0;
196 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300197 i++;
Zhang Ruifc35b352013-02-08 13:09:32 +0800198 }
Eduardo Valentin79491e52013-04-17 17:11:59 +0000199
Zhang Ruifc35b352013-02-08 13:09:32 +0800200 return -EINVAL;
201}
202
Eduardo Valentin44952d32013-04-17 17:12:05 +0000203/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530204 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000205 * @cpu: cpu for which the level is required
206 * @freq: the frequency of interest
207 *
208 * This function will match the cooling level corresponding to the
209 * requested @freq and return it.
210 *
211 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
212 * otherwise.
213 */
Zhang Rui57df8102013-02-08 14:52:06 +0800214unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
215{
216 unsigned int val;
217
218 if (get_property(cpu, (unsigned long)freq, &val, GET_LEVEL))
219 return THERMAL_CSTATE_INVALID;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000220
Zhang Rui57df8102013-02-08 14:52:06 +0800221 return (unsigned long)val;
222}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000223EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800224
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530225/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530226 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
227 * @nb: struct notifier_block * with callback info.
228 * @event: value showing cpufreq event for which this function invoked.
229 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000230 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100231 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000232 * Every time there is a change in policy, we will intercept and
233 * update the cpufreq policy with thermal constraints.
234 *
235 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530236 */
237static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000238 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530239{
240 struct cpufreq_policy *policy = data;
241 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530242 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530243
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530244 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530245 return 0;
246
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530247 mutex_lock(&cooling_cpufreq_lock);
248 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
249 if (!cpumask_test_cpu(policy->cpu,
250 &cpufreq_dev->allowed_cpus))
251 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530252
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530253 max_freq = cpufreq_dev->cpufreq_val;
254
255 if (policy->max != max_freq)
256 cpufreq_verify_within_limits(policy, 0, max_freq);
257 }
258 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530259
260 return 0;
261}
262
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000263/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530264
265/**
266 * cpufreq_get_max_state - callback function to get the max cooling state.
267 * @cdev: thermal cooling device pointer.
268 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000269 *
270 * Callback for the thermal cooling device to return the cpufreq
271 * max cooling state.
272 *
273 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530274 */
275static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
276 unsigned long *state)
277{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100278 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530279
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530280 *state = cpufreq_device->max_level;
281 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530282}
283
284/**
285 * cpufreq_get_cur_state - callback function to get the current cooling state.
286 * @cdev: thermal cooling device pointer.
287 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000288 *
289 * Callback for the thermal cooling device to return the cpufreq
290 * current cooling state.
291 *
292 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530293 */
294static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
295 unsigned long *state)
296{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100297 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530298
hongbo.zhang160b7d82012-10-30 17:48:59 +0100299 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000300
hongbo.zhang160b7d82012-10-30 17:48:59 +0100301 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530302}
303
304/**
305 * cpufreq_set_cur_state - callback function to set the current cooling state.
306 * @cdev: thermal cooling device pointer.
307 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000308 *
309 * Callback for the thermal cooling device to change the cpufreq
310 * current cooling state.
311 *
312 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530313 */
314static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
315 unsigned long state)
316{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100317 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530318 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
319 unsigned int clip_freq;
Viresh Kumar521a2e52014-12-04 09:42:01 +0530320 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530321
Viresh Kumar5194fe42014-12-04 09:42:00 +0530322 /* Check if the old cooling action is same as new cooling action */
323 if (cpufreq_device->cpufreq_state == state)
324 return 0;
325
Viresh Kumar521a2e52014-12-04 09:42:01 +0530326 ret = get_property(cpu, state, &clip_freq, GET_FREQ);
327 if (ret)
328 return ret;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530329
330 cpufreq_device->cpufreq_state = state;
331 cpufreq_device->cpufreq_val = clip_freq;
332
333 cpufreq_update_policy(cpu);
334
335 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530336}
337
338/* Bind cpufreq callbacks to thermal cooling device ops */
339static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
340 .get_max_state = cpufreq_get_max_state,
341 .get_cur_state = cpufreq_get_cur_state,
342 .set_cur_state = cpufreq_set_cur_state,
343};
344
345/* Notifier for cpufreq policy change */
346static struct notifier_block thermal_cpufreq_notifier_block = {
347 .notifier_call = cpufreq_thermal_notifier,
348};
349
350/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400351 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
352 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530353 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530354 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000355 *
356 * This interface function registers the cpufreq cooling device with the name
357 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400358 * cooling devices. It also gives the opportunity to link the cooling device
359 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000360 *
361 * Return: a valid struct thermal_cooling_device pointer on success,
362 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530363 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400364static struct thermal_cooling_device *
365__cpufreq_cooling_register(struct device_node *np,
366 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530367{
368 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530369 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530370 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530371 struct cpufreq_frequency_table *pos, *table;
Viresh Kumar405fb822014-12-04 09:41:55 +0530372 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530373
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530374 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
375 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530376 pr_debug("%s: CPUFreq table not found\n", __func__);
377 return ERR_PTR(-EPROBE_DEFER);
378 }
379
Viresh Kumar98d522f2014-12-04 09:41:50 +0530380 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530381 if (!cpufreq_dev)
382 return ERR_PTR(-ENOMEM);
383
Viresh Kumar521a2e52014-12-04 09:42:01 +0530384 ret = get_property(cpumask_any(clip_cpus), 0, &cpufreq_dev->cpufreq_val,
385 GET_FREQ);
386 if (ret) {
387 pr_err("%s: Failed to get frequency: %d", __func__, ret);
388 cool_dev = ERR_PTR(ret);
Viresh Kumar7adb6352014-12-04 09:41:59 +0530389 goto free_cdev;
390 }
391
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530392 /* Find max levels */
393 cpufreq_for_each_valid_entry(pos, table)
394 cpufreq_dev->max_level++;
395
396 /* max_level is an index, not a counter */
397 cpufreq_dev->max_level--;
398
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530399 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
400
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530401 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
402 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530403 cool_dev = ERR_PTR(ret);
404 goto free_cdev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530405 }
406
Eduardo Valentin99871a72013-04-17 17:12:17 +0000407 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
408 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530409
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400410 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
411 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530412 if (IS_ERR(cool_dev))
413 goto remove_idr;
414
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530415 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530416
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530417 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530418
419 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar2479bb62014-12-04 09:42:04 +0530420 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530421 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000422 CPUFREQ_POLICY_NOTIFIER);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530423 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530424
425 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000426
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530427 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530428
429remove_idr:
430 release_idr(&cpufreq_idr, cpufreq_dev->id);
431free_cdev:
432 kfree(cpufreq_dev);
433
434 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530435}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400436
437/**
438 * cpufreq_cooling_register - function to create cpufreq cooling device.
439 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
440 *
441 * This interface function registers the cpufreq cooling device with the name
442 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
443 * cooling devices.
444 *
445 * Return: a valid struct thermal_cooling_device pointer on success,
446 * on failure, it returns a corresponding ERR_PTR().
447 */
448struct thermal_cooling_device *
449cpufreq_cooling_register(const struct cpumask *clip_cpus)
450{
451 return __cpufreq_cooling_register(NULL, clip_cpus);
452}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000453EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530454
455/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400456 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
457 * @np: a valid struct device_node to the cooling device device tree node
458 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
459 *
460 * This interface function registers the cpufreq cooling device with the name
461 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
462 * cooling devices. Using this API, the cpufreq cooling device will be
463 * linked to the device tree node provided.
464 *
465 * Return: a valid struct thermal_cooling_device pointer on success,
466 * on failure, it returns a corresponding ERR_PTR().
467 */
468struct thermal_cooling_device *
469of_cpufreq_cooling_register(struct device_node *np,
470 const struct cpumask *clip_cpus)
471{
472 if (!np)
473 return ERR_PTR(-EINVAL);
474
475 return __cpufreq_cooling_register(np, clip_cpus);
476}
477EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
478
479/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530480 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
481 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000482 *
483 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530484 */
485void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
486{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400487 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530488
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400489 if (!cdev)
490 return;
491
492 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530493 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530494 list_del(&cpufreq_dev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530495
496 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar2479bb62014-12-04 09:42:04 +0530497 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530498 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000499 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530500 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100501
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530502 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
503 release_idr(&cpufreq_idr, cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530504 kfree(cpufreq_dev);
505}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000506EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);