rtc-rs5c348.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include <linux/module.h>
  26. #define DRV_VERSION "0.2"
  27. #define RS5C348_REG_SECS 0
  28. #define RS5C348_REG_MINS 1
  29. #define RS5C348_REG_HOURS 2
  30. #define RS5C348_REG_WDAY 3
  31. #define RS5C348_REG_DAY 4
  32. #define RS5C348_REG_MONTH 5
  33. #define RS5C348_REG_YEAR 6
  34. #define RS5C348_REG_CTL1 14
  35. #define RS5C348_REG_CTL2 15
  36. #define RS5C348_SECS_MASK 0x7f
  37. #define RS5C348_MINS_MASK 0x7f
  38. #define RS5C348_HOURS_MASK 0x3f
  39. #define RS5C348_WDAY_MASK 0x03
  40. #define RS5C348_DAY_MASK 0x3f
  41. #define RS5C348_MONTH_MASK 0x1f
  42. #define RS5C348_BIT_PM 0x20 /* REG_HOURS */
  43. #define RS5C348_BIT_Y2K 0x80 /* REG_MONTH */
  44. #define RS5C348_BIT_24H 0x20 /* REG_CTL1 */
  45. #define RS5C348_BIT_XSTP 0x10 /* REG_CTL2 */
  46. #define RS5C348_BIT_VDET 0x40 /* REG_CTL2 */
  47. #define RS5C348_CMD_W(addr) (((addr) << 4) | 0x08) /* single write */
  48. #define RS5C348_CMD_R(addr) (((addr) << 4) | 0x0c) /* single read */
  49. #define RS5C348_CMD_MW(addr) (((addr) << 4) | 0x00) /* burst write */
  50. #define RS5C348_CMD_MR(addr) (((addr) << 4) | 0x04) /* burst read */
  51. struct rs5c348_plat_data {
  52. struct rtc_device *rtc;
  53. int rtc_24h;
  54. };
  55. static int
  56. rs5c348_rtc_set_time(struct device *dev, struct rtc_time *tm)
  57. {
  58. struct spi_device *spi = to_spi_device(dev);
  59. struct rs5c348_plat_data *pdata = dev_get_platdata(&spi->dev);
  60. u8 txbuf[5+7], *txp;
  61. int ret;
  62. /* Transfer 5 bytes before writing SEC. This gives 31us for carry. */
  63. txp = txbuf;
  64. txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  65. txbuf[1] = 0; /* dummy */
  66. txbuf[2] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  67. txbuf[3] = 0; /* dummy */
  68. txbuf[4] = RS5C348_CMD_MW(RS5C348_REG_SECS); /* cmd, sec, ... */
  69. txp = &txbuf[5];
  70. txp[RS5C348_REG_SECS] = bin2bcd(tm->tm_sec);
  71. txp[RS5C348_REG_MINS] = bin2bcd(tm->tm_min);
  72. if (pdata->rtc_24h) {
  73. txp[RS5C348_REG_HOURS] = bin2bcd(tm->tm_hour);
  74. } else {
  75. /* hour 0 is AM12, noon is PM12 */
  76. txp[RS5C348_REG_HOURS] = bin2bcd((tm->tm_hour + 11) % 12 + 1) |
  77. (tm->tm_hour >= 12 ? RS5C348_BIT_PM : 0);
  78. }
  79. txp[RS5C348_REG_WDAY] = bin2bcd(tm->tm_wday);
  80. txp[RS5C348_REG_DAY] = bin2bcd(tm->tm_mday);
  81. txp[RS5C348_REG_MONTH] = bin2bcd(tm->tm_mon + 1) |
  82. (tm->tm_year >= 100 ? RS5C348_BIT_Y2K : 0);
  83. txp[RS5C348_REG_YEAR] = bin2bcd(tm->tm_year % 100);
  84. /* write in one transfer to avoid data inconsistency */
  85. ret = spi_write_then_read(spi, txbuf, sizeof(txbuf), NULL, 0);
  86. udelay(62); /* Tcsr 62us */
  87. return ret;
  88. }
  89. static int
  90. rs5c348_rtc_read_time(struct device *dev, struct rtc_time *tm)
  91. {
  92. struct spi_device *spi = to_spi_device(dev);
  93. struct rs5c348_plat_data *pdata = dev_get_platdata(&spi->dev);
  94. u8 txbuf[5], rxbuf[7];
  95. int ret;
  96. /* Transfer 5 byte befores reading SEC. This gives 31us for carry. */
  97. txbuf[0] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  98. txbuf[1] = 0; /* dummy */
  99. txbuf[2] = RS5C348_CMD_R(RS5C348_REG_CTL2); /* cmd, ctl2 */
  100. txbuf[3] = 0; /* dummy */
  101. txbuf[4] = RS5C348_CMD_MR(RS5C348_REG_SECS); /* cmd, sec, ... */
  102. /* read in one transfer to avoid data inconsistency */
  103. ret = spi_write_then_read(spi, txbuf, sizeof(txbuf),
  104. rxbuf, sizeof(rxbuf));
  105. udelay(62); /* Tcsr 62us */
  106. if (ret < 0)
  107. return ret;
  108. tm->tm_sec = bcd2bin(rxbuf[RS5C348_REG_SECS] & RS5C348_SECS_MASK);
  109. tm->tm_min = bcd2bin(rxbuf[RS5C348_REG_MINS] & RS5C348_MINS_MASK);
  110. tm->tm_hour = bcd2bin(rxbuf[RS5C348_REG_HOURS] & RS5C348_HOURS_MASK);
  111. if (!pdata->rtc_24h) {
  112. if (rxbuf[RS5C348_REG_HOURS] & RS5C348_BIT_PM) {
  113. tm->tm_hour -= 20;
  114. tm->tm_hour %= 12;
  115. tm->tm_hour += 12;
  116. } else
  117. tm->tm_hour %= 12;
  118. }
  119. tm->tm_wday = bcd2bin(rxbuf[RS5C348_REG_WDAY] & RS5C348_WDAY_MASK);
  120. tm->tm_mday = bcd2bin(rxbuf[RS5C348_REG_DAY] & RS5C348_DAY_MASK);
  121. tm->tm_mon =
  122. bcd2bin(rxbuf[RS5C348_REG_MONTH] & RS5C348_MONTH_MASK) - 1;
  123. /* year is 1900 + tm->tm_year */
  124. tm->tm_year = bcd2bin(rxbuf[RS5C348_REG_YEAR]) +
  125. ((rxbuf[RS5C348_REG_MONTH] & RS5C348_BIT_Y2K) ? 100 : 0);
  126. if (rtc_valid_tm(tm) < 0) {
  127. dev_err(&spi->dev, "retrieved date/time is not valid.\n");
  128. rtc_time_to_tm(0, tm);
  129. }
  130. return 0;
  131. }
  132. static const struct rtc_class_ops rs5c348_rtc_ops = {
  133. .read_time = rs5c348_rtc_read_time,
  134. .set_time = rs5c348_rtc_set_time,
  135. };
  136. static struct spi_driver rs5c348_driver;
  137. static int rs5c348_probe(struct spi_device *spi)
  138. {
  139. int ret;
  140. struct rtc_device *rtc;
  141. struct rs5c348_plat_data *pdata;
  142. pdata = devm_kzalloc(&spi->dev, sizeof(struct rs5c348_plat_data),
  143. GFP_KERNEL);
  144. if (!pdata)
  145. return -ENOMEM;
  146. spi->dev.platform_data = pdata;
  147. /* Check D7 of SECOND register */
  148. ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_SECS));
  149. if (ret < 0 || (ret & 0x80)) {
  150. dev_err(&spi->dev, "not found.\n");
  151. goto kfree_exit;
  152. }
  153. dev_info(&spi->dev, "chip found, driver version " DRV_VERSION "\n");
  154. dev_info(&spi->dev, "spiclk %u KHz.\n",
  155. (spi->max_speed_hz + 500) / 1000);
  156. /* turn RTC on if it was not on */
  157. ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL2));
  158. if (ret < 0)
  159. goto kfree_exit;
  160. if (ret & (RS5C348_BIT_XSTP | RS5C348_BIT_VDET)) {
  161. u8 buf[2];
  162. struct rtc_time tm;
  163. if (ret & RS5C348_BIT_VDET)
  164. dev_warn(&spi->dev, "voltage-low detected.\n");
  165. if (ret & RS5C348_BIT_XSTP)
  166. dev_warn(&spi->dev, "oscillator-stop detected.\n");
  167. rtc_time_to_tm(0, &tm); /* 1970/1/1 */
  168. ret = rs5c348_rtc_set_time(&spi->dev, &tm);
  169. if (ret < 0)
  170. goto kfree_exit;
  171. buf[0] = RS5C348_CMD_W(RS5C348_REG_CTL2);
  172. buf[1] = 0;
  173. ret = spi_write_then_read(spi, buf, sizeof(buf), NULL, 0);
  174. if (ret < 0)
  175. goto kfree_exit;
  176. }
  177. ret = spi_w8r8(spi, RS5C348_CMD_R(RS5C348_REG_CTL1));
  178. if (ret < 0)
  179. goto kfree_exit;
  180. if (ret & RS5C348_BIT_24H)
  181. pdata->rtc_24h = 1;
  182. rtc = devm_rtc_device_register(&spi->dev, rs5c348_driver.driver.name,
  183. &rs5c348_rtc_ops, THIS_MODULE);
  184. if (IS_ERR(rtc)) {
  185. ret = PTR_ERR(rtc);
  186. goto kfree_exit;
  187. }
  188. pdata->rtc = rtc;
  189. return 0;
  190. kfree_exit:
  191. return ret;
  192. }
  193. static struct spi_driver rs5c348_driver = {
  194. .driver = {
  195. .name = "rtc-rs5c348",
  196. },
  197. .probe = rs5c348_probe,
  198. };
  199. module_spi_driver(rs5c348_driver);
  200. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  201. MODULE_DESCRIPTION("Ricoh RS5C348 RTC driver");
  202. MODULE_LICENSE("GPL");
  203. MODULE_VERSION(DRV_VERSION);
  204. MODULE_ALIAS("spi:rtc-rs5c348");