rtc-s35390a.c 14 KB

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