2 * Real Time Clock driver for WL-HDD
4 * Copyright (C) 2007 Andreas Engel
6 * Hacked together mostly by copying the relevant code parts from:
7 * drivers/i2c/i2c-bcm5365.c
8 * drivers/i2c/i2c-algo-bit.c
12 * This module uses the standard char device (10,135), while the Asus module
13 * rtcdrv.o uses (12,0). So, both can coexist which might be handy during
14 * development (but see the comment in rtc_open()).
17 * You might need to set the clock once after loading the driver the first
18 * time because the driver switches the chip into 24h mode if it is running
22 * For compatibility reasons with the original asus driver, the time can be
23 * read and set via the /dev/rtc device entry. The only accepted data format
24 * is "YYYY:MM:DD:W:HH:MM:SS\n". See OpenWrt wiki for a script which handles
27 * In addition, this driver supports the standard ioctl() calls for setting
28 * and reading the hardware clock, so the ordinary hwclock utility can also
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
37 * - add a /proc/driver/rtc interface?
38 * - make the battery failure bit available through the /proc interface?
40 * $Id: rtc.c 7 2007-05-25 19:37:01Z ae $
43 #include <linux/module.h>
44 #include <linux/kmod.h>
45 #include <linux/kernel.h>
46 #include <linux/types.h>
47 #include <linux/miscdevice.h>
48 #include <linux/ioport.h>
49 #include <linux/fcntl.h>
50 #include <linux/mc146818rtc.h>
51 #include <linux/init.h>
52 #include <linux/spinlock.h>
53 #include <linux/rtc.h>
54 #include <linux/delay.h>
55 #include <linux/version.h>
56 #include <linux/gpio.h>
57 #include <linux/uaccess.h>
59 #include <asm/current.h>
60 #include <asm/system.h>
65 #define RTC_IS_OPEN 0x01 /* Means /dev/rtc is in use. */
67 /* Can be changed via a module parameter. */
68 static int rtc_debug
= 0;
70 static unsigned long rtc_status
= 0; /* Bitmapped status byte. */
72 /* These settings are platform dependents. */
73 unsigned int sda_index
= 0;
74 unsigned int scl_index
= 0;
76 #define I2C_READ_MASK 1
77 #define I2C_WRITE_MASK 0
82 #define RTC_EPOCH 1900
83 #define RTC_I2C_ADDRESS (0x32 << 1)
84 #define RTC_24HOUR_MODE_MASK 0x20
85 #define RTC_PM_MASK 0x20
86 #define RTC_VDET_MASK 0x40
87 #define RTC_Y2K_MASK 0x80
90 * Delay in microseconds for generating the pulses on the I2C bus. We use
91 * a rather conservative setting here. See datasheet of the RTC chip.
95 /* Avoid spurious compiler warnings. */
96 #define UNUSED __attribute__((unused))
98 MODULE_AUTHOR("Andreas Engel");
99 MODULE_LICENSE("GPL");
101 /* Test stolen from switch-adm.c. */
102 module_param(rtc_debug
, int, 0);
104 static inline void sdalo(void)
106 gpio_direction_output(sda_index
, 1);
110 static inline void sdahi(void)
112 gpio_direction_input(sda_index
);
116 static inline void scllo(void)
118 gpio_direction_output(scl_index
, 1);
122 static inline int getscl(void)
124 return (gpio_get_value(scl_index
));
127 static inline int getsda(void)
129 return (gpio_get_value(sda_index
));
133 * We shouldn't simply set the SCL pin to high. Like SDA, the SCL line is
134 * bidirectional too. According to the I2C spec, the slave is allowed to
135 * pull down the SCL line to slow down the clock, so we need to check this.
136 * Generally, we'd need a timeout here, but in our case, we just check the
137 * line, assuming the RTC chip behaves well.
139 static int sclhi(void)
141 gpio_direction_input(scl_index
);
144 printk(KERN_ERR
"SCL pin should be low\n");
150 static void i2c_start(void)
156 static void i2c_stop(void)
163 static int i2c_outb(int c
)
168 /* assert: scl is low */
169 for (i
= 7; i
>= 0; i
--) {
170 if (c
& ( 1 << i
)) {
175 if (sclhi() < 0) { /* timed out */
176 sdahi(); /* we don't want to block the net */
185 /* read ack: SDA should be pulled down by slave */
186 ack
= getsda() == 0; /* ack: sda is pulled low ->success. */
190 printk(KERN_DEBUG
"i2c_outb(0x%02x) -> %s\n",
191 c
, ack
? "ACK": "NAK");
193 return ack
; /* return 1 if device acked */
194 /* assert: scl is low (sda undef) */
197 static int i2c_inb(int ack
)
200 unsigned int indata
= 0;
202 /* assert: scl is low */
205 for (i
= 0; i
< 8; i
++) {
228 printk(KERN_DEBUG
"i2c_inb() -> 0x%02x\n", indata
);
230 /* assert: scl is low */
231 return indata
& 0xff;
234 static void i2c_init(void)
236 /* no gpio_control for EXTIF */
237 // ssb_gpio_control(&ssb, sda_mask | scl_mask, 0);
239 gpio_set_value(sda_index
, 0);
240 gpio_set_value(scl_index
, 0);
245 static int rtc_open(UNUSED
struct inode
*inode
, UNUSED
struct file
*filp
)
247 spin_lock_irq(&rtc_lock
);
249 if (rtc_status
& RTC_IS_OPEN
) {
250 spin_unlock_irq(&rtc_lock
);
254 rtc_status
|= RTC_IS_OPEN
;
257 * The following call is only necessary if we use both this driver and
258 * the proprietary one from asus at the same time (which, b.t.w. only
259 * makes sense during development). Otherwise, each access via the asus
260 * driver will make access via this driver impossible.
264 spin_unlock_irq(&rtc_lock
);
269 static int rtc_release(UNUSED
struct inode
*inode
, UNUSED
struct file
*filp
)
271 /* No need for locking here. */
272 rtc_status
&= ~RTC_IS_OPEN
;
276 static int from_bcd(int bcdnum
)
280 for (fac
= 1; bcdnum
; fac
*= 10) {
281 num
+= (bcdnum
% 16) * fac
;
288 static int to_bcd(int decnum
)
292 for (fac
= 1; decnum
; fac
*= 16) {
293 num
+= (decnum
% 10) * fac
;
300 static void get_rtc_time(struct rtc_time
*rtc_tm
)
305 * Read date and time from the RTC. We use read method (3).
308 spin_lock_irq(&rtc_lock
);
310 i2c_outb(RTC_I2C_ADDRESS
| I2C_READ_MASK
);
311 cr2
= i2c_inb(I2C_ACK
);
312 rtc_tm
->tm_sec
= i2c_inb(I2C_ACK
);
313 rtc_tm
->tm_min
= i2c_inb(I2C_ACK
);
314 rtc_tm
->tm_hour
= i2c_inb(I2C_ACK
);
315 rtc_tm
->tm_wday
= i2c_inb(I2C_ACK
);
316 rtc_tm
->tm_mday
= i2c_inb(I2C_ACK
);
317 rtc_tm
->tm_mon
= i2c_inb(I2C_ACK
);
318 rtc_tm
->tm_year
= i2c_inb(I2C_NAK
);
320 spin_unlock_irq(&rtc_lock
);
322 if (cr2
& RTC_VDET_MASK
) {
323 printk(KERN_WARNING
"***RTC BATTERY FAILURE***\n");
326 /* Handle century bit */
327 if (rtc_tm
->tm_mon
& RTC_Y2K_MASK
) {
328 rtc_tm
->tm_mon
&= ~RTC_Y2K_MASK
;
329 rtc_tm
->tm_year
+= 0x100;
332 rtc_tm
->tm_sec
= from_bcd(rtc_tm
->tm_sec
);
333 rtc_tm
->tm_min
= from_bcd(rtc_tm
->tm_min
);
334 rtc_tm
->tm_hour
= from_bcd(rtc_tm
->tm_hour
);
335 rtc_tm
->tm_mday
= from_bcd(rtc_tm
->tm_mday
);
336 rtc_tm
->tm_mon
= from_bcd(rtc_tm
->tm_mon
) - 1;
337 rtc_tm
->tm_year
= from_bcd(rtc_tm
->tm_year
);
339 rtc_tm
->tm_isdst
= -1; /* DST not known */
342 static void set_rtc_time(struct rtc_time
*rtc_tm
)
344 rtc_tm
->tm_sec
= to_bcd(rtc_tm
->tm_sec
);
345 rtc_tm
->tm_min
= to_bcd(rtc_tm
->tm_min
);
346 rtc_tm
->tm_hour
= to_bcd(rtc_tm
->tm_hour
);
347 rtc_tm
->tm_mday
= to_bcd(rtc_tm
->tm_mday
);
348 rtc_tm
->tm_mon
= to_bcd(rtc_tm
->tm_mon
+ 1);
349 rtc_tm
->tm_year
= to_bcd(rtc_tm
->tm_year
);
351 if (rtc_tm
->tm_year
>= 0x100) {
352 rtc_tm
->tm_year
-= 0x100;
353 rtc_tm
->tm_mon
|= RTC_Y2K_MASK
;
356 spin_lock_irq(&rtc_lock
);
358 i2c_outb(RTC_I2C_ADDRESS
| I2C_WRITE_MASK
);
359 i2c_outb(0x00); /* set starting register to 0 (=seconds) */
360 i2c_outb(rtc_tm
->tm_sec
);
361 i2c_outb(rtc_tm
->tm_min
);
362 i2c_outb(rtc_tm
->tm_hour
);
363 i2c_outb(rtc_tm
->tm_wday
);
364 i2c_outb(rtc_tm
->tm_mday
);
365 i2c_outb(rtc_tm
->tm_mon
);
366 i2c_outb(rtc_tm
->tm_year
);
368 spin_unlock_irq(&rtc_lock
);
371 static ssize_t
rtc_write(UNUSED
struct file
*filp
, const char *buf
,
372 size_t count
, loff_t
*ppos
)
374 struct rtc_time rtc_tm
;
378 if (!capable(CAP_SYS_TIME
))
381 if (ppos
!= &filp
->f_pos
)
385 * For simplicity, the only acceptable format is:
386 * YYYY:MM:DD:W:HH:MM:SS\n
392 if (copy_from_user(buffer
, buf
, count
))
395 buffer
[sizeof(buffer
)-1] = '\0';
399 rtc_tm
.tm_year
= simple_strtoul(p
, &p
, 10);
400 if (*p
++ != ':') goto err_out
;
402 rtc_tm
.tm_mon
= simple_strtoul(p
, &p
, 10) - 1;
403 if (*p
++ != ':') goto err_out
;
405 rtc_tm
.tm_mday
= simple_strtoul(p
, &p
, 10);
406 if (*p
++ != ':') goto err_out
;
408 rtc_tm
.tm_wday
= simple_strtoul(p
, &p
, 10);
409 if (*p
++ != ':') goto err_out
;
411 rtc_tm
.tm_hour
= simple_strtoul(p
, &p
, 10);
412 if (*p
++ != ':') goto err_out
;
414 rtc_tm
.tm_min
= simple_strtoul(p
, &p
, 10);
415 if (*p
++ != ':') goto err_out
;
417 rtc_tm
.tm_sec
= simple_strtoul(p
, &p
, 10);
418 if (*p
!= '\n') goto err_out
;
420 rtc_tm
.tm_year
-= RTC_EPOCH
;
422 set_rtc_time(&rtc_tm
);
429 printk(KERN_ERR
"invalid format: use YYYY:MM:DD:W:HH:MM:SS\\n\n");
434 static ssize_t
rtc_read(UNUSED
struct file
*filp
, char *buf
, size_t count
,
441 if (count
== 0 || *ppos
!= 0)
446 len
= sprintf(wbuf
, "%04d:%02d:%02d:%d:%02d:%02d:%02d\n",
447 tm
.tm_year
+ RTC_EPOCH
,
455 if (len
> (ssize_t
)count
)
458 if (copy_to_user(buf
, wbuf
, len
))
466 static int rtc_do_ioctl(unsigned int cmd
, unsigned long arg
)
468 struct rtc_time rtc_tm
;
472 memset(&rtc_tm
, 0, sizeof(struct rtc_time
));
473 get_rtc_time(&rtc_tm
);
474 if (copy_to_user((void *)arg
, &rtc_tm
, sizeof(rtc_tm
)))
479 if (!capable(CAP_SYS_TIME
))
482 if (copy_from_user(&rtc_tm
, (struct rtc_time
*)arg
,
483 sizeof(struct rtc_time
)))
486 set_rtc_time(&rtc_tm
);
496 static long rtc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
499 ret
= rtc_do_ioctl(cmd
, arg
);
503 static const struct file_operations rtc_fops
= {
504 .owner
= THIS_MODULE
,
508 .unlocked_ioctl
= rtc_ioctl
,
510 .release
= rtc_release
,
513 static struct miscdevice rtc_dev
= {
519 /* Savagely ripped from diag.c. */
520 static inline int startswith (char *source
, char *cmp
)
522 return !strncmp(source
, cmp
, strlen(cmp
));
525 static void platform_detect(void)
528 int et0phyaddr
, et1phyaddr
;
530 /* Based on "model_no". */
531 if (nvram_getenv("model_no", buf
, sizeof(buf
)) >= 0) {
532 if (startswith(buf
, "WL700")) { /* WL700* */
539 if (nvram_getenv("et0phyaddr", buf
, sizeof(buf
)) >= 0 )
540 et0phyaddr
= simple_strtoul(buf
, NULL
, 0);
541 if (nvram_getenv("et1phyaddr", buf
, sizeof(buf
)) >= 0 )
542 et1phyaddr
= simple_strtoul(buf
, NULL
, 0);
544 if (nvram_getenv("hardware_version", buf
, sizeof(buf
)) >= 0) {
545 /* Either WL-300g or WL-HDD, do more extensive checks */
546 if (startswith(buf
, "WL300-") && et0phyaddr
== 0 && et1phyaddr
== 1) {
555 static int __init
rtc_init(void)
561 if (sda_index
== scl_index
) {
562 printk(KERN_ERR
"RTC-RV5C386A: unrecognized platform!\n");
569 * Switch RTC to 24h mode
571 spin_lock_irq(&rtc_lock
);
573 i2c_outb(RTC_I2C_ADDRESS
| I2C_WRITE_MASK
);
574 i2c_outb(0xE4); /* start at address 0xE, transmission mode 4 */
575 cr1
= i2c_inb(I2C_NAK
);
577 spin_unlock_irq(&rtc_lock
);
578 if ((cr1
& RTC_24HOUR_MODE_MASK
) == 0) {
579 /* RTC is running in 12h mode */
580 printk(KERN_INFO
"rtc.o: switching to 24h mode\n");
581 spin_lock_irq(&rtc_lock
);
583 i2c_outb(RTC_I2C_ADDRESS
| I2C_WRITE_MASK
);
585 i2c_outb(cr1
| RTC_24HOUR_MODE_MASK
);
587 spin_unlock_irq(&rtc_lock
);
590 misc_register(&rtc_dev
);
592 printk(KERN_INFO
"RV5C386A Real Time Clock Driver loaded\n");
597 static void __exit
rtc_exit (void)
599 misc_deregister(&rtc_dev
);
600 printk(KERN_INFO
"Successfully removed RTC RV5C386A driver\n");
603 module_init(rtc_init
);
604 module_exit(rtc_exit
);
This page took 0.0704669999999999 seconds and 5 git commands to generate.