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