rtc-as3722.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * rtc-as3722.c - Real Time Clock driver for ams AS3722 PMICs
  3. *
  4. * Copyright (C) 2013 ams AG
  5. * Copyright (c) 2013, NVIDIA Corporation. All rights reserved.
  6. *
  7. * Author: Florian Lobmaier <florian.lobmaier@ams.com>
  8. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/bcd.h>
  21. #include <linux/completion.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/mfd/as3722.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/rtc.h>
  30. #include <linux/time.h>
  31. #define AS3722_RTC_START_YEAR 2000
  32. struct as3722_rtc {
  33. struct rtc_device *rtc;
  34. struct device *dev;
  35. struct as3722 *as3722;
  36. int alarm_irq;
  37. bool irq_enable;
  38. };
  39. static void as3722_time_to_reg(u8 *rbuff, struct rtc_time *tm)
  40. {
  41. rbuff[0] = bin2bcd(tm->tm_sec);
  42. rbuff[1] = bin2bcd(tm->tm_min);
  43. rbuff[2] = bin2bcd(tm->tm_hour);
  44. rbuff[3] = bin2bcd(tm->tm_mday);
  45. rbuff[4] = bin2bcd(tm->tm_mon + 1);
  46. rbuff[5] = bin2bcd(tm->tm_year - (AS3722_RTC_START_YEAR - 1900));
  47. }
  48. static void as3722_reg_to_time(u8 *rbuff, struct rtc_time *tm)
  49. {
  50. tm->tm_sec = bcd2bin(rbuff[0] & 0x7F);
  51. tm->tm_min = bcd2bin(rbuff[1] & 0x7F);
  52. tm->tm_hour = bcd2bin(rbuff[2] & 0x3F);
  53. tm->tm_mday = bcd2bin(rbuff[3] & 0x3F);
  54. tm->tm_mon = bcd2bin(rbuff[4] & 0x1F) - 1;
  55. tm->tm_year = (AS3722_RTC_START_YEAR - 1900) + bcd2bin(rbuff[5] & 0x7F);
  56. return;
  57. }
  58. static int as3722_rtc_read_time(struct device *dev, struct rtc_time *tm)
  59. {
  60. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  61. struct as3722 *as3722 = as3722_rtc->as3722;
  62. u8 as_time_array[6];
  63. int ret;
  64. ret = as3722_block_read(as3722, AS3722_RTC_SECOND_REG,
  65. 6, as_time_array);
  66. if (ret < 0) {
  67. dev_err(dev, "RTC_SECOND reg block read failed %d\n", ret);
  68. return ret;
  69. }
  70. as3722_reg_to_time(as_time_array, tm);
  71. return 0;
  72. }
  73. static int as3722_rtc_set_time(struct device *dev, struct rtc_time *tm)
  74. {
  75. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  76. struct as3722 *as3722 = as3722_rtc->as3722;
  77. u8 as_time_array[6];
  78. int ret;
  79. if (tm->tm_year < (AS3722_RTC_START_YEAR - 1900))
  80. return -EINVAL;
  81. as3722_time_to_reg(as_time_array, tm);
  82. ret = as3722_block_write(as3722, AS3722_RTC_SECOND_REG, 6,
  83. as_time_array);
  84. if (ret < 0)
  85. dev_err(dev, "RTC_SECOND reg block write failed %d\n", ret);
  86. return ret;
  87. }
  88. static int as3722_rtc_alarm_irq_enable(struct device *dev,
  89. unsigned int enabled)
  90. {
  91. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  92. if (enabled && !as3722_rtc->irq_enable) {
  93. enable_irq(as3722_rtc->alarm_irq);
  94. as3722_rtc->irq_enable = true;
  95. } else if (!enabled && as3722_rtc->irq_enable) {
  96. disable_irq(as3722_rtc->alarm_irq);
  97. as3722_rtc->irq_enable = false;
  98. }
  99. return 0;
  100. }
  101. static int as3722_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  102. {
  103. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  104. struct as3722 *as3722 = as3722_rtc->as3722;
  105. u8 as_time_array[6];
  106. int ret;
  107. ret = as3722_block_read(as3722, AS3722_RTC_ALARM_SECOND_REG, 6,
  108. as_time_array);
  109. if (ret < 0) {
  110. dev_err(dev, "RTC_ALARM_SECOND block read failed %d\n", ret);
  111. return ret;
  112. }
  113. as3722_reg_to_time(as_time_array, &alrm->time);
  114. return 0;
  115. }
  116. static int as3722_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  117. {
  118. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  119. struct as3722 *as3722 = as3722_rtc->as3722;
  120. u8 as_time_array[6];
  121. int ret;
  122. if (alrm->time.tm_year < (AS3722_RTC_START_YEAR - 1900))
  123. return -EINVAL;
  124. ret = as3722_rtc_alarm_irq_enable(dev, 0);
  125. if (ret < 0) {
  126. dev_err(dev, "Disable RTC alarm failed\n");
  127. return ret;
  128. }
  129. as3722_time_to_reg(as_time_array, &alrm->time);
  130. ret = as3722_block_write(as3722, AS3722_RTC_ALARM_SECOND_REG, 6,
  131. as_time_array);
  132. if (ret < 0) {
  133. dev_err(dev, "RTC_ALARM_SECOND block write failed %d\n", ret);
  134. return ret;
  135. }
  136. if (alrm->enabled)
  137. ret = as3722_rtc_alarm_irq_enable(dev, alrm->enabled);
  138. return ret;
  139. }
  140. static irqreturn_t as3722_alarm_irq(int irq, void *data)
  141. {
  142. struct as3722_rtc *as3722_rtc = data;
  143. rtc_update_irq(as3722_rtc->rtc, 1, RTC_IRQF | RTC_AF);
  144. return IRQ_HANDLED;
  145. }
  146. static const struct rtc_class_ops as3722_rtc_ops = {
  147. .read_time = as3722_rtc_read_time,
  148. .set_time = as3722_rtc_set_time,
  149. .read_alarm = as3722_rtc_read_alarm,
  150. .set_alarm = as3722_rtc_set_alarm,
  151. .alarm_irq_enable = as3722_rtc_alarm_irq_enable,
  152. };
  153. static int as3722_rtc_probe(struct platform_device *pdev)
  154. {
  155. struct as3722 *as3722 = dev_get_drvdata(pdev->dev.parent);
  156. struct as3722_rtc *as3722_rtc;
  157. int ret;
  158. as3722_rtc = devm_kzalloc(&pdev->dev, sizeof(*as3722_rtc), GFP_KERNEL);
  159. if (!as3722_rtc)
  160. return -ENOMEM;
  161. as3722_rtc->as3722 = as3722;
  162. as3722_rtc->dev = &pdev->dev;
  163. platform_set_drvdata(pdev, as3722_rtc);
  164. /* Enable the RTC to make sure it is running. */
  165. ret = as3722_update_bits(as3722, AS3722_RTC_CONTROL_REG,
  166. AS3722_RTC_ON | AS3722_RTC_ALARM_WAKEUP_EN,
  167. AS3722_RTC_ON | AS3722_RTC_ALARM_WAKEUP_EN);
  168. if (ret < 0) {
  169. dev_err(&pdev->dev, "RTC_CONTROL reg write failed: %d\n", ret);
  170. return ret;
  171. }
  172. device_init_wakeup(&pdev->dev, 1);
  173. as3722_rtc->rtc = devm_rtc_device_register(&pdev->dev, "as3722-rtc",
  174. &as3722_rtc_ops, THIS_MODULE);
  175. if (IS_ERR(as3722_rtc->rtc)) {
  176. ret = PTR_ERR(as3722_rtc->rtc);
  177. dev_err(&pdev->dev, "RTC register failed: %d\n", ret);
  178. return ret;
  179. }
  180. as3722_rtc->alarm_irq = platform_get_irq(pdev, 0);
  181. dev_info(&pdev->dev, "RTC interrupt %d\n", as3722_rtc->alarm_irq);
  182. ret = devm_request_threaded_irq(&pdev->dev, as3722_rtc->alarm_irq, NULL,
  183. as3722_alarm_irq, IRQF_ONESHOT,
  184. "rtc-alarm", as3722_rtc);
  185. if (ret < 0) {
  186. dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n",
  187. as3722_rtc->alarm_irq, ret);
  188. return ret;
  189. }
  190. disable_irq(as3722_rtc->alarm_irq);
  191. return 0;
  192. }
  193. #ifdef CONFIG_PM_SLEEP
  194. static int as3722_rtc_suspend(struct device *dev)
  195. {
  196. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  197. if (device_may_wakeup(dev))
  198. enable_irq_wake(as3722_rtc->alarm_irq);
  199. return 0;
  200. }
  201. static int as3722_rtc_resume(struct device *dev)
  202. {
  203. struct as3722_rtc *as3722_rtc = dev_get_drvdata(dev);
  204. if (device_may_wakeup(dev))
  205. disable_irq_wake(as3722_rtc->alarm_irq);
  206. return 0;
  207. }
  208. #endif
  209. static SIMPLE_DEV_PM_OPS(as3722_rtc_pm_ops, as3722_rtc_suspend,
  210. as3722_rtc_resume);
  211. static struct platform_driver as3722_rtc_driver = {
  212. .probe = as3722_rtc_probe,
  213. .driver = {
  214. .name = "as3722-rtc",
  215. .pm = &as3722_rtc_pm_ops,
  216. },
  217. };
  218. module_platform_driver(as3722_rtc_driver);
  219. MODULE_DESCRIPTION("RTC driver for AS3722 PMICs");
  220. MODULE_ALIAS("platform:as3722-rtc");
  221. MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
  222. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  223. MODULE_LICENSE("GPL");