rtc-m41t94.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for ST M41T94 SPI RTC
  4. *
  5. * Copyright (C) 2008 Kim B. Heino
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/rtc.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/bcd.h>
  13. #define M41T94_REG_SECONDS 0x01
  14. #define M41T94_REG_MINUTES 0x02
  15. #define M41T94_REG_HOURS 0x03
  16. #define M41T94_REG_WDAY 0x04
  17. #define M41T94_REG_DAY 0x05
  18. #define M41T94_REG_MONTH 0x06
  19. #define M41T94_REG_YEAR 0x07
  20. #define M41T94_REG_HT 0x0c
  21. #define M41T94_BIT_HALT 0x40
  22. #define M41T94_BIT_STOP 0x80
  23. #define M41T94_BIT_CB 0x40
  24. #define M41T94_BIT_CEB 0x80
  25. static int m41t94_set_time(struct device *dev, struct rtc_time *tm)
  26. {
  27. struct spi_device *spi = to_spi_device(dev);
  28. u8 buf[8]; /* write cmd + 7 registers */
  29. dev_dbg(dev, "%s secs=%d, mins=%d, "
  30. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  31. "write", tm->tm_sec, tm->tm_min,
  32. tm->tm_hour, tm->tm_mday,
  33. tm->tm_mon, tm->tm_year, tm->tm_wday);
  34. buf[0] = 0x80 | M41T94_REG_SECONDS; /* write time + date */
  35. buf[M41T94_REG_SECONDS] = bin2bcd(tm->tm_sec);
  36. buf[M41T94_REG_MINUTES] = bin2bcd(tm->tm_min);
  37. buf[M41T94_REG_HOURS] = bin2bcd(tm->tm_hour);
  38. buf[M41T94_REG_WDAY] = bin2bcd(tm->tm_wday + 1);
  39. buf[M41T94_REG_DAY] = bin2bcd(tm->tm_mday);
  40. buf[M41T94_REG_MONTH] = bin2bcd(tm->tm_mon + 1);
  41. buf[M41T94_REG_HOURS] |= M41T94_BIT_CEB;
  42. if (tm->tm_year >= 100)
  43. buf[M41T94_REG_HOURS] |= M41T94_BIT_CB;
  44. buf[M41T94_REG_YEAR] = bin2bcd(tm->tm_year % 100);
  45. return spi_write(spi, buf, 8);
  46. }
  47. static int m41t94_read_time(struct device *dev, struct rtc_time *tm)
  48. {
  49. struct spi_device *spi = to_spi_device(dev);
  50. u8 buf[2];
  51. int ret, hour;
  52. /* clear halt update bit */
  53. ret = spi_w8r8(spi, M41T94_REG_HT);
  54. if (ret < 0)
  55. return ret;
  56. if (ret & M41T94_BIT_HALT) {
  57. buf[0] = 0x80 | M41T94_REG_HT;
  58. buf[1] = ret & ~M41T94_BIT_HALT;
  59. spi_write(spi, buf, 2);
  60. }
  61. /* clear stop bit */
  62. ret = spi_w8r8(spi, M41T94_REG_SECONDS);
  63. if (ret < 0)
  64. return ret;
  65. if (ret & M41T94_BIT_STOP) {
  66. buf[0] = 0x80 | M41T94_REG_SECONDS;
  67. buf[1] = ret & ~M41T94_BIT_STOP;
  68. spi_write(spi, buf, 2);
  69. }
  70. tm->tm_sec = bcd2bin(spi_w8r8(spi, M41T94_REG_SECONDS));
  71. tm->tm_min = bcd2bin(spi_w8r8(spi, M41T94_REG_MINUTES));
  72. hour = spi_w8r8(spi, M41T94_REG_HOURS);
  73. tm->tm_hour = bcd2bin(hour & 0x3f);
  74. tm->tm_wday = bcd2bin(spi_w8r8(spi, M41T94_REG_WDAY)) - 1;
  75. tm->tm_mday = bcd2bin(spi_w8r8(spi, M41T94_REG_DAY));
  76. tm->tm_mon = bcd2bin(spi_w8r8(spi, M41T94_REG_MONTH)) - 1;
  77. tm->tm_year = bcd2bin(spi_w8r8(spi, M41T94_REG_YEAR));
  78. if ((hour & M41T94_BIT_CB) || !(hour & M41T94_BIT_CEB))
  79. tm->tm_year += 100;
  80. dev_dbg(dev, "%s secs=%d, mins=%d, "
  81. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  82. "read", tm->tm_sec, tm->tm_min,
  83. tm->tm_hour, tm->tm_mday,
  84. tm->tm_mon, tm->tm_year, tm->tm_wday);
  85. return 0;
  86. }
  87. static const struct rtc_class_ops m41t94_rtc_ops = {
  88. .read_time = m41t94_read_time,
  89. .set_time = m41t94_set_time,
  90. };
  91. static struct spi_driver m41t94_driver;
  92. static int m41t94_probe(struct spi_device *spi)
  93. {
  94. struct rtc_device *rtc;
  95. int res;
  96. spi->bits_per_word = 8;
  97. spi_setup(spi);
  98. res = spi_w8r8(spi, M41T94_REG_SECONDS);
  99. if (res < 0) {
  100. dev_err(&spi->dev, "not found.\n");
  101. return res;
  102. }
  103. rtc = devm_rtc_device_register(&spi->dev, m41t94_driver.driver.name,
  104. &m41t94_rtc_ops, THIS_MODULE);
  105. if (IS_ERR(rtc))
  106. return PTR_ERR(rtc);
  107. spi_set_drvdata(spi, rtc);
  108. return 0;
  109. }
  110. static struct spi_driver m41t94_driver = {
  111. .driver = {
  112. .name = "rtc-m41t94",
  113. },
  114. .probe = m41t94_probe,
  115. };
  116. module_spi_driver(m41t94_driver);
  117. MODULE_AUTHOR("Kim B. Heino <[email protected]>");
  118. MODULE_DESCRIPTION("Driver for ST M41T94 SPI RTC");
  119. MODULE_LICENSE("GPL");
  120. MODULE_ALIAS("spi:rtc-m41t94");