devfreq.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #define DEVFREQ_FLAG_FAST_HINT 0x2
  49. #define DEVFREQ_FLAG_SLOW_HINT 0x4
  50. #define DEVFREQ_FLAG_WAKEUP_MAXFREQ 0x8
  51. /**
  52. * struct devfreq_governor_data - mapping to per device governor data
  53. * @name: The name of the governor.
  54. * @data: Private data for the governor.
  55. *
  56. * Devices may pass in an array of this structure to allow governors
  57. * to get the correct data pointer when they are enabled after
  58. * the devfreq_add_device() call.
  59. */
  60. struct devfreq_governor_data {
  61. const char *name;
  62. void *data;
  63. };
  64. /**
  65. * struct devfreq_dev_profile - Devfreq's user device profile
  66. * @initial_freq: The operating frequency when devfreq_add_device() is
  67. * called.
  68. * @polling_ms: The polling interval in ms. 0 disables polling.
  69. * @target: The device should set its operating frequency at
  70. * freq or lowest-upper-than-freq value. If freq is
  71. * higher than any operable frequency, set maximum.
  72. * Before returning, target function should set
  73. * freq at the current frequency.
  74. * The "flags" parameter's possible values are
  75. * explained above with "DEVFREQ_FLAG_*" macros.
  76. * @get_dev_status: The device should provide the current performance
  77. * status to devfreq, which is used by governors.
  78. * @get_cur_freq: The device should provide the current frequency
  79. * at which it is operating.
  80. * @exit: An optional callback that is called when devfreq
  81. * is removing the devfreq object due to error or
  82. * from devfreq_remove_device() call. If the user
  83. * has registered devfreq->nb at a notifier-head,
  84. * this is the time to unregister it.
  85. * @freq_table: Optional list of frequencies to support statistics.
  86. * @max_state: The size of freq_table.
  87. * @governor_data: Optional array of private data for governors.
  88. * This is used to set devfreq->data correctly
  89. * when a governor is enabled via sysfs or other
  90. * mechanisms after the devfreq_add_device() call.
  91. * @num_governor_data: Number of elements in governor_data.
  92. */
  93. struct devfreq_dev_profile {
  94. unsigned long initial_freq;
  95. unsigned int polling_ms;
  96. int (*target)(struct device *dev, unsigned long *freq, u32 flags);
  97. int (*get_dev_status)(struct device *dev,
  98. struct devfreq_dev_status *stat);
  99. int (*get_cur_freq)(struct device *dev, unsigned long *freq);
  100. void (*exit)(struct device *dev);
  101. unsigned int *freq_table;
  102. unsigned int max_state;
  103. const struct devfreq_governor_data *governor_data;
  104. unsigned int num_governor_data;
  105. };
  106. /**
  107. * struct devfreq_governor - Devfreq policy governor
  108. * @node: list node - contains registered devfreq governors
  109. * @name: Governor's name
  110. * @get_target_freq: Returns desired operating frequency for the device.
  111. * Basically, get_target_freq will run
  112. * devfreq_dev_profile.get_dev_status() to get the
  113. * status of the device (load = busy_time / total_time).
  114. * If no_central_polling is set, this callback is called
  115. * only with update_devfreq() notified by OPP.
  116. * @event_handler: Callback for devfreq core framework to notify events
  117. * to governors. Events include per device governor
  118. * init and exit, opp changes out of devfreq, suspend
  119. * and resume of per device devfreq during device idle.
  120. *
  121. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  122. */
  123. struct devfreq_governor {
  124. struct list_head node;
  125. const char name[DEVFREQ_NAME_LEN];
  126. int (*get_target_freq)(struct devfreq *this, unsigned long *freq,
  127. u32 *flag);
  128. int (*event_handler)(struct devfreq *devfreq,
  129. unsigned int event, void *data);
  130. };
  131. /**
  132. * struct devfreq - Device devfreq structure
  133. * @node: list node - contains the devices with devfreq that have been
  134. * registered.
  135. * @lock: a mutex to protect accessing devfreq.
  136. * @dev: device registered by devfreq class. dev.parent is the device
  137. * using devfreq.
  138. * @profile: device-specific devfreq profile
  139. * @governor: method how to choose frequency based on the usage.
  140. * @governor_name: devfreq governor name for use with this devfreq
  141. * @nb: notifier block used to notify devfreq object that it should
  142. * reevaluate operable frequencies. Devfreq users may use
  143. * devfreq.nb to the corresponding register notifier call chain.
  144. * @work: delayed work for load monitoring.
  145. * @previous_freq: previously configured frequency value.
  146. * @data: Private data of the governor. The devfreq framework does not
  147. * touch this.
  148. * @min_freq: Limit minimum frequency requested by user (0: none)
  149. * @max_freq: Limit maximum frequency requested by user (0: none)
  150. * @stop_polling: devfreq polling status of a device.
  151. * @total_trans: Number of devfreq transitions
  152. * @trans_table: Statistics of devfreq transitions
  153. * @time_in_state: Statistics of devfreq states
  154. * @last_stat_updated: The last time stat updated
  155. *
  156. * This structure stores the devfreq information for a give device.
  157. *
  158. * Note that when a governor accesses entries in struct devfreq in its
  159. * functions except for the context of callbacks defined in struct
  160. * devfreq_governor, the governor should protect its access with the
  161. * struct mutex lock in struct devfreq. A governor may use this mutex
  162. * to protect its own private data in void *data as well.
  163. */
  164. struct devfreq {
  165. struct list_head node;
  166. struct mutex lock;
  167. struct device dev;
  168. struct devfreq_dev_profile *profile;
  169. const struct devfreq_governor *governor;
  170. char governor_name[DEVFREQ_NAME_LEN];
  171. struct notifier_block nb;
  172. struct delayed_work work;
  173. unsigned long previous_freq;
  174. void *data; /* private data for governors */
  175. unsigned long min_freq;
  176. unsigned long max_freq;
  177. bool stop_polling;
  178. /* information for device freqeuncy transition */
  179. unsigned int total_trans;
  180. unsigned int *trans_table;
  181. unsigned long *time_in_state;
  182. unsigned long last_stat_updated;
  183. };
  184. #if defined(CONFIG_PM_DEVFREQ)
  185. extern struct devfreq *devfreq_add_device(struct device *dev,
  186. struct devfreq_dev_profile *profile,
  187. const char *governor_name,
  188. void *data);
  189. extern int devfreq_remove_device(struct devfreq *devfreq);
  190. extern int devfreq_suspend_device(struct devfreq *devfreq);
  191. extern int devfreq_resume_device(struct devfreq *devfreq);
  192. /* Helper functions for devfreq user device driver with OPP. */
  193. extern struct opp *devfreq_recommended_opp(struct device *dev,
  194. unsigned long *freq, u32 flags);
  195. extern int devfreq_register_opp_notifier(struct device *dev,
  196. struct devfreq *devfreq);
  197. extern int devfreq_unregister_opp_notifier(struct device *dev,
  198. struct devfreq *devfreq);
  199. #ifdef CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND
  200. /**
  201. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  202. * and devfreq_add_device
  203. * @upthreshold: If the load is over this value, the frequency jumps.
  204. * Specify 0 to use the default. Valid value = 0 to 100.
  205. * @downdifferential: If the load is under upthreshold - downdifferential,
  206. * the governor may consider slowing the frequency down.
  207. * Specify 0 to use the default. Valid value = 0 to 100.
  208. * downdifferential < upthreshold must hold.
  209. * @simple_scaling: Setting this flag will scale the clocks up only if the
  210. * load is above @upthreshold and will scale the clocks
  211. * down only if the load is below @downdifferential.
  212. *
  213. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  214. * the governor uses the default values.
  215. */
  216. struct devfreq_simple_ondemand_data {
  217. unsigned int upthreshold;
  218. unsigned int downdifferential;
  219. unsigned int simple_scaling;
  220. };
  221. #endif
  222. #else /* !CONFIG_PM_DEVFREQ */
  223. static struct devfreq *devfreq_add_device(struct device *dev,
  224. struct devfreq_dev_profile *profile,
  225. const char *governor_name,
  226. void *data)
  227. {
  228. return NULL;
  229. }
  230. static int devfreq_remove_device(struct devfreq *devfreq)
  231. {
  232. return 0;
  233. }
  234. static int devfreq_suspend_device(struct devfreq *devfreq)
  235. {
  236. return 0;
  237. }
  238. static int devfreq_resume_device(struct devfreq *devfreq)
  239. {
  240. return 0;
  241. }
  242. static struct opp *devfreq_recommended_opp(struct device *dev,
  243. unsigned long *freq, u32 flags)
  244. {
  245. return -EINVAL;
  246. }
  247. static int devfreq_register_opp_notifier(struct device *dev,
  248. struct devfreq *devfreq)
  249. {
  250. return -EINVAL;
  251. }
  252. static int devfreq_unregister_opp_notifier(struct device *dev,
  253. struct devfreq *devfreq)
  254. {
  255. return -EINVAL;
  256. }
  257. #endif /* CONFIG_PM_DEVFREQ */
  258. #endif /* __LINUX_DEVFREQ_H__ */