governor_passive.c 5.3 KB

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