1 // SPDX-License-Identifier: GPL-2.0-only
3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
6 * Copyright (C) 2011 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
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>
28 #include <linux/pm_qos.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" },
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
)
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
);
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
);
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];
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
)
170 for (lev
= 0; lev
< devfreq
->profile
->max_state
; lev
++)
171 if (freq
== devfreq
->profile
->freq_table
[lev
])
177 static int set_freq_table(struct devfreq
*devfreq
)
179 struct devfreq_dev_profile
*profile
= devfreq
->profile
;
180 struct dev_pm_opp
*opp
;
184 /* Initialize the freq_table from OPP table */
185 count
= dev_pm_opp_get_opp_count(devfreq
->dev
.parent
);
189 profile
->max_state
= count
;
190 profile
->freq_table
= devm_kcalloc(devfreq
->dev
.parent
,
192 sizeof(*profile
->freq_table
),
194 if (!profile
->freq_table
) {
195 profile
->max_state
= 0;
199 for (i
= 0, freq
= 0; i
< profile
->max_state
; i
++, freq
++) {
200 opp
= dev_pm_opp_find_freq_ceil(devfreq
->dev
.parent
, &freq
);
202 devm_kfree(devfreq
->dev
.parent
, profile
->freq_table
);
203 profile
->max_state
= 0;
207 profile
->freq_table
[i
] = freq
;
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;
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
)
230 prev_lev
= devfreq_get_freq_level(devfreq
, devfreq
->previous_freq
);
236 devfreq
->stats
.time_in_state
[prev_lev
] +=
237 cur_time
- devfreq
->stats
.last_update
;
239 lev
= devfreq_get_freq_level(devfreq
, freq
);
245 if (lev
!= prev_lev
) {
246 devfreq
->stats
.trans_table
[
247 (prev_lev
* devfreq
->profile
->max_state
) + lev
]++;
248 devfreq
->stats
.total_trans
++;
252 devfreq
->stats
.last_update
= cur_time
;
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
))
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
;
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
,
312 err
= request_module("governor_%s", "simpleondemand");
314 err
= request_module("governor_%s", name
);
315 /* Restore previous state before return */
316 mutex_lock(&devfreq_list_lock
);
318 return (err
< 0) ? ERR_PTR(err
) : ERR_PTR(-EINVAL
);
320 governor
= find_devfreq_governor(name
);
326 static int devfreq_notify_transition(struct devfreq
*devfreq
,
327 struct devfreq_freqs
*freqs
, unsigned int state
)
333 case DEVFREQ_PRECHANGE
:
334 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
335 DEVFREQ_PRECHANGE
, freqs
);
338 case DEVFREQ_POSTCHANGE
:
339 srcu_notifier_call_chain(&devfreq
->transition_notifier_list
,
340 DEVFREQ_POSTCHANGE
, freqs
);
349 static int devfreq_set_target(struct devfreq
*devfreq
, unsigned long new_freq
,
352 struct devfreq_freqs freqs
;
353 unsigned long cur_freq
;
356 if (devfreq
->profile
->get_cur_freq
)
357 devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &cur_freq
);
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
);
367 freqs
.new = cur_freq
;
368 devfreq_notify_transition(devfreq
, &freqs
, DEVFREQ_POSTCHANGE
);
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
;
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
;
411 lockdep_assert_held(&devfreq
->lock
);
413 if (!devfreq
->governor
)
416 /* Reevaluate the proper frequency */
417 err
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
420 get_freq_range(devfreq
, &min_freq
, &max_freq
);
422 if (freq
< min_freq
) {
424 flags
&= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use GLB */
426 if (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
)
458 struct devfreq
*devfreq
= container_of(work
,
459 struct devfreq
, work
.work
);
461 mutex_lock(&devfreq
->lock
);
462 err
= update_devfreq(devfreq
);
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
))
487 switch (devfreq
->profile
->timer
) {
488 case DEVFREQ_TIMER_DEFERRABLE
:
489 INIT_DEFERRABLE_WORK(&devfreq
->work
, devfreq_monitor
);
491 case DEVFREQ_TIMER_DELAYED
:
492 INIT_DELAYED_WORK(&devfreq
->work
, devfreq_monitor
);
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
))
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
);
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
))
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
)
564 mutex_lock(&devfreq
->lock
);
566 if (IS_SUPPORTED_FLAG(devfreq
->governor
->flags
, IRQ_DRIVEN
))
569 if (!devfreq
->stop_polling
)
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
));
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
;
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
))
609 if (devfreq
->stop_polling
)
612 /* if new delay is zero, stop polling */
614 mutex_unlock(&devfreq
->lock
);
615 cancel_delayed_work_sync(&devfreq
->work
);
619 /* if current delay is zero, start polling with new delay */
621 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
622 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
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
));
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)
647 * Called by a notifier that uses devfreq->nb.
649 static int devfreq_notifier_call(struct notifier_block
*nb
, unsigned long type
,
652 struct devfreq
*devfreq
= container_of(nb
, struct devfreq
, nb
);
655 mutex_lock(&devfreq
->lock
);
657 devfreq
->scaling_min_freq
= find_available_min_freq(devfreq
);
658 if (!devfreq
->scaling_min_freq
)
661 devfreq
->scaling_max_freq
= find_available_max_freq(devfreq
);
662 if (!devfreq
->scaling_max_freq
) {
663 devfreq
->scaling_max_freq
= ULONG_MAX
;
667 err
= update_devfreq(devfreq
);
670 mutex_unlock(&devfreq
->lock
);
672 dev_err(devfreq
->dev
.parent
,
673 "failed to update frequency from OPP notifier (%d)\n",
680 * qos_notifier_call() - Common handler for QoS constraints.
681 * @devfreq: the devfreq instance.
683 static int qos_notifier_call(struct devfreq
*devfreq
)
687 mutex_lock(&devfreq
->lock
);
688 err
= update_devfreq(devfreq
);
689 mutex_unlock(&devfreq
->lock
);
691 dev_err(devfreq
->dev
.parent
,
692 "failed to update frequency from PM QoS (%d)\n",
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
);
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
);
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
);
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
);
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
777 struct devfreq
*devfreq_add_device(struct device
*dev
,
778 struct devfreq_dev_profile
*profile
,
779 const char *governor_name
,
782 struct devfreq
*devfreq
;
783 struct devfreq_governor
*governor
;
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",
801 devfreq
= kzalloc(sizeof(struct devfreq
), GFP_KERNEL
);
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
) {
824 if (!devfreq
->profile
->max_state
&& !devfreq
->profile
->freq_table
) {
825 mutex_unlock(&devfreq
->lock
);
826 err
= set_freq_table(devfreq
);
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
);
839 devfreq
->scaling_max_freq
= find_available_max_freq(devfreq
);
840 if (!devfreq
->scaling_max_freq
) {
841 mutex_unlock(&devfreq
->lock
);
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
);
852 mutex_unlock(&devfreq
->lock
);
853 put_device(&devfreq
->dev
);
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
),
862 if (!devfreq
->stats
.trans_table
) {
863 mutex_unlock(&devfreq
->lock
);
868 devfreq
->stats
.time_in_state
= devm_kcalloc(&devfreq
->dev
,
869 devfreq
->profile
->max_state
,
870 sizeof(*devfreq
->stats
.time_in_state
),
872 if (!devfreq
->stats
.time_in_state
) {
873 mutex_unlock(&devfreq
->lock
);
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);
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
);
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
);
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
);
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",
913 err
= PTR_ERR(governor
);
917 devfreq
->governor
= governor
;
918 err
= devfreq
->governor
->event_handler(devfreq
, DEVFREQ_GOV_START
,
921 dev_err(dev
, "%s: Unable to start governor for the device\n",
925 create_sysfs_files(devfreq
, devfreq
->governor
);
927 list_add(&devfreq
->node
, &devfreq_list
);
929 mutex_unlock(&devfreq_list_lock
);
934 mutex_unlock(&devfreq_list_lock
);
936 devfreq_remove_device(devfreq
);
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
)
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
);
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
))
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
991 * This function manages automatically the memory of devfreq device using device
992 * resource management and simplify the free operation for memory of devfreq
995 struct devfreq
*devm_devfreq_add_device(struct device
*dev
,
996 struct devfreq_dev_profile
*profile
,
997 const char *governor_name
,
1000 struct devfreq
**ptr
, *devfreq
;
1002 ptr
= devres_alloc(devm_devfreq_dev_release
, sizeof(*ptr
), GFP_KERNEL
);
1004 return ERR_PTR(-ENOMEM
);
1006 devfreq
= devfreq_add_device(dev
, profile
, governor_name
, data
);
1007 if (IS_ERR(devfreq
)) {
1013 devres_add(dev
, ptr
);
1017 EXPORT_SYMBOL(devm_devfreq_add_device
);
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
;
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
);
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
);
1064 return ERR_PTR(-EINVAL
);
1066 node
= of_parse_phandle(dev
->of_node
, phandle_name
, index
);
1068 return ERR_PTR(-ENODEV
);
1070 devfreq
= devfreq_get_devfreq_by_node(node
);
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
)
1118 if (atomic_inc_return(&devfreq
->suspend_count
) > 1)
1121 if (devfreq
->governor
) {
1122 ret
= devfreq
->governor
->event_handler(devfreq
,
1123 DEVFREQ_GOV_SUSPEND
, NULL
);
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
);
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
)
1155 if (atomic_dec_return(&devfreq
->suspend_count
) >= 1)
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
);
1166 if (devfreq
->governor
) {
1167 ret
= devfreq
->governor
->event_handler(devfreq
,
1168 DEVFREQ_GOV_RESUME
, NULL
);
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
;
1190 mutex_lock(&devfreq_list_lock
);
1191 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
1192 ret
= devfreq_suspend_device(devfreq
);
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
;
1211 mutex_lock(&devfreq_list_lock
);
1212 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
1213 ret
= devfreq_resume_device(devfreq
);
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
;
1232 pr_err("%s: Invalid parameters.\n", __func__
);
1236 mutex_lock(&devfreq_list_lock
);
1237 g
= find_devfreq_governor(governor
->name
);
1239 pr_err("%s: governor %s already registered\n", __func__
,
1245 list_add(&governor
->node
, &devfreq_governor_list
);
1247 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
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
) {
1256 "%s: Governor %s already present\n",
1257 __func__
, devfreq
->governor
->name
);
1258 ret
= devfreq
->governor
->event_handler(devfreq
,
1259 DEVFREQ_GOV_STOP
, NULL
);
1262 "%s: Governor %s stop = %d\n",
1264 devfreq
->governor
->name
, ret
);
1268 devfreq
->governor
= governor
;
1269 ret
= devfreq
->governor
->event_handler(devfreq
,
1270 DEVFREQ_GOV_START
, NULL
);
1272 dev_warn(dev
, "%s: Governor %s start=%d\n",
1273 __func__
, devfreq
->governor
->name
,
1280 mutex_unlock(&devfreq_list_lock
);
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
;
1297 pr_err("%s: Invalid parameters.\n", __func__
);
1301 mutex_lock(&devfreq_list_lock
);
1302 g
= find_devfreq_governor(governor
->name
);
1304 pr_err("%s: governor %s not registered\n", __func__
,
1309 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
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
);
1322 ret
= devfreq
->governor
->event_handler(devfreq
,
1323 DEVFREQ_GOV_STOP
, NULL
);
1325 dev_warn(dev
, "%s: Governor %s stop=%d\n",
1326 __func__
, devfreq
->governor
->name
,
1329 devfreq
->governor
= NULL
;
1333 list_del(&governor
->node
);
1335 mutex_unlock(&devfreq_list_lock
);
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
);
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
);
1365 char str_governor
[DEVFREQ_NAME_LEN
+ 1];
1366 const struct devfreq_governor
*governor
, *prev_governor
;
1371 ret
= sscanf(buf
, "%" __stringify(DEVFREQ_NAME_LEN
) "s", str_governor
);
1375 mutex_lock(&devfreq_list_lock
);
1376 governor
= try_then_request_governor(str_governor
);
1377 if (IS_ERR(governor
)) {
1378 ret
= PTR_ERR(governor
);
1381 if (df
->governor
== governor
) {
1384 } else if (IS_SUPPORTED_FLAG(df
->governor
->flags
, IMMUTABLE
)
1385 || IS_SUPPORTED_FLAG(governor
->flags
, IMMUTABLE
)) {
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
);
1396 dev_warn(dev
, "%s: Governor %s not stopped(%d)\n",
1397 __func__
, df
->governor
->name
, ret
);
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
);
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
);
1418 "%s: reverting to Governor %s failed (%d)\n",
1419 __func__
, prev_governor
->name
, ret
);
1420 df
->governor
= NULL
;
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
);
1432 mutex_unlock(&devfreq_list_lock
);
1438 static DEVICE_ATTR_RW(governor
);
1440 static ssize_t
available_governors_show(struct device
*d
,
1441 struct device_attribute
*attr
,
1444 struct devfreq
*df
= to_devfreq(d
);
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 .
1464 struct devfreq_governor
*governor
;
1466 list_for_each_entry(governor
, &devfreq_governor_list
, node
) {
1467 if (IS_SUPPORTED_FLAG(governor
->flags
, IMMUTABLE
))
1469 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
1470 "%s ", governor
->name
);
1474 mutex_unlock(&devfreq_list_lock
);
1476 /* Truncate the trailing space */
1480 count
+= sprintf(&buf
[count
], "\n");
1484 static DEVICE_ATTR_RO(available_governors
);
1486 static ssize_t
cur_freq_show(struct device
*dev
, struct device_attribute
*attr
,
1490 struct devfreq
*df
= to_devfreq(dev
);
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
;
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
))
1526 ret
= sscanf(buf
, "%lu", &value
);
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
);
1539 static ssize_t
min_freq_show(struct device
*dev
, struct device_attribute
*attr
,
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
;
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
))
1567 ret
= sscanf(buf
, "%lu", &value
);
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".
1582 value
= DIV_ROUND_UP(value
, HZ_PER_KHZ
);
1584 value
= PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE
;
1586 ret
= dev_pm_qos_update_request(&df
->user_max_freq_req
, value
);
1593 static ssize_t
max_freq_show(struct device
*dev
, struct device_attribute
*attr
,
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
,
1611 struct devfreq
*df
= to_devfreq(d
);
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 */
1629 count
+= sprintf(&buf
[count
], "\n");
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
);
1641 unsigned int max_state
;
1645 max_state
= df
->profile
->max_state
;
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
);
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
, "*");
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
);
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
);
1698 if (df
->profile
->max_state
== 0)
1701 err
= kstrtoint(buf
, 10, &value
);
1702 if (err
|| value
!= 0)
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
);
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
,
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
);
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
);
1755 ret
= sscanf(buf
, "%u", &value
);
1759 df
->governor
->event_handler(df
, DEVFREQ_GOV_UPDATE_INTERVAL
, &value
);
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
);
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];
1785 if (!df
->governor
|| !df
->profile
)
1788 ret
= sscanf(buf
, "%16s", str_timer
);
1792 for (i
= 0; i
< DEVFREQ_TIMER_NUM
; i
++) {
1793 if (!strncmp(timer_name
[i
], str_timer
, DEVFREQ_NAME_LEN
)) {
1804 if (df
->profile
->timer
== timer
) {
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
);
1815 dev_warn(dev
, "%s: Governor %s not stopped(%d)\n",
1816 __func__
, df
->governor
->name
, ret
);
1820 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_START
, NULL
);
1822 dev_warn(dev
, "%s: Governor %s not started(%d)\n",
1823 __func__
, df
->governor
->name
, ret
);
1825 return ret
? ret
: count
;
1827 static DEVICE_ATTR_RW(timer
);
1829 #define CREATE_SYSFS_FILE(df, name) \
1832 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \
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
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
;
1878 seq_printf(s
, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n",
1887 seq_printf(s
, "%30s %30s %15s %10s %10s %12s %12s %12s\n",
1888 "------------------------------",
1889 "------------------------------",
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
;
1906 p_devfreq
= data
->parent
;
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
;
1921 mutex_unlock(&devfreq
->lock
);
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",
1935 mutex_unlock(&devfreq_list_lock
);
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");
1951 class_destroy(devfreq_class
);
1952 pr_err("%s: couldn't create workqueue\n", __FILE__
);
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
);
1964 subsys_initcall(devfreq_init
);
1967 * The following are helper functions for devfreq user device drivers with
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
1981 struct dev_pm_opp
*devfreq_recommended_opp(struct device
*dev
,
1982 unsigned long *freq
,
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
);
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
);
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
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
;
2053 ptr
= devres_alloc(devm_devfreq_opp_release
, sizeof(*ptr
), GFP_KERNEL
);
2057 ret
= devfreq_register_opp_notifier(dev
, devfreq
);
2064 devres_add(dev
, ptr
);
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
,
2100 case DEVFREQ_TRANSITION_NOTIFIER
:
2101 ret
= srcu_notifier_chain_register(
2102 &devfreq
->transition_notifier_list
, nb
);
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
,
2128 case DEVFREQ_TRANSITION_NOTIFIER
:
2129 ret
= srcu_notifier_chain_unregister(
2130 &devfreq
->transition_notifier_list
, nb
);
2138 EXPORT_SYMBOL(devfreq_unregister_notifier
);
2140 struct devfreq_notifier_devres
{
2141 struct devfreq
*devfreq
;
2142 struct notifier_block
*nb
;
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
,
2166 struct devfreq_notifier_devres
*ptr
;
2169 ptr
= devres_alloc(devm_devfreq_notifier_release
, sizeof(*ptr
),
2174 ret
= devfreq_register_notifier(devfreq
, nb
, list
);
2180 ptr
->devfreq
= devfreq
;
2183 devres_add(dev
, ptr
);
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
,
2202 WARN_ON(devres_release(dev
, devm_devfreq_notifier_release
,
2203 devm_devfreq_dev_match
, devfreq
));
2205 EXPORT_SYMBOL(devm_devfreq_unregister_notifier
);