rtc-ds1390.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * rtc-ds1390.c -- driver for the Dallas/Maxim DS1390/93/94 SPI RTC
  3. *
  4. * Copyright (C) 2008 Mercury IMC Ltd
  5. * Written by Mark Jackson <mpfj@mimc.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * NOTE: Currently this driver only supports the bare minimum for read
  12. * and write the RTC. The extra features provided by the chip family
  13. * (alarms, trickle charger, different control registers) are unavailable.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/rtc.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/bcd.h>
  21. #include <linux/slab.h>
  22. #define DS1390_REG_100THS 0x00
  23. #define DS1390_REG_SECONDS 0x01
  24. #define DS1390_REG_MINUTES 0x02
  25. #define DS1390_REG_HOURS 0x03
  26. #define DS1390_REG_DAY 0x04
  27. #define DS1390_REG_DATE 0x05
  28. #define DS1390_REG_MONTH_CENT 0x06
  29. #define DS1390_REG_YEAR 0x07
  30. #define DS1390_REG_ALARM_100THS 0x08
  31. #define DS1390_REG_ALARM_SECONDS 0x09
  32. #define DS1390_REG_ALARM_MINUTES 0x0A
  33. #define DS1390_REG_ALARM_HOURS 0x0B
  34. #define DS1390_REG_ALARM_DAY_DATE 0x0C
  35. #define DS1390_REG_CONTROL 0x0D
  36. #define DS1390_REG_STATUS 0x0E
  37. #define DS1390_REG_TRICKLE 0x0F
  38. struct ds1390 {
  39. struct rtc_device *rtc;
  40. u8 txrx_buf[9]; /* cmd + 8 registers */
  41. };
  42. static int ds1390_get_reg(struct device *dev, unsigned char address,
  43. unsigned char *data)
  44. {
  45. struct spi_device *spi = to_spi_device(dev);
  46. struct ds1390 *chip = dev_get_drvdata(dev);
  47. int status;
  48. if (!data)
  49. return -EINVAL;
  50. /* Clear MSB to indicate read */
  51. chip->txrx_buf[0] = address & 0x7f;
  52. /* do the i/o */
  53. status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
  54. if (status != 0)
  55. return status;
  56. *data = chip->txrx_buf[1];
  57. return 0;
  58. }
  59. static int ds1390_read_time(struct device *dev, struct rtc_time *dt)
  60. {
  61. struct spi_device *spi = to_spi_device(dev);
  62. struct ds1390 *chip = dev_get_drvdata(dev);
  63. int status;
  64. /* build the message */
  65. chip->txrx_buf[0] = DS1390_REG_SECONDS;
  66. /* do the i/o */
  67. status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
  68. if (status != 0)
  69. return status;
  70. /* The chip sends data in this order:
  71. * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */
  72. dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
  73. dt->tm_min = bcd2bin(chip->txrx_buf[1]);
  74. dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
  75. dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
  76. dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
  77. /* mask off century bit */
  78. dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
  79. /* adjust for century bit */
  80. dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
  81. return rtc_valid_tm(dt);
  82. }
  83. static int ds1390_set_time(struct device *dev, struct rtc_time *dt)
  84. {
  85. struct spi_device *spi = to_spi_device(dev);
  86. struct ds1390 *chip = dev_get_drvdata(dev);
  87. /* build the message */
  88. chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
  89. chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
  90. chip->txrx_buf[2] = bin2bcd(dt->tm_min);
  91. chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
  92. chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
  93. chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
  94. chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
  95. ((dt->tm_year > 99) ? 0x80 : 0x00);
  96. chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
  97. /* do the i/o */
  98. return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
  99. }
  100. static const struct rtc_class_ops ds1390_rtc_ops = {
  101. .read_time = ds1390_read_time,
  102. .set_time = ds1390_set_time,
  103. };
  104. static int ds1390_probe(struct spi_device *spi)
  105. {
  106. unsigned char tmp;
  107. struct ds1390 *chip;
  108. int res;
  109. spi->mode = SPI_MODE_3;
  110. spi->bits_per_word = 8;
  111. spi_setup(spi);
  112. chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
  113. if (!chip)
  114. return -ENOMEM;
  115. spi_set_drvdata(spi, chip);
  116. res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
  117. if (res != 0) {
  118. dev_err(&spi->dev, "unable to read device\n");
  119. return res;
  120. }
  121. chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390",
  122. &ds1390_rtc_ops, THIS_MODULE);
  123. if (IS_ERR(chip->rtc)) {
  124. dev_err(&spi->dev, "unable to register device\n");
  125. res = PTR_ERR(chip->rtc);
  126. }
  127. return res;
  128. }
  129. static struct spi_driver ds1390_driver = {
  130. .driver = {
  131. .name = "rtc-ds1390",
  132. .owner = THIS_MODULE,
  133. },
  134. .probe = ds1390_probe,
  135. };
  136. module_spi_driver(ds1390_driver);
  137. MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver");
  138. MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
  139. MODULE_LICENSE("GPL");
  140. MODULE_ALIAS("spi:rtc-ds1390");