diff options
Diffstat (limited to 'drivers/rtc')
-rw-r--r-- | drivers/rtc/Kconfig | 2 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 2 | ||||
-rw-r--r-- | drivers/rtc/rtc-at91rm9200.c (renamed from drivers/rtc/rtc-at91.c) | 7 | ||||
-rw-r--r-- | drivers/rtc/rtc-dev.c | 4 | ||||
-rw-r--r-- | drivers/rtc/rtc-ds1672.c | 2 | ||||
-rw-r--r-- | drivers/rtc/rtc-lib.c | 81 | ||||
-rw-r--r-- | drivers/rtc/rtc-omap.c | 3 | ||||
-rw-r--r-- | drivers/rtc/rtc-pcf8563.c | 46 | ||||
-rw-r--r-- | drivers/rtc/rtc-proc.c | 4 | ||||
-rw-r--r-- | drivers/rtc/rtc-rs5c372.c | 537 | ||||
-rw-r--r-- | drivers/rtc/rtc-s3c.c | 6 | ||||
-rw-r--r-- | drivers/rtc/rtc-sa1100.c | 4 | ||||
-rw-r--r-- | drivers/rtc/rtc-sh.c | 251 | ||||
-rw-r--r-- | drivers/rtc/rtc-sysfs.c | 4 | ||||
-rw-r--r-- | drivers/rtc/rtc-x1205.c | 10 |
15 files changed, 824 insertions, 139 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 2a63ab2b47f4..09660e2ab051 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -288,7 +288,7 @@ config RTC_DRV_PL031 To compile this driver as a module, choose M here: the module will be called rtc-pl031. -config RTC_DRV_AT91 +config RTC_DRV_AT91RM9200 tristate "AT91RM9200" depends on RTC_CLASS && ARCH_AT91RM9200 help diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index bd4c45d333f0..e6beedacc966 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -35,5 +35,5 @@ obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o -obj-$(CONFIG_RTC_DRV_AT91) += rtc-at91.o +obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o obj-$(CONFIG_RTC_DRV_SH) += rtc-sh.o diff --git a/drivers/rtc/rtc-at91.c b/drivers/rtc/rtc-at91rm9200.c index 5c8addcaf1fb..a724ab49a797 100644 --- a/drivers/rtc/rtc-at91.c +++ b/drivers/rtc/rtc-at91rm9200.c @@ -33,6 +33,8 @@ #include <asm/mach/time.h> +#include <asm/arch/at91_rtc.h> + #define AT91_RTC_FREQ 1 #define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */ @@ -137,6 +139,9 @@ static int at91_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); tm->tm_year = at91_alarm_year - 1900; + alrm->enabled = (at91_sys_read(AT91_RTC_IMR) & AT91_RTC_ALARM) + ? 1 : 0; + pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __FUNCTION__, 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); @@ -223,8 +228,6 @@ static int at91_rtc_proc(struct device *dev, struct seq_file *seq) { unsigned long imr = at91_sys_read(AT91_RTC_IMR); - seq_printf(seq, "alarm_IRQ\t: %s\n", - (imr & AT91_RTC_ALARM) ? "yes" : "no"); seq_printf(seq, "update_IRQ\t: %s\n", (imr & AT91_RTC_ACKUPD) ? "yes" : "no"); seq_printf(seq, "periodic_IRQ\t: %s\n", diff --git a/drivers/rtc/rtc-dev.c b/drivers/rtc/rtc-dev.c index 828b329e08e0..82f2ac87ccd4 100644 --- a/drivers/rtc/rtc-dev.c +++ b/drivers/rtc/rtc-dev.c @@ -305,7 +305,7 @@ static int rtc_dev_ioctl(struct inode *inode, struct file *file, case RTC_IRQP_READ: if (ops->irq_set_freq) - err = put_user(rtc->irq_freq, (unsigned long *) arg); + err = put_user(rtc->irq_freq, (unsigned long __user *)uarg); break; case RTC_IRQP_SET: @@ -435,7 +435,7 @@ static int rtc_dev_add_device(struct class_device *class_dev, goto err_cdev_del; } - dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n", + dev_dbg(class_dev->dev, "rtc intf: dev (%d:%d)\n", MAJOR(rtc->rtc_dev->devt), MINOR(rtc->rtc_dev->devt)); diff --git a/drivers/rtc/rtc-ds1672.c b/drivers/rtc/rtc-ds1672.c index dfef1637bfb8..205fa28593b7 100644 --- a/drivers/rtc/rtc-ds1672.c +++ b/drivers/rtc/rtc-ds1672.c @@ -199,7 +199,7 @@ static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind) struct i2c_client *client; struct rtc_device *rtc; - dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); + dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__); if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { err = -ENODEV; diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c index ba795a4db1e9..7bbc26a34bd2 100644 --- a/drivers/rtc/rtc-lib.c +++ b/drivers/rtc/rtc-lib.c @@ -117,4 +117,85 @@ int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time) } EXPORT_SYMBOL(rtc_tm_to_time); + +/* Merge the valid (i.e. non-negative) fields of alarm into the current + * time. If the valid alarm fields are earlier than the equivalent + * fields in the time, carry one into the least significant invalid + * field, so that the alarm expiry is in the future. It assumes that the + * least significant invalid field is more significant than the most + * significant valid field, and that the seconds field is valid. + * + * This is used by alarms that take relative (rather than absolute) + * times, and/or have a simple binary second counter instead of + * day/hour/minute/sec registers. + */ +void rtc_merge_alarm(struct rtc_time *now, struct rtc_time *alarm) +{ + int *alarmp = &alarm->tm_sec; + int *timep = &now->tm_sec; + int carry_into, i; + + /* Ignore everything past the 6th element (tm_year). */ + for (i = 5; i > 0; i--) { + if (alarmp[i] < 0) + alarmp[i] = timep[i]; + else + break; + } + + /* No carry needed if all fields are valid. */ + if (i == 5) + return; + + for (carry_into = i + 1; i >= 0; i--) { + if (alarmp[i] < timep[i]) + break; + + if (alarmp[i] > timep[i]) + return; + } + + switch (carry_into) { + case 1: + alarm->tm_min++; + + if (alarm->tm_min < 60) + return; + + alarm->tm_min = 0; + /* fall-through */ + + case 2: + alarm->tm_hour++; + + if (alarm->tm_hour < 60) + return; + + alarm->tm_hour = 0; + /* fall-through */ + + case 3: + alarm->tm_mday++; + + if (alarm->tm_mday <= rtc_days_in_month[alarm->tm_mon]) + return; + + alarm->tm_mday = 1; + /* fall-through */ + + case 4: + alarm->tm_mon++; + + if (alarm->tm_mon <= 12) + return; + + alarm->tm_mon = 1; + /* fall-through */ + + case 5: + alarm->tm_year++; + } +} +EXPORT_SYMBOL(rtc_merge_alarm); + MODULE_LICENSE("GPL"); diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c index eac5fb1fc02f..d59880d44fba 100644 --- a/drivers/rtc/rtc-omap.c +++ b/drivers/rtc/rtc-omap.c @@ -279,9 +279,8 @@ static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) local_irq_enable(); bcd2tm(&alm->time); - alm->pending = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG) + alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG) & OMAP_RTC_INTERRUPTS_IT_ALARM); - alm->enabled = alm->pending && device_may_wakeup(dev); return 0; } diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c index a760cf69af90..038118bbfaea 100644 --- a/drivers/rtc/rtc-pcf8563.c +++ b/drivers/rtc/rtc-pcf8563.c @@ -53,6 +53,25 @@ I2C_CLIENT_INSMOD; #define PCF8563_SC_LV 0x80 /* low voltage */ #define PCF8563_MO_C 0x80 /* century */ +struct pcf8563 { + struct i2c_client client; + /* + * The meaning of MO_C bit varies by the chip type. + * From PCF8563 datasheet: this bit is toggled when the years + * register overflows from 99 to 00 + * 0 indicates the century is 20xx + * 1 indicates the century is 19xx + * From RTC8564 datasheet: this bit indicates change of + * century. When the year digit data overflows from 99 to 00, + * this bit is set. By presetting it to 0 while still in the + * 20th century, it will be set in year 2000, ... + * There seems no reliable way to know how the system use this + * bit. So let's do it heuristically, assuming we are live in + * 1970...2069. + */ + int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */ +}; + static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind); static int pcf8563_detach(struct i2c_client *client); @@ -62,6 +81,7 @@ static int pcf8563_detach(struct i2c_client *client); */ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm) { + struct pcf8563 *pcf8563 = container_of(client, struct pcf8563, client); unsigned char buf[13] = { PCF8563_REG_ST1 }; struct i2c_msg msgs[] = { @@ -94,8 +114,12 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm) tm->tm_mday = BCD2BIN(buf[PCF8563_REG_DM] & 0x3F); tm->tm_wday = buf[PCF8563_REG_DW] & 0x07; tm->tm_mon = BCD2BIN(buf[PCF8563_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ - tm->tm_year = BCD2BIN(buf[PCF8563_REG_YR]) - + (buf[PCF8563_REG_MO] & PCF8563_MO_C ? 0 : 100); + tm->tm_year = BCD2BIN(buf[PCF8563_REG_YR]); + if (tm->tm_year < 70) + tm->tm_year += 100; /* assume we are in 1970...2069 */ + /* detect the polarity heuristically. see note above. */ + pcf8563->c_polarity = (buf[PCF8563_REG_MO] & PCF8563_MO_C) ? + (tm->tm_year >= 100) : (tm->tm_year < 100); dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " "mday=%d, mon=%d, year=%d, wday=%d\n", @@ -114,6 +138,7 @@ static int pcf8563_get_datetime(struct i2c_client *client, struct rtc_time *tm) static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) { + struct pcf8563 *pcf8563 = container_of(client, struct pcf8563, client); int i, err; unsigned char buf[9]; @@ -135,7 +160,7 @@ static int pcf8563_set_datetime(struct i2c_client *client, struct rtc_time *tm) /* year and century */ buf[PCF8563_REG_YR] = BIN2BCD(tm->tm_year % 100); - if (tm->tm_year < 100) + if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100)) buf[PCF8563_REG_MO] |= PCF8563_MO_C; buf[PCF8563_REG_DW] = tm->tm_wday & 0x07; @@ -192,7 +217,7 @@ static int pcf8563_validate_client(struct i2c_client *client) xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); if (xfer != ARRAY_SIZE(msgs)) { - dev_err(&client->adapter->dev, + dev_err(&client->dev, "%s: could not read register 0x%02X\n", __FUNCTION__, pattern[i].reg); @@ -203,7 +228,7 @@ static int pcf8563_validate_client(struct i2c_client *client) if (value > pattern[i].max || value < pattern[i].min) { - dev_dbg(&client->adapter->dev, + dev_dbg(&client->dev, "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, " "max=%d, value=%d, raw=0x%02X\n", __FUNCTION__, i, pattern[i].reg, pattern[i].mask, @@ -248,23 +273,25 @@ static struct i2c_driver pcf8563_driver = { static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind) { + struct pcf8563 *pcf8563; struct i2c_client *client; struct rtc_device *rtc; int err = 0; - dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); + dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__); if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { err = -ENODEV; goto exit; } - if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) { + if (!(pcf8563 = kzalloc(sizeof(struct pcf8563), GFP_KERNEL))) { err = -ENOMEM; goto exit; } + client = &pcf8563->client; client->addr = address; client->driver = &pcf8563_driver; client->adapter = adapter; @@ -301,7 +328,7 @@ exit_detach: i2c_detach_client(client); exit_kfree: - kfree(client); + kfree(pcf8563); exit: return err; @@ -309,6 +336,7 @@ exit: static int pcf8563_detach(struct i2c_client *client) { + struct pcf8563 *pcf8563 = container_of(client, struct pcf8563, client); int err; struct rtc_device *rtc = i2c_get_clientdata(client); @@ -318,7 +346,7 @@ static int pcf8563_detach(struct i2c_client *client) if ((err = i2c_detach_client(client))) return err; - kfree(client); + kfree(pcf8563); return 0; } diff --git a/drivers/rtc/rtc-proc.c b/drivers/rtc/rtc-proc.c index d51d8f20e634..c272afd62173 100644 --- a/drivers/rtc/rtc-proc.c +++ b/drivers/rtc/rtc-proc.c @@ -65,7 +65,7 @@ static int rtc_proc_show(struct seq_file *seq, void *offset) seq_printf(seq, "%02d\n", alrm.time.tm_mday); else seq_printf(seq, "**\n"); - seq_printf(seq, "alrm_wakeup\t: %s\n", + seq_printf(seq, "alarm_IRQ\t: %s\n", alrm.enabled ? "yes" : "no"); seq_printf(seq, "alrm_pending\t: %s\n", alrm.pending ? "yes" : "no"); @@ -120,7 +120,7 @@ static int rtc_proc_add_device(struct class_device *class_dev, ent->owner = rtc->owner; ent->data = class_dev; - dev_info(class_dev->dev, "rtc intf: proc\n"); + dev_dbg(class_dev->dev, "rtc intf: proc\n"); } else rtc_dev = NULL; diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c index e2c7698fdba3..e7851e3739ab 100644 --- a/drivers/rtc/rtc-rs5c372.c +++ b/drivers/rtc/rtc-rs5c372.c @@ -1,5 +1,5 @@ /* - * An I2C driver for the Ricoh RS5C372 RTC + * An I2C driver for Ricoh RS5C372 and RV5C38[67] RTCs * * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net> * Copyright (C) 2006 Tower Technologies @@ -13,7 +13,7 @@ #include <linux/rtc.h> #include <linux/bcd.h> -#define DRV_VERSION "0.3" +#define DRV_VERSION "0.4" /* Addresses to scan */ static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END }; @@ -21,6 +21,13 @@ static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END }; /* Insmod parameters */ I2C_CLIENT_INSMOD; + +/* + * Ricoh has a family of I2C based RTCs, which differ only slightly from + * each other. Differences center on pinout (e.g. how many interrupts, + * output clock, etc) and how the control registers are used. The '372 + * is significant only because that's the one this driver first supported. + */ #define RS5C372_REG_SECS 0 #define RS5C372_REG_MINS 1 #define RS5C372_REG_HOURS 2 @@ -29,59 +36,142 @@ I2C_CLIENT_INSMOD; #define RS5C372_REG_MONTH 5 #define RS5C372_REG_YEAR 6 #define RS5C372_REG_TRIM 7 +# define RS5C372_TRIM_XSL 0x80 +# define RS5C372_TRIM_MASK 0x7F + +#define RS5C_REG_ALARM_A_MIN 8 /* or ALARM_W */ +#define RS5C_REG_ALARM_A_HOURS 9 +#define RS5C_REG_ALARM_A_WDAY 10 + +#define RS5C_REG_ALARM_B_MIN 11 /* or ALARM_D */ +#define RS5C_REG_ALARM_B_HOURS 12 +#define RS5C_REG_ALARM_B_WDAY 13 /* (ALARM_B only) */ + +#define RS5C_REG_CTRL1 14 +# define RS5C_CTRL1_AALE (1 << 7) /* or WALE */ +# define RS5C_CTRL1_BALE (1 << 6) /* or DALE */ +# define RV5C387_CTRL1_24 (1 << 5) +# define RS5C372A_CTRL1_SL1 (1 << 5) +# define RS5C_CTRL1_CT_MASK (7 << 0) +# define RS5C_CTRL1_CT0 (0 << 0) /* no periodic irq */ +# define RS5C_CTRL1_CT4 (4 << 0) /* 1 Hz level irq */ +#define RS5C_REG_CTRL2 15 +# define RS5C372_CTRL2_24 (1 << 5) +# define RS5C_CTRL2_XSTP (1 << 4) +# define RS5C_CTRL2_CTFG (1 << 2) +# define RS5C_CTRL2_AAFG (1 << 1) /* or WAFG */ +# define RS5C_CTRL2_BAFG (1 << 0) /* or DAFG */ + + +/* to read (style 1) or write registers starting at R */ +#define RS5C_ADDR(R) (((R) << 4) | 0) + + +enum rtc_type { + rtc_undef = 0, + rtc_rs5c372a, + rtc_rs5c372b, + rtc_rv5c386, + rtc_rv5c387a, +}; -#define RS5C372_TRIM_XSL 0x80 -#define RS5C372_TRIM_MASK 0x7F +/* REVISIT: this assumes that: + * - we're in the 21st century, so it's safe to ignore the century + * bit for rv5c38[67] (REG_MONTH bit 7); + * - we should use ALARM_A not ALARM_B (may be wrong on some boards) + */ +struct rs5c372 { + struct i2c_client *client; + struct rtc_device *rtc; + enum rtc_type type; + unsigned time24:1; + unsigned has_irq:1; + char buf[17]; + char *regs; + + /* on conversion to a "new style" i2c driver, this vanishes */ + struct i2c_client dev; +}; -#define RS5C372_REG_BASE 0 +static int rs5c_get_regs(struct rs5c372 *rs5c) +{ + struct i2c_client *client = rs5c->client; + struct i2c_msg msgs[] = { + { client->addr, I2C_M_RD, sizeof rs5c->buf, rs5c->buf }, + }; + + /* This implements the third reading method from the datasheet, using + * an internal address that's reset after each transaction (by STOP) + * to 0x0f ... so we read extra registers, and skip the first one. + * + * The first method doesn't work with the iop3xx adapter driver, on at + * least 80219 chips; this works around that bug. + */ + if ((i2c_transfer(client->adapter, msgs, 1)) != 1) { + pr_debug("%s: can't read registers\n", rs5c->rtc->name); + return -EIO; + } -static int rs5c372_attach(struct i2c_adapter *adapter); -static int rs5c372_detach(struct i2c_client *client); -static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind); + dev_dbg(&client->dev, + "%02x %02x %02x (%02x) %02x %02x %02x (%02x), " + "%02x %02x %02x, %02x %02x %02x; %02x %02x\n", + rs5c->regs[0], rs5c->regs[1], rs5c->regs[2], rs5c->regs[3], + rs5c->regs[4], rs5c->regs[5], rs5c->regs[6], rs5c->regs[7], + rs5c->regs[8], rs5c->regs[9], rs5c->regs[10], rs5c->regs[11], + rs5c->regs[12], rs5c->regs[13], rs5c->regs[14], rs5c->regs[15]); -struct rs5c372 { - u8 reg_addr; - u8 regs[17]; - struct i2c_msg msg[1]; - struct i2c_client client; - struct rtc_device *rtc; -}; + return 0; +} -static struct i2c_driver rs5c372_driver = { - .driver = { - .name = "rs5c372", - }, - .attach_adapter = &rs5c372_attach, - .detach_client = &rs5c372_detach, -}; +static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg) +{ + unsigned hour; -static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm) + if (rs5c->time24) + return BCD2BIN(reg & 0x3f); + + hour = BCD2BIN(reg & 0x1f); + if (hour == 12) + hour = 0; + if (reg & 0x20) + hour += 12; + return hour; +} + +static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour) { + if (rs5c->time24) + return BIN2BCD(hour); + + if (hour > 12) + return 0x20 | BIN2BCD(hour - 12); + if (hour == 12) + return 0x20 | BIN2BCD(12); + if (hour == 0) + return BIN2BCD(12); + return BIN2BCD(hour); +} - struct rs5c372 *rs5c372 = i2c_get_clientdata(client); - u8 *buf = &(rs5c372->regs[1]); +static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm) +{ + struct rs5c372 *rs5c = i2c_get_clientdata(client); + int status = rs5c_get_regs(rs5c); - /* this implements the 3rd reading method, according - * to the datasheet. rs5c372 defaults to internal - * address 0xF, so 0x0 is in regs[1] - */ + if (status < 0) + return status; - if ((i2c_transfer(client->adapter, rs5c372->msg, 1)) != 1) { - dev_err(&client->dev, "%s: read error\n", __FUNCTION__); - return -EIO; - } + tm->tm_sec = BCD2BIN(rs5c->regs[RS5C372_REG_SECS] & 0x7f); + tm->tm_min = BCD2BIN(rs5c->regs[RS5C372_REG_MINS] & 0x7f); + tm->tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C372_REG_HOURS]); - tm->tm_sec = BCD2BIN(buf[RS5C372_REG_SECS] & 0x7f); - tm->tm_min = BCD2BIN(buf[RS5C372_REG_MINS] & 0x7f); - tm->tm_hour = BCD2BIN(buf[RS5C372_REG_HOURS] & 0x3f); - tm->tm_wday = BCD2BIN(buf[RS5C372_REG_WDAY] & 0x07); - tm->tm_mday = BCD2BIN(buf[RS5C372_REG_DAY] & 0x3f); + tm->tm_wday = BCD2BIN(rs5c->regs[RS5C372_REG_WDAY] & 0x07); + tm->tm_mday = BCD2BIN(rs5c->regs[RS5C372_REG_DAY] & 0x3f); /* tm->tm_mon is zero-based */ - tm->tm_mon = BCD2BIN(buf[RS5C372_REG_MONTH] & 0x1f) - 1; + tm->tm_mon = BCD2BIN(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1; /* year is 1900 + tm->tm_year */ - tm->tm_year = BCD2BIN(buf[RS5C372_REG_YEAR]) + 100; + tm->tm_year = BCD2BIN(rs5c->regs[RS5C372_REG_YEAR]) + 100; dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " "mday=%d, mon=%d, year=%d, wday=%d\n", @@ -89,22 +179,25 @@ static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm) tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); - return 0; + /* rtc might need initialization */ + return rtc_valid_tm(tm); } static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm) { - unsigned char buf[8] = { RS5C372_REG_BASE }; + struct rs5c372 *rs5c = i2c_get_clientdata(client); + unsigned char buf[8]; - dev_dbg(&client->dev, - "%s: secs=%d, mins=%d, hours=%d " + dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d " "mday=%d, mon=%d, year=%d, wday=%d\n", - __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour, + __FUNCTION__, + tm->tm_sec, tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); + buf[0] = RS5C_ADDR(RS5C372_REG_SECS); buf[1] = BIN2BCD(tm->tm_sec); buf[2] = BIN2BCD(tm->tm_min); - buf[3] = BIN2BCD(tm->tm_hour); + buf[3] = rs5c_hr2reg(rs5c, tm->tm_hour); buf[4] = BIN2BCD(tm->tm_wday); buf[5] = BIN2BCD(tm->tm_mday); buf[6] = BIN2BCD(tm->tm_mon + 1); @@ -118,21 +211,43 @@ static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm) return 0; } +#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE) +#define NEED_TRIM +#endif + +#if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE) +#define NEED_TRIM +#endif + +#ifdef NEED_TRIM static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim) { struct rs5c372 *rs5c372 = i2c_get_clientdata(client); - u8 tmp = rs5c372->regs[RS5C372_REG_TRIM + 1]; + u8 tmp = rs5c372->regs[RS5C372_REG_TRIM]; if (osc) *osc = (tmp & RS5C372_TRIM_XSL) ? 32000 : 32768; if (trim) { - *trim = tmp & RS5C372_TRIM_MASK; - dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, *trim); + dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, tmp); + tmp &= RS5C372_TRIM_MASK; + if (tmp & 0x3e) { + int t = tmp & 0x3f; + + if (tmp & 0x40) + t = (~t | (s8)0xc0) + 1; + else + t = t - 1; + + tmp = t * 2; + } else + tmp = 0; + *trim = tmp; } return 0; } +#endif static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm) { @@ -144,25 +259,190 @@ static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm) return rs5c372_set_datetime(to_i2c_client(dev), tm); } +#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE) + +static int +rs5c_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) +{ + struct i2c_client *client = to_i2c_client(dev); + struct rs5c372 *rs5c = i2c_get_clientdata(client); + unsigned char buf[2]; + int status; + + buf[1] = rs5c->regs[RS5C_REG_CTRL1]; + switch (cmd) { + case RTC_UIE_OFF: + case RTC_UIE_ON: + /* some 327a modes use a different IRQ pin for 1Hz irqs */ + if (rs5c->type == rtc_rs5c372a + && (buf[1] & RS5C372A_CTRL1_SL1)) + return -ENOIOCTLCMD; + case RTC_AIE_OFF: + case RTC_AIE_ON: + /* these irq management calls only make sense for chips + * which are wired up to an IRQ. + */ + if (!rs5c->has_irq) + return -ENOIOCTLCMD; + break; + default: + return -ENOIOCTLCMD; + } + + status = rs5c_get_regs(rs5c); + if (status < 0) + return status; + + buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); + switch (cmd) { + case RTC_AIE_OFF: /* alarm off */ + buf[1] &= ~RS5C_CTRL1_AALE; + break; + case RTC_AIE_ON: /* alarm on */ + buf[1] |= RS5C_CTRL1_AALE; + break; + case RTC_UIE_OFF: /* update off */ + buf[1] &= ~RS5C_CTRL1_CT_MASK; + break; + case RTC_UIE_ON: /* update on */ + buf[1] &= ~RS5C_CTRL1_CT_MASK; + buf[1] |= RS5C_CTRL1_CT4; + break; + } + if ((i2c_master_send(client, buf, 2)) != 2) { + printk(KERN_WARNING "%s: can't update alarm\n", + rs5c->rtc->name); + status = -EIO; + } else + rs5c->regs[RS5C_REG_CTRL1] = buf[1]; + return status; +} + +#else +#define rs5c_rtc_ioctl NULL +#endif + + +/* NOTE: Since RTC_WKALM_{RD,SET} were originally defined for EFI, + * which only exposes a polled programming interface; and since + * these calls map directly to those EFI requests; we don't demand + * we have an IRQ for this chip when we go through this API. + * + * The older x86_pc derived RTC_ALM_{READ,SET} calls require irqs + * though, managed through RTC_AIE_{ON,OFF} requests. + */ + +static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t) +{ + struct i2c_client *client = to_i2c_client(dev); + struct rs5c372 *rs5c = i2c_get_clientdata(client); + int status; + + status = rs5c_get_regs(rs5c); + if (status < 0) + return status; + + /* report alarm time */ + t->time.tm_sec = 0; + t->time.tm_min = BCD2BIN(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f); + t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]); + t->time.tm_mday = -1; + t->time.tm_mon = -1; + t->time.tm_year = -1; + t->time.tm_wday = -1; + t->time.tm_yday = -1; + t->time.tm_isdst = -1; + + /* ... and status */ + t->enabled = !!(rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE); + t->pending = !!(rs5c->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_AAFG); + + return 0; +} + +static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t) +{ + struct i2c_client *client = to_i2c_client(dev); + struct rs5c372 *rs5c = i2c_get_clientdata(client); + int status; + unsigned char buf[4]; + + /* only handle up to 24 hours in the future, like RTC_ALM_SET */ + if (t->time.tm_mday != -1 + || t->time.tm_mon != -1 + || t->time.tm_year != -1) + return -EINVAL; + + /* REVISIT: round up tm_sec */ + + /* if needed, disable irq (clears pending status) */ + status = rs5c_get_regs(rs5c); + if (status < 0) + return status; + if (rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE) { + buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); + buf[1] = rs5c->regs[RS5C_REG_CTRL1] & ~RS5C_CTRL1_AALE; + if (i2c_master_send(client, buf, 2) != 2) { + pr_debug("%s: can't disable alarm\n", rs5c->rtc->name); + return -EIO; + } + rs5c->regs[RS5C_REG_CTRL1] = buf[1]; + } + + /* set alarm */ + buf[0] = RS5C_ADDR(RS5C_REG_ALARM_A_MIN); + buf[1] = BIN2BCD(t->time.tm_min); + buf[2] = rs5c_hr2reg(rs5c, t->time.tm_hour); + buf[3] = 0x7f; /* any/all days */ + if ((i2c_master_send(client, buf, 4)) != 4) { + pr_debug("%s: can't set alarm time\n", rs5c->rtc->name); + return -EIO; + } + + /* ... and maybe enable its irq */ + if (t->enabled) { + buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); + buf[1] = rs5c->regs[RS5C_REG_CTRL1] | RS5C_CTRL1_AALE; + if ((i2c_master_send(client, buf, 2)) != 2) + printk(KERN_WARNING "%s: can't enable alarm\n", + rs5c->rtc->name); + rs5c->regs[RS5C_REG_CTRL1] = buf[1]; + } + + return 0; +} + +#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE) + static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq) { int err, osc, trim; err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim); if (err == 0) { - seq_printf(seq, "%d.%03d KHz\n", osc / 1000, osc % 1000); - seq_printf(seq, "trim\t: %d\n", trim); + seq_printf(seq, "crystal\t\t: %d.%03d KHz\n", + osc / 1000, osc % 1000); + seq_printf(seq, "trim\t\t: %d\n", trim); } return 0; } +#else +#define rs5c372_rtc_proc NULL +#endif + static const struct rtc_class_ops rs5c372_rtc_ops = { .proc = rs5c372_rtc_proc, + .ioctl = rs5c_rtc_ioctl, .read_time = rs5c372_rtc_read_time, .set_time = rs5c372_rtc_set_time, + .read_alarm = rs5c_read_alarm, + .set_alarm = rs5c_set_alarm, }; +#if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE) + static ssize_t rs5c372_sysfs_show_trim(struct device *dev, struct device_attribute *attr, char *buf) { @@ -172,7 +452,7 @@ static ssize_t rs5c372_sysfs_show_trim(struct device *dev, if (err) return err; - return sprintf(buf, "0x%2x\n", trim); + return sprintf(buf, "%d\n", trim); } static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL); @@ -189,18 +469,37 @@ static ssize_t rs5c372_sysfs_show_osc(struct device *dev, } static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL); -static int rs5c372_attach(struct i2c_adapter *adapter) +static int rs5c_sysfs_register(struct device *dev) { - return i2c_probe(adapter, &addr_data, rs5c372_probe); + int err; + + err = device_create_file(dev, &dev_attr_trim); + if (err) + return err; + err = device_create_file(dev, &dev_attr_osc); + if (err) + device_remove_file(dev, &dev_attr_trim); + + return err; +} + +#else +static int rs5c_sysfs_register(struct device *dev) +{ + return 0; } +#endif /* SYSFS */ + +static struct i2c_driver rs5c372_driver; static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind) { int err = 0; struct i2c_client *client; struct rs5c372 *rs5c372; + struct rtc_time tm; - dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); + dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__); if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { err = -ENODEV; @@ -211,7 +510,15 @@ static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind) err = -ENOMEM; goto exit; } - client = &rs5c372->client; + + /* we read registers 0x0f then 0x00-0x0f; skip the first one */ + rs5c372->regs=&rs5c372->buf[1]; + + /* On conversion to a "new style" i2c driver, we'll be handed + * the i2c_client (we won't create it) + */ + client = &rs5c372->dev; + rs5c372->client = client; /* I2C client */ client->addr = address; @@ -222,16 +529,99 @@ static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind) i2c_set_clientdata(client, rs5c372); - rs5c372->msg[0].addr = address; - rs5c372->msg[0].flags = I2C_M_RD; - rs5c372->msg[0].len = sizeof(rs5c372->regs); - rs5c372->msg[0].buf = rs5c372->regs; - /* Inform the i2c layer */ if ((err = i2c_attach_client(client))) goto exit_kfree; - dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); + err = rs5c_get_regs(rs5c372); + if (err < 0) + goto exit_detach; + + /* For "new style" drivers, irq is in i2c_client and chip type + * info comes from i2c_client.dev.platform_data. Meanwhile: + * + * STICK BOARD-SPECIFIC SETUP CODE RIGHT HERE + */ + if (rs5c372->type == rtc_undef) { + rs5c372->type = rtc_rs5c372b; + dev_warn(&client->dev, "assuming rs5c372b\n"); + } + + /* clock may be set for am/pm or 24 hr time */ + switch (rs5c372->type) { + case rtc_rs5c372a: + case rtc_rs5c372b: + /* alarm uses ALARM_A; and nINTRA on 372a, nINTR on 372b. + * so does periodic irq, except some 327a modes. + */ + if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C372_CTRL2_24) + rs5c372->time24 = 1; + break; + case rtc_rv5c386: + case rtc_rv5c387a: + if (rs5c372->regs[RS5C_REG_CTRL1] & RV5C387_CTRL1_24) + rs5c372->time24 = 1; + /* alarm uses ALARM_W; and nINTRB for alarm and periodic + * irq, on both 386 and 387 + */ + break; + default: + dev_err(&client->dev, "unknown RTC type\n"); + goto exit_detach; + } + + /* if the oscillator lost power and no other software (like + * the bootloader) set it up, do it here. + */ + if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_XSTP) { + unsigned char buf[3]; + + rs5c372->regs[RS5C_REG_CTRL2] &= ~RS5C_CTRL2_XSTP; + + buf[0] = RS5C_ADDR(RS5C_REG_CTRL1); + buf[1] = rs5c372->regs[RS5C_REG_CTRL1]; + buf[2] = rs5c372->regs[RS5C_REG_CTRL2]; + + /* use 24hr mode */ + switch (rs5c372->type) { + case rtc_rs5c372a: + case rtc_rs5c372b: + buf[2] |= RS5C372_CTRL2_24; + rs5c372->time24 = 1; + break; + case rtc_rv5c386: + case rtc_rv5c387a: + buf[1] |= RV5C387_CTRL1_24; + rs5c372->time24 = 1; + break; + default: + /* impossible */ + break; + } + + if ((i2c_master_send(client, buf, 3)) != 3) { + dev_err(&client->dev, "setup error\n"); + goto exit_detach; + } + rs5c372->regs[RS5C_REG_CTRL1] = buf[1]; + rs5c372->regs[RS5C_REG_CTRL2] = buf[2]; + } + + if (rs5c372_get_datetime(client, &tm) < 0) + dev_warn(&client->dev, "clock needs to be set\n"); + + dev_info(&client->dev, "%s found, %s, driver version " DRV_VERSION "\n", + ({ char *s; switch (rs5c372->type) { + case rtc_rs5c372a: s = "rs5c372a"; break; + case rtc_rs5c372b: s = "rs5c372b"; break; + case rtc_rv5c386: s = "rv5c386"; break; + case rtc_rv5c387a: s = "rv5c387a"; break; + default: s = "chip"; break; + }; s;}), + rs5c372->time24 ? "24hr" : "am/pm" + ); + + /* FIXME when client->irq exists, use it to register alarm irq */ rs5c372->rtc = rtc_device_register(rs5c372_driver.driver.name, &client->dev, &rs5c372_rtc_ops, THIS_MODULE); @@ -241,18 +631,12 @@ static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind) goto exit_detach; } - err = device_create_file(&client->dev, &dev_attr_trim); + err = rs5c_sysfs_register(&client->dev); if (err) goto exit_devreg; - err = device_create_file(&client->dev, &dev_attr_osc); - if (err) - goto exit_trim; return 0; -exit_trim: - device_remove_file(&client->dev, &dev_attr_trim); - exit_devreg: rtc_device_unregister(rs5c372->rtc); @@ -266,6 +650,11 @@ exit: return err; } +static int rs5c372_attach(struct i2c_adapter *adapter) +{ + return i2c_probe(adapter, &addr_data, rs5c372_probe); +} + static int rs5c372_detach(struct i2c_client *client) { int err; @@ -274,6 +663,8 @@ static int rs5c372_detach(struct i2c_client *client) if (rs5c372->rtc) rtc_device_unregister(rs5c372->rtc); + /* REVISIT properly destroy the sysfs files ... */ + if ((err = i2c_detach_client(client))) return err; @@ -281,6 +672,14 @@ static int rs5c372_detach(struct i2c_client *client) return 0; } +static struct i2c_driver rs5c372_driver = { + .driver = { + .name = "rtc-rs5c372", + }, + .attach_adapter = &rs5c372_attach, + .detach_client = &rs5c372_detach, +}; + static __init int rs5c372_init(void) { return i2c_add_driver(&rs5c372_driver); diff --git a/drivers/rtc/rtc-s3c.c b/drivers/rtc/rtc-s3c.c index e301dea57bb3..f406a2b55aea 100644 --- a/drivers/rtc/rtc-s3c.c +++ b/drivers/rtc/rtc-s3c.c @@ -191,6 +191,8 @@ static int s3c_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alrm) alm_en = readb(base + S3C2410_RTCALM); + alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0; + pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n", alm_en, alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday, @@ -331,12 +333,8 @@ static int s3c_rtc_ioctl(struct device *dev, static int s3c_rtc_proc(struct device *dev, struct seq_file *seq) { - unsigned int rtcalm = readb(s3c_rtc_base + S3C2410_RTCALM); unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT); - seq_printf(seq, "alarm_IRQ\t: %s\n", - (rtcalm & S3C2410_RTCALM_ALMEN) ? "yes" : "no" ); - seq_printf(seq, "periodic_IRQ\t: %s\n", (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" ); diff --git a/drivers/rtc/rtc-sa1100.c b/drivers/rtc/rtc-sa1100.c index bd4d7d174ef4..9c8ead43a59c 100644 --- a/drivers/rtc/rtc-sa1100.c +++ b/drivers/rtc/rtc-sa1100.c @@ -289,9 +289,7 @@ static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) { - seq_printf(seq, "trim/divider\t: 0x%08lx\n", RTTR); - seq_printf(seq, "alarm_IRQ\t: %s\n", - (RTSR & RTSR_ALE) ? "yes" : "no" ); + seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR); seq_printf(seq, "update_IRQ\t: %s\n", (RTSR & RTSR_HZE) ? "yes" : "no"); seq_printf(seq, "periodic_IRQ\t: %s\n", diff --git a/drivers/rtc/rtc-sh.c b/drivers/rtc/rtc-sh.c index 143302a8e79c..198b9f22fbff 100644 --- a/drivers/rtc/rtc-sh.c +++ b/drivers/rtc/rtc-sh.c @@ -2,6 +2,7 @@ * SuperH On-Chip RTC Support * * Copyright (C) 2006 Paul Mundt + * Copyright (C) 2006 Jamie Lenehan * * Based on the old arch/sh/kernel/cpu/rtc.c by: * @@ -21,7 +22,10 @@ #include <linux/seq_file.h> #include <linux/interrupt.h> #include <linux/spinlock.h> -#include <asm/io.h> +#include <linux/io.h> + +#define DRV_NAME "sh-rtc" +#define DRV_VERSION "0.1.2" #ifdef CONFIG_CPU_SH3 #define rtc_reg_size sizeof(u16) @@ -33,22 +37,26 @@ #define RTC_REG(r) ((r) * rtc_reg_size) -#define R64CNT RTC_REG(0) -#define RSECCNT RTC_REG(1) -#define RMINCNT RTC_REG(2) -#define RHRCNT RTC_REG(3) -#define RWKCNT RTC_REG(4) -#define RDAYCNT RTC_REG(5) -#define RMONCNT RTC_REG(6) -#define RYRCNT RTC_REG(7) -#define RSECAR RTC_REG(8) -#define RMINAR RTC_REG(9) -#define RHRAR RTC_REG(10) -#define RWKAR RTC_REG(11) -#define RDAYAR RTC_REG(12) -#define RMONAR RTC_REG(13) -#define RCR1 RTC_REG(14) -#define RCR2 RTC_REG(15) +#define R64CNT RTC_REG(0) + +#define RSECCNT RTC_REG(1) /* RTC sec */ +#define RMINCNT RTC_REG(2) /* RTC min */ +#define RHRCNT RTC_REG(3) /* RTC hour */ +#define RWKCNT RTC_REG(4) /* RTC week */ +#define RDAYCNT RTC_REG(5) /* RTC day */ +#define RMONCNT RTC_REG(6) /* RTC month */ +#define RYRCNT RTC_REG(7) /* RTC year */ +#define RSECAR RTC_REG(8) /* ALARM sec */ +#define RMINAR RTC_REG(9) /* ALARM min */ +#define RHRAR RTC_REG(10) /* ALARM hour */ +#define RWKAR RTC_REG(11) /* ALARM week */ +#define RDAYAR RTC_REG(12) /* ALARM day */ +#define RMONAR RTC_REG(13) /* ALARM month */ +#define RCR1 RTC_REG(14) /* Control */ +#define RCR2 RTC_REG(15) /* Control */ + +/* ALARM Bits - or with BCD encoded value */ +#define AR_ENB 0x80 /* Enable for alarm cmp */ /* RCR1 Bits */ #define RCR1_CF 0x80 /* Carry Flag */ @@ -71,22 +79,28 @@ struct sh_rtc { unsigned int alarm_irq, periodic_irq, carry_irq; struct rtc_device *rtc_dev; spinlock_t lock; + int rearm_aie; }; -static irqreturn_t sh_rtc_interrupt(int irq, void *id) +static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id) { - struct platform_device *pdev = id; + struct platform_device *pdev = to_platform_device(dev_id); struct sh_rtc *rtc = platform_get_drvdata(pdev); unsigned int tmp, events = 0; spin_lock(&rtc->lock); tmp = readb(rtc->regbase + RCR1); + tmp &= ~RCR1_CF; - if (tmp & RCR1_AF) - events |= RTC_AF | RTC_IRQF; - - tmp &= ~(RCR1_CF | RCR1_AF); + if (rtc->rearm_aie) { + if (tmp & RCR1_AF) + tmp &= ~RCR1_AF; /* try to clear AF again */ + else { + tmp |= RCR1_AIE; /* AF has cleared, rearm IRQ */ + rtc->rearm_aie = 0; + } + } writeb(tmp, rtc->regbase + RCR1); @@ -97,9 +111,45 @@ static irqreturn_t sh_rtc_interrupt(int irq, void *id) return IRQ_HANDLED; } -static irqreturn_t sh_rtc_periodic(int irq, void *id) +static irqreturn_t sh_rtc_alarm(int irq, void *dev_id) +{ + struct platform_device *pdev = to_platform_device(dev_id); + struct sh_rtc *rtc = platform_get_drvdata(pdev); + unsigned int tmp, events = 0; + + spin_lock(&rtc->lock); + + tmp = readb(rtc->regbase + RCR1); + + /* + * If AF is set then the alarm has triggered. If we clear AF while + * the alarm time still matches the RTC time then AF will + * immediately be set again, and if AIE is enabled then the alarm + * interrupt will immediately be retrigger. So we clear AIE here + * and use rtc->rearm_aie so that the carry interrupt will keep + * trying to clear AF and once it stays cleared it'll re-enable + * AIE. + */ + if (tmp & RCR1_AF) { + events |= RTC_AF | RTC_IRQF; + + tmp &= ~(RCR1_AF|RCR1_AIE); + + writeb(tmp, rtc->regbase + RCR1); + + rtc->rearm_aie = 1; + + rtc_update_irq(&rtc->rtc_dev->class_dev, 1, events); + } + + spin_unlock(&rtc->lock); + return IRQ_HANDLED; +} + +static irqreturn_t sh_rtc_periodic(int irq, void *dev_id) { - struct sh_rtc *rtc = dev_get_drvdata(id); + struct platform_device *pdev = to_platform_device(dev_id); + struct sh_rtc *rtc = platform_get_drvdata(pdev); spin_lock(&rtc->lock); @@ -139,10 +189,11 @@ static inline void sh_rtc_setaie(struct device *dev, unsigned int enable) tmp = readb(rtc->regbase + RCR1); - if (enable) - tmp |= RCR1_AIE; - else + if (!enable) { tmp &= ~RCR1_AIE; + rtc->rearm_aie = 0; + } else if (rtc->rearm_aie == 0) + tmp |= RCR1_AIE; writeb(tmp, rtc->regbase + RCR1); @@ -177,7 +228,7 @@ static int sh_rtc_open(struct device *dev) goto err_bad_carry; } - ret = request_irq(rtc->alarm_irq, sh_rtc_interrupt, IRQF_DISABLED, + ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED, "sh-rtc alarm", dev); if (unlikely(ret)) { dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n", @@ -200,6 +251,7 @@ static void sh_rtc_release(struct device *dev) struct sh_rtc *rtc = dev_get_drvdata(dev); sh_rtc_setpie(dev, 0); + sh_rtc_setaie(dev, 0); free_irq(rtc->periodic_irq, dev); free_irq(rtc->carry_irq, dev); @@ -212,8 +264,6 @@ static int sh_rtc_proc(struct device *dev, struct seq_file *seq) unsigned int tmp; tmp = readb(rtc->regbase + RCR1); - seq_printf(seq, "alarm_IRQ\t: %s\n", - (tmp & RCR1_AIE) ? "yes" : "no"); seq_printf(seq, "carry_IRQ\t: %s\n", (tmp & RCR1_CIE) ? "yes" : "no"); @@ -267,7 +317,7 @@ static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm) tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT)); tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT)); tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT)); - tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)); + tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1; #if defined(CONFIG_CPU_SH4) yr = readw(rtc->regbase + RYRCNT); @@ -295,7 +345,7 @@ static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm) "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour, - tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); + tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday); if (rtc_valid_tm(tm) < 0) dev_err(dev, "invalid date\n"); @@ -322,7 +372,7 @@ static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm) writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT); writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT); writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT); - writeb(BIN2BCD(tm->tm_mon), rtc->regbase + RMONCNT); + writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT); #ifdef CONFIG_CPU_SH3 year = tm->tm_year % 100; @@ -344,12 +394,140 @@ static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm) return 0; } +static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off) +{ + unsigned int byte; + int value = 0xff; /* return 0xff for ignored values */ + + byte = readb(rtc->regbase + reg_off); + if (byte & AR_ENB) { + byte &= ~AR_ENB; /* strip the enable bit */ + value = BCD2BIN(byte); + } + + return value; +} + +static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) +{ + struct platform_device *pdev = to_platform_device(dev); + struct sh_rtc *rtc = platform_get_drvdata(pdev); + struct rtc_time* tm = &wkalrm->time; + + spin_lock_irq(&rtc->lock); + + tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR); + tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR); + tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR); + tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR); + tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR); + tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR); + if (tm->tm_mon > 0) + tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */ + tm->tm_year = 0xffff; + + wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0; + + spin_unlock_irq(&rtc->lock); + + return 0; +} + +static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc, + int value, int reg_off) +{ + /* < 0 for a value that is ignored */ + if (value < 0) + writeb(0, rtc->regbase + reg_off); + else + writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off); +} + +static int sh_rtc_check_alarm(struct rtc_time* tm) +{ + /* + * The original rtc says anything > 0xc0 is "don't care" or "match + * all" - most users use 0xff but rtc-dev uses -1 for the same thing. + * The original rtc doesn't support years - some things use -1 and + * some 0xffff. We use -1 to make out tests easier. + */ + if (tm->tm_year == 0xffff) + tm->tm_year = -1; + if (tm->tm_mon >= 0xff) + tm->tm_mon = -1; + if (tm->tm_mday >= 0xff) + tm->tm_mday = -1; + if (tm->tm_wday >= 0xff) + tm->tm_wday = -1; + if (tm->tm_hour >= 0xff) + tm->tm_hour = -1; + if (tm->tm_min >= 0xff) + tm->tm_min = -1; + if (tm->tm_sec >= 0xff) + tm->tm_sec = -1; + + if (tm->tm_year > 9999 || + tm->tm_mon >= 12 || + tm->tm_mday == 0 || tm->tm_mday >= 32 || + tm->tm_wday >= 7 || + tm->tm_hour >= 24 || + tm->tm_min >= 60 || + tm->tm_sec >= 60) + return -EINVAL; + + return 0; +} + +static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) +{ + struct platform_device *pdev = to_platform_device(dev); + struct sh_rtc *rtc = platform_get_drvdata(pdev); + unsigned int rcr1; + struct rtc_time *tm = &wkalrm->time; + int mon, err; + + err = sh_rtc_check_alarm(tm); + if (unlikely(err < 0)) + return err; + + spin_lock_irq(&rtc->lock); + + /* disable alarm interrupt and clear the alarm flag */ + rcr1 = readb(rtc->regbase + RCR1); + rcr1 &= ~(RCR1_AF|RCR1_AIE); + writeb(rcr1, rtc->regbase + RCR1); + + rtc->rearm_aie = 0; + + /* set alarm time */ + sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR); + sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR); + sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR); + sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR); + sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR); + mon = tm->tm_mon; + if (mon >= 0) + mon += 1; + sh_rtc_write_alarm_value(rtc, mon, RMONAR); + + if (wkalrm->enabled) { + rcr1 |= RCR1_AIE; + writeb(rcr1, rtc->regbase + RCR1); + } + + spin_unlock_irq(&rtc->lock); + + return 0; +} + static struct rtc_class_ops sh_rtc_ops = { .open = sh_rtc_open, .release = sh_rtc_release, .ioctl = sh_rtc_ioctl, .read_time = sh_rtc_read_time, .set_time = sh_rtc_set_time, + .read_alarm = sh_rtc_read_alarm, + .set_alarm = sh_rtc_set_alarm, .proc = sh_rtc_proc, }; @@ -442,7 +620,7 @@ static int __devexit sh_rtc_remove(struct platform_device *pdev) } static struct platform_driver sh_rtc_platform_driver = { .driver = { - .name = "sh-rtc", + .name = DRV_NAME, .owner = THIS_MODULE, }, .probe = sh_rtc_probe, @@ -463,5 +641,6 @@ module_init(sh_rtc_init); module_exit(sh_rtc_exit); MODULE_DESCRIPTION("SuperH on-chip RTC driver"); -MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); +MODULE_VERSION(DRV_VERSION); +MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, Jamie Lenehan <lenehan@twibble.org>"); MODULE_LICENSE("GPL"); diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c index 625637b84d33..2ddd0cf07140 100644 --- a/drivers/rtc/rtc-sysfs.c +++ b/drivers/rtc/rtc-sysfs.c @@ -78,12 +78,12 @@ static struct attribute_group rtc_attr_group = { .attrs = rtc_attrs, }; -static int __devinit rtc_sysfs_add_device(struct class_device *class_dev, +static int rtc_sysfs_add_device(struct class_device *class_dev, struct class_interface *class_intf) { int err; - dev_info(class_dev->dev, "rtc intf: sysfs\n"); + dev_dbg(class_dev->dev, "rtc intf: sysfs\n"); err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group); if (err) diff --git a/drivers/rtc/rtc-x1205.c b/drivers/rtc/rtc-x1205.c index 9a67487d086b..019ae255b0c8 100644 --- a/drivers/rtc/rtc-x1205.c +++ b/drivers/rtc/rtc-x1205.c @@ -372,7 +372,7 @@ static int x1205_validate_client(struct i2c_client *client) }; if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) { - dev_err(&client->adapter->dev, + dev_err(&client->dev, "%s: could not read register %x\n", __FUNCTION__, probe_zero_pattern[i]); @@ -380,7 +380,7 @@ static int x1205_validate_client(struct i2c_client *client) } if ((buf & probe_zero_pattern[i+1]) != 0) { - dev_err(&client->adapter->dev, + dev_err(&client->dev, "%s: register=%02x, zero pattern=%d, value=%x\n", __FUNCTION__, probe_zero_pattern[i], i, buf); @@ -400,7 +400,7 @@ static int x1205_validate_client(struct i2c_client *client) }; if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) { - dev_err(&client->adapter->dev, + dev_err(&client->dev, "%s: could not read register %x\n", __FUNCTION__, probe_limits_pattern[i].reg); @@ -411,7 +411,7 @@ static int x1205_validate_client(struct i2c_client *client) if (value > probe_limits_pattern[i].max || value < probe_limits_pattern[i].min) { - dev_dbg(&client->adapter->dev, + dev_dbg(&client->dev, "%s: register=%x, lim pattern=%d, value=%d\n", __FUNCTION__, probe_limits_pattern[i].reg, i, value); @@ -506,7 +506,7 @@ static int x1205_probe(struct i2c_adapter *adapter, int address, int kind) struct i2c_client *client; struct rtc_device *rtc; - dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); + dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__); if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { err = -ENODEV; |