fix red
[kugel-rb.git] / firmware / drivers / rtc / rtc_pcf50605.c
blobb030fba37aba93deb3fc10849d517d8803b777d8
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
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 "config.h"
22 #include "i2c.h"
23 #include "rtc.h"
24 #include "kernel.h"
25 #include "system.h"
26 #include "pcf50605.h"
27 #include <stdbool.h>
29 /* Values which each disable one alarm time register */
30 static const char alarm_disable[] = {
31 0x7f, 0x7f, 0x3f, 0x07, 0x3f, 0x1f, 0xff
34 void rtc_init(void)
36 rtc_check_alarm_started(false);
39 int rtc_read_datetime(struct tm *tm)
41 unsigned int i;
42 int rc;
43 unsigned char buf[7];
44 rc = pcf50605_read_multiple(0x0a, buf, sizeof(buf));
46 for (i = 0; i < sizeof(buf); i++)
47 buf[i] = BCD2DEC(buf[i]);
49 tm->tm_sec = buf[0];
50 tm->tm_min = buf[1];
51 tm->tm_hour = buf[2];
52 tm->tm_wday = buf[3];
53 tm->tm_mday = buf[4];
54 tm->tm_mon = buf[5] - 1;
55 tm->tm_year = buf[6] + 100;
57 return rc;
60 int rtc_write_datetime(const struct tm *tm)
62 unsigned int i;
63 unsigned char buf[7];
65 buf[0] = tm->tm_sec;
66 buf[1] = tm->tm_min;
67 buf[2] = tm->tm_hour;
68 buf[3] = tm->tm_wday;
69 buf[4] = tm->tm_mday;
70 buf[5] = tm->tm_mon + 1;
71 buf[6] = tm->tm_year - 100;
73 for (i = 0; i < sizeof(buf); i++)
74 buf[i] = DEC2BCD(buf[i]);
76 pcf50605_write_multiple(0x0a, buf, sizeof(buf));
78 return 1;
81 /**
82 * Checks the PCF interrupt 1 register bit 7 to see if an alarm interrupt has
83 * triggered since last we checked.
85 bool rtc_check_alarm_flag(void)
87 return pcf50605_read(0x02) & 0x80;
90 /**
91 * Enables or disables the alarm.
92 * The Ipod bootloader clears all PCF interrupt registers and always enables
93 * the "wake on RTC" bit on OOCC1, so we have to rely on other means to find
94 * out if we just woke from an alarm.
95 * Return value is always false for us.
97 bool rtc_enable_alarm(bool enable)
99 if (enable) {
100 /* Tell the PCF to ignore everything but second, minute and hour, so
101 * that an alarm will trigger the next time the alarm time occurs.
103 pcf50605_write_multiple(0x14, alarm_disable + 3, 4);
104 /* Unmask the alarm interrupt (might be unneeded) */
105 pcf50605_write(0x5, pcf50605_read(0x5) & ~0x80);
106 /* Make sure wake on RTC is set when shutting down */
107 pcf50605_wakeup_flags |= 0x10;
108 } else {
109 /* We use this year to indicate a disabled alarm. If you happen to live
110 * around this time and are annoyed by this, feel free to seek out my
111 * grave and do something nasty to it.
113 pcf50605_write(0x17, 0x99);
114 /* Make sure we don't wake on RTC after shutting down */
115 pcf50605_wakeup_flags &= ~0x10;
117 return false;
121 * Check if alarm caused unit to start.
123 bool rtc_check_alarm_started(bool release_alarm)
125 static bool run_before = false, alarm_state;
126 bool rc;
128 if (run_before) {
129 rc = alarm_state;
130 alarm_state &= ~release_alarm;
131 } else {
132 char rt[3], at[3];
133 /* The Ipod bootloader seems to read (and thus clear) the PCF interrupt
134 * registers, so we need to find some other way to detect if an alarm
135 * just happened
137 pcf50605_read_multiple(0x0a, rt, 3);
138 pcf50605_read_multiple(0x11, at, 3);
140 /* If alarm time and real time match within 10 seconds of each other, we
141 * assume an alarm just triggered
143 rc = alarm_state = rt[1] == at[1] && rt[2] == at[2]
144 && (rt[0] - at[0]) <= 10;
145 run_before = true;
147 return rc;
150 void rtc_set_alarm(int h, int m)
152 /* Set us to wake at the first second of the specified time */
153 pcf50605_write(0x11, 0);
154 /* Convert to BCD */
155 pcf50605_write(0x12, DEC2BCD(m));
156 pcf50605_write(0x13, DEC2BCD(h));
159 void rtc_get_alarm(int *h, int *m)
161 char buf[2];
163 pcf50605_read_multiple(0x12, buf, sizeof(buf));
164 /* Convert from BCD */
165 *m = BCD2DEC(buf[0]);
166 *h = BCD2DEC(buf[1]);