Zephyr Project API 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
103
108typedef int (*regulator_dvs_state_set_t)(const struct device *dev,
110
115typedef int (*regulator_ship_mode_t)(const struct device *dev);
116
126
131typedef int (*regulator_enable_t)(const struct device *dev);
132
137typedef int (*regulator_disable_t)(const struct device *dev);
138
143typedef unsigned int (*regulator_count_voltages_t)(const struct device *dev);
144
149typedef int (*regulator_list_voltage_t)(const struct device *dev,
150 unsigned int idx, int32_t *volt_uv);
151
156typedef int (*regulator_set_voltage_t)(const struct device *dev, int32_t min_uv,
157 int32_t max_uv);
158
163typedef int (*regulator_get_voltage_t)(const struct device *dev,
164 int32_t *volt_uv);
165
170typedef unsigned int (*regulator_count_current_limits_t)(const struct device *dev);
171
176typedef int (*regulator_list_current_limit_t)(const struct device *dev,
177 unsigned int idx, int32_t *current_ua);
178
183typedef int (*regulator_set_current_limit_t)(const struct device *dev,
184 int32_t min_ua, int32_t max_ua);
185
190typedef int (*regulator_get_current_limit_t)(const struct device *dev,
191 int32_t *curr_ua);
192
197typedef int (*regulator_set_mode_t)(const struct device *dev,
198 regulator_mode_t mode);
199
204typedef int (*regulator_get_mode_t)(const struct device *dev,
205 regulator_mode_t *mode);
206
211typedef int (*regulator_set_active_discharge_t)(const struct device *dev,
212 bool active_discharge);
213
218typedef int (*regulator_get_active_discharge_t)(const struct device *dev,
219 bool *active_discharge);
220
226 const struct device *dev, regulator_error_flags_t *flags);
227
232typedef int (*regulator_set_callback_t)(const struct device *dev,
233 regulator_callback_t cb, const void *const user_data);
234
272
274
276
283#define REGULATOR_ALWAYS_ON BIT(0)
285#define REGULATOR_BOOT_ON BIT(1)
287#define REGULATOR_INIT_ENABLED (REGULATOR_ALWAYS_ON | REGULATOR_BOOT_ON)
289#define REGULATOR_ACTIVE_DISCHARGE_MASK GENMASK(3, 2)
291#define REGULATOR_ACTIVE_DISCHARGE_POS 2
293#define REGULATOR_ACTIVE_DISCHARGE_DISABLE 0
295#define REGULATOR_ACTIVE_DISCHARGE_ENABLE 1
297#define REGULATOR_ACTIVE_DISCHARGE_DEFAULT 2
299#define REGULATOR_ACTIVE_DISCHARGE_SET_BITS(x) \
300 (((x) << REGULATOR_ACTIVE_DISCHARGE_POS) & REGULATOR_ACTIVE_DISCHARGE_MASK)
302#define REGULATOR_ACTIVE_DISCHARGE_GET_BITS(x) \
303 (((x) & REGULATOR_ACTIVE_DISCHARGE_MASK) >> REGULATOR_ACTIVE_DISCHARGE_POS)
305#define REGULATOR_BOOT_OFF BIT(4)
306
308
310#define REGULATOR_INITIAL_MODE_UNKNOWN UINT8_MAX
311
317struct regulator_common_config {
319 int32_t min_uv;
321 int32_t max_uv;
323 int32_t init_uv;
325 int32_t min_ua;
327 int32_t max_ua;
329 int32_t init_ua;
331 uint32_t startup_delay_us;
333 uint32_t off_on_delay_us;
335 const regulator_mode_t *allowed_modes;
337 uint8_t allowed_modes_cnt;
339 regulator_mode_t initial_mode;
342};
343
349#define REGULATOR_DT_COMMON_CONFIG_INIT(node_id) \
350 { \
351 .min_uv = DT_PROP_OR(node_id, regulator_min_microvolt, \
352 INT32_MIN), \
353 .max_uv = DT_PROP_OR(node_id, regulator_max_microvolt, \
354 INT32_MAX), \
355 .init_uv = DT_PROP_OR(node_id, regulator_init_microvolt, \
356 INT32_MIN), \
357 .min_ua = DT_PROP_OR(node_id, regulator_min_microamp, \
358 INT32_MIN), \
359 .max_ua = DT_PROP_OR(node_id, regulator_max_microamp, \
360 INT32_MAX), \
361 .init_ua = DT_PROP_OR(node_id, regulator_init_microamp, \
362 INT32_MIN), \
363 .startup_delay_us = DT_PROP_OR(node_id, startup_delay_us, 0), \
364 .off_on_delay_us = DT_PROP_OR(node_id, off_on_delay_us, 0), \
365 .allowed_modes = (const regulator_mode_t []) \
366 DT_PROP_OR(node_id, regulator_allowed_modes, {}), \
367 .allowed_modes_cnt = \
368 DT_PROP_LEN_OR(node_id, regulator_allowed_modes, 0), \
369 .initial_mode = DT_PROP_OR(node_id, regulator_initial_mode, \
370 REGULATOR_INITIAL_MODE_UNKNOWN), \
371 .flags = ((DT_PROP_OR(node_id, regulator_always_on, 0U) * \
372 REGULATOR_ALWAYS_ON) | \
373 (DT_PROP_OR(node_id, regulator_boot_on, 0U) * \
374 REGULATOR_BOOT_ON) | \
375 (REGULATOR_ACTIVE_DISCHARGE_SET_BITS( \
376 DT_PROP_OR(node_id, regulator_active_discharge, \
377 REGULATOR_ACTIVE_DISCHARGE_DEFAULT))) | \
378 (DT_PROP_OR(node_id, regulator_boot_off, 0U) * \
379 REGULATOR_BOOT_OFF)), \
380 }
381
387#define REGULATOR_DT_INST_COMMON_CONFIG_INIT(inst) \
388 REGULATOR_DT_COMMON_CONFIG_INIT(DT_DRV_INST(inst))
389
395struct regulator_common_data {
396#if defined(CONFIG_REGULATOR_THREAD_SAFE_REFCNT) || defined(__DOXYGEN__)
398 struct k_mutex lock;
399#endif
401 int refcnt;
402};
403
411void regulator_common_data_init(const struct device *dev);
412
434int regulator_common_init(const struct device *dev, bool is_enabled);
435
443static inline bool regulator_common_is_init_enabled(const struct device *dev)
444{
445 const struct regulator_common_config *config =
446 (const struct regulator_common_config *)dev->config;
447
448 return (config->flags & REGULATOR_INIT_ENABLED) != 0U;
449}
450
460static inline int regulator_common_get_min_voltage(const struct device *dev, int32_t *min_uv)
461{
462 const struct regulator_common_config *config =
463 (const struct regulator_common_config *)dev->config;
464
465 if (config->min_uv == INT32_MIN) {
466 return -ENOENT;
467 }
468
469 *min_uv = config->min_uv;
470 return 0;
471}
472
482static inline int regulator_common_get_max_voltage(const struct device *dev, int32_t *max_uv)
483{
484 const struct regulator_common_config *config =
485 (const struct regulator_common_config *)dev->config;
486
487 if (config->max_uv == INT32_MAX) {
488 return -ENOENT;
489 }
490
491 *max_uv = config->max_uv;
492 return 0;
493}
494
496
502
521static inline int regulator_parent_dvs_state_set(const struct device *dev,
523{
524 const struct regulator_parent_driver_api *api = DEVICE_API_GET(regulator_parent, dev);
525
526 if (api->dvs_state_set == NULL) {
527 return -ENOSYS;
528 }
529
530 return api->dvs_state_set(dev, state);
531}
532
546static inline int regulator_parent_ship_mode(const struct device *dev)
547{
548 const struct regulator_parent_driver_api *api = DEVICE_API_GET(regulator_parent, dev);
549
550 if (api->ship_mode == NULL) {
551 return -ENOSYS;
552 }
553
554 return api->ship_mode(dev);
555}
556
558
572int regulator_enable(const struct device *dev);
573
582bool regulator_is_enabled(const struct device *dev);
583
599int regulator_disable(const struct device *dev);
600
612static inline unsigned int regulator_count_voltages(const struct device *dev)
613{
614 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
615
616 if (api->count_voltages == NULL) {
617 return 0U;
618 }
619
620 return api->count_voltages(dev);
621}
622
638static inline int regulator_list_voltage(const struct device *dev,
639 unsigned int idx, int32_t *volt_uv)
640{
641 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
642
643 if (api->list_voltage == NULL) {
644 return -EINVAL;
645 }
646
647 return api->list_voltage(dev, idx, volt_uv);
648}
649
660bool regulator_is_supported_voltage(const struct device *dev, int32_t min_uv,
661 int32_t max_uv);
662
680int regulator_set_voltage(const struct device *dev, int32_t min_uv,
681 int32_t max_uv);
682
692static inline int regulator_get_voltage(const struct device *dev,
693 int32_t *volt_uv)
694{
695 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
696
697 if (api->get_voltage == NULL) {
698 return -ENOSYS;
699 }
700
701 return api->get_voltage(dev, volt_uv);
702}
703
715static inline unsigned int regulator_count_current_limits(const struct device *dev)
716{
717 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
718
719 if (api->count_current_limits == NULL) {
720 return 0U;
721 }
722
723 return api->count_current_limits(dev);
724}
725
741static inline int regulator_list_current_limit(const struct device *dev,
742 unsigned int idx, int32_t *current_ua)
743{
744 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
745
746 if (api->list_current_limit == NULL) {
747 return -EINVAL;
748 }
749
750 return api->list_current_limit(dev, idx, current_ua);
751}
752
769int regulator_set_current_limit(const struct device *dev, int32_t min_ua,
770 int32_t max_ua);
771
781static inline int regulator_get_current_limit(const struct device *dev,
782 int32_t *curr_ua)
783{
784 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
785
786 if (api->get_current_limit == NULL) {
787 return -ENOSYS;
788 }
789
790 return api->get_current_limit(dev, curr_ua);
791}
792
808int regulator_set_mode(const struct device *dev, regulator_mode_t mode);
809
819static inline int regulator_get_mode(const struct device *dev,
820 regulator_mode_t *mode)
821{
822 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
823
824 if (api->get_mode == NULL) {
825 return -ENOSYS;
826 }
827
828 return api->get_mode(dev, mode);
829}
830
840static inline int regulator_set_active_discharge(const struct device *dev,
841 bool active_discharge)
842{
843 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
844
845 if (api->set_active_discharge == NULL) {
846 return -ENOSYS;
847 }
848
849 return api->set_active_discharge(dev, active_discharge);
850}
851
861static inline int regulator_get_active_discharge(const struct device *dev,
862 bool *active_discharge)
863{
864 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
865
866 if (api->get_active_discharge == NULL) {
867 return -ENOSYS;
868 }
869
870 return api->get_active_discharge(dev, active_discharge);
871}
872
882static inline int regulator_get_error_flags(const struct device *dev,
884{
885 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
886
887 if (api->get_error_flags == NULL) {
888 return -ENOSYS;
889 }
890
891 return api->get_error_flags(dev, flags);
892}
893
908static inline int regulator_set_callback(const struct device *dev,
910 const void *const user_data)
911{
912 const struct regulator_driver_api *api = DEVICE_API_GET(regulator, dev);
913
914 if (api->set_callback == NULL) {
915 return -ENOSYS;
916 }
917
918 return api->set_callback(dev, cb, user_data);
919}
920
921#ifdef __cplusplus
922}
923#endif
924
926
927#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:1449
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_get_active_discharge_t)(const struct device *dev, bool *active_discharge)
Get active discharge setting.
Definition regulator.h:218
int regulator_enable(const struct device *dev)
Enable a regulator.
int(* regulator_set_voltage_t)(const struct device *dev, int32_t min_uv, int32_t max_uv)
Set the output voltage.
Definition regulator.h:156
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
int(* regulator_list_current_limit_t)(const struct device *dev, unsigned int idx, int32_t *current_ua)
Obtain the value of a current limit given an index.
Definition regulator.h:176
static unsigned int regulator_count_current_limits(const struct device *dev)
Obtain the number of supported current limit levels.
Definition regulator.h:715
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
int(* regulator_set_active_discharge_t)(const struct device *dev, bool active_discharge)
Set active discharge setting.
Definition regulator.h:211
int(* regulator_get_current_limit_t)(const struct device *dev, int32_t *curr_ua)
Get output current limit.
Definition regulator.h:190
int(* regulator_get_mode_t)(const struct device *dev, regulator_mode_t *mode)
Get mode.
Definition regulator.h:204
static int regulator_get_active_discharge(const struct device *dev, bool *active_discharge)
Get active discharge setting.
Definition regulator.h:861
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:638
unsigned int(* regulator_count_current_limits_t)(const struct device *dev)
Obtain the number of supported current limit levels.
Definition regulator.h:170
static int regulator_get_current_limit(const struct device *dev, int32_t *curr_ua)
Get output current limit.
Definition regulator.h:781
static int regulator_set_active_discharge(const struct device *dev, bool active_discharge)
Set active discharge setting.
Definition regulator.h:840
static int regulator_get_error_flags(const struct device *dev, regulator_error_flags_t *flags)
Get active error flags.
Definition regulator.h:882
int(* regulator_get_error_flags_t)(const struct device *dev, regulator_error_flags_t *flags)
Get active error flags.
Definition regulator.h:225
static unsigned int regulator_count_voltages(const struct device *dev)
Obtain the number of supported voltage levels.
Definition regulator.h:612
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:819
static int regulator_get_voltage(const struct device *dev, int32_t *volt_uv)
Obtain output voltage.
Definition regulator.h:692
int(* regulator_set_mode_t)(const struct device *dev, regulator_mode_t mode)
Set mode.
Definition regulator.h:197
int regulator_disable(const struct device *dev)
Disable a regulator.
int(* regulator_get_voltage_t)(const struct device *dev, int32_t *volt_uv)
Obtain output voltage.
Definition regulator.h:163
int(* regulator_set_current_limit_t)(const struct device *dev, int32_t min_ua, int32_t max_ua)
Set output current limit.
Definition regulator.h:183
int(* regulator_ship_mode_t)(const struct device *dev)
Enter ship mode.
Definition regulator.h:115
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:908
int(* regulator_dvs_state_set_t)(const struct device *dev, regulator_dvs_state_t state)
@def_driverbackendgroup{Regulator,regulator_interface}
Definition regulator.h:108
int(* regulator_disable_t)(const struct device *dev)
Disable a regulator.
Definition regulator.h:137
int(* regulator_enable_t)(const struct device *dev)
Enable a regulator.
Definition regulator.h:131
unsigned int(* regulator_count_voltages_t)(const struct device *dev)
Obtain the number of supported voltage levels.
Definition regulator.h:143
int(* regulator_set_callback_t)(const struct device *dev, regulator_callback_t cb, const void *const user_data)
Set event handler function.
Definition regulator.h:232
int(* regulator_list_voltage_t)(const struct device *dev, unsigned int idx, int32_t *volt_uv)
Obtain the value of a voltage given an index.
Definition regulator.h:149
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:741
@ 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:521
static int regulator_parent_ship_mode(const struct device *dev)
Enter ship mode.
Definition regulator.h:546
#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
#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 * config
Address of device instance config information.
Definition device.h:517
@driver_ops{Regulator}
Definition regulator.h:238
regulator_set_current_limit_t set_current_limit
@driver_ops_optional Set output current limit.
Definition regulator.h:256
regulator_list_current_limit_t list_current_limit
@driver_ops_optional Obtain the value of a current limit given an index.
Definition regulator.h:254
regulator_enable_t enable
@driver_ops_optional Enable a regulator.
Definition regulator.h:240
regulator_set_voltage_t set_voltage
@driver_ops_optional Set the output voltage.
Definition regulator.h:248
regulator_count_voltages_t count_voltages
@driver_ops_optional Obtain the number of supported voltage levels.
Definition regulator.h:244
regulator_get_error_flags_t get_error_flags
@driver_ops_optional Get active error flags.
Definition regulator.h:268
regulator_list_voltage_t list_voltage
@driver_ops_optional Obtain the value of a voltage given an index.
Definition regulator.h:246
regulator_set_active_discharge_t set_active_discharge
@driver_ops_optional Set active discharge setting.
Definition regulator.h:264
regulator_get_mode_t get_mode
@driver_ops_optional Get mode.
Definition regulator.h:262
regulator_set_callback_t set_callback
@driver_ops_optional Set event handler function.
Definition regulator.h:270
regulator_set_mode_t set_mode
@driver_ops_optional Set mode.
Definition regulator.h:260
regulator_get_active_discharge_t get_active_discharge
@driver_ops_optional Get active discharge setting.
Definition regulator.h:266
regulator_get_voltage_t get_voltage
@driver_ops_optional Obtain output voltage.
Definition regulator.h:250
regulator_disable_t disable
@driver_ops_optional Disable a regulator.
Definition regulator.h:242
regulator_get_current_limit_t get_current_limit
@driver_ops_optional Get output current limit.
Definition regulator.h:258
regulator_count_current_limits_t count_current_limits
@driver_ops_optional Obtain the number of supported current limit levels.
Definition regulator.h:252
Regulator event structure.
Definition regulator.h:83
enum regulator_event_type type
Event type.
Definition regulator.h:85
@driver_ops{Regulator Parent}
Definition regulator.h:120
regulator_dvs_state_set_t dvs_state_set
@driver_ops_optional Set a DVS state.
Definition regulator.h:122
regulator_ship_mode_t ship_mode
@driver_ops_optional Enter ship mode.
Definition regulator.h:124
Macro utilities.