1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
4  */
5 
6 #include "hal_conf.h"
7 
8 /** @addtogroup RK_HAL_Driver
9  *  @{
10  */
11 
12 /** @addtogroup PWR
13  *  @{
14  */
15 
16 #ifndef _HAL_PWR_H_
17 #define _HAL_PWR_H_
18 
19 #include "hal_def.h"
20 
21 /** @defgroup PWR_Exported_Definition_Group1 Basic Definition
22  *  @{
23  */
24 
25 /***************************** MACRO Definition ******************************/
26 
27 typedef enum {
28     PWR_ID_NULL = 0,
29     PWR_ID_CORE,
30     PWR_ID_LOG,
31     PWR_ID_DSP_CORE,
32     PWR_ID_VCC_MIPI,
33     PWR_ID_VCC_AUDIO,
34     PWR_ID_DSP_VCC_MIPI,
35     PWR_ID_MEMORY, /**< for share memory and sram */
36     PWR_ID_TOP,
37     PWR_ID_BUCK_1V8, /**< Usually as a transition between high and low voltage */
38     PWR_ID_VCCIO_3V3,
39     PWR_ID_VDD_1V1,
40     PWR_ID_VCCIO_1V8,
41     PWR_ID_VCCIO_1V8_PMU,
42     PWR_ID_VCC_3V0_BL,
43     PWR_ID_VCC_1V8_BUCK,
44     PWR_ID_VCC_1V8_LDO,
45     PWR_ID_VCC33_CODEC,
46     PWR_ID_VCC33_AUDIO,
47     PWR_ID_VCC1,
48     PWR_ID_VLDO3,
49     PWR_ID_VLDO6,
50     PWR_ID_MAX,
51 } ePWR_ID;
52 
53 typedef enum {
54     PWR_CTRL_VOLT_RUN = 0, /**< run mode voltage */
55     PWR_CTRL_VOLT_SSPD, /**< suspend mode voltage */
56     PWR_CTRL_PWR_EN, /**< enable a regulator */
57     PWR_CTRL_PWR_SSPD, /**< suspend mode enable a regulator */
58     PWR_CTRL_VOLT_ST, /**< get a regulator state */
59     PWR_CTRL_MAX
60 } ePWR_CtrlType;
61 
62 #define PWR_FLG_VOLT_RUN  HAL_BIT(PWR_CTRL_VOLT_RUN)
63 #define PWR_FLG_VOLT_SSPD HAL_BIT(PWR_CTRL_VOLT_SSPD)
64 #define PWR_FLG_PWR_EN    HAL_BIT(PWR_CTRL_PWR_EN)
65 #define PWR_FLG_PWR_SSPD  HAL_BIT(PWR_CTRL_PWR_SSPD)
66 #define PWR_FLG_VOLT_ST   HAL_BIT(PWR_CTRL_VOLT_ST)
67 #define PWR_FLG_LINEAR    HAL_BIT(PWR_CTRL_MAX)
68 #define PWR_FLG_FIXED     (PWR_FLG_LINEAR << 1)
69 #define PWR_FLG_ALWAYSON  (PWR_FLG_FIXED << 1)
70 #define PWR_FLG_ENMASK    (PWR_FLG_ALWAYSON << 1)
71 
72 #define DESC_FLAG_LINEAR(flag)   (PWR_FLG_LINEAR | PWR_FLG_VOLT_RUN | (flag))
73 #define DESC_FLAG_N_LINEAR(flag) (PWR_FLG_VOLT_RUN | (flag))
74 
75 #define PWR_INTREG_SHIFT_RUN(reg, sft)            \
76     .preg[PWR_CTRL_VOLT_RUN] = (uint32_t *)(reg), \
77     .shift[PWR_CTRL_VOLT_RUN] = (sft)
78 
79 #define PWR_INTREG_SHIFT_SSPD(reg, sft)            \
80     .preg[PWR_CTRL_VOLT_SSPD] = (uint32_t *)(reg), \
81     .shift[PWR_CTRL_VOLT_SSPD] = (sft)
82 
83 #define PWR_INTREG_SHIFT_EN(reg, sft)           \
84     .preg[PWR_CTRL_PWR_EN] = (uint32_t *)(reg), \
85     .shift[PWR_CTRL_PWR_EN] = (sft)
86 
87 #define PWR_INTREG_SHIFT_ST(reg, sft)            \
88     .preg[PWR_CTRL_VOLT_ST] = (uint32_t *)(reg), \
89     .shift[PWR_CTRL_VOLT_ST] = (sft)
90 
91 #define PWR_DESC_LINEAR_VOLT(min, max, step) \
92     .voltCnt = (((max)-(min))/(step)) + 1,   \
93     .minVolt = (min),                        \
94     .volt_list = {                           \
95         .stepVolt = (step)                   \
96     }
97 
98 #define POWER_LINEAR_RANGE(_minUV, _minSel, _maxSel, _stepUV) \
99 {                                                             \
100     .minUV = _minUV,                                          \
101     .minSel = _minSel,                                        \
102     .maxSel = _maxSel,                                        \
103     .uVStep = _stepUV,                                        \
104 }
105 
106 /***************************** Structure Definition **************************/
107 struct PWR_LINEAR_RANGE {
108     unsigned int minUV;
109     unsigned int minSel;
110     unsigned int maxSel;
111     unsigned int uVStep;
112 };
113 
114 struct PWR_LINEAR_RANGE_TABLE {
115     int nEntry;
116     struct PWR_LINEAR_RANGE *entry;
117 };
118 
119 union U_PWR_VOLT_LIST {
120     int stepVolt;
121     const uint32_t *voltTable;
122     struct PWR_LINEAR_RANGE_TABLE linearTables;
123 };
124 
125 struct PWR_CTRL_INFO {
126     uint16_t pwrId : 8;
127     uint16_t enCnt : 4;
128 };
129 
130 struct PWR_INTREG_DESC {
131     struct PWR_CTRL_INFO info;
132     uint16_t flag;
133     uint8_t voltMask;
134     uint8_t voltCnt;
135     __IO uint32_t *preg[PWR_CTRL_MAX];
136     uint8_t shift[PWR_CTRL_MAX];
137     uint32_t minVolt;
138     union  U_PWR_VOLT_LIST volt_list;
139 };
140 
141 /** @} */
142 
143 /***************************** Function Declare ******************************/
144 /** @defgroup PWR_Public_Function_Declare Public Function Declare
145  *  @{
146  */
147 #ifdef HAL_PWR_INTBUS_MODULE_ENABLED
148 int HAL_PWR_GetEnableState(struct PWR_INTREG_DESC *desc);
149 uint32_t HAL_PWR_GetVoltage(struct PWR_INTREG_DESC *desc);
150 uint32_t HAL_PWR_GetVoltageSuspend(struct PWR_INTREG_DESC *desc);
151 uint32_t HAL_PWR_GetVoltageReal(struct PWR_INTREG_DESC *desc);
152 HAL_Status HAL_PWR_SetVoltage(struct PWR_INTREG_DESC *desc, uint32_t volt);
153 HAL_Status HAL_PWR_SetVoltageSuspend(struct PWR_INTREG_DESC *desc, uint32_t volt);
154 HAL_Status HAL_PWR_Enable(struct PWR_INTREG_DESC *desc);
155 HAL_Status HAL_PWR_Disable(struct PWR_INTREG_DESC *desc);
156 HAL_Check HAL_PWR_CheckDescByPwrId(struct PWR_INTREG_DESC *pdesc,
157                                    ePWR_ID pwrId);
158 uint32_t HAL_PWR_RoundVoltage(struct PWR_INTREG_DESC *desc, uint32_t volt);
159 #endif
160 
161 int HAL_PWR_LinearRangeSelToVolt(const struct PWR_LINEAR_RANGE_TABLE *linearTables, uint32_t sel);
162 int HAL_PWR_LinearRangeVoltToSel(const struct PWR_LINEAR_RANGE_TABLE *linearTables, uint32_t volt);
163 
164 /** @} */
165 
166 #endif
167 
168 /** @} */
169 
170 /** @} */
171