rtc-sc27xx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. * Copyright (C) 2017 Spreadtrum Communications Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/rtc.h>
  14. #define SPRD_RTC_SEC_CNT_VALUE 0x0
  15. #define SPRD_RTC_MIN_CNT_VALUE 0x4
  16. #define SPRD_RTC_HOUR_CNT_VALUE 0x8
  17. #define SPRD_RTC_DAY_CNT_VALUE 0xc
  18. #define SPRD_RTC_SEC_CNT_UPD 0x10
  19. #define SPRD_RTC_MIN_CNT_UPD 0x14
  20. #define SPRD_RTC_HOUR_CNT_UPD 0x18
  21. #define SPRD_RTC_DAY_CNT_UPD 0x1c
  22. #define SPRD_RTC_SEC_ALM_UPD 0x20
  23. #define SPRD_RTC_MIN_ALM_UPD 0x24
  24. #define SPRD_RTC_HOUR_ALM_UPD 0x28
  25. #define SPRD_RTC_DAY_ALM_UPD 0x2c
  26. #define SPRD_RTC_INT_EN 0x30
  27. #define SPRD_RTC_INT_RAW_STS 0x34
  28. #define SPRD_RTC_INT_CLR 0x38
  29. #define SPRD_RTC_INT_MASK_STS 0x3C
  30. #define SPRD_RTC_SEC_ALM_VALUE 0x40
  31. #define SPRD_RTC_MIN_ALM_VALUE 0x44
  32. #define SPRD_RTC_HOUR_ALM_VALUE 0x48
  33. #define SPRD_RTC_DAY_ALM_VALUE 0x4c
  34. #define SPRD_RTC_SPG_VALUE 0x50
  35. #define SPRD_RTC_SPG_UPD 0x54
  36. #define SPRD_RTC_PWR_CTRL 0x58
  37. #define SPRD_RTC_PWR_STS 0x5c
  38. #define SPRD_RTC_SEC_AUXALM_UPD 0x60
  39. #define SPRD_RTC_MIN_AUXALM_UPD 0x64
  40. #define SPRD_RTC_HOUR_AUXALM_UPD 0x68
  41. #define SPRD_RTC_DAY_AUXALM_UPD 0x6c
  42. /* BIT & MASK definition for SPRD_RTC_INT_* registers */
  43. #define SPRD_RTC_SEC_EN BIT(0)
  44. #define SPRD_RTC_MIN_EN BIT(1)
  45. #define SPRD_RTC_HOUR_EN BIT(2)
  46. #define SPRD_RTC_DAY_EN BIT(3)
  47. #define SPRD_RTC_ALARM_EN BIT(4)
  48. #define SPRD_RTC_HRS_FORMAT_EN BIT(5)
  49. #define SPRD_RTC_AUXALM_EN BIT(6)
  50. #define SPRD_RTC_SPG_UPD_EN BIT(7)
  51. #define SPRD_RTC_SEC_UPD_EN BIT(8)
  52. #define SPRD_RTC_MIN_UPD_EN BIT(9)
  53. #define SPRD_RTC_HOUR_UPD_EN BIT(10)
  54. #define SPRD_RTC_DAY_UPD_EN BIT(11)
  55. #define SPRD_RTC_ALMSEC_UPD_EN BIT(12)
  56. #define SPRD_RTC_ALMMIN_UPD_EN BIT(13)
  57. #define SPRD_RTC_ALMHOUR_UPD_EN BIT(14)
  58. #define SPRD_RTC_ALMDAY_UPD_EN BIT(15)
  59. #define SPRD_RTC_INT_MASK GENMASK(15, 0)
  60. #define SPRD_RTC_TIME_INT_MASK \
  61. (SPRD_RTC_SEC_UPD_EN | SPRD_RTC_MIN_UPD_EN | \
  62. SPRD_RTC_HOUR_UPD_EN | SPRD_RTC_DAY_UPD_EN)
  63. #define SPRD_RTC_ALMTIME_INT_MASK \
  64. (SPRD_RTC_ALMSEC_UPD_EN | SPRD_RTC_ALMMIN_UPD_EN | \
  65. SPRD_RTC_ALMHOUR_UPD_EN | SPRD_RTC_ALMDAY_UPD_EN)
  66. #define SPRD_RTC_ALM_INT_MASK \
  67. (SPRD_RTC_SEC_EN | SPRD_RTC_MIN_EN | \
  68. SPRD_RTC_HOUR_EN | SPRD_RTC_DAY_EN | \
  69. SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN)
  70. /* second/minute/hour/day values mask definition */
  71. #define SPRD_RTC_SEC_MASK GENMASK(5, 0)
  72. #define SPRD_RTC_MIN_MASK GENMASK(5, 0)
  73. #define SPRD_RTC_HOUR_MASK GENMASK(4, 0)
  74. #define SPRD_RTC_DAY_MASK GENMASK(15, 0)
  75. /* alarm lock definition for SPRD_RTC_SPG_UPD register */
  76. #define SPRD_RTC_ALMLOCK_MASK GENMASK(7, 0)
  77. #define SPRD_RTC_ALM_UNLOCK 0xa5
  78. #define SPRD_RTC_ALM_LOCK (~SPRD_RTC_ALM_UNLOCK & \
  79. SPRD_RTC_ALMLOCK_MASK)
  80. /* SPG values definition for SPRD_RTC_SPG_UPD register */
  81. #define SPRD_RTC_POWEROFF_ALM_FLAG BIT(8)
  82. /* power control/status definition */
  83. #define SPRD_RTC_POWER_RESET_VALUE 0x96
  84. #define SPRD_RTC_POWER_STS_CLEAR GENMASK(7, 0)
  85. #define SPRD_RTC_POWER_STS_SHIFT 8
  86. #define SPRD_RTC_POWER_STS_VALID \
  87. (~SPRD_RTC_POWER_RESET_VALUE << SPRD_RTC_POWER_STS_SHIFT)
  88. /* timeout of synchronizing time and alarm registers (us) */
  89. #define SPRD_RTC_POLL_TIMEOUT 200000
  90. #define SPRD_RTC_POLL_DELAY_US 20000
  91. struct sprd_rtc {
  92. struct rtc_device *rtc;
  93. struct regmap *regmap;
  94. struct device *dev;
  95. u32 base;
  96. int irq;
  97. bool valid;
  98. };
  99. /*
  100. * The Spreadtrum RTC controller has 3 groups registers, including time, normal
  101. * alarm and auxiliary alarm. The time group registers are used to set RTC time,
  102. * the normal alarm registers are used to set normal alarm, and the auxiliary
  103. * alarm registers are used to set auxiliary alarm. Both alarm event and
  104. * auxiliary alarm event can wake up system from deep sleep, but only alarm
  105. * event can power up system from power down status.
  106. */
  107. enum sprd_rtc_reg_types {
  108. SPRD_RTC_TIME,
  109. SPRD_RTC_ALARM,
  110. SPRD_RTC_AUX_ALARM,
  111. };
  112. static int sprd_rtc_clear_alarm_ints(struct sprd_rtc *rtc)
  113. {
  114. return regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
  115. SPRD_RTC_ALM_INT_MASK);
  116. }
  117. static int sprd_rtc_disable_ints(struct sprd_rtc *rtc)
  118. {
  119. int ret;
  120. ret = regmap_update_bits(rtc->regmap, rtc->base + SPRD_RTC_INT_EN,
  121. SPRD_RTC_INT_MASK, 0);
  122. if (ret)
  123. return ret;
  124. return regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
  125. SPRD_RTC_INT_MASK);
  126. }
  127. static int sprd_rtc_lock_alarm(struct sprd_rtc *rtc, bool lock)
  128. {
  129. int ret;
  130. u32 val;
  131. ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_SPG_VALUE, &val);
  132. if (ret)
  133. return ret;
  134. val &= ~(SPRD_RTC_ALMLOCK_MASK | SPRD_RTC_POWEROFF_ALM_FLAG);
  135. if (lock)
  136. val |= SPRD_RTC_ALM_LOCK;
  137. else
  138. val |= SPRD_RTC_ALM_UNLOCK | SPRD_RTC_POWEROFF_ALM_FLAG;
  139. ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_SPG_UPD, val);
  140. if (ret)
  141. return ret;
  142. /* wait until the SPG value is updated successfully */
  143. ret = regmap_read_poll_timeout(rtc->regmap,
  144. rtc->base + SPRD_RTC_INT_RAW_STS, val,
  145. (val & SPRD_RTC_SPG_UPD_EN),
  146. SPRD_RTC_POLL_DELAY_US,
  147. SPRD_RTC_POLL_TIMEOUT);
  148. if (ret) {
  149. dev_err(rtc->dev, "failed to update SPG value:%d\n", ret);
  150. return ret;
  151. }
  152. return 0;
  153. }
  154. static int sprd_rtc_get_secs(struct sprd_rtc *rtc, enum sprd_rtc_reg_types type,
  155. time64_t *secs)
  156. {
  157. u32 sec_reg, min_reg, hour_reg, day_reg;
  158. u32 val, sec, min, hour, day;
  159. int ret;
  160. switch (type) {
  161. case SPRD_RTC_TIME:
  162. sec_reg = SPRD_RTC_SEC_CNT_VALUE;
  163. min_reg = SPRD_RTC_MIN_CNT_VALUE;
  164. hour_reg = SPRD_RTC_HOUR_CNT_VALUE;
  165. day_reg = SPRD_RTC_DAY_CNT_VALUE;
  166. break;
  167. case SPRD_RTC_ALARM:
  168. sec_reg = SPRD_RTC_SEC_ALM_VALUE;
  169. min_reg = SPRD_RTC_MIN_ALM_VALUE;
  170. hour_reg = SPRD_RTC_HOUR_ALM_VALUE;
  171. day_reg = SPRD_RTC_DAY_ALM_VALUE;
  172. break;
  173. case SPRD_RTC_AUX_ALARM:
  174. sec_reg = SPRD_RTC_SEC_AUXALM_UPD;
  175. min_reg = SPRD_RTC_MIN_AUXALM_UPD;
  176. hour_reg = SPRD_RTC_HOUR_AUXALM_UPD;
  177. day_reg = SPRD_RTC_DAY_AUXALM_UPD;
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. ret = regmap_read(rtc->regmap, rtc->base + sec_reg, &val);
  183. if (ret)
  184. return ret;
  185. sec = val & SPRD_RTC_SEC_MASK;
  186. ret = regmap_read(rtc->regmap, rtc->base + min_reg, &val);
  187. if (ret)
  188. return ret;
  189. min = val & SPRD_RTC_MIN_MASK;
  190. ret = regmap_read(rtc->regmap, rtc->base + hour_reg, &val);
  191. if (ret)
  192. return ret;
  193. hour = val & SPRD_RTC_HOUR_MASK;
  194. ret = regmap_read(rtc->regmap, rtc->base + day_reg, &val);
  195. if (ret)
  196. return ret;
  197. day = val & SPRD_RTC_DAY_MASK;
  198. *secs = (((time64_t)(day * 24) + hour) * 60 + min) * 60 + sec;
  199. return 0;
  200. }
  201. static int sprd_rtc_set_secs(struct sprd_rtc *rtc, enum sprd_rtc_reg_types type,
  202. time64_t secs)
  203. {
  204. u32 sec_reg, min_reg, hour_reg, day_reg, sts_mask;
  205. u32 sec, min, hour, day, val;
  206. int ret, rem;
  207. /* convert seconds to RTC time format */
  208. day = div_s64_rem(secs, 86400, &rem);
  209. hour = rem / 3600;
  210. rem -= hour * 3600;
  211. min = rem / 60;
  212. sec = rem - min * 60;
  213. switch (type) {
  214. case SPRD_RTC_TIME:
  215. sec_reg = SPRD_RTC_SEC_CNT_UPD;
  216. min_reg = SPRD_RTC_MIN_CNT_UPD;
  217. hour_reg = SPRD_RTC_HOUR_CNT_UPD;
  218. day_reg = SPRD_RTC_DAY_CNT_UPD;
  219. sts_mask = SPRD_RTC_TIME_INT_MASK;
  220. break;
  221. case SPRD_RTC_ALARM:
  222. sec_reg = SPRD_RTC_SEC_ALM_UPD;
  223. min_reg = SPRD_RTC_MIN_ALM_UPD;
  224. hour_reg = SPRD_RTC_HOUR_ALM_UPD;
  225. day_reg = SPRD_RTC_DAY_ALM_UPD;
  226. sts_mask = SPRD_RTC_ALMTIME_INT_MASK;
  227. break;
  228. case SPRD_RTC_AUX_ALARM:
  229. sec_reg = SPRD_RTC_SEC_AUXALM_UPD;
  230. min_reg = SPRD_RTC_MIN_AUXALM_UPD;
  231. hour_reg = SPRD_RTC_HOUR_AUXALM_UPD;
  232. day_reg = SPRD_RTC_DAY_AUXALM_UPD;
  233. sts_mask = 0;
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. ret = regmap_write(rtc->regmap, rtc->base + sec_reg, sec);
  239. if (ret)
  240. return ret;
  241. ret = regmap_write(rtc->regmap, rtc->base + min_reg, min);
  242. if (ret)
  243. return ret;
  244. ret = regmap_write(rtc->regmap, rtc->base + hour_reg, hour);
  245. if (ret)
  246. return ret;
  247. ret = regmap_write(rtc->regmap, rtc->base + day_reg, day);
  248. if (ret)
  249. return ret;
  250. if (type == SPRD_RTC_AUX_ALARM)
  251. return 0;
  252. /*
  253. * Since the time and normal alarm registers are put in always-power-on
  254. * region supplied by VDDRTC, then these registers changing time will
  255. * be very long, about 125ms. Thus here we should wait until all
  256. * values are updated successfully.
  257. */
  258. ret = regmap_read_poll_timeout(rtc->regmap,
  259. rtc->base + SPRD_RTC_INT_RAW_STS, val,
  260. ((val & sts_mask) == sts_mask),
  261. SPRD_RTC_POLL_DELAY_US,
  262. SPRD_RTC_POLL_TIMEOUT);
  263. if (ret < 0) {
  264. dev_err(rtc->dev, "set time/alarm values timeout\n");
  265. return ret;
  266. }
  267. return regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
  268. sts_mask);
  269. }
  270. static int sprd_rtc_read_aux_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  271. {
  272. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  273. time64_t secs;
  274. u32 val;
  275. int ret;
  276. ret = sprd_rtc_get_secs(rtc, SPRD_RTC_AUX_ALARM, &secs);
  277. if (ret)
  278. return ret;
  279. rtc_time64_to_tm(secs, &alrm->time);
  280. ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_EN, &val);
  281. if (ret)
  282. return ret;
  283. alrm->enabled = !!(val & SPRD_RTC_AUXALM_EN);
  284. ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_RAW_STS, &val);
  285. if (ret)
  286. return ret;
  287. alrm->pending = !!(val & SPRD_RTC_AUXALM_EN);
  288. return 0;
  289. }
  290. static int sprd_rtc_set_aux_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  291. {
  292. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  293. time64_t secs = rtc_tm_to_time64(&alrm->time);
  294. int ret;
  295. /* clear the auxiliary alarm interrupt status */
  296. ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
  297. SPRD_RTC_AUXALM_EN);
  298. if (ret)
  299. return ret;
  300. ret = sprd_rtc_set_secs(rtc, SPRD_RTC_AUX_ALARM, secs);
  301. if (ret)
  302. return ret;
  303. if (alrm->enabled) {
  304. ret = regmap_update_bits(rtc->regmap,
  305. rtc->base + SPRD_RTC_INT_EN,
  306. SPRD_RTC_AUXALM_EN,
  307. SPRD_RTC_AUXALM_EN);
  308. } else {
  309. ret = regmap_update_bits(rtc->regmap,
  310. rtc->base + SPRD_RTC_INT_EN,
  311. SPRD_RTC_AUXALM_EN, 0);
  312. }
  313. return ret;
  314. }
  315. static int sprd_rtc_read_time(struct device *dev, struct rtc_time *tm)
  316. {
  317. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  318. time64_t secs;
  319. int ret;
  320. if (!rtc->valid) {
  321. dev_warn(dev, "RTC values are invalid\n");
  322. return -EINVAL;
  323. }
  324. ret = sprd_rtc_get_secs(rtc, SPRD_RTC_TIME, &secs);
  325. if (ret)
  326. return ret;
  327. rtc_time64_to_tm(secs, tm);
  328. return 0;
  329. }
  330. static int sprd_rtc_set_time(struct device *dev, struct rtc_time *tm)
  331. {
  332. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  333. time64_t secs = rtc_tm_to_time64(tm);
  334. int ret;
  335. ret = sprd_rtc_set_secs(rtc, SPRD_RTC_TIME, secs);
  336. if (ret)
  337. return ret;
  338. if (!rtc->valid) {
  339. /* Clear RTC power status firstly */
  340. ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_PWR_CTRL,
  341. SPRD_RTC_POWER_STS_CLEAR);
  342. if (ret)
  343. return ret;
  344. /*
  345. * Set RTC power status to indicate now RTC has valid time
  346. * values.
  347. */
  348. ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_PWR_CTRL,
  349. SPRD_RTC_POWER_STS_VALID);
  350. if (ret)
  351. return ret;
  352. rtc->valid = true;
  353. }
  354. return 0;
  355. }
  356. static int sprd_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  357. {
  358. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  359. time64_t secs;
  360. int ret;
  361. u32 val;
  362. /*
  363. * If aie_timer is enabled, we should get the normal alarm time.
  364. * Otherwise we should get auxiliary alarm time.
  365. */
  366. if (rtc->rtc && rtc->rtc->aie_timer.enabled == 0)
  367. return sprd_rtc_read_aux_alarm(dev, alrm);
  368. ret = sprd_rtc_get_secs(rtc, SPRD_RTC_ALARM, &secs);
  369. if (ret)
  370. return ret;
  371. rtc_time64_to_tm(secs, &alrm->time);
  372. ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_EN, &val);
  373. if (ret)
  374. return ret;
  375. alrm->enabled = !!(val & SPRD_RTC_ALARM_EN);
  376. ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_INT_RAW_STS, &val);
  377. if (ret)
  378. return ret;
  379. alrm->pending = !!(val & SPRD_RTC_ALARM_EN);
  380. return 0;
  381. }
  382. static int sprd_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  383. {
  384. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  385. time64_t secs = rtc_tm_to_time64(&alrm->time);
  386. struct rtc_time aie_time =
  387. rtc_ktime_to_tm(rtc->rtc->aie_timer.node.expires);
  388. int ret;
  389. /*
  390. * We have 2 groups alarms: normal alarm and auxiliary alarm. Since
  391. * both normal alarm event and auxiliary alarm event can wake up system
  392. * from deep sleep, but only alarm event can power up system from power
  393. * down status. Moreover we do not need to poll about 125ms when
  394. * updating auxiliary alarm registers. Thus we usually set auxiliary
  395. * alarm when wake up system from deep sleep, and for other scenarios,
  396. * we should set normal alarm with polling status.
  397. *
  398. * So here we check if the alarm time is set by aie_timer, if yes, we
  399. * should set normal alarm, if not, we should set auxiliary alarm which
  400. * means it is just a wake event.
  401. */
  402. if (!rtc->rtc->aie_timer.enabled || rtc_tm_sub(&aie_time, &alrm->time))
  403. return sprd_rtc_set_aux_alarm(dev, alrm);
  404. /* clear the alarm interrupt status firstly */
  405. ret = regmap_write(rtc->regmap, rtc->base + SPRD_RTC_INT_CLR,
  406. SPRD_RTC_ALARM_EN);
  407. if (ret)
  408. return ret;
  409. ret = sprd_rtc_set_secs(rtc, SPRD_RTC_ALARM, secs);
  410. if (ret)
  411. return ret;
  412. if (alrm->enabled) {
  413. ret = regmap_update_bits(rtc->regmap,
  414. rtc->base + SPRD_RTC_INT_EN,
  415. SPRD_RTC_ALARM_EN,
  416. SPRD_RTC_ALARM_EN);
  417. if (ret)
  418. return ret;
  419. /* unlock the alarm to enable the alarm function. */
  420. ret = sprd_rtc_lock_alarm(rtc, false);
  421. } else {
  422. regmap_update_bits(rtc->regmap,
  423. rtc->base + SPRD_RTC_INT_EN,
  424. SPRD_RTC_ALARM_EN, 0);
  425. /*
  426. * Lock the alarm function in case fake alarm event will power
  427. * up systems.
  428. */
  429. ret = sprd_rtc_lock_alarm(rtc, true);
  430. }
  431. return ret;
  432. }
  433. static int sprd_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  434. {
  435. struct sprd_rtc *rtc = dev_get_drvdata(dev);
  436. int ret;
  437. if (enabled) {
  438. ret = regmap_update_bits(rtc->regmap,
  439. rtc->base + SPRD_RTC_INT_EN,
  440. SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN,
  441. SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN);
  442. if (ret)
  443. return ret;
  444. ret = sprd_rtc_lock_alarm(rtc, false);
  445. } else {
  446. regmap_update_bits(rtc->regmap, rtc->base + SPRD_RTC_INT_EN,
  447. SPRD_RTC_ALARM_EN | SPRD_RTC_AUXALM_EN, 0);
  448. ret = sprd_rtc_lock_alarm(rtc, true);
  449. }
  450. return ret;
  451. }
  452. static const struct rtc_class_ops sprd_rtc_ops = {
  453. .read_time = sprd_rtc_read_time,
  454. .set_time = sprd_rtc_set_time,
  455. .read_alarm = sprd_rtc_read_alarm,
  456. .set_alarm = sprd_rtc_set_alarm,
  457. .alarm_irq_enable = sprd_rtc_alarm_irq_enable,
  458. };
  459. static irqreturn_t sprd_rtc_handler(int irq, void *dev_id)
  460. {
  461. struct sprd_rtc *rtc = dev_id;
  462. int ret;
  463. ret = sprd_rtc_clear_alarm_ints(rtc);
  464. if (ret)
  465. return IRQ_RETVAL(ret);
  466. rtc_update_irq(rtc->rtc, 1, RTC_AF | RTC_IRQF);
  467. return IRQ_HANDLED;
  468. }
  469. static int sprd_rtc_check_power_down(struct sprd_rtc *rtc)
  470. {
  471. u32 val;
  472. int ret;
  473. ret = regmap_read(rtc->regmap, rtc->base + SPRD_RTC_PWR_STS, &val);
  474. if (ret)
  475. return ret;
  476. /*
  477. * If the RTC power status value is SPRD_RTC_POWER_RESET_VALUE, which
  478. * means the RTC has been powered down, so the RTC time values are
  479. * invalid.
  480. */
  481. rtc->valid = val == SPRD_RTC_POWER_RESET_VALUE ? false : true;
  482. return 0;
  483. }
  484. static int sprd_rtc_probe(struct platform_device *pdev)
  485. {
  486. struct device_node *node = pdev->dev.of_node;
  487. struct sprd_rtc *rtc;
  488. int ret;
  489. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  490. if (!rtc)
  491. return -ENOMEM;
  492. rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  493. if (!rtc->regmap)
  494. return -ENODEV;
  495. ret = of_property_read_u32(node, "reg", &rtc->base);
  496. if (ret) {
  497. dev_err(&pdev->dev, "failed to get RTC base address\n");
  498. return ret;
  499. }
  500. rtc->irq = platform_get_irq(pdev, 0);
  501. if (rtc->irq < 0) {
  502. dev_err(&pdev->dev, "failed to get RTC irq number\n");
  503. return rtc->irq;
  504. }
  505. rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
  506. if (IS_ERR(rtc->rtc))
  507. return PTR_ERR(rtc->rtc);
  508. rtc->dev = &pdev->dev;
  509. platform_set_drvdata(pdev, rtc);
  510. /* clear all RTC interrupts and disable all RTC interrupts */
  511. ret = sprd_rtc_disable_ints(rtc);
  512. if (ret) {
  513. dev_err(&pdev->dev, "failed to disable RTC interrupts\n");
  514. return ret;
  515. }
  516. /* check if RTC time values are valid */
  517. ret = sprd_rtc_check_power_down(rtc);
  518. if (ret) {
  519. dev_err(&pdev->dev, "failed to check RTC time values\n");
  520. return ret;
  521. }
  522. ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
  523. sprd_rtc_handler,
  524. IRQF_ONESHOT | IRQF_EARLY_RESUME,
  525. pdev->name, rtc);
  526. if (ret < 0) {
  527. dev_err(&pdev->dev, "failed to request RTC irq\n");
  528. return ret;
  529. }
  530. rtc->rtc->ops = &sprd_rtc_ops;
  531. rtc->rtc->range_min = 0;
  532. rtc->rtc->range_max = 5662310399LL;
  533. ret = rtc_register_device(rtc->rtc);
  534. if (ret) {
  535. dev_err(&pdev->dev, "failed to register rtc device\n");
  536. return ret;
  537. }
  538. device_init_wakeup(&pdev->dev, 1);
  539. return 0;
  540. }
  541. static int sprd_rtc_remove(struct platform_device *pdev)
  542. {
  543. device_init_wakeup(&pdev->dev, 0);
  544. return 0;
  545. }
  546. static const struct of_device_id sprd_rtc_of_match[] = {
  547. { .compatible = "sprd,sc2731-rtc", },
  548. { },
  549. };
  550. MODULE_DEVICE_TABLE(of, sprd_rtc_of_match);
  551. static struct platform_driver sprd_rtc_driver = {
  552. .driver = {
  553. .name = "sprd-rtc",
  554. .of_match_table = sprd_rtc_of_match,
  555. },
  556. .probe = sprd_rtc_probe,
  557. .remove = sprd_rtc_remove,
  558. };
  559. module_platform_driver(sprd_rtc_driver);
  560. MODULE_LICENSE("GPL v2");
  561. MODULE_DESCRIPTION("Spreadtrum RTC Device Driver");
  562. MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");