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.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 #define REGULATOR_FLAGS_MASK (REGULATOR_ALWAYS_ON | REGULATOR_PULL_DOWN | \ 31 REGULATOR_BOOT_ON) 32 33 struct regulator_ops; 34 35 /* 36 * struct regu_dt_desc - Regulator description passed to regulator_dt_register() 37 * @priv: Regulator driver private data 38 * @name: Regulator string name for debug purpose 39 * @supply_name: Regulator supply name for node property *-supply or NULL 40 * @ops: Operation handlers for the regulator 41 * @regulator: Pointer to preallocated regulator or NULL if none 42 */ 43 struct regu_dt_desc { 44 void *priv; 45 const char *name; 46 const char *supply_name; 47 const struct regulator_ops *ops; 48 struct regulator *regulator; 49 }; 50 51 /* 52 * Defines the format of struct voltages::entries 53 * 54 * If regulator_voltages::type is VOLTAGE_TYPE_FULL_LIST, then 55 * regulator_voltages@entries stores regulator_voltages::num_levels cells, 56 * listing supported voltage levels in uV from lowest to highest value. 57 * 58 * If regulator_voltages::type is VOLTAGE_TYPE_INCREMENT, then 59 * regulator_voltages::entries stores 3 cells: min level, max level and 60 * level increment step, all in uV. When so, regulator_voltages::num_levels 61 * is meaningless. 62 */ 63 enum voltage_type { 64 VOLTAGE_TYPE_INVALID = 0, 65 VOLTAGE_TYPE_FULL_LIST, /* extensive list in uV */ 66 VOLTAGE_TYPE_INCREMENT /* min, max, increment (in uV) */ 67 }; 68 69 /* 70 * struct regulator_voltages_desc - Voltage levels description 71 * @type: Type of level description 72 * @num_levels: Number of voltage levels when @type is VOLTAGE_TYPE_FULL_LIST 73 * 74 */ 75 struct regulator_voltages_desc { 76 enum voltage_type type; 77 size_t num_levels; 78 }; 79 80 /* 81 * struct regulator - A regulator instance 82 * @ops: Operation handlers for the regulator 83 * @supply: Regulator supply reference or NULL if none 84 * @priv: Regulator driver private data 85 * @name: Regulator string name for debug purpose or NULL 86 * @min_uv: Min possible voltage level in microvolt (uV) 87 * @max_uv: Max possible voltage level in microvolt (uV) 88 * @cur_uv: Current voltage level in microvolt (uV) 89 * @flags: REGULATOR_* property flags 90 * @refcount: Regulator enable request reference counter 91 * @lock: Mutex for concurrent access protection 92 * @voltages_fallback: Default supported voltage range description 93 * @link: Link in initialized regulator list 94 */ 95 struct regulator { 96 /* Fields initialized by caller of regulator_register() */ 97 const struct regulator_ops *ops; 98 struct regulator *supply; 99 void *priv; 100 char *name; 101 int min_uv; 102 int max_uv; 103 /* Fields internal to regulator framework */ 104 int cur_uv; 105 unsigned int flags; 106 unsigned int refcount; 107 struct mutex lock; /* Concurrent access protection */ 108 struct voltages_fallback { 109 struct regulator_voltages_desc desc; 110 int levels[3]; 111 } voltages_fallback; 112 size_t levels_count_fallback; 113 SLIST_ENTRY(regulator) link; 114 }; 115 116 /* 117 * struct regulator_ops - Regulator operation handlers 118 * 119 * @set_state: Enable or disable a regulator 120 * @get_state: Get regulator effective state 121 * @set_voltage: Set voltage level in microvolt (uV) 122 * @get_voltage: Get current voltage in microvolt (uV) 123 * @supported_voltages: Get supported levels description 124 * @supplied_init: Optional, finalize initialization once supply is ready 125 */ 126 struct regulator_ops { 127 TEE_Result (*set_state)(struct regulator *r, bool enabled); 128 TEE_Result (*get_state)(struct regulator *r, bool *enabled); 129 TEE_Result (*set_voltage)(struct regulator *r, int level_uv); 130 TEE_Result (*get_voltage)(struct regulator *r, int *level_uv); 131 TEE_Result (*supported_voltages)(struct regulator *r, 132 struct regulator_voltages_desc **desc, 133 const int **levels); 134 TEE_Result (*supplied_init)(struct regulator *r, const void *fdt, 135 int node); 136 }; 137 138 #ifdef CFG_DRIVERS_REGULATOR 139 /* 140 * regulator_enable() - Enable regulator 141 * @regulator: Regulator reference 142 */ 143 TEE_Result regulator_enable(struct regulator *regulator); 144 145 /* 146 * regulator_disable() - Disable regulator 147 * @regulator: Regulator reference 148 */ 149 TEE_Result regulator_disable(struct regulator *regulator); 150 151 /* 152 * regulator_is_enabled() - Return whether or not regulator is currently enabled 153 * despite its refcount value. 154 * @regulator: Regulator reference 155 */ 156 bool regulator_is_enabled(struct regulator *regulator); 157 158 /* 159 * regulator_set_voltage() - Set regulator to target level in microvolt 160 * @regulator: Regulator reference 161 * @level_uv: Level in microvolt 162 */ 163 TEE_Result regulator_set_voltage(struct regulator *regulator, int level_uv); 164 165 /* 166 * regulator_register() - Register and initialize a regulator 167 * @regulator: Regulator reference 168 */ 169 TEE_Result regulator_register(struct regulator *regulator); 170 171 /* Print registered regulators and their state to the output console */ 172 void regulator_print_state(const char *message); 173 #else 174 static inline TEE_Result regulator_enable(struct regulator *regulator __unused) 175 { 176 return TEE_ERROR_NOT_SUPPORTED; 177 } 178 179 static inline TEE_Result regulator_disable(struct regulator *regulator __unused) 180 { 181 return TEE_ERROR_NOT_SUPPORTED; 182 } 183 184 static inline bool regulator_is_enabled(struct regulator *regulator __unused) 185 { 186 return false; 187 } 188 189 static inline TEE_Result regulator_set_voltage(struct regulator *regul __unused, 190 int level_mv __unused) 191 { 192 return TEE_ERROR_NOT_SUPPORTED; 193 } 194 195 static inline TEE_Result regulator_init(struct regulator *regulator __unused) 196 { 197 return TEE_ERROR_NOT_SUPPORTED; 198 } 199 200 static inline void regulator_print_state(const char *message __unused) 201 { 202 } 203 #endif /*CFG_DRIVERS_REGULATOR*/ 204 205 #if defined(CFG_DRIVERS_REGULATOR) && defined(CFG_DT) 206 /* 207 * regulator_dt_get_supply() - Get a regulator supply from name and DT node 208 * @fdt: FDT to work on 209 * @node: DT node of the regulator consumer 210 * @supply_name: Name of the supply in DT property xxx-supply 211 * @regulator: Output regulator upon success 212 * 213 * Upon success, this function provides the pointer to regulator 214 * defined by DT binding property @name-supply phandle reference. 215 * 216 * This function returns TEE_ERROR_DEFER_DRIVER_INIT if supply exists but is 217 * not yet initialized. 218 */ 219 TEE_Result regulator_dt_get_supply(const void *fdt, int node, 220 const char *supply_name, 221 struct regulator **regulator); 222 223 /* 224 * regulator_dt_register() - Register a regulator to related to a DT node 225 * @fdt: FDT to work on 226 * @node: DT node of the regulator exposed by regulator driver 227 * @provider_node: Node where xxx-supply property is found or -1 if no supply. 228 * @desc: Description of the regulator to register 229 * 230 * This function registers and initializes a regulator instance once its supply 231 * if found, if any. Regulators registered with this function can be found by 232 * their consumer drivers using API function regulator_dt_get_supply() or like. 233 * 234 * Return TEE_SUCCESS in case of success 235 * Return TEE_ERROR_OUT_OF_MEMORY if failed on memory allocation 236 * Return any other TEE_Result compliant code in case of error 237 */ 238 TEE_Result regulator_dt_register(const void *fdt, int node, int provider_node, 239 const struct regu_dt_desc *desc); 240 #else 241 static inline TEE_Result regulator_dt_get_supply(const void *fdt __unused, 242 int node __unused, 243 const char *supply __unused, 244 struct regulator **r __unused) 245 { 246 return TEE_ERROR_NOT_SUPPORTED; 247 } 248 249 static inline TEE_Result 250 regulator_dt_register(const void *fdt __unused, int node __unused, 251 int provider_node __unused, 252 const struct regu_dt_desc *d __unused) 253 { 254 return TEE_ERROR_NOT_SUPPORTED; 255 } 256 #endif /* CFG_DRIVERS_REGULATOR && CFG_DT */ 257 258 /* 259 * regulator_name() - Return regulator name or NULL 260 * @regulator: Regulator reference 261 */ 262 static inline const char *regulator_name(struct regulator *regulator) 263 { 264 return regulator->name; 265 } 266 267 /* 268 * regulator_is_always_on() - Return the state of REGULATOR_ALWAYS_ON flag 269 * @regulator: Regulator reference 270 */ 271 static inline bool regulator_is_always_on(struct regulator *regulator) 272 { 273 return regulator->flags & REGULATOR_ALWAYS_ON; 274 } 275 276 /* 277 * regulator_set_min_voltage() - Set regulator to its min level 278 * @regulator: Regulator reference 279 */ 280 static inline TEE_Result regulator_set_min_voltage(struct regulator *regulator) 281 { 282 return regulator_set_voltage(regulator, regulator->min_uv); 283 } 284 285 /* 286 * regulator_get_voltage() - Get regulator current level in microvolt 287 * @regulator: Regulator reference 288 */ 289 static inline int regulator_get_voltage(struct regulator *regulator) 290 { 291 return regulator->cur_uv; 292 } 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 debug 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