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>
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>
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.17 $"
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)
47
static DEFINE_MUTEX(pcf8563_mutex);
48
static DEFINE_MUTEX(rtc_lock); /* Protect state etc */
50
static const unsigned char days_in_month[] =
51
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
53
static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
55
/* Cache VL bit value read at driver init since writing the RTC_SECOND
56
* register clears the VL status.
58
static int voltage_low;
60
static const struct file_operations pcf8563_fops = {
62
.unlocked_ioctl = pcf8563_unlocked_ioctl,
63
.llseek = noop_llseek,
67
pcf8563_readreg(int reg)
69
unsigned char res = rtc_read(reg);
71
/* The PCF8563 does not return 0 for unimplemented bits. */
78
case RTC_DAY_OF_MONTH:
93
case RTC_CLOCKOUT_FREQ:
94
case RTC_TIMER_CONTROL:
102
pcf8563_writereg(int reg, unsigned char val)
108
get_rtc_time(struct rtc_time *tm)
110
tm->tm_sec = rtc_read(RTC_SECONDS);
111
tm->tm_min = rtc_read(RTC_MINUTES);
112
tm->tm_hour = rtc_read(RTC_HOURS);
113
tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
114
tm->tm_wday = rtc_read(RTC_WEEKDAY);
115
tm->tm_mon = rtc_read(RTC_MONTH);
116
tm->tm_year = rtc_read(RTC_YEAR);
118
if (tm->tm_sec & 0x80) {
119
printk(KERN_ERR "%s: RTC Voltage Low - reliable date/time "
120
"information is no longer guaranteed!\n", PCF8563_NAME);
123
tm->tm_year = bcd2bin(tm->tm_year) +
124
((tm->tm_mon & 0x80) ? 100 : 0);
129
tm->tm_wday &= 0x07; /* Not coded in BCD. */
132
tm->tm_sec = bcd2bin(tm->tm_sec);
133
tm->tm_min = bcd2bin(tm->tm_min);
134
tm->tm_hour = bcd2bin(tm->tm_hour);
135
tm->tm_mday = bcd2bin(tm->tm_mday);
136
tm->tm_mon = bcd2bin(tm->tm_mon);
137
tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
144
static int first = 1;
150
/* Initiate the i2c protocol. */
153
printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
158
* First of all we need to reset the chip. This is done by
159
* clearing control1, control2 and clk freq and resetting
162
if (rtc_write(RTC_CONTROL1, 0x00) < 0)
165
if (rtc_write(RTC_CONTROL2, 0x00) < 0)
168
if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
171
if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
174
/* Reset the alarms. */
175
if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
178
if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
181
if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
184
if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
187
/* Check for low voltage, and warn about it. */
188
if (rtc_read(RTC_SECONDS) & 0x80) {
190
printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
191
"date/time information is no longer guaranteed!\n",
198
printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
206
unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME);
210
* ioctl calls for this driver. Why return -ENOTTY upon error? Because
213
static int pcf8563_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
215
/* Some sanity checks. */
216
if (_IOC_TYPE(cmd) != RTC_MAGIC)
219
if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
227
mutex_lock(&rtc_lock);
228
memset(&tm, 0, sizeof tm);
231
if (copy_to_user((struct rtc_time *) arg, &tm,
233
mutex_unlock(&rtc_lock);
237
mutex_unlock(&rtc_lock);
248
memset(&tm, 0, sizeof tm);
249
if (!capable(CAP_SYS_TIME))
252
if (copy_from_user(&tm, (struct rtc_time *) arg,
256
/* Convert from struct tm to struct rtc_time. */
261
* Check if tm.tm_year is a leap year. A year is a leap
262
* year if it is divisible by 4 but not 100, except
263
* that years divisible by 400 _are_ leap years.
266
leap = (tm.tm_mon == 2) &&
267
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
269
/* Perform some sanity checks. */
270
if ((tm.tm_year < 1970) ||
273
(tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
275
(tm.tm_hour >= 24) ||
280
century = (tm.tm_year >= 2000) ? 0x80 : 0;
281
tm.tm_year = tm.tm_year % 100;
283
tm.tm_year = bin2bcd(tm.tm_year);
284
tm.tm_mon = bin2bcd(tm.tm_mon);
285
tm.tm_mday = bin2bcd(tm.tm_mday);
286
tm.tm_hour = bin2bcd(tm.tm_hour);
287
tm.tm_min = bin2bcd(tm.tm_min);
288
tm.tm_sec = bin2bcd(tm.tm_sec);
289
tm.tm_mon |= century;
291
mutex_lock(&rtc_lock);
293
rtc_write(RTC_YEAR, tm.tm_year);
294
rtc_write(RTC_MONTH, tm.tm_mon);
295
rtc_write(RTC_WEEKDAY, tm.tm_wday); /* Not coded in BCD. */
296
rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
297
rtc_write(RTC_HOURS, tm.tm_hour);
298
rtc_write(RTC_MINUTES, tm.tm_min);
299
rtc_write(RTC_SECONDS, tm.tm_sec);
301
mutex_unlock(&rtc_lock);
307
printk(KERN_ERR "%s: RTC Voltage Low - "
308
"reliable date/time information is no "
309
"longer guaranteed!\n", PCF8563_NAME);
311
if (copy_to_user((int *) arg, &voltage_low, sizeof(int)))
317
/* Clear the VL bit in the seconds register in case
318
* the time has not been set already (which would
319
* have cleared it). This does not really matter
320
* because of the cached voltage_low value but do it
321
* anyway for consistency. */
323
int ret = rtc_read(RTC_SECONDS);
325
rtc_write(RTC_SECONDS, (ret & 0x7F));
327
/* Clear the cached value. */
339
static long pcf8563_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
343
mutex_lock(&pcf8563_mutex);
344
return pcf8563_ioctl(filp, cmd, arg);
345
mutex_unlock(&pcf8563_mutex);
350
static int __init pcf8563_register(void)
352
if (pcf8563_init() < 0) {
353
printk(KERN_INFO "%s: Unable to initialize Real-Time Clock "
354
"Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
358
if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
359
printk(KERN_INFO "%s: Unable to get major numer %d for RTC "
360
"device.\n", PCF8563_NAME, PCF8563_MAJOR);
364
printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME,
367
/* Check for low voltage, and warn about it. */
369
printk(KERN_WARNING "%s: RTC Voltage Low - reliable date/time "
370
"information is no longer guaranteed!\n", PCF8563_NAME);
376
module_init(pcf8563_register);
377
module_exit(pcf8563_exit);