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 (@ref 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 /**
317 * @brief Get maximum supported voltage.
318 *
319 * @param dev Regulator device instance.
320 * @param max_uv Where maximum voltage will be stored, in microvolts.
321 *
322 * @retval 0 If successful
323 * @retval -ENOENT If maximum voltage is not specified.
324 */
regulator_common_get_max_voltage(const struct device * dev,int32_t * max_uv)325 static inline int regulator_common_get_max_voltage(const struct device *dev, int32_t *max_uv)
326 {
327 const struct regulator_common_config *config =
328 (const struct regulator_common_config *)dev->config;
329
330 if (config->max_uv == INT32_MAX) {
331 return -ENOENT;
332 }
333
334 *max_uv = config->max_uv;
335 return 0;
336 }
337
338 /** @endcond */
339
340 /**
341 * @brief Regulator Parent Interface
342 * @defgroup regulator_parent_interface Regulator Parent Interface
343 * @{
344 */
345
346 /**
347 * @brief Set a DVS state.
348 *
349 * Some PMICs feature DVS (Dynamic Voltage Scaling) by allowing to program the
350 * voltage level for multiple states. Such states may be automatically changed
351 * by hardware using GPIO pins. Certain MCUs even allow to automatically
352 * configure specific output pins when entering low-power modes so that PMIC
353 * state is changed without software intervention. This API can be used when
354 * state needs to be changed by software.
355 *
356 * @param dev Parent regulator device instance.
357 * @param state DVS state (vendor specific identifier).
358 *
359 * @retval 0 If successful.
360 * @retval -ENOTSUP If given state is not supported.
361 * @retval -EPERM If state can't be changed by software.
362 * @retval -ENOSYS If function is not implemented.
363 * @retval -errno In case of any other error.
364 */
regulator_parent_dvs_state_set(const struct device * dev,regulator_dvs_state_t state)365 static inline int regulator_parent_dvs_state_set(const struct device *dev,
366 regulator_dvs_state_t state)
367 {
368 const struct regulator_parent_driver_api *api =
369 (const struct regulator_parent_driver_api *)dev->api;
370
371 if (api->dvs_state_set == NULL) {
372 return -ENOSYS;
373 }
374
375 return api->dvs_state_set(dev, state);
376 }
377
378 /**
379 * @brief Enter ship mode.
380 *
381 * Some PMICs feature a ship mode, which allows the system to save power.
382 * Exit from low power is normally by pin transition.
383 *
384 * This API can be used when ship mode needs to be entered.
385 *
386 * @param dev Parent regulator device instance.
387 *
388 * @retval 0 If successful.
389 * @retval -ENOSYS If function is not implemented.
390 * @retval -errno In case of any other error.
391 */
regulator_parent_ship_mode(const struct device * dev)392 static inline int regulator_parent_ship_mode(const struct device *dev)
393 {
394 const struct regulator_parent_driver_api *api =
395 (const struct regulator_parent_driver_api *)dev->api;
396
397 if (api->ship_mode == NULL) {
398 return -ENOSYS;
399 }
400
401 return api->ship_mode(dev);
402 }
403
404 /** @} */
405
406 /**
407 * @brief Enable a regulator.
408 *
409 * Reference-counted request that a regulator be turned on. A regulator is
410 * considered "on" when it has reached a stable/usable state. Regulators that
411 * are always on, or configured in devicetree with `regulator-always-on` will
412 * always stay enabled, and so this function will always succeed.
413 *
414 * @param dev Regulator device instance
415 *
416 * @retval 0 If regulator has been successfully enabled.
417 * @retval -errno Negative errno in case of failure.
418 * @retval -ENOTSUP If regulator enablement can not be controlled.
419 */
420 int regulator_enable(const struct device *dev);
421
422 /**
423 * @brief Check if a regulator is enabled.
424 *
425 * @param dev Regulator device instance.
426 *
427 * @retval true If regulator is enabled.
428 * @retval false If regulator is disabled.
429 */
430 bool regulator_is_enabled(const struct device *dev);
431
432 /**
433 * @brief Disable a regulator.
434 *
435 * Release a regulator after a previous regulator_enable() completed
436 * successfully. Regulators that are always on, or configured in devicetree with
437 * `regulator-always-on` will always stay enabled, and so this function will
438 * always succeed.
439 *
440 * This must be invoked at most once for each successful regulator_enable().
441 *
442 * @param dev Regulator device instance.
443 *
444 * @retval 0 If regulator has been successfully disabled.
445 * @retval -errno Negative errno in case of failure.
446 * @retval -ENOTSUP If regulator disablement can not be controlled.
447 */
448 int regulator_disable(const struct device *dev);
449
450 /**
451 * @brief Obtain the number of supported voltage levels.
452 *
453 * Each voltage level supported by a regulator gets an index, starting from
454 * zero. The total number of supported voltage levels can be used together with
455 * regulator_list_voltage() to list all supported voltage levels.
456 *
457 * @param dev Regulator device instance.
458 *
459 * @return Number of supported voltages.
460 */
regulator_count_voltages(const struct device * dev)461 static inline unsigned int regulator_count_voltages(const struct device *dev)
462 {
463 const struct regulator_driver_api *api =
464 (const struct regulator_driver_api *)dev->api;
465
466 if (api->count_voltages == NULL) {
467 return 0U;
468 }
469
470 return api->count_voltages(dev);
471 }
472
473 /**
474 * @brief Obtain the value of a voltage given an index.
475 *
476 * Each voltage level supported by a regulator gets an index, starting from
477 * zero. Together with regulator_count_voltages(), this function can be used
478 * to iterate over all supported voltages.
479 *
480 * @param dev Regulator device instance.
481 * @param idx Voltage index.
482 * @param[out] volt_uv Where voltage for the given @p index will be stored, in
483 * microvolts.
484 *
485 * @retval 0 If @p index corresponds to a supported voltage.
486 * @retval -EINVAL If @p index does not correspond to a supported voltage.
487 */
regulator_list_voltage(const struct device * dev,unsigned int idx,int32_t * volt_uv)488 static inline int regulator_list_voltage(const struct device *dev,
489 unsigned int idx, int32_t *volt_uv)
490 {
491 const struct regulator_driver_api *api =
492 (const struct regulator_driver_api *)dev->api;
493
494 if (api->list_voltage == NULL) {
495 return -EINVAL;
496 }
497
498 return api->list_voltage(dev, idx, volt_uv);
499 }
500
501 /**
502 * @brief Check if a voltage within a window is supported.
503 *
504 * @param dev Regulator device instance.
505 * @param min_uv Minimum voltage in microvolts.
506 * @param max_uv maximum voltage in microvolts.
507 *
508 * @retval true If voltage is supported.
509 * @retval false If voltage is not supported.
510 */
511 bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
512 int32_t max_uv);
513
514 /**
515 * @brief Set the output voltage.
516 *
517 * The output voltage will be configured to the closest supported output
518 * voltage. regulator_get_voltage() can be used to obtain the actual configured
519 * voltage. The voltage will be applied to the active or selected mode. Output
520 * voltage may be limited using `regulator-min-microvolt` and/or
521 * `regulator-max-microvolt` in devicetree.
522 *
523 * @param dev Regulator device instance.
524 * @param min_uv Minimum acceptable voltage in microvolts.
525 * @param max_uv Maximum acceptable voltage in microvolts.
526 *
527 * @retval 0 If successful.
528 * @retval -EINVAL If the given voltage window is not valid.
529 * @retval -ENOSYS If function is not implemented.
530 * @retval -errno In case of any other error.
531 */
532 int regulator_set_voltage(const struct device *dev, int32_t min_uv,
533 int32_t max_uv);
534
535 /**
536 * @brief Obtain output voltage.
537 *
538 * @param dev Regulator device instance.
539 * @param[out] volt_uv Where configured output voltage will be stored.
540 *
541 * @retval 0 If successful
542 * @retval -ENOSYS If function is not implemented.
543 * @retval -errno In case of any other error.
544 */
regulator_get_voltage(const struct device * dev,int32_t * volt_uv)545 static inline int regulator_get_voltage(const struct device *dev,
546 int32_t *volt_uv)
547 {
548 const struct regulator_driver_api *api =
549 (const struct regulator_driver_api *)dev->api;
550
551 if (api->get_voltage == NULL) {
552 return -ENOSYS;
553 }
554
555 return api->get_voltage(dev, volt_uv);
556 }
557
558 /**
559 * @brief Obtain the number of supported current limit levels.
560 *
561 * Each current limit level supported by a regulator gets an index, starting from
562 * zero. The total number of supported current limit levels can be used together with
563 * regulator_list_current_limit() to list all supported current limit levels.
564 *
565 * @param dev Regulator device instance.
566 *
567 * @return Number of supported current limits.
568 */
regulator_count_current_limits(const struct device * dev)569 static inline unsigned int regulator_count_current_limits(const struct device *dev)
570 {
571 const struct regulator_driver_api *api =
572 (const struct regulator_driver_api *)dev->api;
573
574 if (api->count_current_limits == NULL) {
575 return 0U;
576 }
577
578 return api->count_current_limits(dev);
579 }
580
581 /**
582 * @brief Obtain the value of a current limit given an index.
583 *
584 * Each current limit level supported by a regulator gets an index, starting from
585 * zero. Together with regulator_count_current_limits(), this function can be used
586 * to iterate over all supported current limits.
587 *
588 * @param dev Regulator device instance.
589 * @param idx Current index.
590 * @param[out] current_ua Where current for the given @p index will be stored, in
591 * microamps.
592 *
593 * @retval 0 If @p index corresponds to a supported current limit.
594 * @retval -EINVAL If @p index does not correspond to a supported current limit.
595 */
regulator_list_current_limit(const struct device * dev,unsigned int idx,int32_t * current_ua)596 static inline int regulator_list_current_limit(const struct device *dev,
597 unsigned int idx, int32_t *current_ua)
598 {
599 const struct regulator_driver_api *api =
600 (const struct regulator_driver_api *)dev->api;
601
602 if (api->list_current_limit == NULL) {
603 return -EINVAL;
604 }
605
606 return api->list_current_limit(dev, idx, current_ua);
607 }
608
609 /**
610 * @brief Set output current limit.
611 *
612 * The output current limit will be configured to the closest supported output
613 * current limit. regulator_get_current_limit() can be used to obtain the actual
614 * configured current limit. Current may be limited using `current-min-microamp`
615 * and/or `current-max-microamp` in Devicetree.
616 *
617 * @param dev Regulator device instance.
618 * @param min_ua Minimum acceptable current limit in microamps.
619 * @param max_ua Maximum acceptable current limit in microamps.
620 *
621 * @retval 0 If successful.
622 * @retval -EINVAL If the given current limit window is not valid.
623 * @retval -ENOSYS If function is not implemented.
624 * @retval -errno In case of any other error.
625 */
626 int regulator_set_current_limit(const struct device *dev, int32_t min_ua,
627 int32_t max_ua);
628
629 /**
630 * @brief Get output current limit.
631 *
632 * @param dev Regulator device instance.
633 * @param[out] curr_ua Where output current limit will be stored.
634 *
635 * @retval 0 If successful.
636 * @retval -ENOSYS If function is not implemented.
637 * @retval -errno In case of any other error.
638 */
regulator_get_current_limit(const struct device * dev,int32_t * curr_ua)639 static inline int regulator_get_current_limit(const struct device *dev,
640 int32_t *curr_ua)
641 {
642 const struct regulator_driver_api *api =
643 (const struct regulator_driver_api *)dev->api;
644
645 if (api->get_current_limit == NULL) {
646 return -ENOSYS;
647 }
648
649 return api->get_current_limit(dev, curr_ua);
650 }
651
652 /**
653 * @brief Set mode.
654 *
655 * Regulators can support multiple modes in order to permit different voltage
656 * configuration or better power savings. This API will apply a mode for
657 * the regulator. Allowed modes may be limited using `regulator-allowed-modes`
658 * devicetree property.
659 *
660 * @param dev Regulator device instance.
661 * @param mode Mode to select for this regulator.
662 *
663 * @retval 0 If successful.
664 * @retval -ENOTSUP If mode is not supported.
665 * @retval -ENOSYS If function is not implemented.
666 * @retval -errno In case of any other error.
667 */
668 int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
669
670 /**
671 * @brief Get mode.
672 *
673 * @param dev Regulator device instance.
674 * @param[out] mode Where mode will be stored.
675 *
676 * @retval 0 If successful.
677 * @retval -ENOSYS If function is not implemented.
678 * @retval -errno In case of any other error.
679 */
regulator_get_mode(const struct device * dev,regulator_mode_t * mode)680 static inline int regulator_get_mode(const struct device *dev,
681 regulator_mode_t *mode)
682 {
683 const struct regulator_driver_api *api =
684 (const struct regulator_driver_api *)dev->api;
685
686 if (api->get_mode == NULL) {
687 return -ENOSYS;
688 }
689
690 return api->get_mode(dev, mode);
691 }
692
693 /**
694 * @brief Set active discharge setting.
695 *
696 * @param dev Regulator device instance.
697 * @param active_discharge Active discharge enable or disable.
698 *
699 * @retval 0 If successful.
700 * @retval -ENOSYS If function is not implemented.
701 * @retval -errno In case of any other error.
702 */
regulator_set_active_discharge(const struct device * dev,bool active_discharge)703 static inline int regulator_set_active_discharge(const struct device *dev,
704 bool active_discharge)
705 {
706 const struct regulator_driver_api *api =
707 (const struct regulator_driver_api *)dev->api;
708
709 if (api->set_active_discharge == NULL) {
710 return -ENOSYS;
711 }
712
713 return api->set_active_discharge(dev, active_discharge);
714 }
715
716 /**
717 * @brief Get active discharge setting.
718 *
719 * @param dev Regulator device instance.
720 * @param[out] active_discharge Where active discharge will be stored.
721 *
722 * @retval 0 If successful.
723 * @retval -ENOSYS If function is not implemented.
724 * @retval -errno In case of any other error.
725 */
regulator_get_active_discharge(const struct device * dev,bool * active_discharge)726 static inline int regulator_get_active_discharge(const struct device *dev,
727 bool *active_discharge)
728 {
729 const struct regulator_driver_api *api =
730 (const struct regulator_driver_api *)dev->api;
731
732 if (api->get_active_discharge == NULL) {
733 return -ENOSYS;
734 }
735
736 return api->get_active_discharge(dev, active_discharge);
737 }
738
739 /**
740 * @brief Get active error flags.
741 *
742 * @param dev Regulator device instance.
743 * @param[out] flags Where error flags will be stored.
744 *
745 * @retval 0 If successful.
746 * @retval -ENOSYS If function is not implemented.
747 * @retval -errno In case of any other error.
748 */
regulator_get_error_flags(const struct device * dev,regulator_error_flags_t * flags)749 static inline int regulator_get_error_flags(const struct device *dev,
750 regulator_error_flags_t *flags)
751 {
752 const struct regulator_driver_api *api =
753 (const struct regulator_driver_api *)dev->api;
754
755 if (api->get_error_flags == NULL) {
756 return -ENOSYS;
757 }
758
759 return api->get_error_flags(dev, flags);
760 }
761
762 #ifdef __cplusplus
763 }
764 #endif
765
766 /** @} */
767
768 #endif /* ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_ */
769