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>
26 #include <asm/etraxgpio.h>
27 #include <hwregs/reg_map.h>
28 #include <hwregs/reg_rdwr.h>
29 #include <hwregs/gio_defs.h>
30 #include <hwregs/intr_vect_defs.h>
32 #include <asm/system.h>
35 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
38 #define VIRT_I2C_ADDR 0x40
41 /* The following gio ports on ETRAX FS is available:
42 * pa 8 bits, supports interrupts off, hi, low, set, posedge, negedge anyedge
47 * each port has a rw_px_dout, r_px_din and rw_px_oe register.
50 #define GPIO_MAJOR 120 /* experimental MAJOR number */
59 if (dp_cnt % 1000 == 0) \
66 static char gpio_name[] = "etrax gpio";
69 static wait_queue_head_t *gpio_wq;
72 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
73 static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
76 static int gpio_ioctl(struct inode *inode, struct file *file,
77 unsigned int cmd, unsigned long arg);
78 static ssize_t gpio_write(struct file *file, const char *buf, size_t count,
80 static int gpio_open(struct inode *inode, struct file *filp);
81 static int gpio_release(struct inode *inode, struct file *filp);
82 static unsigned int gpio_poll(struct file *filp,
83 struct poll_table_struct *wait);
85 /* private data per open() of this driver */
88 struct gpio_private *next;
89 /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
90 unsigned char clk_mask;
91 unsigned char data_mask;
92 unsigned char write_msb;
94 /* These fields are generic */
95 unsigned long highalarm, lowalarm;
96 wait_queue_head_t alarm_wq;
100 /* linked list of alarms to check for */
102 static struct gpio_private *alarmlist;
104 static int gpio_some_alarms; /* Set if someone uses alarm */
105 static unsigned long gpio_pa_high_alarms;
106 static unsigned long gpio_pa_low_alarms;
108 static DEFINE_SPINLOCK(alarm_lock);
110 #define NUM_PORTS (GPIO_MINOR_LAST+1)
111 #define GIO_REG_RD_ADDR(reg) \
112 (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
113 #define GIO_REG_WR_ADDR(reg) \
114 (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
115 unsigned long led_dummy;
116 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
117 static unsigned long virtual_dummy;
118 static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
119 static unsigned short cached_virtual_gpio_read;
122 static volatile unsigned long *data_out[NUM_PORTS] = {
123 GIO_REG_WR_ADDR(rw_pa_dout),
124 GIO_REG_WR_ADDR(rw_pb_dout),
126 GIO_REG_WR_ADDR(rw_pc_dout),
127 GIO_REG_WR_ADDR(rw_pd_dout),
128 GIO_REG_WR_ADDR(rw_pe_dout),
129 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
134 static volatile unsigned long *data_in[NUM_PORTS] = {
135 GIO_REG_RD_ADDR(r_pa_din),
136 GIO_REG_RD_ADDR(r_pb_din),
138 GIO_REG_RD_ADDR(r_pc_din),
139 GIO_REG_RD_ADDR(r_pd_din),
140 GIO_REG_RD_ADDR(r_pe_din),
141 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
146 static unsigned long changeable_dir[NUM_PORTS] = {
147 CONFIG_ETRAX_PA_CHANGEABLE_DIR,
148 CONFIG_ETRAX_PB_CHANGEABLE_DIR,
150 CONFIG_ETRAX_PC_CHANGEABLE_DIR,
151 CONFIG_ETRAX_PD_CHANGEABLE_DIR,
152 CONFIG_ETRAX_PE_CHANGEABLE_DIR,
153 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
154 CONFIG_ETRAX_PV_CHANGEABLE_DIR,
158 static unsigned long changeable_bits[NUM_PORTS] = {
159 CONFIG_ETRAX_PA_CHANGEABLE_BITS,
160 CONFIG_ETRAX_PB_CHANGEABLE_BITS,
162 CONFIG_ETRAX_PC_CHANGEABLE_BITS,
163 CONFIG_ETRAX_PD_CHANGEABLE_BITS,
164 CONFIG_ETRAX_PE_CHANGEABLE_BITS,
165 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
166 CONFIG_ETRAX_PV_CHANGEABLE_BITS,
170 static volatile unsigned long *dir_oe[NUM_PORTS] = {
171 GIO_REG_WR_ADDR(rw_pa_oe),
172 GIO_REG_WR_ADDR(rw_pb_oe),
174 GIO_REG_WR_ADDR(rw_pc_oe),
175 GIO_REG_WR_ADDR(rw_pd_oe),
176 GIO_REG_WR_ADDR(rw_pe_oe),
177 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
184 static unsigned int gpio_poll(struct file *file, struct poll_table_struct *wait)
186 unsigned int mask = 0;
187 struct gpio_private *priv = (struct gpio_private *)file->private_data;
189 poll_wait(file, &priv->alarm_wq, wait);
190 if (priv->minor == GPIO_MINOR_A) {
191 reg_gio_rw_intr_cfg intr_cfg;
195 local_irq_save(flags);
196 data = REG_TYPE_CONV(unsigned long, reg_gio_r_pa_din,
197 REG_RD(gio, regi_gio, r_pa_din));
198 /* PA has support for interrupt
199 * lets activate high for those low and with highalarm set
201 intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
203 tmp = ~data & priv->highalarm & 0xFF;
205 intr_cfg.pa0 = regk_gio_hi;
207 intr_cfg.pa1 = regk_gio_hi;
209 intr_cfg.pa2 = regk_gio_hi;
211 intr_cfg.pa3 = regk_gio_hi;
213 intr_cfg.pa4 = regk_gio_hi;
215 intr_cfg.pa5 = regk_gio_hi;
217 intr_cfg.pa6 = regk_gio_hi;
219 intr_cfg.pa7 = regk_gio_hi;
221 * lets activate low for those high and with lowalarm set
223 tmp = data & priv->lowalarm & 0xFF;
225 intr_cfg.pa0 = regk_gio_lo;
227 intr_cfg.pa1 = regk_gio_lo;
229 intr_cfg.pa2 = regk_gio_lo;
231 intr_cfg.pa3 = regk_gio_lo;
233 intr_cfg.pa4 = regk_gio_lo;
235 intr_cfg.pa5 = regk_gio_lo;
237 intr_cfg.pa6 = regk_gio_lo;
239 intr_cfg.pa7 = regk_gio_lo;
241 REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
242 local_irq_restore(flags);
243 } else if (priv->minor <= GPIO_MINOR_E)
244 data = *data_in[priv->minor];
248 if ((data & priv->highalarm) || (~data & priv->lowalarm))
249 mask = POLLIN|POLLRDNORM;
251 DP(printk(KERN_DEBUG "gpio_poll ready: mask 0x%08X\n", mask));
255 int etrax_gpio_wake_up_check(void)
257 struct gpio_private *priv;
258 unsigned long data = 0;
261 spin_lock_irqsave(&alarm_lock, flags);
264 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
265 if (priv->minor == GPIO_MINOR_V)
266 data = (unsigned long)cached_virtual_gpio_read;
268 data = *data_in[priv->minor];
269 if (priv->minor == GPIO_MINOR_A)
270 priv->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
273 data = *data_in[priv->minor];
275 if ((data & priv->highalarm) ||
276 (~data & priv->lowalarm)) {
278 "etrax_gpio_wake_up_check %i\n", priv->minor));
279 wake_up_interruptible(&priv->alarm_wq);
284 spin_unlock_irqrestore(&alarm_lock, flags);
289 gpio_poll_timer_interrupt(int irq, void *dev_id)
291 if (gpio_some_alarms)
292 return IRQ_RETVAL(etrax_gpio_wake_up_check());
297 gpio_pa_interrupt(int irq, void *dev_id)
299 reg_gio_rw_intr_mask intr_mask;
300 reg_gio_r_masked_intr masked_intr;
301 reg_gio_rw_ack_intr ack_intr;
304 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
305 unsigned char enable_gpiov_ack = 0;
308 /* Find what PA interrupts are active */
309 masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
310 tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
312 /* Find those that we have enabled */
313 spin_lock(&alarm_lock);
314 tmp &= (gpio_pa_high_alarms | gpio_pa_low_alarms);
315 spin_unlock(&alarm_lock);
317 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
318 /* Something changed on virtual GPIO. Interrupt is acked by
319 * reading the device.
321 if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
322 i2c_read(VIRT_I2C_ADDR, (void *)&cached_virtual_gpio_read,
323 sizeof(cached_virtual_gpio_read));
324 enable_gpiov_ack = 1;
329 ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
330 REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
332 /* Disable those interrupts.. */
333 intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
334 tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
336 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
337 /* Do not disable interrupt on virtual GPIO. Changes on virtual
338 * pins are only noticed by an interrupt.
340 if (enable_gpiov_ack)
341 tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
343 intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
344 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
346 if (gpio_some_alarms)
347 return IRQ_RETVAL(etrax_gpio_wake_up_check());
352 static ssize_t gpio_write(struct file *file, const char *buf, size_t count,
355 struct gpio_private *priv = (struct gpio_private *)file->private_data;
356 unsigned char data, clk_mask, data_mask, write_msb;
358 unsigned long shadow;
359 volatile unsigned long *port;
360 ssize_t retval = count;
361 /* Only bits 0-7 may be used for write operations but allow all
362 devices except leds... */
363 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
364 if (priv->minor == GPIO_MINOR_V)
367 if (priv->minor == GPIO_MINOR_LEDS)
370 if (!access_ok(VERIFY_READ, buf, count))
372 clk_mask = priv->clk_mask;
373 data_mask = priv->data_mask;
374 /* It must have been configured using the IO_CFG_WRITE_MODE */
375 /* Perhaps a better error code? */
376 if (clk_mask == 0 || data_mask == 0)
378 write_msb = priv->write_msb;
379 D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
380 "msb: %i\n", count, data_mask, clk_mask, write_msb));
381 port = data_out[priv->minor];
386 if (priv->write_msb) {
387 for (i = 7; i >= 0; i--) {
388 local_irq_save(flags);
390 *port = shadow &= ~clk_mask;
392 *port = shadow |= data_mask;
394 *port = shadow &= ~data_mask;
395 /* For FPGA: min 5.0ns (DCC) before CCLK high */
396 *port = shadow |= clk_mask;
397 local_irq_restore(flags);
400 for (i = 0; i <= 7; i++) {
401 local_irq_save(flags);
403 *port = shadow &= ~clk_mask;
405 *port = shadow |= data_mask;
407 *port = shadow &= ~data_mask;
408 /* For FPGA: min 5.0ns (DCC) before CCLK high */
409 *port = shadow |= clk_mask;
410 local_irq_restore(flags);
420 gpio_open(struct inode *inode, struct file *filp)
422 struct gpio_private *priv;
423 int p = iminor(inode);
425 if (p > GPIO_MINOR_LAST)
428 priv = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
432 memset(priv, 0, sizeof(*priv));
436 /* initialize the io/alarm struct */
442 init_waitqueue_head(&priv->alarm_wq);
444 filp->private_data = (void *)priv;
446 /* link it into our alarmlist */
447 spin_lock_irq(&alarm_lock);
448 priv->next = alarmlist;
450 spin_unlock_irq(&alarm_lock);
456 gpio_release(struct inode *inode, struct file *filp)
458 struct gpio_private *p;
459 struct gpio_private *todel;
460 /* local copies while updating them: */
461 unsigned long a_high, a_low;
462 unsigned long some_alarms;
464 /* unlink from alarmlist and free the private structure */
466 spin_lock_irq(&alarm_lock);
468 todel = (struct gpio_private *)filp->private_data;
471 alarmlist = todel->next;
473 while (p->next != todel)
475 p->next = todel->next;
479 /* Check if there are still any alarms set */
485 if (p->minor == GPIO_MINOR_A) {
486 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
487 p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
489 a_high |= p->highalarm;
490 a_low |= p->lowalarm;
493 if (p->highalarm | p->lowalarm)
498 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
499 /* Variables 'some_alarms' and 'a_low' needs to be set here again
500 * to ensure that interrupt for virtual GPIO is handled.
503 a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
506 gpio_some_alarms = some_alarms;
507 gpio_pa_high_alarms = a_high;
508 gpio_pa_low_alarms = a_low;
509 spin_unlock_irq(&alarm_lock);
514 /* Main device API. ioctl's to read/set/clear bits, as well as to
515 * set alarms to wait for using a subsequent select().
518 inline unsigned long setget_input(struct gpio_private *priv, unsigned long arg)
520 /* Set direction 0=unchanged 1=input,
521 * return mask with 1=input
524 unsigned long dir_shadow;
526 local_irq_save(flags);
527 dir_shadow = *dir_oe[priv->minor];
528 dir_shadow &= ~(arg & changeable_dir[priv->minor]);
529 *dir_oe[priv->minor] = dir_shadow;
530 local_irq_restore(flags);
532 if (priv->minor == GPIO_MINOR_A)
533 dir_shadow ^= 0xFF; /* Only 8 bits */
534 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
535 else if (priv->minor == GPIO_MINOR_V)
536 dir_shadow ^= 0xFFFF; /* Only 16 bits */
539 dir_shadow ^= 0x3FFFF; /* Only 18 bits */
544 inline unsigned long setget_output(struct gpio_private *priv, unsigned long arg)
547 unsigned long dir_shadow;
549 local_irq_save(flags);
550 dir_shadow = *dir_oe[priv->minor];
551 dir_shadow |= (arg & changeable_dir[priv->minor]);
552 *dir_oe[priv->minor] = dir_shadow;
553 local_irq_restore(flags);
555 } /* setget_output */
558 gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
561 gpio_ioctl(struct inode *inode, struct file *file,
562 unsigned int cmd, unsigned long arg)
566 unsigned long shadow;
567 struct gpio_private *priv = (struct gpio_private *)file->private_data;
568 if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
571 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
572 if (priv->minor == GPIO_MINOR_V)
573 return virtual_gpio_ioctl(file, cmd, arg);
576 switch (_IOC_NR(cmd)) {
577 case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
579 return *data_in[priv->minor];
582 local_irq_save(flags);
583 /* Set changeable bits with a 1 in arg. */
584 shadow = *data_out[priv->minor];
585 shadow |= (arg & changeable_bits[priv->minor]);
586 *data_out[priv->minor] = shadow;
587 local_irq_restore(flags);
590 local_irq_save(flags);
591 /* Clear changeable bits with a 1 in arg. */
592 shadow = *data_out[priv->minor];
593 shadow &= ~(arg & changeable_bits[priv->minor]);
594 *data_out[priv->minor] = shadow;
595 local_irq_restore(flags);
598 /* Set alarm when bits with 1 in arg go high. */
599 priv->highalarm |= arg;
600 spin_lock_irqsave(&alarm_lock, flags);
601 gpio_some_alarms = 1;
602 if (priv->minor == GPIO_MINOR_A)
603 gpio_pa_high_alarms |= arg;
604 spin_unlock_irqrestore(&alarm_lock, flags);
607 /* Set alarm when bits with 1 in arg go low. */
608 priv->lowalarm |= arg;
609 spin_lock_irqsave(&alarm_lock, flags);
610 gpio_some_alarms = 1;
611 if (priv->minor == GPIO_MINOR_A)
612 gpio_pa_low_alarms |= arg;
613 spin_unlock_irqrestore(&alarm_lock, flags);
616 /* Clear alarm for bits with 1 in arg. */
617 priv->highalarm &= ~arg;
618 priv->lowalarm &= ~arg;
619 spin_lock_irqsave(&alarm_lock, flags);
620 if (priv->minor == GPIO_MINOR_A) {
621 if (gpio_pa_high_alarms & arg ||
622 gpio_pa_low_alarms & arg)
623 /* Must update the gpio_pa_*alarms masks */
626 spin_unlock_irqrestore(&alarm_lock, flags);
628 case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
629 /* Read direction 0=input 1=output */
630 return *dir_oe[priv->minor];
631 case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
632 /* Set direction 0=unchanged 1=input,
633 * return mask with 1=input
635 return setget_input(priv, arg);
637 case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
638 /* Set direction 0=unchanged 1=output,
639 * return mask with 1=output
641 return setget_output(priv, arg);
643 case IO_CFG_WRITE_MODE:
645 unsigned long dir_shadow;
646 dir_shadow = *dir_oe[priv->minor];
648 priv->clk_mask = arg & 0xFF;
649 priv->data_mask = (arg >> 8) & 0xFF;
650 priv->write_msb = (arg >> 16) & 0x01;
651 /* Check if we're allowed to change the bits and
652 * the direction is correct
654 if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
655 (priv->data_mask & changeable_bits[priv->minor]) &&
656 (priv->clk_mask & dir_shadow) &&
657 (priv->data_mask & dir_shadow))) {
665 /* *arg is result of reading the input pins */
666 val = *data_in[priv->minor];
667 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
671 case IO_READ_OUTBITS:
672 /* *arg is result of reading the output shadow */
673 val = *data_out[priv->minor];
674 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
677 case IO_SETGET_INPUT:
678 /* bits set in *arg is set to input,
679 * *arg updated with current input pins.
681 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
683 val = setget_input(priv, val);
684 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
687 case IO_SETGET_OUTPUT:
688 /* bits set in *arg is set to output,
689 * *arg updated with current output pins.
691 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
693 val = setget_output(priv, val);
694 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
698 if (priv->minor == GPIO_MINOR_LEDS)
699 return gpio_leds_ioctl(cmd, arg);
707 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
709 virtual_gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
713 unsigned short shadow;
714 struct gpio_private *priv = (struct gpio_private *)file->private_data;
716 switch (_IOC_NR(cmd)) {
718 local_irq_save(flags);
719 /* Set changeable bits with a 1 in arg. */
720 i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
721 shadow |= ~*dir_oe[priv->minor];
722 shadow |= (arg & changeable_bits[priv->minor]);
723 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
724 local_irq_restore(flags);
727 local_irq_save(flags);
728 /* Clear changeable bits with a 1 in arg. */
729 i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
730 shadow |= ~*dir_oe[priv->minor];
731 shadow &= ~(arg & changeable_bits[priv->minor]);
732 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
733 local_irq_restore(flags);
736 /* Set alarm when bits with 1 in arg go high. */
737 priv->highalarm |= arg;
738 spin_lock(&alarm_lock);
739 gpio_some_alarms = 1;
740 spin_unlock(&alarm_lock);
743 /* Set alarm when bits with 1 in arg go low. */
744 priv->lowalarm |= arg;
745 spin_lock(&alarm_lock);
746 gpio_some_alarms = 1;
747 spin_unlock(&alarm_lock);
750 /* Clear alarm for bits with 1 in arg. */
751 priv->highalarm &= ~arg;
752 priv->lowalarm &= ~arg;
753 spin_lock(&alarm_lock);
754 spin_unlock(&alarm_lock);
756 case IO_CFG_WRITE_MODE:
758 unsigned long dir_shadow;
759 dir_shadow = *dir_oe[priv->minor];
761 priv->clk_mask = arg & 0xFF;
762 priv->data_mask = (arg >> 8) & 0xFF;
763 priv->write_msb = (arg >> 16) & 0x01;
764 /* Check if we're allowed to change the bits and
765 * the direction is correct
767 if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
768 (priv->data_mask & changeable_bits[priv->minor]) &&
769 (priv->clk_mask & dir_shadow) &&
770 (priv->data_mask & dir_shadow))) {
778 /* *arg is result of reading the input pins */
779 val = cached_virtual_gpio_read;
780 val &= ~*dir_oe[priv->minor];
781 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
785 case IO_READ_OUTBITS:
786 /* *arg is result of reading the output shadow */
787 i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
788 val &= *dir_oe[priv->minor];
789 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
792 case IO_SETGET_INPUT:
794 /* bits set in *arg is set to input,
795 * *arg updated with current input pins.
797 unsigned short input_mask = ~*dir_oe[priv->minor];
798 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
800 val = setget_input(priv, val);
801 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
803 if ((input_mask & val) != input_mask) {
804 /* Input pins changed. All ports desired as input
805 * should be set to logic 1.
807 unsigned short change = input_mask ^ val;
808 i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
812 i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
817 case IO_SETGET_OUTPUT:
818 /* bits set in *arg is set to output,
819 * *arg updated with current output pins.
821 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
823 val = setget_output(priv, val);
824 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
832 #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
835 gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
840 switch (_IOC_NR(cmd)) {
841 case IO_LEDACTIVE_SET:
842 green = ((unsigned char) arg) & 1;
843 red = (((unsigned char) arg) >> 1) & 1;
844 CRIS_LED_ACTIVE_SET_G(green);
845 CRIS_LED_ACTIVE_SET_R(red);
855 struct file_operations gpio_fops = {
856 .owner = THIS_MODULE,
861 .release = gpio_release,
864 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
866 virtual_gpio_init(void)
868 reg_gio_rw_intr_cfg intr_cfg;
869 reg_gio_rw_intr_mask intr_mask;
870 unsigned short shadow;
872 shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
873 shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
874 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
876 /* Set interrupt mask and on what state the interrupt shall trigger.
877 * For virtual gpio the interrupt shall trigger on logic '0'.
879 intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
880 intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
882 switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
884 intr_cfg.pa0 = regk_gio_lo;
885 intr_mask.pa0 = regk_gio_yes;
888 intr_cfg.pa1 = regk_gio_lo;
889 intr_mask.pa1 = regk_gio_yes;
892 intr_cfg.pa2 = regk_gio_lo;
893 intr_mask.pa2 = regk_gio_yes;
896 intr_cfg.pa3 = regk_gio_lo;
897 intr_mask.pa3 = regk_gio_yes;
900 intr_cfg.pa4 = regk_gio_lo;
901 intr_mask.pa4 = regk_gio_yes;
904 intr_cfg.pa5 = regk_gio_lo;
905 intr_mask.pa5 = regk_gio_yes;
908 intr_cfg.pa6 = regk_gio_lo;
909 intr_mask.pa6 = regk_gio_yes;
912 intr_cfg.pa7 = regk_gio_lo;
913 intr_mask.pa7 = regk_gio_yes;
917 REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
918 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
920 gpio_pa_low_alarms |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
921 gpio_some_alarms = 1;
925 /* main driver initialization routine, called from mem.c */
932 /* do the formalities */
934 res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
936 printk(KERN_ERR "gpio: couldn't get a major number.\n");
941 CRIS_LED_NETWORK_GRP0_SET(0);
942 CRIS_LED_NETWORK_GRP1_SET(0);
943 CRIS_LED_ACTIVE_SET(0);
944 CRIS_LED_DISK_READ(0);
945 CRIS_LED_DISK_WRITE(0);
947 printk(KERN_INFO "ETRAX FS GPIO driver v2.5, (c) 2003-2007 "
948 "Axis Communications AB\n");
949 /* We call etrax_gpio_wake_up_check() from timer interrupt and
950 * from cpu_idle() in kernel/process.c
951 * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
954 if (request_irq(TIMER0_INTR_VECT, gpio_poll_timer_interrupt,
955 IRQF_SHARED | IRQF_DISABLED, "gpio poll", &alarmlist))
956 printk(KERN_ERR "timer0 irq for gpio\n");
958 if (request_irq(GIO_INTR_VECT, gpio_pa_interrupt,
959 IRQF_SHARED | IRQF_DISABLED, "gpio PA", &alarmlist))
960 printk(KERN_ERR "PA irq for gpio\n");
962 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
969 /* this makes sure that gpio_init is called during kernel boot */
971 module_init(gpio_init);