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 297813dd6fSViresh Kumar extern struct list_head opp_tables; 307813dd6fSViresh Kumar 317813dd6fSViresh Kumar /* 327813dd6fSViresh Kumar * Internal data structure organization with the OPP layer library is as 337813dd6fSViresh Kumar * follows: 347813dd6fSViresh Kumar * opp_tables (root) 357813dd6fSViresh Kumar * |- device 1 (represents voltage domain 1) 367813dd6fSViresh Kumar * | |- opp 1 (availability, freq, voltage) 377813dd6fSViresh Kumar * | |- opp 2 .. 387813dd6fSViresh Kumar * ... ... 397813dd6fSViresh Kumar * | `- opp n .. 407813dd6fSViresh Kumar * |- device 2 (represents the next voltage domain) 417813dd6fSViresh Kumar * ... 427813dd6fSViresh Kumar * `- device m (represents mth voltage domain) 437813dd6fSViresh Kumar * device 1, 2.. are represented by opp_table structure while each opp 447813dd6fSViresh Kumar * is represented by the opp structure. 457813dd6fSViresh Kumar */ 467813dd6fSViresh Kumar 477813dd6fSViresh Kumar /** 487813dd6fSViresh Kumar * struct dev_pm_opp - Generic OPP description structure 497813dd6fSViresh Kumar * @node: opp table node. The nodes are maintained throughout the lifetime 507813dd6fSViresh Kumar * of boot. It is expected only an optimal set of OPPs are 517813dd6fSViresh Kumar * added to the library by the SoC framework. 527813dd6fSViresh Kumar * IMPORTANT: the opp nodes should be maintained in increasing 537813dd6fSViresh Kumar * order. 547813dd6fSViresh Kumar * @kref: for reference count of the OPP. 557813dd6fSViresh Kumar * @available: true/false - marks if this OPP as available or not 567813dd6fSViresh Kumar * @dynamic: not-created from static DT entries. 577813dd6fSViresh Kumar * @turbo: true if turbo (boost) OPP 587813dd6fSViresh Kumar * @suspend: true if suspend OPP 59009acd19SViresh Kumar * @pstate: Device's power domain's performance state. 607813dd6fSViresh Kumar * @rate: Frequency in hertz 615b93ac54SRajendra Nayak * @level: Performance level 627813dd6fSViresh Kumar * @supplies: Power supplies voltage/current values 636d3f922cSGeorgi Djakov * @bandwidth: Interconnect bandwidth values 647813dd6fSViresh Kumar * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's 657813dd6fSViresh Kumar * frequency from any other OPP's frequency. 66da544b61SViresh Kumar * @required_opps: List of OPPs that are required by this OPP. 677813dd6fSViresh Kumar * @opp_table: points back to the opp_table struct this opp belongs to 687813dd6fSViresh Kumar * @np: OPP's device node. 697813dd6fSViresh Kumar * @dentry: debugfs dentry pointer (per opp) 707813dd6fSViresh Kumar * 717813dd6fSViresh Kumar * This structure stores the OPP information for a given device. 727813dd6fSViresh Kumar */ 737813dd6fSViresh Kumar struct dev_pm_opp { 747813dd6fSViresh Kumar struct list_head node; 757813dd6fSViresh Kumar struct kref kref; 767813dd6fSViresh Kumar 777813dd6fSViresh Kumar bool available; 787813dd6fSViresh Kumar bool dynamic; 797813dd6fSViresh Kumar bool turbo; 807813dd6fSViresh Kumar bool suspend; 81009acd19SViresh Kumar unsigned int pstate; 827813dd6fSViresh Kumar unsigned long rate; 835b93ac54SRajendra Nayak unsigned int level; 847813dd6fSViresh Kumar 857813dd6fSViresh Kumar struct dev_pm_opp_supply *supplies; 866d3f922cSGeorgi Djakov struct dev_pm_opp_icc_bw *bandwidth; 877813dd6fSViresh Kumar 887813dd6fSViresh Kumar unsigned long clock_latency_ns; 897813dd6fSViresh Kumar 90da544b61SViresh Kumar struct dev_pm_opp **required_opps; 917813dd6fSViresh Kumar struct opp_table *opp_table; 927813dd6fSViresh Kumar 937813dd6fSViresh Kumar struct device_node *np; 947813dd6fSViresh Kumar 957813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS 967813dd6fSViresh Kumar struct dentry *dentry; 977813dd6fSViresh Kumar #endif 987813dd6fSViresh Kumar }; 997813dd6fSViresh Kumar 1007813dd6fSViresh Kumar /** 1017813dd6fSViresh Kumar * struct opp_device - devices managed by 'struct opp_table' 1027813dd6fSViresh Kumar * @node: list node 1037813dd6fSViresh Kumar * @dev: device to which the struct object belongs 1047813dd6fSViresh Kumar * @dentry: debugfs dentry pointer (per device) 1057813dd6fSViresh Kumar * 1067813dd6fSViresh Kumar * This is an internal data structure maintaining the devices that are managed 1077813dd6fSViresh Kumar * by 'struct opp_table'. 1087813dd6fSViresh Kumar */ 1097813dd6fSViresh Kumar struct opp_device { 1107813dd6fSViresh Kumar struct list_head node; 1117813dd6fSViresh Kumar const struct device *dev; 1127813dd6fSViresh Kumar 1137813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS 1147813dd6fSViresh Kumar struct dentry *dentry; 1157813dd6fSViresh Kumar #endif 1167813dd6fSViresh Kumar }; 1177813dd6fSViresh Kumar 1187813dd6fSViresh Kumar enum opp_table_access { 1197813dd6fSViresh Kumar OPP_TABLE_ACCESS_UNKNOWN = 0, 1207813dd6fSViresh Kumar OPP_TABLE_ACCESS_EXCLUSIVE = 1, 1217813dd6fSViresh Kumar OPP_TABLE_ACCESS_SHARED = 2, 1227813dd6fSViresh Kumar }; 1237813dd6fSViresh Kumar 1247813dd6fSViresh Kumar /** 1257813dd6fSViresh Kumar * struct opp_table - Device opp structure 1267813dd6fSViresh Kumar * @node: table node - contains the devices with OPPs that 1277813dd6fSViresh Kumar * have been registered. Nodes once added are not modified in this 1287813dd6fSViresh Kumar * table. 1297813dd6fSViresh Kumar * @head: notifier head to notify the OPP availability changes. 1307813dd6fSViresh Kumar * @dev_list: list of devices that share these OPPs 1317813dd6fSViresh Kumar * @opp_list: table of opps 1327813dd6fSViresh Kumar * @kref: for reference count of the table. 1333d255699SViresh Kumar * @lock: mutex protecting the opp_list and dev_list. 1347813dd6fSViresh Kumar * @np: struct device_node pointer for opp's DT node. 1357813dd6fSViresh Kumar * @clock_latency_ns_max: Max clock latency in nanoseconds. 13603758d60SViresh Kumar * @parsed_static_opps: Count of devices for which OPPs are initialized from DT. 1377813dd6fSViresh Kumar * @shared_opp: OPP is shared between multiple devices. 1387813dd6fSViresh Kumar * @suspend_opp: Pointer to OPP to be used during device suspend. 1394f018bc0SViresh Kumar * @genpd_virt_dev_lock: Mutex protecting the genpd virtual device pointers. 1404f018bc0SViresh Kumar * @genpd_virt_devs: List of virtual devices for multiple genpd support. 1415d6d106fSViresh Kumar * @required_opp_tables: List of device OPP tables that are required by OPPs in 1425d6d106fSViresh Kumar * this table. 1435d6d106fSViresh Kumar * @required_opp_count: Number of required devices. 1447813dd6fSViresh Kumar * @supported_hw: Array of version number to support. 1457813dd6fSViresh Kumar * @supported_hw_count: Number of elements in supported_hw array. 1467813dd6fSViresh Kumar * @prop_name: A name to postfix to many DT properties, while parsing them. 1477813dd6fSViresh Kumar * @clk: Device's clock handle 1487813dd6fSViresh Kumar * @regulators: Supply regulators 14946f48acaSViresh Kumar * @regulator_count: Number of power supply regulators. Its value can be -1 15046f48acaSViresh Kumar * (uninitialized), 0 (no opp-microvolt property) or > 0 (has opp-microvolt 15146f48acaSViresh Kumar * property). 1526d3f922cSGeorgi Djakov * @paths: Interconnect path handles 1536d3f922cSGeorgi Djakov * @path_count: Number of interconnect paths 154*72f80ce4SViresh Kumar * @enabled: Set to true if the device's resources are enabled/configured. 155009acd19SViresh Kumar * @genpd_performance_state: Device's power domain support performance state. 15661d8e7c7SViresh Kumar * @is_genpd: Marks if the OPP table belongs to a genpd. 1577813dd6fSViresh Kumar * @set_opp: Platform specific set_opp callback 1587813dd6fSViresh Kumar * @set_opp_data: Data to be passed to set_opp callback 1597813dd6fSViresh Kumar * @dentry: debugfs dentry pointer of the real device directory (not links). 1607813dd6fSViresh Kumar * @dentry_name: Name of the real dentry. 1617813dd6fSViresh Kumar * 1627813dd6fSViresh Kumar * @voltage_tolerance_v1: In percentage, for v1 bindings only. 1637813dd6fSViresh Kumar * 1647813dd6fSViresh Kumar * This is an internal data structure maintaining the link to opps attached to 1657813dd6fSViresh Kumar * a device. This structure is not meant to be shared to users as it is 1667813dd6fSViresh Kumar * meant for book keeping and private to OPP library. 1677813dd6fSViresh Kumar */ 1687813dd6fSViresh Kumar struct opp_table { 1697813dd6fSViresh Kumar struct list_head node; 1707813dd6fSViresh Kumar 1717813dd6fSViresh Kumar struct blocking_notifier_head head; 1727813dd6fSViresh Kumar struct list_head dev_list; 1737813dd6fSViresh Kumar struct list_head opp_list; 1747813dd6fSViresh Kumar struct kref kref; 1757813dd6fSViresh Kumar struct mutex lock; 1767813dd6fSViresh Kumar 1777813dd6fSViresh Kumar struct device_node *np; 1787813dd6fSViresh Kumar unsigned long clock_latency_ns_max; 1797813dd6fSViresh Kumar 1807813dd6fSViresh Kumar /* For backward compatibility with v1 bindings */ 1817813dd6fSViresh Kumar unsigned int voltage_tolerance_v1; 1827813dd6fSViresh Kumar 18303758d60SViresh Kumar unsigned int parsed_static_opps; 1847813dd6fSViresh Kumar enum opp_table_access shared_opp; 1857813dd6fSViresh Kumar struct dev_pm_opp *suspend_opp; 1867813dd6fSViresh Kumar 1874f018bc0SViresh Kumar struct mutex genpd_virt_dev_lock; 1884f018bc0SViresh Kumar struct device **genpd_virt_devs; 1895d6d106fSViresh Kumar struct opp_table **required_opp_tables; 1905d6d106fSViresh Kumar unsigned int required_opp_count; 1915d6d106fSViresh Kumar 1927813dd6fSViresh Kumar unsigned int *supported_hw; 1937813dd6fSViresh Kumar unsigned int supported_hw_count; 1947813dd6fSViresh Kumar const char *prop_name; 1957813dd6fSViresh Kumar struct clk *clk; 1967813dd6fSViresh Kumar struct regulator **regulators; 19746f48acaSViresh Kumar int regulator_count; 1986d3f922cSGeorgi Djakov struct icc_path **paths; 1996d3f922cSGeorgi Djakov unsigned int path_count; 200*72f80ce4SViresh Kumar bool enabled; 201009acd19SViresh Kumar bool genpd_performance_state; 20261d8e7c7SViresh Kumar bool is_genpd; 2037813dd6fSViresh Kumar 2047813dd6fSViresh Kumar int (*set_opp)(struct dev_pm_set_opp_data *data); 2057813dd6fSViresh Kumar struct dev_pm_set_opp_data *set_opp_data; 2067813dd6fSViresh Kumar 2077813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS 2087813dd6fSViresh Kumar struct dentry *dentry; 2097813dd6fSViresh Kumar char dentry_name[NAME_MAX]; 2107813dd6fSViresh Kumar #endif 2117813dd6fSViresh Kumar }; 2127813dd6fSViresh Kumar 2137813dd6fSViresh Kumar /* Routines internal to opp core */ 214a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp); 215cdd6ed90SViresh Kumar void _opp_remove_all_static(struct opp_table *opp_table); 2167813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table); 217a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table); 2187813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev); 2197813dd6fSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table); 2202a4eb735SViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev); 2217813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table); 2227813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp); 2236c591eecSSaravana Kannan int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2); 224a1e8c136SViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table, bool rate_not_available); 2257813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic); 2262a4eb735SViresh Kumar void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, int last_cpu); 2277813dd6fSViresh Kumar struct opp_table *_add_opp_table(struct device *dev); 228d0e8ae6cSViresh Kumar void _put_opp_list_kref(struct opp_table *opp_table); 2297813dd6fSViresh Kumar 2307813dd6fSViresh Kumar #ifdef CONFIG_OF 231eb7c8743SViresh Kumar void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index); 2325d6d106fSViresh Kumar void _of_clear_opp_table(struct opp_table *opp_table); 233283d55e6SViresh Kumar struct opp_table *_managed_opp(struct device *dev, int index); 234da544b61SViresh Kumar void _of_opp_free_required_opps(struct opp_table *opp_table, 235da544b61SViresh Kumar struct dev_pm_opp *opp); 2367813dd6fSViresh Kumar #else 237eb7c8743SViresh Kumar static inline void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index) {} 2385d6d106fSViresh Kumar static inline void _of_clear_opp_table(struct opp_table *opp_table) {} 239283d55e6SViresh Kumar static inline struct opp_table *_managed_opp(struct device *dev, int index) { return NULL; } 240da544b61SViresh Kumar static inline void _of_opp_free_required_opps(struct opp_table *opp_table, 241da544b61SViresh Kumar struct dev_pm_opp *opp) {} 2427813dd6fSViresh Kumar #endif 2437813dd6fSViresh Kumar 2447813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS 2457813dd6fSViresh Kumar void opp_debug_remove_one(struct dev_pm_opp *opp); 246a2dea4cbSGreg Kroah-Hartman void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table); 247a2dea4cbSGreg Kroah-Hartman void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table); 2487813dd6fSViresh Kumar void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table); 2497813dd6fSViresh Kumar #else 2507813dd6fSViresh Kumar static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {} 2517813dd6fSViresh Kumar 252a2dea4cbSGreg Kroah-Hartman static inline void opp_debug_create_one(struct dev_pm_opp *opp, 253a2dea4cbSGreg Kroah-Hartman struct opp_table *opp_table) { } 254a2dea4cbSGreg Kroah-Hartman 255a2dea4cbSGreg Kroah-Hartman static inline void opp_debug_register(struct opp_device *opp_dev, 256a2dea4cbSGreg Kroah-Hartman struct opp_table *opp_table) { } 2577813dd6fSViresh Kumar 2587813dd6fSViresh Kumar static inline void opp_debug_unregister(struct opp_device *opp_dev, 2597813dd6fSViresh Kumar struct opp_table *opp_table) 2607813dd6fSViresh Kumar { } 2617813dd6fSViresh Kumar #endif /* DEBUG_FS */ 2627813dd6fSViresh Kumar 2637813dd6fSViresh Kumar #endif /* __DRIVER_OPP_H__ */ 264