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