rtc-s35390a.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_CMD_TIME2 3
  21. #define S35390A_CMD_INT2_REG1 5
  22. #define S35390A_BYTE_YEAR 0
  23. #define S35390A_BYTE_MONTH 1
  24. #define S35390A_BYTE_DAY 2
  25. #define S35390A_BYTE_WDAY 3
  26. #define S35390A_BYTE_HOURS 4
  27. #define S35390A_BYTE_MINS 5
  28. #define S35390A_BYTE_SECS 6
  29. #define S35390A_ALRM_BYTE_WDAY 0
  30. #define S35390A_ALRM_BYTE_HOURS 1
  31. #define S35390A_ALRM_BYTE_MINS 2
  32. #define S35390A_FLAG_POC 0x01
  33. #define S35390A_FLAG_BLD 0x02
  34. #define S35390A_FLAG_24H 0x40
  35. #define S35390A_FLAG_RESET 0x80
  36. #define S35390A_FLAG_TEST 0x01
  37. #define S35390A_INT2_MODE_MASK 0xF0
  38. #define S35390A_INT2_MODE_NOINTR 0x00
  39. #define S35390A_INT2_MODE_FREQ 0x10
  40. #define S35390A_INT2_MODE_ALARM 0x40
  41. #define S35390A_INT2_MODE_PMIN_EDG 0x20
  42. static const struct i2c_device_id s35390a_id[] = {
  43. { "s35390a", 0 },
  44. { }
  45. };
  46. MODULE_DEVICE_TABLE(i2c, s35390a_id);
  47. struct s35390a {
  48. struct i2c_client *client[8];
  49. struct rtc_device *rtc;
  50. int twentyfourhour;
  51. };
  52. static int s35390a_set_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. {
  57. .addr = client->addr,
  58. .len = len,
  59. .buf = buf
  60. },
  61. };
  62. if ((i2c_transfer(client->adapter, msg, 1)) != 1)
  63. return -EIO;
  64. return 0;
  65. }
  66. static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
  67. {
  68. struct i2c_client *client = s35390a->client[reg];
  69. struct i2c_msg msg[] = {
  70. {
  71. .addr = client->addr,
  72. .flags = I2C_M_RD,
  73. .len = len,
  74. .buf = buf
  75. },
  76. };
  77. if ((i2c_transfer(client->adapter, msg, 1)) != 1)
  78. return -EIO;
  79. return 0;
  80. }
  81. static int s35390a_reset(struct s35390a *s35390a)
  82. {
  83. char buf[1];
  84. if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf)) < 0)
  85. return -EIO;
  86. if (!(buf[0] & (S35390A_FLAG_POC | S35390A_FLAG_BLD)))
  87. return 0;
  88. buf[0] |= (S35390A_FLAG_RESET | S35390A_FLAG_24H);
  89. buf[0] &= 0xf0;
  90. return s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf));
  91. }
  92. static int s35390a_disable_test_mode(struct s35390a *s35390a)
  93. {
  94. char buf[1];
  95. if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
  96. return -EIO;
  97. if (!(buf[0] & S35390A_FLAG_TEST))
  98. return 0;
  99. buf[0] &= ~S35390A_FLAG_TEST;
  100. return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
  101. }
  102. static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
  103. {
  104. if (s35390a->twentyfourhour)
  105. return bin2bcd(hour);
  106. if (hour < 12)
  107. return bin2bcd(hour);
  108. return 0x40 | bin2bcd(hour - 12);
  109. }
  110. static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
  111. {
  112. unsigned hour;
  113. if (s35390a->twentyfourhour)
  114. return bcd2bin(reg & 0x3f);
  115. hour = bcd2bin(reg & 0x3f);
  116. if (reg & 0x40)
  117. hour += 12;
  118. return hour;
  119. }
  120. static int s35390a_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  121. {
  122. struct s35390a *s35390a = i2c_get_clientdata(client);
  123. int i, err;
  124. char buf[7];
  125. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
  126. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  127. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  128. tm->tm_wday);
  129. buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
  130. buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
  131. buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
  132. buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
  133. buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
  134. buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
  135. buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
  136. /* This chip expects the bits of each byte to be in reverse order */
  137. for (i = 0; i < 7; ++i)
  138. buf[i] = bitrev8(buf[i]);
  139. err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  140. return err;
  141. }
  142. static int s35390a_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  143. {
  144. struct s35390a *s35390a = i2c_get_clientdata(client);
  145. char buf[7];
  146. int i, err;
  147. err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  148. if (err < 0)
  149. return err;
  150. /* This chip returns the bits of each byte in reverse order */
  151. for (i = 0; i < 7; ++i)
  152. buf[i] = bitrev8(buf[i]);
  153. tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
  154. tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
  155. tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
  156. tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
  157. tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
  158. tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
  159. tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
  160. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
  161. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  162. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  163. tm->tm_wday);
  164. return rtc_valid_tm(tm);
  165. }
  166. static int s35390a_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alm)
  167. {
  168. struct s35390a *s35390a = i2c_get_clientdata(client);
  169. char buf[3], sts = 0;
  170. int err, i;
  171. dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
  172. "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
  173. alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
  174. alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
  175. /* disable interrupt */
  176. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  177. if (err < 0)
  178. return err;
  179. /* clear pending interrupt, if any */
  180. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
  181. if (err < 0)
  182. return err;
  183. if (alm->enabled)
  184. sts = S35390A_INT2_MODE_ALARM;
  185. else
  186. sts = S35390A_INT2_MODE_NOINTR;
  187. /* This chip expects the bits of each byte to be in reverse order */
  188. sts = bitrev8(sts);
  189. /* set interupt mode*/
  190. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  191. if (err < 0)
  192. return err;
  193. if (alm->time.tm_wday != -1)
  194. buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
  195. buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
  196. alm->time.tm_hour) | 0x80;
  197. buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
  198. if (alm->time.tm_hour >= 12)
  199. buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
  200. for (i = 0; i < 3; ++i)
  201. buf[i] = bitrev8(buf[i]);
  202. err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
  203. sizeof(buf));
  204. return err;
  205. }
  206. static int s35390a_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alm)
  207. {
  208. struct s35390a *s35390a = i2c_get_clientdata(client);
  209. char buf[3], sts;
  210. int i, err;
  211. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  212. if (err < 0)
  213. return err;
  214. if (bitrev8(sts) != S35390A_INT2_MODE_ALARM)
  215. return -EINVAL;
  216. err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
  217. if (err < 0)
  218. return err;
  219. /* This chip returns the bits of each byte in reverse order */
  220. for (i = 0; i < 3; ++i) {
  221. buf[i] = bitrev8(buf[i]);
  222. buf[i] &= ~0x80;
  223. }
  224. alm->time.tm_wday = bcd2bin(buf[S35390A_ALRM_BYTE_WDAY]);
  225. alm->time.tm_hour = s35390a_reg2hr(s35390a,
  226. buf[S35390A_ALRM_BYTE_HOURS]);
  227. alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS]);
  228. dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
  229. __func__, alm->time.tm_min, alm->time.tm_hour,
  230. alm->time.tm_wday);
  231. return 0;
  232. }
  233. static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  234. {
  235. return s35390a_read_alarm(to_i2c_client(dev), alm);
  236. }
  237. static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  238. {
  239. return s35390a_set_alarm(to_i2c_client(dev), alm);
  240. }
  241. static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
  242. {
  243. return s35390a_get_datetime(to_i2c_client(dev), tm);
  244. }
  245. static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
  246. {
  247. return s35390a_set_datetime(to_i2c_client(dev), tm);
  248. }
  249. static const struct rtc_class_ops s35390a_rtc_ops = {
  250. .read_time = s35390a_rtc_read_time,
  251. .set_time = s35390a_rtc_set_time,
  252. .set_alarm = s35390a_rtc_set_alarm,
  253. .read_alarm = s35390a_rtc_read_alarm,
  254. };
  255. static struct i2c_driver s35390a_driver;
  256. static int s35390a_probe(struct i2c_client *client,
  257. const struct i2c_device_id *id)
  258. {
  259. int err;
  260. unsigned int i;
  261. struct s35390a *s35390a;
  262. struct rtc_time tm;
  263. char buf[1];
  264. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  265. err = -ENODEV;
  266. goto exit;
  267. }
  268. s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
  269. GFP_KERNEL);
  270. if (!s35390a) {
  271. err = -ENOMEM;
  272. goto exit;
  273. }
  274. s35390a->client[0] = client;
  275. i2c_set_clientdata(client, s35390a);
  276. /* This chip uses multiple addresses, use dummy devices for them */
  277. for (i = 1; i < 8; ++i) {
  278. s35390a->client[i] = i2c_new_dummy(client->adapter,
  279. client->addr + i);
  280. if (!s35390a->client[i]) {
  281. dev_err(&client->dev, "Address %02x unavailable\n",
  282. client->addr + i);
  283. err = -EBUSY;
  284. goto exit_dummy;
  285. }
  286. }
  287. err = s35390a_reset(s35390a);
  288. if (err < 0) {
  289. dev_err(&client->dev, "error resetting chip\n");
  290. goto exit_dummy;
  291. }
  292. err = s35390a_disable_test_mode(s35390a);
  293. if (err < 0) {
  294. dev_err(&client->dev, "error disabling test mode\n");
  295. goto exit_dummy;
  296. }
  297. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, buf, sizeof(buf));
  298. if (err < 0) {
  299. dev_err(&client->dev, "error checking 12/24 hour mode\n");
  300. goto exit_dummy;
  301. }
  302. if (buf[0] & S35390A_FLAG_24H)
  303. s35390a->twentyfourhour = 1;
  304. else
  305. s35390a->twentyfourhour = 0;
  306. if (s35390a_get_datetime(client, &tm) < 0)
  307. dev_warn(&client->dev, "clock needs to be set\n");
  308. device_set_wakeup_capable(&client->dev, 1);
  309. s35390a->rtc = devm_rtc_device_register(&client->dev,
  310. s35390a_driver.driver.name,
  311. &s35390a_rtc_ops, THIS_MODULE);
  312. if (IS_ERR(s35390a->rtc)) {
  313. err = PTR_ERR(s35390a->rtc);
  314. goto exit_dummy;
  315. }
  316. return 0;
  317. exit_dummy:
  318. for (i = 1; i < 8; ++i)
  319. if (s35390a->client[i])
  320. i2c_unregister_device(s35390a->client[i]);
  321. exit:
  322. return err;
  323. }
  324. static int s35390a_remove(struct i2c_client *client)
  325. {
  326. unsigned int i;
  327. struct s35390a *s35390a = i2c_get_clientdata(client);
  328. for (i = 1; i < 8; ++i)
  329. if (s35390a->client[i])
  330. i2c_unregister_device(s35390a->client[i]);
  331. return 0;
  332. }
  333. static struct i2c_driver s35390a_driver = {
  334. .driver = {
  335. .name = "rtc-s35390a",
  336. },
  337. .probe = s35390a_probe,
  338. .remove = s35390a_remove,
  339. .id_table = s35390a_id,
  340. };
  341. module_i2c_driver(s35390a_driver);
  342. MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
  343. MODULE_DESCRIPTION("S35390A RTC driver");
  344. MODULE_LICENSE("GPL");