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/slab.h>
19 #include <linux/opp.h>
20 #include <linux/devfreq.h>
21 #include <linux/workqueue.h>
22 #include <linux/platform_device.h>
23 #include <linux/list.h>
24 #include <linux/printk.h>
25 #include <linux/hrtimer.h>
28 struct class *devfreq_class
;
31 * devfreq_work periodically monitors every registered device.
32 * The minimum polling interval is one jiffy. The polling interval is
33 * determined by the minimum polling period among all polling devfreq
34 * devices. The resolution of polling interval is one jiffy.
37 static struct workqueue_struct
*devfreq_wq
;
38 static struct delayed_work devfreq_work
;
40 /* wait removing if this is to be removed */
41 static struct devfreq
*wait_remove_device
;
43 /* The list of all device-devfreq */
44 static LIST_HEAD(devfreq_list
);
45 static DEFINE_MUTEX(devfreq_list_lock
);
48 * find_device_devfreq() - find devfreq struct using device pointer
49 * @dev: device pointer used to lookup device devfreq.
51 * Search the list of device devfreqs and return the matched device's
52 * devfreq info. devfreq_list_lock should be held by the caller.
54 static struct devfreq
*find_device_devfreq(struct device
*dev
)
56 struct devfreq
*tmp_devfreq
;
58 if (unlikely(IS_ERR_OR_NULL(dev
))) {
59 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__
);
60 return ERR_PTR(-EINVAL
);
62 WARN(!mutex_is_locked(&devfreq_list_lock
),
63 "devfreq_list_lock must be locked.");
65 list_for_each_entry(tmp_devfreq
, &devfreq_list
, node
) {
66 if (tmp_devfreq
->dev
.parent
== dev
)
70 return ERR_PTR(-ENODEV
);
74 * update_devfreq() - Reevaluate the device and configure frequency.
75 * @devfreq: the devfreq instance.
77 * Note: Lock devfreq->lock before calling update_devfreq
78 * This function is exported for governors.
80 int update_devfreq(struct devfreq
*devfreq
)
85 if (!mutex_is_locked(&devfreq
->lock
)) {
86 WARN(true, "devfreq->lock must be locked by the caller.\n");
90 /* Reevaluate the proper frequency */
91 err
= devfreq
->governor
->get_target_freq(devfreq
, &freq
);
95 err
= devfreq
->profile
->target(devfreq
->dev
.parent
, &freq
);
99 devfreq
->previous_freq
= freq
;
104 * devfreq_notifier_call() - Notify that the device frequency requirements
105 * has been changed out of devfreq framework.
106 * @nb the notifier_block (supposed to be devfreq->nb)
110 * Called by a notifier that uses devfreq->nb.
112 static int devfreq_notifier_call(struct notifier_block
*nb
, unsigned long type
,
115 struct devfreq
*devfreq
= container_of(nb
, struct devfreq
, nb
);
118 mutex_lock(&devfreq
->lock
);
119 ret
= update_devfreq(devfreq
);
120 mutex_unlock(&devfreq
->lock
);
126 * _remove_devfreq() - Remove devfreq from the device.
127 * @devfreq: the devfreq struct
128 * @skip: skip calling device_unregister().
130 * Note that the caller should lock devfreq->lock before calling
131 * this. _remove_devfreq() will unlock it and free devfreq
132 * internally. devfreq_list_lock should be locked by the caller
133 * as well (not relased at return)
136 * devfreq->lock: locked before call.
137 * unlocked at return (and freed)
138 * devfreq_list_lock: locked before call.
139 * kept locked at return.
140 * if devfreq is centrally polled.
145 static void _remove_devfreq(struct devfreq
*devfreq
, bool skip
)
147 if (!mutex_is_locked(&devfreq
->lock
)) {
148 WARN(true, "devfreq->lock must be locked by the caller.\n");
151 if (!devfreq
->governor
->no_central_polling
&&
152 !mutex_is_locked(&devfreq_list_lock
)) {
153 WARN(true, "devfreq_list_lock must be locked by the caller.\n");
157 if (devfreq
->being_removed
)
160 devfreq
->being_removed
= true;
162 if (devfreq
->profile
->exit
)
163 devfreq
->profile
->exit(devfreq
->dev
.parent
);
165 if (devfreq
->governor
->exit
)
166 devfreq
->governor
->exit(devfreq
);
168 if (!skip
&& get_device(&devfreq
->dev
)) {
169 device_unregister(&devfreq
->dev
);
170 put_device(&devfreq
->dev
);
173 if (!devfreq
->governor
->no_central_polling
)
174 list_del(&devfreq
->node
);
176 mutex_unlock(&devfreq
->lock
);
177 mutex_destroy(&devfreq
->lock
);
183 * devfreq_dev_release() - Callback for struct device to release the device.
184 * @dev: the devfreq device
186 * This calls _remove_devfreq() if _remove_devfreq() is not called.
187 * Note that devfreq_dev_release() could be called by _remove_devfreq() as
188 * well as by others unregistering the device.
190 static void devfreq_dev_release(struct device
*dev
)
192 struct devfreq
*devfreq
= to_devfreq(dev
);
193 bool central_polling
= !devfreq
->governor
->no_central_polling
;
196 * If devfreq_dev_release() was called by device_unregister() of
197 * _remove_devfreq(), we cannot mutex_lock(&devfreq->lock) and
198 * being_removed is already set. This also partially checks the case
199 * where devfreq_dev_release() is called from a thread other than
200 * the one called _remove_devfreq(); however, this case is
201 * dealt completely with another following being_removed check.
203 * Because being_removed is never being
204 * unset, we do not need to worry about race conditions on
207 if (devfreq
->being_removed
)
211 mutex_lock(&devfreq_list_lock
);
213 mutex_lock(&devfreq
->lock
);
216 * Check being_removed flag again for the case where
217 * devfreq_dev_release() was called in a thread other than the one
218 * possibly called _remove_devfreq().
220 if (devfreq
->being_removed
) {
221 mutex_unlock(&devfreq
->lock
);
225 /* devfreq->lock is unlocked and removed in _removed_devfreq() */
226 _remove_devfreq(devfreq
, true);
230 mutex_unlock(&devfreq_list_lock
);
234 * devfreq_monitor() - Periodically poll devfreq objects.
235 * @work: the work struct used to run devfreq_monitor periodically.
238 static void devfreq_monitor(struct work_struct
*work
)
240 static unsigned long last_polled_at
;
241 struct devfreq
*devfreq
, *tmp
;
243 unsigned long jiffies_passed
;
244 unsigned long next_jiffies
= ULONG_MAX
, now
= jiffies
;
247 /* Initially last_polled_at = 0, polling every device at bootup */
248 jiffies_passed
= now
- last_polled_at
;
249 last_polled_at
= now
;
250 if (jiffies_passed
== 0)
253 mutex_lock(&devfreq_list_lock
);
254 list_for_each_entry_safe(devfreq
, tmp
, &devfreq_list
, node
) {
255 mutex_lock(&devfreq
->lock
);
256 dev
= devfreq
->dev
.parent
;
258 /* Do not remove tmp for a while */
259 wait_remove_device
= tmp
;
261 if (devfreq
->governor
->no_central_polling
||
262 devfreq
->next_polling
== 0) {
263 mutex_unlock(&devfreq
->lock
);
266 mutex_unlock(&devfreq_list_lock
);
269 * Reduce more next_polling if devfreq_wq took an extra
270 * delay. (i.e., CPU has been idled.)
272 if (devfreq
->next_polling
<= jiffies_passed
) {
273 error
= update_devfreq(devfreq
);
275 /* Remove a devfreq with an error. */
276 if (error
&& error
!= -EAGAIN
) {
278 dev_err(dev
, "Due to update_devfreq error(%d), devfreq(%s) is removed from the device\n",
279 error
, devfreq
->governor
->name
);
282 * Unlock devfreq before locking the list
283 * in order to avoid deadlock with
284 * find_device_devfreq or others
286 mutex_unlock(&devfreq
->lock
);
287 mutex_lock(&devfreq_list_lock
);
288 /* Check if devfreq is already removed */
289 if (IS_ERR(find_device_devfreq(dev
)))
291 mutex_lock(&devfreq
->lock
);
292 /* This unlocks devfreq->lock and free it */
293 _remove_devfreq(devfreq
, false);
296 devfreq
->next_polling
= devfreq
->polling_jiffies
;
298 devfreq
->next_polling
-= jiffies_passed
;
301 if (devfreq
->next_polling
)
302 next_jiffies
= (next_jiffies
> devfreq
->next_polling
) ?
303 devfreq
->next_polling
: next_jiffies
;
305 mutex_unlock(&devfreq
->lock
);
306 mutex_lock(&devfreq_list_lock
);
308 wait_remove_device
= NULL
;
309 mutex_unlock(&devfreq_list_lock
);
311 if (next_jiffies
> 0 && next_jiffies
< ULONG_MAX
) {
313 queue_delayed_work(devfreq_wq
, &devfreq_work
, next_jiffies
);
320 * devfreq_add_device() - Add devfreq feature to the device
321 * @dev: the device to add devfreq feature.
322 * @profile: device-specific profile to run devfreq.
323 * @governor: the policy to choose frequency.
324 * @data: private data for the governor. The devfreq framework does not
327 struct devfreq
*devfreq_add_device(struct device
*dev
,
328 struct devfreq_dev_profile
*profile
,
329 const struct devfreq_governor
*governor
,
332 struct devfreq
*devfreq
;
335 if (!dev
|| !profile
|| !governor
) {
336 dev_err(dev
, "%s: Invalid parameters.\n", __func__
);
337 return ERR_PTR(-EINVAL
);
341 if (!governor
->no_central_polling
) {
342 mutex_lock(&devfreq_list_lock
);
343 devfreq
= find_device_devfreq(dev
);
344 mutex_unlock(&devfreq_list_lock
);
345 if (!IS_ERR(devfreq
)) {
346 dev_err(dev
, "%s: Unable to create devfreq for the device. It already has one.\n", __func__
);
352 devfreq
= kzalloc(sizeof(struct devfreq
), GFP_KERNEL
);
354 dev_err(dev
, "%s: Unable to create devfreq for the device\n",
360 mutex_init(&devfreq
->lock
);
361 mutex_lock(&devfreq
->lock
);
362 devfreq
->dev
.parent
= dev
;
363 devfreq
->dev
.class = devfreq_class
;
364 devfreq
->dev
.release
= devfreq_dev_release
;
365 devfreq
->profile
= profile
;
366 devfreq
->governor
= governor
;
367 devfreq
->previous_freq
= profile
->initial_freq
;
368 devfreq
->data
= data
;
369 devfreq
->next_polling
= devfreq
->polling_jiffies
370 = msecs_to_jiffies(devfreq
->profile
->polling_ms
);
371 devfreq
->nb
.notifier_call
= devfreq_notifier_call
;
373 dev_set_name(&devfreq
->dev
, dev_name(dev
));
374 err
= device_register(&devfreq
->dev
);
376 put_device(&devfreq
->dev
);
381 err
= governor
->init(devfreq
);
385 mutex_unlock(&devfreq
->lock
);
387 if (governor
->no_central_polling
)
390 mutex_lock(&devfreq_list_lock
);
392 list_add(&devfreq
->node
, &devfreq_list
);
394 if (devfreq_wq
&& devfreq
->next_polling
&& !polling
) {
396 queue_delayed_work(devfreq_wq
, &devfreq_work
,
397 devfreq
->next_polling
);
399 mutex_unlock(&devfreq_list_lock
);
402 device_unregister(&devfreq
->dev
);
404 mutex_unlock(&devfreq
->lock
);
414 * devfreq_remove_device() - Remove devfreq feature from a device.
415 * @devfreq the devfreq instance to be removed
417 int devfreq_remove_device(struct devfreq
*devfreq
)
422 if (!devfreq
->governor
->no_central_polling
) {
423 mutex_lock(&devfreq_list_lock
);
424 while (wait_remove_device
== devfreq
) {
425 mutex_unlock(&devfreq_list_lock
);
427 mutex_lock(&devfreq_list_lock
);
431 mutex_lock(&devfreq
->lock
);
432 _remove_devfreq(devfreq
, false); /* it unlocks devfreq->lock */
434 if (!devfreq
->governor
->no_central_polling
)
435 mutex_unlock(&devfreq_list_lock
);
440 static ssize_t
show_governor(struct device
*dev
,
441 struct device_attribute
*attr
, char *buf
)
443 return sprintf(buf
, "%s\n", to_devfreq(dev
)->governor
->name
);
446 static ssize_t
show_freq(struct device
*dev
,
447 struct device_attribute
*attr
, char *buf
)
449 return sprintf(buf
, "%lu\n", to_devfreq(dev
)->previous_freq
);
452 static ssize_t
show_polling_interval(struct device
*dev
,
453 struct device_attribute
*attr
, char *buf
)
455 return sprintf(buf
, "%d\n", to_devfreq(dev
)->profile
->polling_ms
);
458 static ssize_t
store_polling_interval(struct device
*dev
,
459 struct device_attribute
*attr
,
460 const char *buf
, size_t count
)
462 struct devfreq
*df
= to_devfreq(dev
);
466 ret
= sscanf(buf
, "%u", &value
);
470 mutex_lock(&df
->lock
);
471 df
->profile
->polling_ms
= value
;
472 df
->next_polling
= df
->polling_jiffies
473 = msecs_to_jiffies(value
);
474 mutex_unlock(&df
->lock
);
478 if (df
->governor
->no_central_polling
)
481 mutex_lock(&devfreq_list_lock
);
482 if (df
->next_polling
> 0 && !polling
) {
484 queue_delayed_work(devfreq_wq
, &devfreq_work
,
487 mutex_unlock(&devfreq_list_lock
);
492 static ssize_t
show_central_polling(struct device
*dev
,
493 struct device_attribute
*attr
, char *buf
)
495 return sprintf(buf
, "%d\n",
496 !to_devfreq(dev
)->governor
->no_central_polling
);
499 static struct device_attribute devfreq_attrs
[] = {
500 __ATTR(governor
, S_IRUGO
, show_governor
, NULL
),
501 __ATTR(cur_freq
, S_IRUGO
, show_freq
, NULL
),
502 __ATTR(central_polling
, S_IRUGO
, show_central_polling
, NULL
),
503 __ATTR(polling_interval
, S_IRUGO
| S_IWUSR
, show_polling_interval
,
504 store_polling_interval
),
509 * devfreq_start_polling() - Initialize data structure for devfreq framework and
510 * start polling registered devfreq devices.
512 static int __init
devfreq_start_polling(void)
514 mutex_lock(&devfreq_list_lock
);
516 devfreq_wq
= create_freezable_workqueue("devfreq_wq");
517 INIT_DELAYED_WORK_DEFERRABLE(&devfreq_work
, devfreq_monitor
);
518 mutex_unlock(&devfreq_list_lock
);
520 devfreq_monitor(&devfreq_work
.work
);
523 late_initcall(devfreq_start_polling
);
525 static int __init
devfreq_init(void)
527 devfreq_class
= class_create(THIS_MODULE
, "devfreq");
528 if (IS_ERR(devfreq_class
)) {
529 pr_err("%s: couldn't create class\n", __FILE__
);
530 return PTR_ERR(devfreq_class
);
532 devfreq_class
->dev_attrs
= devfreq_attrs
;
535 subsys_initcall(devfreq_init
);
537 static void __exit
devfreq_exit(void)
539 class_destroy(devfreq_class
);
541 module_exit(devfreq_exit
);
544 * The followings are helper functions for devfreq user device drivers with
549 * devfreq_recommended_opp() - Helper function to get proper OPP for the
550 * freq value given to target callback.
551 * @dev The devfreq user device. (parent of devfreq)
552 * @freq The frequency given to target function
555 struct opp
*devfreq_recommended_opp(struct device
*dev
, unsigned long *freq
)
557 struct opp
*opp
= opp_find_freq_ceil(dev
, freq
);
559 if (opp
== ERR_PTR(-ENODEV
))
560 opp
= opp_find_freq_floor(dev
, freq
);
565 * devfreq_register_opp_notifier() - Helper function to get devfreq notified
566 * for any changes in the OPP availability
568 * @dev The devfreq user device. (parent of devfreq)
569 * @devfreq The devfreq object.
571 int devfreq_register_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
573 struct srcu_notifier_head
*nh
= opp_get_notifier(dev
);
577 return srcu_notifier_chain_register(nh
, &devfreq
->nb
);
581 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
582 * notified for any changes in the OPP
583 * availability changes anymore.
584 * @dev The devfreq user device. (parent of devfreq)
585 * @devfreq The devfreq object.
587 * At exit() callback of devfreq_dev_profile, this must be included if
588 * devfreq_recommended_opp is used.
590 int devfreq_unregister_opp_notifier(struct device
*dev
, struct devfreq
*devfreq
)
592 struct srcu_notifier_head
*nh
= opp_get_notifier(dev
);
596 return srcu_notifier_chain_unregister(nh
, &devfreq
->nb
);
599 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
600 MODULE_DESCRIPTION("devfreq class support");
601 MODULE_LICENSE("GPL");