]>
Commit | Line | Data |
---|---|---|
8f3a8428 MY |
1 | /* |
2 | * Copyright (C) 2016 by NXP Semiconductors Inc. | |
3 | * Date & Time support for PCF2127 RTC | |
4 | */ | |
5 | ||
6 | /* #define DEBUG */ | |
7 | ||
d678a59d | 8 | #include <common.h> |
8f3a8428 MY |
9 | #include <command.h> |
10 | #include <dm.h> | |
11 | #include <i2c.h> | |
f7ae49fc | 12 | #include <log.h> |
8f3a8428 MY |
13 | #include <rtc.h> |
14 | ||
45a0194b MY |
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 | |
8f3a8428 | 25 | |
a518dd63 | 26 | static int pcf2127_rtc_read(struct udevice *dev, uint offset, u8 *buffer, uint len) |
ef6c26d3 | 27 | { |
caa4daa2 | 28 | struct dm_i2c_chip *chip = dev_get_parent_plat(dev); |
ef6c26d3 CH |
29 | struct i2c_msg msg; |
30 | int ret; | |
31 | ||
32 | /* Set the address of the start register to be read */ | |
33 | ret = dm_i2c_write(dev, offset, NULL, 0); | |
34 | if (ret < 0) | |
35 | return ret; | |
36 | ||
37 | /* Read register's data */ | |
38 | msg.addr = chip->chip_addr; | |
39 | msg.flags |= I2C_M_RD; | |
40 | msg.len = len; | |
41 | msg.buf = buffer; | |
42 | ||
43 | return dm_i2c_xfer(dev, &msg, 1); | |
44 | } | |
45 | ||
29f965a7 RV |
46 | static int pcf2127_rtc_write(struct udevice *dev, uint offset, |
47 | const u8 *buffer, uint len) | |
48 | { | |
49 | return dm_i2c_write(dev, offset, buffer, len); | |
50 | } | |
51 | ||
8f3a8428 MY |
52 | static int pcf2127_rtc_set(struct udevice *dev, const struct rtc_time *tm) |
53 | { | |
ef6c26d3 | 54 | uchar buf[7] = {0}; |
45a0194b | 55 | int i = 0, ret; |
8f3a8428 | 56 | |
8f3a8428 MY |
57 | /* hours, minutes and seconds */ |
58 | buf[i++] = bin2bcd(tm->tm_sec); | |
59 | buf[i++] = bin2bcd(tm->tm_min); | |
60 | buf[i++] = bin2bcd(tm->tm_hour); | |
61 | buf[i++] = bin2bcd(tm->tm_mday); | |
62 | buf[i++] = tm->tm_wday & 0x07; | |
63 | ||
64 | /* month, 1 - 12 */ | |
b8a42e0f | 65 | buf[i++] = bin2bcd(tm->tm_mon); |
8f3a8428 MY |
66 | |
67 | /* year */ | |
68 | buf[i++] = bin2bcd(tm->tm_year % 100); | |
69 | ||
70 | /* write register's data */ | |
ef6c26d3 | 71 | ret = dm_i2c_write(dev, PCF2127_REG_SC, buf, i); |
8f3a8428 | 72 | |
45a0194b | 73 | return ret; |
8f3a8428 MY |
74 | } |
75 | ||
76 | static int pcf2127_rtc_get(struct udevice *dev, struct rtc_time *tm) | |
77 | { | |
45a0194b | 78 | int ret = 0; |
8f3a8428 MY |
79 | uchar buf[10] = { PCF2127_REG_CTRL1 }; |
80 | ||
a518dd63 | 81 | ret = pcf2127_rtc_read(dev, PCF2127_REG_CTRL1, buf, sizeof(buf)); |
45a0194b MY |
82 | if (ret < 0) |
83 | return ret; | |
8f3a8428 MY |
84 | |
85 | if (buf[PCF2127_REG_CTRL3] & 0x04) | |
86 | puts("### Warning: RTC Low Voltage - date/time not reliable\n"); | |
87 | ||
88 | tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); | |
89 | tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); | |
90 | tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); | |
91 | tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); | |
b8a42e0f | 92 | tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F); |
8f3a8428 MY |
93 | tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]) + 1900; |
94 | if (tm->tm_year < 1970) | |
95 | tm->tm_year += 100; /* assume we are in 1970...2069 */ | |
96 | tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; | |
97 | tm->tm_yday = 0; | |
98 | tm->tm_isdst = 0; | |
99 | ||
100 | debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n", | |
101 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, | |
102 | tm->tm_hour, tm->tm_min, tm->tm_sec); | |
103 | ||
45a0194b | 104 | return ret; |
8f3a8428 MY |
105 | } |
106 | ||
107 | static int pcf2127_rtc_reset(struct udevice *dev) | |
108 | { | |
109 | /*Doing nothing here*/ | |
45a0194b | 110 | |
8f3a8428 MY |
111 | return 0; |
112 | } | |
113 | ||
114 | static const struct rtc_ops pcf2127_rtc_ops = { | |
115 | .get = pcf2127_rtc_get, | |
116 | .set = pcf2127_rtc_set, | |
117 | .reset = pcf2127_rtc_reset, | |
a518dd63 | 118 | .read = pcf2127_rtc_read, |
29f965a7 | 119 | .write = pcf2127_rtc_write, |
8f3a8428 MY |
120 | }; |
121 | ||
122 | static const struct udevice_id pcf2127_rtc_ids[] = { | |
7c67ef3f VO |
123 | { .compatible = "nxp,pcf2127" }, |
124 | { .compatible = "nxp,pcf2129" }, | |
125 | { .compatible = "nxp,pca2129" }, | |
8f3a8428 MY |
126 | { } |
127 | }; | |
128 | ||
129 | U_BOOT_DRIVER(rtc_pcf2127) = { | |
130 | .name = "rtc-pcf2127", | |
131 | .id = UCLASS_RTC, | |
132 | .of_match = pcf2127_rtc_ids, | |
133 | .ops = &pcf2127_rtc_ops, | |
134 | }; |