rtc-max6902.c 3.9 KB

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