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