rtc-rx6110.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Driver for the Epson RTC module RX-6110 SA
  3. *
  4. * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
  5. * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
  6. *
  7. * This driver software is distributed as is, without any warranty of any kind,
  8. * either express or implied as further specified in the GNU Public License.
  9. * This software may be used and distributed according to the terms of the GNU
  10. * Public License, version 2 as published by the Free Software Foundation.
  11. * See the file COPYING in the main directory of this archive for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/bcd.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/regmap.h>
  22. #include <linux/rtc.h>
  23. #include <linux/spi/spi.h>
  24. /* RX-6110 Register definitions */
  25. #define RX6110_REG_SEC 0x10
  26. #define RX6110_REG_MIN 0x11
  27. #define RX6110_REG_HOUR 0x12
  28. #define RX6110_REG_WDAY 0x13
  29. #define RX6110_REG_MDAY 0x14
  30. #define RX6110_REG_MONTH 0x15
  31. #define RX6110_REG_YEAR 0x16
  32. #define RX6110_REG_RES1 0x17
  33. #define RX6110_REG_ALMIN 0x18
  34. #define RX6110_REG_ALHOUR 0x19
  35. #define RX6110_REG_ALWDAY 0x1A
  36. #define RX6110_REG_TCOUNT0 0x1B
  37. #define RX6110_REG_TCOUNT1 0x1C
  38. #define RX6110_REG_EXT 0x1D
  39. #define RX6110_REG_FLAG 0x1E
  40. #define RX6110_REG_CTRL 0x1F
  41. #define RX6110_REG_USER0 0x20
  42. #define RX6110_REG_USER1 0x21
  43. #define RX6110_REG_USER2 0x22
  44. #define RX6110_REG_USER3 0x23
  45. #define RX6110_REG_USER4 0x24
  46. #define RX6110_REG_USER5 0x25
  47. #define RX6110_REG_USER6 0x26
  48. #define RX6110_REG_USER7 0x27
  49. #define RX6110_REG_USER8 0x28
  50. #define RX6110_REG_USER9 0x29
  51. #define RX6110_REG_USERA 0x2A
  52. #define RX6110_REG_USERB 0x2B
  53. #define RX6110_REG_USERC 0x2C
  54. #define RX6110_REG_USERD 0x2D
  55. #define RX6110_REG_USERE 0x2E
  56. #define RX6110_REG_USERF 0x2F
  57. #define RX6110_REG_RES2 0x30
  58. #define RX6110_REG_RES3 0x31
  59. #define RX6110_REG_IRQ 0x32
  60. #define RX6110_BIT_ALARM_EN BIT(7)
  61. /* Extension Register (1Dh) bit positions */
  62. #define RX6110_BIT_EXT_TSEL0 BIT(0)
  63. #define RX6110_BIT_EXT_TSEL1 BIT(1)
  64. #define RX6110_BIT_EXT_TSEL2 BIT(2)
  65. #define RX6110_BIT_EXT_WADA BIT(3)
  66. #define RX6110_BIT_EXT_TE BIT(4)
  67. #define RX6110_BIT_EXT_USEL BIT(5)
  68. #define RX6110_BIT_EXT_FSEL0 BIT(6)
  69. #define RX6110_BIT_EXT_FSEL1 BIT(7)
  70. /* Flag Register (1Eh) bit positions */
  71. #define RX6110_BIT_FLAG_VLF BIT(1)
  72. #define RX6110_BIT_FLAG_AF BIT(3)
  73. #define RX6110_BIT_FLAG_TF BIT(4)
  74. #define RX6110_BIT_FLAG_UF BIT(5)
  75. /* Control Register (1Fh) bit positions */
  76. #define RX6110_BIT_CTRL_TBKE BIT(0)
  77. #define RX6110_BIT_CTRL_TBKON BIT(1)
  78. #define RX6110_BIT_CTRL_TSTP BIT(2)
  79. #define RX6110_BIT_CTRL_AIE BIT(3)
  80. #define RX6110_BIT_CTRL_TIE BIT(4)
  81. #define RX6110_BIT_CTRL_UIE BIT(5)
  82. #define RX6110_BIT_CTRL_STOP BIT(6)
  83. #define RX6110_BIT_CTRL_TEST BIT(7)
  84. enum {
  85. RTC_SEC = 0,
  86. RTC_MIN,
  87. RTC_HOUR,
  88. RTC_WDAY,
  89. RTC_MDAY,
  90. RTC_MONTH,
  91. RTC_YEAR,
  92. RTC_NR_TIME
  93. };
  94. #define RX6110_DRIVER_NAME "rx6110"
  95. struct rx6110_data {
  96. struct rtc_device *rtc;
  97. struct regmap *regmap;
  98. };
  99. /**
  100. * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
  101. *
  102. * @tm: holds date and time
  103. * @data: holds the encoding in rx6110 native form
  104. */
  105. static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
  106. {
  107. pr_debug("%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
  108. tm->tm_sec, tm->tm_min, tm->tm_hour,
  109. tm->tm_mday, tm->tm_mon, tm->tm_year);
  110. /*
  111. * The year in the RTC is a value between 0 and 99.
  112. * Assume that this represents the current century
  113. * and disregard all other values.
  114. */
  115. if (tm->tm_year < 100 || tm->tm_year >= 200)
  116. return -EINVAL;
  117. data[RTC_SEC] = bin2bcd(tm->tm_sec);
  118. data[RTC_MIN] = bin2bcd(tm->tm_min);
  119. data[RTC_HOUR] = bin2bcd(tm->tm_hour);
  120. data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
  121. data[RTC_MDAY] = bin2bcd(tm->tm_mday);
  122. data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
  123. data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
  124. return 0;
  125. }
  126. /**
  127. * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
  128. *
  129. * @data: holds the encoding in rx6110 native form
  130. * @tm: holds date and time
  131. */
  132. static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
  133. {
  134. tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
  135. tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
  136. /* only 24-hour clock */
  137. tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
  138. tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
  139. tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
  140. tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
  141. tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
  142. pr_debug("%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
  143. tm->tm_sec, tm->tm_min, tm->tm_hour,
  144. tm->tm_mday, tm->tm_mon, tm->tm_year);
  145. /*
  146. * The year in the RTC is a value between 0 and 99.
  147. * Assume that this represents the current century
  148. * and disregard all other values.
  149. */
  150. if (tm->tm_year < 100 || tm->tm_year >= 200)
  151. return -EINVAL;
  152. return 0;
  153. }
  154. /**
  155. * rx6110_set_time - set the current time in the rx6110 registers
  156. *
  157. * @dev: the rtc device in use
  158. * @tm: holds date and time
  159. *
  160. * BUG: The HW assumes every year that is a multiple of 4 to be a leap
  161. * year. Next time this is wrong is 2100, which will not be a leap year
  162. *
  163. * Note: If STOP is not set/cleared, the clock will start when the seconds
  164. * register is written
  165. *
  166. */
  167. static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
  168. {
  169. struct rx6110_data *rx6110 = dev_get_drvdata(dev);
  170. u8 data[RTC_NR_TIME];
  171. int ret;
  172. ret = rx6110_rtc_tm_to_data(tm, data);
  173. if (ret < 0)
  174. return ret;
  175. /* set STOP bit before changing clock/calendar */
  176. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
  177. RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
  178. if (ret)
  179. return ret;
  180. ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
  181. RTC_NR_TIME);
  182. if (ret)
  183. return ret;
  184. /* The time in the RTC is valid. Be sure to have VLF cleared. */
  185. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
  186. RX6110_BIT_FLAG_VLF, 0);
  187. if (ret)
  188. return ret;
  189. /* clear STOP bit after changing clock/calendar */
  190. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
  191. RX6110_BIT_CTRL_STOP, 0);
  192. return ret;
  193. }
  194. /**
  195. * rx6110_get_time - get the current time from the rx6110 registers
  196. * @dev: the rtc device in use
  197. * @tm: holds date and time
  198. */
  199. static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
  200. {
  201. struct rx6110_data *rx6110 = dev_get_drvdata(dev);
  202. u8 data[RTC_NR_TIME];
  203. int flags;
  204. int ret;
  205. ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
  206. if (ret)
  207. return -EINVAL;
  208. /* check for VLF Flag (set at power-on) */
  209. if ((flags & RX6110_BIT_FLAG_VLF)) {
  210. dev_warn(dev, "Voltage low, data is invalid.\n");
  211. return -EINVAL;
  212. }
  213. /* read registers to date */
  214. ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
  215. RTC_NR_TIME);
  216. if (ret)
  217. return ret;
  218. ret = rx6110_data_to_rtc_tm(data, tm);
  219. if (ret)
  220. return ret;
  221. dev_dbg(dev, "%s: date %ds %dm %dh %dmd %dm %dy\n", __func__,
  222. tm->tm_sec, tm->tm_min, tm->tm_hour,
  223. tm->tm_mday, tm->tm_mon, tm->tm_year);
  224. return 0;
  225. }
  226. static const struct reg_sequence rx6110_default_regs[] = {
  227. { RX6110_REG_RES1, 0xB8 },
  228. { RX6110_REG_RES2, 0x00 },
  229. { RX6110_REG_RES3, 0x10 },
  230. { RX6110_REG_IRQ, 0x00 },
  231. { RX6110_REG_ALMIN, 0x00 },
  232. { RX6110_REG_ALHOUR, 0x00 },
  233. { RX6110_REG_ALWDAY, 0x00 },
  234. };
  235. /**
  236. * rx6110_init - initialize the rx6110 registers
  237. *
  238. * @rx6110: pointer to the rx6110 struct in use
  239. *
  240. */
  241. static int rx6110_init(struct rx6110_data *rx6110)
  242. {
  243. struct rtc_device *rtc = rx6110->rtc;
  244. int flags;
  245. int ret;
  246. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
  247. RX6110_BIT_EXT_TE, 0);
  248. if (ret)
  249. return ret;
  250. ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
  251. ARRAY_SIZE(rx6110_default_regs));
  252. if (ret)
  253. return ret;
  254. ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
  255. if (ret)
  256. return ret;
  257. /* check for VLF Flag (set at power-on) */
  258. if ((flags & RX6110_BIT_FLAG_VLF))
  259. dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
  260. /* check for Alarm Flag */
  261. if (flags & RX6110_BIT_FLAG_AF)
  262. dev_warn(&rtc->dev, "An alarm may have been missed.\n");
  263. /* check for Periodic Timer Flag */
  264. if (flags & RX6110_BIT_FLAG_TF)
  265. dev_warn(&rtc->dev, "Periodic timer was detected\n");
  266. /* check for Update Timer Flag */
  267. if (flags & RX6110_BIT_FLAG_UF)
  268. dev_warn(&rtc->dev, "Update timer was detected\n");
  269. /* clear all flags BUT VLF */
  270. ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
  271. RX6110_BIT_FLAG_AF |
  272. RX6110_BIT_FLAG_UF |
  273. RX6110_BIT_FLAG_TF,
  274. 0);
  275. return ret;
  276. }
  277. static const struct rtc_class_ops rx6110_rtc_ops = {
  278. .read_time = rx6110_get_time,
  279. .set_time = rx6110_set_time,
  280. };
  281. static struct regmap_config regmap_spi_config = {
  282. .reg_bits = 8,
  283. .val_bits = 8,
  284. .max_register = RX6110_REG_IRQ,
  285. .read_flag_mask = 0x80,
  286. };
  287. /**
  288. * rx6110_probe - initialize rtc driver
  289. * @spi: pointer to spi device
  290. */
  291. static int rx6110_probe(struct spi_device *spi)
  292. {
  293. struct rx6110_data *rx6110;
  294. int err;
  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. rx6110->rtc = devm_rtc_device_register(&spi->dev,
  312. RX6110_DRIVER_NAME,
  313. &rx6110_rtc_ops, THIS_MODULE);
  314. if (IS_ERR(rx6110->rtc))
  315. return PTR_ERR(rx6110->rtc);
  316. err = rx6110_init(rx6110);
  317. if (err)
  318. return err;
  319. rx6110->rtc->max_user_freq = 1;
  320. return 0;
  321. }
  322. static int rx6110_remove(struct spi_device *spi)
  323. {
  324. return 0;
  325. }
  326. static const struct spi_device_id rx6110_id[] = {
  327. { "rx6110", 0 },
  328. { }
  329. };
  330. MODULE_DEVICE_TABLE(spi, rx6110_id);
  331. static struct spi_driver rx6110_driver = {
  332. .driver = {
  333. .name = RX6110_DRIVER_NAME,
  334. },
  335. .probe = rx6110_probe,
  336. .remove = rx6110_remove,
  337. .id_table = rx6110_id,
  338. };
  339. module_spi_driver(rx6110_driver);
  340. MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
  341. MODULE_DESCRIPTION("RX-6110 SA RTC driver");
  342. MODULE_LICENSE("GPL");