rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 MediaTek, Inc. |
| 3 | * Author: Wilma Wu <wilma.wu@mediatek.com> |
| 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/regmap.h> |
| 17 | #include <linux/rtc.h> |
| 18 | #include <linux/sched/clock.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/irqdomain.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/of_address.h> |
| 24 | #include <linux/of_irq.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/mfd/mt6358/core.h> |
| 27 | #if defined(CONFIG_MTK_PMIC_CHIP_MT6358) |
| 28 | #include <linux/mfd/mt6358/registers.h> |
| 29 | #elif defined(CONFIG_MTK_PMIC_CHIP_MT6359) |
| 30 | #include <linux/mfd/mt6359/registers.h> |
| 31 | #elif defined(CONFIG_MTK_PMIC_CHIP_MT6389) |
| 32 | #include <linux/mfd/mt6389/registers.h> |
| 33 | #endif |
| 34 | |
| 35 | |
| 36 | #ifdef pr_fmt |
| 37 | #undef pr_fmt |
| 38 | #endif |
| 39 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 40 | |
| 41 | /* we map HW YEA 0 (2000) to 1968 not 1970 because 2000 is the leap year */ |
| 42 | #define RTC_MIN_YEAR 1968 |
| 43 | |
| 44 | /* |
| 45 | * Reset to default date if RTC time is over 2038/1/19 3:14:7 |
| 46 | * Year (YEA) : 1970 ~ 2037 |
| 47 | * Month (MTH) : 1 ~ 12 |
| 48 | * Day of Month (DOM): 1 ~ 31 |
| 49 | */ |
| 50 | |
| 51 | #define RTC_DEFAULT_YEA 2010 |
| 52 | #define RTC_DEFAULT_MTH 1 |
| 53 | #define RTC_DEFAULT_DOM 1 |
| 54 | |
| 55 | /* Min, Hour, Dom... register offset to RTC_TC_SEC */ |
| 56 | #define RTC_OFFSET_SEC 0 |
| 57 | #define RTC_OFFSET_MIN 1 |
| 58 | #define RTC_OFFSET_HOUR 2 |
| 59 | #define RTC_OFFSET_DOM 3 |
| 60 | #define RTC_OFFSET_DOW 4 |
| 61 | #define RTC_OFFSET_MTH 5 |
| 62 | #define RTC_OFFSET_YEAR 6 |
| 63 | #define RTC_OFFSET_COUNT 7 |
| 64 | |
| 65 | |
| 66 | |
| 67 | #define RTC_DSN_ID 0x580 |
| 68 | #define RTC_BBPU 0x8 |
| 69 | #define RTC_IRQ_STA 0xa |
| 70 | #define RTC_IRQ_EN 0xc |
| 71 | #define RTC_CII_EN 0xe |
| 72 | #define RTC_AL_MASK 0x10 |
| 73 | #define RTC_TC_SEC 0x12 |
| 74 | #define RTC_TC_MIN 0x14 |
| 75 | #define RTC_TC_HOU 0x16 |
| 76 | #define RTC_TC_DOM 0x18 |
| 77 | #define RTC_TC_DOW 0x1a |
| 78 | #define RTC_TC_MTH 0x1c |
| 79 | #define RTC_TC_YEA 0x1e |
| 80 | #define RTC_AL_SEC 0x20 |
| 81 | #define RTC_AL_MIN 0x22 |
| 82 | #define RTC_AL_HOU 0x24 |
| 83 | #define RTC_AL_DOM 0x26 |
| 84 | #define RTC_AL_DOW 0x28 |
| 85 | #define RTC_AL_MTH 0x2a |
| 86 | #define RTC_AL_YEA 0x2c |
| 87 | #define RTC_PDN1 0x34 |
| 88 | #define RTC_PDN2 0x36 |
| 89 | #define RTC_SPAR0 0x38 |
| 90 | #define RTC_SPAR1 0x3a |
| 91 | #define RTC_WRTGR 0x42 |
| 92 | #define RTC_OSC32CON 0x2e |
| 93 | #define RTC_POWERKEY1 0x30 |
| 94 | #define RTC_POWERKEY2 0x32 |
| 95 | #define RTC_PROT 0x3c |
| 96 | #define RTC_DIFF 0x3e |
| 97 | #define RTC_CALI 0x40 |
| 98 | #define RTC_CON 0x44 |
| 99 | |
| 100 | #define RTC_AL_SEC_MASK 0x3f |
| 101 | #define RTC_AL_MIN_MASK 0x3f |
| 102 | #define RTC_AL_HOU_MASK 0x1f |
| 103 | #define RTC_AL_DOM_MASK 0x1f |
| 104 | #define RTC_AL_DOW_MASK 0x7 |
| 105 | #define RTC_AL_MTH_MASK 0xf |
| 106 | #define RTC_AL_YEA_MASK 0x7f |
| 107 | |
| 108 | #define RTC_PWRON_SEC_MASK 0x3f |
| 109 | #define RTC_PWRON_MIN_MASK 0x3f |
| 110 | #define RTC_PWRON_HOU_MASK 0x7c0 |
| 111 | #define RTC_PWRON_DOM_MASK 0xf800 |
| 112 | #define RTC_PWRON_MTH_MASK 0xf |
| 113 | #define RTC_PWRON_YEA_MASK 0x7f00 |
| 114 | #define RTC_PWRON_SEC_SHIFT 0x0 |
| 115 | #define RTC_PWRON_MIN_SHIFT 0x0 |
| 116 | #define RTC_PWRON_HOU_SHIFT 0x6 |
| 117 | #define RTC_PWRON_DOM_SHIFT 0xb |
| 118 | #define RTC_PWRON_MTH_SHIFT 0x0 |
| 119 | #define RTC_PWRON_YEA_SHIFT 0x8 |
| 120 | |
| 121 | #define RTC_IRQ_EN_AL BIT(0) |
| 122 | |
| 123 | /* RTC_BBPU bit field */ |
| 124 | #define PMIC_SPAR_SW_SHIFT 1 |
| 125 | #define PMIC_RESET_SPAR_SHIFT 2 |
| 126 | #define PMIC_RESET_ALARM_SHIFT 3 |
| 127 | |
| 128 | #define RTC_BBPU_KEY 0x4300 |
| 129 | #define RTC_BBPU_CBUSY BIT(6) |
| 130 | #define RTC_BBPU_RELOAD BIT(5) |
| 131 | #define RTC_BBPU_RESET_SPAR BIT(PMIC_RESET_SPAR_SHIFT) |
| 132 | #define RTC_BBPU_RESET_ALARM BIT(PMIC_RESET_ALARM_SHIFT) |
| 133 | #define RTC_BBPU_CLR BIT(1) |
| 134 | #define RTC_BBPU_SPAR_SW BIT(PMIC_SPAR_SW_SHIFT) |
| 135 | #define RTC_BBPU_PWREN BIT(0) |
| 136 | |
| 137 | #define RTC_AL_MASK_DOW BIT(4) |
| 138 | |
| 139 | #define RTC_GPIO_USER_MASK 0x1f00 |
| 140 | #define RTC_PDN1_PWRON_TIME BIT(7) |
| 141 | #define RTC_PDN2_PWRON_LOGO BIT(15) |
| 142 | |
| 143 | #define RTC_CON_F32KOB BIT(5) |
| 144 | |
| 145 | #define RTC_POWERKEY1_KEY 0xa357 |
| 146 | #define RTC_POWERKEY2_KEY 0x67d2 |
| 147 | |
| 148 | #define RTC_OSC32CON_UNLOCK1 0x1a57 |
| 149 | #define RTC_OSC32CON_UNLOCK2 0x2b68 |
| 150 | |
| 151 | #define RTC_EMBCK_SRC_SEL BIT(8) |
| 152 | |
| 153 | #define RTC_K_EOSC_RSV_0 BIT(8) |
| 154 | #define RTC_K_EOSC_RSV_1 BIT(9) |
| 155 | #define RTC_K_EOSC_RSV_2 BIT(10) |
| 156 | |
| 157 | #define RTC_BBPU_2SEC_EN BIT(8) |
| 158 | #define RTC_BBPU_AUTO_PDN_SEL BIT(6) |
| 159 | |
| 160 | #define RTC_EMBCK_SEL_K_EOSC BIT(MT6389_RTC_EMBCK_SEL_MODE_SHIFT) |
| 161 | #define RTC_EMBCK_SEL_OPTION BIT(MT6389_RTC_EMBCK_SEL_OPTION_SHIFT) |
| 162 | #define RTC_GPS_CKOUT_EN BIT(MT6389_RTC_GPS_CKOUT_EN_SHIFT) |
| 163 | #define RTC_EOSC32_VCT_EN BIT(MT6389_RTC_EOSC32_VCT_EN_SHIFT) |
| 164 | #define RTC_EOSC32_CHOP_EN BIT(MT6389_RTC_EOSC32_CHOP_EN_SHIFT) |
| 165 | #define RTC_GP_OSC32_CON (2U << MT6389_RTC_GP_OSC32_CON_SHIFT) |
| 166 | #define RTC_REG_XOSC32_ENB BIT(MT6389_RTC_REG_XOSC32_ENB_SHIFT) |
| 167 | #define OSC32CON_ANALOG_SETTING (RTC_GP_OSC32_CON | RTC_EOSC32_CHOP_EN | \ |
| 168 | RTC_EOSC32_VCT_EN | \ |
| 169 | RTC_GPS_CKOUT_EN | RTC_EMBCK_SEL_OPTION | \ |
| 170 | RTC_EMBCK_SEL_K_EOSC) |
| 171 | |
| 172 | #define RTC_EOSC32_LPEN BIT(MT6389_EOSC32_LPEN_SHIFT) |
| 173 | #define RTC_XOSC32_LPEN BIT(MT6389_XOSC32_LPEN_SHIFT) |
| 174 | #define RTC_CON_LPRST BIT(MT6389_LPRST_SHIFT) |
| 175 | /* 1: GPO mode, 0: GPI mode */ |
| 176 | #define RTC_CON_GOE BIT(MT6389_GOE_SHIFT) |
| 177 | #define RTC_CON_GPEN BIT(MT6389_GPEN_SHIFT) |
| 178 | #define RTC_CON_GPU BIT(MT6389_GPU_SHIFT) |
| 179 | #define RTC_CON_LPSTA_RAW BIT(MT6389_LPSTA_RAW_SHIFT) |
| 180 | |
| 181 | #define RTC_PROT_UNLOCK1 0x586a |
| 182 | #define RTC_PROT_UNLOCK2 0x9136 |
| 183 | |
| 184 | #define RTC_EOSC_CALI_TD_01_SEC 0x3 |
| 185 | #define RTC_EOSC_CALI_TD_02_SEC 0x4 |
| 186 | #define RTC_EOSC_CALI_TD_04_SEC 0x5 |
| 187 | #define RTC_EOSC_CALI_TD_08_SEC 0x6 |
| 188 | #define RTC_EOSC_CALI_TD_16_SEC 0x7 |
| 189 | |
| 190 | #define RTC_LPD_OPT_F32K_CK_ALIVE (3U << MT6389_RTC_LPD_OPT_SHIFT) |
| 191 | #define RTC_K_EOSC32_VTCXO_ON_SEL BIT(MT6389_K_EOSC32_VTCXO_ON_SEL_SHIFT) |
| 192 | |
| 193 | #define RTC_SPAR0_32K_LESS BIT(6) |
| 194 | #define RTC_LPD_OPT_MASK (MT6389_RTC_LPD_OPT_MASK << \ |
| 195 | MT6389_RTC_LPD_OPT_SHIFT) |
| 196 | |
| 197 | #define RG_75K_32K_SEL MT6389_TOP_CKSEL_CON0_SET_ADDR |
| 198 | #define RTC_75K_TO_32K BIT(MT6389_RG_RTC_32K1V8_SEL_SHIFT) |
| 199 | |
| 200 | #define RTC_LPD_OPT_EOSC_LPD BIT(MT6389_RTC_LPD_OPT_SHIFT) |
| 201 | |
| 202 | #define RG_FQMTR_RST MT6389_RG_FQMTR_RST_ADDR |
| 203 | #define RG_FQMTR_TCKSEL MT6389_FQMTR_TCKSEL_ADDR |
| 204 | #define RG_FQMTR_WINSET MT6389_FQMTR_WINSET_ADDR |
| 205 | #define RG_FQMTR_BUSY MT6389_FQMTR_BUSY_ADDR |
| 206 | #define RG_FQMTR_DCXO26M_EN MT6389_FQMTR_DCXO26M_EN_ADDR |
| 207 | #define RG_FQMTR_DATA MT6389_FQMTR_DATA_ADDR |
| 208 | |
| 209 | #define RG_FQMTR_32K_CLK_PDN_SET MT6389_TOP_CKPDN_CON0_SET_ADDR |
| 210 | #define RG_FQMTR_32K_CLK_PDN_CLR MT6389_TOP_CKPDN_CON0_CLR_ADDR |
| 211 | #define RG_FQMTR_32K_CLK_PDN_MASK MT6389_RG_FQMTR_32K_CK_PDN_MASK |
| 212 | #define RG_FQMTR_32K_CLK_PDN_SHIFT MT6389_RG_FQMTR_32K_CK_PDN_SHIFT |
| 213 | |
| 214 | #define FQMTR_CLK_CK_PDN_SET MT6389_TOP_CKPDN_CON0_SET_ADDR |
| 215 | #define FQMTR_CLK_CK_PDN_CLR MT6389_TOP_CKPDN_CON0_CLR_ADDR |
| 216 | #define FQMTR_CLK_CK_PDN_MASK MT6389_RG_FQMTR_CK_PDN_MASK |
| 217 | #define FQMTR_CLK_CK_PDN_SHIFT MT6389_RG_FQMTR_CK_PDN_SHIFT |
| 218 | |
| 219 | #define RG_FQMTR_CKSEL MT6389_RG_FQMTR_CK_CKSEL_ADDR |
| 220 | #define RG_FQMTR_CKSEL_SET MT6389_TOP_CKSEL_CON0_SET_ADDR |
| 221 | #define RG_FQMTR_CKSEL_CLR MT6389_TOP_CKSEL_CON0_CLR_ADDR |
| 222 | #define RG_FQMTR_CKSEL_MASK MT6389_RG_FQMTR_CK_CKSEL_MASK |
| 223 | #define RG_FQMTR_CKSEL_SHIFT MT6389_RG_FQMTR_CK_CKSEL_SHIFT |
| 224 | #define RG_FQMTR_TCKSEL_MASK MT6389_FQMTR_TCKSEL_MASK |
| 225 | #define RG_FQMTR_TCKSEL_SHIFT MT6389_FQMTR_TCKSEL_SHIFT |
| 226 | |
| 227 | #define FQMTR_FIX_CLK_26M (0 << RG_FQMTR_CKSEL_SHIFT) |
| 228 | #define FQMTR_FIX_CLK_XOSC_32K_DET (1 << RG_FQMTR_CKSEL_SHIFT) |
| 229 | #define FQMTR_FIX_CLK_EOSC_32K (2 << RG_FQMTR_CKSEL_SHIFT) |
| 230 | #define FQMTR_FIX_CLK_RTC_32K (3 << RG_FQMTR_CKSEL_SHIFT) |
| 231 | #define FQMTR_FIX_CLK_SMPS_CK (4 << RG_FQMTR_CKSEL_SHIFT) |
| 232 | #define FQMTR_FIX_CLK_TCK_SEC (5 << RG_FQMTR_CKSEL_SHIFT) |
| 233 | #define FQMTR_FIX_CLK_PMU_75K (6 << RG_FQMTR_CKSEL_SHIFT) |
| 234 | |
| 235 | #define FQMTR_RST (1U << MT6389_RG_FQMTR_RST_SHIFT) |
| 236 | #define FQMTR_EN (1U << MT6389_FQMTR_EN_SHIFT) |
| 237 | #define FQMTR_BUSY (1U << MT6389_FQMTR_BUSY_SHIFT) |
| 238 | #define FQMTR_DCXO26M_EN (1U << MT6389_FQMTR_DCXO26M_EN_SHIFT) |
| 239 | #define FQMTR_XOSC32_CK 0 |
| 240 | #define FQMTR_DCXO_F32K_CK 1 |
| 241 | #define FQMTR_EOSC32_CK 2 |
| 242 | #define FQMTR_XOSC32_CK_DETECTON 3 |
| 243 | #define FQMTR_FQM26M_CK 4 |
| 244 | #define FQMTR_FQM32K_CK 5 |
| 245 | #define FQMTR_TEST_CK 6 |
| 246 | #define FQMTR_WINSET 0x0000 |
| 247 | |
| 248 | #define RTC_FQMTR_LOW_BASE (794 - 2) |
| 249 | #define RTC_FQMTR_HIGH_BASE (794 + 2) |
| 250 | |
| 251 | #define RTC_XOSCCALI_START 0x0000 |
| 252 | #define RTC_XOSCCALI_END 0x001f |
| 253 | |
| 254 | /* 0 (32k crystal exist) 1 (32k crystal doesn't exist) */ |
| 255 | #define RTC_XOSC32_ENB (MT6389_RTC_XOSC32_ENB_MASK << \ |
| 256 | MT6389_RTC_XOSC32_ENB_SHIFT) |
| 257 | |
| 258 | #define RTC_BBPU_2SEC_STAT_CLEAR (MT6389_BBPU_2SEC_STAT_CLEAR_MASK << \ |
| 259 | MT6389_BBPU_2SEC_STAT_CLEAR_SHIFT) |
| 260 | /* Default 4'b0111, 2nd step suggest to set to 4'b0000 |
| 261 | * EOSC_CALI = charging cap calibration |
| 262 | */ |
| 263 | #define RTC_XOSCCALI_MASK (MT6389_XOSCCALI_MASK << MT6389_XOSCCALI_SHIFT) |
| 264 | |
| 265 | #define IPIMB |
| 266 | |
| 267 | |
| 268 | enum rtc_spare_enum { |
| 269 | RTC_FGSOC = 0, |
| 270 | RTC_ANDROID, |
| 271 | RTC_FAC_RESET, |
| 272 | RTC_BYPASS_PWR, |
| 273 | RTC_PWRON_TIME, |
| 274 | RTC_FAST_BOOT, |
| 275 | RTC_KPOC, |
| 276 | RTC_DEBUG, |
| 277 | RTC_PWRON_AL, |
| 278 | RTC_UART, |
| 279 | RTC_AUTOBOOT, |
| 280 | RTC_PWRON_LOGO, |
| 281 | RTC_32K_LESS, |
| 282 | RTC_LP_DET, |
| 283 | RTC_FG_INIT, |
| 284 | RTC_SPAR_NUM |
| 285 | }; |
| 286 | |
| 287 | enum rtc_reg_set { |
| 288 | RTC_REG, |
| 289 | RTC_MASK, |
| 290 | RTC_SHIFT |
| 291 | }; |
| 292 | |
| 293 | u16 rtc_spare_reg[RTC_SPAR_NUM][3] = { |
| 294 | {RTC_AL_MTH, 0xff, 8}, |
| 295 | {RTC_PDN1, 0xf, 0}, |
| 296 | {RTC_PDN1, 0x3, 4}, |
| 297 | {RTC_PDN1, 0x1, 6}, |
| 298 | {RTC_PDN1, 0x1, 7}, |
| 299 | {RTC_PDN1, 0x1, 13}, |
| 300 | {RTC_PDN1, 0x1, 14}, |
| 301 | {RTC_PDN1, 0x1, 15}, |
| 302 | {RTC_PDN2, 0x1, 4}, |
| 303 | {RTC_PDN2, 0x3, 5}, |
| 304 | {RTC_PDN2, 0x1, 7}, |
| 305 | {RTC_PDN2, 0x1, 15}, |
| 306 | {RTC_SPAR0, 0x1, 6}, |
| 307 | {RTC_SPAR0, 0x1, 7}, |
| 308 | {RTC_AL_HOU, 0xff, 8} |
| 309 | }; |
| 310 | |
| 311 | /* |
| 312 | * RTC_PDN1: |
| 313 | * bit 0 - 3 : Android bits |
| 314 | * bit 4 - 5 : Recovery bits (0x10: factory data reset) |
| 315 | * bit 6 : Bypass PWRKEY bit |
| 316 | * bit 7 : Power-On Time bit |
| 317 | * bit 8 : reserved bit |
| 318 | * bit 9 : reserved bit |
| 319 | * bit 10 : reserved bit |
| 320 | * bit 11 : reserved bit |
| 321 | * bit 12 : reserved bit |
| 322 | * bit 13 : Fast Boot |
| 323 | * bit 14 : Kernel Power Off Charging |
| 324 | * bit 15 : Debug bit |
| 325 | */ |
| 326 | |
| 327 | /* |
| 328 | * RTC_PDN2: |
| 329 | * bit 0 - 3 : MTH in power-on time |
| 330 | * bit 4 : Power-On Alarm bit |
| 331 | * bit 5 - 6 : UART bits |
| 332 | * bit 7 : POWER DROP AUTO BOOT bit |
| 333 | * bit 8 - 14: YEA in power-on time |
| 334 | * bit 15 : Power-On Logo bit |
| 335 | */ |
| 336 | |
| 337 | /* |
| 338 | * RTC_SPAR0: |
| 339 | * bit 0 - 5 : SEC in power-on time |
| 340 | * bit 6 : 32K less bit. True:with 32K, False:Without 32K |
| 341 | * bit 7 : Low power detected in preloader |
| 342 | * bit 8 - 15: reserved bits |
| 343 | */ |
| 344 | |
| 345 | /* |
| 346 | * RTC_SPAR1: |
| 347 | * bit 0 - 5 : MIN in power-on time |
| 348 | * bit 6 - 10 : HOU in power-on time |
| 349 | * bit 11 - 15: DOM in power-on time |
| 350 | */ |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * RTC_NEW_SPARE0: RTC_AL_HOU bit8~15 |
| 355 | * bit 8 ~ 14 : Fuel Gauge |
| 356 | * bit 15 : reserved bits |
| 357 | */ |
| 358 | |
| 359 | /* |
| 360 | * RTC_NEW_SPARE1: RTC_AL_DOM bit8~15 |
| 361 | * bit 8 ~ 15 : reserved bits |
| 362 | */ |
| 363 | |
| 364 | /* |
| 365 | * RTC_NEW_SPARE2: RTC_AL_DOW bit8~15 |
| 366 | * bit 8 ~ 15 : reserved bits |
| 367 | */ |
| 368 | |
| 369 | /* |
| 370 | * RTC_NEW_SPARE3: RTC_AL_MTH bit8~15 |
| 371 | * bit 8 ~ 15 : reserved bits |
| 372 | */ |
| 373 | |
| 374 | static u16 rtc_alarm_reg[RTC_OFFSET_COUNT][3] = { |
| 375 | {RTC_AL_SEC, RTC_AL_SEC_MASK, 0}, |
| 376 | {RTC_AL_MIN, RTC_AL_MIN_MASK, 0}, |
| 377 | {RTC_AL_HOU, RTC_AL_HOU_MASK, 0}, |
| 378 | {RTC_AL_DOM, RTC_AL_DOM_MASK, 0}, |
| 379 | {RTC_AL_DOW, RTC_AL_DOW_MASK, 0}, |
| 380 | {RTC_AL_MTH, RTC_AL_MTH_MASK, 0}, |
| 381 | {RTC_AL_YEA, RTC_AL_YEA_MASK, 0}, |
| 382 | }; |
| 383 | |
| 384 | static u16 rtc_pwron_reg[RTC_OFFSET_COUNT][3] = { |
| 385 | {RTC_SPAR0, RTC_PWRON_SEC_MASK, RTC_PWRON_SEC_SHIFT}, |
| 386 | {RTC_SPAR1, RTC_PWRON_MIN_MASK, RTC_PWRON_MIN_SHIFT}, |
| 387 | {RTC_SPAR1, RTC_PWRON_HOU_MASK, RTC_PWRON_HOU_SHIFT}, |
| 388 | {RTC_SPAR1, RTC_PWRON_DOM_MASK, RTC_PWRON_DOM_SHIFT}, |
| 389 | {0, 0, 0}, |
| 390 | {RTC_PDN2, RTC_PWRON_MTH_MASK, RTC_PWRON_MTH_SHIFT}, |
| 391 | {RTC_PDN2, RTC_PWRON_YEA_MASK, RTC_PWRON_YEA_SHIFT}, |
| 392 | }; |
| 393 | |
| 394 | struct mt6358_misc { |
| 395 | struct device *dev; |
| 396 | spinlock_t lock; |
| 397 | struct regmap *regmap; |
| 398 | u32 addr_base; |
| 399 | }; |
| 400 | |
| 401 | static struct mt6358_misc *rtc_misc; |
| 402 | #ifdef IPIMB |
| 403 | struct regmap *pmic_regmap; |
| 404 | #endif |
| 405 | |
| 406 | static int rtc_eosc_cali_td = 8; |
| 407 | static int dcxo_switch; |
| 408 | static int eosc_k; |
| 409 | static int support_xosc; |
| 410 | module_param(rtc_eosc_cali_td, int, 0664); |
| 411 | |
| 412 | |
| 413 | static int rtc_read(unsigned int reg, unsigned int *val) |
| 414 | { |
| 415 | return regmap_read(rtc_misc->regmap, rtc_misc->addr_base + reg, val); |
| 416 | } |
| 417 | |
| 418 | static int rtc_write(unsigned int reg, unsigned int val) |
| 419 | { |
| 420 | return regmap_write(rtc_misc->regmap, rtc_misc->addr_base + reg, val); |
| 421 | } |
| 422 | |
| 423 | static int rtc_update_bits(unsigned int reg, unsigned int mask, |
| 424 | unsigned int val) |
| 425 | { |
| 426 | return regmap_update_bits(rtc_misc->regmap, |
| 427 | rtc_misc->addr_base + reg, mask, val); |
| 428 | } |
| 429 | |
| 430 | static int rtc_field_read(unsigned int reg, |
| 431 | unsigned int mask, unsigned int shift, |
| 432 | unsigned int *val) |
| 433 | { |
| 434 | int ret; |
| 435 | unsigned int reg_val; |
| 436 | |
| 437 | ret = rtc_read(reg, ®_val); |
| 438 | if (ret != 0) |
| 439 | return ret; |
| 440 | |
| 441 | reg_val &= mask; |
| 442 | reg_val >>= shift; |
| 443 | *val = reg_val; |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | static int rtc_busy_wait(void) |
| 449 | { |
| 450 | unsigned long long timeout = sched_clock() + 500000000; |
| 451 | int ret; |
| 452 | unsigned int bbpu; |
| 453 | u32 pwrkey1, pwrkey2, sec; |
| 454 | |
| 455 | do { |
| 456 | ret = rtc_read(RTC_BBPU, &bbpu); |
| 457 | if (ret < 0) |
| 458 | break; |
| 459 | if ((bbpu & RTC_BBPU_CBUSY) == 0) |
| 460 | break; |
| 461 | else if (sched_clock() > timeout) { |
| 462 | rtc_read(RTC_BBPU, &bbpu); |
| 463 | rtc_read(RTC_POWERKEY1, &pwrkey1); |
| 464 | rtc_read(RTC_POWERKEY2, &pwrkey2); |
| 465 | rtc_read(RTC_TC_SEC, &sec); |
| 466 | pr_err("%s, wait cbusy timeout, %x, %x, %x, %d\n", |
| 467 | __func__, bbpu, pwrkey1, pwrkey2, sec); |
| 468 | ret = -ETIMEDOUT; |
| 469 | break; |
| 470 | } |
| 471 | } while (1); |
| 472 | |
| 473 | return ret; |
| 474 | } |
| 475 | |
| 476 | static int rtc_write_trigger(void) |
| 477 | { |
| 478 | int ret; |
| 479 | |
| 480 | ret = rtc_write(RTC_WRTGR, 1); |
| 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | |
| 484 | return !rtc_busy_wait(); |
| 485 | } |
| 486 | |
| 487 | static int mtk_rtc_get_spare_register(enum rtc_spare_enum cmd) |
| 488 | { |
| 489 | unsigned int tmp_val = 0; |
| 490 | int ret = -EINVAL; |
| 491 | |
| 492 | if (cmd >= 0 && cmd < RTC_SPAR_NUM) { |
| 493 | |
| 494 | ret = rtc_field_read(rtc_spare_reg[cmd][RTC_REG], |
| 495 | rtc_spare_reg[cmd][RTC_MASK] |
| 496 | << rtc_spare_reg[cmd][RTC_SHIFT], |
| 497 | rtc_spare_reg[cmd][RTC_SHIFT], &tmp_val); |
| 498 | |
| 499 | if (ret < 0) |
| 500 | goto exit; |
| 501 | |
| 502 | pr_notice("%s: cmd[%d], get rg[0x%x, 0x%x , %d] = 0x%x\n", |
| 503 | __func__, cmd, |
| 504 | rtc_spare_reg[cmd][RTC_REG], |
| 505 | rtc_spare_reg[cmd][RTC_MASK], |
| 506 | rtc_spare_reg[cmd][RTC_SHIFT], tmp_val); |
| 507 | |
| 508 | return tmp_val; |
| 509 | } |
| 510 | |
| 511 | exit: |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static void mtk_rtc_set_spare_register(enum rtc_spare_enum cmd, u16 val) |
| 516 | { |
| 517 | u32 tmp_val = 0; |
| 518 | int ret; |
| 519 | |
| 520 | if (cmd >= 0 && cmd < RTC_SPAR_NUM) { |
| 521 | |
| 522 | pr_notice("%s: cmd[%d], set rg[0x%x, 0x%x , %d] = 0x%x\n", |
| 523 | __func__, cmd, |
| 524 | rtc_spare_reg[cmd][RTC_REG], |
| 525 | rtc_spare_reg[cmd][RTC_MASK], |
| 526 | rtc_spare_reg[cmd][RTC_SHIFT], val); |
| 527 | |
| 528 | tmp_val = ((val & rtc_spare_reg[cmd][RTC_MASK]) |
| 529 | << rtc_spare_reg[cmd][RTC_SHIFT]); |
| 530 | ret = rtc_update_bits(rtc_spare_reg[cmd][RTC_REG], |
| 531 | rtc_spare_reg[cmd][RTC_MASK] |
| 532 | << rtc_spare_reg[cmd][RTC_SHIFT], |
| 533 | tmp_val); |
| 534 | if (ret < 0) |
| 535 | goto exit; |
| 536 | ret = rtc_write_trigger(); |
| 537 | if (ret < 0) |
| 538 | goto exit; |
| 539 | } |
| 540 | return; |
| 541 | exit: |
| 542 | pr_err("%s error\n", __func__); |
| 543 | } |
| 544 | |
| 545 | int get_rtc_spare_fg_value(void) |
| 546 | { |
| 547 | /* RTC_AL_HOU bit8~14 */ |
| 548 | u16 temp; |
| 549 | unsigned long flags; |
| 550 | |
| 551 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 552 | temp = mtk_rtc_get_spare_register(RTC_FGSOC); |
| 553 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 554 | |
| 555 | return temp; |
| 556 | } |
| 557 | |
| 558 | int set_rtc_spare_fg_value(int val) |
| 559 | { |
| 560 | /* RTC_AL_HOU bit8~14 */ |
| 561 | unsigned long flags; |
| 562 | |
| 563 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 564 | mtk_rtc_set_spare_register(RTC_FGSOC, val); |
| 565 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int get_rtc_spare0_fg_value(void) |
| 571 | { |
| 572 | u16 temp; |
| 573 | unsigned long flags; |
| 574 | |
| 575 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 576 | temp = mtk_rtc_get_spare_register(RTC_FG_INIT); |
| 577 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 578 | |
| 579 | return temp; |
| 580 | } |
| 581 | |
| 582 | int set_rtc_spare0_fg_value(int val) |
| 583 | { |
| 584 | unsigned long flags; |
| 585 | |
| 586 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 587 | mtk_rtc_set_spare_register(RTC_FG_INIT, val); |
| 588 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | bool crystal_exist_status(void) |
| 594 | { |
| 595 | unsigned long flags; |
| 596 | u16 ret; |
| 597 | |
| 598 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 599 | ret = mtk_rtc_get_spare_register(RTC_32K_LESS); |
| 600 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 601 | |
| 602 | if (ret) |
| 603 | return true; |
| 604 | else |
| 605 | return false; |
| 606 | } |
| 607 | EXPORT_SYMBOL(crystal_exist_status); |
| 608 | |
| 609 | static void mtk_rtc_clear_pwron_alarm(void) |
| 610 | { |
| 611 | u16 data[RTC_OFFSET_COUNT]; |
| 612 | int ret, i; |
| 613 | |
| 614 | pr_notice("%s\n", __func__); |
| 615 | |
| 616 | data[RTC_OFFSET_SEC] = 0; |
| 617 | data[RTC_OFFSET_MIN] = 0; |
| 618 | data[RTC_OFFSET_HOUR] = 0; |
| 619 | data[RTC_OFFSET_DOM] = ((RTC_DEFAULT_DOM << RTC_PWRON_DOM_SHIFT) & |
| 620 | RTC_PWRON_DOM_MASK); |
| 621 | data[RTC_OFFSET_MTH] = ((RTC_DEFAULT_MTH << RTC_PWRON_MTH_SHIFT) & |
| 622 | RTC_PWRON_MTH_MASK); |
| 623 | data[RTC_OFFSET_YEAR] = (((RTC_DEFAULT_YEA - RTC_MIN_YEAR) << |
| 624 | RTC_PWRON_YEA_SHIFT) |
| 625 | & RTC_PWRON_YEA_MASK); |
| 626 | |
| 627 | for (i = RTC_OFFSET_SEC; i < RTC_OFFSET_COUNT; i++) { |
| 628 | if (i == RTC_OFFSET_DOW) |
| 629 | continue; |
| 630 | ret = rtc_update_bits(rtc_pwron_reg[i][RTC_REG], |
| 631 | rtc_pwron_reg[i][RTC_MASK], data[i]); |
| 632 | if (ret < 0) |
| 633 | goto exit; |
| 634 | ret = rtc_write_trigger(); |
| 635 | if (ret < 0) |
| 636 | goto exit; |
| 637 | } |
| 638 | |
| 639 | ret = rtc_update_bits(RTC_PDN1, RTC_PDN1_PWRON_TIME, 0); |
| 640 | if (ret < 0) |
| 641 | goto exit; |
| 642 | ret = rtc_update_bits(RTC_PDN2, RTC_PDN2_PWRON_LOGO, 0); |
| 643 | if (ret < 0) |
| 644 | goto exit; |
| 645 | |
| 646 | ret = rtc_write_trigger(); |
| 647 | if (ret < 0) |
| 648 | goto exit; |
| 649 | |
| 650 | return; |
| 651 | |
| 652 | exit: |
| 653 | pr_err("%s error\n", __func__); |
| 654 | } |
| 655 | |
| 656 | static void mtk_rtc_clear_alarm(void) |
| 657 | { |
| 658 | unsigned int irqsta; |
| 659 | u16 data[RTC_OFFSET_COUNT]; |
| 660 | int i, ret; |
| 661 | |
| 662 | ret = rtc_update_bits(RTC_IRQ_EN, RTC_IRQ_EN_AL, 0); |
| 663 | if (ret < 0) |
| 664 | goto exit; |
| 665 | ret = rtc_write_trigger(); |
| 666 | if (ret < 0) |
| 667 | goto exit; |
| 668 | |
| 669 | ret = rtc_read(RTC_IRQ_STA, &irqsta); /* read clear */ |
| 670 | if (ret < 0) |
| 671 | goto exit; |
| 672 | |
| 673 | data[RTC_OFFSET_SEC] = 0; |
| 674 | data[RTC_OFFSET_MIN] = 0; |
| 675 | data[RTC_OFFSET_HOUR] = 0; |
| 676 | data[RTC_OFFSET_DOM] = RTC_DEFAULT_DOM & RTC_AL_DOM_MASK; |
| 677 | data[RTC_OFFSET_MTH] = RTC_DEFAULT_MTH & RTC_AL_MTH_MASK; |
| 678 | data[RTC_OFFSET_YEAR] = ((RTC_DEFAULT_YEA - RTC_MIN_YEAR) & |
| 679 | RTC_AL_YEA_MASK); |
| 680 | |
| 681 | for (i = RTC_OFFSET_SEC; i < RTC_OFFSET_COUNT; i++) { |
| 682 | if (i == RTC_OFFSET_DOW) |
| 683 | continue; |
| 684 | ret = rtc_update_bits(rtc_alarm_reg[i][RTC_REG], |
| 685 | rtc_alarm_reg[i][RTC_MASK], data[i]); |
| 686 | if (ret < 0) |
| 687 | goto exit; |
| 688 | } |
| 689 | |
| 690 | ret = rtc_write_trigger(); |
| 691 | if (ret < 0) |
| 692 | goto exit; |
| 693 | |
| 694 | return; |
| 695 | exit: |
| 696 | pr_err("%s error\n", __func__); |
| 697 | } |
| 698 | |
| 699 | void rtc_mark_recovery(void) |
| 700 | { |
| 701 | unsigned long flags; |
| 702 | |
| 703 | pr_notice("%s\n", __func__); |
| 704 | |
| 705 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 706 | mtk_rtc_set_spare_register(RTC_FAC_RESET, 0x1); |
| 707 | /* Clear alarm setting when doing factory recovery. */ |
| 708 | mtk_rtc_clear_pwron_alarm(); |
| 709 | mtk_rtc_clear_alarm(); |
| 710 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 711 | } |
| 712 | |
| 713 | void rtc_mark_kpoc(void) |
| 714 | { |
| 715 | unsigned long flags; |
| 716 | |
| 717 | pr_notice("%s\n", __func__); |
| 718 | |
| 719 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 720 | mtk_rtc_set_spare_register(RTC_KPOC, 0x1); |
| 721 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 722 | } |
| 723 | |
| 724 | void rtc_mark_fast(void) |
| 725 | { |
| 726 | unsigned long flags; |
| 727 | |
| 728 | pr_notice("%s\n", __func__); |
| 729 | |
| 730 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 731 | mtk_rtc_set_spare_register(RTC_FAST_BOOT, 0x1); |
| 732 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 733 | } |
| 734 | |
| 735 | static int pmic_read_interface(unsigned int RegNum, unsigned int *val, |
| 736 | unsigned int MASK, unsigned int SHIFT) |
| 737 | { |
| 738 | int ret = 0; |
| 739 | |
| 740 | #ifdef IPIMB |
| 741 | ret = regmap_read(pmic_regmap, RegNum, val); |
| 742 | #else |
| 743 | ret = regmap_read(rtc_misc->regmap, RegNum, val); |
| 744 | #endif |
| 745 | if (ret) { |
| 746 | pr_notice("[%s]ret=%d Reg=0x%x val=0x%x MASK=0x%x SHIFT=%d\n", |
| 747 | __func__, ret, RegNum, val, MASK, SHIFT); |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | *val = (*val >> SHIFT) & MASK; |
| 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | static int upmu_get_reg_value(unsigned int reg, unsigned int *reg_val) |
| 757 | { |
| 758 | int ret = 0; |
| 759 | |
| 760 | ret = pmic_read_interface(reg, reg_val, 0xFFFF, 0x0); |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | static int pmic_config_interface(unsigned int RegNum, unsigned int val, |
| 766 | unsigned int MASK, unsigned int SHIFT) |
| 767 | { |
| 768 | int ret = 0; |
| 769 | |
| 770 | #ifdef IPIMB |
| 771 | ret = regmap_update_bits(pmic_regmap, RegNum, |
| 772 | (MASK << SHIFT), (val << SHIFT)); |
| 773 | #else |
| 774 | ret = regmap_update_bits(rtc_misc->regmap, RegNum, |
| 775 | (MASK << SHIFT), (val << SHIFT)); |
| 776 | #endif |
| 777 | if (ret) { |
| 778 | pr_notice("[%s]ret=%d Reg=0x%x val=0x%x MASK=0x%x SHIFT=%d\n", |
| 779 | __func__, ret, RegNum, val, MASK, SHIFT); |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | static int upmu_set_reg_value(unsigned int reg, unsigned int reg_val) |
| 787 | { |
| 788 | unsigned int ret = 0; |
| 789 | |
| 790 | ret = pmic_config_interface(reg, reg_val, 0xFFFF, 0x0); |
| 791 | |
| 792 | return ret; |
| 793 | } |
| 794 | |
| 795 | static void mtk_rtc_enable_k_eosc(void) |
| 796 | { |
| 797 | pr_notice("%s\n", __func__); |
| 798 | |
| 799 | /* Truning on eosc cali mode clock */ |
| 800 | pmic_config_interface(MT6389_SCK_TOP_CKPDN_CON0_CLR_ADDR, 1, |
| 801 | MT6389_RG_RTC_EOSC32_CK_PDN_MASK, |
| 802 | MT6389_RG_RTC_EOSC32_CK_PDN_SHIFT); |
| 803 | |
| 804 | if (rtc_eosc_cali_td != 8) { |
| 805 | pr_notice("%s: rtc_eosc_cali_td = %d\n", |
| 806 | __func__, rtc_eosc_cali_td); |
| 807 | switch (rtc_eosc_cali_td) { |
| 808 | case 1: |
| 809 | pmic_config_interface(MT6389_EOSC_CALI_TD_ADDR, 0x3, |
| 810 | MT6389_EOSC_CALI_TD_MASK, |
| 811 | MT6389_EOSC_CALI_TD_SHIFT); |
| 812 | break; |
| 813 | case 2: |
| 814 | pmic_config_interface(MT6389_EOSC_CALI_TD_ADDR, 0x4, |
| 815 | MT6389_EOSC_CALI_TD_MASK, |
| 816 | MT6389_EOSC_CALI_TD_SHIFT); |
| 817 | break; |
| 818 | case 4: |
| 819 | pmic_config_interface(MT6389_EOSC_CALI_TD_ADDR, 0x5, |
| 820 | MT6389_EOSC_CALI_TD_MASK, |
| 821 | MT6389_EOSC_CALI_TD_SHIFT); |
| 822 | break; |
| 823 | case 16: |
| 824 | pmic_config_interface(MT6389_EOSC_CALI_TD_ADDR, 0x7, |
| 825 | MT6389_EOSC_CALI_TD_MASK, |
| 826 | MT6389_EOSC_CALI_TD_SHIFT); |
| 827 | break; |
| 828 | default: |
| 829 | pmic_config_interface(MT6389_EOSC_CALI_TD_ADDR, 0x6, |
| 830 | MT6389_EOSC_CALI_TD_MASK, |
| 831 | MT6389_EOSC_CALI_TD_SHIFT); |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | /* |
| 836 | * Switch the DCXO from 32k-less mode to RTC mode, |
| 837 | * otherwise, EOSC cali will fail |
| 838 | * RTC mode will have only OFF mode and FPM |
| 839 | */ |
| 840 | if (dcxo_switch) { |
| 841 | pr_notice("%s: dcxo_switch\n", __func__); |
| 842 | pmic_config_interface(MT6389_XO_EN32K_MAN_ADDR, 0, |
| 843 | MT6389_XO_EN32K_MAN_MASK, |
| 844 | MT6389_XO_EN32K_MAN_SHIFT); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | static void mtk_rtc_spar_alarm_clear_wait(void) |
| 849 | { |
| 850 | unsigned long long timeout = sched_clock() + 500000000; |
| 851 | u32 bbpu; |
| 852 | int ret; |
| 853 | |
| 854 | do { |
| 855 | ret = rtc_read(RTC_BBPU, &bbpu); |
| 856 | if (ret < 0) |
| 857 | break; |
| 858 | if ((bbpu & RTC_BBPU_CLR) == 0) |
| 859 | break; |
| 860 | else if (sched_clock() > timeout) { |
| 861 | pr_err("%s, spar/alarm clear time out,\n", __func__); |
| 862 | break; |
| 863 | } |
| 864 | } while (1); |
| 865 | } |
| 866 | |
| 867 | void mt_power_off(void) |
| 868 | { |
| 869 | unsigned long flags; |
| 870 | u32 pdn1 = 0, al_mask = 0, irq_en = 0; |
| 871 | int ret; |
| 872 | |
| 873 | pr_notice("%s\n", __func__); |
| 874 | dump_stack(); |
| 875 | |
| 876 | spin_lock_irqsave(&rtc_misc->lock, flags); |
| 877 | |
| 878 | #ifndef CONFIG_MTK_PMIC_CHIP_MT6389 |
| 879 | ret = rtc_field_read(RTC_PDN1, RTC_GPIO_USER_MASK, |
| 880 | RTC_GPIO_USER_WIFI, &pdn1); |
| 881 | if (ret < 0) |
| 882 | goto exit; |
| 883 | /* disable 32K export if there are no RTC_GPIO users */ |
| 884 | if (!pdn1) { |
| 885 | ret = rtc_update_bits(RTC_CON, RTC_CON_F32KOB, RTC_CON_F32KOB); |
| 886 | if (ret < 0) |
| 887 | goto exit; |
| 888 | ret = rtc_write_trigger(); |
| 889 | if (ret < 0) |
| 890 | goto exit; |
| 891 | } |
| 892 | #endif |
| 893 | |
| 894 | /* lpsd */ |
| 895 | pr_notice("clear lpsd solution\n"); |
| 896 | ret = rtc_write(RTC_BBPU, RTC_BBPU_KEY | RTC_BBPU_CLR | RTC_BBPU_PWREN); |
| 897 | if (ret < 0) |
| 898 | goto exit; |
| 899 | ret = rtc_write(RTC_AL_MASK, RTC_AL_MASK_DOW); /* mask DOW */ |
| 900 | if (ret < 0) |
| 901 | goto exit; |
| 902 | ret = rtc_write_trigger(); |
| 903 | if (ret < 0) |
| 904 | goto exit; |
| 905 | mtk_rtc_spar_alarm_clear_wait(); |
| 906 | |
| 907 | ret = rtc_update_bits(RTC_BBPU, |
| 908 | (RTC_BBPU_KEY | RTC_BBPU_RELOAD), |
| 909 | (RTC_BBPU_KEY | RTC_BBPU_RELOAD)); |
| 910 | if (ret < 0) |
| 911 | goto exit; |
| 912 | ret = rtc_write_trigger(); |
| 913 | if (ret < 0) |
| 914 | goto exit; |
| 915 | |
| 916 | ret = rtc_read(RTC_AL_MASK, &al_mask); |
| 917 | if (ret < 0) |
| 918 | goto exit; |
| 919 | ret = rtc_read(RTC_IRQ_EN, &irq_en); |
| 920 | if (ret < 0) |
| 921 | goto exit; |
| 922 | pr_notice("%s: RTC_AL_MASK= 0x%x RTC_IRQ_EN= 0x%x\n", |
| 923 | __func__, al_mask, irq_en); |
| 924 | |
| 925 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 926 | |
| 927 | //wk_pmic_enable_sdn_delay(); |
| 928 | pmic_config_interface(MT6389_TMA_KEY_ADDR, 0x9CA7, |
| 929 | MT6389_TMA_KEY_MASK, MT6389_TMA_KEY_SHIFT); |
| 930 | pmic_config_interface(MT6389_RG_SDN_DLY_ENB_ADDR, 0, |
| 931 | MT6389_RG_SDN_DLY_ENB_MASK, |
| 932 | MT6389_RG_SDN_DLY_ENB_SHIFT); |
| 933 | pmic_config_interface(MT6389_TMA_KEY_ADDR, 0, |
| 934 | MT6389_TMA_KEY_MASK, MT6389_TMA_KEY_SHIFT); |
| 935 | |
| 936 | #ifndef CONFIG_MTK_PMIC_CHIP_MT6389 |
| 937 | pmic_config_interface(MT6389_RG_PWRHOLD_ADDR, 0, |
| 938 | MT6389_RG_PWRHOLD_MASK, MT6389_RG_PWRHOLD_SHIFT); |
| 939 | #endif |
| 940 | |
| 941 | return; |
| 942 | |
| 943 | exit: |
| 944 | spin_unlock_irqrestore(&rtc_misc->lock, flags); |
| 945 | pr_err("%s error\n", __func__); |
| 946 | |
| 947 | } |
| 948 | |
| 949 | static void mtk_rtc_lpsd_restore_al_mask(void) |
| 950 | { |
| 951 | int ret; |
| 952 | u32 val; |
| 953 | |
| 954 | ret = rtc_update_bits(RTC_BBPU, |
| 955 | (RTC_BBPU_KEY | RTC_BBPU_RELOAD), |
| 956 | (RTC_BBPU_KEY | RTC_BBPU_RELOAD)); |
| 957 | if (ret < 0) |
| 958 | goto exit; |
| 959 | ret = rtc_write_trigger(); |
| 960 | if (ret < 0) |
| 961 | goto exit; |
| 962 | |
| 963 | ret = rtc_read(RTC_AL_MASK, &val); |
| 964 | if (ret < 0) |
| 965 | goto exit; |
| 966 | pr_notice("%s: 1st RTC_AL_MASK = 0x%x\n", __func__, val); |
| 967 | |
| 968 | /* mask DOW */ |
| 969 | ret = rtc_write(RTC_AL_MASK, RTC_AL_MASK_DOW); |
| 970 | if (ret < 0) |
| 971 | goto exit; |
| 972 | ret = rtc_write_trigger(); |
| 973 | if (ret < 0) |
| 974 | goto exit; |
| 975 | |
| 976 | ret = rtc_read(RTC_AL_MASK, &val); |
| 977 | if (ret < 0) |
| 978 | goto exit; |
| 979 | pr_notice("%s: 2nd RTC_AL_MASK = 0x%x\n", __func__, val); |
| 980 | |
| 981 | return; |
| 982 | exit: |
| 983 | pr_err("%s error\n", __func__); |
| 984 | } |
| 985 | |
| 986 | static void mt6358_misc_shutdown(struct platform_device *pdev) |
| 987 | { |
| 988 | if (eosc_k) |
| 989 | mtk_rtc_enable_k_eosc(); |
| 990 | } |
| 991 | |
| 992 | static bool rtc_gpio_init(void) |
| 993 | { |
| 994 | u16 con; |
| 995 | int ret; |
| 996 | u32 val; |
| 997 | |
| 998 | /* GPI mode and pull enable + pull down */ |
| 999 | ret = rtc_read(RTC_CON, &val); |
| 1000 | if (ret < 0) |
| 1001 | goto exit; |
| 1002 | con = val & (RTC_CON_LPSTA_RAW | RTC_CON_LPRST | |
| 1003 | RTC_XOSC32_LPEN | RTC_EOSC32_LPEN); |
| 1004 | con &= ~RTC_CON_GPU; |
| 1005 | con &= ~RTC_CON_F32KOB; //for avoid leak current |
| 1006 | con |= RTC_CON_GPEN | RTC_CON_GOE; |
| 1007 | rtc_write(RTC_CON, con); |
| 1008 | if (rtc_write_trigger()) |
| 1009 | return true; |
| 1010 | else |
| 1011 | return false; |
| 1012 | exit: |
| 1013 | pr_err("%s error\n", __func__); |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | void rtc_reload(void) |
| 1018 | { |
| 1019 | u16 bbpu; |
| 1020 | int ret; |
| 1021 | u32 val; |
| 1022 | |
| 1023 | ret = rtc_read(RTC_BBPU, &val); |
| 1024 | if (ret < 0) |
| 1025 | goto exit; |
| 1026 | bbpu = val | RTC_BBPU_KEY | RTC_BBPU_RELOAD; |
| 1027 | rtc_write(RTC_BBPU, bbpu); |
| 1028 | rtc_write_trigger(); |
| 1029 | return; |
| 1030 | exit: |
| 1031 | pr_err("%s error\n", __func__); |
| 1032 | } |
| 1033 | |
| 1034 | void rtc_xosc_write(u16 val) |
| 1035 | { |
| 1036 | int ret; |
| 1037 | |
| 1038 | rtc_write(RTC_OSC32CON, RTC_OSC32CON_UNLOCK1); |
| 1039 | mdelay(1); |
| 1040 | |
| 1041 | rtc_write(RTC_OSC32CON, RTC_OSC32CON_UNLOCK2); |
| 1042 | mdelay(1); |
| 1043 | |
| 1044 | rtc_write(RTC_OSC32CON, val); |
| 1045 | mdelay(1); |
| 1046 | |
| 1047 | pr_notice("[RTC] xosc write val=%x\n", val); |
| 1048 | } |
| 1049 | |
| 1050 | void rtc_switch_mode(bool XOSC, bool recovery) |
| 1051 | { |
| 1052 | u16 osc32con; |
| 1053 | |
| 1054 | if (XOSC) { |
| 1055 | /* Since HW XTAL_DET is removed, there is a register for |
| 1056 | * switch the external crystal or the embedded clock |
| 1057 | */ |
| 1058 | /* 0: with the external xtal */ |
| 1059 | pmic_config_interface(MT6389_SCK_TOP_XTAL_SEL_ADDR, 0, |
| 1060 | MT6389_SCK_TOP_XTAL_SEL_MASK, |
| 1061 | MT6389_SCK_TOP_XTAL_SEL_SHIFT); |
| 1062 | osc32con = OSC32CON_ANALOG_SETTING; |
| 1063 | /* assume crystal exist mode + XOSCCALI = 0x7 */ |
| 1064 | rtc_xosc_write(osc32con | 0x7); |
| 1065 | if (recovery) |
| 1066 | mdelay(1500); |
| 1067 | } else { |
| 1068 | /* 1: without the external xtal */ |
| 1069 | pmic_config_interface(MT6389_SCK_TOP_XTAL_SEL_ADDR, 1, |
| 1070 | MT6389_SCK_TOP_XTAL_SEL_MASK, |
| 1071 | MT6389_SCK_TOP_XTAL_SEL_SHIFT); |
| 1072 | if (recovery) |
| 1073 | mdelay(100); |
| 1074 | osc32con = OSC32CON_ANALOG_SETTING | RTC_REG_XOSC32_ENB; |
| 1075 | /*crystal not exist + eosc cali = 0xF */ |
| 1076 | rtc_xosc_write(osc32con); |
| 1077 | mdelay(10); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | void rtc_switch_to_xosc_recv_mode(void) |
| 1082 | { |
| 1083 | rtc_switch_mode(true, true); |
| 1084 | } |
| 1085 | |
| 1086 | void rtc_switch_to_dcxo_recv_mode(void) |
| 1087 | { |
| 1088 | rtc_switch_mode(false, true); |
| 1089 | } |
| 1090 | |
| 1091 | static u16 get_frequency_meter(u16 cali, u16 measureSrc, u16 window_size) |
| 1092 | { |
| 1093 | u16 data; |
| 1094 | int ret; |
| 1095 | u32 val; |
| 1096 | unsigned long long timeout = sched_clock() + 500000000; |
| 1097 | |
| 1098 | if (cali != 0) { |
| 1099 | u16 osc32con; |
| 1100 | |
| 1101 | rtc_reload(); |
| 1102 | ret = rtc_read(RTC_OSC32CON, &val); |
| 1103 | if (ret < 0) |
| 1104 | goto exit; |
| 1105 | osc32con = val & 0xFFE0; |
| 1106 | rtc_xosc_write(osc32con | (cali & RTC_XOSCCALI_MASK)); |
| 1107 | } |
| 1108 | |
| 1109 | pmic_config_interface(FQMTR_CLK_CK_PDN_CLR, 1, FQMTR_CLK_CK_PDN_MASK, |
| 1110 | FQMTR_CLK_CK_PDN_SHIFT); |
| 1111 | pmic_config_interface(RG_FQMTR_32K_CLK_PDN_CLR, 1, |
| 1112 | RG_FQMTR_32K_CLK_PDN_MASK, |
| 1113 | RG_FQMTR_32K_CLK_PDN_SHIFT); |
| 1114 | |
| 1115 | pmic_config_interface(MT6389_RG_BANK_FQMTR_RST_ADDR, 1, |
| 1116 | MT6389_RG_BANK_FQMTR_RST_MASK, |
| 1117 | MT6389_RG_BANK_FQMTR_RST_SHIFT); |
| 1118 | udelay(20); |
| 1119 | pmic_config_interface(MT6389_RG_BANK_FQMTR_RST_ADDR, 0, |
| 1120 | MT6389_RG_BANK_FQMTR_RST_MASK, |
| 1121 | MT6389_RG_BANK_FQMTR_RST_SHIFT); |
| 1122 | |
| 1123 | pmic_config_interface(MT6389_FQMTR_DCXO26M_EN_ADDR, 1, |
| 1124 | MT6389_FQMTR_DCXO26M_EN_MASK, |
| 1125 | MT6389_FQMTR_DCXO26M_EN_SHIFT); |
| 1126 | //set freq. meter window value (0=1X32K(fix clock)) |
| 1127 | upmu_set_reg_value(RG_FQMTR_WINSET, window_size); |
| 1128 | //enable freq. meter, set measure clock to 26Mhz |
| 1129 | upmu_set_reg_value(RG_FQMTR_TCKSEL, FQMTR_DCXO26M_EN | measureSrc); |
| 1130 | mdelay(1); |
| 1131 | ret = upmu_get_reg_value(RG_FQMTR_TCKSEL, &val); |
| 1132 | if (ret < 0) |
| 1133 | goto exit; |
| 1134 | upmu_set_reg_value(RG_FQMTR_TCKSEL, val | FQMTR_EN); |
| 1135 | mdelay(1); |
| 1136 | |
| 1137 | // FQMTR read until ready |
| 1138 | do { |
| 1139 | if (sched_clock() > timeout) { |
| 1140 | pr_err("[RTC] get frequency time out\n"); |
| 1141 | break; |
| 1142 | } |
| 1143 | mdelay(1); |
| 1144 | ret = upmu_get_reg_value(RG_FQMTR_BUSY, &val); |
| 1145 | if (ret < 0) |
| 1146 | goto exit; |
| 1147 | } while ((FQMTR_BUSY & val) == FQMTR_BUSY); |
| 1148 | ret = upmu_get_reg_value(RG_FQMTR_DATA, &val); |
| 1149 | if (ret < 0) |
| 1150 | goto exit; |
| 1151 | data = val; //read data should be closed to 26M/32k = 794 |
| 1152 | |
| 1153 | pmic_config_interface(MT6389_FQMTR_DCXO26M_EN_ADDR, 0, |
| 1154 | MT6389_FQMTR_DCXO26M_EN_MASK, |
| 1155 | MT6389_FQMTR_DCXO26M_EN_SHIFT); |
| 1156 | ret = upmu_get_reg_value(RG_FQMTR_TCKSEL, &val); |
| 1157 | if (ret < 0) |
| 1158 | goto exit; |
| 1159 | //enable freq. meter, set measure clock to 26Mhz |
| 1160 | upmu_set_reg_value(RG_FQMTR_TCKSEL, val & ~(FQMTR_DCXO26M_EN)); |
| 1161 | mdelay(1); |
| 1162 | ret = upmu_get_reg_value(RG_FQMTR_TCKSEL, &val); |
| 1163 | if (ret < 0) |
| 1164 | goto exit; |
| 1165 | //enable freq. meter, set measure clock to 26Mhz |
| 1166 | upmu_set_reg_value(RG_FQMTR_TCKSEL, val & ~(FQMTR_EN)); |
| 1167 | pr_notice("[RTC] %s: input=0x%x, output=%d\n", __func__, cali, data); |
| 1168 | |
| 1169 | pmic_config_interface(FQMTR_CLK_CK_PDN_SET, 1, FQMTR_CLK_CK_PDN_MASK, |
| 1170 | FQMTR_CLK_CK_PDN_SHIFT); |
| 1171 | pmic_config_interface(RG_FQMTR_32K_CLK_PDN_SET, 1, |
| 1172 | RG_FQMTR_32K_CLK_PDN_MASK, |
| 1173 | RG_FQMTR_32K_CLK_PDN_SHIFT); |
| 1174 | |
| 1175 | return data; |
| 1176 | exit: |
| 1177 | pr_err("%s error\n", __func__); |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | void rtc_measure_four_clock(u16 *result) |
| 1182 | { |
| 1183 | u16 window_size; |
| 1184 | u16 regval; |
| 1185 | int ret; |
| 1186 | u32 val; |
| 1187 | |
| 1188 | ret = upmu_get_reg_value(RG_FQMTR_CKSEL, &val); |
| 1189 | if (ret < 0) |
| 1190 | goto exit; |
| 1191 | regval = val & (~(RG_FQMTR_CKSEL_MASK << RG_FQMTR_CKSEL_SHIFT)); |
| 1192 | //select 26M as fixed clock |
| 1193 | upmu_set_reg_value(RG_FQMTR_CKSEL, regval | FQMTR_FIX_CLK_26M); |
| 1194 | window_size = 4; |
| 1195 | mdelay(1); |
| 1196 | //select 26M as target clock |
| 1197 | result[0] = get_frequency_meter(0, FQMTR_FQM26M_CK, window_size); |
| 1198 | ret = upmu_get_reg_value(RG_FQMTR_CKSEL, &val); |
| 1199 | if (ret < 0) |
| 1200 | goto exit; |
| 1201 | regval = val & (~(RG_FQMTR_CKSEL_MASK << RG_FQMTR_CKSEL_SHIFT)); |
| 1202 | //select XOSC_DET as fixed clock |
| 1203 | upmu_set_reg_value(RG_FQMTR_CKSEL, regval | FQMTR_FIX_CLK_XOSC_32K_DET); |
| 1204 | window_size = 4; |
| 1205 | mdelay(1); |
| 1206 | //select 26M as target clock |
| 1207 | result[1] = get_frequency_meter(0, FQMTR_FQM26M_CK, window_size); |
| 1208 | ret = upmu_get_reg_value(RG_FQMTR_CKSEL, &val); |
| 1209 | if (ret < 0) |
| 1210 | goto exit; |
| 1211 | regval = val & (~(RG_FQMTR_CKSEL_MASK << RG_FQMTR_CKSEL_SHIFT)); |
| 1212 | //select 26M as fixed clock |
| 1213 | upmu_set_reg_value(RG_FQMTR_CKSEL, regval | FQMTR_FIX_CLK_26M); |
| 1214 | window_size = 3970; // (26M / 32K) * 5 |
| 1215 | mdelay(1); |
| 1216 | //select xosc_32 as target clock |
| 1217 | result[2] = get_frequency_meter(0, FQMTR_XOSC32_CK, window_size); |
| 1218 | //select DCXO_32 as target clock |
| 1219 | result[2] = get_frequency_meter(0, FQMTR_DCXO_F32K_CK, window_size); |
| 1220 | ret = upmu_get_reg_value(RG_FQMTR_CKSEL, &val); |
| 1221 | if (ret < 0) |
| 1222 | goto exit; |
| 1223 | regval = val & (~(RG_FQMTR_CKSEL_MASK << RG_FQMTR_CKSEL_SHIFT)); |
| 1224 | //select EOSC_32 as fixed clock |
| 1225 | upmu_set_reg_value(RG_FQMTR_CKSEL, regval | FQMTR_FIX_CLK_EOSC_32K); |
| 1226 | window_size = 4; |
| 1227 | mdelay(1); |
| 1228 | //select 26M as target clock |
| 1229 | result[3] = get_frequency_meter(0, FQMTR_FQM26M_CK, window_size); |
| 1230 | |
| 1231 | return; |
| 1232 | exit: |
| 1233 | pr_err("%s error\n", __func__); |
| 1234 | } |
| 1235 | |
| 1236 | static bool rtc_xosc_check_clock(u16 *result) |
| 1237 | { |
| 1238 | ///// fix me loose range for frequency meter result//// |
| 1239 | if ((result[0] >= 3 && result[0] <= 7) && |
| 1240 | (result[1] > 1500 && result[1] < 6000) && |
| 1241 | (result[2] == 0) && (result[3] == 0)) |
| 1242 | return true; |
| 1243 | else |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | static bool rtc_eosc_check_clock(u16 *result) |
| 1248 | { |
| 1249 | if ((result[0] >= 3 && result[0] <= 7) && |
| 1250 | (result[1] < 500) && |
| 1251 | (result[2] > 2 && result[2] < 9) && |
| 1252 | (result[3] > 300 && result[3] < 10400)) |
| 1253 | return true; |
| 1254 | else |
| 1255 | return false; |
| 1256 | } |
| 1257 | |
| 1258 | bool Writeif_unlock(void) |
| 1259 | { |
| 1260 | rtc_write(RTC_PROT, RTC_PROT_UNLOCK1); |
| 1261 | if (!rtc_write_trigger()) |
| 1262 | return false; |
| 1263 | rtc_write(RTC_PROT, RTC_PROT_UNLOCK2); |
| 1264 | if (!rtc_write_trigger()) |
| 1265 | return false; |
| 1266 | |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | int eosc_cali(void) |
| 1271 | { |
| 1272 | u32 val, diff1, diff2, eosc_freq, regval; |
| 1273 | int middle; |
| 1274 | int i, left, right; |
| 1275 | int ret; |
| 1276 | |
| 1277 | ret = upmu_get_reg_value(RG_FQMTR_CKSEL, &val); |
| 1278 | if (ret < 0) |
| 1279 | goto exit; |
| 1280 | regval = val & (~(RG_FQMTR_CKSEL_MASK << RG_FQMTR_CKSEL_SHIFT)); |
| 1281 | //select EOSC_32 as fixed clock |
| 1282 | upmu_set_reg_value(RG_FQMTR_CKSEL, regval | FQMTR_FIX_CLK_EOSC_32K); |
| 1283 | |
| 1284 | left = RTC_XOSCCALI_START; |
| 1285 | right = RTC_XOSCCALI_END; |
| 1286 | while (left <= right) { |
| 1287 | middle = (right + left) / 2; |
| 1288 | if (middle == left) |
| 1289 | break; |
| 1290 | |
| 1291 | //select 26M as target clock |
| 1292 | val = get_frequency_meter(middle, |
| 1293 | FQMTR_FQM26M_CK, 0); |
| 1294 | pr_notice("[RTC] EOSC_Cali: val=0x%x\n", val); |
| 1295 | if ((val >= RTC_FQMTR_LOW_BASE) && (val <= RTC_FQMTR_HIGH_BASE)) |
| 1296 | break; |
| 1297 | if (val > RTC_FQMTR_HIGH_BASE) |
| 1298 | right = middle; |
| 1299 | else |
| 1300 | left = middle; |
| 1301 | } |
| 1302 | |
| 1303 | if ((val >= RTC_FQMTR_LOW_BASE) && (val <= RTC_FQMTR_HIGH_BASE)) |
| 1304 | return middle; |
| 1305 | |
| 1306 | val = get_frequency_meter(left, FQMTR_FQM26M_CK, 0); |
| 1307 | if (val > RTC_FQMTR_LOW_BASE) |
| 1308 | diff1 = val - RTC_FQMTR_LOW_BASE; |
| 1309 | else |
| 1310 | diff1 = RTC_FQMTR_LOW_BASE - val; |
| 1311 | |
| 1312 | val = get_frequency_meter(right, FQMTR_FQM26M_CK, 0); |
| 1313 | if (val > RTC_FQMTR_LOW_BASE) |
| 1314 | diff2 = val - RTC_FQMTR_LOW_BASE; |
| 1315 | else |
| 1316 | diff2 = RTC_FQMTR_LOW_BASE - val; |
| 1317 | |
| 1318 | if (diff1 < diff2) |
| 1319 | return left; |
| 1320 | else |
| 1321 | return right; |
| 1322 | exit: |
| 1323 | pr_err("%s error\n", __func__); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | static bool eosc_init(void) |
| 1328 | { |
| 1329 | u16 osc32con, val = 0; |
| 1330 | |
| 1331 | val = eosc_cali(); |
| 1332 | pr_notice("EOSC cali val = 0x%x\n", val); |
| 1333 | //EMB_HW_Mode |
| 1334 | osc32con = OSC32CON_ANALOG_SETTING | RTC_REG_XOSC32_ENB; |
| 1335 | val = (val & 0x001f) | osc32con; |
| 1336 | pr_notice("EOSC cali val = 0x%x\n", val); |
| 1337 | rtc_xosc_write(val); |
| 1338 | return true; |
| 1339 | } |
| 1340 | |
| 1341 | static bool rtc_hw_init(void) |
| 1342 | { |
| 1343 | int ret; |
| 1344 | u32 val; |
| 1345 | unsigned long long timeout = sched_clock() + 500000000; |
| 1346 | |
| 1347 | ret = rtc_read(RTC_BBPU, &val); |
| 1348 | if (ret < 0) |
| 1349 | goto exit; |
| 1350 | rtc_write(RTC_BBPU, |
| 1351 | val | RTC_BBPU_KEY | RTC_BBPU_RESET_ALARM | |
| 1352 | RTC_BBPU_RESET_SPAR & |
| 1353 | (~RTC_BBPU_SPAR_SW)); |
| 1354 | rtc_write_trigger(); |
| 1355 | |
| 1356 | do { |
| 1357 | rtc_reload(); |
| 1358 | ret = rtc_read(RTC_BBPU, &val); |
| 1359 | if (ret < 0) |
| 1360 | goto exit; |
| 1361 | } while (((val & RTC_BBPU_RESET_ALARM) || (val & RTC_BBPU_RESET_SPAR)) |
| 1362 | && sched_clock() < timeout); |
| 1363 | |
| 1364 | if ((val & RTC_BBPU_RESET_ALARM) || (val & RTC_BBPU_RESET_SPAR)) { |
| 1365 | pr_err("[RTC] hw_init timeout\n"); |
| 1366 | return false; |
| 1367 | } |
| 1368 | |
| 1369 | return true; |
| 1370 | exit: |
| 1371 | pr_err("%s error\n", __func__); |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
| 1375 | static bool rtc_lpd_init(void) |
| 1376 | { |
| 1377 | u16 con; |
| 1378 | int ret; |
| 1379 | u32 val; |
| 1380 | |
| 1381 | ret = rtc_read(RTC_CON, &val); |
| 1382 | if (ret < 0) |
| 1383 | goto exit; |
| 1384 | con = val | RTC_XOSC32_LPEN; |
| 1385 | con &= ~RTC_CON_LPRST; |
| 1386 | rtc_write(RTC_CON, con); |
| 1387 | if (!rtc_write_trigger()) |
| 1388 | return false; |
| 1389 | |
| 1390 | con |= RTC_CON_LPRST; |
| 1391 | rtc_write(RTC_CON, con); |
| 1392 | if (!rtc_write_trigger()) |
| 1393 | return false; |
| 1394 | |
| 1395 | con &= ~RTC_CON_LPRST; |
| 1396 | rtc_write(RTC_CON, con); |
| 1397 | if (!rtc_write_trigger()) |
| 1398 | return false; |
| 1399 | |
| 1400 | ret = rtc_read(RTC_CON, &val); |
| 1401 | if (ret < 0) |
| 1402 | goto exit; |
| 1403 | con = val | RTC_EOSC32_LPEN; |
| 1404 | con &= ~RTC_CON_LPRST; |
| 1405 | rtc_write(RTC_CON, con); |
| 1406 | if (!rtc_write_trigger()) |
| 1407 | return false; |
| 1408 | |
| 1409 | con |= RTC_CON_LPRST; |
| 1410 | rtc_write(RTC_CON, con); |
| 1411 | if (!rtc_write_trigger()) |
| 1412 | return false; |
| 1413 | |
| 1414 | con &= ~RTC_CON_LPRST; |
| 1415 | rtc_write(RTC_CON, con); |
| 1416 | if (!rtc_write_trigger()) |
| 1417 | return false; |
| 1418 | |
| 1419 | ret = rtc_read(RTC_CON, &val); |
| 1420 | if (ret < 0) |
| 1421 | goto exit; |
| 1422 | pr_notice("[RTC] %s RTC_CON=0x%x\n", __func__, val); |
| 1423 | ret = rtc_read(RTC_SPAR0, &val); |
| 1424 | if (ret < 0) |
| 1425 | goto exit; |
| 1426 | //bit 7 for low power detected in preloader |
| 1427 | rtc_write(RTC_SPAR0, val | 0x0080); |
| 1428 | if (!rtc_write_trigger()) |
| 1429 | return false; |
| 1430 | |
| 1431 | return true; |
| 1432 | exit: |
| 1433 | pr_err("%s error\n", __func__); |
| 1434 | return false; |
| 1435 | } |
| 1436 | |
| 1437 | static bool rtc_2sec_stat_clear(void) |
| 1438 | { |
| 1439 | int ret; |
| 1440 | u32 val; |
| 1441 | |
| 1442 | pr_notice("[RTC] %s\n", __func__); |
| 1443 | |
| 1444 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1445 | if (ret < 0) |
| 1446 | goto exit; |
| 1447 | rtc_write(RTC_AL_SEC, val & ~RTC_BBPU_2SEC_STAT_CLEAR); |
| 1448 | if (!rtc_write_trigger()) |
| 1449 | return false; |
| 1450 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1451 | if (ret < 0) |
| 1452 | goto exit; |
| 1453 | rtc_write(RTC_AL_SEC, val | RTC_BBPU_2SEC_STAT_CLEAR); |
| 1454 | if (!rtc_write_trigger()) |
| 1455 | return false; |
| 1456 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1457 | if (ret < 0) |
| 1458 | goto exit; |
| 1459 | rtc_write(RTC_AL_SEC, val & ~RTC_BBPU_2SEC_STAT_CLEAR); |
| 1460 | if (!rtc_write_trigger()) |
| 1461 | return false; |
| 1462 | |
| 1463 | return true; |
| 1464 | exit: |
| 1465 | pr_err("%s error\n", __func__); |
| 1466 | return false; |
| 1467 | } |
| 1468 | |
| 1469 | static bool rtc_android_init(void) |
| 1470 | { |
| 1471 | u16 reboot; |
| 1472 | int ret; |
| 1473 | u32 val; |
| 1474 | |
| 1475 | rtc_write(RTC_IRQ_EN, 0); |
| 1476 | rtc_write(RTC_CII_EN, 0); |
| 1477 | rtc_write(RTC_AL_MASK, 0); |
| 1478 | |
| 1479 | ret = rtc_read(RTC_AL_YEA, &val); |
| 1480 | if (ret < 0) |
| 1481 | goto exit; |
| 1482 | rtc_write(RTC_AL_YEA, |
| 1483 | (val & (~RTC_AL_YEA_MASK)) | |
| 1484 | ((1970 - RTC_MIN_YEAR) & RTC_AL_YEA_MASK)); |
| 1485 | rtc_write(RTC_AL_MTH, 1); |
| 1486 | rtc_write(RTC_AL_DOM, 1); /* NEW_SPARE1[0] = 0 */ |
| 1487 | /* RG_EOSC_CALI_TD = 8 sec */ |
| 1488 | rtc_write(RTC_AL_DOW, RTC_EOSC_CALI_TD_08_SEC << |
| 1489 | MT6389_RG_EOSC_CALI_TD_SHIFT | 1); |
| 1490 | |
| 1491 | rtc_write(RTC_AL_HOU, 0); |
| 1492 | rtc_write(RTC_AL_MIN, 0); |
| 1493 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1494 | if (ret < 0) |
| 1495 | goto exit; |
| 1496 | rtc_write(RTC_AL_SEC, val & (~RTC_AL_SEC_MASK)); |
| 1497 | |
| 1498 | rtc_write(RTC_PDN1, 0); /* set Debug bit */ |
| 1499 | rtc_write(RTC_PDN2, 0); |
| 1500 | rtc_write(RTC_SPAR0, 0); |
| 1501 | rtc_write(RTC_SPAR1, 0); |
| 1502 | |
| 1503 | rtc_write(RTC_DIFF, 0); |
| 1504 | rtc_write(RTC_CALI, 0); |
| 1505 | |
| 1506 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1507 | if (ret < 0) |
| 1508 | goto exit; |
| 1509 | reboot = (val & ~RTC_BBPU_2SEC_EN) & ~RTC_BBPU_AUTO_PDN_SEL; |
| 1510 | rtc_write(RTC_AL_SEC, reboot); |
| 1511 | |
| 1512 | if (!rtc_2sec_stat_clear()) |
| 1513 | return false; |
| 1514 | if (!rtc_write_trigger()) |
| 1515 | return false; |
| 1516 | |
| 1517 | /* read clear */ |
| 1518 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1519 | if (ret < 0) |
| 1520 | goto exit; |
| 1521 | |
| 1522 | /* init time counters after resetting RTC_DIFF and RTC_CALI */ |
| 1523 | rtc_write(RTC_TC_YEA, 2010 - RTC_MIN_YEAR); |
| 1524 | rtc_write(RTC_TC_MTH, 1); |
| 1525 | rtc_write(RTC_TC_DOM, 1); |
| 1526 | rtc_write(RTC_TC_DOW, 1); |
| 1527 | rtc_write(RTC_TC_HOU, 0); |
| 1528 | rtc_write(RTC_TC_MIN, 0); |
| 1529 | rtc_write(RTC_TC_SEC, 0); |
| 1530 | if (!rtc_write_trigger()) |
| 1531 | return false; |
| 1532 | |
| 1533 | return true; |
| 1534 | exit: |
| 1535 | pr_err("%s error\n", __func__); |
| 1536 | return false; |
| 1537 | } |
| 1538 | |
| 1539 | static bool rtc_first_boot_init(void) |
| 1540 | { |
| 1541 | int ret; |
| 1542 | u32 val; |
| 1543 | u16 result[4]; |
| 1544 | |
| 1545 | pr_notice("[RTC] %s\n", __func__); |
| 1546 | |
| 1547 | pmic_config_interface(MT6389_XO_EN32K_MAN_ADDR, 0, |
| 1548 | MT6389_XO_EN32K_MAN_MASK, |
| 1549 | MT6389_XO_EN32K_MAN_SHIFT); |
| 1550 | pmic_config_interface(MT6389_XO_XMODE_M_ADDR, 1, MT6389_XO_XMODE_M_MASK, |
| 1551 | MT6389_XO_XMODE_M_SHIFT); |
| 1552 | |
| 1553 | /* always try xosc fisrt */ |
| 1554 | /* switch to XOSC */ |
| 1555 | if (support_xosc) { |
| 1556 | rtc_switch_to_xosc_recv_mode(); |
| 1557 | rtc_measure_four_clock(result); |
| 1558 | } |
| 1559 | if (!support_xosc || !rtc_xosc_check_clock(result)) { |
| 1560 | /* switch to DCXO if XOSC detect failed */ |
| 1561 | rtc_switch_to_dcxo_recv_mode(); |
| 1562 | rtc_measure_four_clock(result); |
| 1563 | if (!rtc_eosc_check_clock(result)) { |
| 1564 | pr_err("32k detection failed\n"); |
| 1565 | return false; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | if (!Writeif_unlock()) { |
| 1570 | pr_err("[RTC] Writeif_unlock fail1\n"); |
| 1571 | return false; |
| 1572 | } |
| 1573 | |
| 1574 | rtc_reload(); |
| 1575 | |
| 1576 | if (!rtc_gpio_init()) |
| 1577 | return false; |
| 1578 | |
| 1579 | /* write powerkeys */ |
| 1580 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1581 | if (ret < 0) |
| 1582 | goto exit; |
| 1583 | rtc_write(RTC_AL_SEC, val & (~RTC_K_EOSC32_VTCXO_ON_SEL)); |
| 1584 | rtc_write(RTC_POWERKEY1, RTC_POWERKEY1_KEY); |
| 1585 | rtc_write(RTC_POWERKEY2, RTC_POWERKEY2_KEY); |
| 1586 | if (!rtc_write_trigger()) { |
| 1587 | pr_err("[RTC] first_boot_init rtc_write_trigger fail1\n"); |
| 1588 | return false; |
| 1589 | } |
| 1590 | //disable both XOSC & EOSC LPD |
| 1591 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1592 | if (ret < 0) |
| 1593 | goto exit; |
| 1594 | rtc_write(RTC_AL_SEC, val | RTC_LPD_OPT_F32K_CK_ALIVE); |
| 1595 | if (!rtc_write_trigger()) { |
| 1596 | pr_err("[RTC] %s rtc_write_trigger fail2\n", __func__); |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | if (!rtc_lpd_init()) |
| 1601 | return false; |
| 1602 | |
| 1603 | //write POWERKEY again to unlock RTC |
| 1604 | rtc_write(RTC_POWERKEY1, RTC_POWERKEY1_KEY); |
| 1605 | rtc_write(RTC_POWERKEY2, RTC_POWERKEY2_KEY); |
| 1606 | if (!rtc_write_trigger()) { |
| 1607 | pr_err("[RTC] %s rtc_write_trigger fail3\n", __func__); |
| 1608 | return false; |
| 1609 | } |
| 1610 | //Enable EOSC LPD |
| 1611 | ret = rtc_read(RTC_AL_SEC, &val); |
| 1612 | if (ret < 0) |
| 1613 | goto exit; |
| 1614 | rtc_write(RTC_AL_SEC, (val & (~RTC_LPD_OPT_MASK)) | |
| 1615 | RTC_LPD_OPT_EOSC_LPD); |
| 1616 | rtc_write_trigger(); |
| 1617 | |
| 1618 | if (!eosc_init()) { |
| 1619 | pr_err("[RTC] %s eosc_init fail\n", __func__); |
| 1620 | return false; |
| 1621 | } |
| 1622 | |
| 1623 | if (!rtc_lpd_init()) { |
| 1624 | pr_err("[RTC] %s rtc_lpd_init fail\n", __func__); |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
| 1628 | if (!rtc_hw_init()) { |
| 1629 | pr_err("[RTC] %s rtc_hw_init fail\n", __func__); |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
| 1633 | if (!rtc_android_init()) { |
| 1634 | pr_err("[RTC] %s rtc_android_init fail\n", __func__); |
| 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | return true; |
| 1639 | exit: |
| 1640 | pr_err("%s error\n", __func__); |
| 1641 | return false; |
| 1642 | } |
| 1643 | |
| 1644 | static bool rtc_get_xosc_mode(void) |
| 1645 | { |
| 1646 | u16 xosc_mode; |
| 1647 | int ret; |
| 1648 | u32 val; |
| 1649 | |
| 1650 | ret = |
| 1651 | pmic_read_interface(MT6389_SCK_TOP_XTAL_SEL_ADDR, |
| 1652 | &val, MT6389_SCK_TOP_XTAL_SEL_MASK, |
| 1653 | MT6389_SCK_TOP_XTAL_SEL_SHIFT); |
| 1654 | if (ret < 0) |
| 1655 | goto exit; |
| 1656 | |
| 1657 | if (val) |
| 1658 | xosc_mode = 0; /* 32k */ |
| 1659 | else |
| 1660 | xosc_mode = 1; /* 32k-less */ |
| 1661 | |
| 1662 | return xosc_mode; |
| 1663 | exit: |
| 1664 | pr_err("%s error\n", __func__); |
| 1665 | return false; |
| 1666 | } |
| 1667 | |
| 1668 | void rtc_init(void) |
| 1669 | { |
| 1670 | u16 spar0; |
| 1671 | int ret; |
| 1672 | u32 val1, val2, val3; |
| 1673 | |
| 1674 | pmic_config_interface(MT6389_TOP_CKPDN_CON0_CLR_ADDR, 1, |
| 1675 | MT6389_RG_FQMTR_CK_PDN_MASK, |
| 1676 | MT6389_RG_FQMTR_CK_PDN_SHIFT); |
| 1677 | pmic_config_interface(MT6389_TOP_CKPDN_CON0_CLR_ADDR, 1, |
| 1678 | MT6389_RG_FQMTR_32K_CK_PDN_MASK, |
| 1679 | MT6389_RG_FQMTR_32K_CK_PDN_SHIFT); |
| 1680 | |
| 1681 | ret = rtc_read(RTC_POWERKEY1, &val1); |
| 1682 | if (ret < 0) |
| 1683 | goto exit; |
| 1684 | ret = rtc_read(RTC_POWERKEY2, &val2); |
| 1685 | if (ret < 0) |
| 1686 | goto exit; |
| 1687 | ret = rtc_read(RTC_CON, &val3); |
| 1688 | if (ret < 0) |
| 1689 | goto exit; |
| 1690 | pr_notice("%s#1 powerkey1 = 0x%x, powerkey2 = 0x%x, %s LPD\n", |
| 1691 | __func__, val1, val2, |
| 1692 | (val3 & RTC_CON_LPSTA_RAW) ? "with" : "without"); |
| 1693 | ret = rtc_read(RTC_BBPU, &val1); |
| 1694 | if (ret < 0) |
| 1695 | goto exit; |
| 1696 | ret = rtc_read(RTC_CON, &val2); |
| 1697 | if (ret < 0) |
| 1698 | goto exit; |
| 1699 | ret = rtc_read(RTC_OSC32CON, &val3); |
| 1700 | if (ret < 0) |
| 1701 | goto exit; |
| 1702 | pr_notice("bbpu = 0x%x, con = 0x%x, osc32con = 0x%x\n", |
| 1703 | val1, val2, val3); |
| 1704 | |
| 1705 | // switch from 32k to 128/4k |
| 1706 | pmic_config_interface(MT6389_TOP_CKSEL_CON0_CLR_ADDR, 1, |
| 1707 | MT6389_RG_RTC_32K1V8_SEL_MASK, |
| 1708 | MT6389_RG_RTC_32K1V8_SEL_SHIFT); |
| 1709 | |
| 1710 | // always do first boot init |
| 1711 | if (!rtc_first_boot_init()) |
| 1712 | goto exit; |
| 1713 | |
| 1714 | // switch from 128/4k to 32k |
| 1715 | pmic_config_interface(MT6389_TOP_CKSEL_CON0_SET_ADDR, 1, |
| 1716 | MT6389_RG_RTC_32K1V8_SEL_MASK, |
| 1717 | MT6389_RG_RTC_32K1V8_SEL_SHIFT); |
| 1718 | |
| 1719 | rtc_reload(); |
| 1720 | |
| 1721 | /* HW K EOSC mode whatever power off (including plug out battery) */ |
| 1722 | ret = rtc_read(RTC_AL_YEA, &val1); |
| 1723 | if (ret < 0) |
| 1724 | goto exit; |
| 1725 | rtc_write(RTC_AL_YEA, |
| 1726 | (val1 | RTC_K_EOSC_RSV_0) & |
| 1727 | (~RTC_K_EOSC_RSV_1) & (~RTC_K_EOSC_RSV_2)); |
| 1728 | pmic_config_interface(MT6389_TOP_CKPDN_CON0_CLR_ADDR, 1, |
| 1729 | MT6389_RG_FQMTR_CK_PDN_MASK, |
| 1730 | MT6389_RG_FQMTR_CK_PDN_SHIFT); |
| 1731 | /* Truning off eosc cali mode clock */ |
| 1732 | pmic_config_interface(MT6389_SCK_TOP_CKPDN_CON0_SET_ADDR, 1, |
| 1733 | MT6389_RG_RTC_EOSC32_CK_PDN_MASK, |
| 1734 | MT6389_RG_RTC_EOSC32_CK_PDN_SHIFT); |
| 1735 | |
| 1736 | //set register to let MD know 32k status |
| 1737 | ret = rtc_read(RTC_SPAR0, &val1); |
| 1738 | if (ret < 0) |
| 1739 | goto exit; |
| 1740 | if (rtc_get_xosc_mode()) { |
| 1741 | rtc_write(RTC_SPAR0, (val1 | RTC_SPAR0_32K_LESS)); |
| 1742 | pr_notice("RTC/32k mode\n"); |
| 1743 | } else { |
| 1744 | rtc_write(RTC_SPAR0, (val1 & ~RTC_SPAR0_32K_LESS)); |
| 1745 | pr_notice("32k-less mode\n"); |
| 1746 | } |
| 1747 | rtc_write_trigger(); |
| 1748 | |
| 1749 | return; |
| 1750 | exit: |
| 1751 | pr_err("%s error\n", __func__); |
| 1752 | } |
| 1753 | |
| 1754 | static int mt6358_misc_probe(struct platform_device *pdev) |
| 1755 | { |
| 1756 | struct mt6358_chip *mt6358_chip = dev_get_drvdata(pdev->dev.parent); |
| 1757 | struct mt6358_misc *misc; |
| 1758 | unsigned long flags; |
| 1759 | |
| 1760 | misc = devm_kzalloc(&pdev->dev, sizeof(struct mt6358_misc), GFP_KERNEL); |
| 1761 | if (!misc) |
| 1762 | return -ENOMEM; |
| 1763 | |
| 1764 | #ifdef IPIMB |
| 1765 | pmic_regmap = mt6358_chip->regmap; |
| 1766 | misc->regmap = dev_get_regmap(pdev->dev.parent->parent, NULL); |
| 1767 | #else |
| 1768 | misc->regmap = mt6358_chip->regmap; |
| 1769 | #endif |
| 1770 | if (!misc->regmap) { |
| 1771 | pr_notice("get regmap failed\n"); |
| 1772 | return -ENODEV; |
| 1773 | } |
| 1774 | |
| 1775 | misc->dev = &pdev->dev; |
| 1776 | spin_lock_init(&misc->lock); |
| 1777 | rtc_misc = misc; |
| 1778 | platform_set_drvdata(pdev, misc); |
| 1779 | |
| 1780 | if (of_property_read_u32(pdev->dev.of_node, |
| 1781 | "base", &rtc_misc->addr_base)) |
| 1782 | rtc_misc->addr_base = RTC_DSN_ID; |
| 1783 | pr_notice("%s: rtc_misc->addr_base =0x%x\n", |
| 1784 | __func__, rtc_misc->addr_base); |
| 1785 | |
| 1786 | if (of_property_read_bool(pdev->dev.of_node, "apply-lpsd-solution")) { |
| 1787 | spin_lock_irqsave(&misc->lock, flags); |
| 1788 | mtk_rtc_lpsd_restore_al_mask(); |
| 1789 | spin_unlock_irqrestore(&misc->lock, flags); |
| 1790 | |
| 1791 | pm_power_off = mt_power_off; |
| 1792 | } |
| 1793 | |
| 1794 | if (of_property_read_bool(pdev->dev.of_node, "dcxo-switch")) |
| 1795 | dcxo_switch = 1; |
| 1796 | |
| 1797 | if (of_property_read_bool(pdev->dev.of_node, "eosc-k")) |
| 1798 | eosc_k = 1; |
| 1799 | |
| 1800 | if (of_property_read_bool(pdev->dev.of_node, "support_xosc")) |
| 1801 | support_xosc = 1; |
| 1802 | |
| 1803 | rtc_init(); |
| 1804 | |
| 1805 | pr_notice("%s done\n", __func__); |
| 1806 | |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
| 1810 | static const struct of_device_id mt6358_misc_of_match[] = { |
| 1811 | {.compatible = "mediatek,mt6358-misc",}, |
| 1812 | {.compatible = "mediatek,mt6359-misc",}, |
| 1813 | {.compatible = "mediatek,mt6389-misc",}, |
| 1814 | {} |
| 1815 | }; |
| 1816 | |
| 1817 | MODULE_DEVICE_TABLE(of, mt6358_misc_of_match); |
| 1818 | |
| 1819 | static struct platform_driver mt6358_misc_driver = { |
| 1820 | .driver = { |
| 1821 | .name = "mt6358-misc", |
| 1822 | .of_match_table = mt6358_misc_of_match, |
| 1823 | }, |
| 1824 | .probe = mt6358_misc_probe, |
| 1825 | .shutdown = mt6358_misc_shutdown, |
| 1826 | }; |
| 1827 | |
| 1828 | module_platform_driver(mt6358_misc_driver); |
| 1829 | |
| 1830 | MODULE_LICENSE("GPL v2"); |
| 1831 | MODULE_AUTHOR("Wilma Wu <wilma.wu@mediatek.com>"); |
| 1832 | MODULE_DESCRIPTION("Misc Driver for MediaTek MT6358 PMIC"); |