mtk_rtc_hal.c 9.7 KB

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