pcf8583.c 4.7 KB

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