rtc-pcf8563.c 9.5 KB

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