devfreq.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. * @immutable: Immutable flag for governor. If the value is 1,
  96. * this govenror is never changeable to other governor.
  97. * @get_target_freq: Returns desired operating frequency for the device.
  98. * Basically, get_target_freq will run
  99. * devfreq_dev_profile.get_dev_status() to get the
  100. * status of the device (load = busy_time / total_time).
  101. * If no_central_polling is set, this callback is called
  102. * only with update_devfreq() notified by OPP.
  103. * @event_handler: Callback for devfreq core framework to notify events
  104. * to governors. Events include per device governor
  105. * init and exit, opp changes out of devfreq, suspend
  106. * and resume of per device devfreq during device idle.
  107. *
  108. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  109. */
  110. struct devfreq_governor {
  111. struct list_head node;
  112. const char name[DEVFREQ_NAME_LEN];
  113. const unsigned int immutable;
  114. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  115. int (*event_handler)(struct devfreq *devfreq,
  116. unsigned int event, void *data);
  117. };
  118. /**
  119. * struct devfreq - Device devfreq structure
  120. * @node: list node - contains the devices with devfreq that have been
  121. * registered.
  122. * @lock: a mutex to protect accessing devfreq.
  123. * @dev: device registered by devfreq class. dev.parent is the device
  124. * using devfreq.
  125. * @profile: device-specific devfreq profile
  126. * @governor: method how to choose frequency based on the usage.
  127. * @governor_name: devfreq governor name for use with this devfreq
  128. * @nb: notifier block used to notify devfreq object that it should
  129. * reevaluate operable frequencies. Devfreq users may use
  130. * devfreq.nb to the corresponding register notifier call chain.
  131. * @work: delayed work for load monitoring.
  132. * @previous_freq: previously configured frequency value.
  133. * @data: Private data of the governor. The devfreq framework does not
  134. * touch this.
  135. * @min_freq: Limit minimum frequency requested by user (0: none)
  136. * @max_freq: Limit maximum frequency requested by user (0: none)
  137. * @stop_polling: devfreq polling status of a device.
  138. * @total_trans: Number of devfreq transitions
  139. * @trans_table: Statistics of devfreq transitions
  140. * @time_in_state: Statistics of devfreq states
  141. * @last_stat_updated: The last time stat updated
  142. * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
  143. *
  144. * This structure stores the devfreq information for a give device.
  145. *
  146. * Note that when a governor accesses entries in struct devfreq in its
  147. * functions except for the context of callbacks defined in struct
  148. * devfreq_governor, the governor should protect its access with the
  149. * struct mutex lock in struct devfreq. A governor may use this mutex
  150. * to protect its own private data in void *data as well.
  151. */
  152. struct devfreq {
  153. struct list_head node;
  154. struct mutex lock;
  155. struct device dev;
  156. struct devfreq_dev_profile *profile;
  157. const struct devfreq_governor *governor;
  158. char governor_name[DEVFREQ_NAME_LEN];
  159. struct notifier_block nb;
  160. struct delayed_work work;
  161. unsigned long previous_freq;
  162. struct devfreq_dev_status last_status;
  163. void *data; /* private data for governors */
  164. unsigned long min_freq;
  165. unsigned long max_freq;
  166. bool stop_polling;
  167. /* information for device frequency transition */
  168. unsigned int total_trans;
  169. unsigned int *trans_table;
  170. unsigned long *time_in_state;
  171. unsigned long last_stat_updated;
  172. struct srcu_notifier_head transition_notifier_list;
  173. };
  174. struct devfreq_freqs {
  175. unsigned long old;
  176. unsigned long new;
  177. };
  178. #if defined(CONFIG_PM_DEVFREQ)
  179. extern struct devfreq *devfreq_add_device(struct device *dev,
  180. struct devfreq_dev_profile *profile,
  181. const char *governor_name,
  182. void *data);
  183. extern int devfreq_remove_device(struct devfreq *devfreq);
  184. extern struct devfreq *devm_devfreq_add_device(struct device *dev,
  185. struct devfreq_dev_profile *profile,
  186. const char *governor_name,
  187. void *data);
  188. extern void devm_devfreq_remove_device(struct device *dev,
  189. struct devfreq *devfreq);
  190. /* Supposed to be called by PM callbacks */
  191. extern int devfreq_suspend_device(struct devfreq *devfreq);
  192. extern int devfreq_resume_device(struct devfreq *devfreq);
  193. /* Helper functions for devfreq user device driver with OPP. */
  194. extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  195. unsigned long *freq, u32 flags);
  196. extern int devfreq_register_opp_notifier(struct device *dev,
  197. struct devfreq *devfreq);
  198. extern int devfreq_unregister_opp_notifier(struct device *dev,
  199. struct devfreq *devfreq);
  200. extern int devm_devfreq_register_opp_notifier(struct device *dev,
  201. struct devfreq *devfreq);
  202. extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
  203. struct devfreq *devfreq);
  204. extern int devfreq_register_notifier(struct devfreq *devfreq,
  205. struct notifier_block *nb,
  206. unsigned int list);
  207. extern int devfreq_unregister_notifier(struct devfreq *devfreq,
  208. struct notifier_block *nb,
  209. unsigned int list);
  210. extern int devm_devfreq_register_notifier(struct device *dev,
  211. struct devfreq *devfreq,
  212. struct notifier_block *nb,
  213. unsigned int list);
  214. extern void devm_devfreq_unregister_notifier(struct device *dev,
  215. struct devfreq *devfreq,
  216. struct notifier_block *nb,
  217. unsigned int list);
  218. extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  219. int index);
  220. /**
  221. * devfreq_update_stats() - update the last_status pointer in struct devfreq
  222. * @df: the devfreq instance whose status needs updating
  223. *
  224. * Governors are recommended to use this function along with last_status,
  225. * which allows other entities to reuse the last_status without affecting
  226. * the values fetched later by governors.
  227. */
  228. static inline int devfreq_update_stats(struct devfreq *df)
  229. {
  230. return df->profile->get_dev_status(df->dev.parent, &df->last_status);
  231. }
  232. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
  233. /**
  234. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  235. * and devfreq_add_device
  236. * @upthreshold: If the load is over this value, the frequency jumps.
  237. * Specify 0 to use the default. Valid value = 0 to 100.
  238. * @downdifferential: If the load is under upthreshold - downdifferential,
  239. * the governor may consider slowing the frequency down.
  240. * Specify 0 to use the default. Valid value = 0 to 100.
  241. * downdifferential < upthreshold must hold.
  242. *
  243. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  244. * the governor uses the default values.
  245. */
  246. struct devfreq_simple_ondemand_data {
  247. unsigned int upthreshold;
  248. unsigned int downdifferential;
  249. };
  250. #endif
  251. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
  252. /**
  253. * struct devfreq_passive_data - void *data fed to struct devfreq
  254. * and devfreq_add_device
  255. * @parent: the devfreq instance of parent device.
  256. * @get_target_freq: Optional callback, Returns desired operating frequency
  257. * for the device using passive governor. That is called
  258. * when passive governor should decide the next frequency
  259. * by using the new frequency of parent devfreq device
  260. * using governors except for passive governor.
  261. * If the devfreq device has the specific method to decide
  262. * the next frequency, should use this callback.
  263. * @this: the devfreq instance of own device.
  264. * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
  265. *
  266. * The devfreq_passive_data have to set the devfreq instance of parent
  267. * device with governors except for the passive governor. But, don't need to
  268. * initialize the 'this' and 'nb' field because the devfreq core will handle
  269. * them.
  270. */
  271. struct devfreq_passive_data {
  272. /* Should set the devfreq instance of parent device */
  273. struct devfreq *parent;
  274. /* Optional callback to decide the next frequency of passvice device */
  275. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  276. /* For passive governor's internal use. Don't need to set them */
  277. struct devfreq *this;
  278. struct notifier_block nb;
  279. };
  280. #endif
  281. #else /* !CONFIG_PM_DEVFREQ */
  282. static inline struct devfreq *devfreq_add_device(struct device *dev,
  283. struct devfreq_dev_profile *profile,
  284. const char *governor_name,
  285. void *data)
  286. {
  287. return ERR_PTR(-ENOSYS);
  288. }
  289. static inline int devfreq_remove_device(struct devfreq *devfreq)
  290. {
  291. return 0;
  292. }
  293. static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
  294. struct devfreq_dev_profile *profile,
  295. const char *governor_name,
  296. void *data)
  297. {
  298. return ERR_PTR(-ENOSYS);
  299. }
  300. static inline void devm_devfreq_remove_device(struct device *dev,
  301. struct devfreq *devfreq)
  302. {
  303. }
  304. static inline int devfreq_suspend_device(struct devfreq *devfreq)
  305. {
  306. return 0;
  307. }
  308. static inline int devfreq_resume_device(struct devfreq *devfreq)
  309. {
  310. return 0;
  311. }
  312. static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  313. unsigned long *freq, u32 flags)
  314. {
  315. return ERR_PTR(-EINVAL);
  316. }
  317. static inline int devfreq_register_opp_notifier(struct device *dev,
  318. struct devfreq *devfreq)
  319. {
  320. return -EINVAL;
  321. }
  322. static inline int devfreq_unregister_opp_notifier(struct device *dev,
  323. struct devfreq *devfreq)
  324. {
  325. return -EINVAL;
  326. }
  327. static inline int devm_devfreq_register_opp_notifier(struct device *dev,
  328. struct devfreq *devfreq)
  329. {
  330. return -EINVAL;
  331. }
  332. static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
  333. struct devfreq *devfreq)
  334. {
  335. }
  336. static inline int devfreq_register_notifier(struct devfreq *devfreq,
  337. struct notifier_block *nb,
  338. unsigned int list)
  339. {
  340. return 0;
  341. }
  342. static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
  343. struct notifier_block *nb,
  344. unsigned int list)
  345. {
  346. return 0;
  347. }
  348. static inline int devm_devfreq_register_notifier(struct device *dev,
  349. struct devfreq *devfreq,
  350. struct notifier_block *nb,
  351. unsigned int list)
  352. {
  353. return 0;
  354. }
  355. static inline void devm_devfreq_unregister_notifier(struct device *dev,
  356. struct devfreq *devfreq,
  357. struct notifier_block *nb,
  358. unsigned int list)
  359. {
  360. }
  361. static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  362. int index)
  363. {
  364. return ERR_PTR(-ENODEV);
  365. }
  366. static inline int devfreq_update_stats(struct devfreq *df)
  367. {
  368. return -EINVAL;
  369. }
  370. #endif /* CONFIG_PM_DEVFREQ */
  371. #endif /* __LINUX_DEVFREQ_H__ */