]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/devfreq/devfreq.c
devfreq: Account only powered time in trans_stat
[sojka/nv-tegra/linux-3.10.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  * Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/stat.h>
22 #include <linux/opp.h>
23 #include <linux/devfreq.h>
24 #include <linux/workqueue.h>
25 #include <linux/platform_device.h>
26 #include <linux/list.h>
27 #include <linux/printk.h>
28 #include <linux/hrtimer.h>
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 (unlikely(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         if (!devfreq->profile->max_state)
82                 return -EINVAL;
83
84         for (lev = 0; lev < devfreq->profile->max_state; lev++) {
85                 if (devfreq->profile->freq_table[lev] >= freq) {
86                         /* below minimum frequency? just return zero level */
87                         if (lev == 0)
88                                 return 0;
89
90                         /* select high freq if the freq is closer to that */
91                         if ((devfreq->profile->freq_table[lev] - freq) <
92                             (freq - devfreq->profile->freq_table[lev - 1]))
93                                 return lev;
94
95                         /* ..otherwise return the low freq */
96                         return lev - 1;
97                 }
98         }
99
100         /* above max freq */
101         return devfreq->profile->max_state - 1;
102 }
103
104 /**
105  * devfreq_update_status() - Update statistics of devfreq behavior
106  * @devfreq:    the devfreq instance
107  * @freq:       the update target frequency
108  */
109 static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
110 {
111         int lev, prev_lev;
112         unsigned long cur_time;
113
114         if (devfreq->suspended)
115                 return 0;
116
117         lev = devfreq_get_freq_level(devfreq, freq);
118         if (lev < 0)
119                 return lev;
120
121         prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
122         if (prev_lev < 0)
123                 return prev_lev;
124
125         cur_time = jiffies;
126         devfreq->time_in_state[lev] +=
127                          cur_time - devfreq->last_stat_updated;
128         if (lev != prev_lev) {
129                 devfreq->trans_table[(prev_lev *
130                                 devfreq->profile->max_state) + lev]++;
131                 devfreq->total_trans++;
132         }
133         devfreq->last_stat_updated = cur_time;
134
135         return 0;
136 }
137
138 /**
139  * find_devfreq_governor() - find devfreq governor from name
140  * @name:       name of the governor
141  *
142  * Search the list of devfreq governors and return the matched
143  * governor's pointer. devfreq_list_lock should be held by the caller.
144  */
145 static struct devfreq_governor *find_devfreq_governor(const char *name)
146 {
147         struct devfreq_governor *tmp_governor;
148
149         if (unlikely(IS_ERR_OR_NULL(name))) {
150                 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
151                 return ERR_PTR(-EINVAL);
152         }
153         WARN(!mutex_is_locked(&devfreq_list_lock),
154              "devfreq_list_lock must be locked.");
155
156         list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
157                 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
158                         return tmp_governor;
159         }
160
161         return ERR_PTR(-ENODEV);
162 }
163
164 /* Load monitoring helper functions for governors use */
165
166 /**
167  * update_devfreq() - Reevaluate the device and configure frequency.
168  * @devfreq:    the devfreq instance.
169  *
170  * Note: Lock devfreq->lock before calling update_devfreq
171  *       This function is exported for governors.
172  */
173 int update_devfreq(struct devfreq *devfreq)
174 {
175         unsigned long freq;
176         int err = 0;
177         u32 flags = 0;
178
179         if (!mutex_is_locked(&devfreq->lock)) {
180                 WARN(true, "devfreq->lock must be locked by the caller.\n");
181                 return -EINVAL;
182         }
183
184         if (!devfreq->governor)
185                 return -EINVAL;
186
187         /* Reevaluate the proper frequency */
188         err = devfreq->governor->get_target_freq(devfreq, &freq);
189         if (err)
190                 return err;
191
192         /*
193          * Adjust the freuqency with user freq and QoS.
194          *
195          * List from the highest proiority
196          * max_freq (probably called by thermal when it's too hot)
197          * min_freq
198          */
199
200         if (devfreq->min_freq && freq < devfreq->min_freq) {
201                 freq = devfreq->min_freq;
202                 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
203         }
204         if (devfreq->max_freq && freq > devfreq->max_freq) {
205                 freq = devfreq->max_freq;
206                 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
207         }
208
209         err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
210         if (err)
211                 return err;
212
213         if (devfreq->profile->freq_table)
214                 if (devfreq_update_status(devfreq, freq))
215                         dev_err(&devfreq->dev,
216                                 "Couldn't update frequency transition information.\n");
217
218         devfreq->previous_freq = freq;
219         return err;
220 }
221 EXPORT_SYMBOL(update_devfreq);
222
223 /**
224  * devfreq_monitor() - Periodically poll devfreq objects.
225  * @work:       the work struct used to run devfreq_monitor periodically.
226  *
227  */
228 static void devfreq_monitor(struct work_struct *work)
229 {
230         int err;
231         struct devfreq *devfreq = container_of(work,
232                                         struct devfreq, work.work);
233
234         mutex_lock(&devfreq->lock);
235         err = update_devfreq(devfreq);
236         if (err)
237                 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
238
239         queue_delayed_work(devfreq_wq, &devfreq->work,
240                                 msecs_to_jiffies(devfreq->profile->polling_ms));
241         mutex_unlock(&devfreq->lock);
242 }
243
244 /**
245  * devfreq_monitor_start() - Start load monitoring of devfreq instance
246  * @devfreq:    the devfreq instance.
247  *
248  * Helper function for starting devfreq device load monitoing. By
249  * default delayed work based monitoring is supported. Function
250  * to be called from governor in response to DEVFREQ_GOV_START
251  * event when device is added to devfreq framework.
252  */
253 void devfreq_monitor_start(struct devfreq *devfreq)
254 {
255         INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
256         if (devfreq->profile->polling_ms)
257                 queue_delayed_work(devfreq_wq, &devfreq->work,
258                         msecs_to_jiffies(devfreq->profile->polling_ms));
259 }
260 EXPORT_SYMBOL(devfreq_monitor_start);
261
262 /**
263  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
264  * @devfreq:    the devfreq instance.
265  *
266  * Helper function to stop devfreq device load monitoing. Function
267  * to be called from governor in response to DEVFREQ_GOV_STOP
268  * event when device is removed from devfreq framework.
269  */
270 void devfreq_monitor_stop(struct devfreq *devfreq)
271 {
272         cancel_delayed_work_sync(&devfreq->work);
273 }
274 EXPORT_SYMBOL(devfreq_monitor_stop);
275
276 /**
277  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
278  * @devfreq:    the devfreq instance.
279  *
280  * Helper function to suspend devfreq device load monitoing. Function
281  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
282  * event or when polling interval is set to zero.
283  *
284  * Note: Though this function is same as devfreq_monitor_stop(),
285  * intentionally kept separate to provide hooks for collecting
286  * transition statistics.
287  */
288 void devfreq_monitor_suspend(struct devfreq *devfreq)
289 {
290         mutex_lock(&devfreq->lock);
291         if (devfreq->stop_polling) {
292                 mutex_unlock(&devfreq->lock);
293                 return;
294         }
295
296         devfreq->stop_polling = true;
297         mutex_unlock(&devfreq->lock);
298         cancel_delayed_work_sync(&devfreq->work);
299 }
300 EXPORT_SYMBOL(devfreq_monitor_suspend);
301
302 /**
303  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
304  * @devfreq:    the devfreq instance.
305  *
306  * Helper function to resume devfreq device load monitoing. Function
307  * to be called from governor in response to DEVFREQ_GOV_RESUME
308  * event or when polling interval is set to non-zero.
309  */
310 void devfreq_monitor_resume(struct devfreq *devfreq)
311 {
312         mutex_lock(&devfreq->lock);
313         if (!devfreq->stop_polling)
314                 goto out;
315
316         if (!delayed_work_pending(&devfreq->work) &&
317                         devfreq->profile->polling_ms)
318                 queue_delayed_work(devfreq_wq, &devfreq->work,
319                         msecs_to_jiffies(devfreq->profile->polling_ms));
320         devfreq->stop_polling = false;
321
322 out:
323         mutex_unlock(&devfreq->lock);
324 }
325 EXPORT_SYMBOL(devfreq_monitor_resume);
326
327 /**
328  * devfreq_interval_update() - Update device devfreq monitoring interval
329  * @devfreq:    the devfreq instance.
330  * @delay:      new polling interval to be set.
331  *
332  * Helper function to set new load monitoring polling interval. Function
333  * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
334  */
335 void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
336 {
337         unsigned int cur_delay = devfreq->profile->polling_ms;
338         unsigned int new_delay = *delay;
339
340         mutex_lock(&devfreq->lock);
341         devfreq->profile->polling_ms = new_delay;
342
343         if (devfreq->stop_polling)
344                 goto out;
345
346         /* if new delay is zero, stop polling */
347         if (!new_delay) {
348                 mutex_unlock(&devfreq->lock);
349                 cancel_delayed_work_sync(&devfreq->work);
350                 return;
351         }
352
353         /* if current delay is zero, start polling with new delay */
354         if (!cur_delay) {
355                 queue_delayed_work(devfreq_wq, &devfreq->work,
356                         msecs_to_jiffies(devfreq->profile->polling_ms));
357                 goto out;
358         }
359
360         /* if current delay is greater than new delay, restart polling */
361         if (cur_delay > new_delay) {
362                 mutex_unlock(&devfreq->lock);
363                 cancel_delayed_work_sync(&devfreq->work);
364                 mutex_lock(&devfreq->lock);
365                 if (!devfreq->stop_polling)
366                         queue_delayed_work(devfreq_wq, &devfreq->work,
367                               msecs_to_jiffies(devfreq->profile->polling_ms));
368         }
369 out:
370         mutex_unlock(&devfreq->lock);
371 }
372 EXPORT_SYMBOL(devfreq_interval_update);
373
374 /**
375  * devfreq_notifier_call() - Notify that the device frequency requirements
376  *                         has been changed out of devfreq framework.
377  * @nb:         the notifier_block (supposed to be devfreq->nb)
378  * @type:       not used
379  * @devp:       not used
380  *
381  * Called by a notifier that uses devfreq->nb.
382  */
383 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
384                                  void *devp)
385 {
386         struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
387         int ret;
388
389         mutex_lock(&devfreq->lock);
390         ret = update_devfreq(devfreq);
391         mutex_unlock(&devfreq->lock);
392
393         return ret;
394 }
395
396 /**
397  * _remove_devfreq() - Remove devfreq from the list and release its resources.
398  * @devfreq:    the devfreq struct
399  * @skip:       skip calling device_unregister().
400  */
401 static void _remove_devfreq(struct devfreq *devfreq, bool skip)
402 {
403         mutex_lock(&devfreq_list_lock);
404         if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
405                 mutex_unlock(&devfreq_list_lock);
406                 dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
407                 return;
408         }
409         list_del(&devfreq->node);
410         mutex_unlock(&devfreq_list_lock);
411
412         if (devfreq->governor)
413                 devfreq->governor->event_handler(devfreq,
414                                                  DEVFREQ_GOV_STOP, NULL);
415
416         if (devfreq->profile->exit)
417                 devfreq->profile->exit(devfreq->dev.parent);
418
419         if (!skip && get_device(&devfreq->dev)) {
420                 device_unregister(&devfreq->dev);
421                 put_device(&devfreq->dev);
422         }
423
424         mutex_destroy(&devfreq->lock);
425         kfree(devfreq);
426 }
427
428 /**
429  * devfreq_dev_release() - Callback for struct device to release the device.
430  * @dev:        the devfreq device
431  *
432  * This calls _remove_devfreq() if _remove_devfreq() is not called.
433  * Note that devfreq_dev_release() could be called by _remove_devfreq() as
434  * well as by others unregistering the device.
435  */
436 static void devfreq_dev_release(struct device *dev)
437 {
438         struct devfreq *devfreq = to_devfreq(dev);
439
440         _remove_devfreq(devfreq, true);
441 }
442
443 /**
444  * devfreq_add_device() - Add devfreq feature to the device
445  * @dev:        the device to add devfreq feature.
446  * @profile:    device-specific profile to run devfreq.
447  * @governor_name:      name of the policy to choose frequency.
448  * @data:       private data for the governor. The devfreq framework does not
449  *              touch this value.
450  */
451 struct devfreq *devfreq_add_device(struct device *dev,
452                                    struct devfreq_dev_profile *profile,
453                                    const char *governor_name,
454                                    void *data)
455 {
456         struct devfreq *devfreq;
457         struct devfreq_governor *governor;
458         int err = 0;
459
460         if (!dev || !profile || !governor_name) {
461                 dev_err(dev, "%s: Invalid parameters.\n", __func__);
462                 return ERR_PTR(-EINVAL);
463         }
464
465         mutex_lock(&devfreq_list_lock);
466         devfreq = find_device_devfreq(dev);
467         mutex_unlock(&devfreq_list_lock);
468         if (!IS_ERR(devfreq)) {
469                 dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
470                 err = -EINVAL;
471                 goto err_out;
472         }
473
474         devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
475         if (!devfreq) {
476                 dev_err(dev, "%s: Unable to create devfreq for the device\n",
477                         __func__);
478                 err = -ENOMEM;
479                 goto err_out;
480         }
481
482         mutex_init(&devfreq->lock);
483         mutex_lock(&devfreq->lock);
484         devfreq->dev.parent = dev;
485         devfreq->dev.class = devfreq_class;
486         devfreq->dev.release = devfreq_dev_release;
487         devfreq->profile = profile;
488         strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
489         devfreq->previous_freq = profile->initial_freq;
490         devfreq->data = data;
491         devfreq->nb.notifier_call = devfreq_notifier_call;
492
493         devfreq->trans_table =  devm_kzalloc(dev, sizeof(unsigned int) *
494                                                 devfreq->profile->max_state *
495                                                 devfreq->profile->max_state,
496                                                 GFP_KERNEL);
497         devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned long) *
498                                                 devfreq->profile->max_state,
499                                                 GFP_KERNEL);
500         devfreq->last_stat_updated = jiffies;
501
502         dev_set_name(&devfreq->dev, dev_name(dev));
503         err = device_register(&devfreq->dev);
504         if (err) {
505                 put_device(&devfreq->dev);
506                 mutex_unlock(&devfreq->lock);
507                 goto err_dev;
508         }
509
510         mutex_unlock(&devfreq->lock);
511
512         mutex_lock(&devfreq_list_lock);
513         list_add(&devfreq->node, &devfreq_list);
514
515         governor = find_devfreq_governor(devfreq->governor_name);
516         if (!IS_ERR(governor))
517                 devfreq->governor = governor;
518         if (devfreq->governor)
519                 err = devfreq->governor->event_handler(devfreq,
520                                         DEVFREQ_GOV_START, NULL);
521         mutex_unlock(&devfreq_list_lock);
522         if (err) {
523                 dev_err(dev, "%s: Unable to start governor for the device\n",
524                         __func__);
525                 goto err_init;
526         }
527
528         return devfreq;
529
530 err_init:
531         list_del(&devfreq->node);
532         device_unregister(&devfreq->dev);
533 err_dev:
534         kfree(devfreq);
535 err_out:
536         return ERR_PTR(err);
537 }
538 EXPORT_SYMBOL(devfreq_add_device);
539
540 /**
541  * devfreq_remove_device() - Remove devfreq feature from a device.
542  * @devfreq:    the devfreq instance to be removed
543  */
544 int devfreq_remove_device(struct devfreq *devfreq)
545 {
546         if (!devfreq)
547                 return -EINVAL;
548
549         _remove_devfreq(devfreq, false);
550
551         return 0;
552 }
553 EXPORT_SYMBOL(devfreq_remove_device);
554
555 /**
556  * devfreq_suspend_device() - Suspend devfreq of a device.
557  * @devfreq: the devfreq instance to be suspended
558  */
559 int devfreq_suspend_device(struct devfreq *devfreq)
560 {
561         if (!devfreq)
562                 return -EINVAL;
563
564         /* Last update before suspend */
565         mutex_lock(&devfreq->lock);
566         devfreq_update_status(devfreq, devfreq->previous_freq);
567         devfreq->suspended = true;
568         mutex_unlock(&devfreq->lock);
569
570         if (!devfreq->governor)
571                 return 0;
572
573         return devfreq->governor->event_handler(devfreq,
574                                 DEVFREQ_GOV_SUSPEND, NULL);
575 }
576 EXPORT_SYMBOL(devfreq_suspend_device);
577
578 /**
579  * devfreq_resume_device() - Resume devfreq of a device.
580  * @devfreq: the devfreq instance to be resumed
581  */
582 int devfreq_resume_device(struct devfreq *devfreq)
583 {
584         if (!devfreq)
585                 return -EINVAL;
586
587         /* Update the timestamp before resuming */
588         mutex_lock(&devfreq->lock);
589         devfreq->last_stat_updated = jiffies;
590         devfreq->suspended = false;
591         mutex_unlock(&devfreq->lock);
592
593         if (!devfreq->governor)
594                 return 0;
595
596         return devfreq->governor->event_handler(devfreq,
597                                 DEVFREQ_GOV_RESUME, NULL);
598 }
599 EXPORT_SYMBOL(devfreq_resume_device);
600
601 /**
602  * devfreq_add_governor() - Add devfreq governor
603  * @governor:   the devfreq governor to be added
604  */
605 int devfreq_add_governor(struct devfreq_governor *governor)
606 {
607         struct devfreq_governor *g;
608         struct devfreq *devfreq;
609         int err = 0;
610
611         if (!governor) {
612                 pr_err("%s: Invalid parameters.\n", __func__);
613                 return -EINVAL;
614         }
615
616         mutex_lock(&devfreq_list_lock);
617         g = find_devfreq_governor(governor->name);
618         if (!IS_ERR(g)) {
619                 pr_err("%s: governor %s already registered\n", __func__,
620                        g->name);
621                 err = -EINVAL;
622                 goto err_out;
623         }
624
625         list_add(&governor->node, &devfreq_governor_list);
626
627         list_for_each_entry(devfreq, &devfreq_list, node) {
628                 int ret = 0;
629                 struct device *dev = devfreq->dev.parent;
630
631                 if (!strncmp(devfreq->governor_name, governor->name,
632                              DEVFREQ_NAME_LEN)) {
633                         /* The following should never occur */
634                         if (devfreq->governor) {
635                                 dev_warn(dev,
636                                          "%s: Governor %s already present\n",
637                                          __func__, devfreq->governor->name);
638                                 ret = devfreq->governor->event_handler(devfreq,
639                                                         DEVFREQ_GOV_STOP, NULL);
640                                 if (ret) {
641                                         dev_warn(dev,
642                                                  "%s: Governor %s stop = %d\n",
643                                                  __func__,
644                                                  devfreq->governor->name, ret);
645                                 }
646                                 /* Fall through */
647                         }
648                         devfreq->governor = governor;
649                         ret = devfreq->governor->event_handler(devfreq,
650                                                 DEVFREQ_GOV_START, NULL);
651                         if (ret) {
652                                 dev_warn(dev, "%s: Governor %s start=%d\n",
653                                          __func__, devfreq->governor->name,
654                                          ret);
655                         }
656                 }
657         }
658
659 err_out:
660         mutex_unlock(&devfreq_list_lock);
661
662         return err;
663 }
664 EXPORT_SYMBOL(devfreq_add_governor);
665
666 /**
667  * devfreq_remove_device() - Remove devfreq feature from a device.
668  * @governor:   the devfreq governor to be removed
669  */
670 int devfreq_remove_governor(struct devfreq_governor *governor)
671 {
672         struct devfreq_governor *g;
673         struct devfreq *devfreq;
674         int err = 0;
675
676         if (!governor) {
677                 pr_err("%s: Invalid parameters.\n", __func__);
678                 return -EINVAL;
679         }
680
681         mutex_lock(&devfreq_list_lock);
682         g = find_devfreq_governor(governor->name);
683         if (IS_ERR(g)) {
684                 pr_err("%s: governor %s not registered\n", __func__,
685                        governor->name);
686                 err = PTR_ERR(g);
687                 goto err_out;
688         }
689         list_for_each_entry(devfreq, &devfreq_list, node) {
690                 int ret;
691                 struct device *dev = devfreq->dev.parent;
692
693                 if (!strncmp(devfreq->governor_name, governor->name,
694                              DEVFREQ_NAME_LEN)) {
695                         /* we should have a devfreq governor! */
696                         if (!devfreq->governor) {
697                                 dev_warn(dev, "%s: Governor %s NOT present\n",
698                                          __func__, governor->name);
699                                 continue;
700                                 /* Fall through */
701                         }
702                         ret = devfreq->governor->event_handler(devfreq,
703                                                 DEVFREQ_GOV_STOP, NULL);
704                         if (ret) {
705                                 dev_warn(dev, "%s: Governor %s stop=%d\n",
706                                          __func__, devfreq->governor->name,
707                                          ret);
708                         }
709                         devfreq->governor = NULL;
710                 }
711         }
712
713         list_del(&governor->node);
714 err_out:
715         mutex_unlock(&devfreq_list_lock);
716
717         return err;
718 }
719 EXPORT_SYMBOL(devfreq_remove_governor);
720
721 static ssize_t show_governor(struct device *dev,
722                              struct device_attribute *attr, char *buf)
723 {
724         if (!to_devfreq(dev)->governor)
725                 return -EINVAL;
726
727         return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
728 }
729
730 static ssize_t store_governor(struct device *dev, struct device_attribute *attr,
731                               const char *buf, size_t count)
732 {
733         struct devfreq *df = to_devfreq(dev);
734         int ret;
735         char str_governor[DEVFREQ_NAME_LEN + 1];
736         struct devfreq_governor *governor;
737
738         ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
739         if (ret != 1)
740                 return -EINVAL;
741
742         mutex_lock(&devfreq_list_lock);
743         governor = find_devfreq_governor(str_governor);
744         if (IS_ERR(governor)) {
745                 ret = PTR_ERR(governor);
746                 goto out;
747         }
748         if (df->governor == governor)
749                 goto out;
750
751         if (df->governor) {
752                 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
753                 if (ret) {
754                         dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
755                                  __func__, df->governor->name, ret);
756                         goto out;
757                 }
758         }
759         df->governor = governor;
760         strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
761         ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
762         if (ret)
763                 dev_warn(dev, "%s: Governor %s not started(%d)\n",
764                          __func__, df->governor->name, ret);
765 out:
766         mutex_unlock(&devfreq_list_lock);
767
768         if (!ret)
769                 ret = count;
770         return ret;
771 }
772 static ssize_t show_available_governors(struct device *d,
773                                     struct device_attribute *attr,
774                                     char *buf)
775 {
776         struct devfreq_governor *tmp_governor;
777         ssize_t count = 0;
778
779         mutex_lock(&devfreq_list_lock);
780         list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
781                 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
782                                    "%s ", tmp_governor->name);
783         mutex_unlock(&devfreq_list_lock);
784
785         /* Truncate the trailing space */
786         if (count)
787                 count--;
788
789         count += sprintf(&buf[count], "\n");
790
791         return count;
792 }
793
794 static ssize_t show_freq(struct device *dev,
795                          struct device_attribute *attr, char *buf)
796 {
797         unsigned long freq;
798         struct devfreq *devfreq = to_devfreq(dev);
799
800         if (devfreq->profile->get_cur_freq &&
801                 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
802                         return sprintf(buf, "%lu\n", freq);
803
804         return sprintf(buf, "%lu\n", devfreq->previous_freq);
805 }
806
807 static ssize_t show_target_freq(struct device *dev,
808                         struct device_attribute *attr, char *buf)
809 {
810         return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
811 }
812
813 static ssize_t show_polling_interval(struct device *dev,
814                                      struct device_attribute *attr, char *buf)
815 {
816         return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
817 }
818
819 static ssize_t store_polling_interval(struct device *dev,
820                                       struct device_attribute *attr,
821                                       const char *buf, size_t count)
822 {
823         struct devfreq *df = to_devfreq(dev);
824         unsigned int value;
825         int ret;
826
827         if (!df->governor)
828                 return -EINVAL;
829
830         ret = sscanf(buf, "%u", &value);
831         if (ret != 1)
832                 return -EINVAL;
833
834         df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
835         ret = count;
836
837         return ret;
838 }
839
840 static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
841                               const char *buf, size_t count)
842 {
843         struct devfreq *df = to_devfreq(dev);
844         unsigned long value;
845         int ret;
846         unsigned long max;
847
848         ret = sscanf(buf, "%lu", &value);
849         if (ret != 1)
850                 return -EINVAL;
851
852         mutex_lock(&df->lock);
853         max = df->max_freq;
854         if (value && max && value > max) {
855                 ret = -EINVAL;
856                 goto unlock;
857         }
858
859         df->min_freq = value;
860         update_devfreq(df);
861         ret = count;
862 unlock:
863         mutex_unlock(&df->lock);
864         return ret;
865 }
866
867 static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
868                              char *buf)
869 {
870         return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
871 }
872
873 static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
874                               const char *buf, size_t count)
875 {
876         struct devfreq *df = to_devfreq(dev);
877         unsigned long value;
878         int ret;
879         unsigned long min;
880
881         ret = sscanf(buf, "%lu", &value);
882         if (ret != 1)
883                 return -EINVAL;
884
885         mutex_lock(&df->lock);
886         min = df->min_freq;
887         if (value && min && value < min) {
888                 ret = -EINVAL;
889                 goto unlock;
890         }
891
892         df->max_freq = value;
893         update_devfreq(df);
894         ret = count;
895 unlock:
896         mutex_unlock(&df->lock);
897         return ret;
898 }
899
900 static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
901                              char *buf)
902 {
903         return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
904 }
905
906 static ssize_t show_available_freqs(struct device *d,
907                                     struct device_attribute *attr,
908                                     char *buf)
909 {
910         struct devfreq *df = to_devfreq(d);
911         struct device *dev = df->dev.parent;
912         struct opp *opp;
913         ssize_t count = 0;
914         unsigned long freq = 0;
915
916         if (df->profile->max_state) {
917                 int i;
918
919                 for (i = 0; i < df->profile->max_state; i++) {
920                         freq = df->profile->freq_table[i];
921                         count += scnprintf(&buf[count],
922                                            (PAGE_SIZE - count - 2), "%lu ",
923                                            freq);
924                 }
925         } else {
926                 rcu_read_lock();
927                 do {
928                         opp = opp_find_freq_ceil(dev, &freq);
929                         if (IS_ERR(opp))
930                                 break;
931
932                         count += scnprintf(&buf[count],
933                                            (PAGE_SIZE - count - 2), "%lu ",
934                                            freq);
935                         freq++;
936                 } while (1);
937                 rcu_read_unlock();
938         }
939
940         /* Truncate the trailing space */
941         if (count)
942                 count--;
943
944         count += sprintf(&buf[count], "\n");
945
946         return count;
947 }
948
949 static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
950                                 char *buf)
951 {
952         struct devfreq *devfreq = to_devfreq(dev);
953         ssize_t len;
954         int i, j, err;
955         unsigned int max_state = devfreq->profile->max_state;
956         int prev_freq_level;
957         unsigned long prev_freq;
958
959         mutex_lock(&devfreq->lock);
960         err = devfreq_update_status(devfreq, devfreq->previous_freq);
961         if (err)
962                 return 0;
963         mutex_unlock(&devfreq->lock);
964
965         /* round the current frequency */
966         prev_freq_level = devfreq_get_freq_level(devfreq,
967                                                  devfreq->previous_freq);
968
969         if (prev_freq_level < 0)
970                 prev_freq = devfreq->previous_freq;
971         else
972                 prev_freq = devfreq->profile->freq_table[prev_freq_level];
973
974         len = sprintf(buf, "   From  :   To\n");
975         len += sprintf(buf + len, "         :");
976         for (i = 0; i < max_state; i++)
977                 len += sprintf(buf + len, "%8lu",
978                                 devfreq->profile->freq_table[i]);
979
980         len += sprintf(buf + len, "   time(ms)\n");
981
982         for (i = 0; i < max_state; i++) {
983                 if (devfreq->profile->freq_table[i] == prev_freq &&
984                     !devfreq->suspended) {
985                         len += sprintf(buf + len, "*");
986                 } else {
987                         len += sprintf(buf + len, " ");
988                 }
989                 len += sprintf(buf + len, "%8lu:",
990                                 devfreq->profile->freq_table[i]);
991                 for (j = 0; j < max_state; j++)
992                         len += sprintf(buf + len, "%8u",
993                                 devfreq->trans_table[(i * max_state) + j]);
994                 len += sprintf(buf + len, "%10u\n",
995                         jiffies_to_msecs(devfreq->time_in_state[i]));
996         }
997
998         len += sprintf(buf + len, "Total transition : %u\n",
999                                         devfreq->total_trans);
1000         return len;
1001 }
1002
1003 static struct device_attribute devfreq_attrs[] = {
1004         __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor),
1005         __ATTR(available_governors, S_IRUGO, show_available_governors, NULL),
1006         __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
1007         __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
1008         __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
1009         __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
1010                store_polling_interval),
1011         __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
1012         __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
1013         __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
1014         { },
1015 };
1016
1017 static int __init devfreq_init(void)
1018 {
1019         devfreq_class = class_create(THIS_MODULE, "devfreq");
1020         if (IS_ERR(devfreq_class)) {
1021                 pr_err("%s: couldn't create class\n", __FILE__);
1022                 return PTR_ERR(devfreq_class);
1023         }
1024
1025         devfreq_wq = create_freezable_workqueue("devfreq_wq");
1026         if (IS_ERR(devfreq_wq)) {
1027                 class_destroy(devfreq_class);
1028                 pr_err("%s: couldn't create workqueue\n", __FILE__);
1029                 return PTR_ERR(devfreq_wq);
1030         }
1031         devfreq_class->dev_attrs = devfreq_attrs;
1032
1033         return 0;
1034 }
1035 subsys_initcall(devfreq_init);
1036
1037 static void __exit devfreq_exit(void)
1038 {
1039         class_destroy(devfreq_class);
1040         destroy_workqueue(devfreq_wq);
1041 }
1042 module_exit(devfreq_exit);
1043
1044 /*
1045  * The followings are helper functions for devfreq user device drivers with
1046  * OPP framework.
1047  */
1048
1049 /**
1050  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1051  *                           freq value given to target callback.
1052  * @dev:        The devfreq user device. (parent of devfreq)
1053  * @freq:       The frequency given to target function
1054  * @flags:      Flags handed from devfreq framework.
1055  *
1056  * Locking: This function must be called under rcu_read_lock(). opp is a rcu
1057  * protected pointer. The reason for the same is that the opp pointer which is
1058  * returned will remain valid for use with opp_get_{voltage, freq} only while
1059  * under the locked area. The pointer returned must be used prior to unlocking
1060  * with rcu_read_unlock() to maintain the integrity of the pointer.
1061  */
1062 struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
1063                                     u32 flags)
1064 {
1065         struct opp *opp;
1066
1067         if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1068                 /* The freq is an upper bound. opp should be lower */
1069                 opp = opp_find_freq_floor(dev, freq);
1070
1071                 /* If not available, use the closest opp */
1072                 if (opp == ERR_PTR(-ERANGE))
1073                         opp = opp_find_freq_ceil(dev, freq);
1074         } else {
1075                 /* The freq is an lower bound. opp should be higher */
1076                 opp = opp_find_freq_ceil(dev, freq);
1077
1078                 /* If not available, use the closest opp */
1079                 if (opp == ERR_PTR(-ERANGE))
1080                         opp = opp_find_freq_floor(dev, freq);
1081         }
1082
1083         return opp;
1084 }
1085
1086 /**
1087  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1088  *                                 for any changes in the OPP availability
1089  *                                 changes
1090  * @dev:        The devfreq user device. (parent of devfreq)
1091  * @devfreq:    The devfreq object.
1092  */
1093 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1094 {
1095         struct srcu_notifier_head *nh;
1096         int ret = 0;
1097
1098         rcu_read_lock();
1099         nh = opp_get_notifier(dev);
1100         if (IS_ERR(nh))
1101                 ret = PTR_ERR(nh);
1102         rcu_read_unlock();
1103         if (!ret)
1104                 ret = srcu_notifier_chain_register(nh, &devfreq->nb);
1105
1106         return ret;
1107 }
1108
1109 /**
1110  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1111  *                                   notified for any changes in the OPP
1112  *                                   availability changes anymore.
1113  * @dev:        The devfreq user device. (parent of devfreq)
1114  * @devfreq:    The devfreq object.
1115  *
1116  * At exit() callback of devfreq_dev_profile, this must be included if
1117  * devfreq_recommended_opp is used.
1118  */
1119 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1120 {
1121         struct srcu_notifier_head *nh;
1122         int ret = 0;
1123
1124         rcu_read_lock();
1125         nh = opp_get_notifier(dev);
1126         if (IS_ERR(nh))
1127                 ret = PTR_ERR(nh);
1128         rcu_read_unlock();
1129         if (!ret)
1130                 ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
1131
1132         return ret;
1133 }
1134
1135 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1136 MODULE_DESCRIPTION("devfreq class support");
1137 MODULE_LICENSE("GPL");