rtc-pcf8563.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind);
  47. static int pcf8563_detach(struct i2c_client *client);
  48. /*
  49. * In the routines that deal directly with the pcf8563 hardware, we use
  50. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  51. */
  52. static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  53. {
  54. unsigned char buf[13] = { PCF8563_REG_ST1 };
  55. struct i2c_msg msgs[] = {
  56. { client->addr, 0, 1, buf }, /* setup read ptr */
  57. { client->addr, I2C_M_RD, 13, buf }, /* read status + date */
  58. };
  59. /* read registers */
  60. if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
  61. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  62. return -EIO;
  63. }
  64. if (buf[PCF8563_REG_SC] & PCF8563_SC_LV)
  65. dev_info(&client->dev,
  66. "low voltage detected, date/time is not reliable.\n");
  67. dev_dbg(&client->dev,
  68. "%s: raw data is st1=%02x, st2=%02x, sec=%02x, min=%02x, hr=%02x, "
  69. "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
  70. __FUNCTION__,
  71. buf[0], buf[1], buf[2], buf[3],
  72. buf[4], buf[5], buf[6], buf[7],
  73. buf[8]);
  74. tm->tm_sec = BCD2BIN(buf[PCF8563_REG_SC] & 0x7F);
  75. tm->tm_min = BCD2BIN(buf[PCF8563_REG_MN] & 0x7F);
  76. tm->tm_hour = BCD2BIN(buf[PCF8563_REG_HR] & 0x3F); /* rtc hr 0-23 */
  77. tm->tm_mday = BCD2BIN(buf[PCF8563_REG_DM] & 0x3F);
  78. tm->tm_wday = buf[PCF8563_REG_DW] & 0x07;
  79. tm->tm_mon = BCD2BIN(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  80. tm->tm_year = BCD2BIN(buf[PCF8563_REG_YR])
  81. + (buf[PCF8563_REG_MO] & PCF8563_MO_C ? 100 : 0);
  82. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  83. "mday=%d, mon=%d, year=%d, wday=%d\n",
  84. __FUNCTION__,
  85. tm->tm_sec, tm->tm_min, tm->tm_hour,
  86. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  87. /* the clock can give out invalid datetime, but we cannot return
  88. * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  89. */
  90. if (rtc_valid_tm(tm) < 0)
  91. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  92. return 0;
  93. }
  94. static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  95. {
  96. int i, err;
  97. unsigned char buf[9];
  98. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  99. "mday=%d, mon=%d, year=%d, wday=%d\n",
  100. __FUNCTION__,
  101. tm->tm_sec, tm->tm_min, tm->tm_hour,
  102. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  103. /* hours, minutes and seconds */
  104. buf[PCF8563_REG_SC] = BIN2BCD(tm->tm_sec);
  105. buf[PCF8563_REG_MN] = BIN2BCD(tm->tm_min);
  106. buf[PCF8563_REG_HR] = BIN2BCD(tm->tm_hour);
  107. buf[PCF8563_REG_DM] = BIN2BCD(tm->tm_mday);
  108. /* month, 1 - 12 */
  109. buf[PCF8563_REG_MO] = BIN2BCD(tm->tm_mon + 1);
  110. /* year and century */
  111. buf[PCF8563_REG_YR] = BIN2BCD(tm->tm_year % 100);
  112. if (tm->tm_year / 100)
  113. buf[PCF8563_REG_MO] |= PCF8563_MO_C;
  114. buf[PCF8563_REG_DW] = tm->tm_wday & 0x07;
  115. /* write register's data */
  116. for (i = 0; i < 7; i++) {
  117. unsigned char data[2] = { PCF8563_REG_SC + i,
  118. buf[PCF8563_REG_SC + i] };
  119. err = i2c_master_send(client, data, sizeof(data));
  120. if (err != sizeof(data)) {
  121. dev_err(&client->dev,
  122. "%s: err=%d addr=%02x, data=%02x\n",
  123. __FUNCTION__, err, data[0], data[1]);
  124. return -EIO;
  125. }
  126. };
  127. return 0;
  128. }
  129. struct pcf8563_limit
  130. {
  131. unsigned char reg;
  132. unsigned char mask;
  133. unsigned char min;
  134. unsigned char max;
  135. };
  136. static int pcf8563_validate_client(struct i2c_client *client)
  137. {
  138. int i;
  139. static const struct pcf8563_limit pattern[] = {
  140. /* register, mask, min, max */
  141. { PCF8563_REG_SC, 0x7F, 0, 59 },
  142. { PCF8563_REG_MN, 0x7F, 0, 59 },
  143. { PCF8563_REG_HR, 0x3F, 0, 23 },
  144. { PCF8563_REG_DM, 0x3F, 0, 31 },
  145. { PCF8563_REG_MO, 0x1F, 0, 12 },
  146. };
  147. /* check limits (only registers with bcd values) */
  148. for (i = 0; i < ARRAY_SIZE(pattern); i++) {
  149. int xfer;
  150. unsigned char value;
  151. unsigned char buf = pattern[i].reg;
  152. struct i2c_msg msgs[] = {
  153. { client->addr, 0, 1, &buf },
  154. { client->addr, I2C_M_RD, 1, &buf },
  155. };
  156. xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  157. if (xfer != ARRAY_SIZE(msgs)) {
  158. dev_err(&client->adapter->dev,
  159. "%s: could not read register 0x%02X\n",
  160. __FUNCTION__, pattern[i].reg);
  161. return -EIO;
  162. }
  163. value = BCD2BIN(buf & pattern[i].mask);
  164. if (value > pattern[i].max ||
  165. value < pattern[i].min) {
  166. dev_dbg(&client->adapter->dev,
  167. "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, "
  168. "max=%d, value=%d, raw=0x%02X\n",
  169. __FUNCTION__, i, pattern[i].reg, pattern[i].mask,
  170. pattern[i].min, pattern[i].max,
  171. value, buf);
  172. return -ENODEV;
  173. }
  174. }
  175. return 0;
  176. }
  177. static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
  178. {
  179. return pcf8563_get_datetime(to_i2c_client(dev), tm);
  180. }
  181. static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
  182. {
  183. return pcf8563_set_datetime(to_i2c_client(dev), tm);
  184. }
  185. static struct rtc_class_ops pcf8563_rtc_ops = {
  186. .read_time = pcf8563_rtc_read_time,
  187. .set_time = pcf8563_rtc_set_time,
  188. };
  189. static int pcf8563_attach(struct i2c_adapter *adapter)
  190. {
  191. return i2c_probe(adapter, &addr_data, pcf8563_probe);
  192. }
  193. static struct i2c_driver pcf8563_driver = {
  194. .driver = {
  195. .name = "pcf8563",
  196. },
  197. .id = I2C_DRIVERID_PCF8563,
  198. .attach_adapter = &pcf8563_attach,
  199. .detach_client = &pcf8563_detach,
  200. };
  201. static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind)
  202. {
  203. struct i2c_client *client;
  204. struct rtc_device *rtc;
  205. int err = 0;
  206. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  207. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  208. err = -ENODEV;
  209. goto exit;
  210. }
  211. if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  212. err = -ENOMEM;
  213. goto exit;
  214. }
  215. client->addr = address;
  216. client->driver = &pcf8563_driver;
  217. client->adapter = adapter;
  218. strlcpy(client->name, pcf8563_driver.driver.name, I2C_NAME_SIZE);
  219. /* Verify the chip is really an PCF8563 */
  220. if (kind < 0) {
  221. if (pcf8563_validate_client(client) < 0) {
  222. err = -ENODEV;
  223. goto exit_kfree;
  224. }
  225. }
  226. /* Inform the i2c layer */
  227. if ((err = i2c_attach_client(client)))
  228. goto exit_kfree;
  229. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  230. rtc = rtc_device_register(pcf8563_driver.driver.name, &client->dev,
  231. &pcf8563_rtc_ops, THIS_MODULE);
  232. if (IS_ERR(rtc)) {
  233. err = PTR_ERR(rtc);
  234. goto exit_detach;
  235. }
  236. i2c_set_clientdata(client, rtc);
  237. return 0;
  238. exit_detach:
  239. i2c_detach_client(client);
  240. exit_kfree:
  241. kfree(client);
  242. exit:
  243. return err;
  244. }
  245. static int pcf8563_detach(struct i2c_client *client)
  246. {
  247. int err;
  248. struct rtc_device *rtc = i2c_get_clientdata(client);
  249. if (rtc)
  250. rtc_device_unregister(rtc);
  251. if ((err = i2c_detach_client(client)))
  252. return err;
  253. kfree(client);
  254. return 0;
  255. }
  256. static int __init pcf8563_init(void)
  257. {
  258. return i2c_add_driver(&pcf8563_driver);
  259. }
  260. static void __exit pcf8563_exit(void)
  261. {
  262. i2c_del_driver(&pcf8563_driver);
  263. }
  264. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  265. MODULE_DESCRIPTION("Philips PCF8563/Epson RTC8564 RTC driver");
  266. MODULE_LICENSE("GPL");
  267. MODULE_VERSION(DRV_VERSION);
  268. module_init(pcf8563_init);
  269. module_exit(pcf8563_exit);