i2c-viapro: Add new PCI device ID for VX855
[linux-2.6] / 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
22 #define DRV_VERSION             "1.0"
23
24 struct ds1286_priv {
25         struct rtc_device *rtc;
26         u32 __iomem *rtcregs;
27         size_t size;
28         unsigned long baseaddr;
29         spinlock_t lock;
30 };
31
32 static inline u8 ds1286_rtc_read(struct ds1286_priv *priv, int reg)
33 {
34         return __raw_readl(&priv->rtcregs[reg]) & 0xff;
35 }
36
37 static inline void ds1286_rtc_write(struct ds1286_priv *priv, u8 data, int reg)
38 {
39         __raw_writel(data, &priv->rtcregs[reg]);
40 }
41
42 #ifdef CONFIG_RTC_INTF_DEV
43
44 static int ds1286_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
45 {
46         struct ds1286_priv *priv = dev_get_drvdata(dev);
47         unsigned long flags;
48         unsigned char val;
49
50         switch (cmd) {
51         case RTC_AIE_OFF:
52                 /* Mask alarm int. enab. bit    */
53                 spin_lock_irqsave(&priv->lock, flags);
54                 val = ds1286_rtc_read(priv, RTC_CMD);
55                 val |=  RTC_TDM;
56                 ds1286_rtc_write(priv, val, RTC_CMD);
57                 spin_unlock_irqrestore(&priv->lock, flags);
58                 break;
59         case RTC_AIE_ON:
60                 /* Allow alarm interrupts.      */
61                 spin_lock_irqsave(&priv->lock, flags);
62                 val = ds1286_rtc_read(priv, RTC_CMD);
63                 val &=  ~RTC_TDM;
64                 ds1286_rtc_write(priv, val, RTC_CMD);
65                 spin_unlock_irqrestore(&priv->lock, flags);
66                 break;
67         case RTC_WIE_OFF:
68                 /* Mask watchdog int. enab. bit */
69                 spin_lock_irqsave(&priv->lock, flags);
70                 val = ds1286_rtc_read(priv, RTC_CMD);
71                 val |= RTC_WAM;
72                 ds1286_rtc_write(priv, val, RTC_CMD);
73                 spin_unlock_irqrestore(&priv->lock, flags);
74                 break;
75         case RTC_WIE_ON:
76                 /* Allow watchdog interrupts.   */
77                 spin_lock_irqsave(&priv->lock, flags);
78                 val = ds1286_rtc_read(priv, RTC_CMD);
79                 val &= ~RTC_WAM;
80                 ds1286_rtc_write(priv, val, RTC_CMD);
81                 spin_unlock_irqrestore(&priv->lock, flags);
82                 break;
83         default:
84                 return -ENOIOCTLCMD;
85         }
86         return 0;
87 }
88
89 #else
90 #define ds1286_ioctl    NULL
91 #endif
92
93 #ifdef CONFIG_PROC_FS
94
95 static int ds1286_proc(struct device *dev, struct seq_file *seq)
96 {
97         struct ds1286_priv *priv = dev_get_drvdata(dev);
98         unsigned char month, cmd, amode;
99         const char *s;
100
101         month = ds1286_rtc_read(priv, RTC_MONTH);
102         seq_printf(seq,
103                    "oscillator\t: %s\n"
104                    "square_wave\t: %s\n",
105                    (month & RTC_EOSC) ? "disabled" : "enabled",
106                    (month & RTC_ESQW) ? "disabled" : "enabled");
107
108         amode = ((ds1286_rtc_read(priv, RTC_MINUTES_ALARM) & 0x80) >> 5) |
109                 ((ds1286_rtc_read(priv, RTC_HOURS_ALARM) & 0x80) >> 6) |
110                 ((ds1286_rtc_read(priv, RTC_DAY_ALARM) & 0x80) >> 7);
111         switch (amode) {
112         case 7:
113                 s = "each minute";
114                 break;
115         case 3:
116                 s = "minutes match";
117                 break;
118         case 1:
119                 s = "hours and minutes match";
120                 break;
121         case 0:
122                 s = "days, hours and minutes match";
123                 break;
124         default:
125                 s = "invalid";
126                 break;
127         }
128         seq_printf(seq, "alarm_mode\t: %s\n", s);
129
130         cmd = ds1286_rtc_read(priv, RTC_CMD);
131         seq_printf(seq,
132                    "alarm_enable\t: %s\n"
133                    "wdog_alarm\t: %s\n"
134                    "alarm_mask\t: %s\n"
135                    "wdog_alarm_mask\t: %s\n"
136                    "interrupt_mode\t: %s\n"
137                    "INTB_mode\t: %s_active\n"
138                    "interrupt_pins\t: %s\n",
139                    (cmd & RTC_TDF) ? "yes" : "no",
140                    (cmd & RTC_WAF) ? "yes" : "no",
141                    (cmd & RTC_TDM) ? "disabled" : "enabled",
142                    (cmd & RTC_WAM) ? "disabled" : "enabled",
143                    (cmd & RTC_PU_LVL) ? "pulse" : "level",
144                    (cmd & RTC_IBH_LO) ? "low" : "high",
145                    (cmd & RTC_IPSW) ? "unswapped" : "swapped");
146         return 0;
147 }
148
149 #else
150 #define ds1286_proc     NULL
151 #endif
152
153 static int ds1286_read_time(struct device *dev, struct rtc_time *tm)
154 {
155         struct ds1286_priv *priv = dev_get_drvdata(dev);
156         unsigned char save_control;
157         unsigned long flags;
158         unsigned long uip_watchdog = jiffies;
159
160         /*
161          * read RTC once any update in progress is done. The update
162          * can take just over 2ms. We wait 10 to 20ms. There is no need to
163          * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
164          * If you need to know *exactly* when a second has started, enable
165          * periodic update complete interrupts, (via ioctl) and then
166          * immediately read /dev/rtc which will block until you get the IRQ.
167          * Once the read clears, read the RTC time (again via ioctl). Easy.
168          */
169
170         if (ds1286_rtc_read(priv, RTC_CMD) & RTC_TE)
171                 while (time_before(jiffies, uip_watchdog + 2*HZ/100))
172                         barrier();
173
174         /*
175          * Only the values that we read from the RTC are set. We leave
176          * tm_wday, tm_yday and tm_isdst untouched. Even though the
177          * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
178          * by the RTC when initially set to a non-zero value.
179          */
180         spin_lock_irqsave(&priv->lock, flags);
181         save_control = ds1286_rtc_read(priv, RTC_CMD);
182         ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
183
184         tm->tm_sec = ds1286_rtc_read(priv, RTC_SECONDS);
185         tm->tm_min = ds1286_rtc_read(priv, RTC_MINUTES);
186         tm->tm_hour = ds1286_rtc_read(priv, RTC_HOURS) & 0x3f;
187         tm->tm_mday = ds1286_rtc_read(priv, RTC_DATE);
188         tm->tm_mon = ds1286_rtc_read(priv, RTC_MONTH) & 0x1f;
189         tm->tm_year = ds1286_rtc_read(priv, RTC_YEAR);
190
191         ds1286_rtc_write(priv, save_control, RTC_CMD);
192         spin_unlock_irqrestore(&priv->lock, flags);
193
194         tm->tm_sec = bcd2bin(tm->tm_sec);
195         tm->tm_min = bcd2bin(tm->tm_min);
196         tm->tm_hour = bcd2bin(tm->tm_hour);
197         tm->tm_mday = bcd2bin(tm->tm_mday);
198         tm->tm_mon = bcd2bin(tm->tm_mon);
199         tm->tm_year = bcd2bin(tm->tm_year);
200
201         /*
202          * Account for differences between how the RTC uses the values
203          * and how they are defined in a struct rtc_time;
204          */
205         if (tm->tm_year < 45)
206                 tm->tm_year += 30;
207         tm->tm_year += 40;
208         if (tm->tm_year < 70)
209                 tm->tm_year += 100;
210
211         tm->tm_mon--;
212
213         return rtc_valid_tm(tm);
214 }
215
216 static int ds1286_set_time(struct device *dev, struct rtc_time *tm)
217 {
218         struct ds1286_priv *priv = dev_get_drvdata(dev);
219         unsigned char mon, day, hrs, min, sec;
220         unsigned char save_control;
221         unsigned int yrs;
222         unsigned long flags;
223
224         yrs = tm->tm_year + 1900;
225         mon = tm->tm_mon + 1;   /* tm_mon starts at zero */
226         day = tm->tm_mday;
227         hrs = tm->tm_hour;
228         min = tm->tm_min;
229         sec = tm->tm_sec;
230
231         if (yrs < 1970)
232                 return -EINVAL;
233
234         yrs -= 1940;
235         if (yrs > 255)    /* They are unsigned */
236                 return -EINVAL;
237
238         if (yrs >= 100)
239                 yrs -= 100;
240
241         sec = bin2bcd(sec);
242         min = bin2bcd(min);
243         hrs = bin2bcd(hrs);
244         day = bin2bcd(day);
245         mon = bin2bcd(mon);
246         yrs = bin2bcd(yrs);
247
248         spin_lock_irqsave(&priv->lock, flags);
249         save_control = ds1286_rtc_read(priv, RTC_CMD);
250         ds1286_rtc_write(priv, (save_control|RTC_TE), RTC_CMD);
251
252         ds1286_rtc_write(priv, yrs, RTC_YEAR);
253         ds1286_rtc_write(priv, mon, RTC_MONTH);
254         ds1286_rtc_write(priv, day, RTC_DATE);
255         ds1286_rtc_write(priv, hrs, RTC_HOURS);
256         ds1286_rtc_write(priv, min, RTC_MINUTES);
257         ds1286_rtc_write(priv, sec, RTC_SECONDS);
258         ds1286_rtc_write(priv, 0, RTC_HUNDREDTH_SECOND);
259
260         ds1286_rtc_write(priv, save_control, RTC_CMD);
261         spin_unlock_irqrestore(&priv->lock, flags);
262         return 0;
263 }
264
265 static int ds1286_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
266 {
267         struct ds1286_priv *priv = dev_get_drvdata(dev);
268         unsigned char cmd;
269         unsigned long flags;
270
271         /*
272          * Only the values that we read from the RTC are set. That
273          * means only tm_wday, tm_hour, tm_min.
274          */
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         cmd = ds1286_rtc_read(priv, RTC_CMD);
280         spin_unlock_irqrestore(&priv->lock, flags);
281
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;
285         return 0;
286 }
287
288 static int ds1286_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
289 {
290         struct ds1286_priv *priv = dev_get_drvdata(dev);
291         unsigned char hrs, min, sec;
292
293         hrs = alm->time.tm_hour;
294         min = alm->time.tm_min;
295         sec = alm->time.tm_sec;
296
297         if (hrs >= 24)
298                 hrs = 0xff;
299
300         if (min >= 60)
301                 min = 0xff;
302
303         if (sec != 0)
304                 return -EINVAL;
305
306         min = bin2bcd(min);
307         hrs = bin2bcd(hrs);
308
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);
313
314         return 0;
315 }
316
317 static const struct rtc_class_ops ds1286_ops = {
318         .ioctl          = ds1286_ioctl,
319         .proc           = ds1286_proc,
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 };
325
326 static int __devinit ds1286_probe(struct platform_device *pdev)
327 {
328         struct rtc_device *rtc;
329         struct resource *res;
330         struct ds1286_priv *priv;
331         int ret = 0;
332
333         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
334         if (!res)
335                 return -ENODEV;
336         priv = kzalloc(sizeof(struct ds1286_priv), GFP_KERNEL);
337         if (!priv)
338                 return -ENOMEM;
339
340         priv->size = res->end - res->start + 1;
341         if (!request_mem_region(res->start, priv->size, pdev->name)) {
342                 ret = -EBUSY;
343                 goto out;
344         }
345         priv->baseaddr = res->start;
346         priv->rtcregs = ioremap(priv->baseaddr, priv->size);
347         if (!priv->rtcregs) {
348                 ret = -ENOMEM;
349                 goto out;
350         }
351         spin_lock_init(&priv->lock);
352         rtc = rtc_device_register("ds1286", &pdev->dev,
353                                   &ds1286_ops, THIS_MODULE);
354         if (IS_ERR(rtc)) {
355                 ret = PTR_ERR(rtc);
356                 goto out;
357         }
358         priv->rtc = rtc;
359         platform_set_drvdata(pdev, priv);
360         return 0;
361
362 out:
363         if (priv->rtc)
364                 rtc_device_unregister(priv->rtc);
365         if (priv->rtcregs)
366                 iounmap(priv->rtcregs);
367         if (priv->baseaddr)
368                 release_mem_region(priv->baseaddr, priv->size);
369         kfree(priv);
370         return ret;
371 }
372
373 static int __devexit ds1286_remove(struct platform_device *pdev)
374 {
375         struct ds1286_priv *priv = platform_get_drvdata(pdev);
376
377         rtc_device_unregister(priv->rtc);
378         iounmap(priv->rtcregs);
379         release_mem_region(priv->baseaddr, priv->size);
380         kfree(priv);
381         return 0;
382 }
383
384 static struct platform_driver ds1286_platform_driver = {
385         .driver         = {
386                 .name   = "rtc-ds1286",
387                 .owner  = THIS_MODULE,
388         },
389         .probe          = ds1286_probe,
390         .remove         = __devexit_p(ds1286_remove),
391 };
392
393 static int __init ds1286_init(void)
394 {
395         return platform_driver_register(&ds1286_platform_driver);
396 }
397
398 static void __exit ds1286_exit(void)
399 {
400         platform_driver_unregister(&ds1286_platform_driver);
401 }
402
403 MODULE_AUTHOR("Thomas Bogendoerfer <tsbogend@alpha.franken.de>");
404 MODULE_DESCRIPTION("DS1286 RTC driver");
405 MODULE_LICENSE("GPL");
406 MODULE_VERSION(DRV_VERSION);
407 MODULE_ALIAS("platform:rtc-ds1286");
408
409 module_init(ds1286_init);
410 module_exit(ds1286_exit);