devfreq.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/pm_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. Governors are recommended not to
  62. * use this directly. Instead, governors are recommended
  63. * to use devfreq_update_stats() along with
  64. * devfreq.last_status.
  65. * @get_cur_freq: The device should provide the current frequency
  66. * at which it is operating.
  67. * @exit: An optional callback that is called when devfreq
  68. * is removing the devfreq object due to error or
  69. * from devfreq_remove_device() call. If the user
  70. * has registered devfreq->nb at a notifier-head,
  71. * this is the time to unregister it.
  72. * @freq_table: Optional list of frequencies to support statistics.
  73. * @max_state: The size of freq_table.
  74. */
  75. struct devfreq_dev_profile {
  76. unsigned long initial_freq;
  77. unsigned int polling_ms;
  78. int (*target)(struct device *dev, unsigned long *freq, u32 flags);
  79. int (*get_dev_status)(struct device *dev,
  80. struct devfreq_dev_status *stat);
  81. int (*get_cur_freq)(struct device *dev, unsigned long *freq);
  82. void (*exit)(struct device *dev);
  83. unsigned int *freq_table;
  84. unsigned int max_state;
  85. };
  86. /**
  87. * struct devfreq_governor - Devfreq policy governor
  88. * @node: list node - contains registered devfreq governors
  89. * @name: Governor's name
  90. * @get_target_freq: Returns desired operating frequency for the device.
  91. * Basically, get_target_freq will run
  92. * devfreq_dev_profile.get_dev_status() to get the
  93. * status of the device (load = busy_time / total_time).
  94. * If no_central_polling is set, this callback is called
  95. * only with update_devfreq() notified by OPP.
  96. * @event_handler: Callback for devfreq core framework to notify events
  97. * to governors. Events include per device governor
  98. * init and exit, opp changes out of devfreq, suspend
  99. * and resume of per device devfreq during device idle.
  100. *
  101. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  102. */
  103. struct devfreq_governor {
  104. struct list_head node;
  105. const char name[DEVFREQ_NAME_LEN];
  106. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  107. int (*event_handler)(struct devfreq *devfreq,
  108. unsigned int event, void *data);
  109. };
  110. /**
  111. * struct devfreq - Device devfreq structure
  112. * @node: list node - contains the devices with devfreq that have been
  113. * registered.
  114. * @lock: a mutex to protect accessing devfreq.
  115. * @dev: device registered by devfreq class. dev.parent is the device
  116. * using devfreq.
  117. * @profile: device-specific devfreq profile
  118. * @governor: method how to choose frequency based on the usage.
  119. * @governor_name: devfreq governor name for use with this devfreq
  120. * @nb: notifier block used to notify devfreq object that it should
  121. * reevaluate operable frequencies. Devfreq users may use
  122. * devfreq.nb to the corresponding register notifier call chain.
  123. * @work: delayed work for load monitoring.
  124. * @previous_freq: previously configured frequency value.
  125. * @data: Private data of the governor. The devfreq framework does not
  126. * touch this.
  127. * @min_freq: Limit minimum frequency requested by user (0: none)
  128. * @max_freq: Limit maximum frequency requested by user (0: none)
  129. * @stop_polling: devfreq polling status of a device.
  130. * @total_trans: Number of devfreq transitions
  131. * @trans_table: Statistics of devfreq transitions
  132. * @time_in_state: Statistics of devfreq states
  133. * @last_stat_updated: The last time stat updated
  134. *
  135. * This structure stores the devfreq information for a give device.
  136. *
  137. * Note that when a governor accesses entries in struct devfreq in its
  138. * functions except for the context of callbacks defined in struct
  139. * devfreq_governor, the governor should protect its access with the
  140. * struct mutex lock in struct devfreq. A governor may use this mutex
  141. * to protect its own private data in void *data as well.
  142. */
  143. struct devfreq {
  144. struct list_head node;
  145. struct mutex lock;
  146. struct device dev;
  147. struct devfreq_dev_profile *profile;
  148. const struct devfreq_governor *governor;
  149. char governor_name[DEVFREQ_NAME_LEN];
  150. struct notifier_block nb;
  151. struct delayed_work work;
  152. unsigned long previous_freq;
  153. struct devfreq_dev_status last_status;
  154. void *data; /* private data for governors */
  155. unsigned long min_freq;
  156. unsigned long max_freq;
  157. bool stop_polling;
  158. /* information for device frequency transition */
  159. unsigned int total_trans;
  160. unsigned int *trans_table;
  161. unsigned long *time_in_state;
  162. unsigned long last_stat_updated;
  163. };
  164. #if defined(CONFIG_PM_DEVFREQ)
  165. extern struct devfreq *devfreq_add_device(struct device *dev,
  166. struct devfreq_dev_profile *profile,
  167. const char *governor_name,
  168. void *data);
  169. extern int devfreq_remove_device(struct devfreq *devfreq);
  170. extern struct devfreq *devm_devfreq_add_device(struct device *dev,
  171. struct devfreq_dev_profile *profile,
  172. const char *governor_name,
  173. void *data);
  174. extern void devm_devfreq_remove_device(struct device *dev,
  175. struct devfreq *devfreq);
  176. /* Supposed to be called by PM callbacks */
  177. extern int devfreq_suspend_device(struct devfreq *devfreq);
  178. extern int devfreq_resume_device(struct devfreq *devfreq);
  179. /* Helper functions for devfreq user device driver with OPP. */
  180. extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  181. unsigned long *freq, u32 flags);
  182. extern int devfreq_register_opp_notifier(struct device *dev,
  183. struct devfreq *devfreq);
  184. extern int devfreq_unregister_opp_notifier(struct device *dev,
  185. struct devfreq *devfreq);
  186. extern int devm_devfreq_register_opp_notifier(struct device *dev,
  187. struct devfreq *devfreq);
  188. extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
  189. struct devfreq *devfreq);
  190. /**
  191. * devfreq_update_stats() - update the last_status pointer in struct devfreq
  192. * @df: the devfreq instance whose status needs updating
  193. *
  194. * Governors are recommended to use this function along with last_status,
  195. * which allows other entities to reuse the last_status without affecting
  196. * the values fetched later by governors.
  197. */
  198. static inline int devfreq_update_stats(struct devfreq *df)
  199. {
  200. return df->profile->get_dev_status(df->dev.parent, &df->last_status);
  201. }
  202. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
  203. /**
  204. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  205. * and devfreq_add_device
  206. * @upthreshold: If the load is over this value, the frequency jumps.
  207. * Specify 0 to use the default. Valid value = 0 to 100.
  208. * @downdifferential: If the load is under upthreshold - downdifferential,
  209. * the governor may consider slowing the frequency down.
  210. * Specify 0 to use the default. Valid value = 0 to 100.
  211. * downdifferential < upthreshold must hold.
  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. };
  220. #endif
  221. #else /* !CONFIG_PM_DEVFREQ */
  222. static inline struct devfreq *devfreq_add_device(struct device *dev,
  223. struct devfreq_dev_profile *profile,
  224. const char *governor_name,
  225. void *data)
  226. {
  227. return ERR_PTR(-ENOSYS);
  228. }
  229. static inline int devfreq_remove_device(struct devfreq *devfreq)
  230. {
  231. return 0;
  232. }
  233. static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
  234. struct devfreq_dev_profile *profile,
  235. const char *governor_name,
  236. void *data)
  237. {
  238. return ERR_PTR(-ENOSYS);
  239. }
  240. static inline void devm_devfreq_remove_device(struct device *dev,
  241. struct devfreq *devfreq)
  242. {
  243. }
  244. static inline int devfreq_suspend_device(struct devfreq *devfreq)
  245. {
  246. return 0;
  247. }
  248. static inline int devfreq_resume_device(struct devfreq *devfreq)
  249. {
  250. return 0;
  251. }
  252. static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  253. unsigned long *freq, u32 flags)
  254. {
  255. return ERR_PTR(-EINVAL);
  256. }
  257. static inline int devfreq_register_opp_notifier(struct device *dev,
  258. struct devfreq *devfreq)
  259. {
  260. return -EINVAL;
  261. }
  262. static inline int devfreq_unregister_opp_notifier(struct device *dev,
  263. struct devfreq *devfreq)
  264. {
  265. return -EINVAL;
  266. }
  267. static inline int devm_devfreq_register_opp_notifier(struct device *dev,
  268. struct devfreq *devfreq)
  269. {
  270. return -EINVAL;
  271. }
  272. static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
  273. struct devfreq *devfreq)
  274. {
  275. }
  276. static inline int devfreq_update_stats(struct devfreq *df)
  277. {
  278. return -EINVAL;
  279. }
  280. #endif /* CONFIG_PM_DEVFREQ */
  281. #endif /* __LINUX_DEVFREQ_H__ */