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