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