rtc-pcf2123.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * An SPI driver for the Philips PCF2123 RTC
  3. * Copyright 2009 Cyber Switching, Inc.
  4. *
  5. * Author: Chris Verges <chrisv@cyberswitching.com>
  6. * Maintainers: http://www.cyberswitching.com
  7. *
  8. * based on the RS5C348 driver in this same directory.
  9. *
  10. * Thanks to Christian Pellegrin <chripell@fsfe.org> for
  11. * the sysfs contributions to this driver.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * Please note that the CS is active high, so platform data
  18. * should look something like:
  19. *
  20. * static struct spi_board_info ek_spi_devices[] = {
  21. * ...
  22. * {
  23. * .modalias = "rtc-pcf2123",
  24. * .chip_select = 1,
  25. * .controller_data = (void *)AT91_PIN_PA10,
  26. * .max_speed_hz = 1000 * 1000,
  27. * .mode = SPI_CS_HIGH,
  28. * .bus_num = 0,
  29. * },
  30. * ...
  31. *};
  32. *
  33. */
  34. #include <linux/bcd.h>
  35. #include <linux/delay.h>
  36. #include <linux/device.h>
  37. #include <linux/errno.h>
  38. #include <linux/init.h>
  39. #include <linux/kernel.h>
  40. #include <linux/of.h>
  41. #include <linux/string.h>
  42. #include <linux/slab.h>
  43. #include <linux/rtc.h>
  44. #include <linux/spi/spi.h>
  45. #include <linux/module.h>
  46. #include <linux/sysfs.h>
  47. /* REGISTERS */
  48. #define PCF2123_REG_CTRL1 (0x00) /* Control Register 1 */
  49. #define PCF2123_REG_CTRL2 (0x01) /* Control Register 2 */
  50. #define PCF2123_REG_SC (0x02) /* datetime */
  51. #define PCF2123_REG_MN (0x03)
  52. #define PCF2123_REG_HR (0x04)
  53. #define PCF2123_REG_DM (0x05)
  54. #define PCF2123_REG_DW (0x06)
  55. #define PCF2123_REG_MO (0x07)
  56. #define PCF2123_REG_YR (0x08)
  57. #define PCF2123_REG_ALRM_MN (0x09) /* Alarm Registers */
  58. #define PCF2123_REG_ALRM_HR (0x0a)
  59. #define PCF2123_REG_ALRM_DM (0x0b)
  60. #define PCF2123_REG_ALRM_DW (0x0c)
  61. #define PCF2123_REG_OFFSET (0x0d) /* Clock Rate Offset Register */
  62. #define PCF2123_REG_TMR_CLKOUT (0x0e) /* Timer Registers */
  63. #define PCF2123_REG_CTDWN_TMR (0x0f)
  64. /* PCF2123_REG_CTRL1 BITS */
  65. #define CTRL1_CLEAR (0) /* Clear */
  66. #define CTRL1_CORR_INT BIT(1) /* Correction irq enable */
  67. #define CTRL1_12_HOUR BIT(2) /* 12 hour time */
  68. #define CTRL1_SW_RESET (BIT(3) | BIT(4) | BIT(6)) /* Software reset */
  69. #define CTRL1_STOP BIT(5) /* Stop the clock */
  70. #define CTRL1_EXT_TEST BIT(7) /* External clock test mode */
  71. /* PCF2123_REG_CTRL2 BITS */
  72. #define CTRL2_TIE BIT(0) /* Countdown timer irq enable */
  73. #define CTRL2_AIE BIT(1) /* Alarm irq enable */
  74. #define CTRL2_TF BIT(2) /* Countdown timer flag */
  75. #define CTRL2_AF BIT(3) /* Alarm flag */
  76. #define CTRL2_TI_TP BIT(4) /* Irq pin generates pulse */
  77. #define CTRL2_MSF BIT(5) /* Minute or second irq flag */
  78. #define CTRL2_SI BIT(6) /* Second irq enable */
  79. #define CTRL2_MI BIT(7) /* Minute irq enable */
  80. /* PCF2123_REG_SC BITS */
  81. #define OSC_HAS_STOPPED BIT(7) /* Clock has been stopped */
  82. /* PCF2123_REG_ALRM_XX BITS */
  83. #define ALRM_ENABLE BIT(7) /* MN, HR, DM, or DW alarm enable */
  84. /* PCF2123_REG_TMR_CLKOUT BITS */
  85. #define CD_TMR_4096KHZ (0) /* 4096 KHz countdown timer */
  86. #define CD_TMR_64HZ (1) /* 64 Hz countdown timer */
  87. #define CD_TMR_1HZ (2) /* 1 Hz countdown timer */
  88. #define CD_TMR_60th_HZ (3) /* 60th Hz countdown timer */
  89. #define CD_TMR_TE BIT(3) /* Countdown timer enable */
  90. /* PCF2123_REG_OFFSET BITS */
  91. #define OFFSET_SIGN_BIT 6 /* 2's complement sign bit */
  92. #define OFFSET_COARSE BIT(7) /* Coarse mode offset */
  93. #define OFFSET_STEP (2170) /* Offset step in parts per billion */
  94. /* READ/WRITE ADDRESS BITS */
  95. #define PCF2123_WRITE BIT(4)
  96. #define PCF2123_READ (BIT(4) | BIT(7))
  97. static struct spi_driver pcf2123_driver;
  98. struct pcf2123_sysfs_reg {
  99. struct device_attribute attr;
  100. char name[2];
  101. };
  102. struct pcf2123_plat_data {
  103. struct rtc_device *rtc;
  104. struct pcf2123_sysfs_reg regs[16];
  105. };
  106. /*
  107. * Causes a 30 nanosecond delay to ensure that the PCF2123 chip select
  108. * is released properly after an SPI write. This function should be
  109. * called after EVERY read/write call over SPI.
  110. */
  111. static inline void pcf2123_delay_trec(void)
  112. {
  113. ndelay(30);
  114. }
  115. static int pcf2123_read(struct device *dev, u8 reg, u8 *rxbuf, size_t size)
  116. {
  117. struct spi_device *spi = to_spi_device(dev);
  118. int ret;
  119. reg |= PCF2123_READ;
  120. ret = spi_write_then_read(spi, &reg, 1, rxbuf, size);
  121. pcf2123_delay_trec();
  122. return ret;
  123. }
  124. static int pcf2123_write(struct device *dev, u8 *txbuf, size_t size)
  125. {
  126. struct spi_device *spi = to_spi_device(dev);
  127. int ret;
  128. txbuf[0] |= PCF2123_WRITE;
  129. ret = spi_write(spi, txbuf, size);
  130. pcf2123_delay_trec();
  131. return ret;
  132. }
  133. static int pcf2123_write_reg(struct device *dev, u8 reg, u8 val)
  134. {
  135. u8 txbuf[2];
  136. txbuf[0] = reg;
  137. txbuf[1] = val;
  138. return pcf2123_write(dev, txbuf, sizeof(txbuf));
  139. }
  140. static ssize_t pcf2123_show(struct device *dev, struct device_attribute *attr,
  141. char *buffer)
  142. {
  143. struct pcf2123_sysfs_reg *r;
  144. u8 rxbuf[1];
  145. unsigned long reg;
  146. int ret;
  147. r = container_of(attr, struct pcf2123_sysfs_reg, attr);
  148. ret = kstrtoul(r->name, 16, &reg);
  149. if (ret)
  150. return ret;
  151. ret = pcf2123_read(dev, reg, rxbuf, 1);
  152. if (ret < 0)
  153. return -EIO;
  154. return sprintf(buffer, "0x%x\n", rxbuf[0]);
  155. }
  156. static ssize_t pcf2123_store(struct device *dev, struct device_attribute *attr,
  157. const char *buffer, size_t count)
  158. {
  159. struct pcf2123_sysfs_reg *r;
  160. unsigned long reg;
  161. unsigned long val;
  162. int ret;
  163. r = container_of(attr, struct pcf2123_sysfs_reg, attr);
  164. ret = kstrtoul(r->name, 16, &reg);
  165. if (ret)
  166. return ret;
  167. ret = kstrtoul(buffer, 10, &val);
  168. if (ret)
  169. return ret;
  170. ret = pcf2123_write_reg(dev, reg, val);
  171. if (ret < 0)
  172. return -EIO;
  173. return count;
  174. }
  175. static int pcf2123_read_offset(struct device *dev, long *offset)
  176. {
  177. int ret;
  178. s8 reg;
  179. ret = pcf2123_read(dev, PCF2123_REG_OFFSET, &reg, 1);
  180. if (ret < 0)
  181. return ret;
  182. if (reg & OFFSET_COARSE)
  183. reg <<= 1; /* multiply by 2 and sign extend */
  184. else
  185. reg = sign_extend32(reg, OFFSET_SIGN_BIT);
  186. *offset = ((long)reg) * OFFSET_STEP;
  187. return 0;
  188. }
  189. /*
  190. * The offset register is a 7 bit signed value with a coarse bit in bit 7.
  191. * The main difference between the two is normal offset adjusts the first
  192. * second of n minutes every other hour, with 61, 62 and 63 being shoved
  193. * into the 60th minute.
  194. * The coarse adjustment does the same, but every hour.
  195. * the two overlap, with every even normal offset value corresponding
  196. * to a coarse offset. Based on this algorithm, it seems that despite the
  197. * name, coarse offset is a better fit for overlapping values.
  198. */
  199. static int pcf2123_set_offset(struct device *dev, long offset)
  200. {
  201. s8 reg;
  202. if (offset > OFFSET_STEP * 127)
  203. reg = 127;
  204. else if (offset < OFFSET_STEP * -128)
  205. reg = -128;
  206. else
  207. reg = (s8)((offset + (OFFSET_STEP >> 1)) / OFFSET_STEP);
  208. /* choose fine offset only for odd values in the normal range */
  209. if (reg & 1 && reg <= 63 && reg >= -64) {
  210. /* Normal offset. Clear the coarse bit */
  211. reg &= ~OFFSET_COARSE;
  212. } else {
  213. /* Coarse offset. Divide by 2 and set the coarse bit */
  214. reg >>= 1;
  215. reg |= OFFSET_COARSE;
  216. }
  217. return pcf2123_write_reg(dev, PCF2123_REG_OFFSET, reg);
  218. }
  219. static int pcf2123_rtc_read_time(struct device *dev, struct rtc_time *tm)
  220. {
  221. u8 rxbuf[7];
  222. int ret;
  223. ret = pcf2123_read(dev, PCF2123_REG_SC, rxbuf, sizeof(rxbuf));
  224. if (ret < 0)
  225. return ret;
  226. if (rxbuf[0] & OSC_HAS_STOPPED) {
  227. dev_info(dev, "clock was stopped. Time is not valid\n");
  228. return -EINVAL;
  229. }
  230. tm->tm_sec = bcd2bin(rxbuf[0] & 0x7F);
  231. tm->tm_min = bcd2bin(rxbuf[1] & 0x7F);
  232. tm->tm_hour = bcd2bin(rxbuf[2] & 0x3F); /* rtc hr 0-23 */
  233. tm->tm_mday = bcd2bin(rxbuf[3] & 0x3F);
  234. tm->tm_wday = rxbuf[4] & 0x07;
  235. tm->tm_mon = bcd2bin(rxbuf[5] & 0x1F) - 1; /* rtc mn 1-12 */
  236. tm->tm_year = bcd2bin(rxbuf[6]);
  237. if (tm->tm_year < 70)
  238. tm->tm_year += 100; /* assume we are in 1970...2069 */
  239. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  240. "mday=%d, mon=%d, year=%d, wday=%d\n",
  241. __func__,
  242. tm->tm_sec, tm->tm_min, tm->tm_hour,
  243. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  244. return rtc_valid_tm(tm);
  245. }
  246. static int pcf2123_rtc_set_time(struct device *dev, struct rtc_time *tm)
  247. {
  248. u8 txbuf[8];
  249. int ret;
  250. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  251. "mday=%d, mon=%d, year=%d, wday=%d\n",
  252. __func__,
  253. tm->tm_sec, tm->tm_min, tm->tm_hour,
  254. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  255. /* Stop the counter first */
  256. ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_STOP);
  257. if (ret < 0)
  258. return ret;
  259. /* Set the new time */
  260. txbuf[0] = PCF2123_REG_SC;
  261. txbuf[1] = bin2bcd(tm->tm_sec & 0x7F);
  262. txbuf[2] = bin2bcd(tm->tm_min & 0x7F);
  263. txbuf[3] = bin2bcd(tm->tm_hour & 0x3F);
  264. txbuf[4] = bin2bcd(tm->tm_mday & 0x3F);
  265. txbuf[5] = tm->tm_wday & 0x07;
  266. txbuf[6] = bin2bcd((tm->tm_mon + 1) & 0x1F); /* rtc mn 1-12 */
  267. txbuf[7] = bin2bcd(tm->tm_year < 100 ? tm->tm_year : tm->tm_year - 100);
  268. ret = pcf2123_write(dev, txbuf, sizeof(txbuf));
  269. if (ret < 0)
  270. return ret;
  271. /* Start the counter */
  272. ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_CLEAR);
  273. if (ret < 0)
  274. return ret;
  275. return 0;
  276. }
  277. static int pcf2123_reset(struct device *dev)
  278. {
  279. int ret;
  280. u8 rxbuf[2];
  281. ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_SW_RESET);
  282. if (ret < 0)
  283. return ret;
  284. /* Stop the counter */
  285. dev_dbg(dev, "stopping RTC\n");
  286. ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_STOP);
  287. if (ret < 0)
  288. return ret;
  289. /* See if the counter was actually stopped */
  290. dev_dbg(dev, "checking for presence of RTC\n");
  291. ret = pcf2123_read(dev, PCF2123_REG_CTRL1, rxbuf, sizeof(rxbuf));
  292. if (ret < 0)
  293. return ret;
  294. dev_dbg(dev, "received data from RTC (0x%02X 0x%02X)\n",
  295. rxbuf[0], rxbuf[1]);
  296. if (!(rxbuf[0] & CTRL1_STOP))
  297. return -ENODEV;
  298. /* Start the counter */
  299. ret = pcf2123_write_reg(dev, PCF2123_REG_CTRL1, CTRL1_CLEAR);
  300. if (ret < 0)
  301. return ret;
  302. return 0;
  303. }
  304. static const struct rtc_class_ops pcf2123_rtc_ops = {
  305. .read_time = pcf2123_rtc_read_time,
  306. .set_time = pcf2123_rtc_set_time,
  307. .read_offset = pcf2123_read_offset,
  308. .set_offset = pcf2123_set_offset,
  309. };
  310. static int pcf2123_probe(struct spi_device *spi)
  311. {
  312. struct rtc_device *rtc;
  313. struct rtc_time tm;
  314. struct pcf2123_plat_data *pdata;
  315. int ret, i;
  316. pdata = devm_kzalloc(&spi->dev, sizeof(struct pcf2123_plat_data),
  317. GFP_KERNEL);
  318. if (!pdata)
  319. return -ENOMEM;
  320. spi->dev.platform_data = pdata;
  321. ret = pcf2123_rtc_read_time(&spi->dev, &tm);
  322. if (ret < 0) {
  323. ret = pcf2123_reset(&spi->dev);
  324. if (ret < 0) {
  325. dev_err(&spi->dev, "chip not found\n");
  326. goto kfree_exit;
  327. }
  328. }
  329. dev_info(&spi->dev, "spiclk %u KHz.\n",
  330. (spi->max_speed_hz + 500) / 1000);
  331. /* Finalize the initialization */
  332. rtc = devm_rtc_device_register(&spi->dev, pcf2123_driver.driver.name,
  333. &pcf2123_rtc_ops, THIS_MODULE);
  334. if (IS_ERR(rtc)) {
  335. dev_err(&spi->dev, "failed to register.\n");
  336. ret = PTR_ERR(rtc);
  337. goto kfree_exit;
  338. }
  339. pdata->rtc = rtc;
  340. for (i = 0; i < 16; i++) {
  341. sysfs_attr_init(&pdata->regs[i].attr.attr);
  342. sprintf(pdata->regs[i].name, "%1x", i);
  343. pdata->regs[i].attr.attr.mode = S_IRUGO | S_IWUSR;
  344. pdata->regs[i].attr.attr.name = pdata->regs[i].name;
  345. pdata->regs[i].attr.show = pcf2123_show;
  346. pdata->regs[i].attr.store = pcf2123_store;
  347. ret = device_create_file(&spi->dev, &pdata->regs[i].attr);
  348. if (ret) {
  349. dev_err(&spi->dev, "Unable to create sysfs %s\n",
  350. pdata->regs[i].name);
  351. goto sysfs_exit;
  352. }
  353. }
  354. return 0;
  355. sysfs_exit:
  356. for (i--; i >= 0; i--)
  357. device_remove_file(&spi->dev, &pdata->regs[i].attr);
  358. kfree_exit:
  359. spi->dev.platform_data = NULL;
  360. return ret;
  361. }
  362. static int pcf2123_remove(struct spi_device *spi)
  363. {
  364. struct pcf2123_plat_data *pdata = dev_get_platdata(&spi->dev);
  365. int i;
  366. if (pdata) {
  367. for (i = 0; i < 16; i++)
  368. if (pdata->regs[i].name[0])
  369. device_remove_file(&spi->dev,
  370. &pdata->regs[i].attr);
  371. }
  372. return 0;
  373. }
  374. #ifdef CONFIG_OF
  375. static const struct of_device_id pcf2123_dt_ids[] = {
  376. { .compatible = "nxp,rtc-pcf2123", },
  377. { /* sentinel */ }
  378. };
  379. MODULE_DEVICE_TABLE(of, pcf2123_dt_ids);
  380. #endif
  381. static struct spi_driver pcf2123_driver = {
  382. .driver = {
  383. .name = "rtc-pcf2123",
  384. .of_match_table = of_match_ptr(pcf2123_dt_ids),
  385. },
  386. .probe = pcf2123_probe,
  387. .remove = pcf2123_remove,
  388. };
  389. module_spi_driver(pcf2123_driver);
  390. MODULE_AUTHOR("Chris Verges <chrisv@cyberswitching.com>");
  391. MODULE_DESCRIPTION("NXP PCF2123 RTC driver");
  392. MODULE_LICENSE("GPL");