rtc-mt6397.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #define RTC_BBPU 0x0000
  26. #define RTC_BBPU_CBUSY BIT(6)
  27. #define RTC_WRTGR 0x003c
  28. #define RTC_IRQ_STA 0x0002
  29. #define RTC_IRQ_STA_AL BIT(0)
  30. #define RTC_IRQ_STA_LP BIT(3)
  31. #define RTC_IRQ_EN 0x0004
  32. #define RTC_IRQ_EN_AL BIT(0)
  33. #define RTC_IRQ_EN_ONESHOT BIT(2)
  34. #define RTC_IRQ_EN_LP BIT(3)
  35. #define RTC_IRQ_EN_ONESHOT_AL (RTC_IRQ_EN_ONESHOT | RTC_IRQ_EN_AL)
  36. #define RTC_AL_MASK 0x0008
  37. #define RTC_AL_MASK_DOW BIT(4)
  38. #define RTC_TC_SEC 0x000a
  39. /* Min, Hour, Dom... register offset to RTC_TC_SEC */
  40. #define RTC_OFFSET_SEC 0
  41. #define RTC_OFFSET_MIN 1
  42. #define RTC_OFFSET_HOUR 2
  43. #define RTC_OFFSET_DOM 3
  44. #define RTC_OFFSET_DOW 4
  45. #define RTC_OFFSET_MTH 5
  46. #define RTC_OFFSET_YEAR 6
  47. #define RTC_OFFSET_COUNT 7
  48. #define RTC_AL_SEC 0x0018
  49. #define RTC_AL_SEC_MASK 0x003f
  50. #define RTC_AL_MIN_MASK 0x003f
  51. #define RTC_AL_HOU_MASK 0x001f
  52. #define RTC_AL_DOM_MASK 0x001f
  53. #define RTC_AL_DOW_MASK 0x0007
  54. #define RTC_AL_MTH_MASK 0x000f
  55. #define RTC_AL_YEA_MASK 0x007f
  56. #define RTC_PDN2 0x002e
  57. #define RTC_PDN2_PWRON_ALARM BIT(4)
  58. #define RTC_MIN_YEAR 1968
  59. #define RTC_BASE_YEAR 1900
  60. #define RTC_NUM_YEARS 128
  61. #define RTC_MIN_YEAR_OFFSET (RTC_MIN_YEAR - RTC_BASE_YEAR)
  62. struct mt6397_rtc {
  63. struct device *dev;
  64. struct rtc_device *rtc_dev;
  65. struct mutex lock;
  66. struct regmap *regmap;
  67. int irq;
  68. u32 addr_base;
  69. };
  70. static int mtk_rtc_write_trigger(struct mt6397_rtc *rtc)
  71. {
  72. unsigned long timeout = jiffies + HZ;
  73. int ret;
  74. u32 data;
  75. ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_WRTGR, 1);
  76. if (ret < 0)
  77. return ret;
  78. while (1) {
  79. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_BBPU,
  80. &data);
  81. if (ret < 0)
  82. break;
  83. if (!(data & RTC_BBPU_CBUSY))
  84. break;
  85. if (time_after(jiffies, timeout)) {
  86. ret = -ETIMEDOUT;
  87. break;
  88. }
  89. cpu_relax();
  90. }
  91. return ret;
  92. }
  93. static irqreturn_t mtk_rtc_irq_handler_thread(int irq, void *data)
  94. {
  95. struct mt6397_rtc *rtc = data;
  96. u32 irqsta, irqen;
  97. int ret;
  98. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_STA, &irqsta);
  99. if ((ret >= 0) && (irqsta & RTC_IRQ_STA_AL)) {
  100. rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF);
  101. irqen = irqsta & ~RTC_IRQ_EN_AL;
  102. mutex_lock(&rtc->lock);
  103. if (regmap_write(rtc->regmap, rtc->addr_base + RTC_IRQ_EN,
  104. irqen) == 0)
  105. mtk_rtc_write_trigger(rtc);
  106. mutex_unlock(&rtc->lock);
  107. return IRQ_HANDLED;
  108. }
  109. return IRQ_NONE;
  110. }
  111. static int __mtk_rtc_read_time(struct mt6397_rtc *rtc,
  112. struct rtc_time *tm, int *sec)
  113. {
  114. int ret;
  115. u16 data[RTC_OFFSET_COUNT];
  116. mutex_lock(&rtc->lock);
  117. ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
  118. data, RTC_OFFSET_COUNT);
  119. if (ret < 0)
  120. goto exit;
  121. tm->tm_sec = data[RTC_OFFSET_SEC];
  122. tm->tm_min = data[RTC_OFFSET_MIN];
  123. tm->tm_hour = data[RTC_OFFSET_HOUR];
  124. tm->tm_mday = data[RTC_OFFSET_DOM];
  125. tm->tm_mon = data[RTC_OFFSET_MTH];
  126. tm->tm_year = data[RTC_OFFSET_YEAR];
  127. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_TC_SEC, sec);
  128. exit:
  129. mutex_unlock(&rtc->lock);
  130. return ret;
  131. }
  132. static int mtk_rtc_read_time(struct device *dev, struct rtc_time *tm)
  133. {
  134. time64_t time;
  135. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  136. int days, sec, ret;
  137. do {
  138. ret = __mtk_rtc_read_time(rtc, tm, &sec);
  139. if (ret < 0)
  140. goto exit;
  141. } while (sec < tm->tm_sec);
  142. /* HW register use 7 bits to store year data, minus
  143. * RTC_MIN_YEAR_OFFSET before write year data to register, and plus
  144. * RTC_MIN_YEAR_OFFSET back after read year from register
  145. */
  146. tm->tm_year += RTC_MIN_YEAR_OFFSET;
  147. /* HW register start mon from one, but tm_mon start from zero. */
  148. tm->tm_mon--;
  149. time = rtc_tm_to_time64(tm);
  150. /* rtc_tm_to_time64 covert Gregorian date to seconds since
  151. * 01-01-1970 00:00:00, and this date is Thursday.
  152. */
  153. days = div_s64(time, 86400);
  154. tm->tm_wday = (days + 4) % 7;
  155. exit:
  156. return ret;
  157. }
  158. static int mtk_rtc_set_time(struct device *dev, struct rtc_time *tm)
  159. {
  160. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  161. int ret;
  162. u16 data[RTC_OFFSET_COUNT];
  163. tm->tm_year -= RTC_MIN_YEAR_OFFSET;
  164. tm->tm_mon++;
  165. data[RTC_OFFSET_SEC] = tm->tm_sec;
  166. data[RTC_OFFSET_MIN] = tm->tm_min;
  167. data[RTC_OFFSET_HOUR] = tm->tm_hour;
  168. data[RTC_OFFSET_DOM] = tm->tm_mday;
  169. data[RTC_OFFSET_MTH] = tm->tm_mon;
  170. data[RTC_OFFSET_YEAR] = tm->tm_year;
  171. mutex_lock(&rtc->lock);
  172. ret = regmap_bulk_write(rtc->regmap, rtc->addr_base + RTC_TC_SEC,
  173. data, RTC_OFFSET_COUNT);
  174. if (ret < 0)
  175. goto exit;
  176. /* Time register write to hardware after call trigger function */
  177. ret = mtk_rtc_write_trigger(rtc);
  178. exit:
  179. mutex_unlock(&rtc->lock);
  180. return ret;
  181. }
  182. static int mtk_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  183. {
  184. struct rtc_time *tm = &alm->time;
  185. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  186. u32 irqen, pdn2;
  187. int ret;
  188. u16 data[RTC_OFFSET_COUNT];
  189. mutex_lock(&rtc->lock);
  190. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_IRQ_EN, &irqen);
  191. if (ret < 0)
  192. goto err_exit;
  193. ret = regmap_read(rtc->regmap, rtc->addr_base + RTC_PDN2, &pdn2);
  194. if (ret < 0)
  195. goto err_exit;
  196. ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
  197. data, RTC_OFFSET_COUNT);
  198. if (ret < 0)
  199. goto err_exit;
  200. alm->enabled = !!(irqen & RTC_IRQ_EN_AL);
  201. alm->pending = !!(pdn2 & RTC_PDN2_PWRON_ALARM);
  202. mutex_unlock(&rtc->lock);
  203. tm->tm_sec = data[RTC_OFFSET_SEC] & RTC_AL_SEC_MASK;
  204. tm->tm_min = data[RTC_OFFSET_MIN] & RTC_AL_MIN_MASK;
  205. tm->tm_hour = data[RTC_OFFSET_HOUR] & RTC_AL_HOU_MASK;
  206. tm->tm_mday = data[RTC_OFFSET_DOM] & RTC_AL_DOM_MASK;
  207. tm->tm_mon = data[RTC_OFFSET_MTH] & RTC_AL_MTH_MASK;
  208. tm->tm_year = data[RTC_OFFSET_YEAR] & RTC_AL_YEA_MASK;
  209. tm->tm_year += RTC_MIN_YEAR_OFFSET;
  210. tm->tm_mon--;
  211. return 0;
  212. err_exit:
  213. mutex_unlock(&rtc->lock);
  214. return ret;
  215. }
  216. static int mtk_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  217. {
  218. struct rtc_time *tm = &alm->time;
  219. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  220. int ret;
  221. u16 data[RTC_OFFSET_COUNT];
  222. tm->tm_year -= RTC_MIN_YEAR_OFFSET;
  223. tm->tm_mon++;
  224. mutex_lock(&rtc->lock);
  225. ret = regmap_bulk_read(rtc->regmap, rtc->addr_base + RTC_AL_SEC,
  226. data, RTC_OFFSET_COUNT);
  227. if (ret < 0)
  228. goto exit;
  229. data[RTC_OFFSET_SEC] = ((data[RTC_OFFSET_SEC] & ~(RTC_AL_SEC_MASK)) |
  230. (tm->tm_sec & RTC_AL_SEC_MASK));
  231. data[RTC_OFFSET_MIN] = ((data[RTC_OFFSET_MIN] & ~(RTC_AL_MIN_MASK)) |
  232. (tm->tm_min & RTC_AL_MIN_MASK));
  233. data[RTC_OFFSET_HOUR] = ((data[RTC_OFFSET_HOUR] & ~(RTC_AL_HOU_MASK)) |
  234. (tm->tm_hour & RTC_AL_HOU_MASK));
  235. data[RTC_OFFSET_DOM] = ((data[RTC_OFFSET_DOM] & ~(RTC_AL_DOM_MASK)) |
  236. (tm->tm_mday & RTC_AL_DOM_MASK));
  237. data[RTC_OFFSET_MTH] = ((data[RTC_OFFSET_MTH] & ~(RTC_AL_MTH_MASK)) |
  238. (tm->tm_mon & RTC_AL_MTH_MASK));
  239. data[RTC_OFFSET_YEAR] = ((data[RTC_OFFSET_YEAR] & ~(RTC_AL_YEA_MASK)) |
  240. (tm->tm_year & RTC_AL_YEA_MASK));
  241. if (alm->enabled) {
  242. ret = regmap_bulk_write(rtc->regmap,
  243. rtc->addr_base + RTC_AL_SEC,
  244. data, RTC_OFFSET_COUNT);
  245. if (ret < 0)
  246. goto exit;
  247. ret = regmap_write(rtc->regmap, rtc->addr_base + RTC_AL_MASK,
  248. RTC_AL_MASK_DOW);
  249. if (ret < 0)
  250. goto exit;
  251. ret = regmap_update_bits(rtc->regmap,
  252. rtc->addr_base + RTC_IRQ_EN,
  253. RTC_IRQ_EN_ONESHOT_AL,
  254. RTC_IRQ_EN_ONESHOT_AL);
  255. if (ret < 0)
  256. goto exit;
  257. } else {
  258. ret = regmap_update_bits(rtc->regmap,
  259. rtc->addr_base + RTC_IRQ_EN,
  260. RTC_IRQ_EN_ONESHOT_AL, 0);
  261. if (ret < 0)
  262. goto exit;
  263. }
  264. /* All alarm time register write to hardware after calling
  265. * mtk_rtc_write_trigger. This can avoid race condition if alarm
  266. * occur happen during writing alarm time register.
  267. */
  268. ret = mtk_rtc_write_trigger(rtc);
  269. exit:
  270. mutex_unlock(&rtc->lock);
  271. return ret;
  272. }
  273. static const struct rtc_class_ops mtk_rtc_ops = {
  274. .read_time = mtk_rtc_read_time,
  275. .set_time = mtk_rtc_set_time,
  276. .read_alarm = mtk_rtc_read_alarm,
  277. .set_alarm = mtk_rtc_set_alarm,
  278. };
  279. static int mtk_rtc_probe(struct platform_device *pdev)
  280. {
  281. struct resource *res;
  282. struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent);
  283. struct mt6397_rtc *rtc;
  284. int ret;
  285. rtc = devm_kzalloc(&pdev->dev, sizeof(struct mt6397_rtc), GFP_KERNEL);
  286. if (!rtc)
  287. return -ENOMEM;
  288. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  289. rtc->addr_base = res->start;
  290. rtc->irq = platform_get_irq(pdev, 0);
  291. if (rtc->irq < 0)
  292. return rtc->irq;
  293. rtc->regmap = mt6397_chip->regmap;
  294. rtc->dev = &pdev->dev;
  295. mutex_init(&rtc->lock);
  296. platform_set_drvdata(pdev, rtc);
  297. rtc->rtc_dev = devm_rtc_allocate_device(rtc->dev);
  298. if (IS_ERR(rtc->rtc_dev))
  299. return PTR_ERR(rtc->rtc_dev);
  300. ret = request_threaded_irq(rtc->irq, NULL,
  301. mtk_rtc_irq_handler_thread,
  302. IRQF_ONESHOT | IRQF_TRIGGER_HIGH,
  303. "mt6397-rtc", rtc);
  304. if (ret) {
  305. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  306. rtc->irq, ret);
  307. return ret;
  308. }
  309. device_init_wakeup(&pdev->dev, 1);
  310. rtc->rtc_dev->ops = &mtk_rtc_ops;
  311. ret = rtc_register_device(rtc->rtc_dev);
  312. if (ret) {
  313. dev_err(&pdev->dev, "register rtc device failed\n");
  314. goto out_free_irq;
  315. }
  316. return 0;
  317. out_free_irq:
  318. free_irq(rtc->irq, rtc);
  319. return ret;
  320. }
  321. static int mtk_rtc_remove(struct platform_device *pdev)
  322. {
  323. struct mt6397_rtc *rtc = platform_get_drvdata(pdev);
  324. free_irq(rtc->irq, rtc);
  325. return 0;
  326. }
  327. #ifdef CONFIG_PM_SLEEP
  328. static int mt6397_rtc_suspend(struct device *dev)
  329. {
  330. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  331. if (device_may_wakeup(dev))
  332. enable_irq_wake(rtc->irq);
  333. return 0;
  334. }
  335. static int mt6397_rtc_resume(struct device *dev)
  336. {
  337. struct mt6397_rtc *rtc = dev_get_drvdata(dev);
  338. if (device_may_wakeup(dev))
  339. disable_irq_wake(rtc->irq);
  340. return 0;
  341. }
  342. #endif
  343. static SIMPLE_DEV_PM_OPS(mt6397_pm_ops, mt6397_rtc_suspend,
  344. mt6397_rtc_resume);
  345. static const struct of_device_id mt6397_rtc_of_match[] = {
  346. { .compatible = "mediatek,mt6397-rtc", },
  347. { }
  348. };
  349. MODULE_DEVICE_TABLE(of, mt6397_rtc_of_match);
  350. static struct platform_driver mtk_rtc_driver = {
  351. .driver = {
  352. .name = "mt6397-rtc",
  353. .of_match_table = mt6397_rtc_of_match,
  354. .pm = &mt6397_pm_ops,
  355. },
  356. .probe = mtk_rtc_probe,
  357. .remove = mtk_rtc_remove,
  358. };
  359. module_platform_driver(mtk_rtc_driver);
  360. MODULE_LICENSE("GPL v2");
  361. MODULE_AUTHOR("Tianping Fang <tianping.fang@mediatek.com>");
  362. MODULE_DESCRIPTION("RTC Driver for MediaTek MT6397 PMIC");