14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/poll.h>
29 #include <hwregs/reg_map.h>
31 #include <hwregs/gio_defs.h>
32 #include <hwregs/intr_vect_defs.h>
35 #include <mach/pinmux.h>
37 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
40 #define VIRT_I2C_ADDR 0x40
50 #define GPIO_MAJOR 120
52 #define I2C_INTERRUPT_BITS 0x300
61 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 long gpio_ioctl(
struct file *
file,
unsigned int cmd,
unsigned long arg);
77 size_t count, loff_t *off);
80 static unsigned int gpio_poll(
struct file *filp,
99 static int gpio_leds_ioctl(
unsigned int cmd,
unsigned long arg);
108 static int wanted_interrupts;
112 #define NUM_PORTS (GPIO_MINOR_LAST+1)
113 #define GIO_REG_RD_ADDR(reg) \
114 (unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
115 #define GIO_REG_WR_ADDR(reg) \
116 (unsigned long *)(regi_gio + REG_WR_ADDR_gio_##reg)
118 static unsigned long port_d_dummy;
119 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
120 static unsigned long port_e_dummy;
121 static unsigned long virtual_dummy;
122 static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
123 static unsigned short cached_virtual_gpio_read;
126 static unsigned long *data_out[
NUM_PORTS] = {
132 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
138 static unsigned long *data_in[
NUM_PORTS] = {
144 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
154 CONFIG_ETRAX_PC_CHANGEABLE_DIR,
156 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
158 CONFIG_ETRAX_PV_CHANGEABLE_DIR,
166 CONFIG_ETRAX_PC_CHANGEABLE_BITS,
168 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
170 CONFIG_ETRAX_PV_CHANGEABLE_BITS,
174 static unsigned long *dir_oe[
NUM_PORTS] = {
180 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
199 for (bit = 0; bit < 32; bit++) {
202 if (priv->
minor < GPIO_MINOR_LEDS)
203 pin += priv->
minor * 4;
205 pin += (priv->
minor - 1) * 4;
210 wanted_interrupts = mask & 0xff;
211 pins |= pin << (intr * 4);
212 }
else if (priv->
lowalarm & (1<<bit)) {
215 wanted_interrupts = mask & 0xff;
216 pins |= pin << (intr * 4);
224 spin_unlock_irqrestore(&gpio_lock, flags);
229 unsigned int mask = 0;
234 if (priv->
minor >= GPIO_MINOR_PWM0 &&
235 priv->
minor <= GPIO_MINOR_LAST_PWM)
238 poll_wait(file, &priv->
alarm_wq, wait);
239 if (priv->
minor <= GPIO_MINOR_D) {
244 tmp |= wanted_interrupts;
264 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
265 unsigned char enable_gpiov_ack = 0;
274 tmp &= wanted_interrupts;
275 spin_unlock_irqrestore(&gpio_lock, flags);
277 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
281 if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
282 i2c_read(VIRT_I2C_ADDR, (
void *)&cached_virtual_gpio_read,
283 sizeof(cached_virtual_gpio_read));
284 enable_gpiov_ack = 1;
296 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
300 if (enable_gpiov_ack)
301 tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
309 static void gpio_write_bit(
unsigned long *
port,
unsigned char data,
int bit,
317 shadow &= ~data_mask;
324 static void gpio_write_byte(
struct gpio_private *priv,
unsigned long *port,
330 for (i = 7; i >= 0; i--)
331 gpio_write_bit(port, data, i, priv->
clk_mask,
334 for (i = 0; i <= 7; i++)
335 gpio_write_bit(port, data, i, priv->
clk_mask,
341 size_t count, loff_t *off)
348 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
349 if (priv->
minor == GPIO_MINOR_V)
352 if (priv->
minor == GPIO_MINOR_LEDS)
355 if (priv->
minor >= GPIO_MINOR_PWM0 &&
356 priv->
minor <= GPIO_MINOR_LAST_PWM)
374 gpio_write_byte(priv, data_out[priv->
minor], *buf++);
376 spin_unlock_irqrestore(&gpio_lock, flags);
380 static int gpio_open(
struct inode *
inode,
struct file *filp)
383 int p = iminor(inode);
385 if (p > GPIO_MINOR_LAST_PWM ||
386 (p > GPIO_MINOR_LAST && p < GPIO_MINOR_PWM0))
395 memset(priv, 0,
sizeof(*priv));
401 if (p <= GPIO_MINOR_LAST) {
411 spin_lock_irq(&gpio_lock);
412 priv->
next = alarmlist;
414 spin_unlock_irq(&gpio_lock);
421 static int gpio_release(
struct inode *inode,
struct file *filp)
426 unsigned long a_high, a_low;
432 if (todel->
minor <= GPIO_MINOR_LAST) {
433 spin_lock_irq(&gpio_lock);
437 alarmlist = todel->
next;
439 while (p->
next != todel)
449 if (p->
minor == GPIO_MINOR_A) {
450 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
451 p->
lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
460 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
464 a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
467 spin_unlock_irq(&gpio_lock);
492 spin_unlock_irqrestore(&gpio_lock, flags);
494 if (priv->
minor == GPIO_MINOR_C)
495 dir_shadow ^= 0xFFFF;
496 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
497 else if (priv->
minor == GPIO_MINOR_V)
498 dir_shadow ^= 0xFFFF;
501 dir_shadow ^= 0xFFFFFFFF;
519 spin_unlock_irqrestore(&gpio_lock, flags);
523 static long gpio_ioctl_unlocked(
struct file *file,
524 unsigned int cmd,
unsigned long arg)
536 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
537 if (priv->
minor == GPIO_MINOR_V)
538 return virtual_gpio_ioctl(file, cmd, arg);
541 if (priv->
minor == GPIO_MINOR_LEDS)
542 return gpio_leds_ioctl(cmd, arg);
544 if (priv->
minor >= GPIO_MINOR_PWM0 &&
545 priv->
minor <= GPIO_MINOR_LAST_PWM)
546 return gpio_pwm_ioctl(priv, cmd, arg);
558 spin_unlock_irqrestore(&gpio_lock, flags);
566 spin_unlock_irqrestore(&gpio_lock, flags);
571 gpio_set_alarm(priv);
576 gpio_set_alarm(priv);
582 gpio_set_alarm(priv);
605 clk_mask = arg & 0xFF;
606 data_mask = (arg >> 8) & 0xFF;
607 write_msb = (arg >> 16) & 0x01;
615 (data_mask & changeable_bits[priv->
minor]) &&
616 (clk_mask & dir_shadow) &&
617 (data_mask & dir_shadow)) {
623 spin_unlock_irqrestore(&gpio_lock, flags);
630 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
635 val = *data_out[priv->
minor];
636 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
646 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
656 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
666 static long gpio_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
671 ret = gpio_ioctl_unlocked(file, cmd, arg);
677 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
678 static int virtual_gpio_ioctl(
struct file *file,
unsigned int cmd,
690 i2c_read(VIRT_I2C_ADDR, (
void *)&shadow,
sizeof(shadow));
693 i2c_write(VIRT_I2C_ADDR, (
void *)&shadow,
sizeof(shadow));
694 spin_unlock_irqrestore(&gpio_lock, flags);
699 i2c_read(VIRT_I2C_ADDR, (
void *)&shadow,
sizeof(shadow));
702 i2c_write(VIRT_I2C_ADDR, (
void *)&shadow,
sizeof(shadow));
703 spin_unlock_irqrestore(&gpio_lock, flags);
741 val = cached_virtual_gpio_read & ~
readl(dir_oe[priv->
minor]);
742 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
748 i2c_read(VIRT_I2C_ADDR, (
void *)&val,
sizeof(val));
750 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
758 unsigned short input_mask = ~
readl(dir_oe[priv->
minor]);
762 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
764 if ((input_mask & val) != input_mask) {
768 unsigned short change = input_mask ^
val;
769 i2c_read(VIRT_I2C_ADDR, (
void *)&shadow,
773 i2c_write(VIRT_I2C_ADDR, (
void *)&shadow,
785 if (
copy_to_user((
void __user *)arg, &val,
sizeof(val)))
795 static int gpio_leds_ioctl(
unsigned int cmd,
unsigned long arg)
802 green = ((
unsigned char) arg) & 1;
803 red = (((
unsigned char) arg) >> 1) & 1;
815 static int gpio_pwm_set_mode(
unsigned long arg,
int pwm_port)
836 12 * pwm_port, rw_pwm_ctrl);
840 static int gpio_pwm_set_period(
unsigned long arg,
int pwm_port)
845 if (
copy_from_user(&periods, (
void __user *)arg,
sizeof(periods)))
847 if (periods.lo > 8191 || periods.hi > 8191)
849 rw_pwm_widths.lo = periods.lo;
850 rw_pwm_widths.hi = periods.hi;
852 12 * pwm_port, rw_pwm_widths);
856 static int gpio_pwm_set_duty(
unsigned long arg,
int pwm_port)
865 rw_pwm_duty.
data = duty;
867 12 * pwm_port, rw_pwm_duty);
871 static int gpio_pwm_ioctl(
struct gpio_private *priv,
unsigned int cmd,
874 int pwm_port = priv->
minor - GPIO_MINOR_PWM0;
878 return gpio_pwm_set_mode(arg, pwm_port);
880 return gpio_pwm_set_period(arg, pwm_port);
882 return gpio_pwm_set_duty(arg, pwm_port);
892 .unlocked_ioctl = gpio_ioctl,
895 .release = gpio_release,
899 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
900 static void __init virtual_gpio_init(
void)
906 shadow = ~virtual_rw_pv_oe;
907 shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
908 i2c_write(VIRT_I2C_ADDR, (
void *)&shadow,
sizeof(shadow));
916 switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
963 "Axis Communications AB\n");
967 res = register_chrdev(
GPIO_MAJOR, gpio_name, &gpio_fops);
990 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO