rtc-rx8010.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Driver for the Epson RTC module RX-8010 SJ
  3. *
  4. * Copyright(C) Timesys Corporation 2015
  5. * Copyright(C) General Electric Company 2015
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/bcd.h>
  13. #include <linux/bitops.h>
  14. #include <linux/i2c.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/rtc.h>
  18. #define RX8010_SEC 0x10
  19. #define RX8010_MIN 0x11
  20. #define RX8010_HOUR 0x12
  21. #define RX8010_WDAY 0x13
  22. #define RX8010_MDAY 0x14
  23. #define RX8010_MONTH 0x15
  24. #define RX8010_YEAR 0x16
  25. #define RX8010_RESV17 0x17
  26. #define RX8010_ALMIN 0x18
  27. #define RX8010_ALHOUR 0x19
  28. #define RX8010_ALWDAY 0x1A
  29. #define RX8010_TCOUNT0 0x1B
  30. #define RX8010_TCOUNT1 0x1C
  31. #define RX8010_EXT 0x1D
  32. #define RX8010_FLAG 0x1E
  33. #define RX8010_CTRL 0x1F
  34. /* 0x20 to 0x2F are user registers */
  35. #define RX8010_RESV30 0x30
  36. #define RX8010_RESV31 0x31
  37. #define RX8010_IRQ 0x32
  38. #define RX8010_EXT_WADA BIT(3)
  39. #define RX8010_FLAG_VLF BIT(1)
  40. #define RX8010_FLAG_AF BIT(3)
  41. #define RX8010_FLAG_TF BIT(4)
  42. #define RX8010_FLAG_UF BIT(5)
  43. #define RX8010_CTRL_AIE BIT(3)
  44. #define RX8010_CTRL_UIE BIT(5)
  45. #define RX8010_CTRL_STOP BIT(6)
  46. #define RX8010_CTRL_TEST BIT(7)
  47. #define RX8010_ALARM_AE BIT(7)
  48. static const struct i2c_device_id rx8010_id[] = {
  49. { "rx8010", 0 },
  50. { }
  51. };
  52. MODULE_DEVICE_TABLE(i2c, rx8010_id);
  53. static const struct of_device_id rx8010_of_match[] = {
  54. { .compatible = "epson,rx8010" },
  55. { }
  56. };
  57. MODULE_DEVICE_TABLE(of, rx8010_of_match);
  58. struct rx8010_data {
  59. struct i2c_client *client;
  60. struct rtc_device *rtc;
  61. u8 ctrlreg;
  62. };
  63. static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
  64. {
  65. struct i2c_client *client = dev_id;
  66. struct rx8010_data *rx8010 = i2c_get_clientdata(client);
  67. int flagreg;
  68. mutex_lock(&rx8010->rtc->ops_lock);
  69. flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
  70. if (flagreg <= 0) {
  71. mutex_unlock(&rx8010->rtc->ops_lock);
  72. return IRQ_NONE;
  73. }
  74. if (flagreg & RX8010_FLAG_VLF)
  75. dev_warn(&client->dev, "Frequency stop detected\n");
  76. if (flagreg & RX8010_FLAG_TF) {
  77. flagreg &= ~RX8010_FLAG_TF;
  78. rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
  79. }
  80. if (flagreg & RX8010_FLAG_AF) {
  81. flagreg &= ~RX8010_FLAG_AF;
  82. rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
  83. }
  84. if (flagreg & RX8010_FLAG_UF) {
  85. flagreg &= ~RX8010_FLAG_UF;
  86. rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
  87. }
  88. i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
  89. mutex_unlock(&rx8010->rtc->ops_lock);
  90. return IRQ_HANDLED;
  91. }
  92. static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
  93. {
  94. struct rx8010_data *rx8010 = dev_get_drvdata(dev);
  95. u8 date[7];
  96. int flagreg;
  97. int err;
  98. flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
  99. if (flagreg < 0)
  100. return flagreg;
  101. if (flagreg & RX8010_FLAG_VLF) {
  102. dev_warn(dev, "Frequency stop detected\n");
  103. return -EINVAL;
  104. }
  105. err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
  106. 7, date);
  107. if (err != 7)
  108. return err < 0 ? err : -EIO;
  109. dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
  110. dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
  111. dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
  112. dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
  113. dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
  114. dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
  115. dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
  116. return 0;
  117. }
  118. static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
  119. {
  120. struct rx8010_data *rx8010 = dev_get_drvdata(dev);
  121. u8 date[7];
  122. int ctrl, flagreg;
  123. int ret;
  124. if ((dt->tm_year < 100) || (dt->tm_year > 199))
  125. return -EINVAL;
  126. /* set STOP bit before changing clock/calendar */
  127. ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
  128. if (ctrl < 0)
  129. return ctrl;
  130. rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
  131. ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
  132. rx8010->ctrlreg);
  133. if (ret < 0)
  134. return ret;
  135. date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
  136. date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
  137. date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
  138. date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
  139. date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
  140. date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
  141. date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
  142. ret = i2c_smbus_write_i2c_block_data(rx8010->client,
  143. RX8010_SEC, 7, date);
  144. if (ret < 0)
  145. return ret;
  146. /* clear STOP bit after changing clock/calendar */
  147. ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
  148. if (ctrl < 0)
  149. return ctrl;
  150. rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
  151. ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
  152. rx8010->ctrlreg);
  153. if (ret < 0)
  154. return ret;
  155. flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
  156. if (flagreg < 0) {
  157. return flagreg;
  158. }
  159. if (flagreg & RX8010_FLAG_VLF)
  160. ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG,
  161. flagreg & ~RX8010_FLAG_VLF);
  162. return 0;
  163. }
  164. static int rx8010_init_client(struct i2c_client *client)
  165. {
  166. struct rx8010_data *rx8010 = i2c_get_clientdata(client);
  167. u8 ctrl[2];
  168. int need_clear = 0, err = 0;
  169. /* Initialize reserved registers as specified in datasheet */
  170. err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8);
  171. if (err < 0)
  172. return err;
  173. err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00);
  174. if (err < 0)
  175. return err;
  176. err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08);
  177. if (err < 0)
  178. return err;
  179. err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00);
  180. if (err < 0)
  181. return err;
  182. err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG,
  183. 2, ctrl);
  184. if (err != 2)
  185. return err < 0 ? err : -EIO;
  186. if (ctrl[0] & RX8010_FLAG_VLF)
  187. dev_warn(&client->dev, "Frequency stop was detected\n");
  188. if (ctrl[0] & RX8010_FLAG_AF) {
  189. dev_warn(&client->dev, "Alarm was detected\n");
  190. need_clear = 1;
  191. }
  192. if (ctrl[0] & RX8010_FLAG_TF)
  193. need_clear = 1;
  194. if (ctrl[0] & RX8010_FLAG_UF)
  195. need_clear = 1;
  196. if (need_clear) {
  197. ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
  198. err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]);
  199. if (err < 0)
  200. return err;
  201. }
  202. rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
  203. return 0;
  204. }
  205. static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  206. {
  207. struct rx8010_data *rx8010 = dev_get_drvdata(dev);
  208. struct i2c_client *client = rx8010->client;
  209. u8 alarmvals[3];
  210. int flagreg;
  211. int err;
  212. err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals);
  213. if (err != 3)
  214. return err < 0 ? err : -EIO;
  215. flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
  216. if (flagreg < 0)
  217. return flagreg;
  218. t->time.tm_sec = 0;
  219. t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
  220. t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
  221. if (!(alarmvals[2] & RX8010_ALARM_AE))
  222. t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
  223. t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
  224. t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
  225. return 0;
  226. }
  227. static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  228. {
  229. struct i2c_client *client = to_i2c_client(dev);
  230. struct rx8010_data *rx8010 = dev_get_drvdata(dev);
  231. u8 alarmvals[3];
  232. int extreg, flagreg;
  233. int err;
  234. flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
  235. if (flagreg < 0) {
  236. return flagreg;
  237. }
  238. if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
  239. rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
  240. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
  241. rx8010->ctrlreg);
  242. if (err < 0) {
  243. return err;
  244. }
  245. }
  246. flagreg &= ~RX8010_FLAG_AF;
  247. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
  248. if (err < 0)
  249. return err;
  250. alarmvals[0] = bin2bcd(t->time.tm_min);
  251. alarmvals[1] = bin2bcd(t->time.tm_hour);
  252. alarmvals[2] = bin2bcd(t->time.tm_mday);
  253. err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN,
  254. 2, alarmvals);
  255. if (err < 0)
  256. return err;
  257. extreg = i2c_smbus_read_byte_data(client, RX8010_EXT);
  258. if (extreg < 0)
  259. return extreg;
  260. extreg |= RX8010_EXT_WADA;
  261. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
  262. if (err < 0)
  263. return err;
  264. if (alarmvals[2] == 0)
  265. alarmvals[2] |= RX8010_ALARM_AE;
  266. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY,
  267. alarmvals[2]);
  268. if (err < 0)
  269. return err;
  270. if (t->enabled) {
  271. if (rx8010->rtc->uie_rtctimer.enabled)
  272. rx8010->ctrlreg |= RX8010_CTRL_UIE;
  273. if (rx8010->rtc->aie_timer.enabled)
  274. rx8010->ctrlreg |=
  275. (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
  276. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
  277. rx8010->ctrlreg);
  278. if (err < 0)
  279. return err;
  280. }
  281. return 0;
  282. }
  283. static int rx8010_alarm_irq_enable(struct device *dev,
  284. unsigned int enabled)
  285. {
  286. struct i2c_client *client = to_i2c_client(dev);
  287. struct rx8010_data *rx8010 = dev_get_drvdata(dev);
  288. int flagreg;
  289. u8 ctrl;
  290. int err;
  291. ctrl = rx8010->ctrlreg;
  292. if (enabled) {
  293. if (rx8010->rtc->uie_rtctimer.enabled)
  294. ctrl |= RX8010_CTRL_UIE;
  295. if (rx8010->rtc->aie_timer.enabled)
  296. ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
  297. } else {
  298. if (!rx8010->rtc->uie_rtctimer.enabled)
  299. ctrl &= ~RX8010_CTRL_UIE;
  300. if (!rx8010->rtc->aie_timer.enabled)
  301. ctrl &= ~RX8010_CTRL_AIE;
  302. }
  303. flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
  304. if (flagreg < 0)
  305. return flagreg;
  306. flagreg &= ~RX8010_FLAG_AF;
  307. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
  308. if (err < 0)
  309. return err;
  310. if (ctrl != rx8010->ctrlreg) {
  311. rx8010->ctrlreg = ctrl;
  312. err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
  313. rx8010->ctrlreg);
  314. if (err < 0)
  315. return err;
  316. }
  317. return 0;
  318. }
  319. static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  320. {
  321. struct i2c_client *client = to_i2c_client(dev);
  322. struct rx8010_data *rx8010 = dev_get_drvdata(dev);
  323. int ret, tmp;
  324. int flagreg;
  325. switch (cmd) {
  326. case RTC_VL_READ:
  327. flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
  328. if (flagreg < 0)
  329. return flagreg;
  330. tmp = !!(flagreg & RX8010_FLAG_VLF);
  331. if (copy_to_user((void __user *)arg, &tmp, sizeof(int)))
  332. return -EFAULT;
  333. return 0;
  334. case RTC_VL_CLR:
  335. flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
  336. if (flagreg < 0) {
  337. return flagreg;
  338. }
  339. flagreg &= ~RX8010_FLAG_VLF;
  340. ret = i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
  341. if (ret < 0)
  342. return ret;
  343. return 0;
  344. default:
  345. return -ENOIOCTLCMD;
  346. }
  347. }
  348. static struct rtc_class_ops rx8010_rtc_ops = {
  349. .read_time = rx8010_get_time,
  350. .set_time = rx8010_set_time,
  351. .ioctl = rx8010_ioctl,
  352. };
  353. static int rx8010_probe(struct i2c_client *client,
  354. const struct i2c_device_id *id)
  355. {
  356. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  357. struct rx8010_data *rx8010;
  358. int err = 0;
  359. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  360. | I2C_FUNC_SMBUS_I2C_BLOCK)) {
  361. dev_err(&adapter->dev, "doesn't support required functionality\n");
  362. return -EIO;
  363. }
  364. rx8010 = devm_kzalloc(&client->dev, sizeof(struct rx8010_data),
  365. GFP_KERNEL);
  366. if (!rx8010)
  367. return -ENOMEM;
  368. rx8010->client = client;
  369. i2c_set_clientdata(client, rx8010);
  370. err = rx8010_init_client(client);
  371. if (err)
  372. return err;
  373. if (client->irq > 0) {
  374. dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
  375. err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  376. rx8010_irq_1_handler,
  377. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  378. "rx8010", client);
  379. if (err) {
  380. dev_err(&client->dev, "unable to request IRQ\n");
  381. client->irq = 0;
  382. } else {
  383. rx8010_rtc_ops.read_alarm = rx8010_read_alarm;
  384. rx8010_rtc_ops.set_alarm = rx8010_set_alarm;
  385. rx8010_rtc_ops.alarm_irq_enable = rx8010_alarm_irq_enable;
  386. }
  387. }
  388. rx8010->rtc = devm_rtc_device_register(&client->dev, client->name,
  389. &rx8010_rtc_ops, THIS_MODULE);
  390. if (IS_ERR(rx8010->rtc)) {
  391. dev_err(&client->dev, "unable to register the class device\n");
  392. return PTR_ERR(rx8010->rtc);
  393. }
  394. rx8010->rtc->max_user_freq = 1;
  395. return err;
  396. }
  397. static struct i2c_driver rx8010_driver = {
  398. .driver = {
  399. .name = "rtc-rx8010",
  400. .of_match_table = of_match_ptr(rx8010_of_match),
  401. },
  402. .probe = rx8010_probe,
  403. .id_table = rx8010_id,
  404. };
  405. module_i2c_driver(rx8010_driver);
  406. MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
  407. MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
  408. MODULE_LICENSE("GPL v2");