opp.h 8.9 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
 * @regulator_count: Number of power supply regulators. Its value can be -1
150
 * @regulator_enabled: Set to true if regulators were previously enabled.
151 152
 * (uninitialized), 0 (no opp-microvolt property) or > 0 (has opp-microvolt
 * property).
153 154
 * @paths: Interconnect path handles
 * @path_count: Number of interconnect paths
155
 * @genpd_performance_state: Device's power domain support performance state.
156
 * @is_genpd: Marks if the OPP table belongs to a genpd.
157
 * @set_opp: Platform specific set_opp callback
158
 * @set_opp_data: Data to be passed to set_opp callback
V
Viresh Kumar 已提交
159 160
 * @dentry:	debugfs dentry pointer of the real device directory (not links).
 * @dentry_name: Name of the real dentry.
161
 *
162 163
 * @voltage_tolerance_v1: In percentage, for v1 bindings only.
 *
164 165 166 167
 * 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.
 */
168
struct opp_table {
169 170
	struct list_head node;

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

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

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

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

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

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

204
	int (*set_opp)(struct dev_pm_set_opp_data *data);
205 206
	struct dev_pm_set_opp_data *set_opp_data;

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

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

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

V
Viresh Kumar 已提交
244 245
#ifdef CONFIG_DEBUG_FS
void opp_debug_remove_one(struct dev_pm_opp *opp);
246 247
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);
248
void opp_debug_unregister(struct opp_device *opp_dev, struct opp_table *opp_table);
V
Viresh Kumar 已提交
249 250 251
#else
static inline void opp_debug_remove_one(struct dev_pm_opp *opp) {}

252 253 254 255 256
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 已提交
257

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

263
#endif		/* __DRIVER_OPP_H__ */