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