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