rtc-pcf50633.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* NXP PCF50633 RTC Driver
  2. *
  3. * (C) 2006-2008 by Openmoko, Inc.
  4. * Author: Balaji Rao <balajirrao@openmoko.org>
  5. * All rights reserved.
  6. *
  7. * Broken down from monstrous PCF50633 driver mainly by
  8. * Harald Welte, Andy Green and Werner Almesberger
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/rtc.h>
  23. #include <linux/bcd.h>
  24. #include <linux/err.h>
  25. #include <linux/mfd/pcf50633/core.h>
  26. #define PCF50633_REG_RTCSC 0x59 /* Second */
  27. #define PCF50633_REG_RTCMN 0x5a /* Minute */
  28. #define PCF50633_REG_RTCHR 0x5b /* Hour */
  29. #define PCF50633_REG_RTCWD 0x5c /* Weekday */
  30. #define PCF50633_REG_RTCDT 0x5d /* Day */
  31. #define PCF50633_REG_RTCMT 0x5e /* Month */
  32. #define PCF50633_REG_RTCYR 0x5f /* Year */
  33. #define PCF50633_REG_RTCSCA 0x60 /* Alarm Second */
  34. #define PCF50633_REG_RTCMNA 0x61 /* Alarm Minute */
  35. #define PCF50633_REG_RTCHRA 0x62 /* Alarm Hour */
  36. #define PCF50633_REG_RTCWDA 0x63 /* Alarm Weekday */
  37. #define PCF50633_REG_RTCDTA 0x64 /* Alarm Day */
  38. #define PCF50633_REG_RTCMTA 0x65 /* Alarm Month */
  39. #define PCF50633_REG_RTCYRA 0x66 /* Alarm Year */
  40. enum pcf50633_time_indexes {
  41. PCF50633_TI_SEC,
  42. PCF50633_TI_MIN,
  43. PCF50633_TI_HOUR,
  44. PCF50633_TI_WKDAY,
  45. PCF50633_TI_DAY,
  46. PCF50633_TI_MONTH,
  47. PCF50633_TI_YEAR,
  48. PCF50633_TI_EXTENT /* always last */
  49. };
  50. struct pcf50633_time {
  51. u_int8_t time[PCF50633_TI_EXTENT];
  52. };
  53. struct pcf50633_rtc {
  54. int alarm_enabled;
  55. int alarm_pending;
  56. struct pcf50633 *pcf;
  57. struct rtc_device *rtc_dev;
  58. };
  59. static void pcf2rtc_time(struct rtc_time *rtc, struct pcf50633_time *pcf)
  60. {
  61. rtc->tm_sec = bcd2bin(pcf->time[PCF50633_TI_SEC]);
  62. rtc->tm_min = bcd2bin(pcf->time[PCF50633_TI_MIN]);
  63. rtc->tm_hour = bcd2bin(pcf->time[PCF50633_TI_HOUR]);
  64. rtc->tm_wday = bcd2bin(pcf->time[PCF50633_TI_WKDAY]);
  65. rtc->tm_mday = bcd2bin(pcf->time[PCF50633_TI_DAY]);
  66. rtc->tm_mon = bcd2bin(pcf->time[PCF50633_TI_MONTH]) - 1;
  67. rtc->tm_year = bcd2bin(pcf->time[PCF50633_TI_YEAR]) + 100;
  68. }
  69. static void rtc2pcf_time(struct pcf50633_time *pcf, struct rtc_time *rtc)
  70. {
  71. pcf->time[PCF50633_TI_SEC] = bin2bcd(rtc->tm_sec);
  72. pcf->time[PCF50633_TI_MIN] = bin2bcd(rtc->tm_min);
  73. pcf->time[PCF50633_TI_HOUR] = bin2bcd(rtc->tm_hour);
  74. pcf->time[PCF50633_TI_WKDAY] = bin2bcd(rtc->tm_wday);
  75. pcf->time[PCF50633_TI_DAY] = bin2bcd(rtc->tm_mday);
  76. pcf->time[PCF50633_TI_MONTH] = bin2bcd(rtc->tm_mon + 1);
  77. pcf->time[PCF50633_TI_YEAR] = bin2bcd(rtc->tm_year % 100);
  78. }
  79. static int
  80. pcf50633_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  81. {
  82. struct pcf50633_rtc *rtc = dev_get_drvdata(dev);
  83. int err;
  84. if (enabled)
  85. err = pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
  86. else
  87. err = pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
  88. if (err < 0)
  89. return err;
  90. rtc->alarm_enabled = enabled;
  91. return 0;
  92. }
  93. static int pcf50633_rtc_read_time(struct device *dev, struct rtc_time *tm)
  94. {
  95. struct pcf50633_rtc *rtc;
  96. struct pcf50633_time pcf_tm;
  97. int ret;
  98. rtc = dev_get_drvdata(dev);
  99. ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSC,
  100. PCF50633_TI_EXTENT,
  101. &pcf_tm.time[0]);
  102. if (ret != PCF50633_TI_EXTENT) {
  103. dev_err(dev, "Failed to read time\n");
  104. return -EIO;
  105. }
  106. dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
  107. pcf_tm.time[PCF50633_TI_DAY],
  108. pcf_tm.time[PCF50633_TI_MONTH],
  109. pcf_tm.time[PCF50633_TI_YEAR],
  110. pcf_tm.time[PCF50633_TI_HOUR],
  111. pcf_tm.time[PCF50633_TI_MIN],
  112. pcf_tm.time[PCF50633_TI_SEC]);
  113. pcf2rtc_time(tm, &pcf_tm);
  114. dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
  115. tm->tm_mday, tm->tm_mon, tm->tm_year,
  116. tm->tm_hour, tm->tm_min, tm->tm_sec);
  117. return rtc_valid_tm(tm);
  118. }
  119. static int pcf50633_rtc_set_time(struct device *dev, struct rtc_time *tm)
  120. {
  121. struct pcf50633_rtc *rtc;
  122. struct pcf50633_time pcf_tm;
  123. int alarm_masked, ret = 0;
  124. rtc = dev_get_drvdata(dev);
  125. dev_dbg(dev, "RTC_TIME: %u.%u.%u %u:%u:%u\n",
  126. tm->tm_mday, tm->tm_mon, tm->tm_year,
  127. tm->tm_hour, tm->tm_min, tm->tm_sec);
  128. rtc2pcf_time(&pcf_tm, tm);
  129. dev_dbg(dev, "PCF_TIME: %02x.%02x.%02x %02x:%02x:%02x\n",
  130. pcf_tm.time[PCF50633_TI_DAY],
  131. pcf_tm.time[PCF50633_TI_MONTH],
  132. pcf_tm.time[PCF50633_TI_YEAR],
  133. pcf_tm.time[PCF50633_TI_HOUR],
  134. pcf_tm.time[PCF50633_TI_MIN],
  135. pcf_tm.time[PCF50633_TI_SEC]);
  136. alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
  137. if (!alarm_masked)
  138. pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
  139. /* Returns 0 on success */
  140. ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSC,
  141. PCF50633_TI_EXTENT,
  142. &pcf_tm.time[0]);
  143. if (!alarm_masked)
  144. pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
  145. return ret;
  146. }
  147. static int pcf50633_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  148. {
  149. struct pcf50633_rtc *rtc;
  150. struct pcf50633_time pcf_tm;
  151. int ret = 0;
  152. rtc = dev_get_drvdata(dev);
  153. alrm->enabled = rtc->alarm_enabled;
  154. alrm->pending = rtc->alarm_pending;
  155. ret = pcf50633_read_block(rtc->pcf, PCF50633_REG_RTCSCA,
  156. PCF50633_TI_EXTENT, &pcf_tm.time[0]);
  157. if (ret != PCF50633_TI_EXTENT) {
  158. dev_err(dev, "Failed to read time\n");
  159. return -EIO;
  160. }
  161. pcf2rtc_time(&alrm->time, &pcf_tm);
  162. return rtc_valid_tm(&alrm->time);
  163. }
  164. static int pcf50633_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  165. {
  166. struct pcf50633_rtc *rtc;
  167. struct pcf50633_time pcf_tm;
  168. int alarm_masked, ret = 0;
  169. rtc = dev_get_drvdata(dev);
  170. rtc2pcf_time(&pcf_tm, &alrm->time);
  171. /* do like mktime does and ignore tm_wday */
  172. pcf_tm.time[PCF50633_TI_WKDAY] = 7;
  173. alarm_masked = pcf50633_irq_mask_get(rtc->pcf, PCF50633_IRQ_ALARM);
  174. /* disable alarm interrupt */
  175. if (!alarm_masked)
  176. pcf50633_irq_mask(rtc->pcf, PCF50633_IRQ_ALARM);
  177. /* Returns 0 on success */
  178. ret = pcf50633_write_block(rtc->pcf, PCF50633_REG_RTCSCA,
  179. PCF50633_TI_EXTENT, &pcf_tm.time[0]);
  180. if (!alrm->enabled)
  181. rtc->alarm_pending = 0;
  182. if (!alarm_masked || alrm->enabled)
  183. pcf50633_irq_unmask(rtc->pcf, PCF50633_IRQ_ALARM);
  184. rtc->alarm_enabled = alrm->enabled;
  185. return ret;
  186. }
  187. static struct rtc_class_ops pcf50633_rtc_ops = {
  188. .read_time = pcf50633_rtc_read_time,
  189. .set_time = pcf50633_rtc_set_time,
  190. .read_alarm = pcf50633_rtc_read_alarm,
  191. .set_alarm = pcf50633_rtc_set_alarm,
  192. .alarm_irq_enable = pcf50633_rtc_alarm_irq_enable,
  193. };
  194. static void pcf50633_rtc_irq(int irq, void *data)
  195. {
  196. struct pcf50633_rtc *rtc = data;
  197. rtc_update_irq(rtc->rtc_dev, 1, RTC_AF | RTC_IRQF);
  198. rtc->alarm_pending = 1;
  199. }
  200. static int pcf50633_rtc_probe(struct platform_device *pdev)
  201. {
  202. struct pcf50633_rtc *rtc;
  203. rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
  204. if (!rtc)
  205. return -ENOMEM;
  206. rtc->pcf = dev_to_pcf50633(pdev->dev.parent);
  207. platform_set_drvdata(pdev, rtc);
  208. rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, "pcf50633-rtc",
  209. &pcf50633_rtc_ops, THIS_MODULE);
  210. if (IS_ERR(rtc->rtc_dev))
  211. return PTR_ERR(rtc->rtc_dev);
  212. pcf50633_register_irq(rtc->pcf, PCF50633_IRQ_ALARM,
  213. pcf50633_rtc_irq, rtc);
  214. return 0;
  215. }
  216. static int pcf50633_rtc_remove(struct platform_device *pdev)
  217. {
  218. struct pcf50633_rtc *rtc;
  219. rtc = platform_get_drvdata(pdev);
  220. pcf50633_free_irq(rtc->pcf, PCF50633_IRQ_ALARM);
  221. return 0;
  222. }
  223. static struct platform_driver pcf50633_rtc_driver = {
  224. .driver = {
  225. .name = "pcf50633-rtc",
  226. },
  227. .probe = pcf50633_rtc_probe,
  228. .remove = pcf50633_rtc_remove,
  229. };
  230. module_platform_driver(pcf50633_rtc_driver);
  231. MODULE_DESCRIPTION("PCF50633 RTC driver");
  232. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  233. MODULE_LICENSE("GPL");