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