rtc-max6916.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* rtc-max6916.c
  3. *
  4. * Driver for MAXIM max6916 Low Current, SPI Compatible
  5. * Real Time Clock
  6. *
  7. * Author : Venkat Prashanth B U <venkat.prashanth2498@gmail.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/rtc.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/bcd.h>
  16. /* Registers in max6916 rtc */
  17. #define MAX6916_SECONDS_REG 0x01
  18. #define MAX6916_MINUTES_REG 0x02
  19. #define MAX6916_HOURS_REG 0x03
  20. #define MAX6916_DATE_REG 0x04
  21. #define MAX6916_MONTH_REG 0x05
  22. #define MAX6916_DAY_REG 0x06
  23. #define MAX6916_YEAR_REG 0x07
  24. #define MAX6916_CONTROL_REG 0x08
  25. #define MAX6916_STATUS_REG 0x0C
  26. #define MAX6916_CLOCK_BURST 0x3F
  27. static int max6916_read_reg(struct device *dev, unsigned char address,
  28. unsigned char *data)
  29. {
  30. struct spi_device *spi = to_spi_device(dev);
  31. *data = address | 0x80;
  32. return spi_write_then_read(spi, data, 1, data, 1);
  33. }
  34. static int max6916_write_reg(struct device *dev, unsigned char address,
  35. unsigned char data)
  36. {
  37. struct spi_device *spi = to_spi_device(dev);
  38. unsigned char buf[2];
  39. buf[0] = address & 0x7F;
  40. buf[1] = data;
  41. return spi_write_then_read(spi, buf, 2, NULL, 0);
  42. }
  43. static int max6916_read_time(struct device *dev, struct rtc_time *dt)
  44. {
  45. struct spi_device *spi = to_spi_device(dev);
  46. int err;
  47. unsigned char buf[8];
  48. buf[0] = MAX6916_CLOCK_BURST | 0x80;
  49. err = spi_write_then_read(spi, buf, 1, buf, 8);
  50. if (err)
  51. return err;
  52. dt->tm_sec = bcd2bin(buf[0]);
  53. dt->tm_min = bcd2bin(buf[1]);
  54. dt->tm_hour = bcd2bin(buf[2] & 0x3F);
  55. dt->tm_mday = bcd2bin(buf[3]);
  56. dt->tm_mon = bcd2bin(buf[4]) - 1;
  57. dt->tm_wday = bcd2bin(buf[5]) - 1;
  58. dt->tm_year = bcd2bin(buf[6]) + 100;
  59. return 0;
  60. }
  61. static int max6916_set_time(struct device *dev, struct rtc_time *dt)
  62. {
  63. struct spi_device *spi = to_spi_device(dev);
  64. unsigned char buf[9];
  65. if (dt->tm_year < 100 || dt->tm_year > 199) {
  66. dev_err(&spi->dev, "Year must be between 2000 and 2099. It's %d.\n",
  67. dt->tm_year + 1900);
  68. return -EINVAL;
  69. }
  70. buf[0] = MAX6916_CLOCK_BURST & 0x7F;
  71. buf[1] = bin2bcd(dt->tm_sec);
  72. buf[2] = bin2bcd(dt->tm_min);
  73. buf[3] = (bin2bcd(dt->tm_hour) & 0X3F);
  74. buf[4] = bin2bcd(dt->tm_mday);
  75. buf[5] = bin2bcd(dt->tm_mon + 1);
  76. buf[6] = bin2bcd(dt->tm_wday + 1);
  77. buf[7] = bin2bcd(dt->tm_year % 100);
  78. buf[8] = bin2bcd(0x00);
  79. /* write the rtc settings */
  80. return spi_write_then_read(spi, buf, 9, NULL, 0);
  81. }
  82. static const struct rtc_class_ops max6916_rtc_ops = {
  83. .read_time = max6916_read_time,
  84. .set_time = max6916_set_time,
  85. };
  86. static int max6916_probe(struct spi_device *spi)
  87. {
  88. struct rtc_device *rtc;
  89. unsigned char data;
  90. int res;
  91. /* spi setup with max6916 in mode 3 and bits per word as 8 */
  92. spi->mode = SPI_MODE_3;
  93. spi->bits_per_word = 8;
  94. spi_setup(spi);
  95. /* RTC Settings */
  96. res = max6916_read_reg(&spi->dev, MAX6916_SECONDS_REG, &data);
  97. if (res)
  98. return res;
  99. /* Disable the write protect of rtc */
  100. max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data);
  101. data = data & ~(1 << 7);
  102. max6916_write_reg(&spi->dev, MAX6916_CONTROL_REG, data);
  103. /*Enable oscillator,disable oscillator stop flag, glitch filter*/
  104. max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data);
  105. data = data & 0x1B;
  106. max6916_write_reg(&spi->dev, MAX6916_STATUS_REG, data);
  107. /* display the settings */
  108. max6916_read_reg(&spi->dev, MAX6916_CONTROL_REG, &data);
  109. dev_info(&spi->dev, "MAX6916 RTC CTRL Reg = 0x%02x\n", data);
  110. max6916_read_reg(&spi->dev, MAX6916_STATUS_REG, &data);
  111. dev_info(&spi->dev, "MAX6916 RTC Status Reg = 0x%02x\n", data);
  112. rtc = devm_rtc_device_register(&spi->dev, "max6916",
  113. &max6916_rtc_ops, THIS_MODULE);
  114. if (IS_ERR(rtc))
  115. return PTR_ERR(rtc);
  116. spi_set_drvdata(spi, rtc);
  117. return 0;
  118. }
  119. static struct spi_driver max6916_driver = {
  120. .driver = {
  121. .name = "max6916",
  122. },
  123. .probe = max6916_probe,
  124. };
  125. module_spi_driver(max6916_driver);
  126. MODULE_DESCRIPTION("MAX6916 SPI RTC DRIVER");
  127. MODULE_AUTHOR("Venkat Prashanth B U <venkat.prashanth2498@gmail.com>");
  128. MODULE_LICENSE("GPL v2");