rtc-pcf8583.c 6.6 KB

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