rtc-pcf2127.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * An I2C and SPI driver for the NXP PCF2127/29 RTC
  3. * Copyright 2013 Til-Technologies
  4. *
  5. * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
  6. *
  7. * based on the other drivers in this same directory.
  8. *
  9. * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/i2c.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/bcd.h>
  18. #include <linux/rtc.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/regmap.h>
  23. #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
  24. #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
  25. #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
  26. #define PCF2127_REG_CTRL3_BLF BIT(2)
  27. #define PCF2127_REG_SC (0x03) /* datetime */
  28. #define PCF2127_REG_MN (0x04)
  29. #define PCF2127_REG_HR (0x05)
  30. #define PCF2127_REG_DM (0x06)
  31. #define PCF2127_REG_DW (0x07)
  32. #define PCF2127_REG_MO (0x08)
  33. #define PCF2127_REG_YR (0x09)
  34. /* the pcf2127 has 512 bytes nvmem, pcf2129 doesn't */
  35. #define PCF2127_REG_RAM_addr_MSB 0x1a
  36. #define PCF2127_REG_RAM_wrt_cmd 0x1c
  37. #define PCF2127_REG_RAM_rd_cmd 0x1d
  38. #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */
  39. struct pcf2127 {
  40. struct rtc_device *rtc;
  41. struct regmap *regmap;
  42. };
  43. /*
  44. * In the routines that deal directly with the pcf2127 hardware, we use
  45. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  46. */
  47. static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
  48. {
  49. struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
  50. unsigned char buf[10];
  51. int ret;
  52. /*
  53. * Avoid reading CTRL2 register as it causes WD_VAL register
  54. * value to reset to 0 which means watchdog is stopped.
  55. */
  56. ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3,
  57. (buf + PCF2127_REG_CTRL3),
  58. ARRAY_SIZE(buf) - PCF2127_REG_CTRL3);
  59. if (ret) {
  60. dev_err(dev, "%s: read error\n", __func__);
  61. return ret;
  62. }
  63. if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
  64. dev_info(dev,
  65. "low voltage detected, check/replace RTC battery.\n");
  66. if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
  67. /*
  68. * no need clear the flag here,
  69. * it will be cleared once the new date is saved
  70. */
  71. dev_warn(dev,
  72. "oscillator stop detected, date/time is not reliable\n");
  73. return -EINVAL;
  74. }
  75. dev_dbg(dev,
  76. "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, "
  77. "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
  78. __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC],
  79. buf[PCF2127_REG_MN], buf[PCF2127_REG_HR],
  80. buf[PCF2127_REG_DM], buf[PCF2127_REG_DW],
  81. buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]);
  82. tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
  83. tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
  84. tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
  85. tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
  86. tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
  87. tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  88. tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
  89. if (tm->tm_year < 70)
  90. tm->tm_year += 100; /* assume we are in 1970...2069 */
  91. dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  92. "mday=%d, mon=%d, year=%d, wday=%d\n",
  93. __func__,
  94. tm->tm_sec, tm->tm_min, tm->tm_hour,
  95. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  96. return 0;
  97. }
  98. static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
  99. {
  100. struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
  101. unsigned char buf[7];
  102. int i = 0, err;
  103. dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
  104. "mday=%d, mon=%d, year=%d, wday=%d\n",
  105. __func__,
  106. tm->tm_sec, tm->tm_min, tm->tm_hour,
  107. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  108. /* hours, minutes and seconds */
  109. buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
  110. buf[i++] = bin2bcd(tm->tm_min);
  111. buf[i++] = bin2bcd(tm->tm_hour);
  112. buf[i++] = bin2bcd(tm->tm_mday);
  113. buf[i++] = tm->tm_wday & 0x07;
  114. /* month, 1 - 12 */
  115. buf[i++] = bin2bcd(tm->tm_mon + 1);
  116. /* year */
  117. buf[i++] = bin2bcd(tm->tm_year % 100);
  118. /* write register's data */
  119. err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
  120. if (err) {
  121. dev_err(dev,
  122. "%s: err=%d", __func__, err);
  123. return err;
  124. }
  125. return 0;
  126. }
  127. #ifdef CONFIG_RTC_INTF_DEV
  128. static int pcf2127_rtc_ioctl(struct device *dev,
  129. unsigned int cmd, unsigned long arg)
  130. {
  131. struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
  132. int touser;
  133. int ret;
  134. switch (cmd) {
  135. case RTC_VL_READ:
  136. ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
  137. if (ret)
  138. return ret;
  139. touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
  140. if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
  141. return -EFAULT;
  142. return 0;
  143. default:
  144. return -ENOIOCTLCMD;
  145. }
  146. }
  147. #else
  148. #define pcf2127_rtc_ioctl NULL
  149. #endif
  150. static const struct rtc_class_ops pcf2127_rtc_ops = {
  151. .ioctl = pcf2127_rtc_ioctl,
  152. .read_time = pcf2127_rtc_read_time,
  153. .set_time = pcf2127_rtc_set_time,
  154. };
  155. static int pcf2127_nvmem_read(void *priv, unsigned int offset,
  156. void *val, size_t bytes)
  157. {
  158. struct pcf2127 *pcf2127 = priv;
  159. int ret;
  160. unsigned char offsetbuf[] = { offset >> 8, offset };
  161. ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB,
  162. offsetbuf, 2);
  163. if (ret)
  164. return ret;
  165. ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_rd_cmd,
  166. val, bytes);
  167. return ret ?: bytes;
  168. }
  169. static int pcf2127_nvmem_write(void *priv, unsigned int offset,
  170. void *val, size_t bytes)
  171. {
  172. struct pcf2127 *pcf2127 = priv;
  173. int ret;
  174. unsigned char offsetbuf[] = { offset >> 8, offset };
  175. ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB,
  176. offsetbuf, 2);
  177. if (ret)
  178. return ret;
  179. ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_wrt_cmd,
  180. val, bytes);
  181. return ret ?: bytes;
  182. }
  183. static int pcf2127_probe(struct device *dev, struct regmap *regmap,
  184. const char *name, bool has_nvmem)
  185. {
  186. struct pcf2127 *pcf2127;
  187. int ret = 0;
  188. dev_dbg(dev, "%s\n", __func__);
  189. pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
  190. if (!pcf2127)
  191. return -ENOMEM;
  192. pcf2127->regmap = regmap;
  193. dev_set_drvdata(dev, pcf2127);
  194. pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
  195. THIS_MODULE);
  196. if (IS_ERR(pcf2127->rtc))
  197. return PTR_ERR(pcf2127->rtc);
  198. if (has_nvmem) {
  199. struct nvmem_config nvmem_cfg = {
  200. .priv = pcf2127,
  201. .reg_read = pcf2127_nvmem_read,
  202. .reg_write = pcf2127_nvmem_write,
  203. .size = 512,
  204. };
  205. ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg);
  206. }
  207. return ret;
  208. }
  209. #ifdef CONFIG_OF
  210. static const struct of_device_id pcf2127_of_match[] = {
  211. { .compatible = "nxp,pcf2127" },
  212. { .compatible = "nxp,pcf2129" },
  213. {}
  214. };
  215. MODULE_DEVICE_TABLE(of, pcf2127_of_match);
  216. #endif
  217. #if IS_ENABLED(CONFIG_I2C)
  218. static int pcf2127_i2c_write(void *context, const void *data, size_t count)
  219. {
  220. struct device *dev = context;
  221. struct i2c_client *client = to_i2c_client(dev);
  222. int ret;
  223. ret = i2c_master_send(client, data, count);
  224. if (ret != count)
  225. return ret < 0 ? ret : -EIO;
  226. return 0;
  227. }
  228. static int pcf2127_i2c_gather_write(void *context,
  229. const void *reg, size_t reg_size,
  230. const void *val, size_t val_size)
  231. {
  232. struct device *dev = context;
  233. struct i2c_client *client = to_i2c_client(dev);
  234. int ret;
  235. void *buf;
  236. if (WARN_ON(reg_size != 1))
  237. return -EINVAL;
  238. buf = kmalloc(val_size + 1, GFP_KERNEL);
  239. if (!buf)
  240. return -ENOMEM;
  241. memcpy(buf, reg, 1);
  242. memcpy(buf + 1, val, val_size);
  243. ret = i2c_master_send(client, buf, val_size + 1);
  244. kfree(buf);
  245. if (ret != val_size + 1)
  246. return ret < 0 ? ret : -EIO;
  247. return 0;
  248. }
  249. static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
  250. void *val, size_t val_size)
  251. {
  252. struct device *dev = context;
  253. struct i2c_client *client = to_i2c_client(dev);
  254. int ret;
  255. if (WARN_ON(reg_size != 1))
  256. return -EINVAL;
  257. ret = i2c_master_send(client, reg, 1);
  258. if (ret != 1)
  259. return ret < 0 ? ret : -EIO;
  260. ret = i2c_master_recv(client, val, val_size);
  261. if (ret != val_size)
  262. return ret < 0 ? ret : -EIO;
  263. return 0;
  264. }
  265. /*
  266. * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
  267. * is that the STOP condition is required between set register address and
  268. * read register data when reading from registers.
  269. */
  270. static const struct regmap_bus pcf2127_i2c_regmap = {
  271. .write = pcf2127_i2c_write,
  272. .gather_write = pcf2127_i2c_gather_write,
  273. .read = pcf2127_i2c_read,
  274. };
  275. static struct i2c_driver pcf2127_i2c_driver;
  276. static int pcf2127_i2c_probe(struct i2c_client *client,
  277. const struct i2c_device_id *id)
  278. {
  279. struct regmap *regmap;
  280. static const struct regmap_config config = {
  281. .reg_bits = 8,
  282. .val_bits = 8,
  283. };
  284. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  285. return -ENODEV;
  286. regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
  287. &client->dev, &config);
  288. if (IS_ERR(regmap)) {
  289. dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
  290. __func__, PTR_ERR(regmap));
  291. return PTR_ERR(regmap);
  292. }
  293. return pcf2127_probe(&client->dev, regmap,
  294. pcf2127_i2c_driver.driver.name, id->driver_data);
  295. }
  296. static const struct i2c_device_id pcf2127_i2c_id[] = {
  297. { "pcf2127", 1 },
  298. { "pcf2129", 0 },
  299. { }
  300. };
  301. MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
  302. static struct i2c_driver pcf2127_i2c_driver = {
  303. .driver = {
  304. .name = "rtc-pcf2127-i2c",
  305. .of_match_table = of_match_ptr(pcf2127_of_match),
  306. },
  307. .probe = pcf2127_i2c_probe,
  308. .id_table = pcf2127_i2c_id,
  309. };
  310. static int pcf2127_i2c_register_driver(void)
  311. {
  312. return i2c_add_driver(&pcf2127_i2c_driver);
  313. }
  314. static void pcf2127_i2c_unregister_driver(void)
  315. {
  316. i2c_del_driver(&pcf2127_i2c_driver);
  317. }
  318. #else
  319. static int pcf2127_i2c_register_driver(void)
  320. {
  321. return 0;
  322. }
  323. static void pcf2127_i2c_unregister_driver(void)
  324. {
  325. }
  326. #endif
  327. #if IS_ENABLED(CONFIG_SPI_MASTER)
  328. static struct spi_driver pcf2127_spi_driver;
  329. static int pcf2127_spi_probe(struct spi_device *spi)
  330. {
  331. static const struct regmap_config config = {
  332. .reg_bits = 8,
  333. .val_bits = 8,
  334. .read_flag_mask = 0xa0,
  335. .write_flag_mask = 0x20,
  336. };
  337. struct regmap *regmap;
  338. regmap = devm_regmap_init_spi(spi, &config);
  339. if (IS_ERR(regmap)) {
  340. dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
  341. __func__, PTR_ERR(regmap));
  342. return PTR_ERR(regmap);
  343. }
  344. return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name,
  345. spi_get_device_id(spi)->driver_data);
  346. }
  347. static const struct spi_device_id pcf2127_spi_id[] = {
  348. { "pcf2127", 1 },
  349. { "pcf2129", 0 },
  350. { }
  351. };
  352. MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
  353. static struct spi_driver pcf2127_spi_driver = {
  354. .driver = {
  355. .name = "rtc-pcf2127-spi",
  356. .of_match_table = of_match_ptr(pcf2127_of_match),
  357. },
  358. .probe = pcf2127_spi_probe,
  359. .id_table = pcf2127_spi_id,
  360. };
  361. static int pcf2127_spi_register_driver(void)
  362. {
  363. return spi_register_driver(&pcf2127_spi_driver);
  364. }
  365. static void pcf2127_spi_unregister_driver(void)
  366. {
  367. spi_unregister_driver(&pcf2127_spi_driver);
  368. }
  369. #else
  370. static int pcf2127_spi_register_driver(void)
  371. {
  372. return 0;
  373. }
  374. static void pcf2127_spi_unregister_driver(void)
  375. {
  376. }
  377. #endif
  378. static int __init pcf2127_init(void)
  379. {
  380. int ret;
  381. ret = pcf2127_i2c_register_driver();
  382. if (ret) {
  383. pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
  384. return ret;
  385. }
  386. ret = pcf2127_spi_register_driver();
  387. if (ret) {
  388. pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
  389. pcf2127_i2c_unregister_driver();
  390. }
  391. return ret;
  392. }
  393. module_init(pcf2127_init)
  394. static void __exit pcf2127_exit(void)
  395. {
  396. pcf2127_spi_unregister_driver();
  397. pcf2127_i2c_unregister_driver();
  398. }
  399. module_exit(pcf2127_exit)
  400. MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
  401. MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
  402. MODULE_LICENSE("GPL v2");