rtc-rx8581.c 9.0 KB

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