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