Minor changes and fix HAVE_LCD_FLIP
[kugel-rb.git] / firmware / drivers / rtc / rtc_pcf50606.c
blobcb6697207b59751e0d520843a524a3ee209bdea9
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 "pcf50606.h"
28 void rtc_init(void)
32 int rtc_read_datetime(struct tm *tm)
34 unsigned int i;
35 int rc, oldlevel;
36 unsigned char buf[7];
38 oldlevel = disable_irq_save();
40 rc = pcf50606_read_multiple(0x0a, buf, sizeof(buf));
42 restore_irq(oldlevel);
44 for (i = 0; i < sizeof(buf); i++)
45 buf[i] = BCD2DEC(buf[i]);
47 tm->tm_sec = buf[0];
48 tm->tm_min = buf[1];
49 tm->tm_hour = buf[2];
50 tm->tm_wday = buf[3];
51 tm->tm_mday = buf[4];
52 tm->tm_mon = buf[5] - 1;
53 #ifdef IRIVER_H300_SERIES
54 /* Special kludge to coexist with the iriver firmware. The iriver firmware
55 stores the date as 1965+nn, and allows a range of 1980..2064. We use
56 1964+nn here to make leap years work correctly, so the date will be one
57 year off in the iriver firmware but at least won't be reset anymore. */
58 tm->tm_year = buf[6] + 64;
59 #else /* Not IRIVER_H300_SERIES */
60 tm->tm_year = buf[6] + 100;
61 #endif /* IRIVER_H300_SERIES */
63 return rc;
66 int rtc_write_datetime(const struct tm *tm)
68 unsigned int i;
69 int rc, oldlevel;
70 unsigned char buf[7];
72 buf[0] = tm->tm_sec;
73 buf[1] = tm->tm_min;
74 buf[2] = tm->tm_hour;
75 buf[3] = tm->tm_wday;
76 buf[4] = tm->tm_mday;
77 buf[5] = tm->tm_mon + 1;
78 #ifdef IRIVER_H300_SERIES
79 /* Iriver firmware compatibility kludge, see rtc_read_datetime(). */
80 buf[6] = tm->tm_year - 64;
81 #else /* Not IRIVER_H300_SERIES */
82 buf[6] = tm->tm_year - 100;
83 #endif /* IRIVER_H300_SERIES */
85 for (i = 0; i < sizeof(buf); i++)
86 buf[i] = DEC2BCD(buf[i]);
88 oldlevel = disable_irq_save();
90 rc = pcf50606_write_multiple(0x0a, buf, sizeof(buf));
92 restore_irq(oldlevel);
94 return rc;