aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc')
-rw-r--r--drivers/rtc/Kconfig14
-rw-r--r--drivers/rtc/Makefile3
-rw-r--r--drivers/rtc/rtc-at91rm9200.c (renamed from drivers/rtc/rtc-at91.c)7
-rw-r--r--drivers/rtc/rtc-dev.c9
-rw-r--r--drivers/rtc/rtc-ds1672.c11
-rw-r--r--drivers/rtc/rtc-ds1742.c67
-rw-r--r--drivers/rtc/rtc-lib.c81
-rw-r--r--drivers/rtc/rtc-omap.c571
-rw-r--r--drivers/rtc/rtc-pcf8563.c6
-rw-r--r--drivers/rtc/rtc-proc.c4
-rw-r--r--drivers/rtc/rtc-rs5c372.c565
-rw-r--r--drivers/rtc/rtc-s3c.c6
-rw-r--r--drivers/rtc/rtc-sa1100.c4
-rw-r--r--drivers/rtc/rtc-sh.c249
-rw-r--r--drivers/rtc/rtc-sysfs.c2
-rw-r--r--drivers/rtc/rtc-test.c9
-rw-r--r--drivers/rtc/rtc-x1205.c22
17 files changed, 1455 insertions, 175 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index fc766a7a611e..09660e2ab051 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -154,15 +154,23 @@ config RTC_DRV_DS1672
154 will be called rtc-ds1672. 154 will be called rtc-ds1672.
155 155
156config RTC_DRV_DS1742 156config RTC_DRV_DS1742
157 tristate "Dallas DS1742" 157 tristate "Dallas DS1742/1743"
158 depends on RTC_CLASS 158 depends on RTC_CLASS
159 help 159 help
160 If you say yes here you get support for the 160 If you say yes here you get support for the
161 Dallas DS1742 timekeeping chip. 161 Dallas DS1742/1743 timekeeping chip.
162 162
163 This driver can also be built as a module. If so, the module 163 This driver can also be built as a module. If so, the module
164 will be called rtc-ds1742. 164 will be called rtc-ds1742.
165 165
166config RTC_DRV_OMAP
167 tristate "TI OMAP1"
168 depends on RTC_CLASS && ( \
169 ARCH_OMAP15XX || ARCH_OMAP16XX || ARCH_OMAP730 )
170 help
171 Say "yes" here to support the real time clock on TI OMAP1 chips.
172 This driver can also be built as a module called rtc-omap.
173
166config RTC_DRV_PCF8563 174config RTC_DRV_PCF8563
167 tristate "Philips PCF8563/Epson RTC8564" 175 tristate "Philips PCF8563/Epson RTC8564"
168 depends on RTC_CLASS && I2C 176 depends on RTC_CLASS && I2C
@@ -280,7 +288,7 @@ config RTC_DRV_PL031
280 To compile this driver as a module, choose M here: the 288 To compile this driver as a module, choose M here: the
281 module will be called rtc-pl031. 289 module will be called rtc-pl031.
282 290
283config RTC_DRV_AT91 291config RTC_DRV_AT91RM9200
284 tristate "AT91RM9200" 292 tristate "AT91RM9200"
285 depends on RTC_CLASS && ARCH_AT91RM9200 293 depends on RTC_CLASS && ARCH_AT91RM9200
286 help 294 help
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 3ba5ff6e6800..e6beedacc966 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
21obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o 21obj-$(CONFIG_RTC_DRV_DS1307) += rtc-ds1307.o
22obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o 22obj-$(CONFIG_RTC_DRV_DS1672) += rtc-ds1672.o
23obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o 23obj-$(CONFIG_RTC_DRV_DS1742) += rtc-ds1742.o
24obj-$(CONFIG_RTC_DRV_OMAP) += rtc-omap.o
24obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o 25obj-$(CONFIG_RTC_DRV_PCF8563) += rtc-pcf8563.o
25obj-$(CONFIG_RTC_DRV_PCF8583) += rtc-pcf8583.o 26obj-$(CONFIG_RTC_DRV_PCF8583) += rtc-pcf8583.o
26obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o 27obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o
@@ -34,5 +35,5 @@ obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41xx.o
34obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o 35obj-$(CONFIG_RTC_DRV_PL031) += rtc-pl031.o
35obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o 36obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o
36obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o 37obj-$(CONFIG_RTC_DRV_V3020) += rtc-v3020.o
37obj-$(CONFIG_RTC_DRV_AT91) += rtc-at91.o 38obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o
38obj-$(CONFIG_RTC_DRV_SH) += rtc-sh.o 39obj-$(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 @@
33 33
34#include <asm/mach/time.h> 34#include <asm/mach/time.h>
35 35
36#include <asm/arch/at91_rtc.h>
37
36 38
37#define AT91_RTC_FREQ 1 39#define AT91_RTC_FREQ 1
38#define AT91_RTC_EPOCH 1900UL /* just like arch/arm/common/rtctime.c */ 40#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)
137 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); 139 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
138 tm->tm_year = at91_alarm_year - 1900; 140 tm->tm_year = at91_alarm_year - 1900;
139 141
142 alrm->enabled = (at91_sys_read(AT91_RTC_IMR) & AT91_RTC_ALARM)
143 ? 1 : 0;
144
140 pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __FUNCTION__, 145 pr_debug("%s(): %4d-%02d-%02d %02d:%02d:%02d\n", __FUNCTION__,
141 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday, 146 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
142 tm->tm_hour, tm->tm_min, tm->tm_sec); 147 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)
223{ 228{
224 unsigned long imr = at91_sys_read(AT91_RTC_IMR); 229 unsigned long imr = at91_sys_read(AT91_RTC_IMR);
225 230
226 seq_printf(seq, "alarm_IRQ\t: %s\n",
227 (imr & AT91_RTC_ALARM) ? "yes" : "no");
228 seq_printf(seq, "update_IRQ\t: %s\n", 231 seq_printf(seq, "update_IRQ\t: %s\n",
229 (imr & AT91_RTC_ACKUPD) ? "yes" : "no"); 232 (imr & AT91_RTC_ACKUPD) ? "yes" : "no");
230 seq_printf(seq, "periodic_IRQ\t: %s\n", 233 seq_printf(seq, "periodic_IRQ\t: %s\n",
diff --git a/drivers/rtc/rtc-dev.c b/drivers/rtc/rtc-dev.c
index 814b9e1873f5..94d3df62a5fa 100644
--- a/drivers/rtc/rtc-dev.c
+++ b/drivers/rtc/rtc-dev.c
@@ -53,9 +53,10 @@ static int rtc_dev_open(struct inode *inode, struct file *file)
53 * Routine to poll RTC seconds field for change as often as possible, 53 * Routine to poll RTC seconds field for change as often as possible,
54 * after first RTC_UIE use timer to reduce polling 54 * after first RTC_UIE use timer to reduce polling
55 */ 55 */
56static void rtc_uie_task(void *data) 56static void rtc_uie_task(struct work_struct *work)
57{ 57{
58 struct rtc_device *rtc = data; 58 struct rtc_device *rtc =
59 container_of(work, struct rtc_device, uie_task);
59 struct rtc_time tm; 60 struct rtc_time tm;
60 int num = 0; 61 int num = 0;
61 int err; 62 int err;
@@ -411,7 +412,7 @@ static int rtc_dev_add_device(struct class_device *class_dev,
411 spin_lock_init(&rtc->irq_lock); 412 spin_lock_init(&rtc->irq_lock);
412 init_waitqueue_head(&rtc->irq_queue); 413 init_waitqueue_head(&rtc->irq_queue);
413#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 414#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
414 INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc); 415 INIT_WORK(&rtc->uie_task, rtc_uie_task);
415 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc); 416 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
416#endif 417#endif
417 418
@@ -434,7 +435,7 @@ static int rtc_dev_add_device(struct class_device *class_dev,
434 goto err_cdev_del; 435 goto err_cdev_del;
435 } 436 }
436 437
437 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n", 438 dev_dbg(class_dev->dev, "rtc intf: dev (%d:%d)\n",
438 MAJOR(rtc->rtc_dev->devt), 439 MAJOR(rtc->rtc_dev->devt),
439 MINOR(rtc->rtc_dev->devt)); 440 MINOR(rtc->rtc_dev->devt));
440 441
diff --git a/drivers/rtc/rtc-ds1672.c b/drivers/rtc/rtc-ds1672.c
index 67e816a9a39f..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)
199 struct i2c_client *client; 199 struct i2c_client *client;
200 struct rtc_device *rtc; 200 struct rtc_device *rtc;
201 201
202 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); 202 dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__);
203 203
204 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { 204 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
205 err = -ENODEV; 205 err = -ENODEV;
@@ -237,17 +237,22 @@ static int ds1672_probe(struct i2c_adapter *adapter, int address, int kind)
237 /* read control register */ 237 /* read control register */
238 err = ds1672_get_control(client, &control); 238 err = ds1672_get_control(client, &control);
239 if (err) 239 if (err)
240 goto exit_detach; 240 goto exit_devreg;
241 241
242 if (control & DS1672_REG_CONTROL_EOSC) 242 if (control & DS1672_REG_CONTROL_EOSC)
243 dev_warn(&client->dev, "Oscillator not enabled. " 243 dev_warn(&client->dev, "Oscillator not enabled. "
244 "Set time to enable.\n"); 244 "Set time to enable.\n");
245 245
246 /* Register sysfs hooks */ 246 /* Register sysfs hooks */
247 device_create_file(&client->dev, &dev_attr_control); 247 err = device_create_file(&client->dev, &dev_attr_control);
248 if (err)
249 goto exit_devreg;
248 250
249 return 0; 251 return 0;
250 252
253exit_devreg:
254 rtc_device_unregister(rtc);
255
251exit_detach: 256exit_detach:
252 i2c_detach_client(client); 257 i2c_detach_client(client);
253 258
diff --git a/drivers/rtc/rtc-ds1742.c b/drivers/rtc/rtc-ds1742.c
index 6273a3d240a2..17633bfa8480 100644
--- a/drivers/rtc/rtc-ds1742.c
+++ b/drivers/rtc/rtc-ds1742.c
@@ -6,6 +6,10 @@
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 *
10 * Copyright (C) 2006 Torsten Ertbjerg Rasmussen <tr@newtec.dk>
11 * - nvram size determined from resource
12 * - this ds1742 driver now supports ds1743.
9 */ 13 */
10 14
11#include <linux/bcd.h> 15#include <linux/bcd.h>
@@ -17,20 +21,19 @@
17#include <linux/platform_device.h> 21#include <linux/platform_device.h>
18#include <linux/io.h> 22#include <linux/io.h>
19 23
20#define DRV_VERSION "0.2" 24#define DRV_VERSION "0.3"
21 25
22#define RTC_REG_SIZE 0x800 26#define RTC_SIZE 8
23#define RTC_OFFSET 0x7f8
24 27
25#define RTC_CONTROL (RTC_OFFSET + 0) 28#define RTC_CONTROL 0
26#define RTC_CENTURY (RTC_OFFSET + 0) 29#define RTC_CENTURY 0
27#define RTC_SECONDS (RTC_OFFSET + 1) 30#define RTC_SECONDS 1
28#define RTC_MINUTES (RTC_OFFSET + 2) 31#define RTC_MINUTES 2
29#define RTC_HOURS (RTC_OFFSET + 3) 32#define RTC_HOURS 3
30#define RTC_DAY (RTC_OFFSET + 4) 33#define RTC_DAY 4
31#define RTC_DATE (RTC_OFFSET + 5) 34#define RTC_DATE 5
32#define RTC_MONTH (RTC_OFFSET + 6) 35#define RTC_MONTH 6
33#define RTC_YEAR (RTC_OFFSET + 7) 36#define RTC_YEAR 7
34 37
35#define RTC_CENTURY_MASK 0x3f 38#define RTC_CENTURY_MASK 0x3f
36#define RTC_SECONDS_MASK 0x7f 39#define RTC_SECONDS_MASK 0x7f
@@ -48,7 +51,10 @@
48 51
49struct rtc_plat_data { 52struct rtc_plat_data {
50 struct rtc_device *rtc; 53 struct rtc_device *rtc;
51 void __iomem *ioaddr; 54 void __iomem *ioaddr_nvram;
55 void __iomem *ioaddr_rtc;
56 size_t size_nvram;
57 size_t size;
52 unsigned long baseaddr; 58 unsigned long baseaddr;
53 unsigned long last_jiffies; 59 unsigned long last_jiffies;
54}; 60};
@@ -57,7 +63,7 @@ static int ds1742_rtc_set_time(struct device *dev, struct rtc_time *tm)
57{ 63{
58 struct platform_device *pdev = to_platform_device(dev); 64 struct platform_device *pdev = to_platform_device(dev);
59 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 65 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
60 void __iomem *ioaddr = pdata->ioaddr; 66 void __iomem *ioaddr = pdata->ioaddr_rtc;
61 u8 century; 67 u8 century;
62 68
63 century = BIN2BCD((tm->tm_year + 1900) / 100); 69 century = BIN2BCD((tm->tm_year + 1900) / 100);
@@ -82,7 +88,7 @@ static int ds1742_rtc_read_time(struct device *dev, struct rtc_time *tm)
82{ 88{
83 struct platform_device *pdev = to_platform_device(dev); 89 struct platform_device *pdev = to_platform_device(dev);
84 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 90 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
85 void __iomem *ioaddr = pdata->ioaddr; 91 void __iomem *ioaddr = pdata->ioaddr_rtc;
86 unsigned int year, month, day, hour, minute, second, week; 92 unsigned int year, month, day, hour, minute, second, week;
87 unsigned int century; 93 unsigned int century;
88 94
@@ -127,10 +133,10 @@ static ssize_t ds1742_nvram_read(struct kobject *kobj, char *buf,
127 struct platform_device *pdev = 133 struct platform_device *pdev =
128 to_platform_device(container_of(kobj, struct device, kobj)); 134 to_platform_device(container_of(kobj, struct device, kobj));
129 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 135 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
130 void __iomem *ioaddr = pdata->ioaddr; 136 void __iomem *ioaddr = pdata->ioaddr_nvram;
131 ssize_t count; 137 ssize_t count;
132 138
133 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) 139 for (count = 0; size > 0 && pos < pdata->size_nvram; count++, size--)
134 *buf++ = readb(ioaddr + pos++); 140 *buf++ = readb(ioaddr + pos++);
135 return count; 141 return count;
136} 142}
@@ -141,10 +147,10 @@ static ssize_t ds1742_nvram_write(struct kobject *kobj, char *buf,
141 struct platform_device *pdev = 147 struct platform_device *pdev =
142 to_platform_device(container_of(kobj, struct device, kobj)); 148 to_platform_device(container_of(kobj, struct device, kobj));
143 struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 149 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
144 void __iomem *ioaddr = pdata->ioaddr; 150 void __iomem *ioaddr = pdata->ioaddr_nvram;
145 ssize_t count; 151 ssize_t count;
146 152
147 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--) 153 for (count = 0; size > 0 && pos < pdata->size_nvram; count++, size--)
148 writeb(*buf++, ioaddr + pos++); 154 writeb(*buf++, ioaddr + pos++);
149 return count; 155 return count;
150} 156}
@@ -155,7 +161,6 @@ static struct bin_attribute ds1742_nvram_attr = {
155 .mode = S_IRUGO | S_IWUGO, 161 .mode = S_IRUGO | S_IWUGO,
156 .owner = THIS_MODULE, 162 .owner = THIS_MODULE,
157 }, 163 },
158 .size = RTC_OFFSET,
159 .read = ds1742_nvram_read, 164 .read = ds1742_nvram_read,
160 .write = ds1742_nvram_write, 165 .write = ds1742_nvram_write,
161}; 166};
@@ -175,19 +180,23 @@ static int __init ds1742_rtc_probe(struct platform_device *pdev)
175 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); 180 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
176 if (!pdata) 181 if (!pdata)
177 return -ENOMEM; 182 return -ENOMEM;
178 if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) { 183 pdata->size = res->end - res->start + 1;
184 if (!request_mem_region(res->start, pdata->size, pdev->name)) {
179 ret = -EBUSY; 185 ret = -EBUSY;
180 goto out; 186 goto out;
181 } 187 }
182 pdata->baseaddr = res->start; 188 pdata->baseaddr = res->start;
183 ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE); 189 ioaddr = ioremap(pdata->baseaddr, pdata->size);
184 if (!ioaddr) { 190 if (!ioaddr) {
185 ret = -ENOMEM; 191 ret = -ENOMEM;
186 goto out; 192 goto out;
187 } 193 }
188 pdata->ioaddr = ioaddr; 194 pdata->ioaddr_nvram = ioaddr;
195 pdata->size_nvram = pdata->size - RTC_SIZE;
196 pdata->ioaddr_rtc = ioaddr + pdata->size_nvram;
189 197
190 /* turn RTC on if it was not on */ 198 /* turn RTC on if it was not on */
199 ioaddr = pdata->ioaddr_rtc;
191 sec = readb(ioaddr + RTC_SECONDS); 200 sec = readb(ioaddr + RTC_SECONDS);
192 if (sec & RTC_STOP) { 201 if (sec & RTC_STOP) {
193 sec &= RTC_SECONDS_MASK; 202 sec &= RTC_SECONDS_MASK;
@@ -208,6 +217,8 @@ static int __init ds1742_rtc_probe(struct platform_device *pdev)
208 pdata->rtc = rtc; 217 pdata->rtc = rtc;
209 pdata->last_jiffies = jiffies; 218 pdata->last_jiffies = jiffies;
210 platform_set_drvdata(pdev, pdata); 219 platform_set_drvdata(pdev, pdata);
220 ds1742_nvram_attr.size = max(ds1742_nvram_attr.size,
221 pdata->size_nvram);
211 ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr); 222 ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr);
212 if (ret) 223 if (ret)
213 goto out; 224 goto out;
@@ -215,10 +226,10 @@ static int __init ds1742_rtc_probe(struct platform_device *pdev)
215 out: 226 out:
216 if (pdata->rtc) 227 if (pdata->rtc)
217 rtc_device_unregister(pdata->rtc); 228 rtc_device_unregister(pdata->rtc);
218 if (ioaddr) 229 if (pdata->ioaddr_nvram)
219 iounmap(ioaddr); 230 iounmap(pdata->ioaddr_nvram);
220 if (pdata->baseaddr) 231 if (pdata->baseaddr)
221 release_mem_region(pdata->baseaddr, RTC_REG_SIZE); 232 release_mem_region(pdata->baseaddr, pdata->size);
222 kfree(pdata); 233 kfree(pdata);
223 return ret; 234 return ret;
224} 235}
@@ -229,8 +240,8 @@ static int __devexit ds1742_rtc_remove(struct platform_device *pdev)
229 240
230 sysfs_remove_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr); 241 sysfs_remove_bin_file(&pdev->dev.kobj, &ds1742_nvram_attr);
231 rtc_device_unregister(pdata->rtc); 242 rtc_device_unregister(pdata->rtc);
232 iounmap(pdata->ioaddr); 243 iounmap(pdata->ioaddr_nvram);
233 release_mem_region(pdata->baseaddr, RTC_REG_SIZE); 244 release_mem_region(pdata->baseaddr, pdata->size);
234 kfree(pdata); 245 kfree(pdata);
235 return 0; 246 return 0;
236} 247}
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)
117} 117}
118EXPORT_SYMBOL(rtc_tm_to_time); 118EXPORT_SYMBOL(rtc_tm_to_time);
119 119
120
121/* Merge the valid (i.e. non-negative) fields of alarm into the current
122 * time. If the valid alarm fields are earlier than the equivalent
123 * fields in the time, carry one into the least significant invalid
124 * field, so that the alarm expiry is in the future. It assumes that the
125 * least significant invalid field is more significant than the most
126 * significant valid field, and that the seconds field is valid.
127 *
128 * This is used by alarms that take relative (rather than absolute)
129 * times, and/or have a simple binary second counter instead of
130 * day/hour/minute/sec registers.
131 */
132void rtc_merge_alarm(struct rtc_time *now, struct rtc_time *alarm)
133{
134 int *alarmp = &alarm->tm_sec;
135 int *timep = &now->tm_sec;
136 int carry_into, i;
137
138 /* Ignore everything past the 6th element (tm_year). */
139 for (i = 5; i > 0; i--) {
140 if (alarmp[i] < 0)
141 alarmp[i] = timep[i];
142 else
143 break;
144 }
145
146 /* No carry needed if all fields are valid. */
147 if (i == 5)
148 return;
149
150 for (carry_into = i + 1; i >= 0; i--) {
151 if (alarmp[i] < timep[i])
152 break;
153
154 if (alarmp[i] > timep[i])
155 return;
156 }
157
158 switch (carry_into) {
159 case 1:
160 alarm->tm_min++;
161
162 if (alarm->tm_min < 60)
163 return;
164
165 alarm->tm_min = 0;
166 /* fall-through */
167
168 case 2:
169 alarm->tm_hour++;
170
171 if (alarm->tm_hour < 60)
172 return;
173
174 alarm->tm_hour = 0;
175 /* fall-through */
176
177 case 3:
178 alarm->tm_mday++;
179
180 if (alarm->tm_mday <= rtc_days_in_month[alarm->tm_mon])
181 return;
182
183 alarm->tm_mday = 1;
184 /* fall-through */
185
186 case 4:
187 alarm->tm_mon++;
188
189 if (alarm->tm_mon <= 12)
190 return;
191
192 alarm->tm_mon = 1;
193 /* fall-through */
194
195 case 5:
196 alarm->tm_year++;
197 }
198}
199EXPORT_SYMBOL(rtc_merge_alarm);
200
120MODULE_LICENSE("GPL"); 201MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-omap.c b/drivers/rtc/rtc-omap.c
new file mode 100644
index 000000000000..d59880d44fba
--- /dev/null
+++ b/drivers/rtc/rtc-omap.c
@@ -0,0 +1,571 @@
1/*
2 * TI OMAP1 Real Time Clock interface for Linux
3 *
4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6 *
7 * Copyright (C) 2006 David Brownell (new RTC framework)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/ioport.h>
19#include <linux/delay.h>
20#include <linux/rtc.h>
21#include <linux/bcd.h>
22#include <linux/platform_device.h>
23
24#include <asm/io.h>
25#include <asm/mach/time.h>
26
27
28/* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
29 * with century-range alarm matching, driven by the 32kHz clock.
30 *
31 * The main user-visible ways it differs from PC RTCs are by omitting
32 * "don't care" alarm fields and sub-second periodic IRQs, and having
33 * an autoadjust mechanism to calibrate to the true oscillator rate.
34 *
35 * Board-specific wiring options include using split power mode with
36 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
37 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
38 * low power modes). See the BOARD-SPECIFIC CUSTOMIZATION comment.
39 */
40
41#define OMAP_RTC_BASE 0xfffb4800
42
43/* RTC registers */
44#define OMAP_RTC_SECONDS_REG 0x00
45#define OMAP_RTC_MINUTES_REG 0x04
46#define OMAP_RTC_HOURS_REG 0x08
47#define OMAP_RTC_DAYS_REG 0x0C
48#define OMAP_RTC_MONTHS_REG 0x10
49#define OMAP_RTC_YEARS_REG 0x14
50#define OMAP_RTC_WEEKS_REG 0x18
51
52#define OMAP_RTC_ALARM_SECONDS_REG 0x20
53#define OMAP_RTC_ALARM_MINUTES_REG 0x24
54#define OMAP_RTC_ALARM_HOURS_REG 0x28
55#define OMAP_RTC_ALARM_DAYS_REG 0x2c
56#define OMAP_RTC_ALARM_MONTHS_REG 0x30
57#define OMAP_RTC_ALARM_YEARS_REG 0x34
58
59#define OMAP_RTC_CTRL_REG 0x40
60#define OMAP_RTC_STATUS_REG 0x44
61#define OMAP_RTC_INTERRUPTS_REG 0x48
62
63#define OMAP_RTC_COMP_LSB_REG 0x4c
64#define OMAP_RTC_COMP_MSB_REG 0x50
65#define OMAP_RTC_OSC_REG 0x54
66
67/* OMAP_RTC_CTRL_REG bit fields: */
68#define OMAP_RTC_CTRL_SPLIT (1<<7)
69#define OMAP_RTC_CTRL_DISABLE (1<<6)
70#define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5)
71#define OMAP_RTC_CTRL_TEST (1<<4)
72#define OMAP_RTC_CTRL_MODE_12_24 (1<<3)
73#define OMAP_RTC_CTRL_AUTO_COMP (1<<2)
74#define OMAP_RTC_CTRL_ROUND_30S (1<<1)
75#define OMAP_RTC_CTRL_STOP (1<<0)
76
77/* OMAP_RTC_STATUS_REG bit fields: */
78#define OMAP_RTC_STATUS_POWER_UP (1<<7)
79#define OMAP_RTC_STATUS_ALARM (1<<6)
80#define OMAP_RTC_STATUS_1D_EVENT (1<<5)
81#define OMAP_RTC_STATUS_1H_EVENT (1<<4)
82#define OMAP_RTC_STATUS_1M_EVENT (1<<3)
83#define OMAP_RTC_STATUS_1S_EVENT (1<<2)
84#define OMAP_RTC_STATUS_RUN (1<<1)
85#define OMAP_RTC_STATUS_BUSY (1<<0)
86
87/* OMAP_RTC_INTERRUPTS_REG bit fields: */
88#define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3)
89#define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2)
90
91
92#define rtc_read(addr) omap_readb(OMAP_RTC_BASE + (addr))
93#define rtc_write(val, addr) omap_writeb(val, OMAP_RTC_BASE + (addr))
94
95
96/* platform_bus isn't hotpluggable, so for static linkage it'd be safe
97 * to get rid of probe() and remove() code ... too bad the driver struct
98 * remembers probe(), that's about 25% of the runtime footprint!!
99 */
100#ifndef MODULE
101#undef __devexit
102#undef __devexit_p
103#define __devexit __exit
104#define __devexit_p __exit_p
105#endif
106
107
108/* we rely on the rtc framework to handle locking (rtc->ops_lock),
109 * so the only other requirement is that register accesses which
110 * require BUSY to be clear are made with IRQs locally disabled
111 */
112static void rtc_wait_not_busy(void)
113{
114 int count = 0;
115 u8 status;
116
117 /* BUSY may stay active for 1/32768 second (~30 usec) */
118 for (count = 0; count < 50; count++) {
119 status = rtc_read(OMAP_RTC_STATUS_REG);
120 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
121 break;
122 udelay(1);
123 }
124 /* now we have ~15 usec to read/write various registers */
125}
126
127static irqreturn_t rtc_irq(int irq, void *class_dev)
128{
129 unsigned long events = 0;
130 u8 irq_data;
131
132 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
133
134 /* alarm irq? */
135 if (irq_data & OMAP_RTC_STATUS_ALARM) {
136 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
137 events |= RTC_IRQF | RTC_AF;
138 }
139
140 /* 1/sec periodic/update irq? */
141 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
142 events |= RTC_IRQF | RTC_UF;
143
144 rtc_update_irq(class_dev, 1, events);
145
146 return IRQ_HANDLED;
147}
148
149#ifdef CONFIG_RTC_INTF_DEV
150
151static int
152omap_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
153{
154 u8 reg;
155
156 switch (cmd) {
157 case RTC_AIE_OFF:
158 case RTC_AIE_ON:
159 case RTC_UIE_OFF:
160 case RTC_UIE_ON:
161 break;
162 default:
163 return -ENOIOCTLCMD;
164 }
165
166 local_irq_disable();
167 rtc_wait_not_busy();
168 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
169 switch (cmd) {
170 /* AIE = Alarm Interrupt Enable */
171 case RTC_AIE_OFF:
172 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
173 break;
174 case RTC_AIE_ON:
175 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
176 break;
177 /* UIE = Update Interrupt Enable (1/second) */
178 case RTC_UIE_OFF:
179 reg &= ~OMAP_RTC_INTERRUPTS_IT_TIMER;
180 break;
181 case RTC_UIE_ON:
182 reg |= OMAP_RTC_INTERRUPTS_IT_TIMER;
183 break;
184 }
185 rtc_wait_not_busy();
186 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
187 local_irq_enable();
188
189 return 0;
190}
191
192#else
193#define omap_rtc_ioctl NULL
194#endif
195
196/* this hardware doesn't support "don't care" alarm fields */
197static int tm2bcd(struct rtc_time *tm)
198{
199 if (rtc_valid_tm(tm) != 0)
200 return -EINVAL;
201
202 tm->tm_sec = BIN2BCD(tm->tm_sec);
203 tm->tm_min = BIN2BCD(tm->tm_min);
204 tm->tm_hour = BIN2BCD(tm->tm_hour);
205 tm->tm_mday = BIN2BCD(tm->tm_mday);
206
207 tm->tm_mon = BIN2BCD(tm->tm_mon + 1);
208
209 /* epoch == 1900 */
210 if (tm->tm_year < 100 || tm->tm_year > 199)
211 return -EINVAL;
212 tm->tm_year = BIN2BCD(tm->tm_year - 100);
213
214 return 0;
215}
216
217static void bcd2tm(struct rtc_time *tm)
218{
219 tm->tm_sec = BCD2BIN(tm->tm_sec);
220 tm->tm_min = BCD2BIN(tm->tm_min);
221 tm->tm_hour = BCD2BIN(tm->tm_hour);
222 tm->tm_mday = BCD2BIN(tm->tm_mday);
223 tm->tm_mon = BCD2BIN(tm->tm_mon) - 1;
224 /* epoch == 1900 */
225 tm->tm_year = BCD2BIN(tm->tm_year) + 100;
226}
227
228
229static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
230{
231 /* we don't report wday/yday/isdst ... */
232 local_irq_disable();
233 rtc_wait_not_busy();
234
235 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
236 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
237 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
238 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
239 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
240 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
241
242 local_irq_enable();
243
244 bcd2tm(tm);
245 return 0;
246}
247
248static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
249{
250 if (tm2bcd(tm) < 0)
251 return -EINVAL;
252 local_irq_disable();
253 rtc_wait_not_busy();
254
255 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
256 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
257 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
258 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
259 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
260 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
261
262 local_irq_enable();
263
264 return 0;
265}
266
267static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
268{
269 local_irq_disable();
270 rtc_wait_not_busy();
271
272 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
273 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
274 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
275 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
276 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
277 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
278
279 local_irq_enable();
280
281 bcd2tm(&alm->time);
282 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
283 & OMAP_RTC_INTERRUPTS_IT_ALARM);
284
285 return 0;
286}
287
288static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
289{
290 u8 reg;
291
292 /* Much userspace code uses RTC_ALM_SET, thus "don't care" for
293 * day/month/year specifies alarms up to 24 hours in the future.
294 * So we need to handle that ... but let's ignore the "don't care"
295 * values for hours/minutes/seconds.
296 */
297 if (alm->time.tm_mday <= 0
298 && alm->time.tm_mon < 0
299 && alm->time.tm_year < 0) {
300 struct rtc_time tm;
301 unsigned long now, then;
302
303 omap_rtc_read_time(dev, &tm);
304 rtc_tm_to_time(&tm, &now);
305
306 alm->time.tm_mday = tm.tm_mday;
307 alm->time.tm_mon = tm.tm_mon;
308 alm->time.tm_year = tm.tm_year;
309 rtc_tm_to_time(&alm->time, &then);
310
311 /* sometimes the alarm wraps into tomorrow */
312 if (then < now) {
313 rtc_time_to_tm(now + 24 * 60 * 60, &tm);
314 alm->time.tm_mday = tm.tm_mday;
315 alm->time.tm_mon = tm.tm_mon;
316 alm->time.tm_year = tm.tm_year;
317 }
318 }
319
320 if (tm2bcd(&alm->time) < 0)
321 return -EINVAL;
322
323 local_irq_disable();
324 rtc_wait_not_busy();
325
326 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
327 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
328 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
329 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
330 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
331 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
332
333 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
334 if (alm->enabled)
335 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
336 else
337 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
338 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
339
340 local_irq_enable();
341
342 return 0;
343}
344
345static struct rtc_class_ops omap_rtc_ops = {
346 .ioctl = omap_rtc_ioctl,
347 .read_time = omap_rtc_read_time,
348 .set_time = omap_rtc_set_time,
349 .read_alarm = omap_rtc_read_alarm,
350 .set_alarm = omap_rtc_set_alarm,
351};
352
353static int omap_rtc_alarm;
354static int omap_rtc_timer;
355
356static int __devinit omap_rtc_probe(struct platform_device *pdev)
357{
358 struct resource *res, *mem;
359 struct rtc_device *rtc;
360 u8 reg, new_ctrl;
361
362 omap_rtc_timer = platform_get_irq(pdev, 0);
363 if (omap_rtc_timer <= 0) {
364 pr_debug("%s: no update irq?\n", pdev->name);
365 return -ENOENT;
366 }
367
368 omap_rtc_alarm = platform_get_irq(pdev, 1);
369 if (omap_rtc_alarm <= 0) {
370 pr_debug("%s: no alarm irq?\n", pdev->name);
371 return -ENOENT;
372 }
373
374 /* NOTE: using static mapping for RTC registers */
375 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
376 if (res && res->start != OMAP_RTC_BASE) {
377 pr_debug("%s: RTC registers at %08x, expected %08x\n",
378 pdev->name, (unsigned) res->start, OMAP_RTC_BASE);
379 return -ENOENT;
380 }
381
382 if (res)
383 mem = request_mem_region(res->start,
384 res->end - res->start + 1,
385 pdev->name);
386 else
387 mem = NULL;
388 if (!mem) {
389 pr_debug("%s: RTC registers at %08x are not free\n",
390 pdev->name, OMAP_RTC_BASE);
391 return -EBUSY;
392 }
393
394 rtc = rtc_device_register(pdev->name, &pdev->dev,
395 &omap_rtc_ops, THIS_MODULE);
396 if (IS_ERR(rtc)) {
397 pr_debug("%s: can't register RTC device, err %ld\n",
398 pdev->name, PTR_ERR(rtc));
399 goto fail;
400 }
401 platform_set_drvdata(pdev, rtc);
402 class_set_devdata(&rtc->class_dev, mem);
403
404 /* clear pending irqs, and set 1/second periodic,
405 * which we'll use instead of update irqs
406 */
407 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
408
409 /* clear old status */
410 reg = rtc_read(OMAP_RTC_STATUS_REG);
411 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
412 pr_info("%s: RTC power up reset detected\n",
413 pdev->name);
414 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
415 }
416 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
417 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
418
419 /* handle periodic and alarm irqs */
420 if (request_irq(omap_rtc_timer, rtc_irq, SA_INTERRUPT,
421 rtc->class_dev.class_id, &rtc->class_dev)) {
422 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
423 pdev->name, omap_rtc_timer);
424 goto fail0;
425 }
426 if (request_irq(omap_rtc_alarm, rtc_irq, SA_INTERRUPT,
427 rtc->class_dev.class_id, &rtc->class_dev)) {
428 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
429 pdev->name, omap_rtc_alarm);
430 goto fail1;
431 }
432
433 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
434 reg = rtc_read(OMAP_RTC_CTRL_REG);
435 if (reg & (u8) OMAP_RTC_CTRL_STOP)
436 pr_info("%s: already running\n", pdev->name);
437
438 /* force to 24 hour mode */
439 new_ctrl = reg & ~(OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
440 new_ctrl |= OMAP_RTC_CTRL_STOP;
441
442 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
443 *
444 * - Boards wired so that RTC_WAKE_INT does something, and muxed
445 * right (W13_1610_RTC_WAKE_INT is the default after chip reset),
446 * should initialize the device wakeup flag appropriately.
447 *
448 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
449 * rather than nPWRON_RESET, should forcibly enable split
450 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
451 * is write-only, and always reads as zero...)
452 */
453 device_init_wakeup(&pdev->dev, 0);
454
455 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
456 pr_info("%s: split power mode\n", pdev->name);
457
458 if (reg != new_ctrl)
459 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
460
461 return 0;
462
463fail1:
464 free_irq(omap_rtc_timer, NULL);
465fail0:
466 rtc_device_unregister(rtc);
467fail:
468 release_resource(mem);
469 return -EIO;
470}
471
472static int __devexit omap_rtc_remove(struct platform_device *pdev)
473{
474 struct rtc_device *rtc = platform_get_drvdata(pdev);;
475
476 device_init_wakeup(&pdev->dev, 0);
477
478 /* leave rtc running, but disable irqs */
479 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
480
481 free_irq(omap_rtc_timer, rtc);
482 free_irq(omap_rtc_alarm, rtc);
483
484 release_resource(class_get_devdata(&rtc->class_dev));
485 rtc_device_unregister(rtc);
486 return 0;
487}
488
489#ifdef CONFIG_PM
490
491static struct timespec rtc_delta;
492static u8 irqstat;
493
494static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state)
495{
496 struct rtc_time rtc_tm;
497 struct timespec time;
498
499 time.tv_nsec = 0;
500 omap_rtc_read_time(NULL, &rtc_tm);
501 rtc_tm_to_time(&rtc_tm, &time.tv_sec);
502
503 save_time_delta(&rtc_delta, &time);
504 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
505
506 /* FIXME the RTC alarm is not currently acting as a wakeup event
507 * source, and in fact this enable() call is just saving a flag
508 * that's never used...
509 */
510 if (device_may_wakeup(&pdev->dev))
511 enable_irq_wake(omap_rtc_alarm);
512 else
513 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
514
515 return 0;
516}
517
518static int omap_rtc_resume(struct platform_device *pdev)
519{
520 struct rtc_time rtc_tm;
521 struct timespec time;
522
523 time.tv_nsec = 0;
524 omap_rtc_read_time(NULL, &rtc_tm);
525 rtc_tm_to_time(&rtc_tm, &time.tv_sec);
526
527 restore_time_delta(&rtc_delta, &time);
528 if (device_may_wakeup(&pdev->dev))
529 disable_irq_wake(omap_rtc_alarm);
530 else
531 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
532 return 0;
533}
534
535#else
536#define omap_rtc_suspend NULL
537#define omap_rtc_resume NULL
538#endif
539
540static void omap_rtc_shutdown(struct platform_device *pdev)
541{
542 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
543}
544
545MODULE_ALIAS("omap_rtc");
546static struct platform_driver omap_rtc_driver = {
547 .probe = omap_rtc_probe,
548 .remove = __devexit_p(omap_rtc_remove),
549 .suspend = omap_rtc_suspend,
550 .resume = omap_rtc_resume,
551 .shutdown = omap_rtc_shutdown,
552 .driver = {
553 .name = "omap_rtc",
554 .owner = THIS_MODULE,
555 },
556};
557
558static int __init rtc_init(void)
559{
560 return platform_driver_register(&omap_rtc_driver);
561}
562module_init(rtc_init);
563
564static void __exit rtc_exit(void)
565{
566 platform_driver_unregister(&omap_rtc_driver);
567}
568module_exit(rtc_exit);
569
570MODULE_AUTHOR("George G. Davis (and others)");
571MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-pcf8563.c b/drivers/rtc/rtc-pcf8563.c
index a760cf69af90..4b72b8ef5d66 100644
--- a/drivers/rtc/rtc-pcf8563.c
+++ b/drivers/rtc/rtc-pcf8563.c
@@ -192,7 +192,7 @@ static int pcf8563_validate_client(struct i2c_client *client)
192 xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 192 xfer = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
193 193
194 if (xfer != ARRAY_SIZE(msgs)) { 194 if (xfer != ARRAY_SIZE(msgs)) {
195 dev_err(&client->adapter->dev, 195 dev_err(&client->dev,
196 "%s: could not read register 0x%02X\n", 196 "%s: could not read register 0x%02X\n",
197 __FUNCTION__, pattern[i].reg); 197 __FUNCTION__, pattern[i].reg);
198 198
@@ -203,7 +203,7 @@ static int pcf8563_validate_client(struct i2c_client *client)
203 203
204 if (value > pattern[i].max || 204 if (value > pattern[i].max ||
205 value < pattern[i].min) { 205 value < pattern[i].min) {
206 dev_dbg(&client->adapter->dev, 206 dev_dbg(&client->dev,
207 "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, " 207 "%s: pattern=%d, reg=%x, mask=0x%02x, min=%d, "
208 "max=%d, value=%d, raw=0x%02X\n", 208 "max=%d, value=%d, raw=0x%02X\n",
209 __FUNCTION__, i, pattern[i].reg, pattern[i].mask, 209 __FUNCTION__, i, pattern[i].reg, pattern[i].mask,
@@ -253,7 +253,7 @@ static int pcf8563_probe(struct i2c_adapter *adapter, int address, int kind)
253 253
254 int err = 0; 254 int err = 0;
255 255
256 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); 256 dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__);
257 257
258 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { 258 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
259 err = -ENODEV; 259 err = -ENODEV;
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)
65 seq_printf(seq, "%02d\n", alrm.time.tm_mday); 65 seq_printf(seq, "%02d\n", alrm.time.tm_mday);
66 else 66 else
67 seq_printf(seq, "**\n"); 67 seq_printf(seq, "**\n");
68 seq_printf(seq, "alrm_wakeup\t: %s\n", 68 seq_printf(seq, "alarm_IRQ\t: %s\n",
69 alrm.enabled ? "yes" : "no"); 69 alrm.enabled ? "yes" : "no");
70 seq_printf(seq, "alrm_pending\t: %s\n", 70 seq_printf(seq, "alrm_pending\t: %s\n",
71 alrm.pending ? "yes" : "no"); 71 alrm.pending ? "yes" : "no");
@@ -120,7 +120,7 @@ static int rtc_proc_add_device(struct class_device *class_dev,
120 ent->owner = rtc->owner; 120 ent->owner = rtc->owner;
121 ent->data = class_dev; 121 ent->data = class_dev;
122 122
123 dev_info(class_dev->dev, "rtc intf: proc\n"); 123 dev_dbg(class_dev->dev, "rtc intf: proc\n");
124 } 124 }
125 else 125 else
126 rtc_dev = NULL; 126 rtc_dev = NULL;
diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c
index a44fe4efa216..e7851e3739ab 100644
--- a/drivers/rtc/rtc-rs5c372.c
+++ b/drivers/rtc/rtc-rs5c372.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * An I2C driver for the Ricoh RS5C372 RTC 2 * An I2C driver for Ricoh RS5C372 and RV5C38[67] RTCs
3 * 3 *
4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net> 4 * Copyright (C) 2005 Pavel Mironchik <pmironchik@optifacio.net>
5 * Copyright (C) 2006 Tower Technologies 5 * Copyright (C) 2006 Tower Technologies
@@ -13,7 +13,7 @@
13#include <linux/rtc.h> 13#include <linux/rtc.h>
14#include <linux/bcd.h> 14#include <linux/bcd.h>
15 15
16#define DRV_VERSION "0.2" 16#define DRV_VERSION "0.4"
17 17
18/* Addresses to scan */ 18/* Addresses to scan */
19static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END }; 19static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END };
@@ -21,6 +21,13 @@ static unsigned short normal_i2c[] = { /* 0x32,*/ I2C_CLIENT_END };
21/* Insmod parameters */ 21/* Insmod parameters */
22I2C_CLIENT_INSMOD; 22I2C_CLIENT_INSMOD;
23 23
24
25/*
26 * Ricoh has a family of I2C based RTCs, which differ only slightly from
27 * each other. Differences center on pinout (e.g. how many interrupts,
28 * output clock, etc) and how the control registers are used. The '372
29 * is significant only because that's the one this driver first supported.
30 */
24#define RS5C372_REG_SECS 0 31#define RS5C372_REG_SECS 0
25#define RS5C372_REG_MINS 1 32#define RS5C372_REG_MINS 1
26#define RS5C372_REG_HOURS 2 33#define RS5C372_REG_HOURS 2
@@ -29,53 +36,142 @@ I2C_CLIENT_INSMOD;
29#define RS5C372_REG_MONTH 5 36#define RS5C372_REG_MONTH 5
30#define RS5C372_REG_YEAR 6 37#define RS5C372_REG_YEAR 6
31#define RS5C372_REG_TRIM 7 38#define RS5C372_REG_TRIM 7
39# define RS5C372_TRIM_XSL 0x80
40# define RS5C372_TRIM_MASK 0x7F
41
42#define RS5C_REG_ALARM_A_MIN 8 /* or ALARM_W */
43#define RS5C_REG_ALARM_A_HOURS 9
44#define RS5C_REG_ALARM_A_WDAY 10
45
46#define RS5C_REG_ALARM_B_MIN 11 /* or ALARM_D */
47#define RS5C_REG_ALARM_B_HOURS 12
48#define RS5C_REG_ALARM_B_WDAY 13 /* (ALARM_B only) */
49
50#define RS5C_REG_CTRL1 14
51# define RS5C_CTRL1_AALE (1 << 7) /* or WALE */
52# define RS5C_CTRL1_BALE (1 << 6) /* or DALE */
53# define RV5C387_CTRL1_24 (1 << 5)
54# define RS5C372A_CTRL1_SL1 (1 << 5)
55# define RS5C_CTRL1_CT_MASK (7 << 0)
56# define RS5C_CTRL1_CT0 (0 << 0) /* no periodic irq */
57# define RS5C_CTRL1_CT4 (4 << 0) /* 1 Hz level irq */
58#define RS5C_REG_CTRL2 15
59# define RS5C372_CTRL2_24 (1 << 5)
60# define RS5C_CTRL2_XSTP (1 << 4)
61# define RS5C_CTRL2_CTFG (1 << 2)
62# define RS5C_CTRL2_AAFG (1 << 1) /* or WAFG */
63# define RS5C_CTRL2_BAFG (1 << 0) /* or DAFG */
64
65
66/* to read (style 1) or write registers starting at R */
67#define RS5C_ADDR(R) (((R) << 4) | 0)
68
69
70enum rtc_type {
71 rtc_undef = 0,
72 rtc_rs5c372a,
73 rtc_rs5c372b,
74 rtc_rv5c386,
75 rtc_rv5c387a,
76};
32 77
33#define RS5C372_TRIM_XSL 0x80 78/* REVISIT: this assumes that:
34#define RS5C372_TRIM_MASK 0x7F 79 * - we're in the 21st century, so it's safe to ignore the century
80 * bit for rv5c38[67] (REG_MONTH bit 7);
81 * - we should use ALARM_A not ALARM_B (may be wrong on some boards)
82 */
83struct rs5c372 {
84 struct i2c_client *client;
85 struct rtc_device *rtc;
86 enum rtc_type type;
87 unsigned time24:1;
88 unsigned has_irq:1;
89 char buf[17];
90 char *regs;
91
92 /* on conversion to a "new style" i2c driver, this vanishes */
93 struct i2c_client dev;
94};
35 95
36#define RS5C372_REG_BASE 0 96static int rs5c_get_regs(struct rs5c372 *rs5c)
97{
98 struct i2c_client *client = rs5c->client;
99 struct i2c_msg msgs[] = {
100 { client->addr, I2C_M_RD, sizeof rs5c->buf, rs5c->buf },
101 };
37 102
38static int rs5c372_attach(struct i2c_adapter *adapter); 103 /* This implements the third reading method from the datasheet, using
39static int rs5c372_detach(struct i2c_client *client); 104 * an internal address that's reset after each transaction (by STOP)
40static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind); 105 * to 0x0f ... so we read extra registers, and skip the first one.
106 *
107 * The first method doesn't work with the iop3xx adapter driver, on at
108 * least 80219 chips; this works around that bug.
109 */
110 if ((i2c_transfer(client->adapter, msgs, 1)) != 1) {
111 pr_debug("%s: can't read registers\n", rs5c->rtc->name);
112 return -EIO;
113 }
41 114
42static struct i2c_driver rs5c372_driver = { 115 dev_dbg(&client->dev,
43 .driver = { 116 "%02x %02x %02x (%02x) %02x %02x %02x (%02x), "
44 .name = "rs5c372", 117 "%02x %02x %02x, %02x %02x %02x; %02x %02x\n",
45 }, 118 rs5c->regs[0], rs5c->regs[1], rs5c->regs[2], rs5c->regs[3],
46 .attach_adapter = &rs5c372_attach, 119 rs5c->regs[4], rs5c->regs[5], rs5c->regs[6], rs5c->regs[7],
47 .detach_client = &rs5c372_detach, 120 rs5c->regs[8], rs5c->regs[9], rs5c->regs[10], rs5c->regs[11],
48}; 121 rs5c->regs[12], rs5c->regs[13], rs5c->regs[14], rs5c->regs[15]);
122
123 return 0;
124}
125
126static unsigned rs5c_reg2hr(struct rs5c372 *rs5c, unsigned reg)
127{
128 unsigned hour;
129
130 if (rs5c->time24)
131 return BCD2BIN(reg & 0x3f);
132
133 hour = BCD2BIN(reg & 0x1f);
134 if (hour == 12)
135 hour = 0;
136 if (reg & 0x20)
137 hour += 12;
138 return hour;
139}
140
141static unsigned rs5c_hr2reg(struct rs5c372 *rs5c, unsigned hour)
142{
143 if (rs5c->time24)
144 return BIN2BCD(hour);
145
146 if (hour > 12)
147 return 0x20 | BIN2BCD(hour - 12);
148 if (hour == 12)
149 return 0x20 | BIN2BCD(12);
150 if (hour == 0)
151 return BIN2BCD(12);
152 return BIN2BCD(hour);
153}
49 154
50static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm) 155static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
51{ 156{
52 unsigned char buf[7] = { RS5C372_REG_BASE }; 157 struct rs5c372 *rs5c = i2c_get_clientdata(client);
158 int status = rs5c_get_regs(rs5c);
53 159
54 /* this implements the 1st reading method, according 160 if (status < 0)
55 * to the datasheet. buf[0] is initialized with 161 return status;
56 * address ptr and transmission format register.
57 */
58 struct i2c_msg msgs[] = {
59 { client->addr, 0, 1, buf },
60 { client->addr, I2C_M_RD, 7, buf },
61 };
62 162
63 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { 163 tm->tm_sec = BCD2BIN(rs5c->regs[RS5C372_REG_SECS] & 0x7f);
64 dev_err(&client->dev, "%s: read error\n", __FUNCTION__); 164 tm->tm_min = BCD2BIN(rs5c->regs[RS5C372_REG_MINS] & 0x7f);
65 return -EIO; 165 tm->tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C372_REG_HOURS]);
66 }
67 166
68 tm->tm_sec = BCD2BIN(buf[RS5C372_REG_SECS] & 0x7f); 167 tm->tm_wday = BCD2BIN(rs5c->regs[RS5C372_REG_WDAY] & 0x07);
69 tm->tm_min = BCD2BIN(buf[RS5C372_REG_MINS] & 0x7f); 168 tm->tm_mday = BCD2BIN(rs5c->regs[RS5C372_REG_DAY] & 0x3f);
70 tm->tm_hour = BCD2BIN(buf[RS5C372_REG_HOURS] & 0x3f);
71 tm->tm_wday = BCD2BIN(buf[RS5C372_REG_WDAY] & 0x07);
72 tm->tm_mday = BCD2BIN(buf[RS5C372_REG_DAY] & 0x3f);
73 169
74 /* tm->tm_mon is zero-based */ 170 /* tm->tm_mon is zero-based */
75 tm->tm_mon = BCD2BIN(buf[RS5C372_REG_MONTH] & 0x1f) - 1; 171 tm->tm_mon = BCD2BIN(rs5c->regs[RS5C372_REG_MONTH] & 0x1f) - 1;
76 172
77 /* year is 1900 + tm->tm_year */ 173 /* year is 1900 + tm->tm_year */
78 tm->tm_year = BCD2BIN(buf[RS5C372_REG_YEAR]) + 100; 174 tm->tm_year = BCD2BIN(rs5c->regs[RS5C372_REG_YEAR]) + 100;
79 175
80 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 176 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
81 "mday=%d, mon=%d, year=%d, wday=%d\n", 177 "mday=%d, mon=%d, year=%d, wday=%d\n",
@@ -83,22 +179,25 @@ static int rs5c372_get_datetime(struct i2c_client *client, struct rtc_time *tm)
83 tm->tm_sec, tm->tm_min, tm->tm_hour, 179 tm->tm_sec, tm->tm_min, tm->tm_hour,
84 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 180 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
85 181
86 return 0; 182 /* rtc might need initialization */
183 return rtc_valid_tm(tm);
87} 184}
88 185
89static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm) 186static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
90{ 187{
91 unsigned char buf[8] = { RS5C372_REG_BASE }; 188 struct rs5c372 *rs5c = i2c_get_clientdata(client);
189 unsigned char buf[8];
92 190
93 dev_dbg(&client->dev, 191 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d "
94 "%s: secs=%d, mins=%d, hours=%d "
95 "mday=%d, mon=%d, year=%d, wday=%d\n", 192 "mday=%d, mon=%d, year=%d, wday=%d\n",
96 __FUNCTION__, tm->tm_sec, tm->tm_min, tm->tm_hour, 193 __FUNCTION__,
194 tm->tm_sec, tm->tm_min, tm->tm_hour,
97 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 195 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
98 196
197 buf[0] = RS5C_ADDR(RS5C372_REG_SECS);
99 buf[1] = BIN2BCD(tm->tm_sec); 198 buf[1] = BIN2BCD(tm->tm_sec);
100 buf[2] = BIN2BCD(tm->tm_min); 199 buf[2] = BIN2BCD(tm->tm_min);
101 buf[3] = BIN2BCD(tm->tm_hour); 200 buf[3] = rs5c_hr2reg(rs5c, tm->tm_hour);
102 buf[4] = BIN2BCD(tm->tm_wday); 201 buf[4] = BIN2BCD(tm->tm_wday);
103 buf[5] = BIN2BCD(tm->tm_mday); 202 buf[5] = BIN2BCD(tm->tm_mday);
104 buf[6] = BIN2BCD(tm->tm_mon + 1); 203 buf[6] = BIN2BCD(tm->tm_mon + 1);
@@ -112,30 +211,43 @@ static int rs5c372_set_datetime(struct i2c_client *client, struct rtc_time *tm)
112 return 0; 211 return 0;
113} 212}
114 213
115static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim) 214#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
116{ 215#define NEED_TRIM
117 unsigned char buf = RS5C372_REG_TRIM; 216#endif
118 217
119 struct i2c_msg msgs[] = { 218#if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
120 { client->addr, 0, 1, &buf }, 219#define NEED_TRIM
121 { client->addr, I2C_M_RD, 1, &buf }, 220#endif
122 };
123 221
124 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) { 222#ifdef NEED_TRIM
125 dev_err(&client->dev, "%s: read error\n", __FUNCTION__); 223static int rs5c372_get_trim(struct i2c_client *client, int *osc, int *trim)
126 return -EIO; 224{
127 } 225 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
226 u8 tmp = rs5c372->regs[RS5C372_REG_TRIM];
128 227
129 if (osc) 228 if (osc)
130 *osc = (buf & RS5C372_TRIM_XSL) ? 32000 : 32768; 229 *osc = (tmp & RS5C372_TRIM_XSL) ? 32000 : 32768;
131 230
132 if (trim) { 231 if (trim) {
133 *trim = buf & RS5C372_TRIM_MASK; 232 dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, tmp);
134 dev_dbg(&client->dev, "%s: raw trim=%x\n", __FUNCTION__, *trim); 233 tmp &= RS5C372_TRIM_MASK;
234 if (tmp & 0x3e) {
235 int t = tmp & 0x3f;
236
237 if (tmp & 0x40)
238 t = (~t | (s8)0xc0) + 1;
239 else
240 t = t - 1;
241
242 tmp = t * 2;
243 } else
244 tmp = 0;
245 *trim = tmp;
135 } 246 }
136 247
137 return 0; 248 return 0;
138} 249}
250#endif
139 251
140static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm) 252static int rs5c372_rtc_read_time(struct device *dev, struct rtc_time *tm)
141{ 253{
@@ -147,25 +259,190 @@ static int rs5c372_rtc_set_time(struct device *dev, struct rtc_time *tm)
147 return rs5c372_set_datetime(to_i2c_client(dev), tm); 259 return rs5c372_set_datetime(to_i2c_client(dev), tm);
148} 260}
149 261
262#if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
263
264static int
265rs5c_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
266{
267 struct i2c_client *client = to_i2c_client(dev);
268 struct rs5c372 *rs5c = i2c_get_clientdata(client);
269 unsigned char buf[2];
270 int status;
271
272 buf[1] = rs5c->regs[RS5C_REG_CTRL1];
273 switch (cmd) {
274 case RTC_UIE_OFF:
275 case RTC_UIE_ON:
276 /* some 327a modes use a different IRQ pin for 1Hz irqs */
277 if (rs5c->type == rtc_rs5c372a
278 && (buf[1] & RS5C372A_CTRL1_SL1))
279 return -ENOIOCTLCMD;
280 case RTC_AIE_OFF:
281 case RTC_AIE_ON:
282 /* these irq management calls only make sense for chips
283 * which are wired up to an IRQ.
284 */
285 if (!rs5c->has_irq)
286 return -ENOIOCTLCMD;
287 break;
288 default:
289 return -ENOIOCTLCMD;
290 }
291
292 status = rs5c_get_regs(rs5c);
293 if (status < 0)
294 return status;
295
296 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1);
297 switch (cmd) {
298 case RTC_AIE_OFF: /* alarm off */
299 buf[1] &= ~RS5C_CTRL1_AALE;
300 break;
301 case RTC_AIE_ON: /* alarm on */
302 buf[1] |= RS5C_CTRL1_AALE;
303 break;
304 case RTC_UIE_OFF: /* update off */
305 buf[1] &= ~RS5C_CTRL1_CT_MASK;
306 break;
307 case RTC_UIE_ON: /* update on */
308 buf[1] &= ~RS5C_CTRL1_CT_MASK;
309 buf[1] |= RS5C_CTRL1_CT4;
310 break;
311 }
312 if ((i2c_master_send(client, buf, 2)) != 2) {
313 printk(KERN_WARNING "%s: can't update alarm\n",
314 rs5c->rtc->name);
315 status = -EIO;
316 } else
317 rs5c->regs[RS5C_REG_CTRL1] = buf[1];
318 return status;
319}
320
321#else
322#define rs5c_rtc_ioctl NULL
323#endif
324
325
326/* NOTE: Since RTC_WKALM_{RD,SET} were originally defined for EFI,
327 * which only exposes a polled programming interface; and since
328 * these calls map directly to those EFI requests; we don't demand
329 * we have an IRQ for this chip when we go through this API.
330 *
331 * The older x86_pc derived RTC_ALM_{READ,SET} calls require irqs
332 * though, managed through RTC_AIE_{ON,OFF} requests.
333 */
334
335static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t)
336{
337 struct i2c_client *client = to_i2c_client(dev);
338 struct rs5c372 *rs5c = i2c_get_clientdata(client);
339 int status;
340
341 status = rs5c_get_regs(rs5c);
342 if (status < 0)
343 return status;
344
345 /* report alarm time */
346 t->time.tm_sec = 0;
347 t->time.tm_min = BCD2BIN(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f);
348 t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]);
349 t->time.tm_mday = -1;
350 t->time.tm_mon = -1;
351 t->time.tm_year = -1;
352 t->time.tm_wday = -1;
353 t->time.tm_yday = -1;
354 t->time.tm_isdst = -1;
355
356 /* ... and status */
357 t->enabled = !!(rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE);
358 t->pending = !!(rs5c->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_AAFG);
359
360 return 0;
361}
362
363static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
364{
365 struct i2c_client *client = to_i2c_client(dev);
366 struct rs5c372 *rs5c = i2c_get_clientdata(client);
367 int status;
368 unsigned char buf[4];
369
370 /* only handle up to 24 hours in the future, like RTC_ALM_SET */
371 if (t->time.tm_mday != -1
372 || t->time.tm_mon != -1
373 || t->time.tm_year != -1)
374 return -EINVAL;
375
376 /* REVISIT: round up tm_sec */
377
378 /* if needed, disable irq (clears pending status) */
379 status = rs5c_get_regs(rs5c);
380 if (status < 0)
381 return status;
382 if (rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE) {
383 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1);
384 buf[1] = rs5c->regs[RS5C_REG_CTRL1] & ~RS5C_CTRL1_AALE;
385 if (i2c_master_send(client, buf, 2) != 2) {
386 pr_debug("%s: can't disable alarm\n", rs5c->rtc->name);
387 return -EIO;
388 }
389 rs5c->regs[RS5C_REG_CTRL1] = buf[1];
390 }
391
392 /* set alarm */
393 buf[0] = RS5C_ADDR(RS5C_REG_ALARM_A_MIN);
394 buf[1] = BIN2BCD(t->time.tm_min);
395 buf[2] = rs5c_hr2reg(rs5c, t->time.tm_hour);
396 buf[3] = 0x7f; /* any/all days */
397 if ((i2c_master_send(client, buf, 4)) != 4) {
398 pr_debug("%s: can't set alarm time\n", rs5c->rtc->name);
399 return -EIO;
400 }
401
402 /* ... and maybe enable its irq */
403 if (t->enabled) {
404 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1);
405 buf[1] = rs5c->regs[RS5C_REG_CTRL1] | RS5C_CTRL1_AALE;
406 if ((i2c_master_send(client, buf, 2)) != 2)
407 printk(KERN_WARNING "%s: can't enable alarm\n",
408 rs5c->rtc->name);
409 rs5c->regs[RS5C_REG_CTRL1] = buf[1];
410 }
411
412 return 0;
413}
414
415#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
416
150static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq) 417static int rs5c372_rtc_proc(struct device *dev, struct seq_file *seq)
151{ 418{
152 int err, osc, trim; 419 int err, osc, trim;
153 420
154 err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim); 421 err = rs5c372_get_trim(to_i2c_client(dev), &osc, &trim);
155 if (err == 0) { 422 if (err == 0) {
156 seq_printf(seq, "%d.%03d KHz\n", osc / 1000, osc % 1000); 423 seq_printf(seq, "crystal\t\t: %d.%03d KHz\n",
157 seq_printf(seq, "trim\t: %d\n", trim); 424 osc / 1000, osc % 1000);
425 seq_printf(seq, "trim\t\t: %d\n", trim);
158 } 426 }
159 427
160 return 0; 428 return 0;
161} 429}
162 430
431#else
432#define rs5c372_rtc_proc NULL
433#endif
434
163static const struct rtc_class_ops rs5c372_rtc_ops = { 435static const struct rtc_class_ops rs5c372_rtc_ops = {
164 .proc = rs5c372_rtc_proc, 436 .proc = rs5c372_rtc_proc,
437 .ioctl = rs5c_rtc_ioctl,
165 .read_time = rs5c372_rtc_read_time, 438 .read_time = rs5c372_rtc_read_time,
166 .set_time = rs5c372_rtc_set_time, 439 .set_time = rs5c372_rtc_set_time,
440 .read_alarm = rs5c_read_alarm,
441 .set_alarm = rs5c_set_alarm,
167}; 442};
168 443
444#if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
445
169static ssize_t rs5c372_sysfs_show_trim(struct device *dev, 446static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
170 struct device_attribute *attr, char *buf) 447 struct device_attribute *attr, char *buf)
171{ 448{
@@ -175,7 +452,7 @@ static ssize_t rs5c372_sysfs_show_trim(struct device *dev,
175 if (err) 452 if (err)
176 return err; 453 return err;
177 454
178 return sprintf(buf, "0x%2x\n", trim); 455 return sprintf(buf, "%d\n", trim);
179} 456}
180static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL); 457static DEVICE_ATTR(trim, S_IRUGO, rs5c372_sysfs_show_trim, NULL);
181 458
@@ -192,29 +469,57 @@ static ssize_t rs5c372_sysfs_show_osc(struct device *dev,
192} 469}
193static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL); 470static DEVICE_ATTR(osc, S_IRUGO, rs5c372_sysfs_show_osc, NULL);
194 471
195static int rs5c372_attach(struct i2c_adapter *adapter) 472static int rs5c_sysfs_register(struct device *dev)
196{ 473{
197 return i2c_probe(adapter, &addr_data, rs5c372_probe); 474 int err;
475
476 err = device_create_file(dev, &dev_attr_trim);
477 if (err)
478 return err;
479 err = device_create_file(dev, &dev_attr_osc);
480 if (err)
481 device_remove_file(dev, &dev_attr_trim);
482
483 return err;
198} 484}
199 485
486#else
487static int rs5c_sysfs_register(struct device *dev)
488{
489 return 0;
490}
491#endif /* SYSFS */
492
493static struct i2c_driver rs5c372_driver;
494
200static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind) 495static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind)
201{ 496{
202 int err = 0; 497 int err = 0;
203 struct i2c_client *client; 498 struct i2c_client *client;
204 struct rtc_device *rtc; 499 struct rs5c372 *rs5c372;
500 struct rtc_time tm;
205 501
206 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); 502 dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__);
207 503
208 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { 504 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
209 err = -ENODEV; 505 err = -ENODEV;
210 goto exit; 506 goto exit;
211 } 507 }
212 508
213 if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) { 509 if (!(rs5c372 = kzalloc(sizeof(struct rs5c372), GFP_KERNEL))) {
214 err = -ENOMEM; 510 err = -ENOMEM;
215 goto exit; 511 goto exit;
216 } 512 }
217 513
514 /* we read registers 0x0f then 0x00-0x0f; skip the first one */
515 rs5c372->regs=&rs5c372->buf[1];
516
517 /* On conversion to a "new style" i2c driver, we'll be handed
518 * the i2c_client (we won't create it)
519 */
520 client = &rs5c372->dev;
521 rs5c372->client = client;
522
218 /* I2C client */ 523 /* I2C client */
219 client->addr = address; 524 client->addr = address;
220 client->driver = &rs5c372_driver; 525 client->driver = &rs5c372_driver;
@@ -222,53 +527,159 @@ static int rs5c372_probe(struct i2c_adapter *adapter, int address, int kind)
222 527
223 strlcpy(client->name, rs5c372_driver.driver.name, I2C_NAME_SIZE); 528 strlcpy(client->name, rs5c372_driver.driver.name, I2C_NAME_SIZE);
224 529
530 i2c_set_clientdata(client, rs5c372);
531
225 /* Inform the i2c layer */ 532 /* Inform the i2c layer */
226 if ((err = i2c_attach_client(client))) 533 if ((err = i2c_attach_client(client)))
227 goto exit_kfree; 534 goto exit_kfree;
228 535
229 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); 536 err = rs5c_get_regs(rs5c372);
537 if (err < 0)
538 goto exit_detach;
230 539
231 rtc = rtc_device_register(rs5c372_driver.driver.name, &client->dev, 540 /* For "new style" drivers, irq is in i2c_client and chip type
232 &rs5c372_rtc_ops, THIS_MODULE); 541 * info comes from i2c_client.dev.platform_data. Meanwhile:
542 *
543 * STICK BOARD-SPECIFIC SETUP CODE RIGHT HERE
544 */
545 if (rs5c372->type == rtc_undef) {
546 rs5c372->type = rtc_rs5c372b;
547 dev_warn(&client->dev, "assuming rs5c372b\n");
548 }
233 549
234 if (IS_ERR(rtc)) { 550 /* clock may be set for am/pm or 24 hr time */
235 err = PTR_ERR(rtc); 551 switch (rs5c372->type) {
552 case rtc_rs5c372a:
553 case rtc_rs5c372b:
554 /* alarm uses ALARM_A; and nINTRA on 372a, nINTR on 372b.
555 * so does periodic irq, except some 327a modes.
556 */
557 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C372_CTRL2_24)
558 rs5c372->time24 = 1;
559 break;
560 case rtc_rv5c386:
561 case rtc_rv5c387a:
562 if (rs5c372->regs[RS5C_REG_CTRL1] & RV5C387_CTRL1_24)
563 rs5c372->time24 = 1;
564 /* alarm uses ALARM_W; and nINTRB for alarm and periodic
565 * irq, on both 386 and 387
566 */
567 break;
568 default:
569 dev_err(&client->dev, "unknown RTC type\n");
236 goto exit_detach; 570 goto exit_detach;
237 } 571 }
238 572
239 i2c_set_clientdata(client, rtc); 573 /* if the oscillator lost power and no other software (like
574 * the bootloader) set it up, do it here.
575 */
576 if (rs5c372->regs[RS5C_REG_CTRL2] & RS5C_CTRL2_XSTP) {
577 unsigned char buf[3];
578
579 rs5c372->regs[RS5C_REG_CTRL2] &= ~RS5C_CTRL2_XSTP;
580
581 buf[0] = RS5C_ADDR(RS5C_REG_CTRL1);
582 buf[1] = rs5c372->regs[RS5C_REG_CTRL1];
583 buf[2] = rs5c372->regs[RS5C_REG_CTRL2];
584
585 /* use 24hr mode */
586 switch (rs5c372->type) {
587 case rtc_rs5c372a:
588 case rtc_rs5c372b:
589 buf[2] |= RS5C372_CTRL2_24;
590 rs5c372->time24 = 1;
591 break;
592 case rtc_rv5c386:
593 case rtc_rv5c387a:
594 buf[1] |= RV5C387_CTRL1_24;
595 rs5c372->time24 = 1;
596 break;
597 default:
598 /* impossible */
599 break;
600 }
601
602 if ((i2c_master_send(client, buf, 3)) != 3) {
603 dev_err(&client->dev, "setup error\n");
604 goto exit_detach;
605 }
606 rs5c372->regs[RS5C_REG_CTRL1] = buf[1];
607 rs5c372->regs[RS5C_REG_CTRL2] = buf[2];
608 }
609
610 if (rs5c372_get_datetime(client, &tm) < 0)
611 dev_warn(&client->dev, "clock needs to be set\n");
240 612
241 device_create_file(&client->dev, &dev_attr_trim); 613 dev_info(&client->dev, "%s found, %s, driver version " DRV_VERSION "\n",
242 device_create_file(&client->dev, &dev_attr_osc); 614 ({ char *s; switch (rs5c372->type) {
615 case rtc_rs5c372a: s = "rs5c372a"; break;
616 case rtc_rs5c372b: s = "rs5c372b"; break;
617 case rtc_rv5c386: s = "rv5c386"; break;
618 case rtc_rv5c387a: s = "rv5c387a"; break;
619 default: s = "chip"; break;
620 }; s;}),
621 rs5c372->time24 ? "24hr" : "am/pm"
622 );
623
624 /* FIXME when client->irq exists, use it to register alarm irq */
625
626 rs5c372->rtc = rtc_device_register(rs5c372_driver.driver.name,
627 &client->dev, &rs5c372_rtc_ops, THIS_MODULE);
628
629 if (IS_ERR(rs5c372->rtc)) {
630 err = PTR_ERR(rs5c372->rtc);
631 goto exit_detach;
632 }
633
634 err = rs5c_sysfs_register(&client->dev);
635 if (err)
636 goto exit_devreg;
243 637
244 return 0; 638 return 0;
245 639
640exit_devreg:
641 rtc_device_unregister(rs5c372->rtc);
642
246exit_detach: 643exit_detach:
247 i2c_detach_client(client); 644 i2c_detach_client(client);
248 645
249exit_kfree: 646exit_kfree:
250 kfree(client); 647 kfree(rs5c372);
251 648
252exit: 649exit:
253 return err; 650 return err;
254} 651}
255 652
653static int rs5c372_attach(struct i2c_adapter *adapter)
654{
655 return i2c_probe(adapter, &addr_data, rs5c372_probe);
656}
657
256static int rs5c372_detach(struct i2c_client *client) 658static int rs5c372_detach(struct i2c_client *client)
257{ 659{
258 int err; 660 int err;
259 struct rtc_device *rtc = i2c_get_clientdata(client); 661 struct rs5c372 *rs5c372 = i2c_get_clientdata(client);
260 662
261 if (rtc) 663 if (rs5c372->rtc)
262 rtc_device_unregister(rtc); 664 rtc_device_unregister(rs5c372->rtc);
665
666 /* REVISIT properly destroy the sysfs files ... */
263 667
264 if ((err = i2c_detach_client(client))) 668 if ((err = i2c_detach_client(client)))
265 return err; 669 return err;
266 670
267 kfree(client); 671 kfree(rs5c372);
268
269 return 0; 672 return 0;
270} 673}
271 674
675static struct i2c_driver rs5c372_driver = {
676 .driver = {
677 .name = "rtc-rs5c372",
678 },
679 .attach_adapter = &rs5c372_attach,
680 .detach_client = &rs5c372_detach,
681};
682
272static __init int rs5c372_init(void) 683static __init int rs5c372_init(void)
273{ 684{
274 return i2c_add_driver(&rs5c372_driver); 685 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)
191 191
192 alm_en = readb(base + S3C2410_RTCALM); 192 alm_en = readb(base + S3C2410_RTCALM);
193 193
194 alrm->enabled = (alm_en & S3C2410_RTCALM_ALMEN) ? 1 : 0;
195
194 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n", 196 pr_debug("read alarm %02x %02x.%02x.%02x %02x/%02x/%02x\n",
195 alm_en, 197 alm_en,
196 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday, 198 alm_tm->tm_year, alm_tm->tm_mon, alm_tm->tm_mday,
@@ -331,12 +333,8 @@ static int s3c_rtc_ioctl(struct device *dev,
331 333
332static int s3c_rtc_proc(struct device *dev, struct seq_file *seq) 334static int s3c_rtc_proc(struct device *dev, struct seq_file *seq)
333{ 335{
334 unsigned int rtcalm = readb(s3c_rtc_base + S3C2410_RTCALM);
335 unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT); 336 unsigned int ticnt = readb(s3c_rtc_base + S3C2410_TICNT);
336 337
337 seq_printf(seq, "alarm_IRQ\t: %s\n",
338 (rtcalm & S3C2410_RTCALM_ALMEN) ? "yes" : "no" );
339
340 seq_printf(seq, "periodic_IRQ\t: %s\n", 338 seq_printf(seq, "periodic_IRQ\t: %s\n",
341 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" ); 339 (ticnt & S3C2410_TICNT_ENABLE) ? "yes" : "no" );
342 340
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)
289 289
290static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq) 290static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
291{ 291{
292 seq_printf(seq, "trim/divider\t: 0x%08lx\n", RTTR); 292 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
293 seq_printf(seq, "alarm_IRQ\t: %s\n",
294 (RTSR & RTSR_ALE) ? "yes" : "no" );
295 seq_printf(seq, "update_IRQ\t: %s\n", 293 seq_printf(seq, "update_IRQ\t: %s\n",
296 (RTSR & RTSR_HZE) ? "yes" : "no"); 294 (RTSR & RTSR_HZE) ? "yes" : "no");
297 seq_printf(seq, "periodic_IRQ\t: %s\n", 295 seq_printf(seq, "periodic_IRQ\t: %s\n",
diff --git a/drivers/rtc/rtc-sh.c b/drivers/rtc/rtc-sh.c
index 143302a8e79c..e9e0934380b8 100644
--- a/drivers/rtc/rtc-sh.c
+++ b/drivers/rtc/rtc-sh.c
@@ -2,6 +2,7 @@
2 * SuperH On-Chip RTC Support 2 * SuperH On-Chip RTC Support
3 * 3 *
4 * Copyright (C) 2006 Paul Mundt 4 * Copyright (C) 2006 Paul Mundt
5 * Copyright (C) 2006 Jamie Lenehan
5 * 6 *
6 * Based on the old arch/sh/kernel/cpu/rtc.c by: 7 * Based on the old arch/sh/kernel/cpu/rtc.c by:
7 * 8 *
@@ -21,7 +22,10 @@
21#include <linux/seq_file.h> 22#include <linux/seq_file.h>
22#include <linux/interrupt.h> 23#include <linux/interrupt.h>
23#include <linux/spinlock.h> 24#include <linux/spinlock.h>
24#include <asm/io.h> 25#include <linux/io.h>
26
27#define DRV_NAME "sh-rtc"
28#define DRV_VERSION "0.1.2"
25 29
26#ifdef CONFIG_CPU_SH3 30#ifdef CONFIG_CPU_SH3
27#define rtc_reg_size sizeof(u16) 31#define rtc_reg_size sizeof(u16)
@@ -33,22 +37,26 @@
33 37
34#define RTC_REG(r) ((r) * rtc_reg_size) 38#define RTC_REG(r) ((r) * rtc_reg_size)
35 39
36#define R64CNT RTC_REG(0) 40#define R64CNT RTC_REG(0)
37#define RSECCNT RTC_REG(1) 41
38#define RMINCNT RTC_REG(2) 42#define RSECCNT RTC_REG(1) /* RTC sec */
39#define RHRCNT RTC_REG(3) 43#define RMINCNT RTC_REG(2) /* RTC min */
40#define RWKCNT RTC_REG(4) 44#define RHRCNT RTC_REG(3) /* RTC hour */
41#define RDAYCNT RTC_REG(5) 45#define RWKCNT RTC_REG(4) /* RTC week */
42#define RMONCNT RTC_REG(6) 46#define RDAYCNT RTC_REG(5) /* RTC day */
43#define RYRCNT RTC_REG(7) 47#define RMONCNT RTC_REG(6) /* RTC month */
44#define RSECAR RTC_REG(8) 48#define RYRCNT RTC_REG(7) /* RTC year */
45#define RMINAR RTC_REG(9) 49#define RSECAR RTC_REG(8) /* ALARM sec */
46#define RHRAR RTC_REG(10) 50#define RMINAR RTC_REG(9) /* ALARM min */
47#define RWKAR RTC_REG(11) 51#define RHRAR RTC_REG(10) /* ALARM hour */
48#define RDAYAR RTC_REG(12) 52#define RWKAR RTC_REG(11) /* ALARM week */
49#define RMONAR RTC_REG(13) 53#define RDAYAR RTC_REG(12) /* ALARM day */
50#define RCR1 RTC_REG(14) 54#define RMONAR RTC_REG(13) /* ALARM month */
51#define RCR2 RTC_REG(15) 55#define RCR1 RTC_REG(14) /* Control */
56#define RCR2 RTC_REG(15) /* Control */
57
58/* ALARM Bits - or with BCD encoded value */
59#define AR_ENB 0x80 /* Enable for alarm cmp */
52 60
53/* RCR1 Bits */ 61/* RCR1 Bits */
54#define RCR1_CF 0x80 /* Carry Flag */ 62#define RCR1_CF 0x80 /* Carry Flag */
@@ -71,22 +79,28 @@ struct sh_rtc {
71 unsigned int alarm_irq, periodic_irq, carry_irq; 79 unsigned int alarm_irq, periodic_irq, carry_irq;
72 struct rtc_device *rtc_dev; 80 struct rtc_device *rtc_dev;
73 spinlock_t lock; 81 spinlock_t lock;
82 int rearm_aie;
74}; 83};
75 84
76static irqreturn_t sh_rtc_interrupt(int irq, void *id) 85static irqreturn_t sh_rtc_interrupt(int irq, void *dev_id)
77{ 86{
78 struct platform_device *pdev = id; 87 struct platform_device *pdev = to_platform_device(dev_id);
79 struct sh_rtc *rtc = platform_get_drvdata(pdev); 88 struct sh_rtc *rtc = platform_get_drvdata(pdev);
80 unsigned int tmp, events = 0; 89 unsigned int tmp, events = 0;
81 90
82 spin_lock(&rtc->lock); 91 spin_lock(&rtc->lock);
83 92
84 tmp = readb(rtc->regbase + RCR1); 93 tmp = readb(rtc->regbase + RCR1);
94 tmp &= ~RCR1_CF;
85 95
86 if (tmp & RCR1_AF) 96 if (rtc->rearm_aie) {
87 events |= RTC_AF | RTC_IRQF; 97 if (tmp & RCR1_AF)
88 98 tmp &= ~RCR1_AF; /* try to clear AF again */
89 tmp &= ~(RCR1_CF | RCR1_AF); 99 else {
100 tmp |= RCR1_AIE; /* AF has cleared, rearm IRQ */
101 rtc->rearm_aie = 0;
102 }
103 }
90 104
91 writeb(tmp, rtc->regbase + RCR1); 105 writeb(tmp, rtc->regbase + RCR1);
92 106
@@ -97,9 +111,45 @@ static irqreturn_t sh_rtc_interrupt(int irq, void *id)
97 return IRQ_HANDLED; 111 return IRQ_HANDLED;
98} 112}
99 113
100static irqreturn_t sh_rtc_periodic(int irq, void *id) 114static irqreturn_t sh_rtc_alarm(int irq, void *dev_id)
101{ 115{
102 struct sh_rtc *rtc = dev_get_drvdata(id); 116 struct platform_device *pdev = to_platform_device(dev_id);
117 struct sh_rtc *rtc = platform_get_drvdata(pdev);
118 unsigned int tmp, events = 0;
119
120 spin_lock(&rtc->lock);
121
122 tmp = readb(rtc->regbase + RCR1);
123
124 /*
125 * If AF is set then the alarm has triggered. If we clear AF while
126 * the alarm time still matches the RTC time then AF will
127 * immediately be set again, and if AIE is enabled then the alarm
128 * interrupt will immediately be retrigger. So we clear AIE here
129 * and use rtc->rearm_aie so that the carry interrupt will keep
130 * trying to clear AF and once it stays cleared it'll re-enable
131 * AIE.
132 */
133 if (tmp & RCR1_AF) {
134 events |= RTC_AF | RTC_IRQF;
135
136 tmp &= ~(RCR1_AF|RCR1_AIE);
137
138 writeb(tmp, rtc->regbase + RCR1);
139
140 rtc->rearm_aie = 1;
141
142 rtc_update_irq(&rtc->rtc_dev->class_dev, 1, events);
143 }
144
145 spin_unlock(&rtc->lock);
146 return IRQ_HANDLED;
147}
148
149static irqreturn_t sh_rtc_periodic(int irq, void *dev_id)
150{
151 struct platform_device *pdev = to_platform_device(dev_id);
152 struct sh_rtc *rtc = platform_get_drvdata(pdev);
103 153
104 spin_lock(&rtc->lock); 154 spin_lock(&rtc->lock);
105 155
@@ -139,10 +189,11 @@ static inline void sh_rtc_setaie(struct device *dev, unsigned int enable)
139 189
140 tmp = readb(rtc->regbase + RCR1); 190 tmp = readb(rtc->regbase + RCR1);
141 191
142 if (enable) 192 if (!enable) {
143 tmp |= RCR1_AIE;
144 else
145 tmp &= ~RCR1_AIE; 193 tmp &= ~RCR1_AIE;
194 rtc->rearm_aie = 0;
195 } else if (rtc->rearm_aie == 0)
196 tmp |= RCR1_AIE;
146 197
147 writeb(tmp, rtc->regbase + RCR1); 198 writeb(tmp, rtc->regbase + RCR1);
148 199
@@ -177,7 +228,7 @@ static int sh_rtc_open(struct device *dev)
177 goto err_bad_carry; 228 goto err_bad_carry;
178 } 229 }
179 230
180 ret = request_irq(rtc->alarm_irq, sh_rtc_interrupt, IRQF_DISABLED, 231 ret = request_irq(rtc->alarm_irq, sh_rtc_alarm, IRQF_DISABLED,
181 "sh-rtc alarm", dev); 232 "sh-rtc alarm", dev);
182 if (unlikely(ret)) { 233 if (unlikely(ret)) {
183 dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n", 234 dev_err(dev, "request alarm IRQ failed with %d, IRQ %d\n",
@@ -200,6 +251,7 @@ static void sh_rtc_release(struct device *dev)
200 struct sh_rtc *rtc = dev_get_drvdata(dev); 251 struct sh_rtc *rtc = dev_get_drvdata(dev);
201 252
202 sh_rtc_setpie(dev, 0); 253 sh_rtc_setpie(dev, 0);
254 sh_rtc_setaie(dev, 0);
203 255
204 free_irq(rtc->periodic_irq, dev); 256 free_irq(rtc->periodic_irq, dev);
205 free_irq(rtc->carry_irq, dev); 257 free_irq(rtc->carry_irq, dev);
@@ -212,8 +264,6 @@ static int sh_rtc_proc(struct device *dev, struct seq_file *seq)
212 unsigned int tmp; 264 unsigned int tmp;
213 265
214 tmp = readb(rtc->regbase + RCR1); 266 tmp = readb(rtc->regbase + RCR1);
215 seq_printf(seq, "alarm_IRQ\t: %s\n",
216 (tmp & RCR1_AIE) ? "yes" : "no");
217 seq_printf(seq, "carry_IRQ\t: %s\n", 267 seq_printf(seq, "carry_IRQ\t: %s\n",
218 (tmp & RCR1_CIE) ? "yes" : "no"); 268 (tmp & RCR1_CIE) ? "yes" : "no");
219 269
@@ -267,7 +317,7 @@ static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
267 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT)); 317 tm->tm_hour = BCD2BIN(readb(rtc->regbase + RHRCNT));
268 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT)); 318 tm->tm_wday = BCD2BIN(readb(rtc->regbase + RWKCNT));
269 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT)); 319 tm->tm_mday = BCD2BIN(readb(rtc->regbase + RDAYCNT));
270 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)); 320 tm->tm_mon = BCD2BIN(readb(rtc->regbase + RMONCNT)) - 1;
271 321
272#if defined(CONFIG_CPU_SH4) 322#if defined(CONFIG_CPU_SH4)
273 yr = readw(rtc->regbase + RYRCNT); 323 yr = readw(rtc->regbase + RYRCNT);
@@ -295,7 +345,7 @@ static int sh_rtc_read_time(struct device *dev, struct rtc_time *tm)
295 "mday=%d, mon=%d, year=%d, wday=%d\n", 345 "mday=%d, mon=%d, year=%d, wday=%d\n",
296 __FUNCTION__, 346 __FUNCTION__,
297 tm->tm_sec, tm->tm_min, tm->tm_hour, 347 tm->tm_sec, tm->tm_min, tm->tm_hour,
298 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 348 tm->tm_mday, tm->tm_mon + 1, tm->tm_year, tm->tm_wday);
299 349
300 if (rtc_valid_tm(tm) < 0) 350 if (rtc_valid_tm(tm) < 0)
301 dev_err(dev, "invalid date\n"); 351 dev_err(dev, "invalid date\n");
@@ -322,7 +372,7 @@ static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
322 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT); 372 writeb(BIN2BCD(tm->tm_hour), rtc->regbase + RHRCNT);
323 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT); 373 writeb(BIN2BCD(tm->tm_wday), rtc->regbase + RWKCNT);
324 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT); 374 writeb(BIN2BCD(tm->tm_mday), rtc->regbase + RDAYCNT);
325 writeb(BIN2BCD(tm->tm_mon), rtc->regbase + RMONCNT); 375 writeb(BIN2BCD(tm->tm_mon + 1), rtc->regbase + RMONCNT);
326 376
327#ifdef CONFIG_CPU_SH3 377#ifdef CONFIG_CPU_SH3
328 year = tm->tm_year % 100; 378 year = tm->tm_year % 100;
@@ -344,12 +394,138 @@ static int sh_rtc_set_time(struct device *dev, struct rtc_time *tm)
344 return 0; 394 return 0;
345} 395}
346 396
397static inline int sh_rtc_read_alarm_value(struct sh_rtc *rtc, int reg_off)
398{
399 unsigned int byte;
400 int value = 0xff; /* return 0xff for ignored values */
401
402 byte = readb(rtc->regbase + reg_off);
403 if (byte & AR_ENB) {
404 byte &= ~AR_ENB; /* strip the enable bit */
405 value = BCD2BIN(byte);
406 }
407
408 return value;
409}
410
411static int sh_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
412{
413 struct platform_device *pdev = to_platform_device(dev);
414 struct sh_rtc *rtc = platform_get_drvdata(pdev);
415 struct rtc_time* tm = &wkalrm->time;
416
417 spin_lock_irq(&rtc->lock);
418
419 tm->tm_sec = sh_rtc_read_alarm_value(rtc, RSECAR);
420 tm->tm_min = sh_rtc_read_alarm_value(rtc, RMINAR);
421 tm->tm_hour = sh_rtc_read_alarm_value(rtc, RHRAR);
422 tm->tm_wday = sh_rtc_read_alarm_value(rtc, RWKAR);
423 tm->tm_mday = sh_rtc_read_alarm_value(rtc, RDAYAR);
424 tm->tm_mon = sh_rtc_read_alarm_value(rtc, RMONAR);
425 if (tm->tm_mon > 0)
426 tm->tm_mon -= 1; /* RTC is 1-12, tm_mon is 0-11 */
427 tm->tm_year = 0xffff;
428
429 wkalrm->enabled = (readb(rtc->regbase + RCR1) & RCR1_AIE) ? 1 : 0;
430
431 spin_unlock_irq(&rtc->lock);
432
433 return 0;
434}
435
436static inline void sh_rtc_write_alarm_value(struct sh_rtc *rtc,
437 int value, int reg_off)
438{
439 /* < 0 for a value that is ignored */
440 if (value < 0)
441 writeb(0, rtc->regbase + reg_off);
442 else
443 writeb(BIN2BCD(value) | AR_ENB, rtc->regbase + reg_off);
444}
445
446static int sh_rtc_check_alarm(struct rtc_time* tm)
447{
448 /*
449 * The original rtc says anything > 0xc0 is "don't care" or "match
450 * all" - most users use 0xff but rtc-dev uses -1 for the same thing.
451 * The original rtc doesn't support years - some things use -1 and
452 * some 0xffff. We use -1 to make out tests easier.
453 */
454 if (tm->tm_year == 0xffff)
455 tm->tm_year = -1;
456 if (tm->tm_mon >= 0xff)
457 tm->tm_mon = -1;
458 if (tm->tm_mday >= 0xff)
459 tm->tm_mday = -1;
460 if (tm->tm_wday >= 0xff)
461 tm->tm_wday = -1;
462 if (tm->tm_hour >= 0xff)
463 tm->tm_hour = -1;
464 if (tm->tm_min >= 0xff)
465 tm->tm_min = -1;
466 if (tm->tm_sec >= 0xff)
467 tm->tm_sec = -1;
468
469 if (tm->tm_year > 9999 ||
470 tm->tm_mon >= 12 ||
471 tm->tm_mday == 0 || tm->tm_mday >= 32 ||
472 tm->tm_wday >= 7 ||
473 tm->tm_hour >= 24 ||
474 tm->tm_min >= 60 ||
475 tm->tm_sec >= 60)
476 return -EINVAL;
477
478 return 0;
479}
480
481static int sh_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
482{
483 struct platform_device *pdev = to_platform_device(dev);
484 struct sh_rtc *rtc = platform_get_drvdata(pdev);
485 unsigned int rcr1;
486 struct rtc_time *tm = &wkalrm->time;
487 int mon, err;
488
489 err = sh_rtc_check_alarm(tm);
490 if (unlikely(err < 0))
491 return err;
492
493 spin_lock_irq(&rtc->lock);
494
495 /* disable alarm interrupt and clear flag */
496 rcr1 = readb(rtc->regbase + RCR1);
497 rcr1 &= ~RCR1_AF;
498 writeb(rcr1 & ~RCR1_AIE, rtc->regbase + RCR1);
499
500 rtc->rearm_aie = 0;
501
502 /* set alarm time */
503 sh_rtc_write_alarm_value(rtc, tm->tm_sec, RSECAR);
504 sh_rtc_write_alarm_value(rtc, tm->tm_min, RMINAR);
505 sh_rtc_write_alarm_value(rtc, tm->tm_hour, RHRAR);
506 sh_rtc_write_alarm_value(rtc, tm->tm_wday, RWKAR);
507 sh_rtc_write_alarm_value(rtc, tm->tm_mday, RDAYAR);
508 mon = tm->tm_mon;
509 if (mon >= 0)
510 mon += 1;
511 sh_rtc_write_alarm_value(rtc, mon, RMONAR);
512
513 /* Restore interrupt activation status */
514 writeb(rcr1, rtc->regbase + RCR1);
515
516 spin_unlock_irq(&rtc->lock);
517
518 return 0;
519}
520
347static struct rtc_class_ops sh_rtc_ops = { 521static struct rtc_class_ops sh_rtc_ops = {
348 .open = sh_rtc_open, 522 .open = sh_rtc_open,
349 .release = sh_rtc_release, 523 .release = sh_rtc_release,
350 .ioctl = sh_rtc_ioctl, 524 .ioctl = sh_rtc_ioctl,
351 .read_time = sh_rtc_read_time, 525 .read_time = sh_rtc_read_time,
352 .set_time = sh_rtc_set_time, 526 .set_time = sh_rtc_set_time,
527 .read_alarm = sh_rtc_read_alarm,
528 .set_alarm = sh_rtc_set_alarm,
353 .proc = sh_rtc_proc, 529 .proc = sh_rtc_proc,
354}; 530};
355 531
@@ -442,7 +618,7 @@ static int __devexit sh_rtc_remove(struct platform_device *pdev)
442} 618}
443static struct platform_driver sh_rtc_platform_driver = { 619static struct platform_driver sh_rtc_platform_driver = {
444 .driver = { 620 .driver = {
445 .name = "sh-rtc", 621 .name = DRV_NAME,
446 .owner = THIS_MODULE, 622 .owner = THIS_MODULE,
447 }, 623 },
448 .probe = sh_rtc_probe, 624 .probe = sh_rtc_probe,
@@ -463,5 +639,6 @@ module_init(sh_rtc_init);
463module_exit(sh_rtc_exit); 639module_exit(sh_rtc_exit);
464 640
465MODULE_DESCRIPTION("SuperH on-chip RTC driver"); 641MODULE_DESCRIPTION("SuperH on-chip RTC driver");
466MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); 642MODULE_VERSION(DRV_VERSION);
643MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>, Jamie Lenehan <lenehan@twibble.org>");
467MODULE_LICENSE("GPL"); 644MODULE_LICENSE("GPL");
diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c
index 625637b84d33..9418a59fb368 100644
--- a/drivers/rtc/rtc-sysfs.c
+++ b/drivers/rtc/rtc-sysfs.c
@@ -83,7 +83,7 @@ static int __devinit rtc_sysfs_add_device(struct class_device *class_dev,
83{ 83{
84 int err; 84 int err;
85 85
86 dev_info(class_dev->dev, "rtc intf: sysfs\n"); 86 dev_dbg(class_dev->dev, "rtc intf: sysfs\n");
87 87
88 err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group); 88 err = sysfs_create_group(&class_dev->kobj, &rtc_attr_group);
89 if (err) 89 if (err)
diff --git a/drivers/rtc/rtc-test.c b/drivers/rtc/rtc-test.c
index 6ef9c62d5032..f50a1b8e1607 100644
--- a/drivers/rtc/rtc-test.c
+++ b/drivers/rtc/rtc-test.c
@@ -123,11 +123,18 @@ static int test_probe(struct platform_device *plat_dev)
123 err = PTR_ERR(rtc); 123 err = PTR_ERR(rtc);
124 return err; 124 return err;
125 } 125 }
126 device_create_file(&plat_dev->dev, &dev_attr_irq); 126
127 err = device_create_file(&plat_dev->dev, &dev_attr_irq);
128 if (err)
129 goto err;
127 130
128 platform_set_drvdata(plat_dev, rtc); 131 platform_set_drvdata(plat_dev, rtc);
129 132
130 return 0; 133 return 0;
134
135err:
136 rtc_device_unregister(rtc);
137 return err;
131} 138}
132 139
133static int __devexit test_remove(struct platform_device *plat_dev) 140static int __devexit test_remove(struct platform_device *plat_dev)
diff --git a/drivers/rtc/rtc-x1205.c b/drivers/rtc/rtc-x1205.c
index 522c69753bbf..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)
372 }; 372 };
373 373
374 if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) { 374 if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) {
375 dev_err(&client->adapter->dev, 375 dev_err(&client->dev,
376 "%s: could not read register %x\n", 376 "%s: could not read register %x\n",
377 __FUNCTION__, probe_zero_pattern[i]); 377 __FUNCTION__, probe_zero_pattern[i]);
378 378
@@ -380,7 +380,7 @@ static int x1205_validate_client(struct i2c_client *client)
380 } 380 }
381 381
382 if ((buf & probe_zero_pattern[i+1]) != 0) { 382 if ((buf & probe_zero_pattern[i+1]) != 0) {
383 dev_err(&client->adapter->dev, 383 dev_err(&client->dev,
384 "%s: register=%02x, zero pattern=%d, value=%x\n", 384 "%s: register=%02x, zero pattern=%d, value=%x\n",
385 __FUNCTION__, probe_zero_pattern[i], i, buf); 385 __FUNCTION__, probe_zero_pattern[i], i, buf);
386 386
@@ -400,7 +400,7 @@ static int x1205_validate_client(struct i2c_client *client)
400 }; 400 };
401 401
402 if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) { 402 if ((xfer = i2c_transfer(client->adapter, msgs, 2)) != 2) {
403 dev_err(&client->adapter->dev, 403 dev_err(&client->dev,
404 "%s: could not read register %x\n", 404 "%s: could not read register %x\n",
405 __FUNCTION__, probe_limits_pattern[i].reg); 405 __FUNCTION__, probe_limits_pattern[i].reg);
406 406
@@ -411,7 +411,7 @@ static int x1205_validate_client(struct i2c_client *client)
411 411
412 if (value > probe_limits_pattern[i].max || 412 if (value > probe_limits_pattern[i].max ||
413 value < probe_limits_pattern[i].min) { 413 value < probe_limits_pattern[i].min) {
414 dev_dbg(&client->adapter->dev, 414 dev_dbg(&client->dev,
415 "%s: register=%x, lim pattern=%d, value=%d\n", 415 "%s: register=%x, lim pattern=%d, value=%d\n",
416 __FUNCTION__, probe_limits_pattern[i].reg, 416 __FUNCTION__, probe_limits_pattern[i].reg,
417 i, value); 417 i, value);
@@ -506,7 +506,7 @@ static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
506 struct i2c_client *client; 506 struct i2c_client *client;
507 struct rtc_device *rtc; 507 struct rtc_device *rtc;
508 508
509 dev_dbg(&adapter->dev, "%s\n", __FUNCTION__); 509 dev_dbg(adapter->class_dev.dev, "%s\n", __FUNCTION__);
510 510
511 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { 511 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
512 err = -ENODEV; 512 err = -ENODEV;
@@ -562,11 +562,19 @@ static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
562 else 562 else
563 dev_err(&client->dev, "couldn't read status\n"); 563 dev_err(&client->dev, "couldn't read status\n");
564 564
565 device_create_file(&client->dev, &dev_attr_atrim); 565 err = device_create_file(&client->dev, &dev_attr_atrim);
566 device_create_file(&client->dev, &dev_attr_dtrim); 566 if (err) goto exit_devreg;
567 err = device_create_file(&client->dev, &dev_attr_dtrim);
568 if (err) goto exit_atrim;
567 569
568 return 0; 570 return 0;
569 571
572exit_atrim:
573 device_remove_file(&client->dev, &dev_attr_atrim);
574
575exit_devreg:
576 rtc_device_unregister(rtc);
577
570exit_detach: 578exit_detach:
571 i2c_detach_client(client); 579 i2c_detach_client(client);
572 580