Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de> |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <rtc.h> |
| 8 | #include <spi.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 9 | #include <power/pmic.h> |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 10 | #include <fsl_pmic.h> |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 11 | |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 12 | int rtc_get(struct rtc_time *rtc) |
| 13 | { |
| 14 | u32 day1, day2, time; |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 15 | int tim, i = 0; |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 16 | struct pmic *p = pmic_get("FSL_PMIC"); |
Stefano Babic | 07735d1 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 17 | int ret; |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 18 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 19 | if (!p) |
| 20 | return -1; |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 21 | do { |
Stefano Babic | 07735d1 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 22 | ret = pmic_reg_read(p, REG_RTC_DAY, &day1); |
| 23 | if (ret < 0) |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 24 | return -1; |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 25 | |
Stefano Babic | 07735d1 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 26 | ret = pmic_reg_read(p, REG_RTC_TIME, &time); |
| 27 | if (ret < 0) |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 28 | return -1; |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 29 | |
Stefano Babic | 07735d1 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 30 | ret = pmic_reg_read(p, REG_RTC_DAY, &day2); |
| 31 | if (ret < 0) |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 32 | return -1; |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 33 | |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 34 | } while (day1 != day2 && i++ < 3); |
| 35 | |
| 36 | tim = day1 * 86400 + time; |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 37 | |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 38 | rtc_to_tm(tim, rtc); |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 39 | |
| 40 | rtc->tm_yday = 0; |
| 41 | rtc->tm_isdst = 0; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 46 | int rtc_set(struct rtc_time *rtc) |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 47 | { |
Stefano Babic | e043203 | 2010-04-16 17:11:19 +0200 | [diff] [blame] | 48 | u32 time, day; |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 49 | struct pmic *p = pmic_get("FSL_PMIC"); |
| 50 | if (!p) |
| 51 | return -1; |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 52 | |
Simon Glass | 4283e84 | 2015-04-20 12:37:19 -0600 | [diff] [blame] | 53 | time = rtc_mktime(rtc); |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 54 | day = time / 86400; |
| 55 | time %= 86400; |
| 56 | |
Stefano Babic | 07735d1 | 2011-10-02 18:38:43 +0200 | [diff] [blame] | 57 | pmic_reg_write(p, REG_RTC_DAY, day); |
| 58 | pmic_reg_write(p, REG_RTC_TIME, time); |
Jean-Christophe PLAGNIOL-VILLARD | 97a2e10 | 2008-09-01 23:06:23 +0200 | [diff] [blame] | 59 | |
Magnus Lilja | de7c7a7 | 2009-11-11 19:56:58 +0100 | [diff] [blame] | 60 | return 0; |
Guennadi Liakhovetski | d438749 | 2008-04-15 14:15:30 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void rtc_reset(void) |
| 64 | { |
| 65 | } |