regulator.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-23 GuEe-GUI first version
  9. */
  10. #ifndef __REGULATOR_H__
  11. #define __REGULATOR_H__
  12. #include <ref.h>
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include <drivers/misc.h>
  16. #define RT_REGULATOR_UVOLT_INVALID (((int)(RT_UINT32_MAX >> 1)))
  17. struct rt_regulator_param
  18. {
  19. const char *name;
  20. int min_uvolt; /* In uV */
  21. int max_uvolt; /* In uV */
  22. int min_uamp; /* In uA */
  23. int max_uamp; /* In uA */
  24. int ramp_delay; /* In uV/usec */
  25. int enable_delay; /* In usec */
  26. int off_on_delay; /* In usec */
  27. rt_uint32_t enable_active_high:1;
  28. rt_uint32_t boot_on:1; /* Is enabled on boot */
  29. rt_uint32_t always_on:1; /* Must be enabled */
  30. rt_uint32_t soft_start:1; /* Ramp voltage slowly */
  31. rt_uint32_t pull_down:1; /* Pull down resistor when regulator off */
  32. rt_uint32_t over_current_protection:1; /* Auto disable on over current */
  33. };
  34. struct rt_regulator_ops;
  35. struct rt_regulator_node
  36. {
  37. rt_list_t list;
  38. rt_list_t children_nodes;
  39. struct rt_device *dev;
  40. struct rt_regulator_node *parent;
  41. const char *supply_name;
  42. const struct rt_regulator_ops *ops;
  43. struct rt_ref ref;
  44. rt_atomic_t enabled_count;
  45. const struct rt_regulator_param *param;
  46. rt_list_t notifier_nodes;
  47. void *priv;
  48. };
  49. /*
  50. * NOTE: Power regulator control is dangerous work. We don't want non-internal
  51. * consumer could access the power regulator tree without regulator's API. So
  52. * we defined the `rt_regulator` member in core instead of here.
  53. */
  54. struct rt_regulator;
  55. #define RT_REGULATOR_MODE_INVALID 0
  56. #define RT_REGULATOR_MODE_FAST RT_BIT(0)
  57. #define RT_REGULATOR_MODE_NORMAL RT_BIT(1)
  58. #define RT_REGULATOR_MODE_IDLE RT_BIT(2)
  59. #define RT_REGULATOR_MODE_STANDBY RT_BIT(3)
  60. struct rt_regulator_ops
  61. {
  62. rt_err_t (*enable)(struct rt_regulator_node *reg);
  63. rt_err_t (*disable)(struct rt_regulator_node *reg);
  64. rt_bool_t (*is_enabled)(struct rt_regulator_node *reg);
  65. rt_err_t (*set_voltage)(struct rt_regulator_node *reg, int min_uvolt, int max_uvolt);
  66. int (*get_voltage)(struct rt_regulator_node *reg);
  67. rt_err_t (*set_mode)(struct rt_regulator_node *reg, rt_uint32_t mode);
  68. rt_int32_t (*get_mode)(struct rt_regulator_node *reg);
  69. rt_err_t (*set_ramp_delay)(struct rt_regulator_node *reg, int ramp);
  70. rt_uint32_t (*enable_time)(struct rt_regulator_node *reg);
  71. };
  72. struct rt_regulator_notifier;
  73. #define RT_REGULATOR_MSG_ENABLE RT_BIT(0)
  74. #define RT_REGULATOR_MSG_DISABLE RT_BIT(1)
  75. #define RT_REGULATOR_MSG_VOLTAGE_CHANGE RT_BIT(2)
  76. #define RT_REGULATOR_MSG_VOLTAGE_CHANGE_ERR RT_BIT(3)
  77. union rt_regulator_notifier_args
  78. {
  79. struct
  80. {
  81. int old_uvolt;
  82. int min_uvolt;
  83. int max_uvolt;
  84. };
  85. };
  86. typedef rt_err_t (*rt_regulator_notifier_callback)(struct rt_regulator_notifier *notifier,
  87. rt_ubase_t msg, void *data);
  88. struct rt_regulator_notifier
  89. {
  90. rt_list_t list;
  91. struct rt_regulator *regulator;
  92. rt_regulator_notifier_callback callback;
  93. void *priv;
  94. };
  95. rt_err_t rt_regulator_register(struct rt_regulator_node *reg_np);
  96. rt_err_t rt_regulator_unregister(struct rt_regulator_node *reg_np);
  97. rt_err_t rt_regulator_notifier_register(struct rt_regulator *reg,
  98. struct rt_regulator_notifier *notifier);
  99. rt_err_t rt_regulator_notifier_unregister(struct rt_regulator *reg,
  100. struct rt_regulator_notifier *notifier);
  101. struct rt_regulator *rt_regulator_get(struct rt_device *dev, const char *id);
  102. void rt_regulator_put(struct rt_regulator *reg);
  103. rt_err_t rt_regulator_enable(struct rt_regulator *reg);
  104. rt_err_t rt_regulator_disable(struct rt_regulator *reg);
  105. rt_bool_t rt_regulator_is_enabled(struct rt_regulator *reg);
  106. rt_bool_t rt_regulator_is_supported_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt);
  107. rt_err_t rt_regulator_set_voltage(struct rt_regulator *reg, int min_uvolt, int max_uvolt);
  108. int rt_regulator_get_voltage(struct rt_regulator *reg);
  109. rt_err_t rt_regulator_set_mode(struct rt_regulator *reg, rt_uint32_t mode);
  110. rt_int32_t rt_regulator_get_mode(struct rt_regulator *reg);
  111. rt_inline rt_err_t rt_regulator_set_voltage_triplet(struct rt_regulator *reg,
  112. int min_uvolt, int target_uvolt, int max_uvolt)
  113. {
  114. if (!rt_regulator_set_voltage(reg, target_uvolt, max_uvolt))
  115. {
  116. return RT_EOK;
  117. }
  118. return rt_regulator_set_voltage(reg, min_uvolt, max_uvolt);
  119. }
  120. #endif /* __REGULATOR_H__ */