[SPARC64]: Update defconfig.
[linux-2.6/mini2440.git] / drivers / rtc / rtc-ds1553.c
blobe27176c0e18fb0cd2ec6f11457d0f7c3ce4664ff
1 /*
2 * An rtc driver for the Dallas DS1553
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
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
8 * published by the Free Software Foundation.
9 */
11 #include <linux/bcd.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/interrupt.h>
17 #include <linux/rtc.h>
18 #include <linux/platform_device.h>
19 #include <linux/io.h>
21 #define DRV_VERSION "0.2"
23 #define RTC_REG_SIZE 0x2000
24 #define RTC_OFFSET 0x1ff0
26 #define RTC_FLAGS (RTC_OFFSET + 0)
27 #define RTC_SECONDS_ALARM (RTC_OFFSET + 2)
28 #define RTC_MINUTES_ALARM (RTC_OFFSET + 3)
29 #define RTC_HOURS_ALARM (RTC_OFFSET + 4)
30 #define RTC_DATE_ALARM (RTC_OFFSET + 5)
31 #define RTC_INTERRUPTS (RTC_OFFSET + 6)
32 #define RTC_WATCHDOG (RTC_OFFSET + 7)
33 #define RTC_CONTROL (RTC_OFFSET + 8)
34 #define RTC_CENTURY (RTC_OFFSET + 8)
35 #define RTC_SECONDS (RTC_OFFSET + 9)
36 #define RTC_MINUTES (RTC_OFFSET + 10)
37 #define RTC_HOURS (RTC_OFFSET + 11)
38 #define RTC_DAY (RTC_OFFSET + 12)
39 #define RTC_DATE (RTC_OFFSET + 13)
40 #define RTC_MONTH (RTC_OFFSET + 14)
41 #define RTC_YEAR (RTC_OFFSET + 15)
43 #define RTC_CENTURY_MASK 0x3f
44 #define RTC_SECONDS_MASK 0x7f
45 #define RTC_DAY_MASK 0x07
47 /* Bits in the Control/Century register */
48 #define RTC_WRITE 0x80
49 #define RTC_READ 0x40
51 /* Bits in the Seconds register */
52 #define RTC_STOP 0x80
54 /* Bits in the Flags register */
55 #define RTC_FLAGS_AF 0x40
56 #define RTC_FLAGS_BLF 0x10
58 /* Bits in the Interrupts register */
59 #define RTC_INTS_AE 0x80
61 struct rtc_plat_data {
62 struct rtc_device *rtc;
63 void __iomem *ioaddr;
64 unsigned long baseaddr;
65 unsigned long last_jiffies;
66 int irq;
67 unsigned int irqen;
68 int alrm_sec;
69 int alrm_min;
70 int alrm_hour;
71 int alrm_mday;
74 static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
76 struct platform_device *pdev = to_platform_device(dev);
77 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
78 void __iomem *ioaddr = pdata->ioaddr;
79 u8 century;
81 century = BIN2BCD((tm->tm_year + 1900) / 100);
83 writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL);
85 writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR);
86 writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH);
87 writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
88 writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE);
89 writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS);
90 writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES);
91 writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
93 /* RTC_CENTURY and RTC_CONTROL share same register */
94 writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
95 writeb(century & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
96 return 0;
99 static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm)
101 struct platform_device *pdev = to_platform_device(dev);
102 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
103 void __iomem *ioaddr = pdata->ioaddr;
104 unsigned int year, month, day, hour, minute, second, week;
105 unsigned int century;
107 /* give enough time to update RTC in case of continuous read */
108 if (pdata->last_jiffies == jiffies)
109 msleep(1);
110 pdata->last_jiffies = jiffies;
111 writeb(RTC_READ, ioaddr + RTC_CONTROL);
112 second = readb(ioaddr + RTC_SECONDS) & RTC_SECONDS_MASK;
113 minute = readb(ioaddr + RTC_MINUTES);
114 hour = readb(ioaddr + RTC_HOURS);
115 day = readb(ioaddr + RTC_DATE);
116 week = readb(ioaddr + RTC_DAY) & RTC_DAY_MASK;
117 month = readb(ioaddr + RTC_MONTH);
118 year = readb(ioaddr + RTC_YEAR);
119 century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
120 writeb(0, ioaddr + RTC_CONTROL);
121 tm->tm_sec = BCD2BIN(second);
122 tm->tm_min = BCD2BIN(minute);
123 tm->tm_hour = BCD2BIN(hour);
124 tm->tm_mday = BCD2BIN(day);
125 tm->tm_wday = BCD2BIN(week);
126 tm->tm_mon = BCD2BIN(month) - 1;
127 /* year is 1900 + tm->tm_year */
128 tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900;
130 if (rtc_valid_tm(tm) < 0) {
131 dev_err(dev, "retrieved date/time is not valid.\n");
132 rtc_time_to_tm(0, tm);
134 return 0;
137 static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
139 void __iomem *ioaddr = pdata->ioaddr;
140 unsigned long flags;
142 spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
143 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
144 0x80 : BIN2BCD(pdata->alrm_mday),
145 ioaddr + RTC_DATE_ALARM);
146 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
147 0x80 : BIN2BCD(pdata->alrm_hour),
148 ioaddr + RTC_HOURS_ALARM);
149 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
150 0x80 : BIN2BCD(pdata->alrm_min),
151 ioaddr + RTC_MINUTES_ALARM);
152 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
153 0x80 : BIN2BCD(pdata->alrm_sec),
154 ioaddr + RTC_SECONDS_ALARM);
155 writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS);
156 readb(ioaddr + RTC_FLAGS); /* clear interrupts */
157 spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags);
160 static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
162 struct platform_device *pdev = to_platform_device(dev);
163 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
165 if (pdata->irq < 0)
166 return -EINVAL;
167 pdata->alrm_mday = alrm->time.tm_mday;
168 pdata->alrm_hour = alrm->time.tm_hour;
169 pdata->alrm_min = alrm->time.tm_min;
170 pdata->alrm_sec = alrm->time.tm_sec;
171 if (alrm->enabled)
172 pdata->irqen |= RTC_AF;
173 ds1553_rtc_update_alarm(pdata);
174 return 0;
177 static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
179 struct platform_device *pdev = to_platform_device(dev);
180 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
182 if (pdata->irq < 0)
183 return -EINVAL;
184 alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
185 alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
186 alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min;
187 alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec;
188 alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0;
189 return 0;
192 static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
194 struct platform_device *pdev = dev_id;
195 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
196 void __iomem *ioaddr = pdata->ioaddr;
197 unsigned long events = RTC_IRQF;
199 /* read and clear interrupt */
200 if (!(readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF))
201 return IRQ_NONE;
202 if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
203 events |= RTC_UF;
204 else
205 events |= RTC_AF;
206 rtc_update_irq(&pdata->rtc->class_dev, 1, events);
207 return IRQ_HANDLED;
210 static void ds1553_rtc_release(struct device *dev)
212 struct platform_device *pdev = to_platform_device(dev);
213 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
215 if (pdata->irq >= 0) {
216 pdata->irqen = 0;
217 ds1553_rtc_update_alarm(pdata);
221 static int ds1553_rtc_ioctl(struct device *dev, unsigned int cmd,
222 unsigned long arg)
224 struct platform_device *pdev = to_platform_device(dev);
225 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
227 if (pdata->irq < 0)
228 return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */
229 switch (cmd) {
230 case RTC_AIE_OFF:
231 pdata->irqen &= ~RTC_AF;
232 ds1553_rtc_update_alarm(pdata);
233 break;
234 case RTC_AIE_ON:
235 pdata->irqen |= RTC_AF;
236 ds1553_rtc_update_alarm(pdata);
237 break;
238 case RTC_UIE_OFF:
239 pdata->irqen &= ~RTC_UF;
240 ds1553_rtc_update_alarm(pdata);
241 break;
242 case RTC_UIE_ON:
243 pdata->irqen |= RTC_UF;
244 ds1553_rtc_update_alarm(pdata);
245 break;
246 default:
247 return -ENOIOCTLCMD;
249 return 0;
252 static const struct rtc_class_ops ds1553_rtc_ops = {
253 .read_time = ds1553_rtc_read_time,
254 .set_time = ds1553_rtc_set_time,
255 .read_alarm = ds1553_rtc_read_alarm,
256 .set_alarm = ds1553_rtc_set_alarm,
257 .release = ds1553_rtc_release,
258 .ioctl = ds1553_rtc_ioctl,
261 static ssize_t ds1553_nvram_read(struct kobject *kobj, char *buf,
262 loff_t pos, size_t size)
264 struct platform_device *pdev =
265 to_platform_device(container_of(kobj, struct device, kobj));
266 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
267 void __iomem *ioaddr = pdata->ioaddr;
268 ssize_t count;
270 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
271 *buf++ = readb(ioaddr + pos++);
272 return count;
275 static ssize_t ds1553_nvram_write(struct kobject *kobj, char *buf,
276 loff_t pos, size_t size)
278 struct platform_device *pdev =
279 to_platform_device(container_of(kobj, struct device, kobj));
280 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
281 void __iomem *ioaddr = pdata->ioaddr;
282 ssize_t count;
284 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
285 writeb(*buf++, ioaddr + pos++);
286 return count;
289 static struct bin_attribute ds1553_nvram_attr = {
290 .attr = {
291 .name = "nvram",
292 .mode = S_IRUGO | S_IWUGO,
293 .owner = THIS_MODULE,
295 .size = RTC_OFFSET,
296 .read = ds1553_nvram_read,
297 .write = ds1553_nvram_write,
300 static int __devinit ds1553_rtc_probe(struct platform_device *pdev)
302 struct rtc_device *rtc;
303 struct resource *res;
304 unsigned int cen, sec;
305 struct rtc_plat_data *pdata = NULL;
306 void __iomem *ioaddr = NULL;
307 int ret = 0;
309 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
310 if (!res)
311 return -ENODEV;
312 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
313 if (!pdata)
314 return -ENOMEM;
315 pdata->irq = -1;
316 if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) {
317 ret = -EBUSY;
318 goto out;
320 pdata->baseaddr = res->start;
321 ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE);
322 if (!ioaddr) {
323 ret = -ENOMEM;
324 goto out;
326 pdata->ioaddr = ioaddr;
327 pdata->irq = platform_get_irq(pdev, 0);
329 /* turn RTC on if it was not on */
330 sec = readb(ioaddr + RTC_SECONDS);
331 if (sec & RTC_STOP) {
332 sec &= RTC_SECONDS_MASK;
333 cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
334 writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
335 writeb(sec, ioaddr + RTC_SECONDS);
336 writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
338 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
339 dev_warn(&pdev->dev, "voltage-low detected.\n");
341 if (pdata->irq >= 0) {
342 writeb(0, ioaddr + RTC_INTERRUPTS);
343 if (request_irq(pdata->irq, ds1553_rtc_interrupt,
344 IRQF_DISABLED | IRQF_SHARED,
345 pdev->name, pdev) < 0) {
346 dev_warn(&pdev->dev, "interrupt not available.\n");
347 pdata->irq = -1;
351 rtc = rtc_device_register(pdev->name, &pdev->dev,
352 &ds1553_rtc_ops, THIS_MODULE);
353 if (IS_ERR(rtc)) {
354 ret = PTR_ERR(rtc);
355 goto out;
357 pdata->rtc = rtc;
358 pdata->last_jiffies = jiffies;
359 platform_set_drvdata(pdev, pdata);
360 ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
361 if (ret)
362 goto out;
363 return 0;
364 out:
365 if (pdata->rtc)
366 rtc_device_unregister(pdata->rtc);
367 if (pdata->irq >= 0)
368 free_irq(pdata->irq, pdev);
369 if (ioaddr)
370 iounmap(ioaddr);
371 if (pdata->baseaddr)
372 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
373 kfree(pdata);
374 return ret;
377 static int __devexit ds1553_rtc_remove(struct platform_device *pdev)
379 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
381 sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
382 rtc_device_unregister(pdata->rtc);
383 if (pdata->irq >= 0) {
384 writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
385 free_irq(pdata->irq, pdev);
387 iounmap(pdata->ioaddr);
388 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
389 kfree(pdata);
390 return 0;
393 static struct platform_driver ds1553_rtc_driver = {
394 .probe = ds1553_rtc_probe,
395 .remove = __devexit_p(ds1553_rtc_remove),
396 .driver = {
397 .name = "ds1553",
398 .owner = THIS_MODULE,
402 static __init int ds1553_init(void)
404 return platform_driver_register(&ds1553_rtc_driver);
407 static __exit void ds1553_exit(void)
409 return platform_driver_unregister(&ds1553_rtc_driver);
412 module_init(ds1553_init);
413 module_exit(ds1553_exit);
415 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
416 MODULE_DESCRIPTION("Dallas DS1553 RTC driver");
417 MODULE_LICENSE("GPL");
418 MODULE_VERSION(DRV_VERSION);