mtk_rtc_hal.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (C) 2010 MediaTek, Inc.
  3. *
  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. #ifdef pr_fmt
  16. #undef pr_fmt
  17. #endif
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/rtc.h>
  24. #include <mach/upmu_hw.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/delay.h>
  29. #include <linux/types.h>
  30. #include <mach/irqs.h>
  31. #include <mach/mtk_rtc_hal.h>
  32. #include <mtk_rtc_hal_common.h>
  33. #include <mach/mt_rtc_hw.h>
  34. #include <mt_pmic_wrap.h>
  35. #if defined CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  36. #include <mt_boot.h>
  37. #endif
  38. #include <mt_gpio.h>
  39. #include <mt-plat/charging.h>
  40. #define hal_rtc_xinfo(fmt, args...) \
  41. pr_notice(fmt, ##args)
  42. #define hal_rtc_xerror(fmt, args...) \
  43. pr_err(fmt, ##args)
  44. #define hal_rtc_xfatal(fmt, args...) \
  45. pr_emerg(fmt, ##args)
  46. /* Causion, for SRCLKENA drop speed too slow (align VIO18) to cause current leakage for 32K less */
  47. #define GPIO_SRCLKEN_PIN (148 | 0x80000000)
  48. /*TODO extern bool pmic_chrdet_status(void);*/
  49. /*
  50. RTC_FGSOC = 0,
  51. RTC_ANDROID,
  52. RTC_RECOVERY,
  53. RTC_FAC_RESET,
  54. RTC_BYPASS_PWR,
  55. RTC_PWRON_TIME,
  56. RTC_FAST_BOOT,
  57. RTC_KPOC,
  58. RTC_DEBUG,
  59. RTC_PWRON_AL,
  60. RTC_UART,
  61. RTC_AUTOBOOT,
  62. RTC_PWRON_LOGO,
  63. RTC_32K_LESS,
  64. RTC_LP_DET,
  65. RTC_SPAR_NUM
  66. */
  67. /*
  68. * RTC_PDN1:
  69. * bit 0 - 3 : Android bits
  70. * bit 4 - 5 : Recovery bits (0x10: factory data reset)
  71. * bit 6 : Bypass PWRKEY bit
  72. * bit 7 : Power-On Time bit
  73. * bit 8 : RTC_GPIO_USER_WIFI bit
  74. * bit 9 : RTC_GPIO_USER_GPS bit
  75. * bit 10 : RTC_GPIO_USER_BT bit
  76. * bit 11 : RTC_GPIO_USER_FM bit
  77. * bit 12 : RTC_GPIO_USER_PMIC bit
  78. * bit 13 : Fast Boot
  79. * bit 14 : Kernel Power Off Charging
  80. * bit 15 : Debug bit
  81. */
  82. /*
  83. * RTC_PDN2:
  84. * bit 0 - 3 : MTH in power-on time
  85. * bit 4 : Power-On Alarm bit
  86. * bit 5 - 6 : UART bits
  87. * bit 7 : autoboot bit
  88. * bit 8 - 14: YEA in power-on time
  89. * bit 15 : Power-On Logo bit
  90. */
  91. /*
  92. * RTC_SPAR0:
  93. * bit 0 - 5 : SEC in power-on time
  94. * bit 6 : 32K less bit. True:with 32K, False:Without 32K
  95. * bit 7 - 15: reserved bits
  96. */
  97. u16 rtc_spare_reg[][3] = {
  98. {RTC_AL_HOU, 0x7f, 8},
  99. {RTC_PDN1, 0xf, 0},
  100. {RTC_PDN1, 0x3, 4},
  101. {RTC_PDN1, 0x1, 6},
  102. {RTC_PDN1, 0x1, 7},
  103. {RTC_PDN1, 0x1, 13},
  104. {RTC_PDN1, 0x1, 14},
  105. {RTC_PDN1, 0x1, 15},
  106. {RTC_PDN2, 0x1, 4},
  107. {RTC_PDN2, 0x3, 5},
  108. {RTC_PDN2, 0x1, 7},
  109. {RTC_PDN2, 0x1, 15},
  110. {RTC_SPAR0, 0x1, 6},
  111. {RTC_SPAR0, 0x1, 7}
  112. };
  113. void hal_rtc_set_abb_32k(u16 enable)
  114. {
  115. hal_rtc_xinfo("ABB 32k not support\n");
  116. }
  117. u16 hal_rtc_get_gpio_32k_status(void)
  118. {
  119. u16 con;
  120. con = rtc_read(RTC_CON);
  121. hal_rtc_xinfo("RTC_GPIO 32k status(RTC_CON=0x%x)\n", con);
  122. if (con & RTC_CON_F32KOB)
  123. return 0;
  124. else
  125. return 1;
  126. }
  127. void hal_rtc_set_gpio_32k_status(u16 user, bool enable)
  128. {
  129. u16 con, pdn1;
  130. if (enable) {
  131. pdn1 = rtc_read(RTC_PDN1);
  132. } else {
  133. pdn1 = rtc_read(RTC_PDN1) & ~(1U << user);
  134. rtc_write(RTC_PDN1, pdn1);
  135. rtc_write_trigger();
  136. }
  137. con = rtc_read(RTC_CON);
  138. if (enable) {
  139. con &= ~RTC_CON_F32KOB;
  140. } else {
  141. if (!(pdn1 & RTC_GPIO_USER_MASK)) { /* no users */
  142. con |= RTC_CON_F32KOB;
  143. }
  144. }
  145. rtc_write(RTC_CON, con);
  146. rtc_write_trigger();
  147. if (enable) {
  148. pdn1 |= (1U << user);
  149. rtc_write(RTC_PDN1, pdn1);
  150. rtc_write_trigger();
  151. }
  152. hal_rtc_xinfo("RTC_GPIO user %d enable = %d 32k (0x%x)\n", user, enable, pdn1);
  153. }
  154. void hal_rtc_bbpu_pwdn(void)
  155. {
  156. u16 ret_val, con;
  157. /* disable 32K export if there are no RTC_GPIO users */
  158. if (!(rtc_read(RTC_PDN1) & RTC_GPIO_USER_MASK)) {
  159. con = rtc_read(RTC_CON) | RTC_CON_F32KOB;
  160. rtc_write(RTC_CON, con);
  161. rtc_write_trigger();
  162. }
  163. ret_val = hal_rtc_get_spare_register(RTC_32K_LESS);
  164. #if !defined(CONFIG_MTK_FPGA)
  165. if (!ret_val && pmic_chrdet_status() == KAL_FALSE) {
  166. /* 1. Set SRCLKENAs GPIO GPIO as Output Mode, Output Low */
  167. mt_set_gpio_dir(GPIO_SRCLKEN_PIN, GPIO_DIR_OUT);
  168. mt_set_gpio_out(GPIO_SRCLKEN_PIN, GPIO_OUT_ZERO);
  169. /* 2. pull PWRBB low */
  170. rtc_bbpu_pwrdown(true);
  171. /* 3. Switch SRCLKENAs GPIO MUX function to GPIO Mode */
  172. mt_set_gpio_mode(GPIO_SRCLKEN_PIN, GPIO_MODE_GPIO);
  173. } else
  174. #endif
  175. {
  176. rtc_bbpu_pwrdown(true);
  177. }
  178. }
  179. void hal_rtc_get_pwron_alarm(struct rtc_time *tm, struct rtc_wkalrm *alm)
  180. {
  181. u16 pdn1, pdn2;
  182. pdn1 = rtc_read(RTC_PDN1);
  183. pdn2 = rtc_read(RTC_PDN2);
  184. alm->enabled = (pdn1 & RTC_PDN1_PWRON_TIME ? (pdn2 & RTC_PDN2_PWRON_LOGO ? 3 : 2) : 0);
  185. alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM); /* return Power-On Alarm bit */
  186. hal_rtc_get_alarm_time(tm);
  187. }
  188. bool hal_rtc_is_lp_irq(void)
  189. {
  190. u16 irqsta;
  191. irqsta = rtc_read(RTC_IRQ_STA); /* read clear */
  192. if (unlikely(!(irqsta & RTC_IRQ_STA_AL))) {
  193. #ifndef USER_BUILD_KERNEL
  194. if (irqsta & RTC_IRQ_STA_LP)
  195. rtc_lp_exception();
  196. #endif
  197. return true;
  198. }
  199. return false;
  200. }
  201. bool hal_rtc_is_pwron_alarm(struct rtc_time *nowtm, struct rtc_time *tm)
  202. {
  203. u16 pdn1;
  204. pdn1 = rtc_read(RTC_PDN1);
  205. hal_rtc_xinfo("pdn1 = 0x%4x\n", pdn1);
  206. if (pdn1 & RTC_PDN1_PWRON_TIME) { /* power-on time is available */
  207. hal_rtc_xinfo("pdn1 = 0x%4x\n", pdn1);
  208. hal_rtc_get_tick_time(nowtm);
  209. hal_rtc_xinfo("pdn1 = 0x%4x\n", pdn1);
  210. if (rtc_read(RTC_TC_SEC) < nowtm->tm_sec) { /* SEC has carried */
  211. hal_rtc_get_tick_time(nowtm);
  212. }
  213. hal_rtc_get_pwron_alarm_time(tm);
  214. return true;
  215. }
  216. return false;
  217. }
  218. void hal_rtc_get_alarm(struct rtc_time *tm, struct rtc_wkalrm *alm)
  219. {
  220. u16 irqen, pdn2;
  221. irqen = rtc_read(RTC_IRQ_EN);
  222. hal_rtc_get_alarm_time(tm);
  223. pdn2 = rtc_read(RTC_PDN2);
  224. alm->enabled = !!(irqen & RTC_IRQ_EN_AL);
  225. alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM); /* return Power-On Alarm bit */
  226. }
  227. void hal_rtc_set_alarm(struct rtc_time *tm)
  228. {
  229. u16 irqen;
  230. hal_rtc_set_alarm_time(tm);
  231. irqen = rtc_read(RTC_IRQ_EN) | RTC_IRQ_EN_ONESHOT_AL;
  232. rtc_write(RTC_IRQ_EN, irqen);
  233. rtc_write_trigger();
  234. }
  235. void hal_rtc_clear_alarm(struct rtc_time *tm)
  236. {
  237. u16 irqsta, irqen, pdn2;
  238. irqen = rtc_read(RTC_IRQ_EN) & ~RTC_IRQ_EN_AL;
  239. pdn2 = rtc_read(RTC_PDN2) & ~RTC_PDN2_PWRON_ALARM;
  240. rtc_write(RTC_IRQ_EN, irqen);
  241. rtc_write(RTC_PDN2, pdn2);
  242. rtc_write_trigger();
  243. irqsta = rtc_read(RTC_IRQ_STA); /* read clear */
  244. hal_rtc_set_alarm_time(tm);
  245. }
  246. void hal_rtc_set_lp_irq(void)
  247. {
  248. u16 irqen;
  249. #ifndef USER_BUILD_KERNEL
  250. irqen = rtc_read(RTC_IRQ_EN) | RTC_IRQ_EN_LP;
  251. #else
  252. irqen = rtc_read(RTC_IRQ_EN) & ~RTC_IRQ_EN_LP;
  253. #endif
  254. rtc_write(RTC_IRQ_EN, irqen);
  255. rtc_write_trigger();
  256. }
  257. void hal_rtc_save_pwron_time(bool enable, struct rtc_time *tm, bool logo)
  258. {
  259. u16 pdn1, pdn2;
  260. hal_rtc_set_pwron_alarm_time(tm);
  261. if (logo)
  262. pdn2 = rtc_read(RTC_PDN2) | RTC_PDN2_PWRON_LOGO;
  263. else
  264. pdn2 = rtc_read(RTC_PDN2) & ~RTC_PDN2_PWRON_LOGO;
  265. rtc_write(RTC_PDN2, pdn2);
  266. if (enable)
  267. pdn1 = rtc_read(RTC_PDN1) | RTC_PDN1_PWRON_TIME;
  268. else
  269. pdn1 = rtc_read(RTC_PDN1) & ~RTC_PDN1_PWRON_TIME;
  270. rtc_write(RTC_PDN1, pdn1);
  271. rtc_write_trigger();
  272. }
  273. #ifdef VRTC_PWM_ENABLE
  274. void hal_rtc_pwm_enable(void)
  275. {
  276. rtc_write(MT_VRTC_PWM_CON0, 0); /*clear*/
  277. hal_rtc_xinfo("hal_rtc_pwm_enable(), RTC_CAP_SEL=%d\n", RTC_CAP_SEL);
  278. switch (RTC_CAP_SEL) {
  279. case 0: /*0.1uF only*/
  280. rtc_write(MT_VRTC_PWM_CON0,
  281. VRTC_PWM_H_DUTY_0_64_MS | VRTC_PWM_L_DUTY_6_4_MS | VRTC_PWM_MODE);
  282. hal_rtc_xinfo("H=0.64ms,L=6.4ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  283. break;
  284. case 1: /*0.1uF + 1uF + 1.5Kohm*/
  285. rtc_write(MT_VRTC_PWM_CON0,
  286. VRTC_PWM_H_DUTY_5_12_MS | VRTC_PWM_L_DUTY_51_2_MS | VRTC_PWM_MODE);
  287. hal_rtc_xinfo("H=5.12ms,L=51.2ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  288. break;
  289. case 2: /*0.1uF + 2.2uF + 1.5Kohm*/
  290. rtc_write(MT_VRTC_PWM_CON0,
  291. VTRC_CAP_SEL | VRTC_PWM_H_DUTY_25_6_MS | VRTC_PWM_L_DUTY_128_0_MS |
  292. VRTC_PWM_MODE);
  293. hal_rtc_xinfo("H=25.6ms,L=128ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  294. break;
  295. case 3: /*0.1uF + 4.7uF + 1.5Kohm*/
  296. rtc_write(MT_VRTC_PWM_CON0,
  297. VTRC_CAP_SEL | VRTC_PWM_H_DUTY_51_2_MS | VRTC_PWM_L_DUTY_256_0_MS |
  298. VRTC_PWM_MODE);
  299. hal_rtc_xinfo("H=51.2ms,L=256ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  300. break;
  301. case 4: /*0.1uF + 10uF + 1.5Kohm*/
  302. rtc_write(MT_VRTC_PWM_CON0,
  303. VTRC_CAP_SEL | VRTC_PWM_H_DUTY_102_4_MS | VRTC_PWM_L_DUTY_512_0_MS |
  304. VRTC_PWM_MODE);
  305. hal_rtc_xinfo("H=102.4ms,L=512ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  306. break;
  307. case 5: /*0.1uF + 22uF + 1.5Kohm*/
  308. rtc_write(MT_VRTC_PWM_CON0,
  309. VTRC_CAP_SEL | VRTC_PWM_H_DUTY_204_8_MS | VRTC_PWM_L_DUTY_1024_0_MS |
  310. VRTC_PWM_MODE);
  311. hal_rtc_xinfo("H=204.8ms,L=1024ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  312. break;
  313. case 6: /*0.1uF + super cap(>>22uF) + 1.5Kohm*/
  314. rtc_write(MT_VRTC_PWM_CON0,
  315. VTRC_CAP_SEL | VRTC_PWM_H_DUTY_204_8_MS | VRTC_PWM_L_DUTY_1024_0_MS |
  316. VRTC_PWM_MODE);
  317. hal_rtc_xinfo("H=204.8ms,L=1024ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  318. break;
  319. case 7: /*0.1uF + little Li battery + 1.5Kohm*/
  320. rtc_write(MT_VRTC_PWM_CON0,
  321. VTRC_CAP_SEL | VRTC_PWM_H_DUTY_204_8_MS | VRTC_PWM_L_DUTY_512_0_MS |
  322. VRTC_PWM_MODE);
  323. hal_rtc_xinfo("H=204.8ms,L=512ms, VRTC_PWM=%x\n", rtc_read(MT_VRTC_PWM_CON0));
  324. break;
  325. default:
  326. hal_rtc_xinfo("RTC CAP SEL is wrong !!!!");
  327. break;
  328. }
  329. }
  330. #endif