pcf8583.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * linux/drivers/acorn/char/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. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/slab.h>
  15. #include <linux/string.h>
  16. #include <linux/mc146818rtc.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/bcd.h>
  20. #include "pcf8583.h"
  21. static struct i2c_driver pcf8583_driver;
  22. static unsigned short ignore[] = { I2C_CLIENT_END };
  23. static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END };
  24. static unsigned short *forces[] = { NULL };
  25. static struct i2c_client_address_data addr_data = {
  26. .normal_i2c = normal_addr,
  27. .probe = ignore,
  28. .ignore = ignore,
  29. .forces = forces,
  30. };
  31. #define set_ctrl(x, v) i2c_set_clientdata(x, (void *)(unsigned int)(v))
  32. #define get_ctrl(x) ((unsigned int)i2c_get_clientdata(x))
  33. static int
  34. pcf8583_attach(struct i2c_adapter *adap, int addr, int kind)
  35. {
  36. struct i2c_client *c;
  37. unsigned char buf[1], ad[1] = { 0 };
  38. struct i2c_msg msgs[2] = {
  39. {
  40. .addr = addr,
  41. .flags = 0,
  42. .len = 1,
  43. .buf = ad,
  44. }, {
  45. .addr = addr,
  46. .flags = I2C_M_RD,
  47. .len = 1,
  48. .buf = buf,
  49. }
  50. };
  51. c = kmalloc(sizeof(*c), GFP_KERNEL);
  52. if (!c)
  53. return -ENOMEM;
  54. memset(c, 0, sizeof(*c));
  55. c->addr = addr;
  56. c->adapter = adap;
  57. c->driver = &pcf8583_driver;
  58. if (i2c_transfer(c->adapter, msgs, 2) == 2)
  59. set_ctrl(c, buf[0]);
  60. return i2c_attach_client(c);
  61. }
  62. static int
  63. pcf8583_probe(struct i2c_adapter *adap)
  64. {
  65. return i2c_probe(adap, &addr_data, pcf8583_attach);
  66. }
  67. static int
  68. pcf8583_detach(struct i2c_client *client)
  69. {
  70. i2c_detach_client(client);
  71. kfree(client);
  72. return 0;
  73. }
  74. static int
  75. pcf8583_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
  76. {
  77. unsigned char buf[8], addr[1] = { 1 };
  78. struct i2c_msg msgs[2] = {
  79. {
  80. .addr = client->addr,
  81. .flags = 0,
  82. .len = 1,
  83. .buf = addr,
  84. }, {
  85. .addr = client->addr,
  86. .flags = I2C_M_RD,
  87. .len = 6,
  88. .buf = buf,
  89. }
  90. };
  91. int ret = -EIO;
  92. memset(buf, 0, sizeof(buf));
  93. ret = i2c_transfer(client->adapter, msgs, 2);
  94. if (ret == 2) {
  95. dt->year_off = buf[4] >> 6;
  96. dt->wday = buf[5] >> 5;
  97. buf[4] &= 0x3f;
  98. buf[5] &= 0x1f;
  99. dt->cs = BCD_TO_BIN(buf[0]);
  100. dt->secs = BCD_TO_BIN(buf[1]);
  101. dt->mins = BCD_TO_BIN(buf[2]);
  102. dt->hours = BCD_TO_BIN(buf[3]);
  103. dt->mday = BCD_TO_BIN(buf[4]);
  104. dt->mon = BCD_TO_BIN(buf[5]);
  105. ret = 0;
  106. }
  107. return ret;
  108. }
  109. static int
  110. pcf8583_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
  111. {
  112. unsigned char buf[8];
  113. int ret, len = 6;
  114. buf[0] = 0;
  115. buf[1] = get_ctrl(client) | 0x80;
  116. buf[2] = BIN_TO_BCD(dt->cs);
  117. buf[3] = BIN_TO_BCD(dt->secs);
  118. buf[4] = BIN_TO_BCD(dt->mins);
  119. buf[5] = BIN_TO_BCD(dt->hours);
  120. if (datetoo) {
  121. len = 8;
  122. buf[6] = BIN_TO_BCD(dt->mday) | (dt->year_off << 6);
  123. buf[7] = BIN_TO_BCD(dt->mon) | (dt->wday << 5);
  124. }
  125. ret = i2c_master_send(client, (char *)buf, len);
  126. if (ret == len)
  127. ret = 0;
  128. buf[1] = get_ctrl(client);
  129. i2c_master_send(client, (char *)buf, 2);
  130. return ret;
  131. }
  132. static int
  133. pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
  134. {
  135. *ctrl = get_ctrl(client);
  136. return 0;
  137. }
  138. static int
  139. pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
  140. {
  141. unsigned char buf[2];
  142. buf[0] = 0;
  143. buf[1] = *ctrl;
  144. set_ctrl(client, *ctrl);
  145. return i2c_master_send(client, (char *)buf, 2);
  146. }
  147. static int
  148. pcf8583_read_mem(struct i2c_client *client, struct mem *mem)
  149. {
  150. unsigned char addr[1];
  151. struct i2c_msg msgs[2] = {
  152. {
  153. .addr = client->addr,
  154. .flags = 0,
  155. .len = 1,
  156. .buf = addr,
  157. }, {
  158. .addr = client->addr,
  159. .flags = I2C_M_RD,
  160. .len = mem->nr,
  161. .buf = mem->data,
  162. }
  163. };
  164. if (mem->loc < 8)
  165. return -EINVAL;
  166. addr[0] = mem->loc;
  167. return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
  168. }
  169. static int
  170. pcf8583_write_mem(struct i2c_client *client, struct mem *mem)
  171. {
  172. unsigned char addr[1];
  173. struct i2c_msg msgs[2] = {
  174. {
  175. .addr = client->addr,
  176. .flags = 0,
  177. .len = 1,
  178. .buf = addr,
  179. }, {
  180. .addr = client->addr,
  181. .flags = I2C_M_NOSTART,
  182. .len = mem->nr,
  183. .buf = mem->data,
  184. }
  185. };
  186. if (mem->loc < 8)
  187. return -EINVAL;
  188. addr[0] = mem->loc;
  189. return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
  190. }
  191. static int
  192. pcf8583_command(struct i2c_client *client, unsigned int cmd, void *arg)
  193. {
  194. switch (cmd) {
  195. case RTC_GETDATETIME:
  196. return pcf8583_get_datetime(client, arg);
  197. case RTC_SETTIME:
  198. return pcf8583_set_datetime(client, arg, 0);
  199. case RTC_SETDATETIME:
  200. return pcf8583_set_datetime(client, arg, 1);
  201. case RTC_GETCTRL:
  202. return pcf8583_get_ctrl(client, arg);
  203. case RTC_SETCTRL:
  204. return pcf8583_set_ctrl(client, arg);
  205. case MEM_READ:
  206. return pcf8583_read_mem(client, arg);
  207. case MEM_WRITE:
  208. return pcf8583_write_mem(client, arg);
  209. default:
  210. return -EINVAL;
  211. }
  212. }
  213. static struct i2c_driver pcf8583_driver = {
  214. .driver = {
  215. .name = "PCF8583",
  216. },
  217. .id = I2C_DRIVERID_PCF8583,
  218. .attach_adapter = pcf8583_probe,
  219. .detach_client = pcf8583_detach,
  220. .command = pcf8583_command
  221. };
  222. static __init int pcf8583_init(void)
  223. {
  224. return i2c_add_driver(&pcf8583_driver);
  225. }
  226. static __exit void pcf8583_exit(void)
  227. {
  228. i2c_del_driver(&pcf8583_driver);
  229. }
  230. module_init(pcf8583_init);
  231. module_exit(pcf8583_exit);
  232. MODULE_AUTHOR("Russell King");
  233. MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
  234. MODULE_LICENSE("GPL");