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