pcf2127.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <rtc.h>
  11. #define PCF2127_REG_CTRL1 (0x00)
  12. #define PCF2127_REG_CTRL2 (0x01)
  13. #define PCF2127_REG_CTRL3 (0x02)
  14. #define PCF2127_REG_SC (0x03) /* datetime */
  15. #define PCF2127_REG_MN (0x04)
  16. #define PCF2127_REG_HR (0x05)
  17. #define PCF2127_REG_DM (0x06)
  18. #define PCF2127_REG_DW (0x07)
  19. #define PCF2127_REG_MO (0x08)
  20. #define PCF2127_REG_YR (0x09)
  21. static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
  22. {
  23. uchar buf[8];
  24. int i = 0;
  25. /* start register address */
  26. buf[i++] = PCF2127_REG_SC;
  27. /* hours, minutes and seconds */
  28. buf[i++] = bin2bcd(tm->tm_sec);
  29. buf[i++] = bin2bcd(tm->tm_min);
  30. buf[i++] = bin2bcd(tm->tm_hour);
  31. buf[i++] = bin2bcd(tm->tm_mday);
  32. buf[i++] = tm->tm_wday & 0x07;
  33. /* month, 1 - 12 */
  34. buf[i++] = bin2bcd(tm->tm_mon + 1);
  35. /* year */
  36. buf[i++] = bin2bcd(tm->tm_year % 100);
  37. /* write register's data */
  38. if (dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)) < 0)
  39. return -1;
  40. return 0;
  41. }
  42. static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
  43. {
  44. int rel = 0;
  45. uchar buf[10] = { PCF2127_REG_CTRL1 };
  46. if (dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1) < 0)
  47. return -1;
  48. if (dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)) < 0)
  49. return -1;
  50. if (buf[PCF2127_REG_CTRL3] & 0x04)
  51. puts("### Warning: RTC Low Voltage - date/time not reliable\n");
  52. tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
  53. tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
  54. tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F);
  55. tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
  56. tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1;
  57. tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900;
  58. if (tm->tm_year < 1970)
  59. tm->tm_year += 100; /* assume we are in 1970...2069 */
  60. tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
  61. tm->tm_yday = 0;
  62. tm->tm_isdst = 0;
  63. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  64. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
  65. tm->tm_hour, tm->tm_min, tm->tm_sec);
  66. return rel;
  67. }
  68. static int pcf2127_rtc_reset(struct udevice *dev)
  69. {
  70. /*Doing nothing here*/
  71. return 0;
  72. }
  73. static const struct rtc_ops pcf2127_rtc_ops = {
  74. .get = pcf2127_rtc_get,
  75. .set = pcf2127_rtc_set,
  76. .reset = pcf2127_rtc_reset,
  77. };
  78. static const struct udevice_id pcf2127_rtc_ids[] = {
  79. { .compatible = "pcf2127-rtc" },
  80. { }
  81. };
  82. U_BOOT_DRIVER(rtc_pcf2127) = {
  83. .name = "rtc-pcf2127",
  84. .id = UCLASS_RTC,
  85. .of_match = pcf2127_rtc_ids,
  86. .ops = &pcf2127_rtc_ops,
  87. };