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/rtc/ds1286.h>
21 #include <linux/slab.h>
24 struct rtc_device
*rtc
;
29 static inline u8
ds1286_rtc_read(struct ds1286_priv
*priv
, int reg
)
31 return __raw_readl(&priv
->rtcregs
[reg
]) & 0xff;
34 static inline void ds1286_rtc_write(struct ds1286_priv
*priv
, u8 data
, int reg
)
36 __raw_writel(data
, &priv
->rtcregs
[reg
]);
40 static int ds1286_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
42 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
46 /* Allow or mask alarm interrupts */
47 spin_lock_irqsave(&priv
->lock
, flags
);
48 val
= ds1286_rtc_read(priv
, RTC_CMD
);
53 ds1286_rtc_write(priv
, val
, RTC_CMD
);
54 spin_unlock_irqrestore(&priv
->lock
, flags
);
59 #ifdef CONFIG_RTC_INTF_DEV
61 static int ds1286_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
63 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
69 /* Mask watchdog int. enab. bit */
70 spin_lock_irqsave(&priv
->lock
, flags
);
71 val
= ds1286_rtc_read(priv
, RTC_CMD
);
73 ds1286_rtc_write(priv
, val
, RTC_CMD
);
74 spin_unlock_irqrestore(&priv
->lock
, flags
);
77 /* Allow watchdog interrupts. */
78 spin_lock_irqsave(&priv
->lock
, flags
);
79 val
= ds1286_rtc_read(priv
, RTC_CMD
);
81 ds1286_rtc_write(priv
, val
, RTC_CMD
);
82 spin_unlock_irqrestore(&priv
->lock
, flags
);
91 #define ds1286_ioctl NULL
96 static int ds1286_proc(struct device
*dev
, struct seq_file
*seq
)
98 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
99 unsigned char month
, cmd
, amode
;
102 month
= ds1286_rtc_read(priv
, RTC_MONTH
);
105 "square_wave\t: %s\n",
106 (month
& RTC_EOSC
) ? "disabled" : "enabled",
107 (month
& RTC_ESQW
) ? "disabled" : "enabled");
109 amode
= ((ds1286_rtc_read(priv
, RTC_MINUTES_ALARM
) & 0x80) >> 5) |
110 ((ds1286_rtc_read(priv
, RTC_HOURS_ALARM
) & 0x80) >> 6) |
111 ((ds1286_rtc_read(priv
, RTC_DAY_ALARM
) & 0x80) >> 7);
120 s
= "hours and minutes match";
123 s
= "days, hours and minutes match";
129 seq_printf(seq
, "alarm_mode\t: %s\n", s
);
131 cmd
= ds1286_rtc_read(priv
, RTC_CMD
);
133 "alarm_enable\t: %s\n"
136 "wdog_alarm_mask\t: %s\n"
137 "interrupt_mode\t: %s\n"
138 "INTB_mode\t: %s_active\n"
139 "interrupt_pins\t: %s\n",
140 (cmd
& RTC_TDF
) ? "yes" : "no",
141 (cmd
& RTC_WAF
) ? "yes" : "no",
142 (cmd
& RTC_TDM
) ? "disabled" : "enabled",
143 (cmd
& RTC_WAM
) ? "disabled" : "enabled",
144 (cmd
& RTC_PU_LVL
) ? "pulse" : "level",
145 (cmd
& RTC_IBH_LO
) ? "low" : "high",
146 (cmd
& RTC_IPSW
) ? "unswapped" : "swapped");
151 #define ds1286_proc NULL
154 static int ds1286_read_time(struct device
*dev
, struct rtc_time
*tm
)
156 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
157 unsigned char save_control
;
159 unsigned long uip_watchdog
= jiffies
;
162 * read RTC once any update in progress is done. The update
163 * can take just over 2ms. We wait 10 to 20ms. There is no need to
164 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
165 * If you need to know *exactly* when a second has started, enable
166 * periodic update complete interrupts, (via ioctl) and then
167 * immediately read /dev/rtc which will block until you get the IRQ.
168 * Once the read clears, read the RTC time (again via ioctl). Easy.
171 if (ds1286_rtc_read(priv
, RTC_CMD
) & RTC_TE
)
172 while (time_before(jiffies
, uip_watchdog
+ 2*HZ
/100))
176 * Only the values that we read from the RTC are set. We leave
177 * tm_wday, tm_yday and tm_isdst untouched. Even though the
178 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
179 * by the RTC when initially set to a non-zero value.
181 spin_lock_irqsave(&priv
->lock
, flags
);
182 save_control
= ds1286_rtc_read(priv
, RTC_CMD
);
183 ds1286_rtc_write(priv
, (save_control
|RTC_TE
), RTC_CMD
);
185 tm
->tm_sec
= ds1286_rtc_read(priv
, RTC_SECONDS
);
186 tm
->tm_min
= ds1286_rtc_read(priv
, RTC_MINUTES
);
187 tm
->tm_hour
= ds1286_rtc_read(priv
, RTC_HOURS
) & 0x3f;
188 tm
->tm_mday
= ds1286_rtc_read(priv
, RTC_DATE
);
189 tm
->tm_mon
= ds1286_rtc_read(priv
, RTC_MONTH
) & 0x1f;
190 tm
->tm_year
= ds1286_rtc_read(priv
, RTC_YEAR
);
192 ds1286_rtc_write(priv
, save_control
, RTC_CMD
);
193 spin_unlock_irqrestore(&priv
->lock
, flags
);
195 tm
->tm_sec
= bcd2bin(tm
->tm_sec
);
196 tm
->tm_min
= bcd2bin(tm
->tm_min
);
197 tm
->tm_hour
= bcd2bin(tm
->tm_hour
);
198 tm
->tm_mday
= bcd2bin(tm
->tm_mday
);
199 tm
->tm_mon
= bcd2bin(tm
->tm_mon
);
200 tm
->tm_year
= bcd2bin(tm
->tm_year
);
203 * Account for differences between how the RTC uses the values
204 * and how they are defined in a struct rtc_time;
206 if (tm
->tm_year
< 45)
209 if (tm
->tm_year
< 70)
217 static int ds1286_set_time(struct device
*dev
, struct rtc_time
*tm
)
219 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
220 unsigned char mon
, day
, hrs
, min
, sec
;
221 unsigned char save_control
;
225 yrs
= tm
->tm_year
+ 1900;
226 mon
= tm
->tm_mon
+ 1; /* tm_mon starts at zero */
236 if (yrs
> 255) /* They are unsigned */
249 spin_lock_irqsave(&priv
->lock
, flags
);
250 save_control
= ds1286_rtc_read(priv
, RTC_CMD
);
251 ds1286_rtc_write(priv
, (save_control
|RTC_TE
), RTC_CMD
);
253 ds1286_rtc_write(priv
, yrs
, RTC_YEAR
);
254 ds1286_rtc_write(priv
, mon
, RTC_MONTH
);
255 ds1286_rtc_write(priv
, day
, RTC_DATE
);
256 ds1286_rtc_write(priv
, hrs
, RTC_HOURS
);
257 ds1286_rtc_write(priv
, min
, RTC_MINUTES
);
258 ds1286_rtc_write(priv
, sec
, RTC_SECONDS
);
259 ds1286_rtc_write(priv
, 0, RTC_HUNDREDTH_SECOND
);
261 ds1286_rtc_write(priv
, save_control
, RTC_CMD
);
262 spin_unlock_irqrestore(&priv
->lock
, flags
);
266 static int ds1286_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
268 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
272 * Only the values that we read from the RTC are set. That
273 * means only tm_wday, tm_hour, tm_min.
275 spin_lock_irqsave(&priv
->lock
, flags
);
276 alm
->time
.tm_min
= ds1286_rtc_read(priv
, RTC_MINUTES_ALARM
) & 0x7f;
277 alm
->time
.tm_hour
= ds1286_rtc_read(priv
, RTC_HOURS_ALARM
) & 0x1f;
278 alm
->time
.tm_wday
= ds1286_rtc_read(priv
, RTC_DAY_ALARM
) & 0x07;
279 ds1286_rtc_read(priv
, RTC_CMD
);
280 spin_unlock_irqrestore(&priv
->lock
, flags
);
282 alm
->time
.tm_min
= bcd2bin(alm
->time
.tm_min
);
283 alm
->time
.tm_hour
= bcd2bin(alm
->time
.tm_hour
);
284 alm
->time
.tm_sec
= 0;
288 static int ds1286_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
290 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
291 unsigned char hrs
, min
, sec
;
293 hrs
= alm
->time
.tm_hour
;
294 min
= alm
->time
.tm_min
;
295 sec
= alm
->time
.tm_sec
;
309 spin_lock(&priv
->lock
);
310 ds1286_rtc_write(priv
, hrs
, RTC_HOURS_ALARM
);
311 ds1286_rtc_write(priv
, min
, RTC_MINUTES_ALARM
);
312 spin_unlock(&priv
->lock
);
317 static const struct rtc_class_ops ds1286_ops
= {
318 .ioctl
= ds1286_ioctl
,
320 .read_time
= ds1286_read_time
,
321 .set_time
= ds1286_set_time
,
322 .read_alarm
= ds1286_read_alarm
,
323 .set_alarm
= ds1286_set_alarm
,
324 .alarm_irq_enable
= ds1286_alarm_irq_enable
,
327 static int ds1286_probe(struct platform_device
*pdev
)
329 struct rtc_device
*rtc
;
330 struct resource
*res
;
331 struct ds1286_priv
*priv
;
333 priv
= devm_kzalloc(&pdev
->dev
, sizeof(struct ds1286_priv
), GFP_KERNEL
);
337 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
338 priv
->rtcregs
= devm_ioremap_resource(&pdev
->dev
, res
);
339 if (IS_ERR(priv
->rtcregs
))
340 return PTR_ERR(priv
->rtcregs
);
342 spin_lock_init(&priv
->lock
);
343 platform_set_drvdata(pdev
, priv
);
344 rtc
= devm_rtc_device_register(&pdev
->dev
, "ds1286", &ds1286_ops
,
352 static struct platform_driver ds1286_platform_driver
= {
354 .name
= "rtc-ds1286",
356 .probe
= ds1286_probe
,
359 module_platform_driver(ds1286_platform_driver
);
361 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
362 MODULE_DESCRIPTION("DS1286 RTC driver");
363 MODULE_LICENSE("GPL");
364 MODULE_ALIAS("platform:rtc-ds1286");