devfreq.h 8.6 KB

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