opp.h 10.0 KB

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