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 #include <linux/slab.h>
23 #define DRV_VERSION "1.0"
26 struct rtc_device
*rtc
;
29 unsigned long baseaddr
;
33 static inline u8
ds1286_rtc_read(struct ds1286_priv
*priv
, int reg
)
35 return __raw_readl(&priv
->rtcregs
[reg
]) & 0xff;
38 static inline void ds1286_rtc_write(struct ds1286_priv
*priv
, u8 data
, int reg
)
40 __raw_writel(data
, &priv
->rtcregs
[reg
]);
44 static int ds1286_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
46 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
50 /* Allow or mask alarm interrupts */
51 spin_lock_irqsave(&priv
->lock
, flags
);
52 val
= ds1286_rtc_read(priv
, RTC_CMD
);
57 ds1286_rtc_write(priv
, val
, RTC_CMD
);
58 spin_unlock_irqrestore(&priv
->lock
, flags
);
63 #ifdef CONFIG_RTC_INTF_DEV
65 static int ds1286_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
67 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
73 /* Mask watchdog int. enab. bit */
74 spin_lock_irqsave(&priv
->lock
, flags
);
75 val
= ds1286_rtc_read(priv
, RTC_CMD
);
77 ds1286_rtc_write(priv
, val
, RTC_CMD
);
78 spin_unlock_irqrestore(&priv
->lock
, flags
);
81 /* Allow watchdog interrupts. */
82 spin_lock_irqsave(&priv
->lock
, flags
);
83 val
= ds1286_rtc_read(priv
, RTC_CMD
);
85 ds1286_rtc_write(priv
, val
, RTC_CMD
);
86 spin_unlock_irqrestore(&priv
->lock
, flags
);
95 #define ds1286_ioctl NULL
100 static int ds1286_proc(struct device
*dev
, struct seq_file
*seq
)
102 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
103 unsigned char month
, cmd
, amode
;
106 month
= ds1286_rtc_read(priv
, RTC_MONTH
);
109 "square_wave\t: %s\n",
110 (month
& RTC_EOSC
) ? "disabled" : "enabled",
111 (month
& RTC_ESQW
) ? "disabled" : "enabled");
113 amode
= ((ds1286_rtc_read(priv
, RTC_MINUTES_ALARM
) & 0x80) >> 5) |
114 ((ds1286_rtc_read(priv
, RTC_HOURS_ALARM
) & 0x80) >> 6) |
115 ((ds1286_rtc_read(priv
, RTC_DAY_ALARM
) & 0x80) >> 7);
124 s
= "hours and minutes match";
127 s
= "days, hours and minutes match";
133 seq_printf(seq
, "alarm_mode\t: %s\n", s
);
135 cmd
= ds1286_rtc_read(priv
, RTC_CMD
);
137 "alarm_enable\t: %s\n"
140 "wdog_alarm_mask\t: %s\n"
141 "interrupt_mode\t: %s\n"
142 "INTB_mode\t: %s_active\n"
143 "interrupt_pins\t: %s\n",
144 (cmd
& RTC_TDF
) ? "yes" : "no",
145 (cmd
& RTC_WAF
) ? "yes" : "no",
146 (cmd
& RTC_TDM
) ? "disabled" : "enabled",
147 (cmd
& RTC_WAM
) ? "disabled" : "enabled",
148 (cmd
& RTC_PU_LVL
) ? "pulse" : "level",
149 (cmd
& RTC_IBH_LO
) ? "low" : "high",
150 (cmd
& RTC_IPSW
) ? "unswapped" : "swapped");
155 #define ds1286_proc NULL
158 static int ds1286_read_time(struct device
*dev
, struct rtc_time
*tm
)
160 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
161 unsigned char save_control
;
163 unsigned long uip_watchdog
= jiffies
;
166 * read RTC once any update in progress is done. The update
167 * can take just over 2ms. We wait 10 to 20ms. There is no need to
168 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
169 * If you need to know *exactly* when a second has started, enable
170 * periodic update complete interrupts, (via ioctl) and then
171 * immediately read /dev/rtc which will block until you get the IRQ.
172 * Once the read clears, read the RTC time (again via ioctl). Easy.
175 if (ds1286_rtc_read(priv
, RTC_CMD
) & RTC_TE
)
176 while (time_before(jiffies
, uip_watchdog
+ 2*HZ
/100))
180 * Only the values that we read from the RTC are set. We leave
181 * tm_wday, tm_yday and tm_isdst untouched. Even though the
182 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
183 * by the RTC when initially set to a non-zero value.
185 spin_lock_irqsave(&priv
->lock
, flags
);
186 save_control
= ds1286_rtc_read(priv
, RTC_CMD
);
187 ds1286_rtc_write(priv
, (save_control
|RTC_TE
), RTC_CMD
);
189 tm
->tm_sec
= ds1286_rtc_read(priv
, RTC_SECONDS
);
190 tm
->tm_min
= ds1286_rtc_read(priv
, RTC_MINUTES
);
191 tm
->tm_hour
= ds1286_rtc_read(priv
, RTC_HOURS
) & 0x3f;
192 tm
->tm_mday
= ds1286_rtc_read(priv
, RTC_DATE
);
193 tm
->tm_mon
= ds1286_rtc_read(priv
, RTC_MONTH
) & 0x1f;
194 tm
->tm_year
= ds1286_rtc_read(priv
, RTC_YEAR
);
196 ds1286_rtc_write(priv
, save_control
, RTC_CMD
);
197 spin_unlock_irqrestore(&priv
->lock
, flags
);
199 tm
->tm_sec
= bcd2bin(tm
->tm_sec
);
200 tm
->tm_min
= bcd2bin(tm
->tm_min
);
201 tm
->tm_hour
= bcd2bin(tm
->tm_hour
);
202 tm
->tm_mday
= bcd2bin(tm
->tm_mday
);
203 tm
->tm_mon
= bcd2bin(tm
->tm_mon
);
204 tm
->tm_year
= bcd2bin(tm
->tm_year
);
207 * Account for differences between how the RTC uses the values
208 * and how they are defined in a struct rtc_time;
210 if (tm
->tm_year
< 45)
213 if (tm
->tm_year
< 70)
218 return rtc_valid_tm(tm
);
221 static int ds1286_set_time(struct device
*dev
, struct rtc_time
*tm
)
223 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
224 unsigned char mon
, day
, hrs
, min
, sec
;
225 unsigned char save_control
;
229 yrs
= tm
->tm_year
+ 1900;
230 mon
= tm
->tm_mon
+ 1; /* tm_mon starts at zero */
240 if (yrs
> 255) /* They are unsigned */
253 spin_lock_irqsave(&priv
->lock
, flags
);
254 save_control
= ds1286_rtc_read(priv
, RTC_CMD
);
255 ds1286_rtc_write(priv
, (save_control
|RTC_TE
), RTC_CMD
);
257 ds1286_rtc_write(priv
, yrs
, RTC_YEAR
);
258 ds1286_rtc_write(priv
, mon
, RTC_MONTH
);
259 ds1286_rtc_write(priv
, day
, RTC_DATE
);
260 ds1286_rtc_write(priv
, hrs
, RTC_HOURS
);
261 ds1286_rtc_write(priv
, min
, RTC_MINUTES
);
262 ds1286_rtc_write(priv
, sec
, RTC_SECONDS
);
263 ds1286_rtc_write(priv
, 0, RTC_HUNDREDTH_SECOND
);
265 ds1286_rtc_write(priv
, save_control
, RTC_CMD
);
266 spin_unlock_irqrestore(&priv
->lock
, flags
);
270 static int ds1286_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
272 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
277 * Only the values that we read from the RTC are set. That
278 * means only tm_wday, tm_hour, tm_min.
280 spin_lock_irqsave(&priv
->lock
, flags
);
281 alm
->time
.tm_min
= ds1286_rtc_read(priv
, RTC_MINUTES_ALARM
) & 0x7f;
282 alm
->time
.tm_hour
= ds1286_rtc_read(priv
, RTC_HOURS_ALARM
) & 0x1f;
283 alm
->time
.tm_wday
= ds1286_rtc_read(priv
, RTC_DAY_ALARM
) & 0x07;
284 cmd
= ds1286_rtc_read(priv
, RTC_CMD
);
285 spin_unlock_irqrestore(&priv
->lock
, flags
);
287 alm
->time
.tm_min
= bcd2bin(alm
->time
.tm_min
);
288 alm
->time
.tm_hour
= bcd2bin(alm
->time
.tm_hour
);
289 alm
->time
.tm_sec
= 0;
293 static int ds1286_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
295 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
296 unsigned char hrs
, min
, sec
;
298 hrs
= alm
->time
.tm_hour
;
299 min
= alm
->time
.tm_min
;
300 sec
= alm
->time
.tm_sec
;
314 spin_lock(&priv
->lock
);
315 ds1286_rtc_write(priv
, hrs
, RTC_HOURS_ALARM
);
316 ds1286_rtc_write(priv
, min
, RTC_MINUTES_ALARM
);
317 spin_unlock(&priv
->lock
);
322 static const struct rtc_class_ops ds1286_ops
= {
323 .ioctl
= ds1286_ioctl
,
325 .read_time
= ds1286_read_time
,
326 .set_time
= ds1286_set_time
,
327 .read_alarm
= ds1286_read_alarm
,
328 .set_alarm
= ds1286_set_alarm
,
329 .alarm_irq_enable
= ds1286_alarm_irq_enable
,
332 static int __devinit
ds1286_probe(struct platform_device
*pdev
)
334 struct rtc_device
*rtc
;
335 struct resource
*res
;
336 struct ds1286_priv
*priv
;
339 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
342 priv
= kzalloc(sizeof(struct ds1286_priv
), GFP_KERNEL
);
346 priv
->size
= res
->end
- res
->start
+ 1;
347 if (!request_mem_region(res
->start
, priv
->size
, pdev
->name
)) {
351 priv
->baseaddr
= res
->start
;
352 priv
->rtcregs
= ioremap(priv
->baseaddr
, priv
->size
);
353 if (!priv
->rtcregs
) {
357 spin_lock_init(&priv
->lock
);
358 rtc
= rtc_device_register("ds1286", &pdev
->dev
,
359 &ds1286_ops
, THIS_MODULE
);
365 platform_set_drvdata(pdev
, priv
);
370 rtc_device_unregister(priv
->rtc
);
372 iounmap(priv
->rtcregs
);
374 release_mem_region(priv
->baseaddr
, priv
->size
);
379 static int __devexit
ds1286_remove(struct platform_device
*pdev
)
381 struct ds1286_priv
*priv
= platform_get_drvdata(pdev
);
383 rtc_device_unregister(priv
->rtc
);
384 iounmap(priv
->rtcregs
);
385 release_mem_region(priv
->baseaddr
, priv
->size
);
390 static struct platform_driver ds1286_platform_driver
= {
392 .name
= "rtc-ds1286",
393 .owner
= THIS_MODULE
,
395 .probe
= ds1286_probe
,
396 .remove
= __devexit_p(ds1286_remove
),
399 static int __init
ds1286_init(void)
401 return platform_driver_register(&ds1286_platform_driver
);
404 static void __exit
ds1286_exit(void)
406 platform_driver_unregister(&ds1286_platform_driver
);
409 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
410 MODULE_DESCRIPTION("DS1286 RTC driver");
411 MODULE_LICENSE("GPL");
412 MODULE_VERSION(DRV_VERSION
);
413 MODULE_ALIAS("platform:rtc-ds1286");
415 module_init(ds1286_init
);
416 module_exit(ds1286_exit
);