opp.h 10 KB

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