Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / devfreq / devfreq.c
blob6aa10de792b33060cd9840c471d62c8f9e87737f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4 * for Non-CPU Devices.
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 */
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
20 #include <linux/pm_opp.h>
21 #include <linux/devfreq.h>
22 #include <linux/workqueue.h>
23 #include <linux/platform_device.h>
24 #include <linux/list.h>
25 #include <linux/printk.h>
26 #include <linux/hrtimer.h>
27 #include <linux/of.h>
28 #include <linux/pm_qos.h>
29 #include "governor.h"
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devfreq.h>
34 #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false)
35 #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false)
36 #define HZ_PER_KHZ 1000
38 static struct class *devfreq_class;
39 static struct dentry *devfreq_debugfs;
42 * devfreq core provides delayed work based load monitoring helper
43 * functions. Governors can use these or can implement their own
44 * monitoring mechanism.
46 static struct workqueue_struct *devfreq_wq;
48 /* The list of all device-devfreq governors */
49 static LIST_HEAD(devfreq_governor_list);
50 /* The list of all device-devfreq */
51 static LIST_HEAD(devfreq_list);
52 static DEFINE_MUTEX(devfreq_list_lock);
54 static const char timer_name[][DEVFREQ_NAME_LEN] = {
55 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" },
56 [DEVFREQ_TIMER_DELAYED] = { "delayed" },
59 /**
60 * find_device_devfreq() - find devfreq struct using device pointer
61 * @dev: device pointer used to lookup device devfreq.
63 * Search the list of device devfreqs and return the matched device's
64 * devfreq info. devfreq_list_lock should be held by the caller.
66 static struct devfreq *find_device_devfreq(struct device *dev)
68 struct devfreq *tmp_devfreq;
70 lockdep_assert_held(&devfreq_list_lock);
72 if (IS_ERR_OR_NULL(dev)) {
73 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
74 return ERR_PTR(-EINVAL);
77 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
78 if (tmp_devfreq->dev.parent == dev)
79 return tmp_devfreq;
82 return ERR_PTR(-ENODEV);
85 static unsigned long find_available_min_freq(struct devfreq *devfreq)
87 struct dev_pm_opp *opp;
88 unsigned long min_freq = 0;
90 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
91 if (IS_ERR(opp))
92 min_freq = 0;
93 else
94 dev_pm_opp_put(opp);
96 return min_freq;
99 static unsigned long find_available_max_freq(struct devfreq *devfreq)
101 struct dev_pm_opp *opp;
102 unsigned long max_freq = ULONG_MAX;
104 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
105 if (IS_ERR(opp))
106 max_freq = 0;
107 else
108 dev_pm_opp_put(opp);
110 return max_freq;
114 * get_freq_range() - Get the current freq range
115 * @devfreq: the devfreq instance
116 * @min_freq: the min frequency
117 * @max_freq: the max frequency
119 * This takes into consideration all constraints.
121 static void get_freq_range(struct devfreq *devfreq,
122 unsigned long *min_freq,
123 unsigned long *max_freq)
125 unsigned long *freq_table = devfreq->profile->freq_table;
126 s32 qos_min_freq, qos_max_freq;
128 lockdep_assert_held(&devfreq->lock);
131 * Initialize minimum/maximum frequency from freq table.
132 * The devfreq drivers can initialize this in either ascending or
133 * descending order and devfreq core supports both.
135 if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
136 *min_freq = freq_table[0];
137 *max_freq = freq_table[devfreq->profile->max_state - 1];
138 } else {
139 *min_freq = freq_table[devfreq->profile->max_state - 1];
140 *max_freq = freq_table[0];
143 /* Apply constraints from PM QoS */
144 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
145 DEV_PM_QOS_MIN_FREQUENCY);
146 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
147 DEV_PM_QOS_MAX_FREQUENCY);
148 *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
149 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
150 *max_freq = min(*max_freq,
151 (unsigned long)HZ_PER_KHZ * qos_max_freq);
153 /* Apply constraints from OPP interface */
154 *min_freq = max(*min_freq, devfreq->scaling_min_freq);
155 *max_freq = min(*max_freq, devfreq->scaling_max_freq);
157 if (*min_freq > *max_freq)
158 *min_freq = *max_freq;
162 * devfreq_get_freq_level() - Lookup freq_table for the frequency
163 * @devfreq: the devfreq instance
164 * @freq: the target frequency
166 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
168 int lev;
170 for (lev = 0; lev < devfreq->profile->max_state; lev++)
171 if (freq == devfreq->profile->freq_table[lev])
172 return lev;
174 return -EINVAL;
177 static int set_freq_table(struct devfreq *devfreq)
179 struct devfreq_dev_profile *profile = devfreq->profile;
180 struct dev_pm_opp *opp;
181 unsigned long freq;
182 int i, count;
184 /* Initialize the freq_table from OPP table */
185 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
186 if (count <= 0)
187 return -EINVAL;
189 profile->max_state = count;
190 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
191 profile->max_state,
192 sizeof(*profile->freq_table),
193 GFP_KERNEL);
194 if (!profile->freq_table) {
195 profile->max_state = 0;
196 return -ENOMEM;
199 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
200 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
201 if (IS_ERR(opp)) {
202 devm_kfree(devfreq->dev.parent, profile->freq_table);
203 profile->max_state = 0;
204 return PTR_ERR(opp);
206 dev_pm_opp_put(opp);
207 profile->freq_table[i] = freq;
210 return 0;
214 * devfreq_update_status() - Update statistics of devfreq behavior
215 * @devfreq: the devfreq instance
216 * @freq: the update target frequency
218 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
220 int lev, prev_lev, ret = 0;
221 u64 cur_time;
223 lockdep_assert_held(&devfreq->lock);
224 cur_time = get_jiffies_64();
226 /* Immediately exit if previous_freq is not initialized yet. */
227 if (!devfreq->previous_freq)
228 goto out;
230 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
231 if (prev_lev < 0) {
232 ret = prev_lev;
233 goto out;
236 devfreq->stats.time_in_state[prev_lev] +=
237 cur_time - devfreq->stats.last_update;
239 lev = devfreq_get_freq_level(devfreq, freq);
240 if (lev < 0) {
241 ret = lev;
242 goto out;
245 if (lev != prev_lev) {
246 devfreq->stats.trans_table[
247 (prev_lev * devfreq->profile->max_state) + lev]++;
248 devfreq->stats.total_trans++;
251 out:
252 devfreq->stats.last_update = cur_time;
253 return ret;
255 EXPORT_SYMBOL(devfreq_update_status);
258 * find_devfreq_governor() - find devfreq governor from name
259 * @name: name of the governor
261 * Search the list of devfreq governors and return the matched
262 * governor's pointer. devfreq_list_lock should be held by the caller.
264 static struct devfreq_governor *find_devfreq_governor(const char *name)
266 struct devfreq_governor *tmp_governor;
268 lockdep_assert_held(&devfreq_list_lock);
270 if (IS_ERR_OR_NULL(name)) {
271 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
272 return ERR_PTR(-EINVAL);
275 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
276 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
277 return tmp_governor;
280 return ERR_PTR(-ENODEV);
284 * try_then_request_governor() - Try to find the governor and request the
285 * module if is not found.
286 * @name: name of the governor
288 * Search the list of devfreq governors and request the module and try again
289 * if is not found. This can happen when both drivers (the governor driver
290 * and the driver that call devfreq_add_device) are built as modules.
291 * devfreq_list_lock should be held by the caller. Returns the matched
292 * governor's pointer or an error pointer.
294 static struct devfreq_governor *try_then_request_governor(const char *name)
296 struct devfreq_governor *governor;
297 int err = 0;
299 lockdep_assert_held(&devfreq_list_lock);
301 if (IS_ERR_OR_NULL(name)) {
302 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
303 return ERR_PTR(-EINVAL);
306 governor = find_devfreq_governor(name);
307 if (IS_ERR(governor)) {
308 mutex_unlock(&devfreq_list_lock);
310 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
311 DEVFREQ_NAME_LEN))
312 err = request_module("governor_%s", "simpleondemand");
313 else
314 err = request_module("governor_%s", name);
315 /* Restore previous state before return */
316 mutex_lock(&devfreq_list_lock);
317 if (err)
318 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
320 governor = find_devfreq_governor(name);
323 return governor;
326 static int devfreq_notify_transition(struct devfreq *devfreq,
327 struct devfreq_freqs *freqs, unsigned int state)
329 if (!devfreq)
330 return -EINVAL;
332 switch (state) {
333 case DEVFREQ_PRECHANGE:
334 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
335 DEVFREQ_PRECHANGE, freqs);
336 break;
338 case DEVFREQ_POSTCHANGE:
339 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
340 DEVFREQ_POSTCHANGE, freqs);
341 break;
342 default:
343 return -EINVAL;
346 return 0;
349 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
350 u32 flags)
352 struct devfreq_freqs freqs;
353 unsigned long cur_freq;
354 int err = 0;
356 if (devfreq->profile->get_cur_freq)
357 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
358 else
359 cur_freq = devfreq->previous_freq;
361 freqs.old = cur_freq;
362 freqs.new = new_freq;
363 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
365 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
366 if (err) {
367 freqs.new = cur_freq;
368 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
369 return err;
373 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE
374 * and DEVFREQ_POSTCHANGE because for showing the correct frequency
375 * change order of between devfreq device and passive devfreq device.
377 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq)
378 trace_devfreq_frequency(devfreq, new_freq, cur_freq);
380 freqs.new = new_freq;
381 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
383 if (devfreq_update_status(devfreq, new_freq))
384 dev_err(&devfreq->dev,
385 "Couldn't update frequency transition information.\n");
387 devfreq->previous_freq = new_freq;
389 if (devfreq->suspend_freq)
390 devfreq->resume_freq = cur_freq;
392 return err;
396 * devfreq_update_target() - Reevaluate the device and configure frequency
397 * on the final stage.
398 * @devfreq: the devfreq instance.
399 * @freq: the new frequency of parent device. This argument
400 * is only used for devfreq device using passive governor.
402 * Note: Lock devfreq->lock before calling devfreq_update_target. This function
403 * should be only used by both update_devfreq() and devfreq governors.
405 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq)
407 unsigned long min_freq, max_freq;
408 int err = 0;
409 u32 flags = 0;
411 lockdep_assert_held(&devfreq->lock);
413 if (!devfreq->governor)
414 return -EINVAL;
416 /* Reevaluate the proper frequency */
417 err = devfreq->governor->get_target_freq(devfreq, &freq);
418 if (err)
419 return err;
420 get_freq_range(devfreq, &min_freq, &max_freq);
422 if (freq < min_freq) {
423 freq = min_freq;
424 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
426 if (freq > max_freq) {
427 freq = max_freq;
428 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
431 return devfreq_set_target(devfreq, freq, flags);
433 EXPORT_SYMBOL(devfreq_update_target);
435 /* Load monitoring helper functions for governors use */
438 * update_devfreq() - Reevaluate the device and configure frequency.
439 * @devfreq: the devfreq instance.
441 * Note: Lock devfreq->lock before calling update_devfreq
442 * This function is exported for governors.
444 int update_devfreq(struct devfreq *devfreq)
446 return devfreq_update_target(devfreq, 0L);
448 EXPORT_SYMBOL(update_devfreq);
451 * devfreq_monitor() - Periodically poll devfreq objects.
452 * @work: the work struct used to run devfreq_monitor periodically.
455 static void devfreq_monitor(struct work_struct *work)
457 int err;
458 struct devfreq *devfreq = container_of(work,
459 struct devfreq, work.work);
461 mutex_lock(&devfreq->lock);
462 err = update_devfreq(devfreq);
463 if (err)
464 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
466 queue_delayed_work(devfreq_wq, &devfreq->work,
467 msecs_to_jiffies(devfreq->profile->polling_ms));
468 mutex_unlock(&devfreq->lock);
470 trace_devfreq_monitor(devfreq);
474 * devfreq_monitor_start() - Start load monitoring of devfreq instance
475 * @devfreq: the devfreq instance.
477 * Helper function for starting devfreq device load monitoring. By
478 * default delayed work based monitoring is supported. Function
479 * to be called from governor in response to DEVFREQ_GOV_START
480 * event when device is added to devfreq framework.
482 void devfreq_monitor_start(struct devfreq *devfreq)
484 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
485 return;
487 switch (devfreq->profile->timer) {
488 case DEVFREQ_TIMER_DEFERRABLE:
489 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
490 break;
491 case DEVFREQ_TIMER_DELAYED:
492 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor);
493 break;
494 default:
495 return;
498 if (devfreq->profile->polling_ms)
499 queue_delayed_work(devfreq_wq, &devfreq->work,
500 msecs_to_jiffies(devfreq->profile->polling_ms));
502 EXPORT_SYMBOL(devfreq_monitor_start);
505 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
506 * @devfreq: the devfreq instance.
508 * Helper function to stop devfreq device load monitoring. Function
509 * to be called from governor in response to DEVFREQ_GOV_STOP
510 * event when device is removed from devfreq framework.
512 void devfreq_monitor_stop(struct devfreq *devfreq)
514 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
515 return;
517 cancel_delayed_work_sync(&devfreq->work);
519 EXPORT_SYMBOL(devfreq_monitor_stop);
522 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
523 * @devfreq: the devfreq instance.
525 * Helper function to suspend devfreq device load monitoring. Function
526 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
527 * event or when polling interval is set to zero.
529 * Note: Though this function is same as devfreq_monitor_stop(),
530 * intentionally kept separate to provide hooks for collecting
531 * transition statistics.
533 void devfreq_monitor_suspend(struct devfreq *devfreq)
535 mutex_lock(&devfreq->lock);
536 if (devfreq->stop_polling) {
537 mutex_unlock(&devfreq->lock);
538 return;
541 devfreq_update_status(devfreq, devfreq->previous_freq);
542 devfreq->stop_polling = true;
543 mutex_unlock(&devfreq->lock);
545 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
546 return;
548 cancel_delayed_work_sync(&devfreq->work);
550 EXPORT_SYMBOL(devfreq_monitor_suspend);
553 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
554 * @devfreq: the devfreq instance.
556 * Helper function to resume devfreq device load monitoring. Function
557 * to be called from governor in response to DEVFREQ_GOV_RESUME
558 * event or when polling interval is set to non-zero.
560 void devfreq_monitor_resume(struct devfreq *devfreq)
562 unsigned long freq;
564 mutex_lock(&devfreq->lock);
566 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
567 goto out_update;
569 if (!devfreq->stop_polling)
570 goto out;
572 if (!delayed_work_pending(&devfreq->work) &&
573 devfreq->profile->polling_ms)
574 queue_delayed_work(devfreq_wq, &devfreq->work,
575 msecs_to_jiffies(devfreq->profile->polling_ms));
577 out_update:
578 devfreq->stats.last_update = get_jiffies_64();
579 devfreq->stop_polling = false;
581 if (devfreq->profile->get_cur_freq &&
582 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
583 devfreq->previous_freq = freq;
585 out:
586 mutex_unlock(&devfreq->lock);
588 EXPORT_SYMBOL(devfreq_monitor_resume);
591 * devfreq_update_interval() - Update device devfreq monitoring interval
592 * @devfreq: the devfreq instance.
593 * @delay: new polling interval to be set.
595 * Helper function to set new load monitoring polling interval. Function
596 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
598 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
600 unsigned int cur_delay = devfreq->profile->polling_ms;
601 unsigned int new_delay = *delay;
603 mutex_lock(&devfreq->lock);
604 devfreq->profile->polling_ms = new_delay;
606 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN))
607 goto out;
609 if (devfreq->stop_polling)
610 goto out;
612 /* if new delay is zero, stop polling */
613 if (!new_delay) {
614 mutex_unlock(&devfreq->lock);
615 cancel_delayed_work_sync(&devfreq->work);
616 return;
619 /* if current delay is zero, start polling with new delay */
620 if (!cur_delay) {
621 queue_delayed_work(devfreq_wq, &devfreq->work,
622 msecs_to_jiffies(devfreq->profile->polling_ms));
623 goto out;
626 /* if current delay is greater than new delay, restart polling */
627 if (cur_delay > new_delay) {
628 mutex_unlock(&devfreq->lock);
629 cancel_delayed_work_sync(&devfreq->work);
630 mutex_lock(&devfreq->lock);
631 if (!devfreq->stop_polling)
632 queue_delayed_work(devfreq_wq, &devfreq->work,
633 msecs_to_jiffies(devfreq->profile->polling_ms));
635 out:
636 mutex_unlock(&devfreq->lock);
638 EXPORT_SYMBOL(devfreq_update_interval);
641 * devfreq_notifier_call() - Notify that the device frequency requirements
642 * has been changed out of devfreq framework.
643 * @nb: the notifier_block (supposed to be devfreq->nb)
644 * @type: not used
645 * @devp: not used
647 * Called by a notifier that uses devfreq->nb.
649 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
650 void *devp)
652 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
653 int err = -EINVAL;
655 mutex_lock(&devfreq->lock);
657 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
658 if (!devfreq->scaling_min_freq)
659 goto out;
661 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
662 if (!devfreq->scaling_max_freq) {
663 devfreq->scaling_max_freq = ULONG_MAX;
664 goto out;
667 err = update_devfreq(devfreq);
669 out:
670 mutex_unlock(&devfreq->lock);
671 if (err)
672 dev_err(devfreq->dev.parent,
673 "failed to update frequency from OPP notifier (%d)\n",
674 err);
676 return NOTIFY_OK;
680 * qos_notifier_call() - Common handler for QoS constraints.
681 * @devfreq: the devfreq instance.
683 static int qos_notifier_call(struct devfreq *devfreq)
685 int err;
687 mutex_lock(&devfreq->lock);
688 err = update_devfreq(devfreq);
689 mutex_unlock(&devfreq->lock);
690 if (err)
691 dev_err(devfreq->dev.parent,
692 "failed to update frequency from PM QoS (%d)\n",
693 err);
695 return NOTIFY_OK;
699 * qos_min_notifier_call() - Callback for QoS min_freq changes.
700 * @nb: Should be devfreq->nb_min
702 static int qos_min_notifier_call(struct notifier_block *nb,
703 unsigned long val, void *ptr)
705 return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
709 * qos_max_notifier_call() - Callback for QoS max_freq changes.
710 * @nb: Should be devfreq->nb_max
712 static int qos_max_notifier_call(struct notifier_block *nb,
713 unsigned long val, void *ptr)
715 return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
719 * devfreq_dev_release() - Callback for struct device to release the device.
720 * @dev: the devfreq device
722 * Remove devfreq from the list and release its resources.
724 static void devfreq_dev_release(struct device *dev)
726 struct devfreq *devfreq = to_devfreq(dev);
727 int err;
729 mutex_lock(&devfreq_list_lock);
730 list_del(&devfreq->node);
731 mutex_unlock(&devfreq_list_lock);
733 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
734 DEV_PM_QOS_MAX_FREQUENCY);
735 if (err && err != -ENOENT)
736 dev_warn(dev->parent,
737 "Failed to remove max_freq notifier: %d\n", err);
738 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
739 DEV_PM_QOS_MIN_FREQUENCY);
740 if (err && err != -ENOENT)
741 dev_warn(dev->parent,
742 "Failed to remove min_freq notifier: %d\n", err);
744 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
745 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
746 if (err < 0)
747 dev_warn(dev->parent,
748 "Failed to remove max_freq request: %d\n", err);
750 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
751 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
752 if (err < 0)
753 dev_warn(dev->parent,
754 "Failed to remove min_freq request: %d\n", err);
757 if (devfreq->profile->exit)
758 devfreq->profile->exit(devfreq->dev.parent);
760 mutex_destroy(&devfreq->lock);
761 kfree(devfreq);
764 static void create_sysfs_files(struct devfreq *devfreq,
765 const struct devfreq_governor *gov);
766 static void remove_sysfs_files(struct devfreq *devfreq,
767 const struct devfreq_governor *gov);
770 * devfreq_add_device() - Add devfreq feature to the device
771 * @dev: the device to add devfreq feature.
772 * @profile: device-specific profile to run devfreq.
773 * @governor_name: name of the policy to choose frequency.
774 * @data: private data for the governor. The devfreq framework does not
775 * touch this value.
777 struct devfreq *devfreq_add_device(struct device *dev,
778 struct devfreq_dev_profile *profile,
779 const char *governor_name,
780 void *data)
782 struct devfreq *devfreq;
783 struct devfreq_governor *governor;
784 int err = 0;
786 if (!dev || !profile || !governor_name) {
787 dev_err(dev, "%s: Invalid parameters.\n", __func__);
788 return ERR_PTR(-EINVAL);
791 mutex_lock(&devfreq_list_lock);
792 devfreq = find_device_devfreq(dev);
793 mutex_unlock(&devfreq_list_lock);
794 if (!IS_ERR(devfreq)) {
795 dev_err(dev, "%s: devfreq device already exists!\n",
796 __func__);
797 err = -EINVAL;
798 goto err_out;
801 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
802 if (!devfreq) {
803 err = -ENOMEM;
804 goto err_out;
807 mutex_init(&devfreq->lock);
808 mutex_lock(&devfreq->lock);
809 devfreq->dev.parent = dev;
810 devfreq->dev.class = devfreq_class;
811 devfreq->dev.release = devfreq_dev_release;
812 INIT_LIST_HEAD(&devfreq->node);
813 devfreq->profile = profile;
814 devfreq->previous_freq = profile->initial_freq;
815 devfreq->last_status.current_frequency = profile->initial_freq;
816 devfreq->data = data;
817 devfreq->nb.notifier_call = devfreq_notifier_call;
819 if (devfreq->profile->timer < 0
820 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) {
821 goto err_out;
824 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
825 mutex_unlock(&devfreq->lock);
826 err = set_freq_table(devfreq);
827 if (err < 0)
828 goto err_dev;
829 mutex_lock(&devfreq->lock);
832 devfreq->scaling_min_freq = find_available_min_freq(devfreq);
833 if (!devfreq->scaling_min_freq) {
834 mutex_unlock(&devfreq->lock);
835 err = -EINVAL;
836 goto err_dev;
839 devfreq->scaling_max_freq = find_available_max_freq(devfreq);
840 if (!devfreq->scaling_max_freq) {
841 mutex_unlock(&devfreq->lock);
842 err = -EINVAL;
843 goto err_dev;
846 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
847 atomic_set(&devfreq->suspend_count, 0);
849 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
850 err = device_register(&devfreq->dev);
851 if (err) {
852 mutex_unlock(&devfreq->lock);
853 put_device(&devfreq->dev);
854 goto err_out;
857 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
858 array3_size(sizeof(unsigned int),
859 devfreq->profile->max_state,
860 devfreq->profile->max_state),
861 GFP_KERNEL);
862 if (!devfreq->stats.trans_table) {
863 mutex_unlock(&devfreq->lock);
864 err = -ENOMEM;
865 goto err_devfreq;
868 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
869 devfreq->profile->max_state,
870 sizeof(*devfreq->stats.time_in_state),
871 GFP_KERNEL);
872 if (!devfreq->stats.time_in_state) {
873 mutex_unlock(&devfreq->lock);
874 err = -ENOMEM;
875 goto err_devfreq;
878 devfreq->stats.total_trans = 0;
879 devfreq->stats.last_update = get_jiffies_64();
881 srcu_init_notifier_head(&devfreq->transition_notifier_list);
883 mutex_unlock(&devfreq->lock);
885 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
886 DEV_PM_QOS_MIN_FREQUENCY, 0);
887 if (err < 0)
888 goto err_devfreq;
889 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
890 DEV_PM_QOS_MAX_FREQUENCY,
891 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
892 if (err < 0)
893 goto err_devfreq;
895 devfreq->nb_min.notifier_call = qos_min_notifier_call;
896 err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min,
897 DEV_PM_QOS_MIN_FREQUENCY);
898 if (err)
899 goto err_devfreq;
901 devfreq->nb_max.notifier_call = qos_max_notifier_call;
902 err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_max,
903 DEV_PM_QOS_MAX_FREQUENCY);
904 if (err)
905 goto err_devfreq;
907 mutex_lock(&devfreq_list_lock);
909 governor = try_then_request_governor(governor_name);
910 if (IS_ERR(governor)) {
911 dev_err(dev, "%s: Unable to find governor for the device\n",
912 __func__);
913 err = PTR_ERR(governor);
914 goto err_init;
917 devfreq->governor = governor;
918 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
919 NULL);
920 if (err) {
921 dev_err(dev, "%s: Unable to start governor for the device\n",
922 __func__);
923 goto err_init;
925 create_sysfs_files(devfreq, devfreq->governor);
927 list_add(&devfreq->node, &devfreq_list);
929 mutex_unlock(&devfreq_list_lock);
931 return devfreq;
933 err_init:
934 mutex_unlock(&devfreq_list_lock);
935 err_devfreq:
936 devfreq_remove_device(devfreq);
937 devfreq = NULL;
938 err_dev:
939 kfree(devfreq);
940 err_out:
941 return ERR_PTR(err);
943 EXPORT_SYMBOL(devfreq_add_device);
946 * devfreq_remove_device() - Remove devfreq feature from a device.
947 * @devfreq: the devfreq instance to be removed
949 * The opposite of devfreq_add_device().
951 int devfreq_remove_device(struct devfreq *devfreq)
953 if (!devfreq)
954 return -EINVAL;
956 if (devfreq->governor) {
957 devfreq->governor->event_handler(devfreq,
958 DEVFREQ_GOV_STOP, NULL);
959 remove_sysfs_files(devfreq, devfreq->governor);
962 device_unregister(&devfreq->dev);
964 return 0;
966 EXPORT_SYMBOL(devfreq_remove_device);
968 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
970 struct devfreq **r = res;
972 if (WARN_ON(!r || !*r))
973 return 0;
975 return *r == data;
978 static void devm_devfreq_dev_release(struct device *dev, void *res)
980 devfreq_remove_device(*(struct devfreq **)res);
984 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
985 * @dev: the device to add devfreq feature.
986 * @profile: device-specific profile to run devfreq.
987 * @governor_name: name of the policy to choose frequency.
988 * @data: private data for the governor. The devfreq framework does not
989 * touch this value.
991 * This function manages automatically the memory of devfreq device using device
992 * resource management and simplify the free operation for memory of devfreq
993 * device.
995 struct devfreq *devm_devfreq_add_device(struct device *dev,
996 struct devfreq_dev_profile *profile,
997 const char *governor_name,
998 void *data)
1000 struct devfreq **ptr, *devfreq;
1002 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
1003 if (!ptr)
1004 return ERR_PTR(-ENOMEM);
1006 devfreq = devfreq_add_device(dev, profile, governor_name, data);
1007 if (IS_ERR(devfreq)) {
1008 devres_free(ptr);
1009 return devfreq;
1012 *ptr = devfreq;
1013 devres_add(dev, ptr);
1015 return devfreq;
1017 EXPORT_SYMBOL(devm_devfreq_add_device);
1019 #ifdef CONFIG_OF
1021 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree
1022 * @node - pointer to device_node
1024 * return the instance of devfreq device
1026 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1028 struct devfreq *devfreq;
1030 if (!node)
1031 return ERR_PTR(-EINVAL);
1033 mutex_lock(&devfreq_list_lock);
1034 list_for_each_entry(devfreq, &devfreq_list, node) {
1035 if (devfreq->dev.parent
1036 && devfreq->dev.parent->of_node == node) {
1037 mutex_unlock(&devfreq_list_lock);
1038 return devfreq;
1041 mutex_unlock(&devfreq_list_lock);
1043 return ERR_PTR(-ENODEV);
1047 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
1048 * @dev - instance to the given device
1049 * @phandle_name - name of property holding a phandle value
1050 * @index - index into list of devfreq
1052 * return the instance of devfreq device
1054 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1055 const char *phandle_name, int index)
1057 struct device_node *node;
1058 struct devfreq *devfreq;
1060 if (!dev || !phandle_name)
1061 return ERR_PTR(-EINVAL);
1063 if (!dev->of_node)
1064 return ERR_PTR(-EINVAL);
1066 node = of_parse_phandle(dev->of_node, phandle_name, index);
1067 if (!node)
1068 return ERR_PTR(-ENODEV);
1070 devfreq = devfreq_get_devfreq_by_node(node);
1071 of_node_put(node);
1073 return devfreq;
1076 #else
1077 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
1079 return ERR_PTR(-ENODEV);
1082 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
1083 const char *phandle_name, int index)
1085 return ERR_PTR(-ENODEV);
1087 #endif /* CONFIG_OF */
1088 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node);
1089 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1092 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1093 * @dev: the device from which to remove devfreq feature.
1094 * @devfreq: the devfreq instance to be removed
1096 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1098 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1099 devm_devfreq_dev_match, devfreq));
1101 EXPORT_SYMBOL(devm_devfreq_remove_device);
1104 * devfreq_suspend_device() - Suspend devfreq of a device.
1105 * @devfreq: the devfreq instance to be suspended
1107 * This function is intended to be called by the pm callbacks
1108 * (e.g., runtime_suspend, suspend) of the device driver that
1109 * holds the devfreq.
1111 int devfreq_suspend_device(struct devfreq *devfreq)
1113 int ret;
1115 if (!devfreq)
1116 return -EINVAL;
1118 if (atomic_inc_return(&devfreq->suspend_count) > 1)
1119 return 0;
1121 if (devfreq->governor) {
1122 ret = devfreq->governor->event_handler(devfreq,
1123 DEVFREQ_GOV_SUSPEND, NULL);
1124 if (ret)
1125 return ret;
1128 if (devfreq->suspend_freq) {
1129 mutex_lock(&devfreq->lock);
1130 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1131 mutex_unlock(&devfreq->lock);
1132 if (ret)
1133 return ret;
1136 return 0;
1138 EXPORT_SYMBOL(devfreq_suspend_device);
1141 * devfreq_resume_device() - Resume devfreq of a device.
1142 * @devfreq: the devfreq instance to be resumed
1144 * This function is intended to be called by the pm callbacks
1145 * (e.g., runtime_resume, resume) of the device driver that
1146 * holds the devfreq.
1148 int devfreq_resume_device(struct devfreq *devfreq)
1150 int ret;
1152 if (!devfreq)
1153 return -EINVAL;
1155 if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1156 return 0;
1158 if (devfreq->resume_freq) {
1159 mutex_lock(&devfreq->lock);
1160 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1161 mutex_unlock(&devfreq->lock);
1162 if (ret)
1163 return ret;
1166 if (devfreq->governor) {
1167 ret = devfreq->governor->event_handler(devfreq,
1168 DEVFREQ_GOV_RESUME, NULL);
1169 if (ret)
1170 return ret;
1173 return 0;
1175 EXPORT_SYMBOL(devfreq_resume_device);
1178 * devfreq_suspend() - Suspend devfreq governors and devices
1180 * Called during system wide Suspend/Hibernate cycles for suspending governors
1181 * and devices preserving the state for resume. On some platforms the devfreq
1182 * device must have precise state (frequency) after resume in order to provide
1183 * fully operating setup.
1185 void devfreq_suspend(void)
1187 struct devfreq *devfreq;
1188 int ret;
1190 mutex_lock(&devfreq_list_lock);
1191 list_for_each_entry(devfreq, &devfreq_list, node) {
1192 ret = devfreq_suspend_device(devfreq);
1193 if (ret)
1194 dev_err(&devfreq->dev,
1195 "failed to suspend devfreq device\n");
1197 mutex_unlock(&devfreq_list_lock);
1201 * devfreq_resume() - Resume devfreq governors and devices
1203 * Called during system wide Suspend/Hibernate cycle for resuming governors and
1204 * devices that are suspended with devfreq_suspend().
1206 void devfreq_resume(void)
1208 struct devfreq *devfreq;
1209 int ret;
1211 mutex_lock(&devfreq_list_lock);
1212 list_for_each_entry(devfreq, &devfreq_list, node) {
1213 ret = devfreq_resume_device(devfreq);
1214 if (ret)
1215 dev_warn(&devfreq->dev,
1216 "failed to resume devfreq device\n");
1218 mutex_unlock(&devfreq_list_lock);
1222 * devfreq_add_governor() - Add devfreq governor
1223 * @governor: the devfreq governor to be added
1225 int devfreq_add_governor(struct devfreq_governor *governor)
1227 struct devfreq_governor *g;
1228 struct devfreq *devfreq;
1229 int err = 0;
1231 if (!governor) {
1232 pr_err("%s: Invalid parameters.\n", __func__);
1233 return -EINVAL;
1236 mutex_lock(&devfreq_list_lock);
1237 g = find_devfreq_governor(governor->name);
1238 if (!IS_ERR(g)) {
1239 pr_err("%s: governor %s already registered\n", __func__,
1240 g->name);
1241 err = -EINVAL;
1242 goto err_out;
1245 list_add(&governor->node, &devfreq_governor_list);
1247 list_for_each_entry(devfreq, &devfreq_list, node) {
1248 int ret = 0;
1249 struct device *dev = devfreq->dev.parent;
1251 if (!strncmp(devfreq->governor->name, governor->name,
1252 DEVFREQ_NAME_LEN)) {
1253 /* The following should never occur */
1254 if (devfreq->governor) {
1255 dev_warn(dev,
1256 "%s: Governor %s already present\n",
1257 __func__, devfreq->governor->name);
1258 ret = devfreq->governor->event_handler(devfreq,
1259 DEVFREQ_GOV_STOP, NULL);
1260 if (ret) {
1261 dev_warn(dev,
1262 "%s: Governor %s stop = %d\n",
1263 __func__,
1264 devfreq->governor->name, ret);
1266 /* Fall through */
1268 devfreq->governor = governor;
1269 ret = devfreq->governor->event_handler(devfreq,
1270 DEVFREQ_GOV_START, NULL);
1271 if (ret) {
1272 dev_warn(dev, "%s: Governor %s start=%d\n",
1273 __func__, devfreq->governor->name,
1274 ret);
1279 err_out:
1280 mutex_unlock(&devfreq_list_lock);
1282 return err;
1284 EXPORT_SYMBOL(devfreq_add_governor);
1287 * devfreq_remove_governor() - Remove devfreq feature from a device.
1288 * @governor: the devfreq governor to be removed
1290 int devfreq_remove_governor(struct devfreq_governor *governor)
1292 struct devfreq_governor *g;
1293 struct devfreq *devfreq;
1294 int err = 0;
1296 if (!governor) {
1297 pr_err("%s: Invalid parameters.\n", __func__);
1298 return -EINVAL;
1301 mutex_lock(&devfreq_list_lock);
1302 g = find_devfreq_governor(governor->name);
1303 if (IS_ERR(g)) {
1304 pr_err("%s: governor %s not registered\n", __func__,
1305 governor->name);
1306 err = PTR_ERR(g);
1307 goto err_out;
1309 list_for_each_entry(devfreq, &devfreq_list, node) {
1310 int ret;
1311 struct device *dev = devfreq->dev.parent;
1313 if (!strncmp(devfreq->governor->name, governor->name,
1314 DEVFREQ_NAME_LEN)) {
1315 /* we should have a devfreq governor! */
1316 if (!devfreq->governor) {
1317 dev_warn(dev, "%s: Governor %s NOT present\n",
1318 __func__, governor->name);
1319 continue;
1320 /* Fall through */
1322 ret = devfreq->governor->event_handler(devfreq,
1323 DEVFREQ_GOV_STOP, NULL);
1324 if (ret) {
1325 dev_warn(dev, "%s: Governor %s stop=%d\n",
1326 __func__, devfreq->governor->name,
1327 ret);
1329 devfreq->governor = NULL;
1333 list_del(&governor->node);
1334 err_out:
1335 mutex_unlock(&devfreq_list_lock);
1337 return err;
1339 EXPORT_SYMBOL(devfreq_remove_governor);
1341 static ssize_t name_show(struct device *dev,
1342 struct device_attribute *attr, char *buf)
1344 struct devfreq *df = to_devfreq(dev);
1345 return sprintf(buf, "%s\n", dev_name(df->dev.parent));
1347 static DEVICE_ATTR_RO(name);
1349 static ssize_t governor_show(struct device *dev,
1350 struct device_attribute *attr, char *buf)
1352 struct devfreq *df = to_devfreq(dev);
1354 if (!df->governor)
1355 return -EINVAL;
1357 return sprintf(buf, "%s\n", df->governor->name);
1360 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1361 const char *buf, size_t count)
1363 struct devfreq *df = to_devfreq(dev);
1364 int ret;
1365 char str_governor[DEVFREQ_NAME_LEN + 1];
1366 const struct devfreq_governor *governor, *prev_governor;
1368 if (!df->governor)
1369 return -EINVAL;
1371 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1372 if (ret != 1)
1373 return -EINVAL;
1375 mutex_lock(&devfreq_list_lock);
1376 governor = try_then_request_governor(str_governor);
1377 if (IS_ERR(governor)) {
1378 ret = PTR_ERR(governor);
1379 goto out;
1381 if (df->governor == governor) {
1382 ret = 0;
1383 goto out;
1384 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)
1385 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) {
1386 ret = -EINVAL;
1387 goto out;
1391 * Stop the current governor and remove the specific sysfs files
1392 * which depend on current governor.
1394 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1395 if (ret) {
1396 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1397 __func__, df->governor->name, ret);
1398 goto out;
1400 remove_sysfs_files(df, df->governor);
1403 * Start the new governor and create the specific sysfs files
1404 * which depend on the new governor.
1406 prev_governor = df->governor;
1407 df->governor = governor;
1408 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1409 if (ret) {
1410 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1411 __func__, df->governor->name, ret);
1413 /* Restore previous governor */
1414 df->governor = prev_governor;
1415 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1416 if (ret) {
1417 dev_err(dev,
1418 "%s: reverting to Governor %s failed (%d)\n",
1419 __func__, prev_governor->name, ret);
1420 df->governor = NULL;
1421 goto out;
1426 * Create the sysfs files for the new governor. But if failed to start
1427 * the new governor, restore the sysfs files of previous governor.
1429 create_sysfs_files(df, df->governor);
1431 out:
1432 mutex_unlock(&devfreq_list_lock);
1434 if (!ret)
1435 ret = count;
1436 return ret;
1438 static DEVICE_ATTR_RW(governor);
1440 static ssize_t available_governors_show(struct device *d,
1441 struct device_attribute *attr,
1442 char *buf)
1444 struct devfreq *df = to_devfreq(d);
1445 ssize_t count = 0;
1447 if (!df->governor)
1448 return -EINVAL;
1450 mutex_lock(&devfreq_list_lock);
1453 * The devfreq with immutable governor (e.g., passive) shows
1454 * only own governor.
1456 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) {
1457 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1458 "%s ", df->governor->name);
1460 * The devfreq device shows the registered governor except for
1461 * immutable governors such as passive governor .
1463 } else {
1464 struct devfreq_governor *governor;
1466 list_for_each_entry(governor, &devfreq_governor_list, node) {
1467 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE))
1468 continue;
1469 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1470 "%s ", governor->name);
1474 mutex_unlock(&devfreq_list_lock);
1476 /* Truncate the trailing space */
1477 if (count)
1478 count--;
1480 count += sprintf(&buf[count], "\n");
1482 return count;
1484 static DEVICE_ATTR_RO(available_governors);
1486 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1487 char *buf)
1489 unsigned long freq;
1490 struct devfreq *df = to_devfreq(dev);
1492 if (!df->profile)
1493 return -EINVAL;
1495 if (df->profile->get_cur_freq &&
1496 !df->profile->get_cur_freq(df->dev.parent, &freq))
1497 return sprintf(buf, "%lu\n", freq);
1499 return sprintf(buf, "%lu\n", df->previous_freq);
1501 static DEVICE_ATTR_RO(cur_freq);
1503 static ssize_t target_freq_show(struct device *dev,
1504 struct device_attribute *attr, char *buf)
1506 struct devfreq *df = to_devfreq(dev);
1508 return sprintf(buf, "%lu\n", df->previous_freq);
1510 static DEVICE_ATTR_RO(target_freq);
1512 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1513 const char *buf, size_t count)
1515 struct devfreq *df = to_devfreq(dev);
1516 unsigned long value;
1517 int ret;
1520 * Protect against theoretical sysfs writes between
1521 * device_add and dev_pm_qos_add_request
1523 if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1524 return -EAGAIN;
1526 ret = sscanf(buf, "%lu", &value);
1527 if (ret != 1)
1528 return -EINVAL;
1530 /* Round down to kHz for PM QoS */
1531 ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1532 value / HZ_PER_KHZ);
1533 if (ret < 0)
1534 return ret;
1536 return count;
1539 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1540 char *buf)
1542 struct devfreq *df = to_devfreq(dev);
1543 unsigned long min_freq, max_freq;
1545 mutex_lock(&df->lock);
1546 get_freq_range(df, &min_freq, &max_freq);
1547 mutex_unlock(&df->lock);
1549 return sprintf(buf, "%lu\n", min_freq);
1551 static DEVICE_ATTR_RW(min_freq);
1553 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1554 const char *buf, size_t count)
1556 struct devfreq *df = to_devfreq(dev);
1557 unsigned long value;
1558 int ret;
1561 * Protect against theoretical sysfs writes between
1562 * device_add and dev_pm_qos_add_request
1564 if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1565 return -EINVAL;
1567 ret = sscanf(buf, "%lu", &value);
1568 if (ret != 1)
1569 return -EINVAL;
1572 * PM QoS frequencies are in kHz so we need to convert. Convert by
1573 * rounding upwards so that the acceptable interval never shrinks.
1575 * For example if the user writes "666666666" to sysfs this value will
1576 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1577 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1579 * A value of zero means "no limit".
1581 if (value)
1582 value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1583 else
1584 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1586 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1587 if (ret < 0)
1588 return ret;
1590 return count;
1593 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1594 char *buf)
1596 struct devfreq *df = to_devfreq(dev);
1597 unsigned long min_freq, max_freq;
1599 mutex_lock(&df->lock);
1600 get_freq_range(df, &min_freq, &max_freq);
1601 mutex_unlock(&df->lock);
1603 return sprintf(buf, "%lu\n", max_freq);
1605 static DEVICE_ATTR_RW(max_freq);
1607 static ssize_t available_frequencies_show(struct device *d,
1608 struct device_attribute *attr,
1609 char *buf)
1611 struct devfreq *df = to_devfreq(d);
1612 ssize_t count = 0;
1613 int i;
1615 if (!df->profile)
1616 return -EINVAL;
1618 mutex_lock(&df->lock);
1620 for (i = 0; i < df->profile->max_state; i++)
1621 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1622 "%lu ", df->profile->freq_table[i]);
1624 mutex_unlock(&df->lock);
1625 /* Truncate the trailing space */
1626 if (count)
1627 count--;
1629 count += sprintf(&buf[count], "\n");
1631 return count;
1633 static DEVICE_ATTR_RO(available_frequencies);
1635 static ssize_t trans_stat_show(struct device *dev,
1636 struct device_attribute *attr, char *buf)
1638 struct devfreq *df = to_devfreq(dev);
1639 ssize_t len;
1640 int i, j;
1641 unsigned int max_state;
1643 if (!df->profile)
1644 return -EINVAL;
1645 max_state = df->profile->max_state;
1647 if (max_state == 0)
1648 return sprintf(buf, "Not Supported.\n");
1650 mutex_lock(&df->lock);
1651 if (!df->stop_polling &&
1652 devfreq_update_status(df, df->previous_freq)) {
1653 mutex_unlock(&df->lock);
1654 return 0;
1656 mutex_unlock(&df->lock);
1658 len = sprintf(buf, " From : To\n");
1659 len += sprintf(buf + len, " :");
1660 for (i = 0; i < max_state; i++)
1661 len += sprintf(buf + len, "%10lu",
1662 df->profile->freq_table[i]);
1664 len += sprintf(buf + len, " time(ms)\n");
1666 for (i = 0; i < max_state; i++) {
1667 if (df->profile->freq_table[i]
1668 == df->previous_freq) {
1669 len += sprintf(buf + len, "*");
1670 } else {
1671 len += sprintf(buf + len, " ");
1673 len += sprintf(buf + len, "%10lu:",
1674 df->profile->freq_table[i]);
1675 for (j = 0; j < max_state; j++)
1676 len += sprintf(buf + len, "%10u",
1677 df->stats.trans_table[(i * max_state) + j]);
1679 len += sprintf(buf + len, "%10llu\n", (u64)
1680 jiffies64_to_msecs(df->stats.time_in_state[i]));
1683 len += sprintf(buf + len, "Total transition : %u\n",
1684 df->stats.total_trans);
1685 return len;
1688 static ssize_t trans_stat_store(struct device *dev,
1689 struct device_attribute *attr,
1690 const char *buf, size_t count)
1692 struct devfreq *df = to_devfreq(dev);
1693 int err, value;
1695 if (!df->profile)
1696 return -EINVAL;
1698 if (df->profile->max_state == 0)
1699 return count;
1701 err = kstrtoint(buf, 10, &value);
1702 if (err || value != 0)
1703 return -EINVAL;
1705 mutex_lock(&df->lock);
1706 memset(df->stats.time_in_state, 0, (df->profile->max_state *
1707 sizeof(*df->stats.time_in_state)));
1708 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1709 df->profile->max_state,
1710 df->profile->max_state));
1711 df->stats.total_trans = 0;
1712 df->stats.last_update = get_jiffies_64();
1713 mutex_unlock(&df->lock);
1715 return count;
1717 static DEVICE_ATTR_RW(trans_stat);
1719 static struct attribute *devfreq_attrs[] = {
1720 &dev_attr_name.attr,
1721 &dev_attr_governor.attr,
1722 &dev_attr_available_governors.attr,
1723 &dev_attr_cur_freq.attr,
1724 &dev_attr_available_frequencies.attr,
1725 &dev_attr_target_freq.attr,
1726 &dev_attr_min_freq.attr,
1727 &dev_attr_max_freq.attr,
1728 &dev_attr_trans_stat.attr,
1729 NULL,
1731 ATTRIBUTE_GROUPS(devfreq);
1733 static ssize_t polling_interval_show(struct device *dev,
1734 struct device_attribute *attr, char *buf)
1736 struct devfreq *df = to_devfreq(dev);
1738 if (!df->profile)
1739 return -EINVAL;
1741 return sprintf(buf, "%d\n", df->profile->polling_ms);
1744 static ssize_t polling_interval_store(struct device *dev,
1745 struct device_attribute *attr,
1746 const char *buf, size_t count)
1748 struct devfreq *df = to_devfreq(dev);
1749 unsigned int value;
1750 int ret;
1752 if (!df->governor)
1753 return -EINVAL;
1755 ret = sscanf(buf, "%u", &value);
1756 if (ret != 1)
1757 return -EINVAL;
1759 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1760 ret = count;
1762 return ret;
1764 static DEVICE_ATTR_RW(polling_interval);
1766 static ssize_t timer_show(struct device *dev,
1767 struct device_attribute *attr, char *buf)
1769 struct devfreq *df = to_devfreq(dev);
1771 if (!df->profile)
1772 return -EINVAL;
1774 return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
1777 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
1778 const char *buf, size_t count)
1780 struct devfreq *df = to_devfreq(dev);
1781 char str_timer[DEVFREQ_NAME_LEN + 1];
1782 int timer = -1;
1783 int ret = 0, i;
1785 if (!df->governor || !df->profile)
1786 return -EINVAL;
1788 ret = sscanf(buf, "%16s", str_timer);
1789 if (ret != 1)
1790 return -EINVAL;
1792 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
1793 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
1794 timer = i;
1795 break;
1799 if (timer < 0) {
1800 ret = -EINVAL;
1801 goto out;
1804 if (df->profile->timer == timer) {
1805 ret = 0;
1806 goto out;
1809 mutex_lock(&df->lock);
1810 df->profile->timer = timer;
1811 mutex_unlock(&df->lock);
1813 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1814 if (ret) {
1815 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1816 __func__, df->governor->name, ret);
1817 goto out;
1820 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1821 if (ret)
1822 dev_warn(dev, "%s: Governor %s not started(%d)\n",
1823 __func__, df->governor->name, ret);
1824 out:
1825 return ret ? ret : count;
1827 static DEVICE_ATTR_RW(timer);
1829 #define CREATE_SYSFS_FILE(df, name) \
1831 int ret; \
1832 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \
1833 if (ret < 0) { \
1834 dev_warn(&df->dev, \
1835 "Unable to create attr(%s)\n", "##name"); \
1839 /* Create the specific sysfs files which depend on each governor. */
1840 static void create_sysfs_files(struct devfreq *devfreq,
1841 const struct devfreq_governor *gov)
1843 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1844 CREATE_SYSFS_FILE(devfreq, polling_interval);
1845 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1846 CREATE_SYSFS_FILE(devfreq, timer);
1849 /* Remove the specific sysfs files which depend on each governor. */
1850 static void remove_sysfs_files(struct devfreq *devfreq,
1851 const struct devfreq_governor *gov)
1853 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
1854 sysfs_remove_file(&devfreq->dev.kobj,
1855 &dev_attr_polling_interval.attr);
1856 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
1857 sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
1861 * devfreq_summary_show() - Show the summary of the devfreq devices
1862 * @s: seq_file instance to show the summary of devfreq devices
1863 * @data: not used
1865 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1866 * It helps that user can know the detailed information of the devfreq devices.
1868 * Return 0 always because it shows the information without any data change.
1870 static int devfreq_summary_show(struct seq_file *s, void *data)
1872 struct devfreq *devfreq;
1873 struct devfreq *p_devfreq = NULL;
1874 unsigned long cur_freq, min_freq, max_freq;
1875 unsigned int polling_ms;
1876 unsigned int timer;
1878 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1879 "dev",
1880 "parent_dev",
1881 "governor",
1882 "timer",
1883 "polling_ms",
1884 "cur_freq_Hz",
1885 "min_freq_Hz",
1886 "max_freq_Hz");
1887 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1888 "------------------------------",
1889 "------------------------------",
1890 "---------------",
1891 "----------",
1892 "----------",
1893 "------------",
1894 "------------",
1895 "------------");
1897 mutex_lock(&devfreq_list_lock);
1899 list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1900 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1901 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE,
1902 DEVFREQ_NAME_LEN)) {
1903 struct devfreq_passive_data *data = devfreq->data;
1905 if (data)
1906 p_devfreq = data->parent;
1907 } else {
1908 p_devfreq = NULL;
1910 #endif
1912 mutex_lock(&devfreq->lock);
1913 cur_freq = devfreq->previous_freq;
1914 get_freq_range(devfreq, &min_freq, &max_freq);
1915 timer = devfreq->profile->timer;
1917 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL))
1918 polling_ms = devfreq->profile->polling_ms;
1919 else
1920 polling_ms = 0;
1921 mutex_unlock(&devfreq->lock);
1923 seq_printf(s,
1924 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n",
1925 dev_name(&devfreq->dev),
1926 p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1927 devfreq->governor->name,
1928 polling_ms ? timer_name[timer] : "null",
1929 polling_ms,
1930 cur_freq,
1931 min_freq,
1932 max_freq);
1935 mutex_unlock(&devfreq_list_lock);
1937 return 0;
1939 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1941 static int __init devfreq_init(void)
1943 devfreq_class = class_create(THIS_MODULE, "devfreq");
1944 if (IS_ERR(devfreq_class)) {
1945 pr_err("%s: couldn't create class\n", __FILE__);
1946 return PTR_ERR(devfreq_class);
1949 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1950 if (!devfreq_wq) {
1951 class_destroy(devfreq_class);
1952 pr_err("%s: couldn't create workqueue\n", __FILE__);
1953 return -ENOMEM;
1955 devfreq_class->dev_groups = devfreq_groups;
1957 devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
1958 debugfs_create_file("devfreq_summary", 0444,
1959 devfreq_debugfs, NULL,
1960 &devfreq_summary_fops);
1962 return 0;
1964 subsys_initcall(devfreq_init);
1967 * The following are helper functions for devfreq user device drivers with
1968 * OPP framework.
1972 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1973 * freq value given to target callback.
1974 * @dev: The devfreq user device. (parent of devfreq)
1975 * @freq: The frequency given to target function
1976 * @flags: Flags handed from devfreq framework.
1978 * The callers are required to call dev_pm_opp_put() for the returned OPP after
1979 * use.
1981 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1982 unsigned long *freq,
1983 u32 flags)
1985 struct dev_pm_opp *opp;
1987 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1988 /* The freq is an upper bound. opp should be lower */
1989 opp = dev_pm_opp_find_freq_floor(dev, freq);
1991 /* If not available, use the closest opp */
1992 if (opp == ERR_PTR(-ERANGE))
1993 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1994 } else {
1995 /* The freq is an lower bound. opp should be higher */
1996 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1998 /* If not available, use the closest opp */
1999 if (opp == ERR_PTR(-ERANGE))
2000 opp = dev_pm_opp_find_freq_floor(dev, freq);
2003 return opp;
2005 EXPORT_SYMBOL(devfreq_recommended_opp);
2008 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
2009 * for any changes in the OPP availability
2010 * changes
2011 * @dev: The devfreq user device. (parent of devfreq)
2012 * @devfreq: The devfreq object.
2014 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
2016 return dev_pm_opp_register_notifier(dev, &devfreq->nb);
2018 EXPORT_SYMBOL(devfreq_register_opp_notifier);
2021 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
2022 * notified for any changes in the OPP
2023 * availability changes anymore.
2024 * @dev: The devfreq user device. (parent of devfreq)
2025 * @devfreq: The devfreq object.
2027 * At exit() callback of devfreq_dev_profile, this must be included if
2028 * devfreq_recommended_opp is used.
2030 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
2032 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
2034 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
2036 static void devm_devfreq_opp_release(struct device *dev, void *res)
2038 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
2042 * devm_devfreq_register_opp_notifier() - Resource-managed
2043 * devfreq_register_opp_notifier()
2044 * @dev: The devfreq user device. (parent of devfreq)
2045 * @devfreq: The devfreq object.
2047 int devm_devfreq_register_opp_notifier(struct device *dev,
2048 struct devfreq *devfreq)
2050 struct devfreq **ptr;
2051 int ret;
2053 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
2054 if (!ptr)
2055 return -ENOMEM;
2057 ret = devfreq_register_opp_notifier(dev, devfreq);
2058 if (ret) {
2059 devres_free(ptr);
2060 return ret;
2063 *ptr = devfreq;
2064 devres_add(dev, ptr);
2066 return 0;
2068 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
2071 * devm_devfreq_unregister_opp_notifier() - Resource-managed
2072 * devfreq_unregister_opp_notifier()
2073 * @dev: The devfreq user device. (parent of devfreq)
2074 * @devfreq: The devfreq object.
2076 void devm_devfreq_unregister_opp_notifier(struct device *dev,
2077 struct devfreq *devfreq)
2079 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
2080 devm_devfreq_dev_match, devfreq));
2082 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
2085 * devfreq_register_notifier() - Register a driver with devfreq
2086 * @devfreq: The devfreq object.
2087 * @nb: The notifier block to register.
2088 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2090 int devfreq_register_notifier(struct devfreq *devfreq,
2091 struct notifier_block *nb,
2092 unsigned int list)
2094 int ret = 0;
2096 if (!devfreq)
2097 return -EINVAL;
2099 switch (list) {
2100 case DEVFREQ_TRANSITION_NOTIFIER:
2101 ret = srcu_notifier_chain_register(
2102 &devfreq->transition_notifier_list, nb);
2103 break;
2104 default:
2105 ret = -EINVAL;
2108 return ret;
2110 EXPORT_SYMBOL(devfreq_register_notifier);
2113 * devfreq_unregister_notifier() - Unregister a driver with devfreq
2114 * @devfreq: The devfreq object.
2115 * @nb: The notifier block to be unregistered.
2116 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2118 int devfreq_unregister_notifier(struct devfreq *devfreq,
2119 struct notifier_block *nb,
2120 unsigned int list)
2122 int ret = 0;
2124 if (!devfreq)
2125 return -EINVAL;
2127 switch (list) {
2128 case DEVFREQ_TRANSITION_NOTIFIER:
2129 ret = srcu_notifier_chain_unregister(
2130 &devfreq->transition_notifier_list, nb);
2131 break;
2132 default:
2133 ret = -EINVAL;
2136 return ret;
2138 EXPORT_SYMBOL(devfreq_unregister_notifier);
2140 struct devfreq_notifier_devres {
2141 struct devfreq *devfreq;
2142 struct notifier_block *nb;
2143 unsigned int list;
2146 static void devm_devfreq_notifier_release(struct device *dev, void *res)
2148 struct devfreq_notifier_devres *this = res;
2150 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
2154 * devm_devfreq_register_notifier()
2155 * - Resource-managed devfreq_register_notifier()
2156 * @dev: The devfreq user device. (parent of devfreq)
2157 * @devfreq: The devfreq object.
2158 * @nb: The notifier block to be unregistered.
2159 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2161 int devm_devfreq_register_notifier(struct device *dev,
2162 struct devfreq *devfreq,
2163 struct notifier_block *nb,
2164 unsigned int list)
2166 struct devfreq_notifier_devres *ptr;
2167 int ret;
2169 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
2170 GFP_KERNEL);
2171 if (!ptr)
2172 return -ENOMEM;
2174 ret = devfreq_register_notifier(devfreq, nb, list);
2175 if (ret) {
2176 devres_free(ptr);
2177 return ret;
2180 ptr->devfreq = devfreq;
2181 ptr->nb = nb;
2182 ptr->list = list;
2183 devres_add(dev, ptr);
2185 return 0;
2187 EXPORT_SYMBOL(devm_devfreq_register_notifier);
2190 * devm_devfreq_unregister_notifier()
2191 * - Resource-managed devfreq_unregister_notifier()
2192 * @dev: The devfreq user device. (parent of devfreq)
2193 * @devfreq: The devfreq object.
2194 * @nb: The notifier block to be unregistered.
2195 * @list: DEVFREQ_TRANSITION_NOTIFIER.
2197 void devm_devfreq_unregister_notifier(struct device *dev,
2198 struct devfreq *devfreq,
2199 struct notifier_block *nb,
2200 unsigned int list)
2202 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
2203 devm_devfreq_dev_match, devfreq));
2205 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);