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/mutex.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 DEFINE_MUTEX(gpio_mutex
);
70 static char gpio_name
[] = "etrax gpio";
72 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
73 static int virtual_gpio_ioctl(struct file
*file
, unsigned int cmd
,
76 static long gpio_ioctl(struct file
*file
, 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
);
395 mutex_lock(&gpio_mutex
);
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
);
418 mutex_unlock(&gpio_mutex
);
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 long gpio_ioctl_unlocked(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 static long gpio_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
671 mutex_lock(&gpio_mutex
);
672 ret
= gpio_ioctl_unlocked(file
, cmd
, arg
);
673 mutex_unlock(&gpio_mutex
);
678 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
679 static int virtual_gpio_ioctl(struct file
*file
, unsigned int cmd
,
684 unsigned short shadow
;
685 struct gpio_private
*priv
= file
->private_data
;
687 switch (_IOC_NR(cmd
)) {
689 spin_lock_irqsave(&gpio_lock
, flags
);
690 /* Set changeable bits with a 1 in arg. */
691 i2c_read(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
692 shadow
|= ~readl(dir_oe
[priv
->minor
]) |
693 (arg
& changeable_bits
[priv
->minor
]);
694 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
695 spin_unlock_irqrestore(&gpio_lock
, flags
);
698 spin_lock_irqsave(&gpio_lock
, flags
);
699 /* Clear changeable bits with a 1 in arg. */
700 i2c_read(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
701 shadow
|= ~readl(dir_oe
[priv
->minor
]) &
702 ~(arg
& changeable_bits
[priv
->minor
]);
703 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
704 spin_unlock_irqrestore(&gpio_lock
, flags
);
707 /* Set alarm when bits with 1 in arg go high. */
708 priv
->highalarm
|= arg
;
711 /* Set alarm when bits with 1 in arg go low. */
712 priv
->lowalarm
|= arg
;
715 /* Clear alarm for bits with 1 in arg. */
716 priv
->highalarm
&= ~arg
;
717 priv
->lowalarm
&= ~arg
;
719 case IO_CFG_WRITE_MODE
:
721 unsigned long dir_shadow
;
722 dir_shadow
= readl(dir_oe
[priv
->minor
]);
724 priv
->clk_mask
= arg
& 0xFF;
725 priv
->data_mask
= (arg
>> 8) & 0xFF;
726 priv
->write_msb
= (arg
>> 16) & 0x01;
727 /* Check if we're allowed to change the bits and
728 * the direction is correct
730 if (!((priv
->clk_mask
& changeable_bits
[priv
->minor
]) &&
731 (priv
->data_mask
& changeable_bits
[priv
->minor
]) &&
732 (priv
->clk_mask
& dir_shadow
) &&
733 (priv
->data_mask
& dir_shadow
))) {
741 /* *arg is result of reading the input pins */
742 val
= cached_virtual_gpio_read
& ~readl(dir_oe
[priv
->minor
]);
743 if (copy_to_user((void __user
*)arg
, &val
, sizeof(val
)))
747 case IO_READ_OUTBITS
:
748 /* *arg is result of reading the output shadow */
749 i2c_read(VIRT_I2C_ADDR
, (void *)&val
, sizeof(val
));
750 val
&= readl(dir_oe
[priv
->minor
]);
751 if (copy_to_user((void __user
*)arg
, &val
, sizeof(val
)))
754 case IO_SETGET_INPUT
:
756 /* bits set in *arg is set to input,
757 * *arg updated with current input pins.
759 unsigned short input_mask
= ~readl(dir_oe
[priv
->minor
]);
760 if (copy_from_user(&val
, (void __user
*)arg
, sizeof(val
)))
762 val
= setget_input(priv
, val
);
763 if (copy_to_user((void __user
*)arg
, &val
, sizeof(val
)))
765 if ((input_mask
& val
) != input_mask
) {
766 /* Input pins changed. All ports desired as input
767 * should be set to logic 1.
769 unsigned short change
= input_mask
^ val
;
770 i2c_read(VIRT_I2C_ADDR
, (void *)&shadow
,
774 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
,
779 case IO_SETGET_OUTPUT
:
780 /* bits set in *arg is set to output,
781 * *arg updated with current output pins.
783 if (copy_from_user(&val
, (void __user
*)arg
, sizeof(val
)))
785 val
= setget_output(priv
, val
);
786 if (copy_to_user((void __user
*)arg
, &val
, sizeof(val
)))
794 #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
796 static int gpio_leds_ioctl(unsigned int cmd
, unsigned long arg
)
801 switch (_IOC_NR(cmd
)) {
802 case IO_LEDACTIVE_SET
:
803 green
= ((unsigned char) arg
) & 1;
804 red
= (((unsigned char) arg
) >> 1) & 1;
805 CRIS_LED_ACTIVE_SET_G(green
);
806 CRIS_LED_ACTIVE_SET_R(red
);
816 static int gpio_pwm_set_mode(unsigned long arg
, int pwm_port
)
818 int pinmux_pwm
= pinmux_pwm0
+ pwm_port
;
820 reg_gio_rw_pwm0_ctrl rw_pwm_ctrl
= {
822 .ccd_override
= regk_gio_no
,
827 if (get_user(mode
, &((struct io_pwm_set_mode
*) arg
)->mode
))
829 rw_pwm_ctrl
.mode
= mode
;
831 allocstatus
= crisv32_pinmux_alloc_fixed(pinmux_pwm
);
833 allocstatus
= crisv32_pinmux_dealloc_fixed(pinmux_pwm
);
836 REG_WRITE(reg_gio_rw_pwm0_ctrl
, REG_ADDR(gio
, regi_gio
, rw_pwm0_ctrl
) +
837 12 * pwm_port
, rw_pwm_ctrl
);
841 static int gpio_pwm_set_period(unsigned long arg
, int pwm_port
)
843 struct io_pwm_set_period periods
;
844 reg_gio_rw_pwm0_var rw_pwm_widths
;
846 if (copy_from_user(&periods
, (void __user
*)arg
, sizeof(periods
)))
848 if (periods
.lo
> 8191 || periods
.hi
> 8191)
850 rw_pwm_widths
.lo
= periods
.lo
;
851 rw_pwm_widths
.hi
= periods
.hi
;
852 REG_WRITE(reg_gio_rw_pwm0_var
, REG_ADDR(gio
, regi_gio
, rw_pwm0_var
) +
853 12 * pwm_port
, rw_pwm_widths
);
857 static int gpio_pwm_set_duty(unsigned long arg
, int pwm_port
)
860 reg_gio_rw_pwm0_data rw_pwm_duty
;
862 if (get_user(duty
, &((struct io_pwm_set_duty
*) arg
)->duty
))
866 rw_pwm_duty
.data
= duty
;
867 REG_WRITE(reg_gio_rw_pwm0_data
, REG_ADDR(gio
, regi_gio
, rw_pwm0_data
) +
868 12 * pwm_port
, rw_pwm_duty
);
872 static int gpio_pwm_ioctl(struct gpio_private
*priv
, unsigned int cmd
,
875 int pwm_port
= priv
->minor
- GPIO_MINOR_PWM0
;
877 switch (_IOC_NR(cmd
)) {
878 case IO_PWM_SET_MODE
:
879 return gpio_pwm_set_mode(arg
, pwm_port
);
880 case IO_PWM_SET_PERIOD
:
881 return gpio_pwm_set_period(arg
, pwm_port
);
882 case IO_PWM_SET_DUTY
:
883 return gpio_pwm_set_duty(arg
, pwm_port
);
890 static const struct file_operations gpio_fops
= {
891 .owner
= THIS_MODULE
,
893 .unlocked_ioctl
= gpio_ioctl
,
896 .release
= gpio_release
,
897 .llseek
= noop_llseek
,
900 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
901 static void __init
virtual_gpio_init(void)
903 reg_gio_rw_intr_cfg intr_cfg
;
904 reg_gio_rw_intr_mask intr_mask
;
905 unsigned short shadow
;
907 shadow
= ~virtual_rw_pv_oe
; /* Input ports should be set to logic 1 */
908 shadow
|= CONFIG_ETRAX_DEF_GIO_PV_OUT
;
909 i2c_write(VIRT_I2C_ADDR
, (void *)&shadow
, sizeof(shadow
));
911 /* Set interrupt mask and on what state the interrupt shall trigger.
912 * For virtual gpio the interrupt shall trigger on logic '0'.
914 intr_cfg
= REG_RD(gio
, regi_gio
, rw_intr_cfg
);
915 intr_mask
= REG_RD(gio
, regi_gio
, rw_intr_mask
);
917 switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN
) {
919 intr_cfg
.pa0
= regk_gio_lo
;
920 intr_mask
.pa0
= regk_gio_yes
;
923 intr_cfg
.pa1
= regk_gio_lo
;
924 intr_mask
.pa1
= regk_gio_yes
;
927 intr_cfg
.pa2
= regk_gio_lo
;
928 intr_mask
.pa2
= regk_gio_yes
;
931 intr_cfg
.pa3
= regk_gio_lo
;
932 intr_mask
.pa3
= regk_gio_yes
;
935 intr_cfg
.pa4
= regk_gio_lo
;
936 intr_mask
.pa4
= regk_gio_yes
;
939 intr_cfg
.pa5
= regk_gio_lo
;
940 intr_mask
.pa5
= regk_gio_yes
;
943 intr_cfg
.pa6
= regk_gio_lo
;
944 intr_mask
.pa6
= regk_gio_yes
;
947 intr_cfg
.pa7
= regk_gio_lo
;
948 intr_mask
.pa7
= regk_gio_yes
;
952 REG_WR(gio
, regi_gio
, rw_intr_cfg
, intr_cfg
);
953 REG_WR(gio
, regi_gio
, rw_intr_mask
, intr_mask
);
957 /* main driver initialization routine, called from mem.c */
959 static int __init
gpio_init(void)
963 printk(KERN_INFO
"ETRAX FS GPIO driver v2.7, (c) 2003-2008 "
964 "Axis Communications AB\n");
966 /* do the formalities */
968 res
= register_chrdev(GPIO_MAJOR
, gpio_name
, &gpio_fops
);
970 printk(KERN_ERR
"gpio: couldn't get a major number.\n");
975 CRIS_LED_NETWORK_GRP0_SET(0);
976 CRIS_LED_NETWORK_GRP1_SET(0);
977 CRIS_LED_ACTIVE_SET(0);
978 CRIS_LED_DISK_READ(0);
979 CRIS_LED_DISK_WRITE(0);
981 int res2
= request_irq(GIO_INTR_VECT
, gpio_interrupt
,
982 IRQF_SHARED
| IRQF_DISABLED
, "gpio", &alarmlist
);
984 printk(KERN_ERR
"err: irq for gpio\n");
988 /* No IRQs by default. */
989 REG_WR_INT(gio
, regi_gio
, rw_intr_pins
, 0);
991 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
998 /* this makes sure that gpio_init is called during kernel boot */
1000 module_init(gpio_init
);