pcf8583.c 4.8 KB

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