ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / arch-v10 / 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 bute.
12  *
13  * Copyright (c) 2002, Axis Communications AB
14  * All rights reserved.
15  *
16  * Author: Tobias Anderberg <tobiasa@axis.com>.
17  *
18  * $Id: pcf8563.c,v 1.1 2002/12/12 08:27:26 starvik Exp $
19  */
20
21 #include <linux/config.h>
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/sched.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/ioctl.h>
30 #include <linux/delay.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <asm/io.h>
35 #include <asm/arch/svinto.h>
36 #include <asm/rtc.h>
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 /* I2C bus slave registers. */
45 #define RTC_I2C_READ            0xa3
46 #define RTC_I2C_WRITE           0xa2
47
48 /* Two simple wrapper macros, saves a few keystrokes. */
49 #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
50 #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
51         
52 static const unsigned char days_in_month[] =
53         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
54
55 int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
56 int pcf8563_open(struct inode *, struct file *);
57 int pcf8563_release(struct inode *, struct file *);
58
59 static struct file_operations pcf8563_fops = {
60         .owner = THIS_MODULE,
61         .ioctl = pcf8563_ioctl,
62         .open = pcf8563_open,
63         .release = pcf8563_release,
64 };
65
66 unsigned char
67 pcf8563_readreg(int reg) 
68 {
69         unsigned char res = i2c_readreg(RTC_I2C_READ, reg);
70
71         /* The PCF8563 does not return 0 for unimplemented bits */
72         switch(reg)
73         {
74                 case RTC_SECONDS:
75                 case RTC_MINUTES:
76                      res &= 0x7f;
77                      break;
78                 case RTC_HOURS:
79                 case RTC_DAY_OF_MONTH:
80                      res &= 0x3f;
81                      break;
82                 case RTC_MONTH:
83                      res = (res & 0x1f) - 1;  /* PCF8563 returns month in range 1-12 */
84                      break;
85         }
86         return res;
87 }
88
89 void
90 pcf8563_writereg(int reg, unsigned char val) 
91 {
92         i2c_writereg(RTC_I2C_WRITE,reg,val);
93 }
94
95 void
96 get_rtc_time(struct rtc_time *tm)
97 {
98         tm->tm_sec = rtc_read(RTC_SECONDS);
99         tm->tm_min = rtc_read(RTC_MINUTES);
100         tm->tm_hour = rtc_read(RTC_HOURS);
101         tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
102         tm->tm_mon = rtc_read(RTC_MONTH);
103         tm->tm_year = rtc_read(RTC_YEAR);
104
105         if (tm->tm_sec & 0x80)
106                 printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!\n", PCF8563_NAME);
107
108         tm->tm_year = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
109         tm->tm_sec &= 0x7f;
110         tm->tm_min &= 0x7f;
111         tm->tm_hour &= 0x3f;
112         tm->tm_mday &= 0x3f;
113         tm->tm_mon &= 0x1f;
114
115         BCD_TO_BIN(tm->tm_sec);
116         BCD_TO_BIN(tm->tm_min);
117         BCD_TO_BIN(tm->tm_hour);
118         BCD_TO_BIN(tm->tm_mday);
119         BCD_TO_BIN(tm->tm_mon);
120         tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
121 }
122
123 int __init
124 pcf8563_init(void)
125 {
126         unsigned char ret;
127         /*
128          * First of all we need to reset the chip. This is done by
129          * clearing control1, control2 and clk freq, clear the 
130          * Voltage Low bit, and resetting all alarms.
131          */
132         if (rtc_write(RTC_CONTROL1, 0x00) < 0)
133                 goto err;
134
135         if (rtc_write(RTC_CONTROL2, 0x00) < 0)
136                 goto err;
137
138         if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
139                 goto err;
140
141         /* Clear the VL bit in the seconds register. */
142         ret = rtc_read(RTC_SECONDS);
143         
144         if (rtc_write(RTC_SECONDS, (ret & 0x7f)) < 0)
145                 goto err;
146                 
147         /* Reset the alarms. */
148         if (rtc_write(RTC_MINUTE_ALARM, 0x00) < 0)
149                 goto err;
150         
151         if (rtc_write(RTC_HOUR_ALARM, 0x00) < 0)
152                 goto err;
153         
154         if (rtc_write(RTC_DAY_ALARM, 0x00) < 0)
155                 goto err;
156         
157         if (rtc_write(RTC_WEEKDAY_ALARM, 0x00) < 0)
158                 goto err;
159
160         if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
161                 printk(KERN_INFO "%s: Unable to get major numer %d for RTC device.\n", 
162                        PCF8563_NAME, PCF8563_MAJOR);
163                 return -1;
164         }
165
166         printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
167         
168         /* Check for low voltage, and warn about it.. */
169         if (rtc_read(RTC_SECONDS) & 0x80)
170                 printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!\n", PCF8563_NAME);
171         
172         return 0;
173
174 err:
175         printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
176         return -1;
177 }
178
179 void __exit
180 pcf8563_exit(void)
181 {
182         if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
183                 printk(KERN_INFO "%s: Unable to unregister device.\n", PCF8563_NAME);
184         }
185 }
186
187 /*
188  * ioctl calls for this driver. Why return -ENOTTY upon error? Because
189  * POSIX says so!
190  */
191 int
192 pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
193 {
194         /* Some sanity checks. */
195         if (_IOC_TYPE(cmd) != RTC_MAGIC)
196                 return -ENOTTY;
197
198         if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
199                 return -ENOTTY;
200
201         switch (cmd) {
202                 case RTC_RD_TIME:
203                         {
204                                 struct rtc_time tm;
205
206                                 get_rtc_time(&tm);
207
208                                 if (copy_to_user((struct rtc_time *) arg, &tm, sizeof(struct rtc_time))) {
209                                         return -EFAULT;
210                                 }
211
212                                 return 0;
213                         }
214                         break;
215                 case RTC_SET_TIME:
216                         {
217                                 int leap;
218                                 int century;
219                                 unsigned long flags;
220                                 struct rtc_time tm;
221
222                                 memset(&tm, 0, sizeof (struct rtc_time));
223                                 if (!capable(CAP_SYS_TIME))
224                                         return -EPERM;
225
226                                 if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof(struct rtc_time)))
227                                         return -EFAULT;
228
229                                 /* Convert from struct tm to struct rtc_time. */
230                                 tm.tm_year += 1900;
231                                 tm.tm_mon += 1;
232                                 
233                                 leap = ((tm.tm_mon == 2) && ((tm.tm_year % 4) == 0)) ? 1 : 0;
234
235                                 /* Perform some sanity checks. */
236                                 if ((tm.tm_year < 1970) ||
237                                     (tm.tm_mon > 12) ||
238                                     (tm.tm_mday == 0) ||
239                                     (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
240                                     (tm.tm_hour >= 24) ||
241                                     (tm.tm_min >= 60) ||
242                                     (tm.tm_sec >= 60))
243                                         return -EINVAL;
244
245                                 century = (tm.tm_year >= 2000) ? 0x80 : 0;
246                                 tm.tm_year = tm.tm_year % 100;
247
248                                 BIN_TO_BCD(tm.tm_year);
249                                 BIN_TO_BCD(tm.tm_mday);
250                                 BIN_TO_BCD(tm.tm_hour);
251                                 BIN_TO_BCD(tm.tm_min);
252                                 BIN_TO_BCD(tm.tm_sec);
253                                 tm.tm_mon |= century;
254                                 
255                                 rtc_write(RTC_YEAR, tm.tm_year);
256                                 rtc_write(RTC_MONTH, tm.tm_mon);
257                                 rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
258                                 rtc_write(RTC_HOURS, tm.tm_hour);
259                                 rtc_write(RTC_MINUTES, tm.tm_min);
260                                 rtc_write(RTC_SECONDS, tm.tm_sec);
261
262                                 return 0;
263                         }
264                         break;
265                 default:
266                                 return -ENOTTY;
267         }
268
269         return 0;
270 }
271
272 int 
273 pcf8563_open(struct inode *inode, struct file *filp)
274 {
275         MOD_INC_USE_COUNT;
276         return 0;
277 }
278
279 int
280 pcf8563_release(struct inode *inode, struct file *filp)
281 {
282         MOD_DEC_USE_COUNT;
283         return 0;
284 }
285
286 module_init(pcf8563_init);
287 module_exit(pcf8563_exit);