opp.h 8.8 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-only */
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Generic OPP Interface
 *
 * Copyright (C) 2009-2010 Texas Instruments Incorporated.
 *	Nishanth Menon
 *	Romit Dasgupta
 *	Kevin Hilman
 */

#ifndef __DRIVER_OPP_H__
#define __DRIVER_OPP_H__

#include <linux/device.h>
15
#include <linux/interconnect.h>
16
#include <linux/kernel.h>
17
#include <linux/kref.h>
18
#include <linux/list.h>
V
Viresh Kumar 已提交
19
#include <linux/limits.h>
20
#include <linux/pm_opp.h>
21
#include <linux/notifier.h>
22

V
Viresh Kumar 已提交
23
struct clk;
24 25
struct regulator;

26
/* Lock to allow exclusive modification to the device and opp lists */
27
extern struct mutex opp_table_lock;
28

29 30
extern struct list_head opp_tables;

31 32 33
/*
 * Internal data structure organization with the OPP layer library is as
 * follows:
34
 * opp_tables (root)
35 36 37 38 39 40 41 42
 *	|- device 1 (represents voltage domain 1)
 *	|	|- opp 1 (availability, freq, voltage)
 *	|	|- opp 2 ..
 *	...	...
 *	|	`- opp n ..
 *	|- device 2 (represents the next voltage domain)
 *	...
 *	`- device m (represents mth voltage domain)
43
 * device 1, 2.. are represented by opp_table structure while each opp
44 45 46 47 48
 * is represented by the opp structure.
 */

/**
 * struct dev_pm_opp - Generic OPP description structure
49
 * @node:	opp table node. The nodes are maintained throughout the lifetime
50 51 52 53
 *		of boot. It is expected only an optimal set of OPPs are
 *		added to the library by the SoC framework.
 *		IMPORTANT: the opp nodes should be maintained in increasing
 *		order.
54
 * @kref:	for reference count of the OPP.
55
 * @available:	true/false - marks if this OPP as available or not
V
Viresh Kumar 已提交
56
 * @dynamic:	not-created from static DT entries.
57
 * @turbo:	true if turbo (boost) OPP
V
Viresh Kumar 已提交
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 65
 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's
 *		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.
V
Viresh Kumar 已提交
69
 * @dentry:	debugfs dentry pointer (per opp)
70 71 72 73 74
 *
 * This structure stores the OPP information for a given device.
 */
struct dev_pm_opp {
	struct list_head node;
75
	struct kref kref;
76 77 78 79

	bool available;
	bool dynamic;
	bool turbo;
V
Viresh Kumar 已提交
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 89
	unsigned long clock_latency_ns;

90
	struct dev_pm_opp **required_opps;
91
	struct opp_table *opp_table;
92 93

	struct device_node *np;
V
Viresh Kumar 已提交
94 95 96 97

#ifdef CONFIG_DEBUG_FS
	struct dentry *dentry;
#endif
98 99 100
};

/**
101
 * struct opp_device - devices managed by 'struct opp_table'
102 103
 * @node:	list node
 * @dev:	device to which the struct object belongs
V
Viresh Kumar 已提交
104
 * @dentry:	debugfs dentry pointer (per device)
105
 *
106 107
 * This is an internal data structure maintaining the devices that are managed
 * by 'struct opp_table'.
108
 */
109
struct opp_device {
110 111
	struct list_head node;
	const struct device *dev;
V
Viresh Kumar 已提交
112 113 114 115

#ifdef CONFIG_DEBUG_FS
	struct dentry *dentry;
#endif
116 117
};

118 119 120 121 122 123
enum opp_table_access {
	OPP_TABLE_ACCESS_UNKNOWN = 0,
	OPP_TABLE_ACCESS_EXCLUSIVE = 1,
	OPP_TABLE_ACCESS_SHARED = 2,
};

124
/**
125 126
 * struct opp_table - Device opp structure
 * @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.
V
Viresh Kumar 已提交
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.
V
Viresh Kumar 已提交
138
 * @suspend_opp: Pointer to OPP to be used during device suspend.
139 140
 * @genpd_virt_dev_lock: Mutex protecting the genpd virtual device pointers.
 * @genpd_virt_devs: List of virtual devices for multiple genpd support.
141 142 143
 * @required_opp_tables: List of device OPP tables that are required by OPPs in
 *		this table.
 * @required_opp_count: Number of required devices.
144 145
 * @supported_hw: Array of version number to support.
 * @supported_hw_count: Number of elements in supported_hw array.
146
 * @prop_name: A name to postfix to many DT properties, while parsing them.
V
Viresh Kumar 已提交
147
 * @clk: Device's clock handle
148
 * @regulators: Supply regulators
149 150 151
 * @regulator_count: Number of power supply regulators. Its value can be -1
 * (uninitialized), 0 (no opp-microvolt property) or > 0 (has opp-microvolt
 * property).
152 153
 * @paths: Interconnect path handles
 * @path_count: Number of interconnect paths
154
 * @genpd_performance_state: Device's power domain support performance state.
155
 * @is_genpd: Marks if the OPP table belongs to a genpd.
156
 * @set_opp: Platform specific set_opp callback
157
 * @set_opp_data: Data to be passed to set_opp callback
V
Viresh Kumar 已提交
158 159
 * @dentry:	debugfs dentry pointer of the real device directory (not links).
 * @dentry_name: Name of the real dentry.
160
 *
161 162
 * @voltage_tolerance_v1: In percentage, for v1 bindings only.
 *
163 164 165 166
 * This is an internal data structure maintaining the link to opps attached to
 * a device. This structure is not meant to be shared to users as it is
 * meant for book keeping and private to OPP library.
 */
167
struct opp_table {
168 169
	struct list_head node;

170
	struct blocking_notifier_head head;
171 172
	struct list_head dev_list;
	struct list_head opp_list;
173
	struct kref kref;
V
Viresh Kumar 已提交
174
	struct mutex lock;
175 176 177

	struct device_node *np;
	unsigned long clock_latency_ns_max;
178 179 180 181

	/* For backward compatibility with v1 bindings */
	unsigned int voltage_tolerance_v1;

182
	unsigned int parsed_static_opps;
183
	enum opp_table_access shared_opp;
184
	struct dev_pm_opp *suspend_opp;
V
Viresh Kumar 已提交
185

186 187
	struct mutex genpd_virt_dev_lock;
	struct device **genpd_virt_devs;
188 189 190
	struct opp_table **required_opp_tables;
	unsigned int required_opp_count;

191 192
	unsigned int *supported_hw;
	unsigned int supported_hw_count;
193
	const char *prop_name;
V
Viresh Kumar 已提交
194
	struct clk *clk;
195
	struct regulator **regulators;
196
	int regulator_count;
197 198
	struct icc_path **paths;
	unsigned int path_count;
199
	bool genpd_performance_state;
200
	bool is_genpd;
201

202
	int (*set_opp)(struct dev_pm_set_opp_data *data);
203 204
	struct dev_pm_set_opp_data *set_opp_data;

V
Viresh Kumar 已提交
205 206 207 208
#ifdef CONFIG_DEBUG_FS
	struct dentry *dentry;
	char dentry_name[NAME_MAX];
#endif
209 210 211
};

/* Routines internal to opp core */
212
void dev_pm_opp_get(struct dev_pm_opp *opp);
213
void _opp_remove_all_static(struct opp_table *opp_table);
214
void _get_opp_table_kref(struct opp_table *opp_table);
215
int _get_opp_count(struct opp_table *opp_table);
216 217
struct opp_table *_find_opp_table(struct device *dev);
struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table);
218
void _dev_pm_opp_find_and_remove_table(struct device *dev);
219 220
struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table);
void _opp_free(struct dev_pm_opp *opp);
221
int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2);
222
int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table, bool rate_not_available);
223
int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic);
224
void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, int last_cpu);
225
struct opp_table *_add_opp_table(struct device *dev);
226
void _put_opp_list_kref(struct opp_table *opp_table);
227 228

#ifdef CONFIG_OF
229
void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index);
230
void _of_clear_opp_table(struct opp_table *opp_table);
231
struct opp_table *_managed_opp(struct device *dev, int index);
232 233
void _of_opp_free_required_opps(struct opp_table *opp_table,
				struct dev_pm_opp *opp);
234
#else
235
static inline void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, int index) {}
236
static inline void _of_clear_opp_table(struct opp_table *opp_table) {}
237
static inline struct opp_table *_managed_opp(struct device *dev, int index) { return NULL; }
238 239
static inline void _of_opp_free_required_opps(struct opp_table *opp_table,
					      struct dev_pm_opp *opp) {}
240
#endif
241

V
Viresh Kumar 已提交
242 243
#ifdef CONFIG_DEBUG_FS
void opp_debug_remove_one(struct dev_pm_opp *opp);
244 245
void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table);
void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table);
246
void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table);
V
Viresh Kumar 已提交
247 248 249
#else
static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {}

250 251 252 253 254
static inline void opp_debug_create_one(struct dev_pm_opp *opp,
					struct opp_table *opp_table) { }

static inline void opp_debug_register(struct opp_device *opp_dev,
				      struct opp_table *opp_table) { }
V
Viresh Kumar 已提交
255

256 257
static inline void opp_debug_unregister(struct opp_device *opp_dev,
					struct opp_table *opp_table)
V
Viresh Kumar 已提交
258 259 260
{ }
#endif		/* DEBUG_FS */

261
#endif		/* __DRIVER_OPP_H__ */