16#ifndef ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
17#define ZEPHYR_INCLUDE_DRIVERS_REGULATOR_H_
33#ifdef CONFIG_REGULATOR_THREAD_SAFE_REFCNT
58#define REGULATOR_ERROR_OVER_VOLTAGE BIT(0)
60#define REGULATOR_ERROR_OVER_CURRENT BIT(1)
62#define REGULATOR_ERROR_OVER_TEMP BIT(2)
68typedef int (*regulator_dvs_state_set_t)(
const struct device *dev,
71typedef int (*regulator_ship_mode_t)(
const struct device *dev);
74__subsystem
struct regulator_parent_driver_api {
75 regulator_dvs_state_set_t dvs_state_set;
76 regulator_ship_mode_t ship_mode;
79typedef int (*regulator_enable_t)(
const struct device *dev);
80typedef int (*regulator_disable_t)(
const struct device *dev);
81typedef unsigned int (*regulator_count_voltages_t)(
const struct device *dev);
82typedef int (*regulator_list_voltage_t)(
const struct device *dev,
83 unsigned int idx,
int32_t *volt_uv);
84typedef int (*regulator_set_voltage_t)(
const struct device *dev,
int32_t min_uv,
86typedef int (*regulator_get_voltage_t)(
const struct device *dev,
88typedef unsigned int (*regulator_count_current_limits_t)(
const struct device *dev);
89typedef int (*regulator_list_current_limit_t)(
const struct device *dev,
90 unsigned int idx,
int32_t *current_ua);
91typedef int (*regulator_set_current_limit_t)(
const struct device *dev,
93typedef int (*regulator_get_current_limit_t)(
const struct device *dev,
95typedef int (*regulator_set_mode_t)(
const struct device *dev,
97typedef int (*regulator_get_mode_t)(
const struct device *dev,
99typedef int (*regulator_set_active_discharge_t)(
const struct device *dev,
100 bool active_discharge);
101typedef int (*regulator_get_active_discharge_t)(
const struct device *dev,
102 bool *active_discharge);
103typedef int (*regulator_get_error_flags_t)(
107__subsystem
struct regulator_driver_api {
108 regulator_enable_t enable;
109 regulator_disable_t disable;
110 regulator_count_voltages_t count_voltages;
111 regulator_list_voltage_t list_voltage;
112 regulator_set_voltage_t set_voltage;
113 regulator_get_voltage_t get_voltage;
114 regulator_count_current_limits_t count_current_limits;
115 regulator_list_current_limit_t list_current_limit;
116 regulator_set_current_limit_t set_current_limit;
117 regulator_get_current_limit_t get_current_limit;
118 regulator_set_mode_t set_mode;
119 regulator_get_mode_t get_mode;
120 regulator_set_active_discharge_t set_active_discharge;
121 regulator_get_active_discharge_t get_active_discharge;
122 regulator_get_error_flags_t get_error_flags;
131#define REGULATOR_ALWAYS_ON BIT(0)
133#define REGULATOR_BOOT_ON BIT(1)
135#define REGULATOR_INIT_ENABLED (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
137#define REGULATOR_ACTIVE_DISCHARGE_MASK GENMASK(3, 2)
139#define REGULATOR_ACTIVE_DISCHARGE_POS 2
141#define REGULATOR_ACTIVE_DISCHARGE_DISABLE 0
143#define REGULATOR_ACTIVE_DISCHARGE_ENABLE 1
145#define REGULATOR_ACTIVE_DISCHARGE_DEFAULT 2
147#define REGULATOR_ACTIVE_DISCHARGE_SET_BITS(x) \
148 (((x) << REGULATOR_ACTIVE_DISCHARGE_POS) & REGULATOR_ACTIVE_DISCHARGE_MASK)
150#define REGULATOR_ACTIVE_DISCHARGE_GET_BITS(x) \
151 (((x) & REGULATOR_ACTIVE_DISCHARGE_MASK) >> REGULATOR_ACTIVE_DISCHARGE_POS)
153#define REGULATOR_BOOT_OFF BIT(4)
158#define REGULATOR_INITIAL_MODE_UNKNOWN UINT8_MAX
165struct regulator_common_config {
197#define REGULATOR_DT_COMMON_CONFIG_INIT(node_id) \
199 .min_uv = DT_PROP_OR(node_id, regulator_min_microvolt, \
201 .max_uv = DT_PROP_OR(node_id, regulator_max_microvolt, \
203 .init_uv = DT_PROP_OR(node_id, regulator_init_microvolt, \
205 .min_ua = DT_PROP_OR(node_id, regulator_min_microamp, \
207 .max_ua = DT_PROP_OR(node_id, regulator_max_microamp, \
209 .init_ua = DT_PROP_OR(node_id, regulator_init_microamp, \
211 .startup_delay_us = DT_PROP_OR(node_id, startup_delay_us, 0), \
212 .off_on_delay_us = DT_PROP_OR(node_id, off_on_delay_us, 0), \
213 .allowed_modes = (const regulator_mode_t []) \
214 DT_PROP_OR(node_id, regulator_allowed_modes, {}), \
215 .allowed_modes_cnt = \
216 DT_PROP_LEN_OR(node_id, regulator_allowed_modes, 0), \
217 .initial_mode = DT_PROP_OR(node_id, regulator_initial_mode, \
218 REGULATOR_INITIAL_MODE_UNKNOWN), \
219 .flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) * \
220 REGULATOR_ALWAYS_ON) | \
221 (DT_PROP_OR(node_id, regulator_boot_on, 0U) * \
222 REGULATOR_BOOT_ON) | \
223 (REGULATOR_ACTIVE_DISCHARGE_SET_BITS( \
224 DT_PROP_OR(node_id, regulator_active_discharge, \
225 REGULATOR_ACTIVE_DISCHARGE_DEFAULT))) | \
226 (DT_PROP_OR(node_id, regulator_boot_off, 0U) * \
227 REGULATOR_BOOT_OFF)), \
235#define REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst) \
236 REGULATOR_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
243struct regulator_common_data {
244#if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
259void regulator_common_data_init(
const struct device *dev);
283int regulator_common_init(
const struct device *dev,
bool is_enabled);
292static inline bool regulator_common_is_init_enabled(
const struct device *dev)
294 const struct regulator_common_config *config =
295 (
const struct regulator_common_config *)dev->
config;
297 return (config->flags & REGULATOR_INIT_ENABLED) != 0U;
309static inline int regulator_common_get_min_voltage(
const struct device *dev,
int32_t *min_uv)
311 const struct regulator_common_config *config =
312 (
const struct regulator_common_config *)dev->
config;
318 *min_uv = config->min_uv;
331static inline int regulator_common_get_max_voltage(
const struct device *dev,
int32_t *max_uv)
333 const struct regulator_common_config *config =
334 (
const struct regulator_common_config *)dev->
config;
340 *max_uv = config->max_uv;
374 const struct regulator_parent_driver_api *api =
375 (
const struct regulator_parent_driver_api *)dev->
api;
377 if (api->dvs_state_set ==
NULL) {
381 return api->dvs_state_set(dev,
state);
400 const struct regulator_parent_driver_api *api =
401 (
const struct regulator_parent_driver_api *)dev->
api;
403 if (api->ship_mode ==
NULL) {
407 return api->ship_mode(dev);
469 const struct regulator_driver_api *api =
470 (
const struct regulator_driver_api *)dev->
api;
472 if (api->count_voltages ==
NULL) {
476 return api->count_voltages(dev);
495 unsigned int idx,
int32_t *volt_uv)
497 const struct regulator_driver_api *api =
498 (
const struct regulator_driver_api *)dev->
api;
500 if (api->list_voltage ==
NULL) {
504 return api->list_voltage(dev, idx, volt_uv);
554 const struct regulator_driver_api *api =
555 (
const struct regulator_driver_api *)dev->
api;
557 if (api->get_voltage ==
NULL) {
561 return api->get_voltage(dev, volt_uv);
577 const struct regulator_driver_api *api =
578 (
const struct regulator_driver_api *)dev->
api;
580 if (api->count_current_limits ==
NULL) {
584 return api->count_current_limits(dev);
603 unsigned int idx,
int32_t *current_ua)
605 const struct regulator_driver_api *api =
606 (
const struct regulator_driver_api *)dev->
api;
608 if (api->list_current_limit ==
NULL) {
612 return api->list_current_limit(dev, idx, current_ua);
648 const struct regulator_driver_api *api =
649 (
const struct regulator_driver_api *)dev->
api;
651 if (api->get_current_limit ==
NULL) {
655 return api->get_current_limit(dev, curr_ua);
689 const struct regulator_driver_api *api =
690 (
const struct regulator_driver_api *)dev->
api;
692 if (api->get_mode ==
NULL) {
696 return api->get_mode(dev, mode);
710 bool active_discharge)
712 const struct regulator_driver_api *api =
713 (
const struct regulator_driver_api *)dev->
api;
715 if (api->set_active_discharge ==
NULL) {
719 return api->set_active_discharge(dev, active_discharge);
733 bool *active_discharge)
735 const struct regulator_driver_api *api =
736 (
const struct regulator_driver_api *)dev->
api;
738 if (api->get_active_discharge ==
NULL) {
742 return api->get_active_discharge(dev, active_discharge);
758 const struct regulator_driver_api *api =
759 (
const struct regulator_driver_api *)dev->
api;
761 if (api->get_error_flags ==
NULL) {
765 return api->get_error_flags(dev,
flags);
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:575
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:732
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:494
static int regulator_get_current_limit(const struct device *dev, int32_t *curr_ua)
Get output current limit.
Definition regulator.h:645
static int regulator_set_active_discharge(const struct device *dev, bool active_discharge)
Set active discharge setting.
Definition regulator.h:709
static int regulator_get_error_flags(const struct device *dev, regulator_error_flags_t *flags)
Get active error flags.
Definition regulator.h:755
static unsigned int regulator_count_voltages(const struct device *dev)
Obtain the number of supported voltage levels.
Definition regulator.h:467
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:686
static int regulator_get_voltage(const struct device *dev, int32_t *volt_uv)
Obtain output voltage.
Definition regulator.h:551
int regulator_disable(const struct device *dev)
Disable a regulator.
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:602
static int regulator_parent_dvs_state_set(const struct device *dev, regulator_dvs_state_t state)
Set a DVS state.
Definition regulator.h:371
static int regulator_parent_ship_mode(const struct device *dev)
Enter ship mode.
Definition regulator.h:398
#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
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:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
const void * config
Address of device instance config information.
Definition device.h:514
Mutex Structure.
Definition kernel.h:3159