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