pcf8583.c 4.8 KB

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