Zephyr API Documentation 4.4.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
regulator.h
Go to the documentation of this file.
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
15
16#ifndef ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
17#define ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
18
27
28#include <errno.h>
29#include <stdint.h>
30
31#include <zephyr/device.h>
32#include <zephyr/devicetree.h>
33#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
34#include <zephyr/kernel.h>
35#endif
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
44
47
50
56
58#define REGULATOR_ERROR_OVER_VOLTAGE BIT(0)
60#define REGULATOR_ERROR_OVER_CURRENT BIT(1)
62#define REGULATOR_ERROR_OVER_TEMP BIT(2)
63
65
75
81
90typedef void (*regulator_callback_t)(const struct device *dev,
91 const struct regulator_event *const evt,
92 const void *const user_data);
93
95
96typedef int (*regulator_dvs_state_set_t)(const struct device *dev,
98
99typedef int (*regulator_ship_mode_t)(const struct device *dev);
100
102__subsystem struct regulator_parent_driver_api {
103 regulator_dvs_state_set_t dvs_state_set;
104 regulator_ship_mode_t ship_mode;
105};
106
107typedef int (*regulator_enable_t)(const struct device *dev);
108typedef int (*regulator_disable_t)(const struct device *dev);
109typedef unsigned int (*regulator_count_voltages_t)(const struct device *dev);
110typedef int (*regulator_list_voltage_t)(const struct device *dev,
111 unsigned int idx, int32_t *volt_uv);
112typedef int (*regulator_set_voltage_t)(const struct device *dev, int32_t min_uv,
113 int32_t max_uv);
114typedef int (*regulator_get_voltage_t)(const struct device *dev,
115 int32_t *volt_uv);
116typedef unsigned int (*regulator_count_current_limits_t)(const struct device *dev);
117typedef int (*regulator_list_current_limit_t)(const struct device *dev,
118 unsigned int idx, int32_t *current_ua);
119typedef int (*regulator_set_current_limit_t)(const struct device *dev,
120 int32_t min_ua, int32_t max_ua);
121typedef int (*regulator_get_current_limit_t)(const struct device *dev,
122 int32_t *curr_ua);
123typedef int (*regulator_set_mode_t)(const struct device *dev,
124 regulator_mode_t mode);
125typedef int (*regulator_get_mode_t)(const struct device *dev,
126 regulator_mode_t *mode);
127typedef int (*regulator_set_active_discharge_t)(const struct device *dev,
128 bool active_discharge);
129typedef int (*regulator_get_active_discharge_t)(const struct device *dev,
130 bool *active_discharge);
131typedef int (*regulator_get_error_flags_t)(
132 const struct device *dev, regulator_error_flags_t *flags);
133typedef int (*regulator_set_callback_t)(const struct device *dev,
134 regulator_callback_t cb, const void *const user_data);
135
137__subsystem struct regulator_driver_api {
138 regulator_enable_t enable;
139 regulator_disable_t disable;
140 regulator_count_voltages_t count_voltages;
141 regulator_list_voltage_t list_voltage;
142 regulator_set_voltage_t set_voltage;
143 regulator_get_voltage_t get_voltage;
144 regulator_count_current_limits_t count_current_limits;
145 regulator_list_current_limit_t list_current_limit;
146 regulator_set_current_limit_t set_current_limit;
147 regulator_get_current_limit_t get_current_limit;
148 regulator_set_mode_t set_mode;
149 regulator_get_mode_t get_mode;
150 regulator_set_active_discharge_t set_active_discharge;
151 regulator_get_active_discharge_t get_active_discharge;
152 regulator_get_error_flags_t get_error_flags;
153 regulator_set_callback_t set_callback;
154};
155
162#define REGULATOR_ALWAYS_ON BIT(0)
164#define REGULATOR_BOOT_ON BIT(1)
166#define REGULATOR_INIT_ENABLED (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
168#define REGULATOR_ACTIVE_DISCHARGE_MASK GENMASK(3, 2)
170#define REGULATOR_ACTIVE_DISCHARGE_POS 2
172#define REGULATOR_ACTIVE_DISCHARGE_DISABLE 0
174#define REGULATOR_ACTIVE_DISCHARGE_ENABLE 1
176#define REGULATOR_ACTIVE_DISCHARGE_DEFAULT 2
178#define REGULATOR_ACTIVE_DISCHARGE_SET_BITS(x) \
179 (((x) << REGULATOR_ACTIVE_DISCHARGE_POS) & REGULATOR_ACTIVE_DISCHARGE_MASK)
181#define REGULATOR_ACTIVE_DISCHARGE_GET_BITS(x) \
182 (((x) & REGULATOR_ACTIVE_DISCHARGE_MASK) >> REGULATOR_ACTIVE_DISCHARGE_POS)
184#define REGULATOR_BOOT_OFF BIT(4)
185
187
189#define REGULATOR_INITIAL_MODE_UNKNOWN UINT8_MAX
190
196struct regulator_common_config {
198 int32_t min_uv;
200 int32_t max_uv;
202 int32_t init_uv;
204 int32_t min_ua;
206 int32_t max_ua;
208 int32_t init_ua;
210 uint32_t startup_delay_us;
212 uint32_t off_on_delay_us;
214 const regulator_mode_t *allowed_modes;
216 uint8_t allowed_modes_cnt;
218 regulator_mode_t initial_mode;
221};
222
228#define REGULATOR_DT_COMMON_CONFIG_INIT(node_id) \
229 { \
230 .min_uv = DT_PROP_OR(node_id, regulator_min_microvolt, \
231 INT32_MIN), \
232 .max_uv = DT_PROP_OR(node_id, regulator_max_microvolt, \
233 INT32_MAX), \
234 .init_uv = DT_PROP_OR(node_id, regulator_init_microvolt, \
235 INT32_MIN), \
236 .min_ua = DT_PROP_OR(node_id, regulator_min_microamp, \
237 INT32_MIN), \
238 .max_ua = DT_PROP_OR(node_id, regulator_max_microamp, \
239 INT32_MAX), \
240 .init_ua = DT_PROP_OR(node_id, regulator_init_microamp, \
241 INT32_MIN), \
242 .startup_delay_us = DT_PROP_OR(node_id, startup_delay_us, 0), \
243 .off_on_delay_us = DT_PROP_OR(node_id, off_on_delay_us, 0), \
244 .allowed_modes = (const regulator_mode_t []) \
245 DT_PROP_OR(node_id, regulator_allowed_modes, {}), \
246 .allowed_modes_cnt = \
247 DT_PROP_LEN_OR(node_id, regulator_allowed_modes, 0), \
248 .initial_mode = DT_PROP_OR(node_id, regulator_initial_mode, \
249 REGULATOR_INITIAL_MODE_UNKNOWN), \
250 .flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) * \
251 REGULATOR_ALWAYS_ON) | \
252 (DT_PROP_OR(node_id, regulator_boot_on, 0U) * \
253 REGULATOR_BOOT_ON) | \
254 (REGULATOR_ACTIVE_DISCHARGE_SET_BITS( \
255 DT_PROP_OR(node_id, regulator_active_discharge, \
256 REGULATOR_ACTIVE_DISCHARGE_DEFAULT))) | \
257 (DT_PROP_OR(node_id, regulator_boot_off, 0U) * \
258 REGULATOR_BOOT_OFF)), \
259 }
260
266#define REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst) \
267 REGULATOR_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
268
274struct regulator_common_data {
275#if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
277 struct k_mutex lock;
278#endif
280 int refcnt;
281};
282
290void regulator_common_data_init(const struct device *dev);
291
314int regulator_common_init(const struct device *dev, bool is_enabled);
315
323static inline bool regulator_common_is_init_enabled(const struct device *dev)
324{
325 const struct regulator_common_config *config =
326 (const struct regulator_common_config *)dev->config;
327
328 return (config->flags & REGULATOR_INIT_ENABLED) != 0U;
329}
330
340static inline int regulator_common_get_min_voltage(const struct device *dev, int32_t *min_uv)
341{
342 const struct regulator_common_config *config =
343 (const struct regulator_common_config *)dev->config;
344
345 if (config->min_uv == INT32_MIN) {
346 return -ENOENT;
347 }
348
349 *min_uv = config->min_uv;
350 return 0;
351}
352
362static inline int regulator_common_get_max_voltage(const struct device *dev, int32_t *max_uv)
363{
364 const struct regulator_common_config *config =
365 (const struct regulator_common_config *)dev->config;
366
367 if (config->max_uv == INT32_MAX) {
368 return -ENOENT;
369 }
370
371 *max_uv = config->max_uv;
372 return 0;
373}
374
376
382
402static inline int regulator_parent_dvs_state_set(const struct device *dev,
404{
405 const struct regulator_parent_driver_api *api =
406 (const struct regulator_parent_driver_api *)dev->api;
407
408 if (api->dvs_state_set == NULL) {
409 return -ENOSYS;
410 }
411
412 return api->dvs_state_set(dev, state);
413}
414
429static inline int regulator_parent_ship_mode(const struct device *dev)
430{
431 const struct regulator_parent_driver_api *api =
432 (const struct regulator_parent_driver_api *)dev->api;
433
434 if (api->ship_mode == NULL) {
435 return -ENOSYS;
436 }
437
438 return api->ship_mode(dev);
439}
440
442
457int regulator_enable(const struct device *dev);
458
467bool regulator_is_enabled(const struct device *dev);
468
485int regulator_disable(const struct device *dev);
486
498static inline unsigned int regulator_count_voltages(const struct device *dev)
499{
500 const struct regulator_driver_api *api =
501 (const struct regulator_driver_api *)dev->api;
502
503 if (api->count_voltages == NULL) {
504 return 0U;
505 }
506
507 return api->count_voltages(dev);
508}
509
525static inline int regulator_list_voltage(const struct device *dev,
526 unsigned int idx, int32_t *volt_uv)
527{
528 const struct regulator_driver_api *api =
529 (const struct regulator_driver_api *)dev->api;
530
531 if (api->list_voltage == NULL) {
532 return -EINVAL;
533 }
534
535 return api->list_voltage(dev, idx, volt_uv);
536}
537
548bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
549 int32_t max_uv);
550
569int regulator_set_voltage(const struct device *dev, int32_t min_uv,
570 int32_t max_uv);
571
582static inline int regulator_get_voltage(const struct device *dev,
583 int32_t *volt_uv)
584{
585 const struct regulator_driver_api *api =
586 (const struct regulator_driver_api *)dev->api;
587
588 if (api->get_voltage == NULL) {
589 return -ENOSYS;
590 }
591
592 return api->get_voltage(dev, volt_uv);
593}
594
606static inline unsigned int regulator_count_current_limits(const struct device *dev)
607{
608 const struct regulator_driver_api *api =
609 (const struct regulator_driver_api *)dev->api;
610
611 if (api->count_current_limits == NULL) {
612 return 0U;
613 }
614
615 return api->count_current_limits(dev);
616}
617
633static inline int regulator_list_current_limit(const struct device *dev,
634 unsigned int idx, int32_t *current_ua)
635{
636 const struct regulator_driver_api *api =
637 (const struct regulator_driver_api *)dev->api;
638
639 if (api->list_current_limit == NULL) {
640 return -EINVAL;
641 }
642
643 return api->list_current_limit(dev, idx, current_ua);
644}
645
663int regulator_set_current_limit(const struct device *dev, int32_t min_ua,
664 int32_t max_ua);
665
676static inline int regulator_get_current_limit(const struct device *dev,
677 int32_t *curr_ua)
678{
679 const struct regulator_driver_api *api =
680 (const struct regulator_driver_api *)dev->api;
681
682 if (api->get_current_limit == NULL) {
683 return -ENOSYS;
684 }
685
686 return api->get_current_limit(dev, curr_ua);
687}
688
705int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
706
717static inline int regulator_get_mode(const struct device *dev,
718 regulator_mode_t *mode)
719{
720 const struct regulator_driver_api *api =
721 (const struct regulator_driver_api *)dev->api;
722
723 if (api->get_mode == NULL) {
724 return -ENOSYS;
725 }
726
727 return api->get_mode(dev, mode);
728}
729
740static inline int regulator_set_active_discharge(const struct device *dev,
741 bool active_discharge)
742{
743 const struct regulator_driver_api *api =
744 (const struct regulator_driver_api *)dev->api;
745
746 if (api->set_active_discharge == NULL) {
747 return -ENOSYS;
748 }
749
750 return api->set_active_discharge(dev, active_discharge);
751}
752
763static inline int regulator_get_active_discharge(const struct device *dev,
764 bool *active_discharge)
765{
766 const struct regulator_driver_api *api =
767 (const struct regulator_driver_api *)dev->api;
768
769 if (api->get_active_discharge == NULL) {
770 return -ENOSYS;
771 }
772
773 return api->get_active_discharge(dev, active_discharge);
774}
775
786static inline int regulator_get_error_flags(const struct device *dev,
788{
789 const struct regulator_driver_api *api =
790 (const struct regulator_driver_api *)dev->api;
791
792 if (api->get_error_flags == NULL) {
793 return -ENOSYS;
794 }
795
796 return api->get_error_flags(dev, flags);
797}
798
813static inline int regulator_set_callback(const struct device *dev,
815 const void *const user_data)
816{
817 const struct regulator_driver_api *api =
818 (const struct regulator_driver_api *)dev->api;
819
820 if (api->set_callback == NULL) {
821 return -ENOSYS;
822 }
823
824 return api->set_callback(dev, cb, user_data);
825}
826
827#ifdef __cplusplus
828}
829#endif
830
832
833#endif /* ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_ */
Devicetree main header.
System error numbers.
void(* regulator_callback_t)(const struct device *dev, const struct regulator_event *const evt, const void *const user_data)
Regulator callback function signature.
Definition regulator.h:90
int regulator_enable(const struct device *dev)
Enable a regulator.
int regulator_set_mode(const struct device *dev, regulator_mode_t mode)
Set mode.
uint8_t regulator_mode_t
Opaque type to store regulator modes.
Definition regulator.h:46
static unsigned int regulator_count_current_limits(const struct device *dev)
Obtain the number of supported current limit levels.
Definition regulator.h:606
bool regulator_is_enabled(const struct device *dev)
Check if a regulator is enabled.
int regulator_set_voltage(const struct device *dev, int32_t min_uv, int32_t max_uv)
Set the output voltage.
int regulator_set_current_limit(const struct device *dev, int32_t min_ua, int32_t max_ua)
Set output current limit.
bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv, int32_t max_uv)
Check if a voltage within a window is supported.
uint8_t regulator_error_flags_t
Opaque bit map for regulator error flags (see REGULATOR_ERRORS).
Definition regulator.h:49
static int regulator_get_active_discharge(const struct device *dev, bool *active_discharge)
Get active discharge setting.
Definition regulator.h:763
static int regulator_list_voltage(const struct device *dev, unsigned int idx, int32_t *volt_uv)
Obtain the value of a voltage given an index.
Definition regulator.h:525
static int regulator_get_current_limit(const struct device *dev, int32_t *curr_ua)
Get output current limit.
Definition regulator.h:676
static int regulator_set_active_discharge(const struct device *dev, bool active_discharge)
Set active discharge setting.
Definition regulator.h:740
static int regulator_get_error_flags(const struct device *dev, regulator_error_flags_t *flags)
Get active error flags.
Definition regulator.h:786
static unsigned int regulator_count_voltages(const struct device *dev)
Obtain the number of supported voltage levels.
Definition regulator.h:498
uint8_t regulator_dvs_state_t
Opaque type to store regulator DVS states.
Definition regulator.h:43
static int regulator_get_mode(const struct device *dev, regulator_mode_t *mode)
Get mode.
Definition regulator.h:717
static int regulator_get_voltage(const struct device *dev, int32_t *volt_uv)
Obtain output voltage.
Definition regulator.h:582
int regulator_disable(const struct device *dev)
Disable a regulator.
regulator_event_type
Regulator event types.
Definition regulator.h:69
static int regulator_set_callback(const struct device *dev, regulator_callback_t cb, const void *const user_data)
Set event handler function.
Definition regulator.h:813
static int regulator_list_current_limit(const struct device *dev, unsigned int idx, int32_t *current_ua)
Obtain the value of a current limit given an index.
Definition regulator.h:633
@ REGULATOR_VOLTAGE_DETECTED
Regulator voltage is detected.
Definition regulator.h:71
@ REGULATOR_VOLTAGE_REMOVED
Regulator voltage is removed.
Definition regulator.h:73
static int regulator_parent_dvs_state_set(const struct device *dev, regulator_dvs_state_t state)
Set a DVS state.
Definition regulator.h:402
static int regulator_parent_ship_mode(const struct device *dev)
Enter ship mode.
Definition regulator.h:429
#define ENOENT
No such file or directory.
Definition errno.h:40
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
Public kernel APIs.
flags
Definition parser.h:97
state
Definition parser_state.h:29
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
#define INT32_MAX
Definition stdint.h:18
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
#define INT32_MIN
Definition stdint.h:24
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
const void * config
Address of device instance config information.
Definition device.h:517
Regulator event structure.
Definition regulator.h:77
enum regulator_event_type type
Event type.
Definition regulator.h:79
Macro utilities.