rtc-s35390a.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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_rtc_set_time(struct device *dev, struct rtc_time *tm)
  176. {
  177. struct i2c_client *client = to_i2c_client(dev);
  178. struct s35390a *s35390a = i2c_get_clientdata(client);
  179. int i, err;
  180. char buf[7], status;
  181. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
  182. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  183. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  184. tm->tm_wday);
  185. if (s35390a_read_status(s35390a, &status) == 1)
  186. s35390a_init(s35390a);
  187. buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
  188. buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
  189. buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
  190. buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
  191. buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
  192. buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
  193. buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
  194. /* This chip expects the bits of each byte to be in reverse order */
  195. for (i = 0; i < 7; ++i)
  196. buf[i] = bitrev8(buf[i]);
  197. err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  198. return err;
  199. }
  200. static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
  201. {
  202. struct i2c_client *client = to_i2c_client(dev);
  203. struct s35390a *s35390a = i2c_get_clientdata(client);
  204. char buf[7], status;
  205. int i, err;
  206. if (s35390a_read_status(s35390a, &status) == 1)
  207. return -EINVAL;
  208. err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
  209. if (err < 0)
  210. return err;
  211. /* This chip returns the bits of each byte in reverse order */
  212. for (i = 0; i < 7; ++i)
  213. buf[i] = bitrev8(buf[i]);
  214. tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
  215. tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
  216. tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
  217. tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
  218. tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
  219. tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
  220. tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
  221. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
  222. "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
  223. tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
  224. tm->tm_wday);
  225. return 0;
  226. }
  227. static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  228. {
  229. struct i2c_client *client = to_i2c_client(dev);
  230. struct s35390a *s35390a = i2c_get_clientdata(client);
  231. char buf[3], sts = 0;
  232. int err, i;
  233. dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
  234. "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
  235. alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
  236. alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
  237. if (alm->time.tm_sec != 0)
  238. dev_warn(&client->dev, "Alarms are only supported on a per minute basis!\n");
  239. /* disable interrupt (which deasserts the irq line) */
  240. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  241. if (err < 0)
  242. return err;
  243. /* clear pending interrupt (in STATUS1 only), if any */
  244. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
  245. if (err < 0)
  246. return err;
  247. if (alm->enabled)
  248. sts = S35390A_INT2_MODE_ALARM;
  249. else
  250. sts = S35390A_INT2_MODE_NOINTR;
  251. /* This chip expects the bits of each byte to be in reverse order */
  252. sts = bitrev8(sts);
  253. /* set interupt mode*/
  254. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  255. if (err < 0)
  256. return err;
  257. if (alm->time.tm_wday != -1)
  258. buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
  259. else
  260. buf[S35390A_ALRM_BYTE_WDAY] = 0;
  261. buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
  262. alm->time.tm_hour) | 0x80;
  263. buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
  264. if (alm->time.tm_hour >= 12)
  265. buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
  266. for (i = 0; i < 3; ++i)
  267. buf[i] = bitrev8(buf[i]);
  268. err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
  269. sizeof(buf));
  270. return err;
  271. }
  272. static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  273. {
  274. struct i2c_client *client = to_i2c_client(dev);
  275. struct s35390a *s35390a = i2c_get_clientdata(client);
  276. char buf[3], sts;
  277. int i, err;
  278. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  279. if (err < 0)
  280. return err;
  281. if ((bitrev8(sts) & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
  282. /*
  283. * When the alarm isn't enabled, the register to configure
  284. * the alarm time isn't accessible.
  285. */
  286. alm->enabled = 0;
  287. return 0;
  288. } else {
  289. alm->enabled = 1;
  290. }
  291. err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
  292. if (err < 0)
  293. return err;
  294. /* This chip returns the bits of each byte in reverse order */
  295. for (i = 0; i < 3; ++i)
  296. buf[i] = bitrev8(buf[i]);
  297. /*
  298. * B0 of the three matching registers is an enable flag. Iff it is set
  299. * the configured value is used for matching.
  300. */
  301. if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
  302. alm->time.tm_wday =
  303. bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
  304. if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
  305. alm->time.tm_hour =
  306. s35390a_reg2hr(s35390a,
  307. buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
  308. if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
  309. alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
  310. /* alarm triggers always at s=0 */
  311. alm->time.tm_sec = 0;
  312. dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
  313. __func__, alm->time.tm_min, alm->time.tm_hour,
  314. alm->time.tm_wday);
  315. return 0;
  316. }
  317. static int s35390a_rtc_ioctl(struct device *dev, unsigned int cmd,
  318. unsigned long arg)
  319. {
  320. struct i2c_client *client = to_i2c_client(dev);
  321. struct s35390a *s35390a = i2c_get_clientdata(client);
  322. char sts;
  323. int err;
  324. switch (cmd) {
  325. case RTC_VL_READ:
  326. /* s35390a_reset set lowvoltage flag and init RTC if needed */
  327. err = s35390a_read_status(s35390a, &sts);
  328. if (err < 0)
  329. return err;
  330. if (copy_to_user((void __user *)arg, &err, sizeof(int)))
  331. return -EFAULT;
  332. break;
  333. case RTC_VL_CLR:
  334. /* update flag and clear register */
  335. err = s35390a_init(s35390a);
  336. if (err < 0)
  337. return err;
  338. break;
  339. default:
  340. return -ENOIOCTLCMD;
  341. }
  342. return 0;
  343. }
  344. static const struct rtc_class_ops s35390a_rtc_ops = {
  345. .read_time = s35390a_rtc_read_time,
  346. .set_time = s35390a_rtc_set_time,
  347. .set_alarm = s35390a_rtc_set_alarm,
  348. .read_alarm = s35390a_rtc_read_alarm,
  349. .ioctl = s35390a_rtc_ioctl,
  350. };
  351. static struct i2c_driver s35390a_driver;
  352. static int s35390a_probe(struct i2c_client *client,
  353. const struct i2c_device_id *id)
  354. {
  355. int err, err_read;
  356. unsigned int i;
  357. struct s35390a *s35390a;
  358. char buf, status1;
  359. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  360. err = -ENODEV;
  361. goto exit;
  362. }
  363. s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
  364. GFP_KERNEL);
  365. if (!s35390a) {
  366. err = -ENOMEM;
  367. goto exit;
  368. }
  369. s35390a->client[0] = client;
  370. i2c_set_clientdata(client, s35390a);
  371. /* This chip uses multiple addresses, use dummy devices for them */
  372. for (i = 1; i < 8; ++i) {
  373. s35390a->client[i] = i2c_new_dummy(client->adapter,
  374. client->addr + i);
  375. if (!s35390a->client[i]) {
  376. dev_err(&client->dev, "Address %02x unavailable\n",
  377. client->addr + i);
  378. err = -EBUSY;
  379. goto exit_dummy;
  380. }
  381. }
  382. err_read = s35390a_read_status(s35390a, &status1);
  383. if (err_read < 0) {
  384. err = err_read;
  385. dev_err(&client->dev, "error resetting chip\n");
  386. goto exit_dummy;
  387. }
  388. if (status1 & S35390A_FLAG_24H)
  389. s35390a->twentyfourhour = 1;
  390. else
  391. s35390a->twentyfourhour = 0;
  392. if (status1 & S35390A_FLAG_INT2) {
  393. /* disable alarm (and maybe test mode) */
  394. buf = 0;
  395. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
  396. if (err < 0) {
  397. dev_err(&client->dev, "error disabling alarm");
  398. goto exit_dummy;
  399. }
  400. } else {
  401. err = s35390a_disable_test_mode(s35390a);
  402. if (err < 0) {
  403. dev_err(&client->dev, "error disabling test mode\n");
  404. goto exit_dummy;
  405. }
  406. }
  407. device_set_wakeup_capable(&client->dev, 1);
  408. s35390a->rtc = devm_rtc_device_register(&client->dev,
  409. s35390a_driver.driver.name,
  410. &s35390a_rtc_ops, THIS_MODULE);
  411. if (IS_ERR(s35390a->rtc)) {
  412. err = PTR_ERR(s35390a->rtc);
  413. goto exit_dummy;
  414. }
  415. /* supports per-minute alarms only, therefore set uie_unsupported */
  416. s35390a->rtc->uie_unsupported = 1;
  417. if (status1 & S35390A_FLAG_INT2)
  418. rtc_update_irq(s35390a->rtc, 1, RTC_AF);
  419. return 0;
  420. exit_dummy:
  421. for (i = 1; i < 8; ++i)
  422. if (s35390a->client[i])
  423. i2c_unregister_device(s35390a->client[i]);
  424. exit:
  425. return err;
  426. }
  427. static int s35390a_remove(struct i2c_client *client)
  428. {
  429. unsigned int i;
  430. struct s35390a *s35390a = i2c_get_clientdata(client);
  431. for (i = 1; i < 8; ++i)
  432. if (s35390a->client[i])
  433. i2c_unregister_device(s35390a->client[i]);
  434. return 0;
  435. }
  436. static struct i2c_driver s35390a_driver = {
  437. .driver = {
  438. .name = "rtc-s35390a",
  439. .of_match_table = of_match_ptr(s35390a_of_match),
  440. },
  441. .probe = s35390a_probe,
  442. .remove = s35390a_remove,
  443. .id_table = s35390a_id,
  444. };
  445. module_i2c_driver(s35390a_driver);
  446. MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
  447. MODULE_DESCRIPTION("S35390A RTC driver");
  448. MODULE_LICENSE("GPL");