devfreq.h 13 KB

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