12 #include <linux/module.h>
13 #include <linux/rtc.h>
14 #include <linux/i2c.h>
17 #include <linux/slab.h>
19 #define S35390A_CMD_STATUS1 0
20 #define S35390A_CMD_STATUS2 1
21 #define S35390A_CMD_TIME1 2
22 #define S35390A_CMD_TIME2 3
23 #define S35390A_CMD_INT2_REG1 5
25 #define S35390A_BYTE_YEAR 0
26 #define S35390A_BYTE_MONTH 1
27 #define S35390A_BYTE_DAY 2
28 #define S35390A_BYTE_WDAY 3
29 #define S35390A_BYTE_HOURS 4
30 #define S35390A_BYTE_MINS 5
31 #define S35390A_BYTE_SECS 6
33 #define S35390A_ALRM_BYTE_WDAY 0
34 #define S35390A_ALRM_BYTE_HOURS 1
35 #define S35390A_ALRM_BYTE_MINS 2
37 #define S35390A_FLAG_POC 0x01
38 #define S35390A_FLAG_BLD 0x02
39 #define S35390A_FLAG_24H 0x40
40 #define S35390A_FLAG_RESET 0x80
41 #define S35390A_FLAG_TEST 0x01
43 #define S35390A_INT2_MODE_MASK 0xF0
45 #define S35390A_INT2_MODE_NOINTR 0x00
46 #define S35390A_INT2_MODE_FREQ 0x10
47 #define S35390A_INT2_MODE_ALARM 0x40
48 #define S35390A_INT2_MODE_PMIN_EDG 0x20
79 static int s35390a_get_reg(
struct s35390a *s35390a,
int reg,
char *buf,
int len)
97 static int s35390a_reset(
struct s35390a *s35390a)
112 static int s35390a_disable_test_mode(
struct s35390a *s35390a)
122 buf[0] &= ~S35390A_FLAG_TEST;
126 static char s35390a_hr2reg(
struct s35390a *s35390a,
int hour)
134 return 0x40 |
bin2bcd(hour - 12);
137 static int s35390a_reg2hr(
struct s35390a *s35390a,
char reg)
153 struct s35390a *s35390a = i2c_get_clientdata(client);
157 dev_dbg(&client->
dev,
"%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
158 "mon=%d, year=%d, wday=%d\n", __func__, tm->
tm_sec,
171 for (i = 0; i < 7; ++
i)
172 buf[i] = bitrev8(buf[i]);
181 struct s35390a *s35390a = i2c_get_clientdata(client);
190 for (i = 0; i < 7; ++
i)
191 buf[i] = bitrev8(buf[i]);
201 dev_dbg(&client->
dev,
"%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
202 "mon=%d, year=%d, wday=%d\n", __func__, tm->
tm_sec,
211 struct s35390a *s35390a = i2c_get_clientdata(client);
212 char buf[3],
sts = 0;
215 dev_dbg(&client->
dev,
"%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
216 "mon=%d, year=%d, wday=%d\n", __func__, alm->
time.tm_sec,
218 alm->
time.tm_mon, alm->
time.tm_year, alm->
time.tm_wday);
243 if (alm->
time.tm_wday != -1)
247 alm->
time.tm_hour) | 0x80;
250 if (alm->
time.tm_hour >= 12)
253 for (i = 0; i < 3; ++
i)
254 buf[i] = bitrev8(buf[i]);
264 struct s35390a *s35390a = i2c_get_clientdata(client);
280 for (i = 0; i < 3; ++
i) {
281 buf[
i] = bitrev8(buf[i]);
286 alm->
time.tm_hour = s35390a_reg2hr(s35390a,
290 dev_dbg(&client->
dev,
"%s: alm is mins=%d, hours=%d, wday=%d\n",
291 __func__, alm->
time.tm_min, alm->
time.tm_hour,
302 static int s35390a_rtc_set_alarm(
struct device *dev,
struct rtc_wkalrm *alm)
307 static int s35390a_rtc_read_time(
struct device *dev,
struct rtc_time *tm)
312 static int s35390a_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
318 .read_time = s35390a_rtc_read_time,
319 .set_time = s35390a_rtc_set_time,
320 .set_alarm = s35390a_rtc_set_alarm,
321 .read_alarm = s35390a_rtc_read_alarm,
327 static int s35390a_probe(
struct i2c_client *client,
332 struct s35390a *s35390a;
341 s35390a = kzalloc(
sizeof(
struct s35390a),
GFP_KERNEL);
348 i2c_set_clientdata(client, s35390a);
351 for (i = 1; i < 8; ++
i) {
354 if (!s35390a->
client[i]) {
355 dev_err(&client->
dev,
"Address %02x unavailable\n",
362 err = s35390a_reset(s35390a);
364 dev_err(&client->
dev,
"error resetting chip\n");
368 err = s35390a_disable_test_mode(s35390a);
370 dev_err(&client->
dev,
"error disabling test mode\n");
376 dev_err(&client->
dev,
"error checking 12/24 hour mode\n");
384 if (s35390a_get_datetime(client, &tm) < 0)
392 if (IS_ERR(s35390a->
rtc)) {
393 err = PTR_ERR(s35390a->
rtc);
399 for (i = 1; i < 8; ++
i)
408 static int s35390a_remove(
struct i2c_client *client)
412 struct s35390a *s35390a = i2c_get_clientdata(client);
413 for (i = 1; i < 8; ++
i)
425 .name =
"rtc-s35390a",
427 .probe = s35390a_probe,
428 .remove = s35390a_remove,
429 .id_table = s35390a_id,