rtc-rx8581.c 9.1 KB

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