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