ypr0: Correct .scrobbler.log path for YPR0
[maemo-rb.git] / firmware / drivers / rtc / rtc_ds1339_ds3231.c
bloba813e8d999c2eb9f631e2c9c49d88b40ff883733
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Robert Kukla
11 * based on Archos code 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 "rtc.h"
22 #include "logf.h"
23 #include "sw_i2c.h"
25 #define RTC_ADDR 0xD0
27 void rtc_init(void)
29 sw_i2c_init();
31 #ifdef HAVE_RTC_ALARM
32 /* Check + save alarm bit first, before the power thread starts watching */
33 rtc_check_alarm_started(false);
34 #endif
38 #ifdef HAVE_RTC_ALARM
40 /* check whether the unit has been started by the RTC alarm function */
41 /* (check for A2F, which => started using wakeup alarm) */
42 bool rtc_check_alarm_started(bool release_alarm)
44 static bool alarm_state, run_before;
45 bool rc;
47 if (run_before) {
48 rc = alarm_state;
49 alarm_state &= ~release_alarm;
50 } else {
51 /* This call resets AF, so we store the state for later recall */
52 rc = alarm_state = rtc_check_alarm_flag();
53 run_before = true;
56 return rc;
59 * Checks the A2F flag. This call resets A2F once read.
62 bool rtc_check_alarm_flag(void)
64 unsigned char buf[1];
65 bool flag = false;
67 sw_i2c_read(RTC_ADDR, 0x0f, buf, 1);
68 if (buf[0] & 0x02) flag = true;
70 buf[0] = 0x00;
71 sw_i2c_write(RTC_ADDR, 0x0f, buf, 1);
73 return flag;
76 /* set alarm time registers to the given time (repeat once per day) */
77 void rtc_set_alarm(int h, int m)
79 unsigned char buf[3];
81 buf[0] = (((m / 10) << 4) | (m % 10)) & 0x7f; /* minutes */
82 buf[1] = (((h / 10) << 4) | (h % 10)) & 0x3f; /* hour */
83 buf[2] = 0x80; /* repeat every day */
85 sw_i2c_write(RTC_ADDR, 0x0b, buf, 3);
88 /* read out the current alarm time */
89 void rtc_get_alarm(int *h, int *m)
91 unsigned char buf[2];
93 sw_i2c_read(RTC_ADDR, 0x0b, buf, 2);
95 *m = ((buf[0] & 0x70) >> 4) * 10 + (buf[0] & 0x0f);
96 *h = ((buf[1] & 0x30) >> 4) * 10 + (buf[1] & 0x0f);
99 /* turn alarm on or off by setting the alarm flag enable */
100 /* the alarm is automatically disabled when the RTC gets Vcc power at startup */
101 /* avoid that an alarm occurs when the device is on because this locks the ON key forever */
102 void rtc_enable_alarm(bool enable)
104 unsigned char buf[2];
106 buf[0] = enable ? 0x26 : 0x24; /* BBSQI INTCN A2IE vs INTCH only */
107 buf[1] = 0x00; /* reset alarm flags (and OSF for good measure) */
109 sw_i2c_write(RTC_ADDR, 0x0e, buf, 2);
112 #endif /* HAVE_RTC_ALARM */
114 int rtc_read_datetime(struct tm *tm)
116 int rc;
117 unsigned char buf[7];
119 rc = sw_i2c_read(RTC_ADDR, 0, buf, sizeof(buf));
121 /* convert from bcd, avoid getting extra bits */
122 tm->tm_sec = BCD2DEC(buf[0] & 0x7f);
123 tm->tm_min = BCD2DEC(buf[1] & 0x7f);
124 tm->tm_hour = BCD2DEC(buf[2] & 0x3f);
125 tm->tm_wday = BCD2DEC(buf[3] & 0x7) - 1; /* timefuncs wants 0..6 for wday */
126 tm->tm_mday = BCD2DEC(buf[4] & 0x3f);
127 tm->tm_mon = BCD2DEC(buf[5] & 0x1f) - 1;
128 tm->tm_year = BCD2DEC(buf[6]) + 100;
130 return rc;
133 int rtc_write_datetime(const struct tm *tm)
135 unsigned int i;
136 int rc;
137 unsigned char buf[7];
139 buf[0] = tm->tm_sec;
140 buf[1] = tm->tm_min;
141 buf[2] = tm->tm_hour;
142 buf[3] = tm->tm_wday + 1; /* chip wants 1..7 for wday */
143 buf[4] = tm->tm_mday;
144 buf[5] = tm->tm_mon + 1;
145 buf[6] = tm->tm_year - 100;
147 for (i = 0; i < sizeof(buf); i++)
148 buf[i] = DEC2BCD(buf[i]);
150 buf[5] |= 0x80; /* chip wants century (always 20xx) */
152 rc = sw_i2c_write(RTC_ADDR, 0, buf, sizeof(buf));
154 return rc;