4 * From Phillips' datasheet:
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.
13 * Copyright (c) 2002-2003, 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>
26 #include <linux/ioctl.h>
27 #include <linux/delay.h>
28 #include <linux/bcd.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
37 #define PCF8563_MAJOR 121 /* Local major number. */
38 #define DEVICE_NAME "rtc" /* Name which is registered in /proc/devices. */
39 #define PCF8563_NAME "PCF8563"
40 #define DRIVER_VERSION "$Revision: 1.1 $"
42 /* Two simple wrapper macros, saves a few keystrokes. */
43 #define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
44 #define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
46 static const unsigned char days_in_month[] =
47 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
49 int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
50 int pcf8563_open(struct inode *, struct file *);
51 int pcf8563_release(struct inode *, struct file *);
53 static const struct file_operations pcf8563_fops = {
57 release: pcf8563_release,
61 pcf8563_readreg(int reg)
63 unsigned char res = rtc_read(reg);
65 /* The PCF8563 does not return 0 for unimplemented bits */
72 case RTC_DAY_OF_MONTH:
87 case RTC_CLOCKOUT_FREQ:
88 case RTC_TIMER_CONTROL:
96 pcf8563_writereg(int reg, unsigned char val)
98 #ifdef CONFIG_ETRAX_RTC_READONLY
99 if (reg == RTC_CONTROL1 || (reg >= RTC_SECONDS && reg <= RTC_YEAR))
107 get_rtc_time(struct rtc_time *tm)
109 tm->tm_sec = rtc_read(RTC_SECONDS);
110 tm->tm_min = rtc_read(RTC_MINUTES);
111 tm->tm_hour = rtc_read(RTC_HOURS);
112 tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
113 tm->tm_wday = rtc_read(RTC_WEEKDAY);
114 tm->tm_mon = rtc_read(RTC_MONTH);
115 tm->tm_year = rtc_read(RTC_YEAR);
117 if (tm->tm_sec & 0x80)
118 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
119 "information is no longer guaranteed!\n", PCF8563_NAME);
121 tm->tm_year = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
126 tm->tm_wday &= 0x07; /* Not coded in BCD. */
129 BCD_TO_BIN(tm->tm_sec);
130 BCD_TO_BIN(tm->tm_min);
131 BCD_TO_BIN(tm->tm_hour);
132 BCD_TO_BIN(tm->tm_mday);
133 BCD_TO_BIN(tm->tm_mon);
134 tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
140 /* Initiate the i2c protocol. */
144 * First of all we need to reset the chip. This is done by
145 * clearing control1, control2 and clk freq and resetting
148 if (rtc_write(RTC_CONTROL1, 0x00) < 0)
151 if (rtc_write(RTC_CONTROL2, 0x00) < 0)
154 if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
157 if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
160 /* Reset the alarms. */
161 if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
164 if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
167 if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
170 if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
173 if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
174 printk(KERN_INFO "%s: Unable to get major number %d for RTC device.\n",
175 PCF8563_NAME, PCF8563_MAJOR);
179 printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
181 /* Check for low voltage, and warn about it.. */
182 if (rtc_read(RTC_SECONDS) & 0x80)
183 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
184 "information is no longer guaranteed!\n", PCF8563_NAME);
189 printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
196 if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
197 printk(KERN_INFO "%s: Unable to unregister device.\n", PCF8563_NAME);
202 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
206 pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
208 /* Some sanity checks. */
209 if (_IOC_TYPE(cmd) != RTC_MAGIC)
212 if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
220 memset(&tm, 0, sizeof (struct rtc_time));
223 if (copy_to_user((struct rtc_time *) arg, &tm, sizeof tm)) {
232 #ifdef CONFIG_ETRAX_RTC_READONLY
240 if (!capable(CAP_SYS_TIME))
243 if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof tm))
246 /* Convert from struct tm to struct rtc_time. */
251 * Check if tm.tm_year is a leap year. A year is a leap
252 * year if it is divisible by 4 but not 100, except
253 * that years divisible by 400 _are_ leap years.
256 leap = (tm.tm_mon == 2) && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
258 /* Perform some sanity checks. */
259 if ((tm.tm_year < 1970) ||
262 (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
264 (tm.tm_hour >= 24) ||
269 century = (tm.tm_year >= 2000) ? 0x80 : 0;
270 tm.tm_year = tm.tm_year % 100;
272 BIN_TO_BCD(tm.tm_year);
273 BIN_TO_BCD(tm.tm_mday);
274 BIN_TO_BCD(tm.tm_hour);
275 BIN_TO_BCD(tm.tm_min);
276 BIN_TO_BCD(tm.tm_sec);
277 tm.tm_mon |= century;
279 rtc_write(RTC_YEAR, tm.tm_year);
280 rtc_write(RTC_MONTH, tm.tm_mon);
281 rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
282 rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
283 rtc_write(RTC_HOURS, tm.tm_hour);
284 rtc_write(RTC_MINUTES, tm.tm_min);
285 rtc_write(RTC_SECONDS, tm.tm_sec);
288 #endif /* !CONFIG_ETRAX_RTC_READONLY */
295 if (rtc_read(RTC_SECONDS) & 0x80) {
297 printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
298 "date/time information is no longer guaranteed!\n",
301 if (copy_to_user((int *) arg, &vl_bit, sizeof(int)))
309 /* Clear the VL bit in the seconds register */
310 int ret = rtc_read(RTC_SECONDS);
312 rtc_write(RTC_SECONDS, (ret & 0x7F));
325 pcf8563_open(struct inode *inode, struct file *filp)
331 pcf8563_release(struct inode *inode, struct file *filp)
336 module_init(pcf8563_init);
337 module_exit(pcf8563_exit);