xref: /linux/drivers/opp/opp.h (revision a88bd2a5)
17813dd6fSViresh Kumar /*
27813dd6fSViresh Kumar  * Generic OPP Interface
37813dd6fSViresh Kumar  *
47813dd6fSViresh Kumar  * Copyright (C) 2009-2010 Texas Instruments Incorporated.
57813dd6fSViresh Kumar  *	Nishanth Menon
67813dd6fSViresh Kumar  *	Romit Dasgupta
77813dd6fSViresh Kumar  *	Kevin Hilman
87813dd6fSViresh Kumar  *
97813dd6fSViresh Kumar  * This program is free software; you can redistribute it and/or modify
107813dd6fSViresh Kumar  * it under the terms of the GNU General Public License version 2 as
117813dd6fSViresh Kumar  * published by the Free Software Foundation.
127813dd6fSViresh Kumar  */
137813dd6fSViresh Kumar 
147813dd6fSViresh Kumar #ifndef __DRIVER_OPP_H__
157813dd6fSViresh Kumar #define __DRIVER_OPP_H__
167813dd6fSViresh Kumar 
177813dd6fSViresh Kumar #include <linux/device.h>
187813dd6fSViresh Kumar #include <linux/kernel.h>
197813dd6fSViresh Kumar #include <linux/kref.h>
207813dd6fSViresh Kumar #include <linux/list.h>
217813dd6fSViresh Kumar #include <linux/limits.h>
227813dd6fSViresh Kumar #include <linux/pm_opp.h>
237813dd6fSViresh Kumar #include <linux/notifier.h>
247813dd6fSViresh Kumar 
257813dd6fSViresh Kumar struct clk;
267813dd6fSViresh Kumar struct regulator;
277813dd6fSViresh Kumar 
287813dd6fSViresh Kumar /* Lock to allow exclusive modification to the device and opp lists */
297813dd6fSViresh Kumar extern struct mutex opp_table_lock;
307813dd6fSViresh Kumar 
317813dd6fSViresh Kumar extern struct list_head opp_tables;
327813dd6fSViresh Kumar 
337813dd6fSViresh Kumar /*
347813dd6fSViresh Kumar  * Internal data structure organization with the OPP layer library is as
357813dd6fSViresh Kumar  * follows:
367813dd6fSViresh Kumar  * opp_tables (root)
377813dd6fSViresh Kumar  *	|- device 1 (represents voltage domain 1)
387813dd6fSViresh Kumar  *	|	|- opp 1 (availability, freq, voltage)
397813dd6fSViresh Kumar  *	|	|- opp 2 ..
407813dd6fSViresh Kumar  *	...	...
417813dd6fSViresh Kumar  *	|	`- opp n ..
427813dd6fSViresh Kumar  *	|- device 2 (represents the next voltage domain)
437813dd6fSViresh Kumar  *	...
447813dd6fSViresh Kumar  *	`- device m (represents mth voltage domain)
457813dd6fSViresh Kumar  * device 1, 2.. are represented by opp_table structure while each opp
467813dd6fSViresh Kumar  * is represented by the opp structure.
477813dd6fSViresh Kumar  */
487813dd6fSViresh Kumar 
497813dd6fSViresh Kumar /**
507813dd6fSViresh Kumar  * struct dev_pm_opp - Generic OPP description structure
517813dd6fSViresh Kumar  * @node:	opp table node. The nodes are maintained throughout the lifetime
527813dd6fSViresh Kumar  *		of boot. It is expected only an optimal set of OPPs are
537813dd6fSViresh Kumar  *		added to the library by the SoC framework.
547813dd6fSViresh Kumar  *		IMPORTANT: the opp nodes should be maintained in increasing
557813dd6fSViresh Kumar  *		order.
567813dd6fSViresh Kumar  * @kref:	for reference count of the OPP.
577813dd6fSViresh Kumar  * @available:	true/false - marks if this OPP as available or not
587813dd6fSViresh Kumar  * @dynamic:	not-created from static DT entries.
597813dd6fSViresh Kumar  * @turbo:	true if turbo (boost) OPP
607813dd6fSViresh Kumar  * @suspend:	true if suspend OPP
61009acd19SViresh Kumar  * @pstate: Device's power domain's performance state.
627813dd6fSViresh Kumar  * @rate:	Frequency in hertz
637813dd6fSViresh Kumar  * @supplies:	Power supplies voltage/current values
647813dd6fSViresh Kumar  * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
657813dd6fSViresh Kumar  *		frequency from any other OPP's frequency.
667813dd6fSViresh Kumar  * @opp_table:	points back to the opp_table struct this opp belongs to
677813dd6fSViresh Kumar  * @np:		OPP's device node.
687813dd6fSViresh Kumar  * @dentry:	debugfs dentry pointer (per opp)
697813dd6fSViresh Kumar  *
707813dd6fSViresh Kumar  * This structure stores the OPP information for a given device.
717813dd6fSViresh Kumar  */
727813dd6fSViresh Kumar struct dev_pm_opp {
737813dd6fSViresh Kumar 	struct list_head node;
747813dd6fSViresh Kumar 	struct kref kref;
757813dd6fSViresh Kumar 
767813dd6fSViresh Kumar 	bool available;
777813dd6fSViresh Kumar 	bool dynamic;
787813dd6fSViresh Kumar 	bool turbo;
797813dd6fSViresh Kumar 	bool suspend;
80009acd19SViresh Kumar 	unsigned int pstate;
817813dd6fSViresh Kumar 	unsigned long rate;
827813dd6fSViresh Kumar 
837813dd6fSViresh Kumar 	struct dev_pm_opp_supply *supplies;
847813dd6fSViresh Kumar 
857813dd6fSViresh Kumar 	unsigned long clock_latency_ns;
867813dd6fSViresh Kumar 
877813dd6fSViresh Kumar 	struct opp_table *opp_table;
887813dd6fSViresh Kumar 
897813dd6fSViresh Kumar 	struct device_node *np;
907813dd6fSViresh Kumar 
917813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
927813dd6fSViresh Kumar 	struct dentry *dentry;
937813dd6fSViresh Kumar #endif
947813dd6fSViresh Kumar };
957813dd6fSViresh Kumar 
967813dd6fSViresh Kumar /**
977813dd6fSViresh Kumar  * struct opp_device - devices managed by 'struct opp_table'
987813dd6fSViresh Kumar  * @node:	list node
997813dd6fSViresh Kumar  * @dev:	device to which the struct object belongs
1007813dd6fSViresh Kumar  * @dentry:	debugfs dentry pointer (per device)
1017813dd6fSViresh Kumar  *
1027813dd6fSViresh Kumar  * This is an internal data structure maintaining the devices that are managed
1037813dd6fSViresh Kumar  * by 'struct opp_table'.
1047813dd6fSViresh Kumar  */
1057813dd6fSViresh Kumar struct opp_device {
1067813dd6fSViresh Kumar 	struct list_head node;
1077813dd6fSViresh Kumar 	const struct device *dev;
1087813dd6fSViresh Kumar 
1097813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
1107813dd6fSViresh Kumar 	struct dentry *dentry;
1117813dd6fSViresh Kumar #endif
1127813dd6fSViresh Kumar };
1137813dd6fSViresh Kumar 
1147813dd6fSViresh Kumar enum opp_table_access {
1157813dd6fSViresh Kumar 	OPP_TABLE_ACCESS_UNKNOWN = 0,
1167813dd6fSViresh Kumar 	OPP_TABLE_ACCESS_EXCLUSIVE = 1,
1177813dd6fSViresh Kumar 	OPP_TABLE_ACCESS_SHARED = 2,
1187813dd6fSViresh Kumar };
1197813dd6fSViresh Kumar 
1207813dd6fSViresh Kumar /**
1217813dd6fSViresh Kumar  * struct opp_table - Device opp structure
1227813dd6fSViresh Kumar  * @node:	table node - contains the devices with OPPs that
1237813dd6fSViresh Kumar  *		have been registered. Nodes once added are not modified in this
1247813dd6fSViresh Kumar  *		table.
1257813dd6fSViresh Kumar  * @head:	notifier head to notify the OPP availability changes.
1267813dd6fSViresh Kumar  * @dev_list:	list of devices that share these OPPs
1277813dd6fSViresh Kumar  * @opp_list:	table of opps
1287813dd6fSViresh Kumar  * @kref:	for reference count of the table.
1297813dd6fSViresh Kumar  * @lock:	mutex protecting the opp_list.
1307813dd6fSViresh Kumar  * @np:		struct device_node pointer for opp's DT node.
1317813dd6fSViresh Kumar  * @clock_latency_ns_max: Max clock latency in nanoseconds.
1327813dd6fSViresh Kumar  * @shared_opp: OPP is shared between multiple devices.
1337813dd6fSViresh Kumar  * @suspend_opp: Pointer to OPP to be used during device suspend.
1347813dd6fSViresh Kumar  * @supported_hw: Array of version number to support.
1357813dd6fSViresh Kumar  * @supported_hw_count: Number of elements in supported_hw array.
1367813dd6fSViresh Kumar  * @prop_name: A name to postfix to many DT properties, while parsing them.
1377813dd6fSViresh Kumar  * @clk: Device's clock handle
1387813dd6fSViresh Kumar  * @regulators: Supply regulators
1397813dd6fSViresh Kumar  * @regulator_count: Number of power supply regulators
140009acd19SViresh Kumar  * @genpd_performance_state: Device's power domain support performance state.
1417813dd6fSViresh Kumar  * @set_opp: Platform specific set_opp callback
1427813dd6fSViresh Kumar  * @set_opp_data: Data to be passed to set_opp callback
143b6aa9836SViresh Kumar  * @get_pstate: Platform specific get_pstate callback
1447813dd6fSViresh Kumar  * @dentry:	debugfs dentry pointer of the real device directory (not links).
1457813dd6fSViresh Kumar  * @dentry_name: Name of the real dentry.
1467813dd6fSViresh Kumar  *
1477813dd6fSViresh Kumar  * @voltage_tolerance_v1: In percentage, for v1 bindings only.
1487813dd6fSViresh Kumar  *
1497813dd6fSViresh Kumar  * This is an internal data structure maintaining the link to opps attached to
1507813dd6fSViresh Kumar  * a device. This structure is not meant to be shared to users as it is
1517813dd6fSViresh Kumar  * meant for book keeping and private to OPP library.
1527813dd6fSViresh Kumar  */
1537813dd6fSViresh Kumar struct opp_table {
1547813dd6fSViresh Kumar 	struct list_head node;
1557813dd6fSViresh Kumar 
1567813dd6fSViresh Kumar 	struct blocking_notifier_head head;
1577813dd6fSViresh Kumar 	struct list_head dev_list;
1587813dd6fSViresh Kumar 	struct list_head opp_list;
1597813dd6fSViresh Kumar 	struct kref kref;
1607813dd6fSViresh Kumar 	struct mutex lock;
1617813dd6fSViresh Kumar 
1627813dd6fSViresh Kumar 	struct device_node *np;
1637813dd6fSViresh Kumar 	unsigned long clock_latency_ns_max;
1647813dd6fSViresh Kumar 
1657813dd6fSViresh Kumar 	/* For backward compatibility with v1 bindings */
1667813dd6fSViresh Kumar 	unsigned int voltage_tolerance_v1;
1677813dd6fSViresh Kumar 
1687813dd6fSViresh Kumar 	enum opp_table_access shared_opp;
1697813dd6fSViresh Kumar 	struct dev_pm_opp *suspend_opp;
1707813dd6fSViresh Kumar 
1717813dd6fSViresh Kumar 	unsigned int *supported_hw;
1727813dd6fSViresh Kumar 	unsigned int supported_hw_count;
1737813dd6fSViresh Kumar 	const char *prop_name;
1747813dd6fSViresh Kumar 	struct clk *clk;
1757813dd6fSViresh Kumar 	struct regulator **regulators;
1767813dd6fSViresh Kumar 	unsigned int regulator_count;
177009acd19SViresh Kumar 	bool genpd_performance_state;
1787813dd6fSViresh Kumar 
1797813dd6fSViresh Kumar 	int (*set_opp)(struct dev_pm_set_opp_data *data);
1807813dd6fSViresh Kumar 	struct dev_pm_set_opp_data *set_opp_data;
181b6aa9836SViresh Kumar 	int (*get_pstate)(struct device *dev, unsigned long rate);
1827813dd6fSViresh Kumar 
1837813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
1847813dd6fSViresh Kumar 	struct dentry *dentry;
1857813dd6fSViresh Kumar 	char dentry_name[NAME_MAX];
1867813dd6fSViresh Kumar #endif
1877813dd6fSViresh Kumar };
1887813dd6fSViresh Kumar 
1897813dd6fSViresh Kumar /* Routines internal to opp core */
190*a88bd2a5SViresh Kumar void dev_pm_opp_get(struct dev_pm_opp *opp);
1917813dd6fSViresh Kumar void _get_opp_table_kref(struct opp_table *opp_table);
192a1e8c136SViresh Kumar int _get_opp_count(struct opp_table *opp_table);
1937813dd6fSViresh Kumar struct opp_table *_find_opp_table(struct device *dev);
1947813dd6fSViresh Kumar struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
1957813dd6fSViresh Kumar void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, bool remove_all);
1967813dd6fSViresh Kumar void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all);
1977813dd6fSViresh Kumar struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table);
1987813dd6fSViresh Kumar void _opp_free(struct dev_pm_opp *opp);
199a1e8c136SViresh Kumar int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table, bool rate_not_available);
2007813dd6fSViresh Kumar int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic);
2017813dd6fSViresh Kumar void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of);
2027813dd6fSViresh Kumar struct opp_table *_add_opp_table(struct device *dev);
2037813dd6fSViresh Kumar 
2047813dd6fSViresh Kumar #ifdef CONFIG_OF
2057813dd6fSViresh Kumar void _of_init_opp_table(struct opp_table *opp_table, struct device *dev);
2067813dd6fSViresh Kumar #else
2077813dd6fSViresh Kumar static inline void _of_init_opp_table(struct opp_table *opp_table, struct device *dev) {}
2087813dd6fSViresh Kumar #endif
2097813dd6fSViresh Kumar 
2107813dd6fSViresh Kumar #ifdef CONFIG_DEBUG_FS
2117813dd6fSViresh Kumar void opp_debug_remove_one(struct dev_pm_opp *opp);
2127813dd6fSViresh Kumar int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table);
2137813dd6fSViresh Kumar int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table);
2147813dd6fSViresh Kumar void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table);
2157813dd6fSViresh Kumar #else
2167813dd6fSViresh Kumar static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {}
2177813dd6fSViresh Kumar 
2187813dd6fSViresh Kumar static inline int opp_debug_create_one(struct dev_pm_opp *opp,
2197813dd6fSViresh Kumar 				       struct opp_table *opp_table)
2207813dd6fSViresh Kumar { return 0; }
2217813dd6fSViresh Kumar static inline int opp_debug_register(struct opp_device *opp_dev,
2227813dd6fSViresh Kumar 				     struct opp_table *opp_table)
2237813dd6fSViresh Kumar { return 0; }
2247813dd6fSViresh Kumar 
2257813dd6fSViresh Kumar static inline void opp_debug_unregister(struct opp_device *opp_dev,
2267813dd6fSViresh Kumar 					struct opp_table *opp_table)
2277813dd6fSViresh Kumar { }
2287813dd6fSViresh Kumar #endif		/* DEBUG_FS */
2297813dd6fSViresh Kumar 
2307813dd6fSViresh Kumar #endif		/* __DRIVER_OPP_H__ */
231