1 /*
2  * Copyright (c) 2019-2020 Peter Bigot Consulting, LLC
3  * Copyright (c) 2021 NXP
4  * Copyright (c) 2022 Nordic Semiconductor ASA
5  * Copyright (c) 2023 EPAM Systems
6  * Copyright (c) 2023 Meta Platforms
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #ifndef ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
11 #define ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
12 
13 /**
14  * @brief Regulator Interface
15  * @defgroup regulator_interface Regulator Interface
16  * @since 2.4
17  * @version 0.1.0
18  * @ingroup io_interfaces
19  * @{
20  */
21 
22 #include <errno.h>
23 #include <stdint.h>
24 
25 #include <zephyr/device.h>
26 #include <zephyr/devicetree.h>
27 #ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
28 #include <zephyr/kernel.h>
29 #endif
30 #include <zephyr/sys/util_macro.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /** Opaque type to store regulator DVS states */
37 typedef uint8_t regulator_dvs_state_t;
38 
39 /** Opaque type to store regulator modes */
40 typedef uint8_t regulator_mode_t;
41 
42 /** Opaque bit map for regulator error flags (see @ref REGULATOR_ERRORS) */
43 typedef uint8_t regulator_error_flags_t;
44 
45 /**
46  * @name Regulator error flags.
47  * @anchor REGULATOR_ERRORS
48  * @{
49  */
50 
51 /** Voltage is too high. */
52 #define REGULATOR_ERROR_OVER_VOLTAGE BIT(0)
53 /** Current is too high. */
54 #define REGULATOR_ERROR_OVER_CURRENT BIT(1)
55 /** Temperature is too high. */
56 #define REGULATOR_ERROR_OVER_TEMP    BIT(2)
57 
58 /** @} */
59 
60 /** @cond INTERNAL_HIDDEN */
61 
62 typedef int (*regulator_dvs_state_set_t)(const struct device *dev,
63 					 regulator_dvs_state_t state);
64 
65 typedef int (*regulator_ship_mode_t)(const struct device *dev);
66 
67 /** @brief Driver-specific API functions to support parent regulator control. */
68 __subsystem struct regulator_parent_driver_api {
69 	regulator_dvs_state_set_t dvs_state_set;
70 	regulator_ship_mode_t ship_mode;
71 };
72 
73 typedef int (*regulator_enable_t)(const struct device *dev);
74 typedef int (*regulator_disable_t)(const struct device *dev);
75 typedef unsigned int (*regulator_count_voltages_t)(const struct device *dev);
76 typedef int (*regulator_list_voltage_t)(const struct device *dev,
77 					unsigned int idx, int32_t *volt_uv);
78 typedef int (*regulator_set_voltage_t)(const struct device *dev, int32_t min_uv,
79 				       int32_t max_uv);
80 typedef int (*regulator_get_voltage_t)(const struct device *dev,
81 				       int32_t *volt_uv);
82 typedef unsigned int (*regulator_count_current_limits_t)(const struct device *dev);
83 typedef int (*regulator_list_current_limit_t)(const struct device *dev,
84 					      unsigned int idx, int32_t *current_ua);
85 typedef int (*regulator_set_current_limit_t)(const struct device *dev,
86 					     int32_t min_ua, int32_t max_ua);
87 typedef int (*regulator_get_current_limit_t)(const struct device *dev,
88 					     int32_t *curr_ua);
89 typedef int (*regulator_set_mode_t)(const struct device *dev,
90 				    regulator_mode_t mode);
91 typedef int (*regulator_get_mode_t)(const struct device *dev,
92 				    regulator_mode_t *mode);
93 typedef int (*regulator_set_active_discharge_t)(const struct device *dev,
94 				    bool active_discharge);
95 typedef int (*regulator_get_active_discharge_t)(const struct device *dev,
96 				    bool *active_discharge);
97 typedef int (*regulator_get_error_flags_t)(
98 	const struct device *dev, regulator_error_flags_t *flags);
99 
100 /** @brief Driver-specific API functions to support regulator control. */
101 __subsystem struct regulator_driver_api {
102 	regulator_enable_t enable;
103 	regulator_disable_t disable;
104 	regulator_count_voltages_t count_voltages;
105 	regulator_list_voltage_t list_voltage;
106 	regulator_set_voltage_t set_voltage;
107 	regulator_get_voltage_t get_voltage;
108 	regulator_count_current_limits_t count_current_limits;
109 	regulator_list_current_limit_t list_current_limit;
110 	regulator_set_current_limit_t set_current_limit;
111 	regulator_get_current_limit_t get_current_limit;
112 	regulator_set_mode_t set_mode;
113 	regulator_get_mode_t get_mode;
114 	regulator_set_active_discharge_t set_active_discharge;
115 	regulator_get_active_discharge_t get_active_discharge;
116 	regulator_get_error_flags_t get_error_flags;
117 };
118 
119 /**
120  * @name Regulator flags
121  * @anchor REGULATOR_FLAGS
122  * @{
123  */
124 /** Indicates regulator must stay always ON */
125 #define REGULATOR_ALWAYS_ON	           BIT(0)
126 /** Indicates regulator must be initialized ON */
127 #define REGULATOR_BOOT_ON	           BIT(1)
128 /** Indicates if regulator must be enabled when initialized */
129 #define REGULATOR_INIT_ENABLED             (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
130 /** Regulator active discharge state mask */
131 #define REGULATOR_ACTIVE_DISCHARGE_MASK    GENMASK(3, 2)
132 /** Regulator active discharge state flag position*/
133 #define REGULATOR_ACTIVE_DISCHARGE_POS     2
134 /** Disable regulator active discharge */
135 #define REGULATOR_ACTIVE_DISCHARGE_DISABLE 0
136 /** Enable regulator active discharge */
137 #define REGULATOR_ACTIVE_DISCHARGE_ENABLE  1
138 /** Leave regulator active discharge state as default */
139 #define REGULATOR_ACTIVE_DISCHARGE_DEFAULT 2
140 /** Regulator active discharge set bits */
141 #define REGULATOR_ACTIVE_DISCHARGE_SET_BITS(x) \
142 	(((x) << REGULATOR_ACTIVE_DISCHARGE_POS) & REGULATOR_ACTIVE_DISCHARGE_MASK)
143 /** Regulator active discharge get bits */
144 #define REGULATOR_ACTIVE_DISCHARGE_GET_BITS(x) \
145 	(((x) & REGULATOR_ACTIVE_DISCHARGE_MASK) >> REGULATOR_ACTIVE_DISCHARGE_POS)
146 /** Indicates regulator must be initialized OFF */
147 #define REGULATOR_BOOT_OFF BIT(4)
148 
149 /** @} */
150 
151 /** Indicates initial mode is unknown/not specified */
152 #define REGULATOR_INITIAL_MODE_UNKNOWN UINT8_MAX
153 
154 /**
155  * @brief Common regulator config.
156  *
157  * This structure **must** be placed first in the driver's config structure.
158  */
159 struct regulator_common_config {
160 	/** Minimum allowed voltage, in microvolts. */
161 	int32_t min_uv;
162 	/** Maximum allowed voltage, in microvolts. */
163 	int32_t max_uv;
164 	/** Initial voltage, in microvolts. */
165 	int32_t init_uv;
166 	/** Minimum allowed current, in microamps. */
167 	int32_t min_ua;
168 	/** Maximum allowed current, in microamps. */
169 	int32_t max_ua;
170 	/** Initial current, in microamps. */
171 	int32_t init_ua;
172 	/** Startup delay, in microseconds. */
173 	uint32_t startup_delay_us;
174 	/** Off to on delay, in microseconds. */
175 	uint32_t off_on_delay_us;
176 	/** Allowed modes */
177 	const regulator_mode_t *allowed_modes;
178 	/** Number of allowed modes */
179 	uint8_t allowed_modes_cnt;
180 	/** Regulator initial mode */
181 	regulator_mode_t initial_mode;
182 	/** Flags (@reg REGULATOR_FLAGS). */
183 	uint8_t flags;
184 };
185 
186 /**
187  * @brief Initialize common driver config from devicetree.
188  *
189  * @param node_id Node identifier.
190  */
191 #define REGULATOR_DT_COMMON_CONFIG_INIT(node_id)                               \
192 	{                                                                      \
193 		.min_uv = DT_PROP_OR(node_id, regulator_min_microvolt,         \
194 				     INT32_MIN),                               \
195 		.max_uv = DT_PROP_OR(node_id, regulator_max_microvolt,         \
196 				     INT32_MAX),                               \
197 		.init_uv = DT_PROP_OR(node_id, regulator_init_microvolt,       \
198 				      INT32_MIN),			       \
199 		.min_ua = DT_PROP_OR(node_id, regulator_min_microamp,          \
200 				     INT32_MIN),                               \
201 		.max_ua = DT_PROP_OR(node_id, regulator_max_microamp,          \
202 				     INT32_MAX),                               \
203 		.init_ua = DT_PROP_OR(node_id, regulator_init_microamp,       \
204 				      INT32_MIN),			       \
205 		.startup_delay_us = DT_PROP_OR(node_id, startup_delay_us, 0),  \
206 		.off_on_delay_us = DT_PROP_OR(node_id, off_on_delay_us, 0),    \
207 		.allowed_modes = (const regulator_mode_t [])                   \
208 			DT_PROP_OR(node_id, regulator_allowed_modes, {}),      \
209 		.allowed_modes_cnt =                                           \
210 			DT_PROP_LEN_OR(node_id, regulator_allowed_modes, 0),   \
211 		.initial_mode = DT_PROP_OR(node_id, regulator_initial_mode,    \
212 					   REGULATOR_INITIAL_MODE_UNKNOWN),    \
213 		.flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) *      \
214 			   REGULATOR_ALWAYS_ON) |                              \
215 			  (DT_PROP_OR(node_id, regulator_boot_on, 0U) *        \
216 			   REGULATOR_BOOT_ON) |                                \
217 			  (REGULATOR_ACTIVE_DISCHARGE_SET_BITS(                \
218 			   DT_PROP_OR(node_id, regulator_active_discharge,     \
219 			   REGULATOR_ACTIVE_DISCHARGE_DEFAULT))) |             \
220 			  (DT_PROP_OR(node_id, regulator_boot_off, 0U) *       \
221 			   REGULATOR_BOOT_OFF)),                               \
222 	}
223 
224 /**
225  * @brief Initialize common driver config from devicetree instance.
226  *
227  * @param inst Instance.
228  */
229 #define REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst)                             \
230 	REGULATOR_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
231 
232 /**
233  * @brief Common regulator data.
234  *
235  * This structure **must** be placed first in the driver's data structure.
236  */
237 struct regulator_common_data {
238 #if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
239 	/** Lock (only if @kconfig{CONFIG_REGULATOR_THREAD_SAFE_REFCNT}=y) */
240 	struct k_mutex lock;
241 #endif
242 	/** Reference count */
243 	int refcnt;
244 };
245 
246 /**
247  * @brief Initialize common regulator data.
248  *
249  * This function **must** be called when driver is initialized.
250  *
251  * @param dev Regulator device instance.
252  */
253 void regulator_common_data_init(const struct device *dev);
254 
255 /**
256  * @brief Common function to initialize the regulator at init time.
257  *
258  * This function needs to be called after drivers initialize the regulator. It
259  * will:
260  *
261  * - Automatically enable the regulator if it is set to `regulator-boot-on`
262  *   or `regulator-always-on` and increase its usage count.
263  * - Automatically disable the regulator if it is set to `regulator-boot-off`.
264  * - Configure the regulator mode if `regulator-initial-mode` is set.
265  * - Ensure regulator voltage is set to a valid range.
266  *
267  * Regulators that are enabled by default in hardware, must set @p is_enabled to
268  * `true`.
269  *
270  * @param dev Regulator device instance
271  * @param is_enabled Indicate if the regulator is enabled by default in
272  * hardware.
273  *
274  * @retval 0 If enabled successfully.
275  * @retval -errno Negative errno in case of failure.
276  */
277 int regulator_common_init(const struct device *dev, bool is_enabled);
278 
279 /**
280  * @brief Check if regulator is expected to be enabled at init time.
281  *
282  * @param dev Regulator device instance
283  * @return true If regulator needs to be enabled at init time.
284  * @return false If regulator does not need to be enabled at init time.
285  */
regulator_common_is_init_enabled(const struct device * dev)286 static inline bool regulator_common_is_init_enabled(const struct device *dev)
287 {
288 	const struct regulator_common_config *config =
289 		(const struct regulator_common_config *)dev->config;
290 
291 	return (config->flags & REGULATOR_INIT_ENABLED) != 0U;
292 }
293 
294 /**
295  * @brief Get minimum supported voltage.
296  *
297  * @param dev Regulator device instance.
298  * @param min_uv Where minimum voltage will be stored, in microvolts.
299  *
300  * @retval 0 If successful
301  * @retval -ENOENT If minimum voltage is not specified.
302  */
regulator_common_get_min_voltage(const struct device * dev,int32_t * min_uv)303 static inline int regulator_common_get_min_voltage(const struct device *dev, int32_t *min_uv)
304 {
305 	const struct regulator_common_config *config =
306 		(const struct regulator_common_config *)dev->config;
307 
308 	if (config->min_uv == INT32_MIN) {
309 		return -ENOENT;
310 	}
311 
312 	*min_uv = config->min_uv;
313 	return 0;
314 }
315 
316 /** @endcond */
317 
318 /**
319  * @brief Regulator Parent Interface
320  * @defgroup regulator_parent_interface Regulator Parent Interface
321  * @{
322  */
323 
324 /**
325  * @brief Set a DVS state.
326  *
327  * Some PMICs feature DVS (Dynamic Voltage Scaling) by allowing to program the
328  * voltage level for multiple states. Such states may be automatically changed
329  * by hardware using GPIO pins. Certain MCUs even allow to automatically
330  * configure specific output pins when entering low-power modes so that PMIC
331  * state is changed without software intervention. This API can be used when
332  * state needs to be changed by software.
333  *
334  * @param dev Parent regulator device instance.
335  * @param state DVS state (vendor specific identifier).
336  *
337  * @retval 0 If successful.
338  * @retval -ENOTSUP If given state is not supported.
339  * @retval -EPERM If state can't be changed by software.
340  * @retval -ENOSYS If function is not implemented.
341  * @retval -errno In case of any other error.
342  */
regulator_parent_dvs_state_set(const struct device * dev,regulator_dvs_state_t state)343 static inline int regulator_parent_dvs_state_set(const struct device *dev,
344 						 regulator_dvs_state_t state)
345 {
346 	const struct regulator_parent_driver_api *api =
347 		(const struct regulator_parent_driver_api *)dev->api;
348 
349 	if (api->dvs_state_set == NULL) {
350 		return -ENOSYS;
351 	}
352 
353 	return api->dvs_state_set(dev, state);
354 }
355 
356 /**
357  * @brief Enter ship mode.
358  *
359  * Some PMICs feature a ship mode, which allows the system to save power.
360  * Exit from low power is normally by pin transition.
361  *
362  * This API can be used when ship mode needs to be entered.
363  *
364  * @param dev Parent regulator device instance.
365  *
366  * @retval 0 If successful.
367  * @retval -ENOSYS If function is not implemented.
368  * @retval -errno In case of any other error.
369  */
regulator_parent_ship_mode(const struct device * dev)370 static inline int regulator_parent_ship_mode(const struct device *dev)
371 {
372 	const struct regulator_parent_driver_api *api =
373 		(const struct regulator_parent_driver_api *)dev->api;
374 
375 	if (api->ship_mode == NULL) {
376 		return -ENOSYS;
377 	}
378 
379 	return api->ship_mode(dev);
380 }
381 
382 /** @} */
383 
384 /**
385  * @brief Enable a regulator.
386  *
387  * Reference-counted request that a regulator be turned on. A regulator is
388  * considered "on" when it has reached a stable/usable state. Regulators that
389  * are always on, or configured in devicetree with `regulator-always-on` will
390  * always stay enabled, and so this function will always succeed.
391  *
392  * @param dev Regulator device instance
393  *
394  * @retval 0 If regulator has been successfully enabled.
395  * @retval -errno Negative errno in case of failure.
396  * @retval -ENOTSUP If regulator enablement can not be controlled.
397  */
398 int regulator_enable(const struct device *dev);
399 
400 /**
401  * @brief Check if a regulator is enabled.
402  *
403  * @param dev Regulator device instance.
404  *
405  * @retval true If regulator is enabled.
406  * @retval false If regulator is disabled.
407  */
408 bool regulator_is_enabled(const struct device *dev);
409 
410 /**
411  * @brief Disable a regulator.
412  *
413  * Release a regulator after a previous regulator_enable() completed
414  * successfully. Regulators that are always on, or configured in devicetree with
415  * `regulator-always-on` will always stay enabled, and so this function will
416  * always succeed.
417  *
418  * This must be invoked at most once for each successful regulator_enable().
419  *
420  * @param dev Regulator device instance.
421  *
422  * @retval 0 If regulator has been successfully disabled.
423  * @retval -errno Negative errno in case of failure.
424  * @retval -ENOTSUP If regulator disablement can not be controlled.
425  */
426 int regulator_disable(const struct device *dev);
427 
428 /**
429  * @brief Obtain the number of supported voltage levels.
430  *
431  * Each voltage level supported by a regulator gets an index, starting from
432  * zero. The total number of supported voltage levels can be used together with
433  * regulator_list_voltage() to list all supported voltage levels.
434  *
435  * @param dev Regulator device instance.
436  *
437  * @return Number of supported voltages.
438  */
regulator_count_voltages(const struct device * dev)439 static inline unsigned int regulator_count_voltages(const struct device *dev)
440 {
441 	const struct regulator_driver_api *api =
442 		(const struct regulator_driver_api *)dev->api;
443 
444 	if (api->count_voltages == NULL) {
445 		return 0U;
446 	}
447 
448 	return api->count_voltages(dev);
449 }
450 
451 /**
452  * @brief Obtain the value of a voltage given an index.
453  *
454  * Each voltage level supported by a regulator gets an index, starting from
455  * zero. Together with regulator_count_voltages(), this function can be used
456  * to iterate over all supported voltages.
457  *
458  * @param dev Regulator device instance.
459  * @param idx Voltage index.
460  * @param[out] volt_uv Where voltage for the given @p index will be stored, in
461  * microvolts.
462  *
463  * @retval 0 If @p index corresponds to a supported voltage.
464  * @retval -EINVAL If @p index does not correspond to a supported voltage.
465  */
regulator_list_voltage(const struct device * dev,unsigned int idx,int32_t * volt_uv)466 static inline int regulator_list_voltage(const struct device *dev,
467 					 unsigned int idx, int32_t *volt_uv)
468 {
469 	const struct regulator_driver_api *api =
470 		(const struct regulator_driver_api *)dev->api;
471 
472 	if (api->list_voltage == NULL) {
473 		return -EINVAL;
474 	}
475 
476 	return api->list_voltage(dev, idx, volt_uv);
477 }
478 
479 /**
480  * @brief Check if a voltage within a window is supported.
481  *
482  * @param dev Regulator device instance.
483  * @param min_uv Minimum voltage in microvolts.
484  * @param max_uv maximum voltage in microvolts.
485  *
486  * @retval true If voltage is supported.
487  * @retval false If voltage is not supported.
488  */
489 bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
490 				    int32_t max_uv);
491 
492 /**
493  * @brief Set the output voltage.
494  *
495  * The output voltage will be configured to the closest supported output
496  * voltage. regulator_get_voltage() can be used to obtain the actual configured
497  * voltage. The voltage will be applied to the active or selected mode. Output
498  * voltage may be limited using `regulator-min-microvolt` and/or
499  * `regulator-max-microvolt` in devicetree.
500  *
501  * @param dev Regulator device instance.
502  * @param min_uv Minimum acceptable voltage in microvolts.
503  * @param max_uv Maximum acceptable voltage in microvolts.
504  *
505  * @retval 0 If successful.
506  * @retval -EINVAL If the given voltage window is not valid.
507  * @retval -ENOSYS If function is not implemented.
508  * @retval -errno In case of any other error.
509  */
510 int regulator_set_voltage(const struct device *dev, int32_t min_uv,
511 			  int32_t max_uv);
512 
513 /**
514  * @brief Obtain output voltage.
515  *
516  * @param dev Regulator device instance.
517  * @param[out] volt_uv Where configured output voltage will be stored.
518  *
519  * @retval 0 If successful
520  * @retval -ENOSYS If function is not implemented.
521  * @retval -errno In case of any other error.
522  */
regulator_get_voltage(const struct device * dev,int32_t * volt_uv)523 static inline int regulator_get_voltage(const struct device *dev,
524 					int32_t *volt_uv)
525 {
526 	const struct regulator_driver_api *api =
527 		(const struct regulator_driver_api *)dev->api;
528 
529 	if (api->get_voltage == NULL) {
530 		return -ENOSYS;
531 	}
532 
533 	return api->get_voltage(dev, volt_uv);
534 }
535 
536 /**
537  * @brief Obtain the number of supported current limit levels.
538  *
539  * Each current limit level supported by a regulator gets an index, starting from
540  * zero. The total number of supported current limit levels can be used together with
541  * regulator_list_current_limit() to list all supported current limit levels.
542  *
543  * @param dev Regulator device instance.
544  *
545  * @return Number of supported current limits.
546  */
regulator_count_current_limits(const struct device * dev)547 static inline unsigned int regulator_count_current_limits(const struct device *dev)
548 {
549 	const struct regulator_driver_api *api =
550 		(const struct regulator_driver_api *)dev->api;
551 
552 	if (api->count_current_limits == NULL) {
553 		return 0U;
554 	}
555 
556 	return api->count_current_limits(dev);
557 }
558 
559 /**
560  * @brief Obtain the value of a current limit given an index.
561  *
562  * Each current limit level supported by a regulator gets an index, starting from
563  * zero. Together with regulator_count_current_limits(), this function can be used
564  * to iterate over all supported current limits.
565  *
566  * @param dev Regulator device instance.
567  * @param idx Current index.
568  * @param[out] current_ua Where current for the given @p index will be stored, in
569  * microamps.
570  *
571  * @retval 0 If @p index corresponds to a supported current limit.
572  * @retval -EINVAL If @p index does not correspond to a supported current limit.
573  */
regulator_list_current_limit(const struct device * dev,unsigned int idx,int32_t * current_ua)574 static inline int regulator_list_current_limit(const struct device *dev,
575 					       unsigned int idx, int32_t *current_ua)
576 {
577 	const struct regulator_driver_api *api =
578 		(const struct regulator_driver_api *)dev->api;
579 
580 	if (api->list_current_limit == NULL) {
581 		return -EINVAL;
582 	}
583 
584 	return api->list_current_limit(dev, idx, current_ua);
585 }
586 
587 /**
588  * @brief Set output current limit.
589  *
590  * The output current limit will be configured to the closest supported output
591  * current limit. regulator_get_current_limit() can be used to obtain the actual
592  * configured current limit. Current may be limited using `current-min-microamp`
593  * and/or `current-max-microamp` in Devicetree.
594  *
595  * @param dev Regulator device instance.
596  * @param min_ua Minimum acceptable current limit in microamps.
597  * @param max_ua Maximum acceptable current limit in microamps.
598  *
599  * @retval 0 If successful.
600  * @retval -EINVAL If the given current limit window is not valid.
601  * @retval -ENOSYS If function is not implemented.
602  * @retval -errno In case of any other error.
603  */
604 int regulator_set_current_limit(const struct device *dev, int32_t min_ua,
605 				int32_t max_ua);
606 
607 /**
608  * @brief Get output current limit.
609  *
610  * @param dev Regulator device instance.
611  * @param[out] curr_ua Where output current limit will be stored.
612  *
613  * @retval 0 If successful.
614  * @retval -ENOSYS If function is not implemented.
615  * @retval -errno In case of any other error.
616  */
regulator_get_current_limit(const struct device * dev,int32_t * curr_ua)617 static inline int regulator_get_current_limit(const struct device *dev,
618 					      int32_t *curr_ua)
619 {
620 	const struct regulator_driver_api *api =
621 		(const struct regulator_driver_api *)dev->api;
622 
623 	if (api->get_current_limit == NULL) {
624 		return -ENOSYS;
625 	}
626 
627 	return api->get_current_limit(dev, curr_ua);
628 }
629 
630 /**
631  * @brief Set mode.
632  *
633  * Regulators can support multiple modes in order to permit different voltage
634  * configuration or better power savings. This API will apply a mode for
635  * the regulator. Allowed modes may be limited using `regulator-allowed-modes`
636  * devicetree property.
637  *
638  * @param dev Regulator device instance.
639  * @param mode Mode to select for this regulator.
640  *
641  * @retval 0 If successful.
642  * @retval -ENOTSUP If mode is not supported.
643  * @retval -ENOSYS If function is not implemented.
644  * @retval -errno In case of any other error.
645  */
646 int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
647 
648 /**
649  * @brief Get mode.
650  *
651  * @param dev Regulator device instance.
652  * @param[out] mode Where mode will be stored.
653  *
654  * @retval 0 If successful.
655  * @retval -ENOSYS If function is not implemented.
656  * @retval -errno In case of any other error.
657  */
regulator_get_mode(const struct device * dev,regulator_mode_t * mode)658 static inline int regulator_get_mode(const struct device *dev,
659 				     regulator_mode_t *mode)
660 {
661 	const struct regulator_driver_api *api =
662 		(const struct regulator_driver_api *)dev->api;
663 
664 	if (api->get_mode == NULL) {
665 		return -ENOSYS;
666 	}
667 
668 	return api->get_mode(dev, mode);
669 }
670 
671 /**
672  * @brief Set active discharge setting.
673  *
674  * @param dev Regulator device instance.
675  * @param active_discharge Active discharge enable or disable.
676  *
677  * @retval 0 If successful.
678  * @retval -ENOSYS If function is not implemented.
679  * @retval -errno In case of any other error.
680  */
regulator_set_active_discharge(const struct device * dev,bool active_discharge)681 static inline int regulator_set_active_discharge(const struct device *dev,
682 				     bool active_discharge)
683 {
684 	const struct regulator_driver_api *api =
685 		(const struct regulator_driver_api *)dev->api;
686 
687 	if (api->set_active_discharge == NULL) {
688 		return -ENOSYS;
689 	}
690 
691 	return api->set_active_discharge(dev, active_discharge);
692 }
693 
694 /**
695  * @brief Get active discharge setting.
696  *
697  * @param dev Regulator device instance.
698  * @param[out] active_discharge Where active discharge will be stored.
699  *
700  * @retval 0 If successful.
701  * @retval -ENOSYS If function is not implemented.
702  * @retval -errno In case of any other error.
703  */
regulator_get_active_discharge(const struct device * dev,bool * active_discharge)704 static inline int regulator_get_active_discharge(const struct device *dev,
705 				     bool *active_discharge)
706 {
707 	const struct regulator_driver_api *api =
708 		(const struct regulator_driver_api *)dev->api;
709 
710 	if (api->get_active_discharge == NULL) {
711 		return -ENOSYS;
712 	}
713 
714 	return api->get_active_discharge(dev, active_discharge);
715 }
716 
717 /**
718  * @brief Get active error flags.
719  *
720  * @param dev Regulator device instance.
721  * @param[out] flags Where error flags will be stored.
722  *
723  * @retval 0 If successful.
724  * @retval -ENOSYS If function is not implemented.
725  * @retval -errno In case of any other error.
726  */
regulator_get_error_flags(const struct device * dev,regulator_error_flags_t * flags)727 static inline int regulator_get_error_flags(const struct device *dev,
728 					    regulator_error_flags_t *flags)
729 {
730 	const struct regulator_driver_api *api =
731 		(const struct regulator_driver_api *)dev->api;
732 
733 	if (api->get_error_flags == NULL) {
734 		return -ENOSYS;
735 	}
736 
737 	return api->get_error_flags(dev, flags);
738 }
739 
740 #ifdef __cplusplus
741 }
742 #endif
743 
744 /** @} */
745 
746 #endif /* ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_ */
747