2 * An I2C driver for Ricoh RS5C372 and RV5C38[67] RTCs
4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5 * Copyright (C) 2006 Tower Technologies
6 * Copyright (C) 2008 Paul Mundt
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/i2c.h>
14 #include <linux/rtc.h>
15 #include <linux/bcd.h>
17 #define DRV_VERSION "0.6"
21 * Ricoh has a family of I2C based RTCs, which differ only slightly from
22 * each other. Differences center on pinout (e.g. how many interrupts,
23 * output clock, etc) and how the control registers are used. The '372
24 * is significant only because that's the one this driver first supported.
26 #define RS5C372_REG_SECS 0
27 #define RS5C372_REG_MINS 1
28 #define RS5C372_REG_HOURS 2
29 #define RS5C372_REG_WDAY 3
30 #define RS5C372_REG_DAY 4
31 #define RS5C372_REG_MONTH 5
32 #define RS5C372_REG_YEAR 6
33 #define RS5C372_REG_TRIM 7
34 # define RS5C372_TRIM_XSL 0x80
35 # define RS5C372_TRIM_MASK 0x7F
37 #define RS5C_REG_ALARM_A_MIN 8 /* or ALARM_W */
38 #define RS5C_REG_ALARM_A_HOURS 9
39 #define RS5C_REG_ALARM_A_WDAY 10
41 #define RS5C_REG_ALARM_B_MIN 11 /* or ALARM_D */
42 #define RS5C_REG_ALARM_B_HOURS 12
43 #define RS5C_REG_ALARM_B_WDAY 13 /* (ALARM_B only) */
45 #define RS5C_REG_CTRL1 14
46 # define RS5C_CTRL1_AALE (1 << 7) /* or WALE */
47 # define RS5C_CTRL1_BALE (1 << 6) /* or DALE */
48 # define RV5C387_CTRL1_24 (1 << 5)
49 # define RS5C372A_CTRL1_SL1 (1 << 5)
50 # define RS5C_CTRL1_CT_MASK (7 << 0)
51 # define RS5C_CTRL1_CT0 (0 << 0) /* no periodic irq */
52 # define RS5C_CTRL1_CT4 (4 << 0) /* 1 Hz level irq */
53 #define RS5C_REG_CTRL2 15
54 # define RS5C372_CTRL2_24 (1 << 5)
55 # define RS5C_CTRL2_XSTP (1 << 4)
56 # define RS5C_CTRL2_CTFG (1 << 2)
57 # define RS5C_CTRL2_AAFG (1 << 1) /* or WAFG */
58 # define RS5C_CTRL2_BAFG (1 << 0) /* or DAFG */
61 /* to read (style 1) or write registers starting at R */
62 #define RS5C_ADDR(R) (((R) << 4) | 0)
73 static const struct i2c_device_id rs5c372_id[] = {
74 { "rs5c372a", rtc_rs5c372a },
75 { "rs5c372b", rtc_rs5c372b },
76 { "rv5c386", rtc_rv5c386 },
77 { "rv5c387a", rtc_rv5c387a },
80 MODULE_DEVICE_TABLE(i2c, rs5c372_id);
82 /* REVISIT: this assumes that:
83 * - we're in the 21st century, so it's safe to ignore the century
84 * bit for rv5c38[67] (REG_MONTH bit 7);
85 * - we should use ALARM_A not ALARM_B (may be wrong on some boards)
88 struct i2c_client *client;
89 struct rtc_device *rtc;
98 static int rs5c_get_regs(struct rs5c372 *rs5c)
100 struct i2c_client *client = rs5c->client;
101 struct i2c_msg msgs[] = {
102 { client->addr, I2C_M_RD, sizeof rs5c->buf, rs5c->buf },
105 /* This implements the third reading method from the datasheet, using
106 * an internal address that's reset after each transaction (by STOP)
107 * to 0x0f ... so we read extra registers, and skip the first one.
109 * The first method doesn't work with the iop3xx adapter driver, on at
110 * least 80219 chips; this works around that bug.
112 * The third method on the other hand doesn't work for the SMBus-only
113 * configurations, so we use the the first method there, stripping off
114 * the extra register in the process.
117 int addr = RS5C_ADDR(RS5C372_REG_SECS);
118 int size = sizeof(rs5c->buf) - 1;
120 if (i2c_smbus_read_i2c_block_data(client, addr, size,
121 rs5c->buf + 1) != size) {
122 dev_warn(&client->dev, "can't read registers\n");
126 if ((i2c_transfer(client->adapter, msgs, 1)) != 1) {
127 dev_warn(&client->dev, "can't read registers\n");
132 dev_dbg(&client->dev,
133 "%02x %02x %02x (%02x) %02x %02x %02x (%02x), "
134 "%02x %02x %02x, %02x %02x %02x; %02x %02x\n",
135 rs5c->regs[0], rs5c->regs[1], rs5c->regs[2], rs5c->regs[3],
136 rs5c->regs[4], rs5c->regs[5], rs5c->regs[6], rs5c->regs[7],
137 rs5c->regs[8], rs5c->regs[9], rs5c->regs[10], rs5c->regs[11],
138 rs5c->regs[12], rs5c->regs[13], rs5c->regs[14], rs5c->regs[15]);
143 static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg)
148 return BCD2BIN(reg & 0x3f);
150 hour = BCD2BIN(reg & 0x1f);
158 static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour)
161 return BIN2BCD(hour);
164 return 0x20 | BIN2BCD(hour - 12);
166 return 0x20 | BIN2BCD(12);
169 return BIN2BCD(hour);
172 static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
174 struct rs5c372 *rs5c = i2c_get_clientdata(client);
175 int status = rs5c_get_regs(rs5c);
180 tm->tm_sec = BCD2BIN(rs5c->regs[RS5C372_REG_SECS] & 0x7f);
181 tm->tm_min = BCD2BIN(rs5c->regs[RS5C372_REG_MINS] & 0x7f);
182 tm->tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C372_REG_HOURS]);
184 tm->tm_wday = BCD2BIN(rs5c->regs[RS5C372_REG_WDAY] & 0x07);
185 tm->tm_mday = BCD2BIN(rs5c->regs[RS5C372_REG_DAY] & 0x3f);
187 /* tm->tm_mon is zero-based */
188 tm->tm_mon = BCD2BIN(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1;
190 /* year is 1900 + tm->tm_year */
191 tm->tm_year = BCD2BIN(rs5c->regs[RS5C372_REG_YEAR]) + 100;
193 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
194 "mday=%d, mon=%d, year=%d, wday=%d\n",
196 tm->tm_sec, tm->tm_min, tm->tm_hour,
197 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
199 /* rtc might need initialization */
200 return rtc_valid_tm(tm);
203 static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
205 struct rs5c372 *rs5c = i2c_get_clientdata(client);
206 unsigned char buf[8];
209 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
210 "mday=%d, mon=%d, year=%d, wday=%d\n",
212 tm->tm_sec, tm->tm_min, tm->tm_hour,
213 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
215 addr = RS5C_ADDR(RS5C372_REG_SECS);
216 buf[0] = BIN2BCD(tm->tm_sec);
217 buf[1] = BIN2BCD(tm->tm_min);
218 buf[2] = rs5c_hr2reg(rs5c, tm->tm_hour);
219 buf[3] = BIN2BCD(tm->tm_wday);
220 buf[4] = BIN2BCD(tm->tm_mday);
221 buf[5] = BIN2BCD(tm->tm_mon + 1);
222 buf[6] = BIN2BCD(tm->tm_year - 100);
224 if (i2c_smbus_write_i2c_block_data(client, addr, sizeof(buf), buf) < 0) {
225 dev_err(&client->dev, "%s: write error\n", __func__);
232 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
236 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
241 static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim)
243 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
244 u8 tmp = rs5c372->regs[RS5C372_REG_TRIM];
247 *osc = (tmp & RS5C372_TRIM_XSL) ? 32000 : 32768;
250 dev_dbg(&client->dev, "%s: raw trim=%x\n", __func__, tmp);
251 tmp &= RS5C372_TRIM_MASK;
256 t = (~t | (s8)0xc0) + 1;
270 static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm)
272 return rs5c372_get_datetime(to_i2c_client(dev), tm);
275 static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm)
277 return rs5c372_set_datetime(to_i2c_client(dev), tm);
280 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
283 rs5c_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
285 struct i2c_client *client = to_i2c_client(dev);
286 struct rs5c372 *rs5c = i2c_get_clientdata(client);
290 buf = rs5c->regs[RS5C_REG_CTRL1];
294 /* some 327a modes use a different IRQ pin for 1Hz irqs */
295 if (rs5c->type == rtc_rs5c372a
296 && (buf & RS5C372A_CTRL1_SL1))
300 /* these irq management calls only make sense for chips
301 * which are wired up to an IRQ.
310 status = rs5c_get_regs(rs5c);
314 addr = RS5C_ADDR(RS5C_REG_CTRL1);
316 case RTC_AIE_OFF: /* alarm off */
317 buf &= ~RS5C_CTRL1_AALE;
319 case RTC_AIE_ON: /* alarm on */
320 buf |= RS5C_CTRL1_AALE;
322 case RTC_UIE_OFF: /* update off */
323 buf &= ~RS5C_CTRL1_CT_MASK;
325 case RTC_UIE_ON: /* update on */
326 buf &= ~RS5C_CTRL1_CT_MASK;
327 buf |= RS5C_CTRL1_CT4;
331 if (i2c_smbus_write_byte_data(client, addr, buf) < 0) {
332 printk(KERN_WARNING "%s: can't update alarm\n",
336 rs5c->regs[RS5C_REG_CTRL1] = buf;
342 #define rs5c_rtc_ioctl NULL
346 /* NOTE: Since RTC_WKALM_{RD,SET} were originally defined for EFI,
347 * which only exposes a polled programming interface; and since
348 * these calls map directly to those EFI requests; we don't demand
349 * we have an IRQ for this chip when we go through this API.
351 * The older x86_pc derived RTC_ALM_{READ,SET} calls require irqs
352 * though, managed through RTC_AIE_{ON,OFF} requests.
355 static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t)
357 struct i2c_client *client = to_i2c_client(dev);
358 struct rs5c372 *rs5c = i2c_get_clientdata(client);
361 status = rs5c_get_regs(rs5c);
365 /* report alarm time */
367 t->time.tm_min = BCD2BIN(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f);
368 t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]);
369 t->time.tm_mday = -1;
371 t->time.tm_year = -1;
372 t->time.tm_wday = -1;
373 t->time.tm_yday = -1;
374 t->time.tm_isdst = -1;
377 t->enabled = !!(rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE);
378 t->pending = !!(rs5c->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_AAFG);
383 static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
385 struct i2c_client *client = to_i2c_client(dev);
386 struct rs5c372 *rs5c = i2c_get_clientdata(client);
388 unsigned char buf[3];
390 /* only handle up to 24 hours in the future, like RTC_ALM_SET */
391 if (t->time.tm_mday != -1
392 || t->time.tm_mon != -1
393 || t->time.tm_year != -1)
396 /* REVISIT: round up tm_sec */
398 /* if needed, disable irq (clears pending status) */
399 status = rs5c_get_regs(rs5c);
402 if (rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE) {
403 addr = RS5C_ADDR(RS5C_REG_CTRL1);
404 buf[0] = rs5c->regs[RS5C_REG_CTRL1] & ~RS5C_CTRL1_AALE;
405 if (i2c_smbus_write_byte_data(client, addr, buf[0]) < 0) {
406 pr_debug("%s: can't disable alarm\n", rs5c->rtc->name);
409 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
413 buf[0] = BIN2BCD(t->time.tm_min);
414 buf[1] = rs5c_hr2reg(rs5c, t->time.tm_hour);
415 buf[2] = 0x7f; /* any/all days */
417 for (i = 0; i < sizeof(buf); i++) {
418 addr = RS5C_ADDR(RS5C_REG_ALARM_A_MIN + i);
419 if (i2c_smbus_write_byte_data(client, addr, buf[i]) < 0) {
420 pr_debug("%s: can't set alarm time\n", rs5c->rtc->name);
425 /* ... and maybe enable its irq */
427 addr = RS5C_ADDR(RS5C_REG_CTRL1);
428 buf[0] = rs5c->regs[RS5C_REG_CTRL1] | RS5C_CTRL1_AALE;
429 if (i2c_smbus_write_byte_data(client, addr, buf[0]) < 0)
430 printk(KERN_WARNING "%s: can't enable alarm\n",
432 rs5c->regs[RS5C_REG_CTRL1] = buf[0];
438 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
440 static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq)
444 err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim);
446 seq_printf(seq, "crystal\t\t: %d.%03d KHz\n",
447 osc / 1000, osc % 1000);
448 seq_printf(seq, "trim\t\t: %d\n", trim);
455 #define rs5c372_rtc_proc NULL
458 static const struct rtc_class_ops rs5c372_rtc_ops = {
459 .proc = rs5c372_rtc_proc,
460 .ioctl = rs5c_rtc_ioctl,
461 .read_time = rs5c372_rtc_read_time,
462 .set_time = rs5c372_rtc_set_time,
463 .read_alarm = rs5c_read_alarm,
464 .set_alarm = rs5c_set_alarm,
467 #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
469 static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
470 struct device_attribute *attr, char *buf)
474 err = rs5c372_get_trim(to_i2c_client(dev), NULL, &trim);
478 return sprintf(buf, "%d\n", trim);
480 static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL);
482 static ssize_t rs5c372_sysfs_show_osc(struct device *dev,
483 struct device_attribute *attr, char *buf)
487 err = rs5c372_get_trim(to_i2c_client(dev), &osc, NULL);
491 return sprintf(buf, "%d.%03d KHz\n", osc / 1000, osc % 1000);
493 static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL);
495 static int rs5c_sysfs_register(struct device *dev)
499 err = device_create_file(dev, &dev_attr_trim);
502 err = device_create_file(dev, &dev_attr_osc);
504 device_remove_file(dev, &dev_attr_trim);
509 static void rs5c_sysfs_unregister(struct device *dev)
511 device_remove_file(dev, &dev_attr_trim);
512 device_remove_file(dev, &dev_attr_osc);
516 static int rs5c_sysfs_register(struct device *dev)
521 static void rs5c_sysfs_unregister(struct device *dev)
527 static struct i2c_driver rs5c372_driver;
529 static int rs5c_oscillator_setup(struct rs5c372 *rs5c372)
531 unsigned char buf[2];
532 int addr, i, ret = 0;
534 if (!(rs5c372->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_XSTP))
536 rs5c372->regs[RS5C_REG_CTRL2] &= ~RS5C_CTRL2_XSTP;
538 addr = RS5C_ADDR(RS5C_REG_CTRL1);
539 buf[0] = rs5c372->regs[RS5C_REG_CTRL1];
540 buf[1] = rs5c372->regs[RS5C_REG_CTRL2];
543 switch (rs5c372->type) {
546 buf[1] |= RS5C372_CTRL2_24;
551 buf[0] |= RV5C387_CTRL1_24;
559 for (i = 0; i < sizeof(buf); i++) {
560 addr = RS5C_ADDR(RS5C_REG_CTRL1 + i);
561 ret = i2c_smbus_write_byte_data(rs5c372->client, addr, buf[i]);
562 if (unlikely(ret < 0))
566 rs5c372->regs[RS5C_REG_CTRL1] = buf[0];
567 rs5c372->regs[RS5C_REG_CTRL2] = buf[1];
572 static int rs5c372_probe(struct i2c_client *client,
573 const struct i2c_device_id *id)
577 struct rs5c372 *rs5c372;
580 dev_dbg(&client->dev, "%s\n", __func__);
582 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
583 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK)) {
585 * If we don't have any master mode adapter, try breaking
586 * it down in to the barest of capabilities.
588 if (i2c_check_functionality(client->adapter,
589 I2C_FUNC_SMBUS_BYTE_DATA |
590 I2C_FUNC_SMBUS_I2C_BLOCK))
593 /* Still no good, give up */
599 if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) {
604 rs5c372->client = client;
605 i2c_set_clientdata(client, rs5c372);
606 rs5c372->type = id->driver_data;
608 /* we read registers 0x0f then 0x00-0x0f; skip the first one */
609 rs5c372->regs = &rs5c372->buf[1];
610 rs5c372->smbus = smbus_mode;
612 err = rs5c_get_regs(rs5c372);
616 /* clock may be set for am/pm or 24 hr time */
617 switch (rs5c372->type) {
620 /* alarm uses ALARM_A; and nINTRA on 372a, nINTR on 372b.
621 * so does periodic irq, except some 327a modes.
623 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C372_CTRL2_24)
628 if (rs5c372->regs[RS5C_REG_CTRL1] & RV5C387_CTRL1_24)
630 /* alarm uses ALARM_W; and nINTRB for alarm and periodic
631 * irq, on both 386 and 387
635 dev_err(&client->dev, "unknown RTC type\n");
639 /* if the oscillator lost power and no other software (like
640 * the bootloader) set it up, do it here.
642 err = rs5c_oscillator_setup(rs5c372);
643 if (unlikely(err < 0)) {
644 dev_err(&client->dev, "setup error\n");
648 if (rs5c372_get_datetime(client, &tm) < 0)
649 dev_warn(&client->dev, "clock needs to be set\n");
651 dev_info(&client->dev, "%s found, %s, driver version " DRV_VERSION "\n",
652 ({ char *s; switch (rs5c372->type) {
653 case rtc_rs5c372a: s = "rs5c372a"; break;
654 case rtc_rs5c372b: s = "rs5c372b"; break;
655 case rtc_rv5c386: s = "rv5c386"; break;
656 case rtc_rv5c387a: s = "rv5c387a"; break;
657 default: s = "chip"; break;
659 rs5c372->time24 ? "24hr" : "am/pm"
662 /* REVISIT use client->irq to register alarm irq ... */
664 rs5c372->rtc = rtc_device_register(rs5c372_driver.driver.name,
665 &client->dev, &rs5c372_rtc_ops, THIS_MODULE);
667 if (IS_ERR(rs5c372->rtc)) {
668 err = PTR_ERR(rs5c372->rtc);
672 err = rs5c_sysfs_register(&client->dev);
679 rtc_device_unregister(rs5c372->rtc);
688 static int rs5c372_remove(struct i2c_client *client)
690 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
692 rtc_device_unregister(rs5c372->rtc);
693 rs5c_sysfs_unregister(&client->dev);
698 static struct i2c_driver rs5c372_driver = {
700 .name = "rtc-rs5c372",
702 .probe = rs5c372_probe,
703 .remove = rs5c372_remove,
704 .id_table = rs5c372_id,
707 static __init int rs5c372_init(void)
709 return i2c_add_driver(&rs5c372_driver);
712 static __exit void rs5c372_exit(void)
714 i2c_del_driver(&rs5c372_driver);
717 module_init(rs5c372_init);
718 module_exit(rs5c372_exit);
721 "Pavel Mironchik <pmironchik@optifacio.net>, "
722 "Alessandro Zummo <a.zummo@towertech.it>, "
723 "Paul Mundt <lethal@linux-sh.org>");
724 MODULE_DESCRIPTION("Ricoh RS5C372 RTC driver");
725 MODULE_LICENSE("GPL");
726 MODULE_VERSION(DRV_VERSION);