rtc-s35390a.c 13 KB

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