rtc-ds3234.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* rtc-ds3234.c
  2. *
  3. * Driver for Dallas Semiconductor (DS3234) SPI RTC with Integrated Crystal
  4. * and SRAM.
  5. *
  6. * Copyright (C) 2008 MIMOMax Wireless Ltd.
  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. #define DS3234_REG_SECONDS 0x00
  21. #define DS3234_REG_MINUTES 0x01
  22. #define DS3234_REG_HOURS 0x02
  23. #define DS3234_REG_DAY 0x03
  24. #define DS3234_REG_DATE 0x04
  25. #define DS3234_REG_MONTH 0x05
  26. #define DS3234_REG_YEAR 0x06
  27. #define DS3234_REG_CENTURY (1 << 7) /* Bit 7 of the Month register */
  28. #define DS3234_REG_CONTROL 0x0E
  29. #define DS3234_REG_CONT_STAT 0x0F
  30. static int ds3234_set_reg(struct device *dev, unsigned char address,
  31. unsigned char data)
  32. {
  33. struct spi_device *spi = to_spi_device(dev);
  34. unsigned char buf[2];
  35. /* MSB must be '1' to indicate write */
  36. buf[0] = address | 0x80;
  37. buf[1] = data;
  38. return spi_write_then_read(spi, buf, 2, NULL, 0);
  39. }
  40. static int ds3234_get_reg(struct device *dev, unsigned char address,
  41. unsigned char *data)
  42. {
  43. struct spi_device *spi = to_spi_device(dev);
  44. *data = address & 0x7f;
  45. return spi_write_then_read(spi, data, 1, data, 1);
  46. }
  47. static int ds3234_read_time(struct device *dev, struct rtc_time *dt)
  48. {
  49. int err;
  50. unsigned char buf[8];
  51. struct spi_device *spi = to_spi_device(dev);
  52. buf[0] = 0x00; /* Start address */
  53. err = spi_write_then_read(spi, buf, 1, buf, 8);
  54. if (err != 0)
  55. return err;
  56. /* Seconds, Minutes, Hours, Day, Date, Month, Year */
  57. dt->tm_sec = bcd2bin(buf[0]);
  58. dt->tm_min = bcd2bin(buf[1]);
  59. dt->tm_hour = bcd2bin(buf[2] & 0x3f);
  60. dt->tm_wday = bcd2bin(buf[3]) - 1; /* 0 = Sun */
  61. dt->tm_mday = bcd2bin(buf[4]);
  62. dt->tm_mon = bcd2bin(buf[5] & 0x1f) - 1; /* 0 = Jan */
  63. dt->tm_year = bcd2bin(buf[6] & 0xff) + 100; /* Assume 20YY */
  64. return rtc_valid_tm(dt);
  65. }
  66. static int ds3234_set_time(struct device *dev, struct rtc_time *dt)
  67. {
  68. ds3234_set_reg(dev, DS3234_REG_SECONDS, bin2bcd(dt->tm_sec));
  69. ds3234_set_reg(dev, DS3234_REG_MINUTES, bin2bcd(dt->tm_min));
  70. ds3234_set_reg(dev, DS3234_REG_HOURS, bin2bcd(dt->tm_hour) & 0x3f);
  71. /* 0 = Sun */
  72. ds3234_set_reg(dev, DS3234_REG_DAY, bin2bcd(dt->tm_wday + 1));
  73. ds3234_set_reg(dev, DS3234_REG_DATE, bin2bcd(dt->tm_mday));
  74. /* 0 = Jan */
  75. ds3234_set_reg(dev, DS3234_REG_MONTH, bin2bcd(dt->tm_mon + 1));
  76. /* Assume 20YY although we just want to make sure not to go negative. */
  77. if (dt->tm_year > 100)
  78. dt->tm_year -= 100;
  79. ds3234_set_reg(dev, DS3234_REG_YEAR, bin2bcd(dt->tm_year));
  80. return 0;
  81. }
  82. static const struct rtc_class_ops ds3234_rtc_ops = {
  83. .read_time = ds3234_read_time,
  84. .set_time = ds3234_set_time,
  85. };
  86. static int ds3234_probe(struct spi_device *spi)
  87. {
  88. struct rtc_device *rtc;
  89. unsigned char tmp;
  90. int res;
  91. spi->mode = SPI_MODE_3;
  92. spi->bits_per_word = 8;
  93. spi_setup(spi);
  94. res = ds3234_get_reg(&spi->dev, DS3234_REG_SECONDS, &tmp);
  95. if (res != 0)
  96. return res;
  97. /* Control settings
  98. *
  99. * CONTROL_REG
  100. * BIT 7 6 5 4 3 2 1 0
  101. * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
  102. *
  103. * 0 0 0 1 1 1 0 0
  104. *
  105. * CONTROL_STAT_REG
  106. * BIT 7 6 5 4 3 2 1 0
  107. * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
  108. *
  109. * 1 0 0 0 1 0 0 0
  110. */
  111. ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
  112. ds3234_set_reg(&spi->dev, DS3234_REG_CONTROL, tmp & 0x1c);
  113. ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
  114. ds3234_set_reg(&spi->dev, DS3234_REG_CONT_STAT, tmp & 0x88);
  115. /* Print our settings */
  116. ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
  117. dev_info(&spi->dev, "Control Reg: 0x%02x\n", tmp);
  118. ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
  119. dev_info(&spi->dev, "Ctrl/Stat Reg: 0x%02x\n", tmp);
  120. rtc = devm_rtc_device_register(&spi->dev, "ds3234",
  121. &ds3234_rtc_ops, THIS_MODULE);
  122. if (IS_ERR(rtc))
  123. return PTR_ERR(rtc);
  124. spi_set_drvdata(spi, rtc);
  125. return 0;
  126. }
  127. static struct spi_driver ds3234_driver = {
  128. .driver = {
  129. .name = "ds3234",
  130. },
  131. .probe = ds3234_probe,
  132. };
  133. module_spi_driver(ds3234_driver);
  134. MODULE_DESCRIPTION("DS3234 SPI RTC driver");
  135. MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
  136. MODULE_LICENSE("GPL");
  137. MODULE_ALIAS("spi:ds3234");