rtc-88pm80x.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Real Time Clock driver for Marvell 88PM80x PMIC
  4. *
  5. * Copyright (c) 2012 Marvell International Ltd.
  6. * Wenzeng Chen<[email protected]>
  7. * Qiao Zhou <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/regmap.h>
  13. #include <linux/mfd/core.h>
  14. #include <linux/mfd/88pm80x.h>
  15. #include <linux/rtc.h>
  16. #define PM800_RTC_COUNTER1 (0xD1)
  17. #define PM800_RTC_COUNTER2 (0xD2)
  18. #define PM800_RTC_COUNTER3 (0xD3)
  19. #define PM800_RTC_COUNTER4 (0xD4)
  20. #define PM800_RTC_EXPIRE1_1 (0xD5)
  21. #define PM800_RTC_EXPIRE1_2 (0xD6)
  22. #define PM800_RTC_EXPIRE1_3 (0xD7)
  23. #define PM800_RTC_EXPIRE1_4 (0xD8)
  24. #define PM800_RTC_TRIM1 (0xD9)
  25. #define PM800_RTC_TRIM2 (0xDA)
  26. #define PM800_RTC_TRIM3 (0xDB)
  27. #define PM800_RTC_TRIM4 (0xDC)
  28. #define PM800_RTC_EXPIRE2_1 (0xDD)
  29. #define PM800_RTC_EXPIRE2_2 (0xDE)
  30. #define PM800_RTC_EXPIRE2_3 (0xDF)
  31. #define PM800_RTC_EXPIRE2_4 (0xE0)
  32. #define PM800_POWER_DOWN_LOG1 (0xE5)
  33. #define PM800_POWER_DOWN_LOG2 (0xE6)
  34. struct pm80x_rtc_info {
  35. struct pm80x_chip *chip;
  36. struct regmap *map;
  37. struct rtc_device *rtc_dev;
  38. struct device *dev;
  39. int irq;
  40. };
  41. static irqreturn_t rtc_update_handler(int irq, void *data)
  42. {
  43. struct pm80x_rtc_info *info = (struct pm80x_rtc_info *)data;
  44. int mask;
  45. mask = PM800_ALARM | PM800_ALARM_WAKEUP;
  46. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask | PM800_ALARM1_EN,
  47. mask);
  48. rtc_update_irq(info->rtc_dev, 1, RTC_AF);
  49. return IRQ_HANDLED;
  50. }
  51. static int pm80x_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  52. {
  53. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  54. if (enabled)
  55. regmap_update_bits(info->map, PM800_RTC_CONTROL,
  56. PM800_ALARM1_EN, PM800_ALARM1_EN);
  57. else
  58. regmap_update_bits(info->map, PM800_RTC_CONTROL,
  59. PM800_ALARM1_EN, 0);
  60. return 0;
  61. }
  62. /*
  63. * Calculate the next alarm time given the requested alarm time mask
  64. * and the current time.
  65. */
  66. static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
  67. struct rtc_time *alrm)
  68. {
  69. unsigned long next_time;
  70. unsigned long now_time;
  71. next->tm_year = now->tm_year;
  72. next->tm_mon = now->tm_mon;
  73. next->tm_mday = now->tm_mday;
  74. next->tm_hour = alrm->tm_hour;
  75. next->tm_min = alrm->tm_min;
  76. next->tm_sec = alrm->tm_sec;
  77. now_time = rtc_tm_to_time64(now);
  78. next_time = rtc_tm_to_time64(next);
  79. if (next_time < now_time) {
  80. /* Advance one day */
  81. next_time += 60 * 60 * 24;
  82. rtc_time64_to_tm(next_time, next);
  83. }
  84. }
  85. static int pm80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
  86. {
  87. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  88. unsigned char buf[4];
  89. unsigned long ticks, base, data;
  90. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  91. base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  92. (buf[1] << 8) | buf[0];
  93. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  94. /* load 32-bit read-only counter */
  95. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  96. data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  97. (buf[1] << 8) | buf[0];
  98. ticks = base + data;
  99. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  100. base, data, ticks);
  101. rtc_time64_to_tm(ticks, tm);
  102. return 0;
  103. }
  104. static int pm80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
  105. {
  106. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  107. unsigned char buf[4];
  108. unsigned long ticks, base, data;
  109. ticks = rtc_tm_to_time64(tm);
  110. /* load 32-bit read-only counter */
  111. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  112. data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  113. (buf[1] << 8) | buf[0];
  114. base = ticks - data;
  115. dev_dbg(info->dev, "set base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  116. base, data, ticks);
  117. buf[0] = base & 0xFF;
  118. buf[1] = (base >> 8) & 0xFF;
  119. buf[2] = (base >> 16) & 0xFF;
  120. buf[3] = (base >> 24) & 0xFF;
  121. regmap_raw_write(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  122. return 0;
  123. }
  124. static int pm80x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  125. {
  126. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  127. unsigned char buf[4];
  128. unsigned long ticks, base, data;
  129. int ret;
  130. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  131. base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  132. (buf[1] << 8) | buf[0];
  133. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  134. regmap_raw_read(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
  135. data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  136. (buf[1] << 8) | buf[0];
  137. ticks = base + data;
  138. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  139. base, data, ticks);
  140. rtc_time64_to_tm(ticks, &alrm->time);
  141. regmap_read(info->map, PM800_RTC_CONTROL, &ret);
  142. alrm->enabled = (ret & PM800_ALARM1_EN) ? 1 : 0;
  143. alrm->pending = (ret & (PM800_ALARM | PM800_ALARM_WAKEUP)) ? 1 : 0;
  144. return 0;
  145. }
  146. static int pm80x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  147. {
  148. struct pm80x_rtc_info *info = dev_get_drvdata(dev);
  149. struct rtc_time now_tm, alarm_tm;
  150. unsigned long ticks, base, data;
  151. unsigned char buf[4];
  152. int mask;
  153. regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_ALARM1_EN, 0);
  154. regmap_raw_read(info->map, PM800_RTC_EXPIRE2_1, buf, 4);
  155. base = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  156. (buf[1] << 8) | buf[0];
  157. dev_dbg(info->dev, "%x-%x-%x-%x\n", buf[0], buf[1], buf[2], buf[3]);
  158. /* load 32-bit read-only counter */
  159. regmap_raw_read(info->map, PM800_RTC_COUNTER1, buf, 4);
  160. data = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
  161. (buf[1] << 8) | buf[0];
  162. ticks = base + data;
  163. dev_dbg(info->dev, "get base:0x%lx, RO count:0x%lx, ticks:0x%lx\n",
  164. base, data, ticks);
  165. rtc_time64_to_tm(ticks, &now_tm);
  166. dev_dbg(info->dev, "%s, now time : %lu\n", __func__, ticks);
  167. rtc_next_alarm_time(&alarm_tm, &now_tm, &alrm->time);
  168. /* get new ticks for alarm in 24 hours */
  169. ticks = rtc_tm_to_time64(&alarm_tm);
  170. dev_dbg(info->dev, "%s, alarm time: %lu\n", __func__, ticks);
  171. data = ticks - base;
  172. buf[0] = data & 0xff;
  173. buf[1] = (data >> 8) & 0xff;
  174. buf[2] = (data >> 16) & 0xff;
  175. buf[3] = (data >> 24) & 0xff;
  176. regmap_raw_write(info->map, PM800_RTC_EXPIRE1_1, buf, 4);
  177. if (alrm->enabled) {
  178. mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
  179. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask, mask);
  180. } else {
  181. mask = PM800_ALARM | PM800_ALARM_WAKEUP | PM800_ALARM1_EN;
  182. regmap_update_bits(info->map, PM800_RTC_CONTROL, mask,
  183. PM800_ALARM | PM800_ALARM_WAKEUP);
  184. }
  185. return 0;
  186. }
  187. static const struct rtc_class_ops pm80x_rtc_ops = {
  188. .read_time = pm80x_rtc_read_time,
  189. .set_time = pm80x_rtc_set_time,
  190. .read_alarm = pm80x_rtc_read_alarm,
  191. .set_alarm = pm80x_rtc_set_alarm,
  192. .alarm_irq_enable = pm80x_rtc_alarm_irq_enable,
  193. };
  194. #ifdef CONFIG_PM_SLEEP
  195. static int pm80x_rtc_suspend(struct device *dev)
  196. {
  197. return pm80x_dev_suspend(dev);
  198. }
  199. static int pm80x_rtc_resume(struct device *dev)
  200. {
  201. return pm80x_dev_resume(dev);
  202. }
  203. #endif
  204. static SIMPLE_DEV_PM_OPS(pm80x_rtc_pm_ops, pm80x_rtc_suspend, pm80x_rtc_resume);
  205. static int pm80x_rtc_probe(struct platform_device *pdev)
  206. {
  207. struct pm80x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  208. struct pm80x_rtc_pdata *pdata = dev_get_platdata(&pdev->dev);
  209. struct pm80x_rtc_info *info;
  210. struct device_node *node = pdev->dev.of_node;
  211. int ret;
  212. if (!pdata && !node) {
  213. dev_err(&pdev->dev,
  214. "pm80x-rtc requires platform data or of_node\n");
  215. return -EINVAL;
  216. }
  217. if (!pdata) {
  218. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  219. if (!pdata) {
  220. dev_err(&pdev->dev, "failed to allocate memory\n");
  221. return -ENOMEM;
  222. }
  223. }
  224. info =
  225. devm_kzalloc(&pdev->dev, sizeof(struct pm80x_rtc_info), GFP_KERNEL);
  226. if (!info)
  227. return -ENOMEM;
  228. info->irq = platform_get_irq(pdev, 0);
  229. if (info->irq < 0) {
  230. ret = -EINVAL;
  231. goto out;
  232. }
  233. info->chip = chip;
  234. info->map = chip->regmap;
  235. if (!info->map) {
  236. dev_err(&pdev->dev, "no regmap!\n");
  237. ret = -EINVAL;
  238. goto out;
  239. }
  240. info->dev = &pdev->dev;
  241. dev_set_drvdata(&pdev->dev, info);
  242. info->rtc_dev = devm_rtc_allocate_device(&pdev->dev);
  243. if (IS_ERR(info->rtc_dev))
  244. return PTR_ERR(info->rtc_dev);
  245. ret = pm80x_request_irq(chip, info->irq, rtc_update_handler,
  246. IRQF_ONESHOT, "rtc", info);
  247. if (ret < 0) {
  248. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  249. info->irq, ret);
  250. goto out;
  251. }
  252. info->rtc_dev->ops = &pm80x_rtc_ops;
  253. info->rtc_dev->range_max = U32_MAX;
  254. ret = devm_rtc_register_device(info->rtc_dev);
  255. if (ret)
  256. goto out_rtc;
  257. /*
  258. * enable internal XO instead of internal 3.25MHz clock since it can
  259. * free running in PMIC power-down state.
  260. */
  261. regmap_update_bits(info->map, PM800_RTC_CONTROL, PM800_RTC1_USE_XO,
  262. PM800_RTC1_USE_XO);
  263. /* remember whether this power up is caused by PMIC RTC or not */
  264. info->rtc_dev->dev.platform_data = &pdata->rtc_wakeup;
  265. device_init_wakeup(&pdev->dev, 1);
  266. return 0;
  267. out_rtc:
  268. pm80x_free_irq(chip, info->irq, info);
  269. out:
  270. return ret;
  271. }
  272. static int pm80x_rtc_remove(struct platform_device *pdev)
  273. {
  274. struct pm80x_rtc_info *info = platform_get_drvdata(pdev);
  275. pm80x_free_irq(info->chip, info->irq, info);
  276. return 0;
  277. }
  278. static struct platform_driver pm80x_rtc_driver = {
  279. .driver = {
  280. .name = "88pm80x-rtc",
  281. .pm = &pm80x_rtc_pm_ops,
  282. },
  283. .probe = pm80x_rtc_probe,
  284. .remove = pm80x_rtc_remove,
  285. };
  286. module_platform_driver(pm80x_rtc_driver);
  287. MODULE_LICENSE("GPL");
  288. MODULE_DESCRIPTION("Marvell 88PM80x RTC driver");
  289. MODULE_AUTHOR("Qiao Zhou <[email protected]>");
  290. MODULE_ALIAS("platform:88pm80x-rtc");