Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / cris / arch-v10 / drivers / pcf8563.c
blob52103d16dc6c0e2b185a8700f8d4263a7683d122
1 /*
2 * PCF8563 RTC
4 * From Phillips' datasheet:
6 * The PCF8563 is a CMOS real-time clock/calendar optimized for low power
7 * consumption. A programmable clock output, interrupt 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.
13 * Copyright (c) 2002-2007, Axis Communications AB
14 * All rights reserved.
16 * Author: Tobias Anderberg <tobiasa@axis.com>.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/sched.h>
24 #include <linux/init.h>
25 #include <linux/fs.h>
26 #include <linux/ioctl.h>
27 #include <linux/delay.h>
28 #include <linux/bcd.h>
29 #include <linux/mutex.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <asm/io.h>
34 #include <asm/rtc.h>
36 #include "i2c.h"
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.24 $"
43 /* I2C bus slave registers. */
44 #define RTC_I2C_READ 0xa3
45 #define RTC_I2C_WRITE 0xa2
47 /* Two simple wrapper macros, saves a few keystrokes. */
48 #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
49 #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
51 static DEFINE_MUTEX(rtc_lock); /* Protect state etc */
53 static const unsigned char days_in_month[] =
54 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
56 int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
58 /* Cache VL bit value read at driver init since writing the RTC_SECOND
59 * register clears the VL status.
61 static int voltage_low;
63 static const struct file_operations pcf8563_fops = {
64 .owner = THIS_MODULE,
65 .ioctl = pcf8563_ioctl,
68 unsigned char
69 pcf8563_readreg(int reg)
71 unsigned char res = rtc_read(reg);
73 /* The PCF8563 does not return 0 for unimplemented bits. */
74 switch (reg) {
75 case RTC_SECONDS:
76 case RTC_MINUTES:
77 res &= 0x7F;
78 break;
79 case RTC_HOURS:
80 case RTC_DAY_OF_MONTH:
81 res &= 0x3F;
82 break;
83 case RTC_WEEKDAY:
84 res &= 0x07;
85 break;
86 case RTC_MONTH:
87 res &= 0x1F;
88 break;
89 case RTC_CONTROL1:
90 res &= 0xA8;
91 break;
92 case RTC_CONTROL2:
93 res &= 0x1F;
94 break;
95 case RTC_CLOCKOUT_FREQ:
96 case RTC_TIMER_CONTROL:
97 res &= 0x83;
98 break;
100 return res;
103 void
104 pcf8563_writereg(int reg, unsigned char val)
106 rtc_write(reg, val);
109 void
110 get_rtc_time(struct rtc_time *tm)
112 tm->tm_sec = rtc_read(RTC_SECONDS);
113 tm->tm_min = rtc_read(RTC_MINUTES);
114 tm->tm_hour = rtc_read(RTC_HOURS);
115 tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
116 tm->tm_wday = rtc_read(RTC_WEEKDAY);
117 tm->tm_mon = rtc_read(RTC_MONTH);
118 tm->tm_year = rtc_read(RTC_YEAR);
120 if (tm->tm_sec & 0x80) {
121 printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
122 "information is no longer guaranteed!\n", PCF8563_NAME);
125 tm->tm_year = BCD_TO_BIN(tm->tm_year) +
126 ((tm->tm_mon & 0x80) ? 100 : 0);
127 tm->tm_sec &= 0x7F;
128 tm->tm_min &= 0x7F;
129 tm->tm_hour &= 0x3F;
130 tm->tm_mday &= 0x3F;
131 tm->tm_wday &= 0x07; /* Not coded in BCD. */
132 tm->tm_mon &= 0x1F;
134 BCD_TO_BIN(tm->tm_sec);
135 BCD_TO_BIN(tm->tm_min);
136 BCD_TO_BIN(tm->tm_hour);
137 BCD_TO_BIN(tm->tm_mday);
138 BCD_TO_BIN(tm->tm_mon);
139 tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
142 int __init
143 pcf8563_init(void)
145 static int res;
146 static int first = 1;
148 if (!first)
149 return res;
150 first = 0;
152 /* Initiate the i2c protocol. */
153 res = i2c_init();
154 if (res < 0) {
155 printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
156 return res;
160 * First of all we need to reset the chip. This is done by
161 * clearing control1, control2 and clk freq and resetting
162 * all alarms.
164 if (rtc_write(RTC_CONTROL1, 0x00) < 0)
165 goto err;
167 if (rtc_write(RTC_CONTROL2, 0x00) < 0)
168 goto err;
170 if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
171 goto err;
173 if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
174 goto err;
176 /* Reset the alarms. */
177 if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
178 goto err;
180 if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
181 goto err;
183 if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
184 goto err;
186 if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
187 goto err;
189 /* Check for low voltage, and warn about it. */
190 if (rtc_read(RTC_SECONDS) & 0x80) {
191 voltage_low = 1;
192 printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
193 "date/time information is no longer guaranteed!\n",
194 PCF8563_NAME);
197 return res;
199 err:
200 printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
201 res = -1;
202 return res;
205 void __exit
206 pcf8563_exit(void)
208 unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);
212 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
213 * POSIX says so!
215 int pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
216 unsigned long arg)
218 /* Some sanity checks. */
219 if (_IOC_TYPE(cmd) != RTC_MAGIC)
220 return -ENOTTY;
222 if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
223 return -ENOTTY;
225 switch (cmd) {
226 case RTC_RD_TIME:
228 struct rtc_time tm;
230 mutex_lock(&rtc_lock);
231 memset(&tm, 0, sizeof tm);
232 get_rtc_time(&tm);
234 if (copy_to_user((struct rtc_time *) arg, &tm,
235 sizeof tm)) {
236 spin_unlock(&rtc_lock);
237 return -EFAULT;
240 mutex_unlock(&rtc_lock);
242 return 0;
244 case RTC_SET_TIME:
246 int leap;
247 int year;
248 int century;
249 struct rtc_time tm;
251 memset(&tm, 0, sizeof tm);
252 if (!capable(CAP_SYS_TIME))
253 return -EPERM;
255 if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof tm))
256 return -EFAULT;
258 /* Convert from struct tm to struct rtc_time. */
259 tm.tm_year += 1900;
260 tm.tm_mon += 1;
263 * Check if tm.tm_year is a leap year. A year is a leap
264 * year if it is divisible by 4 but not 100, except
265 * that years divisible by 400 _are_ leap years.
267 year = tm.tm_year;
268 leap = (tm.tm_mon == 2) &&
269 ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
271 /* Perform some sanity checks. */
272 if ((tm.tm_year < 1970) ||
273 (tm.tm_mon > 12) ||
274 (tm.tm_mday == 0) ||
275 (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
276 (tm.tm_wday >= 7) ||
277 (tm.tm_hour >= 24) ||
278 (tm.tm_min >= 60) ||
279 (tm.tm_sec >= 60))
280 return -EINVAL;
282 century = (tm.tm_year >= 2000) ? 0x80 : 0;
283 tm.tm_year = tm.tm_year % 100;
285 BIN_TO_BCD(tm.tm_year);
286 BIN_TO_BCD(tm.tm_mon);
287 BIN_TO_BCD(tm.tm_mday);
288 BIN_TO_BCD(tm.tm_hour);
289 BIN_TO_BCD(tm.tm_min);
290 BIN_TO_BCD(tm.tm_sec);
291 tm.tm_mon |= century;
293 mutex_lock(&rtc_lock);
295 rtc_write(RTC_YEAR, tm.tm_year);
296 rtc_write(RTC_MONTH, tm.tm_mon);
297 rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
298 rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
299 rtc_write(RTC_HOURS, tm.tm_hour);
300 rtc_write(RTC_MINUTES, tm.tm_min);
301 rtc_write(RTC_SECONDS, tm.tm_sec);
303 mutex_unlock(&rtc_lock);
305 return 0;
307 case RTC_VL_READ:
308 if (voltage_low) {
309 printk(KERN_ERR "%s: RTC Voltage Low - "
310 "reliable date/time information is no "
311 "longer guaranteed!\n", PCF8563_NAME);
314 if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
315 return -EFAULT;
316 return 0;
318 case RTC_VL_CLR:
320 /* Clear the VL bit in the seconds register in case
321 * the time has not been set already (which would
322 * have cleared it). This does not really matter
323 * because of the cached voltage_low value but do it
324 * anyway for consistency. */
326 int ret = rtc_read(RTC_SECONDS);
328 rtc_write(RTC_SECONDS, (ret & 0x7F));
330 /* Clear the cached value. */
331 voltage_low = 0;
333 return 0;
335 default:
336 return -ENOTTY;
339 return 0;
342 static int __init pcf8563_register(void)
344 if (pcf8563_init() < 0) {
345 printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
346 "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
347 return -1;
350 if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
351 printk(KERN_INFO "%s: Unable to get major number %d for RTC device.\n",
352 PCF8563_NAME, PCF8563_MAJOR);
353 return -1;
356 printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
357 DRIVER_VERSION);
359 /* Check for low voltage, and warn about it. */
360 if (voltage_low) {
361 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
362 "information is no longer guaranteed!\n", PCF8563_NAME);
365 return 0;
368 module_init(pcf8563_register);
369 module_exit(pcf8563_exit);