Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
10#define ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
11
19#include <errno.h>
20#include <stdint.h>
21
22#include <zephyr/device.h>
23#include <zephyr/devicetree.h>
24#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
25#include <zephyr/kernel.h>
26#endif
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
35
38
41
49#define REGULATOR_ERROR_OVER_VOLTAGE BIT(0)
51#define REGULATOR_ERROR_OVER_CURRENT BIT(1)
53#define REGULATOR_ERROR_OVER_TEMP BIT(2)
54
59typedef int (*regulator_dvs_state_set_t)(const struct device *dev,
61
62typedef int (*regulator_ship_mode_t)(const struct device *dev);
63
65__subsystem struct regulator_parent_driver_api {
66 regulator_dvs_state_set_t dvs_state_set;
67 regulator_ship_mode_t ship_mode;
68};
69
70typedef int (*regulator_enable_t)(const struct device *dev);
71typedef int (*regulator_disable_t)(const struct device *dev);
72typedef unsigned int (*regulator_count_voltages_t)(const struct device *dev);
73typedef int (*regulator_list_voltage_t)(const struct device *dev,
74 unsigned int idx, int32_t *volt_uv);
75typedef int (*regulator_set_voltage_t)(const struct device *dev, int32_t min_uv,
76 int32_t max_uv);
77typedef int (*regulator_get_voltage_t)(const struct device *dev,
78 int32_t *volt_uv);
79typedef int (*regulator_set_current_limit_t)(const struct device *dev,
80 int32_t min_ua, int32_t max_ua);
81typedef int (*regulator_get_current_limit_t)(const struct device *dev,
82 int32_t *curr_ua);
83typedef int (*regulator_set_mode_t)(const struct device *dev,
84 regulator_mode_t mode);
85typedef int (*regulator_get_mode_t)(const struct device *dev,
86 regulator_mode_t *mode);
87typedef int (*regulator_get_error_flags_t)(
88 const struct device *dev, regulator_error_flags_t *flags);
89
91__subsystem struct regulator_driver_api {
92 regulator_enable_t enable;
93 regulator_disable_t disable;
94 regulator_count_voltages_t count_voltages;
95 regulator_list_voltage_t list_voltage;
96 regulator_set_voltage_t set_voltage;
97 regulator_get_voltage_t get_voltage;
98 regulator_set_current_limit_t set_current_limit;
99 regulator_get_current_limit_t get_current_limit;
100 regulator_set_mode_t set_mode;
101 regulator_get_mode_t get_mode;
102 regulator_get_error_flags_t get_error_flags;
103};
104
111#define REGULATOR_ALWAYS_ON BIT(0)
113#define REGULATOR_BOOT_ON BIT(1)
115#define REGULATOR_INIT_ENABLED (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
116
120#define REGULATOR_INITIAL_MODE_UNKNOWN UINT8_MAX
121
127struct regulator_common_config {
129 int32_t min_uv;
131 int32_t max_uv;
133 int32_t init_uv;
135 int32_t min_ua;
137 int32_t max_ua;
139 const regulator_mode_t *allowed_modes;
141 uint8_t allowed_modes_cnt;
143 regulator_mode_t initial_mode;
146};
147
153#define REGULATOR_DT_COMMON_CONFIG_INIT(node_id) \
154 { \
155 .min_uv = DT_PROP_OR(node_id, regulator_min_microvolt, \
156 INT32_MIN), \
157 .max_uv = DT_PROP_OR(node_id, regulator_max_microvolt, \
158 INT32_MAX), \
159 .init_uv = DT_PROP_OR(node_id, regulator_init_microvolt, \
160 INT32_MIN), \
161 .min_ua = DT_PROP_OR(node_id, regulator_min_microamp, \
162 INT32_MIN), \
163 .max_ua = DT_PROP_OR(node_id, regulator_max_microamp, \
164 INT32_MAX), \
165 .allowed_modes = (const regulator_mode_t []) \
166 DT_PROP_OR(node_id, regulator_allowed_modes, {}), \
167 .allowed_modes_cnt = \
168 DT_PROP_LEN_OR(node_id, regulator_allowed_modes, 0), \
169 .initial_mode = DT_PROP_OR(node_id, regulator_initial_mode, \
170 REGULATOR_INITIAL_MODE_UNKNOWN), \
171 .flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) * \
172 REGULATOR_ALWAYS_ON) | \
173 (DT_PROP_OR(node_id, regulator_boot_on, 0U) * \
174 REGULATOR_BOOT_ON)), \
175 }
176
182#define REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst) \
183 REGULATOR_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
184
190struct regulator_common_data {
191#if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
193 struct k_mutex lock;
194#endif
196 int refcnt;
197};
198
206void regulator_common_data_init(const struct device *dev);
207
229int regulator_common_init(const struct device *dev, bool is_enabled);
230
238static inline bool regulator_common_is_init_enabled(const struct device *dev)
239{
240 const struct regulator_common_config *config =
241 (const struct regulator_common_config *)dev->config;
242
243 return (config->flags & REGULATOR_INIT_ENABLED) != 0U;
244}
245
255static inline int regulator_common_get_min_voltage(const struct device *dev, int32_t *min_uv)
256{
257 const struct regulator_common_config *config =
258 (const struct regulator_common_config *)dev->config;
259
260 if (config->min_uv == INT32_MIN) {
261 return -ENOENT;
262 }
263
264 *min_uv = config->min_uv;
265 return 0;
266}
267
295static inline int regulator_parent_dvs_state_set(const struct device *dev,
297{
298 const struct regulator_parent_driver_api *api =
299 (const struct regulator_parent_driver_api *)dev->api;
300
301 if (api->dvs_state_set == NULL) {
302 return -ENOSYS;
303 }
304
305 return api->dvs_state_set(dev, state);
306}
307
322static inline int regulator_parent_ship_mode(const struct device *dev)
323{
324 const struct regulator_parent_driver_api *api =
325 (const struct regulator_parent_driver_api *)dev->api;
326
327 if (api->ship_mode == NULL) {
328 return -ENOSYS;
329 }
330
331 return api->ship_mode(dev);
332}
333
350int regulator_enable(const struct device *dev);
351
360bool regulator_is_enabled(const struct device *dev);
361
378int regulator_disable(const struct device *dev);
379
391static inline unsigned int regulator_count_voltages(const struct device *dev)
392{
393 const struct regulator_driver_api *api =
394 (const struct regulator_driver_api *)dev->api;
395
396 if (api->count_voltages == NULL) {
397 return 0U;
398 }
399
400 return api->count_voltages(dev);
401}
402
418static inline int regulator_list_voltage(const struct device *dev,
419 unsigned int idx, int32_t *volt_uv)
420{
421 const struct regulator_driver_api *api =
422 (const struct regulator_driver_api *)dev->api;
423
424 if (api->list_voltage == NULL) {
425 return -EINVAL;
426 }
427
428 return api->list_voltage(dev, idx, volt_uv);
429}
430
441bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
442 int32_t max_uv);
443
462int regulator_set_voltage(const struct device *dev, int32_t min_uv,
463 int32_t max_uv);
464
475static inline int regulator_get_voltage(const struct device *dev,
476 int32_t *volt_uv)
477{
478 const struct regulator_driver_api *api =
479 (const struct regulator_driver_api *)dev->api;
480
481 if (api->get_voltage == NULL) {
482 return -ENOSYS;
483 }
484
485 return api->get_voltage(dev, volt_uv);
486}
487
505int regulator_set_current_limit(const struct device *dev, int32_t min_ua,
506 int32_t max_ua);
507
518static inline int regulator_get_current_limit(const struct device *dev,
519 int32_t *curr_ua)
520{
521 const struct regulator_driver_api *api =
522 (const struct regulator_driver_api *)dev->api;
523
524 if (api->get_current_limit == NULL) {
525 return -ENOSYS;
526 }
527
528 return api->get_current_limit(dev, curr_ua);
529}
530
547int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
548
559static inline int regulator_get_mode(const struct device *dev,
560 regulator_mode_t *mode)
561{
562 const struct regulator_driver_api *api =
563 (const struct regulator_driver_api *)dev->api;
564
565 if (api->get_mode == NULL) {
566 return -ENOSYS;
567 }
568
569 return api->get_mode(dev, mode);
570}
571
582static inline int regulator_get_error_flags(const struct device *dev,
584{
585 const struct regulator_driver_api *api =
586 (const struct regulator_driver_api *)dev->api;
587
588 if (api->get_error_flags == NULL) {
589 return -ENOSYS;
590 }
591
592 return api->get_error_flags(dev, flags);
593}
594
595#ifdef __cplusplus
596}
597#endif
598
601#endif /* ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_ */
Devicetree main header.
System error numbers.
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:37
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:40
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:418
static int regulator_get_current_limit(const struct device *dev, int32_t *curr_ua)
Get output current limit.
Definition: regulator.h:518
static int regulator_get_error_flags(const struct device *dev, regulator_error_flags_t *flags)
Get active error flags.
Definition: regulator.h:582
static unsigned int regulator_count_voltages(const struct device *dev)
Obtain the number of supported voltage levels.
Definition: regulator.h:391
uint8_t regulator_dvs_state_t
Opaque type to store regulator DVS states.
Definition: regulator.h:34
static int regulator_get_mode(const struct device *dev, regulator_mode_t *mode)
Get mode.
Definition: regulator.h:559
static int regulator_get_voltage(const struct device *dev, int32_t *volt_uv)
Obtain output voltage.
Definition: regulator.h:475
int regulator_disable(const struct device *dev)
Disable a regulator.
static int regulator_parent_dvs_state_set(const struct device *dev, regulator_dvs_state_t state)
Set a DVS state.
Definition: regulator.h:295
static int regulator_parent_ship_mode(const struct device *dev)
Enter ship mode.
Definition: regulator.h:322
#define ENOENT
No such file or directory.
Definition: errno.h:41
#define EINVAL
Invalid argument.
Definition: errno.h:61
#define ENOSYS
Function not implemented.
Definition: errno.h:83
Public kernel APIs.
flags
Definition: parser.h:96
state
Definition: parser_state.h:29
__INT32_TYPE__ int32_t
Definition: stdint.h:74
__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:381
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:387
const void * config
Address of device instance config information.
Definition: device.h:385
Mutex Structure.
Definition: kernel.h:2911
Macro utilities.