rtc-s35390a.c 13 KB

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