governor_passive.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. rcu_read_lock();
  57. opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0);
  58. rcu_read_unlock();
  59. if (IS_ERR(opp)) {
  60. ret = PTR_ERR(opp);
  61. goto out;
  62. }
  63. /*
  64. * Get the OPP table's index of decided freqeuncy by governor
  65. * of parent device.
  66. */
  67. for (i = 0; i < parent_devfreq->profile->max_state; i++)
  68. if (parent_devfreq->profile->freq_table[i] == *freq)
  69. break;
  70. if (i == parent_devfreq->profile->max_state) {
  71. ret = -EINVAL;
  72. goto out;
  73. }
  74. /* Get the suitable frequency by using index of parent device. */
  75. if (i < devfreq->profile->max_state) {
  76. child_freq = devfreq->profile->freq_table[i];
  77. } else {
  78. count = devfreq->profile->max_state;
  79. child_freq = devfreq->profile->freq_table[count - 1];
  80. }
  81. /* Return the suitable frequency for passive device. */
  82. *freq = child_freq;
  83. out:
  84. return ret;
  85. }
  86. static int update_devfreq_passive(struct devfreq *devfreq, unsigned long freq)
  87. {
  88. int ret;
  89. if (!devfreq->governor)
  90. return -EINVAL;
  91. mutex_lock_nested(&devfreq->lock, SINGLE_DEPTH_NESTING);
  92. ret = devfreq->governor->get_target_freq(devfreq, &freq);
  93. if (ret < 0)
  94. goto out;
  95. ret = devfreq->profile->target(devfreq->dev.parent, &freq, 0);
  96. if (ret < 0)
  97. goto out;
  98. if (devfreq->profile->freq_table
  99. && (devfreq_update_status(devfreq, freq)))
  100. dev_err(&devfreq->dev,
  101. "Couldn't update frequency transition information.\n");
  102. devfreq->previous_freq = freq;
  103. out:
  104. mutex_unlock(&devfreq->lock);
  105. return 0;
  106. }
  107. static int devfreq_passive_notifier_call(struct notifier_block *nb,
  108. unsigned long event, void *ptr)
  109. {
  110. struct devfreq_passive_data *data
  111. = container_of(nb, struct devfreq_passive_data, nb);
  112. struct devfreq *devfreq = (struct devfreq *)data->this;
  113. struct devfreq *parent = (struct devfreq *)data->parent;
  114. struct devfreq_freqs *freqs = (struct devfreq_freqs *)ptr;
  115. unsigned long freq = freqs->new;
  116. switch (event) {
  117. case DEVFREQ_PRECHANGE:
  118. if (parent->previous_freq > freq)
  119. update_devfreq_passive(devfreq, freq);
  120. break;
  121. case DEVFREQ_POSTCHANGE:
  122. if (parent->previous_freq < freq)
  123. update_devfreq_passive(devfreq, freq);
  124. break;
  125. }
  126. return NOTIFY_DONE;
  127. }
  128. static int devfreq_passive_event_handler(struct devfreq *devfreq,
  129. unsigned int event, void *data)
  130. {
  131. struct device *dev = devfreq->dev.parent;
  132. struct devfreq_passive_data *p_data
  133. = (struct devfreq_passive_data *)devfreq->data;
  134. struct devfreq *parent = (struct devfreq *)p_data->parent;
  135. struct notifier_block *nb = &p_data->nb;
  136. int ret = 0;
  137. if (!parent)
  138. return -EPROBE_DEFER;
  139. switch (event) {
  140. case DEVFREQ_GOV_START:
  141. if (!p_data->this)
  142. p_data->this = devfreq;
  143. nb->notifier_call = devfreq_passive_notifier_call;
  144. ret = devm_devfreq_register_notifier(dev, parent, nb,
  145. DEVFREQ_TRANSITION_NOTIFIER);
  146. break;
  147. case DEVFREQ_GOV_STOP:
  148. devm_devfreq_unregister_notifier(dev, parent, nb,
  149. DEVFREQ_TRANSITION_NOTIFIER);
  150. break;
  151. default:
  152. break;
  153. }
  154. return ret;
  155. }
  156. static struct devfreq_governor devfreq_passive = {
  157. .name = "passive",
  158. .immutable = 1,
  159. .get_target_freq = devfreq_passive_get_target_freq,
  160. .event_handler = devfreq_passive_event_handler,
  161. };
  162. static int __init devfreq_passive_init(void)
  163. {
  164. return devfreq_add_governor(&devfreq_passive);
  165. }
  166. subsys_initcall(devfreq_passive_init);
  167. static void __exit devfreq_passive_exit(void)
  168. {
  169. int ret;
  170. ret = devfreq_remove_governor(&devfreq_passive);
  171. if (ret)
  172. pr_err("%s: failed remove governor %d\n", __func__, ret);
  173. }
  174. module_exit(devfreq_passive_exit);
  175. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  176. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  177. MODULE_DESCRIPTION("DEVFREQ Passive governor");
  178. MODULE_LICENSE("GPL v2");