Redo raising the priority of the codec (and voice) thread to fix audio dropouts under...
[kugel-rb.git] / firmware / drivers / rtc / rtc_as3514.c
blob44ef3cc4a178f97eebbf8c7d3e3e92f0a477f9ae
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 struct {
56 unsigned int seconds; /* total seconds to wakeup */
57 int hour; /* wake-up hour */
58 int min; /* wake-up minute */
59 bool enabled; /* alarm enabled or not */
60 unsigned char flag; /* flag used by the OF */
61 } alarm;
63 void rtc_set_alarm(int h, int m)
65 alarm.hour = h;
66 alarm.min = m;
69 void rtc_get_alarm(int *h, int *m)
71 *h = alarm.hour;
72 *m = alarm.min;
75 /* Reads the AS3543 wakeup register */
76 static void alarm_read(void)
78 unsigned char buf[6];
79 unsigned int i;
80 int oldstatus;
82 /* read raw data */
83 oldstatus = disable_irq_save();
84 ascodec_read(0);
85 for (i = 0; i < sizeof(buf); i++) {
86 buf[i] = ascodec_read(AS3543_WAKEUP);
88 restore_irq(oldstatus);
90 /* decode */
91 alarm.seconds = buf[0] | (buf[1] << 8) | ((buf[2] & 0x7F) << 16);
92 alarm.enabled = buf[2] & (1 << 7);
93 alarm.flag = buf[3];
94 alarm.hour = buf[4];
95 alarm.min = buf[5];
98 /* Writes the AS3543 wakeup register */
99 static void alarm_write(void)
101 unsigned char buf[6];
102 unsigned int i;
103 int oldstatus;
105 /* encode */
106 buf[0] = alarm.seconds & 0xFF;
107 buf[1] = (alarm.seconds >> 8) & 0xFF;
108 buf[2] = ((alarm.seconds >> 16) & 0x7F) | (alarm.enabled ? (1 << 7) : 0);
109 buf[3] = alarm.flag;
110 buf[4] = alarm.hour;
111 buf[5] = alarm.min;
113 /* write raw data */
114 oldstatus = disable_irq_save();
115 ascodec_read(0);
116 for (i = 0; i < sizeof(buf); i++) {
117 ascodec_write(AS3543_WAKEUP, buf[i]);
119 restore_irq(oldstatus);
122 void rtc_alarm_poweroff(void)
124 if(!alarm.enabled)
125 return;
127 struct tm tm;
128 rtc_read_datetime(&tm);
129 int hours = alarm.hour - tm.tm_hour;
130 int mins = alarm.min - tm.tm_min;
131 if(mins < 0)
133 mins += 60;
134 hours -= 1;
136 if(hours < 0)
137 hours += 24;
139 uint32_t seconds = hours*3600 + mins*60;
140 if(seconds == 0)
141 seconds = 24*3600;
143 seconds -= tm.tm_sec;
145 /* disable MCLK, it is a wakeup source and prevents proper shutdown */
146 CGU_AUDIO = (2 << 0) | (1 << 11);
147 CGU_PLLBSUP = (1 << 2) | (1 << 3);
149 /* write wakeup register */
150 alarm.seconds = seconds;
151 alarm.enabled = true;
152 alarm_write();
154 /* enable heartbeat watchdog */
155 ascodec_write(AS3514_SYSTEM, (1<<3) | (1<<0));
157 /* In_Cntr : disable heartbeat source */
158 ascodec_write_pmu(0x1a, 4, ascodec_read_pmu(0x1a, 4) & ~(3<<2));
160 while(1);
163 void rtc_enable_alarm(bool enable)
165 alarm.enabled = enable;
168 bool rtc_check_alarm_started(bool release_alarm)
170 /* read wakeup register and check if alarm was enabled */
171 alarm_read();
172 if (!alarm.enabled) {
173 return false;
176 alarm.enabled = !release_alarm;
177 alarm_write();
179 struct tm tm;
180 rtc_read_datetime(&tm);
182 /* were we powered up at the programmed time ? */
183 return alarm.hour == tm.tm_hour && alarm.min == tm.tm_min;
186 bool rtc_check_alarm_flag(void)
188 /* We don't need to do anything special if it has already fired */
189 return false;
191 #endif /* HAVE_RTC_ALARM */
193 void rtc_init(void)
197 int rtc_read_datetime(struct tm *tm)
199 char tmp[4];
200 int i, year, mday, hour, min;
201 unsigned int seconds;
203 /* RTC_AS3514's slave address is 0x46*/
204 for (i = 0; i < 4; i++){
205 tmp[i] = ascodec_read(AS3514_RTC_0 + i);
207 seconds = tmp[0] + (tmp[1]<<8) + (tmp[2]<<16) + (tmp[3]<<24);
208 seconds -= SECS_ADJUST;
210 /* Convert seconds since Jan-1-1980 to format compatible with
211 * get_time() from firmware/common/timefuncs.c */
213 /* weekday */
214 tm->tm_wday = ((seconds % WEEK_SECONDS) / DAY_SECONDS + 2) % 7;
216 /* Year */
217 year = 1980;
218 while (seconds >= LEAP_YEAR_SECONDS)
220 if (is_leapyear(year)){
221 seconds -= LEAP_YEAR_SECONDS;
222 } else {
223 seconds -= YEAR_SECONDS;
226 year++;
229 if (is_leapyear(year)) {
230 days_in_month[1] = 29;
231 } else {
232 days_in_month[1] = 28;
233 if(seconds>YEAR_SECONDS){
234 year++;
235 seconds -= YEAR_SECONDS;
238 tm->tm_year = year%100 + 100;
240 /* Month */
241 for (i = 0; i < 12; i++)
243 if (seconds < days_in_month[i]*DAY_SECONDS){
244 tm->tm_mon = i;
245 break;
248 seconds -= days_in_month[i]*DAY_SECONDS;
251 /* Month Day */
252 mday = seconds/DAY_SECONDS;
253 seconds -= mday*DAY_SECONDS;
254 tm->tm_mday = mday + 1; /* 1 ... 31 */
256 /* Hour */
257 hour = seconds/HOUR_SECONDS;
258 seconds -= hour*HOUR_SECONDS;
259 tm->tm_hour = hour;
261 /* Minute */
262 min = seconds/MINUTE_SECONDS;
263 seconds -= min*MINUTE_SECONDS;
264 tm->tm_min = min;
266 /* Second */
267 tm->tm_sec = seconds;
269 return 7;
272 int rtc_write_datetime(const struct tm *tm)
274 int i, year;
275 unsigned int year_days = 0;
276 unsigned int month_days = 0;
277 unsigned int seconds = 0;
279 year = 2000 + tm->tm_year - 100;
281 if(is_leapyear(year)) {
282 days_in_month[1] = 29;
283 } else {
284 days_in_month[1] = 28;
287 /* Number of days in months gone by this year*/
288 for(i = 0; i < tm->tm_mon; i++){
289 month_days += days_in_month[i];
292 /* Number of days in years gone by since 1-Jan-1980 */
293 year_days = 365*(tm->tm_year-100+20) + (tm->tm_year-100-1)/4 + 6;
295 /* Convert to seconds since 1-Jan-1980 */
296 seconds = tm->tm_sec
297 + tm->tm_min*MINUTE_SECONDS
298 + tm->tm_hour*HOUR_SECONDS
299 + (tm->tm_mday-1)*DAY_SECONDS
300 + month_days*DAY_SECONDS
301 + year_days*DAY_SECONDS;
302 seconds += SECS_ADJUST;
304 /* Send data to RTC */
305 for (i=0; i<4; i++){
306 ascodec_write(AS3514_RTC_0 + i, ((seconds >> (8 * i)) & 0xff));
308 return 1;