rtc-rx4581.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* drivers/rtc/rtc-rx4581.c
  2. *
  3. * written by Torben Hohn <torbenh@linutronix.de>
  4. *
  5. * Based on:
  6. * drivers/rtc/rtc-max6902.c
  7. *
  8. * Copyright (C) 2006 8D Technologies inc.
  9. * Copyright (C) 2004 Compulab Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * Driver for MAX6902 spi RTC
  16. *
  17. * and based on:
  18. * drivers/rtc/rtc-rx8581.c
  19. *
  20. * An I2C driver for the Epson RX8581 RTC
  21. *
  22. * Author: Martyn Welch <martyn.welch@ge.com>
  23. * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  24. *
  25. * This program is free software; you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License version 2 as
  27. * published by the Free Software Foundation.
  28. *
  29. * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
  30. * Copyright 2005-06 Tower Technologies
  31. *
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/init.h>
  37. #include <linux/rtc.h>
  38. #include <linux/spi/spi.h>
  39. #include <linux/bcd.h>
  40. #define RX4581_REG_SC 0x00 /* Second in BCD */
  41. #define RX4581_REG_MN 0x01 /* Minute in BCD */
  42. #define RX4581_REG_HR 0x02 /* Hour in BCD */
  43. #define RX4581_REG_DW 0x03 /* Day of Week */
  44. #define RX4581_REG_DM 0x04 /* Day of Month in BCD */
  45. #define RX4581_REG_MO 0x05 /* Month in BCD */
  46. #define RX4581_REG_YR 0x06 /* Year in BCD */
  47. #define RX4581_REG_RAM 0x07 /* RAM */
  48. #define RX4581_REG_AMN 0x08 /* Alarm Min in BCD*/
  49. #define RX4581_REG_AHR 0x09 /* Alarm Hour in BCD */
  50. #define RX4581_REG_ADM 0x0A
  51. #define RX4581_REG_ADW 0x0A
  52. #define RX4581_REG_TMR0 0x0B
  53. #define RX4581_REG_TMR1 0x0C
  54. #define RX4581_REG_EXT 0x0D /* Extension Register */
  55. #define RX4581_REG_FLAG 0x0E /* Flag Register */
  56. #define RX4581_REG_CTRL 0x0F /* Control Register */
  57. /* Flag Register bit definitions */
  58. #define RX4581_FLAG_UF 0x20 /* Update */
  59. #define RX4581_FLAG_TF 0x10 /* Timer */
  60. #define RX4581_FLAG_AF 0x08 /* Alarm */
  61. #define RX4581_FLAG_VLF 0x02 /* Voltage Low */
  62. /* Control Register bit definitions */
  63. #define RX4581_CTRL_UIE 0x20 /* Update Interrupt Enable */
  64. #define RX4581_CTRL_TIE 0x10 /* Timer Interrupt Enable */
  65. #define RX4581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */
  66. #define RX4581_CTRL_STOP 0x02 /* STOP bit */
  67. #define RX4581_CTRL_RESET 0x01 /* RESET bit */
  68. static int rx4581_set_reg(struct device *dev, unsigned char address,
  69. unsigned char data)
  70. {
  71. struct spi_device *spi = to_spi_device(dev);
  72. unsigned char buf[2];
  73. /* high nibble must be '0' to write */
  74. buf[0] = address & 0x0f;
  75. buf[1] = data;
  76. return spi_write_then_read(spi, buf, 2, NULL, 0);
  77. }
  78. static int rx4581_get_reg(struct device *dev, unsigned char address,
  79. unsigned char *data)
  80. {
  81. struct spi_device *spi = to_spi_device(dev);
  82. /* Set MSB to indicate read */
  83. *data = address | 0x80;
  84. return spi_write_then_read(spi, data, 1, data, 1);
  85. }
  86. /*
  87. * In the routines that deal directly with the rx8581 hardware, we use
  88. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  89. */
  90. static int rx4581_get_datetime(struct device *dev, struct rtc_time *tm)
  91. {
  92. struct spi_device *spi = to_spi_device(dev);
  93. unsigned char date[7];
  94. unsigned char data;
  95. int err;
  96. /* First we ensure that the "update flag" is not set, we read the
  97. * time and date then re-read the "update flag". If the update flag
  98. * has been set, we know that the time has changed during the read so
  99. * we repeat the whole process again.
  100. */
  101. err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
  102. if (err != 0) {
  103. dev_err(dev, "Unable to read device flags\n");
  104. return -EIO;
  105. }
  106. do {
  107. /* If update flag set, clear it */
  108. if (data & RX4581_FLAG_UF) {
  109. err = rx4581_set_reg(dev,
  110. RX4581_REG_FLAG, (data & ~RX4581_FLAG_UF));
  111. if (err != 0) {
  112. dev_err(dev, "Unable to write device "
  113. "flags\n");
  114. return -EIO;
  115. }
  116. }
  117. /* Now read time and date */
  118. date[0] = 0x80;
  119. err = spi_write_then_read(spi, date, 1, date, 7);
  120. if (err < 0) {
  121. dev_err(dev, "Unable to read date\n");
  122. return -EIO;
  123. }
  124. /* Check flag register */
  125. err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
  126. if (err != 0) {
  127. dev_err(dev, "Unable to read device flags\n");
  128. return -EIO;
  129. }
  130. } while (data & RX4581_FLAG_UF);
  131. if (data & RX4581_FLAG_VLF)
  132. dev_info(dev,
  133. "low voltage detected, date/time is not reliable.\n");
  134. dev_dbg(dev,
  135. "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
  136. "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
  137. __func__,
  138. date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
  139. tm->tm_sec = bcd2bin(date[RX4581_REG_SC] & 0x7F);
  140. tm->tm_min = bcd2bin(date[RX4581_REG_MN] & 0x7F);
  141. tm->tm_hour = bcd2bin(date[RX4581_REG_HR] & 0x3F); /* rtc hr 0-23 */
  142. tm->tm_wday = ilog2(date[RX4581_REG_DW] & 0x7F);
  143. tm->tm_mday = bcd2bin(date[RX4581_REG_DM] & 0x3F);
  144. tm->tm_mon = bcd2bin(date[RX4581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  145. tm->tm_year = bcd2bin(date[RX4581_REG_YR]);
  146. if (tm->tm_year < 70)
  147. tm->tm_year += 100; /* assume we are in 1970...2069 */
  148. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  149. "mday=%d, mon=%d, year=%d, wday=%d\n",
  150. __func__,
  151. tm->tm_sec, tm->tm_min, tm->tm_hour,
  152. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  153. err = rtc_valid_tm(tm);
  154. if (err < 0)
  155. dev_err(dev, "retrieved date/time is not valid.\n");
  156. return err;
  157. }
  158. static int rx4581_set_datetime(struct device *dev, struct rtc_time *tm)
  159. {
  160. struct spi_device *spi = to_spi_device(dev);
  161. int err;
  162. unsigned char buf[8], data;
  163. dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
  164. "mday=%d, mon=%d, year=%d, wday=%d\n",
  165. __func__,
  166. tm->tm_sec, tm->tm_min, tm->tm_hour,
  167. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  168. buf[0] = 0x00;
  169. /* hours, minutes and seconds */
  170. buf[RX4581_REG_SC+1] = bin2bcd(tm->tm_sec);
  171. buf[RX4581_REG_MN+1] = bin2bcd(tm->tm_min);
  172. buf[RX4581_REG_HR+1] = bin2bcd(tm->tm_hour);
  173. buf[RX4581_REG_DM+1] = bin2bcd(tm->tm_mday);
  174. /* month, 1 - 12 */
  175. buf[RX4581_REG_MO+1] = bin2bcd(tm->tm_mon + 1);
  176. /* year and century */
  177. buf[RX4581_REG_YR+1] = bin2bcd(tm->tm_year % 100);
  178. buf[RX4581_REG_DW+1] = (0x1 << tm->tm_wday);
  179. /* Stop the clock */
  180. err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
  181. if (err != 0) {
  182. dev_err(dev, "Unable to read control register\n");
  183. return -EIO;
  184. }
  185. err = rx4581_set_reg(dev, RX4581_REG_CTRL,
  186. (data | RX4581_CTRL_STOP));
  187. if (err != 0) {
  188. dev_err(dev, "Unable to write control register\n");
  189. return -EIO;
  190. }
  191. /* write register's data */
  192. err = spi_write_then_read(spi, buf, 8, NULL, 0);
  193. if (err != 0) {
  194. dev_err(dev, "Unable to write to date registers\n");
  195. return -EIO;
  196. }
  197. /* get VLF and clear it */
  198. err = rx4581_get_reg(dev, RX4581_REG_FLAG, &data);
  199. if (err != 0) {
  200. dev_err(dev, "Unable to read flag register\n");
  201. return -EIO;
  202. }
  203. err = rx4581_set_reg(dev, RX4581_REG_FLAG,
  204. (data & ~(RX4581_FLAG_VLF)));
  205. if (err != 0) {
  206. dev_err(dev, "Unable to write flag register\n");
  207. return -EIO;
  208. }
  209. /* Restart the clock */
  210. err = rx4581_get_reg(dev, RX4581_REG_CTRL, &data);
  211. if (err != 0) {
  212. dev_err(dev, "Unable to read control register\n");
  213. return -EIO;
  214. }
  215. err = rx4581_set_reg(dev, RX4581_REG_CTRL,
  216. (data & ~(RX4581_CTRL_STOP)));
  217. if (err != 0) {
  218. dev_err(dev, "Unable to write control register\n");
  219. return -EIO;
  220. }
  221. return 0;
  222. }
  223. static const struct rtc_class_ops rx4581_rtc_ops = {
  224. .read_time = rx4581_get_datetime,
  225. .set_time = rx4581_set_datetime,
  226. };
  227. static int rx4581_probe(struct spi_device *spi)
  228. {
  229. struct rtc_device *rtc;
  230. unsigned char tmp;
  231. int res;
  232. res = rx4581_get_reg(&spi->dev, RX4581_REG_SC, &tmp);
  233. if (res != 0)
  234. return res;
  235. rtc = devm_rtc_device_register(&spi->dev, "rx4581",
  236. &rx4581_rtc_ops, THIS_MODULE);
  237. if (IS_ERR(rtc))
  238. return PTR_ERR(rtc);
  239. spi_set_drvdata(spi, rtc);
  240. return 0;
  241. }
  242. static const struct spi_device_id rx4581_id[] = {
  243. { "rx4581", 0 },
  244. { }
  245. };
  246. MODULE_DEVICE_TABLE(spi, rx4581_id);
  247. static struct spi_driver rx4581_driver = {
  248. .driver = {
  249. .name = "rtc-rx4581",
  250. },
  251. .probe = rx4581_probe,
  252. .id_table = rx4581_id,
  253. };
  254. module_spi_driver(rx4581_driver);
  255. MODULE_DESCRIPTION("rx4581 spi RTC driver");
  256. MODULE_AUTHOR("Torben Hohn");
  257. MODULE_LICENSE("GPL");
  258. MODULE_ALIAS("spi:rtc-rx4581");