1 // SPDX-License-Identifier: GPL-2.0
3 * SII Semiconductor Corporation S35392A RTC driver.
5 * Copyright (c) 2017, General Electric Company
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/bitrev.h>
25 #include <linux/delay.h>
27 #define S35390A_CHIP_ADDR 0x30
29 #define S35390A_CMD_STATUS1 0x0
30 #define S35390A_CMD_STATUS2 0x1
31 #define S35390A_CMD_TIME1 0x2
32 #define S35390A_CMD_TIME2 0x3
33 #define S35390A_CMD_INT2_REG1 0x5
35 #define S35390A_BYTE_YEAR 0
36 #define S35390A_BYTE_MONTH 1
37 #define S35390A_BYTE_DAY 2
38 #define S35390A_BYTE_WDAY 3
39 #define S35390A_BYTE_HOURS 4
40 #define S35390A_BYTE_MINS 5
41 #define S35390A_BYTE_SECS 6
43 /* flags for STATUS1 */
44 #define S35390A_FLAG_POC 0x01
45 #define S35390A_FLAG_BLD 0x02
46 #define S35390A_FLAG_INT2 0x04
47 #define S35390A_FLAG_24H 0x40
48 #define S35390A_FLAG_RESET 0x80
51 * If either BLD or POC is set, then the chip has lost power long enough for
52 * the time value to become invalid.
54 #define S35390A_LOW_VOLTAGE (S35390A_FLAG_POC | S35390A_FLAG_BLD)
56 /*---------------------------------------------------------------------*/
60 #define DEBUGR(fmt, args...) printf(fmt, ##args)
62 #define DEBUGR(fmt, args...)
64 /*---------------------------------------------------------------------*/
67 #define DEV_TYPE struct udevice
74 #define DEV_TYPE struct ludevice
79 #define msleep(a) udelay(a * 1000)
83 static int s35392a_rtc_reset(DEV_TYPE *dev);
85 static int s35392a_rtc_read(DEV_TYPE *dev, u8 reg, u8 *buf, int len)
90 ret = dm_i2c_read(dev, reg, buf, len);
93 ret = i2c_read(S35390A_CHIP_ADDR | reg, 0, -1, buf, len);
99 static int s35392a_rtc_write(DEV_TYPE *dev, u8 reg, u8 *buf, int len)
104 ret = dm_i2c_write(dev, reg, buf, len);
107 ret = i2c_write(S35390A_CHIP_ADDR | reg, 0, 0, buf, len);
113 static int s35392a_rtc_read8(DEV_TYPE *dev, unsigned int reg)
118 ret = s35392a_rtc_read(dev, reg, &val, sizeof(val));
119 return ret < 0 ? ret : val;
122 static int s35392a_rtc_write8(DEV_TYPE *dev, unsigned int reg, int val)
127 ret = s35392a_rtc_write(dev, reg, &lval, sizeof(lval));
128 return ret < 0 ? ret : 0;
131 static int validate_time(const struct rtc_time *tm)
133 if ((tm->tm_year < 2000) || (tm->tm_year > 2099))
136 if ((tm->tm_mon < 1) || (tm->tm_mon > 12))
139 if ((tm->tm_mday < 1) || (tm->tm_mday > 31))
142 if ((tm->tm_wday < 0) || (tm->tm_wday > 6))
145 if ((tm->tm_hour < 0) || (tm->tm_hour > 23))
148 if ((tm->tm_min < 0) || (tm->tm_min > 59))
151 if ((tm->tm_sec < 0) || (tm->tm_sec > 59))
157 void s35392a_rtc_init(DEV_TYPE *dev)
161 status = s35392a_rtc_read8(dev, S35390A_CMD_STATUS1);
165 DEBUGR("init: S35390A_CMD_STATUS1: 0x%x\n", status);
167 lowvoltage = status & S35390A_LOW_VOLTAGE ? 1 : 0;
169 if (status & S35390A_FLAG_POC)
171 * Do not communicate for 0.5 seconds since the power-on
172 * detection circuit is in operation.
176 else if (!lowvoltage)
178 * If both POC and BLD are unset everything is fine.
183 printf("RTC low voltage detected\n");
185 if (!s35392a_rtc_reset(dev))
189 printf("Error RTC init.\n");
192 /* Get the current time from the RTC */
193 static int s35392a_rtc_get(DEV_TYPE *dev, struct rtc_time *tm)
199 DEBUGR("RTC low voltage detected\n");
203 ret = s35392a_rtc_read(dev, S35390A_CMD_TIME1, date, sizeof(date));
205 DEBUGR("Error reading date from RTC\n");
209 /* This chip returns the bits of each byte in reverse order */
210 for (i = 0; i < 7; ++i)
211 date[i] = bitrev8(date[i]);
213 tm->tm_sec = bcd2bin(date[S35390A_BYTE_SECS]);
214 tm->tm_min = bcd2bin(date[S35390A_BYTE_MINS]);
215 tm->tm_hour = bcd2bin(date[S35390A_BYTE_HOURS] & ~S35390A_FLAG_24H);
216 tm->tm_wday = bcd2bin(date[S35390A_BYTE_WDAY]);
217 tm->tm_mday = bcd2bin(date[S35390A_BYTE_DAY]);
218 tm->tm_mon = bcd2bin(date[S35390A_BYTE_MONTH]);
219 tm->tm_year = bcd2bin(date[S35390A_BYTE_YEAR]) + 2000;
221 DEBUGR("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
222 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
223 tm->tm_hour, tm->tm_min, tm->tm_sec);
229 static int s35392a_rtc_set(DEV_TYPE *dev, const struct rtc_time *tm)
235 DEBUGR("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
236 tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
237 tm->tm_hour, tm->tm_min, tm->tm_sec);
239 ret = validate_time(tm);
243 /* We support only 24h mode */
244 ret = s35392a_rtc_read8(dev, S35390A_CMD_STATUS1);
249 ret = s35392a_rtc_write8(dev, S35390A_CMD_STATUS1,
250 status | S35390A_FLAG_24H);
254 date[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 2000);
255 date[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon);
256 date[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
257 date[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
258 date[S35390A_BYTE_HOURS] = bin2bcd(tm->tm_hour);
259 date[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
260 date[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
262 /* This chip expects the bits of each byte to be in reverse order */
263 for (i = 0; i < 7; ++i)
264 date[i] = bitrev8(date[i]);
266 ret = s35392a_rtc_write(dev, S35390A_CMD_TIME1, date, sizeof(date));
268 DEBUGR("Error writing date to RTC\n");
272 /* Now we have time. Reset the low voltage status */
279 static int s35392a_rtc_reset(DEV_TYPE *dev)
283 unsigned int initcount = 0;
285 buf = S35390A_FLAG_RESET;
288 ret = s35392a_rtc_write8(dev, S35390A_CMD_STATUS1, buf);
292 ret = s35392a_rtc_read8(dev, S35390A_CMD_STATUS1);
298 lowvoltage = buf & S35390A_LOW_VOLTAGE ? 1 : 0;
300 if (buf & S35390A_LOW_VOLTAGE) {
301 /* Try up to five times to reset the chip */
313 #ifndef CONFIG_DM_RTC
315 int rtc_get(struct rtc_time *tm)
317 return s35392a_rtc_get(&dev, tm);
320 int rtc_set(struct rtc_time *tm)
322 return s35392a_rtc_set(&dev, tm);
327 s35392a_rtc_reset(&dev);
332 s35392a_rtc_init(&dev);
337 static int s35392a_probe(struct udevice *dev)
339 #if defined(CONFIG_DM_RTC)
340 /* 3-bit "command", or register, is encoded within the device address.
342 i2c_set_chip_offset_len(dev, 0);
343 i2c_set_chip_addr_offset_mask(dev, 0x7);
346 s35392a_rtc_init(dev);
350 static const struct rtc_ops s35392a_rtc_ops = {
351 .get = s35392a_rtc_get,
352 .set = s35392a_rtc_set,
353 .read8 = s35392a_rtc_read8,
354 .write8 = s35392a_rtc_write8,
355 .reset = s35392a_rtc_reset,
358 static const struct udevice_id s35392a_rtc_ids[] = {
359 { .compatible = "sii,s35392a-rtc" },
360 { .compatible = "sii,s35392a" },
361 { .compatible = "s35392a" },
365 U_BOOT_DRIVER(s35392a_rtc) = {
366 .name = "s35392a_rtc",
368 .probe = s35392a_probe,
369 .of_match = s35392a_rtc_ids,
370 .ops = &s35392a_rtc_ops,