rtc-pcf8583.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. dev_warn(dev, "resetting control %02x -> %02x\n",
  155. ctrl, new_ctrl);
  156. err = pcf8583_set_ctrl(client, &new_ctrl);
  157. if (err < 0)
  158. return err;
  159. }
  160. if (pcf8583_get_datetime(client, tm) ||
  161. pcf8583_read_mem(client, &mem))
  162. return -EIO;
  163. real_year = year[0];
  164. /*
  165. * The RTC year holds the LSB two bits of the current
  166. * year, which should reflect the LSB two bits of the
  167. * CMOS copy of the year. Any difference indicates
  168. * that we have to correct the CMOS version.
  169. */
  170. year_offset = tm->tm_year - (real_year & 3);
  171. if (year_offset < 0)
  172. /*
  173. * RTC year wrapped. Adjust it appropriately.
  174. */
  175. year_offset += 4;
  176. tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900;
  177. return 0;
  178. }
  179. static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
  180. {
  181. struct i2c_client *client = to_i2c_client(dev);
  182. unsigned char year[2], chk;
  183. struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year };
  184. struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
  185. unsigned int proper_year = tm->tm_year + 1900;
  186. int ret;
  187. /*
  188. * The RTC's own 2-bit year must reflect the least
  189. * significant two bits of the CMOS year.
  190. */
  191. ret = pcf8583_set_datetime(client, tm, 1);
  192. if (ret)
  193. return ret;
  194. ret = pcf8583_read_mem(client, &cmos_check);
  195. if (ret)
  196. return ret;
  197. ret = pcf8583_read_mem(client, &cmos_year);
  198. if (ret)
  199. return ret;
  200. chk -= year[1] + year[0];
  201. year[1] = proper_year / 100;
  202. year[0] = proper_year % 100;
  203. chk += year[1] + year[0];
  204. ret = pcf8583_write_mem(client, &cmos_year);
  205. if (ret)
  206. return ret;
  207. ret = pcf8583_write_mem(client, &cmos_check);
  208. return ret;
  209. }
  210. static const struct rtc_class_ops pcf8583_rtc_ops = {
  211. .read_time = pcf8583_rtc_read_time,
  212. .set_time = pcf8583_rtc_set_time,
  213. };
  214. static int pcf8583_probe(struct i2c_client *client,
  215. const struct i2c_device_id *id)
  216. {
  217. struct pcf8583 *pcf8583;
  218. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  219. return -ENODEV;
  220. pcf8583 = devm_kzalloc(&client->dev, sizeof(struct pcf8583),
  221. GFP_KERNEL);
  222. if (!pcf8583)
  223. return -ENOMEM;
  224. i2c_set_clientdata(client, pcf8583);
  225. pcf8583->rtc = devm_rtc_device_register(&client->dev,
  226. pcf8583_driver.driver.name,
  227. &pcf8583_rtc_ops, THIS_MODULE);
  228. if (IS_ERR(pcf8583->rtc))
  229. return PTR_ERR(pcf8583->rtc);
  230. return 0;
  231. }
  232. static int pcf8583_remove(struct i2c_client *client)
  233. {
  234. return 0;
  235. }
  236. static const struct i2c_device_id pcf8583_id[] = {
  237. { "pcf8583", 0 },
  238. { }
  239. };
  240. MODULE_DEVICE_TABLE(i2c, pcf8583_id);
  241. static struct i2c_driver pcf8583_driver = {
  242. .driver = {
  243. .name = "pcf8583",
  244. .owner = THIS_MODULE,
  245. },
  246. .probe = pcf8583_probe,
  247. .remove = pcf8583_remove,
  248. .id_table = pcf8583_id,
  249. };
  250. module_i2c_driver(pcf8583_driver);
  251. MODULE_AUTHOR("Russell King");
  252. MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
  253. MODULE_LICENSE("GPL");