pcf2127.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2016 by NXP Semiconductors Inc.
  3. * Date & Time support for PCF2127 RTC
  4. */
  5. /* #define DEBUG */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <dm.h>
  9. #include <i2c.h>
  10. #include <log.h>
  11. #include <rtc.h>
  12. #define PCF2127_REG_CTRL1 0x00
  13. #define PCF2127_REG_CTRL2 0x01
  14. #define PCF2127_REG_CTRL3 0x02
  15. #define PCF2127_REG_SC 0x03
  16. #define PCF2127_REG_MN 0x04
  17. #define PCF2127_REG_HR 0x05
  18. #define PCF2127_REG_DM 0x06
  19. #define PCF2127_REG_DW 0x07
  20. #define PCF2127_REG_MO 0x08
  21. #define PCF2127_REG_YR 0x09
  22. static int pcf2127_rtc_read(struct udevice *dev, uint offset, u8 *buffer, uint len)
  23. {
  24. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  25. struct i2c_msg msg;
  26. int ret;
  27. /* Set the address of the start register to be read */
  28. ret = dm_i2c_write(dev, offset, NULL, 0);
  29. if (ret < 0)
  30. return ret;
  31. /* Read register's data */
  32. msg.addr = chip->chip_addr;
  33. msg.flags |= I2C_M_RD;
  34. msg.len = len;
  35. msg.buf = buffer;
  36. return dm_i2c_xfer(dev, &msg, 1);
  37. }
  38. static int pcf2127_rtc_write(struct udevice *dev, uint offset,
  39. const u8 *buffer, uint len)
  40. {
  41. return dm_i2c_write(dev, offset, buffer, len);
  42. }
  43. static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
  44. {
  45. uchar buf[7] = {0};
  46. int i = 0, ret;
  47. /* hours, minutes and seconds */
  48. buf[i++] = bin2bcd(tm->tm_sec);
  49. buf[i++] = bin2bcd(tm->tm_min);
  50. buf[i++] = bin2bcd(tm->tm_hour);
  51. buf[i++] = bin2bcd(tm->tm_mday);
  52. buf[i++] = tm->tm_wday & 0x07;
  53. /* month, 1 - 12 */
  54. buf[i++] = bin2bcd(tm->tm_mon);
  55. /* year */
  56. buf[i++] = bin2bcd(tm->tm_year % 100);
  57. /* write register's data */
  58. ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i);
  59. return ret;
  60. }
  61. static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
  62. {
  63. int ret = 0;
  64. uchar buf[10] = { PCF2127_REG_CTRL1 };
  65. ret = pcf2127_rtc_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
  66. if (ret < 0)
  67. return ret;
  68. if (buf[PCF2127_REG_CTRL3] & 0x04)
  69. puts("### Warning: RTC Low Voltage - date/time not reliable\n");
  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);
  73. tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
  74. tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F);
  75. tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900;
  76. if (tm->tm_year < 1970)
  77. tm->tm_year += 100; /* assume we are in 1970...2069 */
  78. tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
  79. tm->tm_yday = 0;
  80. tm->tm_isdst = 0;
  81. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  82. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  83. tm->tm_hour, tm->tm_min, tm->tm_sec);
  84. return ret;
  85. }
  86. static int pcf2127_rtc_reset(struct udevice *dev)
  87. {
  88. /*Doing nothing here*/
  89. return 0;
  90. }
  91. static const struct rtc_ops pcf2127_rtc_ops = {
  92. .get = pcf2127_rtc_get,
  93. .set = pcf2127_rtc_set,
  94. .reset = pcf2127_rtc_reset,
  95. .read = pcf2127_rtc_read,
  96. .write = pcf2127_rtc_write,
  97. };
  98. static const struct udevice_id pcf2127_rtc_ids[] = {
  99. { .compatible = "pcf2127-rtc" },
  100. { }
  101. };
  102. U_BOOT_DRIVER(rtc_pcf2127) = {
  103. .name = "rtc-pcf2127",
  104. .id = UCLASS_RTC,
  105. .of_match = pcf2127_rtc_ids,
  106. .ops = &pcf2127_rtc_ops,
  107. };