]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/cris/arch-v32/drivers/pcf8563.c
cris: push down BKL into some device drivers
[net-next-2.6.git] / arch / cris / arch-v32 / drivers / pcf8563.c
CommitLineData
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 *
7edf7440 13 * Copyright (c) 2002-2007, Axis Communications AB
51533b61
MS
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>
f35d7764 27#include <linux/smp_lock.h>
51533b61
MS
28#include <linux/delay.h>
29#include <linux/bcd.h>
7edf7440 30#include <linux/mutex.h>
51533b61
MS
31
32#include <asm/uaccess.h>
33#include <asm/system.h>
34#include <asm/io.h>
35#include <asm/rtc.h>
36
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"
7edf7440 42#define DRIVER_VERSION "$Revision: 1.17 $"
51533b61
MS
43
44/* Two simple wrapper macros, saves a few keystrokes. */
45#define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
46#define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
47
7edf7440
JN
48static DEFINE_MUTEX(rtc_lock); /* Protect state etc */
49
51533b61
MS
50static const unsigned char days_in_month[] =
51 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
52
f35d7764 53static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
7edf7440
JN
54
55/* Cache VL bit value read at driver init since writing the RTC_SECOND
56 * register clears the VL status.
57 */
58static int voltage_low;
51533b61 59
5dfe4c96 60static const struct file_operations pcf8563_fops = {
f35d7764
AB
61 .owner = THIS_MODULE,
62 .unlocked_ioctl = pcf8563_unlocked_ioctl,
51533b61
MS
63};
64
65unsigned char
66pcf8563_readreg(int reg)
67{
68 unsigned char res = rtc_read(reg);
69
7edf7440 70 /* The PCF8563 does not return 0 for unimplemented bits. */
51533b61
MS
71 switch (reg) {
72 case RTC_SECONDS:
73 case RTC_MINUTES:
74 res &= 0x7F;
75 break;
76 case RTC_HOURS:
77 case RTC_DAY_OF_MONTH:
78 res &= 0x3F;
79 break;
80 case RTC_WEEKDAY:
81 res &= 0x07;
82 break;
83 case RTC_MONTH:
84 res &= 0x1F;
85 break;
86 case RTC_CONTROL1:
87 res &= 0xA8;
88 break;
89 case RTC_CONTROL2:
90 res &= 0x1F;
91 break;
92 case RTC_CLOCKOUT_FREQ:
93 case RTC_TIMER_CONTROL:
94 res &= 0x83;
95 break;
96 }
97 return res;
98}
99
100void
101pcf8563_writereg(int reg, unsigned char val)
102{
51533b61
MS
103 rtc_write(reg, val);
104}
105
106void
107get_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
7edf7440
JN
117 if (tm->tm_sec & 0x80) {
118 printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
51533b61 119 "information is no longer guaranteed!\n", PCF8563_NAME);
7edf7440 120 }
51533b61 121
4110a0d6 122 tm->tm_year = bcd2bin(tm->tm_year) +
7edf7440 123 ((tm->tm_mon & 0x80) ? 100 : 0);
51533b61
MS
124 tm->tm_sec &= 0x7F;
125 tm->tm_min &= 0x7F;
126 tm->tm_hour &= 0x3F;
127 tm->tm_mday &= 0x3F;
128 tm->tm_wday &= 0x07; /* Not coded in BCD. */
129 tm->tm_mon &= 0x1F;
130
4110a0d6
AB
131 tm->tm_sec = bcd2bin(tm->tm_sec);
132 tm->tm_min = bcd2bin(tm->tm_min);
133 tm->tm_hour = bcd2bin(tm->tm_hour);
134 tm->tm_mday = bcd2bin(tm->tm_mday);
135 tm->tm_mon = bcd2bin(tm->tm_mon);
51533b61
MS
136 tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
137}
138
139int __init
140pcf8563_init(void)
141{
7edf7440
JN
142 static int res;
143 static int first = 1;
144
145 if (!first)
146 return res;
147 first = 0;
148
51533b61 149 /* Initiate the i2c protocol. */
7edf7440
JN
150 res = i2c_init();
151 if (res < 0) {
152 printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
153 return res;
154 }
51533b61
MS
155
156 /*
157 * First of all we need to reset the chip. This is done by
158 * clearing control1, control2 and clk freq and resetting
159 * all alarms.
160 */
161 if (rtc_write(RTC_CONTROL1, 0x00) < 0)
162 goto err;
163
164 if (rtc_write(RTC_CONTROL2, 0x00) < 0)
165 goto err;
166
167 if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
168 goto err;
169
170 if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
171 goto err;
172
173 /* Reset the alarms. */
174 if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
175 goto err;
176
177 if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
178 goto err;
179
180 if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
181 goto err;
182
183 if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
184 goto err;
185
7edf7440
JN
186 /* Check for low voltage, and warn about it. */
187 if (rtc_read(RTC_SECONDS) & 0x80) {
188 voltage_low = 1;
189 printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
190 "date/time information is no longer guaranteed!\n",
191 PCF8563_NAME);
51533b61
MS
192 }
193
7edf7440 194 return res;
51533b61
MS
195
196err:
197 printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
7edf7440
JN
198 res = -1;
199 return res;
51533b61
MS
200}
201
202void __exit
203pcf8563_exit(void)
204{
68fc4fab 205 unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);
51533b61
MS
206}
207
208/*
209 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
210 * POSIX says so!
211 */
f35d7764 212static int pcf8563_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
51533b61
MS
213{
214 /* Some sanity checks. */
215 if (_IOC_TYPE(cmd) != RTC_MAGIC)
216 return -ENOTTY;
217
218 if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
219 return -ENOTTY;
220
221 switch (cmd) {
7edf7440
JN
222 case RTC_RD_TIME:
223 {
224 struct rtc_time tm;
225
226 mutex_lock(&rtc_lock);
227 memset(&tm, 0, sizeof tm);
228 get_rtc_time(&tm);
229
230 if (copy_to_user((struct rtc_time *) arg, &tm,
231 sizeof tm)) {
9be48a94 232 mutex_unlock(&rtc_lock);
7edf7440 233 return -EFAULT;
51533b61
MS
234 }
235
7edf7440
JN
236 mutex_unlock(&rtc_lock);
237
238 return 0;
239 }
240 case RTC_SET_TIME:
241 {
242 int leap;
243 int year;
244 int century;
245 struct rtc_time tm;
246
247 memset(&tm, 0, sizeof tm);
248 if (!capable(CAP_SYS_TIME))
51533b61 249 return -EPERM;
51533b61 250
7edf7440
JN
251 if (copy_from_user(&tm, (struct rtc_time *) arg,
252 sizeof tm))
253 return -EFAULT;
254
255 /* Convert from struct tm to struct rtc_time. */
256 tm.tm_year += 1900;
257 tm.tm_mon += 1;
258
259 /*
260 * Check if tm.tm_year is a leap year. A year is a leap
261 * year if it is divisible by 4 but not 100, except
262 * that years divisible by 400 _are_ leap years.
263 */
264 year = tm.tm_year;
265 leap = (tm.tm_mon == 2) &&
266 ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
267
268 /* Perform some sanity checks. */
269 if ((tm.tm_year < 1970) ||
270 (tm.tm_mon > 12) ||
271 (tm.tm_mday == 0) ||
272 (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
273 (tm.tm_wday >= 7) ||
274 (tm.tm_hour >= 24) ||
275 (tm.tm_min >= 60) ||
276 (tm.tm_sec >= 60))
277 return -EINVAL;
278
279 century = (tm.tm_year >= 2000) ? 0x80 : 0;
280 tm.tm_year = tm.tm_year % 100;
281
4110a0d6
AB
282 tm.tm_year = bin2bcd(tm.tm_year);
283 tm.tm_mon = bin2bcd(tm.tm_mon);
284 tm.tm_mday = bin2bcd(tm.tm_mday);
285 tm.tm_hour = bin2bcd(tm.tm_hour);
286 tm.tm_min = bin2bcd(tm.tm_min);
287 tm.tm_sec = bin2bcd(tm.tm_sec);
7edf7440
JN
288 tm.tm_mon |= century;
289
290 mutex_lock(&rtc_lock);
291
292 rtc_write(RTC_YEAR, tm.tm_year);
293 rtc_write(RTC_MONTH, tm.tm_mon);
294 rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
295 rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
296 rtc_write(RTC_HOURS, tm.tm_hour);
297 rtc_write(RTC_MINUTES, tm.tm_min);
298 rtc_write(RTC_SECONDS, tm.tm_sec);
299
300 mutex_unlock(&rtc_lock);
301
302 return 0;
303 }
304 case RTC_VL_READ:
305 if (voltage_low)
306 printk(KERN_ERR "%s: RTC Voltage Low - "
307 "reliable date/time information is no "
308 "longer guaranteed!\n", PCF8563_NAME);
51533b61 309
7edf7440
JN
310 if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
311 return -EFAULT;
312 return 0;
51533b61 313
7edf7440
JN
314 case RTC_VL_CLR:
315 {
316 /* Clear the VL bit in the seconds register in case
317 * the time has not been set already (which would
318 * have cleared it). This does not really matter
319 * because of the cached voltage_low value but do it
320 * anyway for consistency. */
51533b61 321
7edf7440 322 int ret = rtc_read(RTC_SECONDS);
51533b61 323
7edf7440 324 rtc_write(RTC_SECONDS, (ret & 0x7F));
51533b61 325
7edf7440
JN
326 /* Clear the cached value. */
327 voltage_low = 0;
51533b61 328
7edf7440
JN
329 return 0;
330 }
331 default:
332 return -ENOTTY;
51533b61
MS
333 }
334
335 return 0;
336}
337
f35d7764
AB
338static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
339{
340 int ret;
341
342 lock_kernel();
343 return pcf8563_ioctl(filp, cmd, arg);
344 unlock_kernel();
345
346 return ret;
347}
348
7edf7440 349static int __init pcf8563_register(void)
51533b61 350{
7edf7440
JN
351 if (pcf8563_init() < 0) {
352 printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
353 "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
354 return -1;
355 }
356
357 if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
358 printk(KERN_INFO "%s: Unable to get major numer %d for RTC "
359 "device.\n", PCF8563_NAME, PCF8563_MAJOR);
360 return -1;
361 }
362
363 printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
364 DRIVER_VERSION);
365
366 /* Check for low voltage, and warn about it. */
367 if (voltage_low) {
368 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
369 "information is no longer guaranteed!\n", PCF8563_NAME);
370 }
51533b61 371
51533b61
MS
372 return 0;
373}
374
7edf7440 375module_init(pcf8563_register);
51533b61 376module_exit(pcf8563_exit);