rtc-tps65910.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * rtc-tps65910.c -- TPS65910 Real Time Clock interface
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
  6. *
  7. * Based on original TI driver rtc-twl.c
  8. * Copyright (C) 2007 MontaVista Software, Inc
  9. * Author: Alexandre Rusev <source@mvista.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/rtc.h>
  22. #include <linux/bcd.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mfd/tps65910.h>
  26. struct tps65910_rtc {
  27. struct rtc_device *rtc;
  28. int irq;
  29. };
  30. /* Total number of RTC registers needed to set time*/
  31. #define NUM_TIME_REGS (TPS65910_YEARS - TPS65910_SECONDS + 1)
  32. static int tps65910_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
  33. {
  34. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  35. u8 val = 0;
  36. if (enabled)
  37. val = TPS65910_RTC_INTERRUPTS_IT_ALARM;
  38. return regmap_write(tps->regmap, TPS65910_RTC_INTERRUPTS, val);
  39. }
  40. /*
  41. * Gets current tps65910 RTC time and date parameters.
  42. *
  43. * The RTC's time/alarm representation is not what gmtime(3) requires
  44. * Linux to use:
  45. *
  46. * - Months are 1..12 vs Linux 0-11
  47. * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
  48. */
  49. static int tps65910_rtc_read_time(struct device *dev, struct rtc_time *tm)
  50. {
  51. unsigned char rtc_data[NUM_TIME_REGS];
  52. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  53. int ret;
  54. /* Copy RTC counting registers to static registers or latches */
  55. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  56. TPS65910_RTC_CTRL_GET_TIME, TPS65910_RTC_CTRL_GET_TIME);
  57. if (ret < 0) {
  58. dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
  59. return ret;
  60. }
  61. ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, rtc_data,
  62. NUM_TIME_REGS);
  63. if (ret < 0) {
  64. dev_err(dev, "reading from RTC failed with err:%d\n", ret);
  65. return ret;
  66. }
  67. tm->tm_sec = bcd2bin(rtc_data[0]);
  68. tm->tm_min = bcd2bin(rtc_data[1]);
  69. tm->tm_hour = bcd2bin(rtc_data[2]);
  70. tm->tm_mday = bcd2bin(rtc_data[3]);
  71. tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
  72. tm->tm_year = bcd2bin(rtc_data[5]) + 100;
  73. return ret;
  74. }
  75. static int tps65910_rtc_set_time(struct device *dev, struct rtc_time *tm)
  76. {
  77. unsigned char rtc_data[NUM_TIME_REGS];
  78. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  79. int ret;
  80. rtc_data[0] = bin2bcd(tm->tm_sec);
  81. rtc_data[1] = bin2bcd(tm->tm_min);
  82. rtc_data[2] = bin2bcd(tm->tm_hour);
  83. rtc_data[3] = bin2bcd(tm->tm_mday);
  84. rtc_data[4] = bin2bcd(tm->tm_mon + 1);
  85. rtc_data[5] = bin2bcd(tm->tm_year - 100);
  86. /* Stop RTC while updating the RTC time registers */
  87. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  88. TPS65910_RTC_CTRL_STOP_RTC, 0);
  89. if (ret < 0) {
  90. dev_err(dev, "RTC stop failed with err:%d\n", ret);
  91. return ret;
  92. }
  93. /* update all the time registers in one shot */
  94. ret = regmap_bulk_write(tps->regmap, TPS65910_SECONDS, rtc_data,
  95. NUM_TIME_REGS);
  96. if (ret < 0) {
  97. dev_err(dev, "rtc_set_time error %d\n", ret);
  98. return ret;
  99. }
  100. /* Start back RTC */
  101. ret = regmap_update_bits(tps->regmap, TPS65910_RTC_CTRL,
  102. TPS65910_RTC_CTRL_STOP_RTC, 1);
  103. if (ret < 0)
  104. dev_err(dev, "RTC start failed with err:%d\n", ret);
  105. return ret;
  106. }
  107. /*
  108. * Gets current tps65910 RTC alarm time.
  109. */
  110. static int tps65910_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  111. {
  112. unsigned char alarm_data[NUM_TIME_REGS];
  113. u32 int_val;
  114. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  115. int ret;
  116. ret = regmap_bulk_read(tps->regmap, TPS65910_SECONDS, alarm_data,
  117. NUM_TIME_REGS);
  118. if (ret < 0) {
  119. dev_err(dev, "rtc_read_alarm error %d\n", ret);
  120. return ret;
  121. }
  122. alm->time.tm_sec = bcd2bin(alarm_data[0]);
  123. alm->time.tm_min = bcd2bin(alarm_data[1]);
  124. alm->time.tm_hour = bcd2bin(alarm_data[2]);
  125. alm->time.tm_mday = bcd2bin(alarm_data[3]);
  126. alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
  127. alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
  128. ret = regmap_read(tps->regmap, TPS65910_RTC_INTERRUPTS, &int_val);
  129. if (ret < 0)
  130. return ret;
  131. if (int_val & TPS65910_RTC_INTERRUPTS_IT_ALARM)
  132. alm->enabled = 1;
  133. return ret;
  134. }
  135. static int tps65910_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  136. {
  137. unsigned char alarm_data[NUM_TIME_REGS];
  138. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  139. int ret;
  140. ret = tps65910_rtc_alarm_irq_enable(dev, 0);
  141. if (ret)
  142. return ret;
  143. alarm_data[0] = bin2bcd(alm->time.tm_sec);
  144. alarm_data[1] = bin2bcd(alm->time.tm_min);
  145. alarm_data[2] = bin2bcd(alm->time.tm_hour);
  146. alarm_data[3] = bin2bcd(alm->time.tm_mday);
  147. alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
  148. alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
  149. /* update all the alarm registers in one shot */
  150. ret = regmap_bulk_write(tps->regmap, TPS65910_ALARM_SECONDS,
  151. alarm_data, NUM_TIME_REGS);
  152. if (ret) {
  153. dev_err(dev, "rtc_set_alarm error %d\n", ret);
  154. return ret;
  155. }
  156. if (alm->enabled)
  157. ret = tps65910_rtc_alarm_irq_enable(dev, 1);
  158. return ret;
  159. }
  160. static irqreturn_t tps65910_rtc_interrupt(int irq, void *rtc)
  161. {
  162. struct device *dev = rtc;
  163. unsigned long events = 0;
  164. struct tps65910 *tps = dev_get_drvdata(dev->parent);
  165. struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
  166. int ret;
  167. u32 rtc_reg;
  168. ret = regmap_read(tps->regmap, TPS65910_RTC_STATUS, &rtc_reg);
  169. if (ret)
  170. return IRQ_NONE;
  171. if (rtc_reg & TPS65910_RTC_STATUS_ALARM)
  172. events = RTC_IRQF | RTC_AF;
  173. ret = regmap_write(tps->regmap, TPS65910_RTC_STATUS, rtc_reg);
  174. if (ret)
  175. return IRQ_NONE;
  176. /* Notify RTC core on event */
  177. rtc_update_irq(tps_rtc->rtc, 1, events);
  178. return IRQ_HANDLED;
  179. }
  180. static const struct rtc_class_ops tps65910_rtc_ops = {
  181. .read_time = tps65910_rtc_read_time,
  182. .set_time = tps65910_rtc_set_time,
  183. .read_alarm = tps65910_rtc_read_alarm,
  184. .set_alarm = tps65910_rtc_set_alarm,
  185. .alarm_irq_enable = tps65910_rtc_alarm_irq_enable,
  186. };
  187. static int tps65910_rtc_probe(struct platform_device *pdev)
  188. {
  189. struct tps65910 *tps65910 = NULL;
  190. struct tps65910_rtc *tps_rtc = NULL;
  191. int ret;
  192. int irq;
  193. u32 rtc_reg;
  194. tps65910 = dev_get_drvdata(pdev->dev.parent);
  195. tps_rtc = devm_kzalloc(&pdev->dev, sizeof(struct tps65910_rtc),
  196. GFP_KERNEL);
  197. if (!tps_rtc)
  198. return -ENOMEM;
  199. /* Clear pending interrupts */
  200. ret = regmap_read(tps65910->regmap, TPS65910_RTC_STATUS, &rtc_reg);
  201. if (ret < 0)
  202. return ret;
  203. ret = regmap_write(tps65910->regmap, TPS65910_RTC_STATUS, rtc_reg);
  204. if (ret < 0)
  205. return ret;
  206. dev_dbg(&pdev->dev, "Enabling rtc-tps65910.\n");
  207. /* Enable RTC digital power domain */
  208. ret = regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
  209. DEVCTRL_RTC_PWDN_MASK, 0 << DEVCTRL_RTC_PWDN_SHIFT);
  210. if (ret < 0)
  211. return ret;
  212. rtc_reg = TPS65910_RTC_CTRL_STOP_RTC;
  213. ret = regmap_write(tps65910->regmap, TPS65910_RTC_CTRL, rtc_reg);
  214. if (ret < 0)
  215. return ret;
  216. platform_set_drvdata(pdev, tps_rtc);
  217. irq = platform_get_irq(pdev, 0);
  218. if (irq <= 0) {
  219. dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
  220. irq);
  221. return -ENXIO;
  222. }
  223. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  224. tps65910_rtc_interrupt, IRQF_TRIGGER_LOW | IRQF_EARLY_RESUME,
  225. dev_name(&pdev->dev), &pdev->dev);
  226. if (ret < 0) {
  227. dev_err(&pdev->dev, "IRQ is not free.\n");
  228. return ret;
  229. }
  230. tps_rtc->irq = irq;
  231. device_set_wakeup_capable(&pdev->dev, 1);
  232. tps_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  233. &tps65910_rtc_ops, THIS_MODULE);
  234. if (IS_ERR(tps_rtc->rtc)) {
  235. ret = PTR_ERR(tps_rtc->rtc);
  236. dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
  237. return ret;
  238. }
  239. return 0;
  240. }
  241. /*
  242. * Disable tps65910 RTC interrupts.
  243. * Sets status flag to free.
  244. */
  245. static int tps65910_rtc_remove(struct platform_device *pdev)
  246. {
  247. tps65910_rtc_alarm_irq_enable(&pdev->dev, 0);
  248. return 0;
  249. }
  250. #ifdef CONFIG_PM_SLEEP
  251. static int tps65910_rtc_suspend(struct device *dev)
  252. {
  253. struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
  254. if (device_may_wakeup(dev))
  255. enable_irq_wake(tps_rtc->irq);
  256. return 0;
  257. }
  258. static int tps65910_rtc_resume(struct device *dev)
  259. {
  260. struct tps65910_rtc *tps_rtc = dev_get_drvdata(dev);
  261. if (device_may_wakeup(dev))
  262. disable_irq_wake(tps_rtc->irq);
  263. return 0;
  264. }
  265. #endif
  266. static SIMPLE_DEV_PM_OPS(tps65910_rtc_pm_ops, tps65910_rtc_suspend,
  267. tps65910_rtc_resume);
  268. static struct platform_driver tps65910_rtc_driver = {
  269. .probe = tps65910_rtc_probe,
  270. .remove = tps65910_rtc_remove,
  271. .driver = {
  272. .name = "tps65910-rtc",
  273. .pm = &tps65910_rtc_pm_ops,
  274. },
  275. };
  276. module_platform_driver(tps65910_rtc_driver);
  277. MODULE_ALIAS("platform:rtc-tps65910");
  278. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  279. MODULE_LICENSE("GPL");