rtc-s35390a.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 (which deasserts the irq line) */
  220. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  221. if (err < 0)
  222. return err;
  223. /* clear pending interrupt (in STATUS1 only), 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. err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
  258. if (err < 0)
  259. return err;
  260. if ((bitrev8(sts) & S35390A_INT2_MODE_MASK) != S35390A_INT2_MODE_ALARM) {
  261. /*
  262. * When the alarm isn't enabled, the register to configure
  263. * the alarm time isn't accessible.
  264. */
  265. alm->enabled = 0;
  266. return 0;
  267. } else {
  268. alm->enabled = 1;
  269. }
  270. err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
  271. if (err < 0)
  272. return err;
  273. /* This chip returns the bits of each byte in reverse order */
  274. for (i = 0; i < 3; ++i)
  275. buf[i] = bitrev8(buf[i]);
  276. /*
  277. * B0 of the three matching registers is an enable flag. Iff it is set
  278. * the configured value is used for matching.
  279. */
  280. if (buf[S35390A_ALRM_BYTE_WDAY] & 0x80)
  281. alm->time.tm_wday =
  282. bcd2bin(buf[S35390A_ALRM_BYTE_WDAY] & ~0x80);
  283. if (buf[S35390A_ALRM_BYTE_HOURS] & 0x80)
  284. alm->time.tm_hour =
  285. s35390a_reg2hr(s35390a,
  286. buf[S35390A_ALRM_BYTE_HOURS] & ~0x80);
  287. if (buf[S35390A_ALRM_BYTE_MINS] & 0x80)
  288. alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS] & ~0x80);
  289. /* alarm triggers always at s=0 */
  290. alm->time.tm_sec = 0;
  291. dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
  292. __func__, alm->time.tm_min, alm->time.tm_hour,
  293. alm->time.tm_wday);
  294. return 0;
  295. }
  296. static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
  297. {
  298. return s35390a_read_alarm(to_i2c_client(dev), alm);
  299. }
  300. static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
  301. {
  302. return s35390a_set_alarm(to_i2c_client(dev), alm);
  303. }
  304. static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
  305. {
  306. return s35390a_get_datetime(to_i2c_client(dev), tm);
  307. }
  308. static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
  309. {
  310. return s35390a_set_datetime(to_i2c_client(dev), tm);
  311. }
  312. static const struct rtc_class_ops s35390a_rtc_ops = {
  313. .read_time = s35390a_rtc_read_time,
  314. .set_time = s35390a_rtc_set_time,
  315. .set_alarm = s35390a_rtc_set_alarm,
  316. .read_alarm = s35390a_rtc_read_alarm,
  317. };
  318. static struct i2c_driver s35390a_driver;
  319. static int s35390a_probe(struct i2c_client *client,
  320. const struct i2c_device_id *id)
  321. {
  322. int err, err_reset;
  323. unsigned int i;
  324. struct s35390a *s35390a;
  325. struct rtc_time tm;
  326. char buf, status1;
  327. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  328. err = -ENODEV;
  329. goto exit;
  330. }
  331. s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
  332. GFP_KERNEL);
  333. if (!s35390a) {
  334. err = -ENOMEM;
  335. goto exit;
  336. }
  337. s35390a->client[0] = client;
  338. i2c_set_clientdata(client, s35390a);
  339. /* This chip uses multiple addresses, use dummy devices for them */
  340. for (i = 1; i < 8; ++i) {
  341. s35390a->client[i] = i2c_new_dummy(client->adapter,
  342. client->addr + i);
  343. if (!s35390a->client[i]) {
  344. dev_err(&client->dev, "Address %02x unavailable\n",
  345. client->addr + i);
  346. err = -EBUSY;
  347. goto exit_dummy;
  348. }
  349. }
  350. err_reset = s35390a_reset(s35390a, &status1);
  351. if (err_reset < 0) {
  352. err = err_reset;
  353. dev_err(&client->dev, "error resetting chip\n");
  354. goto exit_dummy;
  355. }
  356. if (status1 & S35390A_FLAG_24H)
  357. s35390a->twentyfourhour = 1;
  358. else
  359. s35390a->twentyfourhour = 0;
  360. if (status1 & S35390A_FLAG_INT2) {
  361. /* disable alarm (and maybe test mode) */
  362. buf = 0;
  363. err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
  364. if (err < 0) {
  365. dev_err(&client->dev, "error disabling alarm");
  366. goto exit_dummy;
  367. }
  368. } else {
  369. err = s35390a_disable_test_mode(s35390a);
  370. if (err < 0) {
  371. dev_err(&client->dev, "error disabling test mode\n");
  372. goto exit_dummy;
  373. }
  374. }
  375. if (err_reset > 0 || s35390a_get_datetime(client, &tm) < 0)
  376. dev_warn(&client->dev, "clock needs to be set\n");
  377. device_set_wakeup_capable(&client->dev, 1);
  378. s35390a->rtc = devm_rtc_device_register(&client->dev,
  379. s35390a_driver.driver.name,
  380. &s35390a_rtc_ops, THIS_MODULE);
  381. if (IS_ERR(s35390a->rtc)) {
  382. err = PTR_ERR(s35390a->rtc);
  383. goto exit_dummy;
  384. }
  385. if (status1 & S35390A_FLAG_INT2)
  386. rtc_update_irq(s35390a->rtc, 1, RTC_AF);
  387. return 0;
  388. exit_dummy:
  389. for (i = 1; i < 8; ++i)
  390. if (s35390a->client[i])
  391. i2c_unregister_device(s35390a->client[i]);
  392. exit:
  393. return err;
  394. }
  395. static int s35390a_remove(struct i2c_client *client)
  396. {
  397. unsigned int i;
  398. struct s35390a *s35390a = i2c_get_clientdata(client);
  399. for (i = 1; i < 8; ++i)
  400. if (s35390a->client[i])
  401. i2c_unregister_device(s35390a->client[i]);
  402. return 0;
  403. }
  404. static struct i2c_driver s35390a_driver = {
  405. .driver = {
  406. .name = "rtc-s35390a",
  407. },
  408. .probe = s35390a_probe,
  409. .remove = s35390a_remove,
  410. .id_table = s35390a_id,
  411. };
  412. module_i2c_driver(s35390a_driver);
  413. MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
  414. MODULE_DESCRIPTION("S35390A RTC driver");
  415. MODULE_LICENSE("GPL");