blob: cb5a4b9141876e8b9e9d50adf7a2ce6468d92f01 [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;
Viresh Kumarf6859012014-12-04 09:42:06 +053068 unsigned int *freq_table; /* In descending order */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053069 struct cpumask allowed_cpus;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053070 struct list_head node;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053071};
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053072static DEFINE_IDR(cpufreq_idr);
hongbo.zhang160b7d82012-10-30 17:48:59 +010073static DEFINE_MUTEX(cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053074
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +053075static LIST_HEAD(cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053076
77/**
78 * get_idr - function to get a unique id.
79 * @idr: struct idr * handle used to create a id.
80 * @id: int * value generated by this function.
Eduardo Valentin79491e52013-04-17 17:11:59 +000081 *
82 * This function will populate @id with an unique
83 * id, using the idr API.
84 *
85 * Return: 0 on success, an error code on failure.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053086 */
87static int get_idr(struct idr *idr, int *id)
88{
Tejun Heo6deb69f2013-02-27 17:04:46 -080089 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053090
91 mutex_lock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080092 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053093 mutex_unlock(&cooling_cpufreq_lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -080094 if (unlikely(ret < 0))
95 return ret;
96 *id = ret;
Eduardo Valentin79491e52013-04-17 17:11:59 +000097
Amit Daniel Kachhap02361412012-08-16 17:11:40 +053098 return 0;
99}
100
101/**
102 * release_idr - function to free the unique id.
103 * @idr: struct idr * handle used for creating the id.
104 * @id: int value representing the unique id.
105 */
106static void release_idr(struct idr *idr, int id)
107{
108 mutex_lock(&cooling_cpufreq_lock);
109 idr_remove(idr, id);
110 mutex_unlock(&cooling_cpufreq_lock);
111}
112
113/* Below code defines functions to be used for cpufreq as cooling device */
114
Zhang Ruifc35b352013-02-08 13:09:32 +0800115enum cpufreq_cooling_property {
116 GET_LEVEL,
117 GET_FREQ,
Zhang Ruifc35b352013-02-08 13:09:32 +0800118};
119
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000120/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530121 * get_property - fetch a property of interest for a given cpu.
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530122 * @cpufreq_dev: cpufreq_dev for which the property is required
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000123 * @input: query parameter
124 * @output: query return
Viresh Kumar97afa4a2014-12-04 09:42:03 +0530125 * @property: type of query (frequency, level)
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000126 *
127 * This is the common function to
Viresh Kumar97afa4a2014-12-04 09:42:03 +0530128 * 1. translate frequency to cooling state
129 * 2. translate cooling state to frequency
Viresh Kumar728c03c2014-12-04 09:41:47 +0530130 *
Zhang Ruifc35b352013-02-08 13:09:32 +0800131 * Note that the code may be not in good shape
132 * but it is written in this way in order to:
133 * a) reduce duplicate code as most of the code can be shared.
134 * b) make sure the logic is consistent when translating between
135 * cooling states and frequencies.
Eduardo Valentin2d6f28fe2013-04-17 17:12:01 +0000136 *
137 * Return: 0 on success, -EINVAL when invalid parameters are passed.
138 */
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530139static int get_property(struct cpufreq_cooling_device *cpufreq_dev,
140 unsigned long input, unsigned int *output,
Eduardo Valentind8892a02013-04-17 17:12:03 +0000141 enum cpufreq_cooling_property property)
Zhang Ruifc35b352013-02-08 13:09:32 +0800142{
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300143 int i;
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530144 unsigned long level = 0;
Zhang Ruifc35b352013-02-08 13:09:32 +0800145 unsigned int freq = CPUFREQ_ENTRY_INVALID;
146 int descend = -1;
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530147 struct cpufreq_frequency_table *pos, *table;
Eduardo Valentin79d26402013-04-17 17:11:55 +0000148
Zhang Ruifc35b352013-02-08 13:09:32 +0800149 if (!output)
150 return -EINVAL;
151
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530152 table = cpufreq_frequency_get_table(cpumask_first(&cpufreq_dev->allowed_cpus));
Zhang Ruifc35b352013-02-08 13:09:32 +0800153 if (!table)
154 return -EINVAL;
155
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300156 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800157 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300158 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800159 continue;
160
161 /* get the frequency order */
Shawn Guo24c7a382013-05-28 06:22:32 +0000162 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300163 descend = freq > pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800164
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300165 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800166 }
Zhang Ruia1167762014-01-02 11:57:48 +0800167
Zhang Ruifc35b352013-02-08 13:09:32 +0800168 if (property == GET_FREQ)
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530169 level = descend ? input : (cpufreq_dev->max_level - input);
Zhang Ruifc35b352013-02-08 13:09:32 +0800170
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300171 i = 0;
172 cpufreq_for_each_valid_entry(pos, table) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800173 /* ignore duplicate entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300174 if (freq == pos->frequency)
Zhang Ruifc35b352013-02-08 13:09:32 +0800175 continue;
176
177 /* now we have a valid frequency entry */
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300178 freq = pos->frequency;
Zhang Ruifc35b352013-02-08 13:09:32 +0800179
180 if (property == GET_LEVEL && (unsigned int)input == freq) {
181 /* get level by frequency */
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530182 *output = descend ? i : (cpufreq_dev->max_level - i);
Zhang Ruifc35b352013-02-08 13:09:32 +0800183 return 0;
184 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300185 if (property == GET_FREQ && level == i) {
Zhang Ruifc35b352013-02-08 13:09:32 +0800186 /* get frequency by level */
187 *output = freq;
188 return 0;
189 }
Stratos Karafotis3c84ef32014-04-25 23:16:39 +0300190 i++;
Zhang Ruifc35b352013-02-08 13:09:32 +0800191 }
Eduardo Valentin79491e52013-04-17 17:11:59 +0000192
Zhang Ruifc35b352013-02-08 13:09:32 +0800193 return -EINVAL;
194}
195
Eduardo Valentin44952d32013-04-17 17:12:05 +0000196/**
Viresh Kumar728c03c2014-12-04 09:41:47 +0530197 * cpufreq_cooling_get_level - for a given cpu, return the cooling level.
Eduardo Valentin44952d32013-04-17 17:12:05 +0000198 * @cpu: cpu for which the level is required
199 * @freq: the frequency of interest
200 *
201 * This function will match the cooling level corresponding to the
202 * requested @freq and return it.
203 *
204 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
205 * otherwise.
206 */
Zhang Rui57df8102013-02-08 14:52:06 +0800207unsigned long cpufreq_cooling_get_level(unsigned int cpu, unsigned int freq)
208{
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530209 struct cpufreq_cooling_device *cpufreq_dev;
Zhang Rui57df8102013-02-08 14:52:06 +0800210
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530211 mutex_lock(&cooling_cpufreq_lock);
212 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
213 if (cpumask_test_cpu(cpu, &cpufreq_dev->allowed_cpus)) {
214 unsigned int val;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000215
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530216 mutex_unlock(&cooling_cpufreq_lock);
217 if (get_property(cpufreq_dev, (unsigned long)freq, &val,
218 GET_LEVEL))
219 return THERMAL_CSTATE_INVALID;
220
221 return (unsigned long)val;
222 }
223 }
224 mutex_unlock(&cooling_cpufreq_lock);
225
226 pr_err("%s: cpu:%d not part of any cooling device\n", __func__, cpu);
227 return THERMAL_CSTATE_INVALID;
Zhang Rui57df8102013-02-08 14:52:06 +0800228}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000229EXPORT_SYMBOL_GPL(cpufreq_cooling_get_level);
Zhang Rui57df8102013-02-08 14:52:06 +0800230
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530231/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530232 * cpufreq_thermal_notifier - notifier callback for cpufreq policy change.
233 * @nb: struct notifier_block * with callback info.
234 * @event: value showing cpufreq event for which this function invoked.
235 * @data: callback-specific data
Eduardo Valentinbab30552013-04-17 17:12:09 +0000236 *
Javi Merino9746b6e2014-06-25 18:11:17 +0100237 * Callback to hijack the notification on cpufreq policy transition.
Eduardo Valentinbab30552013-04-17 17:12:09 +0000238 * Every time there is a change in policy, we will intercept and
239 * update the cpufreq policy with thermal constraints.
240 *
241 * Return: 0 (success)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530242 */
243static int cpufreq_thermal_notifier(struct notifier_block *nb,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000244 unsigned long event, void *data)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530245{
246 struct cpufreq_policy *policy = data;
247 unsigned long max_freq = 0;
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530248 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530249
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530250 if (event != CPUFREQ_ADJUST)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530251 return 0;
252
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530253 mutex_lock(&cooling_cpufreq_lock);
254 list_for_each_entry(cpufreq_dev, &cpufreq_dev_list, node) {
255 if (!cpumask_test_cpu(policy->cpu,
256 &cpufreq_dev->allowed_cpus))
257 continue;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530258
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530259 max_freq = cpufreq_dev->cpufreq_val;
260
261 if (policy->max != max_freq)
262 cpufreq_verify_within_limits(policy, 0, max_freq);
263 }
264 mutex_unlock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530265
266 return 0;
267}
268
Eduardo Valentin1b9e3522013-04-17 17:12:02 +0000269/* cpufreq cooling device callback functions are defined below */
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530270
271/**
272 * cpufreq_get_max_state - callback function to get the max cooling state.
273 * @cdev: thermal cooling device pointer.
274 * @state: fill this variable with the max cooling state.
Eduardo Valentin62c00422013-04-17 17:12:12 +0000275 *
276 * Callback for the thermal cooling device to return the cpufreq
277 * max cooling state.
278 *
279 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530280 */
281static int cpufreq_get_max_state(struct thermal_cooling_device *cdev,
282 unsigned long *state)
283{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100284 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530285
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530286 *state = cpufreq_device->max_level;
287 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530288}
289
290/**
291 * cpufreq_get_cur_state - callback function to get the current cooling state.
292 * @cdev: thermal cooling device pointer.
293 * @state: fill this variable with the current cooling state.
Eduardo Valentin36725522013-04-17 17:12:13 +0000294 *
295 * Callback for the thermal cooling device to return the cpufreq
296 * current cooling state.
297 *
298 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530299 */
300static int cpufreq_get_cur_state(struct thermal_cooling_device *cdev,
301 unsigned long *state)
302{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100303 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530304
hongbo.zhang160b7d82012-10-30 17:48:59 +0100305 *state = cpufreq_device->cpufreq_state;
Eduardo Valentin79491e52013-04-17 17:11:59 +0000306
hongbo.zhang160b7d82012-10-30 17:48:59 +0100307 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530308}
309
310/**
311 * cpufreq_set_cur_state - callback function to set the current cooling state.
312 * @cdev: thermal cooling device pointer.
313 * @state: set this variable to the current cooling state.
Eduardo Valentin56e05fd2013-04-17 17:12:14 +0000314 *
315 * Callback for the thermal cooling device to change the cpufreq
316 * current cooling state.
317 *
318 * Return: 0 on success, an error code otherwise.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530319 */
320static int cpufreq_set_cur_state(struct thermal_cooling_device *cdev,
321 unsigned long state)
322{
hongbo.zhang160b7d82012-10-30 17:48:59 +0100323 struct cpufreq_cooling_device *cpufreq_device = cdev->devdata;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530324 unsigned int cpu = cpumask_any(&cpufreq_device->allowed_cpus);
325 unsigned int clip_freq;
Viresh Kumar521a2e52014-12-04 09:42:01 +0530326 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530327
Viresh Kumar5194fe42014-12-04 09:42:00 +0530328 /* Check if the old cooling action is same as new cooling action */
329 if (cpufreq_device->cpufreq_state == state)
330 return 0;
331
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530332 ret = get_property(cpufreq_device, state, &clip_freq, GET_FREQ);
Viresh Kumar521a2e52014-12-04 09:42:01 +0530333 if (ret)
334 return ret;
Viresh Kumar5194fe42014-12-04 09:42:00 +0530335
336 cpufreq_device->cpufreq_state = state;
337 cpufreq_device->cpufreq_val = clip_freq;
338
339 cpufreq_update_policy(cpu);
340
341 return 0;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530342}
343
344/* Bind cpufreq callbacks to thermal cooling device ops */
345static struct thermal_cooling_device_ops const cpufreq_cooling_ops = {
346 .get_max_state = cpufreq_get_max_state,
347 .get_cur_state = cpufreq_get_cur_state,
348 .set_cur_state = cpufreq_set_cur_state,
349};
350
351/* Notifier for cpufreq policy change */
352static struct notifier_block thermal_cpufreq_notifier_block = {
353 .notifier_call = cpufreq_thermal_notifier,
354};
355
Viresh Kumarf6859012014-12-04 09:42:06 +0530356static unsigned int find_next_max(struct cpufreq_frequency_table *table,
357 unsigned int prev_max)
358{
359 struct cpufreq_frequency_table *pos;
360 unsigned int max = 0;
361
362 cpufreq_for_each_valid_entry(pos, table) {
363 if (pos->frequency > max && pos->frequency < prev_max)
364 max = pos->frequency;
365 }
366
367 return max;
368}
369
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530370/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400371 * __cpufreq_cooling_register - helper function to create cpufreq cooling device
372 * @np: a valid struct device_node to the cooling device device tree node
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530373 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
Viresh Kumar405fb822014-12-04 09:41:55 +0530374 * Normally this should be same as cpufreq policy->related_cpus.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000375 *
376 * This interface function registers the cpufreq cooling device with the name
377 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400378 * cooling devices. It also gives the opportunity to link the cooling device
379 * with a device tree node, in order to bind it via the thermal DT code.
Eduardo Valentin12cb08b2013-04-17 17:12:15 +0000380 *
381 * Return: a valid struct thermal_cooling_device pointer on success,
382 * on failure, it returns a corresponding ERR_PTR().
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530383 */
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400384static struct thermal_cooling_device *
385__cpufreq_cooling_register(struct device_node *np,
386 const struct cpumask *clip_cpus)
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530387{
388 struct thermal_cooling_device *cool_dev;
Viresh Kumar5d3bdb82014-12-04 09:41:52 +0530389 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530390 char dev_name[THERMAL_NAME_LENGTH];
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530391 struct cpufreq_frequency_table *pos, *table;
Viresh Kumarf6859012014-12-04 09:42:06 +0530392 unsigned int freq, i;
Viresh Kumar405fb822014-12-04 09:41:55 +0530393 int ret;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530394
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530395 table = cpufreq_frequency_get_table(cpumask_first(clip_cpus));
396 if (!table) {
Eduardo Valentin0f1be512014-12-04 09:41:43 +0530397 pr_debug("%s: CPUFreq table not found\n", __func__);
398 return ERR_PTR(-EPROBE_DEFER);
399 }
400
Viresh Kumar98d522f2014-12-04 09:41:50 +0530401 cpufreq_dev = kzalloc(sizeof(*cpufreq_dev), GFP_KERNEL);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530402 if (!cpufreq_dev)
403 return ERR_PTR(-ENOMEM);
404
Viresh Kumarb9f8b412014-12-04 09:42:05 +0530405 ret = get_property(cpufreq_dev, 0, &cpufreq_dev->cpufreq_val, GET_FREQ);
Viresh Kumar521a2e52014-12-04 09:42:01 +0530406 if (ret) {
407 pr_err("%s: Failed to get frequency: %d", __func__, ret);
408 cool_dev = ERR_PTR(ret);
Viresh Kumar7adb6352014-12-04 09:41:59 +0530409 goto free_cdev;
410 }
411
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530412 /* Find max levels */
413 cpufreq_for_each_valid_entry(pos, table)
414 cpufreq_dev->max_level++;
415
Viresh Kumarf6859012014-12-04 09:42:06 +0530416 cpufreq_dev->freq_table = kmalloc(sizeof(*cpufreq_dev->freq_table) *
417 cpufreq_dev->max_level, GFP_KERNEL);
418 if (!cpufreq_dev->freq_table) {
419 return ERR_PTR(-ENOMEM);
420 cool_dev = ERR_PTR(-ENOMEM);
421 goto free_cdev;
422 }
423
Viresh Kumardcc6c7f2014-12-04 09:42:02 +0530424 /* max_level is an index, not a counter */
425 cpufreq_dev->max_level--;
426
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530427 cpumask_copy(&cpufreq_dev->allowed_cpus, clip_cpus);
428
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530429 ret = get_idr(&cpufreq_idr, &cpufreq_dev->id);
430 if (ret) {
Viresh Kumar730abe02014-12-04 09:41:58 +0530431 cool_dev = ERR_PTR(ret);
Viresh Kumarf6859012014-12-04 09:42:06 +0530432 goto free_table;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530433 }
434
Eduardo Valentin99871a72013-04-17 17:12:17 +0000435 snprintf(dev_name, sizeof(dev_name), "thermal-cpufreq-%d",
436 cpufreq_dev->id);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530437
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400438 cool_dev = thermal_of_cooling_device_register(np, dev_name, cpufreq_dev,
439 &cpufreq_cooling_ops);
Viresh Kumar730abe02014-12-04 09:41:58 +0530440 if (IS_ERR(cool_dev))
441 goto remove_idr;
442
Viresh Kumarf6859012014-12-04 09:42:06 +0530443 /* Fill freq-table in descending order of frequencies */
444 for (i = 0, freq = -1; i <= cpufreq_dev->max_level; i++) {
445 freq = find_next_max(table, freq);
446 cpufreq_dev->freq_table[i] = freq;
447
448 /* Warn for duplicate entries */
449 if (!freq)
450 pr_warn("%s: table has duplicate entries\n", __func__);
451 else
452 pr_debug("%s: freq:%u KHz\n", __func__, freq);
453 }
454
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530455 cpufreq_dev->cool_dev = cool_dev;
Viresh Kumar92e615e2014-12-04 09:41:51 +0530456
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530457 mutex_lock(&cooling_cpufreq_lock);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530458
459 /* Register the notifier for first cpufreq cooling device */
Viresh Kumar2479bb642014-12-04 09:42:04 +0530460 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530461 cpufreq_register_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000462 CPUFREQ_POLICY_NOTIFIER);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530463 list_add(&cpufreq_dev->node, &cpufreq_dev_list);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530464
465 mutex_unlock(&cooling_cpufreq_lock);
Eduardo Valentin79491e52013-04-17 17:11:59 +0000466
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530467 return cool_dev;
Viresh Kumar730abe02014-12-04 09:41:58 +0530468
469remove_idr:
470 release_idr(&cpufreq_idr, cpufreq_dev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530471free_table:
472 kfree(cpufreq_dev->freq_table);
Viresh Kumar730abe02014-12-04 09:41:58 +0530473free_cdev:
474 kfree(cpufreq_dev);
475
476 return cool_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530477}
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400478
479/**
480 * cpufreq_cooling_register - function to create cpufreq cooling device.
481 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
482 *
483 * This interface function registers the cpufreq cooling device with the name
484 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
485 * cooling devices.
486 *
487 * Return: a valid struct thermal_cooling_device pointer on success,
488 * on failure, it returns a corresponding ERR_PTR().
489 */
490struct thermal_cooling_device *
491cpufreq_cooling_register(const struct cpumask *clip_cpus)
492{
493 return __cpufreq_cooling_register(NULL, clip_cpus);
494}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000495EXPORT_SYMBOL_GPL(cpufreq_cooling_register);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530496
497/**
Eduardo Valentin39d99cf2013-09-12 19:26:45 -0400498 * of_cpufreq_cooling_register - function to create cpufreq cooling device.
499 * @np: a valid struct device_node to the cooling device device tree node
500 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
501 *
502 * This interface function registers the cpufreq cooling device with the name
503 * "thermal-cpufreq-%x". This api can support multiple instances of cpufreq
504 * cooling devices. Using this API, the cpufreq cooling device will be
505 * linked to the device tree node provided.
506 *
507 * Return: a valid struct thermal_cooling_device pointer on success,
508 * on failure, it returns a corresponding ERR_PTR().
509 */
510struct thermal_cooling_device *
511of_cpufreq_cooling_register(struct device_node *np,
512 const struct cpumask *clip_cpus)
513{
514 if (!np)
515 return ERR_PTR(-EINVAL);
516
517 return __cpufreq_cooling_register(np, clip_cpus);
518}
519EXPORT_SYMBOL_GPL(of_cpufreq_cooling_register);
520
521/**
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530522 * cpufreq_cooling_unregister - function to remove cpufreq cooling device.
523 * @cdev: thermal cooling device pointer.
Eduardo Valentin135266b2013-04-17 17:12:16 +0000524 *
525 * This interface function unregisters the "thermal-cpufreq-%x" cooling device.
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530526 */
527void cpufreq_cooling_unregister(struct thermal_cooling_device *cdev)
528{
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400529 struct cpufreq_cooling_device *cpufreq_dev;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530530
Eduardo Valentin50e66c72013-08-15 10:54:46 -0400531 if (!cdev)
532 return;
533
534 cpufreq_dev = cdev->devdata;
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530535 mutex_lock(&cooling_cpufreq_lock);
Yadwinder Singh Brar2dcd8512014-11-07 19:12:29 +0530536 list_del(&cpufreq_dev->node);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530537
538 /* Unregister the notifier for the last cpufreq cooling device */
Viresh Kumar2479bb642014-12-04 09:42:04 +0530539 if (list_empty(&cpufreq_dev_list))
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530540 cpufreq_unregister_notifier(&thermal_cpufreq_notifier_block,
Eduardo Valentin5fda7f62013-04-17 17:12:11 +0000541 CPUFREQ_POLICY_NOTIFIER);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530542 mutex_unlock(&cooling_cpufreq_lock);
hongbo.zhang160b7d82012-10-30 17:48:59 +0100543
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530544 thermal_cooling_device_unregister(cpufreq_dev->cool_dev);
545 release_idr(&cpufreq_idr, cpufreq_dev->id);
Viresh Kumarf6859012014-12-04 09:42:06 +0530546 kfree(cpufreq_dev->freq_table);
Amit Daniel Kachhap02361412012-08-16 17:11:40 +0530547 kfree(cpufreq_dev);
548}
Eduardo Valentin243dbd92013-04-17 17:11:57 +0000549EXPORT_SYMBOL_GPL(cpufreq_cooling_unregister);