rtc-r9701.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Driver for Epson RTC-9701JE
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Based on rtc-max6902.c
  7. *
  8. * Copyright (C) 2006 8D Technologies inc.
  9. * Copyright (C) 2004 Compulab Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/rtc.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/bcd.h>
  23. #include <linux/delay.h>
  24. #include <linux/bitops.h>
  25. #define RSECCNT 0x00 /* Second Counter */
  26. #define RMINCNT 0x01 /* Minute Counter */
  27. #define RHRCNT 0x02 /* Hour Counter */
  28. #define RWKCNT 0x03 /* Week Counter */
  29. #define RDAYCNT 0x04 /* Day Counter */
  30. #define RMONCNT 0x05 /* Month Counter */
  31. #define RYRCNT 0x06 /* Year Counter */
  32. #define R100CNT 0x07 /* Y100 Counter */
  33. #define RMINAR 0x08 /* Minute Alarm */
  34. #define RHRAR 0x09 /* Hour Alarm */
  35. #define RWKAR 0x0a /* Week/Day Alarm */
  36. #define RTIMCNT 0x0c /* Interval Timer */
  37. #define REXT 0x0d /* Extension Register */
  38. #define RFLAG 0x0e /* RTC Flag Register */
  39. #define RCR 0x0f /* RTC Control Register */
  40. static int write_reg(struct device *dev, int address, unsigned char data)
  41. {
  42. struct spi_device *spi = to_spi_device(dev);
  43. unsigned char buf[2];
  44. buf[0] = address & 0x7f;
  45. buf[1] = data;
  46. return spi_write(spi, buf, ARRAY_SIZE(buf));
  47. }
  48. static int read_regs(struct device *dev, unsigned char *regs, int no_regs)
  49. {
  50. struct spi_device *spi = to_spi_device(dev);
  51. u8 txbuf[1], rxbuf[1];
  52. int k, ret;
  53. ret = 0;
  54. for (k = 0; ret == 0 && k < no_regs; k++) {
  55. txbuf[0] = 0x80 | regs[k];
  56. ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1);
  57. regs[k] = rxbuf[0];
  58. }
  59. return ret;
  60. }
  61. static int r9701_get_datetime(struct device *dev, struct rtc_time *dt)
  62. {
  63. int ret;
  64. unsigned char buf[] = { RSECCNT, RMINCNT, RHRCNT,
  65. RDAYCNT, RMONCNT, RYRCNT };
  66. ret = read_regs(dev, buf, ARRAY_SIZE(buf));
  67. if (ret)
  68. return ret;
  69. memset(dt, 0, sizeof(*dt));
  70. dt->tm_sec = bcd2bin(buf[0]); /* RSECCNT */
  71. dt->tm_min = bcd2bin(buf[1]); /* RMINCNT */
  72. dt->tm_hour = bcd2bin(buf[2]); /* RHRCNT */
  73. dt->tm_mday = bcd2bin(buf[3]); /* RDAYCNT */
  74. dt->tm_mon = bcd2bin(buf[4]) - 1; /* RMONCNT */
  75. dt->tm_year = bcd2bin(buf[5]) + 100; /* RYRCNT */
  76. /* the rtc device may contain illegal values on power up
  77. * according to the data sheet. make sure they are valid.
  78. */
  79. return rtc_valid_tm(dt);
  80. }
  81. static int r9701_set_datetime(struct device *dev, struct rtc_time *dt)
  82. {
  83. int ret, year;
  84. year = dt->tm_year + 1900;
  85. if (year >= 2100 || year < 2000)
  86. return -EINVAL;
  87. ret = write_reg(dev, RHRCNT, bin2bcd(dt->tm_hour));
  88. ret = ret ? ret : write_reg(dev, RMINCNT, bin2bcd(dt->tm_min));
  89. ret = ret ? ret : write_reg(dev, RSECCNT, bin2bcd(dt->tm_sec));
  90. ret = ret ? ret : write_reg(dev, RDAYCNT, bin2bcd(dt->tm_mday));
  91. ret = ret ? ret : write_reg(dev, RMONCNT, bin2bcd(dt->tm_mon + 1));
  92. ret = ret ? ret : write_reg(dev, RYRCNT, bin2bcd(dt->tm_year - 100));
  93. ret = ret ? ret : write_reg(dev, RWKCNT, 1 << dt->tm_wday);
  94. return ret;
  95. }
  96. static const struct rtc_class_ops r9701_rtc_ops = {
  97. .read_time = r9701_get_datetime,
  98. .set_time = r9701_set_datetime,
  99. };
  100. static int r9701_probe(struct spi_device *spi)
  101. {
  102. struct rtc_device *rtc;
  103. struct rtc_time dt;
  104. unsigned char tmp;
  105. int res;
  106. tmp = R100CNT;
  107. res = read_regs(&spi->dev, &tmp, 1);
  108. if (res || tmp != 0x20) {
  109. dev_err(&spi->dev, "cannot read RTC register\n");
  110. return -ENODEV;
  111. }
  112. /*
  113. * The device seems to be present. Now check if the registers
  114. * contain invalid values. If so, try to write a default date:
  115. * 2000/1/1 00:00:00
  116. */
  117. if (r9701_get_datetime(&spi->dev, &dt)) {
  118. dev_info(&spi->dev, "trying to repair invalid date/time\n");
  119. dt.tm_sec = 0;
  120. dt.tm_min = 0;
  121. dt.tm_hour = 0;
  122. dt.tm_mday = 1;
  123. dt.tm_mon = 0;
  124. dt.tm_year = 100;
  125. if (r9701_set_datetime(&spi->dev, &dt) ||
  126. r9701_get_datetime(&spi->dev, &dt)) {
  127. dev_err(&spi->dev, "cannot repair RTC register\n");
  128. return -ENODEV;
  129. }
  130. }
  131. rtc = devm_rtc_device_register(&spi->dev, "r9701",
  132. &r9701_rtc_ops, THIS_MODULE);
  133. if (IS_ERR(rtc))
  134. return PTR_ERR(rtc);
  135. spi_set_drvdata(spi, rtc);
  136. return 0;
  137. }
  138. static int r9701_remove(struct spi_device *spi)
  139. {
  140. return 0;
  141. }
  142. static struct spi_driver r9701_driver = {
  143. .driver = {
  144. .name = "rtc-r9701",
  145. },
  146. .probe = r9701_probe,
  147. .remove = r9701_remove,
  148. };
  149. module_spi_driver(r9701_driver);
  150. MODULE_DESCRIPTION("r9701 spi RTC driver");
  151. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  152. MODULE_LICENSE("GPL");
  153. MODULE_ALIAS("spi:rtc-r9701");