governor_simpleondemand.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * linux/drivers/devfreq/governor_simpleondemand.c
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * MyungJoo Ham <myungjoo.ham@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/devfreq.h>
  14. #include <linux/math64.h>
  15. #include "governor.h"
  16. /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
  17. #define DFSO_UPTHRESHOLD (90)
  18. #define DFSO_DOWNDIFFERENCTIAL (5)
  19. static int devfreq_simple_ondemand_func(struct devfreq *df,
  20. unsigned long *freq)
  21. {
  22. int err;
  23. struct devfreq_dev_status *stat;
  24. unsigned long long a, b;
  25. unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
  26. unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
  27. struct devfreq_simple_ondemand_data *data = df->data;
  28. err = devfreq_update_stats(df);
  29. if (err)
  30. return err;
  31. stat = &df->last_status;
  32. if (data) {
  33. if (data->upthreshold)
  34. dfso_upthreshold = data->upthreshold;
  35. if (data->downdifferential)
  36. dfso_downdifferential = data->downdifferential;
  37. }
  38. if (dfso_upthreshold > 100 ||
  39. dfso_upthreshold < dfso_downdifferential)
  40. return -EINVAL;
  41. /* Assume MAX if it is going to be divided by zero */
  42. if (stat->total_time == 0) {
  43. *freq = DEVFREQ_MAX_FREQ;
  44. return 0;
  45. }
  46. /* Prevent overflow */
  47. if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
  48. stat->busy_time >>= 7;
  49. stat->total_time >>= 7;
  50. }
  51. /* Set MAX if it's busy enough */
  52. if (stat->busy_time * 100 >
  53. stat->total_time * dfso_upthreshold) {
  54. *freq = DEVFREQ_MAX_FREQ;
  55. return 0;
  56. }
  57. /* Set MAX if we do not know the initial frequency */
  58. if (stat->current_frequency == 0) {
  59. *freq = DEVFREQ_MAX_FREQ;
  60. return 0;
  61. }
  62. /* Keep the current frequency */
  63. if (stat->busy_time * 100 >
  64. stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
  65. *freq = stat->current_frequency;
  66. return 0;
  67. }
  68. /* Set the desired frequency based on the load */
  69. a = stat->busy_time;
  70. a *= stat->current_frequency;
  71. b = div_u64(a, stat->total_time);
  72. b *= 100;
  73. b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
  74. *freq = (unsigned long) b;
  75. return 0;
  76. }
  77. static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
  78. unsigned int event, void *data)
  79. {
  80. switch (event) {
  81. case DEVFREQ_GOV_START:
  82. devfreq_monitor_start(devfreq);
  83. break;
  84. case DEVFREQ_GOV_STOP:
  85. devfreq_monitor_stop(devfreq);
  86. break;
  87. case DEVFREQ_GOV_INTERVAL:
  88. devfreq_interval_update(devfreq, (unsigned int *)data);
  89. break;
  90. case DEVFREQ_GOV_SUSPEND:
  91. devfreq_monitor_suspend(devfreq);
  92. break;
  93. case DEVFREQ_GOV_RESUME:
  94. devfreq_monitor_resume(devfreq);
  95. break;
  96. default:
  97. break;
  98. }
  99. return 0;
  100. }
  101. static struct devfreq_governor devfreq_simple_ondemand = {
  102. .name = DEVFREQ_GOV_SIMPLE_ONDEMAND,
  103. .get_target_freq = devfreq_simple_ondemand_func,
  104. .event_handler = devfreq_simple_ondemand_handler,
  105. };
  106. static int __init devfreq_simple_ondemand_init(void)
  107. {
  108. return devfreq_add_governor(&devfreq_simple_ondemand);
  109. }
  110. subsys_initcall(devfreq_simple_ondemand_init);
  111. static void __exit devfreq_simple_ondemand_exit(void)
  112. {
  113. int ret;
  114. ret = devfreq_remove_governor(&devfreq_simple_ondemand);
  115. if (ret)
  116. pr_err("%s: failed remove governor %d\n", __func__, ret);
  117. return;
  118. }
  119. module_exit(devfreq_simple_ondemand_exit);
  120. MODULE_LICENSE("GPL");