rtc-pcf2127.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 DRV_VERSION "0.0.1"
  22. #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */
  23. #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */
  24. #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */
  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. static struct i2c_driver pcf2127_driver;
  33. struct pcf2127 {
  34. struct rtc_device *rtc;
  35. int voltage_low; /* indicates if a low_voltage was detected */
  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. struct pcf2127 *pcf2127 = i2c_get_clientdata(client);
  44. unsigned char buf[10] = { PCF2127_REG_CTRL1 };
  45. /* read registers */
  46. if (i2c_master_send(client, buf, 1) != 1 ||
  47. i2c_master_recv(client, buf, sizeof(buf)) != sizeof(buf)) {
  48. dev_err(&client->dev, "%s: read error\n", __func__);
  49. return -EIO;
  50. }
  51. if (buf[PCF2127_REG_CTRL3] & 0x04) {
  52. pcf2127->voltage_low = 1;
  53. dev_info(&client->dev,
  54. "low voltage detected, date/time is not reliable.\n");
  55. }
  56. dev_dbg(&client->dev,
  57. "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
  58. "sec=%02x, min=%02x, hr=%02x, "
  59. "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
  60. __func__,
  61. buf[0], buf[1], buf[2],
  62. buf[3], buf[4], buf[5],
  63. buf[6], buf[7], buf[8], buf[9]);
  64. tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
  65. tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
  66. tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
  67. tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
  68. tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
  69. tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
  70. tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
  71. if (tm->tm_year < 70)
  72. tm->tm_year += 100; /* assume we are in 1970...2069 */
  73. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  74. "mday=%d, mon=%d, year=%d, wday=%d\n",
  75. __func__,
  76. tm->tm_sec, tm->tm_min, tm->tm_hour,
  77. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  78. /* the clock can give out invalid datetime, but we cannot return
  79. * -EINVAL otherwise hwclock will refuse to set the time on bootup.
  80. */
  81. if (rtc_valid_tm(tm) < 0)
  82. dev_err(&client->dev, "retrieved date/time is not valid.\n");
  83. return 0;
  84. }
  85. static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  86. {
  87. unsigned char buf[8];
  88. int i = 0, err;
  89. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  90. "mday=%d, mon=%d, year=%d, wday=%d\n",
  91. __func__,
  92. tm->tm_sec, tm->tm_min, tm->tm_hour,
  93. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  94. /* start register address */
  95. buf[i++] = PCF2127_REG_SC;
  96. /* hours, minutes and seconds */
  97. buf[i++] = bin2bcd(tm->tm_sec);
  98. buf[i++] = bin2bcd(tm->tm_min);
  99. buf[i++] = bin2bcd(tm->tm_hour);
  100. buf[i++] = bin2bcd(tm->tm_mday);
  101. buf[i++] = tm->tm_wday & 0x07;
  102. /* month, 1 - 12 */
  103. buf[i++] = bin2bcd(tm->tm_mon + 1);
  104. /* year */
  105. buf[i++] = bin2bcd(tm->tm_year % 100);
  106. /* write register's data */
  107. err = i2c_master_send(client, buf, i);
  108. if (err != i) {
  109. dev_err(&client->dev,
  110. "%s: err=%d", __func__, err);
  111. return -EIO;
  112. }
  113. return 0;
  114. }
  115. #ifdef CONFIG_RTC_INTF_DEV
  116. static int pcf2127_rtc_ioctl(struct device *dev,
  117. unsigned int cmd, unsigned long arg)
  118. {
  119. struct pcf2127 *pcf2127 = i2c_get_clientdata(to_i2c_client(dev));
  120. switch (cmd) {
  121. case RTC_VL_READ:
  122. if (pcf2127->voltage_low)
  123. dev_info(dev, "low voltage detected, date/time is not reliable.\n");
  124. if (copy_to_user((void __user *)arg, &pcf2127->voltage_low,
  125. sizeof(int)))
  126. return -EFAULT;
  127. return 0;
  128. default:
  129. return -ENOIOCTLCMD;
  130. }
  131. }
  132. #else
  133. #define pcf2127_rtc_ioctl NULL
  134. #endif
  135. static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
  136. {
  137. return pcf2127_get_datetime(to_i2c_client(dev), tm);
  138. }
  139. static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
  140. {
  141. return pcf2127_set_datetime(to_i2c_client(dev), tm);
  142. }
  143. static const struct rtc_class_ops pcf2127_rtc_ops = {
  144. .ioctl = pcf2127_rtc_ioctl,
  145. .read_time = pcf2127_rtc_read_time,
  146. .set_time = pcf2127_rtc_set_time,
  147. };
  148. static int pcf2127_probe(struct i2c_client *client,
  149. const struct i2c_device_id *id)
  150. {
  151. struct pcf2127 *pcf2127;
  152. dev_dbg(&client->dev, "%s\n", __func__);
  153. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  154. return -ENODEV;
  155. pcf2127 = devm_kzalloc(&client->dev, sizeof(struct pcf2127),
  156. GFP_KERNEL);
  157. if (!pcf2127)
  158. return -ENOMEM;
  159. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  160. i2c_set_clientdata(client, pcf2127);
  161. pcf2127->rtc = devm_rtc_device_register(&client->dev,
  162. pcf2127_driver.driver.name,
  163. &pcf2127_rtc_ops, THIS_MODULE);
  164. return PTR_ERR_OR_ZERO(pcf2127->rtc);
  165. }
  166. static const struct i2c_device_id pcf2127_id[] = {
  167. { "pcf2127", 0 },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(i2c, pcf2127_id);
  171. #ifdef CONFIG_OF
  172. static const struct of_device_id pcf2127_of_match[] = {
  173. { .compatible = "nxp,pcf2127" },
  174. {}
  175. };
  176. MODULE_DEVICE_TABLE(of, pcf2127_of_match);
  177. #endif
  178. static struct i2c_driver pcf2127_driver = {
  179. .driver = {
  180. .name = "rtc-pcf2127",
  181. .owner = THIS_MODULE,
  182. .of_match_table = of_match_ptr(pcf2127_of_match),
  183. },
  184. .probe = pcf2127_probe,
  185. .id_table = pcf2127_id,
  186. };
  187. module_i2c_driver(pcf2127_driver);
  188. MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
  189. MODULE_DESCRIPTION("NXP PCF2127 RTC driver");
  190. MODULE_LICENSE("GPL");
  191. MODULE_VERSION(DRV_VERSION);