rtc-m41t94.c 3.9 KB

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