governor_passive.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * linux/drivers/devfreq/governor_passive.c
  3. *
  4. * Copyright (C) 2016 Samsung Electronics
  5. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  6. * Author: 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. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/devfreq.h>
  15. #include "governor.h"
  16. static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
  17. unsigned long *freq)
  18. {
  19. struct devfreq_passive_data *p_data
  20. = (struct devfreq_passive_data *)devfreq->data;
  21. struct devfreq *parent_devfreq = (struct devfreq *)p_data->parent;
  22. unsigned long child_freq = ULONG_MAX;
  23. struct dev_pm_opp *opp;
  24. int i, count, ret = 0;
  25. /*
  26. * If the devfreq device with passive governor has the specific method
  27. * to determine the next frequency, should use the get_target_freq()
  28. * of struct devfreq_passive_data.
  29. */
  30. if (p_data->get_target_freq) {
  31. ret = p_data->get_target_freq(devfreq, freq);
  32. goto out;
  33. }
  34. /*
  35. * If the parent and passive devfreq device uses the OPP table,
  36. * get the next frequency by using the OPP table.
  37. */
  38. /*
  39. * - parent devfreq device uses the governors except for passive.
  40. * - passive devfreq device uses the passive governor.
  41. *
  42. * Each devfreq has the OPP table. After deciding the new frequency
  43. * from the governor of parent devfreq device, the passive governor
  44. * need to get the index of new frequency on OPP table of parent
  45. * device. And then the index is used for getting the suitable
  46. * new frequency for passive devfreq device.
  47. */
  48. if (!devfreq->profile || !devfreq->profile->freq_table
  49. || devfreq->profile->max_state <= 0)
  50. return -EINVAL;
  51. /*
  52. * The passive governor have to get the correct frequency from OPP
  53. * list of parent device. Because in this case, *freq is temporary
  54. * value which is decided by ondemand governor.
  55. */
  56. opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0);
  57. if (IS_ERR(opp)) {
  58. ret = PTR_ERR(opp);
  59. goto out;
  60. }
  61. dev_pm_opp_put(opp);
  62. /*
  63. * Get the OPP table's index of decided freqeuncy by governor
  64. * of parent device.
  65. */
  66. for (i = 0; i < parent_devfreq->profile->max_state; i++)
  67. if (parent_devfreq->profile->freq_table[i] == *freq)
  68. break;
  69. if (i == parent_devfreq->profile->max_state) {
  70. ret = -EINVAL;
  71. goto out;
  72. }
  73. /* Get the suitable frequency by using index of parent device. */
  74. if (i < devfreq->profile->max_state) {
  75. child_freq = devfreq->profile->freq_table[i];
  76. } else {
  77. count = devfreq->profile->max_state;
  78. child_freq = devfreq->profile->freq_table[count - 1];
  79. }
  80. /* Return the suitable frequency for passive device. */
  81. *freq = child_freq;
  82. out:
  83. return ret;
  84. }
  85. static int update_devfreq_passive(struct devfreq *devfreq, unsigned long freq)
  86. {
  87. int ret;
  88. if (!devfreq->governor)
  89. return -EINVAL;
  90. mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
  91. ret = devfreq->governor->get_target_freq(devfreq, &freq);
  92. if (ret < 0)
  93. goto out;
  94. ret = devfreq->profile->target(devfreq->dev.parent, &freq, 0);
  95. if (ret < 0)
  96. goto out;
  97. if (devfreq->profile->freq_table
  98. && (devfreq_update_status(devfreq, freq)))
  99. dev_err(&devfreq->dev,
  100. "Couldn't update frequency transition information.\n");
  101. devfreq->previous_freq = freq;
  102. out:
  103. mutex_unlock(&devfreq->lock);
  104. return 0;
  105. }
  106. static int devfreq_passive_notifier_call(struct notifier_block *nb,
  107. unsigned long event, void *ptr)
  108. {
  109. struct devfreq_passive_data *data
  110. = container_of(nb, struct devfreq_passive_data, nb);
  111. struct devfreq *devfreq = (struct devfreq *)data->this;
  112. struct devfreq *parent = (struct devfreq *)data->parent;
  113. struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr;
  114. unsigned long freq = freqs->new;
  115. switch (event) {
  116. case DEVFREQ_PRECHANGE:
  117. if (parent->previous_freq > freq)
  118. update_devfreq_passive(devfreq, freq);
  119. break;
  120. case DEVFREQ_POSTCHANGE:
  121. if (parent->previous_freq < freq)
  122. update_devfreq_passive(devfreq, freq);
  123. break;
  124. }
  125. return NOTIFY_DONE;
  126. }
  127. static int devfreq_passive_event_handler(struct devfreq *devfreq,
  128. unsigned int event, void *data)
  129. {
  130. struct devfreq_passive_data *p_data
  131. = (struct devfreq_passive_data *)devfreq->data;
  132. struct devfreq *parent = (struct devfreq *)p_data->parent;
  133. struct notifier_block *nb = &p_data->nb;
  134. int ret = 0;
  135. if (!parent)
  136. return -EPROBE_DEFER;
  137. switch (event) {
  138. case DEVFREQ_GOV_START:
  139. if (!p_data->this)
  140. p_data->this = devfreq;
  141. nb->notifier_call = devfreq_passive_notifier_call;
  142. ret = devfreq_register_notifier(parent, nb,
  143. DEVFREQ_TRANSITION_NOTIFIER);
  144. break;
  145. case DEVFREQ_GOV_STOP:
  146. WARN_ON(devfreq_unregister_notifier(parent, nb,
  147. DEVFREQ_TRANSITION_NOTIFIER));
  148. break;
  149. default:
  150. break;
  151. }
  152. return ret;
  153. }
  154. static struct devfreq_governor devfreq_passive = {
  155. .name = "passive",
  156. .immutable = 1,
  157. .get_target_freq = devfreq_passive_get_target_freq,
  158. .event_handler = devfreq_passive_event_handler,
  159. };
  160. static int __init devfreq_passive_init(void)
  161. {
  162. return devfreq_add_governor(&devfreq_passive);
  163. }
  164. subsys_initcall(devfreq_passive_init);
  165. static void __exit devfreq_passive_exit(void)
  166. {
  167. int ret;
  168. ret = devfreq_remove_governor(&devfreq_passive);
  169. if (ret)
  170. pr_err("%s: failed remove governor %d\n", __func__, ret);
  171. }
  172. module_exit(devfreq_passive_exit);
  173. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  174. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  175. MODULE_DESCRIPTION("DEVFREQ Passive governor");
  176. MODULE_LICENSE("GPL v2");