blob: 38d472b6340656d48d53f2d92d300c12d580e2e5 [file] [log] [blame]
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -07001/*
2 * An rtc driver for the Dallas DS1553
3 *
4 * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
5 *
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 */
10
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>
20
Atsushi Nemoto391b1fe2006-09-30 23:28:18 -070021#define DRV_VERSION "0.2"
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070022
23#define RTC_REG_SIZE 0x2000
24#define RTC_OFFSET 0x1ff0
25
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)
42
43#define RTC_CENTURY_MASK 0x3f
44#define RTC_SECONDS_MASK 0x7f
45#define RTC_DAY_MASK 0x07
46
47/* Bits in the Control/Century register */
48#define RTC_WRITE 0x80
49#define RTC_READ 0x40
50
51/* Bits in the Seconds register */
52#define RTC_STOP 0x80
53
54/* Bits in the Flags register */
55#define RTC_FLAGS_AF 0x40
56#define RTC_FLAGS_BLF 0x10
57
58/* Bits in the Interrupts register */
59#define RTC_INTS_AE 0x80
60
61struct rtc_plat_data {
62 struct rtc_device *rtc;
63 void __iomem *ioaddr;
Atsushi Nemoto49cc8862007-09-18 22:46:30 -070064 resource_size_t baseaddr;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070065 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;
72};
73
74static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
75{
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;
80
Adrian Bunkfe20ba72008-10-18 20:28:41 -070081 century = bin2bcd((tm->tm_year + 1900) / 100);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070082
83 writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL);
84
Adrian Bunkfe20ba72008-10-18 20:28:41 -070085 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);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -070092
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;
97}
98
99static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm)
100{
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;
106
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);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700121 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;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700127 /* year is 1900 + tm->tm_year */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700128 tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700129
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);
133 }
134 return 0;
135}
136
137static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
138{
139 void __iomem *ioaddr = pdata->ioaddr;
140 unsigned long flags;
141
142 spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
143 writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700144 0x80 : bin2bcd(pdata->alrm_mday),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700145 ioaddr + RTC_DATE_ALARM);
146 writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700147 0x80 : bin2bcd(pdata->alrm_hour),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700148 ioaddr + RTC_HOURS_ALARM);
149 writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700150 0x80 : bin2bcd(pdata->alrm_min),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700151 ioaddr + RTC_MINUTES_ALARM);
152 writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700153 0x80 : bin2bcd(pdata->alrm_sec),
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700154 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);
158}
159
160static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
161{
162 struct platform_device *pdev = to_platform_device(dev);
163 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
164
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800165 if (pdata->irq <= 0)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700166 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;
175}
176
177static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
178{
179 struct platform_device *pdev = to_platform_device(dev);
180 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
181
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800182 if (pdata->irq <= 0)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700183 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;
190}
191
David Howells7d12e782006-10-05 14:55:46 +0100192static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700193{
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;
198
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;
David Brownellab6a2d72007-05-08 00:33:30 -0700206 rtc_update_irq(pdata->rtc, 1, events);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700207 return IRQ_HANDLED;
208}
209
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700210static int ds1553_rtc_ioctl(struct device *dev, unsigned int cmd,
211 unsigned long arg)
212{
213 struct platform_device *pdev = to_platform_device(dev);
214 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
215
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800216 if (pdata->irq <= 0)
Atsushi Nemoto0ca06662006-06-27 02:53:58 -0700217 return -ENOIOCTLCMD; /* fall back into rtc-dev's emulation */
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700218 switch (cmd) {
219 case RTC_AIE_OFF:
220 pdata->irqen &= ~RTC_AF;
221 ds1553_rtc_update_alarm(pdata);
222 break;
223 case RTC_AIE_ON:
224 pdata->irqen |= RTC_AF;
225 ds1553_rtc_update_alarm(pdata);
226 break;
227 case RTC_UIE_OFF:
228 pdata->irqen &= ~RTC_UF;
229 ds1553_rtc_update_alarm(pdata);
230 break;
231 case RTC_UIE_ON:
232 pdata->irqen |= RTC_UF;
233 ds1553_rtc_update_alarm(pdata);
234 break;
235 default:
236 return -ENOIOCTLCMD;
237 }
238 return 0;
239}
240
David Brownellff8371a2006-09-30 23:28:17 -0700241static const struct rtc_class_ops ds1553_rtc_ops = {
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700242 .read_time = ds1553_rtc_read_time,
243 .set_time = ds1553_rtc_set_time,
244 .read_alarm = ds1553_rtc_read_alarm,
245 .set_alarm = ds1553_rtc_set_alarm,
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700246 .ioctl = ds1553_rtc_ioctl,
247};
248
Zhang Rui91a69022007-06-09 13:57:22 +0800249static ssize_t ds1553_nvram_read(struct kobject *kobj,
250 struct bin_attribute *bin_attr,
251 char *buf, loff_t pos, size_t size)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700252{
253 struct platform_device *pdev =
254 to_platform_device(container_of(kobj, struct device, kobj));
255 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
256 void __iomem *ioaddr = pdata->ioaddr;
257 ssize_t count;
258
259 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
260 *buf++ = readb(ioaddr + pos++);
261 return count;
262}
263
Zhang Rui91a69022007-06-09 13:57:22 +0800264static ssize_t ds1553_nvram_write(struct kobject *kobj,
265 struct bin_attribute *bin_attr,
266 char *buf, loff_t pos, size_t size)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700267{
268 struct platform_device *pdev =
269 to_platform_device(container_of(kobj, struct device, kobj));
270 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
271 void __iomem *ioaddr = pdata->ioaddr;
272 ssize_t count;
273
274 for (count = 0; size > 0 && pos < RTC_OFFSET; count++, size--)
275 writeb(*buf++, ioaddr + pos++);
276 return count;
277}
278
279static struct bin_attribute ds1553_nvram_attr = {
280 .attr = {
281 .name = "nvram",
David Brownella4b1d502007-11-14 16:58:30 -0800282 .mode = S_IRUGO | S_IWUSR,
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700283 },
284 .size = RTC_OFFSET,
285 .read = ds1553_nvram_read,
286 .write = ds1553_nvram_write,
287};
288
Prarit Bhargavac2391222007-02-12 00:52:29 -0800289static int __devinit ds1553_rtc_probe(struct platform_device *pdev)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700290{
291 struct rtc_device *rtc;
292 struct resource *res;
293 unsigned int cen, sec;
294 struct rtc_plat_data *pdata = NULL;
295 void __iomem *ioaddr = NULL;
296 int ret = 0;
297
298 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
299 if (!res)
300 return -ENODEV;
301 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
302 if (!pdata)
303 return -ENOMEM;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700304 if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) {
305 ret = -EBUSY;
306 goto out;
307 }
308 pdata->baseaddr = res->start;
309 ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE);
310 if (!ioaddr) {
311 ret = -ENOMEM;
312 goto out;
313 }
314 pdata->ioaddr = ioaddr;
315 pdata->irq = platform_get_irq(pdev, 0);
316
317 /* turn RTC on if it was not on */
318 sec = readb(ioaddr + RTC_SECONDS);
319 if (sec & RTC_STOP) {
320 sec &= RTC_SECONDS_MASK;
321 cen = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
322 writeb(RTC_WRITE, ioaddr + RTC_CONTROL);
323 writeb(sec, ioaddr + RTC_SECONDS);
324 writeb(cen & RTC_CENTURY_MASK, ioaddr + RTC_CONTROL);
325 }
326 if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
327 dev_warn(&pdev->dev, "voltage-low detected.\n");
328
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800329 if (pdata->irq > 0) {
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700330 writeb(0, ioaddr + RTC_INTERRUPTS);
David Brownelld728b1e2006-11-25 11:09:28 -0800331 if (request_irq(pdata->irq, ds1553_rtc_interrupt,
332 IRQF_DISABLED | IRQF_SHARED,
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700333 pdev->name, pdev) < 0) {
334 dev_warn(&pdev->dev, "interrupt not available.\n");
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800335 pdata->irq = 0;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700336 }
337 }
338
339 rtc = rtc_device_register(pdev->name, &pdev->dev,
340 &ds1553_rtc_ops, THIS_MODULE);
341 if (IS_ERR(rtc)) {
342 ret = PTR_ERR(rtc);
343 goto out;
344 }
345 pdata->rtc = rtc;
346 pdata->last_jiffies = jiffies;
347 platform_set_drvdata(pdev, pdata);
Atsushi Nemoto391b1fe2006-09-30 23:28:18 -0700348 ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
349 if (ret)
350 goto out;
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700351 return 0;
352 out:
Atsushi Nemoto391b1fe2006-09-30 23:28:18 -0700353 if (pdata->rtc)
354 rtc_device_unregister(pdata->rtc);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800355 if (pdata->irq > 0)
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700356 free_irq(pdata->irq, pdev);
357 if (ioaddr)
358 iounmap(ioaddr);
359 if (pdata->baseaddr)
360 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
361 kfree(pdata);
362 return ret;
363}
364
365static int __devexit ds1553_rtc_remove(struct platform_device *pdev)
366{
367 struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
368
369 sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
370 rtc_device_unregister(pdata->rtc);
Anton Vorontsov2fac6672009-01-06 14:42:11 -0800371 if (pdata->irq > 0) {
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700372 writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
373 free_irq(pdata->irq, pdev);
374 }
375 iounmap(pdata->ioaddr);
376 release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
377 kfree(pdata);
378 return 0;
379}
380
Kay Sieversad28a072008-04-10 21:29:25 -0700381/* work with hotplug and coldplug */
382MODULE_ALIAS("platform:rtc-ds1553");
383
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700384static struct platform_driver ds1553_rtc_driver = {
385 .probe = ds1553_rtc_probe,
386 .remove = __devexit_p(ds1553_rtc_remove),
387 .driver = {
Atsushi Nemotoe7634c22007-10-16 01:28:17 -0700388 .name = "rtc-ds1553",
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700389 .owner = THIS_MODULE,
390 },
391};
392
393static __init int ds1553_init(void)
394{
395 return platform_driver_register(&ds1553_rtc_driver);
396}
397
398static __exit void ds1553_exit(void)
399{
Atsushi Nemotoef154ec2007-07-21 04:37:55 -0700400 platform_driver_unregister(&ds1553_rtc_driver);
Atsushi Nemoto9bf5b4f2006-06-25 05:48:28 -0700401}
402
403module_init(ds1553_init);
404module_exit(ds1553_exit);
405
406MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
407MODULE_DESCRIPTION("Dallas DS1553 RTC driver");
408MODULE_LICENSE("GPL");
409MODULE_VERSION(DRV_VERSION);