xref: /optee_os/core/include/drivers/regulator.h (revision 1dc11585ceccb39c925cc1dfd1650acbb1d70190)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2023, STMicroelectronics
4  */
5 #ifndef __DRIVERS_REGULATOR_H
6 #define __DRIVERS_REGULATOR_H
7 
8 #include <assert.h>
9 #include <bitstring.h>
10 #include <kernel/mutex_pm_aware.h>
11 #include <sys/queue.h>
12 #include <tee_api_types.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <util.h>
16 
17 /* Regulator property flags: related to device tree binding properties */
18 
19 /* Regulator should never be disabled. DT property: regulator-always-on */
20 #define REGULATOR_ALWAYS_ON	BIT(0)
21 /* Enables pull down mode. DT property: regulator-pull-down */
22 #define REGULATOR_PULL_DOWN	BIT(1)
23 /*
24  * It's expected that this regulator was left on by the bootloader.
25  * The core shouldn't prevent it from being turned off later.
26  * DT property: regulator-boot-on
27  */
28 #define REGULATOR_BOOT_ON	BIT(2)
29 /*
30  * Enables over current protection.
31  * DT property: regulator-over-current-protection
32  */
33 #define REGULATOR_OVER_CURRENT	BIT(3)
34 
35 #define REGULATOR_FLAGS_MASK	(REGULATOR_ALWAYS_ON | REGULATOR_PULL_DOWN | \
36 				 REGULATOR_BOOT_ON | REGULATOR_OVER_CURRENT)
37 
38 struct regulator_ops;
39 
40 /*
41  * struct regu_dt_desc - Regulator description passed to regulator_dt_register()
42  * @priv: Regulator driver private data
43  * @name: Regulator string name for debug purpose
44  * @supply_name: Regulator supply name for node property *-supply or NULL
45  * @ops: Operation handlers for the regulator
46  * @regulator: Pointer to preallocated regulator or NULL if none
47  */
48 struct regu_dt_desc {
49 	void *priv;
50 	const char *name;
51 	const char *supply_name;
52 	const struct regulator_ops *ops;
53 	struct regulator *regulator;
54 };
55 
56 /*
57  * Defines the format of struct voltages::entries
58  *
59  * If regulator_voltages::type is VOLTAGE_TYPE_FULL_LIST, then
60  * regulator_voltages@entries stores regulator_voltages::num_levels cells,
61  * listing supported voltage levels in uV from lowest to highest value.
62  *
63  * If regulator_voltages::type is VOLTAGE_TYPE_INCREMENT, then
64  * regulator_voltages::entries stores 3 cells: min level, max level and
65  * level increment step, all in uV. When so, regulator_voltages::num_levels
66  * is meaningless.
67  */
68 enum voltage_type {
69 	VOLTAGE_TYPE_INVALID = 0,
70 	VOLTAGE_TYPE_FULL_LIST, /* extensive list in uV */
71 	VOLTAGE_TYPE_INCREMENT  /* min, max, increment (in uV) */
72 };
73 
74 /*
75  * struct regulator_voltages_desc - Voltage levels description
76  * @type: Type of level description
77  * @num_levels: Number of voltage levels when @type is VOLTAGE_TYPE_FULL_LIST
78  *
79  */
80 struct regulator_voltages_desc {
81 	enum voltage_type type;
82 	size_t num_levels;
83 };
84 
85 /*
86  * struct regulator - A regulator instance
87  * @ops: Operation handlers for the regulator
88  * @supply: Regulator supply reference or NULL if none
89  * @priv: Regulator driver private data
90  * @name: Regulator string name for debug purpose or NULL
91  * @min_uv: Min possible voltage level in microvolt (uV)
92  * @max_uv: Max possible voltage level in microvolt (uV)
93  * @flags: REGULATOR_* property flags
94  * @refcount: Regulator enable request reference counter
95  * @mutex: Concurrent access protection considering PM context sequences
96  * @voltages_fallback: Default supported voltage range description
97  * @link: Link in initialized regulator list
98  */
99 struct regulator {
100 	/* Fields initialized by caller of regulator_register() */
101 	const struct regulator_ops *ops;
102 	struct regulator *supply;
103 	void *priv;
104 	char *name;
105 	int min_uv;
106 	int max_uv;
107 	/* Fields internal to regulator framework */
108 	unsigned int flags;
109 	unsigned int refcount;
110 	struct mutex_pm_aware mutex;
111 	struct voltages_fallback {
112 		struct regulator_voltages_desc desc;
113 		int levels[3];
114 	} voltages_fallback;
115 	size_t levels_count_fallback;
116 	SLIST_ENTRY(regulator) link;
117 };
118 
119 /*
120  * struct regulator_ops - Regulator operation handlers
121  *
122  * @set_state: Enable or disable a regulator
123  * @get_state: Get regulator effective state
124  * @set_voltage: Set voltage level in microvolt (uV)
125  * @get_voltage: Get current voltage in microvolt (uV)
126  * @supported_voltages: Get supported levels description
127  * @supplied_init: Optional, finalize initialization once supply is ready
128  */
129 struct regulator_ops {
130 	TEE_Result (*set_state)(struct regulator *r, bool enabled);
131 	TEE_Result (*get_state)(struct regulator *r, bool *enabled);
132 	TEE_Result (*set_voltage)(struct regulator *r, int level_uv);
133 	TEE_Result (*get_voltage)(struct regulator *r, int *level_uv);
134 	TEE_Result (*supported_voltages)(struct regulator *r,
135 					 struct regulator_voltages_desc **desc,
136 					 const int **levels);
137 	TEE_Result (*supplied_init)(struct regulator *r, const void *fdt,
138 				    int node);
139 };
140 
141 #ifdef CFG_DRIVERS_REGULATOR
142 /*
143  * regulator_enable() - Enable regulator
144  * @regulator: Regulator reference
145  */
146 TEE_Result regulator_enable(struct regulator *regulator);
147 
148 /*
149  * regulator_disable() - Disable regulator
150  * @regulator: Regulator reference
151  */
152 TEE_Result regulator_disable(struct regulator *regulator);
153 
154 /*
155  * regulator_is_enabled() - Return whether or not regulator is currently enabled
156  * despite its refcount value.
157  * @regulator: Regulator reference
158  */
159 bool regulator_is_enabled(struct regulator *regulator);
160 
161 /*
162  * regulator_set_voltage() - Set regulator to target level in microvolt
163  * @regulator: Regulator reference
164  * @level_uv: Level in microvolt
165  */
166 TEE_Result regulator_set_voltage(struct regulator *regulator, int level_uv);
167 
168 /*
169  * regulator_register() - Register and initialize a regulator
170  * @regulator: Regulator reference
171  */
172 TEE_Result regulator_register(struct regulator *regulator);
173 
174 /* Print registered regulators and their state to the output console */
175 void regulator_print_state(const char *message);
176 #else
177 static inline TEE_Result regulator_enable(struct regulator *regulator __unused)
178 {
179 	return TEE_ERROR_NOT_SUPPORTED;
180 }
181 
182 static inline TEE_Result regulator_disable(struct regulator *regulator __unused)
183 {
184 	return TEE_ERROR_NOT_SUPPORTED;
185 }
186 
187 static inline bool regulator_is_enabled(struct regulator *regulator __unused)
188 {
189 	return false;
190 }
191 
192 static inline TEE_Result regulator_set_voltage(struct regulator *regul __unused,
193 					       int level_mv __unused)
194 {
195 	return TEE_ERROR_NOT_SUPPORTED;
196 }
197 
198 static inline TEE_Result regulator_init(struct regulator *regulator __unused)
199 {
200 	return TEE_ERROR_NOT_SUPPORTED;
201 }
202 
203 static inline void regulator_print_state(const char *message __unused)
204 {
205 }
206 #endif /*CFG_DRIVERS_REGULATOR*/
207 
208 #if defined(CFG_DRIVERS_REGULATOR) && defined(CFG_DT)
209 /*
210  * regulator_dt_get_supply() - Get a regulator supply from name and DT node
211  * @fdt: FDT to work on
212  * @node: DT node of the regulator consumer
213  * @supply_name: Name of the supply in DT property xxx-supply
214  * @regulator: Output regulator upon success
215  *
216  * Upon success, this function provides the pointer to regulator
217  * defined by DT binding property @name-supply phandle reference.
218  *
219  * This function returns TEE_ERROR_DEFER_DRIVER_INIT if supply exists but is
220  * not yet initialized.
221  */
222 TEE_Result regulator_dt_get_supply(const void *fdt, int node,
223 				   const char *supply_name,
224 				   struct regulator **regulator);
225 
226 /*
227  * regulator_dt_register() - Register a regulator to related to a DT node
228  * @fdt: FDT to work on
229  * @node: DT node of the regulator exposed by regulator driver
230  * @provider_node: Node where xxx-supply property is found or -1 if no supply.
231  * @desc: Description of the regulator to register
232  *
233  * This function registers and initializes a regulator instance once its supply
234  * if found, if any. Regulators registered with this function can be found by
235  * their consumer drivers using API function regulator_dt_get_supply() or like.
236  *
237  * Return TEE_SUCCESS in case of success
238  * Return TEE_ERROR_OUT_OF_MEMORY if failed on memory allocation
239  * Return any other TEE_Result compliant code in case of error
240  */
241 TEE_Result regulator_dt_register(const void *fdt, int node, int provider_node,
242 				 const struct regu_dt_desc *desc);
243 #else
244 static inline TEE_Result regulator_dt_get_supply(const void *fdt __unused,
245 						 int node __unused,
246 						 const char *supply __unused,
247 						 struct regulator **r __unused)
248 {
249 	return TEE_ERROR_NOT_SUPPORTED;
250 }
251 
252 static inline TEE_Result
253 regulator_dt_register(const void *fdt __unused, int node __unused,
254 		      int provider_node __unused,
255 		      const struct regu_dt_desc *d __unused)
256 {
257 	return TEE_ERROR_NOT_SUPPORTED;
258 }
259 #endif /* CFG_DRIVERS_REGULATOR && CFG_DT */
260 
261 /*
262  * regulator_name() - Return regulator name or NULL
263  * @regulator: Regulator reference
264  */
265 static inline const char *regulator_name(struct regulator *regulator)
266 {
267 	return regulator->name;
268 }
269 
270 /*
271  * regulator_is_always_on() - Return the state of REGULATOR_ALWAYS_ON flag
272  * @regulator: Regulator reference
273  */
274 static inline bool regulator_is_always_on(struct regulator *regulator)
275 {
276 	return regulator->flags & REGULATOR_ALWAYS_ON;
277 }
278 
279 /*
280  * regulator_set_min_voltage() - Set regulator to its min level
281  * @regulator: Regulator reference
282  */
283 static inline TEE_Result regulator_set_min_voltage(struct regulator *regulator)
284 {
285 	return regulator_set_voltage(regulator, regulator->min_uv);
286 }
287 
288 /*
289  * regulator_get_voltage() - Get regulator effective voltage level in microvolt
290  * @regulator: Regulator reference
291  */
292 int regulator_get_voltage(struct regulator *regulator);
293 
294 /*
295  * regulator_get_range() - Get regulator min and/or max support levels
296  * @regulator: Regulator reference
297  * @min_mv: Output reference to min level in microvolt (uV) or NULL
298  * @max_mv: Output reference to max level in microvolt (uV) or NULL
299  */
300 static inline void regulator_get_range(struct regulator *regulator, int *min_uv,
301 				       int *max_uv)
302 {
303 	assert(regulator);
304 	if (min_uv)
305 		*min_uv = regulator->min_uv;
306 	if (max_uv)
307 		*max_uv = regulator->max_uv;
308 }
309 
310 /*
311  * regulator_supported_voltages() - Get regulator supported levels in microvolt
312  * @regulator: Regulator reference
313  * @desc: Output reference to supported voltage levels description
314  * @levels: Output reference to voltage level array, in microvolts
315  *
316  * When @desc->type is VOLTAGE_TYPE_FULL_LIST, number of cells of @*levels
317  * is defined by @desc->num_levels, each cell being a level in microvolts (uV).
318  * When @desc->type is VOLTAGE_TYPE_INCREMENT, @levels has 3 cells:
319  * @levels[0] is the min voltage level, @levels[1] is the max level, @levels[2]
320  * is the incremental level step, all in microvolts (uV).
321  */
322 TEE_Result regulator_supported_voltages(struct regulator *regulator,
323 					struct regulator_voltages_desc **desc,
324 					const int **levels);
325 
326 /* Print current regulator tree summary to console with info trace level */
327 #ifdef CFG_DRIVERS_REGULATOR
328 void regulator_print_tree(void);
329 #else
330 static inline void regulator_print_tree(void)
331 {
332 }
333 #endif /* CFG_DRIVERS_REGULATOR */
334 #endif /* __DRIVERS_REGULATOR_H */
335