1 /*
2  * Copyright (c) 2021, STMicroelectronics - All Rights Reserved
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #ifndef REGULATOR_H
7 #define REGULATOR_H
8 
9 #include <platform_def.h>
10 
11 #ifndef PLAT_NB_RDEVS
12 #error "Missing PLAT_NB_RDEVS"
13 #endif
14 
15 /*
16  * Consumer interface
17  */
18 
19 /* regulator-always-on : regulator should never be disabled */
20 #define REGUL_ALWAYS_ON		BIT(0)
21 /*
22  * regulator-boot-on:
23  * It's expected that this regulator was left on by the bootloader.
24  * The core shouldn't prevent it from being turned off later.
25  * The regulator is needed to exit from suspend so it is turned on during suspend entry.
26  */
27 #define REGUL_BOOT_ON		BIT(1)
28 /* regulator-over-current-protection: Enable over current protection. */
29 #define REGUL_OCP		BIT(2)
30 /* regulator-active-discharge: enable active discharge. */
31 #define REGUL_ACTIVE_DISCHARGE	BIT(3)
32 /* regulator-pull-down: Enable pull down resistor when the regulator is disabled. */
33 #define REGUL_PULL_DOWN		BIT(4)
34 /*
35  * st,mask-reset: set mask reset for the regulator, meaning that the regulator
36  * setting is maintained during pmic reset.
37  */
38 #define REGUL_MASK_RESET	BIT(5)
39 /* st,regulator-sink-source: set the regulator in sink source mode */
40 #define REGUL_SINK_SOURCE	BIT(6)
41 /* st,regulator-bypass: set the regulator in bypass mode */
42 #define REGUL_ENABLE_BYPASS	BIT(7)
43 
44 struct rdev *regulator_get_by_name(const char *node_name);
45 
46 struct rdev *regulator_get_by_supply_name(const void *fdt, int node, const char *name);
47 
48 int regulator_enable(struct rdev *rdev);
49 int regulator_disable(struct rdev *rdev);
50 int regulator_is_enabled(const struct rdev *rdev);
51 
52 int regulator_set_voltage(struct rdev *rdev, uint16_t volt);
53 int regulator_set_min_voltage(struct rdev *rdev);
54 int regulator_get_voltage(const struct rdev *rdev);
55 
56 int regulator_list_voltages(const struct rdev *rdev, const uint16_t **levels, size_t *count);
57 void regulator_get_range(const struct rdev *rdev, uint16_t *min_mv, uint16_t *max_mv);
58 int regulator_set_flag(struct rdev *rdev, uint16_t flag);
59 
60 /*
61  * Driver Interface
62  */
63 
64 /* set_state() arguments */
65 #define STATE_DISABLE		false
66 #define STATE_ENABLE		true
67 
68 struct regul_description {
69 	const char *node_name;
70 	const struct regul_ops *ops;
71 	const void *driver_data;
72 	const char *supply_name;
73 	const uint32_t enable_ramp_delay;
74 };
75 
76 struct regul_ops {
77 	int (*set_state)(const struct regul_description *desc, bool state);
78 	int (*get_state)(const struct regul_description *desc);
79 	int (*set_voltage)(const struct regul_description *desc, uint16_t mv);
80 	int (*get_voltage)(const struct regul_description *desc);
81 	int (*list_voltages)(const struct regul_description *desc,
82 			     const uint16_t **levels, size_t *count);
83 	int (*set_flag)(const struct regul_description *desc, uint16_t flag);
84 	void (*lock)(const struct regul_description *desc);
85 	void (*unlock)(const struct regul_description *desc);
86 };
87 
88 int regulator_register(const struct regul_description *desc, int node);
89 
90 /*
91  * Internal regulator structure
92  * The structure is internal to the core, and the content should not be used
93  * by a consumer nor a driver.
94  */
95 struct rdev {
96 	const struct regul_description *desc;
97 
98 	int32_t phandle;
99 
100 	uint16_t min_mv;
101 	uint16_t max_mv;
102 
103 	uint16_t flags;
104 
105 	uint32_t enable_ramp_delay;
106 };
107 
108 #endif /* REGULATOR_H */
109