rtc-rc5t583.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * rtc-rc5t583.c -- RICOH RC5T583 Real Time Clock
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/rtc.h>
  24. #include <linux/bcd.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/mfd/rc5t583.h>
  28. struct rc5t583_rtc {
  29. struct rtc_device *rtc;
  30. /* To store the list of enabled interrupts, during system suspend */
  31. u32 irqen;
  32. };
  33. /* Total number of RTC registers needed to set time*/
  34. #define NUM_TIME_REGS (RC5T583_RTC_YEAR - RC5T583_RTC_SEC + 1)
  35. /* Total number of RTC registers needed to set Y-Alarm*/
  36. #define NUM_YAL_REGS (RC5T583_RTC_AY_YEAR - RC5T583_RTC_AY_MIN + 1)
  37. /* Set Y-Alarm interrupt */
  38. #define SET_YAL BIT(5)
  39. /* Get Y-Alarm interrupt status*/
  40. #define GET_YAL_STATUS BIT(3)
  41. static int rc5t583_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
  42. {
  43. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  44. u8 val;
  45. /* Set Y-Alarm, based on 'enabled' */
  46. val = enabled ? SET_YAL : 0;
  47. return regmap_update_bits(rc5t583->regmap, RC5T583_RTC_CTL1, SET_YAL,
  48. val);
  49. }
  50. /*
  51. * Gets current rc5t583 RTC time and date parameters.
  52. *
  53. * The RTC's time/alarm representation is not what gmtime(3) requires
  54. * Linux to use:
  55. *
  56. * - Months are 1..12 vs Linux 0-11
  57. * - Years are 0..99 vs Linux 1900..N (we assume 21st century)
  58. */
  59. static int rc5t583_rtc_read_time(struct device *dev, struct rtc_time *tm)
  60. {
  61. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  62. u8 rtc_data[NUM_TIME_REGS];
  63. int ret;
  64. ret = regmap_bulk_read(rc5t583->regmap, RC5T583_RTC_SEC, rtc_data,
  65. NUM_TIME_REGS);
  66. if (ret < 0) {
  67. dev_err(dev, "RTC read time failed with err:%d\n", ret);
  68. return ret;
  69. }
  70. tm->tm_sec = bcd2bin(rtc_data[0]);
  71. tm->tm_min = bcd2bin(rtc_data[1]);
  72. tm->tm_hour = bcd2bin(rtc_data[2]);
  73. tm->tm_wday = bcd2bin(rtc_data[3]);
  74. tm->tm_mday = bcd2bin(rtc_data[4]);
  75. tm->tm_mon = bcd2bin(rtc_data[5]) - 1;
  76. tm->tm_year = bcd2bin(rtc_data[6]) + 100;
  77. return ret;
  78. }
  79. static int rc5t583_rtc_set_time(struct device *dev, struct rtc_time *tm)
  80. {
  81. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  82. unsigned char rtc_data[NUM_TIME_REGS];
  83. int ret;
  84. rtc_data[0] = bin2bcd(tm->tm_sec);
  85. rtc_data[1] = bin2bcd(tm->tm_min);
  86. rtc_data[2] = bin2bcd(tm->tm_hour);
  87. rtc_data[3] = bin2bcd(tm->tm_wday);
  88. rtc_data[4] = bin2bcd(tm->tm_mday);
  89. rtc_data[5] = bin2bcd(tm->tm_mon + 1);
  90. rtc_data[6] = bin2bcd(tm->tm_year - 100);
  91. ret = regmap_bulk_write(rc5t583->regmap, RC5T583_RTC_SEC, rtc_data,
  92. NUM_TIME_REGS);
  93. if (ret < 0) {
  94. dev_err(dev, "RTC set time failed with error %d\n", ret);
  95. return ret;
  96. }
  97. return ret;
  98. }
  99. static int rc5t583_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  100. {
  101. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  102. unsigned char alarm_data[NUM_YAL_REGS];
  103. u32 interrupt_enable;
  104. int ret;
  105. ret = regmap_bulk_read(rc5t583->regmap, RC5T583_RTC_AY_MIN, alarm_data,
  106. NUM_YAL_REGS);
  107. if (ret < 0) {
  108. dev_err(dev, "rtc_read_alarm error %d\n", ret);
  109. return ret;
  110. }
  111. alm->time.tm_sec = 0;
  112. alm->time.tm_min = bcd2bin(alarm_data[0]);
  113. alm->time.tm_hour = bcd2bin(alarm_data[1]);
  114. alm->time.tm_mday = bcd2bin(alarm_data[2]);
  115. alm->time.tm_mon = bcd2bin(alarm_data[3]) - 1;
  116. alm->time.tm_year = bcd2bin(alarm_data[4]) + 100;
  117. ret = regmap_read(rc5t583->regmap, RC5T583_RTC_CTL1, &interrupt_enable);
  118. if (ret < 0)
  119. return ret;
  120. /* check if YALE is set */
  121. if (interrupt_enable & SET_YAL)
  122. alm->enabled = 1;
  123. return ret;
  124. }
  125. static int rc5t583_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  126. {
  127. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  128. unsigned char alarm_data[NUM_YAL_REGS];
  129. int ret;
  130. ret = rc5t583_rtc_alarm_irq_enable(dev, 0);
  131. if (ret)
  132. return ret;
  133. alarm_data[0] = bin2bcd(alm->time.tm_min);
  134. alarm_data[1] = bin2bcd(alm->time.tm_hour);
  135. alarm_data[2] = bin2bcd(alm->time.tm_mday);
  136. alarm_data[3] = bin2bcd(alm->time.tm_mon + 1);
  137. alarm_data[4] = bin2bcd(alm->time.tm_year - 100);
  138. ret = regmap_bulk_write(rc5t583->regmap, RC5T583_RTC_AY_MIN, alarm_data,
  139. NUM_YAL_REGS);
  140. if (ret) {
  141. dev_err(dev, "rtc_set_alarm error %d\n", ret);
  142. return ret;
  143. }
  144. if (alm->enabled)
  145. ret = rc5t583_rtc_alarm_irq_enable(dev, 1);
  146. return ret;
  147. }
  148. static irqreturn_t rc5t583_rtc_interrupt(int irq, void *rtc)
  149. {
  150. struct device *dev = rtc;
  151. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  152. struct rc5t583_rtc *rc5t583_rtc = dev_get_drvdata(dev);
  153. unsigned long events = 0;
  154. int ret;
  155. u32 rtc_reg;
  156. ret = regmap_read(rc5t583->regmap, RC5T583_RTC_CTL2, &rtc_reg);
  157. if (ret < 0)
  158. return IRQ_NONE;
  159. if (rtc_reg & GET_YAL_STATUS) {
  160. events = RTC_IRQF | RTC_AF;
  161. /* clear pending Y-alarm interrupt bit */
  162. rtc_reg &= ~GET_YAL_STATUS;
  163. }
  164. ret = regmap_write(rc5t583->regmap, RC5T583_RTC_CTL2, rtc_reg);
  165. if (ret)
  166. return IRQ_NONE;
  167. /* Notify RTC core on event */
  168. rtc_update_irq(rc5t583_rtc->rtc, 1, events);
  169. return IRQ_HANDLED;
  170. }
  171. static const struct rtc_class_ops rc5t583_rtc_ops = {
  172. .read_time = rc5t583_rtc_read_time,
  173. .set_time = rc5t583_rtc_set_time,
  174. .read_alarm = rc5t583_rtc_read_alarm,
  175. .set_alarm = rc5t583_rtc_set_alarm,
  176. .alarm_irq_enable = rc5t583_rtc_alarm_irq_enable,
  177. };
  178. static int rc5t583_rtc_probe(struct platform_device *pdev)
  179. {
  180. struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
  181. struct rc5t583_rtc *ricoh_rtc;
  182. struct rc5t583_platform_data *pmic_plat_data;
  183. int ret;
  184. int irq;
  185. ricoh_rtc = devm_kzalloc(&pdev->dev, sizeof(struct rc5t583_rtc),
  186. GFP_KERNEL);
  187. if (!ricoh_rtc)
  188. return -ENOMEM;
  189. platform_set_drvdata(pdev, ricoh_rtc);
  190. /* Clear pending interrupts */
  191. ret = regmap_write(rc5t583->regmap, RC5T583_RTC_CTL2, 0);
  192. if (ret < 0)
  193. return ret;
  194. /* clear RTC Adjust register */
  195. ret = regmap_write(rc5t583->regmap, RC5T583_RTC_ADJ, 0);
  196. if (ret < 0) {
  197. dev_err(&pdev->dev, "unable to program rtc_adjust reg\n");
  198. return -EBUSY;
  199. }
  200. pmic_plat_data = dev_get_platdata(rc5t583->dev);
  201. irq = pmic_plat_data->irq_base;
  202. if (irq <= 0) {
  203. dev_warn(&pdev->dev, "Wake up is not possible as irq = %d\n",
  204. irq);
  205. return ret;
  206. }
  207. irq += RC5T583_IRQ_YALE;
  208. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  209. rc5t583_rtc_interrupt, IRQF_TRIGGER_LOW,
  210. "rtc-rc5t583", &pdev->dev);
  211. if (ret < 0) {
  212. dev_err(&pdev->dev, "IRQ is not free.\n");
  213. return ret;
  214. }
  215. device_init_wakeup(&pdev->dev, 1);
  216. ricoh_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  217. &rc5t583_rtc_ops, THIS_MODULE);
  218. if (IS_ERR(ricoh_rtc->rtc)) {
  219. ret = PTR_ERR(ricoh_rtc->rtc);
  220. dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. /*
  226. * Disable rc5t583 RTC interrupts.
  227. * Sets status flag to free.
  228. */
  229. static int rc5t583_rtc_remove(struct platform_device *pdev)
  230. {
  231. struct rc5t583_rtc *rc5t583_rtc = platform_get_drvdata(pdev);
  232. rc5t583_rtc_alarm_irq_enable(&rc5t583_rtc->rtc->dev, 0);
  233. return 0;
  234. }
  235. #ifdef CONFIG_PM_SLEEP
  236. static int rc5t583_rtc_suspend(struct device *dev)
  237. {
  238. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  239. struct rc5t583_rtc *rc5t583_rtc = dev_get_drvdata(dev);
  240. int ret;
  241. /* Store current list of enabled interrupts*/
  242. ret = regmap_read(rc5t583->regmap, RC5T583_RTC_CTL1,
  243. &rc5t583_rtc->irqen);
  244. return ret;
  245. }
  246. static int rc5t583_rtc_resume(struct device *dev)
  247. {
  248. struct rc5t583 *rc5t583 = dev_get_drvdata(dev->parent);
  249. struct rc5t583_rtc *rc5t583_rtc = dev_get_drvdata(dev);
  250. /* Restore list of enabled interrupts before suspend */
  251. return regmap_write(rc5t583->regmap, RC5T583_RTC_CTL1,
  252. rc5t583_rtc->irqen);
  253. }
  254. #endif
  255. static SIMPLE_DEV_PM_OPS(rc5t583_rtc_pm_ops, rc5t583_rtc_suspend,
  256. rc5t583_rtc_resume);
  257. static struct platform_driver rc5t583_rtc_driver = {
  258. .probe = rc5t583_rtc_probe,
  259. .remove = rc5t583_rtc_remove,
  260. .driver = {
  261. .name = "rtc-rc5t583",
  262. .pm = &rc5t583_rtc_pm_ops,
  263. },
  264. };
  265. module_platform_driver(rc5t583_rtc_driver);
  266. MODULE_ALIAS("platform:rtc-rc5t583");
  267. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  268. MODULE_LICENSE("GPL v2");