1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing, Uwe Freese, Laurent Baum,
11 * Przemyslaw Holubowski
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
31 #define RTC_CTRL1 0x00
32 #define RTC_CTRL2 0x01
33 #define RTC_ALARM_MINUTES 0x09
34 #define RTC_ALARM_HOURS 0x0A
35 #define RTC_ALARM_DAY 0x0B
36 #define RTC_ALARM_WDAY 0x0C
37 #define RTC_TIMER_CTRL 0x0E
38 #define RTC_TIMER 0x0F
40 /* Control 2 register flags */
45 #define RTC_TI_TP 0x10
47 /* Alarm registers flags */
50 /* Timer register flags */
53 bool rtc_lock_alarm_clear
=true;
60 /* initialize Control 1 register */
62 pp_i2c_send(0x51, RTC_CTRL1
,tmp
);
64 /* read value of the Control 2 register - we'll need it to preserve alarm and timer interrupt assertion flags */
65 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,&tmp
);
66 /* preserve alarm and timer interrupt flags */
67 tmp
&= (RTC_TF
| RTC_AF
| RTC_TIE
| RTC_AIE
);
68 pp_i2c_send(0x51, RTC_CTRL2
,tmp
);
71 int rtc_read_datetime(unsigned char* buf
)
76 /*RTC_E8564's slave address is 0x51*/
77 read
= i2c_readbytes(0x51,0x02,7,buf
);
79 /* swap wday and mday to be compatible with
80 * get_time() from firmware/common/timefuncs.c */
88 int rtc_write_datetime(unsigned char* buf
)
93 /* swap wday and mday to be compatible with
94 * set_time() in firmware/common/timefuncs.c */
100 pp_i2c_send(0x51, 0x02+i
,buf
[i
]);
105 void rtc_set_alarm(int h
, int m
)
107 unsigned char buf
[4]={0};
111 /* clear alarm interrupt */
112 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,buf
);
114 pp_i2c_send(0x51, RTC_CTRL2
,buf
[0]);
116 /* prepare new alarm */
118 buf
[0] = (((m
/10) << 4) | m
%10);
120 /* ignore minutes comparison query */
124 buf
[1] = (((h
/10) << 4) | h
%10);
126 /* ignore hours comparison query */
129 /* ignore day and wday */
133 /* write new alarm */
135 pp_i2c_send(0x51, RTC_ALARM_MINUTES
+i
,buf
[i
]);
137 /* note: alarm is not enabled at the point */
140 void rtc_get_alarm(int *h
, int *m
)
142 unsigned char buf
[4]={0};
144 /* get alarm preset */
145 i2c_readbytes(0x51, RTC_ALARM_MINUTES
,4,buf
);
147 *m
= ((buf
[0] >> 4) & 0x7)*10 + (buf
[0] & 0x0f);
148 *h
= ((buf
[1] >> 4) & 0x3)*10 + (buf
[1] & 0x0f);
152 bool rtc_enable_alarm(bool enable
)
159 /* enable alarm interrupt */
160 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,&tmp
);
163 pp_i2c_send(0x51, RTC_CTRL2
,tmp
);
167 /* disable alarm interrupt */
168 if(rtc_lock_alarm_clear
)
169 /* lock disabling alarm before it was checked whether or not the unit was started by RTC alarm */
171 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,&tmp
);
172 tmp
&= ~(RTC_AIE
| RTC_AF
);
173 pp_i2c_send(0x51, RTC_CTRL2
,tmp
);
179 bool rtc_check_alarm_started(bool release_alarm
)
181 static bool alarm_state
, run_before
= false;
188 started
= alarm_state
;
189 alarm_state
&= ~release_alarm
;
193 /* read Control 2 register which contains alarm flag */
194 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,&tmp
);
196 alarm_state
= started
= ( (tmp
& RTC_AF
) && (tmp
& RTC_AIE
) );
198 if(release_alarm
&& started
)
200 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,&tmp
);
201 /* clear alarm interrupt enable and alarm flag */
202 tmp
&= ~(RTC_AF
| RTC_AIE
);
203 pp_i2c_send(0x51, RTC_CTRL2
,tmp
);
206 rtc_lock_alarm_clear
= false;
212 bool rtc_check_alarm_flag(void)
217 /* read Control 2 register which contains alarm flag */
218 rv
= i2c_readbytes(0x51,RTC_CTRL2
,1,&tmp
);
220 return (tmp
& RTC_AF
);