Add lang strings to hotkey setting macros
[kugel-rb.git] / firmware / drivers / rtc / rtc_e8564.c
blob8b9f6bfa424b4de1f9c9d08ff589e318c0898329
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_E8564's slave address is 0x51*/
31 #define RTC_ADDR 0x51
33 /* RTC registers */
34 #define RTC_CTRL1 0x00
35 #define RTC_CTRL2 0x01
36 #define RTC_ALARM_MINUTES 0x09
37 #define RTC_ALARM_HOURS 0x0A
38 #define RTC_ALARM_DAY 0x0B
39 #define RTC_ALARM_WDAY 0x0C
40 #define RTC_TIMER_CTRL 0x0E
41 #define RTC_TIMER 0x0F
43 /* Control 2 register flags */
44 #define RTC_TIE 0x01
45 #define RTC_AIE 0x02
46 #define RTC_TF 0x04
47 #define RTC_AF 0x08
48 #define RTC_TI_TP 0x10
50 /* Alarm registers flags */
51 #define RTC_AE 0x80
53 /* Timer register flags */
54 #define RTC_TE 0x80
56 bool rtc_lock_alarm_clear=true;
58 void rtc_init(void)
60 unsigned char tmp;
61 int rv;
63 /* initialize Control 1 register */
64 tmp = 0;
65 pp_i2c_send(RTC_ADDR, RTC_CTRL1, tmp);
67 /* read value of the Control 2 register - we'll need it to preserve alarm and timer interrupt assertion flags */
68 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
69 /* preserve alarm and timer interrupt flags */
70 tmp &= (RTC_TF | RTC_AF | RTC_TIE | RTC_AIE);
71 pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
74 int rtc_read_datetime(struct tm *tm)
76 int read;
77 unsigned char buf[7];
79 read = i2c_readbytes(RTC_ADDR, 0x02, sizeof(buf), buf);
81 /* convert from bcd, avoid getting extra bits */
82 tm->tm_sec = BCD2DEC(buf[0] & 0x7f);
83 tm->tm_min = BCD2DEC(buf[1] & 0x7f);
84 tm->tm_hour = BCD2DEC(buf[2] & 0x3f);
85 tm->tm_mday = BCD2DEC(buf[3] & 0x3f);
86 tm->tm_wday = BCD2DEC(buf[4] & 0x7);
87 tm->tm_mon = BCD2DEC(buf[5] & 0x1f) - 1;
88 tm->tm_year = BCD2DEC(buf[6]) + 100;
90 return read;
93 int rtc_write_datetime(const struct tm *tm)
95 unsigned int i;
96 unsigned char buf[7];
98 buf[0] = tm->tm_sec;
99 buf[1] = tm->tm_min;
100 buf[2] = tm->tm_hour;
101 buf[3] = tm->tm_mday;
102 buf[4] = tm->tm_wday;
103 buf[5] = tm->tm_mon + 1;
104 buf[6] = tm->tm_year - 100;
106 for (i = 0; i < sizeof(buf); i++)
107 pp_i2c_send(RTC_ADDR, 0x02 + i, DEC2BCD(buf[i]));
109 return 1;
112 void rtc_set_alarm(int h, int m)
114 unsigned char buf[4] = {0};
115 int i, rv;
117 /* clear alarm interrupt */
118 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, buf);
119 buf[0] &= RTC_AF;
120 pp_i2c_send(RTC_ADDR, RTC_CTRL2, buf[0]);
122 /* prepare new alarm */
123 if( m >= 0 )
124 buf[0] = DEC2BCD(m);
125 else
126 /* ignore minutes comparison query */
127 buf[0] = RTC_AE;
129 if( h >= 0 )
130 buf[1] = DEC2BCD(h);
131 else
132 /* ignore hours comparison query */
133 buf[1] = RTC_AE;
135 /* ignore day and wday */
136 buf[2] = RTC_AE;
137 buf[3] = RTC_AE;
139 /* write new alarm */
140 for(i = 0; i < 4; i++)
141 pp_i2c_send(RTC_ADDR, RTC_ALARM_MINUTES + i, buf[i]);
143 /* note: alarm is not enabled at the point */
146 void rtc_get_alarm(int *h, int *m)
148 unsigned char buf[4]={0};
150 /* get alarm preset */
151 i2c_readbytes(RTC_ADDR, RTC_ALARM_MINUTES, 4 ,buf);
153 *m = BCD2DEC(buf[0] & 0x7f);
154 *h = BCD2DEC(buf[0] & 0x3f);
157 bool rtc_enable_alarm(bool enable)
159 unsigned char tmp=0;
160 int rv=0;
162 if(enable)
164 /* enable alarm interrupt */
165 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
166 tmp |= RTC_AIE;
167 tmp &= ~RTC_AF;
168 pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
170 else
172 /* disable alarm interrupt */
173 if(rtc_lock_alarm_clear)
174 /* lock disabling alarm before it was checked whether or not the unit was started by RTC alarm */
175 return false;
176 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
177 tmp &= ~(RTC_AIE | RTC_AF);
178 pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
181 return false;
184 bool rtc_check_alarm_started(bool release_alarm)
186 static bool alarm_state, run_before = false;
187 unsigned char tmp=0;
188 bool started;
189 int rv=0;
191 if (run_before)
193 started = alarm_state;
194 alarm_state &= ~release_alarm;
196 else
198 /* read Control 2 register which contains alarm flag */
199 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
201 alarm_state = started = ( (tmp & RTC_AF) && (tmp & RTC_AIE) );
203 if(release_alarm && started)
205 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
206 /* clear alarm interrupt enable and alarm flag */
207 tmp &= ~(RTC_AF | RTC_AIE);
208 pp_i2c_send(RTC_ADDR, RTC_CTRL2, tmp);
210 run_before = true;
211 rtc_lock_alarm_clear = false;
214 return started;
217 bool rtc_check_alarm_flag(void)
219 unsigned char tmp=0;
220 int rv=0;
222 /* read Control 2 register which contains alarm flag */
223 rv = i2c_readbytes(RTC_ADDR, RTC_CTRL2, 1, &tmp);
225 return (tmp & RTC_AF);