rtc-ds3234.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* drivers/rtc/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. * Changelog:
  13. *
  14. * 07-May-2008: Dennis Aberilla <denzzzhome@yahoo.com>
  15. * - Created based on the max6902 code. Only implements the
  16. * date/time keeping functions; no SRAM yet.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/rtc.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/bcd.h>
  23. #define DS3234_REG_SECONDS 0x00
  24. #define DS3234_REG_MINUTES 0x01
  25. #define DS3234_REG_HOURS 0x02
  26. #define DS3234_REG_DAY 0x03
  27. #define DS3234_REG_DATE 0x04
  28. #define DS3234_REG_MONTH 0x05
  29. #define DS3234_REG_YEAR 0x06
  30. #define DS3234_REG_CENTURY (1 << 7) /* Bit 7 of the Month register */
  31. #define DS3234_REG_CONTROL 0x0E
  32. #define DS3234_REG_CONT_STAT 0x0F
  33. #undef DS3234_DEBUG
  34. struct ds3234 {
  35. struct rtc_device *rtc;
  36. u8 buf[8]; /* Burst read: addr + 7 regs */
  37. u8 tx_buf[2];
  38. u8 rx_buf[2];
  39. };
  40. static void ds3234_set_reg(struct device *dev, unsigned char address,
  41. unsigned char data)
  42. {
  43. struct spi_device *spi = to_spi_device(dev);
  44. unsigned char buf[2];
  45. /* MSB must be '1' to indicate write */
  46. buf[0] = address | 0x80;
  47. buf[1] = data;
  48. spi_write(spi, buf, 2);
  49. }
  50. static int ds3234_get_reg(struct device *dev, unsigned char address,
  51. unsigned char *data)
  52. {
  53. struct spi_device *spi = to_spi_device(dev);
  54. struct ds3234 *chip = dev_get_drvdata(dev);
  55. struct spi_message message;
  56. struct spi_transfer xfer;
  57. int status;
  58. if (!data)
  59. return -EINVAL;
  60. /* Build our spi message */
  61. spi_message_init(&message);
  62. memset(&xfer, 0, sizeof(xfer));
  63. /* Address + dummy tx byte */
  64. xfer.len = 2;
  65. xfer.tx_buf = chip->tx_buf;
  66. xfer.rx_buf = chip->rx_buf;
  67. chip->tx_buf[0] = address;
  68. chip->tx_buf[1] = 0xff;
  69. spi_message_add_tail(&xfer, &message);
  70. /* do the i/o */
  71. status = spi_sync(spi, &message);
  72. if (status == 0)
  73. status = message.status;
  74. else
  75. return status;
  76. *data = chip->rx_buf[1];
  77. return status;
  78. }
  79. static int ds3234_get_datetime(struct device *dev, struct rtc_time *dt)
  80. {
  81. struct spi_device *spi = to_spi_device(dev);
  82. struct ds3234 *chip = dev_get_drvdata(dev);
  83. struct spi_message message;
  84. struct spi_transfer xfer;
  85. int status;
  86. /* build the message */
  87. spi_message_init(&message);
  88. memset(&xfer, 0, sizeof(xfer));
  89. xfer.len = 1 + 7; /* Addr + 7 registers */
  90. xfer.tx_buf = chip->buf;
  91. xfer.rx_buf = chip->buf;
  92. chip->buf[0] = 0x00; /* Start address */
  93. spi_message_add_tail(&xfer, &message);
  94. /* do the i/o */
  95. status = spi_sync(spi, &message);
  96. if (status == 0)
  97. status = message.status;
  98. else
  99. return status;
  100. /* Seconds, Minutes, Hours, Day, Date, Month, Year */
  101. dt->tm_sec = bcd2bin(chip->buf[1]);
  102. dt->tm_min = bcd2bin(chip->buf[2]);
  103. dt->tm_hour = bcd2bin(chip->buf[3] & 0x3f);
  104. dt->tm_wday = bcd2bin(chip->buf[4]) - 1; /* 0 = Sun */
  105. dt->tm_mday = bcd2bin(chip->buf[5]);
  106. dt->tm_mon = bcd2bin(chip->buf[6] & 0x1f) - 1; /* 0 = Jan */
  107. dt->tm_year = bcd2bin(chip->buf[7] & 0xff) + 100; /* Assume 20YY */
  108. #ifdef DS3234_DEBUG
  109. dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
  110. dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
  111. dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
  112. dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
  113. dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
  114. dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
  115. dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
  116. dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
  117. #endif
  118. return 0;
  119. }
  120. static int ds3234_set_datetime(struct device *dev, struct rtc_time *dt)
  121. {
  122. #ifdef DS3234_DEBUG
  123. dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
  124. dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
  125. dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
  126. dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
  127. dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
  128. dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
  129. dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
  130. dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
  131. #endif
  132. ds3234_set_reg(dev, DS3234_REG_SECONDS, bin2bcd(dt->tm_sec));
  133. ds3234_set_reg(dev, DS3234_REG_MINUTES, bin2bcd(dt->tm_min));
  134. ds3234_set_reg(dev, DS3234_REG_HOURS, bin2bcd(dt->tm_hour) & 0x3f);
  135. /* 0 = Sun */
  136. ds3234_set_reg(dev, DS3234_REG_DAY, bin2bcd(dt->tm_wday + 1));
  137. ds3234_set_reg(dev, DS3234_REG_DATE, bin2bcd(dt->tm_mday));
  138. /* 0 = Jan */
  139. ds3234_set_reg(dev, DS3234_REG_MONTH, bin2bcd(dt->tm_mon + 1));
  140. /* Assume 20YY although we just want to make sure not to go negative. */
  141. if (dt->tm_year > 100)
  142. dt->tm_year -= 100;
  143. ds3234_set_reg(dev, DS3234_REG_YEAR, bin2bcd(dt->tm_year));
  144. return 0;
  145. }
  146. static int ds3234_read_time(struct device *dev, struct rtc_time *tm)
  147. {
  148. return ds3234_get_datetime(dev, tm);
  149. }
  150. static int ds3234_set_time(struct device *dev, struct rtc_time *tm)
  151. {
  152. return ds3234_set_datetime(dev, tm);
  153. }
  154. static const struct rtc_class_ops ds3234_rtc_ops = {
  155. .read_time = ds3234_read_time,
  156. .set_time = ds3234_set_time,
  157. };
  158. static int __devinit ds3234_probe(struct spi_device *spi)
  159. {
  160. struct rtc_device *rtc;
  161. unsigned char tmp;
  162. struct ds3234 *chip;
  163. int res;
  164. rtc = rtc_device_register("ds3234",
  165. &spi->dev, &ds3234_rtc_ops, THIS_MODULE);
  166. if (IS_ERR(rtc))
  167. return PTR_ERR(rtc);
  168. spi->mode = SPI_MODE_3;
  169. spi->bits_per_word = 8;
  170. spi_setup(spi);
  171. chip = kzalloc(sizeof(struct ds3234), GFP_KERNEL);
  172. if (!chip) {
  173. rtc_device_unregister(rtc);
  174. return -ENOMEM;
  175. }
  176. chip->rtc = rtc;
  177. dev_set_drvdata(&spi->dev, chip);
  178. res = ds3234_get_reg(&spi->dev, DS3234_REG_SECONDS, &tmp);
  179. if (res) {
  180. rtc_device_unregister(rtc);
  181. return res;
  182. }
  183. /* Control settings
  184. *
  185. * CONTROL_REG
  186. * BIT 7 6 5 4 3 2 1 0
  187. * EOSC BBSQW CONV RS2 RS1 INTCN A2IE A1IE
  188. *
  189. * 0 0 0 1 1 1 0 0
  190. *
  191. * CONTROL_STAT_REG
  192. * BIT 7 6 5 4 3 2 1 0
  193. * OSF BB32kHz CRATE1 CRATE0 EN32kHz BSY A2F A1F
  194. *
  195. * 1 0 0 0 1 0 0 0
  196. */
  197. ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
  198. ds3234_set_reg(&spi->dev, DS3234_REG_CONTROL, tmp & 0x1c);
  199. ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
  200. ds3234_set_reg(&spi->dev, DS3234_REG_CONT_STAT, tmp & 0x88);
  201. /* Print our settings */
  202. ds3234_get_reg(&spi->dev, DS3234_REG_CONTROL, &tmp);
  203. dev_info(&spi->dev, "Control Reg: 0x%02x\n", tmp);
  204. ds3234_get_reg(&spi->dev, DS3234_REG_CONT_STAT, &tmp);
  205. dev_info(&spi->dev, "Ctrl/Stat Reg: 0x%02x\n", tmp);
  206. return 0;
  207. }
  208. static int __devexit ds3234_remove(struct spi_device *spi)
  209. {
  210. struct ds3234 *chip = platform_get_drvdata(spi);
  211. struct rtc_device *rtc = chip->rtc;
  212. if (rtc)
  213. rtc_device_unregister(rtc);
  214. kfree(chip);
  215. return 0;
  216. }
  217. static struct spi_driver ds3234_driver = {
  218. .driver = {
  219. .name = "ds3234",
  220. .bus = &spi_bus_type,
  221. .owner = THIS_MODULE,
  222. },
  223. .probe = ds3234_probe,
  224. .remove = __devexit_p(ds3234_remove),
  225. };
  226. static __init int ds3234_init(void)
  227. {
  228. printk(KERN_INFO "DS3234 SPI RTC Driver\n");
  229. return spi_register_driver(&ds3234_driver);
  230. }
  231. module_init(ds3234_init);
  232. static __exit void ds3234_exit(void)
  233. {
  234. spi_unregister_driver(&ds3234_driver);
  235. }
  236. module_exit(ds3234_exit);
  237. MODULE_DESCRIPTION("DS3234 SPI RTC driver");
  238. MODULE_AUTHOR("Dennis Aberilla <denzzzhome@yahoo.com>");
  239. MODULE_LICENSE("GPL");