rtc-s35390a.c 13 KB

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