rtc-pcf8563.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * An I2C driver for the Philips PCF8563 RTC
  3. * Copyright 2005-06 Tower Technologies
  4. *
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. * Maintainers: http://www.nslu2-linux.org/
  7. *
  8. * based on the other drivers in this same directory.
  9. *
  10. * http://www.semiconductors.philips.com/acrobat/datasheets/PCF8563-04.pdf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/i2c.h>
  17. #include <linux/bcd.h>
  18. #include <linux/rtc.h>
  19. #define DRV_VERSION "0.4.3"
  20. #define PCF8563_REG_ST1 0x00 /* status */
  21. #define PCF8563_REG_ST2 0x01
  22. #define PCF8563_REG_SC 0x02 /* datetime */
  23. #define PCF8563_REG_MN 0x03
  24. #define PCF8563_REG_HR 0x04
  25. #define PCF8563_REG_DM 0x05
  26. #define PCF8563_REG_DW 0x06
  27. #define PCF8563_REG_MO 0x07
  28. #define PCF8563_REG_YR 0x08
  29. #define PCF8563_REG_AMN 0x09 /* alarm */
  30. #define PCF8563_REG_AHR 0x0A
  31. #define PCF8563_REG_ADM 0x0B
  32. #define PCF8563_REG_ADW 0x0C
  33. #define PCF8563_REG_CLKO 0x0D /* clock out */
  34. #define PCF8563_REG_TMRC 0x0E /* timer control */
  35. #define PCF8563_REG_TMR 0x0F /* timer */
  36. #define PCF8563_SC_LV 0x80 /* low voltage */
  37. #define PCF8563_MO_C 0x80 /* century */
  38. static struct i2c_driver pcf8563_driver;
  39. struct pcf8563 {
  40. struct rtc_device *rtc;
  41. /*
  42. * The meaning of MO_C bit varies by the chip type.
  43. * From PCF8563 datasheet: this bit is toggled when the years
  44. * register overflows from 99 to 00
  45. * 0 indicates the century is 20xx
  46. * 1 indicates the century is 19xx
  47. * From RTC8564 datasheet: this bit indicates change of
  48. * century. When the year digit data overflows from 99 to 00,
  49. * this bit is set. By presetting it to 0 while still in the
  50. * 20th century, it will be set in year 2000, ...
  51. * There seems no reliable way to know how the system use this
  52. * bit. So let's do it heuristically, assuming we are live in
  53. * 1970...2069.
  54. */
  55. int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
  56. };
  57. /*
  58. * In the routines that deal directly with the pcf8563 hardware, we use
  59. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  60. */
  61. static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  62. {
  63. struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
  64. unsigned char buf[13] = { PCF8563_REG_ST1 };
  65. struct i2c_msg msgs[] = {
  66. { client->addr, 0, 1, buf }, /* setup read ptr */
  67. { client->addr, I2C_M_RD, 13, buf }, /* read status + date */
  68. };
  69. /* read registers */
  70. if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
  71. dev_err(&client->dev, "%s: read error\n", __func__);
  72. return -EIO;
  73. }
  74. if (buf[PCF8563_REG_SC] & PCF8563_SC_LV)
  75. dev_info(&client->dev,
  76. "low voltage detected, date/time is not reliable.\n");
  77. dev_dbg(&client->dev,
  78. "%s: raw data is st1=%02x, st2=%02x, sec=%02x, min=%02x, hr=%02x, "
  79. "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
  80. __func__,
  81. buf[0], buf[1], buf[2], buf[3],
  82. buf[4], buf[5], buf[6], buf[7],
  83. buf[8]);
  84. tm->tm_sec = BCD2BIN(buf[PCF8563_REG_SC] & 0x7F);
  85. tm->tm_min = BCD2BIN(buf[PCF8563_REG_MN] & 0x7F);
  86. tm->tm_hour = BCD2BIN(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */
  87. tm->tm_mday = BCD2BIN(buf[PCF8563_REG_DM] & 0x3F);
  88. tm->tm_wday = buf[PCF8563_REG_DW] & 0x07;
  89. tm->tm_mon = BCD2BIN(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  90. tm->tm_year = BCD2BIN(buf[PCF8563_REG_YR]);
  91. if (tm->tm_year < 70)
  92. tm->tm_year += 100; /* assume we are in 1970...2069 */
  93. /* detect the polarity heuristically. see note above. */
  94. pcf8563->c_polarity = (buf[PCF8563_REG_MO] & PCF8563_MO_C) ?
  95. (tm->tm_year >= 100) : (tm->tm_year < 100);
  96. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  97. "mday=%d, mon=%d, year=%d, wday=%d\n",
  98. __func__,
  99. tm->tm_sec, tm->tm_min, tm->tm_hour,
  100. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  101. /* the clock can give out invalid datetime, but we cannot return
  102. * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  103. */
  104. if (rtc_valid_tm(tm) < 0)
  105. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  106. return 0;
  107. }
  108. static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  109. {
  110. struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
  111. int i, err;
  112. unsigned char buf[9];
  113. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  114. "mday=%d, mon=%d, year=%d, wday=%d\n",
  115. __func__,
  116. tm->tm_sec, tm->tm_min, tm->tm_hour,
  117. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  118. /* hours, minutes and seconds */
  119. buf[PCF8563_REG_SC] = BIN2BCD(tm->tm_sec);
  120. buf[PCF8563_REG_MN] = BIN2BCD(tm->tm_min);
  121. buf[PCF8563_REG_HR] = BIN2BCD(tm->tm_hour);
  122. buf[PCF8563_REG_DM] = BIN2BCD(tm->tm_mday);
  123. /* month, 1 - 12 */
  124. buf[PCF8563_REG_MO] = BIN2BCD(tm->tm_mon + 1);
  125. /* year and century */
  126. buf[PCF8563_REG_YR] = BIN2BCD(tm->tm_year % 100);
  127. if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100))
  128. buf[PCF8563_REG_MO] |= PCF8563_MO_C;
  129. buf[PCF8563_REG_DW] = tm->tm_wday & 0x07;
  130. /* write register's data */
  131. for (i = 0; i < 7; i++) {
  132. unsigned char data[2] = { PCF8563_REG_SC + i,
  133. buf[PCF8563_REG_SC + i] };
  134. err = i2c_master_send(client, data, sizeof(data));
  135. if (err != sizeof(data)) {
  136. dev_err(&client->dev,
  137. "%s: err=%d addr=%02x, data=%02x\n",
  138. __func__, err, data[0], data[1]);
  139. return -EIO;
  140. }
  141. };
  142. return 0;
  143. }
  144. struct pcf8563_limit
  145. {
  146. unsigned char reg;
  147. unsigned char mask;
  148. unsigned char min;
  149. unsigned char max;
  150. };
  151. static int pcf8563_validate_client(struct i2c_client *client)
  152. {
  153. int i;
  154. static const struct pcf8563_limit pattern[] = {
  155. /* register, mask, min, max */
  156. { PCF8563_REG_SC, 0x7F, 0, 59 },
  157. { PCF8563_REG_MN, 0x7F, 0, 59 },
  158. { PCF8563_REG_HR, 0x3F, 0, 23 },
  159. { PCF8563_REG_DM, 0x3F, 0, 31 },
  160. { PCF8563_REG_MO, 0x1F, 0, 12 },
  161. };
  162. /* check limits (only registers with bcd values) */
  163. for (i = 0; i < ARRAY_SIZE(pattern); i++) {
  164. int xfer;
  165. unsigned char value;
  166. unsigned char buf = pattern[i].reg;
  167. struct i2c_msg msgs[] = {
  168. { client->addr, 0, 1, &buf },
  169. { client->addr, I2C_M_RD, 1, &buf },
  170. };
  171. xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  172. if (xfer != ARRAY_SIZE(msgs)) {
  173. dev_err(&client->dev,
  174. "%s: could not read register 0x%02X\n",
  175. __func__, pattern[i].reg);
  176. return -EIO;
  177. }
  178. value = BCD2BIN(buf & pattern[i].mask);
  179. if (value > pattern[i].max ||
  180. value < pattern[i].min) {
  181. dev_dbg(&client->dev,
  182. "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, "
  183. "max=%d, value=%d, raw=0x%02X\n",
  184. __func__, i, pattern[i].reg, pattern[i].mask,
  185. pattern[i].min, pattern[i].max,
  186. value, buf);
  187. return -ENODEV;
  188. }
  189. }
  190. return 0;
  191. }
  192. static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
  193. {
  194. return pcf8563_get_datetime(to_i2c_client(dev), tm);
  195. }
  196. static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
  197. {
  198. return pcf8563_set_datetime(to_i2c_client(dev), tm);
  199. }
  200. static const struct rtc_class_ops pcf8563_rtc_ops = {
  201. .read_time = pcf8563_rtc_read_time,
  202. .set_time = pcf8563_rtc_set_time,
  203. };
  204. static int pcf8563_probe(struct i2c_client *client,
  205. const struct i2c_device_id *id)
  206. {
  207. struct pcf8563 *pcf8563;
  208. int err = 0;
  209. dev_dbg(&client->dev, "%s\n", __func__);
  210. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  211. return -ENODEV;
  212. pcf8563 = kzalloc(sizeof(struct pcf8563), GFP_KERNEL);
  213. if (!pcf8563)
  214. return -ENOMEM;
  215. /* Verify the chip is really an PCF8563 */
  216. if (pcf8563_validate_client(client) < 0) {
  217. err = -ENODEV;
  218. goto exit_kfree;
  219. }
  220. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  221. pcf8563->rtc = rtc_device_register(pcf8563_driver.driver.name,
  222. &client->dev, &pcf8563_rtc_ops, THIS_MODULE);
  223. if (IS_ERR(pcf8563->rtc)) {
  224. err = PTR_ERR(pcf8563->rtc);
  225. goto exit_kfree;
  226. }
  227. i2c_set_clientdata(client, pcf8563);
  228. return 0;
  229. exit_kfree:
  230. kfree(pcf8563);
  231. return err;
  232. }
  233. static int pcf8563_remove(struct i2c_client *client)
  234. {
  235. struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
  236. if (pcf8563->rtc)
  237. rtc_device_unregister(pcf8563->rtc);
  238. kfree(pcf8563);
  239. return 0;
  240. }
  241. static const struct i2c_device_id pcf8563_id[] = {
  242. { "pcf8563", 0 },
  243. { "rtc8564", 0 },
  244. { }
  245. };
  246. MODULE_DEVICE_TABLE(i2c, pcf8563_id);
  247. static struct i2c_driver pcf8563_driver = {
  248. .driver = {
  249. .name = "rtc-pcf8563",
  250. },
  251. .probe = pcf8563_probe,
  252. .remove = pcf8563_remove,
  253. .id_table = pcf8563_id,
  254. };
  255. static int __init pcf8563_init(void)
  256. {
  257. return i2c_add_driver(&pcf8563_driver);
  258. }
  259. static void __exit pcf8563_exit(void)
  260. {
  261. i2c_del_driver(&pcf8563_driver);
  262. }
  263. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  264. MODULE_DESCRIPTION("Philips PCF8563/Epson RTC8564 RTC driver");
  265. MODULE_LICENSE("GPL");
  266. MODULE_VERSION(DRV_VERSION);
  267. module_init(pcf8563_init);
  268. module_exit(pcf8563_exit);