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