]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/devfreq/devfreq.c
Merge remote-tracking branch 'signal/for-next'
[karo-tx-linux.git] / drivers / devfreq / devfreq.c
1 /*
2  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
3  *          for Non-CPU Devices.
4  *
5  * Copyright (C) 2011 Samsung Electronics
6  *      MyungJoo Ham <myungjoo.ham@samsung.com>
7  *
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.
11  */
12
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>
28 #include "governor.h"
29
30 static struct class *devfreq_class;
31
32 /*
33  * devfreq core provides delayed work based load monitoring helper
34  * functions. Governors can use these or can implement their own
35  * monitoring mechanism.
36  */
37 static struct workqueue_struct *devfreq_wq;
38
39 /* The list of all device-devfreq */
40 static LIST_HEAD(devfreq_list);
41 static DEFINE_MUTEX(devfreq_list_lock);
42
43 /**
44  * find_device_devfreq() - find devfreq struct using device pointer
45  * @dev:        device pointer used to lookup device devfreq.
46  *
47  * Search the list of device devfreqs and return the matched device's
48  * devfreq info. devfreq_list_lock should be held by the caller.
49  */
50 static struct devfreq *find_device_devfreq(struct device *dev)
51 {
52         struct devfreq *tmp_devfreq;
53
54         if (unlikely(IS_ERR_OR_NULL(dev))) {
55                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
56                 return ERR_PTR(-EINVAL);
57         }
58         WARN(!mutex_is_locked(&devfreq_list_lock),
59              "devfreq_list_lock must be locked.");
60
61         list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
62                 if (tmp_devfreq->dev.parent == dev)
63                         return tmp_devfreq;
64         }
65
66         return ERR_PTR(-ENODEV);
67 }
68
69 /* Load monitoring helper functions for governors use */
70
71 /**
72  * update_devfreq() - Reevaluate the device and configure frequency.
73  * @devfreq:    the devfreq instance.
74  *
75  * Note: Lock devfreq->lock before calling update_devfreq
76  *       This function is exported for governors.
77  */
78 int update_devfreq(struct devfreq *devfreq)
79 {
80         unsigned long freq;
81         int err = 0;
82         u32 flags = 0;
83
84         if (!mutex_is_locked(&devfreq->lock)) {
85                 WARN(true, "devfreq->lock must be locked by the caller.\n");
86                 return -EINVAL;
87         }
88
89         /* Reevaluate the proper frequency */
90         err = devfreq->governor->get_target_freq(devfreq, &freq);
91         if (err)
92                 return err;
93
94         /*
95          * Adjust the freuqency with user freq and QoS.
96          *
97          * List from the highest proiority
98          * max_freq (probably called by thermal when it's too hot)
99          * min_freq
100          */
101
102         if (devfreq->min_freq && freq < devfreq->min_freq) {
103                 freq = devfreq->min_freq;
104                 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
105         }
106         if (devfreq->max_freq && freq > devfreq->max_freq) {
107                 freq = devfreq->max_freq;
108                 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
109         }
110
111         err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
112         if (err)
113                 return err;
114
115         devfreq->previous_freq = freq;
116         return err;
117 }
118
119 /**
120  * devfreq_monitor() - Periodically poll devfreq objects.
121  * @work:       the work struct used to run devfreq_monitor periodically.
122  *
123  */
124 static void devfreq_monitor(struct work_struct *work)
125 {
126         int err;
127         struct devfreq *devfreq = container_of(work,
128                                         struct devfreq, work.work);
129
130         mutex_lock(&devfreq->lock);
131         err = update_devfreq(devfreq);
132         if (err)
133                 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
134
135         queue_delayed_work(devfreq_wq, &devfreq->work,
136                                 msecs_to_jiffies(devfreq->profile->polling_ms));
137         mutex_unlock(&devfreq->lock);
138 }
139
140 /**
141  * devfreq_monitor_start() - Start load monitoring of devfreq instance
142  * @devfreq:    the devfreq instance.
143  *
144  * Helper function for starting devfreq device load monitoing. By
145  * default delayed work based monitoring is supported. Function
146  * to be called from governor in response to DEVFREQ_GOV_START
147  * event when device is added to devfreq framework.
148  */
149 void devfreq_monitor_start(struct devfreq *devfreq)
150 {
151         INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
152         if (devfreq->profile->polling_ms)
153                 queue_delayed_work(devfreq_wq, &devfreq->work,
154                         msecs_to_jiffies(devfreq->profile->polling_ms));
155 }
156
157 /**
158  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
159  * @devfreq:    the devfreq instance.
160  *
161  * Helper function to stop devfreq device load monitoing. Function
162  * to be called from governor in response to DEVFREQ_GOV_STOP
163  * event when device is removed from devfreq framework.
164  */
165 void devfreq_monitor_stop(struct devfreq *devfreq)
166 {
167         cancel_delayed_work_sync(&devfreq->work);
168 }
169
170 /**
171  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
172  * @devfreq:    the devfreq instance.
173  *
174  * Helper function to suspend devfreq device load monitoing. Function
175  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
176  * event or when polling interval is set to zero.
177  *
178  * Note: Though this function is same as devfreq_monitor_stop(),
179  * intentionally kept separate to provide hooks for collecting
180  * transition statistics.
181  */
182 void devfreq_monitor_suspend(struct devfreq *devfreq)
183 {
184         mutex_lock(&devfreq->lock);
185         if (devfreq->stop_polling) {
186                 mutex_unlock(&devfreq->lock);
187                 return;
188         }
189
190         devfreq->stop_polling = true;
191         mutex_unlock(&devfreq->lock);
192         cancel_delayed_work_sync(&devfreq->work);
193 }
194
195 /**
196  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
197  * @devfreq:    the devfreq instance.
198  *
199  * Helper function to resume devfreq device load monitoing. Function
200  * to be called from governor in response to DEVFREQ_GOV_RESUME
201  * event or when polling interval is set to non-zero.
202  */
203 void devfreq_monitor_resume(struct devfreq *devfreq)
204 {
205         mutex_lock(&devfreq->lock);
206         if (!devfreq->stop_polling)
207                 goto out;
208
209         if (!delayed_work_pending(&devfreq->work) &&
210                         devfreq->profile->polling_ms)
211                 queue_delayed_work(devfreq_wq, &devfreq->work,
212                         msecs_to_jiffies(devfreq->profile->polling_ms));
213         devfreq->stop_polling = false;
214
215 out:
216         mutex_unlock(&devfreq->lock);
217 }
218
219 /**
220  * devfreq_interval_update() - Update device devfreq monitoring interval
221  * @devfreq:    the devfreq instance.
222  * @delay:      new polling interval to be set.
223  *
224  * Helper function to set new load monitoring polling interval. Function
225  * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
226  */
227 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
228 {
229         unsigned int cur_delay = devfreq->profile->polling_ms;
230         unsigned int new_delay = *delay;
231
232         mutex_lock(&devfreq->lock);
233         devfreq->profile->polling_ms = new_delay;
234
235         if (devfreq->stop_polling)
236                 goto out;
237
238         /* if new delay is zero, stop polling */
239         if (!new_delay) {
240                 mutex_unlock(&devfreq->lock);
241                 cancel_delayed_work_sync(&devfreq->work);
242                 return;
243         }
244
245         /* if current delay is zero, start polling with new delay */
246         if (!cur_delay) {
247                 queue_delayed_work(devfreq_wq, &devfreq->work,
248                         msecs_to_jiffies(devfreq->profile->polling_ms));
249                 goto out;
250         }
251
252         /* if current delay is greater than new delay, restart polling */
253         if (cur_delay > new_delay) {
254                 mutex_unlock(&devfreq->lock);
255                 cancel_delayed_work_sync(&devfreq->work);
256                 mutex_lock(&devfreq->lock);
257                 if (!devfreq->stop_polling)
258                         queue_delayed_work(devfreq_wq, &devfreq->work,
259                               msecs_to_jiffies(devfreq->profile->polling_ms));
260         }
261 out:
262         mutex_unlock(&devfreq->lock);
263 }
264
265 /**
266  * devfreq_notifier_call() - Notify that the device frequency requirements
267  *                         has been changed out of devfreq framework.
268  * @nb:         the notifier_block (supposed to be devfreq->nb)
269  * @type:       not used
270  * @devp:       not used
271  *
272  * Called by a notifier that uses devfreq->nb.
273  */
274 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
275                                  void *devp)
276 {
277         struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
278         int ret;
279
280         mutex_lock(&devfreq->lock);
281         ret = update_devfreq(devfreq);
282         mutex_unlock(&devfreq->lock);
283
284         return ret;
285 }
286
287 /**
288  * _remove_devfreq() - Remove devfreq from the devfreq list and
289                 release its resources.
290  * @devfreq:    the devfreq struct
291  * @skip:       skip calling device_unregister().
292  */
293 static void _remove_devfreq(struct devfreq *devfreq, bool skip)
294 {
295         mutex_lock(&devfreq_list_lock);
296         if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
297                 mutex_unlock(&devfreq_list_lock);
298                 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
299                 return;
300         }
301         list_del(&devfreq->node);
302         mutex_unlock(&devfreq_list_lock);
303
304         devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_STOP, NULL);
305
306         if (devfreq->profile->exit)
307                 devfreq->profile->exit(devfreq->dev.parent);
308
309         if (!skip && get_device(&devfreq->dev)) {
310                 device_unregister(&devfreq->dev);
311                 put_device(&devfreq->dev);
312         }
313
314         mutex_destroy(&devfreq->lock);
315         kfree(devfreq);
316 }
317
318 /**
319  * devfreq_dev_release() - Callback for struct device to release the device.
320  * @dev:        the devfreq device
321  *
322  * This calls _remove_devfreq() if _remove_devfreq() is not called.
323  * Note that devfreq_dev_release() could be called by _remove_devfreq() as
324  * well as by others unregistering the device.
325  */
326 static void devfreq_dev_release(struct device *dev)
327 {
328         struct devfreq *devfreq = to_devfreq(dev);
329
330         _remove_devfreq(devfreq, true);
331 }
332
333 /**
334  * devfreq_add_device() - Add devfreq feature to the device
335  * @dev:        the device to add devfreq feature.
336  * @profile:    device-specific profile to run devfreq.
337  * @governor:   the policy to choose frequency.
338  * @data:       private data for the governor. The devfreq framework does not
339  *              touch this value.
340  */
341 struct devfreq *devfreq_add_device(struct device *dev,
342                                    struct devfreq_dev_profile *profile,
343                                    const struct devfreq_governor *governor,
344                                    void *data)
345 {
346         struct devfreq *devfreq;
347         int err = 0;
348
349         if (!dev || !profile || !governor) {
350                 dev_err(dev, "%s: Invalid parameters.\n", __func__);
351                 return ERR_PTR(-EINVAL);
352         }
353
354         mutex_lock(&devfreq_list_lock);
355         devfreq = find_device_devfreq(dev);
356         mutex_unlock(&devfreq_list_lock);
357         if (!IS_ERR(devfreq)) {
358                 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
359                 err = -EINVAL;
360                 goto err_out;
361         }
362
363         devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
364         if (!devfreq) {
365                 dev_err(dev, "%s: Unable to create devfreq for the device\n",
366                         __func__);
367                 err = -ENOMEM;
368                 goto err_out;
369         }
370
371         mutex_init(&devfreq->lock);
372         mutex_lock(&devfreq->lock);
373         devfreq->dev.parent = dev;
374         devfreq->dev.class = devfreq_class;
375         devfreq->dev.release = devfreq_dev_release;
376         devfreq->profile = profile;
377         devfreq->governor = governor;
378         devfreq->previous_freq = profile->initial_freq;
379         devfreq->data = data;
380         devfreq->nb.notifier_call = devfreq_notifier_call;
381
382         dev_set_name(&devfreq->dev, dev_name(dev));
383         err = device_register(&devfreq->dev);
384         if (err) {
385                 put_device(&devfreq->dev);
386                 mutex_unlock(&devfreq->lock);
387                 goto err_dev;
388         }
389
390         mutex_unlock(&devfreq->lock);
391
392         mutex_lock(&devfreq_list_lock);
393         list_add(&devfreq->node, &devfreq_list);
394         mutex_unlock(&devfreq_list_lock);
395
396         err = devfreq->governor->event_handler(devfreq,
397                                 DEVFREQ_GOV_START, NULL);
398         if (err) {
399                 dev_err(dev, "%s: Unable to start governor for the device\n",
400                         __func__);
401                 goto err_init;
402         }
403
404         return devfreq;
405
406 err_init:
407         list_del(&devfreq->node);
408         device_unregister(&devfreq->dev);
409 err_dev:
410         kfree(devfreq);
411 err_out:
412         return ERR_PTR(err);
413 }
414 EXPORT_SYMBOL(devfreq_add_device);
415
416 /**
417  * devfreq_remove_device() - Remove devfreq feature from a device.
418  * @devfreq:    the devfreq instance to be removed
419  */
420 int devfreq_remove_device(struct devfreq *devfreq)
421 {
422         if (!devfreq)
423                 return -EINVAL;
424
425         _remove_devfreq(devfreq, false);
426
427         return 0;
428 }
429 EXPORT_SYMBOL(devfreq_remove_device);
430
431 /**
432  * devfreq_suspend_device() - Suspend devfreq of a device.
433  * @devfreq     the devfreq instance to be suspended
434  */
435 int devfreq_suspend_device(struct devfreq *devfreq)
436 {
437         if (!devfreq)
438                 return -EINVAL;
439
440         return devfreq->governor->event_handler(devfreq,
441                                 DEVFREQ_GOV_SUSPEND, NULL);
442 }
443 EXPORT_SYMBOL(devfreq_suspend_device);
444
445 /**
446  * devfreq_resume_device() - Resume devfreq of a device.
447  * @devfreq     the devfreq instance to be resumed
448  */
449 int devfreq_resume_device(struct devfreq *devfreq)
450 {
451         if (!devfreq)
452                 return -EINVAL;
453
454         return devfreq->governor->event_handler(devfreq,
455                                 DEVFREQ_GOV_RESUME, NULL);
456 }
457 EXPORT_SYMBOL(devfreq_resume_device);
458
459 static ssize_t show_governor(struct device *dev,
460                              struct device_attribute *attr, char *buf)
461 {
462         return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
463 }
464
465 static ssize_t show_freq(struct device *dev,
466                          struct device_attribute *attr, char *buf)
467 {
468         unsigned long freq;
469         struct devfreq *devfreq = to_devfreq(dev);
470
471         if (devfreq->profile->get_cur_freq &&
472                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
473                         return sprintf(buf, "%lu\n", freq);
474
475         return sprintf(buf, "%lu\n", devfreq->previous_freq);
476 }
477
478 static ssize_t show_target_freq(struct device *dev,
479                         struct device_attribute *attr, char *buf)
480 {
481         return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
482 }
483
484 static ssize_t show_polling_interval(struct device *dev,
485                                      struct device_attribute *attr, char *buf)
486 {
487         return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
488 }
489
490 static ssize_t store_polling_interval(struct device *dev,
491                                       struct device_attribute *attr,
492                                       const char *buf, size_t count)
493 {
494         struct devfreq *df = to_devfreq(dev);
495         unsigned int value;
496         int ret;
497
498         ret = sscanf(buf, "%u", &value);
499         if (ret != 1)
500                 return -EINVAL;
501
502         df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
503         ret = count;
504
505         return ret;
506 }
507
508 static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
509                               const char *buf, size_t count)
510 {
511         struct devfreq *df = to_devfreq(dev);
512         unsigned long value;
513         int ret;
514         unsigned long max;
515
516         ret = sscanf(buf, "%lu", &value);
517         if (ret != 1)
518                 return -EINVAL;
519
520         mutex_lock(&df->lock);
521         max = df->max_freq;
522         if (value && max && value > max) {
523                 ret = -EINVAL;
524                 goto unlock;
525         }
526
527         df->min_freq = value;
528         update_devfreq(df);
529         ret = count;
530 unlock:
531         mutex_unlock(&df->lock);
532         return ret;
533 }
534
535 static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
536                              char *buf)
537 {
538         return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
539 }
540
541 static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
542                               const char *buf, size_t count)
543 {
544         struct devfreq *df = to_devfreq(dev);
545         unsigned long value;
546         int ret;
547         unsigned long min;
548
549         ret = sscanf(buf, "%lu", &value);
550         if (ret != 1)
551                 return -EINVAL;
552
553         mutex_lock(&df->lock);
554         min = df->min_freq;
555         if (value && min && value < min) {
556                 ret = -EINVAL;
557                 goto unlock;
558         }
559
560         df->max_freq = value;
561         update_devfreq(df);
562         ret = count;
563 unlock:
564         mutex_unlock(&df->lock);
565         return ret;
566 }
567
568 static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
569                              char *buf)
570 {
571         return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
572 }
573
574 static struct device_attribute devfreq_attrs[] = {
575         __ATTR(governor, S_IRUGO, show_governor, NULL),
576         __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
577         __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
578         __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
579                store_polling_interval),
580         __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
581         __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
582         { },
583 };
584
585 static int __init devfreq_init(void)
586 {
587         devfreq_class = class_create(THIS_MODULE, "devfreq");
588         if (IS_ERR(devfreq_class)) {
589                 pr_err("%s: couldn't create class\n", __FILE__);
590                 return PTR_ERR(devfreq_class);
591         }
592
593         devfreq_wq = create_freezable_workqueue("devfreq_wq");
594         if (IS_ERR(devfreq_wq)) {
595                 class_destroy(devfreq_class);
596                 pr_err("%s: couldn't create workqueue\n", __FILE__);
597                 return PTR_ERR(devfreq_wq);
598         }
599         devfreq_class->dev_attrs = devfreq_attrs;
600
601         return 0;
602 }
603 subsys_initcall(devfreq_init);
604
605 static void __exit devfreq_exit(void)
606 {
607         class_destroy(devfreq_class);
608         destroy_workqueue(devfreq_wq);
609 }
610 module_exit(devfreq_exit);
611
612 /*
613  * The followings are helper functions for devfreq user device drivers with
614  * OPP framework.
615  */
616
617 /**
618  * devfreq_recommended_opp() - Helper function to get proper OPP for the
619  *                           freq value given to target callback.
620  * @dev:        The devfreq user device. (parent of devfreq)
621  * @freq:       The frequency given to target function
622  * @flags:      Flags handed from devfreq framework.
623  *
624  */
625 struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
626                                     u32 flags)
627 {
628         struct opp *opp;
629
630         if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
631                 /* The freq is an upper bound. opp should be lower */
632                 opp = opp_find_freq_floor(dev, freq);
633
634                 /* If not available, use the closest opp */
635                 if (opp == ERR_PTR(-ERANGE))
636                         opp = opp_find_freq_ceil(dev, freq);
637         } else {
638                 /* The freq is an lower bound. opp should be higher */
639                 opp = opp_find_freq_ceil(dev, freq);
640
641                 /* If not available, use the closest opp */
642                 if (opp == ERR_PTR(-ERANGE))
643                         opp = opp_find_freq_floor(dev, freq);
644         }
645
646         return opp;
647 }
648
649 /**
650  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
651  *                                 for any changes in the OPP availability
652  *                                 changes
653  * @dev:        The devfreq user device. (parent of devfreq)
654  * @devfreq:    The devfreq object.
655  */
656 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
657 {
658         struct srcu_notifier_head *nh = opp_get_notifier(dev);
659
660         if (IS_ERR(nh))
661                 return PTR_ERR(nh);
662         return srcu_notifier_chain_register(nh, &devfreq->nb);
663 }
664
665 /**
666  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
667  *                                   notified for any changes in the OPP
668  *                                   availability changes anymore.
669  * @dev:        The devfreq user device. (parent of devfreq)
670  * @devfreq:    The devfreq object.
671  *
672  * At exit() callback of devfreq_dev_profile, this must be included if
673  * devfreq_recommended_opp is used.
674  */
675 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
676 {
677         struct srcu_notifier_head *nh = opp_get_notifier(dev);
678
679         if (IS_ERR(nh))
680                 return PTR_ERR(nh);
681         return srcu_notifier_chain_unregister(nh, &devfreq->nb);
682 }
683
684 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
685 MODULE_DESCRIPTION("devfreq class support");
686 MODULE_LICENSE("GPL");