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