13 #include <linux/i2c.h>
14 #include <linux/rtc.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 #define DRV_VERSION "0.6"
28 #define RS5C372_REG_SECS 0
29 #define RS5C372_REG_MINS 1
30 #define RS5C372_REG_HOURS 2
31 #define RS5C372_REG_WDAY 3
32 #define RS5C372_REG_DAY 4
33 #define RS5C372_REG_MONTH 5
34 #define RS5C372_REG_YEAR 6
35 #define RS5C372_REG_TRIM 7
36 # define RS5C372_TRIM_XSL 0x80
37 # define RS5C372_TRIM_MASK 0x7F
39 #define RS5C_REG_ALARM_A_MIN 8
40 #define RS5C_REG_ALARM_A_HOURS 9
41 #define RS5C_REG_ALARM_A_WDAY 10
43 #define RS5C_REG_ALARM_B_MIN 11
44 #define RS5C_REG_ALARM_B_HOURS 12
45 #define RS5C_REG_ALARM_B_WDAY 13
47 #define RS5C_REG_CTRL1 14
48 # define RS5C_CTRL1_AALE (1 << 7)
49 # define RS5C_CTRL1_BALE (1 << 6)
50 # define RV5C387_CTRL1_24 (1 << 5)
51 # define RS5C372A_CTRL1_SL1 (1 << 5)
52 # define RS5C_CTRL1_CT_MASK (7 << 0)
53 # define RS5C_CTRL1_CT0 (0 << 0)
54 # define RS5C_CTRL1_CT4 (4 << 0)
55 #define RS5C_REG_CTRL2 15
56 # define RS5C372_CTRL2_24 (1 << 5)
57 # define R2025_CTRL2_XST (1 << 5)
58 # define RS5C_CTRL2_XSTP (1 << 4)
59 # define RS5C_CTRL2_CTFG (1 << 2)
60 # define RS5C_CTRL2_AAFG (1 << 1)
61 # define RS5C_CTRL2_BAFG (1 << 0)
65 #define RS5C_ADDR(R) (((R) << 4) | 0)
103 static int rs5c_get_regs(
struct rs5c372 *rs5c)
110 .len =
sizeof(rs5c->
buf),
128 int size =
sizeof(rs5c->
buf) - 1;
143 "%02x %02x %02x (%02x) %02x %02x %02x (%02x), "
144 "%02x %02x %02x, %02x %02x %02x; %02x %02x\n",
153 static unsigned rs5c_reg2hr(
struct rs5c372 *rs5c,
unsigned reg)
168 static unsigned rs5c_hr2reg(
struct rs5c372 *rs5c,
unsigned hour)
174 return 0x20 |
bin2bcd(hour - 12);
184 struct rs5c372 *rs5c = i2c_get_clientdata(client);
185 int status = rs5c_get_regs(rs5c);
203 dev_dbg(&client->
dev,
"%s: tm is secs=%d, mins=%d, hours=%d, "
204 "mday=%d, mon=%d, year=%d, wday=%d\n",
215 struct rs5c372 *rs5c = i2c_get_clientdata(client);
216 unsigned char buf[7];
219 dev_dbg(&client->
dev,
"%s: tm is secs=%d, mins=%d, hours=%d "
220 "mday=%d, mon=%d, year=%d, wday=%d\n",
228 buf[2] = rs5c_hr2reg(rs5c, tm->
tm_hour);
235 dev_err(&client->
dev,
"%s: write error\n", __func__);
242 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
246 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
251 static int rs5c372_get_trim(
struct i2c_client *client,
int *
osc,
int *trim)
260 dev_dbg(&client->
dev,
"%s: raw trim=%x\n", __func__, tmp);
266 t = (~t | (
s8)0xc0) + 1;
285 static int rs5c372_rtc_set_time(
struct device *dev,
struct rtc_time *tm)
291 static int rs5c_rtc_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
294 struct rs5c372 *rs5c = i2c_get_clientdata(client);
303 status = rs5c_get_regs(rs5c);
336 struct rs5c372 *rs5c = i2c_get_clientdata(client);
339 status = rs5c_get_regs(rs5c);
347 t->
time.tm_mday = -1;
349 t->
time.tm_year = -1;
350 t->
time.tm_wday = -1;
351 t->
time.tm_yday = -1;
352 t->
time.tm_isdst = -1;
364 struct rs5c372 *rs5c = i2c_get_clientdata(client);
366 unsigned char buf[3];
369 if (t->
time.tm_mday != -1
370 || t->
time.tm_mon != -1
371 || t->
time.tm_year != -1)
377 status = rs5c_get_regs(rs5c);
384 pr_debug(
"%s: can't disable alarm\n", rs5c->
rtc->name);
392 buf[1] = rs5c_hr2reg(rs5c, t->
time.tm_hour);
395 for (i = 0; i <
sizeof(
buf); i++) {
398 pr_debug(
"%s: can't set alarm time\n", rs5c->
rtc->name);
416 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
425 osc / 1000, osc % 1000);
433 #define rs5c372_rtc_proc NULL
438 .read_time = rs5c372_rtc_read_time,
439 .set_time = rs5c372_rtc_set_time,
440 .read_alarm = rs5c_read_alarm,
441 .set_alarm = rs5c_set_alarm,
442 .alarm_irq_enable = rs5c_rtc_alarm_irq_enable,
445 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
456 return sprintf(buf,
"%d\n", trim);
469 return sprintf(buf,
"%d.%03d KHz\n", osc / 1000, osc % 1000);
473 static int rs5c_sysfs_register(
struct device *dev)
487 static void rs5c_sysfs_unregister(
struct device *dev)
494 static int rs5c_sysfs_register(
struct device *dev)
499 static void rs5c_sysfs_unregister(
struct device *dev)
507 static int rs5c_oscillator_setup(
struct rs5c372 *rs5c372)
509 unsigned char buf[2];
527 switch (rs5c372->
type) {
544 for (i = 0; i <
sizeof(
buf); i++) {
557 static int rs5c372_probe(
struct i2c_client *client,
562 struct rs5c372 *rs5c372;
573 if (i2c_check_functionality(client->
adapter,
584 if (!(rs5c372 = kzalloc(
sizeof(
struct rs5c372),
GFP_KERNEL))) {
590 i2c_set_clientdata(client, rs5c372);
591 rs5c372->
type =
id->driver_data;
594 rs5c372->
regs = &rs5c372->
buf[1];
595 rs5c372->
smbus = smbus_mode;
597 err = rs5c_get_regs(rs5c372);
602 switch (rs5c372->
type) {
631 err = rs5c_oscillator_setup(rs5c372);
637 if (rs5c372_get_datetime(client, &tm) < 0)
641 ({
char *
s; switch (rs5c372->
type) {
647 default:
s =
"chip";
break;
649 rs5c372->
time24 ?
"24hr" :
"am/pm"
657 if (IS_ERR(rs5c372->
rtc)) {
658 err = PTR_ERR(rs5c372->
rtc);
662 err = rs5c_sysfs_register(&client->
dev);
678 static int rs5c372_remove(
struct i2c_client *client)
680 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
683 rs5c_sysfs_unregister(&client->
dev);
690 .name =
"rtc-rs5c372",
692 .probe = rs5c372_probe,
693 .remove = rs5c372_remove,
694 .id_table = rs5c372_id,