2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/errno.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/opp.h>
22 #include <linux/devfreq.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/list.h>
26 #include <linux/printk.h>
27 #include <linux/hrtimer.h>
30 static struct class *devfreq_class
;
33 * devfreq core provides delayed work based load monitoring helper
34 * functions. Governors can use these or can implement their own
35 * monitoring mechanism.
37 static struct workqueue_struct
*devfreq_wq
;
39 /* The list of all device-devfreq governors */
40 static LIST_HEAD(devfreq_governor_list
);
41 /* The list of all device-devfreq */
42 static LIST_HEAD(devfreq_list
);
43 static DEFINE_MUTEX(devfreq_list_lock
);
46 * find_device_devfreq() - find devfreq struct using device pointer
47 * @dev: device pointer used to lookup device devfreq.
49 * Search the list of device devfreqs and return the matched device's
50 * devfreq info. devfreq_list_lock should be held by the caller.
52 static struct devfreq
*find_device_devfreq(struct device
*dev
)
54 struct devfreq
*tmp_devfreq
;
56 if (unlikely(IS_ERR_OR_NULL(dev
))) {
57 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
58 return ERR_PTR(-EINVAL
);
60 WARN(!mutex_is_locked(&devfreq_list_lock
),
61 "devfreq_list_lock must be locked.");
63 list_for_each_entry(tmp_devfreq
, &devfreq_list
, node
) {
64 if (tmp_devfreq
->dev
.parent
== dev
)
68 return ERR_PTR(-ENODEV
);
72 * devfreq_get_freq_level() - Lookup freq_table for the frequency
73 * @devfreq: the devfreq instance
74 * @freq: the target frequency
76 static int devfreq_get_freq_level(struct devfreq
*devfreq
, unsigned long freq
)
80 for (lev
= 0; lev
< devfreq
->profile
->max_state
; lev
++)
81 if (freq
== devfreq
->profile
->freq_table
[lev
])
88 * devfreq_update_status() - Update statistics of devfreq behavior
89 * @devfreq: the devfreq instance
90 * @freq: the update target frequency
92 static int devfreq_update_status(struct devfreq
*devfreq
, unsigned long freq
)
94 int lev
, prev_lev
, ret
= 0;
95 unsigned long cur_time
;
99 prev_lev
= devfreq_get_freq_level(devfreq
, devfreq
->previous_freq
);
101 pr_err("DEVFREQ: invalid index to update status\n");
106 devfreq
->time_in_state
[prev_lev
] +=
107 cur_time
- devfreq
->last_stat_updated
;
109 lev
= devfreq_get_freq_level(devfreq
, freq
);
115 if (lev
!= prev_lev
) {
116 devfreq
->trans_table
[(prev_lev
*
117 devfreq
->profile
->max_state
) + lev
]++;
118 devfreq
->total_trans
++;
122 devfreq
->last_stat_updated
= cur_time
;
127 * find_devfreq_governor() - find devfreq governor from name
128 * @name: name of the governor
130 * Search the list of devfreq governors and return the matched
131 * governor's pointer. devfreq_list_lock should be held by the caller.
133 static struct devfreq_governor
*find_devfreq_governor(const char *name
)
135 struct devfreq_governor
*tmp_governor
;
137 if (unlikely(IS_ERR_OR_NULL(name
))) {
138 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
139 return ERR_PTR(-EINVAL
);
141 WARN(!mutex_is_locked(&devfreq_list_lock
),
142 "devfreq_list_lock must be locked.");
144 list_for_each_entry(tmp_governor
, &devfreq_governor_list
, node
) {
145 if (!strncmp(tmp_governor
->name
, name
, DEVFREQ_NAME_LEN
))
149 return ERR_PTR(-ENODEV
);
152 /* Load monitoring helper functions for governors use */
155 * update_devfreq() - Reevaluate the device and configure frequency.
156 * @devfreq: the devfreq instance.
158 * Note: Lock devfreq->lock before calling update_devfreq
159 * This function is exported for governors.
161 int update_devfreq(struct devfreq
*devfreq
)
167 if (!mutex_is_locked(&devfreq
->lock
)) {
168 WARN(true, "devfreq->lock must be locked by the caller.\n");
172 if (!devfreq
->governor
)
175 /* Reevaluate the proper frequency */
176 err
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
181 * Adjust the freuqency with user freq and QoS.
183 * List from the highest proiority
184 * max_freq (probably called by thermal when it's too hot)
188 if (devfreq
->min_freq
&& freq
< devfreq
->min_freq
) {
189 freq
= devfreq
->min_freq
;
190 flags
&= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use GLB */
192 if (devfreq
->max_freq
&& freq
> devfreq
->max_freq
) {
193 freq
= devfreq
->max_freq
;
194 flags
|= DEVFREQ_FLAG_LEAST_UPPER_BOUND
; /* Use LUB */
197 err
= devfreq
->profile
->target(devfreq
->dev
.parent
, &freq
, flags
);
201 if (devfreq
->profile
->freq_table
)
202 if (devfreq_update_status(devfreq
, freq
))
203 dev_err(&devfreq
->dev
,
204 "Couldn't update frequency transition information.\n");
206 devfreq
->previous_freq
= freq
;
209 EXPORT_SYMBOL(update_devfreq
);
212 * devfreq_monitor() - Periodically poll devfreq objects.
213 * @work: the work struct used to run devfreq_monitor periodically.
216 static void devfreq_monitor(struct work_struct
*work
)
219 struct devfreq
*devfreq
= container_of(work
,
220 struct devfreq
, work
.work
);
222 mutex_lock(&devfreq
->lock
);
223 err
= update_devfreq(devfreq
);
224 if (err
&& err
!= -EAGAIN
)
225 dev_err(&devfreq
->dev
, "dvfs failed with (%d) error\n", err
);
226 #ifdef CONFIG_SCHED_HMP
227 mod_delayed_work_on(0, devfreq_wq
, &devfreq
->work
,
228 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
230 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
231 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
233 mutex_unlock(&devfreq
->lock
);
237 * devfreq_monitor_start() - Start load monitoring of devfreq instance
238 * @devfreq: the devfreq instance.
240 * Helper function for starting devfreq device load monitoing. By
241 * default delayed work based monitoring is supported. Function
242 * to be called from governor in response to DEVFREQ_GOV_START
243 * event when device is added to devfreq framework.
245 void devfreq_monitor_start(struct devfreq
*devfreq
)
247 INIT_DELAYED_WORK(&devfreq
->work
, devfreq_monitor
);
248 if (devfreq
->profile
->polling_ms
)
249 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
250 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
252 EXPORT_SYMBOL(devfreq_monitor_start
);
255 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
256 * @devfreq: the devfreq instance.
258 * Helper function to stop devfreq device load monitoing. Function
259 * to be called from governor in response to DEVFREQ_GOV_STOP
260 * event when device is removed from devfreq framework.
262 void devfreq_monitor_stop(struct devfreq
*devfreq
)
264 cancel_delayed_work_sync(&devfreq
->work
);
266 EXPORT_SYMBOL(devfreq_monitor_stop
);
269 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
270 * @devfreq: the devfreq instance.
272 * Helper function to suspend devfreq device load monitoing. Function
273 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
274 * event or when polling interval is set to zero.
276 * Note: Though this function is same as devfreq_monitor_stop(),
277 * intentionally kept separate to provide hooks for collecting
278 * transition statistics.
280 void devfreq_monitor_suspend(struct devfreq
*devfreq
)
282 mutex_lock(&devfreq
->lock
);
283 if (devfreq
->stop_polling
) {
284 mutex_unlock(&devfreq
->lock
);
288 devfreq
->stop_polling
= true;
289 mutex_unlock(&devfreq
->lock
);
290 cancel_delayed_work_sync(&devfreq
->work
);
292 EXPORT_SYMBOL(devfreq_monitor_suspend
);
295 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
296 * @devfreq: the devfreq instance.
298 * Helper function to resume devfreq device load monitoing. Function
299 * to be called from governor in response to DEVFREQ_GOV_RESUME
300 * event or when polling interval is set to non-zero.
302 void devfreq_monitor_resume(struct devfreq
*devfreq
)
304 mutex_lock(&devfreq
->lock
);
305 if (!devfreq
->stop_polling
)
308 if (!delayed_work_pending(&devfreq
->work
) &&
309 devfreq
->profile
->polling_ms
)
310 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
311 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
312 devfreq
->stop_polling
= false;
315 mutex_unlock(&devfreq
->lock
);
317 EXPORT_SYMBOL(devfreq_monitor_resume
);
320 * devfreq_interval_update() - Update device devfreq monitoring interval
321 * @devfreq: the devfreq instance.
322 * @delay: new polling interval to be set.
324 * Helper function to set new load monitoring polling interval. Function
325 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
327 void devfreq_interval_update(struct devfreq
*devfreq
, unsigned int *delay
)
329 unsigned int cur_delay
= devfreq
->profile
->polling_ms
;
330 unsigned int new_delay
= *delay
;
332 mutex_lock(&devfreq
->lock
);
333 devfreq
->profile
->polling_ms
= new_delay
;
335 if (devfreq
->stop_polling
)
338 /* if new delay is zero, stop polling */
340 mutex_unlock(&devfreq
->lock
);
341 cancel_delayed_work_sync(&devfreq
->work
);
345 /* if current delay is zero, start polling with new delay */
347 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
348 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
352 /* if current delay is greater than new delay, restart polling */
353 if (cur_delay
> new_delay
) {
354 mutex_unlock(&devfreq
->lock
);
355 cancel_delayed_work_sync(&devfreq
->work
);
356 mutex_lock(&devfreq
->lock
);
357 if (!devfreq
->stop_polling
)
358 queue_delayed_work(devfreq_wq
, &devfreq
->work
,
359 msecs_to_jiffies(devfreq
->profile
->polling_ms
));
362 mutex_unlock(&devfreq
->lock
);
364 EXPORT_SYMBOL(devfreq_interval_update
);
367 * devfreq_notifier_call() - Notify that the device frequency requirements
368 * has been changed out of devfreq framework.
369 * @nb: the notifier_block (supposed to be devfreq->nb)
373 * Called by a notifier that uses devfreq->nb.
375 static int devfreq_notifier_call(struct notifier_block
*nb
, unsigned long type
,
378 struct devfreq
*devfreq
= container_of(nb
, struct devfreq
, nb
);
381 mutex_lock(&devfreq
->lock
);
382 ret
= update_devfreq(devfreq
);
383 mutex_unlock(&devfreq
->lock
);
389 * _remove_devfreq() - Remove devfreq from the list and release its resources.
390 * @devfreq: the devfreq struct
391 * @skip: skip calling device_unregister().
393 static void _remove_devfreq(struct devfreq
*devfreq
, bool skip
)
395 mutex_lock(&devfreq_list_lock
);
396 if (IS_ERR(find_device_devfreq(devfreq
->dev
.parent
))) {
397 mutex_unlock(&devfreq_list_lock
);
398 dev_warn(&devfreq
->dev
, "releasing devfreq which doesn't exist\n");
401 list_del(&devfreq
->node
);
402 mutex_unlock(&devfreq_list_lock
);
404 if (devfreq
->governor
)
405 devfreq
->governor
->event_handler(devfreq
,
406 DEVFREQ_GOV_STOP
, NULL
);
408 if (devfreq
->profile
->exit
)
409 devfreq
->profile
->exit(devfreq
->dev
.parent
);
411 if (!skip
&& get_device(&devfreq
->dev
)) {
412 device_unregister(&devfreq
->dev
);
413 put_device(&devfreq
->dev
);
416 mutex_destroy(&devfreq
->lock
);
421 * devfreq_dev_release() - Callback for struct device to release the device.
422 * @dev: the devfreq device
424 * This calls _remove_devfreq() if _remove_devfreq() is not called.
425 * Note that devfreq_dev_release() could be called by _remove_devfreq() as
426 * well as by others unregistering the device.
428 static void devfreq_dev_release(struct device
*dev
)
430 struct devfreq
*devfreq
= to_devfreq(dev
);
432 _remove_devfreq(devfreq
, true);
436 * devfreq_add_device() - Add devfreq feature to the device
437 * @dev: the device to add devfreq feature.
438 * @profile: device-specific profile to run devfreq.
439 * @governor_name: name of the policy to choose frequency.
440 * @data: private data for the governor. The devfreq framework does not
443 struct devfreq
*devfreq_add_device(struct device
*dev
,
444 struct devfreq_dev_profile
*profile
,
445 const char *governor_name
,
448 struct devfreq
*devfreq
;
449 struct devfreq_governor
*governor
;
452 if (!dev
|| !profile
|| !governor_name
) {
453 dev_err(dev
, "%s: Invalid parameters.\n", __func__
);
454 return ERR_PTR(-EINVAL
);
457 mutex_lock(&devfreq_list_lock
);
458 devfreq
= find_device_devfreq(dev
);
459 mutex_unlock(&devfreq_list_lock
);
460 if (!IS_ERR(devfreq
)) {
461 dev_err(dev
, "%s: Unable to create devfreq for the device. It already has one.\n", __func__
);
466 devfreq
= kzalloc(sizeof(struct devfreq
), GFP_KERNEL
);
468 dev_err(dev
, "%s: Unable to create devfreq for the device\n",
474 mutex_init(&devfreq
->lock
);
475 mutex_lock(&devfreq
->lock
);
476 devfreq
->dev
.parent
= dev
;
477 devfreq
->dev
.class = devfreq_class
;
478 devfreq
->dev
.release
= devfreq_dev_release
;
479 devfreq
->profile
= profile
;
480 strncpy(devfreq
->governor_name
, governor_name
, DEVFREQ_NAME_LEN
);
481 devfreq
->previous_freq
= profile
->initial_freq
;
482 devfreq
->data
= data
;
483 devfreq
->nb
.notifier_call
= devfreq_notifier_call
;
485 devfreq
->trans_table
= devm_kzalloc(dev
, sizeof(unsigned int) *
486 devfreq
->profile
->max_state
*
487 devfreq
->profile
->max_state
,
489 devfreq
->time_in_state
= devm_kzalloc(dev
, sizeof(unsigned long) *
490 devfreq
->profile
->max_state
,
492 devfreq
->last_stat_updated
= jiffies
;
494 dev_set_name(&devfreq
->dev
, dev_name(dev
));
495 err
= device_register(&devfreq
->dev
);
497 put_device(&devfreq
->dev
);
498 mutex_unlock(&devfreq
->lock
);
502 mutex_unlock(&devfreq
->lock
);
504 mutex_lock(&devfreq_list_lock
);
505 list_add(&devfreq
->node
, &devfreq_list
);
507 governor
= find_devfreq_governor(devfreq
->governor_name
);
508 if (!IS_ERR(governor
))
509 devfreq
->governor
= governor
;
510 if (devfreq
->governor
)
511 err
= devfreq
->governor
->event_handler(devfreq
,
512 DEVFREQ_GOV_START
, NULL
);
513 mutex_unlock(&devfreq_list_lock
);
515 dev_err(dev
, "%s: Unable to start governor for the device\n",
523 list_del(&devfreq
->node
);
524 device_unregister(&devfreq
->dev
);
530 EXPORT_SYMBOL(devfreq_add_device
);
533 * devfreq_remove_device() - Remove devfreq feature from a device.
534 * @devfreq: the devfreq instance to be removed
536 int devfreq_remove_device(struct devfreq
*devfreq
)
541 _remove_devfreq(devfreq
, false);
545 EXPORT_SYMBOL(devfreq_remove_device
);
548 * devfreq_suspend_device() - Suspend devfreq of a device.
549 * @devfreq: the devfreq instance to be suspended
551 int devfreq_suspend_device(struct devfreq
*devfreq
)
556 if (!devfreq
->governor
)
559 return devfreq
->governor
->event_handler(devfreq
,
560 DEVFREQ_GOV_SUSPEND
, NULL
);
562 EXPORT_SYMBOL(devfreq_suspend_device
);
565 * devfreq_resume_device() - Resume devfreq of a device.
566 * @devfreq: the devfreq instance to be resumed
568 int devfreq_resume_device(struct devfreq
*devfreq
)
573 if (!devfreq
->governor
)
576 return devfreq
->governor
->event_handler(devfreq
,
577 DEVFREQ_GOV_RESUME
, NULL
);
579 EXPORT_SYMBOL(devfreq_resume_device
);
582 * devfreq_add_governor() - Add devfreq governor
583 * @governor: the devfreq governor to be added
585 int devfreq_add_governor(struct devfreq_governor
*governor
)
587 struct devfreq_governor
*g
;
588 struct devfreq
*devfreq
;
592 pr_err("%s: Invalid parameters.\n", __func__
);
596 mutex_lock(&devfreq_list_lock
);
597 g
= find_devfreq_governor(governor
->name
);
599 pr_err("%s: governor %s already registered\n", __func__
,
605 list_add(&governor
->node
, &devfreq_governor_list
);
607 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
609 struct device
*dev
= devfreq
->dev
.parent
;
611 if (!strncmp(devfreq
->governor_name
, governor
->name
,
613 /* The following should never occur */
614 if (devfreq
->governor
) {
616 "%s: Governor %s already present\n",
617 __func__
, devfreq
->governor
->name
);
618 ret
= devfreq
->governor
->event_handler(devfreq
,
619 DEVFREQ_GOV_STOP
, NULL
);
622 "%s: Governor %s stop = %d\n",
624 devfreq
->governor
->name
, ret
);
628 devfreq
->governor
= governor
;
629 ret
= devfreq
->governor
->event_handler(devfreq
,
630 DEVFREQ_GOV_START
, NULL
);
632 dev_warn(dev
, "%s: Governor %s start=%d\n",
633 __func__
, devfreq
->governor
->name
,
640 mutex_unlock(&devfreq_list_lock
);
644 EXPORT_SYMBOL(devfreq_add_governor
);
647 * devfreq_remove_device() - Remove devfreq feature from a device.
648 * @governor: the devfreq governor to be removed
650 int devfreq_remove_governor(struct devfreq_governor
*governor
)
652 struct devfreq_governor
*g
;
653 struct devfreq
*devfreq
;
657 pr_err("%s: Invalid parameters.\n", __func__
);
661 mutex_lock(&devfreq_list_lock
);
662 g
= find_devfreq_governor(governor
->name
);
664 pr_err("%s: governor %s not registered\n", __func__
,
669 list_for_each_entry(devfreq
, &devfreq_list
, node
) {
671 struct device
*dev
= devfreq
->dev
.parent
;
673 if (!strncmp(devfreq
->governor_name
, governor
->name
,
675 /* we should have a devfreq governor! */
676 if (!devfreq
->governor
) {
677 dev_warn(dev
, "%s: Governor %s NOT present\n",
678 __func__
, governor
->name
);
682 ret
= devfreq
->governor
->event_handler(devfreq
,
683 DEVFREQ_GOV_STOP
, NULL
);
685 dev_warn(dev
, "%s: Governor %s stop=%d\n",
686 __func__
, devfreq
->governor
->name
,
689 devfreq
->governor
= NULL
;
693 list_del(&governor
->node
);
695 mutex_unlock(&devfreq_list_lock
);
699 EXPORT_SYMBOL(devfreq_remove_governor
);
701 static ssize_t
show_governor(struct device
*dev
,
702 struct device_attribute
*attr
, char *buf
)
704 if (!to_devfreq(dev
)->governor
)
707 return sprintf(buf
, "%s\n", to_devfreq(dev
)->governor
->name
);
710 static ssize_t
store_governor(struct device
*dev
, struct device_attribute
*attr
,
711 const char *buf
, size_t count
)
713 struct devfreq
*df
= to_devfreq(dev
);
715 char str_governor
[DEVFREQ_NAME_LEN
+ 1];
716 struct devfreq_governor
*governor
;
718 ret
= sscanf(buf
, "%" __stringify(DEVFREQ_NAME_LEN
) "s", str_governor
);
722 mutex_lock(&devfreq_list_lock
);
723 governor
= find_devfreq_governor(str_governor
);
724 if (IS_ERR(governor
)) {
725 ret
= PTR_ERR(governor
);
728 if (df
->governor
== governor
)
732 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_STOP
, NULL
);
734 dev_warn(dev
, "%s: Governor %s not stopped(%d)\n",
735 __func__
, df
->governor
->name
, ret
);
739 df
->governor
= governor
;
740 strncpy(df
->governor_name
, governor
->name
, DEVFREQ_NAME_LEN
);
741 ret
= df
->governor
->event_handler(df
, DEVFREQ_GOV_START
, NULL
);
743 dev_warn(dev
, "%s: Governor %s not started(%d)\n",
744 __func__
, df
->governor
->name
, ret
);
746 mutex_unlock(&devfreq_list_lock
);
752 static ssize_t
show_available_governors(struct device
*d
,
753 struct device_attribute
*attr
,
756 struct devfreq_governor
*tmp_governor
;
759 mutex_lock(&devfreq_list_lock
);
760 list_for_each_entry(tmp_governor
, &devfreq_governor_list
, node
)
761 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
762 "%s ", tmp_governor
->name
);
763 mutex_unlock(&devfreq_list_lock
);
765 /* Truncate the trailing space */
769 count
+= sprintf(&buf
[count
], "\n");
774 static ssize_t
show_freq(struct device
*dev
,
775 struct device_attribute
*attr
, char *buf
)
778 struct devfreq
*devfreq
= to_devfreq(dev
);
780 if (devfreq
->profile
->get_cur_freq
&&
781 !devfreq
->profile
->get_cur_freq(devfreq
->dev
.parent
, &freq
))
782 return sprintf(buf
, "%lu\n", freq
);
784 return sprintf(buf
, "%lu\n", devfreq
->previous_freq
);
787 static ssize_t
show_target_freq(struct device
*dev
,
788 struct device_attribute
*attr
, char *buf
)
790 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->previous_freq
);
793 static ssize_t
show_polling_interval(struct device
*dev
,
794 struct device_attribute
*attr
, char *buf
)
796 return sprintf(buf
, "%d\n", to_devfreq(dev
)->profile
->polling_ms
);
799 static ssize_t
store_polling_interval(struct device
*dev
,
800 struct device_attribute
*attr
,
801 const char *buf
, size_t count
)
803 struct devfreq
*df
= to_devfreq(dev
);
810 ret
= sscanf(buf
, "%u", &value
);
814 df
->governor
->event_handler(df
, DEVFREQ_GOV_INTERVAL
, &value
);
820 static ssize_t
store_min_freq(struct device
*dev
, struct device_attribute
*attr
,
821 const char *buf
, size_t count
)
823 struct devfreq
*df
= to_devfreq(dev
);
828 ret
= sscanf(buf
, "%lu", &value
);
832 mutex_lock(&df
->lock
);
834 if (value
&& max
&& value
> max
) {
839 df
->min_freq
= value
;
843 mutex_unlock(&df
->lock
);
847 static ssize_t
show_min_freq(struct device
*dev
, struct device_attribute
*attr
,
850 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->min_freq
);
853 static ssize_t
store_max_freq(struct device
*dev
, struct device_attribute
*attr
,
854 const char *buf
, size_t count
)
856 struct devfreq
*df
= to_devfreq(dev
);
861 ret
= sscanf(buf
, "%lu", &value
);
865 mutex_lock(&df
->lock
);
867 if (value
&& min
&& value
< min
) {
872 df
->max_freq
= value
;
876 mutex_unlock(&df
->lock
);
880 static ssize_t
show_max_freq(struct device
*dev
, struct device_attribute
*attr
,
883 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->max_freq
);
886 static ssize_t
show_available_freqs(struct device
*d
,
887 struct device_attribute
*attr
,
890 struct devfreq
*df
= to_devfreq(d
);
891 struct device
*dev
= df
->dev
.parent
;
894 unsigned long freq
= 0;
898 opp
= opp_find_freq_ceil(dev
, &freq
);
902 count
+= scnprintf(&buf
[count
], (PAGE_SIZE
- count
- 2),
908 /* Truncate the trailing space */
912 count
+= sprintf(&buf
[count
], "\n");
917 static ssize_t
show_trans_table(struct device
*dev
, struct device_attribute
*attr
,
920 struct devfreq
*devfreq
= to_devfreq(dev
);
923 unsigned int max_state
= devfreq
->profile
->max_state
;
925 err
= devfreq_update_status(devfreq
, devfreq
->previous_freq
);
929 len
= sprintf(buf
, " From : To\n");
930 len
+= sprintf(buf
+ len
, " :");
931 for (i
= 0; i
< max_state
; i
++)
932 len
+= sprintf(buf
+ len
, "%8u",
933 devfreq
->profile
->freq_table
[i
]);
935 len
+= sprintf(buf
+ len
, " time(ms)\n");
937 for (i
= 0; i
< max_state
; i
++) {
938 if (devfreq
->profile
->freq_table
[i
]
939 == devfreq
->previous_freq
) {
940 len
+= sprintf(buf
+ len
, "*");
942 len
+= sprintf(buf
+ len
, " ");
944 len
+= sprintf(buf
+ len
, "%8u:",
945 devfreq
->profile
->freq_table
[i
]);
946 for (j
= 0; j
< max_state
; j
++)
947 len
+= sprintf(buf
+ len
, "%8u",
948 devfreq
->trans_table
[(i
* max_state
) + j
]);
949 len
+= sprintf(buf
+ len
, "%10u\n",
950 jiffies_to_msecs(devfreq
->time_in_state
[i
]));
953 len
+= sprintf(buf
+ len
, "Total transition : %u\n",
954 devfreq
->total_trans
);
958 static ssize_t
show_time_in_state(struct device
*dev
,
959 struct device_attribute
*attr
, char *buf
)
961 struct devfreq
*devfreq
= to_devfreq(dev
);
964 unsigned int max_state
= devfreq
->profile
->max_state
;
966 err
= devfreq_update_status(devfreq
, devfreq
->previous_freq
);
970 for (i
= 0; i
< max_state
; i
++) {
971 len
+= sprintf(buf
+ len
, "%8u",
972 devfreq
->profile
->freq_table
[i
]);
973 len
+= sprintf(buf
+ len
, "%10u\n",
974 jiffies_to_msecs(devfreq
->time_in_state
[i
]));
979 static struct device_attribute devfreq_attrs
[] = {
980 __ATTR(governor
, S_IRUGO
| S_IWUSR
, show_governor
, store_governor
),
981 __ATTR(available_governors
, S_IRUGO
, show_available_governors
, NULL
),
982 __ATTR(cur_freq
, S_IRUGO
, show_freq
, NULL
),
983 __ATTR(available_frequencies
, S_IRUGO
, show_available_freqs
, NULL
),
984 __ATTR(target_freq
, S_IRUGO
, show_target_freq
, NULL
),
985 __ATTR(polling_interval
, S_IRUGO
| S_IWUSR
, show_polling_interval
,
986 store_polling_interval
),
987 __ATTR(min_freq
, S_IRUGO
| S_IWUSR
, show_min_freq
, store_min_freq
),
988 __ATTR(max_freq
, S_IRUGO
| S_IWUSR
, show_max_freq
, store_max_freq
),
989 __ATTR(trans_stat
, S_IRUGO
, show_trans_table
, NULL
),
990 __ATTR(time_in_state_raw
, S_IRUGO
, show_time_in_state
, NULL
),
994 static int __init
devfreq_init(void)
996 devfreq_class
= class_create(THIS_MODULE
, "devfreq");
997 if (IS_ERR(devfreq_class
)) {
998 pr_err("%s: couldn't create class\n", __FILE__
);
999 return PTR_ERR(devfreq_class
);
1002 devfreq_wq
= create_freezable_workqueue("devfreq_wq");
1003 if (IS_ERR(devfreq_wq
)) {
1004 class_destroy(devfreq_class
);
1005 pr_err("%s: couldn't create workqueue\n", __FILE__
);
1006 return PTR_ERR(devfreq_wq
);
1008 devfreq_class
->dev_attrs
= devfreq_attrs
;
1012 subsys_initcall(devfreq_init
);
1014 static void __exit
devfreq_exit(void)
1016 class_destroy(devfreq_class
);
1017 destroy_workqueue(devfreq_wq
);
1019 module_exit(devfreq_exit
);
1022 * The followings are helper functions for devfreq user device drivers with
1027 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1028 * freq value given to target callback.
1029 * @dev: The devfreq user device. (parent of devfreq)
1030 * @freq: The frequency given to target function
1031 * @flags: Flags handed from devfreq framework.
1033 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1034 * protected pointer. The reason for the same is that the opp pointer which is
1035 * returned will remain valid for use with opp_get_{voltage, freq} only while
1036 * under the locked area. The pointer returned must be used prior to unlocking
1037 * with rcu_read_unlock() to maintain the integrity of the pointer.
1039 struct opp
*devfreq_recommended_opp(struct device
*dev
, unsigned long *freq
,
1044 if (flags
& DEVFREQ_FLAG_LEAST_UPPER_BOUND
) {
1045 /* The freq is an upper bound. opp should be lower */
1046 opp
= opp_find_freq_floor(dev
, freq
);
1048 /* If not available, use the closest opp */
1049 if (opp
== ERR_PTR(-ERANGE
))
1050 opp
= opp_find_freq_ceil(dev
, freq
);
1052 /* The freq is an lower bound. opp should be higher */
1053 opp
= opp_find_freq_ceil(dev
, freq
);
1055 /* If not available, use the closest opp */
1056 if (opp
== ERR_PTR(-ERANGE
))
1057 opp
= opp_find_freq_floor(dev
, freq
);
1064 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1065 * for any changes in the OPP availability
1067 * @dev: The devfreq user device. (parent of devfreq)
1068 * @devfreq: The devfreq object.
1070 int devfreq_register_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1072 struct srcu_notifier_head
*nh
;
1076 nh
= opp_get_notifier(dev
);
1081 ret
= srcu_notifier_chain_register(nh
, &devfreq
->nb
);
1087 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1088 * notified for any changes in the OPP
1089 * availability changes anymore.
1090 * @dev: The devfreq user device. (parent of devfreq)
1091 * @devfreq: The devfreq object.
1093 * At exit() callback of devfreq_dev_profile, this must be included if
1094 * devfreq_recommended_opp is used.
1096 int devfreq_unregister_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
1098 struct srcu_notifier_head
*nh
;
1102 nh
= opp_get_notifier(dev
);
1107 ret
= srcu_notifier_chain_unregister(nh
, &devfreq
->nb
);
1112 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1113 MODULE_DESCRIPTION("devfreq class support");
1114 MODULE_LICENSE("GPL");