devfreq.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /* Helper functions for devfreq user device driver with OPP. */
  179. extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  180. unsigned long *freq, u32 flags);
  181. extern int devfreq_register_opp_notifier(struct device *dev,
  182. struct devfreq *devfreq);
  183. extern int devfreq_unregister_opp_notifier(struct device *dev,
  184. struct devfreq *devfreq);
  185. extern int devm_devfreq_register_opp_notifier(struct device *dev,
  186. struct devfreq *devfreq);
  187. extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
  188. struct devfreq *devfreq);
  189. extern int devfreq_register_notifier(struct devfreq *devfreq,
  190. struct notifier_block *nb,
  191. unsigned int list);
  192. extern int devfreq_unregister_notifier(struct devfreq *devfreq,
  193. struct notifier_block *nb,
  194. unsigned int list);
  195. extern int devm_devfreq_register_notifier(struct device *dev,
  196. struct devfreq *devfreq,
  197. struct notifier_block *nb,
  198. unsigned int list);
  199. extern void devm_devfreq_unregister_notifier(struct device *dev,
  200. struct devfreq *devfreq,
  201. struct notifier_block *nb,
  202. unsigned int list);
  203. extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  204. int index);
  205. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
  206. /**
  207. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  208. * and devfreq_add_device
  209. * @upthreshold: If the load is over this value, the frequency jumps.
  210. * Specify 0 to use the default. Valid value = 0 to 100.
  211. * @downdifferential: If the load is under upthreshold - downdifferential,
  212. * the governor may consider slowing the frequency down.
  213. * Specify 0 to use the default. Valid value = 0 to 100.
  214. * downdifferential < upthreshold must hold.
  215. *
  216. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  217. * the governor uses the default values.
  218. */
  219. struct devfreq_simple_ondemand_data {
  220. unsigned int upthreshold;
  221. unsigned int downdifferential;
  222. };
  223. #endif
  224. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
  225. /**
  226. * struct devfreq_passive_data - void *data fed to struct devfreq
  227. * and devfreq_add_device
  228. * @parent: the devfreq instance of parent device.
  229. * @get_target_freq: Optional callback, Returns desired operating frequency
  230. * for the device using passive governor. That is called
  231. * when passive governor should decide the next frequency
  232. * by using the new frequency of parent devfreq device
  233. * using governors except for passive governor.
  234. * If the devfreq device has the specific method to decide
  235. * the next frequency, should use this callback.
  236. * @this: the devfreq instance of own device.
  237. * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
  238. *
  239. * The devfreq_passive_data have to set the devfreq instance of parent
  240. * device with governors except for the passive governor. But, don't need to
  241. * initialize the 'this' and 'nb' field because the devfreq core will handle
  242. * them.
  243. */
  244. struct devfreq_passive_data {
  245. /* Should set the devfreq instance of parent device */
  246. struct devfreq *parent;
  247. /* Optional callback to decide the next frequency of passvice device */
  248. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  249. /* For passive governor's internal use. Don't need to set them */
  250. struct devfreq *this;
  251. struct notifier_block nb;
  252. };
  253. #endif
  254. #else /* !CONFIG_PM_DEVFREQ */
  255. static inline struct devfreq *devfreq_add_device(struct device *dev,
  256. struct devfreq_dev_profile *profile,
  257. const char *governor_name,
  258. void *data)
  259. {
  260. return ERR_PTR(-ENOSYS);
  261. }
  262. static inline int devfreq_remove_device(struct devfreq *devfreq)
  263. {
  264. return 0;
  265. }
  266. static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
  267. struct devfreq_dev_profile *profile,
  268. const char *governor_name,
  269. void *data)
  270. {
  271. return ERR_PTR(-ENOSYS);
  272. }
  273. static inline void devm_devfreq_remove_device(struct device *dev,
  274. struct devfreq *devfreq)
  275. {
  276. }
  277. static inline int devfreq_suspend_device(struct devfreq *devfreq)
  278. {
  279. return 0;
  280. }
  281. static inline int devfreq_resume_device(struct devfreq *devfreq)
  282. {
  283. return 0;
  284. }
  285. static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  286. unsigned long *freq, u32 flags)
  287. {
  288. return ERR_PTR(-EINVAL);
  289. }
  290. static inline int devfreq_register_opp_notifier(struct device *dev,
  291. struct devfreq *devfreq)
  292. {
  293. return -EINVAL;
  294. }
  295. static inline int devfreq_unregister_opp_notifier(struct device *dev,
  296. struct devfreq *devfreq)
  297. {
  298. return -EINVAL;
  299. }
  300. static inline int devm_devfreq_register_opp_notifier(struct device *dev,
  301. struct devfreq *devfreq)
  302. {
  303. return -EINVAL;
  304. }
  305. static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
  306. struct devfreq *devfreq)
  307. {
  308. }
  309. static inline int devfreq_register_notifier(struct devfreq *devfreq,
  310. struct notifier_block *nb,
  311. unsigned int list)
  312. {
  313. return 0;
  314. }
  315. static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
  316. struct notifier_block *nb,
  317. unsigned int list)
  318. {
  319. return 0;
  320. }
  321. static inline int devm_devfreq_register_notifier(struct device *dev,
  322. struct devfreq *devfreq,
  323. struct notifier_block *nb,
  324. unsigned int list)
  325. {
  326. return 0;
  327. }
  328. static inline void devm_devfreq_unregister_notifier(struct device *dev,
  329. struct devfreq *devfreq,
  330. struct notifier_block *nb,
  331. unsigned int list)
  332. {
  333. }
  334. static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  335. int index)
  336. {
  337. return ERR_PTR(-ENODEV);
  338. }
  339. static inline int devfreq_update_stats(struct devfreq *df)
  340. {
  341. return -EINVAL;
  342. }
  343. #endif /* CONFIG_PM_DEVFREQ */
  344. #endif /* __LINUX_DEVFREQ_H__ */