rtc-rx8010.c 12 KB

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