rtc-ds1216.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. struct ds1216_regs {
  13. u8 tsec;
  14. u8 sec;
  15. u8 min;
  16. u8 hour;
  17. u8 wday;
  18. u8 mday;
  19. u8 month;
  20. u8 year;
  21. };
  22. #define DS1216_HOUR_1224 (1 << 7)
  23. #define DS1216_HOUR_AMPM (1 << 5)
  24. struct ds1216_priv {
  25. struct rtc_device *rtc;
  26. void __iomem *ioaddr;
  27. };
  28. static const u8 magic[] = {
  29. 0xc5, 0x3a, 0xa3, 0x5c, 0xc5, 0x3a, 0xa3, 0x5c
  30. };
  31. /*
  32. * Read the 64 bit we'd like to have - It a series
  33. * of 64 bits showing up in the LSB of the base register.
  34. *
  35. */
  36. static void ds1216_read(u8 __iomem *ioaddr, u8 *buf)
  37. {
  38. unsigned char c;
  39. int i, j;
  40. for (i = 0; i < 8; i++) {
  41. c = 0;
  42. for (j = 0; j < 8; j++)
  43. c |= (readb(ioaddr) & 0x1) << j;
  44. buf[i] = c;
  45. }
  46. }
  47. static void ds1216_write(u8 __iomem *ioaddr, const u8 *buf)
  48. {
  49. unsigned char c;
  50. int i, j;
  51. for (i = 0; i < 8; i++) {
  52. c = buf[i];
  53. for (j = 0; j < 8; j++) {
  54. writeb(c, ioaddr);
  55. c = c >> 1;
  56. }
  57. }
  58. }
  59. static void ds1216_switch_ds_to_clock(u8 __iomem *ioaddr)
  60. {
  61. /* Reset magic pointer */
  62. readb(ioaddr);
  63. /* Write 64 bit magic to DS1216 */
  64. ds1216_write(ioaddr, magic);
  65. }
  66. static int ds1216_rtc_read_time(struct device *dev, struct rtc_time *tm)
  67. {
  68. struct ds1216_priv *priv = dev_get_drvdata(dev);
  69. struct ds1216_regs regs;
  70. ds1216_switch_ds_to_clock(priv->ioaddr);
  71. ds1216_read(priv->ioaddr, (u8 *)&regs);
  72. tm->tm_sec = bcd2bin(regs.sec);
  73. tm->tm_min = bcd2bin(regs.min);
  74. if (regs.hour & DS1216_HOUR_1224) {
  75. /* AM/PM mode */
  76. tm->tm_hour = bcd2bin(regs.hour & 0x1f);
  77. if (regs.hour & DS1216_HOUR_AMPM)
  78. tm->tm_hour += 12;
  79. } else
  80. tm->tm_hour = bcd2bin(regs.hour & 0x3f);
  81. tm->tm_wday = (regs.wday & 7) - 1;
  82. tm->tm_mday = bcd2bin(regs.mday & 0x3f);
  83. tm->tm_mon = bcd2bin(regs.month & 0x1f);
  84. tm->tm_year = bcd2bin(regs.year);
  85. if (tm->tm_year < 70)
  86. tm->tm_year += 100;
  87. return 0;
  88. }
  89. static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct ds1216_priv *priv = dev_get_drvdata(dev);
  92. struct ds1216_regs regs;
  93. ds1216_switch_ds_to_clock(priv->ioaddr);
  94. ds1216_read(priv->ioaddr, (u8 *)&regs);
  95. regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
  96. regs.sec = bin2bcd(tm->tm_sec);
  97. regs.min = bin2bcd(tm->tm_min);
  98. regs.hour &= DS1216_HOUR_1224;
  99. if (regs.hour && tm->tm_hour > 12) {
  100. regs.hour |= DS1216_HOUR_AMPM;
  101. tm->tm_hour -= 12;
  102. }
  103. regs.hour |= bin2bcd(tm->tm_hour);
  104. regs.wday &= ~7;
  105. regs.wday |= tm->tm_wday;
  106. regs.mday = bin2bcd(tm->tm_mday);
  107. regs.month = bin2bcd(tm->tm_mon);
  108. regs.year = bin2bcd(tm->tm_year % 100);
  109. ds1216_switch_ds_to_clock(priv->ioaddr);
  110. ds1216_write(priv->ioaddr, (u8 *)&regs);
  111. return 0;
  112. }
  113. static const struct rtc_class_ops ds1216_rtc_ops = {
  114. .read_time = ds1216_rtc_read_time,
  115. .set_time = ds1216_rtc_set_time,
  116. };
  117. static int __init ds1216_rtc_probe(struct platform_device *pdev)
  118. {
  119. struct resource *res;
  120. struct ds1216_priv *priv;
  121. u8 dummy[8];
  122. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  123. if (!priv)
  124. return -ENOMEM;
  125. platform_set_drvdata(pdev, priv);
  126. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  127. priv->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  128. if (IS_ERR(priv->ioaddr))
  129. return PTR_ERR(priv->ioaddr);
  130. priv->rtc = devm_rtc_device_register(&pdev->dev, "ds1216",
  131. &ds1216_rtc_ops, THIS_MODULE);
  132. if (IS_ERR(priv->rtc))
  133. return PTR_ERR(priv->rtc);
  134. /* dummy read to get clock into a known state */
  135. ds1216_read(priv->ioaddr, dummy);
  136. return 0;
  137. }
  138. static struct platform_driver ds1216_rtc_platform_driver = {
  139. .driver = {
  140. .name = "rtc-ds1216",
  141. },
  142. };
  143. module_platform_driver_probe(ds1216_rtc_platform_driver, ds1216_rtc_probe);
  144. MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
  145. MODULE_DESCRIPTION("DS1216 RTC driver");
  146. MODULE_LICENSE("GPL");
  147. MODULE_ALIAS("platform:rtc-ds1216");