]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/rtc/rtc-ds1286.c
Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[mv-sheeva.git] / drivers / rtc / rtc-ds1286.c
1 /*
2  * DS1286 Real Time Clock interface for Linux
3  *
4  * Copyright (C) 1998, 1999, 2000 Ralf Baechle
5  * Copyright (C) 2008 Thomas Bogendoerfer
6  *
7  * Based on code written by Paul Gortmaker.
8  *
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.
13  */
14
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>
20 #include <linux/io.h>
21 #include <linux/slab.h>
22
23 #define DRV_VERSION             "1.0"
24
25 struct ds1286_priv {
26         struct rtc_device *rtc;
27         u32 __iomem *rtcregs;
28         size_t size;
29         unsigned long baseaddr;
30         spinlock_t lock;
31 };
32
33 static inline u8 ds1286_rtc_read(struct ds1286_priv *priv, int reg)
34 {
35         return __raw_readl(&priv->rtcregs[reg]) & 0xff;
36 }
37
38 static inline void ds1286_rtc_write(struct ds1286_priv *priv, u8 data, int reg)
39 {
40         __raw_writel(data, &priv->rtcregs[reg]);
41 }
42
43 #ifdef CONFIG_RTC_INTF_DEV
44
45 static int ds1286_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
46 {
47         struct ds1286_priv *priv = dev_get_drvdata(dev);
48         unsigned long flags;
49         unsigned char val;
50
51         switch (cmd) {
52         case RTC_AIE_OFF:
53                 /* Mask alarm int. enab. bit    */
54                 spin_lock_irqsave(&priv->lock, flags);
55                 val = ds1286_rtc_read(priv, RTC_CMD);
56                 val |=  RTC_TDM;
57                 ds1286_rtc_write(priv, val, RTC_CMD);
58                 spin_unlock_irqrestore(&priv->lock, flags);
59                 break;
60         case RTC_AIE_ON:
61                 /* Allow alarm interrupts.      */
62                 spin_lock_irqsave(&priv->lock, flags);
63                 val = ds1286_rtc_read(priv, RTC_CMD);
64                 val &=  ~RTC_TDM;
65                 ds1286_rtc_write(priv, val, RTC_CMD);
66                 spin_unlock_irqrestore(&priv->lock, flags);
67                 break;
68         case RTC_WIE_OFF:
69                 /* Mask watchdog int. enab. bit */
70                 spin_lock_irqsave(&priv->lock, flags);
71                 val = ds1286_rtc_read(priv, RTC_CMD);
72                 val |= RTC_WAM;
73                 ds1286_rtc_write(priv, val, RTC_CMD);
74                 spin_unlock_irqrestore(&priv->lock, flags);
75                 break;
76         case RTC_WIE_ON:
77                 /* Allow watchdog interrupts.   */
78                 spin_lock_irqsave(&priv->lock, flags);
79                 val = ds1286_rtc_read(priv, RTC_CMD);
80                 val &= ~RTC_WAM;
81                 ds1286_rtc_write(priv, val, RTC_CMD);
82                 spin_unlock_irqrestore(&priv->lock, flags);
83                 break;
84         default:
85                 return -ENOIOCTLCMD;
86         }
87         return 0;
88 }
89
90 #else
91 #define ds1286_ioctl    NULL
92 #endif
93
94 #ifdef CONFIG_PROC_FS
95
96 static int ds1286_proc(struct device *dev, struct seq_file *seq)
97 {
98         struct ds1286_priv *priv = dev_get_drvdata(dev);
99         unsigned char month, cmd, amode;
100         const char *s;
101
102         month = ds1286_rtc_read(priv, RTC_MONTH);
103         seq_printf(seq,
104                    "oscillator\t: %s\n"
105                    "square_wave\t: %s\n",
106                    (month & RTC_EOSC) ? "disabled" : "enabled",
107                    (month & RTC_ESQW) ? "disabled" : "enabled");
108
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);
112         switch (amode) {
113         case 7:
114                 s = "each minute";
115                 break;
116         case 3:
117                 s = "minutes match";
118                 break;
119         case 1:
120                 s = "hours and minutes match";
121                 break;
122         case 0:
123                 s = "days, hours and minutes match";
124                 break;
125         default:
126                 s = "invalid";
127                 break;
128         }
129         seq_printf(seq, "alarm_mode\t: %s\n", s);
130
131         cmd = ds1286_rtc_read(priv, RTC_CMD);
132         seq_printf(seq,
133                    "alarm_enable\t: %s\n"
134                    "wdog_alarm\t: %s\n"
135                    "alarm_mask\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");
147         return 0;
148 }
149
150 #else
151 #define ds1286_proc     NULL
152 #endif
153
154 static int ds1286_read_time(struct device *dev, struct rtc_time *tm)
155 {
156         struct ds1286_priv *priv = dev_get_drvdata(dev);
157         unsigned char save_control;
158         unsigned long flags;
159         unsigned long uip_watchdog = jiffies;
160
161         /*
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.
169          */
170
171         if (ds1286_rtc_read(priv, RTC_CMD) & RTC_TE)
172                 while (time_before(jiffies, uip_watchdog + 2*HZ/100))
173                         barrier();
174
175         /*
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.
180          */
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);
184
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);
191
192         ds1286_rtc_write(priv, save_control, RTC_CMD);
193         spin_unlock_irqrestore(&priv->lock, flags);
194
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);
201
202         /*
203          * Account for differences between how the RTC uses the values
204          * and how they are defined in a struct rtc_time;
205          */
206         if (tm->tm_year < 45)
207                 tm->tm_year += 30;
208         tm->tm_year += 40;
209         if (tm->tm_year < 70)
210                 tm->tm_year += 100;
211
212         tm->tm_mon--;
213
214         return rtc_valid_tm(tm);
215 }
216
217 static int ds1286_set_time(struct device *dev, struct rtc_time *tm)
218 {
219         struct ds1286_priv *priv = dev_get_drvdata(dev);
220         unsigned char mon, day, hrs, min, sec;
221         unsigned char save_control;
222         unsigned int yrs;
223         unsigned long flags;
224
225         yrs = tm->tm_year + 1900;
226         mon = tm->tm_mon + 1;   /* tm_mon starts at zero */
227         day = tm->tm_mday;
228         hrs = tm->tm_hour;
229         min = tm->tm_min;
230         sec = tm->tm_sec;
231
232         if (yrs < 1970)
233                 return -EINVAL;
234
235         yrs -= 1940;
236         if (yrs > 255)    /* They are unsigned */
237                 return -EINVAL;
238
239         if (yrs >= 100)
240                 yrs -= 100;
241
242         sec = bin2bcd(sec);
243         min = bin2bcd(min);
244         hrs = bin2bcd(hrs);
245         day = bin2bcd(day);
246         mon = bin2bcd(mon);
247         yrs = bin2bcd(yrs);
248
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);
252
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);
260
261         ds1286_rtc_write(priv, save_control, RTC_CMD);
262         spin_unlock_irqrestore(&priv->lock, flags);
263         return 0;
264 }
265
266 static int ds1286_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
267 {
268         struct ds1286_priv *priv = dev_get_drvdata(dev);
269         unsigned char cmd;
270         unsigned long flags;
271
272         /*
273          * Only the values that we read from the RTC are set. That
274          * means only tm_wday, tm_hour, tm_min.
275          */
276         spin_lock_irqsave(&priv->lock, flags);
277         alm->time.tm_min = ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x7f;
278         alm->time.tm_hour = ds1286_rtc_read(priv, RTC_HOURS_ALARM)  & 0x1f;
279         alm->time.tm_wday = ds1286_rtc_read(priv, RTC_DAY_ALARM)    & 0x07;
280         cmd = ds1286_rtc_read(priv, RTC_CMD);
281         spin_unlock_irqrestore(&priv->lock, flags);
282
283         alm->time.tm_min = bcd2bin(alm->time.tm_min);
284         alm->time.tm_hour = bcd2bin(alm->time.tm_hour);
285         alm->time.tm_sec = 0;
286         return 0;
287 }
288
289 static int ds1286_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
290 {
291         struct ds1286_priv *priv = dev_get_drvdata(dev);
292         unsigned char hrs, min, sec;
293
294         hrs = alm->time.tm_hour;
295         min = alm->time.tm_min;
296         sec = alm->time.tm_sec;
297
298         if (hrs >= 24)
299                 hrs = 0xff;
300
301         if (min >= 60)
302                 min = 0xff;
303
304         if (sec != 0)
305                 return -EINVAL;
306
307         min = bin2bcd(min);
308         hrs = bin2bcd(hrs);
309
310         spin_lock(&priv->lock);
311         ds1286_rtc_write(priv, hrs, RTC_HOURS_ALARM);
312         ds1286_rtc_write(priv, min, RTC_MINUTES_ALARM);
313         spin_unlock(&priv->lock);
314
315         return 0;
316 }
317
318 static const struct rtc_class_ops ds1286_ops = {
319         .ioctl          = ds1286_ioctl,
320         .proc           = ds1286_proc,
321         .read_time      = ds1286_read_time,
322         .set_time       = ds1286_set_time,
323         .read_alarm     = ds1286_read_alarm,
324         .set_alarm      = ds1286_set_alarm,
325 };
326
327 static int __devinit ds1286_probe(struct platform_device *pdev)
328 {
329         struct rtc_device *rtc;
330         struct resource *res;
331         struct ds1286_priv *priv;
332         int ret = 0;
333
334         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
335         if (!res)
336                 return -ENODEV;
337         priv = kzalloc(sizeof(struct ds1286_priv), GFP_KERNEL);
338         if (!priv)
339                 return -ENOMEM;
340
341         priv->size = res->end - res->start + 1;
342         if (!request_mem_region(res->start, priv->size, pdev->name)) {
343                 ret = -EBUSY;
344                 goto out;
345         }
346         priv->baseaddr = res->start;
347         priv->rtcregs = ioremap(priv->baseaddr, priv->size);
348         if (!priv->rtcregs) {
349                 ret = -ENOMEM;
350                 goto out;
351         }
352         spin_lock_init(&priv->lock);
353         rtc = rtc_device_register("ds1286", &pdev->dev,
354                                   &ds1286_ops, THIS_MODULE);
355         if (IS_ERR(rtc)) {
356                 ret = PTR_ERR(rtc);
357                 goto out;
358         }
359         priv->rtc = rtc;
360         platform_set_drvdata(pdev, priv);
361         return 0;
362
363 out:
364         if (priv->rtc)
365                 rtc_device_unregister(priv->rtc);
366         if (priv->rtcregs)
367                 iounmap(priv->rtcregs);
368         if (priv->baseaddr)
369                 release_mem_region(priv->baseaddr, priv->size);
370         kfree(priv);
371         return ret;
372 }
373
374 static int __devexit ds1286_remove(struct platform_device *pdev)
375 {
376         struct ds1286_priv *priv = platform_get_drvdata(pdev);
377
378         rtc_device_unregister(priv->rtc);
379         iounmap(priv->rtcregs);
380         release_mem_region(priv->baseaddr, priv->size);
381         kfree(priv);
382         return 0;
383 }
384
385 static struct platform_driver ds1286_platform_driver = {
386         .driver         = {
387                 .name   = "rtc-ds1286",
388                 .owner  = THIS_MODULE,
389         },
390         .probe          = ds1286_probe,
391         .remove         = __devexit_p(ds1286_remove),
392 };
393
394 static int __init ds1286_init(void)
395 {
396         return platform_driver_register(&ds1286_platform_driver);
397 }
398
399 static void __exit ds1286_exit(void)
400 {
401         platform_driver_unregister(&ds1286_platform_driver);
402 }
403
404 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
405 MODULE_DESCRIPTION("DS1286 RTC driver");
406 MODULE_LICENSE("GPL");
407 MODULE_VERSION(DRV_VERSION);
408 MODULE_ALIAS("platform:rtc-ds1286");
409
410 module_init(ds1286_init);
411 module_exit(ds1286_exit);