18f3a8428SMeng Yi /*
28f3a8428SMeng Yi * Copyright (C) 2016 by NXP Semiconductors Inc.
38f3a8428SMeng Yi * Date & Time support for PCF2127 RTC
48f3a8428SMeng Yi */
58f3a8428SMeng Yi
68f3a8428SMeng Yi /* #define DEBUG */
78f3a8428SMeng Yi
88f3a8428SMeng Yi #include <common.h>
98f3a8428SMeng Yi #include <command.h>
108f3a8428SMeng Yi #include <dm.h>
118f3a8428SMeng Yi #include <i2c.h>
128f3a8428SMeng Yi #include <rtc.h>
138f3a8428SMeng Yi
14*45a0194bSMeng Yi #define PCF2127_REG_CTRL1 0x00
15*45a0194bSMeng Yi #define PCF2127_REG_CTRL2 0x01
16*45a0194bSMeng Yi #define PCF2127_REG_CTRL3 0x02
17*45a0194bSMeng Yi #define PCF2127_REG_SC 0x03
18*45a0194bSMeng Yi #define PCF2127_REG_MN 0x04
19*45a0194bSMeng Yi #define PCF2127_REG_HR 0x05
20*45a0194bSMeng Yi #define PCF2127_REG_DM 0x06
21*45a0194bSMeng Yi #define PCF2127_REG_DW 0x07
22*45a0194bSMeng Yi #define PCF2127_REG_MO 0x08
23*45a0194bSMeng Yi #define PCF2127_REG_YR 0x09
248f3a8428SMeng Yi
pcf2127_rtc_set(struct udevice * dev,const struct rtc_time * tm)258f3a8428SMeng Yi static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm)
268f3a8428SMeng Yi {
278f3a8428SMeng Yi uchar buf[8];
28*45a0194bSMeng Yi int i = 0, ret;
298f3a8428SMeng Yi
308f3a8428SMeng Yi /* start register address */
318f3a8428SMeng Yi buf[i++] = PCF2127_REG_SC;
328f3a8428SMeng Yi
338f3a8428SMeng Yi /* hours, minutes and seconds */
348f3a8428SMeng Yi buf[i++] = bin2bcd(tm->tm_sec);
358f3a8428SMeng Yi buf[i++] = bin2bcd(tm->tm_min);
368f3a8428SMeng Yi buf[i++] = bin2bcd(tm->tm_hour);
378f3a8428SMeng Yi buf[i++] = bin2bcd(tm->tm_mday);
388f3a8428SMeng Yi buf[i++] = tm->tm_wday & 0x07;
398f3a8428SMeng Yi
408f3a8428SMeng Yi /* month, 1 - 12 */
418f3a8428SMeng Yi buf[i++] = bin2bcd(tm->tm_mon + 1);
428f3a8428SMeng Yi
438f3a8428SMeng Yi /* year */
448f3a8428SMeng Yi buf[i++] = bin2bcd(tm->tm_year % 100);
458f3a8428SMeng Yi
468f3a8428SMeng Yi /* write register's data */
47*45a0194bSMeng Yi ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
488f3a8428SMeng Yi
49*45a0194bSMeng Yi return ret;
508f3a8428SMeng Yi }
518f3a8428SMeng Yi
pcf2127_rtc_get(struct udevice * dev,struct rtc_time * tm)528f3a8428SMeng Yi static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm)
538f3a8428SMeng Yi {
54*45a0194bSMeng Yi int ret = 0;
558f3a8428SMeng Yi uchar buf[10] = { PCF2127_REG_CTRL1 };
568f3a8428SMeng Yi
57*45a0194bSMeng Yi ret = dm_i2c_write(dev, PCF2127_REG_CTRL1, buf, 1);
58*45a0194bSMeng Yi if (ret < 0)
59*45a0194bSMeng Yi return ret;
60*45a0194bSMeng Yi ret = dm_i2c_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf));
61*45a0194bSMeng Yi if (ret < 0)
62*45a0194bSMeng Yi return ret;
638f3a8428SMeng Yi
648f3a8428SMeng Yi if (buf[PCF2127_REG_CTRL3] & 0x04)
658f3a8428SMeng Yi puts("### Warning: RTC Low Voltage - date/time not reliable\n");
668f3a8428SMeng Yi
678f3a8428SMeng Yi tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
688f3a8428SMeng Yi tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
698f3a8428SMeng Yi tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F);
708f3a8428SMeng Yi tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
718f3a8428SMeng Yi tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1;
728f3a8428SMeng Yi tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900;
738f3a8428SMeng Yi if (tm->tm_year < 1970)
748f3a8428SMeng Yi tm->tm_year += 100; /* assume we are in 1970...2069 */
758f3a8428SMeng Yi tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
768f3a8428SMeng Yi tm->tm_yday = 0;
778f3a8428SMeng Yi tm->tm_isdst = 0;
788f3a8428SMeng Yi
798f3a8428SMeng Yi debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
808f3a8428SMeng Yi tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
818f3a8428SMeng Yi tm->tm_hour, tm->tm_min, tm->tm_sec);
828f3a8428SMeng Yi
83*45a0194bSMeng Yi return ret;
848f3a8428SMeng Yi }
858f3a8428SMeng Yi
pcf2127_rtc_reset(struct udevice * dev)868f3a8428SMeng Yi static int pcf2127_rtc_reset(struct udevice *dev)
878f3a8428SMeng Yi {
888f3a8428SMeng Yi /*Doing nothing here*/
89*45a0194bSMeng Yi
908f3a8428SMeng Yi return 0;
918f3a8428SMeng Yi }
928f3a8428SMeng Yi
938f3a8428SMeng Yi static const struct rtc_ops pcf2127_rtc_ops = {
948f3a8428SMeng Yi .get = pcf2127_rtc_get,
958f3a8428SMeng Yi .set = pcf2127_rtc_set,
968f3a8428SMeng Yi .reset = pcf2127_rtc_reset,
978f3a8428SMeng Yi };
988f3a8428SMeng Yi
998f3a8428SMeng Yi static const struct udevice_id pcf2127_rtc_ids[] = {
1008f3a8428SMeng Yi { .compatible = "pcf2127-rtc" },
1018f3a8428SMeng Yi { }
1028f3a8428SMeng Yi };
1038f3a8428SMeng Yi
1048f3a8428SMeng Yi U_BOOT_DRIVER(rtc_pcf2127) = {
1058f3a8428SMeng Yi .name = "rtc-pcf2127",
1068f3a8428SMeng Yi .id = UCLASS_RTC,
1078f3a8428SMeng Yi .of_match = pcf2127_rtc_ids,
1088f3a8428SMeng Yi .ops = &pcf2127_rtc_ops,
1098f3a8428SMeng Yi };
110