rtc-mt6397.c 9.6 KB

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