opp.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Generic OPP Interface
  4. *
  5. * Copyright (C) 2009-2010 Texas Instruments Incorporated.
  6. * Nishanth Menon
  7. * Romit Dasgupta
  8. * Kevin Hilman
  9. */
  10. #ifndef __DRIVER_OPP_H__
  11. #define __DRIVER_OPP_H__
  12. #include <linux/device.h>
  13. #include <linux/interconnect.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kref.h>
  16. #include <linux/list.h>
  17. #include <linux/limits.h>
  18. #include <linux/pm_opp.h>
  19. #include <linux/notifier.h>
  20. struct clk;
  21. struct regulator;
  22. /* Lock to allow exclusive modification to the device and opp lists */
  23. extern struct mutex opp_table_lock;
  24. extern struct list_head opp_tables;
  25. /* OPP Config flags */
  26. #define OPP_CONFIG_CLK BIT(0)
  27. #define OPP_CONFIG_REGULATOR BIT(1)
  28. #define OPP_CONFIG_REGULATOR_HELPER BIT(2)
  29. #define OPP_CONFIG_PROP_NAME BIT(3)
  30. #define OPP_CONFIG_SUPPORTED_HW BIT(4)
  31. #define OPP_CONFIG_REQUIRED_DEV BIT(5)
  32. /**
  33. * struct opp_config_data - data for set config operations
  34. * @opp_table: OPP table
  35. * @flags: OPP config flags
  36. * @required_dev_index: The position in the array of required_devs
  37. *
  38. * This structure stores the OPP config information for each OPP table
  39. * configuration by the callers.
  40. */
  41. struct opp_config_data {
  42. struct opp_table *opp_table;
  43. unsigned int flags;
  44. unsigned int required_dev_index;
  45. };
  46. /**
  47. * struct dev_pm_opp_icc_bw - Interconnect bandwidth values
  48. * @avg: Average bandwidth corresponding to this OPP (in icc units)
  49. * @peak: Peak bandwidth corresponding to this OPP (in icc units)
  50. *
  51. * This structure stores the bandwidth values for a single interconnect path.
  52. */
  53. struct dev_pm_opp_icc_bw {
  54. u32 avg;
  55. u32 peak;
  56. };
  57. /*
  58. * Internal data structure organization with the OPP layer library is as
  59. * follows:
  60. * opp_tables (root)
  61. * |- device 1 (represents voltage domain 1)
  62. * | |- opp 1 (availability, freq, voltage)
  63. * | |- opp 2 ..
  64. * ... ...
  65. * | `- opp n ..
  66. * |- device 2 (represents the next voltage domain)
  67. * ...
  68. * `- device m (represents mth voltage domain)
  69. * device 1, 2.. are represented by opp_table structure while each opp
  70. * is represented by the opp structure.
  71. */
  72. /**
  73. * struct dev_pm_opp - Generic OPP description structure
  74. * @node: opp table node. The nodes are maintained throughout the lifetime
  75. * of boot. It is expected only an optimal set of OPPs are
  76. * added to the library by the SoC framework.
  77. * IMPORTANT: the opp nodes should be maintained in increasing
  78. * order.
  79. * @kref: for reference count of the OPP.
  80. * @available: true/false - marks if this OPP as available or not
  81. * @dynamic: not-created from static DT entries.
  82. * @turbo: true if turbo (boost) OPP
  83. * @suspend: true if suspend OPP
  84. * @removed: flag indicating that OPP's reference is dropped by OPP core.
  85. * @rates: Frequencies in hertz
  86. * @level: Performance level
  87. * @supplies: Power supplies voltage/current values
  88. * @bandwidth: Interconnect bandwidth values
  89. * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
  90. * frequency from any other OPP's frequency.
  91. * @required_opps: List of OPPs that are required by this OPP.
  92. * @opp_table: points back to the opp_table struct this opp belongs to
  93. * @np: OPP's device node.
  94. * @dentry: debugfs dentry pointer (per opp)
  95. *
  96. * This structure stores the OPP information for a given device.
  97. */
  98. struct dev_pm_opp {
  99. struct list_head node;
  100. struct kref kref;
  101. bool available;
  102. bool dynamic;
  103. bool turbo;
  104. bool suspend;
  105. bool removed;
  106. unsigned long *rates;
  107. unsigned int level;
  108. struct dev_pm_opp_supply *supplies;
  109. struct dev_pm_opp_icc_bw *bandwidth;
  110. unsigned long clock_latency_ns;
  111. struct dev_pm_opp **required_opps;
  112. struct opp_table *opp_table;
  113. struct device_node *np;
  114. #ifdef CONFIG_DEBUG_FS
  115. struct dentry *dentry;
  116. const char *of_name;
  117. #endif
  118. };
  119. /**
  120. * struct opp_device - devices managed by 'struct opp_table'
  121. * @node: list node
  122. * @dev: device to which the struct object belongs
  123. * @dentry: debugfs dentry pointer (per device)
  124. *
  125. * This is an internal data structure maintaining the devices that are managed
  126. * by 'struct opp_table'.
  127. */
  128. struct opp_device {
  129. struct list_head node;
  130. const struct device *dev;
  131. #ifdef CONFIG_DEBUG_FS
  132. struct dentry *dentry;
  133. #endif
  134. };
  135. enum opp_table_access {
  136. OPP_TABLE_ACCESS_UNKNOWN = 0,
  137. OPP_TABLE_ACCESS_EXCLUSIVE = 1,
  138. OPP_TABLE_ACCESS_SHARED = 2,
  139. };
  140. /**
  141. * struct opp_table - Device opp structure
  142. * @node: table node - contains the devices with OPPs that
  143. * have been registered. Nodes once added are not modified in this
  144. * table.
  145. * @head: notifier head to notify the OPP availability changes.
  146. * @dev_list: list of devices that share these OPPs
  147. * @opp_list: table of opps
  148. * @kref: for reference count of the table.
  149. * @lock: mutex protecting the opp_list and dev_list.
  150. * @np: struct device_node pointer for opp's DT node.
  151. * @clock_latency_ns_max: Max clock latency in nanoseconds.
  152. * @parsed_static_opps: Count of devices for which OPPs are initialized from DT.
  153. * @shared_opp: OPP is shared between multiple devices.
  154. * @current_rate_single_clk: Currently configured frequency for single clk.
  155. * @current_opp: Currently configured OPP for the table.
  156. * @suspend_opp: Pointer to OPP to be used during device suspend.
  157. * @required_opp_tables: List of device OPP tables that are required by OPPs in
  158. * this table.
  159. * @required_devs: List of devices for required OPP tables.
  160. * @required_opp_count: Number of required devices.
  161. * @supported_hw: Array of version number to support.
  162. * @supported_hw_count: Number of elements in supported_hw array.
  163. * @prop_name: A name to postfix to many DT properties, while parsing them.
  164. * @config_clks: Platform specific config_clks() callback.
  165. * @clks: Device's clock handles, for multiple clocks.
  166. * @clk: Device's clock handle, for single clock.
  167. * @clk_count: Number of clocks.
  168. * @config_regulators: Platform specific config_regulators() callback.
  169. * @regulators: Supply regulators
  170. * @regulator_count: Number of power supply regulators. Its value can be -1
  171. * (uninitialized), 0 (no opp-microvolt property) or > 0 (has opp-microvolt
  172. * property).
  173. * @paths: Interconnect path handles
  174. * @path_count: Number of interconnect paths
  175. * @enabled: Set to true if the device's resources are enabled/configured.
  176. * @is_genpd: Marks if the OPP table belongs to a genpd.
  177. * @dentry: debugfs dentry pointer of the real device directory (not links).
  178. * @dentry_name: Name of the real dentry.
  179. *
  180. * @voltage_tolerance_v1: In percentage, for v1 bindings only.
  181. *
  182. * This is an internal data structure maintaining the link to opps attached to
  183. * a device. This structure is not meant to be shared to users as it is
  184. * meant for book keeping and private to OPP library.
  185. */
  186. struct opp_table {
  187. struct list_head node, lazy;
  188. struct blocking_notifier_head head;
  189. struct list_head dev_list;
  190. struct list_head opp_list;
  191. struct kref kref;
  192. struct mutex lock;
  193. struct device_node *np;
  194. unsigned long clock_latency_ns_max;
  195. /* For backward compatibility with v1 bindings */
  196. unsigned int voltage_tolerance_v1;
  197. unsigned int parsed_static_opps;
  198. enum opp_table_access shared_opp;
  199. unsigned long current_rate_single_clk;
  200. struct dev_pm_opp *current_opp;
  201. struct dev_pm_opp *suspend_opp;
  202. struct opp_table **required_opp_tables;
  203. struct device **required_devs;
  204. unsigned int required_opp_count;
  205. unsigned int *supported_hw;
  206. unsigned int supported_hw_count;
  207. const char *prop_name;
  208. config_clks_t config_clks;
  209. struct clk **clks;
  210. struct clk *clk;
  211. int clk_count;
  212. config_regulators_t config_regulators;
  213. struct regulator **regulators;
  214. int regulator_count;
  215. struct icc_path **paths;
  216. unsigned int path_count;
  217. bool enabled;
  218. bool is_genpd;
  219. #ifdef CONFIG_DEBUG_FS
  220. struct dentry *dentry;
  221. char dentry_name[NAME_MAX];
  222. #endif
  223. };
  224. /* Routines internal to opp core */
  225. bool _opp_remove_all_static(struct opp_table *opp_table);
  226. int _get_opp_count(struct opp_table *opp_table);
  227. struct opp_table *_find_opp_table(struct device *dev);
  228. struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
  229. struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table);
  230. void _opp_free(struct dev_pm_opp *opp);
  231. int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1, struct dev_pm_opp *opp2);
  232. int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
  233. int _opp_add_v1(struct opp_table *opp_table, struct device *dev, struct dev_pm_opp_data *data, bool dynamic);
  234. void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, int last_cpu);
  235. struct opp_table *_add_opp_table_indexed(struct device *dev, int index, bool getclk);
  236. void _required_opps_available(struct dev_pm_opp *opp, int count);
  237. static inline bool lazy_linking_pending(struct opp_table *opp_table)
  238. {
  239. return unlikely(!list_empty(&opp_table->lazy));
  240. }
  241. #ifdef CONFIG_OF
  242. void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index);
  243. void _of_clear_opp_table(struct opp_table *opp_table);
  244. struct opp_table *_managed_opp(struct device *dev, int index);
  245. void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp);
  246. #else
  247. static inline void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index) {}
  248. static inline void _of_clear_opp_table(struct opp_table *opp_table) {}
  249. static inline struct opp_table *_managed_opp(struct device *dev, int index) { return NULL; }
  250. static inline void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp) {}
  251. #endif
  252. #ifdef CONFIG_DEBUG_FS
  253. void opp_debug_remove_one(struct dev_pm_opp *opp);
  254. void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table);
  255. void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table);
  256. void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table);
  257. #else
  258. static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {}
  259. static inline void opp_debug_create_one(struct dev_pm_opp *opp,
  260. struct opp_table *opp_table) { }
  261. static inline void opp_debug_register(struct opp_device *opp_dev,
  262. struct opp_table *opp_table) { }
  263. static inline void opp_debug_unregister(struct opp_device *opp_dev,
  264. struct opp_table *opp_table)
  265. { }
  266. #endif /* DEBUG_FS */
  267. #endif /* __DRIVER_OPP_H__ */