rtc-rx8581.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * An I2C driver for the Epson RX8581 RTC
  3. *
  4. * Author: Martyn Welch <martyn.welch@ge.com>
  5. * Copyright 2008 GE Intelligent Platforms Embedded Systems, Inc.
  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. * Based on: rtc-pcf8563.c (An I2C driver for the Philips PCF8563 RTC)
  12. * Copyright 2005-06 Tower Technologies
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/bcd.h>
  17. #include <linux/rtc.h>
  18. #include <linux/log2.h>
  19. #define DRV_VERSION "0.1"
  20. #define RX8581_REG_SC 0x00 /* Second in BCD */
  21. #define RX8581_REG_MN 0x01 /* Minute in BCD */
  22. #define RX8581_REG_HR 0x02 /* Hour in BCD */
  23. #define RX8581_REG_DW 0x03 /* Day of Week */
  24. #define RX8581_REG_DM 0x04 /* Day of Month in BCD */
  25. #define RX8581_REG_MO 0x05 /* Month in BCD */
  26. #define RX8581_REG_YR 0x06 /* Year in BCD */
  27. #define RX8581_REG_RAM 0x07 /* RAM */
  28. #define RX8581_REG_AMN 0x08 /* Alarm Min in BCD*/
  29. #define RX8581_REG_AHR 0x09 /* Alarm Hour in BCD */
  30. #define RX8581_REG_ADM 0x0A
  31. #define RX8581_REG_ADW 0x0A
  32. #define RX8581_REG_TMR0 0x0B
  33. #define RX8581_REG_TMR1 0x0C
  34. #define RX8581_REG_EXT 0x0D /* Extension Register */
  35. #define RX8581_REG_FLAG 0x0E /* Flag Register */
  36. #define RX8581_REG_CTRL 0x0F /* Control Register */
  37. /* Flag Register bit definitions */
  38. #define RX8581_FLAG_UF 0x20 /* Update */
  39. #define RX8581_FLAG_TF 0x10 /* Timer */
  40. #define RX8581_FLAG_AF 0x08 /* Alarm */
  41. #define RX8581_FLAG_VLF 0x02 /* Voltage Low */
  42. /* Control Register bit definitions */
  43. #define RX8581_CTRL_UIE 0x20 /* Update Interrupt Enable */
  44. #define RX8581_CTRL_TIE 0x10 /* Timer Interrupt Enable */
  45. #define RX8581_CTRL_AIE 0x08 /* Alarm Interrupt Enable */
  46. #define RX8581_CTRL_STOP 0x02 /* STOP bit */
  47. #define RX8581_CTRL_RESET 0x01 /* RESET bit */
  48. static struct i2c_driver rx8581_driver;
  49. /*
  50. * In the routines that deal directly with the rx8581 hardware, we use
  51. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  52. */
  53. static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  54. {
  55. unsigned char date[7];
  56. int data, err;
  57. /* First we ensure that the "update flag" is not set, we read the
  58. * time and date then re-read the "update flag". If the update flag
  59. * has been set, we know that the time has changed during the read so
  60. * we repeat the whole process again.
  61. */
  62. data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
  63. if (data < 0) {
  64. dev_err(&client->dev, "Unable to read device flags\n");
  65. return -EIO;
  66. }
  67. do {
  68. /* If update flag set, clear it */
  69. if (data & RX8581_FLAG_UF) {
  70. err = i2c_smbus_write_byte_data(client,
  71. RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
  72. if (err != 0) {
  73. dev_err(&client->dev, "Unable to write device "
  74. "flags\n");
  75. return -EIO;
  76. }
  77. }
  78. /* Now read time and date */
  79. err = i2c_smbus_read_i2c_block_data(client, RX8581_REG_SC,
  80. 7, date);
  81. if (err < 0) {
  82. dev_err(&client->dev, "Unable to read date\n");
  83. return -EIO;
  84. }
  85. /* Check flag register */
  86. data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
  87. if (data < 0) {
  88. dev_err(&client->dev, "Unable to read device flags\n");
  89. return -EIO;
  90. }
  91. } while (data & RX8581_FLAG_UF);
  92. if (data & RX8581_FLAG_VLF)
  93. dev_info(&client->dev,
  94. "low voltage detected, date/time is not reliable.\n");
  95. dev_dbg(&client->dev,
  96. "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
  97. "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
  98. __func__,
  99. date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
  100. tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
  101. tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
  102. tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F); /* rtc hr 0-23 */
  103. tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
  104. tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
  105. tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  106. tm->tm_year = bcd2bin(date[RX8581_REG_YR]);
  107. if (tm->tm_year < 70)
  108. tm->tm_year += 100; /* assume we are in 1970...2069 */
  109. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  110. "mday=%d, mon=%d, year=%d, wday=%d\n",
  111. __func__,
  112. tm->tm_sec, tm->tm_min, tm->tm_hour,
  113. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  114. err = rtc_valid_tm(tm);
  115. if (err < 0)
  116. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  117. return err;
  118. }
  119. static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  120. {
  121. int data, err;
  122. unsigned char buf[7];
  123. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  124. "mday=%d, mon=%d, year=%d, wday=%d\n",
  125. __func__,
  126. tm->tm_sec, tm->tm_min, tm->tm_hour,
  127. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  128. /* hours, minutes and seconds */
  129. buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
  130. buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
  131. buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
  132. buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
  133. /* month, 1 - 12 */
  134. buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
  135. /* year and century */
  136. buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
  137. buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
  138. /* Stop the clock */
  139. data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
  140. if (data < 0) {
  141. dev_err(&client->dev, "Unable to read control register\n");
  142. return -EIO;
  143. }
  144. err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
  145. (data | RX8581_CTRL_STOP));
  146. if (err < 0) {
  147. dev_err(&client->dev, "Unable to write control register\n");
  148. return -EIO;
  149. }
  150. /* write register's data */
  151. err = i2c_smbus_write_i2c_block_data(client, RX8581_REG_SC, 7, buf);
  152. if (err < 0) {
  153. dev_err(&client->dev, "Unable to write to date registers\n");
  154. return -EIO;
  155. }
  156. /* get VLF and clear it */
  157. data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
  158. if (data < 0) {
  159. dev_err(&client->dev, "Unable to read flag register\n");
  160. return -EIO;
  161. }
  162. err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
  163. (data & ~(RX8581_FLAG_VLF)));
  164. if (err != 0) {
  165. dev_err(&client->dev, "Unable to write flag register\n");
  166. return -EIO;
  167. }
  168. /* Restart the clock */
  169. data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
  170. if (data < 0) {
  171. dev_err(&client->dev, "Unable to read control register\n");
  172. return -EIO;
  173. }
  174. err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
  175. (data & ~(RX8581_CTRL_STOP)));
  176. if (err != 0) {
  177. dev_err(&client->dev, "Unable to write control register\n");
  178. return -EIO;
  179. }
  180. return 0;
  181. }
  182. static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
  183. {
  184. return rx8581_get_datetime(to_i2c_client(dev), tm);
  185. }
  186. static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
  187. {
  188. return rx8581_set_datetime(to_i2c_client(dev), tm);
  189. }
  190. static const struct rtc_class_ops rx8581_rtc_ops = {
  191. .read_time = rx8581_rtc_read_time,
  192. .set_time = rx8581_rtc_set_time,
  193. };
  194. static int __devinit rx8581_probe(struct i2c_client *client,
  195. const struct i2c_device_id *id)
  196. {
  197. struct rtc_device *rtc;
  198. dev_dbg(&client->dev, "%s\n", __func__);
  199. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  200. return -ENODEV;
  201. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  202. rtc = rtc_device_register(rx8581_driver.driver.name,
  203. &client->dev, &rx8581_rtc_ops, THIS_MODULE);
  204. if (IS_ERR(rtc))
  205. return PTR_ERR(rtc);
  206. i2c_set_clientdata(client, rtc);
  207. return 0;
  208. }
  209. static int __devexit rx8581_remove(struct i2c_client *client)
  210. {
  211. struct rtc_device *rtc = i2c_get_clientdata(client);
  212. rtc_device_unregister(rtc);
  213. return 0;
  214. }
  215. static const struct i2c_device_id rx8581_id[] = {
  216. { "rx8581", 0 },
  217. { }
  218. };
  219. MODULE_DEVICE_TABLE(i2c, rx8581_id);
  220. static struct i2c_driver rx8581_driver = {
  221. .driver = {
  222. .name = "rtc-rx8581",
  223. .owner = THIS_MODULE,
  224. },
  225. .probe = rx8581_probe,
  226. .remove = __devexit_p(rx8581_remove),
  227. .id_table = rx8581_id,
  228. };
  229. module_i2c_driver(rx8581_driver);
  230. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
  231. MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
  232. MODULE_LICENSE("GPL");
  233. MODULE_VERSION(DRV_VERSION);