1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef REGULATOR_CORE_H 10 #define REGULATOR_CORE_H 11 12 #include "hdf_base.h" 13 #include "hdf_device_desc.h" 14 #include "osal_mutex.h" 15 #include "regulator_if.h" 16 17 18 #ifdef __cplusplus 19 #if __cplusplus 20 extern "C" { 21 #endif 22 #endif /* __cplusplus */ 23 24 #ifdef REGULATOR_PRINTK_OPEN 25 #define REGULATOR_PRINT_LOG_DBG(fmt, arg...) \ 26 dprintf("[HDF]-[REGULATOR]:[%s][%d]---" fmt "\r\n", __func__, __LINE__, ##arg) 27 #define REGULATOR_PRINT_LOG_INF(fmt, arg...) \ 28 dprintf("[HDF]-[REGULATOR]:[%s][%d]---" fmt "\r\n", __func__, __LINE__, ##arg) 29 #define REGULATOR_PRINT_LOG_ERR(fmt, arg...) \ 30 dprintf("[HDF]-[REGULATOR]:[%s][%d]---" fmt "\r\n", __func__, __LINE__, ##arg) 31 #else 32 #define REGULATOR_PRINT_LOG_DBG(fmt, arg...) HDF_LOGD_WRAPPER("[HDF]-[REGULATOR]:" fmt "\r\n", ##arg) 33 #define REGULATOR_PRINT_LOG_INF(fmt, arg...) HDF_LOGI_WRAPPER("[HDF]-[REGULATOR]:" fmt "\r\n", ##arg) 34 #define REGULATOR_PRINT_LOG_ERR(fmt, arg...) HDF_LOGE_WRAPPER("[HDF]-[REGULATOR]:" fmt "\r\n", ##arg) 35 #endif 36 37 #define CHECK_NULL_PTR_RETURN_VALUE(ptr, ret) do { \ 38 if ((ptr) == NULL) { \ 39 REGULATOR_PRINT_LOG_ERR("%s:line %d pointer is null and return ret", __func__, __LINE__); \ 40 return (ret); \ 41 } \ 42 } while (0) 43 44 #define CHECK_NULL_PTR_RETURN(ptr) do { \ 45 if ((ptr) == NULL) { \ 46 REGULATOR_PRINT_LOG_ERR("%s:line %d pointer is null and return", __func__, __LINE__); \ 47 return; \ 48 } \ 49 } while (0) 50 51 #define CHECK_PARSER_RESULT_RETURN_VALUE(ret, str) do { \ 52 if ((ret) != HDF_SUCCESS) { \ 53 REGULATOR_PRINT_LOG_ERR("%s:line %d %s fail, ret = %d!", __func__, __LINE__, (str), (ret)); \ 54 return HDF_FAILURE; \ 55 } \ 56 } while (0) 57 58 struct RegulatorStatusChangeInfo { 59 const char *name; 60 uint32_t status; 61 }; 62 /** 63 * @brief Defines a callback that will be invoked when a regulator's status change. 64 * 65 */ 66 typedef int32_t (*RegulatorStatusChangecb)(struct RegulatorStatusChangeInfo *); 67 68 struct RegulatorConstraints { 69 uint8_t alwaysOn; /* the regulator is alwaysOn */ 70 uint8_t mode; /* mode: voltage or current */ 71 uint32_t minUv; /* min voltage that can be set by the regulator */ 72 uint32_t maxUv; /* max voltage that can be set by the regulator */ 73 uint32_t minUa; /* min current that can be set by the regulator */ 74 uint32_t maxUa; /* max current that can be set by the regulator */ 75 }; 76 77 struct RegulatorDesc { 78 const char *name; /* regulator name */ 79 const char *parentName; /* regulator parent name */ 80 struct RegulatorConstraints constraints; /* the regulator constraint info */ 81 uint32_t minUv; /* min voltage */ 82 uint32_t maxUv; /* min voltage */ 83 uint32_t minUa; /* min current */ 84 uint32_t maxUa; /* max current */ 85 uint32_t status; /* the regulator's status, on or off */ 86 int useCount; 87 int consumerRegNums; /* the regulator's consumer nums */ 88 RegulatorStatusChangecb cb; /* when regulator status change, can notify by call cb */ 89 }; 90 91 struct RegulatorNode { 92 struct RegulatorDesc regulatorInfo; 93 struct DListHead node; 94 struct RegulatorMethod *ops; 95 void *priv; 96 struct OsalMutex lock; 97 }; 98 99 struct RegulatorMethod { 100 int32_t (*open)(struct RegulatorNode *node); 101 int32_t (*close)(struct RegulatorNode *node); 102 int32_t (*release)(struct RegulatorNode *node); 103 int32_t (*enable)(struct RegulatorNode *node); 104 int32_t (*disable)(struct RegulatorNode *node); 105 int32_t (*forceDisable)(struct RegulatorNode *node); 106 int32_t (*setVoltage)(struct RegulatorNode *node, uint32_t minUv, uint32_t maxUv); 107 int32_t (*getVoltage)(struct RegulatorNode *node, uint32_t *voltage); 108 int32_t (*setCurrent)(struct RegulatorNode *node, uint32_t minUa, uint32_t maxUa); 109 int32_t (*getCurrent)(struct RegulatorNode *node, uint32_t *regCurrent); 110 int32_t (*getStatus)(struct RegulatorNode *node, uint32_t *status); 111 }; 112 113 /** 114 * @brief Find and return a regulator controller by regulator info 115 * @param info Indicates regulator info. 116 * @return a regulator controller 117 */ 118 struct RegulatorNode *RegulatorNodeOpen(const char *name); 119 int32_t RegulatorNodeClose(struct RegulatorNode *node); 120 /** 121 * @brief add a regulator controller to manager list 122 * @param node Indicates a regulator controller. 123 * @constraints: first add parent, then add child 124 * @return success or fail 125 */ 126 int32_t RegulatorNodeAdd(struct RegulatorNode *node); 127 int32_t RegulatorNodeRemove(const char *name); 128 /** 129 * @brief remove all regulator controllers 130 * @param 131 * @return success or fail 132 */ 133 int32_t RegulatorNodeRemoveAll(void); 134 /** 135 * @brief enable a regulator 136 * @param node Indicates a regulator controller. 137 * @return success or fail 138 */ 139 int32_t RegulatorNodeEnable(struct RegulatorNode *node); 140 /** 141 * @brief disable a regulator if it's downstream node all close, if alwayson is true,forbid disable 142 * @param node Indicates a regulator controller. 143 * @return success or fail 144 */ 145 int32_t RegulatorNodeDisable(struct RegulatorNode *node); 146 /** 147 * @brief disable a regulator regardless of whether there is an downstream node 148 * @param node Indicates a regulator controller. 149 * @return success or fail 150 */ 151 int32_t RegulatorNodeForceDisable(struct RegulatorNode *node); 152 /** 153 * @brief set regulator voltage 154 * @param node Indicates a regulator controller. 155 * @param minUv min voltage 156 * @param maxUv max voltage 157 * @return success or fail 158 */ 159 int32_t RegulatorNodeSetVoltage(struct RegulatorNode *node, uint32_t minUv, uint32_t maxUv); 160 /** 161 * @brief get regulator voltage 162 * @param node Indicates a regulator controller. 163 * @param voltage regulator voltage 164 * @return success or fail 165 */ 166 int32_t RegulatorNodeGetVoltage(struct RegulatorNode *node, uint32_t *voltage); 167 /** 168 * @brief set regulator current 169 * @param node Indicates a regulator controller. 170 * @param minUa min current 171 * @param maxUa max current 172 * @return success or fail 173 */ 174 int32_t RegulatorNodeSetCurrent(struct RegulatorNode *node, uint32_t minUA, uint32_t maxUA); 175 /** 176 * @brief get regulator current 177 * @param node Indicates a regulator controller. 178 * @param current regulator current 179 * @return success or fail 180 */ 181 int32_t RegulatorNodeGetCurrent(struct RegulatorNode *node, uint32_t *regCurrent); 182 /** 183 * @brief get regulator status 184 * @param node Indicates a regulator controller. 185 * @param status regulator status,enable or disable 186 * @return success or fail 187 */ 188 int32_t RegulatorNodeGetStatus(struct RegulatorNode *node, uint32_t *status); 189 /** 190 * @brief register regulator's callback when it's status change 191 * @param node Indicates a regulator controller. 192 * @param cb callback function 193 * @return success or fail 194 */ 195 int32_t RegulatorNodeRegisterStatusChangeCb(struct RegulatorNode *node, RegulatorStatusChangecb cb); 196 int32_t RegulatorNodeStatusCb(struct RegulatorNode *node); 197 void RegulatorNodeListPrint(void); 198 int32_t RegulatorTreeInfoInit(struct RegulatorNode *node); 199 200 #ifdef __cplusplus 201 #if __cplusplus 202 } 203 #endif 204 #endif /* __cplusplus */ 205 206 #endif /* REGULATOR_CORE_H */ 207