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/mutex.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 DEFINE_MUTEX(gpio_mutex);
68 static char gpio_name[] = "etrax gpio";
71 static wait_queue_head_t *gpio_wq;
74 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
75 static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
78 static long gpio_ioctl(struct file *file, 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 = 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 = 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);
433 mutex_lock(&gpio_mutex);
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);
454 mutex_unlock(&gpio_mutex);
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 = 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 */
560 static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
563 gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
567 unsigned long shadow;
568 struct gpio_private *priv = file->private_data;
569 if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
572 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
573 if (priv->minor == GPIO_MINOR_V)
574 return virtual_gpio_ioctl(file, cmd, arg);
577 switch (_IOC_NR(cmd)) {
578 case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
580 return *data_in[priv->minor];
583 local_irq_save(flags);
584 /* Set changeable bits with a 1 in arg. */
585 shadow = *data_out[priv->minor];
586 shadow |= (arg & changeable_bits[priv->minor]);
587 *data_out[priv->minor] = shadow;
588 local_irq_restore(flags);
591 local_irq_save(flags);
592 /* Clear changeable bits with a 1 in arg. */
593 shadow = *data_out[priv->minor];
594 shadow &= ~(arg & changeable_bits[priv->minor]);
595 *data_out[priv->minor] = shadow;
596 local_irq_restore(flags);
599 /* Set alarm when bits with 1 in arg go high. */
600 priv->highalarm |= arg;
601 spin_lock_irqsave(&alarm_lock, flags);
602 gpio_some_alarms = 1;
603 if (priv->minor == GPIO_MINOR_A)
604 gpio_pa_high_alarms |= arg;
605 spin_unlock_irqrestore(&alarm_lock, flags);
608 /* Set alarm when bits with 1 in arg go low. */
609 priv->lowalarm |= arg;
610 spin_lock_irqsave(&alarm_lock, flags);
611 gpio_some_alarms = 1;
612 if (priv->minor == GPIO_MINOR_A)
613 gpio_pa_low_alarms |= arg;
614 spin_unlock_irqrestore(&alarm_lock, flags);
617 /* Clear alarm for bits with 1 in arg. */
618 priv->highalarm &= ~arg;
619 priv->lowalarm &= ~arg;
620 spin_lock_irqsave(&alarm_lock, flags);
621 if (priv->minor == GPIO_MINOR_A) {
622 if (gpio_pa_high_alarms & arg ||
623 gpio_pa_low_alarms & arg)
624 /* Must update the gpio_pa_*alarms masks */
627 spin_unlock_irqrestore(&alarm_lock, flags);
629 case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
630 /* Read direction 0=input 1=output */
631 return *dir_oe[priv->minor];
632 case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
633 /* Set direction 0=unchanged 1=input,
634 * return mask with 1=input
636 return setget_input(priv, arg);
638 case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
639 /* Set direction 0=unchanged 1=output,
640 * return mask with 1=output
642 return setget_output(priv, arg);
644 case IO_CFG_WRITE_MODE:
646 unsigned long dir_shadow;
647 dir_shadow = *dir_oe[priv->minor];
649 priv->clk_mask = arg & 0xFF;
650 priv->data_mask = (arg >> 8) & 0xFF;
651 priv->write_msb = (arg >> 16) & 0x01;
652 /* Check if we're allowed to change the bits and
653 * the direction is correct
655 if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
656 (priv->data_mask & changeable_bits[priv->minor]) &&
657 (priv->clk_mask & dir_shadow) &&
658 (priv->data_mask & dir_shadow))) {
666 /* *arg is result of reading the input pins */
667 val = *data_in[priv->minor];
668 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
672 case IO_READ_OUTBITS:
673 /* *arg is result of reading the output shadow */
674 val = *data_out[priv->minor];
675 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
678 case IO_SETGET_INPUT:
679 /* bits set in *arg is set to input,
680 * *arg updated with current input pins.
682 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
684 val = setget_input(priv, val);
685 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
688 case IO_SETGET_OUTPUT:
689 /* bits set in *arg is set to output,
690 * *arg updated with current output pins.
692 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
694 val = setget_output(priv, val);
695 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
699 if (priv->minor == GPIO_MINOR_LEDS)
700 return gpio_leds_ioctl(cmd, arg);
708 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
712 mutex_lock(&gpio_mutex);
713 ret = gpio_ioctl_unlocked(file, cmd, arg);
714 mutex_unlock(&gpio_mutex);
719 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
721 virtual_gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
725 unsigned short shadow;
726 struct gpio_private *priv = file->private_data;
728 switch (_IOC_NR(cmd)) {
730 local_irq_save(flags);
731 /* Set 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 local_irq_save(flags);
740 /* Clear changeable bits with a 1 in arg. */
741 i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
742 shadow |= ~*dir_oe[priv->minor];
743 shadow &= ~(arg & changeable_bits[priv->minor]);
744 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
745 local_irq_restore(flags);
748 /* Set alarm when bits with 1 in arg go high. */
749 priv->highalarm |= arg;
750 spin_lock(&alarm_lock);
751 gpio_some_alarms = 1;
752 spin_unlock(&alarm_lock);
755 /* Set alarm when bits with 1 in arg go low. */
756 priv->lowalarm |= arg;
757 spin_lock(&alarm_lock);
758 gpio_some_alarms = 1;
759 spin_unlock(&alarm_lock);
762 /* Clear alarm for bits with 1 in arg. */
763 priv->highalarm &= ~arg;
764 priv->lowalarm &= ~arg;
765 spin_lock(&alarm_lock);
766 spin_unlock(&alarm_lock);
768 case IO_CFG_WRITE_MODE:
770 unsigned long dir_shadow;
771 dir_shadow = *dir_oe[priv->minor];
773 priv->clk_mask = arg & 0xFF;
774 priv->data_mask = (arg >> 8) & 0xFF;
775 priv->write_msb = (arg >> 16) & 0x01;
776 /* Check if we're allowed to change the bits and
777 * the direction is correct
779 if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
780 (priv->data_mask & changeable_bits[priv->minor]) &&
781 (priv->clk_mask & dir_shadow) &&
782 (priv->data_mask & dir_shadow))) {
790 /* *arg is result of reading the input pins */
791 val = cached_virtual_gpio_read;
792 val &= ~*dir_oe[priv->minor];
793 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
797 case IO_READ_OUTBITS:
798 /* *arg is result of reading the output shadow */
799 i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
800 val &= *dir_oe[priv->minor];
801 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
804 case IO_SETGET_INPUT:
806 /* bits set in *arg is set to input,
807 * *arg updated with current input pins.
809 unsigned short input_mask = ~*dir_oe[priv->minor];
810 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
812 val = setget_input(priv, val);
813 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
815 if ((input_mask & val) != input_mask) {
816 /* Input pins changed. All ports desired as input
817 * should be set to logic 1.
819 unsigned short change = input_mask ^ val;
820 i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
824 i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
829 case IO_SETGET_OUTPUT:
830 /* bits set in *arg is set to output,
831 * *arg updated with current output pins.
833 if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
835 val = setget_output(priv, val);
836 if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
844 #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
847 gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
852 switch (_IOC_NR(cmd)) {
853 case IO_LEDACTIVE_SET:
854 green = ((unsigned char) arg) & 1;
855 red = (((unsigned char) arg) >> 1) & 1;
856 CRIS_LED_ACTIVE_SET_G(green);
857 CRIS_LED_ACTIVE_SET_R(red);
867 static const struct file_operations gpio_fops = {
868 .owner = THIS_MODULE,
870 .unlocked_ioctl = gpio_ioctl,
873 .release = gpio_release,
874 .llseek = noop_llseek,
877 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
879 virtual_gpio_init(void)
881 reg_gio_rw_intr_cfg intr_cfg;
882 reg_gio_rw_intr_mask intr_mask;
883 unsigned short shadow;
885 shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
886 shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
887 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
889 /* Set interrupt mask and on what state the interrupt shall trigger.
890 * For virtual gpio the interrupt shall trigger on logic '0'.
892 intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
893 intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
895 switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
897 intr_cfg.pa0 = regk_gio_lo;
898 intr_mask.pa0 = regk_gio_yes;
901 intr_cfg.pa1 = regk_gio_lo;
902 intr_mask.pa1 = regk_gio_yes;
905 intr_cfg.pa2 = regk_gio_lo;
906 intr_mask.pa2 = regk_gio_yes;
909 intr_cfg.pa3 = regk_gio_lo;
910 intr_mask.pa3 = regk_gio_yes;
913 intr_cfg.pa4 = regk_gio_lo;
914 intr_mask.pa4 = regk_gio_yes;
917 intr_cfg.pa5 = regk_gio_lo;
918 intr_mask.pa5 = regk_gio_yes;
921 intr_cfg.pa6 = regk_gio_lo;
922 intr_mask.pa6 = regk_gio_yes;
925 intr_cfg.pa7 = regk_gio_lo;
926 intr_mask.pa7 = regk_gio_yes;
930 REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
931 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
933 gpio_pa_low_alarms |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
934 gpio_some_alarms = 1;
938 /* main driver initialization routine, called from mem.c */
945 /* do the formalities */
947 res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
949 printk(KERN_ERR "gpio: couldn't get a major number.\n");
954 CRIS_LED_NETWORK_GRP0_SET(0);
955 CRIS_LED_NETWORK_GRP1_SET(0);
956 CRIS_LED_ACTIVE_SET(0);
957 CRIS_LED_DISK_READ(0);
958 CRIS_LED_DISK_WRITE(0);
960 printk(KERN_INFO "ETRAX FS GPIO driver v2.5, (c) 2003-2007 "
961 "Axis Communications AB\n");
962 /* We call etrax_gpio_wake_up_check() from timer interrupt and
963 * from cpu_idle() in kernel/process.c
964 * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
967 if (request_irq(TIMER0_INTR_VECT, gpio_poll_timer_interrupt,
968 IRQF_SHARED | IRQF_DISABLED, "gpio poll", &alarmlist))
969 printk(KERN_ERR "timer0 irq for gpio\n");
971 if (request_irq(GIO_INTR_VECT, gpio_pa_interrupt,
972 IRQF_SHARED | IRQF_DISABLED, "gpio PA", &alarmlist))
973 printk(KERN_ERR "PA irq for gpio\n");
975 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
982 /* this makes sure that gpio_init is called during kernel boot */
984 module_init(gpio_init);