PM / devfreq: Add devfreq_get_devfreq_by_phandle()
[cascardo/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/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>
28 #include <linux/of.h>
29 #include "governor.h"
30
31 static struct class *devfreq_class;
32
33 /*
34  * devfreq core provides delayed work based load monitoring helper
35  * functions. Governors can use these or can implement their own
36  * monitoring mechanism.
37  */
38 static struct workqueue_struct *devfreq_wq;
39
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);
45
46 /**
47  * find_device_devfreq() - find devfreq struct using device pointer
48  * @dev:        device pointer used to lookup device devfreq.
49  *
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.
52  */
53 static struct devfreq *find_device_devfreq(struct device *dev)
54 {
55         struct devfreq *tmp_devfreq;
56
57         if (IS_ERR_OR_NULL(dev)) {
58                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
59                 return ERR_PTR(-EINVAL);
60         }
61         WARN(!mutex_is_locked(&devfreq_list_lock),
62              "devfreq_list_lock must be locked.");
63
64         list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
65                 if (tmp_devfreq->dev.parent == dev)
66                         return tmp_devfreq;
67         }
68
69         return ERR_PTR(-ENODEV);
70 }
71
72 /**
73  * devfreq_get_freq_level() - Lookup freq_table for the frequency
74  * @devfreq:    the devfreq instance
75  * @freq:       the target frequency
76  */
77 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
78 {
79         int lev;
80
81         for (lev = 0; lev < devfreq->profile->max_state; lev++)
82                 if (freq == devfreq->profile->freq_table[lev])
83                         return lev;
84
85         return -EINVAL;
86 }
87
88 /**
89  * devfreq_set_freq_table() - Initialize freq_table for the frequency
90  * @devfreq:    the devfreq instance
91  */
92 static void devfreq_set_freq_table(struct devfreq *devfreq)
93 {
94         struct devfreq_dev_profile *profile = devfreq->profile;
95         struct dev_pm_opp *opp;
96         unsigned long freq;
97         int i, count;
98
99         /* Initialize the freq_table from OPP table */
100         count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
101         if (count <= 0)
102                 return;
103
104         profile->max_state = count;
105         profile->freq_table = devm_kcalloc(devfreq->dev.parent,
106                                         profile->max_state,
107                                         sizeof(*profile->freq_table),
108                                         GFP_KERNEL);
109         if (!profile->freq_table) {
110                 profile->max_state = 0;
111                 return;
112         }
113
114         rcu_read_lock();
115         for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
116                 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
117                 if (IS_ERR(opp)) {
118                         devm_kfree(devfreq->dev.parent, profile->freq_table);
119                         profile->max_state = 0;
120                         rcu_read_unlock();
121                         return;
122                 }
123                 profile->freq_table[i] = freq;
124         }
125         rcu_read_unlock();
126 }
127
128 /**
129  * devfreq_update_status() - Update statistics of devfreq behavior
130  * @devfreq:    the devfreq instance
131  * @freq:       the update target frequency
132  */
133 static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
134 {
135         int lev, prev_lev, ret = 0;
136         unsigned long cur_time;
137
138         cur_time = jiffies;
139
140         prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
141         if (prev_lev < 0) {
142                 ret = prev_lev;
143                 goto out;
144         }
145
146         devfreq->time_in_state[prev_lev] +=
147                          cur_time - devfreq->last_stat_updated;
148
149         lev = devfreq_get_freq_level(devfreq, freq);
150         if (lev < 0) {
151                 ret = lev;
152                 goto out;
153         }
154
155         if (lev != prev_lev) {
156                 devfreq->trans_table[(prev_lev *
157                                 devfreq->profile->max_state) + lev]++;
158                 devfreq->total_trans++;
159         }
160
161 out:
162         devfreq->last_stat_updated = cur_time;
163         return ret;
164 }
165
166 /**
167  * find_devfreq_governor() - find devfreq governor from name
168  * @name:       name of the governor
169  *
170  * Search the list of devfreq governors and return the matched
171  * governor's pointer. devfreq_list_lock should be held by the caller.
172  */
173 static struct devfreq_governor *find_devfreq_governor(const char *name)
174 {
175         struct devfreq_governor *tmp_governor;
176
177         if (IS_ERR_OR_NULL(name)) {
178                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
179                 return ERR_PTR(-EINVAL);
180         }
181         WARN(!mutex_is_locked(&devfreq_list_lock),
182              "devfreq_list_lock must be locked.");
183
184         list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
185                 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
186                         return tmp_governor;
187         }
188
189         return ERR_PTR(-ENODEV);
190 }
191
192 /* Load monitoring helper functions for governors use */
193
194 /**
195  * update_devfreq() - Reevaluate the device and configure frequency.
196  * @devfreq:    the devfreq instance.
197  *
198  * Note: Lock devfreq->lock before calling update_devfreq
199  *       This function is exported for governors.
200  */
201 int update_devfreq(struct devfreq *devfreq)
202 {
203         unsigned long freq;
204         int err = 0;
205         u32 flags = 0;
206
207         if (!mutex_is_locked(&devfreq->lock)) {
208                 WARN(true, "devfreq->lock must be locked by the caller.\n");
209                 return -EINVAL;
210         }
211
212         if (!devfreq->governor)
213                 return -EINVAL;
214
215         /* Reevaluate the proper frequency */
216         err = devfreq->governor->get_target_freq(devfreq, &freq);
217         if (err)
218                 return err;
219
220         /*
221          * Adjust the frequency with user freq and QoS.
222          *
223          * List from the highest priority
224          * max_freq
225          * min_freq
226          */
227
228         if (devfreq->min_freq && freq < devfreq->min_freq) {
229                 freq = devfreq->min_freq;
230                 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
231         }
232         if (devfreq->max_freq && freq > devfreq->max_freq) {
233                 freq = devfreq->max_freq;
234                 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
235         }
236
237         err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
238         if (err)
239                 return err;
240
241         if (devfreq->profile->freq_table)
242                 if (devfreq_update_status(devfreq, freq))
243                         dev_err(&devfreq->dev,
244                                 "Couldn't update frequency transition information.\n");
245
246         devfreq->previous_freq = freq;
247         return err;
248 }
249 EXPORT_SYMBOL(update_devfreq);
250
251 /**
252  * devfreq_monitor() - Periodically poll devfreq objects.
253  * @work:       the work struct used to run devfreq_monitor periodically.
254  *
255  */
256 static void devfreq_monitor(struct work_struct *work)
257 {
258         int err;
259         struct devfreq *devfreq = container_of(work,
260                                         struct devfreq, work.work);
261
262         mutex_lock(&devfreq->lock);
263         err = update_devfreq(devfreq);
264         if (err)
265                 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
266
267         queue_delayed_work(devfreq_wq, &devfreq->work,
268                                 msecs_to_jiffies(devfreq->profile->polling_ms));
269         mutex_unlock(&devfreq->lock);
270 }
271
272 /**
273  * devfreq_monitor_start() - Start load monitoring of devfreq instance
274  * @devfreq:    the devfreq instance.
275  *
276  * Helper function for starting devfreq device load monitoing. By
277  * default delayed work based monitoring is supported. Function
278  * to be called from governor in response to DEVFREQ_GOV_START
279  * event when device is added to devfreq framework.
280  */
281 void devfreq_monitor_start(struct devfreq *devfreq)
282 {
283         INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
284         if (devfreq->profile->polling_ms)
285                 queue_delayed_work(devfreq_wq, &devfreq->work,
286                         msecs_to_jiffies(devfreq->profile->polling_ms));
287 }
288 EXPORT_SYMBOL(devfreq_monitor_start);
289
290 /**
291  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
292  * @devfreq:    the devfreq instance.
293  *
294  * Helper function to stop devfreq device load monitoing. Function
295  * to be called from governor in response to DEVFREQ_GOV_STOP
296  * event when device is removed from devfreq framework.
297  */
298 void devfreq_monitor_stop(struct devfreq *devfreq)
299 {
300         cancel_delayed_work_sync(&devfreq->work);
301 }
302 EXPORT_SYMBOL(devfreq_monitor_stop);
303
304 /**
305  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
306  * @devfreq:    the devfreq instance.
307  *
308  * Helper function to suspend devfreq device load monitoing. Function
309  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
310  * event or when polling interval is set to zero.
311  *
312  * Note: Though this function is same as devfreq_monitor_stop(),
313  * intentionally kept separate to provide hooks for collecting
314  * transition statistics.
315  */
316 void devfreq_monitor_suspend(struct devfreq *devfreq)
317 {
318         mutex_lock(&devfreq->lock);
319         if (devfreq->stop_polling) {
320                 mutex_unlock(&devfreq->lock);
321                 return;
322         }
323
324         devfreq_update_status(devfreq, devfreq->previous_freq);
325         devfreq->stop_polling = true;
326         mutex_unlock(&devfreq->lock);
327         cancel_delayed_work_sync(&devfreq->work);
328 }
329 EXPORT_SYMBOL(devfreq_monitor_suspend);
330
331 /**
332  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
333  * @devfreq:    the devfreq instance.
334  *
335  * Helper function to resume devfreq device load monitoing. Function
336  * to be called from governor in response to DEVFREQ_GOV_RESUME
337  * event or when polling interval is set to non-zero.
338  */
339 void devfreq_monitor_resume(struct devfreq *devfreq)
340 {
341         unsigned long freq;
342
343         mutex_lock(&devfreq->lock);
344         if (!devfreq->stop_polling)
345                 goto out;
346
347         if (!delayed_work_pending(&devfreq->work) &&
348                         devfreq->profile->polling_ms)
349                 queue_delayed_work(devfreq_wq, &devfreq->work,
350                         msecs_to_jiffies(devfreq->profile->polling_ms));
351
352         devfreq->last_stat_updated = jiffies;
353         devfreq->stop_polling = false;
354
355         if (devfreq->profile->get_cur_freq &&
356                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
357                 devfreq->previous_freq = freq;
358
359 out:
360         mutex_unlock(&devfreq->lock);
361 }
362 EXPORT_SYMBOL(devfreq_monitor_resume);
363
364 /**
365  * devfreq_interval_update() - Update device devfreq monitoring interval
366  * @devfreq:    the devfreq instance.
367  * @delay:      new polling interval to be set.
368  *
369  * Helper function to set new load monitoring polling interval. Function
370  * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
371  */
372 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
373 {
374         unsigned int cur_delay = devfreq->profile->polling_ms;
375         unsigned int new_delay = *delay;
376
377         mutex_lock(&devfreq->lock);
378         devfreq->profile->polling_ms = new_delay;
379
380         if (devfreq->stop_polling)
381                 goto out;
382
383         /* if new delay is zero, stop polling */
384         if (!new_delay) {
385                 mutex_unlock(&devfreq->lock);
386                 cancel_delayed_work_sync(&devfreq->work);
387                 return;
388         }
389
390         /* if current delay is zero, start polling with new delay */
391         if (!cur_delay) {
392                 queue_delayed_work(devfreq_wq, &devfreq->work,
393                         msecs_to_jiffies(devfreq->profile->polling_ms));
394                 goto out;
395         }
396
397         /* if current delay is greater than new delay, restart polling */
398         if (cur_delay > new_delay) {
399                 mutex_unlock(&devfreq->lock);
400                 cancel_delayed_work_sync(&devfreq->work);
401                 mutex_lock(&devfreq->lock);
402                 if (!devfreq->stop_polling)
403                         queue_delayed_work(devfreq_wq, &devfreq->work,
404                               msecs_to_jiffies(devfreq->profile->polling_ms));
405         }
406 out:
407         mutex_unlock(&devfreq->lock);
408 }
409 EXPORT_SYMBOL(devfreq_interval_update);
410
411 /**
412  * devfreq_notifier_call() - Notify that the device frequency requirements
413  *                         has been changed out of devfreq framework.
414  * @nb:         the notifier_block (supposed to be devfreq->nb)
415  * @type:       not used
416  * @devp:       not used
417  *
418  * Called by a notifier that uses devfreq->nb.
419  */
420 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
421                                  void *devp)
422 {
423         struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
424         int ret;
425
426         mutex_lock(&devfreq->lock);
427         ret = update_devfreq(devfreq);
428         mutex_unlock(&devfreq->lock);
429
430         return ret;
431 }
432
433 /**
434  * _remove_devfreq() - Remove devfreq from the list and release its resources.
435  * @devfreq:    the devfreq struct
436  */
437 static void _remove_devfreq(struct devfreq *devfreq)
438 {
439         mutex_lock(&devfreq_list_lock);
440         if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
441                 mutex_unlock(&devfreq_list_lock);
442                 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
443                 return;
444         }
445         list_del(&devfreq->node);
446         mutex_unlock(&devfreq_list_lock);
447
448         if (devfreq->governor)
449                 devfreq->governor->event_handler(devfreq,
450                                                  DEVFREQ_GOV_STOP, NULL);
451
452         if (devfreq->profile->exit)
453                 devfreq->profile->exit(devfreq->dev.parent);
454
455         mutex_destroy(&devfreq->lock);
456         kfree(devfreq);
457 }
458
459 /**
460  * devfreq_dev_release() - Callback for struct device to release the device.
461  * @dev:        the devfreq device
462  *
463  * This calls _remove_devfreq() if _remove_devfreq() is not called.
464  */
465 static void devfreq_dev_release(struct device *dev)
466 {
467         struct devfreq *devfreq = to_devfreq(dev);
468
469         _remove_devfreq(devfreq);
470 }
471
472 /**
473  * devfreq_add_device() - Add devfreq feature to the device
474  * @dev:        the device to add devfreq feature.
475  * @profile:    device-specific profile to run devfreq.
476  * @governor_name:      name of the policy to choose frequency.
477  * @data:       private data for the governor. The devfreq framework does not
478  *              touch this value.
479  */
480 struct devfreq *devfreq_add_device(struct device *dev,
481                                    struct devfreq_dev_profile *profile,
482                                    const char *governor_name,
483                                    void *data)
484 {
485         struct devfreq *devfreq;
486         struct devfreq_governor *governor;
487         int err = 0;
488
489         if (!dev || !profile || !governor_name) {
490                 dev_err(dev, "%s: Invalid parameters.\n", __func__);
491                 return ERR_PTR(-EINVAL);
492         }
493
494         mutex_lock(&devfreq_list_lock);
495         devfreq = find_device_devfreq(dev);
496         mutex_unlock(&devfreq_list_lock);
497         if (!IS_ERR(devfreq)) {
498                 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
499                 err = -EINVAL;
500                 goto err_out;
501         }
502
503         devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
504         if (!devfreq) {
505                 dev_err(dev, "%s: Unable to create devfreq for the device\n",
506                         __func__);
507                 err = -ENOMEM;
508                 goto err_out;
509         }
510
511         mutex_init(&devfreq->lock);
512         mutex_lock(&devfreq->lock);
513         devfreq->dev.parent = dev;
514         devfreq->dev.class = devfreq_class;
515         devfreq->dev.release = devfreq_dev_release;
516         devfreq->profile = profile;
517         strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
518         devfreq->previous_freq = profile->initial_freq;
519         devfreq->data = data;
520         devfreq->nb.notifier_call = devfreq_notifier_call;
521
522         if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
523                 mutex_unlock(&devfreq->lock);
524                 devfreq_set_freq_table(devfreq);
525                 mutex_lock(&devfreq->lock);
526         }
527
528         devfreq->trans_table =  devm_kzalloc(dev, sizeof(unsigned int) *
529                                                 devfreq->profile->max_state *
530                                                 devfreq->profile->max_state,
531                                                 GFP_KERNEL);
532         devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
533                                                 devfreq->profile->max_state,
534                                                 GFP_KERNEL);
535         devfreq->last_stat_updated = jiffies;
536
537         dev_set_name(&devfreq->dev, "%s", dev_name(dev));
538         err = device_register(&devfreq->dev);
539         if (err) {
540                 put_device(&devfreq->dev);
541                 mutex_unlock(&devfreq->lock);
542                 goto err_out;
543         }
544
545         mutex_unlock(&devfreq->lock);
546
547         mutex_lock(&devfreq_list_lock);
548         list_add(&devfreq->node, &devfreq_list);
549
550         governor = find_devfreq_governor(devfreq->governor_name);
551         if (!IS_ERR(governor))
552                 devfreq->governor = governor;
553         if (devfreq->governor)
554                 err = devfreq->governor->event_handler(devfreq,
555                                         DEVFREQ_GOV_START, NULL);
556         mutex_unlock(&devfreq_list_lock);
557         if (err) {
558                 dev_err(dev, "%s: Unable to start governor for the device\n",
559                         __func__);
560                 goto err_init;
561         }
562
563         return devfreq;
564
565 err_init:
566         list_del(&devfreq->node);
567         device_unregister(&devfreq->dev);
568         kfree(devfreq);
569 err_out:
570         return ERR_PTR(err);
571 }
572 EXPORT_SYMBOL(devfreq_add_device);
573
574 /**
575  * devfreq_remove_device() - Remove devfreq feature from a device.
576  * @devfreq:    the devfreq instance to be removed
577  *
578  * The opposite of devfreq_add_device().
579  */
580 int devfreq_remove_device(struct devfreq *devfreq)
581 {
582         if (!devfreq)
583                 return -EINVAL;
584
585         device_unregister(&devfreq->dev);
586         put_device(&devfreq->dev);
587
588         return 0;
589 }
590 EXPORT_SYMBOL(devfreq_remove_device);
591
592 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
593 {
594         struct devfreq **r = res;
595
596         if (WARN_ON(!r || !*r))
597                 return 0;
598
599         return *r == data;
600 }
601
602 static void devm_devfreq_dev_release(struct device *dev, void *res)
603 {
604         devfreq_remove_device(*(struct devfreq **)res);
605 }
606
607 /**
608  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
609  * @dev:        the device to add devfreq feature.
610  * @profile:    device-specific profile to run devfreq.
611  * @governor_name:      name of the policy to choose frequency.
612  * @data:       private data for the governor. The devfreq framework does not
613  *              touch this value.
614  *
615  * This function manages automatically the memory of devfreq device using device
616  * resource management and simplify the free operation for memory of devfreq
617  * device.
618  */
619 struct devfreq *devm_devfreq_add_device(struct device *dev,
620                                         struct devfreq_dev_profile *profile,
621                                         const char *governor_name,
622                                         void *data)
623 {
624         struct devfreq **ptr, *devfreq;
625
626         ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
627         if (!ptr)
628                 return ERR_PTR(-ENOMEM);
629
630         devfreq = devfreq_add_device(dev, profile, governor_name, data);
631         if (IS_ERR(devfreq)) {
632                 devres_free(ptr);
633                 return ERR_PTR(-ENOMEM);
634         }
635
636         *ptr = devfreq;
637         devres_add(dev, ptr);
638
639         return devfreq;
640 }
641 EXPORT_SYMBOL(devm_devfreq_add_device);
642
643 #ifdef CONFIG_OF
644 /*
645  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
646  * @dev - instance to the given device
647  * @index - index into list of devfreq
648  *
649  * return the instance of devfreq device
650  */
651 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
652 {
653         struct device_node *node;
654         struct devfreq *devfreq;
655
656         if (!dev)
657                 return ERR_PTR(-EINVAL);
658
659         if (!dev->of_node)
660                 return ERR_PTR(-EINVAL);
661
662         node = of_parse_phandle(dev->of_node, "devfreq", index);
663         if (!node)
664                 return ERR_PTR(-ENODEV);
665
666         mutex_lock(&devfreq_list_lock);
667         list_for_each_entry(devfreq, &devfreq_list, node) {
668                 if (devfreq->dev.parent
669                         && devfreq->dev.parent->of_node == node) {
670                         mutex_unlock(&devfreq_list_lock);
671                         return devfreq;
672                 }
673         }
674         mutex_unlock(&devfreq_list_lock);
675
676         return ERR_PTR(-EPROBE_DEFER);
677 }
678 #else
679 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
680 {
681         return ERR_PTR(-ENODEV);
682 }
683 #endif /* CONFIG_OF */
684 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
685
686 /**
687  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
688  * @dev:        the device to add devfreq feature.
689  * @devfreq:    the devfreq instance to be removed
690  */
691 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
692 {
693         WARN_ON(devres_release(dev, devm_devfreq_dev_release,
694                                devm_devfreq_dev_match, devfreq));
695 }
696 EXPORT_SYMBOL(devm_devfreq_remove_device);
697
698 /**
699  * devfreq_suspend_device() - Suspend devfreq of a device.
700  * @devfreq: the devfreq instance to be suspended
701  *
702  * This function is intended to be called by the pm callbacks
703  * (e.g., runtime_suspend, suspend) of the device driver that
704  * holds the devfreq.
705  */
706 int devfreq_suspend_device(struct devfreq *devfreq)
707 {
708         if (!devfreq)
709                 return -EINVAL;
710
711         if (!devfreq->governor)
712                 return 0;
713
714         return devfreq->governor->event_handler(devfreq,
715                                 DEVFREQ_GOV_SUSPEND, NULL);
716 }
717 EXPORT_SYMBOL(devfreq_suspend_device);
718
719 /**
720  * devfreq_resume_device() - Resume devfreq of a device.
721  * @devfreq: the devfreq instance to be resumed
722  *
723  * This function is intended to be called by the pm callbacks
724  * (e.g., runtime_resume, resume) of the device driver that
725  * holds the devfreq.
726  */
727 int devfreq_resume_device(struct devfreq *devfreq)
728 {
729         if (!devfreq)
730                 return -EINVAL;
731
732         if (!devfreq->governor)
733                 return 0;
734
735         return devfreq->governor->event_handler(devfreq,
736                                 DEVFREQ_GOV_RESUME, NULL);
737 }
738 EXPORT_SYMBOL(devfreq_resume_device);
739
740 /**
741  * devfreq_add_governor() - Add devfreq governor
742  * @governor:   the devfreq governor to be added
743  */
744 int devfreq_add_governor(struct devfreq_governor *governor)
745 {
746         struct devfreq_governor *g;
747         struct devfreq *devfreq;
748         int err = 0;
749
750         if (!governor) {
751                 pr_err("%s: Invalid parameters.\n", __func__);
752                 return -EINVAL;
753         }
754
755         mutex_lock(&devfreq_list_lock);
756         g = find_devfreq_governor(governor->name);
757         if (!IS_ERR(g)) {
758                 pr_err("%s: governor %s already registered\n", __func__,
759                        g->name);
760                 err = -EINVAL;
761                 goto err_out;
762         }
763
764         list_add(&governor->node, &devfreq_governor_list);
765
766         list_for_each_entry(devfreq, &devfreq_list, node) {
767                 int ret = 0;
768                 struct device *dev = devfreq->dev.parent;
769
770                 if (!strncmp(devfreq->governor_name, governor->name,
771                              DEVFREQ_NAME_LEN)) {
772                         /* The following should never occur */
773                         if (devfreq->governor) {
774                                 dev_warn(dev,
775                                          "%s: Governor %s already present\n",
776                                          __func__, devfreq->governor->name);
777                                 ret = devfreq->governor->event_handler(devfreq,
778                                                         DEVFREQ_GOV_STOP, NULL);
779                                 if (ret) {
780                                         dev_warn(dev,
781                                                  "%s: Governor %s stop = %d\n",
782                                                  __func__,
783                                                  devfreq->governor->name, ret);
784                                 }
785                                 /* Fall through */
786                         }
787                         devfreq->governor = governor;
788                         ret = devfreq->governor->event_handler(devfreq,
789                                                 DEVFREQ_GOV_START, NULL);
790                         if (ret) {
791                                 dev_warn(dev, "%s: Governor %s start=%d\n",
792                                          __func__, devfreq->governor->name,
793                                          ret);
794                         }
795                 }
796         }
797
798 err_out:
799         mutex_unlock(&devfreq_list_lock);
800
801         return err;
802 }
803 EXPORT_SYMBOL(devfreq_add_governor);
804
805 /**
806  * devfreq_remove_device() - Remove devfreq feature from a device.
807  * @governor:   the devfreq governor to be removed
808  */
809 int devfreq_remove_governor(struct devfreq_governor *governor)
810 {
811         struct devfreq_governor *g;
812         struct devfreq *devfreq;
813         int err = 0;
814
815         if (!governor) {
816                 pr_err("%s: Invalid parameters.\n", __func__);
817                 return -EINVAL;
818         }
819
820         mutex_lock(&devfreq_list_lock);
821         g = find_devfreq_governor(governor->name);
822         if (IS_ERR(g)) {
823                 pr_err("%s: governor %s not registered\n", __func__,
824                        governor->name);
825                 err = PTR_ERR(g);
826                 goto err_out;
827         }
828         list_for_each_entry(devfreq, &devfreq_list, node) {
829                 int ret;
830                 struct device *dev = devfreq->dev.parent;
831
832                 if (!strncmp(devfreq->governor_name, governor->name,
833                              DEVFREQ_NAME_LEN)) {
834                         /* we should have a devfreq governor! */
835                         if (!devfreq->governor) {
836                                 dev_warn(dev, "%s: Governor %s NOT present\n",
837                                          __func__, governor->name);
838                                 continue;
839                                 /* Fall through */
840                         }
841                         ret = devfreq->governor->event_handler(devfreq,
842                                                 DEVFREQ_GOV_STOP, NULL);
843                         if (ret) {
844                                 dev_warn(dev, "%s: Governor %s stop=%d\n",
845                                          __func__, devfreq->governor->name,
846                                          ret);
847                         }
848                         devfreq->governor = NULL;
849                 }
850         }
851
852         list_del(&governor->node);
853 err_out:
854         mutex_unlock(&devfreq_list_lock);
855
856         return err;
857 }
858 EXPORT_SYMBOL(devfreq_remove_governor);
859
860 static ssize_t governor_show(struct device *dev,
861                              struct device_attribute *attr, char *buf)
862 {
863         if (!to_devfreq(dev)->governor)
864                 return -EINVAL;
865
866         return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
867 }
868
869 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
870                               const char *buf, size_t count)
871 {
872         struct devfreq *df = to_devfreq(dev);
873         int ret;
874         char str_governor[DEVFREQ_NAME_LEN + 1];
875         struct devfreq_governor *governor;
876
877         ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
878         if (ret != 1)
879                 return -EINVAL;
880
881         mutex_lock(&devfreq_list_lock);
882         governor = find_devfreq_governor(str_governor);
883         if (IS_ERR(governor)) {
884                 ret = PTR_ERR(governor);
885                 goto out;
886         }
887         if (df->governor == governor) {
888                 ret = 0;
889                 goto out;
890         }
891
892         if (df->governor) {
893                 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
894                 if (ret) {
895                         dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
896                                  __func__, df->governor->name, ret);
897                         goto out;
898                 }
899         }
900         df->governor = governor;
901         strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
902         ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
903         if (ret)
904                 dev_warn(dev, "%s: Governor %s not started(%d)\n",
905                          __func__, df->governor->name, ret);
906 out:
907         mutex_unlock(&devfreq_list_lock);
908
909         if (!ret)
910                 ret = count;
911         return ret;
912 }
913 static DEVICE_ATTR_RW(governor);
914
915 static ssize_t available_governors_show(struct device *d,
916                                         struct device_attribute *attr,
917                                         char *buf)
918 {
919         struct devfreq_governor *tmp_governor;
920         ssize_t count = 0;
921
922         mutex_lock(&devfreq_list_lock);
923         list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
924                 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
925                                    "%s ", tmp_governor->name);
926         mutex_unlock(&devfreq_list_lock);
927
928         /* Truncate the trailing space */
929         if (count)
930                 count--;
931
932         count += sprintf(&buf[count], "\n");
933
934         return count;
935 }
936 static DEVICE_ATTR_RO(available_governors);
937
938 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
939                              char *buf)
940 {
941         unsigned long freq;
942         struct devfreq *devfreq = to_devfreq(dev);
943
944         if (devfreq->profile->get_cur_freq &&
945                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
946                         return sprintf(buf, "%lu\n", freq);
947
948         return sprintf(buf, "%lu\n", devfreq->previous_freq);
949 }
950 static DEVICE_ATTR_RO(cur_freq);
951
952 static ssize_t target_freq_show(struct device *dev,
953                                 struct device_attribute *attr, char *buf)
954 {
955         return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
956 }
957 static DEVICE_ATTR_RO(target_freq);
958
959 static ssize_t polling_interval_show(struct device *dev,
960                                      struct device_attribute *attr, char *buf)
961 {
962         return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
963 }
964
965 static ssize_t polling_interval_store(struct device *dev,
966                                       struct device_attribute *attr,
967                                       const char *buf, size_t count)
968 {
969         struct devfreq *df = to_devfreq(dev);
970         unsigned int value;
971         int ret;
972
973         if (!df->governor)
974                 return -EINVAL;
975
976         ret = sscanf(buf, "%u", &value);
977         if (ret != 1)
978                 return -EINVAL;
979
980         df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
981         ret = count;
982
983         return ret;
984 }
985 static DEVICE_ATTR_RW(polling_interval);
986
987 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
988                               const char *buf, size_t count)
989 {
990         struct devfreq *df = to_devfreq(dev);
991         unsigned long value;
992         int ret;
993         unsigned long max;
994
995         ret = sscanf(buf, "%lu", &value);
996         if (ret != 1)
997                 return -EINVAL;
998
999         mutex_lock(&df->lock);
1000         max = df->max_freq;
1001         if (value && max && value > max) {
1002                 ret = -EINVAL;
1003                 goto unlock;
1004         }
1005
1006         df->min_freq = value;
1007         update_devfreq(df);
1008         ret = count;
1009 unlock:
1010         mutex_unlock(&df->lock);
1011         return ret;
1012 }
1013
1014 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1015                               const char *buf, size_t count)
1016 {
1017         struct devfreq *df = to_devfreq(dev);
1018         unsigned long value;
1019         int ret;
1020         unsigned long min;
1021
1022         ret = sscanf(buf, "%lu", &value);
1023         if (ret != 1)
1024                 return -EINVAL;
1025
1026         mutex_lock(&df->lock);
1027         min = df->min_freq;
1028         if (value && min && value < min) {
1029                 ret = -EINVAL;
1030                 goto unlock;
1031         }
1032
1033         df->max_freq = value;
1034         update_devfreq(df);
1035         ret = count;
1036 unlock:
1037         mutex_unlock(&df->lock);
1038         return ret;
1039 }
1040
1041 #define show_one(name)                                          \
1042 static ssize_t name##_show                                      \
1043 (struct device *dev, struct device_attribute *attr, char *buf)  \
1044 {                                                               \
1045         return sprintf(buf, "%lu\n", to_devfreq(dev)->name);    \
1046 }
1047 show_one(min_freq);
1048 show_one(max_freq);
1049
1050 static DEVICE_ATTR_RW(min_freq);
1051 static DEVICE_ATTR_RW(max_freq);
1052
1053 static ssize_t available_frequencies_show(struct device *d,
1054                                           struct device_attribute *attr,
1055                                           char *buf)
1056 {
1057         struct devfreq *df = to_devfreq(d);
1058         struct device *dev = df->dev.parent;
1059         struct dev_pm_opp *opp;
1060         ssize_t count = 0;
1061         unsigned long freq = 0;
1062
1063         rcu_read_lock();
1064         do {
1065                 opp = dev_pm_opp_find_freq_ceil(dev, &freq);
1066                 if (IS_ERR(opp))
1067                         break;
1068
1069                 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1070                                    "%lu ", freq);
1071                 freq++;
1072         } while (1);
1073         rcu_read_unlock();
1074
1075         /* Truncate the trailing space */
1076         if (count)
1077                 count--;
1078
1079         count += sprintf(&buf[count], "\n");
1080
1081         return count;
1082 }
1083 static DEVICE_ATTR_RO(available_frequencies);
1084
1085 static ssize_t trans_stat_show(struct device *dev,
1086                                struct device_attribute *attr, char *buf)
1087 {
1088         struct devfreq *devfreq = to_devfreq(dev);
1089         ssize_t len;
1090         int i, j;
1091         unsigned int max_state = devfreq->profile->max_state;
1092
1093         if (!devfreq->stop_polling &&
1094                         devfreq_update_status(devfreq, devfreq->previous_freq))
1095                 return 0;
1096         if (max_state == 0)
1097                 return sprintf(buf, "Not Supported.\n");
1098
1099         len = sprintf(buf, "     From  :   To\n");
1100         len += sprintf(buf + len, "           :");
1101         for (i = 0; i < max_state; i++)
1102                 len += sprintf(buf + len, "%10lu",
1103                                 devfreq->profile->freq_table[i]);
1104
1105         len += sprintf(buf + len, "   time(ms)\n");
1106
1107         for (i = 0; i < max_state; i++) {
1108                 if (devfreq->profile->freq_table[i]
1109                                         == devfreq->previous_freq) {
1110                         len += sprintf(buf + len, "*");
1111                 } else {
1112                         len += sprintf(buf + len, " ");
1113                 }
1114                 len += sprintf(buf + len, "%10lu:",
1115                                 devfreq->profile->freq_table[i]);
1116                 for (j = 0; j < max_state; j++)
1117                         len += sprintf(buf + len, "%10u",
1118                                 devfreq->trans_table[(i * max_state) + j]);
1119                 len += sprintf(buf + len, "%10u\n",
1120                         jiffies_to_msecs(devfreq->time_in_state[i]));
1121         }
1122
1123         len += sprintf(buf + len, "Total transition : %u\n",
1124                                         devfreq->total_trans);
1125         return len;
1126 }
1127 static DEVICE_ATTR_RO(trans_stat);
1128
1129 static struct attribute *devfreq_attrs[] = {
1130         &dev_attr_governor.attr,
1131         &dev_attr_available_governors.attr,
1132         &dev_attr_cur_freq.attr,
1133         &dev_attr_available_frequencies.attr,
1134         &dev_attr_target_freq.attr,
1135         &dev_attr_polling_interval.attr,
1136         &dev_attr_min_freq.attr,
1137         &dev_attr_max_freq.attr,
1138         &dev_attr_trans_stat.attr,
1139         NULL,
1140 };
1141 ATTRIBUTE_GROUPS(devfreq);
1142
1143 static int __init devfreq_init(void)
1144 {
1145         devfreq_class = class_create(THIS_MODULE, "devfreq");
1146         if (IS_ERR(devfreq_class)) {
1147                 pr_err("%s: couldn't create class\n", __FILE__);
1148                 return PTR_ERR(devfreq_class);
1149         }
1150
1151         devfreq_wq = create_freezable_workqueue("devfreq_wq");
1152         if (!devfreq_wq) {
1153                 class_destroy(devfreq_class);
1154                 pr_err("%s: couldn't create workqueue\n", __FILE__);
1155                 return -ENOMEM;
1156         }
1157         devfreq_class->dev_groups = devfreq_groups;
1158
1159         return 0;
1160 }
1161 subsys_initcall(devfreq_init);
1162
1163 static void __exit devfreq_exit(void)
1164 {
1165         class_destroy(devfreq_class);
1166         destroy_workqueue(devfreq_wq);
1167 }
1168 module_exit(devfreq_exit);
1169
1170 /*
1171  * The followings are helper functions for devfreq user device drivers with
1172  * OPP framework.
1173  */
1174
1175 /**
1176  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1177  *                           freq value given to target callback.
1178  * @dev:        The devfreq user device. (parent of devfreq)
1179  * @freq:       The frequency given to target function
1180  * @flags:      Flags handed from devfreq framework.
1181  *
1182  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1183  * protected pointer. The reason for the same is that the opp pointer which is
1184  * returned will remain valid for use with opp_get_{voltage, freq} only while
1185  * under the locked area. The pointer returned must be used prior to unlocking
1186  * with rcu_read_unlock() to maintain the integrity of the pointer.
1187  */
1188 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1189                                            unsigned long *freq,
1190                                            u32 flags)
1191 {
1192         struct dev_pm_opp *opp;
1193
1194         if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1195                 /* The freq is an upper bound. opp should be lower */
1196                 opp = dev_pm_opp_find_freq_floor(dev, freq);
1197
1198                 /* If not available, use the closest opp */
1199                 if (opp == ERR_PTR(-ERANGE))
1200                         opp = dev_pm_opp_find_freq_ceil(dev, freq);
1201         } else {
1202                 /* The freq is an lower bound. opp should be higher */
1203                 opp = dev_pm_opp_find_freq_ceil(dev, freq);
1204
1205                 /* If not available, use the closest opp */
1206                 if (opp == ERR_PTR(-ERANGE))
1207                         opp = dev_pm_opp_find_freq_floor(dev, freq);
1208         }
1209
1210         return opp;
1211 }
1212 EXPORT_SYMBOL(devfreq_recommended_opp);
1213
1214 /**
1215  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1216  *                                 for any changes in the OPP availability
1217  *                                 changes
1218  * @dev:        The devfreq user device. (parent of devfreq)
1219  * @devfreq:    The devfreq object.
1220  */
1221 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1222 {
1223         struct srcu_notifier_head *nh;
1224         int ret = 0;
1225
1226         rcu_read_lock();
1227         nh = dev_pm_opp_get_notifier(dev);
1228         if (IS_ERR(nh))
1229                 ret = PTR_ERR(nh);
1230         rcu_read_unlock();
1231         if (!ret)
1232                 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1233
1234         return ret;
1235 }
1236 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1237
1238 /**
1239  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1240  *                                   notified for any changes in the OPP
1241  *                                   availability changes anymore.
1242  * @dev:        The devfreq user device. (parent of devfreq)
1243  * @devfreq:    The devfreq object.
1244  *
1245  * At exit() callback of devfreq_dev_profile, this must be included if
1246  * devfreq_recommended_opp is used.
1247  */
1248 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1249 {
1250         struct srcu_notifier_head *nh;
1251         int ret = 0;
1252
1253         rcu_read_lock();
1254         nh = dev_pm_opp_get_notifier(dev);
1255         if (IS_ERR(nh))
1256                 ret = PTR_ERR(nh);
1257         rcu_read_unlock();
1258         if (!ret)
1259                 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1260
1261         return ret;
1262 }
1263 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1264
1265 static void devm_devfreq_opp_release(struct device *dev, void *res)
1266 {
1267         devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1268 }
1269
1270 /**
1271  * devm_ devfreq_register_opp_notifier()
1272  *              - Resource-managed devfreq_register_opp_notifier()
1273  * @dev:        The devfreq user device. (parent of devfreq)
1274  * @devfreq:    The devfreq object.
1275  */
1276 int devm_devfreq_register_opp_notifier(struct device *dev,
1277                                        struct devfreq *devfreq)
1278 {
1279         struct devfreq **ptr;
1280         int ret;
1281
1282         ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1283         if (!ptr)
1284                 return -ENOMEM;
1285
1286         ret = devfreq_register_opp_notifier(dev, devfreq);
1287         if (ret) {
1288                 devres_free(ptr);
1289                 return ret;
1290         }
1291
1292         *ptr = devfreq;
1293         devres_add(dev, ptr);
1294
1295         return 0;
1296 }
1297 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1298
1299 /**
1300  * devm_devfreq_unregister_opp_notifier()
1301  *              - Resource-managed devfreq_unregister_opp_notifier()
1302  * @dev:        The devfreq user device. (parent of devfreq)
1303  * @devfreq:    The devfreq object.
1304  */
1305 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1306                                          struct devfreq *devfreq)
1307 {
1308         WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1309                                devm_devfreq_dev_match, devfreq));
1310 }
1311 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1312
1313 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1314 MODULE_DESCRIPTION("devfreq class support");
1315 MODULE_LICENSE("GPL");