rtc-pcf2127.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * An I2C driver for the NXP PCF2127 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. * http://www.nxp.com/documents/data_sheet/PCF2127AT.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/bcd.h>
  17. #include <linux/rtc.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
  22. #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
  23. #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
  24. #define PCF2127_REG_CTRL3_BLF BIT(2)
  25. #define PCF2127_REG_SC (0x03) /* datetime */
  26. #define PCF2127_REG_MN (0x04)
  27. #define PCF2127_REG_HR (0x05)
  28. #define PCF2127_REG_DM (0x06)
  29. #define PCF2127_REG_DW (0x07)
  30. #define PCF2127_REG_MO (0x08)
  31. #define PCF2127_REG_YR (0x09)
  32. #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */
  33. static struct i2c_driver pcf2127_driver;
  34. struct pcf2127 {
  35. struct rtc_device *rtc;
  36. };
  37. /*
  38. * In the routines that deal directly with the pcf2127 hardware, we use
  39. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
  40. */
  41. static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm)
  42. {
  43. unsigned char buf[10] = { PCF2127_REG_CTRL1 };
  44. /* read registers */
  45. if (i2c_master_send(client, buf, 1) != 1 ||
  46. i2c_master_recv(client, buf, sizeof(buf)) != sizeof(buf)) {
  47. dev_err(&client->dev, "%s: read error\n", __func__);
  48. return -EIO;
  49. }
  50. if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
  51. dev_info(&client->dev,
  52. "low voltage detected, check/replace RTC battery.\n");
  53. if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
  54. /*
  55. * no need clear the flag here,
  56. * it will be cleared once the new date is saved
  57. */
  58. dev_warn(&client->dev,
  59. "oscillator stop detected, date/time is not reliable\n");
  60. return -EINVAL;
  61. }
  62. dev_dbg(&client->dev,
  63. "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
  64. "sec=%02x, min=%02x, hr=%02x, "
  65. "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
  66. __func__,
  67. buf[0], buf[1], buf[2],
  68. buf[3], buf[4], buf[5],
  69. buf[6], buf[7], buf[8], buf[9]);
  70. tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
  71. tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
  72. tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
  73. tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
  74. tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
  75. tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  76. tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
  77. if (tm->tm_year < 70)
  78. tm->tm_year += 100; /* assume we are in 1970...2069 */
  79. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  80. "mday=%d, mon=%d, year=%d, wday=%d\n",
  81. __func__,
  82. tm->tm_sec, tm->tm_min, tm->tm_hour,
  83. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  84. return rtc_valid_tm(tm);
  85. }
  86. static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  87. {
  88. unsigned char buf[8];
  89. int i = 0, err;
  90. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  91. "mday=%d, mon=%d, year=%d, wday=%d\n",
  92. __func__,
  93. tm->tm_sec, tm->tm_min, tm->tm_hour,
  94. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  95. /* start register address */
  96. buf[i++] = PCF2127_REG_SC;
  97. /* hours, minutes and seconds */
  98. buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */
  99. buf[i++] = bin2bcd(tm->tm_min);
  100. buf[i++] = bin2bcd(tm->tm_hour);
  101. buf[i++] = bin2bcd(tm->tm_mday);
  102. buf[i++] = tm->tm_wday & 0x07;
  103. /* month, 1 - 12 */
  104. buf[i++] = bin2bcd(tm->tm_mon + 1);
  105. /* year */
  106. buf[i++] = bin2bcd(tm->tm_year % 100);
  107. /* write register's data */
  108. err = i2c_master_send(client, buf, i);
  109. if (err != i) {
  110. dev_err(&client->dev,
  111. "%s: err=%d", __func__, err);
  112. return -EIO;
  113. }
  114. return 0;
  115. }
  116. #ifdef CONFIG_RTC_INTF_DEV
  117. static int pcf2127_rtc_ioctl(struct device *dev,
  118. unsigned int cmd, unsigned long arg)
  119. {
  120. struct i2c_client *client = to_i2c_client(dev);
  121. unsigned char buf = PCF2127_REG_CTRL3;
  122. int touser;
  123. int ret;
  124. switch (cmd) {
  125. case RTC_VL_READ:
  126. ret = i2c_master_send(client, &buf, 1);
  127. if (!ret)
  128. ret = -EIO;
  129. if (ret < 0)
  130. return ret;
  131. ret = i2c_master_recv(client, &buf, 1);
  132. if (!ret)
  133. ret = -EIO;
  134. if (ret < 0)
  135. return ret;
  136. touser = buf & PCF2127_REG_CTRL3_BLF ? 1 : 0;
  137. if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
  138. return -EFAULT;
  139. return 0;
  140. default:
  141. return -ENOIOCTLCMD;
  142. }
  143. }
  144. #else
  145. #define pcf2127_rtc_ioctl NULL
  146. #endif
  147. static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
  148. {
  149. return pcf2127_get_datetime(to_i2c_client(dev), tm);
  150. }
  151. static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
  152. {
  153. return pcf2127_set_datetime(to_i2c_client(dev), tm);
  154. }
  155. static const struct rtc_class_ops pcf2127_rtc_ops = {
  156. .ioctl = pcf2127_rtc_ioctl,
  157. .read_time = pcf2127_rtc_read_time,
  158. .set_time = pcf2127_rtc_set_time,
  159. };
  160. static int pcf2127_probe(struct i2c_client *client,
  161. const struct i2c_device_id *id)
  162. {
  163. struct pcf2127 *pcf2127;
  164. dev_dbg(&client->dev, "%s\n", __func__);
  165. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  166. return -ENODEV;
  167. pcf2127 = devm_kzalloc(&client->dev, sizeof(struct pcf2127),
  168. GFP_KERNEL);
  169. if (!pcf2127)
  170. return -ENOMEM;
  171. i2c_set_clientdata(client, pcf2127);
  172. pcf2127->rtc = devm_rtc_device_register(&client->dev,
  173. pcf2127_driver.driver.name,
  174. &pcf2127_rtc_ops, THIS_MODULE);
  175. return PTR_ERR_OR_ZERO(pcf2127->rtc);
  176. }
  177. static const struct i2c_device_id pcf2127_id[] = {
  178. { "pcf2127", 0 },
  179. { }
  180. };
  181. MODULE_DEVICE_TABLE(i2c, pcf2127_id);
  182. #ifdef CONFIG_OF
  183. static const struct of_device_id pcf2127_of_match[] = {
  184. { .compatible = "nxp,pcf2127" },
  185. {}
  186. };
  187. MODULE_DEVICE_TABLE(of, pcf2127_of_match);
  188. #endif
  189. static struct i2c_driver pcf2127_driver = {
  190. .driver = {
  191. .name = "rtc-pcf2127",
  192. .of_match_table = of_match_ptr(pcf2127_of_match),
  193. },
  194. .probe = pcf2127_probe,
  195. .id_table = pcf2127_id,
  196. };
  197. module_i2c_driver(pcf2127_driver);
  198. MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
  199. MODULE_DESCRIPTION("NXP PCF2127 RTC driver");
  200. MODULE_LICENSE("GPL v2");