rtc-pcf8583.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/mc146818rtc.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 unsigned short normal_i2c[] = { 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 = BCD_TO_BIN(buf[1]);
  72. dt->tm_min = BCD_TO_BIN(buf[2]);
  73. dt->tm_hour = BCD_TO_BIN(buf[3]);
  74. dt->tm_mday = BCD_TO_BIN(buf[4]);
  75. dt->tm_mon = BCD_TO_BIN(buf[5]);
  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] = BIN_TO_BCD(dt->tm_sec);
  87. buf[4] = BIN_TO_BCD(dt->tm_min);
  88. buf[5] = BIN_TO_BCD(dt->tm_hour);
  89. if (datetoo) {
  90. len = 8;
  91. buf[6] = BIN_TO_BCD(dt->tm_mday) | (dt->tm_year << 6);
  92. buf[7] = BIN_TO_BCD(dt->tm_mon) | (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 addr[1];
  138. struct i2c_msg msgs[2] = {
  139. {
  140. .addr = client->addr,
  141. .flags = 0,
  142. .len = 1,
  143. .buf = addr,
  144. }, {
  145. .addr = client->addr,
  146. .flags = I2C_M_NOSTART,
  147. .len = mem->nr,
  148. .buf = mem->data,
  149. }
  150. };
  151. if (mem->loc < 8)
  152. return -EINVAL;
  153. addr[0] = mem->loc;
  154. return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
  155. }
  156. static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm)
  157. {
  158. struct i2c_client *client = to_i2c_client(dev);
  159. unsigned char ctrl, year[2];
  160. struct rtc_mem mem = { CMOS_YEAR, sizeof(year), year };
  161. int real_year, year_offset, err;
  162. /*
  163. * Ensure that the RTC is running.
  164. */
  165. pcf8583_get_ctrl(client, &ctrl);
  166. if (ctrl & (CTRL_STOP | CTRL_HOLD)) {
  167. unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD);
  168. printk(KERN_WARNING "RTC: resetting control %02x -> %02x\n",
  169. ctrl, new_ctrl);
  170. if ((err = pcf8583_set_ctrl(client, &new_ctrl)) < 0)
  171. return err;
  172. }
  173. if (pcf8583_get_datetime(client, tm) ||
  174. pcf8583_read_mem(client, &mem))
  175. return -EIO;
  176. real_year = year[0];
  177. /*
  178. * The RTC year holds the LSB two bits of the current
  179. * year, which should reflect the LSB two bits of the
  180. * CMOS copy of the year. Any difference indicates
  181. * that we have to correct the CMOS version.
  182. */
  183. year_offset = tm->tm_year - (real_year & 3);
  184. if (year_offset < 0)
  185. /*
  186. * RTC year wrapped. Adjust it appropriately.
  187. */
  188. year_offset += 4;
  189. tm->tm_year = real_year + year_offset + year[1] * 100;
  190. return 0;
  191. }
  192. static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm)
  193. {
  194. struct i2c_client *client = to_i2c_client(dev);
  195. unsigned char year[2], chk;
  196. struct rtc_mem cmos_year = { CMOS_YEAR, sizeof(year), year };
  197. struct rtc_mem cmos_check = { CMOS_CHECKSUM, 1, &chk };
  198. int ret;
  199. /*
  200. * The RTC's own 2-bit year must reflect the least
  201. * significant two bits of the CMOS year.
  202. */
  203. ret = pcf8583_set_datetime(client, tm, 1);
  204. if (ret)
  205. return ret;
  206. ret = pcf8583_read_mem(client, &cmos_check);
  207. if (ret)
  208. return ret;
  209. ret = pcf8583_read_mem(client, &cmos_year);
  210. if (ret)
  211. return ret;
  212. chk -= year[1] + year[0];
  213. year[1] = tm->tm_year / 100;
  214. year[0] = tm->tm_year % 100;
  215. chk += year[1] + year[0];
  216. ret = pcf8583_write_mem(client, &cmos_year);
  217. if (ret)
  218. return ret;
  219. ret = pcf8583_write_mem(client, &cmos_check);
  220. return ret;
  221. }
  222. static const struct rtc_class_ops pcf8583_rtc_ops = {
  223. .read_time = pcf8583_rtc_read_time,
  224. .set_time = pcf8583_rtc_set_time,
  225. };
  226. static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind);
  227. static int pcf8583_attach(struct i2c_adapter *adap)
  228. {
  229. return i2c_probe(adap, &addr_data, pcf8583_probe);
  230. }
  231. static int pcf8583_detach(struct i2c_client *client)
  232. {
  233. int err;
  234. struct pcf8583 *pcf = i2c_get_clientdata(client);
  235. struct rtc_device *rtc = pcf->rtc;
  236. if (rtc)
  237. rtc_device_unregister(rtc);
  238. if ((err = i2c_detach_client(client)))
  239. return err;
  240. kfree(pcf);
  241. return 0;
  242. }
  243. static struct i2c_driver pcf8583_driver = {
  244. .driver = {
  245. .name = "pcf8583",
  246. },
  247. .id = I2C_DRIVERID_PCF8583,
  248. .attach_adapter = pcf8583_attach,
  249. .detach_client = pcf8583_detach,
  250. };
  251. static int pcf8583_probe(struct i2c_adapter *adap, int addr, int kind)
  252. {
  253. struct pcf8583 *pcf;
  254. struct i2c_client *client;
  255. struct rtc_device *rtc;
  256. unsigned char buf[1], ad[1] = { 0 };
  257. int err;
  258. struct i2c_msg msgs[2] = {
  259. {
  260. .addr = addr,
  261. .flags = 0,
  262. .len = 1,
  263. .buf = ad,
  264. }, {
  265. .addr = addr,
  266. .flags = I2C_M_RD,
  267. .len = 1,
  268. .buf = buf,
  269. }
  270. };
  271. pcf = kzalloc(sizeof(*pcf), GFP_KERNEL);
  272. if (!pcf)
  273. return -ENOMEM;
  274. client = &pcf->client;
  275. client->addr = addr;
  276. client->adapter = adap;
  277. client->driver = &pcf8583_driver;
  278. strlcpy(client->name, pcf8583_driver.driver.name, I2C_NAME_SIZE);
  279. if (i2c_transfer(client->adapter, msgs, 2) != 2) {
  280. err = -EIO;
  281. goto exit_kfree;
  282. }
  283. err = i2c_attach_client(client);
  284. if (err)
  285. goto exit_kfree;
  286. rtc = rtc_device_register(pcf8583_driver.driver.name, &client->dev,
  287. &pcf8583_rtc_ops, THIS_MODULE);
  288. if (IS_ERR(rtc)) {
  289. err = PTR_ERR(rtc);
  290. goto exit_detach;
  291. }
  292. pcf->rtc = rtc;
  293. i2c_set_clientdata(client, pcf);
  294. set_ctrl(client, buf[0]);
  295. return 0;
  296. exit_detach:
  297. i2c_detach_client(client);
  298. exit_kfree:
  299. kfree(pcf);
  300. return err;
  301. }
  302. static __init int pcf8583_init(void)
  303. {
  304. return i2c_add_driver(&pcf8583_driver);
  305. }
  306. static __exit void pcf8583_exit(void)
  307. {
  308. i2c_del_driver(&pcf8583_driver);
  309. }
  310. module_init(pcf8583_init);
  311. module_exit(pcf8583_exit);
  312. MODULE_AUTHOR("Russell King");
  313. MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
  314. MODULE_LICENSE("GPL");