rtc-rx8581.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. return 0;
  146. }
  147. static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  148. {
  149. int data, err;
  150. unsigned char buf[7];
  151. struct rx8581 *rx8581 = i2c_get_clientdata(client);
  152. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  153. "mday=%d, mon=%d, year=%d, wday=%d\n",
  154. __func__,
  155. tm->tm_sec, tm->tm_min, tm->tm_hour,
  156. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  157. /* hours, minutes and seconds */
  158. buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
  159. buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
  160. buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
  161. buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
  162. /* month, 1 - 12 */
  163. buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
  164. /* year and century */
  165. buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
  166. buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
  167. /* Stop the clock */
  168. data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
  169. if (data < 0) {
  170. dev_err(&client->dev, "Unable to read control register\n");
  171. return -EIO;
  172. }
  173. err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
  174. (data | RX8581_CTRL_STOP));
  175. if (err < 0) {
  176. dev_err(&client->dev, "Unable to write control register\n");
  177. return -EIO;
  178. }
  179. /* write register's data */
  180. err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
  181. if (err < 0) {
  182. dev_err(&client->dev, "Unable to write to date registers\n");
  183. return -EIO;
  184. }
  185. /* get VLF and clear it */
  186. data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
  187. if (data < 0) {
  188. dev_err(&client->dev, "Unable to read flag register\n");
  189. return -EIO;
  190. }
  191. err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
  192. (data & ~(RX8581_FLAG_VLF)));
  193. if (err != 0) {
  194. dev_err(&client->dev, "Unable to write flag register\n");
  195. return -EIO;
  196. }
  197. /* Restart the clock */
  198. data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
  199. if (data < 0) {
  200. dev_err(&client->dev, "Unable to read control register\n");
  201. return -EIO;
  202. }
  203. err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
  204. (data & ~(RX8581_CTRL_STOP)));
  205. if (err != 0) {
  206. dev_err(&client->dev, "Unable to write control register\n");
  207. return -EIO;
  208. }
  209. return 0;
  210. }
  211. static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
  212. {
  213. return rx8581_get_datetime(to_i2c_client(dev), tm);
  214. }
  215. static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
  216. {
  217. return rx8581_set_datetime(to_i2c_client(dev), tm);
  218. }
  219. static const struct rtc_class_ops rx8581_rtc_ops = {
  220. .read_time = rx8581_rtc_read_time,
  221. .set_time = rx8581_rtc_set_time,
  222. };
  223. static int rx8581_probe(struct i2c_client *client,
  224. const struct i2c_device_id *id)
  225. {
  226. struct rx8581 *rx8581;
  227. dev_dbg(&client->dev, "%s\n", __func__);
  228. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
  229. && !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  230. return -EIO;
  231. rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
  232. if (!rx8581)
  233. return -ENOMEM;
  234. i2c_set_clientdata(client, rx8581);
  235. rx8581->client = client;
  236. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
  237. rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
  238. rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
  239. } else {
  240. rx8581->read_block_data = rx8581_read_block_data;
  241. rx8581->write_block_data = rx8581_write_block_data;
  242. }
  243. rx8581->rtc = devm_rtc_device_register(&client->dev,
  244. rx8581_driver.driver.name, &rx8581_rtc_ops, THIS_MODULE);
  245. if (IS_ERR(rx8581->rtc)) {
  246. dev_err(&client->dev,
  247. "unable to register the class device\n");
  248. return PTR_ERR(rx8581->rtc);
  249. }
  250. return 0;
  251. }
  252. static const struct i2c_device_id rx8581_id[] = {
  253. { "rx8581", 0 },
  254. { }
  255. };
  256. MODULE_DEVICE_TABLE(i2c, rx8581_id);
  257. static const struct of_device_id rx8581_of_match[] = {
  258. { .compatible = "epson,rx8581" },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(of, rx8581_of_match);
  262. static struct i2c_driver rx8581_driver = {
  263. .driver = {
  264. .name = "rtc-rx8581",
  265. .of_match_table = of_match_ptr(rx8581_of_match),
  266. },
  267. .probe = rx8581_probe,
  268. .id_table = rx8581_id,
  269. };
  270. module_i2c_driver(rx8581_driver);
  271. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
  272. MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
  273. MODULE_LICENSE("GPL");