cris: autoconvert trivial BKL users
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / cris / arch-v10 / drivers / ds1302.c
blob4b92ad08b0ff2c8fa6534c19a843d578c4cff636
1 /*!***************************************************************************
2 *!
3 *! FILE NAME : ds1302.c
4 *!
5 *! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O
6 *!
7 *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init
8 *!
9 *! ---------------------------------------------------------------------------
11 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
13 *!***************************************************************************/
16 #include <linux/fs.h>
17 #include <linux/init.h>
18 #include <linux/mm.h>
19 #include <linux/module.h>
20 #include <linux/miscdevice.h>
21 #include <linux/delay.h>
22 #include <linux/mutex.h>
23 #include <linux/bcd.h>
24 #include <linux/capability.h>
26 #include <asm/uaccess.h>
27 #include <asm/system.h>
28 #include <arch/svinto.h>
29 #include <asm/io.h>
30 #include <asm/rtc.h>
31 #include <arch/io_interface_mux.h>
33 #include "i2c.h"
35 #define RTC_MAJOR_NR 121 /* local major, change later */
37 static DEFINE_MUTEX(ds1302_mutex);
38 static const char ds1302_name[] = "ds1302";
40 /* The DS1302 might be connected to different bits on different products.
41 * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
42 * but SDA can have a selected direction.
43 * For now, only PORT_PB is hardcoded.
46 /* The RST bit may be on either the Generic Port or Port PB. */
47 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
48 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
49 #define TK_RST_DIR(x)
50 #else
51 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
52 #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
53 #endif
56 #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
57 #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
59 #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
60 /* 1 is out, 0 is in */
61 #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
62 #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
66 * The reason for tempudelay and not udelay is that loops_per_usec
67 * (used in udelay) is not set when functions here are called from time.c
70 static void tempudelay(int usecs)
72 volatile int loops;
74 for(loops = usecs * 12; loops > 0; loops--)
75 /* nothing */;
79 /* Send 8 bits. */
80 static void
81 out_byte(unsigned char x)
83 int i;
84 TK_SDA_DIR(1);
85 for (i = 8; i--;) {
86 /* The chip latches incoming bits on the rising edge of SCL. */
87 TK_SCL_OUT(0);
88 TK_SDA_OUT(x & 1);
89 tempudelay(1);
90 TK_SCL_OUT(1);
91 tempudelay(1);
92 x >>= 1;
94 TK_SDA_DIR(0);
97 static unsigned char
98 in_byte(void)
100 unsigned char x = 0;
101 int i;
103 /* Read byte. Bits come LSB first, on the falling edge of SCL.
104 * Assume SDA is in input direction already.
106 TK_SDA_DIR(0);
108 for (i = 8; i--;) {
109 TK_SCL_OUT(0);
110 tempudelay(1);
111 x >>= 1;
112 x |= (TK_SDA_IN() << 7);
113 TK_SCL_OUT(1);
114 tempudelay(1);
117 return x;
120 /* Prepares for a transaction by de-activating RST (active-low). */
122 static void
123 start(void)
125 TK_SCL_OUT(0);
126 tempudelay(1);
127 TK_RST_OUT(0);
128 tempudelay(5);
129 TK_RST_OUT(1);
132 /* Ends a transaction by taking RST active again. */
134 static void
135 stop(void)
137 tempudelay(2);
138 TK_RST_OUT(0);
141 /* Enable writing. */
143 static void
144 ds1302_wenable(void)
146 start();
147 out_byte(0x8e); /* Write control register */
148 out_byte(0x00); /* Disable write protect bit 7 = 0 */
149 stop();
152 /* Disable writing. */
154 static void
155 ds1302_wdisable(void)
157 start();
158 out_byte(0x8e); /* Write control register */
159 out_byte(0x80); /* Disable write protect bit 7 = 0 */
160 stop();
165 /* Read a byte from the selected register in the DS1302. */
167 unsigned char
168 ds1302_readreg(int reg)
170 unsigned char x;
172 start();
173 out_byte(0x81 | (reg << 1)); /* read register */
174 x = in_byte();
175 stop();
177 return x;
180 /* Write a byte to the selected register. */
182 void
183 ds1302_writereg(int reg, unsigned char val)
185 #ifndef CONFIG_ETRAX_RTC_READONLY
186 int do_writereg = 1;
187 #else
188 int do_writereg = 0;
190 if (reg == RTC_TRICKLECHARGER)
191 do_writereg = 1;
192 #endif
194 if (do_writereg) {
195 ds1302_wenable();
196 start();
197 out_byte(0x80 | (reg << 1)); /* write register */
198 out_byte(val);
199 stop();
200 ds1302_wdisable();
204 void
205 get_rtc_time(struct rtc_time *rtc_tm)
207 unsigned long flags;
209 local_irq_save(flags);
211 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
212 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
213 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
214 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
215 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
216 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
218 local_irq_restore(flags);
220 rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec);
221 rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min);
222 rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour);
223 rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday);
224 rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon);
225 rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year);
228 * Account for differences between how the RTC uses the values
229 * and how they are defined in a struct rtc_time;
232 if (rtc_tm->tm_year <= 69)
233 rtc_tm->tm_year += 100;
235 rtc_tm->tm_mon--;
238 static unsigned char days_in_mo[] =
239 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
241 /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
243 static int rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
245 unsigned long flags;
247 switch(cmd) {
248 case RTC_RD_TIME: /* read the time/date from RTC */
250 struct rtc_time rtc_tm;
252 memset(&rtc_tm, 0, sizeof (struct rtc_time));
253 get_rtc_time(&rtc_tm);
254 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
255 return -EFAULT;
256 return 0;
259 case RTC_SET_TIME: /* set the RTC */
261 struct rtc_time rtc_tm;
262 unsigned char mon, day, hrs, min, sec, leap_yr;
263 unsigned int yrs;
265 if (!capable(CAP_SYS_TIME))
266 return -EPERM;
268 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time)))
269 return -EFAULT;
271 yrs = rtc_tm.tm_year + 1900;
272 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
273 day = rtc_tm.tm_mday;
274 hrs = rtc_tm.tm_hour;
275 min = rtc_tm.tm_min;
276 sec = rtc_tm.tm_sec;
279 if ((yrs < 1970) || (yrs > 2069))
280 return -EINVAL;
282 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
284 if ((mon > 12) || (day == 0))
285 return -EINVAL;
287 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
288 return -EINVAL;
290 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
291 return -EINVAL;
293 if (yrs >= 2000)
294 yrs -= 2000; /* RTC (0, 1, ... 69) */
295 else
296 yrs -= 1900; /* RTC (70, 71, ... 99) */
298 sec = bin2bcd(sec);
299 min = bin2bcd(min);
300 hrs = bin2bcd(hrs);
301 day = bin2bcd(day);
302 mon = bin2bcd(mon);
303 yrs = bin2bcd(yrs);
305 local_irq_save(flags);
306 CMOS_WRITE(yrs, RTC_YEAR);
307 CMOS_WRITE(mon, RTC_MONTH);
308 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
309 CMOS_WRITE(hrs, RTC_HOURS);
310 CMOS_WRITE(min, RTC_MINUTES);
311 CMOS_WRITE(sec, RTC_SECONDS);
312 local_irq_restore(flags);
314 /* Notice that at this point, the RTC is updated but
315 * the kernel is still running with the old time.
316 * You need to set that separately with settimeofday
317 * or adjtimex.
319 return 0;
322 case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */
324 int tcs_val;
326 if (!capable(CAP_SYS_TIME))
327 return -EPERM;
329 if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
330 return -EFAULT;
332 tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
333 ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
334 return 0;
336 case RTC_VL_READ:
338 /* TODO:
339 * Implement voltage low detection support
341 printk(KERN_WARNING "DS1302: RTC Voltage Low detection"
342 " is not supported\n");
343 return 0;
345 case RTC_VL_CLR:
347 /* TODO:
348 * Nothing to do since Voltage Low detection is not supported
350 return 0;
352 default:
353 return -ENOIOCTLCMD;
357 static long rtc_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
359 int ret;
361 mutex_lock(&ds1302_mutex);
362 ret = rtc_ioctl(file, cmd, arg);
363 mutex_unlock(&ds1302_mutex);
365 return ret;
368 static void
369 print_rtc_status(void)
371 struct rtc_time tm;
373 get_rtc_time(&tm);
376 * There is no way to tell if the luser has the RTC set for local
377 * time or for Universal Standard Time (GMT). Probably local though.
380 printk(KERN_INFO "rtc_time\t: %02d:%02d:%02d\n",
381 tm.tm_hour, tm.tm_min, tm.tm_sec);
382 printk(KERN_INFO "rtc_date\t: %04d-%02d-%02d\n",
383 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
386 /* The various file operations we support. */
388 static const struct file_operations rtc_fops = {
389 .owner = THIS_MODULE,
390 .unlocked_ioctl = rtc_unlocked_ioctl,
393 /* Probe for the chip by writing something to its RAM and try reading it back. */
395 #define MAGIC_PATTERN 0x42
397 static int __init
398 ds1302_probe(void)
400 int retval, res;
402 TK_RST_DIR(1);
403 TK_SCL_DIR(1);
404 TK_SDA_DIR(0);
406 /* Try to talk to timekeeper. */
408 ds1302_wenable();
409 start();
410 out_byte(0xc0); /* write RAM byte 0 */
411 out_byte(MAGIC_PATTERN); /* write something magic */
412 start();
413 out_byte(0xc1); /* read RAM byte 0 */
415 if((res = in_byte()) == MAGIC_PATTERN) {
416 stop();
417 ds1302_wdisable();
418 printk(KERN_INFO "%s: RTC found.\n", ds1302_name);
419 printk(KERN_INFO "%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n",
420 ds1302_name,
421 CONFIG_ETRAX_DS1302_SDABIT,
422 CONFIG_ETRAX_DS1302_SCLBIT,
423 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
424 "GENIO",
425 #else
426 "PB",
427 #endif
428 CONFIG_ETRAX_DS1302_RSTBIT);
429 print_rtc_status();
430 retval = 1;
431 } else {
432 stop();
433 retval = 0;
436 return retval;
440 /* Just probe for the RTC and register the device to handle the ioctl needed. */
442 int __init
443 ds1302_init(void)
445 #ifdef CONFIG_ETRAX_I2C
446 i2c_init();
447 #endif
449 if (!ds1302_probe()) {
450 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
451 #if CONFIG_ETRAX_DS1302_RSTBIT == 27
453 * The only way to set g27 to output is to enable ATA.
455 * Make sure that R_GEN_CONFIG is setup correct.
457 /* Allocating the ATA interface will grab almost all
458 * pins in I/O groups a, b, c and d. A consequence of
459 * allocating the ATA interface is that the fixed
460 * interfaces shared RAM, parallel port 0, parallel
461 * port 1, parallel port W, SCSI-8 port 0, SCSI-8 port
462 * 1, SCSI-W, serial port 2, serial port 3,
463 * synchronous serial port 3 and USB port 2 and almost
464 * all GPIO pins on port g cannot be used.
466 if (cris_request_io_interface(if_ata, "ds1302/ATA")) {
467 printk(KERN_WARNING "ds1302: Failed to get IO interface\n");
468 return -1;
471 #elif CONFIG_ETRAX_DS1302_RSTBIT == 0
472 if (cris_io_interface_allocate_pins(if_gpio_grp_a,
473 'g',
474 CONFIG_ETRAX_DS1302_RSTBIT,
475 CONFIG_ETRAX_DS1302_RSTBIT)) {
476 printk(KERN_WARNING "ds1302: Failed to get IO interface\n");
477 return -1;
480 /* Set the direction of this bit to out. */
481 genconfig_shadow = ((genconfig_shadow &
482 ~IO_MASK(R_GEN_CONFIG, g0dir)) |
483 (IO_STATE(R_GEN_CONFIG, g0dir, out)));
484 *R_GEN_CONFIG = genconfig_shadow;
485 #endif
486 if (!ds1302_probe()) {
487 printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
488 return -1;
490 #else
491 printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name);
492 return -1;
493 #endif
495 /* Initialise trickle charger */
496 ds1302_writereg(RTC_TRICKLECHARGER,
497 RTC_TCR_PATTERN |(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE & 0x0F));
498 /* Start clock by resetting CLOCK_HALT */
499 ds1302_writereg(RTC_SECONDS, (ds1302_readreg(RTC_SECONDS) & 0x7F));
500 return 0;
503 static int __init ds1302_register(void)
505 ds1302_init();
506 if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) {
507 printk(KERN_INFO "%s: unable to get major %d for rtc\n",
508 ds1302_name, RTC_MAJOR_NR);
509 return -1;
511 return 0;
515 module_init(ds1302_register);