rtc-ds1216.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Dallas DS1216 RTC driver
  4. *
  5. * Copyright (c) 2007 Thomas Bogendoerfer
  6. *
  7. */
  8. #include <linux/module.h>
  9. #include <linux/rtc.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/bcd.h>
  12. #include <linux/slab.h>
  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 ds1216_priv *priv = dev_get_drvdata(dev);
  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 0;
  89. }
  90. static int ds1216_rtc_set_time(struct device *dev, struct rtc_time *tm)
  91. {
  92. struct ds1216_priv *priv = dev_get_drvdata(dev);
  93. struct ds1216_regs regs;
  94. ds1216_switch_ds_to_clock(priv->ioaddr);
  95. ds1216_read(priv->ioaddr, (u8 *)&regs);
  96. regs.tsec = 0; /* clear 0.1 and 0.01 seconds */
  97. regs.sec = bin2bcd(tm->tm_sec);
  98. regs.min = bin2bcd(tm->tm_min);
  99. regs.hour &= DS1216_HOUR_1224;
  100. if (regs.hour && tm->tm_hour > 12) {
  101. regs.hour |= DS1216_HOUR_AMPM;
  102. tm->tm_hour -= 12;
  103. }
  104. regs.hour |= bin2bcd(tm->tm_hour);
  105. regs.wday &= ~7;
  106. regs.wday |= tm->tm_wday;
  107. regs.mday = bin2bcd(tm->tm_mday);
  108. regs.month = bin2bcd(tm->tm_mon);
  109. regs.year = bin2bcd(tm->tm_year % 100);
  110. ds1216_switch_ds_to_clock(priv->ioaddr);
  111. ds1216_write(priv->ioaddr, (u8 *)&regs);
  112. return 0;
  113. }
  114. static const struct rtc_class_ops ds1216_rtc_ops = {
  115. .read_time = ds1216_rtc_read_time,
  116. .set_time = ds1216_rtc_set_time,
  117. };
  118. static int __init ds1216_rtc_probe(struct platform_device *pdev)
  119. {
  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. priv->ioaddr = devm_platform_ioremap_resource(pdev, 0);
  127. if (IS_ERR(priv->ioaddr))
  128. return PTR_ERR(priv->ioaddr);
  129. priv->rtc = devm_rtc_device_register(&pdev->dev, "ds1216",
  130. &ds1216_rtc_ops, THIS_MODULE);
  131. if (IS_ERR(priv->rtc))
  132. return PTR_ERR(priv->rtc);
  133. /* dummy read to get clock into a known state */
  134. ds1216_read(priv->ioaddr, dummy);
  135. return 0;
  136. }
  137. static struct platform_driver ds1216_rtc_platform_driver = {
  138. .driver = {
  139. .name = "rtc-ds1216",
  140. },
  141. };
  142. module_platform_driver_probe(ds1216_rtc_platform_driver, ds1216_rtc_probe);
  143. MODULE_AUTHOR("Thomas Bogendoerfer <[email protected]>");
  144. MODULE_DESCRIPTION("DS1216 RTC driver");
  145. MODULE_LICENSE("GPL");
  146. MODULE_ALIAS("platform:rtc-ds1216");