Sansa clip+: prevent an unnecessary OF database refresh when using the wake-up alarm...
[kugel-rb.git] / firmware / drivers / rtc / rtc_as3514.c
blob81137bda14e18d2b5a6daf2880163aed833aafd2
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 #ifdef HAVE_RTC_ALARM /* as3543 */
55 static int wakeup_h;
56 static int wakeup_m;
57 static bool alarm_enabled = false;
59 void rtc_set_alarm(int h, int m)
61 wakeup_h = h;
62 wakeup_m = m;
65 void rtc_get_alarm(int *h, int *m)
67 *h = wakeup_h;
68 *m = wakeup_m;
71 void rtc_alarm_poweroff(void)
73 if(!alarm_enabled)
74 return;
76 struct tm tm;
77 rtc_read_datetime(&tm);
78 int hours = wakeup_h - tm.tm_hour;
79 int mins = wakeup_m - tm.tm_min;
80 if(mins < 0)
82 mins += 60;
83 hours -= 1;
85 if(hours < 0)
86 hours += 24;
88 uint32_t seconds = hours*3600 + mins*60;
89 if(seconds == 0)
90 seconds = 24*3600;
92 seconds -= tm.tm_sec;
94 disable_irq();
96 ascodec_write(AS3543_WAKEUP, seconds);
97 seconds >>= 8;
98 ascodec_write(AS3543_WAKEUP, seconds);
99 seconds >>= 8;
100 seconds |= 1<<7; /* enable bit */
101 ascodec_write(AS3543_WAKEUP, seconds);
103 /* write 0x80 to prevent the OF refreshing its database from the microSD */
104 ascodec_write(AS3543_WAKEUP, 0x80);
106 /* write our desired time of wake up to detect power-up from RTC */
107 ascodec_write(AS3543_WAKEUP, wakeup_h);
108 ascodec_write(AS3543_WAKEUP, wakeup_m);
110 /* enable hearbeat watchdog */
111 ascodec_write(AS3514_SYSTEM, (1<<3) | (1<<0));
113 /* In_Cntr : disable hearbeat source */
114 ascodec_write_pmu(0x1a, 4, ascodec_read_pmu(0x1a, 4) & ~(3<<2));
116 while(1);
119 void rtc_enable_alarm(bool enable)
121 alarm_enabled = enable;
124 bool rtc_check_alarm_started(bool release_alarm)
126 (void) release_alarm;
128 /* 3 first reads give the 23 bits counter and enable bit */
129 ascodec_read(AS3543_WAKEUP); /* bits 7:0 */
130 ascodec_read(AS3543_WAKEUP); /* bits 15:8 */
131 if(!(ascodec_read(AS3543_WAKEUP) & (1<<7))) /* enable bit */
132 return false;
134 /* skip WAKEUP[3] which the OF uses for other purposes */
135 ascodec_read(AS3543_WAKEUP);
137 /* subsequent reads give the 16 bytes static SRAM */
138 wakeup_h = ascodec_read(AS3543_WAKEUP);
139 wakeup_m = ascodec_read(AS3543_WAKEUP);
141 struct tm tm;
142 rtc_read_datetime(&tm);
144 /* were we powered up at the programmed time ? */
145 return wakeup_h == tm.tm_hour && wakeup_m == tm.tm_min;
148 bool rtc_check_alarm_flag(void)
150 /* We don't need to do anything special if it has already fired */
151 return false;
153 #endif /* HAVE_RTC_ALARM */
155 void rtc_init(void)
159 int rtc_read_datetime(struct tm *tm)
161 char tmp[4];
162 int i, year, mday, hour, min;
163 unsigned int seconds;
165 /* RTC_AS3514's slave address is 0x46*/
166 for (i = 0; i < 4; i++){
167 tmp[i] = ascodec_read(AS3514_RTC_0 + i);
169 seconds = tmp[0] + (tmp[1]<<8) + (tmp[2]<<16) + (tmp[3]<<24);
170 seconds -= SECS_ADJUST;
172 /* Convert seconds since Jan-1-1980 to format compatible with
173 * get_time() from firmware/common/timefuncs.c */
175 /* weekday */
176 tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
178 /* Year */
179 year = 1980;
180 while (seconds >= LEAP_YEAR_SECONDS)
182 if (is_leapyear(year)){
183 seconds -= LEAP_YEAR_SECONDS;
184 } else {
185 seconds -= YEAR_SECONDS;
188 year++;
191 if (is_leapyear(year)) {
192 days_in_month[1] = 29;
193 } else {
194 days_in_month[1] = 28;
195 if(seconds>YEAR_SECONDS){
196 year++;
197 seconds -= YEAR_SECONDS;
200 tm->tm_year = year%100 + 100;
202 /* Month */
203 for (i = 0; i < 12; i++)
205 if (seconds < days_in_month[i]*DAY_SECONDS){
206 tm->tm_mon = i;
207 break;
210 seconds -= days_in_month[i]*DAY_SECONDS;
213 /* Month Day */
214 mday = seconds/DAY_SECONDS;
215 seconds -= mday*DAY_SECONDS;
216 tm->tm_mday = mday + 1; /* 1 ... 31 */
218 /* Hour */
219 hour = seconds/HOUR_SECONDS;
220 seconds -= hour*HOUR_SECONDS;
221 tm->tm_hour = hour;
223 /* Minute */
224 min = seconds/MINUTE_SECONDS;
225 seconds -= min*MINUTE_SECONDS;
226 tm->tm_min = min;
228 /* Second */
229 tm->tm_sec = seconds;
231 return 7;
234 int rtc_write_datetime(const struct tm *tm)
236 int i, year;
237 unsigned int year_days = 0;
238 unsigned int month_days = 0;
239 unsigned int seconds = 0;
241 year = 2000 + tm->tm_year - 100;
243 if(is_leapyear(year)) {
244 days_in_month[1] = 29;
245 } else {
246 days_in_month[1] = 28;
249 /* Number of days in months gone by this year*/
250 for(i = 0; i < tm->tm_mon; i++){
251 month_days += days_in_month[i];
254 /* Number of days in years gone by since 1-Jan-1980 */
255 year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
257 /* Convert to seconds since 1-Jan-1980 */
258 seconds = tm->tm_sec
259 + tm->tm_min*MINUTE_SECONDS
260 + tm->tm_hour*HOUR_SECONDS
261 + (tm->tm_mday-1)*DAY_SECONDS
262 + month_days*DAY_SECONDS
263 + year_days*DAY_SECONDS;
264 seconds += SECS_ADJUST;
266 /* Send data to RTC */
267 for (i=0; i<4; i++){
268 ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
270 return 1;