2 * DS1286 Real Time Clock interface for Linux
4 * Copyright (C) 1998, 1999, 2000 Ralf Baechle
5 * Copyright (C) 2008 Thomas Bogendoerfer
7 * Based on code written by Paul Gortmaker.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/module.h>
16 #include <linux/rtc.h>
17 #include <linux/platform_device.h>
18 #include <linux/bcd.h>
19 #include <linux/ds1286.h>
21 #define DRV_VERSION "1.0"
24 struct rtc_device *rtc;
27 unsigned long baseaddr;
31 static inline u8 ds1286_rtc_read(struct ds1286_priv *priv, int reg)
33 return __raw_readl(&priv->rtcregs[reg]) & 0xff;
36 static inline void ds1286_rtc_write(struct ds1286_priv *priv, u8 data, int reg)
38 __raw_writel(data, &priv->rtcregs[reg]);
41 #ifdef CONFIG_RTC_INTF_DEV
43 static int ds1286_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
45 struct ds1286_priv *priv = dev_get_drvdata(dev);
51 /* Mask alarm int. enab. bit */
52 spin_lock_irqsave(&priv->lock, flags);
53 val = ds1286_rtc_read(priv, RTC_CMD);
55 ds1286_rtc_write(priv, val, RTC_CMD);
56 spin_unlock_irqrestore(&priv->lock, flags);
59 /* Allow alarm interrupts. */
60 spin_lock_irqsave(&priv->lock, flags);
61 val = ds1286_rtc_read(priv, RTC_CMD);
63 ds1286_rtc_write(priv, val, RTC_CMD);
64 spin_unlock_irqrestore(&priv->lock, flags);
67 /* Mask watchdog int. enab. bit */
68 spin_lock_irqsave(&priv->lock, flags);
69 val = ds1286_rtc_read(priv, RTC_CMD);
71 ds1286_rtc_write(priv, val, RTC_CMD);
72 spin_unlock_irqrestore(&priv->lock, flags);
75 /* Allow watchdog interrupts. */
76 spin_lock_irqsave(&priv->lock, flags);
77 val = ds1286_rtc_read(priv, RTC_CMD);
79 ds1286_rtc_write(priv, val, RTC_CMD);
80 spin_unlock_irqrestore(&priv->lock, flags);
89 #define ds1286_ioctl NULL
94 static int ds1286_proc(struct device *dev, struct seq_file *seq)
96 struct ds1286_priv *priv = dev_get_drvdata(dev);
97 unsigned char month, cmd, amode;
100 month = ds1286_rtc_read(priv, RTC_MONTH);
103 "square_wave\t: %s\n",
104 (month & RTC_EOSC) ? "disabled" : "enabled",
105 (month & RTC_ESQW) ? "disabled" : "enabled");
107 amode = ((ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x80) >> 5) |
108 ((ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x80) >> 6) |
109 ((ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x80) >> 7);
118 s = "hours and minutes match";
121 s = "days, hours and minutes match";
127 seq_printf(seq, "alarm_mode\t: %s\n", s);
129 cmd = ds1286_rtc_read(priv, RTC_CMD);
131 "alarm_enable\t: %s\n"
134 "wdog_alarm_mask\t: %s\n"
135 "interrupt_mode\t: %s\n"
136 "INTB_mode\t: %s_active\n"
137 "interrupt_pins\t: %s\n",
138 (cmd & RTC_TDF) ? "yes" : "no",
139 (cmd & RTC_WAF) ? "yes" : "no",
140 (cmd & RTC_TDM) ? "disabled" : "enabled",
141 (cmd & RTC_WAM) ? "disabled" : "enabled",
142 (cmd & RTC_PU_LVL) ? "pulse" : "level",
143 (cmd & RTC_IBH_LO) ? "low" : "high",
144 (cmd & RTC_IPSW) ? "unswapped" : "swapped");
149 #define ds1286_proc NULL
152 static int ds1286_read_time(struct device *dev, struct rtc_time *tm)
154 struct ds1286_priv *priv = dev_get_drvdata(dev);
155 unsigned char save_control;
157 unsigned long uip_watchdog = jiffies;
160 * read RTC once any update in progress is done. The update
161 * can take just over 2ms. We wait 10 to 20ms. There is no need to
162 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
163 * If you need to know *exactly* when a second has started, enable
164 * periodic update complete interrupts, (via ioctl) and then
165 * immediately read /dev/rtc which will block until you get the IRQ.
166 * Once the read clears, read the RTC time (again via ioctl). Easy.
169 if (ds1286_rtc_read(priv, RTC_CMD) & RTC_TE)
170 while (time_before(jiffies, uip_watchdog + 2*HZ/100))
174 * Only the values that we read from the RTC are set. We leave
175 * tm_wday, tm_yday and tm_isdst untouched. Even though the
176 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
177 * by the RTC when initially set to a non-zero value.
179 spin_lock_irqsave(&priv->lock, flags);
180 save_control = ds1286_rtc_read(priv, RTC_CMD);
181 ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
183 tm->tm_sec = ds1286_rtc_read(priv, RTC_SECONDS);
184 tm->tm_min = ds1286_rtc_read(priv, RTC_MINUTES);
185 tm->tm_hour = ds1286_rtc_read(priv, RTC_HOURS) & 0x3f;
186 tm->tm_mday = ds1286_rtc_read(priv, RTC_DATE);
187 tm->tm_mon = ds1286_rtc_read(priv, RTC_MONTH) & 0x1f;
188 tm->tm_year = ds1286_rtc_read(priv, RTC_YEAR);
190 ds1286_rtc_write(priv, save_control, RTC_CMD);
191 spin_unlock_irqrestore(&priv->lock, flags);
193 tm->tm_sec = bcd2bin(tm->tm_sec);
194 tm->tm_min = bcd2bin(tm->tm_min);
195 tm->tm_hour = bcd2bin(tm->tm_hour);
196 tm->tm_mday = bcd2bin(tm->tm_mday);
197 tm->tm_mon = bcd2bin(tm->tm_mon);
198 tm->tm_year = bcd2bin(tm->tm_year);
201 * Account for differences between how the RTC uses the values
202 * and how they are defined in a struct rtc_time;
204 if (tm->tm_year < 45)
207 if (tm->tm_year < 70)
212 return rtc_valid_tm(tm);
215 static int ds1286_set_time(struct device *dev, struct rtc_time *tm)
217 struct ds1286_priv *priv = dev_get_drvdata(dev);
218 unsigned char mon, day, hrs, min, sec;
219 unsigned char save_control;
223 yrs = tm->tm_year + 1900;
224 mon = tm->tm_mon + 1; /* tm_mon starts at zero */
234 if (yrs > 255) /* They are unsigned */
247 spin_lock_irqsave(&priv->lock, flags);
248 save_control = ds1286_rtc_read(priv, RTC_CMD);
249 ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
251 ds1286_rtc_write(priv, yrs, RTC_YEAR);
252 ds1286_rtc_write(priv, mon, RTC_MONTH);
253 ds1286_rtc_write(priv, day, RTC_DATE);
254 ds1286_rtc_write(priv, hrs, RTC_HOURS);
255 ds1286_rtc_write(priv, min, RTC_MINUTES);
256 ds1286_rtc_write(priv, sec, RTC_SECONDS);
257 ds1286_rtc_write(priv, 0, RTC_HUNDREDTH_SECOND);
259 ds1286_rtc_write(priv, save_control, RTC_CMD);
260 spin_unlock_irqrestore(&priv->lock, flags);
264 static int ds1286_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
266 struct ds1286_priv *priv = dev_get_drvdata(dev);
271 * Only the values that we read from the RTC are set. That
272 * means only tm_wday, tm_hour, tm_min.
274 spin_lock_irqsave(&priv->lock, flags);
275 alm->time.tm_min = ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x7f;
276 alm->time.tm_hour = ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x1f;
277 alm->time.tm_wday = ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x07;
278 cmd = ds1286_rtc_read(priv, RTC_CMD);
279 spin_unlock_irqrestore(&priv->lock, flags);
281 alm->time.tm_min = bcd2bin(alm->time.tm_min);
282 alm->time.tm_hour = bcd2bin(alm->time.tm_hour);
283 alm->time.tm_sec = 0;
287 static int ds1286_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
289 struct ds1286_priv *priv = dev_get_drvdata(dev);
290 unsigned char hrs, min, sec;
292 hrs = alm->time.tm_hour;
293 min = alm->time.tm_min;
294 sec = alm->time.tm_sec;
308 spin_lock(&priv->lock);
309 ds1286_rtc_write(priv, hrs, RTC_HOURS_ALARM);
310 ds1286_rtc_write(priv, min, RTC_MINUTES_ALARM);
311 spin_unlock(&priv->lock);
316 static const struct rtc_class_ops ds1286_ops = {
317 .ioctl = ds1286_ioctl,
319 .read_time = ds1286_read_time,
320 .set_time = ds1286_set_time,
321 .read_alarm = ds1286_read_alarm,
322 .set_alarm = ds1286_set_alarm,
325 static int __devinit ds1286_probe(struct platform_device *pdev)
327 struct rtc_device *rtc;
328 struct resource *res;
329 struct ds1286_priv *priv;
332 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
335 priv = kzalloc(sizeof(struct ds1286_priv), GFP_KERNEL);
339 priv->size = res->end - res->start + 1;
340 if (!request_mem_region(res->start, priv->size, pdev->name)) {
344 priv->baseaddr = res->start;
345 priv->rtcregs = ioremap(priv->baseaddr, priv->size);
346 if (!priv->rtcregs) {
350 spin_lock_init(&priv->lock);
351 rtc = rtc_device_register("ds1286", &pdev->dev,
352 &ds1286_ops, THIS_MODULE);
358 platform_set_drvdata(pdev, priv);
363 rtc_device_unregister(priv->rtc);
365 iounmap(priv->rtcregs);
367 release_mem_region(priv->baseaddr, priv->size);
372 static int __devexit ds1286_remove(struct platform_device *pdev)
374 struct ds1286_priv *priv = platform_get_drvdata(pdev);
376 rtc_device_unregister(priv->rtc);
377 iounmap(priv->rtcregs);
378 release_mem_region(priv->baseaddr, priv->size);
383 static struct platform_driver ds1286_platform_driver = {
385 .name = "rtc-ds1286",
386 .owner = THIS_MODULE,
388 .probe = ds1286_probe,
389 .remove = __devexit_p(ds1286_remove),
392 static int __init ds1286_init(void)
394 return platform_driver_register(&ds1286_platform_driver);
397 static void __exit ds1286_exit(void)
399 platform_driver_unregister(&ds1286_platform_driver);
402 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
403 MODULE_DESCRIPTION("DS1286 RTC driver");
404 MODULE_LICENSE("GPL");
405 MODULE_VERSION(DRV_VERSION);
406 MODULE_ALIAS("platform:rtc-ds1286");
408 module_init(ds1286_init);
409 module_exit(ds1286_exit);