This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / cris / arch-v32 / drivers / pcf8563.c
1 /*
2  * PCF8563 RTC
3  *
4  * From Phillips' datasheet:
5  *
6  * The PCF8563 is a CMOS real-time clock/calendar optimized for low power
7  * consumption. A programmable clock output, interupt output and voltage
8  * low detector are also provided. All address and data are transferred
9  * serially via two-line bidirectional I2C-bus. Maximum bus speed is
10  * 400 kbits/s. The built-in word address register is incremented
11  * automatically after each written or read byte.
12  *
13  * Copyright (c) 2002-2003, Axis Communications AB
14  * All rights reserved.
15  *
16  * Author: Tobias Anderberg <tobiasa@axis.com>.
17  *
18  */
19
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/sched.h>
25 #include <linux/init.h>
26 #include <linux/fs.h>
27 #include <linux/ioctl.h>
28 #include <linux/delay.h>
29 #include <linux/bcd.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <asm/io.h>
34 #include <asm/rtc.h>
35
36 #include "i2c.h"
37
38 #define PCF8563_MAJOR   121     /* Local major number. */
39 #define DEVICE_NAME     "rtc"   /* Name which is registered in /proc/devices. */
40 #define PCF8563_NAME    "PCF8563"
41 #define DRIVER_VERSION  "$Revision: 1.1 $"
42
43 /* Two simple wrapper macros, saves a few keystrokes. */
44 #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
45 #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
46
47 static const unsigned char days_in_month[] =
48         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
49
50 int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
51 int pcf8563_open(struct inode *, struct file *);
52 int pcf8563_release(struct inode *, struct file *);
53
54 static struct file_operations pcf8563_fops = {
55         owner: THIS_MODULE,
56         ioctl: pcf8563_ioctl,
57         open: pcf8563_open,
58         release: pcf8563_release,
59 };
60
61 unsigned char
62 pcf8563_readreg(int reg)
63 {
64         unsigned char res = rtc_read(reg);
65
66         /* The PCF8563 does not return 0 for unimplemented bits */
67         switch (reg) {
68                 case RTC_SECONDS:
69                 case RTC_MINUTES:
70                         res &= 0x7F;
71                         break;
72                 case RTC_HOURS:
73                 case RTC_DAY_OF_MONTH:
74                         res &= 0x3F;
75                         break;
76                 case RTC_WEEKDAY:
77                         res &= 0x07;
78                         break;
79                 case RTC_MONTH:
80                         res &= 0x1F;
81                         break;
82                 case RTC_CONTROL1:
83                         res &= 0xA8;
84                         break;
85                 case RTC_CONTROL2:
86                         res &= 0x1F;
87                         break;
88                 case RTC_CLOCKOUT_FREQ:
89                 case RTC_TIMER_CONTROL:
90                         res &= 0x83;
91                         break;
92         }
93         return res;
94 }
95
96 void
97 pcf8563_writereg(int reg, unsigned char val)
98 {
99 #ifdef CONFIG_ETRAX_RTC_READONLY
100         if (reg == RTC_CONTROL1 || (reg >= RTC_SECONDS && reg <= RTC_YEAR))
101                 return;
102 #endif
103
104         rtc_write(reg, val);
105 }
106
107 void
108 get_rtc_time(struct rtc_time *tm)
109 {
110         tm->tm_sec  = rtc_read(RTC_SECONDS);
111         tm->tm_min  = rtc_read(RTC_MINUTES);
112         tm->tm_hour = rtc_read(RTC_HOURS);
113         tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
114         tm->tm_wday = rtc_read(RTC_WEEKDAY);
115         tm->tm_mon  = rtc_read(RTC_MONTH);
116         tm->tm_year = rtc_read(RTC_YEAR);
117
118         if (tm->tm_sec & 0x80)
119                 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
120                        "information is no longer guaranteed!\n", PCF8563_NAME);
121
122         tm->tm_year  = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
123         tm->tm_sec  &= 0x7F;
124         tm->tm_min  &= 0x7F;
125         tm->tm_hour &= 0x3F;
126         tm->tm_mday &= 0x3F;
127         tm->tm_wday &= 0x07; /* Not coded in BCD. */
128         tm->tm_mon  &= 0x1F;
129
130         BCD_TO_BIN(tm->tm_sec);
131         BCD_TO_BIN(tm->tm_min);
132         BCD_TO_BIN(tm->tm_hour);
133         BCD_TO_BIN(tm->tm_mday);
134         BCD_TO_BIN(tm->tm_mon);
135         tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
136 }
137
138 int __init
139 pcf8563_init(void)
140 {
141         /* Initiate the i2c protocol. */
142         i2c_init();
143
144         /*
145          * First of all we need to reset the chip. This is done by
146          * clearing control1, control2 and clk freq and resetting
147          * all alarms.
148          */
149         if (rtc_write(RTC_CONTROL1, 0x00) < 0)
150                 goto err;
151
152         if (rtc_write(RTC_CONTROL2, 0x00) < 0)
153                 goto err;
154
155         if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
156                 goto err;
157
158         if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
159                 goto err;
160
161         /* Reset the alarms. */
162         if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
163                 goto err;
164
165         if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
166                 goto err;
167
168         if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
169                 goto err;
170
171         if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
172                 goto err;
173
174         if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
175                 printk(KERN_INFO "%s: Unable to get major numer %d for RTC device.\n",
176                        PCF8563_NAME, PCF8563_MAJOR);
177                 return -1;
178         }
179
180         printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
181
182         /* Check for low voltage, and warn about it.. */
183         if (rtc_read(RTC_SECONDS) & 0x80)
184                 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
185                        "information is no longer guaranteed!\n", PCF8563_NAME);
186
187         return 0;
188
189 err:
190         printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
191         return -1;
192 }
193
194 void __exit
195 pcf8563_exit(void)
196 {
197         if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
198                 printk(KERN_INFO "%s: Unable to unregister device.\n", PCF8563_NAME);
199         }
200 }
201
202 /*
203  * ioctl calls for this driver. Why return -ENOTTY upon error? Because
204  * POSIX says so!
205  */
206 int
207 pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
208 {
209         /* Some sanity checks. */
210         if (_IOC_TYPE(cmd) != RTC_MAGIC)
211                 return -ENOTTY;
212
213         if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
214                 return -ENOTTY;
215
216         switch (cmd) {
217                 case RTC_RD_TIME:
218                 {
219                         struct rtc_time tm;
220
221                         memset(&tm, 0, sizeof (struct rtc_time));
222                         get_rtc_time(&tm);
223
224                         if (copy_to_user((struct rtc_time *) arg, &tm, sizeof tm)) {
225                                 return -EFAULT;
226                         }
227
228                         return 0;
229                 }
230
231                 case RTC_SET_TIME:
232                 {
233 #ifdef CONFIG_ETRAX_RTC_READONLY
234                         return -EPERM;
235 #else
236                         int leap;
237                         int year;
238                         int century;
239                         struct rtc_time tm;
240
241                         if (!capable(CAP_SYS_TIME))
242                                 return -EPERM;
243
244                         if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof tm))
245                                 return -EFAULT;
246
247                         /* Convert from struct tm to struct rtc_time. */
248                         tm.tm_year += 1900;
249                         tm.tm_mon += 1;
250
251                         /*
252                          * Check if tm.tm_year is a leap year. A year is a leap
253                          * year if it is divisible by 4 but not 100, except
254                          * that years divisible by 400 _are_ leap years.
255                          */
256                         year = tm.tm_year;
257                         leap = (tm.tm_mon == 2) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
258
259                         /* Perform some sanity checks. */
260                         if ((tm.tm_year < 1970) ||
261                             (tm.tm_mon > 12) ||
262                             (tm.tm_mday == 0) ||
263                             (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
264                             (tm.tm_wday >= 7) ||
265                             (tm.tm_hour >= 24) ||
266                             (tm.tm_min >= 60) ||
267                             (tm.tm_sec >= 60))
268                                 return -EINVAL;
269
270                         century = (tm.tm_year >= 2000) ? 0x80 : 0;
271                         tm.tm_year = tm.tm_year % 100;
272
273                         BIN_TO_BCD(tm.tm_year);
274                         BIN_TO_BCD(tm.tm_mday);
275                         BIN_TO_BCD(tm.tm_hour);
276                         BIN_TO_BCD(tm.tm_min);
277                         BIN_TO_BCD(tm.tm_sec);
278                         tm.tm_mon |= century;
279
280                         rtc_write(RTC_YEAR, tm.tm_year);
281                         rtc_write(RTC_MONTH, tm.tm_mon);
282                         rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
283                         rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
284                         rtc_write(RTC_HOURS, tm.tm_hour);
285                         rtc_write(RTC_MINUTES, tm.tm_min);
286                         rtc_write(RTC_SECONDS, tm.tm_sec);
287
288                         return 0;
289 #endif /* !CONFIG_ETRAX_RTC_READONLY */
290                 }
291
292                 case RTC_VLOW_RD:
293                 {
294                         int vl_bit = 0;
295
296                         if (rtc_read(RTC_SECONDS) & 0x80) {
297                                 vl_bit = 1;
298                                 printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
299                                        "date/time information is no longer guaranteed!\n",
300                                        PCF8563_NAME);
301                         }
302                         if (copy_to_user((int *) arg, &vl_bit, sizeof(int)))
303                                 return -EFAULT;
304
305                         return 0;
306                 }
307
308                 case RTC_VLOW_SET:
309                 {
310                         /* Clear the VL bit in the seconds register */
311                         int ret = rtc_read(RTC_SECONDS);
312
313                         rtc_write(RTC_SECONDS, (ret & 0x7F));
314
315                         return 0;
316                 }
317
318                 default:
319                         return -ENOTTY;
320         }
321
322         return 0;
323 }
324
325 int
326 pcf8563_open(struct inode *inode, struct file *filp)
327 {
328         MOD_INC_USE_COUNT;
329         return 0;
330 }
331
332 int
333 pcf8563_release(struct inode *inode, struct file *filp)
334 {
335         MOD_DEC_USE_COUNT;
336         return 0;
337 }
338
339 module_init(pcf8563_init);
340 module_exit(pcf8563_exit);