rtc-ds1216.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 platform_device *pdev = to_platform_device(dev);
  69. struct ds1216_priv *priv = platform_get_drvdata(pdev);
  70. struct ds1216_regs regs;
  71. ds1216_switch_ds_to_clock(priv->ioaddr);
  72. ds1216_read(priv->ioaddr, (u8 *)&regs);
  73. tm->tm_sec = bcd2bin(regs.sec);
  74. tm->tm_min = bcd2bin(regs.min);
  75. if (regs.hour & DS1216_HOUR_1224) {
  76. /* AM/PM mode */
  77. tm->tm_hour = bcd2bin(regs.hour & 0x1f);
  78. if (regs.hour & DS1216_HOUR_AMPM)
  79. tm->tm_hour += 12;
  80. } else
  81. tm->tm_hour = bcd2bin(regs.hour & 0x3f);
  82. tm->tm_wday = (regs.wday & 7) - 1;
  83. tm->tm_mday = bcd2bin(regs.mday & 0x3f);
  84. tm->tm_mon = bcd2bin(regs.month & 0x1f);
  85. tm->tm_year = bcd2bin(regs.year);
  86. if (tm->tm_year < 70)
  87. tm->tm_year += 100;
  88. return rtc_valid_tm(tm);
  89. }
  90. static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
  91. {
  92. struct platform_device *pdev = to_platform_device(dev);
  93. struct ds1216_priv *priv = platform_get_drvdata(pdev);
  94. struct ds1216_regs regs;
  95. ds1216_switch_ds_to_clock(priv->ioaddr);
  96. ds1216_read(priv->ioaddr, (u8 *)&regs);
  97. regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
  98. regs.sec = bin2bcd(tm->tm_sec);
  99. regs.min = bin2bcd(tm->tm_min);
  100. regs.hour &= DS1216_HOUR_1224;
  101. if (regs.hour && tm->tm_hour > 12) {
  102. regs.hour |= DS1216_HOUR_AMPM;
  103. tm->tm_hour -= 12;
  104. }
  105. regs.hour |= bin2bcd(tm->tm_hour);
  106. regs.wday &= ~7;
  107. regs.wday |= tm->tm_wday;
  108. regs.mday = bin2bcd(tm->tm_mday);
  109. regs.month = bin2bcd(tm->tm_mon);
  110. regs.year = bin2bcd(tm->tm_year % 100);
  111. ds1216_switch_ds_to_clock(priv->ioaddr);
  112. ds1216_write(priv->ioaddr, (u8 *)&regs);
  113. return 0;
  114. }
  115. static const struct rtc_class_ops ds1216_rtc_ops = {
  116. .read_time = ds1216_rtc_read_time,
  117. .set_time = ds1216_rtc_set_time,
  118. };
  119. static int __init ds1216_rtc_probe(struct platform_device *pdev)
  120. {
  121. struct resource *res;
  122. struct ds1216_priv *priv;
  123. u8 dummy[8];
  124. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  125. if (!priv)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, priv);
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. priv->ioaddr = devm_ioremap_resource(&pdev->dev, res);
  130. if (IS_ERR(priv->ioaddr))
  131. return PTR_ERR(priv->ioaddr);
  132. priv->rtc = devm_rtc_device_register(&pdev->dev, "ds1216",
  133. &ds1216_rtc_ops, THIS_MODULE);
  134. if (IS_ERR(priv->rtc))
  135. return PTR_ERR(priv->rtc);
  136. /* dummy read to get clock into a known state */
  137. ds1216_read(priv->ioaddr, dummy);
  138. return 0;
  139. }
  140. static struct platform_driver ds1216_rtc_platform_driver = {
  141. .driver = {
  142. .name = "rtc-ds1216",
  143. },
  144. };
  145. module_platform_driver_probe(ds1216_rtc_platform_driver, ds1216_rtc_probe);
  146. MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
  147. MODULE_DESCRIPTION("DS1216 RTC driver");
  148. MODULE_LICENSE("GPL");
  149. MODULE_ALIAS("platform:rtc-ds1216");