1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar * Generic OPP Interface
47813dd6fSViresh Kumar *
57813dd6fSViresh Kumar * Copyright (C) 2009-2010 Texas Instruments Incorporated.
67813dd6fSViresh Kumar * Nishanth Menon
77813dd6fSViresh Kumar * Romit Dasgupta
87813dd6fSViresh Kumar * Kevin Hilman
97813dd6fSViresh Kumar */
107813dd6fSViresh Kumar
117813dd6fSViresh Kumar #ifndef __DRIVER_OPP_H__
127813dd6fSViresh Kumar #define __DRIVER_OPP_H__
137813dd6fSViresh Kumar
147813dd6fSViresh Kumar #include <linux/device.h>
156d3f922cSGeorgi Djakov #include <linux/interconnect.h>
167813dd6fSViresh Kumar #include <linux/kernel.h>
177813dd6fSViresh Kumar #include <linux/kref.h>
187813dd6fSViresh Kumar #include <linux/list.h>
197813dd6fSViresh Kumar #include <linux/limits.h>
207813dd6fSViresh Kumar #include <linux/pm_opp.h>
217813dd6fSViresh Kumar #include <linux/notifier.h>
227813dd6fSViresh Kumar
237813dd6fSViresh Kumar struct clk;
247813dd6fSViresh Kumar struct regulator;
257813dd6fSViresh Kumar
267813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
277813dd6fSViresh Kumar extern struct mutex opp_table_lock;
287813dd6fSViresh Kumar
29167eb2bdSViresh Kumar extern struct list_head opp_tables;
307813dd6fSViresh Kumar
3111b9b663SViresh Kumar /* OPP Config flags */
3211b9b663SViresh Kumar #define OPP_CONFIG_CLK BIT(0)
3311b9b663SViresh Kumar #define OPP_CONFIG_REGULATOR BIT(1)
3411b9b663SViresh Kumar #define OPP_CONFIG_REGULATOR_HELPER BIT(2)
3511b9b663SViresh Kumar #define OPP_CONFIG_PROP_NAME BIT(3)
3611b9b663SViresh Kumar #define OPP_CONFIG_SUPPORTED_HW BIT(4)
3711b9b663SViresh Kumar #define OPP_CONFIG_GENPD BIT(5)
38e37440e7SViresh Kumar #define OPP_CONFIG_REQUIRED_DEVS BIT(6)
3911b9b663SViresh Kumar
4011b9b663SViresh Kumar /**
4111b9b663SViresh Kumar * struct opp_config_data - data for set config operations
4211b9b663SViresh Kumar * @opp_table: OPP table
4311b9b663SViresh Kumar * @flags: OPP config flags
4411b9b663SViresh Kumar *
4511b9b663SViresh Kumar * This structure stores the OPP config information for each OPP table
4611b9b663SViresh Kumar * configuration by the callers.
4711b9b663SViresh Kumar */
4811b9b663SViresh Kumar struct opp_config_data {
4911b9b663SViresh Kumar struct opp_table *opp_table;
5011b9b663SViresh Kumar unsigned int flags;
5111b9b663SViresh Kumar };
5211b9b663SViresh Kumar
533b82024cSViresh Kumar /**
543b82024cSViresh Kumar * struct dev_pm_opp_icc_bw - Interconnect bandwidth values
553b82024cSViresh Kumar * @avg: Average bandwidth corresponding to this OPP (in icc units)
563b82024cSViresh Kumar * @peak: Peak bandwidth corresponding to this OPP (in icc units)
573b82024cSViresh Kumar *
583b82024cSViresh Kumar * This structure stores the bandwidth values for a single interconnect path.
593b82024cSViresh Kumar */
603b82024cSViresh Kumar struct dev_pm_opp_icc_bw {
613b82024cSViresh Kumar u32 avg;
623b82024cSViresh Kumar u32 peak;
633b82024cSViresh Kumar };
643b82024cSViresh Kumar
657813dd6fSViresh Kumar /*
667813dd6fSViresh Kumar * Internal data structure organization with the OPP layer library is as
677813dd6fSViresh Kumar * follows:
687813dd6fSViresh Kumar * opp_tables (root)
697813dd6fSViresh Kumar * |- device 1 (represents voltage domain 1)
707813dd6fSViresh Kumar * | |- opp 1 (availability, freq, voltage)
717813dd6fSViresh Kumar * | |- opp 2 ..
727813dd6fSViresh Kumar * ... ...
737813dd6fSViresh Kumar * | `- opp n ..
747813dd6fSViresh Kumar * |- device 2 (represents the next voltage domain)
757813dd6fSViresh Kumar * ...
767813dd6fSViresh Kumar * `- device m (represents mth voltage domain)
777813dd6fSViresh Kumar * device 1, 2.. are represented by opp_table structure while each opp
787813dd6fSViresh Kumar * is represented by the opp structure.
797813dd6fSViresh Kumar */
807813dd6fSViresh Kumar
817813dd6fSViresh Kumar /**
827813dd6fSViresh Kumar * struct dev_pm_opp - Generic OPP description structure
837813dd6fSViresh Kumar * @node: opp table node. The nodes are maintained throughout the lifetime
847813dd6fSViresh Kumar * of boot. It is expected only an optimal set of OPPs are
857813dd6fSViresh Kumar * added to the library by the SoC framework.
867813dd6fSViresh Kumar * IMPORTANT: the opp nodes should be maintained in increasing
877813dd6fSViresh Kumar * order.
887813dd6fSViresh Kumar * @kref: for reference count of the OPP.
897813dd6fSViresh Kumar * @available: true/false - marks if this OPP as available or not
907813dd6fSViresh Kumar * @dynamic: not-created from static DT entries.
917813dd6fSViresh Kumar * @turbo: true if turbo (boost) OPP
927813dd6fSViresh Kumar * @suspend: true if suspend OPP
93606a5d42SBeata Michalska * @removed: flag indicating that OPP's reference is dropped by OPP core.
942083da24SViresh Kumar * @rates: Frequencies in hertz
955b93ac54SRajendra Nayak * @level: Performance level
967813dd6fSViresh Kumar * @supplies: Power supplies voltage/current values
976d3f922cSGeorgi Djakov * @bandwidth: Interconnect bandwidth values
987813dd6fSViresh Kumar * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
997813dd6fSViresh Kumar * frequency from any other OPP's frequency.
100da544b61SViresh Kumar * @required_opps: List of OPPs that are required by this OPP.
1017813dd6fSViresh Kumar * @opp_table: points back to the opp_table struct this opp belongs to
1027813dd6fSViresh Kumar * @np: OPP's device node.
1037813dd6fSViresh Kumar * @dentry: debugfs dentry pointer (per opp)
1047813dd6fSViresh Kumar *
1057813dd6fSViresh Kumar * This structure stores the OPP information for a given device.
1067813dd6fSViresh Kumar */
1077813dd6fSViresh Kumar struct dev_pm_opp {
1087813dd6fSViresh Kumar struct list_head node;
1097813dd6fSViresh Kumar struct kref kref;
1107813dd6fSViresh Kumar
1117813dd6fSViresh Kumar bool available;
1127813dd6fSViresh Kumar bool dynamic;
1137813dd6fSViresh Kumar bool turbo;
1147813dd6fSViresh Kumar bool suspend;
115606a5d42SBeata Michalska bool removed;
1162083da24SViresh Kumar unsigned long *rates;
1175b93ac54SRajendra Nayak unsigned int level;
1187813dd6fSViresh Kumar
1197813dd6fSViresh Kumar struct dev_pm_opp_supply *supplies;
1206d3f922cSGeorgi Djakov struct dev_pm_opp_icc_bw *bandwidth;
1217813dd6fSViresh Kumar
1227813dd6fSViresh Kumar unsigned long clock_latency_ns;
1237813dd6fSViresh Kumar
124da544b61SViresh Kumar struct dev_pm_opp **required_opps;
1257813dd6fSViresh Kumar struct opp_table *opp_table;
1267813dd6fSViresh Kumar
1277813dd6fSViresh Kumar struct device_node *np;
1287813dd6fSViresh Kumar
1297813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
1307813dd6fSViresh Kumar struct dentry *dentry;
131021dbecaSViresh Kumar const char *of_name;
1327813dd6fSViresh Kumar #endif
1337813dd6fSViresh Kumar };
1347813dd6fSViresh Kumar
1357813dd6fSViresh Kumar /**
1367813dd6fSViresh Kumar * struct opp_device - devices managed by 'struct opp_table'
1377813dd6fSViresh Kumar * @node: list node
1387813dd6fSViresh Kumar * @dev: device to which the struct object belongs
1397813dd6fSViresh Kumar * @dentry: debugfs dentry pointer (per device)
1407813dd6fSViresh Kumar *
1417813dd6fSViresh Kumar * This is an internal data structure maintaining the devices that are managed
1427813dd6fSViresh Kumar * by 'struct opp_table'.
1437813dd6fSViresh Kumar */
1447813dd6fSViresh Kumar struct opp_device {
1457813dd6fSViresh Kumar struct list_head node;
1467813dd6fSViresh Kumar const struct device *dev;
1477813dd6fSViresh Kumar
1487813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
1497813dd6fSViresh Kumar struct dentry *dentry;
1507813dd6fSViresh Kumar #endif
1517813dd6fSViresh Kumar };
1527813dd6fSViresh Kumar
1537813dd6fSViresh Kumar enum opp_table_access {
1547813dd6fSViresh Kumar OPP_TABLE_ACCESS_UNKNOWN = 0,
1557813dd6fSViresh Kumar OPP_TABLE_ACCESS_EXCLUSIVE = 1,
1567813dd6fSViresh Kumar OPP_TABLE_ACCESS_SHARED = 2,
1577813dd6fSViresh Kumar };
1587813dd6fSViresh Kumar
1597813dd6fSViresh Kumar /**
1607813dd6fSViresh Kumar * struct opp_table - Device opp structure
1617813dd6fSViresh Kumar * @node: table node - contains the devices with OPPs that
1627813dd6fSViresh Kumar * have been registered. Nodes once added are not modified in this
1637813dd6fSViresh Kumar * table.
1647813dd6fSViresh Kumar * @head: notifier head to notify the OPP availability changes.
1657813dd6fSViresh Kumar * @dev_list: list of devices that share these OPPs
1667813dd6fSViresh Kumar * @opp_list: table of opps
1677813dd6fSViresh Kumar * @kref: for reference count of the table.
1683d255699SViresh Kumar * @lock: mutex protecting the opp_list and dev_list.
1697813dd6fSViresh Kumar * @np: struct device_node pointer for opp's DT node.
1707813dd6fSViresh Kumar * @clock_latency_ns_max: Max clock latency in nanoseconds.
17103758d60SViresh Kumar * @parsed_static_opps: Count of devices for which OPPs are initialized from DT.
1727813dd6fSViresh Kumar * @shared_opp: OPP is shared between multiple devices.
173*dcfec12bSViresh Kumar * @current_rate_single_clk: Currently configured frequency for single clk.
17481c4d8a3SViresh Kumar * @current_opp: Currently configured OPP for the table.
1757813dd6fSViresh Kumar * @suspend_opp: Pointer to OPP to be used during device suspend.
1765d6d106fSViresh Kumar * @required_opp_tables: List of device OPP tables that are required by OPPs in
1775d6d106fSViresh Kumar * this table.
178e37440e7SViresh Kumar * @required_devs: List of devices for required OPP tables.
1795d6d106fSViresh Kumar * @required_opp_count: Number of required devices.
1807813dd6fSViresh Kumar * @supported_hw: Array of version number to support.
1817813dd6fSViresh Kumar * @supported_hw_count: Number of elements in supported_hw array.
1827813dd6fSViresh Kumar * @prop_name: A name to postfix to many DT properties, while parsing them.
1832083da24SViresh Kumar * @config_clks: Platform specific config_clks() callback.
1842083da24SViresh Kumar * @clks: Device's clock handles, for multiple clocks.
1852083da24SViresh Kumar * @clk: Device's clock handle, for single clock.
1862083da24SViresh Kumar * @clk_count: Number of clocks.
187aee3352fSViresh Kumar * @config_regulators: Platform specific config_regulators() callback.
1887813dd6fSViresh Kumar * @regulators: Supply regulators
18946f48acaSViresh Kumar * @regulator_count: Number of power supply regulators. Its value can be -1
19046f48acaSViresh Kumar * (uninitialized), 0 (no opp-microvolt property) or > 0 (has opp-microvolt
19146f48acaSViresh Kumar * property).
1926d3f922cSGeorgi Djakov * @paths: Interconnect path handles
1936d3f922cSGeorgi Djakov * @path_count: Number of interconnect paths
19472f80ce4SViresh Kumar * @enabled: Set to true if the device's resources are enabled/configured.
19561d8e7c7SViresh Kumar * @is_genpd: Marks if the OPP table belongs to a genpd.
1967813dd6fSViresh Kumar * @dentry: debugfs dentry pointer of the real device directory (not links).
1977813dd6fSViresh Kumar * @dentry_name: Name of the real dentry.
1987813dd6fSViresh Kumar *
1997813dd6fSViresh Kumar * @voltage_tolerance_v1: In percentage, for v1 bindings only.
2007813dd6fSViresh Kumar *
2017813dd6fSViresh Kumar * This is an internal data structure maintaining the link to opps attached to
2027813dd6fSViresh Kumar * a device. This structure is not meant to be shared to users as it is
2037813dd6fSViresh Kumar * meant for book keeping and private to OPP library.
2047813dd6fSViresh Kumar */
2057813dd6fSViresh Kumar struct opp_table {
2067eba0c76SViresh Kumar struct list_head node, lazy;
2077813dd6fSViresh Kumar
2087813dd6fSViresh Kumar struct blocking_notifier_head head;
2097813dd6fSViresh Kumar struct list_head dev_list;
2107813dd6fSViresh Kumar struct list_head opp_list;
2117813dd6fSViresh Kumar struct kref kref;
2127813dd6fSViresh Kumar struct mutex lock;
2137813dd6fSViresh Kumar
2147813dd6fSViresh Kumar struct device_node *np;
2157813dd6fSViresh Kumar unsigned long clock_latency_ns_max;
2167813dd6fSViresh Kumar
2177813dd6fSViresh Kumar /* For backward compatibility with v1 bindings */
2187813dd6fSViresh Kumar unsigned int voltage_tolerance_v1;
2197813dd6fSViresh Kumar
22003758d60SViresh Kumar unsigned int parsed_static_opps;
2217813dd6fSViresh Kumar enum opp_table_access shared_opp;
222*dcfec12bSViresh Kumar unsigned long current_rate_single_clk;
22381c4d8a3SViresh Kumar struct dev_pm_opp *current_opp;
2247813dd6fSViresh Kumar struct dev_pm_opp *suspend_opp;
2257813dd6fSViresh Kumar
2265d6d106fSViresh Kumar struct opp_table **required_opp_tables;
227e37440e7SViresh Kumar struct device **required_devs;
2285d6d106fSViresh Kumar unsigned int required_opp_count;
2295d6d106fSViresh Kumar
2307813dd6fSViresh Kumar unsigned int *supported_hw;
2317813dd6fSViresh Kumar unsigned int supported_hw_count;
2327813dd6fSViresh Kumar const char *prop_name;
2332083da24SViresh Kumar config_clks_t config_clks;
2342083da24SViresh Kumar struct clk **clks;
2357813dd6fSViresh Kumar struct clk *clk;
2362083da24SViresh Kumar int clk_count;
237aee3352fSViresh Kumar config_regulators_t config_regulators;
2387813dd6fSViresh Kumar struct regulator **regulators;
23946f48acaSViresh Kumar int regulator_count;
2406d3f922cSGeorgi Djakov struct icc_path **paths;
2416d3f922cSGeorgi Djakov unsigned int path_count;
24272f80ce4SViresh Kumar bool enabled;
24361d8e7c7SViresh Kumar bool is_genpd;
2447813dd6fSViresh Kumar
2457813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
2467813dd6fSViresh Kumar struct dentry *dentry;
2477813dd6fSViresh Kumar char dentry_name[NAME_MAX];
2487813dd6fSViresh Kumar #endif
2497813dd6fSViresh Kumar };
2507813dd6fSViresh Kumar
2517813dd6fSViresh Kumar /* Routines internal to opp core */
252a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp);
253922ff075SViresh Kumar bool _opp_remove_all_static(struct opp_table *opp_table);
2547813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table);
255a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table);
2567813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev);
2577813dd6fSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
2587813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table);
2597813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp);
2602083da24SViresh Kumar int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1, struct dev_pm_opp *opp2);
2614768914bSViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table);
262248a38d5SUlf Hansson int _opp_add_v1(struct opp_table *opp_table, struct device *dev, struct dev_pm_opp_data *data, bool dynamic);
2632a4eb735SViresh Kumar void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, int last_cpu);
26432439ac7SViresh Kumar struct opp_table *_add_opp_table_indexed(struct device *dev, int index, bool getclk);
265d0e8ae6cSViresh Kumar void _put_opp_list_kref(struct opp_table *opp_table);
2667eba0c76SViresh Kumar void _required_opps_available(struct dev_pm_opp *opp, int count);
267528f2d8dSViresh Kumar void _update_set_required_opps(struct opp_table *opp_table);
2687eba0c76SViresh Kumar
lazy_linking_pending(struct opp_table * opp_table)2697eba0c76SViresh Kumar static inline bool lazy_linking_pending(struct opp_table *opp_table)
2707eba0c76SViresh Kumar {
2717eba0c76SViresh Kumar return unlikely(!list_empty(&opp_table->lazy));
2727eba0c76SViresh Kumar }
2737813dd6fSViresh Kumar
2747813dd6fSViresh Kumar #ifdef CONFIG_OF
275eb7c8743SViresh Kumar void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index);
2765d6d106fSViresh Kumar void _of_clear_opp_table(struct opp_table *opp_table);
277283d55e6SViresh Kumar struct opp_table *_managed_opp(struct device *dev, int index);
2783466ea2cSLiang He void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp);
2797813dd6fSViresh Kumar #else
_of_init_opp_table(struct opp_table * opp_table,struct device * dev,int index)280eb7c8743SViresh Kumar 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)2815d6d106fSViresh Kumar static inline void _of_clear_opp_table(struct opp_table *opp_table) {}
_managed_opp(struct device * dev,int index)282283d55e6SViresh Kumar static inline struct opp_table *_managed_opp(struct device *dev, int index) { return NULL; }
_of_clear_opp(struct opp_table * opp_table,struct dev_pm_opp * opp)2833466ea2cSLiang He static inline void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp) {}
2847813dd6fSViresh Kumar #endif
2857813dd6fSViresh Kumar
2867813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
2877813dd6fSViresh Kumar void opp_debug_remove_one(struct dev_pm_opp *opp);
288a2dea4cbSGreg Kroah-Hartman void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table);
289a2dea4cbSGreg Kroah-Hartman void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table);
2907813dd6fSViresh Kumar void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table);
2917813dd6fSViresh Kumar #else
opp_debug_remove_one(struct dev_pm_opp * opp)2927813dd6fSViresh Kumar static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {}
2937813dd6fSViresh Kumar
opp_debug_create_one(struct dev_pm_opp * opp,struct opp_table * opp_table)294a2dea4cbSGreg Kroah-Hartman static inline void opp_debug_create_one(struct dev_pm_opp *opp,
295a2dea4cbSGreg Kroah-Hartman struct opp_table *opp_table) { }
296a2dea4cbSGreg Kroah-Hartman
opp_debug_register(struct opp_device * opp_dev,struct opp_table * opp_table)297a2dea4cbSGreg Kroah-Hartman static inline void opp_debug_register(struct opp_device *opp_dev,
298a2dea4cbSGreg Kroah-Hartman struct opp_table *opp_table) { }
2997813dd6fSViresh Kumar
opp_debug_unregister(struct opp_device * opp_dev,struct opp_table * opp_table)3007813dd6fSViresh Kumar static inline void opp_debug_unregister(struct opp_device *opp_dev,
3017813dd6fSViresh Kumar struct opp_table *opp_table)
3027813dd6fSViresh Kumar { }
3037813dd6fSViresh Kumar #endif /* DEBUG_FS */
3047813dd6fSViresh Kumar
3057813dd6fSViresh Kumar #endif /* __DRIVER_OPP_H__ */
306