1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 by Bertrik Sikken
11 * Copyright (C) 2008 by Robert Kukla
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
24 #include "i2c-s5l8700.h"
26 /* Driver for the Seiko S35390A real-time clock chip with i2c interface
28 This driver was derived from rtc_mr100.c and adapted for the S35390A
29 used in the Meizu M3 (and possibly others).
36 #define REALTIME_DATA1 2
37 #define REALTIME_DATA2 3
40 #define CLOCK_CORR_REG 6
44 static void reverse_bits(unsigned char* v
, int size
)
46 static const unsigned char flipnibble
[] =
47 {0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E,
48 0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F};
51 for (i
= 0; i
< size
; i
++) {
52 v
[i
] = (flipnibble
[v
[i
] & 0x0F] << 4) |
53 flipnibble
[(v
[i
] >> 4) & 0x0F];
61 int rtc_read_datetime(struct tm
*tm
)
66 ret
= i2c_read(RTC_ADDR
| (REALTIME_DATA1
<< 1), -1, sizeof(buf
), buf
);
67 reverse_bits(buf
, sizeof(buf
));
69 buf
[4] &= 0x3f; /* mask out p.m. flag */
71 for (i
= 0; i
< sizeof(buf
); i
++)
79 tm
->tm_mon
= buf
[1] - 1;
80 tm
->tm_year
= buf
[0] + 100;
85 int rtc_write_datetime(const struct tm
*tm
)
95 buf
[1] = tm
->tm_mon
+ 1;
96 buf
[0] = tm
->tm_year
- 100;
98 for (i
= 0; i
< sizeof(buf
); i
++)
101 reverse_bits(buf
, sizeof(buf
));
102 ret
= i2c_write(RTC_ADDR
| (REALTIME_DATA1
<< 1), -1, sizeof(buf
), buf
);