FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / drivers / rtc / rtc_e8564.c
blobb4cd4b91f87fbed0df3d59d1d5320040f3b4f0d6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "config.h"
23 #include "i2c.h"
24 #include "rtc.h"
25 #include "kernel.h"
26 #include "system.h"
27 #include "i2c-pp.h"
28 #include <stdbool.h>
30 /* RTC registers */
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 */
41 #define RTC_TIE 0x01
42 #define RTC_AIE 0x02
43 #define RTC_TF 0x04
44 #define RTC_AF 0x08
45 #define RTC_TI_TP 0x10
47 /* Alarm registers flags */
48 #define RTC_AE 0x80
50 /* Timer register flags */
51 #define RTC_TE 0x80
53 bool rtc_lock_alarm_clear=true;
55 void rtc_init(void)
57 unsigned char tmp;
58 int rv;
60 /* initialize Control 1 register */
61 tmp = 0;
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)
73 unsigned char tmp;
74 int read;
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 */
81 tmp=buf[3];
82 buf[3]=buf[4];
83 buf[4]=tmp;
85 return read;
88 int rtc_write_datetime(unsigned char* buf)
90 int i;
91 unsigned char tmp;
93 /* swap wday and mday to be compatible with
94 * set_time() in firmware/common/timefuncs.c */
95 tmp=buf[3];
96 buf[3]=buf[4];
97 buf[4]=tmp;
99 for (i=0;i<7;i++){
100 pp_i2c_send(0x51, 0x02+i,buf[i]);
102 return 1;
105 void rtc_set_alarm(int h, int m)
107 unsigned char buf[4]={0};
108 int rv=0;
109 int i=0;
111 /* clear alarm interrupt */
112 rv = i2c_readbytes(0x51,RTC_CTRL2,1,buf);
113 buf[0] &= RTC_AF;
114 pp_i2c_send(0x51, RTC_CTRL2,buf[0]);
116 /* prepare new alarm */
117 if( m >= 0 )
118 buf[0] = (((m/10) << 4) | m%10);
119 else
120 /* ignore minutes comparison query */
121 buf[0] = RTC_AE;
123 if( h >= 0 )
124 buf[1] = (((h/10) << 4) | h%10);
125 else
126 /* ignore hours comparison query */
127 buf[1] = RTC_AE;
129 /* ignore day and wday */
130 buf[2] = RTC_AE;
131 buf[3] = RTC_AE;
133 /* write new alarm */
134 for(;i<4;i++)
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)
154 unsigned char tmp=0;
155 int rv=0;
157 if(enable)
159 /* enable alarm interrupt */
160 rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp);
161 tmp |= RTC_AIE;
162 tmp &= ~RTC_AF;
163 pp_i2c_send(0x51, RTC_CTRL2,tmp);
165 else
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 */
170 return false;
171 rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp);
172 tmp &= ~(RTC_AIE | RTC_AF);
173 pp_i2c_send(0x51, RTC_CTRL2,tmp);
176 return false;
179 bool rtc_check_alarm_started(bool release_alarm)
181 static bool alarm_state, run_before = false;
182 unsigned char tmp=0;
183 bool started;
184 int rv=0;
186 if (run_before)
188 started = alarm_state;
189 alarm_state &= ~release_alarm;
191 else
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);
205 run_before = true;
206 rtc_lock_alarm_clear = false;
209 return started;
212 bool rtc_check_alarm_flag(void)
214 unsigned char tmp=0;
215 int rv=0;
217 /* read Control 2 register which contains alarm flag */
218 rv = i2c_readbytes(0x51,RTC_CTRL2,1,&tmp);
220 return (tmp & RTC_AF);