governor_simpleondemand.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/devfreq.h>
  13. #include <linux/math64.h>
  14. /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
  15. #define DFSO_UPTHRESHOLD (90)
  16. #define DFSO_DOWNDIFFERENCTIAL (5)
  17. static int devfreq_simple_ondemand_func(struct devfreq *df,
  18. unsigned long *freq)
  19. {
  20. struct devfreq_dev_status stat;
  21. int err = df->profile->get_dev_status(df->dev.parent, &stat);
  22. unsigned long long a, b;
  23. unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
  24. unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
  25. struct devfreq_simple_ondemand_data *data = df->data;
  26. unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
  27. if (err)
  28. return err;
  29. if (data) {
  30. if (data->upthreshold)
  31. dfso_upthreshold = data->upthreshold;
  32. if (data->downdifferential)
  33. dfso_downdifferential = data->downdifferential;
  34. }
  35. if (dfso_upthreshold > 100 ||
  36. dfso_upthreshold < dfso_downdifferential)
  37. return -EINVAL;
  38. /* Assume MAX if it is going to be divided by zero */
  39. if (stat.total_time == 0) {
  40. *freq = max;
  41. return 0;
  42. }
  43. /* Prevent overflow */
  44. if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) {
  45. stat.busy_time >>= 7;
  46. stat.total_time >>= 7;
  47. }
  48. /* Set MAX if it's busy enough */
  49. if (stat.busy_time * 100 >
  50. stat.total_time * dfso_upthreshold) {
  51. *freq = max;
  52. return 0;
  53. }
  54. /* Set MAX if we do not know the initial frequency */
  55. if (stat.current_frequency == 0) {
  56. *freq = max;
  57. return 0;
  58. }
  59. /* Keep the current frequency */
  60. if (stat.busy_time * 100 >
  61. stat.total_time * (dfso_upthreshold - dfso_downdifferential)) {
  62. *freq = stat.current_frequency;
  63. return 0;
  64. }
  65. /* Set the desired frequency based on the load */
  66. a = stat.busy_time;
  67. a *= stat.current_frequency;
  68. b = div_u64(a, stat.total_time);
  69. b *= 100;
  70. b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
  71. *freq = (unsigned long) b;
  72. if (df->min_freq && *freq < df->min_freq)
  73. *freq = df->min_freq;
  74. if (df->max_freq && *freq > df->max_freq)
  75. *freq = df->max_freq;
  76. return 0;
  77. }
  78. const struct devfreq_governor devfreq_simple_ondemand = {
  79. .name = "simple_ondemand",
  80. .get_target_freq = devfreq_simple_ondemand_func,
  81. };