rtc-pcf2123.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/string.h>
  41. #include <linux/slab.h>
  42. #include <linux/rtc.h>
  43. #include <linux/spi/spi.h>
  44. #define DRV_VERSION "0.6"
  45. #define PCF2123_REG_CTRL1 (0x00) /* Control Register 1 */
  46. #define PCF2123_REG_CTRL2 (0x01) /* Control Register 2 */
  47. #define PCF2123_REG_SC (0x02) /* datetime */
  48. #define PCF2123_REG_MN (0x03)
  49. #define PCF2123_REG_HR (0x04)
  50. #define PCF2123_REG_DM (0x05)
  51. #define PCF2123_REG_DW (0x06)
  52. #define PCF2123_REG_MO (0x07)
  53. #define PCF2123_REG_YR (0x08)
  54. #define PCF2123_SUBADDR (1 << 4)
  55. #define PCF2123_WRITE ((0 << 7) | PCF2123_SUBADDR)
  56. #define PCF2123_READ ((1 << 7) | PCF2123_SUBADDR)
  57. static struct spi_driver pcf2123_driver;
  58. struct pcf2123_sysfs_reg {
  59. struct device_attribute attr;
  60. char name[2];
  61. };
  62. struct pcf2123_plat_data {
  63. struct rtc_device *rtc;
  64. struct pcf2123_sysfs_reg regs[16];
  65. };
  66. /*
  67. * Causes a 30 nanosecond delay to ensure that the PCF2123 chip select
  68. * is released properly after an SPI write. This function should be
  69. * called after EVERY read/write call over SPI.
  70. */
  71. static inline void pcf2123_delay_trec(void)
  72. {
  73. ndelay(30);
  74. }
  75. static ssize_t pcf2123_show(struct device *dev, struct device_attribute *attr,
  76. char *buffer)
  77. {
  78. struct spi_device *spi = to_spi_device(dev);
  79. struct pcf2123_sysfs_reg *r;
  80. u8 txbuf[1], rxbuf[1];
  81. unsigned long reg;
  82. int ret;
  83. r = container_of(attr, struct pcf2123_sysfs_reg, attr);
  84. if (strict_strtoul(r->name, 16, &reg))
  85. return -EINVAL;
  86. txbuf[0] = PCF2123_READ | reg;
  87. ret = spi_write_then_read(spi, txbuf, 1, rxbuf, 1);
  88. if (ret < 0)
  89. return -EIO;
  90. pcf2123_delay_trec();
  91. return sprintf(buffer, "0x%x\n", rxbuf[0]);
  92. }
  93. static ssize_t pcf2123_store(struct device *dev, struct device_attribute *attr,
  94. const char *buffer, size_t count) {
  95. struct spi_device *spi = to_spi_device(dev);
  96. struct pcf2123_sysfs_reg *r;
  97. u8 txbuf[2];
  98. unsigned long reg;
  99. unsigned long val;
  100. int ret;
  101. r = container_of(attr, struct pcf2123_sysfs_reg, attr);
  102. if (strict_strtoul(r->name, 16, &reg)
  103. || strict_strtoul(buffer, 10, &val))
  104. return -EINVAL;
  105. txbuf[0] = PCF2123_WRITE | reg;
  106. txbuf[1] = val;
  107. ret = spi_write(spi, txbuf, sizeof(txbuf));
  108. if (ret < 0)
  109. return -EIO;
  110. pcf2123_delay_trec();
  111. return count;
  112. }
  113. static int pcf2123_rtc_read_time(struct device *dev, struct rtc_time *tm)
  114. {
  115. struct spi_device *spi = to_spi_device(dev);
  116. u8 txbuf[1], rxbuf[7];
  117. int ret;
  118. txbuf[0] = PCF2123_READ | PCF2123_REG_SC;
  119. ret = spi_write_then_read(spi, txbuf, sizeof(txbuf),
  120. rxbuf, sizeof(rxbuf));
  121. if (ret < 0)
  122. return ret;
  123. pcf2123_delay_trec();
  124. tm->tm_sec = bcd2bin(rxbuf[0] & 0x7F);
  125. tm->tm_min = bcd2bin(rxbuf[1] & 0x7F);
  126. tm->tm_hour = bcd2bin(rxbuf[2] & 0x3F); /* rtc hr 0-23 */
  127. tm->tm_mday = bcd2bin(rxbuf[3] & 0x3F);
  128. tm->tm_wday = rxbuf[4] & 0x07;
  129. tm->tm_mon = bcd2bin(rxbuf[5] & 0x1F) - 1; /* rtc mn 1-12 */
  130. tm->tm_year = bcd2bin(rxbuf[6]);
  131. if (tm->tm_year < 70)
  132. tm->tm_year += 100; /* assume we are in 1970...2069 */
  133. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  134. "mday=%d, mon=%d, year=%d, wday=%d\n",
  135. __func__,
  136. tm->tm_sec, tm->tm_min, tm->tm_hour,
  137. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  138. /* the clock can give out invalid datetime, but we cannot return
  139. * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  140. */
  141. if (rtc_valid_tm(tm) < 0)
  142. dev_err(dev, "retrieved date/time is not valid.\n");
  143. return 0;
  144. }
  145. static int pcf2123_rtc_set_time(struct device *dev, struct rtc_time *tm)
  146. {
  147. struct spi_device *spi = to_spi_device(dev);
  148. u8 txbuf[8];
  149. int ret;
  150. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  151. "mday=%d, mon=%d, year=%d, wday=%d\n",
  152. __func__,
  153. tm->tm_sec, tm->tm_min, tm->tm_hour,
  154. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  155. /* Stop the counter first */
  156. txbuf[0] = PCF2123_WRITE | PCF2123_REG_CTRL1;
  157. txbuf[1] = 0x20;
  158. ret = spi_write(spi, txbuf, 2);
  159. if (ret < 0)
  160. return ret;
  161. pcf2123_delay_trec();
  162. /* Set the new time */
  163. txbuf[0] = PCF2123_WRITE | PCF2123_REG_SC;
  164. txbuf[1] = bin2bcd(tm->tm_sec & 0x7F);
  165. txbuf[2] = bin2bcd(tm->tm_min & 0x7F);
  166. txbuf[3] = bin2bcd(tm->tm_hour & 0x3F);
  167. txbuf[4] = bin2bcd(tm->tm_mday & 0x3F);
  168. txbuf[5] = tm->tm_wday & 0x07;
  169. txbuf[6] = bin2bcd((tm->tm_mon + 1) & 0x1F); /* rtc mn 1-12 */
  170. txbuf[7] = bin2bcd(tm->tm_year < 100 ? tm->tm_year : tm->tm_year - 100);
  171. ret = spi_write(spi, txbuf, sizeof(txbuf));
  172. if (ret < 0)
  173. return ret;
  174. pcf2123_delay_trec();
  175. /* Start the counter */
  176. txbuf[0] = PCF2123_WRITE | PCF2123_REG_CTRL1;
  177. txbuf[1] = 0x00;
  178. ret = spi_write(spi, txbuf, 2);
  179. if (ret < 0)
  180. return ret;
  181. pcf2123_delay_trec();
  182. return 0;
  183. }
  184. static const struct rtc_class_ops pcf2123_rtc_ops = {
  185. .read_time = pcf2123_rtc_read_time,
  186. .set_time = pcf2123_rtc_set_time,
  187. };
  188. static int __devinit pcf2123_probe(struct spi_device *spi)
  189. {
  190. struct rtc_device *rtc;
  191. struct pcf2123_plat_data *pdata;
  192. u8 txbuf[2], rxbuf[2];
  193. int ret, i;
  194. pdata = kzalloc(sizeof(struct pcf2123_plat_data), GFP_KERNEL);
  195. if (!pdata)
  196. return -ENOMEM;
  197. spi->dev.platform_data = pdata;
  198. /* Send a software reset command */
  199. txbuf[0] = PCF2123_WRITE | PCF2123_REG_CTRL1;
  200. txbuf[1] = 0x58;
  201. dev_dbg(&spi->dev, "resetting RTC (0x%02X 0x%02X)\n",
  202. txbuf[0], txbuf[1]);
  203. ret = spi_write(spi, txbuf, 2 * sizeof(u8));
  204. if (ret < 0)
  205. goto kfree_exit;
  206. pcf2123_delay_trec();
  207. /* Stop the counter */
  208. txbuf[0] = PCF2123_WRITE | PCF2123_REG_CTRL1;
  209. txbuf[1] = 0x20;
  210. dev_dbg(&spi->dev, "stopping RTC (0x%02X 0x%02X)\n",
  211. txbuf[0], txbuf[1]);
  212. ret = spi_write(spi, txbuf, 2 * sizeof(u8));
  213. if (ret < 0)
  214. goto kfree_exit;
  215. pcf2123_delay_trec();
  216. /* See if the counter was actually stopped */
  217. txbuf[0] = PCF2123_READ | PCF2123_REG_CTRL1;
  218. dev_dbg(&spi->dev, "checking for presence of RTC (0x%02X)\n",
  219. txbuf[0]);
  220. ret = spi_write_then_read(spi, txbuf, 1 * sizeof(u8),
  221. rxbuf, 2 * sizeof(u8));
  222. dev_dbg(&spi->dev, "received data from RTC (0x%02X 0x%02X)\n",
  223. rxbuf[0], rxbuf[1]);
  224. if (ret < 0)
  225. goto kfree_exit;
  226. pcf2123_delay_trec();
  227. if (!(rxbuf[0] & 0x20)) {
  228. dev_err(&spi->dev, "chip not found\n");
  229. goto kfree_exit;
  230. }
  231. dev_info(&spi->dev, "chip found, driver version " DRV_VERSION "\n");
  232. dev_info(&spi->dev, "spiclk %u KHz.\n",
  233. (spi->max_speed_hz + 500) / 1000);
  234. /* Start the counter */
  235. txbuf[0] = PCF2123_WRITE | PCF2123_REG_CTRL1;
  236. txbuf[1] = 0x00;
  237. ret = spi_write(spi, txbuf, sizeof(txbuf));
  238. if (ret < 0)
  239. goto kfree_exit;
  240. pcf2123_delay_trec();
  241. /* Finalize the initialization */
  242. rtc = rtc_device_register(pcf2123_driver.driver.name, &spi->dev,
  243. &pcf2123_rtc_ops, THIS_MODULE);
  244. if (IS_ERR(rtc)) {
  245. dev_err(&spi->dev, "failed to register.\n");
  246. ret = PTR_ERR(rtc);
  247. goto kfree_exit;
  248. }
  249. pdata->rtc = rtc;
  250. for (i = 0; i < 16; i++) {
  251. sprintf(pdata->regs[i].name, "%1x", i);
  252. pdata->regs[i].attr.attr.mode = S_IRUGO | S_IWUSR;
  253. pdata->regs[i].attr.attr.name = pdata->regs[i].name;
  254. pdata->regs[i].attr.show = pcf2123_show;
  255. pdata->regs[i].attr.store = pcf2123_store;
  256. ret = device_create_file(&spi->dev, &pdata->regs[i].attr);
  257. if (ret) {
  258. dev_err(&spi->dev, "Unable to create sysfs %s\n",
  259. pdata->regs[i].name);
  260. goto sysfs_exit;
  261. }
  262. }
  263. return 0;
  264. sysfs_exit:
  265. for (i--; i >= 0; i--)
  266. device_remove_file(&spi->dev, &pdata->regs[i].attr);
  267. kfree_exit:
  268. kfree(pdata);
  269. spi->dev.platform_data = NULL;
  270. return ret;
  271. }
  272. static int __devexit pcf2123_remove(struct spi_device *spi)
  273. {
  274. struct pcf2123_plat_data *pdata = spi->dev.platform_data;
  275. int i;
  276. if (pdata) {
  277. struct rtc_device *rtc = pdata->rtc;
  278. if (rtc)
  279. rtc_device_unregister(rtc);
  280. for (i = 0; i < 16; i++)
  281. if (pdata->regs[i].name[0])
  282. device_remove_file(&spi->dev,
  283. &pdata->regs[i].attr);
  284. kfree(pdata);
  285. }
  286. return 0;
  287. }
  288. static struct spi_driver pcf2123_driver = {
  289. .driver = {
  290. .name = "rtc-pcf2123",
  291. .bus = &spi_bus_type,
  292. .owner = THIS_MODULE,
  293. },
  294. .probe = pcf2123_probe,
  295. .remove = __devexit_p(pcf2123_remove),
  296. };
  297. static int __init pcf2123_init(void)
  298. {
  299. return spi_register_driver(&pcf2123_driver);
  300. }
  301. static void __exit pcf2123_exit(void)
  302. {
  303. spi_unregister_driver(&pcf2123_driver);
  304. }
  305. MODULE_AUTHOR("Chris Verges <chrisv@cyberswitching.com>");
  306. MODULE_DESCRIPTION("NXP PCF2123 RTC driver");
  307. MODULE_LICENSE("GPL");
  308. MODULE_VERSION(DRV_VERSION);
  309. module_init(pcf2123_init);
  310. module_exit(pcf2123_exit);