blob: 9e396bcdae95489583798019ef8d4eb236776849 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02002/*
3 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02004 */
5
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +02006#include <rtc.h>
7#include <spi.h>
Łukasz Majewski1c6dba12012-11-13 03:21:55 +00008#include <power/pmic.h>
Stefano Babice0432032010-04-16 17:11:19 +02009#include <fsl_pmic.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020010
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020011int rtc_get(struct rtc_time *rtc)
12{
13 u32 day1, day2, time;
Stefano Babice0432032010-04-16 17:11:19 +020014 int tim, i = 0;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000015 struct pmic *p = pmic_get("FSL_PMIC");
Stefano Babic07735d12011-10-02 18:38:43 +020016 int ret;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020017
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000018 if (!p)
19 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020020 do {
Stefano Babic07735d12011-10-02 18:38:43 +020021 ret = pmic_reg_read(p, REG_RTC_DAY, &day1);
22 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020023 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020024
Stefano Babic07735d12011-10-02 18:38:43 +020025 ret = pmic_reg_read(p, REG_RTC_TIME, &time);
26 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020027 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020028
Stefano Babic07735d12011-10-02 18:38:43 +020029 ret = pmic_reg_read(p, REG_RTC_DAY, &day2);
30 if (ret < 0)
Stefano Babice0432032010-04-16 17:11:19 +020031 return -1;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020032
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020033 } while (day1 != day2 && i++ < 3);
34
35 tim = day1 * 86400 + time;
Stefano Babice0432032010-04-16 17:11:19 +020036
Simon Glass12994662015-04-20 12:37:18 -060037 rtc_to_tm(tim, rtc);
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020038
39 rtc->tm_yday = 0;
40 rtc->tm_isdst = 0;
41
42 return 0;
43}
44
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020045int rtc_set(struct rtc_time *rtc)
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020046{
Stefano Babice0432032010-04-16 17:11:19 +020047 u32 time, day;
Łukasz Majewski1c6dba12012-11-13 03:21:55 +000048 struct pmic *p = pmic_get("FSL_PMIC");
49 if (!p)
50 return -1;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020051
Simon Glass4283e842015-04-20 12:37:19 -060052 time = rtc_mktime(rtc);
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020053 day = time / 86400;
54 time %= 86400;
55
Stefano Babic07735d12011-10-02 18:38:43 +020056 pmic_reg_write(p, REG_RTC_DAY, day);
57 pmic_reg_write(p, REG_RTC_TIME, time);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +020058
Magnus Liljade7c7a72009-11-11 19:56:58 +010059 return 0;
Guennadi Liakhovetskid4387492008-04-15 14:15:30 +020060}
61
62void rtc_reset(void)
63{
64}