rtc-pcf8583.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * drivers/rtc/rtc-pcf8583.c
  3. *
  4. * Copyright (C) 2000 Russell King
  5. * Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix
  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. * Driver for PCF8583 RTC & RAM chip
  12. *
  13. * Converted to the generic RTC susbsystem by G. Liakhovetski (2006)
  14. */
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/slab.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 rtc_device *rtc;
  29. unsigned char ctrl;
  30. };
  31. #define CTRL_STOP 0x80
  32. #define CTRL_HOLD 0x40
  33. #define CTRL_32KHZ 0x00
  34. #define CTRL_MASK 0x08
  35. #define CTRL_ALARMEN 0x04
  36. #define CTRL_ALARM 0x02
  37. #define CTRL_TIMER 0x01
  38. static struct i2c_driver pcf8583_driver;
  39. #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl
  40. #define set_ctrl(x, v) get_ctrl(x) = v
  41. #define CMOS_YEAR (64 + 128)
  42. #define CMOS_CHECKSUM (63)
  43. static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt)
  44. {
  45. unsigned char buf[8], addr[1] = { 1 };
  46. struct i2c_msg msgs[2] = {
  47. {
  48. .addr = client->addr,
  49. .flags = 0,
  50. .len = 1,
  51. .buf = addr,
  52. }, {
  53. .addr = client->addr,
  54. .flags = I2C_M_RD,
  55. .len = 6,
  56. .buf = buf,
  57. }
  58. };
  59. int ret;
  60. memset(buf, 0, sizeof(buf));
  61. ret = i2c_transfer(client->adapter, msgs, 2);
  62. if (ret == 2) {
  63. dt->tm_year = buf[4] >> 6;
  64. dt->tm_wday = buf[5] >> 5;
  65. buf[4] &= 0x3f;
  66. buf[5] &= 0x1f;
  67. dt->tm_sec = bcd2bin(buf[1]);
  68. dt->tm_min = bcd2bin(buf[2]);
  69. dt->tm_hour = bcd2bin(buf[3]);
  70. dt->tm_mday = bcd2bin(buf[4]);
  71. dt->tm_mon = bcd2bin(buf[5]) - 1;
  72. }
  73. return ret == 2 ? 0 : -EIO;
  74. }
  75. static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo)
  76. {
  77. unsigned char buf[8];
  78. int ret, len = 6;
  79. buf[0] = 0;
  80. buf[1] = get_ctrl(client) | 0x80;
  81. buf[2] = 0;
  82. buf[3] = bin2bcd(dt->tm_sec);
  83. buf[4] = bin2bcd(dt->tm_min);
  84. buf[5] = bin2bcd(dt->tm_hour);
  85. if (datetoo) {
  86. len = 8;
  87. buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6);
  88. buf[7] = bin2bcd(dt->tm_mon + 1) | (dt->tm_wday << 5);
  89. }
  90. ret = i2c_master_send(client, (char *)buf, len);
  91. if (ret != len)
  92. return -EIO;
  93. buf[1] = get_ctrl(client);
  94. ret = i2c_master_send(client, (char *)buf, 2);
  95. return ret == 2 ? 0 : -EIO;
  96. }
  97. static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
  98. {
  99. *ctrl = get_ctrl(client);
  100. return 0;
  101. }
  102. static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
  103. {
  104. unsigned char buf[2];
  105. buf[0] = 0;
  106. buf[1] = *ctrl;
  107. set_ctrl(client, *ctrl);
  108. return i2c_master_send(client, (char *)buf, 2);
  109. }
  110. static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem)
  111. {
  112. unsigned char addr[1];
  113. struct i2c_msg msgs[2] = {
  114. {
  115. .addr = client->addr,
  116. .flags = 0,
  117. .len = 1,
  118. .buf = addr,
  119. }, {
  120. .addr = client->addr,
  121. .flags = I2C_M_RD,
  122. .len = mem->nr,
  123. .buf = mem->data,
  124. }
  125. };
  126. if (mem->loc < 8)
  127. return -EINVAL;
  128. addr[0] = mem->loc;
  129. return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
  130. }
  131. static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem)
  132. {
  133. unsigned char buf[9];
  134. int ret;
  135. if (mem->loc < 8 || mem->nr > 8)
  136. return -EINVAL;
  137. buf[0] = mem->loc;
  138. memcpy(buf + 1, mem->data, mem->nr);
  139. ret = i2c_master_send(client, buf, mem->nr + 1);
  140. return ret == mem->nr + 1 ? 0 : -EIO;
  141. }
  142. static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
  143. {
  144. struct i2c_client *client = to_i2c_client(dev);
  145. unsigned char ctrl, year[2];
  146. struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
  147. int real_year, year_offset, err;
  148. /*
  149. * Ensure that the RTC is running.
  150. */
  151. pcf8583_get_ctrl(client, &ctrl);
  152. if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
  153. unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
  154. printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
  155. ctrl, new_ctrl);
  156. if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
  157. return err;
  158. }
  159. if (pcf8583_get_datetime(client, tm) ||
  160. pcf8583_read_mem(client, &mem))
  161. return -EIO;
  162. real_year = year[0];
  163. /*
  164. * The RTC year holds the LSB two bits of the current
  165. * year, which should reflect the LSB two bits of the
  166. * CMOS copy of the year. Any difference indicates
  167. * that we have to correct the CMOS version.
  168. */
  169. year_offset = tm->tm_year - (real_year & 3);
  170. if (year_offset < 0)
  171. /*
  172. * RTC year wrapped. Adjust it appropriately.
  173. */
  174. year_offset += 4;
  175. tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
  176. return 0;
  177. }
  178. static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
  179. {
  180. struct i2c_client *client = to_i2c_client(dev);
  181. unsigned char year[2], chk;
  182. struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year };
  183. struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
  184. unsigned int proper_year = tm->tm_year + 1900;
  185. int ret;
  186. /*
  187. * The RTC's own 2-bit year must reflect the least
  188. * significant two bits of the CMOS year.
  189. */
  190. ret = pcf8583_set_datetime(client, tm, 1);
  191. if (ret)
  192. return ret;
  193. ret = pcf8583_read_mem(client, &cmos_check);
  194. if (ret)
  195. return ret;
  196. ret = pcf8583_read_mem(client, &cmos_year);
  197. if (ret)
  198. return ret;
  199. chk -= year[1] + year[0];
  200. year[1] = proper_year / 100;
  201. year[0] = proper_year % 100;
  202. chk += year[1] + year[0];
  203. ret = pcf8583_write_mem(client, &cmos_year);
  204. if (ret)
  205. return ret;
  206. ret = pcf8583_write_mem(client, &cmos_check);
  207. return ret;
  208. }
  209. static const struct rtc_class_ops pcf8583_rtc_ops = {
  210. .read_time = pcf8583_rtc_read_time,
  211. .set_time = pcf8583_rtc_set_time,
  212. };
  213. static int pcf8583_probe(struct i2c_client *client,
  214. const struct i2c_device_id *id)
  215. {
  216. struct pcf8583 *pcf8583;
  217. int err;
  218. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  219. return -ENODEV;
  220. pcf8583 = kzalloc(sizeof(struct pcf8583), GFP_KERNEL);
  221. if (!pcf8583)
  222. return -ENOMEM;
  223. i2c_set_clientdata(client, pcf8583);
  224. pcf8583->rtc = rtc_device_register(pcf8583_driver.driver.name,
  225. &client->dev, &pcf8583_rtc_ops, THIS_MODULE);
  226. if (IS_ERR(pcf8583->rtc)) {
  227. err = PTR_ERR(pcf8583->rtc);
  228. goto exit_kfree;
  229. }
  230. return 0;
  231. exit_kfree:
  232. kfree(pcf8583);
  233. return err;
  234. }
  235. static int __devexit pcf8583_remove(struct i2c_client *client)
  236. {
  237. struct pcf8583 *pcf8583 = i2c_get_clientdata(client);
  238. if (pcf8583->rtc)
  239. rtc_device_unregister(pcf8583->rtc);
  240. kfree(pcf8583);
  241. return 0;
  242. }
  243. static const struct i2c_device_id pcf8583_id[] = {
  244. { "pcf8583", 0 },
  245. { }
  246. };
  247. MODULE_DEVICE_TABLE(i2c, pcf8583_id);
  248. static struct i2c_driver pcf8583_driver = {
  249. .driver = {
  250. .name = "pcf8583",
  251. .owner = THIS_MODULE,
  252. },
  253. .probe = pcf8583_probe,
  254. .remove = __devexit_p(pcf8583_remove),
  255. .id_table = pcf8583_id,
  256. };
  257. module_i2c_driver(pcf8583_driver);
  258. MODULE_AUTHOR("Russell King");
  259. MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
  260. MODULE_LICENSE("GPL");