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
;
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
]);
42 static int ds1286_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
44 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
48 /* Allow or mask alarm interrupts */
49 spin_lock_irqsave(&priv
->lock
, flags
);
50 val
= ds1286_rtc_read(priv
, RTC_CMD
);
55 ds1286_rtc_write(priv
, val
, RTC_CMD
);
56 spin_unlock_irqrestore(&priv
->lock
, flags
);
61 #ifdef CONFIG_RTC_INTF_DEV
63 static int ds1286_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
65 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
71 /* Mask watchdog int. enab. bit */
72 spin_lock_irqsave(&priv
->lock
, flags
);
73 val
= ds1286_rtc_read(priv
, RTC_CMD
);
75 ds1286_rtc_write(priv
, val
, RTC_CMD
);
76 spin_unlock_irqrestore(&priv
->lock
, flags
);
79 /* Allow watchdog interrupts. */
80 spin_lock_irqsave(&priv
->lock
, flags
);
81 val
= ds1286_rtc_read(priv
, RTC_CMD
);
83 ds1286_rtc_write(priv
, val
, RTC_CMD
);
84 spin_unlock_irqrestore(&priv
->lock
, flags
);
93 #define ds1286_ioctl NULL
98 static int ds1286_proc(struct device
*dev
, struct seq_file
*seq
)
100 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
101 unsigned char month
, cmd
, amode
;
104 month
= ds1286_rtc_read(priv
, RTC_MONTH
);
107 "square_wave\t: %s\n",
108 (month
& RTC_EOSC
) ? "disabled" : "enabled",
109 (month
& RTC_ESQW
) ? "disabled" : "enabled");
111 amode
= ((ds1286_rtc_read(priv
, RTC_MINUTES_ALARM
) & 0x80) >> 5) |
112 ((ds1286_rtc_read(priv
, RTC_HOURS_ALARM
) & 0x80) >> 6) |
113 ((ds1286_rtc_read(priv
, RTC_DAY_ALARM
) & 0x80) >> 7);
122 s
= "hours and minutes match";
125 s
= "days, hours and minutes match";
131 seq_printf(seq
, "alarm_mode\t: %s\n", s
);
133 cmd
= ds1286_rtc_read(priv
, RTC_CMD
);
135 "alarm_enable\t: %s\n"
138 "wdog_alarm_mask\t: %s\n"
139 "interrupt_mode\t: %s\n"
140 "INTB_mode\t: %s_active\n"
141 "interrupt_pins\t: %s\n",
142 (cmd
& RTC_TDF
) ? "yes" : "no",
143 (cmd
& RTC_WAF
) ? "yes" : "no",
144 (cmd
& RTC_TDM
) ? "disabled" : "enabled",
145 (cmd
& RTC_WAM
) ? "disabled" : "enabled",
146 (cmd
& RTC_PU_LVL
) ? "pulse" : "level",
147 (cmd
& RTC_IBH_LO
) ? "low" : "high",
148 (cmd
& RTC_IPSW
) ? "unswapped" : "swapped");
153 #define ds1286_proc NULL
156 static int ds1286_read_time(struct device
*dev
, struct rtc_time
*tm
)
158 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
159 unsigned char save_control
;
161 unsigned long uip_watchdog
= jiffies
;
164 * read RTC once any update in progress is done. The update
165 * can take just over 2ms. We wait 10 to 20ms. There is no need to
166 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
167 * If you need to know *exactly* when a second has started, enable
168 * periodic update complete interrupts, (via ioctl) and then
169 * immediately read /dev/rtc which will block until you get the IRQ.
170 * Once the read clears, read the RTC time (again via ioctl). Easy.
173 if (ds1286_rtc_read(priv
, RTC_CMD
) & RTC_TE
)
174 while (time_before(jiffies
, uip_watchdog
+ 2*HZ
/100))
178 * Only the values that we read from the RTC are set. We leave
179 * tm_wday, tm_yday and tm_isdst untouched. Even though the
180 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
181 * by the RTC when initially set to a non-zero value.
183 spin_lock_irqsave(&priv
->lock
, flags
);
184 save_control
= ds1286_rtc_read(priv
, RTC_CMD
);
185 ds1286_rtc_write(priv
, (save_control
|RTC_TE
), RTC_CMD
);
187 tm
->tm_sec
= ds1286_rtc_read(priv
, RTC_SECONDS
);
188 tm
->tm_min
= ds1286_rtc_read(priv
, RTC_MINUTES
);
189 tm
->tm_hour
= ds1286_rtc_read(priv
, RTC_HOURS
) & 0x3f;
190 tm
->tm_mday
= ds1286_rtc_read(priv
, RTC_DATE
);
191 tm
->tm_mon
= ds1286_rtc_read(priv
, RTC_MONTH
) & 0x1f;
192 tm
->tm_year
= ds1286_rtc_read(priv
, RTC_YEAR
);
194 ds1286_rtc_write(priv
, save_control
, RTC_CMD
);
195 spin_unlock_irqrestore(&priv
->lock
, flags
);
197 tm
->tm_sec
= bcd2bin(tm
->tm_sec
);
198 tm
->tm_min
= bcd2bin(tm
->tm_min
);
199 tm
->tm_hour
= bcd2bin(tm
->tm_hour
);
200 tm
->tm_mday
= bcd2bin(tm
->tm_mday
);
201 tm
->tm_mon
= bcd2bin(tm
->tm_mon
);
202 tm
->tm_year
= bcd2bin(tm
->tm_year
);
205 * Account for differences between how the RTC uses the values
206 * and how they are defined in a struct rtc_time;
208 if (tm
->tm_year
< 45)
211 if (tm
->tm_year
< 70)
216 return rtc_valid_tm(tm
);
219 static int ds1286_set_time(struct device
*dev
, struct rtc_time
*tm
)
221 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
222 unsigned char mon
, day
, hrs
, min
, sec
;
223 unsigned char save_control
;
227 yrs
= tm
->tm_year
+ 1900;
228 mon
= tm
->tm_mon
+ 1; /* tm_mon starts at zero */
238 if (yrs
> 255) /* They are unsigned */
251 spin_lock_irqsave(&priv
->lock
, flags
);
252 save_control
= ds1286_rtc_read(priv
, RTC_CMD
);
253 ds1286_rtc_write(priv
, (save_control
|RTC_TE
), RTC_CMD
);
255 ds1286_rtc_write(priv
, yrs
, RTC_YEAR
);
256 ds1286_rtc_write(priv
, mon
, RTC_MONTH
);
257 ds1286_rtc_write(priv
, day
, RTC_DATE
);
258 ds1286_rtc_write(priv
, hrs
, RTC_HOURS
);
259 ds1286_rtc_write(priv
, min
, RTC_MINUTES
);
260 ds1286_rtc_write(priv
, sec
, RTC_SECONDS
);
261 ds1286_rtc_write(priv
, 0, RTC_HUNDREDTH_SECOND
);
263 ds1286_rtc_write(priv
, save_control
, RTC_CMD
);
264 spin_unlock_irqrestore(&priv
->lock
, flags
);
268 static int ds1286_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
270 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
274 * Only the values that we read from the RTC are set. That
275 * means only tm_wday, tm_hour, tm_min.
277 spin_lock_irqsave(&priv
->lock
, flags
);
278 alm
->time
.tm_min
= ds1286_rtc_read(priv
, RTC_MINUTES_ALARM
) & 0x7f;
279 alm
->time
.tm_hour
= ds1286_rtc_read(priv
, RTC_HOURS_ALARM
) & 0x1f;
280 alm
->time
.tm_wday
= ds1286_rtc_read(priv
, RTC_DAY_ALARM
) & 0x07;
281 ds1286_rtc_read(priv
, RTC_CMD
);
282 spin_unlock_irqrestore(&priv
->lock
, flags
);
284 alm
->time
.tm_min
= bcd2bin(alm
->time
.tm_min
);
285 alm
->time
.tm_hour
= bcd2bin(alm
->time
.tm_hour
);
286 alm
->time
.tm_sec
= 0;
290 static int ds1286_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
292 struct ds1286_priv
*priv
= dev_get_drvdata(dev
);
293 unsigned char hrs
, min
, sec
;
295 hrs
= alm
->time
.tm_hour
;
296 min
= alm
->time
.tm_min
;
297 sec
= alm
->time
.tm_sec
;
311 spin_lock(&priv
->lock
);
312 ds1286_rtc_write(priv
, hrs
, RTC_HOURS_ALARM
);
313 ds1286_rtc_write(priv
, min
, RTC_MINUTES_ALARM
);
314 spin_unlock(&priv
->lock
);
319 static const struct rtc_class_ops ds1286_ops
= {
320 .ioctl
= ds1286_ioctl
,
322 .read_time
= ds1286_read_time
,
323 .set_time
= ds1286_set_time
,
324 .read_alarm
= ds1286_read_alarm
,
325 .set_alarm
= ds1286_set_alarm
,
326 .alarm_irq_enable
= ds1286_alarm_irq_enable
,
329 static int ds1286_probe(struct platform_device
*pdev
)
331 struct rtc_device
*rtc
;
332 struct resource
*res
;
333 struct ds1286_priv
*priv
;
335 priv
= devm_kzalloc(&pdev
->dev
, sizeof(struct ds1286_priv
), GFP_KERNEL
);
339 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
340 priv
->rtcregs
= devm_ioremap_resource(&pdev
->dev
, res
);
341 if (IS_ERR(priv
->rtcregs
))
342 return PTR_ERR(priv
->rtcregs
);
344 spin_lock_init(&priv
->lock
);
345 platform_set_drvdata(pdev
, priv
);
346 rtc
= devm_rtc_device_register(&pdev
->dev
, "ds1286", &ds1286_ops
,
354 static struct platform_driver ds1286_platform_driver
= {
356 .name
= "rtc-ds1286",
358 .probe
= ds1286_probe
,
361 module_platform_driver(ds1286_platform_driver
);
363 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
364 MODULE_DESCRIPTION("DS1286 RTC driver");
365 MODULE_LICENSE("GPL");
366 MODULE_VERSION(DRV_VERSION
);
367 MODULE_ALIAS("platform:rtc-ds1286");