devfreq.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * @get_cur_freq: The device should provide the current frequency
  63. * at which it is operating.
  64. * @exit: An optional callback that is called when devfreq
  65. * is removing the devfreq object due to error or
  66. * from devfreq_remove_device() call. If the user
  67. * has registered devfreq->nb at a notifier-head,
  68. * this is the time to unregister it.
  69. * @freq_table: Optional list of frequencies to support statistics.
  70. * @max_state: The size of freq_table.
  71. */
  72. struct devfreq_dev_profile {
  73. unsigned long initial_freq;
  74. unsigned int polling_ms;
  75. int (*target)(struct device *dev, unsigned long *freq, u32 flags);
  76. int (*get_dev_status)(struct device *dev,
  77. struct devfreq_dev_status *stat);
  78. int (*get_cur_freq)(struct device *dev, unsigned long *freq);
  79. void (*exit)(struct device *dev);
  80. unsigned int *freq_table;
  81. unsigned int max_state;
  82. };
  83. /**
  84. * struct devfreq_governor - Devfreq policy governor
  85. * @name: Governor's name
  86. * @get_target_freq: Returns desired operating frequency for the device.
  87. * Basically, get_target_freq will run
  88. * devfreq_dev_profile.get_dev_status() to get the
  89. * status of the device (load = busy_time / total_time).
  90. * If no_central_polling is set, this callback is called
  91. * only with update_devfreq() notified by OPP.
  92. * @event_handler: Callback for devfreq core framework to notify events
  93. * to governors. Events include per device governor
  94. * init and exit, opp changes out of devfreq, suspend
  95. * and resume of per device devfreq during device idle.
  96. *
  97. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  98. */
  99. struct devfreq_governor {
  100. const char name[DEVFREQ_NAME_LEN];
  101. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  102. int (*event_handler)(struct devfreq *devfreq,
  103. unsigned int event, void *data);
  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. * @work: delayed work for load monitoring.
  118. * @previous_freq: previously configured frequency value.
  119. * @data: Private data of the governor. The devfreq framework does not
  120. * touch this.
  121. * @min_freq: Limit minimum frequency requested by user (0: none)
  122. * @max_freq: Limit maximum frequency requested by user (0: none)
  123. * @stop_polling: devfreq polling status of a device.
  124. * @total_trans: Number of devfreq transitions
  125. * @trans_table: Statistics of devfreq transitions
  126. * @time_in_state: Statistics of devfreq states
  127. * @last_stat_updated: The last time stat updated
  128. *
  129. * This structure stores the devfreq information for a give device.
  130. *
  131. * Note that when a governor accesses entries in struct devfreq in its
  132. * functions except for the context of callbacks defined in struct
  133. * devfreq_governor, the governor should protect its access with the
  134. * struct mutex lock in struct devfreq. A governor may use this mutex
  135. * to protect its own private data in void *data as well.
  136. */
  137. struct devfreq {
  138. struct list_head node;
  139. struct mutex lock;
  140. struct device dev;
  141. struct devfreq_dev_profile *profile;
  142. const struct devfreq_governor *governor;
  143. struct notifier_block nb;
  144. struct delayed_work work;
  145. unsigned long previous_freq;
  146. void *data; /* private data for governors */
  147. unsigned long min_freq;
  148. unsigned long max_freq;
  149. bool stop_polling;
  150. /* information for device freqeuncy transition */
  151. unsigned int total_trans;
  152. unsigned int *trans_table;
  153. unsigned long *time_in_state;
  154. unsigned long last_stat_updated;
  155. };
  156. #if defined(CONFIG_PM_DEVFREQ)
  157. extern struct devfreq *devfreq_add_device(struct device *dev,
  158. struct devfreq_dev_profile *profile,
  159. const struct devfreq_governor *governor,
  160. void *data);
  161. extern int devfreq_remove_device(struct devfreq *devfreq);
  162. extern int devfreq_suspend_device(struct devfreq *devfreq);
  163. extern int devfreq_resume_device(struct devfreq *devfreq);
  164. /* Helper functions for devfreq user device driver with OPP. */
  165. extern struct opp *devfreq_recommended_opp(struct device *dev,
  166. unsigned long *freq, u32 flags);
  167. extern int devfreq_register_opp_notifier(struct device *dev,
  168. struct devfreq *devfreq);
  169. extern int devfreq_unregister_opp_notifier(struct device *dev,
  170. struct devfreq *devfreq);
  171. #ifdef CONFIG_DEVFREQ_GOV_POWERSAVE
  172. extern const struct devfreq_governor devfreq_powersave;
  173. #endif
  174. #ifdef CONFIG_DEVFREQ_GOV_PERFORMANCE
  175. extern const struct devfreq_governor devfreq_performance;
  176. #endif
  177. #ifdef CONFIG_DEVFREQ_GOV_USERSPACE
  178. extern const struct devfreq_governor devfreq_userspace;
  179. #endif
  180. #ifdef CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND
  181. extern const struct devfreq_governor devfreq_simple_ondemand;
  182. /**
  183. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  184. * and devfreq_add_device
  185. * @upthreshold: If the load is over this value, the frequency jumps.
  186. * Specify 0 to use the default. Valid value = 0 to 100.
  187. * @downdifferential: If the load is under upthreshold - downdifferential,
  188. * the governor may consider slowing the frequency down.
  189. * Specify 0 to use the default. Valid value = 0 to 100.
  190. * downdifferential < upthreshold must hold.
  191. *
  192. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  193. * the governor uses the default values.
  194. */
  195. struct devfreq_simple_ondemand_data {
  196. unsigned int upthreshold;
  197. unsigned int downdifferential;
  198. };
  199. #endif
  200. #else /* !CONFIG_PM_DEVFREQ */
  201. static struct devfreq *devfreq_add_device(struct device *dev,
  202. struct devfreq_dev_profile *profile,
  203. struct devfreq_governor *governor,
  204. void *data)
  205. {
  206. return NULL;
  207. }
  208. static int devfreq_remove_device(struct devfreq *devfreq)
  209. {
  210. return 0;
  211. }
  212. static int devfreq_suspend_device(struct devfreq *devfreq)
  213. {
  214. return 0;
  215. }
  216. static int devfreq_resume_device(struct devfreq *devfreq)
  217. {
  218. return 0;
  219. }
  220. static struct opp *devfreq_recommended_opp(struct device *dev,
  221. unsigned long *freq, u32 flags)
  222. {
  223. return -EINVAL;
  224. }
  225. static int devfreq_register_opp_notifier(struct device *dev,
  226. struct devfreq *devfreq)
  227. {
  228. return -EINVAL;
  229. }
  230. static int devfreq_unregister_opp_notifier(struct device *dev,
  231. struct devfreq *devfreq)
  232. {
  233. return -EINVAL;
  234. }
  235. #define devfreq_powersave NULL
  236. #define devfreq_performance NULL
  237. #define devfreq_userspace NULL
  238. #define devfreq_simple_ondemand NULL
  239. #endif /* CONFIG_PM_DEVFREQ */
  240. #endif /* __LINUX_DEVFREQ_H__ */