rtc-s35390a.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Seiko Instruments S-35390A RTC Driver
  3. *
  4. * Copyright (c) 2007 Byron Bradley
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/rtc.h>
  13. #include <linux/i2c.h>
  14. #include <linux/bitrev.h>
  15. #include <linux/bcd.h>
  16. #include <linux/slab.h>
  17. #define S35390A_CMD_STATUS1 0
  18. #define S35390A_CMD_STATUS2 1
  19. #define S35390A_CMD_TIME1 2
  20. #define S35390A_BYTE_YEAR 0
  21. #define S35390A_BYTE_MONTH 1
  22. #define S35390A_BYTE_DAY 2
  23. #define S35390A_BYTE_WDAY 3
  24. #define S35390A_BYTE_HOURS 4
  25. #define S35390A_BYTE_MINS 5
  26. #define S35390A_BYTE_SECS 6
  27. #define S35390A_FLAG_POC 0x01
  28. #define S35390A_FLAG_BLD 0x02
  29. #define S35390A_FLAG_24H 0x40
  30. #define S35390A_FLAG_RESET 0x80
  31. #define S35390A_FLAG_TEST 0x01
  32. static const struct i2c_device_id s35390a_id[] = {
  33. { "s35390a", 0 },
  34. { }
  35. };
  36. MODULE_DEVICE_TABLE(i2c, s35390a_id);
  37. struct s35390a {
  38. struct i2c_client *client[8];
  39. struct rtc_device *rtc;
  40. int twentyfourhour;
  41. };
  42. static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
  43. {
  44. struct i2c_client *client = s35390a->client[reg];
  45. struct i2c_msg msg[] = {
  46. { client->addr, 0, len, buf },
  47. };
  48. if ((i2c_transfer(client->adapter, msg, 1)) != 1)
  49. return -EIO;
  50. return 0;
  51. }
  52. static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
  53. {
  54. struct i2c_client *client = s35390a->client[reg];
  55. struct i2c_msg msg[] = {
  56. { client->addr, I2C_M_RD, len, buf },
  57. };
  58. if ((i2c_transfer(client->adapter, msg, 1)) != 1)
  59. return -EIO;
  60. return 0;
  61. }
  62. static int s35390a_reset(struct s35390a *s35390a)
  63. {
  64. char buf[1];
  65. if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf)) < 0)
  66. return -EIO;
  67. if (!(buf[0] & (S35390A_FLAG_POC | S35390A_FLAG_BLD)))
  68. return 0;
  69. buf[0] |= (S35390A_FLAG_RESET | S35390A_FLAG_24H);
  70. buf[0] &= 0xf0;
  71. return s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf));
  72. }
  73. static int s35390a_disable_test_mode(struct s35390a *s35390a)
  74. {
  75. char buf[1];
  76. if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
  77. return -EIO;
  78. if (!(buf[0] & S35390A_FLAG_TEST))
  79. return 0;
  80. buf[0] &= ~S35390A_FLAG_TEST;
  81. return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
  82. }
  83. static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
  84. {
  85. if (s35390a->twentyfourhour)
  86. return bin2bcd(hour);
  87. if (hour < 12)
  88. return bin2bcd(hour);
  89. return 0x40 | bin2bcd(hour - 12);
  90. }
  91. static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
  92. {
  93. unsigned hour;
  94. if (s35390a->twentyfourhour)
  95. return bcd2bin(reg & 0x3f);
  96. hour = bcd2bin(reg & 0x3f);
  97. if (reg & 0x40)
  98. hour += 12;
  99. return hour;
  100. }
  101. static int s35390a_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  102. {
  103. struct s35390a *s35390a = i2c_get_clientdata(client);
  104. int i, err;
  105. char buf[7];
  106. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
  107. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  108. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  109. tm->tm_wday);
  110. buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
  111. buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
  112. buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
  113. buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
  114. buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
  115. buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
  116. buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
  117. /* This chip expects the bits of each byte to be in reverse order */
  118. for (i = 0; i < 7; ++i)
  119. buf[i] = bitrev8(buf[i]);
  120. err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  121. return err;
  122. }
  123. static int s35390a_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  124. {
  125. struct s35390a *s35390a = i2c_get_clientdata(client);
  126. char buf[7];
  127. int i, err;
  128. err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  129. if (err < 0)
  130. return err;
  131. /* This chip returns the bits of each byte in reverse order */
  132. for (i = 0; i < 7; ++i)
  133. buf[i] = bitrev8(buf[i]);
  134. tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
  135. tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
  136. tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
  137. tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
  138. tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
  139. tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
  140. tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
  141. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
  142. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  143. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  144. tm->tm_wday);
  145. return rtc_valid_tm(tm);
  146. }
  147. static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
  148. {
  149. return s35390a_get_datetime(to_i2c_client(dev), tm);
  150. }
  151. static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
  152. {
  153. return s35390a_set_datetime(to_i2c_client(dev), tm);
  154. }
  155. static const struct rtc_class_ops s35390a_rtc_ops = {
  156. .read_time = s35390a_rtc_read_time,
  157. .set_time = s35390a_rtc_set_time,
  158. };
  159. static struct i2c_driver s35390a_driver;
  160. static int s35390a_probe(struct i2c_client *client,
  161. const struct i2c_device_id *id)
  162. {
  163. int err;
  164. unsigned int i;
  165. struct s35390a *s35390a;
  166. struct rtc_time tm;
  167. char buf[1];
  168. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  169. err = -ENODEV;
  170. goto exit;
  171. }
  172. s35390a = kzalloc(sizeof(struct s35390a), GFP_KERNEL);
  173. if (!s35390a) {
  174. err = -ENOMEM;
  175. goto exit;
  176. }
  177. s35390a->client[0] = client;
  178. i2c_set_clientdata(client, s35390a);
  179. /* This chip uses multiple addresses, use dummy devices for them */
  180. for (i = 1; i < 8; ++i) {
  181. s35390a->client[i] = i2c_new_dummy(client->adapter,
  182. client->addr + i);
  183. if (!s35390a->client[i]) {
  184. dev_err(&client->dev, "Address %02x unavailable\n",
  185. client->addr + i);
  186. err = -EBUSY;
  187. goto exit_dummy;
  188. }
  189. }
  190. err = s35390a_reset(s35390a);
  191. if (err < 0) {
  192. dev_err(&client->dev, "error resetting chip\n");
  193. goto exit_dummy;
  194. }
  195. err = s35390a_disable_test_mode(s35390a);
  196. if (err < 0) {
  197. dev_err(&client->dev, "error disabling test mode\n");
  198. goto exit_dummy;
  199. }
  200. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf));
  201. if (err < 0) {
  202. dev_err(&client->dev, "error checking 12/24 hour mode\n");
  203. goto exit_dummy;
  204. }
  205. if (buf[0] & S35390A_FLAG_24H)
  206. s35390a->twentyfourhour = 1;
  207. else
  208. s35390a->twentyfourhour = 0;
  209. if (s35390a_get_datetime(client, &tm) < 0)
  210. dev_warn(&client->dev, "clock needs to be set\n");
  211. s35390a->rtc = rtc_device_register(s35390a_driver.driver.name,
  212. &client->dev, &s35390a_rtc_ops, THIS_MODULE);
  213. if (IS_ERR(s35390a->rtc)) {
  214. err = PTR_ERR(s35390a->rtc);
  215. goto exit_dummy;
  216. }
  217. return 0;
  218. exit_dummy:
  219. for (i = 1; i < 8; ++i)
  220. if (s35390a->client[i])
  221. i2c_unregister_device(s35390a->client[i]);
  222. kfree(s35390a);
  223. exit:
  224. return err;
  225. }
  226. static int s35390a_remove(struct i2c_client *client)
  227. {
  228. unsigned int i;
  229. struct s35390a *s35390a = i2c_get_clientdata(client);
  230. for (i = 1; i < 8; ++i)
  231. if (s35390a->client[i])
  232. i2c_unregister_device(s35390a->client[i]);
  233. rtc_device_unregister(s35390a->rtc);
  234. kfree(s35390a);
  235. return 0;
  236. }
  237. static struct i2c_driver s35390a_driver = {
  238. .driver = {
  239. .name = "rtc-s35390a",
  240. },
  241. .probe = s35390a_probe,
  242. .remove = s35390a_remove,
  243. .id_table = s35390a_id,
  244. };
  245. module_i2c_driver(s35390a_driver);
  246. MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
  247. MODULE_DESCRIPTION("S35390A RTC driver");
  248. MODULE_LICENSE("GPL");