rtc-pcf8583.c 6.4 KB

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