blob: 051eb4821bc7da26848762095f96883c8be4575c [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 *
Viresh Kumar73904cb2014-12-04 09:42:08 +05307 * Copyright (C) 2014 Viresh Kumar <viresh.kumar@linaro.org>
8 *
Amit Daniel Kachhap02361412012-08-16 17:11:40 +05309 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053025#include <linux/module.h>
26#include <linux/thermal.h>
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053027#include <linux/cpufreq.h>
28#include <linux/err.h>
29#include <linux/slab.h>
30#include <linux/cpu.h>
31#include <linux/cpu_cooling.h>
32
Viresh Kumar07d888d2014-12-04 09:41:49 +053033/*
34 * Cooling state <-> CPUFreq frequency
35 *
36 * Cooling states are translated to frequencies throughout this driver and this
37 * is the relation between them.
38 *
39 * Highest cooling state corresponds to lowest possible frequency.
40 *
41 * i.e.
42 * level 0 --> 1st Max Freq
43 * level 1 --> 2nd Max Freq
44 * ...
45 */
46
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053047/**
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000048 * struct cpufreq_cooling_device - data for cooling device with cpufreq
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053049 * @id: unique integer value corresponding to each cpufreq_cooling_device
50 * registered.
Eduardo Valentin3b3c0742013-04-17 17:11:56 +000051 * @cool_dev: thermal_cooling_device pointer to keep track of the
52 * registered cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053053 * @cpufreq_state: integer value representing the current state of cpufreq
54 * cooling devices.
55 * @cpufreq_val: integer value representing the absolute value of the clipped
56 * frequency.
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053057 * @max_level: maximum cooling level. One less than total number of valid
58 * cpufreq frequencies.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053059 * @allowed_cpus: all the cpus involved for this cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053060 *
Viresh Kumarbeca6052014-12-04 09:41:48 +053061 * This structure is required for keeping information of each registered
62 * cpufreq_cooling_device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053063 */
64struct cpufreq_cooling_device {
65 int id;
66 struct thermal_cooling_device *cool_dev;
67 unsigned int cpufreq_state;
68 unsigned int cpufreq_val;
Viresh Kumardcc6c7f2014-12-04 09:42:02 +053069 unsigned int max_level;
Viresh Kumarf6859012014-12-04 09:42:06 +053070 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053071 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053072 struct list_head node;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053073};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053074static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +010075static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053076
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053077static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053078
79/**
80 * get_idr - function to get a unique id.
81 * @idr: struct idr * handle used to create a id.
82 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +000083 *
84 * This function will populate @id with an unique
85 * id, using the idr API.
86 *
87 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053088 */
89static int get_idr(struct idr *idr, int *id)
90{
Tejun Heo6deb69f2013-02-27 17:04:46 -080091 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053092
93 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080094 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053095 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080096 if (unlikely(ret < 0))
97 return ret;
98 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +000099
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530100 return 0;
101}
102
103/**
104 * release_idr - function to free the unique id.
105 * @idr: struct idr * handle used for creating the id.
106 * @id: int value representing the unique id.
107 */
108static void release_idr(struct idr *idr, int id)
109{
110 mutex_lock(&cooling_cpufreq_lock);
111 idr_remove(idr, id);
112 mutex_unlock(&cooling_cpufreq_lock);
113}
114
115/* Below code defines functions to be used for cpufreq as cooling device */
116
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000117/**
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530118 * get_level: Find the level for a particular frequency
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530119 * @cpufreq_dev: cpufreq_dev for which the property is required
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530120 * @freq: Frequency
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000121 *
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530122 * Return: level on success, THERMAL_CSTATE_INVALID on error.
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000123 */
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530124static unsigned long get_level(struct cpufreq_cooling_device *cpufreq_dev,
125 unsigned int freq)
Zhang Ruifc35b352013-02-08 13:09:32 +0800126{
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530127 unsigned long level;
Eduardo Valentin79d26402013-04-17 17:11:55 +0000128
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530129 for (level = 0; level <= cpufreq_dev->max_level; level++) {
130 if (freq == cpufreq_dev->freq_table[level])
131 return level;
Zhang Ruifc35b352013-02-08 13:09:32 +0800132
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530133 if (freq > cpufreq_dev->freq_table[level])
134 break;
Zhang Ruifc35b352013-02-08 13:09:32 +0800135 }
Zhang Ruia1167762014-01-02 11:57:48 +0800136
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530137 return THERMAL_CSTATE_INVALID;
Zhang Ruifc35b352013-02-08 13:09:32 +0800138}
139
Eduardo Valentin44952d32013-04-17 17:12:05 +0000140/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530141 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000142 * @cpu: cpu for which the level is required
143 * @freq: the frequency of interest
144 *
145 * This function will match the cooling level corresponding to the
146 * requested @freq and return it.
147 *
148 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
149 * otherwise.
150 */
Zhang Rui57df8102013-02-08 14:52:06 +0800151unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
152{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530153 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800154
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530155 mutex_lock(&cooling_cpufreq_lock);
156 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
157 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530158 mutex_unlock(&cooling_cpufreq_lock);
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530159 return get_level(cpufreq_dev, freq);
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530160 }
161 }
162 mutex_unlock(&cooling_cpufreq_lock);
163
164 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
165 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800166}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000167EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800168
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530169/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530170 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
171 * @nb: struct notifier_block * with callback info.
172 * @event: value showing cpufreq event for which this function invoked.
173 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000174 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100175 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000176 * Every time there is a change in policy, we will intercept and
177 * update the cpufreq policy with thermal constraints.
178 *
179 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530180 */
181static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000182 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530183{
184 struct cpufreq_policy *policy = data;
185 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530186 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530187
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530188 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530189 return 0;
190
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530191 mutex_lock(&cooling_cpufreq_lock);
192 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
193 if (!cpumask_test_cpu(policy->cpu,
194 &cpufreq_dev->allowed_cpus))
195 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530196
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530197 max_freq = cpufreq_dev->cpufreq_val;
198
199 if (policy->max != max_freq)
200 cpufreq_verify_within_limits(policy, 0, max_freq);
201 }
202 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530203
204 return 0;
205}
206
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000207/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530208
209/**
210 * cpufreq_get_max_state - callback function to get the max cooling state.
211 * @cdev: thermal cooling device pointer.
212 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000213 *
214 * Callback for the thermal cooling device to return the cpufreq
215 * max cooling state.
216 *
217 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530218 */
219static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
220 unsigned long *state)
221{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100222 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530223
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530224 *state = cpufreq_device->max_level;
225 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530226}
227
228/**
229 * cpufreq_get_cur_state - callback function to get the current cooling state.
230 * @cdev: thermal cooling device pointer.
231 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000232 *
233 * Callback for the thermal cooling device to return the cpufreq
234 * current cooling state.
235 *
236 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530237 */
238static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
239 unsigned long *state)
240{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100241 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530242
hongbo.zhang160b7d82012-10-30 17:48:59 +0100243 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000244
hongbo.zhang160b7d82012-10-30 17:48:59 +0100245 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530246}
247
248/**
249 * cpufreq_set_cur_state - callback function to set the current cooling state.
250 * @cdev: thermal cooling device pointer.
251 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000252 *
253 * Callback for the thermal cooling device to change the cpufreq
254 * current cooling state.
255 *
256 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530257 */
258static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
259 unsigned long state)
260{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100261 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530262 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
263 unsigned int clip_freq;
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530264
265 /* Request state should be less than max_level */
266 if (WARN_ON(state > cpufreq_device->max_level))
267 return -EINVAL;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530268
Viresh Kumar5194fe42014-12-04 09:42:00 +0530269 /* Check if the old cooling action is same as new cooling action */
270 if (cpufreq_device->cpufreq_state == state)
271 return 0;
272
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530273 clip_freq = cpufreq_device->freq_table[state];
Viresh Kumar5194fe42014-12-04 09:42:00 +0530274 cpufreq_device->cpufreq_state = state;
275 cpufreq_device->cpufreq_val = clip_freq;
276
277 cpufreq_update_policy(cpu);
278
279 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530280}
281
282/* Bind cpufreq callbacks to thermal cooling device ops */
283static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
284 .get_max_state = cpufreq_get_max_state,
285 .get_cur_state = cpufreq_get_cur_state,
286 .set_cur_state = cpufreq_set_cur_state,
287};
288
289/* Notifier for cpufreq policy change */
290static struct notifier_block thermal_cpufreq_notifier_block = {
291 .notifier_call = cpufreq_thermal_notifier,
292};
293
Viresh Kumarf6859012014-12-04 09:42:06 +0530294static unsigned int find_next_max(struct cpufreq_frequency_table *table,
295 unsigned int prev_max)
296{
297 struct cpufreq_frequency_table *pos;
298 unsigned int max = 0;
299
300 cpufreq_for_each_valid_entry(pos, table) {
301 if (pos->frequency > max && pos->frequency < prev_max)
302 max = pos->frequency;
303 }
304
305 return max;
306}
307
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530308/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400309 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
310 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530311 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530312 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000313 *
314 * This interface function registers the cpufreq cooling device with the name
315 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400316 * cooling devices. It also gives the opportunity to link the cooling device
317 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000318 *
319 * Return: a valid struct thermal_cooling_device pointer on success,
320 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530321 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400322static struct thermal_cooling_device *
323__cpufreq_cooling_register(struct device_node *np,
324 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530325{
326 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530327 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530328 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530329 struct cpufreq_frequency_table *pos, *table;
Viresh Kumarf6859012014-12-04 09:42:06 +0530330 unsigned int freq, i;
Viresh Kumar405fb822014-12-04 09:41:55 +0530331 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530332
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530333 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
334 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530335 pr_debug("%s: CPUFreq table not found\n", __func__);
336 return ERR_PTR(-EPROBE_DEFER);
337 }
338
Viresh Kumar98d522f2014-12-04 09:41:50 +0530339 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530340 if (!cpufreq_dev)
341 return ERR_PTR(-ENOMEM);
342
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530343 /* Find max levels */
344 cpufreq_for_each_valid_entry(pos, table)
345 cpufreq_dev->max_level++;
346
Viresh Kumarf6859012014-12-04 09:42:06 +0530347 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
348 cpufreq_dev->max_level, GFP_KERNEL);
349 if (!cpufreq_dev->freq_table) {
350 return ERR_PTR(-ENOMEM);
351 cool_dev = ERR_PTR(-ENOMEM);
352 goto free_cdev;
353 }
354
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530355 /* max_level is an index, not a counter */
356 cpufreq_dev->max_level--;
357
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530358 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
359
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530360 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
361 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530362 cool_dev = ERR_PTR(ret);
Viresh Kumarf6859012014-12-04 09:42:06 +0530363 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530364 }
365
Eduardo Valentin99871a72013-04-17 17:12:17 +0000366 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
367 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530368
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400369 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
370 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530371 if (IS_ERR(cool_dev))
372 goto remove_idr;
373
Viresh Kumarf6859012014-12-04 09:42:06 +0530374 /* Fill freq-table in descending order of frequencies */
375 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
376 freq = find_next_max(table, freq);
377 cpufreq_dev->freq_table[i] = freq;
378
379 /* Warn for duplicate entries */
380 if (!freq)
381 pr_warn("%s: table has duplicate entries\n", __func__);
382 else
383 pr_debug("%s: freq:%u KHz\n", __func__, freq);
384 }
385
Viresh Kumar4843c4a2014-12-04 09:42:07 +0530386 cpufreq_dev->cpufreq_val = cpufreq_dev->freq_table[0];
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530387 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530388
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530389 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530390
391 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar2479bb62014-12-04 09:42:04 +0530392 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530393 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000394 CPUFREQ_POLICY_NOTIFIER);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530395 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530396
397 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000398
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530399 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530400
401remove_idr:
402 release_idr(&cpufreq_idr, cpufreq_dev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530403free_table:
404 kfree(cpufreq_dev->freq_table);
Viresh Kumar730abe02014-12-04 09:41:58 +0530405free_cdev:
406 kfree(cpufreq_dev);
407
408 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530409}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400410
411/**
412 * cpufreq_cooling_register - function to create cpufreq cooling device.
413 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
414 *
415 * This interface function registers the cpufreq cooling device with the name
416 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
417 * cooling devices.
418 *
419 * Return: a valid struct thermal_cooling_device pointer on success,
420 * on failure, it returns a corresponding ERR_PTR().
421 */
422struct thermal_cooling_device *
423cpufreq_cooling_register(const struct cpumask *clip_cpus)
424{
425 return __cpufreq_cooling_register(NULL, clip_cpus);
426}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000427EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530428
429/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400430 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
431 * @np: a valid struct device_node to the cooling device device tree node
432 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
433 *
434 * This interface function registers the cpufreq cooling device with the name
435 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
436 * cooling devices. Using this API, the cpufreq cooling device will be
437 * linked to the device tree node provided.
438 *
439 * Return: a valid struct thermal_cooling_device pointer on success,
440 * on failure, it returns a corresponding ERR_PTR().
441 */
442struct thermal_cooling_device *
443of_cpufreq_cooling_register(struct device_node *np,
444 const struct cpumask *clip_cpus)
445{
446 if (!np)
447 return ERR_PTR(-EINVAL);
448
449 return __cpufreq_cooling_register(np, clip_cpus);
450}
451EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
452
453/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530454 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
455 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000456 *
457 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530458 */
459void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
460{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400461 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530462
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400463 if (!cdev)
464 return;
465
466 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530467 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530468 list_del(&cpufreq_dev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530469
470 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar2479bb62014-12-04 09:42:04 +0530471 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530472 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000473 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530474 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100475
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530476 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
477 release_idr(&cpufreq_idr, cpufreq_dev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530478 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530479 kfree(cpufreq_dev);
480}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000481EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);