36e5b0122cf311d006caab3b1974176eaffb8b3a
[platform/kernel/u-boot.git] / drivers / rtc / rx8025.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007
4  * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
5  */
6
7 /*
8  * Epson RX8025 RTC driver.
9  */
10
11 #include <common.h>
12 #include <command.h>
13 #include <dm.h>
14 #include <i2c.h>
15 #include <rtc.h>
16
17 /*---------------------------------------------------------------------*/
18 #undef DEBUG_RTC
19
20 #ifdef DEBUG_RTC
21 #define DEBUGR(fmt,args...) printf(fmt ,##args)
22 #else
23 #define DEBUGR(fmt,args...)
24 #endif
25 /*---------------------------------------------------------------------*/
26
27 /*
28  * RTC register addresses
29  */
30 #define RTC_SEC_REG_ADDR        0x00
31 #define RTC_MIN_REG_ADDR        0x01
32 #define RTC_HR_REG_ADDR         0x02
33 #define RTC_DAY_REG_ADDR        0x03
34 #define RTC_DATE_REG_ADDR       0x04
35 #define RTC_MON_REG_ADDR        0x05
36 #define RTC_YR_REG_ADDR         0x06
37
38 #define RTC_CTL1_REG_ADDR       0x0e
39 #define RTC_CTL2_REG_ADDR       0x0f
40
41 /*
42  * Control register 1 bits
43  */
44 #define RTC_CTL1_BIT_2412       0x20
45
46 /*
47  * Control register 2 bits
48  */
49 #define RTC_CTL2_BIT_PON        0x10
50 #define RTC_CTL2_BIT_VDET       0x40
51 #define RTC_CTL2_BIT_XST        0x20
52 #define RTC_CTL2_BIT_VDSL       0x80
53
54 /*
55  * Note: the RX8025 I2C RTC requires register
56  * reads and write to consist of a single bus
57  * cycle. It is not allowed to write the register
58  * address in a first cycle that is terminated by
59  * a STOP condition. The chips needs a 'restart'
60  * sequence (start sequence without a prior stop).
61  */
62
63 #define rtc_read(reg) buf[(reg) & 0xf]
64
65 static int rtc_write(struct udevice *dev, uchar reg, uchar val);
66
67 /*
68  * Get the current time from the RTC
69  */
70 static int rx8025_rtc_get(struct udevice *dev, struct rtc_time *tmp)
71 {
72         int rel = 0;
73         uchar sec, min, hour, mday, wday, mon, year, ctl2;
74         uchar buf[16];
75
76         if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
77                 printf("Error reading from RTC\n");
78                 return -EIO;
79         }
80
81         sec = rtc_read(RTC_SEC_REG_ADDR);
82         min = rtc_read(RTC_MIN_REG_ADDR);
83         hour = rtc_read(RTC_HR_REG_ADDR);
84         wday = rtc_read(RTC_DAY_REG_ADDR);
85         mday = rtc_read(RTC_DATE_REG_ADDR);
86         mon = rtc_read(RTC_MON_REG_ADDR);
87         year = rtc_read(RTC_YR_REG_ADDR);
88
89         DEBUGR("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
90                "hr: %02x min: %02x sec: %02x\n",
91                year, mon, mday, wday, hour, min, sec);
92
93         /* dump status */
94         ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
95         if (ctl2 & RTC_CTL2_BIT_PON) {
96                 printf("RTC: power-on detected\n");
97                 rel = -1;
98         }
99
100         if (ctl2 & RTC_CTL2_BIT_VDET) {
101                 printf("RTC: voltage drop detected\n");
102                 rel = -1;
103         }
104
105         if (!(ctl2 & RTC_CTL2_BIT_XST)) {
106                 printf("RTC: oscillator stop detected\n");
107                 rel = -1;
108         }
109
110         tmp->tm_sec  = bcd2bin(sec & 0x7F);
111         tmp->tm_min  = bcd2bin(min & 0x7F);
112         if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
113                 tmp->tm_hour = bcd2bin(hour & 0x3F);
114         else
115                 tmp->tm_hour = bcd2bin(hour & 0x1F) % 12 +
116                                ((hour & 0x20) ? 12 : 0);
117
118         tmp->tm_mday = bcd2bin (mday & 0x3F);
119         tmp->tm_mon  = bcd2bin (mon & 0x1F);
120         tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
121         tmp->tm_wday = bcd2bin (wday & 0x07);
122         tmp->tm_yday = 0;
123         tmp->tm_isdst= 0;
124
125         DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
126                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
127                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
128
129         return rel;
130 }
131
132 /*
133  * Set the RTC
134  */
135 static int rx8025_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
136 {
137         DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
138                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
139                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
140
141         if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
142                 printf("WARNING: year should be between 1970 and 2069!\n");
143
144         if (rtc_write(dev, RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100)))
145                 return -EIO;
146
147         if (rtc_write(dev, RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon)))
148                 return -EIO;
149
150         if (rtc_write(dev, RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday)))
151                 return -EIO;
152
153         if (rtc_write(dev, RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday)))
154                 return -EIO;
155
156         if (rtc_write(dev, RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour)))
157                 return -EIO;
158
159         if (rtc_write(dev, RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min)))
160                 return -EIO;
161
162         if (rtc_write(dev, RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec)))
163                 return -EIO;
164
165         return rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
166 }
167
168 /*
169  * Reset the RTC
170  */
171 static int rx8025_rtc_reset(struct udevice *dev)
172 {
173         uchar buf[16];
174         uchar ctl2;
175
176         if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
177                 printf("Error reading from RTC\n");
178                 return -EIO;
179         }
180
181         ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
182         ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
183         ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
184
185         return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2);
186 }
187
188 /*
189  * Helper functions
190  */
191 static int rtc_write(struct udevice *dev, uchar reg, uchar val)
192 {
193         uchar buf[2];
194         buf[0] = reg << 4;
195         buf[1] = val;
196
197         if (dm_i2c_write(dev, 0, buf, 2)) {
198                 printf("Error writing to RTC\n");
199                 return -EIO;
200         }
201
202         return 0;
203 }
204
205 static int rx8025_probe(struct udevice *dev)
206 {
207         uchar buf[16];
208         int ret = 0;
209
210         if (i2c_get_chip_offset_len(dev) != 1)
211                 ret = i2c_set_chip_offset_len(dev, 1);
212
213         if (ret)
214                 return ret;
215
216         return dm_i2c_read(dev, 0, buf, sizeof(buf));
217 }
218
219 static const struct rtc_ops rx8025_rtc_ops = {
220         .get = rx8025_rtc_get,
221         .set = rx8025_rtc_set,
222         .reset = rx8025_rtc_reset,
223 };
224
225 static const struct udevice_id rx8025_rtc_ids[] = {
226         { .compatible = "epson,rx8025" },
227         { }
228 };
229
230 U_BOOT_DRIVER(rx8010sj_rtc) = {
231         .name     = "rx8025_rtc",
232         .id           = UCLASS_RTC,
233         .probe    = rx8025_probe,
234         .of_match = rx8025_rtc_ids,
235         .ops      = &rx8025_rtc_ops,
236 };