2 * ETRAX CRISv32 general port I/O device
4 * Copyright (c) 1999-2006 Axis Communications AB
6 * Authors: Bjorn Wesen (initial version)
7 * Ola Knutsson (LED handling)
8 * Johan Adolfsson (read/set directions, write, port G,
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/ioport.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/poll.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/smp_lock.h>
27 #include <asm/etraxgpio.h>
28 #include <hwregs/reg_map.h>
29 #include <hwregs/reg_rdwr.h>
30 #include <hwregs/gio_defs.h>
31 #include <hwregs/intr_vect_defs.h>
33 #include <asm/system.h>
36 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
39 #define VIRT_I2C_ADDR 0x40
42 /* The following gio ports on ETRAX FS is available:
43 * pa 8 bits, supports interrupts off, hi, low, set, posedge, negedge anyedge
48 * each port has a rw_px_dout, r_px_din and rw_px_oe register.
51 #define GPIO_MAJOR 120 /* experimental MAJOR number */
60 if (dp_cnt % 1000 == 0) \
67 static char gpio_name
[] = "etrax gpio";
70 static wait_queue_head_t
*gpio_wq
;
73 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
74 static int virtual_gpio_ioctl(struct file
*file
, unsigned int cmd
,
77 static int gpio_ioctl(struct inode
*inode
, struct file
*file
,
78 unsigned int cmd
, unsigned long arg
);
79 static ssize_t
gpio_write(struct file
*file
, const char *buf
, size_t count
,
81 static int gpio_open(struct inode
*inode
, struct file
*filp
);
82 static int gpio_release(struct inode
*inode
, struct file
*filp
);
83 static unsigned int gpio_poll(struct file
*filp
,
84 struct poll_table_struct
*wait
);
86 /* private data per open() of this driver */
89 struct gpio_private
*next
;
90 /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
91 unsigned char clk_mask
;
92 unsigned char data_mask
;
93 unsigned char write_msb
;
95 /* These fields are generic */
96 unsigned long highalarm
, lowalarm
;
97 wait_queue_head_t alarm_wq
;
101 /* linked list of alarms to check for */
103 static struct gpio_private
*alarmlist
;
105 static int gpio_some_alarms
; /* Set if someone uses alarm */
106 static unsigned long gpio_pa_high_alarms
;
107 static unsigned long gpio_pa_low_alarms
;
109 static DEFINE_SPINLOCK(alarm_lock
);
111 #define NUM_PORTS (GPIO_MINOR_LAST+1)
112 #define GIO_REG_RD_ADDR(reg) \
113 (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
114 #define GIO_REG_WR_ADDR(reg) \
115 (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
116 unsigned long led_dummy
;
117 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
118 static unsigned long virtual_dummy
;
119 static unsigned long virtual_rw_pv_oe
= CONFIG_ETRAX_DEF_GIO_PV_OE
;
120 static unsigned short cached_virtual_gpio_read
;
123 static volatile unsigned long *data_out
[NUM_PORTS
] = {
124 GIO_REG_WR_ADDR(rw_pa_dout
),
125 GIO_REG_WR_ADDR(rw_pb_dout
),
127 GIO_REG_WR_ADDR(rw_pc_dout
),
128 GIO_REG_WR_ADDR(rw_pd_dout
),
129 GIO_REG_WR_ADDR(rw_pe_dout
),
130 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
135 static volatile unsigned long *data_in
[NUM_PORTS
] = {
136 GIO_REG_RD_ADDR(r_pa_din
),
137 GIO_REG_RD_ADDR(r_pb_din
),
139 GIO_REG_RD_ADDR(r_pc_din
),
140 GIO_REG_RD_ADDR(r_pd_din
),
141 GIO_REG_RD_ADDR(r_pe_din
),
142 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
147 static unsigned long changeable_dir
[NUM_PORTS
] = {
148 CONFIG_ETRAX_PA_CHANGEABLE_DIR
,
149 CONFIG_ETRAX_PB_CHANGEABLE_DIR
,
151 CONFIG_ETRAX_PC_CHANGEABLE_DIR
,
152 CONFIG_ETRAX_PD_CHANGEABLE_DIR
,
153 CONFIG_ETRAX_PE_CHANGEABLE_DIR
,
154 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
155 CONFIG_ETRAX_PV_CHANGEABLE_DIR
,
159 static unsigned long changeable_bits
[NUM_PORTS
] = {
160 CONFIG_ETRAX_PA_CHANGEABLE_BITS
,
161 CONFIG_ETRAX_PB_CHANGEABLE_BITS
,
163 CONFIG_ETRAX_PC_CHANGEABLE_BITS
,
164 CONFIG_ETRAX_PD_CHANGEABLE_BITS
,
165 CONFIG_ETRAX_PE_CHANGEABLE_BITS
,
166 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
167 CONFIG_ETRAX_PV_CHANGEABLE_BITS
,
171 static volatile unsigned long *dir_oe
[NUM_PORTS
] = {
172 GIO_REG_WR_ADDR(rw_pa_oe
),
173 GIO_REG_WR_ADDR(rw_pb_oe
),
175 GIO_REG_WR_ADDR(rw_pc_oe
),
176 GIO_REG_WR_ADDR(rw_pd_oe
),
177 GIO_REG_WR_ADDR(rw_pe_oe
),
178 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
185 static unsigned int gpio_poll(struct file
*file
, struct poll_table_struct
*wait
)
187 unsigned int mask
= 0;
188 struct gpio_private
*priv
= (struct gpio_private
*)file
->private_data
;
190 poll_wait(file
, &priv
->alarm_wq
, wait
);
191 if (priv
->minor
== GPIO_MINOR_A
) {
192 reg_gio_rw_intr_cfg intr_cfg
;
196 local_irq_save(flags
);
197 data
= REG_TYPE_CONV(unsigned long, reg_gio_r_pa_din
,
198 REG_RD(gio
, regi_gio
, r_pa_din
));
199 /* PA has support for interrupt
200 * lets activate high for those low and with highalarm set
202 intr_cfg
= REG_RD(gio
, regi_gio
, rw_intr_cfg
);
204 tmp
= ~data
& priv
->highalarm
& 0xFF;
206 intr_cfg
.pa0
= regk_gio_hi
;
208 intr_cfg
.pa1
= regk_gio_hi
;
210 intr_cfg
.pa2
= regk_gio_hi
;
212 intr_cfg
.pa3
= regk_gio_hi
;
214 intr_cfg
.pa4
= regk_gio_hi
;
216 intr_cfg
.pa5
= regk_gio_hi
;
218 intr_cfg
.pa6
= regk_gio_hi
;
220 intr_cfg
.pa7
= regk_gio_hi
;
222 * lets activate low for those high and with lowalarm set
224 tmp
= data
& priv
->lowalarm
& 0xFF;
226 intr_cfg
.pa0
= regk_gio_lo
;
228 intr_cfg
.pa1
= regk_gio_lo
;
230 intr_cfg
.pa2
= regk_gio_lo
;
232 intr_cfg
.pa3
= regk_gio_lo
;
234 intr_cfg
.pa4
= regk_gio_lo
;
236 intr_cfg
.pa5
= regk_gio_lo
;
238 intr_cfg
.pa6
= regk_gio_lo
;
240 intr_cfg
.pa7
= regk_gio_lo
;
242 REG_WR(gio
, regi_gio
, rw_intr_cfg
, intr_cfg
);
243 local_irq_restore(flags
);
244 } else if (priv
->minor
<= GPIO_MINOR_E
)
245 data
= *data_in
[priv
->minor
];
249 if ((data
& priv
->highalarm
) || (~data
& priv
->lowalarm
))
250 mask
= POLLIN
|POLLRDNORM
;
252 DP(printk(KERN_DEBUG
"gpio_poll ready: mask 0x%08X\n", mask
));
256 int etrax_gpio_wake_up_check(void)
258 struct gpio_private
*priv
;
259 unsigned long data
= 0;
262 spin_lock_irqsave(&alarm_lock
, flags
);
265 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
266 if (priv
->minor
== GPIO_MINOR_V
)
267 data
= (unsigned long)cached_virtual_gpio_read
;
269 data
= *data_in
[priv
->minor
];
270 if (priv
->minor
== GPIO_MINOR_A
)
271 priv
->lowalarm
|= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
);
274 data
= *data_in
[priv
->minor
];
276 if ((data
& priv
->highalarm
) ||
277 (~data
& priv
->lowalarm
)) {
279 "etrax_gpio_wake_up_check %i\n", priv
->minor
));
280 wake_up_interruptible(&priv
->alarm_wq
);
285 spin_unlock_irqrestore(&alarm_lock
, flags
);
290 gpio_poll_timer_interrupt(int irq
, void *dev_id
)
292 if (gpio_some_alarms
)
293 return IRQ_RETVAL(etrax_gpio_wake_up_check());
298 gpio_pa_interrupt(int irq
, void *dev_id
)
300 reg_gio_rw_intr_mask intr_mask
;
301 reg_gio_r_masked_intr masked_intr
;
302 reg_gio_rw_ack_intr ack_intr
;
305 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
306 unsigned char enable_gpiov_ack
= 0;
309 /* Find what PA interrupts are active */
310 masked_intr
= REG_RD(gio
, regi_gio
, r_masked_intr
);
311 tmp
= REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr
, masked_intr
);
313 /* Find those that we have enabled */
314 spin_lock(&alarm_lock
);
315 tmp
&= (gpio_pa_high_alarms
| gpio_pa_low_alarms
);
316 spin_unlock(&alarm_lock
);
318 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
319 /* Something changed on virtual GPIO. Interrupt is acked by
320 * reading the device.
322 if (tmp
& (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
)) {
323 i2c_read(VIRT_I2C_ADDR
, (void *)&cached_virtual_gpio_read
,
324 sizeof(cached_virtual_gpio_read
));
325 enable_gpiov_ack
= 1;
330 ack_intr
= REG_TYPE_CONV(reg_gio_rw_ack_intr
, unsigned long, tmp
);
331 REG_WR(gio
, regi_gio
, rw_ack_intr
, ack_intr
);
333 /* Disable those interrupts.. */
334 intr_mask
= REG_RD(gio
, regi_gio
, rw_intr_mask
);
335 tmp2
= REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask
, intr_mask
);
337 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
338 /* Do not disable interrupt on virtual GPIO. Changes on virtual
339 * pins are only noticed by an interrupt.
341 if (enable_gpiov_ack
)
342 tmp2
|= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
);
344 intr_mask
= REG_TYPE_CONV(reg_gio_rw_intr_mask
, unsigned long, tmp2
);
345 REG_WR(gio
, regi_gio
, rw_intr_mask
, intr_mask
);
347 if (gpio_some_alarms
)
348 return IRQ_RETVAL(etrax_gpio_wake_up_check());
353 static ssize_t
gpio_write(struct file
*file
, const char *buf
, size_t count
,
356 struct gpio_private
*priv
= (struct gpio_private
*)file
->private_data
;
357 unsigned char data
, clk_mask
, data_mask
, write_msb
;
359 unsigned long shadow
;
360 volatile unsigned long *port
;
361 ssize_t retval
= count
;
362 /* Only bits 0-7 may be used for write operations but allow all
363 devices except leds... */
364 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
365 if (priv
->minor
== GPIO_MINOR_V
)
368 if (priv
->minor
== GPIO_MINOR_LEDS
)
371 if (!access_ok(VERIFY_READ
, buf
, count
))
373 clk_mask
= priv
->clk_mask
;
374 data_mask
= priv
->data_mask
;
375 /* It must have been configured using the IO_CFG_WRITE_MODE */
376 /* Perhaps a better error code? */
377 if (clk_mask
== 0 || data_mask
== 0)
379 write_msb
= priv
->write_msb
;
380 D(printk(KERN_DEBUG
"gpio_write: %lu to data 0x%02X clk 0x%02X "
381 "msb: %i\n", count
, data_mask
, clk_mask
, write_msb
));
382 port
= data_out
[priv
->minor
];
387 if (priv
->write_msb
) {
388 for (i
= 7; i
>= 0; i
--) {
389 local_irq_save(flags
);
391 *port
= shadow
&= ~clk_mask
;
393 *port
= shadow
|= data_mask
;
395 *port
= shadow
&= ~data_mask
;
396 /* For FPGA: min 5.0ns (DCC) before CCLK high */
397 *port
= shadow
|= clk_mask
;
398 local_irq_restore(flags
);
401 for (i
= 0; i
<= 7; i
++) {
402 local_irq_save(flags
);
404 *port
= shadow
&= ~clk_mask
;
406 *port
= shadow
|= data_mask
;
408 *port
= shadow
&= ~data_mask
;
409 /* For FPGA: min 5.0ns (DCC) before CCLK high */
410 *port
= shadow
|= clk_mask
;
411 local_irq_restore(flags
);
421 gpio_open(struct inode
*inode
, struct file
*filp
)
423 struct gpio_private
*priv
;
424 int p
= iminor(inode
);
426 if (p
> GPIO_MINOR_LAST
)
429 priv
= kmalloc(sizeof(struct gpio_private
), GFP_KERNEL
);
434 memset(priv
, 0, sizeof(*priv
));
438 /* initialize the io/alarm struct */
444 init_waitqueue_head(&priv
->alarm_wq
);
446 filp
->private_data
= (void *)priv
;
448 /* link it into our alarmlist */
449 spin_lock_irq(&alarm_lock
);
450 priv
->next
= alarmlist
;
452 spin_unlock_irq(&alarm_lock
);
459 gpio_release(struct inode
*inode
, struct file
*filp
)
461 struct gpio_private
*p
;
462 struct gpio_private
*todel
;
463 /* local copies while updating them: */
464 unsigned long a_high
, a_low
;
465 unsigned long some_alarms
;
467 /* unlink from alarmlist and free the private structure */
469 spin_lock_irq(&alarm_lock
);
471 todel
= (struct gpio_private
*)filp
->private_data
;
474 alarmlist
= todel
->next
;
476 while (p
->next
!= todel
)
478 p
->next
= todel
->next
;
482 /* Check if there are still any alarms set */
488 if (p
->minor
== GPIO_MINOR_A
) {
489 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
490 p
->lowalarm
|= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
);
492 a_high
|= p
->highalarm
;
493 a_low
|= p
->lowalarm
;
496 if (p
->highalarm
| p
->lowalarm
)
501 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
502 /* Variables 'some_alarms' and 'a_low' needs to be set here again
503 * to ensure that interrupt for virtual GPIO is handled.
506 a_low
|= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
);
509 gpio_some_alarms
= some_alarms
;
510 gpio_pa_high_alarms
= a_high
;
511 gpio_pa_low_alarms
= a_low
;
512 spin_unlock_irq(&alarm_lock
);
517 /* Main device API. ioctl's to read/set/clear bits, as well as to
518 * set alarms to wait for using a subsequent select().
521 inline unsigned long setget_input(struct gpio_private
*priv
, unsigned long arg
)
523 /* Set direction 0=unchanged 1=input,
524 * return mask with 1=input
527 unsigned long dir_shadow
;
529 local_irq_save(flags
);
530 dir_shadow
= *dir_oe
[priv
->minor
];
531 dir_shadow
&= ~(arg
& changeable_dir
[priv
->minor
]);
532 *dir_oe
[priv
->minor
] = dir_shadow
;
533 local_irq_restore(flags
);
535 if (priv
->minor
== GPIO_MINOR_A
)
536 dir_shadow
^= 0xFF; /* Only 8 bits */
537 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
538 else if (priv
->minor
== GPIO_MINOR_V
)
539 dir_shadow
^= 0xFFFF; /* Only 16 bits */
542 dir_shadow
^= 0x3FFFF; /* Only 18 bits */
547 inline unsigned long setget_output(struct gpio_private
*priv
, unsigned long arg
)
550 unsigned long dir_shadow
;
552 local_irq_save(flags
);
553 dir_shadow
= *dir_oe
[priv
->minor
];
554 dir_shadow
|= (arg
& changeable_dir
[priv
->minor
]);
555 *dir_oe
[priv
->minor
] = dir_shadow
;
556 local_irq_restore(flags
);
558 } /* setget_output */
561 gpio_leds_ioctl(unsigned int cmd
, unsigned long arg
);
564 gpio_ioctl(struct inode
*inode
, struct file
*file
,
565 unsigned int cmd
, unsigned long arg
)
569 unsigned long shadow
;
570 struct gpio_private
*priv
= (struct gpio_private
*)file
->private_data
;
571 if (_IOC_TYPE(cmd
) != ETRAXGPIO_IOCTYPE
)
574 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
575 if (priv
->minor
== GPIO_MINOR_V
)
576 return virtual_gpio_ioctl(file
, cmd
, arg
);
579 switch (_IOC_NR(cmd
)) {
580 case IO_READBITS
: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
582 return *data_in
[priv
->minor
];
585 local_irq_save(flags
);
586 /* Set changeable bits with a 1 in arg. */
587 shadow
= *data_out
[priv
->minor
];
588 shadow
|= (arg
& changeable_bits
[priv
->minor
]);
589 *data_out
[priv
->minor
] = shadow
;
590 local_irq_restore(flags
);
593 local_irq_save(flags
);
594 /* Clear changeable bits with a 1 in arg. */
595 shadow
= *data_out
[priv
->minor
];
596 shadow
&= ~(arg
& changeable_bits
[priv
->minor
]);
597 *data_out
[priv
->minor
] = shadow
;
598 local_irq_restore(flags
);
601 /* Set alarm when bits with 1 in arg go high. */
602 priv
->highalarm
|= arg
;
603 spin_lock_irqsave(&alarm_lock
, flags
);
604 gpio_some_alarms
= 1;
605 if (priv
->minor
== GPIO_MINOR_A
)
606 gpio_pa_high_alarms
|= arg
;
607 spin_unlock_irqrestore(&alarm_lock
, flags
);
610 /* Set alarm when bits with 1 in arg go low. */
611 priv
->lowalarm
|= arg
;
612 spin_lock_irqsave(&alarm_lock
, flags
);
613 gpio_some_alarms
= 1;
614 if (priv
->minor
== GPIO_MINOR_A
)
615 gpio_pa_low_alarms
|= arg
;
616 spin_unlock_irqrestore(&alarm_lock
, flags
);
619 /* Clear alarm for bits with 1 in arg. */
620 priv
->highalarm
&= ~arg
;
621 priv
->lowalarm
&= ~arg
;
622 spin_lock_irqsave(&alarm_lock
, flags
);
623 if (priv
->minor
== GPIO_MINOR_A
) {
624 if (gpio_pa_high_alarms
& arg
||
625 gpio_pa_low_alarms
& arg
)
626 /* Must update the gpio_pa_*alarms masks */
629 spin_unlock_irqrestore(&alarm_lock
, flags
);
631 case IO_READDIR
: /* Use IO_SETGET_INPUT/OUTPUT instead! */
632 /* Read direction 0=input 1=output */
633 return *dir_oe
[priv
->minor
];
634 case IO_SETINPUT
: /* Use IO_SETGET_INPUT instead! */
635 /* Set direction 0=unchanged 1=input,
636 * return mask with 1=input
638 return setget_input(priv
, arg
);
640 case IO_SETOUTPUT
: /* Use IO_SETGET_OUTPUT instead! */
641 /* Set direction 0=unchanged 1=output,
642 * return mask with 1=output
644 return setget_output(priv
, arg
);
646 case IO_CFG_WRITE_MODE
:
648 unsigned long dir_shadow
;
649 dir_shadow
= *dir_oe
[priv
->minor
];
651 priv
->clk_mask
= arg
& 0xFF;
652 priv
->data_mask
= (arg
>> 8) & 0xFF;
653 priv
->write_msb
= (arg
>> 16) & 0x01;
654 /* Check if we're allowed to change the bits and
655 * the direction is correct
657 if (!((priv
->clk_mask
& changeable_bits
[priv
->minor
]) &&
658 (priv
->data_mask
& changeable_bits
[priv
->minor
]) &&
659 (priv
->clk_mask
& dir_shadow
) &&
660 (priv
->data_mask
& dir_shadow
))) {
668 /* *arg is result of reading the input pins */
669 val
= *data_in
[priv
->minor
];
670 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
674 case IO_READ_OUTBITS
:
675 /* *arg is result of reading the output shadow */
676 val
= *data_out
[priv
->minor
];
677 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
680 case IO_SETGET_INPUT
:
681 /* bits set in *arg is set to input,
682 * *arg updated with current input pins.
684 if (copy_from_user(&val
, (unsigned long *)arg
, sizeof(val
)))
686 val
= setget_input(priv
, val
);
687 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
690 case IO_SETGET_OUTPUT
:
691 /* bits set in *arg is set to output,
692 * *arg updated with current output pins.
694 if (copy_from_user(&val
, (unsigned long *)arg
, sizeof(val
)))
696 val
= setget_output(priv
, val
);
697 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
701 if (priv
->minor
== GPIO_MINOR_LEDS
)
702 return gpio_leds_ioctl(cmd
, arg
);
710 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
712 virtual_gpio_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
716 unsigned short shadow
;
717 struct gpio_private
*priv
= (struct gpio_private
*)file
->private_data
;
719 switch (_IOC_NR(cmd
)) {
721 local_irq_save(flags
);
722 /* Set changeable bits with a 1 in arg. */
723 i2c_read(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
724 shadow
|= ~*dir_oe
[priv
->minor
];
725 shadow
|= (arg
& changeable_bits
[priv
->minor
]);
726 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
727 local_irq_restore(flags
);
730 local_irq_save(flags
);
731 /* Clear changeable bits with a 1 in arg. */
732 i2c_read(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
733 shadow
|= ~*dir_oe
[priv
->minor
];
734 shadow
&= ~(arg
& changeable_bits
[priv
->minor
]);
735 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
736 local_irq_restore(flags
);
739 /* Set alarm when bits with 1 in arg go high. */
740 priv
->highalarm
|= arg
;
741 spin_lock(&alarm_lock
);
742 gpio_some_alarms
= 1;
743 spin_unlock(&alarm_lock
);
746 /* Set alarm when bits with 1 in arg go low. */
747 priv
->lowalarm
|= arg
;
748 spin_lock(&alarm_lock
);
749 gpio_some_alarms
= 1;
750 spin_unlock(&alarm_lock
);
753 /* Clear alarm for bits with 1 in arg. */
754 priv
->highalarm
&= ~arg
;
755 priv
->lowalarm
&= ~arg
;
756 spin_lock(&alarm_lock
);
757 spin_unlock(&alarm_lock
);
759 case IO_CFG_WRITE_MODE
:
761 unsigned long dir_shadow
;
762 dir_shadow
= *dir_oe
[priv
->minor
];
764 priv
->clk_mask
= arg
& 0xFF;
765 priv
->data_mask
= (arg
>> 8) & 0xFF;
766 priv
->write_msb
= (arg
>> 16) & 0x01;
767 /* Check if we're allowed to change the bits and
768 * the direction is correct
770 if (!((priv
->clk_mask
& changeable_bits
[priv
->minor
]) &&
771 (priv
->data_mask
& changeable_bits
[priv
->minor
]) &&
772 (priv
->clk_mask
& dir_shadow
) &&
773 (priv
->data_mask
& dir_shadow
))) {
781 /* *arg is result of reading the input pins */
782 val
= cached_virtual_gpio_read
;
783 val
&= ~*dir_oe
[priv
->minor
];
784 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
788 case IO_READ_OUTBITS
:
789 /* *arg is result of reading the output shadow */
790 i2c_read(VIRT_I2C_ADDR
, (void *)&val
, sizeof(val
));
791 val
&= *dir_oe
[priv
->minor
];
792 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
795 case IO_SETGET_INPUT
:
797 /* bits set in *arg is set to input,
798 * *arg updated with current input pins.
800 unsigned short input_mask
= ~*dir_oe
[priv
->minor
];
801 if (copy_from_user(&val
, (unsigned long *)arg
, sizeof(val
)))
803 val
= setget_input(priv
, val
);
804 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
806 if ((input_mask
& val
) != input_mask
) {
807 /* Input pins changed. All ports desired as input
808 * should be set to logic 1.
810 unsigned short change
= input_mask
^ val
;
811 i2c_read(VIRT_I2C_ADDR
, (void *)&shadow
,
815 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
,
820 case IO_SETGET_OUTPUT
:
821 /* bits set in *arg is set to output,
822 * *arg updated with current output pins.
824 if (copy_from_user(&val
, (unsigned long *)arg
, sizeof(val
)))
826 val
= setget_output(priv
, val
);
827 if (copy_to_user((unsigned long *)arg
, &val
, sizeof(val
)))
835 #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
838 gpio_leds_ioctl(unsigned int cmd
, unsigned long arg
)
843 switch (_IOC_NR(cmd
)) {
844 case IO_LEDACTIVE_SET
:
845 green
= ((unsigned char) arg
) & 1;
846 red
= (((unsigned char) arg
) >> 1) & 1;
847 CRIS_LED_ACTIVE_SET_G(green
);
848 CRIS_LED_ACTIVE_SET_R(red
);
858 struct file_operations gpio_fops
= {
859 .owner
= THIS_MODULE
,
864 .release
= gpio_release
,
867 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
869 virtual_gpio_init(void)
871 reg_gio_rw_intr_cfg intr_cfg
;
872 reg_gio_rw_intr_mask intr_mask
;
873 unsigned short shadow
;
875 shadow
= ~virtual_rw_pv_oe
; /* Input ports should be set to logic 1 */
876 shadow
|= CONFIG_ETRAX_DEF_GIO_PV_OUT
;
877 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
879 /* Set interrupt mask and on what state the interrupt shall trigger.
880 * For virtual gpio the interrupt shall trigger on logic '0'.
882 intr_cfg
= REG_RD(gio
, regi_gio
, rw_intr_cfg
);
883 intr_mask
= REG_RD(gio
, regi_gio
, rw_intr_mask
);
885 switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
) {
887 intr_cfg
.pa0
= regk_gio_lo
;
888 intr_mask
.pa0
= regk_gio_yes
;
891 intr_cfg
.pa1
= regk_gio_lo
;
892 intr_mask
.pa1
= regk_gio_yes
;
895 intr_cfg
.pa2
= regk_gio_lo
;
896 intr_mask
.pa2
= regk_gio_yes
;
899 intr_cfg
.pa3
= regk_gio_lo
;
900 intr_mask
.pa3
= regk_gio_yes
;
903 intr_cfg
.pa4
= regk_gio_lo
;
904 intr_mask
.pa4
= regk_gio_yes
;
907 intr_cfg
.pa5
= regk_gio_lo
;
908 intr_mask
.pa5
= regk_gio_yes
;
911 intr_cfg
.pa6
= regk_gio_lo
;
912 intr_mask
.pa6
= regk_gio_yes
;
915 intr_cfg
.pa7
= regk_gio_lo
;
916 intr_mask
.pa7
= regk_gio_yes
;
920 REG_WR(gio
, regi_gio
, rw_intr_cfg
, intr_cfg
);
921 REG_WR(gio
, regi_gio
, rw_intr_mask
, intr_mask
);
923 gpio_pa_low_alarms
|= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
);
924 gpio_some_alarms
= 1;
928 /* main driver initialization routine, called from mem.c */
935 /* do the formalities */
937 res
= register_chrdev(GPIO_MAJOR
, gpio_name
, &gpio_fops
);
939 printk(KERN_ERR
"gpio: couldn't get a major number.\n");
944 CRIS_LED_NETWORK_GRP0_SET(0);
945 CRIS_LED_NETWORK_GRP1_SET(0);
946 CRIS_LED_ACTIVE_SET(0);
947 CRIS_LED_DISK_READ(0);
948 CRIS_LED_DISK_WRITE(0);
950 printk(KERN_INFO
"ETRAX FS GPIO driver v2.5, (c) 2003-2007 "
951 "Axis Communications AB\n");
952 /* We call etrax_gpio_wake_up_check() from timer interrupt and
953 * from cpu_idle() in kernel/process.c
954 * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
957 if (request_irq(TIMER0_INTR_VECT
, gpio_poll_timer_interrupt
,
958 IRQF_SHARED
| IRQF_DISABLED
, "gpio poll", &alarmlist
))
959 printk(KERN_ERR
"timer0 irq for gpio\n");
961 if (request_irq(GIO_INTR_VECT
, gpio_pa_interrupt
,
962 IRQF_SHARED
| IRQF_DISABLED
, "gpio PA", &alarmlist
))
963 printk(KERN_ERR
"PA irq for gpio\n");
965 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
972 /* this makes sure that gpio_init is called during kernel boot */
974 module_init(gpio_init
);