rtc-rx6110.c 12 KB

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