rtc-mt6397.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * Copyright (c) 2014-2015 MediaTek Inc.
  3. * Author: Tianping.Fang <tianping.fang@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  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. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/regmap.h>
  18. #include <linux/rtc.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/io.h>
  24. #include <linux/mfd/mt6397/core.h>
  25. #include <linux/reboot.h>
  26. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  27. #include "../misc/mediatek/include/mt-plat/mtk_boot_common.h"
  28. #endif
  29. #define RTC_BBPU 0x0000
  30. #define RTC_BBPU_CBUSY BIT(6)
  31. #define RTC_WRTGR 0x003c
  32. #define RTC_IRQ_STA 0x0002
  33. #define RTC_IRQ_STA_AL BIT(0)
  34. #define RTC_IRQ_STA_LP BIT(3)
  35. #define RTC_IRQ_EN 0x0004
  36. #define RTC_IRQ_EN_AL BIT(0)
  37. #define RTC_IRQ_EN_ONESHOT BIT(2)
  38. #define RTC_IRQ_EN_LP BIT(3)
  39. #define RTC_IRQ_EN_ONESHOT_AL (RTC_IRQ_EN_ONESHOT | RTC_IRQ_EN_AL)
  40. #define RTC_AL_YEA_MASK 0x007f
  41. #define RTC_AL_MTH_MASK 0x000f
  42. #define RTC_AL_DOM_MASK 0x001f
  43. #define RTC_AL_HOU_MASK 0x001f
  44. #define RTC_AL_MIN_MASK 0x003f
  45. #define RTC_AL_SEC_MASK 0x003f
  46. #define RTC_AL_MASK 0x0008
  47. #define RTC_AL_MASK_DOW BIT(4)
  48. #define RTC_TC_SEC 0x000a
  49. #define RTC_SPAR1 0x0032
  50. #define RTC_AL_SEC 0x0018
  51. /* Min, Hour, Dom... register offset to RTC_TC_SEC */
  52. #define RTC_OFFSET_SEC 0
  53. #define RTC_OFFSET_MIN 1
  54. #define RTC_OFFSET_HOUR 2
  55. #define RTC_OFFSET_DOM 3
  56. #define RTC_OFFSET_DOW 4
  57. #define RTC_OFFSET_MTH 5
  58. #define RTC_OFFSET_YEAR 6
  59. #define RTC_OFFSET_COUNT 7
  60. #define RTC_AL_SEC 0x0018
  61. #define RTC_AL_SEC_MASK 0x003f
  62. #define RTC_AL_MIN_MASK 0x003f
  63. #define RTC_AL_HOU_MASK 0x001f
  64. #define RTC_AL_DOM_MASK 0x001f
  65. #define RTC_AL_DOW_MASK 0x0007
  66. #define RTC_AL_MTH_MASK 0x000f
  67. #define RTC_AL_YEA_MASK 0x007f
  68. #define RTC_PDN2 0x002e
  69. #define RTC_PDN1 0x002c
  70. #define RTC_SPAR0 0x0030
  71. #define RTC_PDN2_PWRON_ALARM BIT(4)
  72. #define RTC_PDN1_PWRON_TIME BIT(7)
  73. #define RTC_PDN2_PWRON_LOGO BIT(15)
  74. #define RTC_BBPU_RELOAD BIT(5)
  75. #define RTC_BBPU_KEY (0x43 << 8)
  76. #define RTC_PWRON_YEA RTC_PDN2
  77. #define RTC_PWRON_YEA_MASK 0x7f00
  78. #define RTC_PWRON_YEA_SHIFT 8
  79. #define RTC_PWRON_MTH RTC_PDN2
  80. #define RTC_PWRON_MTH_MASK 0x000f
  81. #define RTC_PWRON_MTH_SHIFT 0
  82. #define RTC_PWRON_SEC RTC_SPAR0
  83. #define RTC_PWRON_SEC_MASK 0x003f
  84. #define RTC_PWRON_SEC_SHIFT 0
  85. #define RTC_SPAR0_ALARM_BOOT BIT(8)
  86. #define RTC_PWRON_MIN RTC_SPAR1
  87. #define RTC_PWRON_MIN_MASK 0x003f
  88. #define RTC_PWRON_MIN_SHIFT 0
  89. #define RTC_PWRON_HOU RTC_SPAR1
  90. #define RTC_PWRON_HOU_MASK 0x07c0
  91. #define RTC_PWRON_HOU_SHIFT 6
  92. #define RTC_PWRON_DOM RTC_SPAR1
  93. #define RTC_PWRON_DOM_MASK 0xf800
  94. #define RTC_PWRON_DOM_SHIFT 11
  95. #define RTC_MIN_YEAR 1968
  96. #define RTC_BASE_YEAR 1900
  97. #define RTC_NUM_YEARS 128
  98. #define RTC_MIN_YEAR_OFFSET (RTC_MIN_YEAR - RTC_BASE_YEAR)
  99. struct mt6397_rtc {
  100. struct device *dev;
  101. struct rtc_device *rtc_dev;
  102. struct mutex lock;
  103. struct regmap *regmap;
  104. int irq;
  105. u32 addr_base;
  106. };
  107. static struct mt6397_rtc *mt_rtc;
  108. static int mtk_rtc_write_trigger(struct mt6397_rtc *rtc)
  109. {
  110. unsigned long timeout = jiffies + HZ;
  111. int ret;
  112. u32 data;
  113. ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_WRTGR, 1);
  114. if (ret < 0)
  115. return ret;
  116. while (1) {
  117. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
  118. &data);
  119. if (ret < 0)
  120. break;
  121. if (!(data & RTC_BBPU_CBUSY))
  122. break;
  123. if (time_after(jiffies, timeout)) {
  124. ret = -ETIMEDOUT;
  125. break;
  126. }
  127. cpu_relax();
  128. }
  129. return ret;
  130. }
  131. static void _mtk_rtc_save_pwron_alarm(void)
  132. {
  133. u32 pdn1, pdn2;
  134. int ret;
  135. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN1, &pdn1);
  136. if (ret < 0)
  137. goto exit;
  138. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN2, &pdn2);
  139. if (ret < 0)
  140. goto exit;
  141. pdn1 &= ~RTC_PDN1_PWRON_TIME;
  142. pdn2 |= RTC_PDN2_PWRON_ALARM;
  143. ret = regmap_write(mt_rtc->regmap,
  144. mt_rtc->addr_base + RTC_PDN1, pdn1);
  145. if (ret < 0)
  146. goto exit;
  147. ret = regmap_write(mt_rtc->regmap,
  148. mt_rtc->addr_base + RTC_PDN2, pdn2);
  149. if (ret < 0)
  150. goto exit;
  151. mtk_rtc_write_trigger(mt_rtc);
  152. return;
  153. exit:
  154. dev_err(mt_rtc->dev, "%s regmap write/read error!!!\n", __func__);
  155. }
  156. static void _mtk_rtc_set_alarm(struct rtc_time *tm)
  157. {
  158. u16 data[RTC_OFFSET_COUNT], data_b[RTC_OFFSET_COUNT];
  159. int ret;
  160. ret = regmap_bulk_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_AL_SEC,
  161. data_b, RTC_OFFSET_COUNT);
  162. if (ret < 0)
  163. goto exit;
  164. data[RTC_OFFSET_SEC] = ((data_b[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK))
  165. | (tm->tm_sec & RTC_AL_SEC_MASK));
  166. data[RTC_OFFSET_MIN] = ((data_b[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK))
  167. | (tm->tm_min & RTC_AL_MIN_MASK));
  168. data[RTC_OFFSET_HOUR] = ((data_b[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK))
  169. | (tm->tm_hour & RTC_AL_HOU_MASK));
  170. data[RTC_OFFSET_DOM] = ((data_b[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK))
  171. | (tm->tm_mday & RTC_AL_DOM_MASK));
  172. data[RTC_OFFSET_MTH] = ((data_b[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK))
  173. | (tm->tm_mon & RTC_AL_MTH_MASK));
  174. data[RTC_OFFSET_YEAR] = ((data_b[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK))
  175. | (tm->tm_year & RTC_AL_YEA_MASK));
  176. dev_notice(mt_rtc->dev, "set al time = %04d/%02d/%02d %02d:%02d:%02d\n",
  177. tm->tm_year + RTC_MIN_YEAR, tm->tm_mon, tm->tm_mday,
  178. tm->tm_hour, tm->tm_min, tm->tm_sec);
  179. ret = regmap_bulk_write(mt_rtc->regmap,
  180. mt_rtc->addr_base + RTC_AL_SEC,
  181. data, RTC_OFFSET_COUNT);
  182. if (ret < 0)
  183. goto exit;
  184. ret = regmap_write(mt_rtc->regmap, mt_rtc->addr_base + RTC_AL_MASK,
  185. RTC_AL_MASK_DOW);
  186. if (ret < 0)
  187. goto exit;
  188. ret = regmap_update_bits(mt_rtc->regmap,
  189. mt_rtc->addr_base + RTC_IRQ_EN,
  190. RTC_IRQ_EN_ONESHOT_AL,
  191. RTC_IRQ_EN_ONESHOT_AL);
  192. if (ret < 0)
  193. goto exit;
  194. mtk_rtc_write_trigger(mt_rtc);
  195. return;
  196. exit:
  197. dev_err(mt_rtc->dev, "regmap write/read error!!!\n");
  198. }
  199. static void _rtc_get_tick(struct rtc_time *tm)
  200. {
  201. int ret;
  202. u16 data[RTC_OFFSET_COUNT];
  203. ret = regmap_bulk_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_TC_SEC,
  204. data, RTC_OFFSET_COUNT);
  205. if (ret < 0)
  206. goto exit;
  207. tm->tm_sec = data[RTC_OFFSET_SEC];
  208. tm->tm_min = data[RTC_OFFSET_MIN];
  209. tm->tm_hour = data[RTC_OFFSET_HOUR];
  210. tm->tm_mday = data[RTC_OFFSET_DOM];
  211. tm->tm_mon = data[RTC_OFFSET_MTH];
  212. tm->tm_year = data[RTC_OFFSET_YEAR];
  213. return;
  214. exit:
  215. dev_err(mt_rtc->dev, "%s regmap write/read error!!!\n", __func__);
  216. }
  217. static void _mtk_rtc_get_tick_time(struct rtc_time *tm)
  218. {
  219. u32 bbpu, sec;
  220. int ret;
  221. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_BBPU, &bbpu);
  222. if (ret < 0)
  223. goto exit;
  224. bbpu |= RTC_BBPU_KEY | RTC_BBPU_RELOAD;
  225. ret = regmap_write(mt_rtc->regmap, mt_rtc->addr_base + RTC_BBPU, bbpu);
  226. if (ret < 0)
  227. goto exit;
  228. mtk_rtc_write_trigger(mt_rtc);
  229. _rtc_get_tick(tm);
  230. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_TC_SEC, &sec);
  231. if (ret < 0)
  232. goto exit;
  233. if (sec < tm->tm_sec) { /* SEC has carried */
  234. _rtc_get_tick(tm);
  235. }
  236. return;
  237. exit:
  238. dev_err(mt_rtc->dev, "%s regmap write/read error!!!\n", __func__);
  239. }
  240. static void _mtk_rtc_get_pwron_alarm_time(struct rtc_time *tm)
  241. {
  242. u32 spar1, pdn2, spar0;
  243. int ret;
  244. /*RTC_PWRON_SEC == SPAR0 */
  245. ret = regmap_read(mt_rtc->regmap,
  246. mt_rtc->addr_base + RTC_SPAR0, &spar0);
  247. if (ret < 0)
  248. goto exit;
  249. /*RTC_PWRON_DOM == RTC_PWRON_HOU */
  250. /*== RTC_PWRON_MIN == RTC_SPAR1*/
  251. ret = regmap_read(mt_rtc->regmap,
  252. mt_rtc->addr_base + RTC_SPAR1, &spar1);
  253. if (ret < 0)
  254. goto exit;
  255. /*RTC_PWRON_MTH == RTC_PWRON_YEAR== SPAR0 */
  256. ret = regmap_read(mt_rtc->regmap,
  257. mt_rtc->addr_base + RTC_PDN2, &pdn2);
  258. if (ret < 0)
  259. goto exit;
  260. dev_notice(mt_rtc->dev, "spar0=0x%x, spar1=0x%x, pdn2=0x%x!!!\n",
  261. spar0, spar1, pdn2);
  262. tm->tm_sec = (spar0 & RTC_PWRON_SEC_MASK) >> RTC_PWRON_SEC_SHIFT;
  263. tm->tm_min = (spar1 & RTC_PWRON_MIN_MASK) >> RTC_PWRON_MIN_SHIFT;
  264. tm->tm_hour = (spar1 & RTC_PWRON_HOU_MASK) >> RTC_PWRON_HOU_SHIFT;
  265. tm->tm_mday = (spar1 & RTC_PWRON_DOM_MASK) >> RTC_PWRON_DOM_SHIFT;
  266. tm->tm_mon = (pdn2 & RTC_PWRON_MTH_MASK) >> RTC_PWRON_MTH_SHIFT;
  267. tm->tm_year = (pdn2 & RTC_PWRON_YEA_MASK) >> RTC_PWRON_YEA_SHIFT;
  268. dev_notice(mt_rtc->dev,
  269. "year=0x%x,mon=0x%x,mday =0x%x hou=0x%x,min=0x%x,sec=0x%x\n",
  270. tm->tm_year, tm->tm_mon, tm->tm_mday,
  271. tm->tm_hour, tm->tm_min, tm->tm_sec);
  272. return;
  273. exit:
  274. dev_err(mt_rtc->dev, "%s regmap write/read error!!!\n", __func__);
  275. }
  276. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  277. static void _mtk_rtc_set_alarm_boot(void)
  278. {
  279. u32 spar0;
  280. int ret;
  281. ret = regmap_read(mt_rtc->regmap,
  282. mt_rtc->addr_base + RTC_SPAR0, &spar0);
  283. if (ret < 0)
  284. goto exit;
  285. spar0 |= RTC_SPAR0_ALARM_BOOT;
  286. ret = regmap_write(mt_rtc->regmap,
  287. mt_rtc->addr_base + RTC_SPAR0, spar0);
  288. if (ret < 0)
  289. goto exit;
  290. mtk_rtc_write_trigger(mt_rtc);
  291. return;
  292. exit:
  293. dev_err(mt_rtc->dev, "regmap write/read error!!!\n");
  294. }
  295. #endif
  296. static bool _mtk_rtc_is_pwron_alarm(struct rtc_time *nowtm, struct rtc_time *tm)
  297. {
  298. u32 pdn1, sec;
  299. int ret;
  300. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN1, &pdn1);
  301. if (ret < 0)
  302. goto exit;
  303. dev_notice(mt_rtc->dev, "pdn1 = 0x%x!!!\n", pdn1);
  304. /* power-on time is available */
  305. if (pdn1 & RTC_PDN1_PWRON_TIME) {
  306. _mtk_rtc_get_tick_time(nowtm);
  307. ret = regmap_read(mt_rtc->regmap,
  308. mt_rtc->addr_base + RTC_TC_SEC, &sec);
  309. if (ret < 0)
  310. goto exit;
  311. if (sec < nowtm->tm_sec) { /* SEC has carried */
  312. _mtk_rtc_get_tick_time(nowtm);
  313. }
  314. _mtk_rtc_get_pwron_alarm_time(tm);
  315. return true;
  316. }
  317. return false;
  318. exit:
  319. dev_err(mt_rtc->dev, "%s regmap write/read error!!!\n", __func__);
  320. return false;
  321. }
  322. static irqreturn_t mtk_rtc_irq_handler_thread(int irq, void *data)
  323. {
  324. struct mt6397_rtc *rtc = data;
  325. u32 irqsta, irqen;
  326. int ret;
  327. bool pwron_alm = false, pwron_alarm = false;
  328. struct rtc_time nowtm;
  329. struct rtc_time tm;
  330. mutex_lock(&rtc->lock);
  331. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_STA, &irqsta);
  332. if ((ret >= 0) && (irqsta & RTC_IRQ_STA_AL)) {
  333. pwron_alarm = _mtk_rtc_is_pwron_alarm(&nowtm, &tm);
  334. nowtm.tm_year += RTC_MIN_YEAR;
  335. tm.tm_year += RTC_MIN_YEAR;
  336. dev_notice(mt_rtc->dev, "[RTC] nowtm = %d/%d/%d %d:%d:%d\n",
  337. nowtm.tm_year, nowtm.tm_mon, nowtm.tm_mday,
  338. nowtm.tm_hour, nowtm.tm_min, nowtm.tm_sec);
  339. if (pwron_alarm) {
  340. unsigned long now_time, time;
  341. now_time = mktime(nowtm.tm_year,
  342. nowtm.tm_mon,
  343. nowtm.tm_mday,
  344. nowtm.tm_hour,
  345. nowtm.tm_min,
  346. nowtm.tm_sec);
  347. time = mktime(tm.tm_year,
  348. tm.tm_mon,
  349. tm.tm_mday,
  350. tm.tm_hour,
  351. tm.tm_min,
  352. tm.tm_sec);
  353. /* power on */
  354. if (now_time >= time - 1 && now_time <= time + 4) {
  355. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  356. if (get_boot_mode() ==
  357. KERNEL_POWER_OFF_CHARGING_BOOT
  358. || get_boot_mode() ==
  359. LOW_POWER_OFF_CHARGING_BOOT) {
  360. dev_notice(mt_rtc->dev,
  361. "KPOC alarm!!!\n");
  362. time += 1;
  363. rtc_time_to_tm(time, &tm);
  364. tm.tm_year -= RTC_MIN_YEAR_OFFSET;
  365. tm.tm_mon += 1;
  366. /* tm.tm_sec += 1; */
  367. _mtk_rtc_set_alarm(&tm);
  368. _mtk_rtc_set_alarm_boot();
  369. mutex_unlock(&rtc->lock);
  370. machine_restart(NULL);
  371. } else {
  372. _mtk_rtc_save_pwron_alarm();
  373. pwron_alm = true;
  374. }
  375. #else
  376. _mtk_rtc_save_pwron_alarm();
  377. pwron_alm = true;
  378. #endif
  379. } else if (now_time < time) { /* set power-on alarm */
  380. tm.tm_year -= RTC_MIN_YEAR;
  381. dev_notice(mt_rtc->dev,
  382. "KPOC alarm again!!!\n");
  383. if (tm.tm_sec == 0) {
  384. tm.tm_sec = 59;
  385. tm.tm_min -= 1;
  386. } else {
  387. tm.tm_sec -= 1;
  388. }
  389. _mtk_rtc_set_alarm(&tm);
  390. }
  391. }
  392. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  393. irqen = irqsta & ~RTC_IRQ_EN_AL;
  394. if (regmap_write(rtc->regmap, rtc->addr_base + RTC_IRQ_EN,
  395. irqen) == 0)
  396. mtk_rtc_write_trigger(rtc);
  397. mutex_unlock(&rtc->lock);
  398. return IRQ_HANDLED;
  399. }
  400. mutex_unlock(&rtc->lock);
  401. return IRQ_NONE;
  402. }
  403. static int __mtk_rtc_read_time(struct mt6397_rtc *rtc,
  404. struct rtc_time *tm, int *sec)
  405. {
  406. int ret;
  407. u16 data[RTC_OFFSET_COUNT];
  408. mutex_lock(&rtc->lock);
  409. ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
  410. data, RTC_OFFSET_COUNT);
  411. if (ret < 0)
  412. goto exit;
  413. tm->tm_sec = data[RTC_OFFSET_SEC];
  414. tm->tm_min = data[RTC_OFFSET_MIN];
  415. tm->tm_hour = data[RTC_OFFSET_HOUR];
  416. tm->tm_mday = data[RTC_OFFSET_DOM];
  417. tm->tm_mon = data[RTC_OFFSET_MTH];
  418. tm->tm_year = data[RTC_OFFSET_YEAR];
  419. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec);
  420. exit:
  421. mutex_unlock(&rtc->lock);
  422. return ret;
  423. }
  424. void rtc_read_pwron_alarm(struct rtc_wkalrm *alm)
  425. {
  426. struct rtc_time *tm;
  427. u32 pdn1, pdn2;
  428. int ret;
  429. u16 data[RTC_OFFSET_COUNT];
  430. if (alm == NULL)
  431. return;
  432. dev_notice(mt_rtc->dev, "%s!!!\n", __func__);
  433. tm = &alm->time;
  434. mutex_lock(&mt_rtc->lock);
  435. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN1, &pdn1);
  436. if (ret < 0)
  437. goto exit;
  438. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN2, &pdn2);
  439. if (ret < 0)
  440. goto exit;
  441. ret = regmap_bulk_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_AL_SEC,
  442. data, RTC_OFFSET_COUNT);
  443. if (ret < 0)
  444. goto exit;
  445. alm->enabled = (pdn1 & RTC_PDN1_PWRON_TIME
  446. ? (pdn2 & RTC_PDN2_PWRON_LOGO ? 3 : 2) : 0);
  447. /* return Power-On Alarm bit */
  448. alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM);
  449. tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK;
  450. tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK;
  451. tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK;
  452. tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK;
  453. tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK;
  454. tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK;
  455. mutex_unlock(&mt_rtc->lock);
  456. tm->tm_year += RTC_MIN_YEAR_OFFSET;
  457. tm->tm_mon--;
  458. dev_notice(mt_rtc->dev,
  459. "power-on = %04d/%02d/%02d %02d:%02d:%02d (%d)(%d)\n",
  460. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
  461. tm->tm_min, tm->tm_sec, alm->enabled, alm->pending);
  462. return;
  463. exit:
  464. mutex_unlock(&mt_rtc->lock);
  465. dev_err(mt_rtc->dev, "regmap write/read error!!!\n");
  466. }
  467. static void _rtc_set_pwron_alarm_time(struct rtc_time *tm)
  468. {
  469. u32 spar1, pdn2, spar0;
  470. int ret;
  471. u32 tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec;
  472. dev_notice(mt_rtc->dev, "_rtc_save_pwron_time!!!\n");
  473. /*RTC_PWRON_YEAR == RTC_PWRON_MTH==PDN2 */
  474. ret = regmap_read(mt_rtc->regmap,
  475. mt_rtc->addr_base + RTC_PDN2, &pdn2);
  476. if (ret < 0)
  477. goto exit;
  478. /*RTC_PWRON_DOM == RTC_PWRON_HOU */
  479. /*== RTC_PWRON_MIN == RTC_SPAR1*/
  480. ret = regmap_read(mt_rtc->regmap,
  481. mt_rtc->addr_base + RTC_SPAR1, &spar1);
  482. if (ret < 0)
  483. goto exit;
  484. ret = regmap_read(mt_rtc->regmap,
  485. mt_rtc->addr_base + RTC_SPAR0, &spar0);
  486. if (ret < 0)
  487. goto exit;
  488. tm_year = (tm->tm_year << RTC_PWRON_YEA_SHIFT) & RTC_PWRON_YEA_MASK;
  489. tm_mon = (tm->tm_mon << RTC_PWRON_MTH_SHIFT) & RTC_PWRON_MTH_MASK;
  490. tm_mday = (tm->tm_mday << RTC_PWRON_DOM_SHIFT) & RTC_PWRON_DOM_MASK;
  491. tm_hour = (tm->tm_hour << RTC_PWRON_HOU_SHIFT) & RTC_PWRON_HOU_MASK;
  492. tm_min = (tm->tm_min << RTC_PWRON_MIN_SHIFT) & RTC_PWRON_MIN_MASK;
  493. tm_sec = (tm->tm_sec << RTC_PWRON_SEC_SHIFT) & RTC_PWRON_SEC_MASK;
  494. tm_year |= pdn2 & ~(RTC_PWRON_YEA_MASK);
  495. ret = regmap_write(mt_rtc->regmap,
  496. mt_rtc->addr_base + RTC_PDN2, tm_year);
  497. if (ret < 0)
  498. goto exit;
  499. mtk_rtc_write_trigger(mt_rtc);
  500. ret = regmap_read(mt_rtc->regmap,
  501. mt_rtc->addr_base + RTC_PDN2, &pdn2);
  502. if (ret < 0)
  503. goto exit;
  504. tm_mon |= pdn2 & ~(RTC_PWRON_MTH_MASK);
  505. ret = regmap_write(mt_rtc->regmap,
  506. mt_rtc->addr_base + RTC_PDN2, tm_mon);
  507. if (ret < 0)
  508. goto exit;
  509. mtk_rtc_write_trigger(mt_rtc);
  510. tm_mday |= spar1 & ~(RTC_PWRON_DOM_MASK);
  511. ret = regmap_write(mt_rtc->regmap,
  512. mt_rtc->addr_base + RTC_SPAR1, tm_mday);
  513. if (ret < 0)
  514. goto exit;
  515. mtk_rtc_write_trigger(mt_rtc);
  516. ret = regmap_read(mt_rtc->regmap,
  517. mt_rtc->addr_base + RTC_SPAR1, &spar1);
  518. if (ret < 0)
  519. goto exit;
  520. tm_hour |= spar1 & ~(RTC_PWRON_HOU_MASK);
  521. ret = regmap_write(mt_rtc->regmap,
  522. mt_rtc->addr_base + RTC_SPAR1, tm_hour);
  523. if (ret < 0)
  524. goto exit;
  525. mtk_rtc_write_trigger(mt_rtc);
  526. ret = regmap_read(mt_rtc->regmap,
  527. mt_rtc->addr_base + RTC_SPAR1, &spar1);
  528. if (ret < 0)
  529. goto exit;
  530. tm_min |= spar1 & ~(RTC_PWRON_MIN_MASK);
  531. ret = regmap_write(mt_rtc->regmap,
  532. mt_rtc->addr_base + RTC_SPAR1, tm_min);
  533. if (ret < 0)
  534. goto exit;
  535. mtk_rtc_write_trigger(mt_rtc);
  536. tm_sec |= spar0 & ~(RTC_PWRON_SEC_MASK);
  537. ret = regmap_write(mt_rtc->regmap,
  538. mt_rtc->addr_base + RTC_SPAR0, tm_sec);
  539. if (ret < 0)
  540. goto exit;
  541. mtk_rtc_write_trigger(mt_rtc);
  542. return;
  543. exit:
  544. dev_err(mt_rtc->dev, "regmap write/read error!!!\n");
  545. }
  546. void mtk_rtc_save_pwron_time(bool enable, struct rtc_time *tm, bool logo)
  547. {
  548. u32 pdn1, pdn2;
  549. int ret;
  550. dev_notice(mt_rtc->dev, "%s!!!\n", __func__);
  551. _rtc_set_pwron_alarm_time(tm);
  552. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN2, &pdn2);
  553. if (ret < 0)
  554. goto exit;
  555. ret = regmap_read(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN1, &pdn1);
  556. if (ret < 0)
  557. goto exit;
  558. if (logo)
  559. pdn2 |= RTC_PDN2_PWRON_LOGO;
  560. else
  561. pdn2 &= ~RTC_PDN2_PWRON_LOGO;
  562. ret = regmap_write(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN2, pdn2);
  563. if (ret < 0)
  564. goto exit;
  565. if (enable)
  566. pdn1 |= RTC_PDN1_PWRON_TIME;
  567. else
  568. pdn1 &= ~RTC_PDN1_PWRON_TIME;
  569. ret = regmap_write(mt_rtc->regmap, mt_rtc->addr_base + RTC_PDN1, pdn1);
  570. if (ret < 0)
  571. goto exit;
  572. mtk_rtc_write_trigger(mt_rtc);
  573. return;
  574. exit:
  575. dev_err(mt_rtc->dev, "regmap write/read error!!!\n");
  576. }
  577. static int mtk_rtc_read_time(struct device *dev, struct rtc_time *tm)
  578. {
  579. time64_t time;
  580. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  581. int days, sec, ret;
  582. do {
  583. ret = __mtk_rtc_read_time(rtc, tm, &sec);
  584. if (ret < 0)
  585. goto exit;
  586. } while (sec < tm->tm_sec);
  587. /* HW register use 7 bits to store year data, minus
  588. * RTC_MIN_YEAR_OFFSET before write year data to register, and plus
  589. * RTC_MIN_YEAR_OFFSET back after read year from register
  590. */
  591. tm->tm_year += RTC_MIN_YEAR_OFFSET;
  592. /* HW register start mon from one, but tm_mon start from zero. */
  593. tm->tm_mon--;
  594. time = rtc_tm_to_time64(tm);
  595. /* rtc_tm_to_time64 covert Gregorian date to seconds since
  596. * 01-01-1970 00:00:00, and this date is Thursday.
  597. */
  598. days = div_s64(time, 86400);
  599. tm->tm_wday = (days + 4) % 7;
  600. exit:
  601. return ret;
  602. }
  603. static int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm)
  604. {
  605. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  606. int ret;
  607. u16 data[RTC_OFFSET_COUNT];
  608. tm->tm_year -= RTC_MIN_YEAR_OFFSET;
  609. tm->tm_mon++;
  610. data[RTC_OFFSET_SEC] = tm->tm_sec;
  611. data[RTC_OFFSET_MIN] = tm->tm_min;
  612. data[RTC_OFFSET_HOUR] = tm->tm_hour;
  613. data[RTC_OFFSET_DOM] = tm->tm_mday;
  614. data[RTC_OFFSET_MTH] = tm->tm_mon;
  615. data[RTC_OFFSET_YEAR] = tm->tm_year;
  616. mutex_lock(&rtc->lock);
  617. ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
  618. data, RTC_OFFSET_COUNT);
  619. if (ret < 0)
  620. goto exit;
  621. /* Time register write to hardware after call trigger function */
  622. ret = mtk_rtc_write_trigger(rtc);
  623. exit:
  624. mutex_unlock(&rtc->lock);
  625. return ret;
  626. }
  627. static int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  628. {
  629. struct rtc_time *tm = &alm->time;
  630. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  631. u32 irqen, pdn2;
  632. int ret;
  633. u16 data[RTC_OFFSET_COUNT];
  634. mutex_lock(&rtc->lock);
  635. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, &irqen);
  636. if (ret < 0)
  637. goto err_exit;
  638. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_PDN2, &pdn2);
  639. if (ret < 0)
  640. goto err_exit;
  641. ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
  642. data, RTC_OFFSET_COUNT);
  643. if (ret < 0)
  644. goto err_exit;
  645. alm->enabled = !!(irqen & RTC_IRQ_EN_AL);
  646. alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM);
  647. mutex_unlock(&rtc->lock);
  648. tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK;
  649. tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK;
  650. tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK;
  651. tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK;
  652. tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK;
  653. tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK;
  654. tm->tm_year += RTC_MIN_YEAR_OFFSET;
  655. tm->tm_mon--;
  656. return 0;
  657. err_exit:
  658. mutex_unlock(&rtc->lock);
  659. return ret;
  660. }
  661. static int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  662. {
  663. struct rtc_time *tm = &alm->time;
  664. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  665. int ret;
  666. u16 data[RTC_OFFSET_COUNT];
  667. u32 irqsta, irqen, pdn2;
  668. tm->tm_year -= RTC_MIN_YEAR_OFFSET;
  669. tm->tm_mon++;
  670. ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
  671. data, RTC_OFFSET_COUNT);
  672. if (ret < 0)
  673. goto exit;
  674. data[RTC_OFFSET_SEC] = ((data[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK)) |
  675. (tm->tm_sec & RTC_AL_SEC_MASK));
  676. data[RTC_OFFSET_MIN] = ((data[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK)) |
  677. (tm->tm_min & RTC_AL_MIN_MASK));
  678. data[RTC_OFFSET_HOUR] = ((data[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK)) |
  679. (tm->tm_hour & RTC_AL_HOU_MASK));
  680. data[RTC_OFFSET_DOM] = ((data[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK)) |
  681. (tm->tm_mday & RTC_AL_DOM_MASK));
  682. data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) |
  683. (tm->tm_mon & RTC_AL_MTH_MASK));
  684. data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) |
  685. (tm->tm_year & RTC_AL_YEA_MASK));
  686. dev_notice(rtc->dev,
  687. "set al time = %04d/%02d/%02d %02d:%02d:%02d (%d)\n",
  688. tm->tm_year + RTC_MIN_YEAR, tm->tm_mon, tm->tm_mday,
  689. tm->tm_hour, tm->tm_min, tm->tm_sec, alm->enabled);
  690. mutex_lock(&rtc->lock);
  691. switch (alm->enabled) {
  692. case 2:
  693. /* enable power-on alarm */
  694. mtk_rtc_save_pwron_time(true, tm, false);
  695. break;
  696. case 3:
  697. /* enable power-on alarm with logo */
  698. mtk_rtc_save_pwron_time(true, tm, true);
  699. break;
  700. case 4:
  701. /* disable power-on alarm */
  702. mtk_rtc_save_pwron_time(false, tm, false);
  703. break;
  704. default:
  705. break;
  706. }
  707. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, &irqen);
  708. if (ret < 0)
  709. goto exit;
  710. irqen &= ~RTC_IRQ_EN_AL;
  711. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_PDN2, &pdn2);
  712. if (ret < 0)
  713. goto exit;
  714. pdn2 &= ~RTC_PDN2_PWRON_ALARM;
  715. ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, irqen);
  716. if (ret < 0)
  717. goto exit;
  718. ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_PDN2, pdn2);
  719. if (ret < 0)
  720. goto exit;
  721. mtk_rtc_write_trigger(rtc);
  722. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_STA, &irqsta);
  723. if (ret < 0)
  724. goto exit;
  725. if (alm->enabled) {
  726. ret = regmap_bulk_write(rtc->regmap,
  727. rtc->addr_base + RTC_AL_SEC,
  728. data, RTC_OFFSET_COUNT);
  729. if (ret < 0)
  730. goto exit;
  731. ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_AL_MASK,
  732. RTC_AL_MASK_DOW);
  733. if (ret < 0)
  734. goto exit;
  735. ret = regmap_update_bits(rtc->regmap,
  736. rtc->addr_base + RTC_IRQ_EN,
  737. RTC_IRQ_EN_ONESHOT_AL,
  738. RTC_IRQ_EN_ONESHOT_AL);
  739. if (ret < 0)
  740. goto exit;
  741. } else {
  742. ret = regmap_update_bits(rtc->regmap,
  743. rtc->addr_base + RTC_IRQ_EN,
  744. RTC_IRQ_EN_ONESHOT_AL, 0);
  745. if (ret < 0)
  746. goto exit;
  747. }
  748. /* All alarm time register write to hardware after calling
  749. * mtk_rtc_write_trigger. This can avoid race condition if alarm
  750. * occur happen during writing alarm time register.
  751. */
  752. ret = mtk_rtc_write_trigger(rtc);
  753. exit:
  754. mutex_unlock(&rtc->lock);
  755. return ret;
  756. }
  757. static const struct rtc_class_ops mtk_rtc_ops = {
  758. .read_time = mtk_rtc_read_time,
  759. .set_time = mtk_rtc_set_time,
  760. .read_alarm = mtk_rtc_read_alarm,
  761. .set_alarm = mtk_rtc_set_alarm,
  762. };
  763. static int mtk_rtc_probe(struct platform_device *pdev)
  764. {
  765. struct resource *res;
  766. struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent);
  767. struct mt6397_rtc *rtc;
  768. int ret;
  769. rtc = devm_kzalloc(&pdev->dev, sizeof(struct mt6397_rtc), GFP_KERNEL);
  770. if (!rtc)
  771. return -ENOMEM;
  772. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  773. rtc->addr_base = res->start;
  774. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  775. rtc->irq = irq_create_mapping(mt6397_chip->irq_domain, res->start);
  776. if (rtc->irq <= 0)
  777. return -EINVAL;
  778. rtc->regmap = mt6397_chip->regmap;
  779. rtc->dev = &pdev->dev;
  780. mutex_init(&rtc->lock);
  781. mt_rtc = rtc;
  782. platform_set_drvdata(pdev, rtc);
  783. rtc->rtc_dev = devm_rtc_allocate_device(rtc->dev);
  784. if (IS_ERR(rtc->rtc_dev))
  785. return PTR_ERR(rtc->rtc_dev);
  786. ret = request_threaded_irq(rtc->irq, NULL,
  787. mtk_rtc_irq_handler_thread,
  788. IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
  789. "mt6397-rtc", rtc);
  790. if (ret) {
  791. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  792. rtc->irq, ret);
  793. goto out_dispose_irq;
  794. }
  795. device_init_wakeup(&pdev->dev, 1);
  796. rtc->rtc_dev->ops = &mtk_rtc_ops;
  797. ret = rtc_register_device(rtc->rtc_dev);
  798. if (ret) {
  799. dev_err(&pdev->dev, "register rtc device failed\n");
  800. goto out_free_irq;
  801. }
  802. return 0;
  803. out_free_irq:
  804. free_irq(rtc->irq, rtc->rtc_dev);
  805. out_dispose_irq:
  806. irq_dispose_mapping(rtc->irq);
  807. return ret;
  808. }
  809. static int mtk_rtc_remove(struct platform_device *pdev)
  810. {
  811. struct mt6397_rtc *rtc = platform_get_drvdata(pdev);
  812. free_irq(rtc->irq, rtc->rtc_dev);
  813. irq_dispose_mapping(rtc->irq);
  814. return 0;
  815. }
  816. #ifdef CONFIG_PM_SLEEP
  817. static int mt6397_rtc_suspend(struct device *dev)
  818. {
  819. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  820. if (device_may_wakeup(dev))
  821. enable_irq_wake(rtc->irq);
  822. return 0;
  823. }
  824. static int mt6397_rtc_resume(struct device *dev)
  825. {
  826. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  827. if (device_may_wakeup(dev))
  828. disable_irq_wake(rtc->irq);
  829. return 0;
  830. }
  831. #endif
  832. static SIMPLE_DEV_PM_OPS(mt6397_pm_ops, mt6397_rtc_suspend,
  833. mt6397_rtc_resume);
  834. static const struct of_device_id mt6397_rtc_of_match[] = {
  835. { .compatible = "mediatek,mt6397-rtc", },
  836. { .compatible = "mediatek,mt6323-rtc", },
  837. { .compatible = "mediatek,mt6392-rtc", },
  838. { }
  839. };
  840. MODULE_DEVICE_TABLE(of, mt6397_rtc_of_match);
  841. static struct platform_driver mtk_rtc_driver = {
  842. .driver = {
  843. .name = "mt6397-rtc",
  844. .of_match_table = mt6397_rtc_of_match,
  845. .pm = &mt6397_pm_ops,
  846. },
  847. .probe = mtk_rtc_probe,
  848. .remove = mtk_rtc_remove,
  849. };
  850. module_platform_driver(mtk_rtc_driver);
  851. MODULE_LICENSE("GPL v2");
  852. MODULE_AUTHOR("Tianping Fang <tianping.fang@mediatek.com>");
  853. MODULE_DESCRIPTION("RTC Driver for MediaTek MT6397 PMIC");
  854. MODULE_ALIAS("platform:mt6397-rtc");