[U-Boot] [PATCH] Add RTC chip pcf2127 support

Meng Yi meng.yi at nxp.com
Tue Nov 29 06:32:53 CET 2016


This driver compatible with pcf2127 and pcf2129

Signed-off-by: Meng Yi <meng.yi at nxp.com>
---
 drivers/rtc/Makefile  |  1 +
 drivers/rtc/pcf2127.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 drivers/rtc/pcf2127.c

diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index fc38a3f..c919427 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -45,6 +45,7 @@ obj-$(CONFIG_RTC_MV) += mvrtc.o
 obj-$(CONFIG_RTC_MX27) += mx27rtc.o
 obj-$(CONFIG_RTC_MXS) += mxsrtc.o
 obj-$(CONFIG_RTC_PCF8563) += pcf8563.o
+obj-$(CONFIG_RTC_PCF2127) += pcf2127.o
 obj-$(CONFIG_RTC_PL031) += pl031.o
 obj-$(CONFIG_RTC_PT7C4338) += pt7c4338.o
 obj-$(CONFIG_RTC_RS5C372A) += rs5c372.o
diff --git a/drivers/rtc/pcf2127.c b/drivers/rtc/pcf2127.c
new file mode 100644
index 0000000..4cb0934
--- /dev/null
+++ b/drivers/rtc/pcf2127.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2016 by NXP Semiconductors Inc.
+ * Date & Time support for PCF2127 RTC
+ */
+
+/*	#define	DEBUG	*/
+
+#include <common.h>
+#include <command.h>
+#include <rtc.h>
+#include <i2c.h>
+
+#if defined(CONFIG_CMD_DATE)
+
+#define PCF2127_REG_CTRL1	(0x00)
+#define PCF2127_REG_CTRL2	(0x01)
+#define PCF2127_REG_CTRL3	(0x02)
+#define PCF2127_REG_SC		(0x03)  /* datetime */
+#define PCF2127_REG_MN		(0x04)
+#define PCF2127_REG_HR		(0x05)
+#define PCF2127_REG_DM		(0x06)
+#define PCF2127_REG_DW		(0x07)
+#define PCF2127_REG_MO		(0x08)
+#define PCF2127_REG_YR		(0x09)
+
+int rtc_set(struct rtc_time *tm)
+{
+	uchar buf[8];
+	int i = 0, err;
+
+	/* start register address */
+	buf[i++] = PCF2127_REG_SC;
+
+	/* hours, minutes and seconds */
+	buf[i++] = bin2bcd(tm->tm_sec);
+	buf[i++] = bin2bcd(tm->tm_min);
+	buf[i++] = bin2bcd(tm->tm_hour);
+	buf[i++] = bin2bcd(tm->tm_mday);
+	buf[i++] = tm->tm_wday & 0x07;
+
+	/* month, 1 - 12 */
+	buf[i++] = bin2bcd(tm->tm_mon + 1);
+
+	/* year */
+	buf[i++] = bin2bcd(tm->tm_year % 100);
+
+	/* write register's data */
+	err = i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
+			PCF2127_REG_CTRL1, 0, buf, sizeof(buf));
+	if (err != 0)
+		return -1;
+
+	return 0;
+}
+
+int rtc_get(struct rtc_time *tmp)
+{
+	int rel = 0;
+	uchar buf[10] = { PCF2127_REG_CTRL1 };
+
+	if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
+		      PCF2127_REG_CTRL1, 0, buf, 1) != 0)
+		return -1;
+	if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR,
+		     PCF2127_REG_CTRL1, 0, buf, sizeof(buf)) != 0)
+		return -1;
+
+	if (buf[PCF2127_REG_CTRL3] & 0x04)
+		puts("### Warning: RTC Low Voltage - date/time not reliable\n");
+
+	tmp->tm_sec  = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
+	tmp->tm_min  = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
+	tmp->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F);
+	tmp->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
+	tmp->tm_mon  = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1;
+	tmp->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900;
+	if (tmp->tm_year < 1970)
+		tmp->tm_year += 100;	/* assume we are in 1970...2069 */
+	tmp->tm_wday = buf[PCF2127_REG_DW] & 0x07;
+	tmp->tm_yday = 0;
+	tmp->tm_isdst = 0;
+
+	debug("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
+	      tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
+	      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
+
+	return rel;
+}
+
+void rtc_reset(void)
+{
+	/*Doing nothing here*/
+}
+#endif
-- 
2.1.0.27.g96db324



More information about the U-Boot mailing list