devfreq.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * @event_handler Callback for devfreq core framework to notify events
  86. * to governors. Events include per device governor
  87. * init and exit, opp changes out of devfreq, suspend
  88. * and resume of per device devfreq during device idle.
  89. *
  90. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  91. */
  92. struct devfreq_governor {
  93. const char name[DEVFREQ_NAME_LEN];
  94. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  95. int (*event_handler)(struct devfreq *devfreq,
  96. unsigned int event, void *data);
  97. };
  98. /**
  99. * struct devfreq - Device devfreq structure
  100. * @node list node - contains the devices with devfreq that have been
  101. * registered.
  102. * @lock a mutex to protect accessing devfreq.
  103. * @dev device registered by devfreq class. dev.parent is the device
  104. * using devfreq.
  105. * @profile device-specific devfreq profile
  106. * @governor method how to choose frequency based on the usage.
  107. * @nb notifier block used to notify devfreq object that it should
  108. * reevaluate operable frequencies. Devfreq users may use
  109. * devfreq.nb to the corresponding register notifier call chain.
  110. * @work delayed work for load monitoring.
  111. * @previous_freq previously configured frequency value.
  112. * @data Private data of the governor. The devfreq framework does not
  113. * touch this.
  114. * @min_freq Limit minimum frequency requested by user (0: none)
  115. * @max_freq Limit maximum frequency requested by user (0: none)
  116. * @stop_polling devfreq polling status of a device.
  117. *
  118. * This structure stores the devfreq information for a give device.
  119. *
  120. * Note that when a governor accesses entries in struct devfreq in its
  121. * functions except for the context of callbacks defined in struct
  122. * devfreq_governor, the governor should protect its access with the
  123. * struct mutex lock in struct devfreq. A governor may use this mutex
  124. * to protect its own private data in void *data as well.
  125. */
  126. struct devfreq {
  127. struct list_head node;
  128. struct mutex lock;
  129. struct device dev;
  130. struct devfreq_dev_profile *profile;
  131. const struct devfreq_governor *governor;
  132. struct notifier_block nb;
  133. struct delayed_work work;
  134. unsigned long previous_freq;
  135. void *data; /* private data for governors */
  136. unsigned long min_freq;
  137. unsigned long max_freq;
  138. bool stop_polling;
  139. };
  140. #if defined(CONFIG_PM_DEVFREQ)
  141. extern struct devfreq *devfreq_add_device(struct device *dev,
  142. struct devfreq_dev_profile *profile,
  143. const struct devfreq_governor *governor,
  144. void *data);
  145. extern int devfreq_remove_device(struct devfreq *devfreq);
  146. extern int devfreq_suspend_device(struct devfreq *devfreq);
  147. extern int devfreq_resume_device(struct devfreq *devfreq);
  148. /* Helper functions for devfreq user device driver with OPP. */
  149. extern struct opp *devfreq_recommended_opp(struct device *dev,
  150. unsigned long *freq, u32 flags);
  151. extern int devfreq_register_opp_notifier(struct device *dev,
  152. struct devfreq *devfreq);
  153. extern int devfreq_unregister_opp_notifier(struct device *dev,
  154. struct devfreq *devfreq);
  155. #ifdef CONFIG_DEVFREQ_GOV_POWERSAVE
  156. extern const struct devfreq_governor devfreq_powersave;
  157. #endif
  158. #ifdef CONFIG_DEVFREQ_GOV_PERFORMANCE
  159. extern const struct devfreq_governor devfreq_performance;
  160. #endif
  161. #ifdef CONFIG_DEVFREQ_GOV_USERSPACE
  162. extern const struct devfreq_governor devfreq_userspace;
  163. #endif
  164. #ifdef CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND
  165. extern const struct devfreq_governor devfreq_simple_ondemand;
  166. /**
  167. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  168. * and devfreq_add_device
  169. * @ upthreshold If the load is over this value, the frequency jumps.
  170. * Specify 0 to use the default. Valid value = 0 to 100.
  171. * @ downdifferential If the load is under upthreshold - downdifferential,
  172. * the governor may consider slowing the frequency down.
  173. * Specify 0 to use the default. Valid value = 0 to 100.
  174. * downdifferential < upthreshold must hold.
  175. *
  176. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  177. * the governor uses the default values.
  178. */
  179. struct devfreq_simple_ondemand_data {
  180. unsigned int upthreshold;
  181. unsigned int downdifferential;
  182. };
  183. #endif
  184. #else /* !CONFIG_PM_DEVFREQ */
  185. static struct devfreq *devfreq_add_device(struct device *dev,
  186. struct devfreq_dev_profile *profile,
  187. struct devfreq_governor *governor,
  188. void *data)
  189. {
  190. return NULL;
  191. }
  192. static int devfreq_remove_device(struct devfreq *devfreq)
  193. {
  194. return 0;
  195. }
  196. static int devfreq_suspend_device(struct devfreq *devfreq)
  197. {
  198. return 0;
  199. }
  200. static int devfreq_resume_device(struct devfreq *devfreq)
  201. {
  202. return 0;
  203. }
  204. static struct opp *devfreq_recommended_opp(struct device *dev,
  205. unsigned long *freq, u32 flags)
  206. {
  207. return -EINVAL;
  208. }
  209. static int devfreq_register_opp_notifier(struct device *dev,
  210. struct devfreq *devfreq)
  211. {
  212. return -EINVAL;
  213. }
  214. static int devfreq_unregister_opp_notifier(struct device *dev,
  215. struct devfreq *devfreq)
  216. {
  217. return -EINVAL;
  218. }
  219. #define devfreq_powersave NULL
  220. #define devfreq_performance NULL
  221. #define devfreq_userspace NULL
  222. #define devfreq_simple_ondemand NULL
  223. #endif /* CONFIG_PM_DEVFREQ */
  224. #endif /* __LINUX_DEVFREQ_H__ */