2 * drivers/thermal/clock_cooling.c
4 * Copyright (C) 2014 Eduardo Valentin <edubezval@gmail.com>
6 * Copyright (C) 2013 Texas Instruments Inc.
7 * Contact: Eduardo Valentin <eduardo.valentin@ti.com>
9 * Highly based on cpu_cooling.c.
10 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
11 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 2 of the License.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 #include <linux/clk.h>
23 #include <linux/cpufreq.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26 #include <linux/idr.h>
27 #include <linux/mutex.h>
28 #include <linux/pm_opp.h>
29 #include <linux/slab.h>
30 #include <linux/thermal.h>
31 #include <linux/clock_cooling.h>
34 * struct clock_cooling_device - data for cooling device with clock
35 * @id: unique integer value corresponding to each clock_cooling_device
37 * @dev: struct device pointer to the device being used to cool off using
39 * @cdev: thermal_cooling_device pointer to keep track of the
40 * registered cooling device.
41 * @clk_rate_change_nb: reference to notifier block used to receive clock
43 * @freq_table: frequency table used to keep track of available frequencies.
44 * @clock_state: integer value representing the current state of clock
46 * @clock_val: integer value representing the absolute value of the clipped
48 * @clk: struct clk reference used to enforce clock limits.
49 * @lock: mutex lock to protect this struct.
51 * This structure is required for keeping information of each
52 * clock_cooling_device registered. In order to prevent corruption of this a
53 * mutex @lock is used.
55 struct clock_cooling_device {
58 struct thermal_cooling_device *cdev;
59 struct notifier_block clk_rate_change_nb;
60 struct cpufreq_frequency_table *freq_table;
61 unsigned long clock_state;
62 unsigned long clock_val;
64 struct mutex lock; /* lock to protect the content of this struct */
66 #define to_clock_cooling_device(x) \
67 container_of(x, struct clock_cooling_device, clk_rate_change_nb)
68 static DEFINE_IDR(clock_idr);
69 static DEFINE_MUTEX(cooling_clock_lock);
72 * clock_cooling_get_idr - function to get an unique id.
73 * @id: int * value generated by this function.
75 * This function will populate @id with an unique
76 * id, using the idr API.
78 * Return: 0 on success, an error code on failure.
80 static int clock_cooling_get_idr(int *id)
84 mutex_lock(&cooling_clock_lock);
85 ret = idr_alloc(&clock_idr, NULL, 0, 0, GFP_KERNEL);
86 mutex_unlock(&cooling_clock_lock);
87 if (unlikely(ret < 0))
95 * release_idr - function to free the unique id.
96 * @id: int value representing the unique id.
98 static void release_idr(int id)
100 mutex_lock(&cooling_clock_lock);
101 idr_remove(&clock_idr, id);
102 mutex_unlock(&cooling_clock_lock);
105 /* Below code defines functions to be used for clock as cooling device */
107 enum clock_cooling_property {
114 * clock_cooling_get_property - fetch a property of interest for a give cpu.
115 * @ccdev: clock cooling device reference
116 * @input: query parameter
117 * @output: query return
118 * @property: type of query (frequency, level, max level)
120 * This is the common function to
121 * 1. get maximum clock cooling states
122 * 2. translate frequency to cooling state
123 * 3. translate cooling state to frequency
124 * Note that the code may be not in good shape
125 * but it is written in this way in order to:
126 * a) reduce duplicate code as most of the code can be shared.
127 * b) make sure the logic is consistent when translating between
128 * cooling states and frequencies.
130 * Return: 0 on success, -EINVAL when invalid parameters are passed.
132 static int clock_cooling_get_property(struct clock_cooling_device *ccdev,
134 unsigned long *output,
135 enum clock_cooling_property property)
138 unsigned long max_level = 0, level = 0;
139 unsigned int freq = CPUFREQ_ENTRY_INVALID;
141 struct cpufreq_frequency_table *pos, *table = ccdev->freq_table;
149 cpufreq_for_each_valid_entry(pos, table) {
150 /* ignore duplicate entry */
151 if (freq == pos->frequency)
154 /* get the frequency order */
155 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
156 descend = freq > pos->frequency;
158 freq = pos->frequency;
162 /* No valid cpu frequency entry */
166 /* max_level is an index, not a counter */
170 if (property == GET_MAXL) {
175 if (property == GET_FREQ)
176 level = descend ? input : (max_level - input);
179 cpufreq_for_each_valid_entry(pos, table) {
180 /* ignore duplicate entry */
181 if (freq == pos->frequency)
184 /* now we have a valid frequency entry */
185 freq = pos->frequency;
187 if (property == GET_LEVEL && (unsigned int)input == freq) {
188 /* get level by frequency */
189 *output = descend ? i : (max_level - i);
192 if (property == GET_FREQ && level == i) {
193 /* get frequency by level */
204 * clock_cooling_get_level - return the cooling level of given clock cooling.
205 * @cdev: reference of a thermal cooling device of used as clock cooling device
206 * @freq: the frequency of interest
208 * This function will match the cooling level corresponding to the
209 * requested @freq and return it.
211 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
214 unsigned long clock_cooling_get_level(struct thermal_cooling_device *cdev,
217 struct clock_cooling_device *ccdev = cdev->devdata;
220 if (clock_cooling_get_property(ccdev, (unsigned long)freq, &val,
222 return THERMAL_CSTATE_INVALID;
226 EXPORT_SYMBOL_GPL(clock_cooling_get_level);
229 * clock_cooling_get_frequency - get the absolute value of frequency from level.
230 * @ccdev: clock cooling device reference
231 * @level: cooling level
233 * This function matches cooling level with frequency. Based on a cooling level
234 * of frequency, equals cooling state of cpu cooling device, it will return
235 * the corresponding frequency.
236 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
238 * Return: 0 on error, the corresponding frequency otherwise.
241 clock_cooling_get_frequency(struct clock_cooling_device *ccdev,
247 ret = clock_cooling_get_property(ccdev, level, &freq, GET_FREQ);
255 * clock_cooling_apply - function to apply frequency clipping.
256 * @ccdev: clock_cooling_device pointer containing frequency clipping data.
257 * @cooling_state: value of the cooling state.
259 * Function used to make sure the clock layer is aware of current thermal
260 * limits. The limits are applied by updating the clock rate in case it is
261 * higher than the corresponding frequency based on the requested cooling_state.
263 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
266 static int clock_cooling_apply(struct clock_cooling_device *ccdev,
267 unsigned long cooling_state)
269 unsigned long clip_freq, cur_freq;
272 /* Here we write the clipping */
273 /* Check if the old cooling action is same as new cooling action */
274 if (ccdev->clock_state == cooling_state)
277 clip_freq = clock_cooling_get_frequency(ccdev, cooling_state);
281 cur_freq = clk_get_rate(ccdev->clk);
283 mutex_lock(&ccdev->lock);
284 ccdev->clock_state = cooling_state;
285 ccdev->clock_val = clip_freq;
286 /* enforce clock level */
287 if (cur_freq > clip_freq)
288 ret = clk_set_rate(ccdev->clk, clip_freq);
289 mutex_unlock(&ccdev->lock);
295 * clock_cooling_clock_notifier - notifier callback on clock rate changes.
296 * @nb: struct notifier_block * with callback info.
297 * @event: value showing clock event for which this function invoked.
298 * @data: callback-specific data
300 * Callback to hijack the notification on clock transition.
301 * Every time there is a clock change, we intercept all pre change events
302 * and block the transition in case the new rate infringes thermal limits.
304 * Return: NOTIFY_DONE (success) or NOTIFY_BAD (new_rate > thermal limit).
306 static int clock_cooling_clock_notifier(struct notifier_block *nb,
307 unsigned long event, void *data)
309 struct clk_notifier_data *ndata = data;
310 struct clock_cooling_device *ccdev = to_clock_cooling_device(nb);
313 case PRE_RATE_CHANGE:
315 * checks on current state
316 * TODO: current method is not best we can find as it
317 * allows possibly voltage transitions, in case DVFS
318 * layer is also hijacking clock pre notifications.
320 if (ndata->new_rate > ccdev->clock_val)
323 case POST_RATE_CHANGE:
324 case ABORT_RATE_CHANGE:
330 /* clock cooling device thermal callback functions are defined below */
333 * clock_cooling_get_max_state - callback function to get the max cooling state.
334 * @cdev: thermal cooling device pointer.
335 * @state: fill this variable with the max cooling state.
337 * Callback for the thermal cooling device to return the clock
340 * Return: 0 on success, an error code otherwise.
342 static int clock_cooling_get_max_state(struct thermal_cooling_device *cdev,
343 unsigned long *state)
345 struct clock_cooling_device *ccdev = cdev->devdata;
346 unsigned long count = 0;
349 ret = clock_cooling_get_property(ccdev, 0, &count, GET_MAXL);
357 * clock_cooling_get_cur_state - function to get the current cooling state.
358 * @cdev: thermal cooling device pointer.
359 * @state: fill this variable with the current cooling state.
361 * Callback for the thermal cooling device to return the clock
362 * current cooling state.
364 * Return: 0 (success)
366 static int clock_cooling_get_cur_state(struct thermal_cooling_device *cdev,
367 unsigned long *state)
369 struct clock_cooling_device *ccdev = cdev->devdata;
371 *state = ccdev->clock_state;
377 * clock_cooling_set_cur_state - function to set the current cooling state.
378 * @cdev: thermal cooling device pointer.
379 * @state: set this variable to the current cooling state.
381 * Callback for the thermal cooling device to change the clock cooling
382 * current cooling state.
384 * Return: 0 on success, an error code otherwise.
386 static int clock_cooling_set_cur_state(struct thermal_cooling_device *cdev,
389 struct clock_cooling_device *clock_device = cdev->devdata;
391 return clock_cooling_apply(clock_device, state);
394 /* Bind clock callbacks to thermal cooling device ops */
395 static struct thermal_cooling_device_ops const clock_cooling_ops = {
396 .get_max_state = clock_cooling_get_max_state,
397 .get_cur_state = clock_cooling_get_cur_state,
398 .set_cur_state = clock_cooling_set_cur_state,
402 * clock_cooling_register - function to create clock cooling device.
403 * @dev: struct device pointer to the device used as clock cooling device.
404 * @clock_name: string containing the clock used as cooling mechanism.
406 * This interface function registers the clock cooling device with the name
407 * "thermal-clock-%x". The cooling device is based on clock frequencies.
408 * The struct device is assumed to be capable of DVFS transitions.
409 * The OPP layer is used to fetch and fill the available frequencies for
410 * the referred device. The ordered frequency table is used to control
411 * the clock cooling device cooling states and to limit clock transitions
412 * based on the cooling state requested by the thermal framework.
414 * Return: a valid struct thermal_cooling_device pointer on success,
415 * on failure, it returns a corresponding ERR_PTR().
417 struct thermal_cooling_device *
418 clock_cooling_register(struct device *dev, const char *clock_name)
420 struct thermal_cooling_device *cdev;
421 struct clock_cooling_device *ccdev = NULL;
422 char dev_name[THERMAL_NAME_LENGTH];
425 ccdev = devm_kzalloc(dev, sizeof(*ccdev), GFP_KERNEL);
427 return ERR_PTR(-ENOMEM);
429 mutex_init(&ccdev->lock);
431 ccdev->clk = devm_clk_get(dev, clock_name);
432 if (IS_ERR(ccdev->clk))
433 return ERR_CAST(ccdev->clk);
435 ret = clock_cooling_get_idr(&ccdev->id);
437 return ERR_PTR(-EINVAL);
439 snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id);
441 cdev = thermal_cooling_device_register(dev_name, ccdev,
444 release_idr(ccdev->id);
445 return ERR_PTR(-EINVAL);
448 ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier;
450 /* Assuming someone has already filled the opp table for this device */
451 ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table);
453 release_idr(ccdev->id);
456 ccdev->clock_state = 0;
457 ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0);
459 clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb);
463 EXPORT_SYMBOL_GPL(clock_cooling_register);
466 * clock_cooling_unregister - function to remove clock cooling device.
467 * @cdev: thermal cooling device pointer.
469 * This interface function unregisters the "thermal-clock-%x" cooling device.
471 void clock_cooling_unregister(struct thermal_cooling_device *cdev)
473 struct clock_cooling_device *ccdev;
478 ccdev = cdev->devdata;
480 clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb);
481 dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table);
483 thermal_cooling_device_unregister(ccdev->cdev);
484 release_idr(ccdev->id);
486 EXPORT_SYMBOL_GPL(clock_cooling_unregister);