devfreq.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/opp.h>
  17. #define DEVFREQ_NAME_LEN 16
  18. struct devfreq;
  19. /**
  20. * struct devfreq_dev_status - Data given from devfreq user device to
  21. * governors. Represents the performance
  22. * statistics.
  23. * @total_time: The total time represented by this instance of
  24. * devfreq_dev_status
  25. * @busy_time: The time that the device was working among the
  26. * total_time.
  27. * @current_frequency: The operating frequency.
  28. * @private_data: An entry not specified by the devfreq framework.
  29. * A device and a specific governor may have their
  30. * own protocol with private_data. However, because
  31. * this is governor-specific, a governor using this
  32. * will be only compatible with devices aware of it.
  33. */
  34. struct devfreq_dev_status {
  35. /* both since the last measure */
  36. unsigned long total_time;
  37. unsigned long busy_time;
  38. unsigned long current_frequency;
  39. void *private_data;
  40. };
  41. /*
  42. * The resulting frequency should be at most this. (this bound is the
  43. * least upper bound; thus, the resulting freq should be lower or same)
  44. * If the flag is not set, the resulting frequency should be at most the
  45. * bound (greatest lower bound)
  46. */
  47. #define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
  48. /**
  49. * struct devfreq_dev_profile - Devfreq's user device profile
  50. * @initial_freq: The operating frequency when devfreq_add_device() is
  51. * called.
  52. * @polling_ms: The polling interval in ms. 0 disables polling.
  53. * @target: The device should set its operating frequency at
  54. * freq or lowest-upper-than-freq value. If freq is
  55. * higher than any operable frequency, set maximum.
  56. * Before returning, target function should set
  57. * freq at the current frequency.
  58. * The "flags" parameter's possible values are
  59. * explained above with "DEVFREQ_FLAG_*" macros.
  60. * @get_dev_status: The device should provide the current performance
  61. * status to devfreq, which is used by governors.
  62. * @get_cur_freq: The device should provide the current frequency
  63. * at which it is operating.
  64. * @exit: An optional callback that is called when devfreq
  65. * is removing the devfreq object due to error or
  66. * from devfreq_remove_device() call. If the user
  67. * has registered devfreq->nb at a notifier-head,
  68. * this is the time to unregister it.
  69. * @freq_table: Optional list of frequencies to support statistics.
  70. * @max_state: The size of freq_table.
  71. */
  72. struct devfreq_dev_profile {
  73. unsigned long initial_freq;
  74. unsigned int polling_ms;
  75. int (*target)(struct device *dev, unsigned long *freq, u32 flags);
  76. int (*get_dev_status)(struct device *dev,
  77. struct devfreq_dev_status *stat);
  78. int (*get_cur_freq)(struct device *dev, unsigned long *freq);
  79. void (*exit)(struct device *dev);
  80. unsigned int *freq_table;
  81. unsigned int max_state;
  82. };
  83. /**
  84. * struct devfreq_governor - Devfreq policy governor
  85. * @node: list node - contains registered devfreq governors
  86. * @name: Governor's name
  87. * @get_target_freq: Returns desired operating frequency for the device.
  88. * Basically, get_target_freq will run
  89. * devfreq_dev_profile.get_dev_status() to get the
  90. * status of the device (load = busy_time / total_time).
  91. * If no_central_polling is set, this callback is called
  92. * only with update_devfreq() notified by OPP.
  93. * @event_handler: Callback for devfreq core framework to notify events
  94. * to governors. Events include per device governor
  95. * init and exit, opp changes out of devfreq, suspend
  96. * and resume of per device devfreq during device idle.
  97. *
  98. * Note that the callbacks are called with devfreq->lock locked by devfreq.
  99. */
  100. struct devfreq_governor {
  101. struct list_head node;
  102. const char name[DEVFREQ_NAME_LEN];
  103. int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
  104. int (*event_handler)(struct devfreq *devfreq,
  105. unsigned int event, void *data);
  106. };
  107. /**
  108. * struct devfreq - Device devfreq structure
  109. * @node: list node - contains the devices with devfreq that have been
  110. * registered.
  111. * @lock: a mutex to protect accessing devfreq.
  112. * @dev: device registered by devfreq class. dev.parent is the device
  113. * using devfreq.
  114. * @profile: device-specific devfreq profile
  115. * @governor: method how to choose frequency based on the usage.
  116. * @governor_name: devfreq governor name for use with this devfreq
  117. * @nb: notifier block used to notify devfreq object that it should
  118. * reevaluate operable frequencies. Devfreq users may use
  119. * devfreq.nb to the corresponding register notifier call chain.
  120. * @work: delayed work for load monitoring.
  121. * @previous_freq: previously configured frequency value.
  122. * @data: Private data of the governor. The devfreq framework does not
  123. * touch this.
  124. * @min_freq: Limit minimum frequency requested by user (0: none)
  125. * @max_freq: Limit maximum frequency requested by user (0: none)
  126. * @stop_polling: devfreq polling status of a device.
  127. * @total_trans: Number of devfreq transitions
  128. * @trans_table: Statistics of devfreq transitions
  129. * @time_in_state: Statistics of devfreq states
  130. * @last_stat_updated: The last time stat updated
  131. *
  132. * This structure stores the devfreq information for a give device.
  133. *
  134. * Note that when a governor accesses entries in struct devfreq in its
  135. * functions except for the context of callbacks defined in struct
  136. * devfreq_governor, the governor should protect its access with the
  137. * struct mutex lock in struct devfreq. A governor may use this mutex
  138. * to protect its own private data in void *data as well.
  139. */
  140. struct devfreq {
  141. struct list_head node;
  142. struct mutex lock;
  143. struct device dev;
  144. struct devfreq_dev_profile *profile;
  145. const struct devfreq_governor *governor;
  146. char governor_name[DEVFREQ_NAME_LEN];
  147. struct notifier_block nb;
  148. struct delayed_work work;
  149. unsigned long previous_freq;
  150. void *data; /* private data for governors */
  151. unsigned long min_freq;
  152. unsigned long max_freq;
  153. bool stop_polling;
  154. /* information for device freqeuncy transition */
  155. unsigned int total_trans;
  156. unsigned int *trans_table;
  157. unsigned long *time_in_state;
  158. unsigned long last_stat_updated;
  159. };
  160. #if defined(CONFIG_PM_DEVFREQ)
  161. extern struct devfreq *devfreq_add_device(struct device *dev,
  162. struct devfreq_dev_profile *profile,
  163. const char *governor_name,
  164. void *data);
  165. extern int devfreq_remove_device(struct devfreq *devfreq);
  166. extern int devfreq_suspend_device(struct devfreq *devfreq);
  167. extern int devfreq_resume_device(struct devfreq *devfreq);
  168. /* Helper functions for devfreq user device driver with OPP. */
  169. extern struct opp *devfreq_recommended_opp(struct device *dev,
  170. unsigned long *freq, u32 flags);
  171. extern int devfreq_register_opp_notifier(struct device *dev,
  172. struct devfreq *devfreq);
  173. extern int devfreq_unregister_opp_notifier(struct device *dev,
  174. struct devfreq *devfreq);
  175. #ifdef CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND
  176. /**
  177. * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
  178. * and devfreq_add_device
  179. * @upthreshold: If the load is over this value, the frequency jumps.
  180. * Specify 0 to use the default. Valid value = 0 to 100.
  181. * @downdifferential: If the load is under upthreshold - downdifferential,
  182. * the governor may consider slowing the frequency down.
  183. * Specify 0 to use the default. Valid value = 0 to 100.
  184. * downdifferential < upthreshold must hold.
  185. *
  186. * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  187. * the governor uses the default values.
  188. */
  189. struct devfreq_simple_ondemand_data {
  190. unsigned int upthreshold;
  191. unsigned int downdifferential;
  192. };
  193. #endif
  194. #else /* !CONFIG_PM_DEVFREQ */
  195. static struct devfreq *devfreq_add_device(struct device *dev,
  196. struct devfreq_dev_profile *profile,
  197. const char *governor_name,
  198. void *data)
  199. {
  200. return NULL;
  201. }
  202. static int devfreq_remove_device(struct devfreq *devfreq)
  203. {
  204. return 0;
  205. }
  206. static int devfreq_suspend_device(struct devfreq *devfreq)
  207. {
  208. return 0;
  209. }
  210. static int devfreq_resume_device(struct devfreq *devfreq)
  211. {
  212. return 0;
  213. }
  214. static struct opp *devfreq_recommended_opp(struct device *dev,
  215. unsigned long *freq, u32 flags)
  216. {
  217. return -EINVAL;
  218. }
  219. static int devfreq_register_opp_notifier(struct device *dev,
  220. struct devfreq *devfreq)
  221. {
  222. return -EINVAL;
  223. }
  224. static int devfreq_unregister_opp_notifier(struct device *dev,
  225. struct devfreq *devfreq)
  226. {
  227. return -EINVAL;
  228. }
  229. #endif /* CONFIG_PM_DEVFREQ */
  230. #endif /* __LINUX_DEVFREQ_H__ */