rtc-s35390a.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. #include <linux/delay.h>
  18. #define S35390A_CMD_STATUS1 0
  19. #define S35390A_CMD_STATUS2 1
  20. #define S35390A_CMD_TIME1 2
  21. #define S35390A_CMD_TIME2 3
  22. #define S35390A_CMD_INT2_REG1 5
  23. #define S35390A_BYTE_YEAR 0
  24. #define S35390A_BYTE_MONTH 1
  25. #define S35390A_BYTE_DAY 2
  26. #define S35390A_BYTE_WDAY 3
  27. #define S35390A_BYTE_HOURS 4
  28. #define S35390A_BYTE_MINS 5
  29. #define S35390A_BYTE_SECS 6
  30. #define S35390A_ALRM_BYTE_WDAY 0
  31. #define S35390A_ALRM_BYTE_HOURS 1
  32. #define S35390A_ALRM_BYTE_MINS 2
  33. /* flags for STATUS1 */
  34. #define S35390A_FLAG_POC 0x01
  35. #define S35390A_FLAG_BLD 0x02
  36. #define S35390A_FLAG_INT2 0x04
  37. #define S35390A_FLAG_24H 0x40
  38. #define S35390A_FLAG_RESET 0x80
  39. /* flag for STATUS2 */
  40. #define S35390A_FLAG_TEST 0x01
  41. #define S35390A_INT2_MODE_MASK 0xF0
  42. #define S35390A_INT2_MODE_NOINTR 0x00
  43. #define S35390A_INT2_MODE_FREQ 0x10
  44. #define S35390A_INT2_MODE_ALARM 0x40
  45. #define S35390A_INT2_MODE_PMIN_EDG 0x20
  46. static const struct i2c_device_id s35390a_id[] = {
  47. { "s35390a", 0 },
  48. { }
  49. };
  50. MODULE_DEVICE_TABLE(i2c, s35390a_id);
  51. static const struct of_device_id s35390a_of_match[] = {
  52. { .compatible = "s35390a" },
  53. { .compatible = "sii,s35390a" },
  54. { }
  55. };
  56. MODULE_DEVICE_TABLE(of, s35390a_of_match);
  57. struct s35390a {
  58. struct i2c_client *client[8];
  59. struct rtc_device *rtc;
  60. int twentyfourhour;
  61. };
  62. static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
  63. {
  64. struct i2c_client *client = s35390a->client[reg];
  65. struct i2c_msg msg[] = {
  66. {
  67. .addr = client->addr,
  68. .len = len,
  69. .buf = buf
  70. },
  71. };
  72. if ((i2c_transfer(client->adapter, msg, 1)) != 1)
  73. return -EIO;
  74. return 0;
  75. }
  76. static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
  77. {
  78. struct i2c_client *client = s35390a->client[reg];
  79. struct i2c_msg msg[] = {
  80. {
  81. .addr = client->addr,
  82. .flags = I2C_M_RD,
  83. .len = len,
  84. .buf = buf
  85. },
  86. };
  87. if ((i2c_transfer(client->adapter, msg, 1)) != 1)
  88. return -EIO;
  89. return 0;
  90. }
  91. /*
  92. * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset.
  93. * To keep the information if an irq is pending, pass the value read from
  94. * STATUS1 to the caller.
  95. */
  96. static int s35390a_reset(struct s35390a *s35390a, char *status1)
  97. {
  98. char buf;
  99. int ret;
  100. unsigned initcount = 0;
  101. ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
  102. if (ret < 0)
  103. return ret;
  104. if (*status1 & S35390A_FLAG_POC)
  105. /*
  106. * Do not communicate for 0.5 seconds since the power-on
  107. * detection circuit is in operation.
  108. */
  109. msleep(500);
  110. else if (!(*status1 & S35390A_FLAG_BLD))
  111. /*
  112. * If both POC and BLD are unset everything is fine.
  113. */
  114. return 0;
  115. /*
  116. * At least one of POC and BLD are set, so reinitialise chip. Keeping
  117. * this information in the hardware to know later that the time isn't
  118. * valid is unfortunately not possible because POC and BLD are cleared
  119. * on read. So the reset is best done now.
  120. *
  121. * The 24H bit is kept over reset, so set it already here.
  122. */
  123. initialize:
  124. *status1 = S35390A_FLAG_24H;
  125. buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
  126. ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
  127. if (ret < 0)
  128. return ret;
  129. ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
  130. if (ret < 0)
  131. return ret;
  132. if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) {
  133. /* Try up to five times to reset the chip */
  134. if (initcount < 5) {
  135. ++initcount;
  136. goto initialize;
  137. } else
  138. return -EIO;
  139. }
  140. return 1;
  141. }
  142. static int s35390a_disable_test_mode(struct s35390a *s35390a)
  143. {
  144. char buf[1];
  145. if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
  146. return -EIO;
  147. if (!(buf[0] & S35390A_FLAG_TEST))
  148. return 0;
  149. buf[0] &= ~S35390A_FLAG_TEST;
  150. return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
  151. }
  152. static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
  153. {
  154. if (s35390a->twentyfourhour)
  155. return bin2bcd(hour);
  156. if (hour < 12)
  157. return bin2bcd(hour);
  158. return 0x40 | bin2bcd(hour - 12);
  159. }
  160. static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
  161. {
  162. unsigned hour;
  163. if (s35390a->twentyfourhour)
  164. return bcd2bin(reg & 0x3f);
  165. hour = bcd2bin(reg & 0x3f);
  166. if (reg & 0x40)
  167. hour += 12;
  168. return hour;
  169. }
  170. static int s35390a_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  171. {
  172. struct s35390a *s35390a = i2c_get_clientdata(client);
  173. int i, err;
  174. char buf[7];
  175. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
  176. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  177. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  178. tm->tm_wday);
  179. buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
  180. buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
  181. buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
  182. buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
  183. buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
  184. buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
  185. buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
  186. /* This chip expects the bits of each byte to be in reverse order */
  187. for (i = 0; i < 7; ++i)
  188. buf[i] = bitrev8(buf[i]);
  189. err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  190. return err;
  191. }
  192. static int s35390a_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  193. {
  194. struct s35390a *s35390a = i2c_get_clientdata(client);
  195. char buf[7];
  196. int i, err;
  197. err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  198. if (err < 0)
  199. return err;
  200. /* This chip returns the bits of each byte in reverse order */
  201. for (i = 0; i < 7; ++i)
  202. buf[i] = bitrev8(buf[i]);
  203. tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
  204. tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
  205. tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
  206. tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
  207. tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
  208. tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
  209. tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
  210. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
  211. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  212. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  213. tm->tm_wday);
  214. return rtc_valid_tm(tm);
  215. }
  216. static int s35390a_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alm)
  217. {
  218. struct s35390a *s35390a = i2c_get_clientdata(client);
  219. char buf[3], sts = 0;
  220. int err, i;
  221. dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
  222. "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
  223. alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
  224. alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
  225. /* disable interrupt (which deasserts the irq line) */
  226. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  227. if (err < 0)
  228. return err;
  229. /* clear pending interrupt (in STATUS1 only), if any */
  230. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
  231. if (err < 0)
  232. return err;
  233. if (alm->enabled)
  234. sts = S35390A_INT2_MODE_ALARM;
  235. else
  236. sts = S35390A_INT2_MODE_NOINTR;
  237. /* This chip expects the bits of each byte to be in reverse order */
  238. sts = bitrev8(sts);
  239. /* set interupt mode*/
  240. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  241. if (err < 0)
  242. return err;
  243. if (alm->time.tm_wday != -1)
  244. buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
  245. else
  246. buf[S35390A_ALRM_BYTE_WDAY] = 0;
  247. buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
  248. alm->time.tm_hour) | 0x80;
  249. buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
  250. if (alm->time.tm_hour >= 12)
  251. buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
  252. for (i = 0; i < 3; ++i)
  253. buf[i] = bitrev8(buf[i]);
  254. err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
  255. sizeof(buf));
  256. return err;
  257. }
  258. static int s35390a_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alm)
  259. {
  260. struct s35390a *s35390a = i2c_get_clientdata(client);
  261. char buf[3], sts;
  262. int i, err;
  263. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  264. if (err < 0)
  265. return err;
  266. if ((bitrev8(sts) & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
  267. /*
  268. * When the alarm isn't enabled, the register to configure
  269. * the alarm time isn't accessible.
  270. */
  271. alm->enabled = 0;
  272. return 0;
  273. } else {
  274. alm->enabled = 1;
  275. }
  276. err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
  277. if (err < 0)
  278. return err;
  279. /* This chip returns the bits of each byte in reverse order */
  280. for (i = 0; i < 3; ++i)
  281. buf[i] = bitrev8(buf[i]);
  282. /*
  283. * B0 of the three matching registers is an enable flag. Iff it is set
  284. * the configured value is used for matching.
  285. */
  286. if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
  287. alm->time.tm_wday =
  288. bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
  289. if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
  290. alm->time.tm_hour =
  291. s35390a_reg2hr(s35390a,
  292. buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
  293. if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
  294. alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
  295. /* alarm triggers always at s=0 */
  296. alm->time.tm_sec = 0;
  297. dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
  298. __func__, alm->time.tm_min, alm->time.tm_hour,
  299. alm->time.tm_wday);
  300. return 0;
  301. }
  302. static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  303. {
  304. return s35390a_read_alarm(to_i2c_client(dev), alm);
  305. }
  306. static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  307. {
  308. return s35390a_set_alarm(to_i2c_client(dev), alm);
  309. }
  310. static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
  311. {
  312. return s35390a_get_datetime(to_i2c_client(dev), tm);
  313. }
  314. static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
  315. {
  316. return s35390a_set_datetime(to_i2c_client(dev), tm);
  317. }
  318. static const struct rtc_class_ops s35390a_rtc_ops = {
  319. .read_time = s35390a_rtc_read_time,
  320. .set_time = s35390a_rtc_set_time,
  321. .set_alarm = s35390a_rtc_set_alarm,
  322. .read_alarm = s35390a_rtc_read_alarm,
  323. };
  324. static struct i2c_driver s35390a_driver;
  325. static int s35390a_probe(struct i2c_client *client,
  326. const struct i2c_device_id *id)
  327. {
  328. int err, err_reset;
  329. unsigned int i;
  330. struct s35390a *s35390a;
  331. struct rtc_time tm;
  332. char buf, status1;
  333. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  334. err = -ENODEV;
  335. goto exit;
  336. }
  337. s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
  338. GFP_KERNEL);
  339. if (!s35390a) {
  340. err = -ENOMEM;
  341. goto exit;
  342. }
  343. s35390a->client[0] = client;
  344. i2c_set_clientdata(client, s35390a);
  345. /* This chip uses multiple addresses, use dummy devices for them */
  346. for (i = 1; i < 8; ++i) {
  347. s35390a->client[i] = i2c_new_dummy(client->adapter,
  348. client->addr + i);
  349. if (!s35390a->client[i]) {
  350. dev_err(&client->dev, "Address %02x unavailable\n",
  351. client->addr + i);
  352. err = -EBUSY;
  353. goto exit_dummy;
  354. }
  355. }
  356. err_reset = s35390a_reset(s35390a, &status1);
  357. if (err_reset < 0) {
  358. err = err_reset;
  359. dev_err(&client->dev, "error resetting chip\n");
  360. goto exit_dummy;
  361. }
  362. if (status1 & S35390A_FLAG_24H)
  363. s35390a->twentyfourhour = 1;
  364. else
  365. s35390a->twentyfourhour = 0;
  366. if (status1 & S35390A_FLAG_INT2) {
  367. /* disable alarm (and maybe test mode) */
  368. buf = 0;
  369. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
  370. if (err < 0) {
  371. dev_err(&client->dev, "error disabling alarm");
  372. goto exit_dummy;
  373. }
  374. } else {
  375. err = s35390a_disable_test_mode(s35390a);
  376. if (err < 0) {
  377. dev_err(&client->dev, "error disabling test mode\n");
  378. goto exit_dummy;
  379. }
  380. }
  381. if (err_reset > 0 || s35390a_get_datetime(client, &tm) < 0)
  382. dev_warn(&client->dev, "clock needs to be set\n");
  383. device_set_wakeup_capable(&client->dev, 1);
  384. s35390a->rtc = devm_rtc_device_register(&client->dev,
  385. s35390a_driver.driver.name,
  386. &s35390a_rtc_ops, THIS_MODULE);
  387. if (IS_ERR(s35390a->rtc)) {
  388. err = PTR_ERR(s35390a->rtc);
  389. goto exit_dummy;
  390. }
  391. if (status1 & S35390A_FLAG_INT2)
  392. rtc_update_irq(s35390a->rtc, 1, RTC_AF);
  393. return 0;
  394. exit_dummy:
  395. for (i = 1; i < 8; ++i)
  396. if (s35390a->client[i])
  397. i2c_unregister_device(s35390a->client[i]);
  398. exit:
  399. return err;
  400. }
  401. static int s35390a_remove(struct i2c_client *client)
  402. {
  403. unsigned int i;
  404. struct s35390a *s35390a = i2c_get_clientdata(client);
  405. for (i = 1; i < 8; ++i)
  406. if (s35390a->client[i])
  407. i2c_unregister_device(s35390a->client[i]);
  408. return 0;
  409. }
  410. static struct i2c_driver s35390a_driver = {
  411. .driver = {
  412. .name = "rtc-s35390a",
  413. .of_match_table = of_match_ptr(s35390a_of_match),
  414. },
  415. .probe = s35390a_probe,
  416. .remove = s35390a_remove,
  417. .id_table = s35390a_id,
  418. };
  419. module_i2c_driver(s35390a_driver);
  420. MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
  421. MODULE_DESCRIPTION("S35390A RTC driver");
  422. MODULE_LICENSE("GPL");