fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / drivers / rtc / rtc_as3514.c
blob8597d138fb4d1af9e76a4b1f326b8eef5d176104
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Barry Wardell
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdbool.h>
22 #include "config.h"
23 #include "rtc.h"
24 #include "as3514.h"
25 #include "ascodec.h"
27 /* AMS Sansas start counting from Jan 1st 1970 instead of 1980 (not as3525v2) */
28 #if (CONFIG_CPU==AS3525)
29 #define SECS_ADJUST 315532800 /* seconds between 1970-1-1 and 1980-1-1 */
30 #elif (CONFIG_CPU==AS3525v2)
31 #define SECS_ADJUST 315532800 - (2*365*24*3600) - 26*(24*3600) + 7*3600 + 25*60
32 #else
33 #define SECS_ADJUST 0
34 #endif
36 #define MINUTE_SECONDS 60
37 #define HOUR_SECONDS 3600
38 #define DAY_SECONDS 86400
39 #define WEEK_SECONDS 604800
40 #define YEAR_SECONDS 31536000
41 #define LEAP_YEAR_SECONDS 31622400
43 /* Days in each month */
44 static unsigned int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
46 static inline bool is_leapyear(int year)
48 if( ((year%4)==0) && (((year%100)!=0) || ((year%400)==0)) )
49 return true;
50 else
51 return false;
54 void rtc_init(void)
58 int rtc_read_datetime(struct tm *tm)
60 char tmp[4];
61 int i, year, mday, hour, min;
62 unsigned int seconds;
64 /* RTC_AS3514's slave address is 0x46*/
65 for (i = 0; i < 4; i++){
66 tmp[i] = ascodec_read(AS3514_RTC_0 + i);
68 seconds = tmp[0] + (tmp[1]<<8) + (tmp[2]<<16) + (tmp[3]<<24);
69 seconds -= SECS_ADJUST;
71 /* Convert seconds since Jan-1-1980 to format compatible with
72 * get_time() from firmware/common/timefuncs.c */
74 /* weekday */
75 tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
77 /* Year */
78 year = 1980;
79 while (seconds >= LEAP_YEAR_SECONDS)
81 if (is_leapyear(year)){
82 seconds -= LEAP_YEAR_SECONDS;
83 } else {
84 seconds -= YEAR_SECONDS;
87 year++;
90 if (is_leapyear(year)) {
91 days_in_month[1] = 29;
92 } else {
93 days_in_month[1] = 28;
94 if(seconds>YEAR_SECONDS){
95 year++;
96 seconds -= YEAR_SECONDS;
99 tm->tm_year = year%100 + 100;
101 /* Month */
102 for (i = 0; i < 12; i++)
104 if (seconds < days_in_month[i]*DAY_SECONDS){
105 tm->tm_mon = i;
106 break;
109 seconds -= days_in_month[i]*DAY_SECONDS;
112 /* Month Day */
113 mday = seconds/DAY_SECONDS;
114 seconds -= mday*DAY_SECONDS;
115 tm->tm_mday = mday + 1; /* 1 ... 31 */
117 /* Hour */
118 hour = seconds/HOUR_SECONDS;
119 seconds -= hour*HOUR_SECONDS;
120 tm->tm_hour = hour;
122 /* Minute */
123 min = seconds/MINUTE_SECONDS;
124 seconds -= min*MINUTE_SECONDS;
125 tm->tm_min = min;
127 /* Second */
128 tm->tm_sec = seconds;
130 return 7;
133 int rtc_write_datetime(const struct tm *tm)
135 int i, year;
136 unsigned int year_days = 0;
137 unsigned int month_days = 0;
138 unsigned int seconds = 0;
140 year = 2000 + tm->tm_year - 100;
142 if(is_leapyear(year)) {
143 days_in_month[1] = 29;
144 } else {
145 days_in_month[1] = 28;
148 /* Number of days in months gone by this year*/
149 for(i = 0; i < tm->tm_mon; i++){
150 month_days += days_in_month[i];
153 /* Number of days in years gone by since 1-Jan-1980 */
154 year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
156 /* Convert to seconds since 1-Jan-1980 */
157 seconds = tm->tm_sec
158 + tm->tm_min*MINUTE_SECONDS
159 + tm->tm_hour*HOUR_SECONDS
160 + (tm->tm_mday-1)*DAY_SECONDS
161 + month_days*DAY_SECONDS
162 + year_days*DAY_SECONDS;
163 seconds += SECS_ADJUST;
165 /* Send data to RTC */
166 for (i=0; i<4; i++){
167 ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
169 return 1;