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