devfreq.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
  3. * for Non-CPU Devices.
  4. *
  5. * Copyright (C) 2011 Samsung Electronics
  6. * 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. #ifndef __LINUX_DEVFREQ_H__
  13. #define __LINUX_DEVFREQ_H__
  14. #include <linux/device.h>
  15. #include <linux/notifier.h>
  16. #include <linux/opp.h>
  17. #define DEVFREQ_NAME_LEN 16
  18. struct devfreq;
  19. /**
  20. * struct devfreq_dev_status - Data given from devfreq user device to
  21. * governors. Represents the performance
  22. * statistics.
  23. * @total_time The total time represented by this instance of
  24. * devfreq_dev_status
  25. * @busy_time The time that the device was working among the
  26. * total_time.
  27. * @current_frequency The operating frequency.
  28. * @private_data An entry not specified by the devfreq framework.
  29. * A device and a specific governor may have their
  30. * own protocol with private_data. However, because
  31. * this is governor-specific, a governor using this
  32. * will be only compatible with devices aware of it.
  33. */
  34. struct devfreq_dev_status {
  35. /* both since the last measure */
  36. unsigned long total_time;
  37. unsigned long busy_time;
  38. unsigned long current_frequency;
  39. void *private_data;
  40. };
  41. /**
  42. * struct devfreq_dev_profile - Devfreq's user device profile
  43. * @initial_freq The operating frequency when devfreq_add_device() is
  44. * called.
  45. * @polling_ms The polling interval in ms. 0 disables polling.
  46. * @target The device should set its operating frequency at
  47. * freq or lowest-upper-than-freq value. If freq is
  48. * higher than any operable frequency, set maximum.
  49. * Before returning, target function should set
  50. * freq at the current frequency.
  51. * @get_dev_status The device should provide the current performance
  52. * status to devfreq, which is used by governors.
  53. * @exit An optional callback that is called when devfreq
  54. * is removing the devfreq object due to error or
  55. * from devfreq_remove_device() call. If the user
  56. * has registered devfreq->nb at a notifier-head,
  57. * this is the time to unregister it.
  58. */
  59. struct devfreq_dev_profile {
  60. unsigned long initial_freq;
  61. unsigned int polling_ms;
  62. int (*target)(struct device *dev, unsigned long *freq);
  63. int (*get_dev_status)(struct device *dev,
  64. struct devfreq_dev_status *stat);
  65. void (*exit)(struct device *dev);
  66. };
  67. /**
  68. * struct devfreq_governor - Devfreq policy governor
  69. * @name Governor's name
  70. * @get_target_freq Returns desired operating frequency for the device.
  71. * Basically, get_target_freq will run
  72. * devfreq_dev_profile.get_dev_status() to get the
  73. * status of the device (load = busy_time / total_time).
  74. * If no_central_polling is set, this callback is called
  75. * only with update_devfreq() notified by OPP.
  76. * @init Called when the devfreq is being attached to a device
  77. * @exit Called when the devfreq is being removed from a
  78. * device. Governor should stop any internal routines
  79. * before return because related data may be
  80. * freed after exit().
  81. * @no_central_polling Do not use devfreq's central polling mechanism.
  82. * When this is set, devfreq will not call
  83. * get_target_freq with devfreq_monitor(). However,
  84. * devfreq will call get_target_freq with
  85. * devfreq_update() notified by OPP framework.
  86. *
  87. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  88. */
  89. struct devfreq_governor {
  90. const char name[DEVFREQ_NAME_LEN];
  91. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  92. int (*init)(struct devfreq *this);
  93. void (*exit)(struct devfreq *this);
  94. const bool no_central_polling;
  95. };
  96. /**
  97. * struct devfreq - Device devfreq structure
  98. * @node list node - contains the devices with devfreq that have been
  99. * registered.
  100. * @lock a mutex to protect accessing devfreq.
  101. * @dev device registered by devfreq class. dev.parent is the device
  102. * using devfreq.
  103. * @profile device-specific devfreq profile
  104. * @governor method how to choose frequency based on the usage.
  105. * @nb notifier block used to notify devfreq object that it should
  106. * reevaluate operable frequencies. Devfreq users may use
  107. * devfreq.nb to the corresponding register notifier call chain.
  108. * @polling_jiffies interval in jiffies.
  109. * @previous_freq previously configured frequency value.
  110. * @next_polling the number of remaining jiffies to poll with
  111. * "devfreq_monitor" executions to reevaluate
  112. * frequency/voltage of the device. Set by
  113. * profile's polling_ms interval.
  114. * @data Private data of the governor. The devfreq framework does not
  115. * touch this.
  116. * @being_removed a flag to mark that this object is being removed in
  117. * order to prevent trying to remove the object multiple times.
  118. * @min_freq Limit minimum frequency requested by user (0: none)
  119. * @max_freq Limit maximum frequency requested by user (0: none)
  120. *
  121. * This structure stores the devfreq information for a give device.
  122. *
  123. * Note that when a governor accesses entries in struct devfreq in its
  124. * functions except for the context of callbacks defined in struct
  125. * devfreq_governor, the governor should protect its access with the
  126. * struct mutex lock in struct devfreq. A governor may use this mutex
  127. * to protect its own private data in void *data as well.
  128. */
  129. struct devfreq {
  130. struct list_head node;
  131. struct mutex lock;
  132. struct device dev;
  133. struct devfreq_dev_profile *profile;
  134. const struct devfreq_governor *governor;
  135. struct notifier_block nb;
  136. unsigned long polling_jiffies;
  137. unsigned long previous_freq;
  138. unsigned int next_polling;
  139. void *data; /* private data for governors */
  140. bool being_removed;
  141. unsigned long min_freq;
  142. unsigned long max_freq;
  143. };
  144. #if defined(CONFIG_PM_DEVFREQ)
  145. extern struct devfreq *devfreq_add_device(struct device *dev,
  146. struct devfreq_dev_profile *profile,
  147. const struct devfreq_governor *governor,
  148. void *data);
  149. extern int devfreq_remove_device(struct devfreq *devfreq);
  150. /* Helper functions for devfreq user device driver with OPP. */
  151. extern struct opp *devfreq_recommended_opp(struct device *dev,
  152. unsigned long *freq);
  153. extern int devfreq_register_opp_notifier(struct device *dev,
  154. struct devfreq *devfreq);
  155. extern int devfreq_unregister_opp_notifier(struct device *dev,
  156. struct devfreq *devfreq);
  157. #ifdef CONFIG_DEVFREQ_GOV_POWERSAVE
  158. extern const struct devfreq_governor devfreq_powersave;
  159. #endif
  160. #ifdef CONFIG_DEVFREQ_GOV_PERFORMANCE
  161. extern const struct devfreq_governor devfreq_performance;
  162. #endif
  163. #ifdef CONFIG_DEVFREQ_GOV_USERSPACE
  164. extern const struct devfreq_governor devfreq_userspace;
  165. #endif
  166. #ifdef CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND
  167. extern const struct devfreq_governor devfreq_simple_ondemand;
  168. /**
  169. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  170. * and devfreq_add_device
  171. * @ upthreshold If the load is over this value, the frequency jumps.
  172. * Specify 0 to use the default. Valid value = 0 to 100.
  173. * @ downdifferential If the load is under upthreshold - downdifferential,
  174. * the governor may consider slowing the frequency down.
  175. * Specify 0 to use the default. Valid value = 0 to 100.
  176. * downdifferential < upthreshold must hold.
  177. *
  178. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  179. * the governor uses the default values.
  180. */
  181. struct devfreq_simple_ondemand_data {
  182. unsigned int upthreshold;
  183. unsigned int downdifferential;
  184. };
  185. #endif
  186. #else /* !CONFIG_PM_DEVFREQ */
  187. static struct devfreq *devfreq_add_device(struct device *dev,
  188. struct devfreq_dev_profile *profile,
  189. struct devfreq_governor *governor,
  190. void *data)
  191. {
  192. return NULL;
  193. }
  194. static int devfreq_remove_device(struct devfreq *devfreq)
  195. {
  196. return 0;
  197. }
  198. static struct opp *devfreq_recommended_opp(struct device *dev,
  199. unsigned long *freq)
  200. {
  201. return -EINVAL;
  202. }
  203. static int devfreq_register_opp_notifier(struct device *dev,
  204. struct devfreq *devfreq)
  205. {
  206. return -EINVAL;
  207. }
  208. static int devfreq_unregister_opp_notifier(struct device *dev,
  209. struct devfreq *devfreq)
  210. {
  211. return -EINVAL;
  212. }
  213. #define devfreq_powersave NULL
  214. #define devfreq_performance NULL
  215. #define devfreq_userspace NULL
  216. #define devfreq_simple_ondemand NULL
  217. #endif /* CONFIG_PM_DEVFREQ */
  218. #endif /* __LINUX_DEVFREQ_H__ */