FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / drivers / rtc / rtc_pcf50605.c
blobfe12766c4af512ebab6a8ad9858dc5d0ff02feac
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(unsigned char* buf)
41 return pcf50605_read_multiple(0x0a, buf, 7);
44 int rtc_write_datetime(unsigned char* buf)
46 pcf50605_write_multiple(0x0a, buf, 7);
47 return 1;
50 /**
51 * Checks the PCF interrupt 1 register bit 7 to see if an alarm interrupt has
52 * triggered since last we checked.
54 bool rtc_check_alarm_flag(void)
56 return pcf50605_read(0x02) & 0x80;
59 /**
60 * Enables or disables the alarm.
61 * The Ipod bootloader clears all PCF interrupt registers and always enables
62 * the "wake on RTC" bit on OOCC1, so we have to rely on other means to find
63 * out if we just woke from an alarm.
64 * Return value is always false for us.
66 bool rtc_enable_alarm(bool enable)
68 if (enable) {
69 /* Tell the PCF to ignore everything but second, minute and hour, so
70 * that an alarm will trigger the next time the alarm time occurs.
72 pcf50605_write_multiple(0x14, alarm_disable + 3, 4);
73 /* Unmask the alarm interrupt (might be unneeded) */
74 pcf50605_write(0x5, pcf50605_read(0x5) & ~0x80);
75 /* Make sure wake on RTC is set when shutting down */
76 pcf50605_wakeup_flags |= 0x10;
77 } else {
78 /* We use this year to indicate a disabled alarm. If you happen to live
79 * around this time and are annoyed by this, feel free to seek out my
80 * grave and do something nasty to it.
82 pcf50605_write(0x17, 0x99);
83 /* Make sure we don't wake on RTC after shutting down */
84 pcf50605_wakeup_flags &= ~0x10;
86 return false;
89 /**
90 * Check if alarm caused unit to start.
92 bool rtc_check_alarm_started(bool release_alarm)
94 static bool run_before = false, alarm_state;
95 bool rc;
97 if (run_before) {
98 rc = alarm_state;
99 alarm_state &= ~release_alarm;
100 } else {
101 char rt[3], at[3];
102 /* The Ipod bootloader seems to read (and thus clear) the PCF interrupt
103 * registers, so we need to find some other way to detect if an alarm
104 * just happened
106 pcf50605_read_multiple(0x0a, rt, 3);
107 pcf50605_read_multiple(0x11, at, 3);
109 /* If alarm time and real time match within 10 seconds of each other, we
110 * assume an alarm just triggered
112 rc = alarm_state = rt[1] == at[1] && rt[2] == at[2]
113 && (rt[0] - at[0]) <= 10;
114 run_before = true;
116 return rc;
119 void rtc_set_alarm(int h, int m)
121 /* Set us to wake at the first second of the specified time */
122 pcf50605_write(0x11, 0);
123 /* Convert to BCD */
124 pcf50605_write(0x12, ((m/10) << 4) | m%10);
125 pcf50605_write(0x13, ((h/10) << 4) | h%10);
128 void rtc_get_alarm(int *h, int *m)
130 char buf[2];
132 pcf50605_read_multiple(0x12, buf, 2);
133 /* Convert from BCD */
134 *m = ((buf[0] >> 4) & 0x7)*10 + (buf[0] & 0x0f);
135 *h = ((buf[1] >> 4) & 0x3)*10 + (buf[1] & 0x0f);