blob: af517c210383478c14c0a09e607e6d26a06a8a6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCF8563 RTC
3 *
4 * From Phillips' datasheet:
5 *
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 bute.
12 *
13 * Copyright (c) 2002, Axis Communications AB
14 * All rights reserved.
15 *
16 * Author: Tobias Anderberg <tobiasa@axis.com>.
17 *
Mikael Starvik7e920422005-07-27 11:44:34 -070018 * $Id: pcf8563.c,v 1.11 2005/03/07 13:13:07 starvik Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
21#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/types.h>
25#include <linux/sched.h>
26#include <linux/init.h>
27#include <linux/fs.h>
28#include <linux/ioctl.h>
29#include <linux/delay.h>
30#include <linux/bcd.h>
Randy Dunlapa9415642006-01-11 12:17:48 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/uaccess.h>
34#include <asm/system.h>
35#include <asm/io.h>
36#include <asm/arch/svinto.h>
37#include <asm/rtc.h>
38#include "i2c.h"
39
40#define PCF8563_MAJOR 121 /* Local major number. */
41#define DEVICE_NAME "rtc" /* Name which is registered in /proc/devices. */
42#define PCF8563_NAME "PCF8563"
Mikael Starvik7e920422005-07-27 11:44:34 -070043#define DRIVER_VERSION "$Revision: 1.11 $"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* I2C bus slave registers. */
46#define RTC_I2C_READ 0xa3
47#define RTC_I2C_WRITE 0xa2
48
49/* Two simple wrapper macros, saves a few keystrokes. */
50#define rtc_read(x) i2c_readreg(RTC_I2C_READ, x)
51#define rtc_write(x,y) i2c_writereg(RTC_I2C_WRITE, x, y)
Mikael Starvik7e920422005-07-27 11:44:34 -070052
53static DEFINE_SPINLOCK(rtc_lock); /* Protect state etc */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55static const unsigned char days_in_month[] =
56 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
57
58int pcf8563_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
59
60static struct file_operations pcf8563_fops = {
61 .owner = THIS_MODULE,
62 .ioctl = pcf8563_ioctl,
63};
64
65unsigned char
66pcf8563_readreg(int reg)
67{
68 unsigned char res = i2c_readreg(RTC_I2C_READ, reg);
69
70 /* The PCF8563 does not return 0 for unimplemented bits */
71 switch(reg)
72 {
73 case RTC_SECONDS:
74 case RTC_MINUTES:
75 res &= 0x7f;
76 break;
77 case RTC_HOURS:
78 case RTC_DAY_OF_MONTH:
79 res &= 0x3f;
80 break;
81 case RTC_MONTH:
82 res = (res & 0x1f) - 1; /* PCF8563 returns month in range 1-12 */
83 break;
84 }
85 return res;
86}
87
88void
89pcf8563_writereg(int reg, unsigned char val)
90{
91#ifdef CONFIG_ETRAX_RTC_READONLY
92 if (reg == RTC_CONTROL1 || (reg >= RTC_SECONDS && reg <= RTC_YEAR))
93 return;
94#endif
95
96 rtc_write(reg, val);
97}
98
99void
100get_rtc_time(struct rtc_time *tm)
101{
102 tm->tm_sec = rtc_read(RTC_SECONDS);
103 tm->tm_min = rtc_read(RTC_MINUTES);
104 tm->tm_hour = rtc_read(RTC_HOURS);
105 tm->tm_mday = rtc_read(RTC_DAY_OF_MONTH);
106 tm->tm_mon = rtc_read(RTC_MONTH);
107 tm->tm_year = rtc_read(RTC_YEAR);
108
109 if (tm->tm_sec & 0x80)
110 printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!\n", PCF8563_NAME);
111
112 tm->tm_year = BCD_TO_BIN(tm->tm_year) + ((tm->tm_mon & 0x80) ? 100 : 0);
113 tm->tm_sec &= 0x7f;
114 tm->tm_min &= 0x7f;
115 tm->tm_hour &= 0x3f;
116 tm->tm_mday &= 0x3f;
117 tm->tm_mon &= 0x1f;
118
119 BCD_TO_BIN(tm->tm_sec);
120 BCD_TO_BIN(tm->tm_min);
121 BCD_TO_BIN(tm->tm_hour);
122 BCD_TO_BIN(tm->tm_mday);
123 BCD_TO_BIN(tm->tm_mon);
124 tm->tm_mon--; /* Month is 1..12 in RTC but 0..11 in linux */
125}
126
127int __init
128pcf8563_init(void)
129{
Mikael Starvik7e920422005-07-27 11:44:34 -0700130 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Mikael Starvik7e920422005-07-27 11:44:34 -0700132 if ((ret = i2c_init())) {
133 printk(KERN_CRIT "pcf8563_init: failed to init i2c\n");
134 return ret;
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /*
138 * First of all we need to reset the chip. This is done by
139 * clearing control1, control2 and clk freq, clear the
140 * Voltage Low bit, and resetting all alarms.
141 */
142 if (rtc_write(RTC_CONTROL1, 0x00) < 0)
143 goto err;
144
145 if (rtc_write(RTC_CONTROL2, 0x00) < 0)
146 goto err;
147
148 if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
149 goto err;
150
151 /* Clear the VL bit in the seconds register. */
152 ret = rtc_read(RTC_SECONDS);
153
154 if (rtc_write(RTC_SECONDS, (ret & 0x7f)) < 0)
155 goto err;
156
157 /* Reset the alarms. */
158 if (rtc_write(RTC_MINUTE_ALARM, 0x00) < 0)
159 goto err;
160
161 if (rtc_write(RTC_HOUR_ALARM, 0x00) < 0)
162 goto err;
163
164 if (rtc_write(RTC_DAY_ALARM, 0x00) < 0)
165 goto err;
166
167 if (rtc_write(RTC_WEEKDAY_ALARM, 0x00) < 0)
168 goto err;
169
170 /* Check for low voltage, and warn about it.. */
171 if (rtc_read(RTC_SECONDS) & 0x80)
172 printk(KERN_WARNING "%s: RTC Low Voltage - date/time is not reliable!\n", PCF8563_NAME);
173
174 return 0;
175
176err:
177 printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
178 return -1;
179}
180
181void __exit
182pcf8563_exit(void)
183{
184 if (unregister_chrdev(PCF8563_MAJOR, DEVICE_NAME) < 0) {
185 printk(KERN_INFO "%s: Unable to unregister device.\n", PCF8563_NAME);
186 }
187}
188
189/*
190 * ioctl calls for this driver. Why return -ENOTTY upon error? Because
191 * POSIX says so!
192 */
193int
194pcf8563_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
195{
196 /* Some sanity checks. */
197 if (_IOC_TYPE(cmd) != RTC_MAGIC)
198 return -ENOTTY;
199
200 if (_IOC_NR(cmd) > RTC_MAX_IOCTL)
201 return -ENOTTY;
202
203 switch (cmd) {
204 case RTC_RD_TIME:
205 {
206 struct rtc_time tm;
207
Mikael Starvik7e920422005-07-27 11:44:34 -0700208 spin_lock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 get_rtc_time(&tm);
210
211 if (copy_to_user((struct rtc_time *) arg, &tm, sizeof(struct rtc_time))) {
Mikael Starvik7e920422005-07-27 11:44:34 -0700212 spin_unlock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -EFAULT;
214 }
215
Mikael Starvik7e920422005-07-27 11:44:34 -0700216 spin_unlock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218 }
219 break;
220 case RTC_SET_TIME:
221 {
222#ifdef CONFIG_ETRAX_RTC_READONLY
223 return -EPERM;
224#else
225 int leap;
226 int century;
227 struct rtc_time tm;
228
229 memset(&tm, 0, sizeof (struct rtc_time));
230 if (!capable(CAP_SYS_TIME))
231 return -EPERM;
232
233 if (copy_from_user(&tm, (struct rtc_time *) arg, sizeof(struct rtc_time)))
234 return -EFAULT;
235
236 /* Convert from struct tm to struct rtc_time. */
237 tm.tm_year += 1900;
238 tm.tm_mon += 1;
239
240 leap = ((tm.tm_mon == 2) && ((tm.tm_year % 4) == 0)) ? 1 : 0;
241
242 /* Perform some sanity checks. */
243 if ((tm.tm_year < 1970) ||
244 (tm.tm_mon > 12) ||
245 (tm.tm_mday == 0) ||
246 (tm.tm_mday > days_in_month[tm.tm_mon] + leap) ||
247 (tm.tm_hour >= 24) ||
248 (tm.tm_min >= 60) ||
249 (tm.tm_sec >= 60))
250 return -EINVAL;
251
252 century = (tm.tm_year >= 2000) ? 0x80 : 0;
253 tm.tm_year = tm.tm_year % 100;
254
255 BIN_TO_BCD(tm.tm_year);
256 BIN_TO_BCD(tm.tm_mday);
257 BIN_TO_BCD(tm.tm_hour);
258 BIN_TO_BCD(tm.tm_min);
259 BIN_TO_BCD(tm.tm_sec);
260 tm.tm_mon |= century;
Mikael Starvik7e920422005-07-27 11:44:34 -0700261
262 spin_lock(&rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 rtc_write(RTC_YEAR, tm.tm_year);
265 rtc_write(RTC_MONTH, tm.tm_mon);
266 rtc_write(RTC_DAY_OF_MONTH, tm.tm_mday);
267 rtc_write(RTC_HOURS, tm.tm_hour);
268 rtc_write(RTC_MINUTES, tm.tm_min);
269 rtc_write(RTC_SECONDS, tm.tm_sec);
270
Mikael Starvik7e920422005-07-27 11:44:34 -0700271 spin_unlock(&rtc_lock);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274#endif /* !CONFIG_ETRAX_RTC_READONLY */
275 }
276
277 case RTC_VLOW_RD:
278 {
279 int vl_bit = 0;
280
281 if (rtc_read(RTC_SECONDS) & 0x80) {
282 vl_bit = 1;
283 printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
284 "date/time information is no longer guaranteed!\n",
285 PCF8563_NAME);
286 }
287 if (copy_to_user((int *) arg, &vl_bit, sizeof(int)))
288 return -EFAULT;
289
290 return 0;
291 }
292
293 case RTC_VLOW_SET:
294 {
295 /* Clear the VL bit in the seconds register */
296 int ret = rtc_read(RTC_SECONDS);
297
298 rtc_write(RTC_SECONDS, (ret & 0x7F));
299
300 return 0;
301 }
302
303 default:
304 return -ENOTTY;
305 }
306
307 return 0;
308}
309
310static int __init
311pcf8563_register(void)
312{
313 pcf8563_init();
314 if (register_chrdev(PCF8563_MAJOR, DEVICE_NAME, &pcf8563_fops) < 0) {
315 printk(KERN_INFO "%s: Unable to get major numer %d for RTC device.\n",
316 PCF8563_NAME, PCF8563_MAJOR);
317 return -1;
318 }
319
320 printk(KERN_INFO "%s Real-Time Clock Driver, %s\n", PCF8563_NAME, DRIVER_VERSION);
321 return 0;
322}
323
324module_init(pcf8563_register);
325module_exit(pcf8563_exit);