Add a get_offset API call to make codec_advance_buffer_loc_callback work.
[Rockbox.git] / firmware / drivers / rtc / rtc_pcf50605.c
blob93fa391ae3e6f0c04e5d7b5af9336f9280c95064
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 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "i2c.h"
21 #include "rtc.h"
22 #include "kernel.h"
23 #include "system.h"
24 #include "pcf50605.h"
25 #include <stdbool.h>
27 /* Values which each disable one alarm time register */
28 static const char alarm_disable[] = {
29 0x7f, 0x7f, 0x3f, 0x07, 0x3f, 0x1f, 0xff
32 void rtc_init(void)
34 rtc_check_alarm_started(false);
37 int rtc_read_datetime(unsigned char* buf)
39 return pcf50605_read_multiple(0x0a, buf, 7);
42 int rtc_write_datetime(unsigned char* buf)
44 pcf50605_write_multiple(0x0a, buf, 7);
45 return 1;
48 /**
49 * Checks the PCF interrupt 1 register bit 7 to see if an alarm interrupt has
50 * triggered since last we checked.
52 bool rtc_check_alarm_flag(void)
54 return pcf50605_read(0x02) & 0x80;
57 /**
58 * Enables or disables the alarm.
59 * The Ipod bootloader clears all PCF interrupt registers and always enables
60 * the "wake on RTC" bit on OOCC1, so we have to rely on other means to find
61 * out if we just woke from an alarm.
62 * Return value is always false for us.
64 bool rtc_enable_alarm(bool enable)
66 if (enable) {
67 /* Tell the PCF to ignore everything but second, minute and hour, so
68 * that an alarm will trigger the next time the alarm time occurs.
70 pcf50605_write_multiple(0x14, alarm_disable + 3, 4);
71 /* Unmask the alarm interrupt (might be unneeded) */
72 pcf50605_write(0x5, pcf50605_read(0x5) & ~0x80);
73 /* Make sure wake on RTC is set when shutting down */
74 pcf50605_wakeup_flags |= 0x10;
75 } else {
76 /* We use this year to indicate a disabled alarm. If you happen to live
77 * around this time and are annoyed by this, feel free to seek out my
78 * grave and do something nasty to it.
80 pcf50605_write(0x17, 0x99);
81 /* Make sure we don't wake on RTC after shutting down */
82 pcf50605_wakeup_flags &= ~0x10;
84 return false;
87 /**
88 * Check if alarm caused unit to start.
90 bool rtc_check_alarm_started(bool release_alarm)
92 static bool run_before = false, alarm_state;
93 bool rc;
95 if (run_before) {
96 rc = alarm_state;
97 alarm_state &= ~release_alarm;
98 } else {
99 char rt[3], at[3];
100 /* The Ipod bootloader seems to read (and thus clear) the PCF interrupt
101 * registers, so we need to find some other way to detect if an alarm
102 * just happened
104 pcf50605_read_multiple(0x0a, rt, 3);
105 pcf50605_read_multiple(0x11, at, 3);
107 /* If alarm time and real time match within 10 seconds of each other, we
108 * assume an alarm just triggered
110 rc = alarm_state = rt[1] == at[1] && rt[2] == at[2]
111 && (rt[0] - at[0]) <= 10;
112 run_before = true;
114 return rc;
117 void rtc_set_alarm(int h, int m)
119 /* Set us to wake at the first second of the specified time */
120 pcf50605_write(0x11, 0);
121 /* Convert to BCD */
122 pcf50605_write(0x12, ((m/10) << 4) | m%10);
123 pcf50605_write(0x13, ((h/10) << 4) | h%10);
126 void rtc_get_alarm(int *h, int *m)
128 char buf[2];
130 pcf50605_read_multiple(0x12, buf, 2);
131 /* Convert from BCD */
132 *m = ((buf[0] >> 4) & 0x7)*10 + (buf[0] & 0x0f);
133 *h = ((buf[1] >> 4) & 0x3)*10 + (buf[1] & 0x0f);