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/export.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/pm_opp.h>
22 #include <linux/devfreq.h>
23 #include <linux/workqueue.h>
24 #include <linux/platform_device.h>
25 #include <linux/list.h>
26 #include <linux/printk.h>
27 #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 (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 for (lev = 0; lev < devfreq->profile->max_state; lev++)
82 if (freq == devfreq->profile->freq_table[lev])
89 * devfreq_set_freq_table() - Initialize freq_table for the frequency
90 * @devfreq: the devfreq instance
92 static void devfreq_set_freq_table(struct devfreq *devfreq)
94 struct devfreq_dev_profile *profile = devfreq->profile;
95 struct dev_pm_opp *opp;
99 /* Initialize the freq_table from OPP table */
100 count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
104 profile->max_state = count;
105 profile->freq_table = devm_kcalloc(devfreq->dev.parent,
107 sizeof(*profile->freq_table),
109 if (!profile->freq_table) {
110 profile->max_state = 0;
115 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
116 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
118 devm_kfree(devfreq->dev.parent, profile->freq_table);
119 profile->max_state = 0;
123 profile->freq_table[i] = freq;
129 * devfreq_update_status() - Update statistics of devfreq behavior
130 * @devfreq: the devfreq instance
131 * @freq: the update target frequency
133 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
135 int lev, prev_lev, ret = 0;
136 unsigned long cur_time;
140 /* Immediately exit if previous_freq is not initialized yet. */
141 if (!devfreq->previous_freq)
144 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
150 devfreq->time_in_state[prev_lev] +=
151 cur_time - devfreq->last_stat_updated;
153 lev = devfreq_get_freq_level(devfreq, freq);
159 if (lev != prev_lev) {
160 devfreq->trans_table[(prev_lev *
161 devfreq->profile->max_state) + lev]++;
162 devfreq->total_trans++;
166 devfreq->last_stat_updated = cur_time;
169 EXPORT_SYMBOL(devfreq_update_status);
172 * find_devfreq_governor() - find devfreq governor from name
173 * @name: name of the governor
175 * Search the list of devfreq governors and return the matched
176 * governor's pointer. devfreq_list_lock should be held by the caller.
178 static struct devfreq_governor *find_devfreq_governor(const char *name)
180 struct devfreq_governor *tmp_governor;
182 if (IS_ERR_OR_NULL(name)) {
183 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
184 return ERR_PTR(-EINVAL);
186 WARN(!mutex_is_locked(&devfreq_list_lock),
187 "devfreq_list_lock must be locked.");
189 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
190 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
194 return ERR_PTR(-ENODEV);
197 static int devfreq_notify_transition(struct devfreq *devfreq,
198 struct devfreq_freqs *freqs, unsigned int state)
204 case DEVFREQ_PRECHANGE:
205 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
206 DEVFREQ_PRECHANGE, freqs);
209 case DEVFREQ_POSTCHANGE:
210 srcu_notifier_call_chain(&devfreq->transition_notifier_list,
211 DEVFREQ_POSTCHANGE, freqs);
220 /* Load monitoring helper functions for governors use */
223 * update_devfreq() - Reevaluate the device and configure frequency.
224 * @devfreq: the devfreq instance.
226 * Note: Lock devfreq->lock before calling update_devfreq
227 * This function is exported for governors.
229 int update_devfreq(struct devfreq *devfreq)
231 struct devfreq_freqs freqs;
232 unsigned long freq, cur_freq;
236 if (!mutex_is_locked(&devfreq->lock)) {
237 WARN(true, "devfreq->lock must be locked by the caller.\n");
241 if (!devfreq->governor)
244 /* Reevaluate the proper frequency */
245 err = devfreq->governor->get_target_freq(devfreq, &freq);
250 * Adjust the frequency with user freq and QoS.
252 * List from the highest priority
257 if (devfreq->min_freq && freq < devfreq->min_freq) {
258 freq = devfreq->min_freq;
259 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
261 if (devfreq->max_freq && freq > devfreq->max_freq) {
262 freq = devfreq->max_freq;
263 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
266 if (devfreq->profile->get_cur_freq)
267 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
269 cur_freq = devfreq->previous_freq;
271 freqs.old = cur_freq;
273 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
275 err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
277 freqs.new = cur_freq;
278 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
283 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
285 if (devfreq->profile->freq_table)
286 if (devfreq_update_status(devfreq, freq))
287 dev_err(&devfreq->dev,
288 "Couldn't update frequency transition information.\n");
290 devfreq->previous_freq = freq;
293 EXPORT_SYMBOL(update_devfreq);
296 * devfreq_monitor() - Periodically poll devfreq objects.
297 * @work: the work struct used to run devfreq_monitor periodically.
300 static void devfreq_monitor(struct work_struct *work)
303 struct devfreq *devfreq = container_of(work,
304 struct devfreq, work.work);
306 mutex_lock(&devfreq->lock);
307 err = update_devfreq(devfreq);
309 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
311 queue_delayed_work(devfreq_wq, &devfreq->work,
312 msecs_to_jiffies(devfreq->profile->polling_ms));
313 mutex_unlock(&devfreq->lock);
317 * devfreq_monitor_start() - Start load monitoring of devfreq instance
318 * @devfreq: the devfreq instance.
320 * Helper function for starting devfreq device load monitoing. By
321 * default delayed work based monitoring is supported. Function
322 * to be called from governor in response to DEVFREQ_GOV_START
323 * event when device is added to devfreq framework.
325 void devfreq_monitor_start(struct devfreq *devfreq)
327 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
328 if (devfreq->profile->polling_ms)
329 queue_delayed_work(devfreq_wq, &devfreq->work,
330 msecs_to_jiffies(devfreq->profile->polling_ms));
332 EXPORT_SYMBOL(devfreq_monitor_start);
335 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
336 * @devfreq: the devfreq instance.
338 * Helper function to stop devfreq device load monitoing. Function
339 * to be called from governor in response to DEVFREQ_GOV_STOP
340 * event when device is removed from devfreq framework.
342 void devfreq_monitor_stop(struct devfreq *devfreq)
344 cancel_delayed_work_sync(&devfreq->work);
346 EXPORT_SYMBOL(devfreq_monitor_stop);
349 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
350 * @devfreq: the devfreq instance.
352 * Helper function to suspend devfreq device load monitoing. Function
353 * to be called from governor in response to DEVFREQ_GOV_SUSPEND
354 * event or when polling interval is set to zero.
356 * Note: Though this function is same as devfreq_monitor_stop(),
357 * intentionally kept separate to provide hooks for collecting
358 * transition statistics.
360 void devfreq_monitor_suspend(struct devfreq *devfreq)
362 mutex_lock(&devfreq->lock);
363 if (devfreq->stop_polling) {
364 mutex_unlock(&devfreq->lock);
368 devfreq_update_status(devfreq, devfreq->previous_freq);
369 devfreq->stop_polling = true;
370 mutex_unlock(&devfreq->lock);
371 cancel_delayed_work_sync(&devfreq->work);
373 EXPORT_SYMBOL(devfreq_monitor_suspend);
376 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
377 * @devfreq: the devfreq instance.
379 * Helper function to resume devfreq device load monitoing. Function
380 * to be called from governor in response to DEVFREQ_GOV_RESUME
381 * event or when polling interval is set to non-zero.
383 void devfreq_monitor_resume(struct devfreq *devfreq)
387 mutex_lock(&devfreq->lock);
388 if (!devfreq->stop_polling)
391 if (!delayed_work_pending(&devfreq->work) &&
392 devfreq->profile->polling_ms)
393 queue_delayed_work(devfreq_wq, &devfreq->work,
394 msecs_to_jiffies(devfreq->profile->polling_ms));
396 devfreq->last_stat_updated = jiffies;
397 devfreq->stop_polling = false;
399 if (devfreq->profile->get_cur_freq &&
400 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
401 devfreq->previous_freq = freq;
404 mutex_unlock(&devfreq->lock);
406 EXPORT_SYMBOL(devfreq_monitor_resume);
409 * devfreq_interval_update() - Update device devfreq monitoring interval
410 * @devfreq: the devfreq instance.
411 * @delay: new polling interval to be set.
413 * Helper function to set new load monitoring polling interval. Function
414 * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
416 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
418 unsigned int cur_delay = devfreq->profile->polling_ms;
419 unsigned int new_delay = *delay;
421 mutex_lock(&devfreq->lock);
422 devfreq->profile->polling_ms = new_delay;
424 if (devfreq->stop_polling)
427 /* if new delay is zero, stop polling */
429 mutex_unlock(&devfreq->lock);
430 cancel_delayed_work_sync(&devfreq->work);
434 /* if current delay is zero, start polling with new delay */
436 queue_delayed_work(devfreq_wq, &devfreq->work,
437 msecs_to_jiffies(devfreq->profile->polling_ms));
441 /* if current delay is greater than new delay, restart polling */
442 if (cur_delay > new_delay) {
443 mutex_unlock(&devfreq->lock);
444 cancel_delayed_work_sync(&devfreq->work);
445 mutex_lock(&devfreq->lock);
446 if (!devfreq->stop_polling)
447 queue_delayed_work(devfreq_wq, &devfreq->work,
448 msecs_to_jiffies(devfreq->profile->polling_ms));
451 mutex_unlock(&devfreq->lock);
453 EXPORT_SYMBOL(devfreq_interval_update);
456 * devfreq_notifier_call() - Notify that the device frequency requirements
457 * has been changed out of devfreq framework.
458 * @nb: the notifier_block (supposed to be devfreq->nb)
462 * Called by a notifier that uses devfreq->nb.
464 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
467 struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
470 mutex_lock(&devfreq->lock);
471 ret = update_devfreq(devfreq);
472 mutex_unlock(&devfreq->lock);
478 * devfreq_dev_release() - Callback for struct device to release the device.
479 * @dev: the devfreq device
481 * Remove devfreq from the list and release its resources.
483 static void devfreq_dev_release(struct device *dev)
485 struct devfreq *devfreq = to_devfreq(dev);
487 mutex_lock(&devfreq_list_lock);
488 if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
489 mutex_unlock(&devfreq_list_lock);
490 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
493 list_del(&devfreq->node);
494 mutex_unlock(&devfreq_list_lock);
496 if (devfreq->governor)
497 devfreq->governor->event_handler(devfreq,
498 DEVFREQ_GOV_STOP, NULL);
500 if (devfreq->profile->exit)
501 devfreq->profile->exit(devfreq->dev.parent);
503 mutex_destroy(&devfreq->lock);
508 * devfreq_add_device() - Add devfreq feature to the device
509 * @dev: the device to add devfreq feature.
510 * @profile: device-specific profile to run devfreq.
511 * @governor_name: name of the policy to choose frequency.
512 * @data: private data for the governor. The devfreq framework does not
515 struct devfreq *devfreq_add_device(struct device *dev,
516 struct devfreq_dev_profile *profile,
517 const char *governor_name,
520 struct devfreq *devfreq;
521 struct devfreq_governor *governor;
524 if (!dev || !profile || !governor_name) {
525 dev_err(dev, "%s: Invalid parameters.\n", __func__);
526 return ERR_PTR(-EINVAL);
529 mutex_lock(&devfreq_list_lock);
530 devfreq = find_device_devfreq(dev);
531 mutex_unlock(&devfreq_list_lock);
532 if (!IS_ERR(devfreq)) {
533 dev_err(dev, "%s: Unable to create devfreq for the device.\n",
539 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
545 mutex_init(&devfreq->lock);
546 mutex_lock(&devfreq->lock);
547 devfreq->dev.parent = dev;
548 devfreq->dev.class = devfreq_class;
549 devfreq->dev.release = devfreq_dev_release;
550 devfreq->profile = profile;
551 strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
552 devfreq->previous_freq = profile->initial_freq;
553 devfreq->last_status.current_frequency = profile->initial_freq;
554 devfreq->data = data;
555 devfreq->nb.notifier_call = devfreq_notifier_call;
557 if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
558 mutex_unlock(&devfreq->lock);
559 devfreq_set_freq_table(devfreq);
560 mutex_lock(&devfreq->lock);
563 dev_set_name(&devfreq->dev, "%s", dev_name(dev));
564 err = device_register(&devfreq->dev);
566 mutex_unlock(&devfreq->lock);
570 devfreq->trans_table = devm_kzalloc(&devfreq->dev,
571 sizeof(unsigned int) *
572 devfreq->profile->max_state *
573 devfreq->profile->max_state,
575 devfreq->time_in_state = devm_kzalloc(&devfreq->dev,
576 sizeof(unsigned long) *
577 devfreq->profile->max_state,
579 devfreq->last_stat_updated = jiffies;
581 srcu_init_notifier_head(&devfreq->transition_notifier_list);
583 mutex_unlock(&devfreq->lock);
585 mutex_lock(&devfreq_list_lock);
586 list_add(&devfreq->node, &devfreq_list);
588 governor = find_devfreq_governor(devfreq->governor_name);
589 if (IS_ERR(governor)) {
590 dev_err(dev, "%s: Unable to find governor for the device\n",
592 err = PTR_ERR(governor);
596 devfreq->governor = governor;
597 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
600 dev_err(dev, "%s: Unable to start governor for the device\n",
604 mutex_unlock(&devfreq_list_lock);
609 list_del(&devfreq->node);
610 mutex_unlock(&devfreq_list_lock);
612 device_unregister(&devfreq->dev);
616 EXPORT_SYMBOL(devfreq_add_device);
619 * devfreq_remove_device() - Remove devfreq feature from a device.
620 * @devfreq: the devfreq instance to be removed
622 * The opposite of devfreq_add_device().
624 int devfreq_remove_device(struct devfreq *devfreq)
629 device_unregister(&devfreq->dev);
633 EXPORT_SYMBOL(devfreq_remove_device);
635 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
637 struct devfreq **r = res;
639 if (WARN_ON(!r || !*r))
645 static void devm_devfreq_dev_release(struct device *dev, void *res)
647 devfreq_remove_device(*(struct devfreq **)res);
651 * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
652 * @dev: the device to add devfreq feature.
653 * @profile: device-specific profile to run devfreq.
654 * @governor_name: name of the policy to choose frequency.
655 * @data: private data for the governor. The devfreq framework does not
658 * This function manages automatically the memory of devfreq device using device
659 * resource management and simplify the free operation for memory of devfreq
662 struct devfreq *devm_devfreq_add_device(struct device *dev,
663 struct devfreq_dev_profile *profile,
664 const char *governor_name,
667 struct devfreq **ptr, *devfreq;
669 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
671 return ERR_PTR(-ENOMEM);
673 devfreq = devfreq_add_device(dev, profile, governor_name, data);
674 if (IS_ERR(devfreq)) {
676 return ERR_PTR(-ENOMEM);
680 devres_add(dev, ptr);
684 EXPORT_SYMBOL(devm_devfreq_add_device);
688 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
689 * @dev - instance to the given device
690 * @index - index into list of devfreq
692 * return the instance of devfreq device
694 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
696 struct device_node *node;
697 struct devfreq *devfreq;
700 return ERR_PTR(-EINVAL);
703 return ERR_PTR(-EINVAL);
705 node = of_parse_phandle(dev->of_node, "devfreq", index);
707 return ERR_PTR(-ENODEV);
709 mutex_lock(&devfreq_list_lock);
710 list_for_each_entry(devfreq, &devfreq_list, node) {
711 if (devfreq->dev.parent
712 && devfreq->dev.parent->of_node == node) {
713 mutex_unlock(&devfreq_list_lock);
718 mutex_unlock(&devfreq_list_lock);
721 return ERR_PTR(-EPROBE_DEFER);
724 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
726 return ERR_PTR(-ENODEV);
728 #endif /* CONFIG_OF */
729 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
732 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
733 * @dev: the device to add devfreq feature.
734 * @devfreq: the devfreq instance to be removed
736 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
738 WARN_ON(devres_release(dev, devm_devfreq_dev_release,
739 devm_devfreq_dev_match, devfreq));
741 EXPORT_SYMBOL(devm_devfreq_remove_device);
744 * devfreq_suspend_device() - Suspend devfreq of a device.
745 * @devfreq: the devfreq instance to be suspended
747 * This function is intended to be called by the pm callbacks
748 * (e.g., runtime_suspend, suspend) of the device driver that
751 int devfreq_suspend_device(struct devfreq *devfreq)
756 if (!devfreq->governor)
759 return devfreq->governor->event_handler(devfreq,
760 DEVFREQ_GOV_SUSPEND, NULL);
762 EXPORT_SYMBOL(devfreq_suspend_device);
765 * devfreq_resume_device() - Resume devfreq of a device.
766 * @devfreq: the devfreq instance to be resumed
768 * This function is intended to be called by the pm callbacks
769 * (e.g., runtime_resume, resume) of the device driver that
772 int devfreq_resume_device(struct devfreq *devfreq)
777 if (!devfreq->governor)
780 return devfreq->governor->event_handler(devfreq,
781 DEVFREQ_GOV_RESUME, NULL);
783 EXPORT_SYMBOL(devfreq_resume_device);
786 * devfreq_add_governor() - Add devfreq governor
787 * @governor: the devfreq governor to be added
789 int devfreq_add_governor(struct devfreq_governor *governor)
791 struct devfreq_governor *g;
792 struct devfreq *devfreq;
796 pr_err("%s: Invalid parameters.\n", __func__);
800 mutex_lock(&devfreq_list_lock);
801 g = find_devfreq_governor(governor->name);
803 pr_err("%s: governor %s already registered\n", __func__,
809 list_add(&governor->node, &devfreq_governor_list);
811 list_for_each_entry(devfreq, &devfreq_list, node) {
813 struct device *dev = devfreq->dev.parent;
815 if (!strncmp(devfreq->governor_name, governor->name,
817 /* The following should never occur */
818 if (devfreq->governor) {
820 "%s: Governor %s already present\n",
821 __func__, devfreq->governor->name);
822 ret = devfreq->governor->event_handler(devfreq,
823 DEVFREQ_GOV_STOP, NULL);
826 "%s: Governor %s stop = %d\n",
828 devfreq->governor->name, ret);
832 devfreq->governor = governor;
833 ret = devfreq->governor->event_handler(devfreq,
834 DEVFREQ_GOV_START, NULL);
836 dev_warn(dev, "%s: Governor %s start=%d\n",
837 __func__, devfreq->governor->name,
844 mutex_unlock(&devfreq_list_lock);
848 EXPORT_SYMBOL(devfreq_add_governor);
851 * devfreq_remove_governor() - Remove devfreq feature from a device.
852 * @governor: the devfreq governor to be removed
854 int devfreq_remove_governor(struct devfreq_governor *governor)
856 struct devfreq_governor *g;
857 struct devfreq *devfreq;
861 pr_err("%s: Invalid parameters.\n", __func__);
865 mutex_lock(&devfreq_list_lock);
866 g = find_devfreq_governor(governor->name);
868 pr_err("%s: governor %s not registered\n", __func__,
873 list_for_each_entry(devfreq, &devfreq_list, node) {
875 struct device *dev = devfreq->dev.parent;
877 if (!strncmp(devfreq->governor_name, governor->name,
879 /* we should have a devfreq governor! */
880 if (!devfreq->governor) {
881 dev_warn(dev, "%s: Governor %s NOT present\n",
882 __func__, governor->name);
886 ret = devfreq->governor->event_handler(devfreq,
887 DEVFREQ_GOV_STOP, NULL);
889 dev_warn(dev, "%s: Governor %s stop=%d\n",
890 __func__, devfreq->governor->name,
893 devfreq->governor = NULL;
897 list_del(&governor->node);
899 mutex_unlock(&devfreq_list_lock);
903 EXPORT_SYMBOL(devfreq_remove_governor);
905 static ssize_t governor_show(struct device *dev,
906 struct device_attribute *attr, char *buf)
908 if (!to_devfreq(dev)->governor)
911 return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
914 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
915 const char *buf, size_t count)
917 struct devfreq *df = to_devfreq(dev);
919 char str_governor[DEVFREQ_NAME_LEN + 1];
920 struct devfreq_governor *governor;
922 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
926 mutex_lock(&devfreq_list_lock);
927 governor = find_devfreq_governor(str_governor);
928 if (IS_ERR(governor)) {
929 ret = PTR_ERR(governor);
932 if (df->governor == governor) {
935 } else if (df->governor->immutable || governor->immutable) {
941 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
943 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
944 __func__, df->governor->name, ret);
948 df->governor = governor;
949 strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
950 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
952 dev_warn(dev, "%s: Governor %s not started(%d)\n",
953 __func__, df->governor->name, ret);
955 mutex_unlock(&devfreq_list_lock);
961 static DEVICE_ATTR_RW(governor);
963 static ssize_t available_governors_show(struct device *d,
964 struct device_attribute *attr,
967 struct devfreq *df = to_devfreq(d);
970 mutex_lock(&devfreq_list_lock);
973 * The devfreq with immutable governor (e.g., passive) shows
976 if (df->governor->immutable) {
977 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
978 "%s ", df->governor_name);
980 * The devfreq device shows the registered governor except for
981 * immutable governors such as passive governor .
984 struct devfreq_governor *governor;
986 list_for_each_entry(governor, &devfreq_governor_list, node) {
987 if (governor->immutable)
989 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
990 "%s ", governor->name);
994 mutex_unlock(&devfreq_list_lock);
996 /* Truncate the trailing space */
1000 count += sprintf(&buf[count], "\n");
1004 static DEVICE_ATTR_RO(available_governors);
1006 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1010 struct devfreq *devfreq = to_devfreq(dev);
1012 if (devfreq->profile->get_cur_freq &&
1013 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1014 return sprintf(buf, "%lu\n", freq);
1016 return sprintf(buf, "%lu\n", devfreq->previous_freq);
1018 static DEVICE_ATTR_RO(cur_freq);
1020 static ssize_t target_freq_show(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1023 return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1025 static DEVICE_ATTR_RO(target_freq);
1027 static ssize_t polling_interval_show(struct device *dev,
1028 struct device_attribute *attr, char *buf)
1030 return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1033 static ssize_t polling_interval_store(struct device *dev,
1034 struct device_attribute *attr,
1035 const char *buf, size_t count)
1037 struct devfreq *df = to_devfreq(dev);
1044 ret = sscanf(buf, "%u", &value);
1048 df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
1053 static DEVICE_ATTR_RW(polling_interval);
1055 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1056 const char *buf, size_t count)
1058 struct devfreq *df = to_devfreq(dev);
1059 unsigned long value;
1063 ret = sscanf(buf, "%lu", &value);
1067 mutex_lock(&df->lock);
1069 if (value && max && value > max) {
1074 df->min_freq = value;
1078 mutex_unlock(&df->lock);
1082 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1083 const char *buf, size_t count)
1085 struct devfreq *df = to_devfreq(dev);
1086 unsigned long value;
1090 ret = sscanf(buf, "%lu", &value);
1094 mutex_lock(&df->lock);
1096 if (value && min && value < min) {
1101 df->max_freq = value;
1105 mutex_unlock(&df->lock);
1109 #define show_one(name) \
1110 static ssize_t name##_show \
1111 (struct device *dev, struct device_attribute *attr, char *buf) \
1113 return sprintf(buf, "%lu\n", to_devfreq(dev)->name); \
1118 static DEVICE_ATTR_RW(min_freq);
1119 static DEVICE_ATTR_RW(max_freq);
1121 static ssize_t available_frequencies_show(struct device *d,
1122 struct device_attribute *attr,
1125 struct devfreq *df = to_devfreq(d);
1126 struct device *dev = df->dev.parent;
1127 struct dev_pm_opp *opp;
1129 unsigned long freq = 0;
1133 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1137 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1143 /* Truncate the trailing space */
1147 count += sprintf(&buf[count], "\n");
1151 static DEVICE_ATTR_RO(available_frequencies);
1153 static ssize_t trans_stat_show(struct device *dev,
1154 struct device_attribute *attr, char *buf)
1156 struct devfreq *devfreq = to_devfreq(dev);
1159 unsigned int max_state = devfreq->profile->max_state;
1161 if (!devfreq->stop_polling &&
1162 devfreq_update_status(devfreq, devfreq->previous_freq))
1165 return sprintf(buf, "Not Supported.\n");
1167 len = sprintf(buf, " From : To\n");
1168 len += sprintf(buf + len, " :");
1169 for (i = 0; i < max_state; i++)
1170 len += sprintf(buf + len, "%10lu",
1171 devfreq->profile->freq_table[i]);
1173 len += sprintf(buf + len, " time(ms)\n");
1175 for (i = 0; i < max_state; i++) {
1176 if (devfreq->profile->freq_table[i]
1177 == devfreq->previous_freq) {
1178 len += sprintf(buf + len, "*");
1180 len += sprintf(buf + len, " ");
1182 len += sprintf(buf + len, "%10lu:",
1183 devfreq->profile->freq_table[i]);
1184 for (j = 0; j < max_state; j++)
1185 len += sprintf(buf + len, "%10u",
1186 devfreq->trans_table[(i * max_state) + j]);
1187 len += sprintf(buf + len, "%10u\n",
1188 jiffies_to_msecs(devfreq->time_in_state[i]));
1191 len += sprintf(buf + len, "Total transition : %u\n",
1192 devfreq->total_trans);
1195 static DEVICE_ATTR_RO(trans_stat);
1197 static struct attribute *devfreq_attrs[] = {
1198 &dev_attr_governor.attr,
1199 &dev_attr_available_governors.attr,
1200 &dev_attr_cur_freq.attr,
1201 &dev_attr_available_frequencies.attr,
1202 &dev_attr_target_freq.attr,
1203 &dev_attr_polling_interval.attr,
1204 &dev_attr_min_freq.attr,
1205 &dev_attr_max_freq.attr,
1206 &dev_attr_trans_stat.attr,
1209 ATTRIBUTE_GROUPS(devfreq);
1211 static int __init devfreq_init(void)
1213 devfreq_class = class_create(THIS_MODULE, "devfreq");
1214 if (IS_ERR(devfreq_class)) {
1215 pr_err("%s: couldn't create class\n", __FILE__);
1216 return PTR_ERR(devfreq_class);
1219 devfreq_wq = create_freezable_workqueue("devfreq_wq");
1221 class_destroy(devfreq_class);
1222 pr_err("%s: couldn't create workqueue\n", __FILE__);
1225 devfreq_class->dev_groups = devfreq_groups;
1229 subsys_initcall(devfreq_init);
1232 * The followings are helper functions for devfreq user device drivers with
1237 * devfreq_recommended_opp() - Helper function to get proper OPP for the
1238 * freq value given to target callback.
1239 * @dev: The devfreq user device. (parent of devfreq)
1240 * @freq: The frequency given to target function
1241 * @flags: Flags handed from devfreq framework.
1243 * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1244 * protected pointer. The reason for the same is that the opp pointer which is
1245 * returned will remain valid for use with opp_get_{voltage, freq} only while
1246 * under the locked area. The pointer returned must be used prior to unlocking
1247 * with rcu_read_unlock() to maintain the integrity of the pointer.
1249 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1250 unsigned long *freq,
1253 struct dev_pm_opp *opp;
1255 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1256 /* The freq is an upper bound. opp should be lower */
1257 opp = dev_pm_opp_find_freq_floor(dev, freq);
1259 /* If not available, use the closest opp */
1260 if (opp == ERR_PTR(-ERANGE))
1261 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1263 /* The freq is an lower bound. opp should be higher */
1264 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1266 /* If not available, use the closest opp */
1267 if (opp == ERR_PTR(-ERANGE))
1268 opp = dev_pm_opp_find_freq_floor(dev, freq);
1273 EXPORT_SYMBOL(devfreq_recommended_opp);
1276 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1277 * for any changes in the OPP availability
1279 * @dev: The devfreq user device. (parent of devfreq)
1280 * @devfreq: The devfreq object.
1282 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1284 struct srcu_notifier_head *nh;
1288 nh = dev_pm_opp_get_notifier(dev);
1293 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1297 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1300 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1301 * notified for any changes in the OPP
1302 * availability changes anymore.
1303 * @dev: The devfreq user device. (parent of devfreq)
1304 * @devfreq: The devfreq object.
1306 * At exit() callback of devfreq_dev_profile, this must be included if
1307 * devfreq_recommended_opp is used.
1309 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1311 struct srcu_notifier_head *nh;
1315 nh = dev_pm_opp_get_notifier(dev);
1320 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1324 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1326 static void devm_devfreq_opp_release(struct device *dev, void *res)
1328 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1332 * devm_ devfreq_register_opp_notifier()
1333 * - Resource-managed devfreq_register_opp_notifier()
1334 * @dev: The devfreq user device. (parent of devfreq)
1335 * @devfreq: The devfreq object.
1337 int devm_devfreq_register_opp_notifier(struct device *dev,
1338 struct devfreq *devfreq)
1340 struct devfreq **ptr;
1343 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1347 ret = devfreq_register_opp_notifier(dev, devfreq);
1354 devres_add(dev, ptr);
1358 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1361 * devm_devfreq_unregister_opp_notifier()
1362 * - Resource-managed devfreq_unregister_opp_notifier()
1363 * @dev: The devfreq user device. (parent of devfreq)
1364 * @devfreq: The devfreq object.
1366 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1367 struct devfreq *devfreq)
1369 WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1370 devm_devfreq_dev_match, devfreq));
1372 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1375 * devfreq_register_notifier() - Register a driver with devfreq
1376 * @devfreq: The devfreq object.
1377 * @nb: The notifier block to register.
1378 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1380 int devfreq_register_notifier(struct devfreq *devfreq,
1381 struct notifier_block *nb,
1390 case DEVFREQ_TRANSITION_NOTIFIER:
1391 ret = srcu_notifier_chain_register(
1392 &devfreq->transition_notifier_list, nb);
1400 EXPORT_SYMBOL(devfreq_register_notifier);
1403 * devfreq_unregister_notifier() - Unregister a driver with devfreq
1404 * @devfreq: The devfreq object.
1405 * @nb: The notifier block to be unregistered.
1406 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1408 int devfreq_unregister_notifier(struct devfreq *devfreq,
1409 struct notifier_block *nb,
1418 case DEVFREQ_TRANSITION_NOTIFIER:
1419 ret = srcu_notifier_chain_unregister(
1420 &devfreq->transition_notifier_list, nb);
1428 EXPORT_SYMBOL(devfreq_unregister_notifier);
1430 struct devfreq_notifier_devres {
1431 struct devfreq *devfreq;
1432 struct notifier_block *nb;
1436 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1438 struct devfreq_notifier_devres *this = res;
1440 devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1444 * devm_devfreq_register_notifier()
1445 - Resource-managed devfreq_register_notifier()
1446 * @dev: The devfreq user device. (parent of devfreq)
1447 * @devfreq: The devfreq object.
1448 * @nb: The notifier block to be unregistered.
1449 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1451 int devm_devfreq_register_notifier(struct device *dev,
1452 struct devfreq *devfreq,
1453 struct notifier_block *nb,
1456 struct devfreq_notifier_devres *ptr;
1459 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1464 ret = devfreq_register_notifier(devfreq, nb, list);
1470 ptr->devfreq = devfreq;
1473 devres_add(dev, ptr);
1477 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1480 * devm_devfreq_unregister_notifier()
1481 - Resource-managed devfreq_unregister_notifier()
1482 * @dev: The devfreq user device. (parent of devfreq)
1483 * @devfreq: The devfreq object.
1484 * @nb: The notifier block to be unregistered.
1485 * @list: DEVFREQ_TRANSITION_NOTIFIER.
1487 void devm_devfreq_unregister_notifier(struct device *dev,
1488 struct devfreq *devfreq,
1489 struct notifier_block *nb,
1492 WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1493 devm_devfreq_dev_match, devfreq));
1495 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);