rtc-ds1390.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * rtc-ds1390.c -- driver for DS1390/93/94
  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/platform_device.h>
  16. #include <linux/rtc.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/bcd.h>
  19. #define DS1390_REG_100THS 0x00
  20. #define DS1390_REG_SECONDS 0x01
  21. #define DS1390_REG_MINUTES 0x02
  22. #define DS1390_REG_HOURS 0x03
  23. #define DS1390_REG_DAY 0x04
  24. #define DS1390_REG_DATE 0x05
  25. #define DS1390_REG_MONTH_CENT 0x06
  26. #define DS1390_REG_YEAR 0x07
  27. #define DS1390_REG_ALARM_100THS 0x08
  28. #define DS1390_REG_ALARM_SECONDS 0x09
  29. #define DS1390_REG_ALARM_MINUTES 0x0A
  30. #define DS1390_REG_ALARM_HOURS 0x0B
  31. #define DS1390_REG_ALARM_DAY_DATE 0x0C
  32. #define DS1390_REG_CONTROL 0x0D
  33. #define DS1390_REG_STATUS 0x0E
  34. #define DS1390_REG_TRICKLE 0x0F
  35. struct ds1390 {
  36. struct rtc_device *rtc;
  37. u8 txrx_buf[9]; /* cmd + 8 registers */
  38. };
  39. static void ds1390_set_reg(struct device *dev, unsigned char address,
  40. unsigned char data)
  41. {
  42. struct spi_device *spi = to_spi_device(dev);
  43. struct ds1390 *chip = dev_get_drvdata(dev);
  44. /* Set MSB to indicate write */
  45. chip->txrx_buf[0] = address | 0x80;
  46. chip->txrx_buf[1] = data;
  47. /* do the i/o */
  48. spi_write_then_read(spi, chip->txrx_buf, 2, NULL, 0);
  49. }
  50. static int ds1390_get_reg(struct device *dev, unsigned char address,
  51. unsigned char *data)
  52. {
  53. struct spi_device *spi = to_spi_device(dev);
  54. struct ds1390 *chip = dev_get_drvdata(dev);
  55. int status;
  56. if (!data)
  57. return -EINVAL;
  58. /* Clear MSB to indicate read */
  59. chip->txrx_buf[0] = address & 0x7f;
  60. /* do the i/o */
  61. status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 1);
  62. if (status != 0)
  63. return status;
  64. *data = chip->txrx_buf[1];
  65. return 0;
  66. }
  67. static int ds1390_get_datetime(struct device *dev, struct rtc_time *dt)
  68. {
  69. struct spi_device *spi = to_spi_device(dev);
  70. struct ds1390 *chip = dev_get_drvdata(dev);
  71. int status;
  72. /* build the message */
  73. chip->txrx_buf[0] = DS1390_REG_SECONDS;
  74. /* do the i/o */
  75. status = spi_write_then_read(spi, chip->txrx_buf, 1, chip->txrx_buf, 8);
  76. if (status != 0)
  77. return status;
  78. /* The chip sends data in this order:
  79. * Seconds, Minutes, Hours, Day, Date, Month / Century, Year */
  80. dt->tm_sec = bcd2bin(chip->txrx_buf[0]);
  81. dt->tm_min = bcd2bin(chip->txrx_buf[1]);
  82. dt->tm_hour = bcd2bin(chip->txrx_buf[2]);
  83. dt->tm_wday = bcd2bin(chip->txrx_buf[3]);
  84. dt->tm_mday = bcd2bin(chip->txrx_buf[4]);
  85. /* mask off century bit */
  86. dt->tm_mon = bcd2bin(chip->txrx_buf[5] & 0x7f) - 1;
  87. /* adjust for century bit */
  88. dt->tm_year = bcd2bin(chip->txrx_buf[6]) + ((chip->txrx_buf[5] & 0x80) ? 100 : 0);
  89. return rtc_valid_tm(dt);
  90. }
  91. static int ds1390_set_datetime(struct device *dev, struct rtc_time *dt)
  92. {
  93. struct spi_device *spi = to_spi_device(dev);
  94. struct ds1390 *chip = dev_get_drvdata(dev);
  95. /* build the message */
  96. chip->txrx_buf[0] = DS1390_REG_SECONDS | 0x80;
  97. chip->txrx_buf[1] = bin2bcd(dt->tm_sec);
  98. chip->txrx_buf[2] = bin2bcd(dt->tm_min);
  99. chip->txrx_buf[3] = bin2bcd(dt->tm_hour);
  100. chip->txrx_buf[4] = bin2bcd(dt->tm_wday);
  101. chip->txrx_buf[5] = bin2bcd(dt->tm_mday);
  102. chip->txrx_buf[6] = bin2bcd(dt->tm_mon + 1) |
  103. ((dt->tm_year > 99) ? 0x80 : 0x00);
  104. chip->txrx_buf[7] = bin2bcd(dt->tm_year % 100);
  105. /* do the i/o */
  106. return spi_write_then_read(spi, chip->txrx_buf, 8, NULL, 0);
  107. }
  108. static int ds1390_read_time(struct device *dev, struct rtc_time *tm)
  109. {
  110. return ds1390_get_datetime(dev, tm);
  111. }
  112. static int ds1390_set_time(struct device *dev, struct rtc_time *tm)
  113. {
  114. return ds1390_set_datetime(dev, tm);
  115. }
  116. static const struct rtc_class_ops ds1390_rtc_ops = {
  117. .read_time = ds1390_read_time,
  118. .set_time = ds1390_set_time,
  119. };
  120. static int __devinit ds1390_probe(struct spi_device *spi)
  121. {
  122. struct rtc_device *rtc;
  123. unsigned char tmp;
  124. struct ds1390 *chip;
  125. int res;
  126. printk(KERN_DEBUG "DS1390 SPI RTC driver\n");
  127. rtc = rtc_device_register("ds1390",
  128. &spi->dev, &ds1390_rtc_ops, THIS_MODULE);
  129. if (IS_ERR(rtc)) {
  130. printk(KERN_ALERT "RTC : unable to register device\n");
  131. return PTR_ERR(rtc);
  132. }
  133. spi->mode = SPI_MODE_3;
  134. spi->bits_per_word = 8;
  135. spi_setup(spi);
  136. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  137. if (!chip) {
  138. printk(KERN_ALERT "RTC : unable to allocate device memory\n");
  139. rtc_device_unregister(rtc);
  140. return -ENOMEM;
  141. }
  142. chip->rtc = rtc;
  143. dev_set_drvdata(&spi->dev, chip);
  144. res = ds1390_get_reg(&spi->dev, DS1390_REG_SECONDS, &tmp);
  145. if (res) {
  146. printk(KERN_ALERT "RTC : unable to read device\n");
  147. rtc_device_unregister(rtc);
  148. return res;
  149. }
  150. return 0;
  151. }
  152. static int __devexit ds1390_remove(struct spi_device *spi)
  153. {
  154. struct ds1390 *chip = platform_get_drvdata(spi);
  155. struct rtc_device *rtc = chip->rtc;
  156. if (rtc)
  157. rtc_device_unregister(rtc);
  158. kfree(chip);
  159. return 0;
  160. }
  161. static struct spi_driver ds1390_driver = {
  162. .driver = {
  163. .name = "rtc-ds1390",
  164. .owner = THIS_MODULE,
  165. },
  166. .probe = ds1390_probe,
  167. .remove = __devexit_p(ds1390_remove),
  168. };
  169. static __init int ds1390_init(void)
  170. {
  171. return spi_register_driver(&ds1390_driver);
  172. }
  173. module_init(ds1390_init);
  174. static __exit void ds1390_exit(void)
  175. {
  176. spi_unregister_driver(&ds1390_driver);
  177. }
  178. module_exit(ds1390_exit);
  179. MODULE_DESCRIPTION("DS1390/93/94 SPI RTC driver");
  180. MODULE_AUTHOR("Mark Jackson <mpfj@mimc.co.uk>");
  181. MODULE_LICENSE("GPL");