rb532: fix a module dependency issue
[openwrt.git] / package / rtc-rv5c386a / src / rtc.c
1 /*
2 * Real Time Clock driver for WL-HDD
3 *
4 * Copyright (C) 2007 Andreas Engel
5 *
6 * Hacked together mostly by copying the relevant code parts from:
7 * drivers/i2c/i2c-bcm5365.c
8 * drivers/i2c/i2c-algo-bit.c
9 * drivers/char/rtc.c
10 *
11 * Note 1:
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()).
15 *
16 * Note 2:
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
19 * in 12h mode.
20 *
21 * Usage:
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
25 * this format.
26 *
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
29 * be used.
30 *
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.
35 *
36 * TODO:
37 * - add a /proc/driver/rtc interface?
38 * - make the battery failure bit available through the /proc interface?
39 *
40 * $Id: rtc.c 7 2007-05-25 19:37:01Z ae $
41 */
42
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/init.h>
51 #include <linux/spinlock.h>
52 #include <linux/rtc.h>
53 #include <linux/delay.h>
54 #include <linux/version.h>
55
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58
59 #include "gpio.h"
60
61 #define RTC_IS_OPEN 0x01 /* Means /dev/rtc is in use. */
62
63 /* Can be changed via a module parameter. */
64 static int rtc_debug = 0;
65
66 static unsigned long rtc_status = 0; /* Bitmapped status byte. */
67
68 static spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
69
70 /* These settings are platform dependents. */
71 unsigned int sda_index = 0;
72 unsigned int scl_index = 0;
73
74 #define I2C_READ_MASK 1
75 #define I2C_WRITE_MASK 0
76
77 #define I2C_ACK 1
78 #define I2C_NAK 0
79
80 #define RTC_EPOCH 1900
81 #define RTC_I2C_ADDRESS (0x32 << 1)
82 #define RTC_24HOUR_MODE_MASK 0x20
83 #define RTC_PM_MASK 0x20
84 #define RTC_VDET_MASK 0x40
85 #define RTC_Y2K_MASK 0x80
86
87 /*
88 * Delay in microseconds for generating the pulses on the I2C bus. We use
89 * a rather conservative setting here. See datasheet of the RTC chip.
90 */
91 #define ADAP_DELAY 50
92
93 /* Avoid spurious compiler warnings. */
94 #define UNUSED __attribute__((unused))
95
96 MODULE_AUTHOR("Andreas Engel");
97 MODULE_LICENSE("GPL");
98
99 /* Test stolen from switch-adm.c. */
100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,52)
101 module_param(rtc_debug, int, 0);
102 #else
103 MODULE_PARM(rtc_debug, "i");
104 #endif
105
106 static inline void sdalo(void)
107 {
108 gpio_direction_output(sda_index, 1);
109 udelay(ADAP_DELAY);
110 }
111
112 static inline void sdahi(void)
113 {
114 gpio_direction_input(sda_index);
115 udelay(ADAP_DELAY);
116 }
117
118 static inline void scllo(void)
119 {
120 gpio_direction_output(scl_index, 1);
121 udelay(ADAP_DELAY);
122 }
123
124 static inline int getscl(void)
125 {
126 return (gpio_get_value(scl_index));
127 }
128
129 static inline int getsda(void)
130 {
131 return (gpio_get_value(sda_index));
132 }
133
134 /*
135 * We shouldn't simply set the SCL pin to high. Like SDA, the SCL line is
136 * bidirectional too. According to the I2C spec, the slave is allowed to
137 * pull down the SCL line to slow down the clock, so we need to check this.
138 * Generally, we'd need a timeout here, but in our case, we just check the
139 * line, assuming the RTC chip behaves well.
140 */
141 static int sclhi(void)
142 {
143 gpio_direction_input(scl_index);
144 udelay(ADAP_DELAY);
145 if (!getscl()) {
146 printk(KERN_ERR "SCL pin should be low\n");
147 return -ETIMEDOUT;
148 }
149 return 0;
150 }
151
152 static void i2c_start(void)
153 {
154 sdalo();
155 scllo();
156 }
157
158 static void i2c_stop(void)
159 {
160 sdalo();
161 sclhi();
162 sdahi();
163 }
164
165 static int i2c_outb(int c)
166 {
167 int i;
168 int ack;
169
170 /* assert: scl is low */
171 for (i = 7; i >= 0; i--) {
172 if (c & ( 1 << i )) {
173 sdahi();
174 } else {
175 sdalo();
176 }
177 if (sclhi() < 0) { /* timed out */
178 sdahi(); /* we don't want to block the net */
179 return -ETIMEDOUT;
180 };
181 scllo();
182 }
183 sdahi();
184 if (sclhi() < 0) {
185 return -ETIMEDOUT;
186 };
187 /* read ack: SDA should be pulled down by slave */
188 ack = getsda() == 0; /* ack: sda is pulled low ->success. */
189 scllo();
190
191 if (rtc_debug)
192 printk(KERN_DEBUG "i2c_outb(0x%02x) -> %s\n",
193 c, ack ? "ACK": "NAK");
194
195 return ack; /* return 1 if device acked */
196 /* assert: scl is low (sda undef) */
197 }
198
199 static int i2c_inb(int ack)
200 {
201 int i;
202 unsigned int indata = 0;
203
204 /* assert: scl is low */
205
206 sdahi();
207 for (i = 0; i < 8; i++) {
208 if (sclhi() < 0) {
209 return -ETIMEDOUT;
210 };
211 indata *= 2;
212 if (getsda())
213 indata |= 0x01;
214 scllo();
215 }
216 if (ack) {
217 sdalo();
218 } else {
219 sdahi();
220 }
221
222 if (sclhi() < 0) {
223 sdahi();
224 return -ETIMEDOUT;
225 }
226 scllo();
227 sdahi();
228
229 if (rtc_debug)
230 printk(KERN_DEBUG "i2c_inb() -> 0x%02x\n", indata);
231
232 /* assert: scl is low */
233 return indata & 0xff;
234 }
235
236 static void i2c_init(void)
237 {
238 /* no gpio_control for EXTIF */
239 // gpio_control(sda_mask | scl_mask, 0);
240
241 gpio_set_value(sda_index, 0);
242 gpio_set_value(scl_index, 0);
243 sdahi();
244 sclhi();
245 }
246
247 static int rtc_open(UNUSED struct inode *inode, UNUSED struct file *filp)
248 {
249 spin_lock_irq(&rtc_lock);
250
251 if (rtc_status & RTC_IS_OPEN) {
252 spin_unlock_irq(&rtc_lock);
253 return -EBUSY;
254 }
255
256 rtc_status |= RTC_IS_OPEN;
257
258 /*
259 * The following call is only necessary if we use both this driver and
260 * the proprietary one from asus at the same time (which, b.t.w. only
261 * makes sense during development). Otherwise, each access via the asus
262 * driver will make access via this driver impossible.
263 */
264 i2c_init();
265
266 spin_unlock_irq(&rtc_lock);
267
268 return 0;
269 }
270
271 static int rtc_release(UNUSED struct inode *inode, UNUSED struct file *filp)
272 {
273 /* No need for locking here. */
274 rtc_status &= ~RTC_IS_OPEN;
275 return 0;
276 }
277
278 static int from_bcd(int bcdnum)
279 {
280 int fac, num = 0;
281
282 for (fac = 1; bcdnum; fac *= 10) {
283 num += (bcdnum % 16) * fac;
284 bcdnum /= 16;
285 }
286
287 return num;
288 }
289
290 static int to_bcd(int decnum)
291 {
292 int fac, num = 0;
293
294 for (fac = 1; decnum; fac *= 16) {
295 num += (decnum % 10) * fac;
296 decnum /= 10;
297 }
298
299 return num;
300 }
301
302 static void get_rtc_time(struct rtc_time *rtc_tm)
303 {
304 int cr2;
305
306 /*
307 * Read date and time from the RTC. We use read method (3).
308 */
309
310 i2c_start();
311 i2c_outb(RTC_I2C_ADDRESS | I2C_READ_MASK);
312 cr2 = i2c_inb(I2C_ACK);
313 rtc_tm->tm_sec = i2c_inb(I2C_ACK);
314 rtc_tm->tm_min = i2c_inb(I2C_ACK);
315 rtc_tm->tm_hour = i2c_inb(I2C_ACK);
316 rtc_tm->tm_wday = i2c_inb(I2C_ACK);
317 rtc_tm->tm_mday = i2c_inb(I2C_ACK);
318 rtc_tm->tm_mon = i2c_inb(I2C_ACK);
319 rtc_tm->tm_year = i2c_inb(I2C_NAK);
320 i2c_stop();
321
322 if (cr2 & RTC_VDET_MASK) {
323 printk(KERN_WARNING "***RTC BATTERY FAILURE***\n");
324 }
325
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;
330 }
331
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);
338
339 rtc_tm->tm_isdst = -1; /* DST not known */
340 }
341
342 static void set_rtc_time(struct rtc_time *rtc_tm)
343 {
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);
350
351 if (rtc_tm->tm_year >= 0x100) {
352 rtc_tm->tm_year -= 0x100;
353 rtc_tm->tm_mon |= RTC_Y2K_MASK;
354 }
355
356 i2c_start();
357 i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
358 i2c_outb(0x00); /* set starting register to 0 (=seconds) */
359 i2c_outb(rtc_tm->tm_sec);
360 i2c_outb(rtc_tm->tm_min);
361 i2c_outb(rtc_tm->tm_hour);
362 i2c_outb(rtc_tm->tm_wday);
363 i2c_outb(rtc_tm->tm_mday);
364 i2c_outb(rtc_tm->tm_mon);
365 i2c_outb(rtc_tm->tm_year);
366 i2c_stop();
367 }
368
369 static ssize_t rtc_write(UNUSED struct file *filp, const char *buf,
370 size_t count, loff_t *ppos)
371 {
372 struct rtc_time rtc_tm;
373 char buffer[23];
374 char *p;
375
376 if (!capable(CAP_SYS_TIME))
377 return -EACCES;
378
379 if (ppos != &filp->f_pos)
380 return -ESPIPE;
381
382 /*
383 * For simplicity, the only acceptable format is:
384 * YYYY:MM:DD:W:HH:MM:SS\n
385 */
386
387 if (count != 22)
388 goto err_out;
389
390 if (copy_from_user(buffer, buf, count))
391 return -EFAULT;
392
393 buffer[sizeof(buffer)-1] = '\0';
394
395 p = &buffer[0];
396
397 rtc_tm.tm_year = simple_strtoul(p, &p, 10);
398 if (*p++ != ':') goto err_out;
399
400 rtc_tm.tm_mon = simple_strtoul(p, &p, 10) - 1;
401 if (*p++ != ':') goto err_out;
402
403 rtc_tm.tm_mday = simple_strtoul(p, &p, 10);
404 if (*p++ != ':') goto err_out;
405
406 rtc_tm.tm_wday = simple_strtoul(p, &p, 10);
407 if (*p++ != ':') goto err_out;
408
409 rtc_tm.tm_hour = simple_strtoul(p, &p, 10);
410 if (*p++ != ':') goto err_out;
411
412 rtc_tm.tm_min = simple_strtoul(p, &p, 10);
413 if (*p++ != ':') goto err_out;
414
415 rtc_tm.tm_sec = simple_strtoul(p, &p, 10);
416 if (*p != '\n') goto err_out;
417
418 rtc_tm.tm_year -= RTC_EPOCH;
419
420 set_rtc_time(&rtc_tm);
421
422 *ppos += count;
423
424 return count;
425
426 err_out:
427 printk(KERN_ERR "invalid format: use YYYY:MM:DD:W:HH:MM:SS\\n\n");
428 return -EINVAL;
429 }
430
431
432 static ssize_t rtc_read(UNUSED struct file *filp, char *buf, size_t count,
433 loff_t *ppos)
434 {
435 char wbuf[23];
436 struct rtc_time tm;
437 ssize_t len;
438
439 if (count == 0 || *ppos != 0)
440 return 0;
441
442 get_rtc_time(&tm);
443
444 len = sprintf(wbuf, "%04d:%02d:%02d:%d:%02d:%02d:%02d\n",
445 tm.tm_year + RTC_EPOCH,
446 tm.tm_mon + 1,
447 tm.tm_mday,
448 tm.tm_wday,
449 tm.tm_hour,
450 tm.tm_min,
451 tm.tm_sec);
452
453 if (len > (ssize_t)count)
454 len = count;
455
456 if (copy_to_user(buf, wbuf, len))
457 return -EFAULT;
458
459 *ppos += len;
460
461 return len;
462 }
463
464 static int rtc_ioctl(UNUSED struct inode *inode, UNUSED struct file *filp,
465 unsigned int cmd, unsigned long arg)
466 {
467 struct rtc_time rtc_tm;
468
469 switch (cmd) {
470 case RTC_RD_TIME:
471 memset(&rtc_tm, 0, sizeof(struct rtc_time));
472 get_rtc_time(&rtc_tm);
473 if (copy_to_user((void *)arg, &rtc_tm, sizeof(rtc_tm)))
474 return -EFAULT;
475 break;
476
477 case RTC_SET_TIME:
478 if (!capable(CAP_SYS_TIME))
479 return -EACCES;
480
481 if (copy_from_user(&rtc_tm, (struct rtc_time *)arg,
482 sizeof(struct rtc_time)))
483 return -EFAULT;
484
485 set_rtc_time(&rtc_tm);
486 break;
487
488 default:
489 return -ENOTTY;
490 }
491
492 return 0;
493 }
494
495 static struct file_operations rtc_fops = {
496 .owner = THIS_MODULE,
497 .llseek = no_llseek,
498 .read = rtc_read,
499 .write = rtc_write,
500 .ioctl = rtc_ioctl,
501 .open = rtc_open,
502 .release = rtc_release,
503 };
504
505 static struct miscdevice rtc_dev = {
506 .minor = RTC_MINOR,
507 .name = "rtc",
508 .fops = &rtc_fops,
509 };
510
511 /* Savagely ripped from diag.c. */
512 extern char *nvram_get(char *str);
513 #define getvar(str) (nvram_get(str)?:"")
514 static inline int startswith (char *source, char *cmp)
515 { return !strncmp(source,cmp,strlen(cmp)); }
516 static void platform_detect(void)
517 {
518 char *buf;
519
520 /* Based on "model_no". */
521 if ((buf = nvram_get("model_no"))) {
522 if (startswith(buf,"WL700")) { /* WL700* */
523 sda_index = 2;
524 scl_index = 5;
525 return;
526 }
527 }
528
529 if (startswith(getvar("hardware_version"), "WL300-")) {
530 /* Either WL-300g or WL-HDD, do more extensive checks */
531 if ((simple_strtoul(getvar("et0phyaddr"), NULL, 0) == 0) &&
532 (simple_strtoul(getvar("et1phyaddr"), NULL, 0) == 1)) {
533 sda_index = 4;
534 scl_index = 5;
535 return;
536 }
537 }
538 /* not found */
539 }
540
541 static int __init rtc_init(void)
542 {
543 int cr1;
544
545 platform_detect();
546
547 if (sda_index == scl_index) {
548 printk(KERN_ERR "RTC-RV5C386A: unrecognized platform!\n");
549 return -ENODEV;
550 }
551
552 i2c_init();
553
554 /*
555 * Switch RTC to 24h mode
556 */
557 i2c_start();
558 i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
559 i2c_outb(0xE4); /* start at address 0xE, transmission mode 4 */
560 cr1 = i2c_inb(I2C_NAK);
561 i2c_stop();
562 if ((cr1 & RTC_24HOUR_MODE_MASK) == 0) {
563 /* RTC is running in 12h mode */
564 printk(KERN_INFO "rtc.o: switching to 24h mode\n");
565 i2c_start();
566 i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
567 i2c_outb(0xE0);
568 i2c_outb(cr1 | RTC_24HOUR_MODE_MASK);
569 i2c_stop();
570 }
571
572 misc_register(&rtc_dev);
573
574 printk(KERN_INFO "RV5C386A Real Time Clock Driver loaded\n");
575
576 return 0;
577 }
578
579 static void __exit rtc_exit (void)
580 {
581 misc_deregister(&rtc_dev);
582 printk(KERN_INFO "Successfully removed RTC RV5C386A driver\n");
583 }
584
585 module_init(rtc_init);
586 module_exit(rtc_exit);
587
588 /*
589 * Local Variables:
590 * indent-tabs-mode:t
591 * c-basic-offset:8
592 * End:
593 */
This page took 0.084627 seconds and 5 git commands to generate.