rtc-max6902.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* drivers/rtc/rtc-max6902.c
  3. *
  4. * Copyright (C) 2006 8D Technologies inc.
  5. * Copyright (C) 2004 Compulab Ltd.
  6. *
  7. * Driver for MAX6902 spi RTC
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/init.h>
  13. #include <linux/rtc.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/bcd.h>
  16. #define MAX6902_REG_SECONDS 0x01
  17. #define MAX6902_REG_MINUTES 0x03
  18. #define MAX6902_REG_HOURS 0x05
  19. #define MAX6902_REG_DATE 0x07
  20. #define MAX6902_REG_MONTH 0x09
  21. #define MAX6902_REG_DAY 0x0B
  22. #define MAX6902_REG_YEAR 0x0D
  23. #define MAX6902_REG_CONTROL 0x0F
  24. #define MAX6902_REG_CENTURY 0x13
  25. static int max6902_set_reg(struct device *dev, unsigned char address,
  26. unsigned char data)
  27. {
  28. struct spi_device *spi = to_spi_device(dev);
  29. unsigned char buf[2];
  30. /* MSB must be '0' to write */
  31. buf[0] = address & 0x7f;
  32. buf[1] = data;
  33. return spi_write_then_read(spi, buf, 2, NULL, 0);
  34. }
  35. static int max6902_get_reg(struct device *dev, unsigned char address,
  36. unsigned char *data)
  37. {
  38. struct spi_device *spi = to_spi_device(dev);
  39. /* Set MSB to indicate read */
  40. *data = address | 0x80;
  41. return spi_write_then_read(spi, data, 1, data, 1);
  42. }
  43. static int max6902_read_time(struct device *dev, struct rtc_time *dt)
  44. {
  45. int err, century;
  46. struct spi_device *spi = to_spi_device(dev);
  47. unsigned char buf[8];
  48. buf[0] = 0xbf; /* Burst read */
  49. err = spi_write_then_read(spi, buf, 1, buf, 8);
  50. if (err != 0)
  51. return err;
  52. /* The chip sends data in this order:
  53. * Seconds, Minutes, Hours, Date, Month, Day, Year */
  54. dt->tm_sec = bcd2bin(buf[0]);
  55. dt->tm_min = bcd2bin(buf[1]);
  56. dt->tm_hour = bcd2bin(buf[2]);
  57. dt->tm_mday = bcd2bin(buf[3]);
  58. dt->tm_mon = bcd2bin(buf[4]) - 1;
  59. dt->tm_wday = bcd2bin(buf[5]);
  60. dt->tm_year = bcd2bin(buf[6]);
  61. /* Read century */
  62. err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
  63. if (err != 0)
  64. return err;
  65. century = bcd2bin(buf[0]) * 100;
  66. dt->tm_year += century;
  67. dt->tm_year -= 1900;
  68. return 0;
  69. }
  70. static int max6902_set_time(struct device *dev, struct rtc_time *dt)
  71. {
  72. dt->tm_year = dt->tm_year + 1900;
  73. /* Remove write protection */
  74. max6902_set_reg(dev, MAX6902_REG_CONTROL, 0);
  75. max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec));
  76. max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min));
  77. max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour));
  78. max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday));
  79. max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1));
  80. max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday));
  81. max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100));
  82. max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100));
  83. /* Compulab used a delay here. However, the datasheet
  84. * does not mention a delay being required anywhere... */
  85. /* delay(2000); */
  86. /* Write protect */
  87. max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80);
  88. return 0;
  89. }
  90. static const struct rtc_class_ops max6902_rtc_ops = {
  91. .read_time = max6902_read_time,
  92. .set_time = max6902_set_time,
  93. };
  94. static int max6902_probe(struct spi_device *spi)
  95. {
  96. struct rtc_device *rtc;
  97. unsigned char tmp;
  98. int res;
  99. spi->mode = SPI_MODE_3;
  100. spi->bits_per_word = 8;
  101. spi_setup(spi);
  102. res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
  103. if (res != 0)
  104. return res;
  105. rtc = devm_rtc_device_register(&spi->dev, "max6902",
  106. &max6902_rtc_ops, THIS_MODULE);
  107. if (IS_ERR(rtc))
  108. return PTR_ERR(rtc);
  109. spi_set_drvdata(spi, rtc);
  110. return 0;
  111. }
  112. static struct spi_driver max6902_driver = {
  113. .driver = {
  114. .name = "rtc-max6902",
  115. },
  116. .probe = max6902_probe,
  117. };
  118. module_spi_driver(max6902_driver);
  119. MODULE_DESCRIPTION("max6902 spi RTC driver");
  120. MODULE_AUTHOR("Raphael Assenat");
  121. MODULE_LICENSE("GPL");
  122. MODULE_ALIAS("spi:rtc-max6902");