1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Seiko Instruments S-35390A RTC Driver
5 * Copyright (c) 2007 Byron Bradley
8 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/bitrev.h>
12 #include <linux/bcd.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
16 #define S35390A_CMD_STATUS1 0
17 #define S35390A_CMD_STATUS2 1
18 #define S35390A_CMD_TIME1 2
19 #define S35390A_CMD_TIME2 3
20 #define S35390A_CMD_INT2_REG1 5
22 #define S35390A_BYTE_YEAR 0
23 #define S35390A_BYTE_MONTH 1
24 #define S35390A_BYTE_DAY 2
25 #define S35390A_BYTE_WDAY 3
26 #define S35390A_BYTE_HOURS 4
27 #define S35390A_BYTE_MINS 5
28 #define S35390A_BYTE_SECS 6
30 #define S35390A_ALRM_BYTE_WDAY 0
31 #define S35390A_ALRM_BYTE_HOURS 1
32 #define S35390A_ALRM_BYTE_MINS 2
34 /* flags for STATUS1 */
35 #define S35390A_FLAG_POC 0x01
36 #define S35390A_FLAG_BLD 0x02
37 #define S35390A_FLAG_INT2 0x04
38 #define S35390A_FLAG_24H 0x40
39 #define S35390A_FLAG_RESET 0x80
41 /* flag for STATUS2 */
42 #define S35390A_FLAG_TEST 0x01
44 #define S35390A_INT2_MODE_MASK 0xF0
46 #define S35390A_INT2_MODE_NOINTR 0x00
47 #define S35390A_INT2_MODE_FREQ 0x10
48 #define S35390A_INT2_MODE_ALARM 0x40
49 #define S35390A_INT2_MODE_PMIN_EDG 0x20
51 static const struct i2c_device_id s35390a_id
[] = {
55 MODULE_DEVICE_TABLE(i2c
, s35390a_id
);
57 static const struct of_device_id s35390a_of_match
[] = {
58 { .compatible
= "s35390a" },
59 { .compatible
= "sii,s35390a" },
62 MODULE_DEVICE_TABLE(of
, s35390a_of_match
);
65 struct i2c_client
*client
[8];
66 struct rtc_device
*rtc
;
70 static int s35390a_set_reg(struct s35390a
*s35390a
, int reg
, char *buf
, int len
)
72 struct i2c_client
*client
= s35390a
->client
[reg
];
73 struct i2c_msg msg
[] = {
81 if ((i2c_transfer(client
->adapter
, msg
, 1)) != 1)
87 static int s35390a_get_reg(struct s35390a
*s35390a
, int reg
, char *buf
, int len
)
89 struct i2c_client
*client
= s35390a
->client
[reg
];
90 struct i2c_msg msg
[] = {
99 if ((i2c_transfer(client
->adapter
, msg
, 1)) != 1)
105 static int s35390a_init(struct s35390a
*s35390a
)
109 unsigned initcount
= 0;
112 * At least one of POC and BLD are set, so reinitialise chip. Keeping
113 * this information in the hardware to know later that the time isn't
114 * valid is unfortunately not possible because POC and BLD are cleared
115 * on read. So the reset is best done now.
117 * The 24H bit is kept over reset, so set it already here.
120 buf
= S35390A_FLAG_RESET
| S35390A_FLAG_24H
;
121 ret
= s35390a_set_reg(s35390a
, S35390A_CMD_STATUS1
, &buf
, 1);
126 ret
= s35390a_get_reg(s35390a
, S35390A_CMD_STATUS1
, &buf
, 1);
130 if (buf
& (S35390A_FLAG_POC
| S35390A_FLAG_BLD
)) {
131 /* Try up to five times to reset the chip */
143 * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset.
144 * To keep the information if an irq is pending, pass the value read from
145 * STATUS1 to the caller.
147 static int s35390a_read_status(struct s35390a
*s35390a
, char *status1
)
151 ret
= s35390a_get_reg(s35390a
, S35390A_CMD_STATUS1
, status1
, 1);
155 if (*status1
& S35390A_FLAG_POC
) {
157 * Do not communicate for 0.5 seconds since the power-on
158 * detection circuit is in operation.
162 } else if (*status1
& S35390A_FLAG_BLD
)
165 * If both POC and BLD are unset everything is fine.
170 static int s35390a_disable_test_mode(struct s35390a
*s35390a
)
174 if (s35390a_get_reg(s35390a
, S35390A_CMD_STATUS2
, buf
, sizeof(buf
)) < 0)
177 if (!(buf
[0] & S35390A_FLAG_TEST
))
180 buf
[0] &= ~S35390A_FLAG_TEST
;
181 return s35390a_set_reg(s35390a
, S35390A_CMD_STATUS2
, buf
, sizeof(buf
));
184 static char s35390a_hr2reg(struct s35390a
*s35390a
, int hour
)
186 if (s35390a
->twentyfourhour
)
187 return bin2bcd(hour
);
190 return bin2bcd(hour
);
192 return 0x40 | bin2bcd(hour
- 12);
195 static int s35390a_reg2hr(struct s35390a
*s35390a
, char reg
)
199 if (s35390a
->twentyfourhour
)
200 return bcd2bin(reg
& 0x3f);
202 hour
= bcd2bin(reg
& 0x3f);
209 static int s35390a_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
211 struct i2c_client
*client
= to_i2c_client(dev
);
212 struct s35390a
*s35390a
= i2c_get_clientdata(client
);
216 dev_dbg(&client
->dev
, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
217 "mon=%d, year=%d, wday=%d\n", __func__
, tm
->tm_sec
,
218 tm
->tm_min
, tm
->tm_hour
, tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
,
221 if (s35390a_read_status(s35390a
, &status
) == 1)
222 s35390a_init(s35390a
);
224 buf
[S35390A_BYTE_YEAR
] = bin2bcd(tm
->tm_year
- 100);
225 buf
[S35390A_BYTE_MONTH
] = bin2bcd(tm
->tm_mon
+ 1);
226 buf
[S35390A_BYTE_DAY
] = bin2bcd(tm
->tm_mday
);
227 buf
[S35390A_BYTE_WDAY
] = bin2bcd(tm
->tm_wday
);
228 buf
[S35390A_BYTE_HOURS
] = s35390a_hr2reg(s35390a
, tm
->tm_hour
);
229 buf
[S35390A_BYTE_MINS
] = bin2bcd(tm
->tm_min
);
230 buf
[S35390A_BYTE_SECS
] = bin2bcd(tm
->tm_sec
);
232 /* This chip expects the bits of each byte to be in reverse order */
233 for (i
= 0; i
< 7; ++i
)
234 buf
[i
] = bitrev8(buf
[i
]);
236 err
= s35390a_set_reg(s35390a
, S35390A_CMD_TIME1
, buf
, sizeof(buf
));
241 static int s35390a_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
243 struct i2c_client
*client
= to_i2c_client(dev
);
244 struct s35390a
*s35390a
= i2c_get_clientdata(client
);
248 if (s35390a_read_status(s35390a
, &status
) == 1)
251 err
= s35390a_get_reg(s35390a
, S35390A_CMD_TIME1
, buf
, sizeof(buf
));
255 /* This chip returns the bits of each byte in reverse order */
256 for (i
= 0; i
< 7; ++i
)
257 buf
[i
] = bitrev8(buf
[i
]);
259 tm
->tm_sec
= bcd2bin(buf
[S35390A_BYTE_SECS
]);
260 tm
->tm_min
= bcd2bin(buf
[S35390A_BYTE_MINS
]);
261 tm
->tm_hour
= s35390a_reg2hr(s35390a
, buf
[S35390A_BYTE_HOURS
]);
262 tm
->tm_wday
= bcd2bin(buf
[S35390A_BYTE_WDAY
]);
263 tm
->tm_mday
= bcd2bin(buf
[S35390A_BYTE_DAY
]);
264 tm
->tm_mon
= bcd2bin(buf
[S35390A_BYTE_MONTH
]) - 1;
265 tm
->tm_year
= bcd2bin(buf
[S35390A_BYTE_YEAR
]) + 100;
267 dev_dbg(&client
->dev
, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
268 "mon=%d, year=%d, wday=%d\n", __func__
, tm
->tm_sec
,
269 tm
->tm_min
, tm
->tm_hour
, tm
->tm_mday
, tm
->tm_mon
, tm
->tm_year
,
275 static int s35390a_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
277 struct i2c_client
*client
= to_i2c_client(dev
);
278 struct s35390a
*s35390a
= i2c_get_clientdata(client
);
279 char buf
[3], sts
= 0;
282 dev_dbg(&client
->dev
, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
283 "mon=%d, year=%d, wday=%d\n", __func__
, alm
->time
.tm_sec
,
284 alm
->time
.tm_min
, alm
->time
.tm_hour
, alm
->time
.tm_mday
,
285 alm
->time
.tm_mon
, alm
->time
.tm_year
, alm
->time
.tm_wday
);
287 /* disable interrupt (which deasserts the irq line) */
288 err
= s35390a_set_reg(s35390a
, S35390A_CMD_STATUS2
, &sts
, sizeof(sts
));
292 /* clear pending interrupt (in STATUS1 only), if any */
293 err
= s35390a_get_reg(s35390a
, S35390A_CMD_STATUS1
, &sts
, sizeof(sts
));
298 sts
= S35390A_INT2_MODE_ALARM
;
300 sts
= S35390A_INT2_MODE_NOINTR
;
302 /* This chip expects the bits of each byte to be in reverse order */
305 /* set interupt mode*/
306 err
= s35390a_set_reg(s35390a
, S35390A_CMD_STATUS2
, &sts
, sizeof(sts
));
310 if (alm
->time
.tm_wday
!= -1)
311 buf
[S35390A_ALRM_BYTE_WDAY
] = bin2bcd(alm
->time
.tm_wday
) | 0x80;
313 buf
[S35390A_ALRM_BYTE_WDAY
] = 0;
315 buf
[S35390A_ALRM_BYTE_HOURS
] = s35390a_hr2reg(s35390a
,
316 alm
->time
.tm_hour
) | 0x80;
317 buf
[S35390A_ALRM_BYTE_MINS
] = bin2bcd(alm
->time
.tm_min
) | 0x80;
319 if (alm
->time
.tm_hour
>= 12)
320 buf
[S35390A_ALRM_BYTE_HOURS
] |= 0x40;
322 for (i
= 0; i
< 3; ++i
)
323 buf
[i
] = bitrev8(buf
[i
]);
325 err
= s35390a_set_reg(s35390a
, S35390A_CMD_INT2_REG1
, buf
,
331 static int s35390a_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
333 struct i2c_client
*client
= to_i2c_client(dev
);
334 struct s35390a
*s35390a
= i2c_get_clientdata(client
);
338 err
= s35390a_get_reg(s35390a
, S35390A_CMD_STATUS2
, &sts
, sizeof(sts
));
342 if ((bitrev8(sts
) & S35390A_INT2_MODE_MASK
) != S35390A_INT2_MODE_ALARM
) {
344 * When the alarm isn't enabled, the register to configure
345 * the alarm time isn't accessible.
353 err
= s35390a_get_reg(s35390a
, S35390A_CMD_INT2_REG1
, buf
, sizeof(buf
));
357 /* This chip returns the bits of each byte in reverse order */
358 for (i
= 0; i
< 3; ++i
)
359 buf
[i
] = bitrev8(buf
[i
]);
362 * B0 of the three matching registers is an enable flag. Iff it is set
363 * the configured value is used for matching.
365 if (buf
[S35390A_ALRM_BYTE_WDAY
] & 0x80)
367 bcd2bin(buf
[S35390A_ALRM_BYTE_WDAY
] & ~0x80);
369 if (buf
[S35390A_ALRM_BYTE_HOURS
] & 0x80)
371 s35390a_reg2hr(s35390a
,
372 buf
[S35390A_ALRM_BYTE_HOURS
] & ~0x80);
374 if (buf
[S35390A_ALRM_BYTE_MINS
] & 0x80)
375 alm
->time
.tm_min
= bcd2bin(buf
[S35390A_ALRM_BYTE_MINS
] & ~0x80);
377 /* alarm triggers always at s=0 */
378 alm
->time
.tm_sec
= 0;
380 dev_dbg(&client
->dev
, "%s: alm is mins=%d, hours=%d, wday=%d\n",
381 __func__
, alm
->time
.tm_min
, alm
->time
.tm_hour
,
387 static int s35390a_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
390 struct i2c_client
*client
= to_i2c_client(dev
);
391 struct s35390a
*s35390a
= i2c_get_clientdata(client
);
397 /* s35390a_reset set lowvoltage flag and init RTC if needed */
398 err
= s35390a_read_status(s35390a
, &sts
);
401 if (copy_to_user((void __user
*)arg
, &err
, sizeof(int)))
405 /* update flag and clear register */
406 err
= s35390a_init(s35390a
);
417 static const struct rtc_class_ops s35390a_rtc_ops
= {
418 .read_time
= s35390a_rtc_read_time
,
419 .set_time
= s35390a_rtc_set_time
,
420 .set_alarm
= s35390a_rtc_set_alarm
,
421 .read_alarm
= s35390a_rtc_read_alarm
,
422 .ioctl
= s35390a_rtc_ioctl
,
425 static struct i2c_driver s35390a_driver
;
427 static int s35390a_probe(struct i2c_client
*client
,
428 const struct i2c_device_id
*id
)
432 struct s35390a
*s35390a
;
435 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
440 s35390a
= devm_kzalloc(&client
->dev
, sizeof(struct s35390a
),
447 s35390a
->client
[0] = client
;
448 i2c_set_clientdata(client
, s35390a
);
450 /* This chip uses multiple addresses, use dummy devices for them */
451 for (i
= 1; i
< 8; ++i
) {
452 s35390a
->client
[i
] = i2c_new_dummy(client
->adapter
,
454 if (!s35390a
->client
[i
]) {
455 dev_err(&client
->dev
, "Address %02x unavailable\n",
462 err_read
= s35390a_read_status(s35390a
, &status1
);
465 dev_err(&client
->dev
, "error resetting chip\n");
469 if (status1
& S35390A_FLAG_24H
)
470 s35390a
->twentyfourhour
= 1;
472 s35390a
->twentyfourhour
= 0;
474 if (status1
& S35390A_FLAG_INT2
) {
475 /* disable alarm (and maybe test mode) */
477 err
= s35390a_set_reg(s35390a
, S35390A_CMD_STATUS2
, &buf
, 1);
479 dev_err(&client
->dev
, "error disabling alarm");
483 err
= s35390a_disable_test_mode(s35390a
);
485 dev_err(&client
->dev
, "error disabling test mode\n");
490 device_set_wakeup_capable(&client
->dev
, 1);
492 s35390a
->rtc
= devm_rtc_device_register(&client
->dev
,
493 s35390a_driver
.driver
.name
,
494 &s35390a_rtc_ops
, THIS_MODULE
);
496 if (IS_ERR(s35390a
->rtc
)) {
497 err
= PTR_ERR(s35390a
->rtc
);
501 if (status1
& S35390A_FLAG_INT2
)
502 rtc_update_irq(s35390a
->rtc
, 1, RTC_AF
);
507 for (i
= 1; i
< 8; ++i
)
508 if (s35390a
->client
[i
])
509 i2c_unregister_device(s35390a
->client
[i
]);
515 static int s35390a_remove(struct i2c_client
*client
)
518 struct s35390a
*s35390a
= i2c_get_clientdata(client
);
520 for (i
= 1; i
< 8; ++i
)
521 if (s35390a
->client
[i
])
522 i2c_unregister_device(s35390a
->client
[i
]);
527 static struct i2c_driver s35390a_driver
= {
529 .name
= "rtc-s35390a",
530 .of_match_table
= of_match_ptr(s35390a_of_match
),
532 .probe
= s35390a_probe
,
533 .remove
= s35390a_remove
,
534 .id_table
= s35390a_id
,
537 module_i2c_driver(s35390a_driver
);
539 MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
540 MODULE_DESCRIPTION("S35390A RTC driver");
541 MODULE_LICENSE("GPL");