1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
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
73
74
75
76
77
78
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
96
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
106
107enum clock_cooling_property {
108 GET_LEVEL,
109 GET_FREQ,
110 GET_MAXL,
111};
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
151 if (freq == pos->frequency)
152 continue;
153
154
155 if (freq != CPUFREQ_ENTRY_INVALID && descend == -1)
156 descend = freq > pos->frequency;
157
158 freq = pos->frequency;
159 max_level++;
160 }
161
162
163 if (max_level == 0)
164 return -EINVAL;
165
166
167 max_level--;
168
169
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
181 if (freq == pos->frequency)
182 continue;
183
184
185 freq = pos->frequency;
186
187 if (property == GET_LEVEL && (unsigned int)input == freq) {
188
189 *output = descend ? i : (max_level - i);
190 return 0;
191 }
192 if (property == GET_FREQ && level == i) {
193
194 *output = freq;
195 return 0;
196 }
197 i++;
198 }
199
200 return -EINVAL;
201}
202
203
204
205
206
207
208
209
210
211
212
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
230
231
232
233
234
235
236
237
238
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
256
257
258
259
260
261
262
263
264
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
273
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
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
296
297
298
299
300
301
302
303
304
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
316
317
318
319
320 if (ndata->new_rate > ccdev->clock_val)
321 return NOTIFY_BAD;
322
323 case POST_RATE_CHANGE:
324 case ABORT_RATE_CHANGE:
325 default:
326 return NOTIFY_DONE;
327 }
328}
329
330
331
332
333
334
335
336
337
338
339
340
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
358
359
360
361
362
363
364
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
378
379
380
381
382
383
384
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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
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 mutex_init(&ccdev->lock);
430 ccdev->dev = dev;
431 ccdev->clk = devm_clk_get(dev, clock_name);
432 if (IS_ERR(ccdev->clk))
433 return ERR_CAST(ccdev->clk);
434
435 ret = clock_cooling_get_idr(&ccdev->id);
436 if (ret)
437 return ERR_PTR(-EINVAL);
438
439 snprintf(dev_name, sizeof(dev_name), "thermal-clock-%d", ccdev->id);
440
441 cdev = thermal_cooling_device_register(dev_name, ccdev,
442 &clock_cooling_ops);
443 if (IS_ERR(cdev)) {
444 release_idr(ccdev->id);
445 return ERR_PTR(-EINVAL);
446 }
447 ccdev->cdev = cdev;
448 ccdev->clk_rate_change_nb.notifier_call = clock_cooling_clock_notifier;
449
450
451 ret = dev_pm_opp_init_cpufreq_table(dev, &ccdev->freq_table);
452 if (ret) {
453 release_idr(ccdev->id);
454 return ERR_PTR(ret);
455 }
456 ccdev->clock_state = 0;
457 ccdev->clock_val = clock_cooling_get_frequency(ccdev, 0);
458
459 clk_notifier_register(ccdev->clk, &ccdev->clk_rate_change_nb);
460
461 return cdev;
462}
463EXPORT_SYMBOL_GPL(clock_cooling_register);
464
465
466
467
468
469
470
471void clock_cooling_unregister(struct thermal_cooling_device *cdev)
472{
473 struct clock_cooling_device *ccdev;
474
475 if (!cdev)
476 return;
477
478 ccdev = cdev->devdata;
479
480 clk_notifier_unregister(ccdev->clk, &ccdev->clk_rate_change_nb);
481 dev_pm_opp_free_cpufreq_table(ccdev->dev, &ccdev->freq_table);
482
483 thermal_cooling_device_unregister(ccdev->cdev);
484 release_idr(ccdev->id);
485}
486EXPORT_SYMBOL_GPL(clock_cooling_unregister);
487