2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * Copyright (c) 2013-2014, NVIDIA CORPORATION. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/stat.h>
22 #include <linux/opp.h>
23 #include <linux/devfreq.h>
24 #include <linux/workqueue.h>
25 #include <linux/platform_device.h>
26 #include <linux/list.h>
27 #include <linux/printk.h>
28 #include <linux/hrtimer.h>
31 static struct class *devfreq_class;
34 * devfreq core provides delayed work based load monitoring helper
35 * functions. Governors can use these or can implement their own
36 * monitoring mechanism.
38 static struct workqueue_struct *devfreq_wq;
40 /* The list of all device-devfreq governors */
41 static LIST_HEAD(devfreq_governor_list);
42 /* The list of all device-devfreq */
43 static LIST_HEAD(devfreq_list);
44 static DEFINE_MUTEX(devfreq_list_lock);
47 * find_device_devfreq() - find devfreq struct using device pointer
48 * @dev: device pointer used to lookup device devfreq.
50 * Search the list of device devfreqs and return the matched device's
51 * devfreq info. devfreq_list_lock should be held by the caller.
53 static struct devfreq *find_device_devfreq(struct device *dev)
55 struct devfreq *tmp_devfreq;
57 if (unlikely(IS_ERR_OR_NULL(dev))) {
58 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
59 return ERR_PTR(-EINVAL);
61 WARN(!mutex_is_locked(&devfreq_list_lock),
62 "devfreq_list_lock must be locked.");
64 list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
65 if (tmp_devfreq->dev.parent == dev)
69 return ERR_PTR(-ENODEV);
73 * devfreq_get_freq_level() - Lookup freq_table for the frequency
74 * @devfreq: the devfreq instance
75 * @freq: the target frequency
77 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
81 if (!devfreq->profile->max_state)
84 for (lev = 0; lev < devfreq->profile->max_state; lev++) {
85 if (devfreq->profile->freq_table[lev] >= freq) {
86 /* below minimum frequency? just return zero level */
90 /* select high freq if the freq is closer to that */
91 if ((devfreq->profile->freq_table[lev] - freq) <
92 (freq - devfreq->profile->freq_table[lev - 1]))
95 /* ..otherwise return the low freq */
101 return devfreq->profile->max_state - 1;
105 * devfreq_update_status() - Update statistics of devfreq behavior
106 * @devfreq: the devfreq instance
107 * @freq: the update target frequency
109 static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
112 unsigned long cur_time;
114 if (devfreq->suspended)
117 lev = devfreq_get_freq_level(devfreq, freq);
121 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
126 devfreq->time_in_state[lev] +=
127 cur_time - devfreq->last_stat_updated;
128 if (lev != prev_lev) {
129 devfreq->trans_table[(prev_lev *
130 devfreq->profile->max_state) + lev]++;
131 devfreq->total_trans++;
133 devfreq->last_stat_updated = cur_time;
139 * find_devfreq_governor() - find devfreq governor from name
140 * @name: name of the governor
142 * Search the list of devfreq governors and return the matched
143 * governor's pointer. devfreq_list_lock should be held by the caller.
145 static struct devfreq_governor *find_devfreq_governor(const char *name)
147 struct devfreq_governor *tmp_governor;
149 if (unlikely(IS_ERR_OR_NULL(name))) {
150 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
151 return ERR_PTR(-EINVAL);
153 WARN(!mutex_is_locked(&devfreq_list_lock),
154 "devfreq_list_lock must be locked.");
156 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
157 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
161 return ERR_PTR(-ENODEV);
164 /* Load monitoring helper functions for governors use */
167 * update_devfreq() - Reevaluate the device and configure frequency.
168 * @devfreq: the devfreq instance.
170 * Note: Lock devfreq->lock before calling update_devfreq
171 * This function is exported for governors.
173 int update_devfreq(struct devfreq *devfreq)
179 if (!mutex_is_locked(&devfreq->lock)) {
180 WARN(true, "devfreq->lock must be locked by the caller.\n");
184 if (!devfreq->governor)
187 /* Reevaluate the proper frequency */
188 err = devfreq->governor->get_target_freq(devfreq, &freq);
193 * Adjust the freuqency with user freq and QoS.
195 * List from the highest proiority
196 * max_freq (probably called by thermal when it's too hot)
200 if (devfreq->min_freq && freq < devfreq->min_freq) {
201 freq = devfreq->min_freq;
202 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
204 if (devfreq->max_freq && freq > devfreq->max_freq) {
205 freq = devfreq->max_freq;
206 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
209 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
213 if (devfreq->profile->freq_table)
214 if (devfreq_update_status(devfreq, freq))
215 dev_err(&devfreq->dev,
216 "Couldn't update frequency transition information.\n");
218 devfreq->previous_freq = freq;
221 EXPORT_SYMBOL(update_devfreq);
224 * devfreq_monitor() - Periodically poll devfreq objects.
225 * @work: the work struct used to run devfreq_monitor periodically.
228 static void devfreq_monitor(struct work_struct *work)
231 struct devfreq *devfreq = container_of(work,
232 struct devfreq, work.work);
234 mutex_lock(&devfreq->lock);
235 err = update_devfreq(devfreq);
237 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
239 queue_delayed_work(devfreq_wq, &devfreq->work,
240 msecs_to_jiffies(devfreq->profile->polling_ms));
241 mutex_unlock(&devfreq->lock);
245 * devfreq_monitor_start() - Start load monitoring of devfreq instance
246 * @devfreq: the devfreq instance.
248 * Helper function for starting devfreq device load monitoing. By
249 * default delayed work based monitoring is supported. Function
250 * to be called from governor in response to DEVFREQ_GOV_START
251 * event when device is added to devfreq framework.
253 void devfreq_monitor_start(struct devfreq *devfreq)
255 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
256 if (devfreq->profile->polling_ms)
257 queue_delayed_work(devfreq_wq, &devfreq->work,
258 msecs_to_jiffies(devfreq->profile->polling_ms));
260 EXPORT_SYMBOL(devfreq_monitor_start);
263 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
264 * @devfreq: the devfreq instance.
266 * Helper function to stop devfreq device load monitoing. Function
267 * to be called from governor in response to DEVFREQ_GOV_STOP
268 * event when device is removed from devfreq framework.
270 void devfreq_monitor_stop(struct devfreq *devfreq)
272 cancel_delayed_work_sync(&devfreq->work);
274 EXPORT_SYMBOL(devfreq_monitor_stop);
277 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
278 * @devfreq: the devfreq instance.
280 * Helper function to suspend devfreq device load monitoing. Function
281 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
282 * event or when polling interval is set to zero.
284 * Note: Though this function is same as devfreq_monitor_stop(),
285 * intentionally kept separate to provide hooks for collecting
286 * transition statistics.
288 void devfreq_monitor_suspend(struct devfreq *devfreq)
290 mutex_lock(&devfreq->lock);
291 if (devfreq->stop_polling) {
292 mutex_unlock(&devfreq->lock);
296 devfreq->stop_polling = true;
297 mutex_unlock(&devfreq->lock);
298 cancel_delayed_work_sync(&devfreq->work);
300 EXPORT_SYMBOL(devfreq_monitor_suspend);
303 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
304 * @devfreq: the devfreq instance.
306 * Helper function to resume devfreq device load monitoing. Function
307 * to be called from governor in response to DEVFREQ_GOV_RESUME
308 * event or when polling interval is set to non-zero.
310 void devfreq_monitor_resume(struct devfreq *devfreq)
312 mutex_lock(&devfreq->lock);
313 if (!devfreq->stop_polling)
316 if (!delayed_work_pending(&devfreq->work) &&
317 devfreq->profile->polling_ms)
318 queue_delayed_work(devfreq_wq, &devfreq->work,
319 msecs_to_jiffies(devfreq->profile->polling_ms));
320 devfreq->stop_polling = false;
323 mutex_unlock(&devfreq->lock);
325 EXPORT_SYMBOL(devfreq_monitor_resume);
328 * devfreq_interval_update() - Update device devfreq monitoring interval
329 * @devfreq: the devfreq instance.
330 * @delay: new polling interval to be set.
332 * Helper function to set new load monitoring polling interval. Function
333 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
335 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
337 unsigned int cur_delay = devfreq->profile->polling_ms;
338 unsigned int new_delay = *delay;
340 mutex_lock(&devfreq->lock);
341 devfreq->profile->polling_ms = new_delay;
343 if (devfreq->stop_polling)
346 /* if new delay is zero, stop polling */
348 mutex_unlock(&devfreq->lock);
349 cancel_delayed_work_sync(&devfreq->work);
353 /* if current delay is zero, start polling with new delay */
355 queue_delayed_work(devfreq_wq, &devfreq->work,
356 msecs_to_jiffies(devfreq->profile->polling_ms));
360 /* if current delay is greater than new delay, restart polling */
361 if (cur_delay > new_delay) {
362 mutex_unlock(&devfreq->lock);
363 cancel_delayed_work_sync(&devfreq->work);
364 mutex_lock(&devfreq->lock);
365 if (!devfreq->stop_polling)
366 queue_delayed_work(devfreq_wq, &devfreq->work,
367 msecs_to_jiffies(devfreq->profile->polling_ms));
370 mutex_unlock(&devfreq->lock);
372 EXPORT_SYMBOL(devfreq_interval_update);
375 * devfreq_notifier_call() - Notify that the device frequency requirements
376 * has been changed out of devfreq framework.
377 * @nb: the notifier_block (supposed to be devfreq->nb)
381 * Called by a notifier that uses devfreq->nb.
383 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
386 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
389 mutex_lock(&devfreq->lock);
390 ret = update_devfreq(devfreq);
391 mutex_unlock(&devfreq->lock);
397 * _remove_devfreq() - Remove devfreq from the list and release its resources.
398 * @devfreq: the devfreq struct
399 * @skip: skip calling device_unregister().
401 static void _remove_devfreq(struct devfreq *devfreq, bool skip)
403 mutex_lock(&devfreq_list_lock);
404 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
405 mutex_unlock(&devfreq_list_lock);
406 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
409 list_del(&devfreq->node);
410 mutex_unlock(&devfreq_list_lock);
412 if (devfreq->governor)
413 devfreq->governor->event_handler(devfreq,
414 DEVFREQ_GOV_STOP, NULL);
416 if (devfreq->profile->exit)
417 devfreq->profile->exit(devfreq->dev.parent);
419 if (!skip && get_device(&devfreq->dev)) {
420 device_unregister(&devfreq->dev);
421 put_device(&devfreq->dev);
424 mutex_destroy(&devfreq->lock);
429 * devfreq_dev_release() - Callback for struct device to release the device.
430 * @dev: the devfreq device
432 * This calls _remove_devfreq() if _remove_devfreq() is not called.
433 * Note that devfreq_dev_release() could be called by _remove_devfreq() as
434 * well as by others unregistering the device.
436 static void devfreq_dev_release(struct device *dev)
438 struct devfreq *devfreq = to_devfreq(dev);
440 _remove_devfreq(devfreq, true);
444 * devfreq_add_device() - Add devfreq feature to the device
445 * @dev: the device to add devfreq feature.
446 * @profile: device-specific profile to run devfreq.
447 * @governor_name: name of the policy to choose frequency.
448 * @data: private data for the governor. The devfreq framework does not
451 struct devfreq *devfreq_add_device(struct device *dev,
452 struct devfreq_dev_profile *profile,
453 const char *governor_name,
456 struct devfreq *devfreq;
457 struct devfreq_governor *governor;
460 if (!dev || !profile || !governor_name) {
461 dev_err(dev, "%s: Invalid parameters.\n", __func__);
462 return ERR_PTR(-EINVAL);
465 mutex_lock(&devfreq_list_lock);
466 devfreq = find_device_devfreq(dev);
467 mutex_unlock(&devfreq_list_lock);
468 if (!IS_ERR(devfreq)) {
469 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
474 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
476 dev_err(dev, "%s: Unable to create devfreq for the device\n",
482 mutex_init(&devfreq->lock);
483 mutex_lock(&devfreq->lock);
484 devfreq->dev.parent = dev;
485 devfreq->dev.class = devfreq_class;
486 devfreq->dev.release = devfreq_dev_release;
487 devfreq->profile = profile;
488 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
489 devfreq->previous_freq = profile->initial_freq;
490 devfreq->data = data;
491 devfreq->nb.notifier_call = devfreq_notifier_call;
493 devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
494 devfreq->profile->max_state *
495 devfreq->profile->max_state,
497 devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
498 devfreq->profile->max_state,
500 devfreq->last_stat_updated = jiffies;
502 dev_set_name(&devfreq->dev, dev_name(dev));
503 err = device_register(&devfreq->dev);
505 put_device(&devfreq->dev);
506 mutex_unlock(&devfreq->lock);
510 mutex_unlock(&devfreq->lock);
512 mutex_lock(&devfreq_list_lock);
513 list_add(&devfreq->node, &devfreq_list);
515 governor = find_devfreq_governor(devfreq->governor_name);
516 if (!IS_ERR(governor))
517 devfreq->governor = governor;
518 if (devfreq->governor)
519 err = devfreq->governor->event_handler(devfreq,
520 DEVFREQ_GOV_START, NULL);
521 mutex_unlock(&devfreq_list_lock);
523 dev_err(dev, "%s: Unable to start governor for the device\n",
531 list_del(&devfreq->node);
532 device_unregister(&devfreq->dev);
538 EXPORT_SYMBOL(devfreq_add_device);
541 * devfreq_remove_device() - Remove devfreq feature from a device.
542 * @devfreq: the devfreq instance to be removed
544 int devfreq_remove_device(struct devfreq *devfreq)
549 _remove_devfreq(devfreq, false);
553 EXPORT_SYMBOL(devfreq_remove_device);
556 * devfreq_suspend_device() - Suspend devfreq of a device.
557 * @devfreq: the devfreq instance to be suspended
559 int devfreq_suspend_device(struct devfreq *devfreq)
564 /* Last update before suspend */
565 mutex_lock(&devfreq->lock);
566 devfreq_update_status(devfreq, devfreq->previous_freq);
567 devfreq->suspended = true;
568 mutex_unlock(&devfreq->lock);
570 if (!devfreq->governor)
573 return devfreq->governor->event_handler(devfreq,
574 DEVFREQ_GOV_SUSPEND, NULL);
576 EXPORT_SYMBOL(devfreq_suspend_device);
579 * devfreq_resume_device() - Resume devfreq of a device.
580 * @devfreq: the devfreq instance to be resumed
582 int devfreq_resume_device(struct devfreq *devfreq)
587 /* Update the timestamp before resuming */
588 mutex_lock(&devfreq->lock);
589 devfreq->last_stat_updated = jiffies;
590 devfreq->suspended = false;
591 mutex_unlock(&devfreq->lock);
593 if (!devfreq->governor)
596 return devfreq->governor->event_handler(devfreq,
597 DEVFREQ_GOV_RESUME, NULL);
599 EXPORT_SYMBOL(devfreq_resume_device);
602 * devfreq_add_governor() - Add devfreq governor
603 * @governor: the devfreq governor to be added
605 int devfreq_add_governor(struct devfreq_governor *governor)
607 struct devfreq_governor *g;
608 struct devfreq *devfreq;
612 pr_err("%s: Invalid parameters.\n", __func__);
616 mutex_lock(&devfreq_list_lock);
617 g = find_devfreq_governor(governor->name);
619 pr_err("%s: governor %s already registered\n", __func__,
625 list_add(&governor->node, &devfreq_governor_list);
627 list_for_each_entry(devfreq, &devfreq_list, node) {
629 struct device *dev = devfreq->dev.parent;
631 if (!strncmp(devfreq->governor_name, governor->name,
633 /* The following should never occur */
634 if (devfreq->governor) {
636 "%s: Governor %s already present\n",
637 __func__, devfreq->governor->name);
638 ret = devfreq->governor->event_handler(devfreq,
639 DEVFREQ_GOV_STOP, NULL);
642 "%s: Governor %s stop = %d\n",
644 devfreq->governor->name, ret);
648 devfreq->governor = governor;
649 ret = devfreq->governor->event_handler(devfreq,
650 DEVFREQ_GOV_START, NULL);
652 dev_warn(dev, "%s: Governor %s start=%d\n",
653 __func__, devfreq->governor->name,
660 mutex_unlock(&devfreq_list_lock);
664 EXPORT_SYMBOL(devfreq_add_governor);
667 * devfreq_remove_device() - Remove devfreq feature from a device.
668 * @governor: the devfreq governor to be removed
670 int devfreq_remove_governor(struct devfreq_governor *governor)
672 struct devfreq_governor *g;
673 struct devfreq *devfreq;
677 pr_err("%s: Invalid parameters.\n", __func__);
681 mutex_lock(&devfreq_list_lock);
682 g = find_devfreq_governor(governor->name);
684 pr_err("%s: governor %s not registered\n", __func__,
689 list_for_each_entry(devfreq, &devfreq_list, node) {
691 struct device *dev = devfreq->dev.parent;
693 if (!strncmp(devfreq->governor_name, governor->name,
695 /* we should have a devfreq governor! */
696 if (!devfreq->governor) {
697 dev_warn(dev, "%s: Governor %s NOT present\n",
698 __func__, governor->name);
702 ret = devfreq->governor->event_handler(devfreq,
703 DEVFREQ_GOV_STOP, NULL);
705 dev_warn(dev, "%s: Governor %s stop=%d\n",
706 __func__, devfreq->governor->name,
709 devfreq->governor = NULL;
713 list_del(&governor->node);
715 mutex_unlock(&devfreq_list_lock);
719 EXPORT_SYMBOL(devfreq_remove_governor);
721 static ssize_t show_governor(struct device *dev,
722 struct device_attribute *attr, char *buf)
724 if (!to_devfreq(dev)->governor)
727 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
730 static ssize_t store_governor(struct device *dev, struct device_attribute *attr,
731 const char *buf, size_t count)
733 struct devfreq *df = to_devfreq(dev);
735 char str_governor[DEVFREQ_NAME_LEN + 1];
736 struct devfreq_governor *governor;
738 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
742 mutex_lock(&devfreq_list_lock);
743 governor = find_devfreq_governor(str_governor);
744 if (IS_ERR(governor)) {
745 ret = PTR_ERR(governor);
748 if (df->governor == governor)
752 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
754 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
755 __func__, df->governor->name, ret);
759 df->governor = governor;
760 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
761 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
763 dev_warn(dev, "%s: Governor %s not started(%d)\n",
764 __func__, df->governor->name, ret);
766 mutex_unlock(&devfreq_list_lock);
772 static ssize_t show_available_governors(struct device *d,
773 struct device_attribute *attr,
776 struct devfreq_governor *tmp_governor;
779 mutex_lock(&devfreq_list_lock);
780 list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
781 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
782 "%s ", tmp_governor->name);
783 mutex_unlock(&devfreq_list_lock);
785 /* Truncate the trailing space */
789 count += sprintf(&buf[count], "\n");
794 static ssize_t show_freq(struct device *dev,
795 struct device_attribute *attr, char *buf)
798 struct devfreq *devfreq = to_devfreq(dev);
800 if (devfreq->profile->get_cur_freq &&
801 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
802 return sprintf(buf, "%lu\n", freq);
804 return sprintf(buf, "%lu\n", devfreq->previous_freq);
807 static ssize_t show_target_freq(struct device *dev,
808 struct device_attribute *attr, char *buf)
810 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
813 static ssize_t show_polling_interval(struct device *dev,
814 struct device_attribute *attr, char *buf)
816 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
819 static ssize_t store_polling_interval(struct device *dev,
820 struct device_attribute *attr,
821 const char *buf, size_t count)
823 struct devfreq *df = to_devfreq(dev);
830 ret = sscanf(buf, "%u", &value);
834 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
840 static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
841 const char *buf, size_t count)
843 struct devfreq *df = to_devfreq(dev);
848 ret = sscanf(buf, "%lu", &value);
852 mutex_lock(&df->lock);
854 if (value && max && value > max) {
859 df->min_freq = value;
863 mutex_unlock(&df->lock);
867 static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
870 return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
873 static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
874 const char *buf, size_t count)
876 struct devfreq *df = to_devfreq(dev);
881 ret = sscanf(buf, "%lu", &value);
885 mutex_lock(&df->lock);
887 if (value && min && value < min) {
892 df->max_freq = value;
896 mutex_unlock(&df->lock);
900 static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
903 return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
906 static ssize_t show_available_freqs(struct device *d,
907 struct device_attribute *attr,
910 struct devfreq *df = to_devfreq(d);
911 struct device *dev = df->dev.parent;
914 unsigned long freq = 0;
916 if (df->profile->max_state) {
919 for (i = 0; i < df->profile->max_state; i++) {
920 freq = df->profile->freq_table[i];
921 count += scnprintf(&buf[count],
922 (PAGE_SIZE - count - 2), "%lu ",
928 opp = opp_find_freq_ceil(dev, &freq);
932 count += scnprintf(&buf[count],
933 (PAGE_SIZE - count - 2), "%lu ",
940 /* Truncate the trailing space */
944 count += sprintf(&buf[count], "\n");
949 static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
952 struct devfreq *devfreq = to_devfreq(dev);
955 unsigned int max_state = devfreq->profile->max_state;
957 unsigned long prev_freq;
959 mutex_lock(&devfreq->lock);
960 err = devfreq_update_status(devfreq, devfreq->previous_freq);
963 mutex_unlock(&devfreq->lock);
965 /* round the current frequency */
966 prev_freq_level = devfreq_get_freq_level(devfreq,
967 devfreq->previous_freq);
969 if (prev_freq_level < 0)
970 prev_freq = devfreq->previous_freq;
972 prev_freq = devfreq->profile->freq_table[prev_freq_level];
974 len = sprintf(buf, " From : To\n");
975 len += sprintf(buf + len, " :");
976 for (i = 0; i < max_state; i++)
977 len += sprintf(buf + len, "%8lu",
978 devfreq->profile->freq_table[i]);
980 len += sprintf(buf + len, " time(ms)\n");
982 for (i = 0; i < max_state; i++) {
983 if (devfreq->profile->freq_table[i] == prev_freq &&
984 !devfreq->suspended) {
985 len += sprintf(buf + len, "*");
987 len += sprintf(buf + len, " ");
989 len += sprintf(buf + len, "%8lu:",
990 devfreq->profile->freq_table[i]);
991 for (j = 0; j < max_state; j++)
992 len += sprintf(buf + len, "%8u",
993 devfreq->trans_table[(i * max_state) + j]);
994 len += sprintf(buf + len, "%10u\n",
995 jiffies_to_msecs(devfreq->time_in_state[i]));
998 len += sprintf(buf + len, "Total transition : %u\n",
999 devfreq->total_trans);
1003 static struct device_attribute devfreq_attrs[] = {
1004 __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor),
1005 __ATTR(available_governors, S_IRUGO, show_available_governors, NULL),
1006 __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
1007 __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
1008 __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
1009 __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
1010 store_polling_interval),
1011 __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
1012 __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
1013 __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
1017 static int __init devfreq_init(void)
1019 devfreq_class = class_create(THIS_MODULE, "devfreq");
1020 if (IS_ERR(devfreq_class)) {
1021 pr_err("%s: couldn't create class\n", __FILE__);
1022 return PTR_ERR(devfreq_class);
1025 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1026 if (IS_ERR(devfreq_wq)) {
1027 class_destroy(devfreq_class);
1028 pr_err("%s: couldn't create workqueue\n", __FILE__);
1029 return PTR_ERR(devfreq_wq);
1031 devfreq_class->dev_attrs = devfreq_attrs;
1035 subsys_initcall(devfreq_init);
1037 static void __exit devfreq_exit(void)
1039 class_destroy(devfreq_class);
1040 destroy_workqueue(devfreq_wq);
1042 module_exit(devfreq_exit);
1045 * The followings are helper functions for devfreq user device drivers with
1050 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1051 * freq value given to target callback.
1052 * @dev: The devfreq user device. (parent of devfreq)
1053 * @freq: The frequency given to target function
1054 * @flags: Flags handed from devfreq framework.
1056 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1057 * protected pointer. The reason for the same is that the opp pointer which is
1058 * returned will remain valid for use with opp_get_{voltage, freq} only while
1059 * under the locked area. The pointer returned must be used prior to unlocking
1060 * with rcu_read_unlock() to maintain the integrity of the pointer.
1062 struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
1067 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1068 /* The freq is an upper bound. opp should be lower */
1069 opp = opp_find_freq_floor(dev, freq);
1071 /* If not available, use the closest opp */
1072 if (opp == ERR_PTR(-ERANGE))
1073 opp = opp_find_freq_ceil(dev, freq);
1075 /* The freq is an lower bound. opp should be higher */
1076 opp = opp_find_freq_ceil(dev, freq);
1078 /* If not available, use the closest opp */
1079 if (opp == ERR_PTR(-ERANGE))
1080 opp = opp_find_freq_floor(dev, freq);
1087 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1088 * for any changes in the OPP availability
1090 * @dev: The devfreq user device. (parent of devfreq)
1091 * @devfreq: The devfreq object.
1093 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1095 struct srcu_notifier_head *nh;
1099 nh = opp_get_notifier(dev);
1104 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1110 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1111 * notified for any changes in the OPP
1112 * availability changes anymore.
1113 * @dev: The devfreq user device. (parent of devfreq)
1114 * @devfreq: The devfreq object.
1116 * At exit() callback of devfreq_dev_profile, this must be included if
1117 * devfreq_recommended_opp is used.
1119 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1121 struct srcu_notifier_head *nh;
1125 nh = opp_get_notifier(dev);
1130 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1135 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1136 MODULE_DESCRIPTION("devfreq class support");
1137 MODULE_LICENSE("GPL");