1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef __PMU_H__ 16 #define __PMU_H__ 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #include <stdint.h> 23 #include "hal_analogif.h" 24 #include "hal_cmu.h" 25 #include "hal_gpio.h" 26 #include "plat_addr_map.h" 27 #include CHIP_SPECIFIC_HDR(pmu) 28 29 #define PMU_DEBUG_TRACE(n, s, ...) TR_DUMMY(n, s, ##__VA_ARGS__) 30 #define PMU_INFO_TRACE_IMM(n, s, ...) TR_INFO((n) | TR_ATTR_IMM, s, ##__VA_ARGS__) 31 #define PMU_INFO_TRACE(n, s, ...) TR_INFO(n, s, ##__VA_ARGS__) 32 33 #ifndef ISPI_PMU_REG 34 #define ISPI_PMU_REG(reg) (reg) 35 #endif 36 #define pmu_read(reg, val) hal_analogif_reg_read(ISPI_PMU_REG(reg), val) 37 #define pmu_write(reg, val) hal_analogif_reg_write(ISPI_PMU_REG(reg), val) 38 #define wifi_read(reg, val) hal_analogif_reg_read(ISPI_WFRF_REG(reg), val) 39 #define wifi_write(reg, val) hal_analogif_reg_write(ISPI_WFRF_REG(reg), val) 40 41 #define PMU_MANUAL_MODE 1 42 #define PMU_AUTO_MODE 0 43 #define PMU_LDO_ON 1 44 #define PMU_LDO_OFF 0 45 #define PMU_LP_MODE_ON 1 46 #define PMU_LP_MODE_OFF 0 47 #define PMU_DSLEEP_MODE_ON 1 48 #define PMU_DSLEEP_MODE_OFF 0 49 50 #ifndef SECURITY_VALUE_T 51 union SECURITY_VALUE_T { 52 struct { 53 unsigned short security_en :1; 54 unsigned short key_id :3; 55 unsigned short key_chksum :2; 56 unsigned short vendor_id :6; 57 unsigned short vendor_chksum :3; 58 unsigned short chksum :1; 59 }; 60 unsigned short reg; 61 }; 62 #endif 63 64 enum PMU_CHARGER_STATUS_T { 65 PMU_CHARGER_PLUGIN, 66 PMU_CHARGER_PLUGOUT, 67 PMU_CHARGER_UNKNOWN, 68 }; 69 70 enum PMU_POWER_ON_CAUSE_T { 71 PMU_POWER_ON_CAUSE_NONE, 72 PMU_POWER_ON_CAUSE_POWER_KEY, 73 PMU_POWER_ON_CAUSE_RTC, 74 PMU_POWER_ON_CAUSE_CHARGER_ACON, 75 PMU_POWER_ON_CAUSE_CHARGER_ACOFF, 76 }; 77 78 enum PMU_USB_PIN_CHK_STATUS_T { 79 PMU_USB_PIN_CHK_NONE, 80 // Chip acts as host 81 PMU_USB_PIN_CHK_DEV_CONN, 82 // Chip acts as host 83 PMU_USB_PIN_CHK_DEV_DISCONN, 84 // Chip acts as device 85 PMU_USB_PIN_CHK_HOST_RESUME, 86 87 PMU_USB_PIN_CHK_STATUS_QTY 88 }; 89 90 enum PMU_USB_CONFIG_TYPE_T { 91 PMU_USB_CONFIG_TYPE_NONE, 92 PMU_USB_CONFIG_TYPE_DEVICE, 93 PMU_USB_CONFIG_TYPE_HOST, 94 }; 95 96 enum PMU_POWER_MODE_T { 97 PMU_POWER_MODE_NONE, 98 PMU_POWER_MODE_LDO, 99 PMU_POWER_MODE_ANA_DCDC, 100 PMU_POWER_MODE_DIG_DCDC, 101 }; 102 103 enum PMU_VIORISE_REQ_USER_T { 104 PMU_VIORISE_REQ_USER_PWL0, 105 PMU_VIORISE_REQ_USER_PWL1, 106 PMU_VIORISE_REQ_USER_FLASH, 107 PMU_VIORISE_REQ_USER_CHARGER, 108 109 PMU_VIORISE_REQ_USER_QTY 110 }; 111 112 struct PMU_LED_BR_CFG_T { 113 uint16_t off_time_ms; 114 uint16_t on_time_ms; 115 uint16_t fade_time_ms; 116 }; 117 118 typedef void (*PMU_USB_PIN_CHK_CALLBACK)(enum PMU_USB_PIN_CHK_STATUS_T status); 119 120 typedef void (*PMU_RTC_IRQ_HANDLER_T)(uint32_t seconds); 121 122 typedef void (*PMU_CHARGER_IRQ_HANDLER_T)(enum PMU_CHARGER_STATUS_T status); 123 124 typedef void (*PMU_WDT_IRQ_HANDLER_T)(void); 125 126 typedef void (*PMU_IRQ_UNIFIED_HANDLER_T)(uint16_t irq_status); 127 128 int pmu_open(void); 129 130 void pmu_sleep(void); 131 132 void pmu_wakeup(void); 133 134 void pmu_mode_change(enum PMU_POWER_MODE_T mode); 135 136 int pmu_get_security_value(union SECURITY_VALUE_T *val); 137 138 void pmu_shutdown(void); 139 140 void pmu_reboot(void); 141 142 int pmu_get_efuse(enum PMU_EFUSE_PAGE_T page, unsigned short *efuse); 143 144 void pmu_codec_config(int enable); 145 146 void pmu_anc_config(int enable); 147 148 void pmu_usb_config(enum PMU_USB_CONFIG_TYPE_T type); 149 150 void pmu_sleep_en(unsigned char sleep_en); 151 152 void pmu_flash_write_config(void); 153 154 void pmu_flash_read_config(void); 155 156 void pmu_flash_freq_config(uint32_t freq); 157 158 void pmu_psram_freq_config(uint32_t freq); 159 160 void pmu_fir_high_speed_config(int enable); 161 162 void pmu_fir_freq_config(uint32_t freq); 163 164 void pmu_iir_freq_config(uint32_t freq); 165 166 void pmu_iir_eq_freq_config(uint32_t freq); 167 168 void pmu_rs_freq_config(uint32_t freq); 169 170 void pmu_rs_adc_freq_config(uint32_t freq); 171 172 void pmu_sys_freq_config(enum HAL_CMU_FREQ_T freq); 173 174 void pmu_high_performance_mode_enable(bool enable); 175 176 void pmu_charger_init(void); 177 178 void pmu_charger_set_irq_handler(PMU_CHARGER_IRQ_HANDLER_T handler); 179 180 void pmu_charger_plugin_config(void); 181 182 void pmu_charger_plugout_config(void); 183 184 enum PMU_CHARGER_STATUS_T pmu_charger_get_status(void); 185 186 int pmu_usb_config_pin_status_check(enum PMU_USB_PIN_CHK_STATUS_T status, PMU_USB_PIN_CHK_CALLBACK callback, int enable); 187 188 void pmu_usb_enable_pin_status_check(void); 189 190 void pmu_usb_disable_pin_status_check(void); 191 192 void pmu_usb_get_pin_status(int *dp, int *dm); 193 194 void pmu_rtc_enable(void); 195 196 void pmu_rtc_disable(void); 197 198 int pmu_rtc_enabled(void); 199 200 void pmu_rtc_set(uint32_t seconds); 201 202 uint32_t pmu_rtc_get(void); 203 204 void pmu_rtc_set_alarm(uint32_t seconds); 205 206 uint32_t pmu_rtc_get_alarm(void); 207 208 void pmu_rtc_clear_alarm(void); 209 210 int pmu_rtc_alarm_status_set(void); 211 212 int pmu_rtc_alarm_alerted(); 213 214 void pmu_rtc_set_irq_handler(PMU_RTC_IRQ_HANDLER_T handler); 215 216 void pmu_viorise_req(enum PMU_VIORISE_REQ_USER_T user, bool rise); 217 218 enum PMU_POWER_ON_CAUSE_T pmu_get_power_on_cause(void); 219 220 int pmu_debug_config_ana(uint16_t volt); 221 222 int pmu_debug_config_codec(uint16_t volt); 223 224 int pmu_debug_config_audio_output(bool diff); 225 226 int pmu_debug_config_vcrystal(bool on); 227 228 void pmu_debug_reliability_test(int stage); 229 230 void pmu_at_skip_shutdown(bool enable); 231 232 void pmu_led_set_direction(enum HAL_GPIO_PIN_T pin, enum HAL_GPIO_DIR_T dir); 233 234 enum HAL_GPIO_DIR_T pmu_led_get_direction(enum HAL_GPIO_PIN_T pin); 235 236 void pmu_led_set_voltage_domains(enum HAL_IOMUX_PIN_T pin, enum HAL_IOMUX_PIN_VOLTAGE_DOMAINS_T volt); 237 238 void pmu_led_set_pull_select(enum HAL_IOMUX_PIN_T pin, enum HAL_IOMUX_PIN_PULL_SELECT_T pull_sel); 239 240 void pmu_led_set_value(enum HAL_GPIO_PIN_T pin, int data); 241 242 int pmu_led_get_value(enum HAL_GPIO_PIN_T pin); 243 244 void pmu_led_breathing_enable(enum HAL_IOMUX_PIN_T pin, const struct PMU_LED_BR_CFG_T *cfg); 245 246 void pmu_led_breathing_disable(enum HAL_IOMUX_PIN_T pin); 247 248 void pmu_wdt_set_irq_handler(PMU_WDT_IRQ_HANDLER_T handler); 249 250 int pmu_wdt_config(uint32_t irq_ms, uint32_t reset_ms); 251 252 void pmu_wdt_start(void); 253 254 void pmu_wdt_stop(void); 255 256 void pmu_wdt_feed(void); 257 258 void pmu_ntc_capture_enable(void); 259 260 void pmu_ntc_capture_disable(void); 261 262 int pmu_set_irq_unified_handler(enum PMU_IRQ_TYPE_T type, PMU_IRQ_UNIFIED_HANDLER_T hdlr); 263 264 enum HAL_PWRKEY_IRQ_T pmu_pwrkey_irq_value_to_state(uint16_t irq_status); 265 266 //========================================================================= 267 // APIs for internal use only 268 //========================================================================= 269 270 void pmu_wdt_save_context(void); 271 272 void pmu_wdt_restore_context(void); 273 274 void pmu_charger_save_context(void); 275 276 void pmu_charger_shutdown_config(void); 277 278 int pmu_get_gpadc_data_bits(void); 279 280 #ifdef __cplusplus 281 } 282 #endif 283 284 #endif 285 286