rtc-max6916.c 4.1 KB

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