1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4 * for Non-CPU Devices.
5 *
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/devfreq_cooling.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/pm_opp.h>
22 #include <linux/devfreq.h>
23 #include <linux/devfreq-governor.h>
24 #include <linux/workqueue.h>
25 #include <linux/platform_device.h>
26 #include <linux/list.h>
27 #include <linux/printk.h>
28 #include <linux/hrtimer.h>
29 #include <linux/of.h>
30 #include <linux/pm_qos.h>
31 #include <linux/units.h>
32
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/devfreq.h>
35
36 #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false)
37 #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false)
38
39 static struct class *devfreq_class;
40 static struct dentry *devfreq_debugfs;
41 static const struct attribute_group gov_attr_group;
42
43 /*
44 * devfreq core provides delayed work based load monitoring helper
45 * functions. Governors can use these or can implement their own
46 * monitoring mechanism.
47 */
48 static struct workqueue_struct *devfreq_wq;
49
50 /* The list of all device-devfreq governors */
51 static LIST_HEAD(devfreq_governor_list);
52 /* The list of all device-devfreq */
53 static LIST_HEAD(devfreq_list);
54 static DEFINE_MUTEX(devfreq_list_lock);
55
56 static const char timer_name[][DEVFREQ_NAME_LEN] = {
57 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
58 [DEVFREQ_TIMER_DELAYED] = { "delayed" },
59 };
60
61 /**
62 * find_device_devfreq() - find devfreq struct using device pointer
63 * @dev: device pointer used to lookup device devfreq.
64 *
65 * Search the list of device devfreqs and return the matched device's
66 * devfreq info. devfreq_list_lock should be held by the caller.
67 */
find_device_devfreq(struct device * dev)68 static struct devfreq *find_device_devfreq(struct device *dev)
69 {
70 struct devfreq *tmp_devfreq;
71
72 lockdep_assert_held(&devfreq_list_lock);
73
74 if (IS_ERR_OR_NULL(dev)) {
75 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
76 return ERR_PTR(-EINVAL);
77 }
78
79 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
80 if (tmp_devfreq->dev.parent == dev)
81 return tmp_devfreq;
82 }
83
84 return ERR_PTR(-ENODEV);
85 }
86
find_available_min_freq(struct devfreq * devfreq)87 static unsigned long find_available_min_freq(struct devfreq *devfreq)
88 {
89 struct dev_pm_opp *opp;
90 unsigned long min_freq = 0;
91
92 opp = dev_pm_opp_find_freq_ceil_indexed(devfreq->dev.parent, &min_freq, 0);
93 if (IS_ERR(opp))
94 min_freq = 0;
95 else
96 dev_pm_opp_put(opp);
97
98 return min_freq;
99 }
100
find_available_max_freq(struct devfreq * devfreq)101 static unsigned long find_available_max_freq(struct devfreq *devfreq)
102 {
103 struct dev_pm_opp *opp;
104 unsigned long max_freq = ULONG_MAX;
105
106 opp = dev_pm_opp_find_freq_floor_indexed(devfreq->dev.parent, &max_freq, 0);
107 if (IS_ERR(opp))
108 max_freq = 0;
109 else
110 dev_pm_opp_put(opp);
111
112 return max_freq;
113 }
114
115 /**
116 * devfreq_get_freq_range() - Get the current freq range
117 * @devfreq: the devfreq instance
118 * @min_freq: the min frequency
119 * @max_freq: the max frequency
120 *
121 * This takes into consideration all constraints.
122 */
devfreq_get_freq_range(struct devfreq * devfreq,unsigned long * min_freq,unsigned long * max_freq)123 void devfreq_get_freq_range(struct devfreq *devfreq,
124 unsigned long *min_freq,
125 unsigned long *max_freq)
126 {
127 unsigned long *freq_table = devfreq->freq_table;
128 s32 qos_min_freq, qos_max_freq;
129
130 lockdep_assert_held(&devfreq->lock);
131
132 /*
133 * Initialize minimum/maximum frequency from freq table.
134 * The devfreq drivers can initialize this in either ascending or
135 * descending order and devfreq core supports both.
136 */
137 if (freq_table[0] < freq_table[devfreq->max_state - 1]) {
138 *min_freq = freq_table[0];
139 *max_freq = freq_table[devfreq->max_state - 1];
140 } else {
141 *min_freq = freq_table[devfreq->max_state - 1];
142 *max_freq = freq_table[0];
143 }
144
145 /* Apply constraints from PM QoS */
146 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
147 DEV_PM_QOS_MIN_FREQUENCY);
148 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
149 DEV_PM_QOS_MAX_FREQUENCY);
150 *min_freq = max(*min_freq, HZ_PER_KHZ * qos_min_freq);
151 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
152 *max_freq = min(*max_freq, HZ_PER_KHZ * qos_max_freq);
153
154 /* Apply constraints from OPP interface */
155 *max_freq = clamp(*max_freq, devfreq->scaling_min_freq, devfreq->scaling_max_freq);
156 *min_freq = clamp(*min_freq, devfreq->scaling_min_freq, *max_freq);
157 }
158 EXPORT_SYMBOL(devfreq_get_freq_range);
159
160 /**
161 * devfreq_get_freq_level() - Lookup freq_table for the frequency
162 * @devfreq: the devfreq instance
163 * @freq: the target frequency
164 */
devfreq_get_freq_level(struct devfreq * devfreq,unsigned long freq)165 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
166 {
167 int lev;
168
169 for (lev = 0; lev < devfreq->max_state; lev++)
170 if (freq == devfreq->freq_table[lev])
171 return lev;
172
173 return -EINVAL;
174 }
175
set_freq_table(struct devfreq * devfreq)176 static int set_freq_table(struct devfreq *devfreq)
177 {
178 struct dev_pm_opp *opp;
179 unsigned long freq;
180 int i, count;
181
182 /* Initialize the freq_table from OPP table */
183 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
184 if (count <= 0)
185 return -EINVAL;
186
187 devfreq->max_state = count;
188 devfreq->freq_table = devm_kcalloc(devfreq->dev.parent,
189 devfreq->max_state,
190 sizeof(*devfreq->freq_table),
191 GFP_KERNEL);
192 if (!devfreq->freq_table)
193 return -ENOMEM;
194
195 for (i = 0, freq = 0; i < devfreq->max_state; i++, freq++) {
196 opp = dev_pm_opp_find_freq_ceil_indexed(devfreq->dev.parent, &freq, 0);
197 if (IS_ERR(opp)) {
198 devm_kfree(devfreq->dev.parent, devfreq->freq_table);
199 return PTR_ERR(opp);
200 }
201 dev_pm_opp_put(opp);
202 devfreq->freq_table[i] = freq;
203 }
204
205 return 0;
206 }
207
208 /**
209 * devfreq_update_status() - Update statistics of devfreq behavior
210 * @devfreq: the devfreq instance
211 * @freq: the update target frequency
212 */
devfreq_update_status(struct devfreq * devfreq,unsigned long freq)213 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
214 {
215 int lev, prev_lev, ret = 0;
216 u64 cur_time;
217
218 lockdep_assert_held(&devfreq->lock);
219 cur_time = get_jiffies_64();
220
221 /* Immediately exit if previous_freq is not initialized yet. */
222 if (!devfreq->previous_freq)
223 goto out;
224
225 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
226 if (prev_lev < 0) {
227 ret = prev_lev;
228 goto out;
229 }
230
231 devfreq->stats.time_in_state[prev_lev] +=
232 cur_time - devfreq->stats.last_update;
233
234 lev = devfreq_get_freq_level(devfreq, freq);
235 if (lev < 0) {
236 ret = lev;
237 goto out;
238 }
239
240 if (lev != prev_lev) {
241 devfreq->stats.trans_table[
242 (prev_lev * devfreq->max_state) + lev]++;
243 devfreq->stats.total_trans++;
244 }
245
246 out:
247 devfreq->stats.last_update = cur_time;
248 return ret;
249 }
250 EXPORT_SYMBOL(devfreq_update_status);
251
252 /**
253 * find_devfreq_governor() - find devfreq governor from name
254 * @name: name of the governor
255 *
256 * Search the list of devfreq governors and return the matched
257 * governor's pointer. devfreq_list_lock should be held by the caller.
258 */
find_devfreq_governor(const char * name)259 static struct devfreq_governor *find_devfreq_governor(const char *name)
260 {
261 struct devfreq_governor *tmp_governor;
262
263 lockdep_assert_held(&devfreq_list_lock);
264
265 if (IS_ERR_OR_NULL(name)) {
266 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
267 return ERR_PTR(-EINVAL);
268 }
269
270 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
271 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
272 return tmp_governor;
273 }
274
275 return ERR_PTR(-ENODEV);
276 }
277
278 /**
279 * try_then_request_governor() - Try to find the governor and request the
280 * module if is not found.
281 * @name: name of the governor
282 *
283 * Search the list of devfreq governors and request the module and try again
284 * if is not found. This can happen when both drivers (the governor driver
285 * and the driver that call devfreq_add_device) are built as modules.
286 * devfreq_list_lock should be held by the caller. Returns the matched
287 * governor's pointer or an error pointer.
288 */
try_then_request_governor(const char * name)289 static struct devfreq_governor *try_then_request_governor(const char *name)
290 {
291 struct devfreq_governor *governor;
292 int err = 0;
293
294 lockdep_assert_held(&devfreq_list_lock);
295
296 if (IS_ERR_OR_NULL(name)) {
297 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
298 return ERR_PTR(-EINVAL);
299 }
300
301 governor = find_devfreq_governor(name);
302 if (IS_ERR(governor)) {
303 mutex_unlock(&devfreq_list_lock);
304
305 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
306 DEVFREQ_NAME_LEN))
307 err = request_module("governor_%s", "simpleondemand");
308 else
309 err = request_module("governor_%s", name);
310 /* Restore previous state before return */
311 mutex_lock(&devfreq_list_lock);
312 if (err)
313 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
314
315 governor = find_devfreq_governor(name);
316 }
317
318 return governor;
319 }
320
devfreq_notify_transition(struct devfreq * devfreq,struct devfreq_freqs * freqs,unsigned int state)321 static int devfreq_notify_transition(struct devfreq *devfreq,
322 struct devfreq_freqs *freqs, unsigned int state)
323 {
324 if (!devfreq)
325 return -EINVAL;
326
327 switch (state) {
328 case DEVFREQ_PRECHANGE:
329 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
330 DEVFREQ_PRECHANGE, freqs);
331 break;
332
333 case DEVFREQ_POSTCHANGE:
334 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
335 DEVFREQ_POSTCHANGE, freqs);
336 break;
337 default:
338 return -EINVAL;
339 }
340
341 return 0;
342 }
343
devfreq_set_target(struct devfreq * devfreq,unsigned long new_freq,u32 flags)344 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
345 u32 flags)
346 {
347 struct devfreq_freqs freqs;
348 unsigned long cur_freq;
349 int err = 0;
350
351 if (devfreq->profile->get_cur_freq)
352 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
353 else
354 cur_freq = devfreq->previous_freq;
355
356 freqs.old = cur_freq;
357 freqs.new = new_freq;
358 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
359
360 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
361 if (err) {
362 freqs.new = cur_freq;
363 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
364 return err;
365 }
366
367 /*
368 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE
369 * and DEVFREQ_POSTCHANGE because for showing the correct frequency
370 * change order of between devfreq device and passive devfreq device.
371 */
372 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq)
373 trace_devfreq_frequency(devfreq, new_freq, cur_freq);
374
375 freqs.new = new_freq;
376 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
377
378 if (devfreq_update_status(devfreq, new_freq))
379 dev_warn(&devfreq->dev,
380 "Couldn't update frequency transition information.\n");
381
382 devfreq->previous_freq = new_freq;
383
384 if (devfreq->suspend_freq)
385 devfreq->resume_freq = new_freq;
386
387 return err;
388 }
389
390 /**
391 * devfreq_update_target() - Reevaluate the device and configure frequency
392 * on the final stage.
393 * @devfreq: the devfreq instance.
394 * @freq: the new frequency of parent device. This argument
395 * is only used for devfreq device using passive governor.
396 *
397 * Note: Lock devfreq->lock before calling devfreq_update_target. This function
398 * should be only used by both update_devfreq() and devfreq governors.
399 */
devfreq_update_target(struct devfreq * devfreq,unsigned long freq)400 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
401 {
402 unsigned long min_freq, max_freq;
403 int err = 0;
404 u32 flags = 0;
405
406 lockdep_assert_held(&devfreq->lock);
407
408 if (!devfreq->governor)
409 return -EINVAL;
410
411 /* Reevaluate the proper frequency */
412 err = devfreq->governor->get_target_freq(devfreq, &freq);
413 if (err)
414 return err;
415 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
416
417 if (freq < min_freq) {
418 freq = min_freq;
419 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
420 }
421 if (freq > max_freq) {
422 freq = max_freq;
423 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
424 }
425
426 return devfreq_set_target(devfreq, freq, flags);
427 }
428 EXPORT_SYMBOL(devfreq_update_target);
429
430 /* Load monitoring helper functions for governors use */
431
432 /**
433 * update_devfreq() - Reevaluate the device and configure frequency.
434 * @devfreq: the devfreq instance.
435 *
436 * Note: Lock devfreq->lock before calling update_devfreq
437 * This function is exported for governors.
438 */
update_devfreq(struct devfreq * devfreq)439 int update_devfreq(struct devfreq *devfreq)
440 {
441 return devfreq_update_target(devfreq, 0L);
442 }
443 EXPORT_SYMBOL(update_devfreq);
444
445 /**
446 * devfreq_monitor() - Periodically poll devfreq objects.
447 * @work: the work struct used to run devfreq_monitor periodically.
448 *
449 */
devfreq_monitor(struct work_struct * work)450 static void devfreq_monitor(struct work_struct *work)
451 {
452 int err;
453 struct devfreq *devfreq = container_of(work,
454 struct devfreq, work.work);
455
456 mutex_lock(&devfreq->lock);
457 err = update_devfreq(devfreq);
458 if (err)
459 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
460
461 if (devfreq->stop_polling)
462 goto out;
463
464 queue_delayed_work(devfreq_wq, &devfreq->work,
465 msecs_to_jiffies(devfreq->profile->polling_ms));
466
467 out:
468 mutex_unlock(&devfreq->lock);
469 trace_devfreq_monitor(devfreq);
470 }
471
472 /**
473 * devfreq_monitor_start() - Start load monitoring of devfreq instance
474 * @devfreq: the devfreq instance.
475 *
476 * Helper function for starting devfreq device load monitoring. By default,
477 * deferrable timer is used for load monitoring. But the users can change this
478 * behavior using the "timer" type in devfreq_dev_profile. This function will be
479 * called by devfreq governor in response to the DEVFREQ_GOV_START event
480 * generated while adding a device to the devfreq framework.
481 */
devfreq_monitor_start(struct devfreq * devfreq)482 void devfreq_monitor_start(struct devfreq *devfreq)
483 {
484 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
485 return;
486
487 mutex_lock(&devfreq->lock);
488 if (delayed_work_pending(&devfreq->work))
489 goto out;
490
491 switch (devfreq->profile->timer) {
492 case DEVFREQ_TIMER_DEFERRABLE:
493 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
494 break;
495 case DEVFREQ_TIMER_DELAYED:
496 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
497 break;
498 default:
499 goto out;
500 }
501
502 if (devfreq->profile->polling_ms)
503 queue_delayed_work(devfreq_wq, &devfreq->work,
504 msecs_to_jiffies(devfreq->profile->polling_ms));
505
506 out:
507 devfreq->stop_polling = false;
508 mutex_unlock(&devfreq->lock);
509 }
510 EXPORT_SYMBOL(devfreq_monitor_start);
511
512 /**
513 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
514 * @devfreq: the devfreq instance.
515 *
516 * Helper function to stop devfreq device load monitoring. Function
517 * to be called from governor in response to DEVFREQ_GOV_STOP
518 * event when device is removed from devfreq framework.
519 */
devfreq_monitor_stop(struct devfreq * devfreq)520 void devfreq_monitor_stop(struct devfreq *devfreq)
521 {
522 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
523 return;
524
525 mutex_lock(&devfreq->lock);
526 if (devfreq->stop_polling) {
527 mutex_unlock(&devfreq->lock);
528 return;
529 }
530
531 devfreq->stop_polling = true;
532 mutex_unlock(&devfreq->lock);
533 cancel_delayed_work_sync(&devfreq->work);
534 }
535 EXPORT_SYMBOL(devfreq_monitor_stop);
536
537 /**
538 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
539 * @devfreq: the devfreq instance.
540 *
541 * Helper function to suspend devfreq device load monitoring. Function
542 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
543 * event or when polling interval is set to zero.
544 *
545 * Note: Though this function is same as devfreq_monitor_stop(),
546 * intentionally kept separate to provide hooks for collecting
547 * transition statistics.
548 */
devfreq_monitor_suspend(struct devfreq * devfreq)549 void devfreq_monitor_suspend(struct devfreq *devfreq)
550 {
551 mutex_lock(&devfreq->lock);
552 if (devfreq->stop_polling) {
553 mutex_unlock(&devfreq->lock);
554 return;
555 }
556
557 devfreq_update_status(devfreq, devfreq->previous_freq);
558 devfreq->stop_polling = true;
559 mutex_unlock(&devfreq->lock);
560
561 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
562 return;
563
564 cancel_delayed_work_sync(&devfreq->work);
565 }
566 EXPORT_SYMBOL(devfreq_monitor_suspend);
567
568 /**
569 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
570 * @devfreq: the devfreq instance.
571 *
572 * Helper function to resume devfreq device load monitoring. Function
573 * to be called from governor in response to DEVFREQ_GOV_RESUME
574 * event or when polling interval is set to non-zero.
575 */
devfreq_monitor_resume(struct devfreq * devfreq)576 void devfreq_monitor_resume(struct devfreq *devfreq)
577 {
578 unsigned long freq;
579
580 mutex_lock(&devfreq->lock);
581
582 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
583 goto out_update;
584
585 if (!devfreq->stop_polling)
586 goto out;
587
588 if (!delayed_work_pending(&devfreq->work) &&
589 devfreq->profile->polling_ms)
590 queue_delayed_work(devfreq_wq, &devfreq->work,
591 msecs_to_jiffies(devfreq->profile->polling_ms));
592
593 out_update:
594 devfreq->stats.last_update = get_jiffies_64();
595 devfreq->stop_polling = false;
596
597 if (devfreq->profile->get_cur_freq &&
598 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
599 devfreq->previous_freq = freq;
600
601 out:
602 mutex_unlock(&devfreq->lock);
603 }
604 EXPORT_SYMBOL(devfreq_monitor_resume);
605
606 /**
607 * devfreq_update_interval() - Update device devfreq monitoring interval
608 * @devfreq: the devfreq instance.
609 * @delay: new polling interval to be set.
610 *
611 * Helper function to set new load monitoring polling interval. Function
612 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
613 */
devfreq_update_interval(struct devfreq * devfreq,unsigned int * delay)614 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
615 {
616 unsigned int cur_delay = devfreq->profile->polling_ms;
617 unsigned int new_delay = *delay;
618
619 mutex_lock(&devfreq->lock);
620 devfreq->profile->polling_ms = new_delay;
621
622 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
623 goto out;
624
625 if (devfreq->stop_polling)
626 goto out;
627
628 /* if new delay is zero, stop polling */
629 if (!new_delay) {
630 mutex_unlock(&devfreq->lock);
631 cancel_delayed_work_sync(&devfreq->work);
632 return;
633 }
634
635 /* if current delay is zero, start polling with new delay */
636 if (!cur_delay) {
637 queue_delayed_work(devfreq_wq, &devfreq->work,
638 msecs_to_jiffies(devfreq->profile->polling_ms));
639 goto out;
640 }
641
642 /* if current delay is greater than new delay, restart polling */
643 if (cur_delay > new_delay) {
644 mutex_unlock(&devfreq->lock);
645 cancel_delayed_work_sync(&devfreq->work);
646 mutex_lock(&devfreq->lock);
647 if (!devfreq->stop_polling)
648 queue_delayed_work(devfreq_wq, &devfreq->work,
649 msecs_to_jiffies(devfreq->profile->polling_ms));
650 }
651 out:
652 mutex_unlock(&devfreq->lock);
653 }
654 EXPORT_SYMBOL(devfreq_update_interval);
655
656 /**
657 * devfreq_notifier_call() - Notify that the device frequency requirements
658 * has been changed out of devfreq framework.
659 * @nb: the notifier_block (supposed to be devfreq->nb)
660 * @type: not used
661 * @devp: not used
662 *
663 * Called by a notifier that uses devfreq->nb.
664 */
devfreq_notifier_call(struct notifier_block * nb,unsigned long type,void * devp)665 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
666 void *devp)
667 {
668 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
669 int err = -EINVAL;
670
671 mutex_lock(&devfreq->lock);
672
673 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
674 if (!devfreq->scaling_min_freq)
675 goto out;
676
677 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
678 if (!devfreq->scaling_max_freq) {
679 devfreq->scaling_max_freq = ULONG_MAX;
680 goto out;
681 }
682
683 err = update_devfreq(devfreq);
684
685 out:
686 mutex_unlock(&devfreq->lock);
687 if (err)
688 dev_err(devfreq->dev.parent,
689 "failed to update frequency from OPP notifier (%d)\n",
690 err);
691
692 return NOTIFY_OK;
693 }
694
695 /**
696 * qos_notifier_call() - Common handler for QoS constraints.
697 * @devfreq: the devfreq instance.
698 */
qos_notifier_call(struct devfreq * devfreq)699 static int qos_notifier_call(struct devfreq *devfreq)
700 {
701 int err;
702
703 mutex_lock(&devfreq->lock);
704 err = update_devfreq(devfreq);
705 mutex_unlock(&devfreq->lock);
706 if (err)
707 dev_err(devfreq->dev.parent,
708 "failed to update frequency from PM QoS (%d)\n",
709 err);
710
711 return NOTIFY_OK;
712 }
713
714 /**
715 * qos_min_notifier_call() - Callback for QoS min_freq changes.
716 * @nb: Should be devfreq->nb_min
717 * @val: not used
718 * @ptr: not used
719 */
qos_min_notifier_call(struct notifier_block * nb,unsigned long val,void * ptr)720 static int qos_min_notifier_call(struct notifier_block *nb,
721 unsigned long val, void *ptr)
722 {
723 return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
724 }
725
726 /**
727 * qos_max_notifier_call() - Callback for QoS max_freq changes.
728 * @nb: Should be devfreq->nb_max
729 * @val: not used
730 * @ptr: not used
731 */
qos_max_notifier_call(struct notifier_block * nb,unsigned long val,void * ptr)732 static int qos_max_notifier_call(struct notifier_block *nb,
733 unsigned long val, void *ptr)
734 {
735 return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
736 }
737
738 /**
739 * devfreq_dev_release() - Callback for struct device to release the device.
740 * @dev: the devfreq device
741 *
742 * Remove devfreq from the list and release its resources.
743 */
devfreq_dev_release(struct device * dev)744 static void devfreq_dev_release(struct device *dev)
745 {
746 struct devfreq *devfreq = to_devfreq(dev);
747 int err;
748
749 mutex_lock(&devfreq_list_lock);
750 list_del(&devfreq->node);
751 mutex_unlock(&devfreq_list_lock);
752
753 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
754 DEV_PM_QOS_MAX_FREQUENCY);
755 if (err && err != -ENOENT)
756 dev_warn(dev->parent,
757 "Failed to remove max_freq notifier: %d\n", err);
758 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
759 DEV_PM_QOS_MIN_FREQUENCY);
760 if (err && err != -ENOENT)
761 dev_warn(dev->parent,
762 "Failed to remove min_freq notifier: %d\n", err);
763
764 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
765 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
766 if (err < 0)
767 dev_warn(dev->parent,
768 "Failed to remove max_freq request: %d\n", err);
769 }
770 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
771 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
772 if (err < 0)
773 dev_warn(dev->parent,
774 "Failed to remove min_freq request: %d\n", err);
775 }
776
777 if (devfreq->profile->exit)
778 devfreq->profile->exit(devfreq->dev.parent);
779
780 if (devfreq->opp_table)
781 dev_pm_opp_put_opp_table(devfreq->opp_table);
782
783 mutex_destroy(&devfreq->lock);
784 srcu_cleanup_notifier_head(&devfreq->transition_notifier_list);
785 kfree(devfreq);
786 }
787
788 /**
789 * devfreq_add_device() - Add devfreq feature to the device
790 * @dev: the device to add devfreq feature.
791 * @profile: device-specific profile to run devfreq.
792 * @governor_name: name of the policy to choose frequency.
793 * @data: devfreq driver pass to governors, governor should not change it.
794 */
devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)795 struct devfreq *devfreq_add_device(struct device *dev,
796 struct devfreq_dev_profile *profile,
797 const char *governor_name,
798 void *data)
799 {
800 struct devfreq *devfreq;
801 struct devfreq_governor *governor;
802 int err = 0;
803
804 if (!dev || !profile || !governor_name) {
805 dev_err(dev, "%s: Invalid parameters.\n", __func__);
806 return ERR_PTR(-EINVAL);
807 }
808
809 mutex_lock(&devfreq_list_lock);
810 devfreq = find_device_devfreq(dev);
811 mutex_unlock(&devfreq_list_lock);
812 if (!IS_ERR(devfreq)) {
813 dev_err(dev, "%s: devfreq device already exists!\n",
814 __func__);
815 err = -EINVAL;
816 goto err_out;
817 }
818
819 devfreq = kzalloc_obj(struct devfreq);
820 if (!devfreq) {
821 err = -ENOMEM;
822 goto err_out;
823 }
824
825 mutex_init(&devfreq->lock);
826 mutex_lock(&devfreq->lock);
827 devfreq->dev.parent = dev;
828 devfreq->dev.class = devfreq_class;
829 devfreq->dev.groups = profile->dev_groups;
830 devfreq->dev.release = devfreq_dev_release;
831 INIT_LIST_HEAD(&devfreq->node);
832 devfreq->profile = profile;
833 devfreq->previous_freq = profile->initial_freq;
834 devfreq->last_status.current_frequency = profile->initial_freq;
835 devfreq->data = data;
836 devfreq->nb.notifier_call = devfreq_notifier_call;
837
838 if (devfreq->profile->timer < 0
839 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
840 mutex_unlock(&devfreq->lock);
841 err = -EINVAL;
842 goto err_dev;
843 }
844
845 if (!devfreq->profile->max_state || !devfreq->profile->freq_table) {
846 mutex_unlock(&devfreq->lock);
847 err = set_freq_table(devfreq);
848 if (err < 0)
849 goto err_dev;
850 mutex_lock(&devfreq->lock);
851 } else {
852 devfreq->freq_table = devfreq->profile->freq_table;
853 devfreq->max_state = devfreq->profile->max_state;
854 }
855
856 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
857 if (!devfreq->scaling_min_freq) {
858 mutex_unlock(&devfreq->lock);
859 err = -EINVAL;
860 goto err_dev;
861 }
862
863 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
864 if (!devfreq->scaling_max_freq) {
865 mutex_unlock(&devfreq->lock);
866 err = -EINVAL;
867 goto err_dev;
868 }
869
870 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
871 devfreq->opp_table = dev_pm_opp_get_opp_table(dev);
872 if (IS_ERR(devfreq->opp_table))
873 devfreq->opp_table = NULL;
874
875 atomic_set(&devfreq->suspend_count, 0);
876
877 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
878 err = device_register(&devfreq->dev);
879 if (err) {
880 mutex_unlock(&devfreq->lock);
881 put_device(&devfreq->dev);
882 goto err_out;
883 }
884
885 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
886 array3_size(sizeof(unsigned int),
887 devfreq->max_state,
888 devfreq->max_state),
889 GFP_KERNEL);
890 if (!devfreq->stats.trans_table) {
891 mutex_unlock(&devfreq->lock);
892 err = -ENOMEM;
893 goto err_devfreq;
894 }
895
896 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
897 devfreq->max_state,
898 sizeof(*devfreq->stats.time_in_state),
899 GFP_KERNEL);
900 if (!devfreq->stats.time_in_state) {
901 mutex_unlock(&devfreq->lock);
902 err = -ENOMEM;
903 goto err_devfreq;
904 }
905
906 devfreq->stats.total_trans = 0;
907 devfreq->stats.last_update = get_jiffies_64();
908
909 srcu_init_notifier_head(&devfreq->transition_notifier_list);
910
911 mutex_unlock(&devfreq->lock);
912
913 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
914 DEV_PM_QOS_MIN_FREQUENCY, 0);
915 if (err < 0)
916 goto err_devfreq;
917 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
918 DEV_PM_QOS_MAX_FREQUENCY,
919 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
920 if (err < 0)
921 goto err_devfreq;
922
923 devfreq->nb_min.notifier_call = qos_min_notifier_call;
924 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min,
925 DEV_PM_QOS_MIN_FREQUENCY);
926 if (err)
927 goto err_devfreq;
928
929 devfreq->nb_max.notifier_call = qos_max_notifier_call;
930 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max,
931 DEV_PM_QOS_MAX_FREQUENCY);
932 if (err)
933 goto err_devfreq;
934
935 mutex_lock(&devfreq_list_lock);
936
937 governor = try_then_request_governor(governor_name);
938 if (IS_ERR(governor)) {
939 dev_err(dev, "%s: Unable to find governor for the device\n",
940 __func__);
941 err = PTR_ERR(governor);
942 goto err_init;
943 }
944
945 devfreq->governor = governor;
946 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
947 NULL);
948 if (err) {
949 dev_err_probe(dev, err,
950 "%s: Unable to start governor for the device\n",
951 __func__);
952 goto err_init;
953 }
954
955 err = sysfs_update_group(&devfreq->dev.kobj, &gov_attr_group);
956 if (err)
957 goto err_init;
958
959 list_add(&devfreq->node, &devfreq_list);
960
961 mutex_unlock(&devfreq_list_lock);
962
963 if (devfreq->profile->is_cooling_device) {
964 devfreq->cdev = devfreq_cooling_em_register(devfreq, NULL);
965 if (IS_ERR(devfreq->cdev))
966 devfreq->cdev = NULL;
967 }
968
969 return devfreq;
970
971 err_init:
972 mutex_unlock(&devfreq_list_lock);
973 err_devfreq:
974 devfreq_remove_device(devfreq);
975 devfreq = NULL;
976 err_dev:
977 kfree(devfreq);
978 err_out:
979 return ERR_PTR(err);
980 }
981 EXPORT_SYMBOL(devfreq_add_device);
982
983 /**
984 * devfreq_remove_device() - Remove devfreq feature from a device.
985 * @devfreq: the devfreq instance to be removed
986 *
987 * The opposite of devfreq_add_device().
988 */
devfreq_remove_device(struct devfreq * devfreq)989 int devfreq_remove_device(struct devfreq *devfreq)
990 {
991 if (!devfreq)
992 return -EINVAL;
993
994 devfreq_cooling_unregister(devfreq->cdev);
995
996 if (devfreq->governor)
997 devfreq->governor->event_handler(devfreq,
998 DEVFREQ_GOV_STOP, NULL);
999 device_unregister(&devfreq->dev);
1000
1001 return 0;
1002 }
1003 EXPORT_SYMBOL(devfreq_remove_device);
1004
devm_devfreq_dev_match(struct device * dev,void * res,void * data)1005 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
1006 {
1007 struct devfreq **r = res;
1008
1009 if (WARN_ON(!r || !*r))
1010 return 0;
1011
1012 return *r == data;
1013 }
1014
devm_devfreq_dev_release(struct device * dev,void * res)1015 static void devm_devfreq_dev_release(struct device *dev, void *res)
1016 {
1017 devfreq_remove_device(*(struct devfreq **)res);
1018 }
1019
1020 /**
1021 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
1022 * @dev: the device to add devfreq feature.
1023 * @profile: device-specific profile to run devfreq.
1024 * @governor_name: name of the policy to choose frequency.
1025 * @data: devfreq driver pass to governors, governor should not change it.
1026 *
1027 * This function manages automatically the memory of devfreq device using device
1028 * resource management and simplify the free operation for memory of devfreq
1029 * device.
1030 */
devm_devfreq_add_device(struct device * dev,struct devfreq_dev_profile * profile,const char * governor_name,void * data)1031 struct devfreq *devm_devfreq_add_device(struct device *dev,
1032 struct devfreq_dev_profile *profile,
1033 const char *governor_name,
1034 void *data)
1035 {
1036 struct devfreq **ptr, *devfreq;
1037
1038 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1039 if (!ptr)
1040 return ERR_PTR(-ENOMEM);
1041
1042 devfreq = devfreq_add_device(dev, profile, governor_name, data);
1043 if (IS_ERR(devfreq)) {
1044 devres_free(ptr);
1045 return devfreq;
1046 }
1047
1048 *ptr = devfreq;
1049 devres_add(dev, ptr);
1050
1051 return devfreq;
1052 }
1053 EXPORT_SYMBOL(devm_devfreq_add_device);
1054
1055 #ifdef CONFIG_OF
1056 /*
1057 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1058 * @node - pointer to device_node
1059 *
1060 * return the instance of devfreq device
1061 */
devfreq_get_devfreq_by_node(struct device_node * node)1062 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1063 {
1064 struct devfreq *devfreq;
1065
1066 if (!node)
1067 return ERR_PTR(-EINVAL);
1068
1069 mutex_lock(&devfreq_list_lock);
1070 list_for_each_entry(devfreq, &devfreq_list, node) {
1071 if (devfreq->dev.parent
1072 && device_match_of_node(devfreq->dev.parent, node)) {
1073 mutex_unlock(&devfreq_list_lock);
1074 return devfreq;
1075 }
1076 }
1077 mutex_unlock(&devfreq_list_lock);
1078
1079 return ERR_PTR(-ENODEV);
1080 }
1081
1082 /*
1083 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1084 * @dev - instance to the given device
1085 * @phandle_name - name of property holding a phandle value
1086 * @index - index into list of devfreq
1087 *
1088 * return the instance of devfreq device
1089 */
devfreq_get_devfreq_by_phandle(struct device * dev,const char * phandle_name,int index)1090 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1091 const char *phandle_name, int index)
1092 {
1093 struct device_node *node;
1094 struct devfreq *devfreq;
1095
1096 if (!dev || !phandle_name)
1097 return ERR_PTR(-EINVAL);
1098
1099 if (!dev->of_node)
1100 return ERR_PTR(-EINVAL);
1101
1102 node = of_parse_phandle(dev->of_node, phandle_name, index);
1103 if (!node)
1104 return ERR_PTR(-ENODEV);
1105
1106 devfreq = devfreq_get_devfreq_by_node(node);
1107 of_node_put(node);
1108
1109 return devfreq;
1110 }
1111
1112 #else
devfreq_get_devfreq_by_node(struct device_node * node)1113 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1114 {
1115 return ERR_PTR(-ENODEV);
1116 }
1117
devfreq_get_devfreq_by_phandle(struct device * dev,const char * phandle_name,int index)1118 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1119 const char *phandle_name, int index)
1120 {
1121 return ERR_PTR(-ENODEV);
1122 }
1123 #endif /* CONFIG_OF */
1124 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1125 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1126
1127 /**
1128 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1129 * @dev: the device from which to remove devfreq feature.
1130 * @devfreq: the devfreq instance to be removed
1131 */
devm_devfreq_remove_device(struct device * dev,struct devfreq * devfreq)1132 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1133 {
1134 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1135 devm_devfreq_dev_match, devfreq));
1136 }
1137 EXPORT_SYMBOL(devm_devfreq_remove_device);
1138
1139 /**
1140 * devfreq_suspend_device() - Suspend devfreq of a device.
1141 * @devfreq: the devfreq instance to be suspended
1142 *
1143 * This function is intended to be called by the pm callbacks
1144 * (e.g., runtime_suspend, suspend) of the device driver that
1145 * holds the devfreq.
1146 */
devfreq_suspend_device(struct devfreq * devfreq)1147 int devfreq_suspend_device(struct devfreq *devfreq)
1148 {
1149 int ret;
1150
1151 if (!devfreq)
1152 return -EINVAL;
1153
1154 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1155 return 0;
1156
1157 if (devfreq->governor) {
1158 ret = devfreq->governor->event_handler(devfreq,
1159 DEVFREQ_GOV_SUSPEND, NULL);
1160 if (ret)
1161 return ret;
1162 }
1163
1164 if (devfreq->suspend_freq) {
1165 mutex_lock(&devfreq->lock);
1166 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1167 mutex_unlock(&devfreq->lock);
1168 if (ret)
1169 return ret;
1170 }
1171
1172 return 0;
1173 }
1174 EXPORT_SYMBOL(devfreq_suspend_device);
1175
1176 /**
1177 * devfreq_resume_device() - Resume devfreq of a device.
1178 * @devfreq: the devfreq instance to be resumed
1179 *
1180 * This function is intended to be called by the pm callbacks
1181 * (e.g., runtime_resume, resume) of the device driver that
1182 * holds the devfreq.
1183 */
devfreq_resume_device(struct devfreq * devfreq)1184 int devfreq_resume_device(struct devfreq *devfreq)
1185 {
1186 int ret;
1187
1188 if (!devfreq)
1189 return -EINVAL;
1190
1191 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1192 return 0;
1193
1194 if (devfreq->resume_freq) {
1195 mutex_lock(&devfreq->lock);
1196 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1197 mutex_unlock(&devfreq->lock);
1198 if (ret)
1199 return ret;
1200 }
1201
1202 if (devfreq->governor) {
1203 ret = devfreq->governor->event_handler(devfreq,
1204 DEVFREQ_GOV_RESUME, NULL);
1205 if (ret)
1206 return ret;
1207 }
1208
1209 return 0;
1210 }
1211 EXPORT_SYMBOL(devfreq_resume_device);
1212
1213 /**
1214 * devfreq_suspend() - Suspend devfreq governors and devices
1215 *
1216 * Called during system wide Suspend/Hibernate cycles for suspending governors
1217 * and devices preserving the state for resume. On some platforms the devfreq
1218 * device must have precise state (frequency) after resume in order to provide
1219 * fully operating setup.
1220 */
devfreq_suspend(void)1221 void devfreq_suspend(void)
1222 {
1223 struct devfreq *devfreq;
1224 int ret;
1225
1226 mutex_lock(&devfreq_list_lock);
1227 list_for_each_entry(devfreq, &devfreq_list, node) {
1228 ret = devfreq_suspend_device(devfreq);
1229 if (ret)
1230 dev_err(&devfreq->dev,
1231 "failed to suspend devfreq device\n");
1232 }
1233 mutex_unlock(&devfreq_list_lock);
1234 }
1235
1236 /**
1237 * devfreq_resume() - Resume devfreq governors and devices
1238 *
1239 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1240 * devices that are suspended with devfreq_suspend().
1241 */
devfreq_resume(void)1242 void devfreq_resume(void)
1243 {
1244 struct devfreq *devfreq;
1245 int ret;
1246
1247 mutex_lock(&devfreq_list_lock);
1248 list_for_each_entry(devfreq, &devfreq_list, node) {
1249 ret = devfreq_resume_device(devfreq);
1250 if (ret)
1251 dev_warn(&devfreq->dev,
1252 "failed to resume devfreq device\n");
1253 }
1254 mutex_unlock(&devfreq_list_lock);
1255 }
1256
1257 /**
1258 * devfreq_add_governor() - Add devfreq governor
1259 * @governor: the devfreq governor to be added
1260 */
devfreq_add_governor(struct devfreq_governor * governor)1261 int devfreq_add_governor(struct devfreq_governor *governor)
1262 {
1263 struct devfreq_governor *g;
1264 struct devfreq *devfreq;
1265 int err = 0;
1266
1267 if (!governor) {
1268 pr_err("%s: Invalid parameters.\n", __func__);
1269 return -EINVAL;
1270 }
1271
1272 mutex_lock(&devfreq_list_lock);
1273 g = find_devfreq_governor(governor->name);
1274 if (!IS_ERR(g)) {
1275 pr_err("%s: governor %s already registered\n", __func__,
1276 g->name);
1277 err = -EINVAL;
1278 goto err_out;
1279 }
1280
1281 list_add(&governor->node, &devfreq_governor_list);
1282
1283 list_for_each_entry(devfreq, &devfreq_list, node) {
1284 int ret = 0;
1285 struct device *dev = devfreq->dev.parent;
1286
1287 if (!strncmp(devfreq->governor->name, governor->name,
1288 DEVFREQ_NAME_LEN)) {
1289 /* The following should never occur */
1290 if (devfreq->governor) {
1291 dev_warn(dev,
1292 "%s: Governor %s already present\n",
1293 __func__, devfreq->governor->name);
1294 ret = devfreq->governor->event_handler(devfreq,
1295 DEVFREQ_GOV_STOP, NULL);
1296 if (ret) {
1297 dev_warn(dev,
1298 "%s: Governor %s stop = %d\n",
1299 __func__,
1300 devfreq->governor->name, ret);
1301 }
1302 /* Fall through */
1303 }
1304 devfreq->governor = governor;
1305 ret = devfreq->governor->event_handler(devfreq,
1306 DEVFREQ_GOV_START, NULL);
1307 if (ret) {
1308 dev_warn(dev, "%s: Governor %s start=%d\n",
1309 __func__, devfreq->governor->name,
1310 ret);
1311 }
1312 }
1313 }
1314
1315 err_out:
1316 mutex_unlock(&devfreq_list_lock);
1317
1318 return err;
1319 }
1320 EXPORT_SYMBOL(devfreq_add_governor);
1321
devm_devfreq_remove_governor(void * governor)1322 static void devm_devfreq_remove_governor(void *governor)
1323 {
1324 WARN_ON(devfreq_remove_governor(governor));
1325 }
1326
1327 /**
1328 * devm_devfreq_add_governor() - Add devfreq governor
1329 * @dev: device which adds devfreq governor
1330 * @governor: the devfreq governor to be added
1331 *
1332 * This is a resource-managed variant of devfreq_add_governor().
1333 */
devm_devfreq_add_governor(struct device * dev,struct devfreq_governor * governor)1334 int devm_devfreq_add_governor(struct device *dev,
1335 struct devfreq_governor *governor)
1336 {
1337 int err;
1338
1339 err = devfreq_add_governor(governor);
1340 if (err)
1341 return err;
1342
1343 return devm_add_action_or_reset(dev, devm_devfreq_remove_governor,
1344 governor);
1345 }
1346 EXPORT_SYMBOL(devm_devfreq_add_governor);
1347
1348 /**
1349 * devfreq_remove_governor() - Remove devfreq feature from a device.
1350 * @governor: the devfreq governor to be removed
1351 */
devfreq_remove_governor(struct devfreq_governor * governor)1352 int devfreq_remove_governor(struct devfreq_governor *governor)
1353 {
1354 struct devfreq_governor *g;
1355 struct devfreq *devfreq;
1356 int err = 0;
1357
1358 if (!governor) {
1359 pr_err("%s: Invalid parameters.\n", __func__);
1360 return -EINVAL;
1361 }
1362
1363 mutex_lock(&devfreq_list_lock);
1364 g = find_devfreq_governor(governor->name);
1365 if (IS_ERR(g)) {
1366 pr_err("%s: governor %s not registered\n", __func__,
1367 governor->name);
1368 err = PTR_ERR(g);
1369 goto err_out;
1370 }
1371 list_for_each_entry(devfreq, &devfreq_list, node) {
1372 int ret;
1373 struct device *dev = devfreq->dev.parent;
1374
1375 if (!devfreq->governor)
1376 continue;
1377
1378 if (!strncmp(devfreq->governor->name, governor->name,
1379 DEVFREQ_NAME_LEN)) {
1380 ret = devfreq->governor->event_handler(devfreq,
1381 DEVFREQ_GOV_STOP, NULL);
1382 if (ret) {
1383 dev_warn(dev, "%s: Governor %s stop=%d\n",
1384 __func__, devfreq->governor->name,
1385 ret);
1386 }
1387 devfreq->governor = NULL;
1388 }
1389 }
1390
1391 list_del(&governor->node);
1392 err_out:
1393 mutex_unlock(&devfreq_list_lock);
1394
1395 return err;
1396 }
1397 EXPORT_SYMBOL(devfreq_remove_governor);
1398
name_show(struct device * dev,struct device_attribute * attr,char * buf)1399 static ssize_t name_show(struct device *dev,
1400 struct device_attribute *attr, char *buf)
1401 {
1402 struct devfreq *df = to_devfreq(dev);
1403 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1404 }
1405 static DEVICE_ATTR_RO(name);
1406
governor_show(struct device * dev,struct device_attribute * attr,char * buf)1407 static ssize_t governor_show(struct device *dev,
1408 struct device_attribute *attr, char *buf)
1409 {
1410 struct devfreq *df = to_devfreq(dev);
1411
1412 if (!df->governor)
1413 return -EINVAL;
1414
1415 return sprintf(buf, "%s\n", df->governor->name);
1416 }
1417
governor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1418 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1419 const char *buf, size_t count)
1420 {
1421 struct devfreq *df = to_devfreq(dev);
1422 int ret;
1423 char str_governor[DEVFREQ_NAME_LEN + 1];
1424 const struct devfreq_governor *governor, *prev_governor;
1425
1426 if (!df->governor)
1427 return -EINVAL;
1428
1429 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1430 if (ret != 1)
1431 return -EINVAL;
1432
1433 mutex_lock(&devfreq_list_lock);
1434 governor = try_then_request_governor(str_governor);
1435 if (IS_ERR(governor)) {
1436 ret = PTR_ERR(governor);
1437 goto out;
1438 }
1439 if (df->governor == governor) {
1440 ret = 0;
1441 goto out;
1442 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1443 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1444 ret = -EINVAL;
1445 goto out;
1446 }
1447
1448 /*
1449 * Stop the current governor and remove the specific sysfs files
1450 * which depend on current governor.
1451 */
1452 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1453 if (ret) {
1454 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1455 __func__, df->governor->name, ret);
1456 goto out;
1457 }
1458
1459 /*
1460 * Start the new governor and create the specific sysfs files
1461 * which depend on the new governor.
1462 */
1463 prev_governor = df->governor;
1464 df->governor = governor;
1465 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1466 if (ret) {
1467 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1468 __func__, df->governor->name, ret);
1469
1470 /* Restore previous governor */
1471 df->governor = prev_governor;
1472 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1473 if (ret) {
1474 dev_err(dev,
1475 "%s: reverting to Governor %s failed (%d)\n",
1476 __func__, prev_governor->name, ret);
1477 df->governor = NULL;
1478 goto out;
1479 }
1480 }
1481
1482 /*
1483 * Create the sysfs files for the new governor. But if failed to start
1484 * the new governor, restore the sysfs files of previous governor.
1485 */
1486 ret = sysfs_update_group(&df->dev.kobj, &gov_attr_group);
1487
1488 out:
1489 mutex_unlock(&devfreq_list_lock);
1490
1491 if (!ret)
1492 ret = count;
1493 return ret;
1494 }
1495 static DEVICE_ATTR_RW(governor);
1496
available_governors_show(struct device * d,struct device_attribute * attr,char * buf)1497 static ssize_t available_governors_show(struct device *d,
1498 struct device_attribute *attr,
1499 char *buf)
1500 {
1501 struct devfreq *df = to_devfreq(d);
1502 ssize_t count = 0;
1503
1504 if (!df->governor)
1505 return -EINVAL;
1506
1507 mutex_lock(&devfreq_list_lock);
1508
1509 /*
1510 * The devfreq with immutable governor (e.g., passive) shows
1511 * only own governor.
1512 */
1513 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1514 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1515 "%s ", df->governor->name);
1516 /*
1517 * The devfreq device shows the registered governor except for
1518 * immutable governors such as passive governor .
1519 */
1520 } else {
1521 struct devfreq_governor *governor;
1522
1523 list_for_each_entry(governor, &devfreq_governor_list, node) {
1524 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1525 continue;
1526 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1527 "%s ", governor->name);
1528 }
1529 }
1530
1531 mutex_unlock(&devfreq_list_lock);
1532
1533 /* Truncate the trailing space */
1534 if (count)
1535 count--;
1536
1537 count += sprintf(&buf[count], "\n");
1538
1539 return count;
1540 }
1541 static DEVICE_ATTR_RO(available_governors);
1542
cur_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1543 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1544 char *buf)
1545 {
1546 unsigned long freq;
1547 struct devfreq *df = to_devfreq(dev);
1548
1549 if (!df->profile)
1550 return -EINVAL;
1551
1552 if (df->profile->get_cur_freq &&
1553 !df->profile->get_cur_freq(df->dev.parent, &freq))
1554 return sprintf(buf, "%lu\n", freq);
1555
1556 return sprintf(buf, "%lu\n", df->previous_freq);
1557 }
1558 static DEVICE_ATTR_RO(cur_freq);
1559
target_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1560 static ssize_t target_freq_show(struct device *dev,
1561 struct device_attribute *attr, char *buf)
1562 {
1563 struct devfreq *df = to_devfreq(dev);
1564
1565 return sprintf(buf, "%lu\n", df->previous_freq);
1566 }
1567 static DEVICE_ATTR_RO(target_freq);
1568
min_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1569 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1570 const char *buf, size_t count)
1571 {
1572 struct devfreq *df = to_devfreq(dev);
1573 unsigned long value;
1574 int ret;
1575
1576 /*
1577 * Protect against theoretical sysfs writes between
1578 * device_add and dev_pm_qos_add_request
1579 */
1580 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1581 return -EAGAIN;
1582
1583 ret = sscanf(buf, "%lu", &value);
1584 if (ret != 1)
1585 return -EINVAL;
1586
1587 /* Round down to kHz for PM QoS */
1588 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1589 value / HZ_PER_KHZ);
1590 if (ret < 0)
1591 return ret;
1592
1593 return count;
1594 }
1595
min_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1596 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1597 char *buf)
1598 {
1599 struct devfreq *df = to_devfreq(dev);
1600 unsigned long min_freq, max_freq;
1601
1602 mutex_lock(&df->lock);
1603 devfreq_get_freq_range(df, &min_freq, &max_freq);
1604 mutex_unlock(&df->lock);
1605
1606 return sprintf(buf, "%lu\n", min_freq);
1607 }
1608 static DEVICE_ATTR_RW(min_freq);
1609
max_freq_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1610 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1611 const char *buf, size_t count)
1612 {
1613 struct devfreq *df = to_devfreq(dev);
1614 unsigned long value;
1615 int ret;
1616
1617 /*
1618 * Protect against theoretical sysfs writes between
1619 * device_add and dev_pm_qos_add_request
1620 */
1621 if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1622 return -EINVAL;
1623
1624 ret = sscanf(buf, "%lu", &value);
1625 if (ret != 1)
1626 return -EINVAL;
1627
1628 /*
1629 * PM QoS frequencies are in kHz so we need to convert. Convert by
1630 * rounding upwards so that the acceptable interval never shrinks.
1631 *
1632 * For example if the user writes "666666666" to sysfs this value will
1633 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1634 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1635 *
1636 * A value of zero means "no limit".
1637 */
1638 if (value)
1639 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1640 else
1641 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1642
1643 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1644 if (ret < 0)
1645 return ret;
1646
1647 return count;
1648 }
1649
max_freq_show(struct device * dev,struct device_attribute * attr,char * buf)1650 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1651 char *buf)
1652 {
1653 struct devfreq *df = to_devfreq(dev);
1654 unsigned long min_freq, max_freq;
1655
1656 mutex_lock(&df->lock);
1657 devfreq_get_freq_range(df, &min_freq, &max_freq);
1658 mutex_unlock(&df->lock);
1659
1660 return sprintf(buf, "%lu\n", max_freq);
1661 }
1662 static DEVICE_ATTR_RW(max_freq);
1663
available_frequencies_show(struct device * d,struct device_attribute * attr,char * buf)1664 static ssize_t available_frequencies_show(struct device *d,
1665 struct device_attribute *attr,
1666 char *buf)
1667 {
1668 struct devfreq *df = to_devfreq(d);
1669 ssize_t count = 0;
1670 int i;
1671
1672 if (!df->profile)
1673 return -EINVAL;
1674
1675 mutex_lock(&df->lock);
1676
1677 for (i = 0; i < df->max_state; i++)
1678 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1679 "%lu ", df->freq_table[i]);
1680
1681 mutex_unlock(&df->lock);
1682 /* Truncate the trailing space */
1683 if (count)
1684 count--;
1685
1686 count += sprintf(&buf[count], "\n");
1687
1688 return count;
1689 }
1690 static DEVICE_ATTR_RO(available_frequencies);
1691
trans_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1692 static ssize_t trans_stat_show(struct device *dev,
1693 struct device_attribute *attr, char *buf)
1694 {
1695 struct devfreq *df = to_devfreq(dev);
1696 ssize_t len = 0;
1697 int i, j;
1698 unsigned int max_state;
1699
1700 if (!df->profile)
1701 return -EINVAL;
1702 max_state = df->max_state;
1703
1704 if (max_state == 0)
1705 return sysfs_emit(buf, "Not Supported.\n");
1706
1707 mutex_lock(&df->lock);
1708 if (!df->stop_polling &&
1709 devfreq_update_status(df, df->previous_freq)) {
1710 mutex_unlock(&df->lock);
1711 return 0;
1712 }
1713 mutex_unlock(&df->lock);
1714
1715 len += sysfs_emit_at(buf, len, " From : To\n");
1716 len += sysfs_emit_at(buf, len, " :");
1717 for (i = 0; i < max_state; i++) {
1718 if (len >= PAGE_SIZE - 1)
1719 break;
1720 len += sysfs_emit_at(buf, len, "%10lu",
1721 df->freq_table[i]);
1722 }
1723
1724 if (len >= PAGE_SIZE - 1)
1725 return PAGE_SIZE - 1;
1726 len += sysfs_emit_at(buf, len, " time(ms)\n");
1727
1728 for (i = 0; i < max_state; i++) {
1729 if (len >= PAGE_SIZE - 1)
1730 break;
1731 if (df->freq_table[i] == df->previous_freq)
1732 len += sysfs_emit_at(buf, len, "*");
1733 else
1734 len += sysfs_emit_at(buf, len, " ");
1735 if (len >= PAGE_SIZE - 1)
1736 break;
1737 len += sysfs_emit_at(buf, len, "%10lu:", df->freq_table[i]);
1738 for (j = 0; j < max_state; j++) {
1739 if (len >= PAGE_SIZE - 1)
1740 break;
1741 len += sysfs_emit_at(buf, len, "%10u",
1742 df->stats.trans_table[(i * max_state) + j]);
1743 }
1744 if (len >= PAGE_SIZE - 1)
1745 break;
1746 len += sysfs_emit_at(buf, len, "%10llu\n", (u64)
1747 jiffies64_to_msecs(df->stats.time_in_state[i]));
1748 }
1749
1750 if (len < PAGE_SIZE - 1)
1751 len += sysfs_emit_at(buf, len, "Total transition : %u\n",
1752 df->stats.total_trans);
1753 if (len >= PAGE_SIZE - 1) {
1754 pr_warn_once("devfreq transition table exceeds PAGE_SIZE. Disabling\n");
1755 return -EFBIG;
1756 }
1757
1758 return len;
1759 }
1760
trans_stat_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1761 static ssize_t trans_stat_store(struct device *dev,
1762 struct device_attribute *attr,
1763 const char *buf, size_t count)
1764 {
1765 struct devfreq *df = to_devfreq(dev);
1766 int err, value;
1767
1768 if (!df->profile)
1769 return -EINVAL;
1770
1771 if (df->max_state == 0)
1772 return count;
1773
1774 err = kstrtoint(buf, 10, &value);
1775 if (err || value != 0)
1776 return -EINVAL;
1777
1778 mutex_lock(&df->lock);
1779 memset(df->stats.time_in_state, 0, (df->max_state *
1780 sizeof(*df->stats.time_in_state)));
1781 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1782 df->max_state,
1783 df->max_state));
1784 df->stats.total_trans = 0;
1785 df->stats.last_update = get_jiffies_64();
1786 mutex_unlock(&df->lock);
1787
1788 return count;
1789 }
1790 static DEVICE_ATTR_RW(trans_stat);
1791
1792 static struct attribute *devfreq_attrs[] = {
1793 &dev_attr_name.attr,
1794 &dev_attr_governor.attr,
1795 &dev_attr_available_governors.attr,
1796 &dev_attr_cur_freq.attr,
1797 &dev_attr_available_frequencies.attr,
1798 &dev_attr_target_freq.attr,
1799 &dev_attr_min_freq.attr,
1800 &dev_attr_max_freq.attr,
1801 &dev_attr_trans_stat.attr,
1802 NULL,
1803 };
1804
polling_interval_show(struct device * dev,struct device_attribute * attr,char * buf)1805 static ssize_t polling_interval_show(struct device *dev,
1806 struct device_attribute *attr, char *buf)
1807 {
1808 struct devfreq *df = to_devfreq(dev);
1809
1810 /* Protect against race between sysfs attrs update and read/write */
1811 guard(mutex)(&devfreq_list_lock);
1812
1813 if (!df->profile || !df->governor ||
1814 !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
1815 return -EINVAL;
1816
1817 return sprintf(buf, "%d\n", df->profile->polling_ms);
1818 }
1819
polling_interval_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1820 static ssize_t polling_interval_store(struct device *dev,
1821 struct device_attribute *attr,
1822 const char *buf, size_t count)
1823 {
1824 struct devfreq *df = to_devfreq(dev);
1825 unsigned int value;
1826 int ret;
1827
1828 guard(mutex)(&devfreq_list_lock);
1829
1830 if (!df->governor ||
1831 !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
1832 return -EINVAL;
1833
1834 ret = sscanf(buf, "%u", &value);
1835 if (ret != 1)
1836 return -EINVAL;
1837
1838 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1839 ret = count;
1840
1841 return ret;
1842 }
1843 static DEVICE_ATTR_RW(polling_interval);
1844
timer_show(struct device * dev,struct device_attribute * attr,char * buf)1845 static ssize_t timer_show(struct device *dev,
1846 struct device_attribute *attr, char *buf)
1847 {
1848 struct devfreq *df = to_devfreq(dev);
1849
1850 guard(mutex)(&devfreq_list_lock);
1851
1852 if (!df->profile || !df->governor ||
1853 !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
1854 return -EINVAL;
1855
1856 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1857 }
1858
timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1859 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1860 const char *buf, size_t count)
1861 {
1862 struct devfreq *df = to_devfreq(dev);
1863 char str_timer[DEVFREQ_NAME_LEN + 1];
1864 int timer = -1;
1865 int ret = 0, i;
1866
1867 guard(mutex)(&devfreq_list_lock);
1868
1869 if (!df->governor || !df->profile ||
1870 !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
1871 return -EINVAL;
1872
1873 ret = sscanf(buf, "%16s", str_timer);
1874 if (ret != 1)
1875 return -EINVAL;
1876
1877 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1878 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1879 timer = i;
1880 break;
1881 }
1882 }
1883
1884 if (timer < 0) {
1885 ret = -EINVAL;
1886 goto out;
1887 }
1888
1889 if (df->profile->timer == timer) {
1890 ret = 0;
1891 goto out;
1892 }
1893
1894 mutex_lock(&df->lock);
1895 df->profile->timer = timer;
1896 mutex_unlock(&df->lock);
1897
1898 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1899 if (ret) {
1900 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1901 __func__, df->governor->name, ret);
1902 goto out;
1903 }
1904
1905 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1906 if (ret)
1907 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1908 __func__, df->governor->name, ret);
1909 out:
1910 return ret ? ret : count;
1911 }
1912 static DEVICE_ATTR_RW(timer);
1913
1914 static struct attribute *governor_attrs[] = {
1915 &dev_attr_polling_interval.attr,
1916 &dev_attr_timer.attr,
1917 NULL
1918 };
1919
gov_attr_visible(struct kobject * kobj,struct attribute * attr,int n)1920 static umode_t gov_attr_visible(struct kobject *kobj,
1921 struct attribute *attr, int n)
1922 {
1923 struct device *dev = kobj_to_dev(kobj);
1924 struct devfreq *df = to_devfreq(dev);
1925
1926 if (!df->governor || !df->governor->attrs)
1927 return 0;
1928
1929 if (attr == &dev_attr_polling_interval.attr &&
1930 IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
1931 return attr->mode;
1932
1933 if (attr == &dev_attr_timer.attr &&
1934 IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
1935 return attr->mode;
1936
1937 return 0;
1938 }
1939
1940 static const struct attribute_group devfreq_group = {
1941 .attrs = devfreq_attrs,
1942 };
1943
1944 static const struct attribute_group gov_attr_group = {
1945 .attrs = governor_attrs,
1946 .is_visible = gov_attr_visible,
1947 };
1948
1949 static const struct attribute_group *devfreq_groups[] = {
1950 &devfreq_group,
1951 &gov_attr_group,
1952 NULL
1953 };
1954
1955 /**
1956 * devfreq_summary_show() - Show the summary of the devfreq devices
1957 * @s: seq_file instance to show the summary of devfreq devices
1958 * @data: not used
1959 *
1960 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1961 * It helps that user can know the detailed information of the devfreq devices.
1962 *
1963 * Return 0 always because it shows the information without any data change.
1964 */
devfreq_summary_show(struct seq_file * s,void * data)1965 static int devfreq_summary_show(struct seq_file *s, void *data)
1966 {
1967 struct devfreq *devfreq;
1968 struct devfreq *p_devfreq = NULL;
1969 unsigned long cur_freq, min_freq, max_freq;
1970 unsigned int polling_ms;
1971 unsigned int timer;
1972
1973 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1974 "dev",
1975 "parent_dev",
1976 "governor",
1977 "timer",
1978 "polling_ms",
1979 "cur_freq_Hz",
1980 "min_freq_Hz",
1981 "max_freq_Hz");
1982 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1983 "------------------------------",
1984 "------------------------------",
1985 "---------------",
1986 "----------",
1987 "----------",
1988 "------------",
1989 "------------",
1990 "------------");
1991
1992 mutex_lock(&devfreq_list_lock);
1993
1994 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1995 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1996 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1997 DEVFREQ_NAME_LEN)) {
1998 struct devfreq_passive_data *data = devfreq->data;
1999
2000 if (data)
2001 p_devfreq = data->parent;
2002 } else {
2003 p_devfreq = NULL;
2004 }
2005 #endif
2006
2007 mutex_lock(&devfreq->lock);
2008 cur_freq = devfreq->previous_freq;
2009 devfreq_get_freq_range(devfreq, &min_freq, &max_freq);
2010 timer = devfreq->profile->timer;
2011
2012 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
2013 polling_ms = devfreq->profile->polling_ms;
2014 else
2015 polling_ms = 0;
2016 mutex_unlock(&devfreq->lock);
2017
2018 seq_printf(s,
2019 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
2020 dev_name(&devfreq->dev),
2021 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
2022 devfreq->governor->name,
2023 polling_ms ? timer_name[timer] : "null",
2024 polling_ms,
2025 cur_freq,
2026 min_freq,
2027 max_freq);
2028 }
2029
2030 mutex_unlock(&devfreq_list_lock);
2031
2032 return 0;
2033 }
2034 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
2035
devfreq_init(void)2036 static int __init devfreq_init(void)
2037 {
2038 devfreq_class = class_create("devfreq");
2039 if (IS_ERR(devfreq_class)) {
2040 pr_err("%s: couldn't create class\n", __FILE__);
2041 return PTR_ERR(devfreq_class);
2042 }
2043
2044 devfreq_wq = create_freezable_workqueue("devfreq_wq");
2045 if (!devfreq_wq) {
2046 class_destroy(devfreq_class);
2047 pr_err("%s: couldn't create workqueue\n", __FILE__);
2048 return -ENOMEM;
2049 }
2050 devfreq_class->dev_groups = devfreq_groups;
2051
2052 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
2053 debugfs_create_file("devfreq_summary", 0444,
2054 devfreq_debugfs, NULL,
2055 &devfreq_summary_fops);
2056
2057 return 0;
2058 }
2059 subsys_initcall(devfreq_init);
2060
2061 /*
2062 * The following are helper functions for devfreq user device drivers with
2063 * OPP framework.
2064 */
2065
2066 /**
2067 * devfreq_recommended_opp() - Helper function to get proper OPP for the
2068 * freq value given to target callback.
2069 * @dev: The devfreq user device. (parent of devfreq)
2070 * @freq: The frequency given to target function
2071 * @flags: Flags handed from devfreq framework.
2072 *
2073 * The callers are required to call dev_pm_opp_put() for the returned OPP after
2074 * use.
2075 */
devfreq_recommended_opp(struct device * dev,unsigned long * freq,u32 flags)2076 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
2077 unsigned long *freq,
2078 u32 flags)
2079 {
2080 struct dev_pm_opp *opp;
2081
2082 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
2083 /* The freq is an upper bound. opp should be lower */
2084 opp = dev_pm_opp_find_freq_floor_indexed(dev, freq, 0);
2085
2086 /* If not available, use the closest opp */
2087 if (opp == ERR_PTR(-ERANGE))
2088 opp = dev_pm_opp_find_freq_ceil_indexed(dev, freq, 0);
2089 } else {
2090 /* The freq is an lower bound. opp should be higher */
2091 opp = dev_pm_opp_find_freq_ceil_indexed(dev, freq, 0);
2092
2093 /* If not available, use the closest opp */
2094 if (opp == ERR_PTR(-ERANGE))
2095 opp = dev_pm_opp_find_freq_floor_indexed(dev, freq, 0);
2096 }
2097
2098 return opp;
2099 }
2100 EXPORT_SYMBOL(devfreq_recommended_opp);
2101
2102 /**
2103 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2104 * for any changes in the OPP availability
2105 * changes
2106 * @dev: The devfreq user device. (parent of devfreq)
2107 * @devfreq: The devfreq object.
2108 */
devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)2109 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2110 {
2111 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2112 }
2113 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2114
2115 /**
2116 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2117 * notified for any changes in the OPP
2118 * availability changes anymore.
2119 * @dev: The devfreq user device. (parent of devfreq)
2120 * @devfreq: The devfreq object.
2121 *
2122 * At exit() callback of devfreq_dev_profile, this must be included if
2123 * devfreq_recommended_opp is used.
2124 */
devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)2125 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2126 {
2127 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2128 }
2129 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2130
devm_devfreq_opp_release(struct device * dev,void * res)2131 static void devm_devfreq_opp_release(struct device *dev, void *res)
2132 {
2133 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2134 }
2135
2136 /**
2137 * devm_devfreq_register_opp_notifier() - Resource-managed
2138 * devfreq_register_opp_notifier()
2139 * @dev: The devfreq user device. (parent of devfreq)
2140 * @devfreq: The devfreq object.
2141 */
devm_devfreq_register_opp_notifier(struct device * dev,struct devfreq * devfreq)2142 int devm_devfreq_register_opp_notifier(struct device *dev,
2143 struct devfreq *devfreq)
2144 {
2145 struct devfreq **ptr;
2146 int ret;
2147
2148 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2149 if (!ptr)
2150 return -ENOMEM;
2151
2152 ret = devfreq_register_opp_notifier(dev, devfreq);
2153 if (ret) {
2154 devres_free(ptr);
2155 return ret;
2156 }
2157
2158 *ptr = devfreq;
2159 devres_add(dev, ptr);
2160
2161 return 0;
2162 }
2163 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2164
2165 /**
2166 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2167 * devfreq_unregister_opp_notifier()
2168 * @dev: The devfreq user device. (parent of devfreq)
2169 * @devfreq: The devfreq object.
2170 */
devm_devfreq_unregister_opp_notifier(struct device * dev,struct devfreq * devfreq)2171 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2172 struct devfreq *devfreq)
2173 {
2174 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2175 devm_devfreq_dev_match, devfreq));
2176 }
2177 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2178
2179 /**
2180 * devfreq_register_notifier() - Register a driver with devfreq
2181 * @devfreq: The devfreq object.
2182 * @nb: The notifier block to register.
2183 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2184 */
devfreq_register_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2185 int devfreq_register_notifier(struct devfreq *devfreq,
2186 struct notifier_block *nb,
2187 unsigned int list)
2188 {
2189 int ret = 0;
2190
2191 if (!devfreq)
2192 return -EINVAL;
2193
2194 switch (list) {
2195 case DEVFREQ_TRANSITION_NOTIFIER:
2196 ret = srcu_notifier_chain_register(
2197 &devfreq->transition_notifier_list, nb);
2198 break;
2199 default:
2200 ret = -EINVAL;
2201 }
2202
2203 return ret;
2204 }
2205 EXPORT_SYMBOL(devfreq_register_notifier);
2206
2207 /*
2208 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2209 * @devfreq: The devfreq object.
2210 * @nb: The notifier block to be unregistered.
2211 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2212 */
devfreq_unregister_notifier(struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2213 int devfreq_unregister_notifier(struct devfreq *devfreq,
2214 struct notifier_block *nb,
2215 unsigned int list)
2216 {
2217 int ret = 0;
2218
2219 if (!devfreq)
2220 return -EINVAL;
2221
2222 switch (list) {
2223 case DEVFREQ_TRANSITION_NOTIFIER:
2224 ret = srcu_notifier_chain_unregister(
2225 &devfreq->transition_notifier_list, nb);
2226 break;
2227 default:
2228 ret = -EINVAL;
2229 }
2230
2231 return ret;
2232 }
2233 EXPORT_SYMBOL(devfreq_unregister_notifier);
2234
2235 struct devfreq_notifier_devres {
2236 struct devfreq *devfreq;
2237 struct notifier_block *nb;
2238 unsigned int list;
2239 };
2240
devm_devfreq_notifier_release(struct device * dev,void * res)2241 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2242 {
2243 struct devfreq_notifier_devres *this = res;
2244
2245 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2246 }
2247
2248 /**
2249 * devm_devfreq_register_notifier()
2250 * - Resource-managed devfreq_register_notifier()
2251 * @dev: The devfreq user device. (parent of devfreq)
2252 * @devfreq: The devfreq object.
2253 * @nb: The notifier block to be unregistered.
2254 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2255 */
devm_devfreq_register_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2256 int devm_devfreq_register_notifier(struct device *dev,
2257 struct devfreq *devfreq,
2258 struct notifier_block *nb,
2259 unsigned int list)
2260 {
2261 struct devfreq_notifier_devres *ptr;
2262 int ret;
2263
2264 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2265 GFP_KERNEL);
2266 if (!ptr)
2267 return -ENOMEM;
2268
2269 ret = devfreq_register_notifier(devfreq, nb, list);
2270 if (ret) {
2271 devres_free(ptr);
2272 return ret;
2273 }
2274
2275 ptr->devfreq = devfreq;
2276 ptr->nb = nb;
2277 ptr->list = list;
2278 devres_add(dev, ptr);
2279
2280 return 0;
2281 }
2282 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2283
2284 /**
2285 * devm_devfreq_unregister_notifier()
2286 * - Resource-managed devfreq_unregister_notifier()
2287 * @dev: The devfreq user device. (parent of devfreq)
2288 * @devfreq: The devfreq object.
2289 * @nb: The notifier block to be unregistered.
2290 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2291 */
devm_devfreq_unregister_notifier(struct device * dev,struct devfreq * devfreq,struct notifier_block * nb,unsigned int list)2292 void devm_devfreq_unregister_notifier(struct device *dev,
2293 struct devfreq *devfreq,
2294 struct notifier_block *nb,
2295 unsigned int list)
2296 {
2297 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2298 devm_devfreq_dev_match, devfreq));
2299 }
2300 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
2301