rtc-ds1390.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #include <linux/of.h>
  23. #define DS1390_REG_100THS 0x00
  24. #define DS1390_REG_SECONDS 0x01
  25. #define DS1390_REG_MINUTES 0x02
  26. #define DS1390_REG_HOURS 0x03
  27. #define DS1390_REG_DAY 0x04
  28. #define DS1390_REG_DATE 0x05
  29. #define DS1390_REG_MONTH_CENT 0x06
  30. #define DS1390_REG_YEAR 0x07
  31. #define DS1390_REG_ALARM_100THS 0x08
  32. #define DS1390_REG_ALARM_SECONDS 0x09
  33. #define DS1390_REG_ALARM_MINUTES 0x0A
  34. #define DS1390_REG_ALARM_HOURS 0x0B
  35. #define DS1390_REG_ALARM_DAY_DATE 0x0C
  36. #define DS1390_REG_CONTROL 0x0D
  37. #define DS1390_REG_STATUS 0x0E
  38. #define DS1390_REG_TRICKLE 0x0F
  39. #define DS1390_TRICKLE_CHARGER_ENABLE 0xA0
  40. #define DS1390_TRICKLE_CHARGER_250_OHM 0x01
  41. #define DS1390_TRICKLE_CHARGER_2K_OHM 0x02
  42. #define DS1390_TRICKLE_CHARGER_4K_OHM 0x03
  43. #define DS1390_TRICKLE_CHARGER_NO_DIODE 0x04
  44. #define DS1390_TRICKLE_CHARGER_DIODE 0x08
  45. struct ds1390 {
  46. struct rtc_device *rtc;
  47. u8 txrx_buf[9]; /* cmd + 8 registers */
  48. };
  49. static void ds1390_set_reg(struct device *dev, unsigned char address,
  50. unsigned char data)
  51. {
  52. struct spi_device *spi = to_spi_device(dev);
  53. unsigned char buf[2];
  54. /* MSB must be '1' to write */
  55. buf[0] = address | 0x80;
  56. buf[1] = data;
  57. spi_write(spi, buf, 2);
  58. }
  59. static int ds1390_get_reg(struct device *dev, unsigned char address,
  60. unsigned char *data)
  61. {
  62. struct spi_device *spi = to_spi_device(dev);
  63. struct ds1390 *chip = dev_get_drvdata(dev);
  64. int status;
  65. if (!data)
  66. return -EINVAL;
  67. /* Clear MSB to indicate read */
  68. chip->txrx_buf[0] = address & 0x7f;
  69. /* do the i/o */
  70. status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
  71. if (status != 0)
  72. return status;
  73. *data = chip->txrx_buf[0];
  74. return 0;
  75. }
  76. static void ds1390_trickle_of_init(struct spi_device *spi)
  77. {
  78. u32 ohms = 0;
  79. u8 value;
  80. if (of_property_read_u32(spi->dev.of_node, "trickle-resistor-ohms",
  81. &ohms))
  82. goto out;
  83. /* Enable charger */
  84. value = DS1390_TRICKLE_CHARGER_ENABLE;
  85. if (of_property_read_bool(spi->dev.of_node, "trickle-diode-disable"))
  86. value |= DS1390_TRICKLE_CHARGER_NO_DIODE;
  87. else
  88. value |= DS1390_TRICKLE_CHARGER_DIODE;
  89. /* Resistor select */
  90. switch (ohms) {
  91. case 250:
  92. value |= DS1390_TRICKLE_CHARGER_250_OHM;
  93. break;
  94. case 2000:
  95. value |= DS1390_TRICKLE_CHARGER_2K_OHM;
  96. break;
  97. case 4000:
  98. value |= DS1390_TRICKLE_CHARGER_4K_OHM;
  99. break;
  100. default:
  101. dev_warn(&spi->dev,
  102. "Unsupported ohm value %02ux in dt\n", ohms);
  103. return;
  104. }
  105. ds1390_set_reg(&spi->dev, DS1390_REG_TRICKLE, value);
  106. out:
  107. return;
  108. }
  109. static int ds1390_read_time(struct device *dev, struct rtc_time *dt)
  110. {
  111. struct spi_device *spi = to_spi_device(dev);
  112. struct ds1390 *chip = dev_get_drvdata(dev);
  113. int status;
  114. /* build the message */
  115. chip->txrx_buf[0] = DS1390_REG_SECONDS;
  116. /* do the i/o */
  117. status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
  118. if (status != 0)
  119. return status;
  120. /* The chip sends data in this order:
  121. * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */
  122. dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
  123. dt->tm_min = bcd2bin(chip->txrx_buf[1]);
  124. dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
  125. dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
  126. dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
  127. /* mask off century bit */
  128. dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
  129. /* adjust for century bit */
  130. dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
  131. return rtc_valid_tm(dt);
  132. }
  133. static int ds1390_set_time(struct device *dev, struct rtc_time *dt)
  134. {
  135. struct spi_device *spi = to_spi_device(dev);
  136. struct ds1390 *chip = dev_get_drvdata(dev);
  137. /* build the message */
  138. chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
  139. chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
  140. chip->txrx_buf[2] = bin2bcd(dt->tm_min);
  141. chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
  142. chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
  143. chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
  144. chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
  145. ((dt->tm_year > 99) ? 0x80 : 0x00);
  146. chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
  147. /* do the i/o */
  148. return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
  149. }
  150. static const struct rtc_class_ops ds1390_rtc_ops = {
  151. .read_time = ds1390_read_time,
  152. .set_time = ds1390_set_time,
  153. };
  154. static int ds1390_probe(struct spi_device *spi)
  155. {
  156. unsigned char tmp;
  157. struct ds1390 *chip;
  158. int res;
  159. spi->mode = SPI_MODE_3;
  160. spi->bits_per_word = 8;
  161. spi_setup(spi);
  162. chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
  163. if (!chip)
  164. return -ENOMEM;
  165. spi_set_drvdata(spi, chip);
  166. res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
  167. if (res != 0) {
  168. dev_err(&spi->dev, "unable to read device\n");
  169. return res;
  170. }
  171. if (spi->dev.of_node)
  172. ds1390_trickle_of_init(spi);
  173. chip->rtc = devm_rtc_device_register(&spi->dev, "ds1390",
  174. &ds1390_rtc_ops, THIS_MODULE);
  175. if (IS_ERR(chip->rtc)) {
  176. dev_err(&spi->dev, "unable to register device\n");
  177. res = PTR_ERR(chip->rtc);
  178. }
  179. return res;
  180. }
  181. static struct spi_driver ds1390_driver = {
  182. .driver = {
  183. .name = "rtc-ds1390",
  184. },
  185. .probe = ds1390_probe,
  186. };
  187. module_spi_driver(ds1390_driver);
  188. MODULE_DESCRIPTION("Dallas/Maxim DS1390/93/94 SPI RTC driver");
  189. MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
  190. MODULE_LICENSE("GPL");
  191. MODULE_ALIAS("spi:rtc-ds1390");