rtc-rs5c348.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * A SPI driver for the Ricoh RS5C348 RTC
  3. *
  4. * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * The board specific init code should provide characteristics of this
  11. * device:
  12. * Mode 1 (High-Active, Shift-Then-Sample), High Avtive CS
  13. */
  14. #include <linux/bcd.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include <linux/slab.h>
  22. #include <linux/rtc.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/spi/spi.h>
  25. #define DRV_VERSION "0.2"
  26. #define RS5C348_REG_SECS 0
  27. #define RS5C348_REG_MINS 1
  28. #define RS5C348_REG_HOURS 2
  29. #define RS5C348_REG_WDAY 3
  30. #define RS5C348_REG_DAY 4
  31. #define RS5C348_REG_MONTH 5
  32. #define RS5C348_REG_YEAR 6
  33. #define RS5C348_REG_CTL1 14
  34. #define RS5C348_REG_CTL2 15
  35. #define RS5C348_SECS_MASK 0x7f
  36. #define RS5C348_MINS_MASK 0x7f
  37. #define RS5C348_HOURS_MASK 0x3f
  38. #define RS5C348_WDAY_MASK 0x03
  39. #define RS5C348_DAY_MASK 0x3f
  40. #define RS5C348_MONTH_MASK 0x1f
  41. #define RS5C348_BIT_PM 0x20 /* REG_HOURS */
  42. #define RS5C348_BIT_Y2K 0x80 /* REG_MONTH */
  43. #define RS5C348_BIT_24H 0x20 /* REG_CTL1 */
  44. #define RS5C348_BIT_XSTP 0x10 /* REG_CTL2 */
  45. #define RS5C348_BIT_VDET 0x40 /* REG_CTL2 */
  46. #define RS5C348_CMD_W(addr) (((addr) << 4) | 0x08) /* single write */
  47. #define RS5C348_CMD_R(addr) (((addr) << 4) | 0x0c) /* single read */
  48. #define RS5C348_CMD_MW(addr) (((addr) << 4) | 0x00) /* burst write */
  49. #define RS5C348_CMD_MR(addr) (((addr) << 4) | 0x04) /* burst read */
  50. struct rs5c348_plat_data {
  51. struct rtc_device *rtc;
  52. int rtc_24h;
  53. };
  54. static int
  55. rs5c348_rtc_set_time(struct device *dev, struct rtc_time *tm)
  56. {
  57. struct spi_device *spi = to_spi_device(dev);
  58. struct rs5c348_plat_data *pdata = spi->dev.platform_data;
  59. u8 txbuf[5+7], *txp;
  60. int ret;
  61. /* Transfer 5 bytes before writing SEC. This gives 31us for carry. */
  62. txp = txbuf;
  63. txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  64. txbuf[1] = 0; /* dummy */
  65. txbuf[2] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  66. txbuf[3] = 0; /* dummy */
  67. txbuf[4] = RS5C348_CMD_MW(RS5C348_REG_SECS); /* cmd, sec, ... */
  68. txp = &txbuf[5];
  69. txp[RS5C348_REG_SECS] = bin2bcd(tm->tm_sec);
  70. txp[RS5C348_REG_MINS] = bin2bcd(tm->tm_min);
  71. if (pdata->rtc_24h) {
  72. txp[RS5C348_REG_HOURS] = bin2bcd(tm->tm_hour);
  73. } else {
  74. /* hour 0 is AM12, noon is PM12 */
  75. txp[RS5C348_REG_HOURS] = bin2bcd((tm->tm_hour + 11) % 12 + 1) |
  76. (tm->tm_hour >= 12 ? RS5C348_BIT_PM : 0);
  77. }
  78. txp[RS5C348_REG_WDAY] = bin2bcd(tm->tm_wday);
  79. txp[RS5C348_REG_DAY] = bin2bcd(tm->tm_mday);
  80. txp[RS5C348_REG_MONTH] = bin2bcd(tm->tm_mon + 1) |
  81. (tm->tm_year >= 100 ? RS5C348_BIT_Y2K : 0);
  82. txp[RS5C348_REG_YEAR] = bin2bcd(tm->tm_year % 100);
  83. /* write in one transfer to avoid data inconsistency */
  84. ret = spi_write_then_read(spi, txbuf, sizeof(txbuf), NULL, 0);
  85. udelay(62); /* Tcsr 62us */
  86. return ret;
  87. }
  88. static int
  89. rs5c348_rtc_read_time(struct device *dev, struct rtc_time *tm)
  90. {
  91. struct spi_device *spi = to_spi_device(dev);
  92. struct rs5c348_plat_data *pdata = spi->dev.platform_data;
  93. u8 txbuf[5], rxbuf[7];
  94. int ret;
  95. /* Transfer 5 byte befores reading SEC. This gives 31us for carry. */
  96. txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  97. txbuf[1] = 0; /* dummy */
  98. txbuf[2] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  99. txbuf[3] = 0; /* dummy */
  100. txbuf[4] = RS5C348_CMD_MR(RS5C348_REG_SECS); /* cmd, sec, ... */
  101. /* read in one transfer to avoid data inconsistency */
  102. ret = spi_write_then_read(spi, txbuf, sizeof(txbuf),
  103. rxbuf, sizeof(rxbuf));
  104. udelay(62); /* Tcsr 62us */
  105. if (ret < 0)
  106. return ret;
  107. tm->tm_sec = bcd2bin(rxbuf[RS5C348_REG_SECS] & RS5C348_SECS_MASK);
  108. tm->tm_min = bcd2bin(rxbuf[RS5C348_REG_MINS] & RS5C348_MINS_MASK);
  109. tm->tm_hour = bcd2bin(rxbuf[RS5C348_REG_HOURS] & RS5C348_HOURS_MASK);
  110. if (!pdata->rtc_24h) {
  111. tm->tm_hour %= 12;
  112. if (rxbuf[RS5C348_REG_HOURS] & RS5C348_BIT_PM)
  113. tm->tm_hour += 12;
  114. }
  115. tm->tm_wday = bcd2bin(rxbuf[RS5C348_REG_WDAY] & RS5C348_WDAY_MASK);
  116. tm->tm_mday = bcd2bin(rxbuf[RS5C348_REG_DAY] & RS5C348_DAY_MASK);
  117. tm->tm_mon =
  118. bcd2bin(rxbuf[RS5C348_REG_MONTH] & RS5C348_MONTH_MASK) - 1;
  119. /* year is 1900 + tm->tm_year */
  120. tm->tm_year = bcd2bin(rxbuf[RS5C348_REG_YEAR]) +
  121. ((rxbuf[RS5C348_REG_MONTH] & RS5C348_BIT_Y2K) ? 100 : 0);
  122. if (rtc_valid_tm(tm) < 0) {
  123. dev_err(&spi->dev, "retrieved date/time is not valid.\n");
  124. rtc_time_to_tm(0, tm);
  125. }
  126. return 0;
  127. }
  128. static const struct rtc_class_ops rs5c348_rtc_ops = {
  129. .read_time = rs5c348_rtc_read_time,
  130. .set_time = rs5c348_rtc_set_time,
  131. };
  132. static struct spi_driver rs5c348_driver;
  133. static int __devinit rs5c348_probe(struct spi_device *spi)
  134. {
  135. int ret;
  136. struct rtc_device *rtc;
  137. struct rs5c348_plat_data *pdata;
  138. pdata = kzalloc(sizeof(struct rs5c348_plat_data), GFP_KERNEL);
  139. if (!pdata)
  140. return -ENOMEM;
  141. spi->dev.platform_data = pdata;
  142. /* Check D7 of SECOND register */
  143. ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_SECS));
  144. if (ret < 0 || (ret & 0x80)) {
  145. dev_err(&spi->dev, "not found.\n");
  146. goto kfree_exit;
  147. }
  148. dev_info(&spi->dev, "chip found, driver version " DRV_VERSION "\n");
  149. dev_info(&spi->dev, "spiclk %u KHz.\n",
  150. (spi->max_speed_hz + 500) / 1000);
  151. /* turn RTC on if it was not on */
  152. ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2));
  153. if (ret < 0)
  154. goto kfree_exit;
  155. if (ret & (RS5C348_BIT_XSTP | RS5C348_BIT_VDET)) {
  156. u8 buf[2];
  157. struct rtc_time tm;
  158. if (ret & RS5C348_BIT_VDET)
  159. dev_warn(&spi->dev, "voltage-low detected.\n");
  160. if (ret & RS5C348_BIT_XSTP)
  161. dev_warn(&spi->dev, "oscillator-stop detected.\n");
  162. rtc_time_to_tm(0, &tm); /* 1970/1/1 */
  163. ret = rs5c348_rtc_set_time(&spi->dev, &tm);
  164. if (ret < 0)
  165. goto kfree_exit;
  166. buf[0] = RS5C348_CMD_W(RS5C348_REG_CTL2);
  167. buf[1] = 0;
  168. ret = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0);
  169. if (ret < 0)
  170. goto kfree_exit;
  171. }
  172. ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL1));
  173. if (ret < 0)
  174. goto kfree_exit;
  175. if (ret & RS5C348_BIT_24H)
  176. pdata->rtc_24h = 1;
  177. rtc = rtc_device_register(rs5c348_driver.driver.name, &spi->dev,
  178. &rs5c348_rtc_ops, THIS_MODULE);
  179. if (IS_ERR(rtc)) {
  180. ret = PTR_ERR(rtc);
  181. goto kfree_exit;
  182. }
  183. pdata->rtc = rtc;
  184. return 0;
  185. kfree_exit:
  186. kfree(pdata);
  187. return ret;
  188. }
  189. static int __devexit rs5c348_remove(struct spi_device *spi)
  190. {
  191. struct rs5c348_plat_data *pdata = spi->dev.platform_data;
  192. struct rtc_device *rtc = pdata->rtc;
  193. if (rtc)
  194. rtc_device_unregister(rtc);
  195. kfree(pdata);
  196. return 0;
  197. }
  198. static struct spi_driver rs5c348_driver = {
  199. .driver = {
  200. .name = "rtc-rs5c348",
  201. .bus = &spi_bus_type,
  202. .owner = THIS_MODULE,
  203. },
  204. .probe = rs5c348_probe,
  205. .remove = __devexit_p(rs5c348_remove),
  206. };
  207. static __init int rs5c348_init(void)
  208. {
  209. return spi_register_driver(&rs5c348_driver);
  210. }
  211. static __exit void rs5c348_exit(void)
  212. {
  213. spi_unregister_driver(&rs5c348_driver);
  214. }
  215. module_init(rs5c348_init);
  216. module_exit(rs5c348_exit);
  217. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  218. MODULE_DESCRIPTION("Ricoh RS5C348 RTC driver");
  219. MODULE_LICENSE("GPL");
  220. MODULE_VERSION(DRV_VERSION);
  221. MODULE_ALIAS("spi:rtc-rs5c348");