rtc-ds1216.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Dallas DS1216 RTC driver
  3. *
  4. * Copyright (c) 2007 Thomas Bogendoerfer
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/rtc.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/bcd.h>
  11. #include <linux/slab.h>
  12. #define DRV_VERSION "0.2"
  13. struct ds1216_regs {
  14. u8 tsec;
  15. u8 sec;
  16. u8 min;
  17. u8 hour;
  18. u8 wday;
  19. u8 mday;
  20. u8 month;
  21. u8 year;
  22. };
  23. #define DS1216_HOUR_1224 (1 << 7)
  24. #define DS1216_HOUR_AMPM (1 << 5)
  25. struct ds1216_priv {
  26. struct rtc_device *rtc;
  27. void __iomem *ioaddr;
  28. size_t size;
  29. unsigned long baseaddr;
  30. };
  31. static const u8 magic[] = {
  32. 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
  33. };
  34. /*
  35. * Read the 64 bit we'd like to have - It a series
  36. * of 64 bits showing up in the LSB of the base register.
  37. *
  38. */
  39. static void ds1216_read(u8 __iomem *ioaddr, u8 *buf)
  40. {
  41. unsigned char c;
  42. int i, j;
  43. for (i = 0; i < 8; i++) {
  44. c = 0;
  45. for (j = 0; j < 8; j++)
  46. c |= (readb(ioaddr) & 0x1) << j;
  47. buf[i] = c;
  48. }
  49. }
  50. static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf)
  51. {
  52. unsigned char c;
  53. int i, j;
  54. for (i = 0; i < 8; i++) {
  55. c = buf[i];
  56. for (j = 0; j < 8; j++) {
  57. writeb(c, ioaddr);
  58. c = c >> 1;
  59. }
  60. }
  61. }
  62. static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr)
  63. {
  64. /* Reset magic pointer */
  65. readb(ioaddr);
  66. /* Write 64 bit magic to DS1216 */
  67. ds1216_write(ioaddr, magic);
  68. }
  69. static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm)
  70. {
  71. struct platform_device *pdev = to_platform_device(dev);
  72. struct ds1216_priv *priv = platform_get_drvdata(pdev);
  73. struct ds1216_regs regs;
  74. ds1216_switch_ds_to_clock(priv->ioaddr);
  75. ds1216_read(priv->ioaddr, (u8 *)&regs);
  76. tm->tm_sec = bcd2bin(regs.sec);
  77. tm->tm_min = bcd2bin(regs.min);
  78. if (regs.hour & DS1216_HOUR_1224) {
  79. /* AM/PM mode */
  80. tm->tm_hour = bcd2bin(regs.hour & 0x1f);
  81. if (regs.hour & DS1216_HOUR_AMPM)
  82. tm->tm_hour += 12;
  83. } else
  84. tm->tm_hour = bcd2bin(regs.hour & 0x3f);
  85. tm->tm_wday = (regs.wday & 7) - 1;
  86. tm->tm_mday = bcd2bin(regs.mday & 0x3f);
  87. tm->tm_mon = bcd2bin(regs.month & 0x1f);
  88. tm->tm_year = bcd2bin(regs.year);
  89. if (tm->tm_year < 70)
  90. tm->tm_year += 100;
  91. return rtc_valid_tm(tm);
  92. }
  93. static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
  94. {
  95. struct platform_device *pdev = to_platform_device(dev);
  96. struct ds1216_priv *priv = platform_get_drvdata(pdev);
  97. struct ds1216_regs regs;
  98. ds1216_switch_ds_to_clock(priv->ioaddr);
  99. ds1216_read(priv->ioaddr, (u8 *)&regs);
  100. regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
  101. regs.sec = bin2bcd(tm->tm_sec);
  102. regs.min = bin2bcd(tm->tm_min);
  103. regs.hour &= DS1216_HOUR_1224;
  104. if (regs.hour && tm->tm_hour > 12) {
  105. regs.hour |= DS1216_HOUR_AMPM;
  106. tm->tm_hour -= 12;
  107. }
  108. regs.hour |= bin2bcd(tm->tm_hour);
  109. regs.wday &= ~7;
  110. regs.wday |= tm->tm_wday;
  111. regs.mday = bin2bcd(tm->tm_mday);
  112. regs.month = bin2bcd(tm->tm_mon);
  113. regs.year = bin2bcd(tm->tm_year % 100);
  114. ds1216_switch_ds_to_clock(priv->ioaddr);
  115. ds1216_write(priv->ioaddr, (u8 *)&regs);
  116. return 0;
  117. }
  118. static const struct rtc_class_ops ds1216_rtc_ops = {
  119. .read_time = ds1216_rtc_read_time,
  120. .set_time = ds1216_rtc_set_time,
  121. };
  122. static int __init ds1216_rtc_probe(struct platform_device *pdev)
  123. {
  124. struct resource *res;
  125. struct ds1216_priv *priv;
  126. int ret = 0;
  127. u8 dummy[8];
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. if (!res)
  130. return -ENODEV;
  131. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  132. if (!priv)
  133. return -ENOMEM;
  134. platform_set_drvdata(pdev, priv);
  135. priv->size = resource_size(res);
  136. if (!request_mem_region(res->start, priv->size, pdev->name)) {
  137. ret = -EBUSY;
  138. goto out;
  139. }
  140. priv->baseaddr = res->start;
  141. priv->ioaddr = ioremap(priv->baseaddr, priv->size);
  142. if (!priv->ioaddr) {
  143. ret = -ENOMEM;
  144. goto out;
  145. }
  146. priv->rtc = rtc_device_register("ds1216", &pdev->dev,
  147. &ds1216_rtc_ops, THIS_MODULE);
  148. if (IS_ERR(priv->rtc)) {
  149. ret = PTR_ERR(priv->rtc);
  150. goto out;
  151. }
  152. /* dummy read to get clock into a known state */
  153. ds1216_read(priv->ioaddr, dummy);
  154. return 0;
  155. out:
  156. if (priv->ioaddr)
  157. iounmap(priv->ioaddr);
  158. if (priv->baseaddr)
  159. release_mem_region(priv->baseaddr, priv->size);
  160. kfree(priv);
  161. return ret;
  162. }
  163. static int __exit ds1216_rtc_remove(struct platform_device *pdev)
  164. {
  165. struct ds1216_priv *priv = platform_get_drvdata(pdev);
  166. rtc_device_unregister(priv->rtc);
  167. iounmap(priv->ioaddr);
  168. release_mem_region(priv->baseaddr, priv->size);
  169. kfree(priv);
  170. return 0;
  171. }
  172. static struct platform_driver ds1216_rtc_platform_driver = {
  173. .driver = {
  174. .name = "rtc-ds1216",
  175. .owner = THIS_MODULE,
  176. },
  177. .remove = __exit_p(ds1216_rtc_remove),
  178. };
  179. static int __init ds1216_rtc_init(void)
  180. {
  181. return platform_driver_probe(&ds1216_rtc_platform_driver, ds1216_rtc_probe);
  182. }
  183. static void __exit ds1216_rtc_exit(void)
  184. {
  185. platform_driver_unregister(&ds1216_rtc_platform_driver);
  186. }
  187. MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
  188. MODULE_DESCRIPTION("DS1216 RTC driver");
  189. MODULE_LICENSE("GPL");
  190. MODULE_VERSION(DRV_VERSION);
  191. MODULE_ALIAS("platform:rtc-ds1216");
  192. module_init(ds1216_rtc_init);
  193. module_exit(ds1216_rtc_exit);