Zephyr API Documentation 4.4.99
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
33
34#include <errno.h>
35#include <stdint.h>
36
37#include <zephyr/device.h>
38#include <zephyr/devicetree.h>
39#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
40#include <zephyr/kernel.h>
41#endif
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
50
53
56
62
64#define REGULATOR_ERROR_OVER_VOLTAGE BIT(0)
66#define REGULATOR_ERROR_OVER_CURRENT BIT(1)
68#define REGULATOR_ERROR_OVER_TEMP BIT(2)
69
71
81
87
95typedef void (*regulator_callback_t)(const struct device *dev,
96 const struct regulator_event *const evt,
97 const void *const user_data);
98
100
101typedef int (*regulator_dvs_state_set_t)(const struct device *dev,
103
104typedef int (*regulator_ship_mode_t)(const struct device *dev);
105
107__subsystem struct regulator_parent_driver_api {
108 regulator_dvs_state_set_t dvs_state_set;
109 regulator_ship_mode_t ship_mode;
110};
111
112typedef int (*regulator_enable_t)(const struct device *dev);
113typedef int (*regulator_disable_t)(const struct device *dev);
114typedef unsigned int (*regulator_count_voltages_t)(const struct device *dev);
115typedef int (*regulator_list_voltage_t)(const struct device *dev,
116 unsigned int idx, int32_t *volt_uv);
117typedef int (*regulator_set_voltage_t)(const struct device *dev, int32_t min_uv,
118 int32_t max_uv);
119typedef int (*regulator_get_voltage_t)(const struct device *dev,
120 int32_t *volt_uv);
121typedef unsigned int (*regulator_count_current_limits_t)(const struct device *dev);
122typedef int (*regulator_list_current_limit_t)(const struct device *dev,
123 unsigned int idx, int32_t *current_ua);
124typedef int (*regulator_set_current_limit_t)(const struct device *dev,
125 int32_t min_ua, int32_t max_ua);
126typedef int (*regulator_get_current_limit_t)(const struct device *dev,
127 int32_t *curr_ua);
128typedef int (*regulator_set_mode_t)(const struct device *dev,
129 regulator_mode_t mode);
130typedef int (*regulator_get_mode_t)(const struct device *dev,
131 regulator_mode_t *mode);
132typedef int (*regulator_set_active_discharge_t)(const struct device *dev,
133 bool active_discharge);
134typedef int (*regulator_get_active_discharge_t)(const struct device *dev,
135 bool *active_discharge);
136typedef int (*regulator_get_error_flags_t)(
137 const struct device *dev, regulator_error_flags_t *flags);
138typedef int (*regulator_set_callback_t)(const struct device *dev,
139 regulator_callback_t cb, const void *const user_data);
140
142__subsystem struct regulator_driver_api {
143 regulator_enable_t enable;
144 regulator_disable_t disable;
145 regulator_count_voltages_t count_voltages;
146 regulator_list_voltage_t list_voltage;
147 regulator_set_voltage_t set_voltage;
148 regulator_get_voltage_t get_voltage;
149 regulator_count_current_limits_t count_current_limits;
150 regulator_list_current_limit_t list_current_limit;
151 regulator_set_current_limit_t set_current_limit;
152 regulator_get_current_limit_t get_current_limit;
153 regulator_set_mode_t set_mode;
154 regulator_get_mode_t get_mode;
155 regulator_set_active_discharge_t set_active_discharge;
156 regulator_get_active_discharge_t get_active_discharge;
157 regulator_get_error_flags_t get_error_flags;
158 regulator_set_callback_t set_callback;
159};
160
167#define REGULATOR_ALWAYS_ON BIT(0)
169#define REGULATOR_BOOT_ON BIT(1)
171#define REGULATOR_INIT_ENABLED (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
173#define REGULATOR_ACTIVE_DISCHARGE_MASK GENMASK(3, 2)
175#define REGULATOR_ACTIVE_DISCHARGE_POS 2
177#define REGULATOR_ACTIVE_DISCHARGE_DISABLE 0
179#define REGULATOR_ACTIVE_DISCHARGE_ENABLE 1
181#define REGULATOR_ACTIVE_DISCHARGE_DEFAULT 2
183#define REGULATOR_ACTIVE_DISCHARGE_SET_BITS(x) \
184 (((x) << REGULATOR_ACTIVE_DISCHARGE_POS) & REGULATOR_ACTIVE_DISCHARGE_MASK)
186#define REGULATOR_ACTIVE_DISCHARGE_GET_BITS(x) \
187 (((x) & REGULATOR_ACTIVE_DISCHARGE_MASK) >> REGULATOR_ACTIVE_DISCHARGE_POS)
189#define REGULATOR_BOOT_OFF BIT(4)
190
192
194#define REGULATOR_INITIAL_MODE_UNKNOWN UINT8_MAX
195
201struct regulator_common_config {
203 int32_t min_uv;
205 int32_t max_uv;
207 int32_t init_uv;
209 int32_t min_ua;
211 int32_t max_ua;
213 int32_t init_ua;
215 uint32_t startup_delay_us;
217 uint32_t off_on_delay_us;
219 const regulator_mode_t *allowed_modes;
221 uint8_t allowed_modes_cnt;
223 regulator_mode_t initial_mode;
226};
227
233#define REGULATOR_DT_COMMON_CONFIG_INIT(node_id) \
234 { \
235 .min_uv = DT_PROP_OR(node_id, regulator_min_microvolt, \
236 INT32_MIN), \
237 .max_uv = DT_PROP_OR(node_id, regulator_max_microvolt, \
238 INT32_MAX), \
239 .init_uv = DT_PROP_OR(node_id, regulator_init_microvolt, \
240 INT32_MIN), \
241 .min_ua = DT_PROP_OR(node_id, regulator_min_microamp, \
242 INT32_MIN), \
243 .max_ua = DT_PROP_OR(node_id, regulator_max_microamp, \
244 INT32_MAX), \
245 .init_ua = DT_PROP_OR(node_id, regulator_init_microamp, \
246 INT32_MIN), \
247 .startup_delay_us = DT_PROP_OR(node_id, startup_delay_us, 0), \
248 .off_on_delay_us = DT_PROP_OR(node_id, off_on_delay_us, 0), \
249 .allowed_modes = (const regulator_mode_t []) \
250 DT_PROP_OR(node_id, regulator_allowed_modes, {}), \
251 .allowed_modes_cnt = \
252 DT_PROP_LEN_OR(node_id, regulator_allowed_modes, 0), \
253 .initial_mode = DT_PROP_OR(node_id, regulator_initial_mode, \
254 REGULATOR_INITIAL_MODE_UNKNOWN), \
255 .flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) * \
256 REGULATOR_ALWAYS_ON) | \
257 (DT_PROP_OR(node_id, regulator_boot_on, 0U) * \
258 REGULATOR_BOOT_ON) | \
259 (REGULATOR_ACTIVE_DISCHARGE_SET_BITS( \
260 DT_PROP_OR(node_id, regulator_active_discharge, \
261 REGULATOR_ACTIVE_DISCHARGE_DEFAULT))) | \
262 (DT_PROP_OR(node_id, regulator_boot_off, 0U) * \
263 REGULATOR_BOOT_OFF)), \
264 }
265
271#define REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst) \
272 REGULATOR_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
273
279struct regulator_common_data {
280#if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
282 struct k_mutex lock;
283#endif
285 int refcnt;
286};
287
295void regulator_common_data_init(const struct device *dev);
296
319int regulator_common_init(const struct device *dev, bool is_enabled);
320
328static inline bool regulator_common_is_init_enabled(const struct device *dev)
329{
330 const struct regulator_common_config *config =
331 (const struct regulator_common_config *)dev->config;
332
333 return (config->flags & REGULATOR_INIT_ENABLED) != 0U;
334}
335
345static inline int regulator_common_get_min_voltage(const struct device *dev, int32_t *min_uv)
346{
347 const struct regulator_common_config *config =
348 (const struct regulator_common_config *)dev->config;
349
350 if (config->min_uv == INT32_MIN) {
351 return -ENOENT;
352 }
353
354 *min_uv = config->min_uv;
355 return 0;
356}
357
367static inline int regulator_common_get_max_voltage(const struct device *dev, int32_t *max_uv)
368{
369 const struct regulator_common_config *config =
370 (const struct regulator_common_config *)dev->config;
371
372 if (config->max_uv == INT32_MAX) {
373 return -ENOENT;
374 }
375
376 *max_uv = config->max_uv;
377 return 0;
378}
379
381
387
407static inline int regulator_parent_dvs_state_set(const struct device *dev,
409{
410 const struct regulator_parent_driver_api *api = DEVICE_API_GET(regulator_parent, dev);
411
412 if (api->dvs_state_set == NULL) {
413 return -ENOSYS;
414 }
415
416 return api->dvs_state_set(dev, state);
417}
418
433static inline int regulator_parent_ship_mode(const struct device *dev)
434{
435 const struct regulator_parent_driver_api *api = DEVICE_API_GET(regulator_parent, dev);
436
437 if (api->ship_mode == NULL) {
438 return -ENOSYS;
439 }
440
441 return api->ship_mode(dev);
442}
443
445
460int regulator_enable(const struct device *dev);
461
470bool regulator_is_enabled(const struct device *dev);
471
488int regulator_disable(const struct device *dev);
489
501static inline unsigned int regulator_count_voltages(const struct device *dev)
502{
503 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
504
505 if (api->count_voltages == NULL) {
506 return 0U;
507 }
508
509 return api->count_voltages(dev);
510}
511
527static inline int regulator_list_voltage(const struct device *dev,
528 unsigned int idx, int32_t *volt_uv)
529{
530 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
531
532 if (api->list_voltage == NULL) {
533 return -EINVAL;
534 }
535
536 return api->list_voltage(dev, idx, volt_uv);
537}
538
549bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
550 int32_t max_uv);
551
570int regulator_set_voltage(const struct device *dev, int32_t min_uv,
571 int32_t max_uv);
572
583static inline int regulator_get_voltage(const struct device *dev,
584 int32_t *volt_uv)
585{
586 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
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 = DEVICE_API_GET(regulator, dev);
609
610 if (api->count_current_limits == NULL) {
611 return 0U;
612 }
613
614 return api->count_current_limits(dev);
615}
616
632static inline int regulator_list_current_limit(const struct device *dev,
633 unsigned int idx, int32_t *current_ua)
634{
635 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
636
637 if (api->list_current_limit == NULL) {
638 return -EINVAL;
639 }
640
641 return api->list_current_limit(dev, idx, current_ua);
642}
643
661int regulator_set_current_limit(const struct device *dev, int32_t min_ua,
662 int32_t max_ua);
663
674static inline int regulator_get_current_limit(const struct device *dev,
675 int32_t *curr_ua)
676{
677 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
678
679 if (api->get_current_limit == NULL) {
680 return -ENOSYS;
681 }
682
683 return api->get_current_limit(dev, curr_ua);
684}
685
702int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
703
714static inline int regulator_get_mode(const struct device *dev,
715 regulator_mode_t *mode)
716{
717 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
718
719 if (api->get_mode == NULL) {
720 return -ENOSYS;
721 }
722
723 return api->get_mode(dev, mode);
724}
725
736static inline int regulator_set_active_discharge(const struct device *dev,
737 bool active_discharge)
738{
739 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
740
741 if (api->set_active_discharge == NULL) {
742 return -ENOSYS;
743 }
744
745 return api->set_active_discharge(dev, active_discharge);
746}
747
758static inline int regulator_get_active_discharge(const struct device *dev,
759 bool *active_discharge)
760{
761 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
762
763 if (api->get_active_discharge == NULL) {
764 return -ENOSYS;
765 }
766
767 return api->get_active_discharge(dev, active_discharge);
768}
769
780static inline int regulator_get_error_flags(const struct device *dev,
782{
783 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
784
785 if (api->get_error_flags == NULL) {
786 return -ENOSYS;
787 }
788
789 return api->get_error_flags(dev, flags);
790}
791
806static inline int regulator_set_callback(const struct device *dev,
808 const void *const user_data)
809{
810 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
811
812 if (api->set_callback == NULL) {
813 return -ENOSYS;
814 }
815
816 return api->set_callback(dev, cb, user_data);
817}
818
819#ifdef __cplusplus
820}
821#endif
822
824
825#endif /* ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1425
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:95
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:52
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:55
static int regulator_get_active_discharge(const struct device *dev, bool *active_discharge)
Get active discharge setting.
Definition regulator.h:758
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:527
static int regulator_get_current_limit(const struct device *dev, int32_t *curr_ua)
Get output current limit.
Definition regulator.h:674
static int regulator_set_active_discharge(const struct device *dev, bool active_discharge)
Set active discharge setting.
Definition regulator.h:736
static int regulator_get_error_flags(const struct device *dev, regulator_error_flags_t *flags)
Get active error flags.
Definition regulator.h:780
static unsigned int regulator_count_voltages(const struct device *dev)
Obtain the number of supported voltage levels.
Definition regulator.h:501
uint8_t regulator_dvs_state_t
Opaque type to store regulator DVS states.
Definition regulator.h:49
static int regulator_get_mode(const struct device *dev, regulator_mode_t *mode)
Get mode.
Definition regulator.h:714
static int regulator_get_voltage(const struct device *dev, int32_t *volt_uv)
Obtain output voltage.
Definition regulator.h:583
int regulator_disable(const struct device *dev)
Disable a regulator.
regulator_event_type
Regulator event types.
Definition regulator.h:75
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:806
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:632
@ REGULATOR_VOLTAGE_DETECTED
Regulator voltage is detected.
Definition regulator.h:77
@ REGULATOR_VOLTAGE_REMOVED
Regulator voltage is removed.
Definition regulator.h:79
static int regulator_parent_dvs_state_set(const struct device *dev, regulator_dvs_state_t state)
Set a DVS state.
Definition regulator.h:407
static int regulator_parent_ship_mode(const struct device *dev)
Enter ship mode.
Definition regulator.h:433
#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
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 * config
Address of device instance config information.
Definition device.h:517
Regulator event structure.
Definition regulator.h:83
enum regulator_event_type type
Event type.
Definition regulator.h:85
Macro utilities.