devfreq.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. /* DEVFREQ notifier interface */
  19. #define DEVFREQ_TRANSITION_NOTIFIER (0)
  20. /* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
  21. #define DEVFREQ_PRECHANGE (0)
  22. #define DEVFREQ_POSTCHANGE (1)
  23. struct devfreq;
  24. /**
  25. * struct devfreq_dev_status - Data given from devfreq user device to
  26. * governors. Represents the performance
  27. * statistics.
  28. * @total_time: The total time represented by this instance of
  29. * devfreq_dev_status
  30. * @busy_time: The time that the device was working among the
  31. * total_time.
  32. * @current_frequency: The operating frequency.
  33. * @private_data: An entry not specified by the devfreq framework.
  34. * A device and a specific governor may have their
  35. * own protocol with private_data. However, because
  36. * this is governor-specific, a governor using this
  37. * will be only compatible with devices aware of it.
  38. */
  39. struct devfreq_dev_status {
  40. /* both since the last measure */
  41. unsigned long total_time;
  42. unsigned long busy_time;
  43. unsigned long current_frequency;
  44. void *private_data;
  45. };
  46. /*
  47. * The resulting frequency should be at most this. (this bound is the
  48. * least upper bound; thus, the resulting freq should be lower or same)
  49. * If the flag is not set, the resulting frequency should be at most the
  50. * bound (greatest lower bound)
  51. */
  52. #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
  53. /**
  54. * struct devfreq_dev_profile - Devfreq's user device profile
  55. * @initial_freq: The operating frequency when devfreq_add_device() is
  56. * called.
  57. * @polling_ms: The polling interval in ms. 0 disables polling.
  58. * @target: The device should set its operating frequency at
  59. * freq or lowest-upper-than-freq value. If freq is
  60. * higher than any operable frequency, set maximum.
  61. * Before returning, target function should set
  62. * freq at the current frequency.
  63. * The "flags" parameter's possible values are
  64. * explained above with "DEVFREQ_FLAG_*" macros.
  65. * @get_dev_status: The device should provide the current performance
  66. * status to devfreq. Governors are recommended not to
  67. * use this directly. Instead, governors are recommended
  68. * to use devfreq_update_stats() along with
  69. * devfreq.last_status.
  70. * @get_cur_freq: The device should provide the current frequency
  71. * at which it is operating.
  72. * @exit: An optional callback that is called when devfreq
  73. * is removing the devfreq object due to error or
  74. * from devfreq_remove_device() call. If the user
  75. * has registered devfreq->nb at a notifier-head,
  76. * this is the time to unregister it.
  77. * @freq_table: Optional list of frequencies to support statistics.
  78. * @max_state: The size of freq_table.
  79. */
  80. struct devfreq_dev_profile {
  81. unsigned long initial_freq;
  82. unsigned int polling_ms;
  83. int (*target)(struct device *dev, unsigned long *freq, u32 flags);
  84. int (*get_dev_status)(struct device *dev,
  85. struct devfreq_dev_status *stat);
  86. int (*get_cur_freq)(struct device *dev, unsigned long *freq);
  87. void (*exit)(struct device *dev);
  88. unsigned long *freq_table;
  89. unsigned int max_state;
  90. };
  91. /**
  92. * struct devfreq_governor - Devfreq policy governor
  93. * @node: list node - contains registered devfreq governors
  94. * @name: Governor's name
  95. * @get_target_freq: Returns desired operating frequency for the device.
  96. * Basically, get_target_freq will run
  97. * devfreq_dev_profile.get_dev_status() to get the
  98. * status of the device (load = busy_time / total_time).
  99. * If no_central_polling is set, this callback is called
  100. * only with update_devfreq() notified by OPP.
  101. * @event_handler: Callback for devfreq core framework to notify events
  102. * to governors. Events include per device governor
  103. * init and exit, opp changes out of devfreq, suspend
  104. * and resume of per device devfreq during device idle.
  105. *
  106. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  107. */
  108. struct devfreq_governor {
  109. struct list_head node;
  110. const char name[DEVFREQ_NAME_LEN];
  111. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  112. int (*event_handler)(struct devfreq *devfreq,
  113. unsigned int event, void *data);
  114. };
  115. /**
  116. * struct devfreq - Device devfreq structure
  117. * @node: list node - contains the devices with devfreq that have been
  118. * registered.
  119. * @lock: a mutex to protect accessing devfreq.
  120. * @dev: device registered by devfreq class. dev.parent is the device
  121. * using devfreq.
  122. * @profile: device-specific devfreq profile
  123. * @governor: method how to choose frequency based on the usage.
  124. * @governor_name: devfreq governor name for use with this devfreq
  125. * @nb: notifier block used to notify devfreq object that it should
  126. * reevaluate operable frequencies. Devfreq users may use
  127. * devfreq.nb to the corresponding register notifier call chain.
  128. * @work: delayed work for load monitoring.
  129. * @previous_freq: previously configured frequency value.
  130. * @data: Private data of the governor. The devfreq framework does not
  131. * touch this.
  132. * @min_freq: Limit minimum frequency requested by user (0: none)
  133. * @max_freq: Limit maximum frequency requested by user (0: none)
  134. * @stop_polling: devfreq polling status of a device.
  135. * @total_trans: Number of devfreq transitions
  136. * @trans_table: Statistics of devfreq transitions
  137. * @time_in_state: Statistics of devfreq states
  138. * @last_stat_updated: The last time stat updated
  139. * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
  140. *
  141. * This structure stores the devfreq information for a give device.
  142. *
  143. * Note that when a governor accesses entries in struct devfreq in its
  144. * functions except for the context of callbacks defined in struct
  145. * devfreq_governor, the governor should protect its access with the
  146. * struct mutex lock in struct devfreq. A governor may use this mutex
  147. * to protect its own private data in void *data as well.
  148. */
  149. struct devfreq {
  150. struct list_head node;
  151. struct mutex lock;
  152. struct device dev;
  153. struct devfreq_dev_profile *profile;
  154. const struct devfreq_governor *governor;
  155. char governor_name[DEVFREQ_NAME_LEN];
  156. struct notifier_block nb;
  157. struct delayed_work work;
  158. unsigned long previous_freq;
  159. struct devfreq_dev_status last_status;
  160. void *data; /* private data for governors */
  161. unsigned long min_freq;
  162. unsigned long max_freq;
  163. bool stop_polling;
  164. /* information for device frequency transition */
  165. unsigned int total_trans;
  166. unsigned int *trans_table;
  167. unsigned long *time_in_state;
  168. unsigned long last_stat_updated;
  169. struct srcu_notifier_head transition_notifier_list;
  170. };
  171. struct devfreq_freqs {
  172. unsigned long old;
  173. unsigned long new;
  174. };
  175. #if defined(CONFIG_PM_DEVFREQ)
  176. extern struct devfreq *devfreq_add_device(struct device *dev,
  177. struct devfreq_dev_profile *profile,
  178. const char *governor_name,
  179. void *data);
  180. extern int devfreq_remove_device(struct devfreq *devfreq);
  181. extern struct devfreq *devm_devfreq_add_device(struct device *dev,
  182. struct devfreq_dev_profile *profile,
  183. const char *governor_name,
  184. void *data);
  185. extern void devm_devfreq_remove_device(struct device *dev,
  186. struct devfreq *devfreq);
  187. /* Supposed to be called by PM callbacks */
  188. extern int devfreq_suspend_device(struct devfreq *devfreq);
  189. extern int devfreq_resume_device(struct devfreq *devfreq);
  190. /* Helper functions for devfreq user device driver with OPP. */
  191. extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  192. unsigned long *freq, u32 flags);
  193. extern int devfreq_register_opp_notifier(struct device *dev,
  194. struct devfreq *devfreq);
  195. extern int devfreq_unregister_opp_notifier(struct device *dev,
  196. struct devfreq *devfreq);
  197. extern int devm_devfreq_register_opp_notifier(struct device *dev,
  198. struct devfreq *devfreq);
  199. extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
  200. struct devfreq *devfreq);
  201. extern int devfreq_register_notifier(struct devfreq *devfreq,
  202. struct notifier_block *nb,
  203. unsigned int list);
  204. extern int devfreq_unregister_notifier(struct devfreq *devfreq,
  205. struct notifier_block *nb,
  206. unsigned int list);
  207. extern int devm_devfreq_register_notifier(struct device *dev,
  208. struct devfreq *devfreq,
  209. struct notifier_block *nb,
  210. unsigned int list);
  211. extern void devm_devfreq_unregister_notifier(struct device *dev,
  212. struct devfreq *devfreq,
  213. struct notifier_block *nb,
  214. unsigned int list);
  215. extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  216. int index);
  217. /**
  218. * devfreq_update_stats() - update the last_status pointer in struct devfreq
  219. * @df: the devfreq instance whose status needs updating
  220. *
  221. * Governors are recommended to use this function along with last_status,
  222. * which allows other entities to reuse the last_status without affecting
  223. * the values fetched later by governors.
  224. */
  225. static inline int devfreq_update_stats(struct devfreq *df)
  226. {
  227. return df->profile->get_dev_status(df->dev.parent, &df->last_status);
  228. }
  229. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
  230. /**
  231. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  232. * and devfreq_add_device
  233. * @upthreshold: If the load is over this value, the frequency jumps.
  234. * Specify 0 to use the default. Valid value = 0 to 100.
  235. * @downdifferential: If the load is under upthreshold - downdifferential,
  236. * the governor may consider slowing the frequency down.
  237. * Specify 0 to use the default. Valid value = 0 to 100.
  238. * downdifferential < upthreshold must hold.
  239. *
  240. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  241. * the governor uses the default values.
  242. */
  243. struct devfreq_simple_ondemand_data {
  244. unsigned int upthreshold;
  245. unsigned int downdifferential;
  246. };
  247. #endif
  248. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
  249. /**
  250. * struct devfreq_passive_data - void *data fed to struct devfreq
  251. * and devfreq_add_device
  252. * @parent: the devfreq instance of parent device.
  253. * @get_target_freq: Optional callback, Returns desired operating frequency
  254. * for the device using passive governor. That is called
  255. * when passive governor should decide the next frequency
  256. * by using the new frequency of parent devfreq device
  257. * using governors except for passive governor.
  258. * If the devfreq device has the specific method to decide
  259. * the next frequency, should use this callback.
  260. * @this: the devfreq instance of own device.
  261. * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
  262. *
  263. * The devfreq_passive_data have to set the devfreq instance of parent
  264. * device with governors except for the passive governor. But, don't need to
  265. * initialize the 'this' and 'nb' field because the devfreq core will handle
  266. * them.
  267. */
  268. struct devfreq_passive_data {
  269. /* Should set the devfreq instance of parent device */
  270. struct devfreq *parent;
  271. /* Optional callback to decide the next frequency of passvice device */
  272. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  273. /* For passive governor's internal use. Don't need to set them */
  274. struct devfreq *this;
  275. struct notifier_block nb;
  276. };
  277. #endif
  278. #else /* !CONFIG_PM_DEVFREQ */
  279. static inline struct devfreq *devfreq_add_device(struct device *dev,
  280. struct devfreq_dev_profile *profile,
  281. const char *governor_name,
  282. void *data)
  283. {
  284. return ERR_PTR(-ENOSYS);
  285. }
  286. static inline int devfreq_remove_device(struct devfreq *devfreq)
  287. {
  288. return 0;
  289. }
  290. static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
  291. struct devfreq_dev_profile *profile,
  292. const char *governor_name,
  293. void *data)
  294. {
  295. return ERR_PTR(-ENOSYS);
  296. }
  297. static inline void devm_devfreq_remove_device(struct device *dev,
  298. struct devfreq *devfreq)
  299. {
  300. }
  301. static inline int devfreq_suspend_device(struct devfreq *devfreq)
  302. {
  303. return 0;
  304. }
  305. static inline int devfreq_resume_device(struct devfreq *devfreq)
  306. {
  307. return 0;
  308. }
  309. static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  310. unsigned long *freq, u32 flags)
  311. {
  312. return ERR_PTR(-EINVAL);
  313. }
  314. static inline int devfreq_register_opp_notifier(struct device *dev,
  315. struct devfreq *devfreq)
  316. {
  317. return -EINVAL;
  318. }
  319. static inline int devfreq_unregister_opp_notifier(struct device *dev,
  320. struct devfreq *devfreq)
  321. {
  322. return -EINVAL;
  323. }
  324. static inline int devm_devfreq_register_opp_notifier(struct device *dev,
  325. struct devfreq *devfreq)
  326. {
  327. return -EINVAL;
  328. }
  329. static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
  330. struct devfreq *devfreq)
  331. {
  332. }
  333. static inline int devfreq_register_notifier(struct devfreq *devfreq,
  334. struct notifier_block *nb,
  335. unsigned int list)
  336. {
  337. return 0;
  338. }
  339. static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
  340. struct notifier_block *nb,
  341. unsigned int list)
  342. {
  343. return 0;
  344. }
  345. static inline int devm_devfreq_register_notifier(struct device *dev,
  346. struct devfreq *devfreq,
  347. struct notifier_block *nb,
  348. unsigned int list)
  349. {
  350. return 0;
  351. }
  352. static inline void devm_devfreq_unregister_notifier(struct device *dev,
  353. struct devfreq *devfreq,
  354. struct notifier_block *nb,
  355. unsigned int list)
  356. {
  357. }
  358. static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  359. int index)
  360. {
  361. return ERR_PTR(-ENODEV);
  362. }
  363. static inline int devfreq_update_stats(struct devfreq *df)
  364. {
  365. return -EINVAL;
  366. }
  367. #endif /* CONFIG_PM_DEVFREQ */
  368. #endif /* __LINUX_DEVFREQ_H__ */