Prefetch inodes in readdir
[pohmelfs.git] / arch / cris / arch-v32 / drivers / mach-a3 / gpio.c
blobc845831e222590c7e6289d4fef846ea21305aadf
1 /*
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,
9 * port to ETRAX FS.
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>
20 #include <linux/fs.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>
33 #include <asm/io.h>
34 #include <asm/system.h>
35 #include <asm/irq.h>
36 #include <mach/pinmux.h>
38 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
39 #include "../i2c.h"
41 #define VIRT_I2C_ADDR 0x40
42 #endif
44 /* The following gio ports on ARTPEC-3 is available:
45 * pa 32 bits
46 * pb 32 bits
47 * pc 16 bits
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 */
55 #define D(x)
57 #if 0
58 static int dp_cnt;
59 #define DP(x) \
60 do { \
61 dp_cnt++; \
62 if (dp_cnt % 1000 == 0) \
63 x; \
64 } while (0)
65 #else
66 #define DP(x)
67 #endif
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,
74 unsigned long arg);
75 #endif
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 */
86 struct gpio_private {
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;
92 unsigned char pad1;
93 /* These fields are generic */
94 unsigned long highalarm, lowalarm;
95 wait_queue_head_t alarm_wq;
96 int minor;
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,
102 unsigned long arg);
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;
125 #endif
127 static unsigned long *data_out[NUM_PORTS] = {
128 GIO_REG_WR_ADDR(rw_pa_dout),
129 GIO_REG_WR_ADDR(rw_pb_dout),
130 &led_dummy,
131 GIO_REG_WR_ADDR(rw_pc_dout),
132 &port_d_dummy,
133 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
134 &port_e_dummy,
135 &virtual_dummy,
136 #endif
139 static unsigned long *data_in[NUM_PORTS] = {
140 GIO_REG_RD_ADDR(r_pa_din),
141 GIO_REG_RD_ADDR(r_pb_din),
142 &led_dummy,
143 GIO_REG_RD_ADDR(r_pc_din),
144 GIO_REG_RD_ADDR(r_pd_din),
145 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
146 &port_e_dummy,
147 &virtual_dummy,
148 #endif
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,
160 #endif
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,
172 #endif
175 static unsigned long *dir_oe[NUM_PORTS] = {
176 GIO_REG_WR_ADDR(rw_pa_oe),
177 GIO_REG_WR_ADDR(rw_pb_oe),
178 &led_dummy,
179 GIO_REG_WR_ADDR(rw_pc_oe),
180 &port_d_dummy,
181 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
182 &port_e_dummy,
183 &virtual_rw_pv_oe,
184 #endif
187 static void gpio_set_alarm(struct gpio_private *priv)
189 int bit;
190 int intr_cfg;
191 int mask;
192 int pins;
193 unsigned long flags;
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++) {
201 int intr = bit % 8;
202 int pin = bit / 8;
203 if (priv->minor < GPIO_MINOR_LEDS)
204 pin += priv->minor * 4;
205 else
206 pin += (priv->minor - 1) * 4;
208 if (priv->highalarm & (1<<bit)) {
209 intr_cfg |= (regk_gio_hi << (intr * 3));
210 mask |= 1 << intr;
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));
215 mask |= 1 << intr;
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;
232 unsigned long data;
233 unsigned long tmp;
235 if (priv->minor >= GPIO_MINOR_PWM0 &&
236 priv->minor <= GPIO_MINOR_LAST_PWM)
237 return 0;
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);
247 } else
248 return 0;
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));
254 return 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;
262 unsigned long flags;
263 unsigned long tmp;
264 unsigned long tmp2;
265 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
266 unsigned char enable_gpiov_ack = 0;
267 #endif
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;
287 #endif
289 /* Ack them */
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);
296 tmp2 &= ~tmp;
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);
303 #endif
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);
315 if (data & 1 << bit)
316 shadow |= data_mask;
317 else
318 shadow &= ~data_mask;
319 writel(shadow, port);
320 /* For FPGA: min 5.0ns (DCC) before CCLK high */
321 shadow |= clk_mask;
322 writel(shadow, port);
325 static void gpio_write_byte(struct gpio_private *priv, unsigned long *port,
326 unsigned char data)
328 int i;
330 if (priv->write_msb)
331 for (i = 7; i >= 0; i--)
332 gpio_write_bit(port, data, i, priv->clk_mask,
333 priv->data_mask);
334 else
335 for (i = 0; i <= 7; i++)
336 gpio_write_bit(port, data, i, priv->clk_mask,
337 priv->data_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;
345 unsigned long flags;
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)
351 return -EFAULT;
352 #endif
353 if (priv->minor == GPIO_MINOR_LEDS)
354 return -EFAULT;
356 if (priv->minor >= GPIO_MINOR_PWM0 &&
357 priv->minor <= GPIO_MINOR_LAST_PWM)
358 return -EFAULT;
360 if (!access_ok(VERIFY_READ, buf, count))
361 return -EFAULT;
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)
366 return -EPERM;
368 D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
369 "msb: %i\n",
370 count, priv->data_mask, priv->clk_mask, priv->write_msb));
372 spin_lock_irqsave(&gpio_lock, flags);
374 while (count--)
375 gpio_write_byte(priv, data_out[priv->minor], *buf++);
377 spin_unlock_irqrestore(&gpio_lock, flags);
378 return retval;
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))
388 return -EINVAL;
390 priv = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
392 if (!priv)
393 return -ENOMEM;
395 mutex_lock(&gpio_mutex);
396 memset(priv, 0, sizeof(*priv));
398 priv->minor = p;
399 filp->private_data = priv;
401 /* initialize the io/alarm struct, not for PWM ports though */
402 if (p <= GPIO_MINOR_LAST) {
404 priv->clk_mask = 0;
405 priv->data_mask = 0;
406 priv->highalarm = 0;
407 priv->lowalarm = 0;
409 init_waitqueue_head(&priv->alarm_wq);
411 /* link it into our alarmlist */
412 spin_lock_irq(&gpio_lock);
413 priv->next = alarmlist;
414 alarmlist = priv;
415 spin_unlock_irq(&gpio_lock);
418 mutex_unlock(&gpio_mutex);
419 return 0;
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);
435 p = alarmlist;
437 if (p == todel)
438 alarmlist = todel->next;
439 else {
440 while (p->next != todel)
441 p = p->next;
442 p->next = todel->next;
445 /* Check if there are still any alarms set */
446 p = alarmlist;
447 a_high = 0;
448 a_low = 0;
449 while (p) {
450 if (p->minor == GPIO_MINOR_A) {
451 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
452 p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
453 #endif
454 a_high |= p->highalarm;
455 a_low |= p->lowalarm;
458 p = p->next;
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);
466 #endif
468 spin_unlock_irq(&gpio_lock);
470 kfree(todel);
472 return 0;
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
484 unsigned long flags;
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 */
500 #endif
501 else
502 dir_shadow ^= 0xFFFFFFFF; /* PA, PB and PD 32 bits */
504 return dir_shadow;
506 } /* setget_input */
508 static inline unsigned long setget_output(struct gpio_private *priv,
509 unsigned long arg)
511 unsigned long flags;
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);
521 return dir_shadow;
522 } /* setget_output */
524 static long gpio_ioctl_unlocked(struct file *file,
525 unsigned int cmd, unsigned long arg)
527 unsigned long flags;
528 unsigned long val;
529 unsigned long shadow;
530 struct gpio_private *priv = file->private_data;
532 if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
533 return -ENOTTY;
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);
540 #endif
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 */
551 /* Read the port. */
552 return readl(data_in[priv->minor]);
553 case IO_SETBITS:
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);
560 break;
561 case IO_CLRBITS:
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);
568 break;
569 case IO_HIGHALARM:
570 /* Set alarm when bits with 1 in arg go high. */
571 priv->highalarm |= arg;
572 gpio_set_alarm(priv);
573 break;
574 case IO_LOWALARM:
575 /* Set alarm when bits with 1 in arg go low. */
576 priv->lowalarm |= arg;
577 gpio_set_alarm(priv);
578 break;
579 case IO_CLRALARM:
580 /* Clear alarm for bits with 1 in arg. */
581 priv->highalarm &= ~arg;
582 priv->lowalarm &= ~arg;
583 gpio_set_alarm(priv);
584 break;
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:
603 int res = -EPERM;
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;
622 res = 0;
624 spin_unlock_irqrestore(&gpio_lock, flags);
626 return res;
628 case IO_READ_INBITS:
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)))
632 return -EFAULT;
633 return 0;
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)))
638 return -EFAULT;
639 break;
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)))
645 return -EFAULT;
646 val = setget_input(priv, val);
647 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
648 return -EFAULT;
649 break;
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)))
655 return -EFAULT;
656 val = setget_output(priv, val);
657 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
658 return -EFAULT;
659 break;
660 default:
661 return -EINVAL;
662 } /* switch */
664 return 0;
667 static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
669 long ret;
671 mutex_lock(&gpio_mutex);
672 ret = gpio_ioctl_unlocked(file, cmd, arg);
673 mutex_unlock(&gpio_mutex);
675 return ret;
678 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
679 static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
680 unsigned long arg)
682 unsigned long flags;
683 unsigned short val;
684 unsigned short shadow;
685 struct gpio_private *priv = file->private_data;
687 switch (_IOC_NR(cmd)) {
688 case IO_SETBITS:
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);
696 break;
697 case IO_CLRBITS:
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);
705 break;
706 case IO_HIGHALARM:
707 /* Set alarm when bits with 1 in arg go high. */
708 priv->highalarm |= arg;
709 break;
710 case IO_LOWALARM:
711 /* Set alarm when bits with 1 in arg go low. */
712 priv->lowalarm |= arg;
713 break;
714 case IO_CLRALARM:
715 /* Clear alarm for bits with 1 in arg. */
716 priv->highalarm &= ~arg;
717 priv->lowalarm &= ~arg;
718 break;
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))) {
734 priv->clk_mask = 0;
735 priv->data_mask = 0;
736 return -EPERM;
738 break;
740 case IO_READ_INBITS:
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)))
744 return -EFAULT;
745 return 0;
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)))
752 return -EFAULT;
753 break;
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)))
761 return -EFAULT;
762 val = setget_input(priv, val);
763 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
764 return -EFAULT;
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,
771 sizeof(shadow));
772 shadow &= ~change;
773 shadow |= val;
774 i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
775 sizeof(shadow));
777 break;
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)))
784 return -EFAULT;
785 val = setget_output(priv, val);
786 if (copy_to_user((void __user *)arg, &val, sizeof(val)))
787 return -EFAULT;
788 break;
789 default:
790 return -EINVAL;
791 } /* switch */
792 return 0;
794 #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
796 static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
798 unsigned char green;
799 unsigned char red;
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);
807 break;
809 default:
810 return -EINVAL;
811 } /* switch */
813 return 0;
816 static int gpio_pwm_set_mode(unsigned long arg, int pwm_port)
818 int pinmux_pwm = pinmux_pwm0 + pwm_port;
819 int mode;
820 reg_gio_rw_pwm0_ctrl rw_pwm_ctrl = {
821 .ccd_val = 0,
822 .ccd_override = regk_gio_no,
823 .mode = regk_gio_no
825 int allocstatus;
827 if (get_user(mode, &((struct io_pwm_set_mode *) arg)->mode))
828 return -EFAULT;
829 rw_pwm_ctrl.mode = mode;
830 if (mode != PWM_OFF)
831 allocstatus = crisv32_pinmux_alloc_fixed(pinmux_pwm);
832 else
833 allocstatus = crisv32_pinmux_dealloc_fixed(pinmux_pwm);
834 if (allocstatus)
835 return allocstatus;
836 REG_WRITE(reg_gio_rw_pwm0_ctrl, REG_ADDR(gio, regi_gio, rw_pwm0_ctrl) +
837 12 * pwm_port, rw_pwm_ctrl);
838 return 0;
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)))
847 return -EFAULT;
848 if (periods.lo > 8191 || periods.hi > 8191)
849 return -EINVAL;
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);
854 return 0;
857 static int gpio_pwm_set_duty(unsigned long arg, int pwm_port)
859 unsigned int duty;
860 reg_gio_rw_pwm0_data rw_pwm_duty;
862 if (get_user(duty, &((struct io_pwm_set_duty *) arg)->duty))
863 return -EFAULT;
864 if (duty > 255)
865 return -EINVAL;
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);
869 return 0;
872 static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
873 unsigned long arg)
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);
884 default:
885 return -EINVAL;
887 return 0;
890 static const struct file_operations gpio_fops = {
891 .owner = THIS_MODULE,
892 .poll = gpio_poll,
893 .unlocked_ioctl = gpio_ioctl,
894 .write = gpio_write,
895 .open = gpio_open,
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) {
918 case 0:
919 intr_cfg.pa0 = regk_gio_lo;
920 intr_mask.pa0 = regk_gio_yes;
921 break;
922 case 1:
923 intr_cfg.pa1 = regk_gio_lo;
924 intr_mask.pa1 = regk_gio_yes;
925 break;
926 case 2:
927 intr_cfg.pa2 = regk_gio_lo;
928 intr_mask.pa2 = regk_gio_yes;
929 break;
930 case 3:
931 intr_cfg.pa3 = regk_gio_lo;
932 intr_mask.pa3 = regk_gio_yes;
933 break;
934 case 4:
935 intr_cfg.pa4 = regk_gio_lo;
936 intr_mask.pa4 = regk_gio_yes;
937 break;
938 case 5:
939 intr_cfg.pa5 = regk_gio_lo;
940 intr_mask.pa5 = regk_gio_yes;
941 break;
942 case 6:
943 intr_cfg.pa6 = regk_gio_lo;
944 intr_mask.pa6 = regk_gio_yes;
945 break;
946 case 7:
947 intr_cfg.pa7 = regk_gio_lo;
948 intr_mask.pa7 = regk_gio_yes;
949 break;
952 REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
953 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
955 #endif
957 /* main driver initialization routine, called from mem.c */
959 static int __init gpio_init(void)
961 int res;
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);
969 if (res < 0) {
970 printk(KERN_ERR "gpio: couldn't get a major number.\n");
971 return res;
974 /* Clear all leds */
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);
983 if (res2) {
984 printk(KERN_ERR "err: irq for gpio\n");
985 return res2;
988 /* No IRQs by default. */
989 REG_WR_INT(gio, regi_gio, rw_intr_pins, 0);
991 #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
992 virtual_gpio_init();
993 #endif
995 return res;
998 /* this makes sure that gpio_init is called during kernel boot */
1000 module_init(gpio_init);