rtc-pcf8583.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * drivers/rtc/rtc-pcf8583.c
  3. *
  4. * Copyright (C) 2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Driver for PCF8583 RTC & RAM chip
  11. *
  12. * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/rtc.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/bcd.h>
  22. struct rtc_mem {
  23. unsigned int loc;
  24. unsigned int nr;
  25. unsigned char *data;
  26. };
  27. struct pcf8583 {
  28. struct i2c_client client;
  29. struct rtc_device *rtc;
  30. unsigned char ctrl;
  31. };
  32. #define CTRL_STOP 0x80
  33. #define CTRL_HOLD 0x40
  34. #define CTRL_32KHZ 0x00
  35. #define CTRL_MASK 0x08
  36. #define CTRL_ALARMEN 0x04
  37. #define CTRL_ALARM 0x02
  38. #define CTRL_TIMER 0x01
  39. static const unsigned short normal_i2c[] = { 0x50, I2C_CLIENT_END };
  40. /* Module parameters */
  41. I2C_CLIENT_INSMOD;
  42. static struct i2c_driver pcf8583_driver;
  43. #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
  44. #define set_ctrl(x, v) get_ctrl(x) = v
  45. #define CMOS_YEAR (64 + 128)
  46. #define CMOS_CHECKSUM (63)
  47. static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
  48. {
  49. unsigned char buf[8], addr[1] = { 1 };
  50. struct i2c_msg msgs[2] = {
  51. {
  52. .addr = client->addr,
  53. .flags = 0,
  54. .len = 1,
  55. .buf = addr,
  56. }, {
  57. .addr = client->addr,
  58. .flags = I2C_M_RD,
  59. .len = 6,
  60. .buf = buf,
  61. }
  62. };
  63. int ret;
  64. memset(buf, 0, sizeof(buf));
  65. ret = i2c_transfer(client->adapter, msgs, 2);
  66. if (ret == 2) {
  67. dt->tm_year = buf[4] >> 6;
  68. dt->tm_wday = buf[5] >> 5;
  69. buf[4] &= 0x3f;
  70. buf[5] &= 0x1f;
  71. dt->tm_sec = BCD2BIN(buf[1]);
  72. dt->tm_min = BCD2BIN(buf[2]);
  73. dt->tm_hour = BCD2BIN(buf[3]);
  74. dt->tm_mday = BCD2BIN(buf[4]);
  75. dt->tm_mon = BCD2BIN(buf[5]) - 1;
  76. }
  77. return ret == 2 ? 0 : -EIO;
  78. }
  79. static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
  80. {
  81. unsigned char buf[8];
  82. int ret, len = 6;
  83. buf[0] = 0;
  84. buf[1] = get_ctrl(client) | 0x80;
  85. buf[2] = 0;
  86. buf[3] = BIN2BCD(dt->tm_sec);
  87. buf[4] = BIN2BCD(dt->tm_min);
  88. buf[5] = BIN2BCD(dt->tm_hour);
  89. if (datetoo) {
  90. len = 8;
  91. buf[6] = BIN2BCD(dt->tm_mday) | (dt->tm_year << 6);
  92. buf[7] = BIN2BCD(dt->tm_mon + 1) | (dt->tm_wday << 5);
  93. }
  94. ret = i2c_master_send(client, (char *)buf, len);
  95. if (ret != len)
  96. return -EIO;
  97. buf[1] = get_ctrl(client);
  98. ret = i2c_master_send(client, (char *)buf, 2);
  99. return ret == 2 ? 0 : -EIO;
  100. }
  101. static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
  102. {
  103. *ctrl = get_ctrl(client);
  104. return 0;
  105. }
  106. static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
  107. {
  108. unsigned char buf[2];
  109. buf[0] = 0;
  110. buf[1] = *ctrl;
  111. set_ctrl(client, *ctrl);
  112. return i2c_master_send(client, (char *)buf, 2);
  113. }
  114. static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
  115. {
  116. unsigned char addr[1];
  117. struct i2c_msg msgs[2] = {
  118. {
  119. .addr = client->addr,
  120. .flags = 0,
  121. .len = 1,
  122. .buf = addr,
  123. }, {
  124. .addr = client->addr,
  125. .flags = I2C_M_RD,
  126. .len = mem->nr,
  127. .buf = mem->data,
  128. }
  129. };
  130. if (mem->loc < 8)
  131. return -EINVAL;
  132. addr[0] = mem->loc;
  133. return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
  134. }
  135. static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
  136. {
  137. unsigned char buf[9];
  138. int ret;
  139. if (mem->loc < 8 || mem->nr > 8)
  140. return -EINVAL;
  141. buf[0] = mem->loc;
  142. memcpy(buf + 1, mem->data, mem->nr);
  143. ret = i2c_master_send(client, buf, mem->nr + 1);
  144. return ret == mem->nr + 1 ? 0 : -EIO;
  145. }
  146. static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
  147. {
  148. struct i2c_client *client = to_i2c_client(dev);
  149. unsigned char ctrl, year[2];
  150. struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
  151. int real_year, year_offset, err;
  152. /*
  153. * Ensure that the RTC is running.
  154. */
  155. pcf8583_get_ctrl(client, &ctrl);
  156. if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
  157. unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
  158. printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
  159. ctrl, new_ctrl);
  160. if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
  161. return err;
  162. }
  163. if (pcf8583_get_datetime(client, tm) ||
  164. pcf8583_read_mem(client, &mem))
  165. return -EIO;
  166. real_year = year[0];
  167. /*
  168. * The RTC year holds the LSB two bits of the current
  169. * year, which should reflect the LSB two bits of the
  170. * CMOS copy of the year. Any difference indicates
  171. * that we have to correct the CMOS version.
  172. */
  173. year_offset = tm->tm_year - (real_year & 3);
  174. if (year_offset < 0)
  175. /*
  176. * RTC year wrapped. Adjust it appropriately.
  177. */
  178. year_offset += 4;
  179. tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
  180. return 0;
  181. }
  182. static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
  183. {
  184. struct i2c_client *client = to_i2c_client(dev);
  185. unsigned char year[2], chk;
  186. struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year };
  187. struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
  188. unsigned int proper_year = tm->tm_year + 1900;
  189. int ret;
  190. /*
  191. * The RTC's own 2-bit year must reflect the least
  192. * significant two bits of the CMOS year.
  193. */
  194. ret = pcf8583_set_datetime(client, tm, 1);
  195. if (ret)
  196. return ret;
  197. ret = pcf8583_read_mem(client, &cmos_check);
  198. if (ret)
  199. return ret;
  200. ret = pcf8583_read_mem(client, &cmos_year);
  201. if (ret)
  202. return ret;
  203. chk -= year[1] + year[0];
  204. year[1] = proper_year / 100;
  205. year[0] = proper_year % 100;
  206. chk += year[1] + year[0];
  207. ret = pcf8583_write_mem(client, &cmos_year);
  208. if (ret)
  209. return ret;
  210. ret = pcf8583_write_mem(client, &cmos_check);
  211. return ret;
  212. }
  213. static const struct rtc_class_ops pcf8583_rtc_ops = {
  214. .read_time = pcf8583_rtc_read_time,
  215. .set_time = pcf8583_rtc_set_time,
  216. };
  217. static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind);
  218. static int pcf8583_attach(struct i2c_adapter *adap)
  219. {
  220. return i2c_probe(adap, &addr_data, pcf8583_probe);
  221. }
  222. static int pcf8583_detach(struct i2c_client *client)
  223. {
  224. int err;
  225. struct pcf8583 *pcf = i2c_get_clientdata(client);
  226. struct rtc_device *rtc = pcf->rtc;
  227. if (rtc)
  228. rtc_device_unregister(rtc);
  229. if ((err = i2c_detach_client(client)))
  230. return err;
  231. kfree(pcf);
  232. return 0;
  233. }
  234. static struct i2c_driver pcf8583_driver = {
  235. .driver = {
  236. .name = "pcf8583",
  237. },
  238. .id = I2C_DRIVERID_PCF8583,
  239. .attach_adapter = pcf8583_attach,
  240. .detach_client = pcf8583_detach,
  241. };
  242. static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind)
  243. {
  244. struct pcf8583 *pcf;
  245. struct i2c_client *client;
  246. struct rtc_device *rtc;
  247. unsigned char buf[1], ad[1] = { 0 };
  248. int err;
  249. struct i2c_msg msgs[2] = {
  250. {
  251. .addr = addr,
  252. .flags = 0,
  253. .len = 1,
  254. .buf = ad,
  255. }, {
  256. .addr = addr,
  257. .flags = I2C_M_RD,
  258. .len = 1,
  259. .buf = buf,
  260. }
  261. };
  262. if (!i2c_check_functionality(adap, I2C_FUNC_I2C))
  263. return 0;
  264. pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
  265. if (!pcf)
  266. return -ENOMEM;
  267. client = &pcf->client;
  268. client->addr = addr;
  269. client->adapter = adap;
  270. client->driver = &pcf8583_driver;
  271. strlcpy(client->name, pcf8583_driver.driver.name, I2C_NAME_SIZE);
  272. if (i2c_transfer(client->adapter, msgs, 2) != 2) {
  273. err = -EIO;
  274. goto exit_kfree;
  275. }
  276. err = i2c_attach_client(client);
  277. if (err)
  278. goto exit_kfree;
  279. rtc = rtc_device_register(pcf8583_driver.driver.name, &client->dev,
  280. &pcf8583_rtc_ops, THIS_MODULE);
  281. if (IS_ERR(rtc)) {
  282. err = PTR_ERR(rtc);
  283. goto exit_detach;
  284. }
  285. pcf->rtc = rtc;
  286. i2c_set_clientdata(client, pcf);
  287. set_ctrl(client, buf[0]);
  288. return 0;
  289. exit_detach:
  290. i2c_detach_client(client);
  291. exit_kfree:
  292. kfree(pcf);
  293. return err;
  294. }
  295. static __init int pcf8583_init(void)
  296. {
  297. return i2c_add_driver(&pcf8583_driver);
  298. }
  299. static __exit void pcf8583_exit(void)
  300. {
  301. i2c_del_driver(&pcf8583_driver);
  302. }
  303. module_init(pcf8583_init);
  304. module_exit(pcf8583_exit);
  305. MODULE_AUTHOR("Russell King");
  306. MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
  307. MODULE_LICENSE("GPL");