rtc-ds1216.c 4.0 KB

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