2 * Artec-3 general port I/O device
4 * Copyright (c) 2007 Axis Communications AB
6 * Authors: Bjorn Wesen (initial version)
7 * Ola Knutsson (LED handling)
8 * Johan Adolfsson (read/set directions, write, port G,
10 * Ricard Wanderlof (PWM for Artpec-3)
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/ioport.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/poll.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/smp_lock.h>
28 #include <asm/etraxgpio.h>
29 #include <hwregs/reg_map.h>
30 #include <hwregs/reg_rdwr.h>
31 #include <hwregs/gio_defs.h>
32 #include <hwregs/intr_vect_defs.h>
34 #include <asm/system.h>
36 #include <mach/pinmux.h>
38 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
41 #define VIRT_I2C_ADDR 0x40
44 /* The following gio ports on ARTPEC-3 is available:
48 * each port has a rw_px_dout, r_px_din and rw_px_oe register.
51 #define GPIO_MAJOR 120 /* experimental MAJOR number */
53 #define I2C_INTERRUPT_BITS 0x300 /* i2c0_done and i2c1_done bits */
62 if (dp_cnt % 1000 == 0) \
69 static char gpio_name[] = "etrax gpio";
71 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
72 static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
75 static int gpio_ioctl(struct inode *inode, struct file *file,
76 unsigned int cmd, unsigned long arg);
77 static ssize_t gpio_write(struct file *file, const char __user *buf,
78 size_t count, loff_t *off);
79 static int gpio_open(struct inode *inode, struct file *filp);
80 static int gpio_release(struct inode *inode, struct file *filp);
81 static unsigned int gpio_poll(struct file *filp,
82 struct poll_table_struct *wait);
84 /* private data per open() of this driver */
87 struct gpio_private *next;
88 /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
89 unsigned char clk_mask;
90 unsigned char data_mask;
91 unsigned char write_msb;
93 /* These fields are generic */
94 unsigned long highalarm, lowalarm;
95 wait_queue_head_t alarm_wq;
99 static void gpio_set_alarm(struct gpio_private *priv);
100 static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
101 static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
105 /* linked list of alarms to check for */
107 static struct gpio_private *alarmlist;
109 static int wanted_interrupts;
111 static DEFINE_SPINLOCK(gpio_lock);
113 #define NUM_PORTS (GPIO_MINOR_LAST+1)
114 #define GIO_REG_RD_ADDR(reg) \
115 (unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
116 #define GIO_REG_WR_ADDR(reg) \
117 (unsigned long *)(regi_gio + REG_WR_ADDR_gio_##reg)
118 static unsigned long led_dummy;
119 static unsigned long port_d_dummy; /* Only input on Artpec-3 */
120 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
121 static unsigned long port_e_dummy; /* Non existent on Artpec-3 */
122 static unsigned long virtual_dummy;
123 static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
124 static unsigned short cached_virtual_gpio_read;
127 static unsigned long *data_out[NUM_PORTS] = {
128 GIO_REG_WR_ADDR(rw_pa_dout),
129 GIO_REG_WR_ADDR(rw_pb_dout),
131 GIO_REG_WR_ADDR(rw_pc_dout),
133 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
139 static unsigned long *data_in[NUM_PORTS] = {
140 GIO_REG_RD_ADDR(r_pa_din),
141 GIO_REG_RD_ADDR(r_pb_din),
143 GIO_REG_RD_ADDR(r_pc_din),
144 GIO_REG_RD_ADDR(r_pd_din),
145 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
151 static unsigned long changeable_dir[NUM_PORTS] = {
152 CONFIG_ETRAX_PA_CHANGEABLE_DIR,
153 CONFIG_ETRAX_PB_CHANGEABLE_DIR,
155 CONFIG_ETRAX_PC_CHANGEABLE_DIR,
157 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
159 CONFIG_ETRAX_PV_CHANGEABLE_DIR,
163 static unsigned long changeable_bits[NUM_PORTS] = {
164 CONFIG_ETRAX_PA_CHANGEABLE_BITS,
165 CONFIG_ETRAX_PB_CHANGEABLE_BITS,
167 CONFIG_ETRAX_PC_CHANGEABLE_BITS,
169 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
171 CONFIG_ETRAX_PV_CHANGEABLE_BITS,
175 static unsigned long *dir_oe[NUM_PORTS] = {
176 GIO_REG_WR_ADDR(rw_pa_oe),
177 GIO_REG_WR_ADDR(rw_pb_oe),
179 GIO_REG_WR_ADDR(rw_pc_oe),
181 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
187 static void gpio_set_alarm(struct gpio_private *priv)
195 spin_lock_irqsave(&gpio_lock, flags);
196 intr_cfg = REG_RD_INT(gio, regi_gio, rw_intr_cfg);
197 pins = REG_RD_INT(gio, regi_gio, rw_intr_pins);
198 mask = REG_RD_INT(gio, regi_gio, rw_intr_mask) & I2C_INTERRUPT_BITS;
200 for (bit = 0; bit < 32; bit++) {
203 if (priv->minor < GPIO_MINOR_LEDS)
204 pin += priv->minor * 4;
206 pin += (priv->minor - 1) * 4;
208 if (priv->highalarm & (1<<bit)) {
209 intr_cfg |= (regk_gio_hi << (intr * 3));
211 wanted_interrupts = mask & 0xff;
212 pins |= pin << (intr * 4);
213 } else if (priv->lowalarm & (1<<bit)) {
214 intr_cfg |= (regk_gio_lo << (intr * 3));
216 wanted_interrupts = mask & 0xff;
217 pins |= pin << (intr * 4);
221 REG_WR_INT(gio, regi_gio, rw_intr_cfg, intr_cfg);
222 REG_WR_INT(gio, regi_gio, rw_intr_pins, pins);
223 REG_WR_INT(gio, regi_gio, rw_intr_mask, mask);
225 spin_unlock_irqrestore(&gpio_lock, flags);
228 static unsigned int gpio_poll(struct file *file, struct poll_table_struct *wait)
230 unsigned int mask = 0;
231 struct gpio_private *priv = file->private_data;
235 if (priv->minor >= GPIO_MINOR_PWM0 &&
236 priv->minor <= GPIO_MINOR_LAST_PWM)
239 poll_wait(file, &priv->alarm_wq, wait);
240 if (priv->minor <= GPIO_MINOR_D) {
241 data = readl(data_in[priv->minor]);
242 REG_WR_INT(gio, regi_gio, rw_ack_intr, wanted_interrupts);
243 tmp = REG_RD_INT(gio, regi_gio, rw_intr_mask);
244 tmp &= I2C_INTERRUPT_BITS;
245 tmp |= wanted_interrupts;
246 REG_WR_INT(gio, regi_gio, rw_intr_mask, tmp);
250 if ((data & priv->highalarm) || (~data & priv->lowalarm))
251 mask = POLLIN|POLLRDNORM;
253 DP(printk(KERN_DEBUG "gpio_poll ready: mask 0x%08X\n", mask));
257 static irqreturn_t gpio_interrupt(int irq, void *dev_id)
259 reg_gio_rw_intr_mask intr_mask;
260 reg_gio_r_masked_intr masked_intr;
261 reg_gio_rw_ack_intr ack_intr;
265 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
266 unsigned char enable_gpiov_ack = 0;
269 /* Find what PA interrupts are active */
270 masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
271 tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
273 /* Find those that we have enabled */
274 spin_lock_irqsave(&gpio_lock, flags);
275 tmp &= wanted_interrupts;
276 spin_unlock_irqrestore(&gpio_lock, flags);
278 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
279 /* Something changed on virtual GPIO. Interrupt is acked by
280 * reading the device.
282 if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
283 i2c_read(VIRT_I2C_ADDR, (void *)&cached_virtual_gpio_read,
284 sizeof(cached_virtual_gpio_read));
285 enable_gpiov_ack = 1;
290 ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
291 REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
293 /* Disable those interrupts.. */
294 intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
295 tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
297 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
298 /* Do not disable interrupt on virtual GPIO. Changes on virtual
299 * pins are only noticed by an interrupt.
301 if (enable_gpiov_ack)
302 tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
304 intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
305 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
307 return IRQ_RETVAL(tmp);
310 static void gpio_write_bit(unsigned long *port, unsigned char data, int bit,
311 unsigned char clk_mask, unsigned char data_mask)
313 unsigned long shadow = readl(port) & ~clk_mask;
314 writel(shadow, port);
318 shadow &= ~data_mask;
319 writel(shadow, port);
320 /* For FPGA: min 5.0ns (DCC) before CCLK high */
322 writel(shadow, port);
325 static void gpio_write_byte(struct gpio_private *priv, unsigned long *port,
331 for (i = 7; i >= 0; i--)
332 gpio_write_bit(port, data, i, priv->clk_mask,
335 for (i = 0; i <= 7; i++)
336 gpio_write_bit(port, data, i, priv->clk_mask,
341 static ssize_t gpio_write(struct file *file, const char __user *buf,
342 size_t count, loff_t *off)
344 struct gpio_private *priv = file->private_data;
346 ssize_t retval = count;
347 /* Only bits 0-7 may be used for write operations but allow all
348 devices except leds... */
349 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
350 if (priv->minor == GPIO_MINOR_V)
353 if (priv->minor == GPIO_MINOR_LEDS)
356 if (priv->minor >= GPIO_MINOR_PWM0 &&
357 priv->minor <= GPIO_MINOR_LAST_PWM)
360 if (!access_ok(VERIFY_READ, buf, count))
363 /* It must have been configured using the IO_CFG_WRITE_MODE */
364 /* Perhaps a better error code? */
365 if (priv->clk_mask == 0 || priv->data_mask == 0)
368 D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
370 count, priv->data_mask, priv->clk_mask, priv->write_msb));
372 spin_lock_irqsave(&gpio_lock, flags);
375 gpio_write_byte(priv, data_out[priv->minor], *buf++);
377 spin_unlock_irqrestore(&gpio_lock, flags);
381 static int gpio_open(struct inode *inode, struct file *filp)
383 struct gpio_private *priv;
384 int p = iminor(inode);
386 if (p > GPIO_MINOR_LAST_PWM ||
387 (p > GPIO_MINOR_LAST && p < GPIO_MINOR_PWM0))
390 priv = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
396 memset(priv, 0, sizeof(*priv));
399 filp->private_data = priv;
401 /* initialize the io/alarm struct, not for PWM ports though */
402 if (p <= GPIO_MINOR_LAST) {
409 init_waitqueue_head(&priv->alarm_wq);
411 /* link it into our alarmlist */
412 spin_lock_irq(&gpio_lock);
413 priv->next = alarmlist;
415 spin_unlock_irq(&gpio_lock);
422 static int gpio_release(struct inode *inode, struct file *filp)
424 struct gpio_private *p;
425 struct gpio_private *todel;
426 /* local copies while updating them: */
427 unsigned long a_high, a_low;
429 /* prepare to free private structure */
430 todel = filp->private_data;
432 /* unlink from alarmlist - only for non-PWM ports though */
433 if (todel->minor <= GPIO_MINOR_LAST) {
434 spin_lock_irq(&gpio_lock);
438 alarmlist = todel->next;
440 while (p->next != todel)
442 p->next = todel->next;
445 /* Check if there are still any alarms set */
450 if (p->minor == GPIO_MINOR_A) {
451 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
452 p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
454 a_high |= p->highalarm;
455 a_low |= p->lowalarm;
461 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
462 /* Variable 'a_low' needs to be set here again
463 * to ensure that interrupt for virtual GPIO is handled.
465 a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
468 spin_unlock_irq(&gpio_lock);
475 /* Main device API. ioctl's to read/set/clear bits, as well as to
476 * set alarms to wait for using a subsequent select().
479 inline unsigned long setget_input(struct gpio_private *priv, unsigned long arg)
481 /* Set direction 0=unchanged 1=input,
482 * return mask with 1=input
485 unsigned long dir_shadow;
487 spin_lock_irqsave(&gpio_lock, flags);
489 dir_shadow = readl(dir_oe[priv->minor]) &
490 ~(arg & changeable_dir[priv->minor]);
491 writel(dir_shadow, dir_oe[priv->minor]);
493 spin_unlock_irqrestore(&gpio_lock, flags);
495 if (priv->minor == GPIO_MINOR_C)
496 dir_shadow ^= 0xFFFF; /* Only 16 bits */
497 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
498 else if (priv->minor == GPIO_MINOR_V)
499 dir_shadow ^= 0xFFFF; /* Only 16 bits */
502 dir_shadow ^= 0xFFFFFFFF; /* PA, PB and PD 32 bits */
508 static inline unsigned long setget_output(struct gpio_private *priv,
512 unsigned long dir_shadow;
514 spin_lock_irqsave(&gpio_lock, flags);
516 dir_shadow = readl(dir_oe[priv->minor]) |
517 (arg & changeable_dir[priv->minor]);
518 writel(dir_shadow, dir_oe[priv->minor]);
520 spin_unlock_irqrestore(&gpio_lock, flags);
522 } /* setget_output */
524 static int gpio_ioctl(struct inode *inode, struct file *file,
525 unsigned int cmd, unsigned long arg)
529 unsigned long shadow;
530 struct gpio_private *priv = file->private_data;
532 if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
535 /* Check for special ioctl handlers first */
537 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
538 if (priv->minor == GPIO_MINOR_V)
539 return virtual_gpio_ioctl(file, cmd, arg);
542 if (priv->minor == GPIO_MINOR_LEDS)
543 return gpio_leds_ioctl(cmd, arg);
545 if (priv->minor >= GPIO_MINOR_PWM0 &&
546 priv->minor <= GPIO_MINOR_LAST_PWM)
547 return gpio_pwm_ioctl(priv, cmd, arg);
549 switch (_IOC_NR(cmd)) {
550 case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
552 return readl(data_in[priv->minor]);
554 spin_lock_irqsave(&gpio_lock, flags);
555 /* Set changeable bits with a 1 in arg. */
556 shadow = readl(data_out[priv->minor]) |
557 (arg & changeable_bits[priv->minor]);
558 writel(shadow, data_out[priv->minor]);
559 spin_unlock_irqrestore(&gpio_lock, flags);
562 spin_lock_irqsave(&gpio_lock, flags);
563 /* Clear changeable bits with a 1 in arg. */
564 shadow = readl(data_out[priv->minor]) &
565 ~(arg & changeable_bits[priv->minor]);
566 writel(shadow, data_out[priv->minor]);
567 spin_unlock_irqrestore(&gpio_lock, flags);
570 /* Set alarm when bits with 1 in arg go high. */
571 priv->highalarm |= arg;
572 gpio_set_alarm(priv);
575 /* Set alarm when bits with 1 in arg go low. */
576 priv->lowalarm |= arg;
577 gpio_set_alarm(priv);
580 /* Clear alarm for bits with 1 in arg. */
581 priv->highalarm &= ~arg;
582 priv->lowalarm &= ~arg;
583 gpio_set_alarm(priv);
585 case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
586 /* Read direction 0=input 1=output */
587 return readl(dir_oe[priv->minor]);
589 case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
590 /* Set direction 0=unchanged 1=input,
591 * return mask with 1=input
593 return setget_input(priv, arg);
595 case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
596 /* Set direction 0=unchanged 1=output,
597 * return mask with 1=output
599 return setget_output(priv, arg);
601 case IO_CFG_WRITE_MODE:
604 unsigned long dir_shadow, clk_mask, data_mask, write_msb;
606 clk_mask = arg & 0xFF;
607 data_mask = (arg >> 8) & 0xFF;
608 write_msb = (arg >> 16) & 0x01;
610 /* Check if we're allowed to change the bits and
611 * the direction is correct
613 spin_lock_irqsave(&gpio_lock, flags);
614 dir_shadow = readl(dir_oe[priv->minor]);
615 if ((clk_mask & changeable_bits[priv->minor]) &&
616 (data_mask & changeable_bits[priv->minor]) &&
617 (clk_mask & dir_shadow) &&
618 (data_mask & dir_shadow)) {
619 priv->clk_mask = clk_mask;
620 priv->data_mask = data_mask;
621 priv->write_msb = write_msb;
624 spin_unlock_irqrestore(&gpio_lock, flags);
629 /* *arg is result of reading the input pins */
630 val = readl(data_in[priv->minor]);
631 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
634 case IO_READ_OUTBITS:
635 /* *arg is result of reading the output shadow */
636 val = *data_out[priv->minor];
637 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
640 case IO_SETGET_INPUT:
641 /* bits set in *arg is set to input,
642 * *arg updated with current input pins.
644 if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
646 val = setget_input(priv, val);
647 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
650 case IO_SETGET_OUTPUT:
651 /* bits set in *arg is set to output,
652 * *arg updated with current output pins.
654 if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
656 val = setget_output(priv, val);
657 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
667 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
668 static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
673 unsigned short shadow;
674 struct gpio_private *priv = file->private_data;
676 switch (_IOC_NR(cmd)) {
678 spin_lock_irqsave(&gpio_lock, flags);
679 /* Set changeable bits with a 1 in arg. */
680 i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
681 shadow |= ~readl(dir_oe[priv->minor]) |
682 (arg & changeable_bits[priv->minor]);
683 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
684 spin_lock_irqrestore(&gpio_lock, flags);
687 spin_lock_irqsave(&gpio_lock, flags);
688 /* Clear changeable bits with a 1 in arg. */
689 i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
690 shadow |= ~readl(dir_oe[priv->minor]) &
691 ~(arg & changeable_bits[priv->minor]);
692 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
693 spin_lock_irqrestore(&gpio_lock, flags);
696 /* Set alarm when bits with 1 in arg go high. */
697 priv->highalarm |= arg;
700 /* Set alarm when bits with 1 in arg go low. */
701 priv->lowalarm |= arg;
704 /* Clear alarm for bits with 1 in arg. */
705 priv->highalarm &= ~arg;
706 priv->lowalarm &= ~arg;
708 case IO_CFG_WRITE_MODE:
710 unsigned long dir_shadow;
711 dir_shadow = readl(dir_oe[priv->minor]);
713 priv->clk_mask = arg & 0xFF;
714 priv->data_mask = (arg >> 8) & 0xFF;
715 priv->write_msb = (arg >> 16) & 0x01;
716 /* Check if we're allowed to change the bits and
717 * the direction is correct
719 if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
720 (priv->data_mask & changeable_bits[priv->minor]) &&
721 (priv->clk_mask & dir_shadow) &&
722 (priv->data_mask & dir_shadow))) {
730 /* *arg is result of reading the input pins */
731 val = cached_virtual_gpio_read & ~readl(dir_oe[priv->minor]);
732 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
736 case IO_READ_OUTBITS:
737 /* *arg is result of reading the output shadow */
738 i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
739 val &= readl(dir_oe[priv->minor]);
740 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
743 case IO_SETGET_INPUT:
745 /* bits set in *arg is set to input,
746 * *arg updated with current input pins.
748 unsigned short input_mask = ~readl(dir_oe[priv->minor]);
749 if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
751 val = setget_input(priv, val);
752 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
754 if ((input_mask & val) != input_mask) {
755 /* Input pins changed. All ports desired as input
756 * should be set to logic 1.
758 unsigned short change = input_mask ^ val;
759 i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
763 i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
768 case IO_SETGET_OUTPUT:
769 /* bits set in *arg is set to output,
770 * *arg updated with current output pins.
772 if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
774 val = setget_output(priv, val);
775 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
783 #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
785 static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
790 switch (_IOC_NR(cmd)) {
791 case IO_LEDACTIVE_SET:
792 green = ((unsigned char) arg) & 1;
793 red = (((unsigned char) arg) >> 1) & 1;
794 CRIS_LED_ACTIVE_SET_G(green);
795 CRIS_LED_ACTIVE_SET_R(red);
805 static int gpio_pwm_set_mode(unsigned long arg, int pwm_port)
807 int pinmux_pwm = pinmux_pwm0 + pwm_port;
809 reg_gio_rw_pwm0_ctrl rw_pwm_ctrl = {
811 .ccd_override = regk_gio_no,
816 if (get_user(mode, &((struct io_pwm_set_mode *) arg)->mode))
818 rw_pwm_ctrl.mode = mode;
820 allocstatus = crisv32_pinmux_alloc_fixed(pinmux_pwm);
822 allocstatus = crisv32_pinmux_dealloc_fixed(pinmux_pwm);
825 REG_WRITE(reg_gio_rw_pwm0_ctrl, REG_ADDR(gio, regi_gio, rw_pwm0_ctrl) +
826 12 * pwm_port, rw_pwm_ctrl);
830 static int gpio_pwm_set_period(unsigned long arg, int pwm_port)
832 struct io_pwm_set_period periods;
833 reg_gio_rw_pwm0_var rw_pwm_widths;
835 if (copy_from_user(&periods, (void __user *)arg, sizeof(periods)))
837 if (periods.lo > 8191 || periods.hi > 8191)
839 rw_pwm_widths.lo = periods.lo;
840 rw_pwm_widths.hi = periods.hi;
841 REG_WRITE(reg_gio_rw_pwm0_var, REG_ADDR(gio, regi_gio, rw_pwm0_var) +
842 12 * pwm_port, rw_pwm_widths);
846 static int gpio_pwm_set_duty(unsigned long arg, int pwm_port)
849 reg_gio_rw_pwm0_data rw_pwm_duty;
851 if (get_user(duty, &((struct io_pwm_set_duty *) arg)->duty))
855 rw_pwm_duty.data = duty;
856 REG_WRITE(reg_gio_rw_pwm0_data, REG_ADDR(gio, regi_gio, rw_pwm0_data) +
857 12 * pwm_port, rw_pwm_duty);
861 static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
864 int pwm_port = priv->minor - GPIO_MINOR_PWM0;
866 switch (_IOC_NR(cmd)) {
867 case IO_PWM_SET_MODE:
868 return gpio_pwm_set_mode(arg, pwm_port);
869 case IO_PWM_SET_PERIOD:
870 return gpio_pwm_set_period(arg, pwm_port);
871 case IO_PWM_SET_DUTY:
872 return gpio_pwm_set_duty(arg, pwm_port);
879 static const struct file_operations gpio_fops = {
880 .owner = THIS_MODULE,
885 .release = gpio_release,
888 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
889 static void __init virtual_gpio_init(void)
891 reg_gio_rw_intr_cfg intr_cfg;
892 reg_gio_rw_intr_mask intr_mask;
893 unsigned short shadow;
895 shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
896 shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
897 i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
899 /* Set interrupt mask and on what state the interrupt shall trigger.
900 * For virtual gpio the interrupt shall trigger on logic '0'.
902 intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
903 intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
905 switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
907 intr_cfg.pa0 = regk_gio_lo;
908 intr_mask.pa0 = regk_gio_yes;
911 intr_cfg.pa1 = regk_gio_lo;
912 intr_mask.pa1 = regk_gio_yes;
915 intr_cfg.pa2 = regk_gio_lo;
916 intr_mask.pa2 = regk_gio_yes;
919 intr_cfg.pa3 = regk_gio_lo;
920 intr_mask.pa3 = regk_gio_yes;
923 intr_cfg.pa4 = regk_gio_lo;
924 intr_mask.pa4 = regk_gio_yes;
927 intr_cfg.pa5 = regk_gio_lo;
928 intr_mask.pa5 = regk_gio_yes;
931 intr_cfg.pa6 = regk_gio_lo;
932 intr_mask.pa6 = regk_gio_yes;
935 intr_cfg.pa7 = regk_gio_lo;
936 intr_mask.pa7 = regk_gio_yes;
940 REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
941 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
945 /* main driver initialization routine, called from mem.c */
947 static int __init gpio_init(void)
951 printk(KERN_INFO "ETRAX FS GPIO driver v2.7, (c) 2003-2008 "
952 "Axis Communications AB\n");
954 /* do the formalities */
956 res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
958 printk(KERN_ERR "gpio: couldn't get a major number.\n");
963 CRIS_LED_NETWORK_GRP0_SET(0);
964 CRIS_LED_NETWORK_GRP1_SET(0);
965 CRIS_LED_ACTIVE_SET(0);
966 CRIS_LED_DISK_READ(0);
967 CRIS_LED_DISK_WRITE(0);
969 int res2 = request_irq(GIO_INTR_VECT, gpio_interrupt,
970 IRQF_SHARED | IRQF_DISABLED, "gpio", &alarmlist);
972 printk(KERN_ERR "err: irq for gpio\n");
976 /* No IRQs by default. */
977 REG_WR_INT(gio, regi_gio, rw_intr_pins, 0);
979 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
986 /* this makes sure that gpio_init is called during kernel boot */
988 module_init(gpio_init);