blob: 53db3870ba0413eb510fd63d7152717a92668e72 [file] [log] [blame]
Mikael Starvik51533b62005-07-27 11:44:44 -07001/*
2 * PCF8563 RTC
3 *
4 * From Phillips' datasheet:
5 *
6 * The PCF8563 is a CMOS real-time clock/calendar optimized for low power
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02007 * consumption. A programmable clock output, interrupt output and voltage
Mikael Starvik51533b62005-07-27 11:44:44 -07008 * 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 *
Jesper Nilsson7edf7442008-01-24 14:24:09 +010013 * Copyright (c) 2002-2007, Axis Communications AB
Mikael Starvik51533b62005-07-27 11:44:44 -070014 * All rights reserved.
15 *
16 * Author: Tobias Anderberg <tobiasa@axis.com>.
17 *
18 */
19
Mikael Starvik51533b62005-07-27 11:44:44 -070020#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>
Jesper Nilsson7edf7442008-01-24 14:24:09 +010029#include <linux/mutex.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070030
31#include <asm/uaccess.h>
32#include <asm/system.h>
33#include <asm/io.h>
34#include <asm/rtc.h>
35
36#include "i2c.h"
37
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"
Jesper Nilsson7edf7442008-01-24 14:24:09 +010041#define DRIVER_VERSION "$Revision: 1.17 $"
Mikael Starvik51533b62005-07-27 11:44:44 -070042
43/* Two simple wrapper macros, saves a few keystrokes. */
44#define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
45#define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
46
Jesper Nilsson7edf7442008-01-24 14:24:09 +010047static DEFINE_MUTEX(rtc_lock); /* Protect state etc */
48
Mikael Starvik51533b62005-07-27 11:44:44 -070049static const unsigned char days_in_month[] =
50 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
51
52int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
Jesper Nilsson7edf7442008-01-24 14:24:09 +010053
54/* Cache VL bit value read at driver init since writing the RTC_SECOND
55 * register clears the VL status.
56 */
57static int voltage_low;
Mikael Starvik51533b62005-07-27 11:44:44 -070058
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -080059static const struct file_operations pcf8563_fops = {
Robert P. J. Day7ff90572007-07-15 23:38:46 -070060 .owner = THIS_MODULE,
Jesper Nilsson7edf7442008-01-24 14:24:09 +010061 .ioctl = pcf8563_ioctl
Mikael Starvik51533b62005-07-27 11:44:44 -070062};
63
64unsigned char
65pcf8563_readreg(int reg)
66{
67 unsigned char res = rtc_read(reg);
68
Jesper Nilsson7edf7442008-01-24 14:24:09 +010069 /* The PCF8563 does not return 0 for unimplemented bits. */
Mikael Starvik51533b62005-07-27 11:44:44 -070070 switch (reg) {
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_WEEKDAY:
80 res &= 0x07;
81 break;
82 case RTC_MONTH:
83 res &= 0x1F;
84 break;
85 case RTC_CONTROL1:
86 res &= 0xA8;
87 break;
88 case RTC_CONTROL2:
89 res &= 0x1F;
90 break;
91 case RTC_CLOCKOUT_FREQ:
92 case RTC_TIMER_CONTROL:
93 res &= 0x83;
94 break;
95 }
96 return res;
97}
98
99void
100pcf8563_writereg(int reg, unsigned char val)
101{
Mikael Starvik51533b62005-07-27 11:44:44 -0700102 rtc_write(reg, val);
103}
104
105void
106get_rtc_time(struct rtc_time *tm)
107{
108 tm->tm_sec = rtc_read(RTC_SECONDS);
109 tm->tm_min = rtc_read(RTC_MINUTES);
110 tm->tm_hour = rtc_read(RTC_HOURS);
111 tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
112 tm->tm_wday = rtc_read(RTC_WEEKDAY);
113 tm->tm_mon = rtc_read(RTC_MONTH);
114 tm->tm_year = rtc_read(RTC_YEAR);
115
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100116 if (tm->tm_sec & 0x80) {
117 printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
Mikael Starvik51533b62005-07-27 11:44:44 -0700118 "information is no longer guaranteed!\n", PCF8563_NAME);
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100119 }
Mikael Starvik51533b62005-07-27 11:44:44 -0700120
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100121 tm->tm_year = BCD_TO_BIN(tm->tm_year) +
122 ((tm->tm_mon & 0x80) ? 100 : 0);
Mikael Starvik51533b62005-07-27 11:44:44 -0700123 tm->tm_sec &= 0x7F;
124 tm->tm_min &= 0x7F;
125 tm->tm_hour &= 0x3F;
126 tm->tm_mday &= 0x3F;
127 tm->tm_wday &= 0x07; /* Not coded in BCD. */
128 tm->tm_mon &= 0x1F;
129
130 BCD_TO_BIN(tm->tm_sec);
131 BCD_TO_BIN(tm->tm_min);
132 BCD_TO_BIN(tm->tm_hour);
133 BCD_TO_BIN(tm->tm_mday);
134 BCD_TO_BIN(tm->tm_mon);
135 tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
136}
137
138int __init
139pcf8563_init(void)
140{
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100141 static int res;
142 static int first = 1;
143
144 if (!first)
145 return res;
146 first = 0;
147
Mikael Starvik51533b62005-07-27 11:44:44 -0700148 /* Initiate the i2c protocol. */
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100149 res = i2c_init();
150 if (res < 0) {
151 printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
152 return res;
153 }
Mikael Starvik51533b62005-07-27 11:44:44 -0700154
155 /*
156 * First of all we need to reset the chip. This is done by
157 * clearing control1, control2 and clk freq and resetting
158 * all alarms.
159 */
160 if (rtc_write(RTC_CONTROL1, 0x00) < 0)
161 goto err;
162
163 if (rtc_write(RTC_CONTROL2, 0x00) < 0)
164 goto err;
165
166 if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
167 goto err;
168
169 if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
170 goto err;
171
172 /* Reset the alarms. */
173 if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
174 goto err;
175
176 if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
177 goto err;
178
179 if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
180 goto err;
181
182 if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
183 goto err;
184
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100185 /* Check for low voltage, and warn about it. */
186 if (rtc_read(RTC_SECONDS) & 0x80) {
187 voltage_low = 1;
188 printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
189 "date/time information is no longer guaranteed!\n",
190 PCF8563_NAME);
Mikael Starvik51533b62005-07-27 11:44:44 -0700191 }
192
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100193 return res;
Mikael Starvik51533b62005-07-27 11:44:44 -0700194
195err:
196 printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100197 res = -1;
198 return res;
Mikael Starvik51533b62005-07-27 11:44:44 -0700199}
200
201void __exit
202pcf8563_exit(void)
203{
Akinobu Mita68fc4fa2007-07-19 01:47:50 -0700204 unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);
Mikael Starvik51533b62005-07-27 11:44:44 -0700205}
206
207/*
208 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
209 * POSIX says so!
210 */
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100211int pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
212 unsigned long arg)
Mikael Starvik51533b62005-07-27 11:44:44 -0700213{
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) {
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100222 case RTC_RD_TIME:
223 {
224 struct rtc_time tm;
Mikael Starvik51533b62005-07-27 11:44:44 -0700225
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100226 mutex_lock(&rtc_lock);
227 memset(&tm, 0, sizeof tm);
228 get_rtc_time(&tm);
Mikael Starvik51533b62005-07-27 11:44:44 -0700229
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100230 if (copy_to_user((struct rtc_time *) arg, &tm,
231 sizeof tm)) {
232 spin_unlock(&rtc_lock);
233 return -EFAULT;
Mikael Starvik51533b62005-07-27 11:44:44 -0700234 }
235
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100236 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))
Mikael Starvik51533b62005-07-27 11:44:44 -0700249 return -EPERM;
Mikael Starvik51533b62005-07-27 11:44:44 -0700250
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100251 if (copy_from_user(&tm, (struct rtc_time *) arg,
252 sizeof tm))
253 return -EFAULT;
Mikael Starvik51533b62005-07-27 11:44:44 -0700254
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100255 /* Convert from struct tm to struct rtc_time. */
256 tm.tm_year += 1900;
257 tm.tm_mon += 1;
Mikael Starvik51533b62005-07-27 11:44:44 -0700258
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100259 /*
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);
Mikael Starvik51533b62005-07-27 11:44:44 -0700267
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100268 /* 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;
Mikael Starvik51533b62005-07-27 11:44:44 -0700278
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100279 century = (tm.tm_year >= 2000) ? 0x80 : 0;
280 tm.tm_year = tm.tm_year % 100;
Mikael Starvik51533b62005-07-27 11:44:44 -0700281
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100282 BIN_TO_BCD(tm.tm_year);
283 BIN_TO_BCD(tm.tm_mon);
284 BIN_TO_BCD(tm.tm_mday);
285 BIN_TO_BCD(tm.tm_hour);
286 BIN_TO_BCD(tm.tm_min);
287 BIN_TO_BCD(tm.tm_sec);
288 tm.tm_mon |= century;
Mikael Starvik51533b62005-07-27 11:44:44 -0700289
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100290 mutex_lock(&rtc_lock);
Mikael Starvik51533b62005-07-27 11:44:44 -0700291
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100292 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);
Mikael Starvik51533b62005-07-27 11:44:44 -0700299
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100300 mutex_unlock(&rtc_lock);
Mikael Starvik51533b62005-07-27 11:44:44 -0700301
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100302 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);
Mikael Starvik51533b62005-07-27 11:44:44 -0700309
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100310 if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
311 return -EFAULT;
312 return 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700313
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100314 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. */
Mikael Starvik51533b62005-07-27 11:44:44 -0700321
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100322 int ret = rtc_read(RTC_SECONDS);
Mikael Starvik51533b62005-07-27 11:44:44 -0700323
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100324 rtc_write(RTC_SECONDS, (ret & 0x7F));
Mikael Starvik51533b62005-07-27 11:44:44 -0700325
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100326 /* Clear the cached value. */
327 voltage_low = 0;
Mikael Starvik51533b62005-07-27 11:44:44 -0700328
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100329 return 0;
330 }
331 default:
332 return -ENOTTY;
Mikael Starvik51533b62005-07-27 11:44:44 -0700333 }
334
335 return 0;
336}
337
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100338static int __init pcf8563_register(void)
Mikael Starvik51533b62005-07-27 11:44:44 -0700339{
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100340 if (pcf8563_init() < 0) {
341 printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
342 "Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
343 return -1;
344 }
345
346 if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
347 printk(KERN_INFO "%s: Unable to get major numer %d for RTC "
348 "device.\n", PCF8563_NAME, PCF8563_MAJOR);
349 return -1;
350 }
351
352 printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
353 DRIVER_VERSION);
354
355 /* Check for low voltage, and warn about it. */
356 if (voltage_low) {
357 printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
358 "information is no longer guaranteed!\n", PCF8563_NAME);
359 }
360
Mikael Starvik51533b62005-07-27 11:44:44 -0700361 return 0;
362}
363
Jesper Nilsson7edf7442008-01-24 14:24:09 +0100364module_init(pcf8563_register);
Mikael Starvik51533b62005-07-27 11:44:44 -0700365module_exit(pcf8563_exit);