Loading...
1/*
2 * drivers/thermal/clock_cooling.c
3 *
4 * Copyright (C) 2014 Eduardo Valentin <edubezval@gmail.com>
5 *
6 * Copyright (C) 2013 Texas Instruments Inc.
7 * Contact: Eduardo Valentin <eduardo.valentin@ti.com>
8 *
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>
12 *
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.
16 *
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.
21 */
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>
32
33/**
34 * struct clock_cooling_device - data for cooling device with clock
35 * @id: unique integer value corresponding to each clock_cooling_device
36 * registered.
37 * @dev: struct device pointer to the device being used to cool off using
38 * clock frequencies.
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
42 * rate changes.
43 * @freq_table: frequency table used to keep track of available frequencies.
44 * @clock_state: integer value representing the current state of clock
45 * cooling devices.
46 * @clock_val: integer value representing the absolute value of the clipped
47 * frequency.
48 * @clk: struct clk reference used to enforce clock limits.
49 * @lock: mutex lock to protect this struct.
50 *
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.
54 */
55struct clock_cooling_device {
56 int id;
57 struct device *dev;
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;
63 struct clk *clk;
64 struct mutex lock; /* lock to protect the content of this struct */
65};
66#define to_clock_cooling_device(x) \
67 container_of(x, struct clock_cooling_device, clk_rate_change_nb)
68static DEFINE_IDR(clock_idr);
69static DEFINE_MUTEX(cooling_clock_lock);
70
71/**
72 * clock_cooling_get_idr - function to get an unique id.
73 * @id: int * value generated by this function.
74 *
75 * This function will populate @id with an unique
76 * id, using the idr API.
77 *
78 * Return: 0 on success, an error code on failure.
79 */
80static int clock_cooling_get_idr(int *id)
81{
82 int ret;
83
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))
88 return ret;
89 *id = ret;
90
91 return 0;
92}
93
94/**
95 * release_idr - function to free the unique id.
96 * @id: int value representing the unique id.
97 */
98static void release_idr(int id)
99{
100 mutex_lock(&cooling_clock_lock);
101 idr_remove(&clock_idr, id);
102 mutex_unlock(&cooling_clock_lock);
103}
104
105/* Below code defines functions to be used for clock as cooling device */
106
107enum clock_cooling_property {
108 GET_LEVEL,
109 GET_FREQ,
110 GET_MAXL,
111};
112
113/**
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)
119 *
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.
129 *
130 * Return: 0 on success, -EINVAL when invalid parameters are passed.
131 */
132static int clock_cooling_get_property(struct clock_cooling_device *ccdev,
133 unsigned long input,
134 unsigned long *output,
135 enum clock_cooling_property property)
136{
137 int i;
138 unsigned long max_level = 0, level = 0;
139 unsigned int freq = CPUFREQ_ENTRY_INVALID;
140 int descend = -1;
141 struct cpufreq_frequency_table *pos, *table = ccdev->freq_table;
142
143 if (!output)
144 return -EINVAL;
145
146 if (!table)
147 return -EINVAL;
148
149 cpufreq_for_each_valid_entry(pos, table) {
150 /* ignore duplicate entry */
151 if (freq == pos->frequency)
152 continue;
153
154 /* get the frequency order */
155 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
156 descend = freq > pos->frequency;
157
158 freq = pos->frequency;
159 max_level++;
160 }
161
162 /* No valid cpu frequency entry */
163 if (max_level == 0)
164 return -EINVAL;
165
166 /* max_level is an index, not a counter */
167 max_level--;
168
169 /* get max level */
170 if (property == GET_MAXL) {
171 *output = max_level;
172 return 0;
173 }
174
175 if (property == GET_FREQ)
176 level = descend ? input : (max_level - input);
177
178 i = 0;
179 cpufreq_for_each_valid_entry(pos, table) {
180 /* ignore duplicate entry */
181 if (freq == pos->frequency)
182 continue;
183
184 /* now we have a valid frequency entry */
185 freq = pos->frequency;
186
187 if (property == GET_LEVEL && (unsigned int)input == freq) {
188 /* get level by frequency */
189 *output = descend ? i : (max_level - i);
190 return 0;
191 }
192 if (property == GET_FREQ && level == i) {
193 /* get frequency by level */
194 *output = freq;
195 return 0;
196 }
197 i++;
198 }
199
200 return -EINVAL;
201}
202
203/**
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
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 */
214unsigned long clock_cooling_get_level(struct thermal_cooling_device *cdev,
215 unsigned long freq)
216{
217 struct clock_cooling_device *ccdev = cdev->devdata;
218 unsigned long val;
219
220 if (clock_cooling_get_property(ccdev, (unsigned long)freq, &val,
221 GET_LEVEL))
222 return THERMAL_CSTATE_INVALID;
223
224 return val;
225}
226EXPORT_SYMBOL_GPL(clock_cooling_get_level);
227
228/**
229 * clock_cooling_get_frequency - get the absolute value of frequency from level.
230 * @ccdev: clock cooling device reference
231 * @level: cooling level
232 *
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
237 *
238 * Return: 0 on error, the corresponding frequency otherwise.
239 */
240static unsigned long
241clock_cooling_get_frequency(struct clock_cooling_device *ccdev,
242 unsigned long level)
243{
244 int ret = 0;
245 unsigned long freq;
246
247 ret = clock_cooling_get_property(ccdev, level, &freq, GET_FREQ);
248 if (ret)
249 return 0;
250
251 return freq;
252}
253
254/**
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.
258 *
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.
262 *
263 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
264 * cooling state).
265 */
266static int clock_cooling_apply(struct clock_cooling_device *ccdev,
267 unsigned long cooling_state)
268{
269 unsigned long clip_freq, cur_freq;
270 int ret = 0;
271
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)
275 return 0;
276
277 clip_freq = clock_cooling_get_frequency(ccdev, cooling_state);
278 if (!clip_freq)
279 return -EINVAL;
280
281 cur_freq = clk_get_rate(ccdev->clk);
282
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);
290
291 return ret;
292}
293
294/**
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
299 *
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.
303 *
304 * Return: NOTIFY_DONE (success) or NOTIFY_BAD (new_rate > thermal limit).
305 */
306static int clock_cooling_clock_notifier(struct notifier_block *nb,
307 unsigned long event, void *data)
308{
309 struct clk_notifier_data *ndata = data;
310 struct clock_cooling_device *ccdev = to_clock_cooling_device(nb);
311
312 switch (event) {
313 case PRE_RATE_CHANGE:
314 /*
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.
319 */
320 if (ndata->new_rate > ccdev->clock_val)
321 return NOTIFY_BAD;
322 /* fall through */
323 case POST_RATE_CHANGE:
324 case ABORT_RATE_CHANGE:
325 default:
326 return NOTIFY_DONE;
327 }
328}
329
330/* clock cooling device thermal callback functions are defined below */
331
332/**
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.
336 *
337 * Callback for the thermal cooling device to return the clock
338 * max cooling state.
339 *
340 * Return: 0 on success, an error code otherwise.
341 */
342static int clock_cooling_get_max_state(struct thermal_cooling_device *cdev,
343 unsigned long *state)
344{
345 struct clock_cooling_device *ccdev = cdev->devdata;
346 unsigned long count = 0;
347 int ret;
348
349 ret = clock_cooling_get_property(ccdev, 0, &count, GET_MAXL);
350 if (!ret)
351 *state = count;
352
353 return ret;
354}
355
356/**
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.
360 *
361 * Callback for the thermal cooling device to return the clock
362 * current cooling state.
363 *
364 * Return: 0 (success)
365 */
366static int clock_cooling_get_cur_state(struct thermal_cooling_device *cdev,
367 unsigned long *state)
368{
369 struct clock_cooling_device *ccdev = cdev->devdata;
370
371 *state = ccdev->clock_state;
372
373 return 0;
374}
375
376/**
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.
380 *
381 * Callback for the thermal cooling device to change the clock cooling
382 * current cooling state.
383 *
384 * Return: 0 on success, an error code otherwise.
385 */
386static int clock_cooling_set_cur_state(struct thermal_cooling_device *cdev,
387 unsigned long state)
388{
389 struct clock_cooling_device *clock_device = cdev->devdata;
390
391 return clock_cooling_apply(clock_device, state);
392}
393
394/* Bind clock callbacks to thermal cooling device ops */
395static 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,
399};
400
401/**
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.
405 *
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.
413 *
414 * Return: a valid struct thermal_cooling_device pointer on success,
415 * on failure, it returns a corresponding ERR_PTR().
416 */
417struct thermal_cooling_device *
418clock_cooling_register(struct device *dev, const char *clock_name)
419{
420 struct thermal_cooling_device *cdev;
421 struct clock_cooling_device *ccdev = NULL;
422 char dev_name[THERMAL_NAME_LENGTH];
423 int ret = 0;
424
425 ccdev = devm_kzalloc(dev, sizeof(*ccdev), GFP_KERNEL);
426 if (!ccdev)
427 return ERR_PTR(-ENOMEM);
428
429 ccdev->dev = dev;
430 ccdev->clk = devm_clk_get(dev, clock_name);
431 if (IS_ERR(ccdev->clk))
432 return ERR_CAST(ccdev->clk);
433
434 ret = clock_cooling_get_idr(&ccdev->id);
435 if (ret)
436 return ERR_PTR(-EINVAL);
437
438 snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id);
439
440 cdev = thermal_cooling_device_register(dev_name, ccdev,
441 &clock_cooling_ops);
442 if (IS_ERR(cdev)) {
443 release_idr(ccdev->id);
444 return ERR_PTR(-EINVAL);
445 }
446 ccdev->cdev = cdev;
447 ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier;
448
449 /* Assuming someone has already filled the opp table for this device */
450 ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table);
451 if (ret) {
452 release_idr(ccdev->id);
453 return ERR_PTR(ret);
454 }
455 ccdev->clock_state = 0;
456 ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0);
457
458 clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb);
459
460 return cdev;
461}
462EXPORT_SYMBOL_GPL(clock_cooling_register);
463
464/**
465 * clock_cooling_unregister - function to remove clock cooling device.
466 * @cdev: thermal cooling device pointer.
467 *
468 * This interface function unregisters the "thermal-clock-%x" cooling device.
469 */
470void clock_cooling_unregister(struct thermal_cooling_device *cdev)
471{
472 struct clock_cooling_device *ccdev;
473
474 if (!cdev)
475 return;
476
477 ccdev = cdev->devdata;
478
479 clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb);
480 dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table);
481
482 thermal_cooling_device_unregister(ccdev->cdev);
483 release_idr(ccdev->id);
484}
485EXPORT_SYMBOL_GPL(clock_cooling_unregister);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * drivers/thermal/clock_cooling.c
4 *
5 * Copyright (C) 2014 Eduardo Valentin <edubezval@gmail.com>
6 *
7 * Copyright (C) 2013 Texas Instruments Inc.
8 * Contact: Eduardo Valentin <eduardo.valentin@ti.com>
9 *
10 * Highly based on cpu_cooling.c.
11 * Copyright (C) 2012 Samsung Electronics Co., Ltd(http://www.samsung.com)
12 * Copyright (C) 2012 Amit Daniel <amit.kachhap@linaro.org>
13 */
14#include <linux/clk.h>
15#include <linux/cpufreq.h>
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/idr.h>
19#include <linux/mutex.h>
20#include <linux/pm_opp.h>
21#include <linux/slab.h>
22#include <linux/thermal.h>
23#include <linux/clock_cooling.h>
24
25/**
26 * struct clock_cooling_device - data for cooling device with clock
27 * @id: unique integer value corresponding to each clock_cooling_device
28 * registered.
29 * @dev: struct device pointer to the device being used to cool off using
30 * clock frequencies.
31 * @cdev: thermal_cooling_device pointer to keep track of the
32 * registered cooling device.
33 * @clk_rate_change_nb: reference to notifier block used to receive clock
34 * rate changes.
35 * @freq_table: frequency table used to keep track of available frequencies.
36 * @clock_state: integer value representing the current state of clock
37 * cooling devices.
38 * @clock_val: integer value representing the absolute value of the clipped
39 * frequency.
40 * @clk: struct clk reference used to enforce clock limits.
41 * @lock: mutex lock to protect this struct.
42 *
43 * This structure is required for keeping information of each
44 * clock_cooling_device registered. In order to prevent corruption of this a
45 * mutex @lock is used.
46 */
47struct clock_cooling_device {
48 int id;
49 struct device *dev;
50 struct thermal_cooling_device *cdev;
51 struct notifier_block clk_rate_change_nb;
52 struct cpufreq_frequency_table *freq_table;
53 unsigned long clock_state;
54 unsigned long clock_val;
55 struct clk *clk;
56 struct mutex lock; /* lock to protect the content of this struct */
57};
58#define to_clock_cooling_device(x) \
59 container_of(x, struct clock_cooling_device, clk_rate_change_nb)
60static DEFINE_IDA(clock_ida);
61
62/* Below code defines functions to be used for clock as cooling device */
63
64enum clock_cooling_property {
65 GET_LEVEL,
66 GET_FREQ,
67 GET_MAXL,
68};
69
70/**
71 * clock_cooling_get_property - fetch a property of interest for a give cpu.
72 * @ccdev: clock cooling device reference
73 * @input: query parameter
74 * @output: query return
75 * @property: type of query (frequency, level, max level)
76 *
77 * This is the common function to
78 * 1. get maximum clock cooling states
79 * 2. translate frequency to cooling state
80 * 3. translate cooling state to frequency
81 * Note that the code may be not in good shape
82 * but it is written in this way in order to:
83 * a) reduce duplicate code as most of the code can be shared.
84 * b) make sure the logic is consistent when translating between
85 * cooling states and frequencies.
86 *
87 * Return: 0 on success, -EINVAL when invalid parameters are passed.
88 */
89static int clock_cooling_get_property(struct clock_cooling_device *ccdev,
90 unsigned long input,
91 unsigned long *output,
92 enum clock_cooling_property property)
93{
94 int i;
95 unsigned long max_level = 0, level = 0;
96 unsigned int freq = CPUFREQ_ENTRY_INVALID;
97 int descend = -1;
98 struct cpufreq_frequency_table *pos, *table = ccdev->freq_table;
99
100 if (!output)
101 return -EINVAL;
102
103 if (!table)
104 return -EINVAL;
105
106 cpufreq_for_each_valid_entry(pos, table) {
107 /* ignore duplicate entry */
108 if (freq == pos->frequency)
109 continue;
110
111 /* get the frequency order */
112 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
113 descend = freq > pos->frequency;
114
115 freq = pos->frequency;
116 max_level++;
117 }
118
119 /* No valid cpu frequency entry */
120 if (max_level == 0)
121 return -EINVAL;
122
123 /* max_level is an index, not a counter */
124 max_level--;
125
126 /* get max level */
127 if (property == GET_MAXL) {
128 *output = max_level;
129 return 0;
130 }
131
132 if (property == GET_FREQ)
133 level = descend ? input : (max_level - input);
134
135 i = 0;
136 cpufreq_for_each_valid_entry(pos, table) {
137 /* ignore duplicate entry */
138 if (freq == pos->frequency)
139 continue;
140
141 /* now we have a valid frequency entry */
142 freq = pos->frequency;
143
144 if (property == GET_LEVEL && (unsigned int)input == freq) {
145 /* get level by frequency */
146 *output = descend ? i : (max_level - i);
147 return 0;
148 }
149 if (property == GET_FREQ && level == i) {
150 /* get frequency by level */
151 *output = freq;
152 return 0;
153 }
154 i++;
155 }
156
157 return -EINVAL;
158}
159
160/**
161 * clock_cooling_get_level - return the cooling level of given clock cooling.
162 * @cdev: reference of a thermal cooling device of used as clock cooling device
163 * @freq: the frequency of interest
164 *
165 * This function will match the cooling level corresponding to the
166 * requested @freq and return it.
167 *
168 * Return: The matched cooling level on success or THERMAL_CSTATE_INVALID
169 * otherwise.
170 */
171unsigned long clock_cooling_get_level(struct thermal_cooling_device *cdev,
172 unsigned long freq)
173{
174 struct clock_cooling_device *ccdev = cdev->devdata;
175 unsigned long val;
176
177 if (clock_cooling_get_property(ccdev, (unsigned long)freq, &val,
178 GET_LEVEL))
179 return THERMAL_CSTATE_INVALID;
180
181 return val;
182}
183EXPORT_SYMBOL_GPL(clock_cooling_get_level);
184
185/**
186 * clock_cooling_get_frequency - get the absolute value of frequency from level.
187 * @ccdev: clock cooling device reference
188 * @level: cooling level
189 *
190 * This function matches cooling level with frequency. Based on a cooling level
191 * of frequency, equals cooling state of cpu cooling device, it will return
192 * the corresponding frequency.
193 * e.g level=0 --> 1st MAX FREQ, level=1 ---> 2nd MAX FREQ, .... etc
194 *
195 * Return: 0 on error, the corresponding frequency otherwise.
196 */
197static unsigned long
198clock_cooling_get_frequency(struct clock_cooling_device *ccdev,
199 unsigned long level)
200{
201 int ret = 0;
202 unsigned long freq;
203
204 ret = clock_cooling_get_property(ccdev, level, &freq, GET_FREQ);
205 if (ret)
206 return 0;
207
208 return freq;
209}
210
211/**
212 * clock_cooling_apply - function to apply frequency clipping.
213 * @ccdev: clock_cooling_device pointer containing frequency clipping data.
214 * @cooling_state: value of the cooling state.
215 *
216 * Function used to make sure the clock layer is aware of current thermal
217 * limits. The limits are applied by updating the clock rate in case it is
218 * higher than the corresponding frequency based on the requested cooling_state.
219 *
220 * Return: 0 on success, an error code otherwise (-EINVAL in case wrong
221 * cooling state).
222 */
223static int clock_cooling_apply(struct clock_cooling_device *ccdev,
224 unsigned long cooling_state)
225{
226 unsigned long clip_freq, cur_freq;
227 int ret = 0;
228
229 /* Here we write the clipping */
230 /* Check if the old cooling action is same as new cooling action */
231 if (ccdev->clock_state == cooling_state)
232 return 0;
233
234 clip_freq = clock_cooling_get_frequency(ccdev, cooling_state);
235 if (!clip_freq)
236 return -EINVAL;
237
238 cur_freq = clk_get_rate(ccdev->clk);
239
240 mutex_lock(&ccdev->lock);
241 ccdev->clock_state = cooling_state;
242 ccdev->clock_val = clip_freq;
243 /* enforce clock level */
244 if (cur_freq > clip_freq)
245 ret = clk_set_rate(ccdev->clk, clip_freq);
246 mutex_unlock(&ccdev->lock);
247
248 return ret;
249}
250
251/**
252 * clock_cooling_clock_notifier - notifier callback on clock rate changes.
253 * @nb: struct notifier_block * with callback info.
254 * @event: value showing clock event for which this function invoked.
255 * @data: callback-specific data
256 *
257 * Callback to hijack the notification on clock transition.
258 * Every time there is a clock change, we intercept all pre change events
259 * and block the transition in case the new rate infringes thermal limits.
260 *
261 * Return: NOTIFY_DONE (success) or NOTIFY_BAD (new_rate > thermal limit).
262 */
263static int clock_cooling_clock_notifier(struct notifier_block *nb,
264 unsigned long event, void *data)
265{
266 struct clk_notifier_data *ndata = data;
267 struct clock_cooling_device *ccdev = to_clock_cooling_device(nb);
268
269 switch (event) {
270 case PRE_RATE_CHANGE:
271 /*
272 * checks on current state
273 * TODO: current method is not best we can find as it
274 * allows possibly voltage transitions, in case DVFS
275 * layer is also hijacking clock pre notifications.
276 */
277 if (ndata->new_rate > ccdev->clock_val)
278 return NOTIFY_BAD;
279 /* fall through */
280 case POST_RATE_CHANGE:
281 case ABORT_RATE_CHANGE:
282 default:
283 return NOTIFY_DONE;
284 }
285}
286
287/* clock cooling device thermal callback functions are defined below */
288
289/**
290 * clock_cooling_get_max_state - callback function to get the max cooling state.
291 * @cdev: thermal cooling device pointer.
292 * @state: fill this variable with the max cooling state.
293 *
294 * Callback for the thermal cooling device to return the clock
295 * max cooling state.
296 *
297 * Return: 0 on success, an error code otherwise.
298 */
299static int clock_cooling_get_max_state(struct thermal_cooling_device *cdev,
300 unsigned long *state)
301{
302 struct clock_cooling_device *ccdev = cdev->devdata;
303 unsigned long count = 0;
304 int ret;
305
306 ret = clock_cooling_get_property(ccdev, 0, &count, GET_MAXL);
307 if (!ret)
308 *state = count;
309
310 return ret;
311}
312
313/**
314 * clock_cooling_get_cur_state - function to get the current cooling state.
315 * @cdev: thermal cooling device pointer.
316 * @state: fill this variable with the current cooling state.
317 *
318 * Callback for the thermal cooling device to return the clock
319 * current cooling state.
320 *
321 * Return: 0 (success)
322 */
323static int clock_cooling_get_cur_state(struct thermal_cooling_device *cdev,
324 unsigned long *state)
325{
326 struct clock_cooling_device *ccdev = cdev->devdata;
327
328 *state = ccdev->clock_state;
329
330 return 0;
331}
332
333/**
334 * clock_cooling_set_cur_state - function to set the current cooling state.
335 * @cdev: thermal cooling device pointer.
336 * @state: set this variable to the current cooling state.
337 *
338 * Callback for the thermal cooling device to change the clock cooling
339 * current cooling state.
340 *
341 * Return: 0 on success, an error code otherwise.
342 */
343static int clock_cooling_set_cur_state(struct thermal_cooling_device *cdev,
344 unsigned long state)
345{
346 struct clock_cooling_device *clock_device = cdev->devdata;
347
348 return clock_cooling_apply(clock_device, state);
349}
350
351/* Bind clock callbacks to thermal cooling device ops */
352static struct thermal_cooling_device_ops const clock_cooling_ops = {
353 .get_max_state = clock_cooling_get_max_state,
354 .get_cur_state = clock_cooling_get_cur_state,
355 .set_cur_state = clock_cooling_set_cur_state,
356};
357
358/**
359 * clock_cooling_register - function to create clock cooling device.
360 * @dev: struct device pointer to the device used as clock cooling device.
361 * @clock_name: string containing the clock used as cooling mechanism.
362 *
363 * This interface function registers the clock cooling device with the name
364 * "thermal-clock-%x". The cooling device is based on clock frequencies.
365 * The struct device is assumed to be capable of DVFS transitions.
366 * The OPP layer is used to fetch and fill the available frequencies for
367 * the referred device. The ordered frequency table is used to control
368 * the clock cooling device cooling states and to limit clock transitions
369 * based on the cooling state requested by the thermal framework.
370 *
371 * Return: a valid struct thermal_cooling_device pointer on success,
372 * on failure, it returns a corresponding ERR_PTR().
373 */
374struct thermal_cooling_device *
375clock_cooling_register(struct device *dev, const char *clock_name)
376{
377 struct thermal_cooling_device *cdev;
378 struct clock_cooling_device *ccdev = NULL;
379 char dev_name[THERMAL_NAME_LENGTH];
380 int ret = 0;
381
382 ccdev = devm_kzalloc(dev, sizeof(*ccdev), GFP_KERNEL);
383 if (!ccdev)
384 return ERR_PTR(-ENOMEM);
385
386 mutex_init(&ccdev->lock);
387 ccdev->dev = dev;
388 ccdev->clk = devm_clk_get(dev, clock_name);
389 if (IS_ERR(ccdev->clk))
390 return ERR_CAST(ccdev->clk);
391
392 ret = ida_simple_get(&clock_ida, 0, 0, GFP_KERNEL);
393 if (ret < 0)
394 return ERR_PTR(ret);
395 ccdev->id = ret;
396
397 snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id);
398
399 cdev = thermal_cooling_device_register(dev_name, ccdev,
400 &clock_cooling_ops);
401 if (IS_ERR(cdev)) {
402 ida_simple_remove(&clock_ida, ccdev->id);
403 return ERR_PTR(-EINVAL);
404 }
405 ccdev->cdev = cdev;
406 ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier;
407
408 /* Assuming someone has already filled the opp table for this device */
409 ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table);
410 if (ret) {
411 ida_simple_remove(&clock_ida, ccdev->id);
412 return ERR_PTR(ret);
413 }
414 ccdev->clock_state = 0;
415 ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0);
416
417 clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb);
418
419 return cdev;
420}
421EXPORT_SYMBOL_GPL(clock_cooling_register);
422
423/**
424 * clock_cooling_unregister - function to remove clock cooling device.
425 * @cdev: thermal cooling device pointer.
426 *
427 * This interface function unregisters the "thermal-clock-%x" cooling device.
428 */
429void clock_cooling_unregister(struct thermal_cooling_device *cdev)
430{
431 struct clock_cooling_device *ccdev;
432
433 if (!cdev)
434 return;
435
436 ccdev = cdev->devdata;
437
438 clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb);
439 dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table);
440
441 thermal_cooling_device_unregister(ccdev->cdev);
442 ida_simple_remove(&clock_ida, ccdev->id);
443}
444EXPORT_SYMBOL_GPL(clock_cooling_unregister);