rtc-rx6110.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the Epson RTC module RX-6110 SA
  4. *
  5. * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
  6. * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
  7. */
  8. #include <linux/bcd.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/rtc.h>
  14. #include <linux/of.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/i2c.h>
  17. /* RX-6110 Register definitions */
  18. #define RX6110_REG_SEC 0x10
  19. #define RX6110_REG_MIN 0x11
  20. #define RX6110_REG_HOUR 0x12
  21. #define RX6110_REG_WDAY 0x13
  22. #define RX6110_REG_MDAY 0x14
  23. #define RX6110_REG_MONTH 0x15
  24. #define RX6110_REG_YEAR 0x16
  25. #define RX6110_REG_RES1 0x17
  26. #define RX6110_REG_ALMIN 0x18
  27. #define RX6110_REG_ALHOUR 0x19
  28. #define RX6110_REG_ALWDAY 0x1A
  29. #define RX6110_REG_TCOUNT0 0x1B
  30. #define RX6110_REG_TCOUNT1 0x1C
  31. #define RX6110_REG_EXT 0x1D
  32. #define RX6110_REG_FLAG 0x1E
  33. #define RX6110_REG_CTRL 0x1F
  34. #define RX6110_REG_USER0 0x20
  35. #define RX6110_REG_USER1 0x21
  36. #define RX6110_REG_USER2 0x22
  37. #define RX6110_REG_USER3 0x23
  38. #define RX6110_REG_USER4 0x24
  39. #define RX6110_REG_USER5 0x25
  40. #define RX6110_REG_USER6 0x26
  41. #define RX6110_REG_USER7 0x27
  42. #define RX6110_REG_USER8 0x28
  43. #define RX6110_REG_USER9 0x29
  44. #define RX6110_REG_USERA 0x2A
  45. #define RX6110_REG_USERB 0x2B
  46. #define RX6110_REG_USERC 0x2C
  47. #define RX6110_REG_USERD 0x2D
  48. #define RX6110_REG_USERE 0x2E
  49. #define RX6110_REG_USERF 0x2F
  50. #define RX6110_REG_RES2 0x30
  51. #define RX6110_REG_RES3 0x31
  52. #define RX6110_REG_IRQ 0x32
  53. #define RX6110_BIT_ALARM_EN BIT(7)
  54. /* Extension Register (1Dh) bit positions */
  55. #define RX6110_BIT_EXT_TSEL0 BIT(0)
  56. #define RX6110_BIT_EXT_TSEL1 BIT(1)
  57. #define RX6110_BIT_EXT_TSEL2 BIT(2)
  58. #define RX6110_BIT_EXT_WADA BIT(3)
  59. #define RX6110_BIT_EXT_TE BIT(4)
  60. #define RX6110_BIT_EXT_USEL BIT(5)
  61. #define RX6110_BIT_EXT_FSEL0 BIT(6)
  62. #define RX6110_BIT_EXT_FSEL1 BIT(7)
  63. /* Flag Register (1Eh) bit positions */
  64. #define RX6110_BIT_FLAG_VLF BIT(1)
  65. #define RX6110_BIT_FLAG_AF BIT(3)
  66. #define RX6110_BIT_FLAG_TF BIT(4)
  67. #define RX6110_BIT_FLAG_UF BIT(5)
  68. /* Control Register (1Fh) bit positions */
  69. #define RX6110_BIT_CTRL_TBKE BIT(0)
  70. #define RX6110_BIT_CTRL_TBKON BIT(1)
  71. #define RX6110_BIT_CTRL_TSTP BIT(2)
  72. #define RX6110_BIT_CTRL_AIE BIT(3)
  73. #define RX6110_BIT_CTRL_TIE BIT(4)
  74. #define RX6110_BIT_CTRL_UIE BIT(5)
  75. #define RX6110_BIT_CTRL_STOP BIT(6)
  76. #define RX6110_BIT_CTRL_TEST BIT(7)
  77. enum {
  78. RTC_SEC = 0,
  79. RTC_MIN,
  80. RTC_HOUR,
  81. RTC_WDAY,
  82. RTC_MDAY,
  83. RTC_MONTH,
  84. RTC_YEAR,
  85. RTC_NR_TIME
  86. };
  87. #define RX6110_DRIVER_NAME "rx6110"
  88. struct rx6110_data {
  89. struct rtc_device *rtc;
  90. struct regmap *regmap;
  91. };
  92. /**
  93. * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
  94. *
  95. * @tm: holds date and time
  96. * @data: holds the encoding in rx6110 native form
  97. */
  98. static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
  99. {
  100. pr_debug("%s: date %ptRr\n", __func__, tm);
  101. /*
  102. * The year in the RTC is a value between 0 and 99.
  103. * Assume that this represents the current century
  104. * and disregard all other values.
  105. */
  106. if (tm->tm_year < 100 || tm->tm_year >= 200)
  107. return -EINVAL;
  108. data[RTC_SEC] = bin2bcd(tm->tm_sec);
  109. data[RTC_MIN] = bin2bcd(tm->tm_min);
  110. data[RTC_HOUR] = bin2bcd(tm->tm_hour);
  111. data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
  112. data[RTC_MDAY] = bin2bcd(tm->tm_mday);
  113. data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
  114. data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
  115. return 0;
  116. }
  117. /**
  118. * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
  119. *
  120. * @data: holds the encoding in rx6110 native form
  121. * @tm: holds date and time
  122. */
  123. static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
  124. {
  125. tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
  126. tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
  127. /* only 24-hour clock */
  128. tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
  129. tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
  130. tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
  131. tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
  132. tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
  133. pr_debug("%s: date %ptRr\n", __func__, tm);
  134. /*
  135. * The year in the RTC is a value between 0 and 99.
  136. * Assume that this represents the current century
  137. * and disregard all other values.
  138. */
  139. if (tm->tm_year < 100 || tm->tm_year >= 200)
  140. return -EINVAL;
  141. return 0;
  142. }
  143. /**
  144. * rx6110_set_time - set the current time in the rx6110 registers
  145. *
  146. * @dev: the rtc device in use
  147. * @tm: holds date and time
  148. *
  149. * BUG: The HW assumes every year that is a multiple of 4 to be a leap
  150. * year. Next time this is wrong is 2100, which will not be a leap year
  151. *
  152. * Note: If STOP is not set/cleared, the clock will start when the seconds
  153. * register is written
  154. *
  155. */
  156. static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
  157. {
  158. struct rx6110_data *rx6110 = dev_get_drvdata(dev);
  159. u8 data[RTC_NR_TIME];
  160. int ret;
  161. ret = rx6110_rtc_tm_to_data(tm, data);
  162. if (ret < 0)
  163. return ret;
  164. /* set STOP bit before changing clock/calendar */
  165. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
  166. RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
  167. if (ret)
  168. return ret;
  169. ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
  170. RTC_NR_TIME);
  171. if (ret)
  172. return ret;
  173. /* The time in the RTC is valid. Be sure to have VLF cleared. */
  174. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
  175. RX6110_BIT_FLAG_VLF, 0);
  176. if (ret)
  177. return ret;
  178. /* clear STOP bit after changing clock/calendar */
  179. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
  180. RX6110_BIT_CTRL_STOP, 0);
  181. return ret;
  182. }
  183. /**
  184. * rx6110_get_time - get the current time from the rx6110 registers
  185. * @dev: the rtc device in use
  186. * @tm: holds date and time
  187. */
  188. static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
  189. {
  190. struct rx6110_data *rx6110 = dev_get_drvdata(dev);
  191. u8 data[RTC_NR_TIME];
  192. int flags;
  193. int ret;
  194. ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
  195. if (ret)
  196. return -EINVAL;
  197. /* check for VLF Flag (set at power-on) */
  198. if ((flags & RX6110_BIT_FLAG_VLF)) {
  199. dev_warn(dev, "Voltage low, data is invalid.\n");
  200. return -EINVAL;
  201. }
  202. /* read registers to date */
  203. ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
  204. RTC_NR_TIME);
  205. if (ret)
  206. return ret;
  207. ret = rx6110_data_to_rtc_tm(data, tm);
  208. if (ret)
  209. return ret;
  210. dev_dbg(dev, "%s: date %ptRr\n", __func__, tm);
  211. return 0;
  212. }
  213. static const struct reg_sequence rx6110_default_regs[] = {
  214. { RX6110_REG_RES1, 0xB8 },
  215. { RX6110_REG_RES2, 0x00 },
  216. { RX6110_REG_RES3, 0x10 },
  217. { RX6110_REG_IRQ, 0x00 },
  218. { RX6110_REG_ALMIN, 0x00 },
  219. { RX6110_REG_ALHOUR, 0x00 },
  220. { RX6110_REG_ALWDAY, 0x00 },
  221. };
  222. /**
  223. * rx6110_init - initialize the rx6110 registers
  224. *
  225. * @rx6110: pointer to the rx6110 struct in use
  226. *
  227. */
  228. static int rx6110_init(struct rx6110_data *rx6110)
  229. {
  230. struct rtc_device *rtc = rx6110->rtc;
  231. int flags;
  232. int ret;
  233. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
  234. RX6110_BIT_EXT_TE, 0);
  235. if (ret)
  236. return ret;
  237. ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
  238. ARRAY_SIZE(rx6110_default_regs));
  239. if (ret)
  240. return ret;
  241. ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
  242. if (ret)
  243. return ret;
  244. /* check for VLF Flag (set at power-on) */
  245. if ((flags & RX6110_BIT_FLAG_VLF))
  246. dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
  247. /* check for Alarm Flag */
  248. if (flags & RX6110_BIT_FLAG_AF)
  249. dev_warn(&rtc->dev, "An alarm may have been missed.\n");
  250. /* check for Periodic Timer Flag */
  251. if (flags & RX6110_BIT_FLAG_TF)
  252. dev_warn(&rtc->dev, "Periodic timer was detected\n");
  253. /* check for Update Timer Flag */
  254. if (flags & RX6110_BIT_FLAG_UF)
  255. dev_warn(&rtc->dev, "Update timer was detected\n");
  256. /* clear all flags BUT VLF */
  257. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
  258. RX6110_BIT_FLAG_AF |
  259. RX6110_BIT_FLAG_UF |
  260. RX6110_BIT_FLAG_TF,
  261. 0);
  262. return ret;
  263. }
  264. static const struct rtc_class_ops rx6110_rtc_ops = {
  265. .read_time = rx6110_get_time,
  266. .set_time = rx6110_set_time,
  267. };
  268. static int rx6110_probe(struct rx6110_data *rx6110, struct device *dev)
  269. {
  270. int err;
  271. rx6110->rtc = devm_rtc_device_register(dev,
  272. RX6110_DRIVER_NAME,
  273. &rx6110_rtc_ops, THIS_MODULE);
  274. if (IS_ERR(rx6110->rtc))
  275. return PTR_ERR(rx6110->rtc);
  276. err = rx6110_init(rx6110);
  277. if (err)
  278. return err;
  279. return 0;
  280. }
  281. #if IS_ENABLED(CONFIG_SPI_MASTER)
  282. static const struct regmap_config regmap_spi_config = {
  283. .reg_bits = 8,
  284. .val_bits = 8,
  285. .max_register = RX6110_REG_IRQ,
  286. .read_flag_mask = 0x80,
  287. };
  288. /**
  289. * rx6110_spi_probe - initialize rtc driver
  290. * @spi: pointer to spi device
  291. */
  292. static int rx6110_spi_probe(struct spi_device *spi)
  293. {
  294. struct rx6110_data *rx6110;
  295. if ((spi->bits_per_word && spi->bits_per_word != 8) ||
  296. (spi->max_speed_hz > 2000000) ||
  297. (spi->mode != (SPI_CS_HIGH | SPI_CPOL | SPI_CPHA))) {
  298. dev_warn(&spi->dev, "SPI settings: bits_per_word: %d, max_speed_hz: %d, mode: %xh\n",
  299. spi->bits_per_word, spi->max_speed_hz, spi->mode);
  300. dev_warn(&spi->dev, "driving device in an unsupported mode");
  301. }
  302. rx6110 = devm_kzalloc(&spi->dev, sizeof(*rx6110), GFP_KERNEL);
  303. if (!rx6110)
  304. return -ENOMEM;
  305. rx6110->regmap = devm_regmap_init_spi(spi, &regmap_spi_config);
  306. if (IS_ERR(rx6110->regmap)) {
  307. dev_err(&spi->dev, "regmap init failed for rtc rx6110\n");
  308. return PTR_ERR(rx6110->regmap);
  309. }
  310. spi_set_drvdata(spi, rx6110);
  311. return rx6110_probe(rx6110, &spi->dev);
  312. }
  313. static const struct spi_device_id rx6110_spi_id[] = {
  314. { "rx6110", 0 },
  315. { }
  316. };
  317. MODULE_DEVICE_TABLE(spi, rx6110_spi_id);
  318. static const __maybe_unused struct of_device_id rx6110_spi_of_match[] = {
  319. { .compatible = "epson,rx6110" },
  320. { },
  321. };
  322. MODULE_DEVICE_TABLE(of, rx6110_spi_of_match);
  323. static struct spi_driver rx6110_spi_driver = {
  324. .driver = {
  325. .name = RX6110_DRIVER_NAME,
  326. .of_match_table = of_match_ptr(rx6110_spi_of_match),
  327. },
  328. .probe = rx6110_spi_probe,
  329. .id_table = rx6110_spi_id,
  330. };
  331. static int rx6110_spi_register(void)
  332. {
  333. return spi_register_driver(&rx6110_spi_driver);
  334. }
  335. static void rx6110_spi_unregister(void)
  336. {
  337. spi_unregister_driver(&rx6110_spi_driver);
  338. }
  339. #else
  340. static int rx6110_spi_register(void)
  341. {
  342. return 0;
  343. }
  344. static void rx6110_spi_unregister(void)
  345. {
  346. }
  347. #endif /* CONFIG_SPI_MASTER */
  348. #if IS_ENABLED(CONFIG_I2C)
  349. static const struct regmap_config regmap_i2c_config = {
  350. .reg_bits = 8,
  351. .val_bits = 8,
  352. .max_register = RX6110_REG_IRQ,
  353. .read_flag_mask = 0x80,
  354. };
  355. static int rx6110_i2c_probe(struct i2c_client *client)
  356. {
  357. struct i2c_adapter *adapter = client->adapter;
  358. struct rx6110_data *rx6110;
  359. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  360. | I2C_FUNC_SMBUS_I2C_BLOCK)) {
  361. dev_err(&adapter->dev,
  362. "doesn't support required functionality\n");
  363. return -EIO;
  364. }
  365. rx6110 = devm_kzalloc(&client->dev, sizeof(*rx6110), GFP_KERNEL);
  366. if (!rx6110)
  367. return -ENOMEM;
  368. rx6110->regmap = devm_regmap_init_i2c(client, &regmap_i2c_config);
  369. if (IS_ERR(rx6110->regmap)) {
  370. dev_err(&client->dev, "regmap init failed for rtc rx6110\n");
  371. return PTR_ERR(rx6110->regmap);
  372. }
  373. i2c_set_clientdata(client, rx6110);
  374. return rx6110_probe(rx6110, &client->dev);
  375. }
  376. static const struct acpi_device_id rx6110_i2c_acpi_match[] = {
  377. { "SECC6110" },
  378. { }
  379. };
  380. MODULE_DEVICE_TABLE(acpi, rx6110_i2c_acpi_match);
  381. static const struct i2c_device_id rx6110_i2c_id[] = {
  382. { "rx6110" },
  383. { }
  384. };
  385. MODULE_DEVICE_TABLE(i2c, rx6110_i2c_id);
  386. static struct i2c_driver rx6110_i2c_driver = {
  387. .driver = {
  388. .name = RX6110_DRIVER_NAME,
  389. .acpi_match_table = rx6110_i2c_acpi_match,
  390. },
  391. .probe = rx6110_i2c_probe,
  392. .id_table = rx6110_i2c_id,
  393. };
  394. static int rx6110_i2c_register(void)
  395. {
  396. return i2c_add_driver(&rx6110_i2c_driver);
  397. }
  398. static void rx6110_i2c_unregister(void)
  399. {
  400. i2c_del_driver(&rx6110_i2c_driver);
  401. }
  402. #else
  403. static int rx6110_i2c_register(void)
  404. {
  405. return 0;
  406. }
  407. static void rx6110_i2c_unregister(void)
  408. {
  409. }
  410. #endif /* CONFIG_I2C */
  411. static int __init rx6110_module_init(void)
  412. {
  413. int ret;
  414. ret = rx6110_spi_register();
  415. if (ret)
  416. return ret;
  417. ret = rx6110_i2c_register();
  418. if (ret)
  419. rx6110_spi_unregister();
  420. return ret;
  421. }
  422. module_init(rx6110_module_init);
  423. static void __exit rx6110_module_exit(void)
  424. {
  425. rx6110_spi_unregister();
  426. rx6110_i2c_unregister();
  427. }
  428. module_exit(rx6110_module_exit);
  429. MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
  430. MODULE_DESCRIPTION("RX-6110 SA RTC driver");
  431. MODULE_LICENSE("GPL");