rtc-ds1390.c 6.0 KB

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