09bf365f635044b291c5e5855a77aec0378a7943
[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 enum rx_model {
28         model_rx_8025,
29         model_rx_8035,
30 };
31
32 /*
33  * RTC register addresses
34  */
35 #define RTC_SEC_REG_ADDR        0x00
36 #define RTC_MIN_REG_ADDR        0x01
37 #define RTC_HR_REG_ADDR         0x02
38 #define RTC_DAY_REG_ADDR        0x03
39 #define RTC_DATE_REG_ADDR       0x04
40 #define RTC_MON_REG_ADDR        0x05
41 #define RTC_YR_REG_ADDR         0x06
42
43 #define RTC_CTL1_REG_ADDR       0x0e
44 #define RTC_CTL2_REG_ADDR       0x0f
45
46 /*
47  * Control register 1 bits
48  */
49 #define RTC_CTL1_BIT_2412       0x20
50
51 /*
52  * Control register 2 bits
53  */
54 #define RTC_CTL2_BIT_PON        0x10
55 #define RTC_CTL2_BIT_VDET       0x40
56 #define RTC_CTL2_BIT_XST        0x20
57 #define RTC_CTL2_BIT_VDSL       0x80
58
59 /*
60  * Note: the RX8025 I2C RTC requires register
61  * reads and write to consist of a single bus
62  * cycle. It is not allowed to write the register
63  * address in a first cycle that is terminated by
64  * a STOP condition. The chips needs a 'restart'
65  * sequence (start sequence without a prior stop).
66  */
67
68 #define rtc_read(reg) buf[(reg) & 0xf]
69
70 static int rtc_write(struct udevice *dev, uchar reg, uchar val);
71
72 static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
73 {
74         int xstp = ctrl2 & RTC_CTL2_BIT_XST;
75         /* XSTP bit has different polarity on RX-8025 vs RX-8035.
76          * RX-8025: 0 == oscillator stopped
77          * RX-8035: 1 == oscillator stopped
78          */
79
80         if (model == model_rx_8025)
81                 xstp = !xstp;
82
83         return xstp;
84 }
85
86 /*
87  * Get the current time from the RTC
88  */
89 static int rx8025_rtc_get(struct udevice *dev, struct rtc_time *tmp)
90 {
91         int rel = 0;
92         uchar sec, min, hour, mday, wday, mon, year, ctl2;
93         uchar buf[16];
94
95         if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
96                 printf("Error reading from RTC\n");
97                 return -EIO;
98         }
99
100         sec = rtc_read(RTC_SEC_REG_ADDR);
101         min = rtc_read(RTC_MIN_REG_ADDR);
102         hour = rtc_read(RTC_HR_REG_ADDR);
103         wday = rtc_read(RTC_DAY_REG_ADDR);
104         mday = rtc_read(RTC_DATE_REG_ADDR);
105         mon = rtc_read(RTC_MON_REG_ADDR);
106         year = rtc_read(RTC_YR_REG_ADDR);
107
108         DEBUGR("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
109                "hr: %02x min: %02x sec: %02x\n",
110                year, mon, mday, wday, hour, min, sec);
111
112         /* dump status */
113         ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
114         if (ctl2 & RTC_CTL2_BIT_PON) {
115                 printf("RTC: power-on detected\n");
116                 rel = -1;
117         }
118
119         if (ctl2 & RTC_CTL2_BIT_VDET) {
120                 printf("RTC: voltage drop detected\n");
121                 rel = -1;
122         }
123         if (rx8025_is_osc_stopped(dev->driver_data, ctl2)) {
124                 printf("RTC: oscillator stop detected\n");
125                 rel = -1;
126         }
127
128         tmp->tm_sec  = bcd2bin(sec & 0x7F);
129         tmp->tm_min  = bcd2bin(min & 0x7F);
130         if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
131                 tmp->tm_hour = bcd2bin(hour & 0x3F);
132         else
133                 tmp->tm_hour = bcd2bin(hour & 0x1F) % 12 +
134                                ((hour & 0x20) ? 12 : 0);
135
136         tmp->tm_mday = bcd2bin (mday & 0x3F);
137         tmp->tm_mon  = bcd2bin (mon & 0x1F);
138         tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
139         tmp->tm_wday = bcd2bin (wday & 0x07);
140         tmp->tm_yday = 0;
141         tmp->tm_isdst= 0;
142
143         DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
144                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
145                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
146
147         return rel;
148 }
149
150 /*
151  * Set the RTC
152  */
153 static int rx8025_rtc_set(struct udevice *dev, const struct rtc_time *tmp)
154 {
155         DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
156                tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
157                tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
158
159         if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
160                 printf("WARNING: year should be between 1970 and 2069!\n");
161
162         if (rtc_write(dev, RTC_YR_REG_ADDR, bin2bcd(tmp->tm_year % 100)))
163                 return -EIO;
164
165         if (rtc_write(dev, RTC_MON_REG_ADDR, bin2bcd(tmp->tm_mon)))
166                 return -EIO;
167
168         if (rtc_write(dev, RTC_DAY_REG_ADDR, bin2bcd(tmp->tm_wday)))
169                 return -EIO;
170
171         if (rtc_write(dev, RTC_DATE_REG_ADDR, bin2bcd(tmp->tm_mday)))
172                 return -EIO;
173
174         if (rtc_write(dev, RTC_HR_REG_ADDR, bin2bcd(tmp->tm_hour)))
175                 return -EIO;
176
177         if (rtc_write(dev, RTC_MIN_REG_ADDR, bin2bcd(tmp->tm_min)))
178                 return -EIO;
179
180         if (rtc_write(dev, RTC_SEC_REG_ADDR, bin2bcd(tmp->tm_sec)))
181                 return -EIO;
182
183         return rtc_write(dev, RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
184 }
185
186 /*
187  * Reset the RTC
188  */
189 static int rx8025_rtc_reset(struct udevice *dev)
190 {
191         uchar buf[16];
192         uchar ctl2;
193
194         if (dm_i2c_read(dev, 0, buf, sizeof(buf))) {
195                 printf("Error reading from RTC\n");
196                 return -EIO;
197         }
198
199         ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
200         ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
201
202         if (dev->driver_data == model_rx_8035)
203                 ctl2 &= ~(RTC_CTL2_BIT_XST);
204         else
205                 ctl2 |= RTC_CTL2_BIT_XST;
206
207         return rtc_write(dev, RTC_CTL2_REG_ADDR, ctl2);
208 }
209
210 /*
211  * Helper functions
212  */
213 static int rtc_write(struct udevice *dev, uchar reg, uchar val)
214 {
215         uchar buf[2];
216         buf[0] = reg << 4;
217         buf[1] = val;
218
219         if (dm_i2c_write(dev, 0, buf, 2)) {
220                 printf("Error writing to RTC\n");
221                 return -EIO;
222         }
223
224         return 0;
225 }
226
227 static int rx8025_probe(struct udevice *dev)
228 {
229         uchar buf[16];
230         int ret = 0;
231
232         if (i2c_get_chip_offset_len(dev) != 1)
233                 ret = i2c_set_chip_offset_len(dev, 1);
234
235         if (ret)
236                 return ret;
237
238         return dm_i2c_read(dev, 0, buf, sizeof(buf));
239 }
240
241 static const struct rtc_ops rx8025_rtc_ops = {
242         .get = rx8025_rtc_get,
243         .set = rx8025_rtc_set,
244         .reset = rx8025_rtc_reset,
245 };
246
247 static const struct udevice_id rx8025_rtc_ids[] = {
248         { .compatible = "epson,rx8025", .data = model_rx_8025 },
249         { .compatible = "epson,rx8035", .data = model_rx_8035 },
250         { }
251 };
252
253 U_BOOT_DRIVER(rx8025_rtc) = {
254         .name     = "rx8025_rtc",
255         .id           = UCLASS_RTC,
256         .probe    = rx8025_probe,
257         .of_match = rx8025_rtc_ids,
258         .ops      = &rx8025_rtc_ops,
259 };