2008-01-25 14:42:41 +00:00
|
|
|
/*
|
2005-04-16 22:20:36 +00:00
|
|
|
* Etrax general port I/O device
|
|
|
|
*
|
2008-01-25 14:42:41 +00:00
|
|
|
* Copyright (c) 1999-2007 Axis Communications AB
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Authors: Bjorn Wesen (initial version)
|
|
|
|
* Ola Knutsson (LED handling)
|
|
|
|
* Johan Adolfsson (read/set directions, write, port G)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
|
|
|
|
#include <asm/etraxgpio.h>
|
2008-10-21 15:45:58 +00:00
|
|
|
#include <arch/svinto.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/system.h>
|
|
|
|
#include <asm/irq.h>
|
2008-10-21 15:45:58 +00:00
|
|
|
#include <arch/io_interface_mux.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#define GPIO_MAJOR 120 /* experimental MAJOR number */
|
|
|
|
|
|
|
|
#define D(x)
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int dp_cnt;
|
|
|
|
#define DP(x) do { dp_cnt++; if (dp_cnt % 1000 == 0) x; }while(0)
|
|
|
|
#else
|
|
|
|
#define DP(x)
|
|
|
|
#endif
|
2008-01-25 14:42:41 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static char gpio_name[] = "etrax gpio";
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static wait_queue_head_t *gpio_wq;
|
|
|
|
#endif
|
|
|
|
|
2010-08-02 11:17:05 +00:00
|
|
|
static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
|
2008-02-06 13:52:40 +00:00
|
|
|
static ssize_t gpio_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *off);
|
2005-04-16 22:20:36 +00:00
|
|
|
static int gpio_open(struct inode *inode, struct file *filp);
|
|
|
|
static int gpio_release(struct inode *inode, struct file *filp);
|
|
|
|
static unsigned int gpio_poll(struct file *filp, struct poll_table_struct *wait);
|
|
|
|
|
|
|
|
/* private data per open() of this driver */
|
|
|
|
|
|
|
|
struct gpio_private {
|
|
|
|
struct gpio_private *next;
|
|
|
|
/* These fields are for PA and PB only */
|
|
|
|
volatile unsigned char *port, *shadow;
|
|
|
|
volatile unsigned char *dir, *dir_shadow;
|
|
|
|
unsigned char changeable_dir;
|
|
|
|
unsigned char changeable_bits;
|
|
|
|
unsigned char clk_mask;
|
|
|
|
unsigned char data_mask;
|
|
|
|
unsigned char write_msb;
|
|
|
|
unsigned char pad1, pad2, pad3;
|
|
|
|
/* These fields are generic */
|
|
|
|
unsigned long highalarm, lowalarm;
|
|
|
|
wait_queue_head_t alarm_wq;
|
|
|
|
int minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* linked list of alarms to check for */
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
static struct gpio_private *alarmlist;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
static int gpio_some_alarms; /* Set if someone uses alarm */
|
|
|
|
static unsigned long gpio_pa_irq_enabled_mask;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-07-27 18:44:34 +00:00
|
|
|
static DEFINE_SPINLOCK(gpio_lock); /* Protect directions etc */
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Port A and B use 8 bit access, but Port G is 32 bit */
|
|
|
|
#define NUM_PORTS (GPIO_MINOR_B+1)
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
static volatile unsigned char *ports[NUM_PORTS] = {
|
|
|
|
R_PORT_PA_DATA,
|
2005-04-16 22:20:36 +00:00
|
|
|
R_PORT_PB_DATA,
|
|
|
|
};
|
|
|
|
static volatile unsigned char *shads[NUM_PORTS] = {
|
2008-01-25 14:42:41 +00:00
|
|
|
&port_pa_data_shadow,
|
2005-04-16 22:20:36 +00:00
|
|
|
&port_pb_data_shadow
|
|
|
|
};
|
|
|
|
|
|
|
|
/* What direction bits that are user changeable 1=changeable*/
|
|
|
|
#ifndef CONFIG_ETRAX_PA_CHANGEABLE_DIR
|
|
|
|
#define CONFIG_ETRAX_PA_CHANGEABLE_DIR 0x00
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ETRAX_PB_CHANGEABLE_DIR
|
|
|
|
#define CONFIG_ETRAX_PB_CHANGEABLE_DIR 0x00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_ETRAX_PA_CHANGEABLE_BITS
|
|
|
|
#define CONFIG_ETRAX_PA_CHANGEABLE_BITS 0xFF
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_ETRAX_PB_CHANGEABLE_BITS
|
|
|
|
#define CONFIG_ETRAX_PB_CHANGEABLE_BITS 0xFF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
static unsigned char changeable_dir[NUM_PORTS] = {
|
2005-04-16 22:20:36 +00:00
|
|
|
CONFIG_ETRAX_PA_CHANGEABLE_DIR,
|
2008-01-25 14:42:41 +00:00
|
|
|
CONFIG_ETRAX_PB_CHANGEABLE_DIR
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
2008-01-25 14:42:41 +00:00
|
|
|
static unsigned char changeable_bits[NUM_PORTS] = {
|
2005-04-16 22:20:36 +00:00
|
|
|
CONFIG_ETRAX_PA_CHANGEABLE_BITS,
|
2008-01-25 14:42:41 +00:00
|
|
|
CONFIG_ETRAX_PB_CHANGEABLE_BITS
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
static volatile unsigned char *dir[NUM_PORTS] = {
|
|
|
|
R_PORT_PA_DIR,
|
|
|
|
R_PORT_PB_DIR
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static volatile unsigned char *dir_shadow[NUM_PORTS] = {
|
2008-01-25 14:42:41 +00:00
|
|
|
&port_pa_dir_shadow,
|
|
|
|
&port_pb_dir_shadow
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2005-07-27 18:44:34 +00:00
|
|
|
/* All bits in port g that can change dir. */
|
|
|
|
static const unsigned long int changeable_dir_g_mask = 0x01FFFF01;
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
/* Port G is 32 bit, handle it special, some bits are both inputs
|
2005-04-16 22:20:36 +00:00
|
|
|
and outputs at the same time, only some of the bits can change direction
|
|
|
|
and some of them in groups of 8 bit. */
|
|
|
|
static unsigned long changeable_dir_g;
|
|
|
|
static unsigned long dir_g_in_bits;
|
|
|
|
static unsigned long dir_g_out_bits;
|
|
|
|
static unsigned long dir_g_shadow; /* 1=output */
|
|
|
|
|
|
|
|
#define USE_PORTS(priv) ((priv)->minor <= GPIO_MINOR_B)
|
|
|
|
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
static unsigned int gpio_poll(struct file *file, poll_table *wait)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
unsigned int mask = 0;
|
2008-02-06 13:52:40 +00:00
|
|
|
struct gpio_private *priv = file->private_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long data;
|
2008-01-25 14:42:41 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
poll_wait(file, &priv->alarm_wq, wait);
|
|
|
|
if (priv->minor == GPIO_MINOR_A) {
|
|
|
|
unsigned long tmp;
|
|
|
|
data = *R_PORT_PA_DATA;
|
|
|
|
/* PA has support for high level interrupt -
|
|
|
|
* lets activate for those low and with highalarm set
|
|
|
|
*/
|
|
|
|
tmp = ~data & priv->highalarm & 0xFF;
|
|
|
|
tmp = (tmp << R_IRQ_MASK1_SET__pa0__BITNR);
|
2008-01-25 14:42:41 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
gpio_pa_irq_enabled_mask |= tmp;
|
|
|
|
*R_IRQ_MASK1_SET = tmp;
|
|
|
|
} else if (priv->minor == GPIO_MINOR_B)
|
|
|
|
data = *R_PORT_PB_DATA;
|
|
|
|
else if (priv->minor == GPIO_MINOR_G)
|
|
|
|
data = *R_PORT_G_DATA;
|
2007-11-15 01:00:06 +00:00
|
|
|
else {
|
2008-01-25 14:42:41 +00:00
|
|
|
mask = 0;
|
|
|
|
goto out;
|
2007-11-15 01:00:06 +00:00
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((data & priv->highalarm) ||
|
|
|
|
(~data & priv->lowalarm)) {
|
|
|
|
mask = POLLIN|POLLRDNORM;
|
|
|
|
}
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
DP(printk("gpio_poll ready: mask 0x%08X\n", mask));
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
int etrax_gpio_wake_up_check(void)
|
|
|
|
{
|
2008-01-25 14:42:41 +00:00
|
|
|
struct gpio_private *priv;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long data = 0;
|
|
|
|
int ret = 0;
|
2008-01-25 14:42:41 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
|
|
|
priv = alarmlist;
|
2005-04-16 22:20:36 +00:00
|
|
|
while (priv) {
|
2008-01-25 14:42:41 +00:00
|
|
|
if (USE_PORTS(priv))
|
2005-04-16 22:20:36 +00:00
|
|
|
data = *priv->port;
|
2008-01-25 14:42:41 +00:00
|
|
|
else if (priv->minor == GPIO_MINOR_G)
|
2005-04-16 22:20:36 +00:00
|
|
|
data = *R_PORT_G_DATA;
|
2008-01-25 14:42:41 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if ((data & priv->highalarm) ||
|
|
|
|
(~data & priv->lowalarm)) {
|
|
|
|
DP(printk("etrax_gpio_wake_up_check %i\n",priv->minor));
|
|
|
|
wake_up_interruptible(&priv->alarm_wq);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
priv = priv->next;
|
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t
|
2008-01-25 14:42:41 +00:00
|
|
|
gpio_poll_timer_interrupt(int irq, void *dev_id)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
if (gpio_some_alarms) {
|
|
|
|
etrax_gpio_wake_up_check();
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
return IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t
|
2008-02-06 13:52:40 +00:00
|
|
|
gpio_interrupt(int irq, void *dev_id)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
unsigned long tmp;
|
2008-01-25 14:42:41 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Find what PA interrupts are active */
|
|
|
|
tmp = (*R_IRQ_READ1);
|
|
|
|
|
|
|
|
/* Find those that we have enabled */
|
|
|
|
tmp &= gpio_pa_irq_enabled_mask;
|
|
|
|
|
|
|
|
/* Clear them.. */
|
|
|
|
*R_IRQ_MASK1_CLR = tmp;
|
|
|
|
gpio_pa_irq_enabled_mask &= ~tmp;
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
if (gpio_some_alarms)
|
2005-04-16 22:20:36 +00:00
|
|
|
return IRQ_RETVAL(etrax_gpio_wake_up_check());
|
2008-01-25 14:42:41 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
static void gpio_write_bit(struct gpio_private *priv,
|
|
|
|
unsigned char data, int bit)
|
|
|
|
{
|
|
|
|
*priv->port = *priv->shadow &= ~(priv->clk_mask);
|
|
|
|
if (data & 1 << bit)
|
|
|
|
*priv->port = *priv->shadow |= priv->data_mask;
|
|
|
|
else
|
|
|
|
*priv->port = *priv->shadow &= ~(priv->data_mask);
|
|
|
|
|
|
|
|
/* For FPGA: min 5.0ns (DCC) before CCLK high */
|
|
|
|
*priv->port = *priv->shadow |= priv->clk_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gpio_write_byte(struct gpio_private *priv, unsigned char data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (priv->write_msb)
|
|
|
|
for (i = 7; i >= 0; i--)
|
|
|
|
gpio_write_bit(priv, data, i);
|
|
|
|
else
|
|
|
|
for (i = 0; i <= 7; i++)
|
|
|
|
gpio_write_bit(priv, data, i);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
static ssize_t gpio_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *off)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-02-06 13:52:40 +00:00
|
|
|
struct gpio_private *priv = file->private_data;
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned long flags;
|
2008-01-25 14:42:41 +00:00
|
|
|
ssize_t retval = count;
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
if (priv->minor != GPIO_MINOR_A && priv->minor != GPIO_MINOR_B)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (!access_ok(VERIFY_READ, buf, count))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* It must have been configured using the IO_CFG_WRITE_MODE */
|
|
|
|
/* Perhaps a better error code? */
|
2008-01-25 14:42:41 +00:00
|
|
|
if (priv->clk_mask == 0 || priv->data_mask == 0) {
|
2007-11-15 01:00:06 +00:00
|
|
|
retval = -EPERM;
|
|
|
|
goto out;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
|
|
|
|
D(printk(KERN_DEBUG "gpio_write: %02X to data 0x%02X "
|
|
|
|
"clk 0x%02X msb: %i\n",
|
|
|
|
count, priv->data_mask, priv->clk_mask, priv->write_msb));
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
gpio_write_byte(priv, *buf++);
|
|
|
|
|
2007-11-15 01:00:06 +00:00
|
|
|
out:
|
2008-01-25 14:42:41 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpio_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct gpio_private *priv;
|
2006-07-10 11:45:02 +00:00
|
|
|
int p = iminor(inode);
|
2008-01-25 14:42:41 +00:00
|
|
|
unsigned long flags;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (p > GPIO_MINOR_LAST)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
priv = kzalloc(sizeof(struct gpio_private), GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
priv->minor = p;
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
/* initialize the io/alarm struct */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (USE_PORTS(priv)) { /* A and B */
|
|
|
|
priv->port = ports[p];
|
|
|
|
priv->shadow = shads[p];
|
|
|
|
priv->dir = dir[p];
|
|
|
|
priv->dir_shadow = dir_shadow[p];
|
|
|
|
priv->changeable_dir = changeable_dir[p];
|
|
|
|
priv->changeable_bits = changeable_bits[p];
|
|
|
|
} else {
|
|
|
|
priv->port = NULL;
|
|
|
|
priv->shadow = NULL;
|
|
|
|
priv->dir = NULL;
|
|
|
|
priv->dir_shadow = NULL;
|
|
|
|
priv->changeable_dir = 0;
|
|
|
|
priv->changeable_bits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->highalarm = 0;
|
|
|
|
priv->lowalarm = 0;
|
|
|
|
priv->clk_mask = 0;
|
|
|
|
priv->data_mask = 0;
|
|
|
|
init_waitqueue_head(&priv->alarm_wq);
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
filp->private_data = priv;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
/* link it into our alarmlist */
|
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
|
|
|
priv->next = alarmlist;
|
|
|
|
alarmlist = priv;
|
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpio_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2005-07-27 18:44:34 +00:00
|
|
|
struct gpio_private *p;
|
|
|
|
struct gpio_private *todel;
|
2008-01-25 14:42:41 +00:00
|
|
|
unsigned long flags;
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
p = alarmlist;
|
2008-02-06 13:52:40 +00:00
|
|
|
todel = filp->private_data;
|
2005-07-27 18:44:34 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* unlink from alarmlist and free the private structure */
|
|
|
|
|
|
|
|
if (p == todel) {
|
|
|
|
alarmlist = todel->next;
|
|
|
|
} else {
|
|
|
|
while (p->next != todel)
|
|
|
|
p = p->next;
|
|
|
|
p->next = todel->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(todel);
|
|
|
|
/* Check if there are still any alarms set */
|
|
|
|
p = alarmlist;
|
|
|
|
while (p) {
|
|
|
|
if (p->highalarm | p->lowalarm) {
|
|
|
|
gpio_some_alarms = 1;
|
2008-01-25 14:42:41 +00:00
|
|
|
goto out;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
gpio_some_alarms = 0;
|
2008-01-25 14:42:41 +00:00
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
/* Main device API. ioctl's to read/set/clear bits, as well as to
|
2005-04-16 22:20:36 +00:00
|
|
|
* set alarms to wait for using a subsequent select().
|
|
|
|
*/
|
|
|
|
unsigned long inline setget_input(struct gpio_private *priv, unsigned long arg)
|
|
|
|
{
|
2008-01-25 14:42:41 +00:00
|
|
|
/* Set direction 0=unchanged 1=input,
|
|
|
|
* return mask with 1=input */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
2008-01-25 14:42:41 +00:00
|
|
|
*priv->dir = *priv->dir_shadow &=
|
2005-04-16 22:20:36 +00:00
|
|
|
~((unsigned char)arg & priv->changeable_dir);
|
|
|
|
return ~(*priv->dir_shadow) & 0xFF; /* Only 8 bits */
|
2008-01-25 14:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->minor != GPIO_MINOR_G)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* We must fiddle with R_GEN_CONFIG to change dir */
|
|
|
|
if (((arg & dir_g_in_bits) != arg) &&
|
|
|
|
(arg & changeable_dir_g)) {
|
|
|
|
arg &= changeable_dir_g;
|
|
|
|
/* Clear bits in genconfig to set to input */
|
|
|
|
if (arg & (1<<0)) {
|
|
|
|
genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, g0dir);
|
|
|
|
dir_g_in_bits |= (1<<0);
|
|
|
|
dir_g_out_bits &= ~(1<<0);
|
|
|
|
}
|
|
|
|
if ((arg & 0x0000FF00) == 0x0000FF00) {
|
|
|
|
genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, g8_15dir);
|
|
|
|
dir_g_in_bits |= 0x0000FF00;
|
|
|
|
dir_g_out_bits &= ~0x0000FF00;
|
|
|
|
}
|
|
|
|
if ((arg & 0x00FF0000) == 0x00FF0000) {
|
|
|
|
genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, g16_23dir);
|
|
|
|
dir_g_in_bits |= 0x00FF0000;
|
|
|
|
dir_g_out_bits &= ~0x00FF0000;
|
|
|
|
}
|
|
|
|
if (arg & (1<<24)) {
|
|
|
|
genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG, g24dir);
|
|
|
|
dir_g_in_bits |= (1<<24);
|
|
|
|
dir_g_out_bits &= ~(1<<24);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
D(printk(KERN_DEBUG "gpio: SETINPUT on port G set "
|
|
|
|
"genconfig to 0x%08lX "
|
|
|
|
"in_bits: 0x%08lX "
|
|
|
|
"out_bits: 0x%08lX\n",
|
|
|
|
(unsigned long)genconfig_shadow,
|
|
|
|
dir_g_in_bits, dir_g_out_bits));
|
|
|
|
*R_GEN_CONFIG = genconfig_shadow;
|
|
|
|
/* Must be a >120 ns delay before writing this again */
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
return dir_g_in_bits;
|
2005-04-16 22:20:36 +00:00
|
|
|
} /* setget_input */
|
|
|
|
|
|
|
|
unsigned long inline setget_output(struct gpio_private *priv, unsigned long arg)
|
|
|
|
{
|
|
|
|
if (USE_PORTS(priv)) {
|
2008-01-25 14:42:41 +00:00
|
|
|
*priv->dir = *priv->dir_shadow |=
|
|
|
|
((unsigned char)arg & priv->changeable_dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
return *priv->dir_shadow;
|
2008-01-25 14:42:41 +00:00
|
|
|
}
|
|
|
|
if (priv->minor != GPIO_MINOR_G)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* We must fiddle with R_GEN_CONFIG to change dir */
|
|
|
|
if (((arg & dir_g_out_bits) != arg) &&
|
|
|
|
(arg & changeable_dir_g)) {
|
|
|
|
/* Set bits in genconfig to set to output */
|
|
|
|
if (arg & (1<<0)) {
|
|
|
|
genconfig_shadow |= IO_MASK(R_GEN_CONFIG, g0dir);
|
|
|
|
dir_g_out_bits |= (1<<0);
|
|
|
|
dir_g_in_bits &= ~(1<<0);
|
|
|
|
}
|
|
|
|
if ((arg & 0x0000FF00) == 0x0000FF00) {
|
|
|
|
genconfig_shadow |= IO_MASK(R_GEN_CONFIG, g8_15dir);
|
|
|
|
dir_g_out_bits |= 0x0000FF00;
|
|
|
|
dir_g_in_bits &= ~0x0000FF00;
|
|
|
|
}
|
|
|
|
if ((arg & 0x00FF0000) == 0x00FF0000) {
|
|
|
|
genconfig_shadow |= IO_MASK(R_GEN_CONFIG, g16_23dir);
|
|
|
|
dir_g_out_bits |= 0x00FF0000;
|
|
|
|
dir_g_in_bits &= ~0x00FF0000;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
if (arg & (1<<24)) {
|
|
|
|
genconfig_shadow |= IO_MASK(R_GEN_CONFIG, g24dir);
|
|
|
|
dir_g_out_bits |= (1<<24);
|
|
|
|
dir_g_in_bits &= ~(1<<24);
|
|
|
|
}
|
|
|
|
D(printk(KERN_INFO "gpio: SETOUTPUT on port G set "
|
|
|
|
"genconfig to 0x%08lX "
|
|
|
|
"in_bits: 0x%08lX "
|
|
|
|
"out_bits: 0x%08lX\n",
|
|
|
|
(unsigned long)genconfig_shadow,
|
|
|
|
dir_g_in_bits, dir_g_out_bits));
|
|
|
|
*R_GEN_CONFIG = genconfig_shadow;
|
|
|
|
/* Must be a >120 ns delay before writing this again */
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 14:42:41 +00:00
|
|
|
return dir_g_out_bits & 0x7FFFFFFF;
|
2005-04-16 22:20:36 +00:00
|
|
|
} /* setget_output */
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
|
|
|
|
|
2010-08-02 11:17:05 +00:00
|
|
|
static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long val;
|
2005-07-27 18:44:34 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
struct gpio_private *priv = file->private_data;
|
2008-01-25 14:42:41 +00:00
|
|
|
if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (_IOC_NR(cmd)) {
|
|
|
|
case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
|
|
|
|
// read the port
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = *priv->port;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else if (priv->minor == GPIO_MINOR_G) {
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = (*R_PORT_G_DATA) & 0x7FFFFFFF;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_SETBITS:
|
|
|
|
// set changeable bits with a 1 in arg
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
2010-08-02 11:17:05 +00:00
|
|
|
*priv->port = *priv->shadow |=
|
2005-04-16 22:20:36 +00:00
|
|
|
((unsigned char)arg & priv->changeable_bits);
|
|
|
|
} else if (priv->minor == GPIO_MINOR_G) {
|
|
|
|
*R_PORT_G_DATA = port_g_data_shadow |= (arg & dir_g_out_bits);
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_CLRBITS:
|
|
|
|
// clear changeable bits with a 1 in arg
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
2010-08-02 11:17:05 +00:00
|
|
|
*priv->port = *priv->shadow &=
|
2005-04-16 22:20:36 +00:00
|
|
|
~((unsigned char)arg & priv->changeable_bits);
|
|
|
|
} else if (priv->minor == GPIO_MINOR_G) {
|
|
|
|
*R_PORT_G_DATA = port_g_data_shadow &= ~((unsigned long)arg & dir_g_out_bits);
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_HIGHALARM:
|
|
|
|
// set alarm when bits with 1 in arg go high
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
priv->highalarm |= arg;
|
|
|
|
gpio_some_alarms = 1;
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_LOWALARM:
|
|
|
|
// set alarm when bits with 1 in arg go low
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
priv->lowalarm |= arg;
|
|
|
|
gpio_some_alarms = 1;
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_CLRALARM:
|
2010-08-02 11:17:05 +00:00
|
|
|
/* clear alarm for bits with 1 in arg */
|
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
priv->highalarm &= ~arg;
|
|
|
|
priv->lowalarm &= ~arg;
|
|
|
|
{
|
|
|
|
/* Must update gpio_some_alarms */
|
|
|
|
struct gpio_private *p = alarmlist;
|
|
|
|
int some_alarms;
|
2008-01-25 14:42:41 +00:00
|
|
|
p = alarmlist;
|
2005-04-16 22:20:36 +00:00
|
|
|
some_alarms = 0;
|
|
|
|
while (p) {
|
|
|
|
if (p->highalarm | p->lowalarm) {
|
|
|
|
some_alarms = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
gpio_some_alarms = some_alarms;
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
|
|
|
|
/* Read direction 0=input 1=output */
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = *priv->dir_shadow;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else if (priv->minor == GPIO_MINOR_G) {
|
|
|
|
/* Note: Some bits are both in and out,
|
|
|
|
* Those that are dual is set here as well.
|
|
|
|
*/
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = (dir_g_shadow | dir_g_out_bits) & 0x7FFFFFFF;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
|
2010-08-02 11:17:05 +00:00
|
|
|
/* Set direction 0=unchanged 1=input,
|
|
|
|
* return mask with 1=input
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = setget_input(priv, arg) & 0x7FFFFFFF;
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
|
2010-08-02 11:17:05 +00:00
|
|
|
/* Set direction 0=unchanged 1=output,
|
|
|
|
* return mask with 1=output
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = setget_output(priv, arg) & 0x7FFFFFFF;
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-07-27 18:44:34 +00:00
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
case IO_SHUTDOWN:
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
SOFT_SHUTDOWN();
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_GET_PWR_BT:
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
#if defined (CONFIG_ETRAX_SOFT_SHUTDOWN)
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = (*R_PORT_G_DATA & ( 1 << CONFIG_ETRAX_POWERBUTTON_BIT));
|
2005-04-16 22:20:36 +00:00
|
|
|
#else
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_CFG_WRITE_MODE:
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
priv->clk_mask = arg & 0xFF;
|
|
|
|
priv->data_mask = (arg >> 8) & 0xFF;
|
|
|
|
priv->write_msb = (arg >> 16) & 0x01;
|
|
|
|
/* Check if we're allowed to change the bits and
|
|
|
|
* the direction is correct
|
|
|
|
*/
|
|
|
|
if (!((priv->clk_mask & priv->changeable_bits) &&
|
|
|
|
(priv->data_mask & priv->changeable_bits) &&
|
|
|
|
(priv->clk_mask & *priv->dir_shadow) &&
|
|
|
|
(priv->data_mask & *priv->dir_shadow)))
|
|
|
|
{
|
|
|
|
priv->clk_mask = 0;
|
|
|
|
priv->data_mask = 0;
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EPERM;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
2010-08-02 11:17:05 +00:00
|
|
|
case IO_READ_INBITS:
|
2005-04-16 22:20:36 +00:00
|
|
|
/* *arg is result of reading the input pins */
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
|
|
|
val = *priv->port;
|
|
|
|
} else if (priv->minor == GPIO_MINOR_G) {
|
|
|
|
val = *R_PORT_G_DATA;
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2008-02-06 13:52:40 +00:00
|
|
|
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EFAULT;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_READ_OUTBITS:
|
|
|
|
/* *arg is result of reading the output shadow */
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (USE_PORTS(priv)) {
|
|
|
|
val = *priv->shadow;
|
|
|
|
} else if (priv->minor == GPIO_MINOR_G) {
|
|
|
|
val = port_g_data_shadow;
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2008-02-06 13:52:40 +00:00
|
|
|
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EFAULT;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
2010-08-02 11:17:05 +00:00
|
|
|
case IO_SETGET_INPUT:
|
2005-04-16 22:20:36 +00:00
|
|
|
/* bits set in *arg is set to input,
|
|
|
|
* *arg updated with current input pins.
|
|
|
|
*/
|
2008-02-06 13:52:40 +00:00
|
|
|
if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
|
2005-07-27 18:44:34 +00:00
|
|
|
{
|
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
val = setget_input(priv, val);
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2008-02-06 13:52:40 +00:00
|
|
|
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EFAULT;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
case IO_SETGET_OUTPUT:
|
|
|
|
/* bits set in *arg is set to output,
|
|
|
|
* *arg updated with current output pins.
|
|
|
|
*/
|
2008-02-06 13:52:40 +00:00
|
|
|
if (copy_from_user(&val, (void __user *)arg, sizeof(val))) {
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
val = setget_output(priv, val);
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2008-02-06 13:52:40 +00:00
|
|
|
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EFAULT;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (priv->minor == GPIO_MINOR_LEDS)
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = gpio_leds_ioctl(cmd, arg);
|
2005-04-16 22:20:36 +00:00
|
|
|
else
|
2005-07-27 18:44:34 +00:00
|
|
|
ret = -EINVAL;
|
2010-08-02 11:17:05 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
} /* switch */
|
2005-07-27 18:44:34 +00:00
|
|
|
|
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
unsigned char green;
|
|
|
|
unsigned char red;
|
|
|
|
|
|
|
|
switch (_IOC_NR(cmd)) {
|
|
|
|
case IO_LEDACTIVE_SET:
|
2008-01-25 14:42:41 +00:00
|
|
|
green = ((unsigned char)arg) & 1;
|
|
|
|
red = (((unsigned char)arg) >> 1) & 1;
|
|
|
|
CRIS_LED_ACTIVE_SET_G(green);
|
|
|
|
CRIS_LED_ACTIVE_SET_R(red);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_LED_SETBIT:
|
2008-01-25 14:42:41 +00:00
|
|
|
CRIS_LED_BIT_SET(arg);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_LED_CLRBIT:
|
2008-01-25 14:42:41 +00:00
|
|
|
CRIS_LED_BIT_CLR(arg);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
} /* switch */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
static const struct file_operations gpio_fops = {
|
2010-07-30 17:04:37 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.poll = gpio_poll,
|
|
|
|
.unlocked_ioctl = gpio_ioctl,
|
|
|
|
.write = gpio_write,
|
|
|
|
.open = gpio_open,
|
|
|
|
.release = gpio_release,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 16:52:59 +00:00
|
|
|
.llseek = noop_llseek,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
static void ioif_watcher(const unsigned int gpio_in_available,
|
|
|
|
const unsigned int gpio_out_available,
|
|
|
|
const unsigned char pa_available,
|
|
|
|
const unsigned char pb_available)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2005-07-27 18:44:34 +00:00
|
|
|
unsigned long int flags;
|
2008-01-25 14:42:41 +00:00
|
|
|
|
|
|
|
D(printk(KERN_DEBUG "gpio.c: ioif_watcher called\n"));
|
|
|
|
D(printk(KERN_DEBUG "gpio.c: G in: 0x%08x G out: 0x%08x "
|
|
|
|
"PA: 0x%02x PB: 0x%02x\n",
|
|
|
|
gpio_in_available, gpio_out_available,
|
|
|
|
pa_available, pb_available));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-07-27 18:44:34 +00:00
|
|
|
spin_lock_irqsave(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-07-27 18:44:34 +00:00
|
|
|
dir_g_in_bits = gpio_in_available;
|
|
|
|
dir_g_out_bits = gpio_out_available;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Initialise the dir_g_shadow etc. depending on genconfig */
|
|
|
|
/* 0=input 1=output */
|
2008-01-25 14:42:41 +00:00
|
|
|
if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g0dir, out))
|
2005-04-16 22:20:36 +00:00
|
|
|
dir_g_shadow |= (1 << 0);
|
|
|
|
if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g8_15dir, out))
|
|
|
|
dir_g_shadow |= 0x0000FF00;
|
|
|
|
if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g16_23dir, out))
|
|
|
|
dir_g_shadow |= 0x00FF0000;
|
|
|
|
if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g24dir, out))
|
|
|
|
dir_g_shadow |= (1 << 24);
|
|
|
|
|
2005-07-27 18:44:34 +00:00
|
|
|
changeable_dir_g = changeable_dir_g_mask;
|
2005-04-16 22:20:36 +00:00
|
|
|
changeable_dir_g &= dir_g_out_bits;
|
|
|
|
changeable_dir_g &= dir_g_in_bits;
|
2008-01-25 14:42:41 +00:00
|
|
|
|
|
|
|
/* Correct the bits that can change direction */
|
2005-04-16 22:20:36 +00:00
|
|
|
dir_g_out_bits &= ~changeable_dir_g;
|
|
|
|
dir_g_out_bits |= dir_g_shadow;
|
|
|
|
dir_g_in_bits &= ~changeable_dir_g;
|
|
|
|
dir_g_in_bits |= (~dir_g_shadow & changeable_dir_g);
|
|
|
|
|
2005-07-27 18:44:34 +00:00
|
|
|
spin_unlock_irqrestore(&gpio_lock, flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
printk(KERN_INFO "GPIO port G: in_bits: 0x%08lX out_bits: 0x%08lX "
|
|
|
|
"val: %08lX\n",
|
2005-04-16 22:20:36 +00:00
|
|
|
dir_g_in_bits, dir_g_out_bits, (unsigned long)*R_PORT_G_DATA);
|
|
|
|
printk(KERN_INFO "GPIO port G: dir: %08lX changeable: %08lX\n",
|
|
|
|
dir_g_shadow, changeable_dir_g);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* main driver initialization routine, called from mem.c */
|
|
|
|
|
2008-02-06 13:52:40 +00:00
|
|
|
static int __init gpio_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
#if defined (CONFIG_ETRAX_CSP0_LEDS)
|
|
|
|
int i;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
|
|
|
|
if (res < 0) {
|
|
|
|
printk(KERN_ERR "gpio: couldn't get a major number.\n");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear all leds */
|
|
|
|
#if defined (CONFIG_ETRAX_CSP0_LEDS) || defined (CONFIG_ETRAX_PA_LEDS) || defined (CONFIG_ETRAX_PB_LEDS)
|
2008-01-25 14:42:41 +00:00
|
|
|
CRIS_LED_NETWORK_SET(0);
|
|
|
|
CRIS_LED_ACTIVE_SET(0);
|
|
|
|
CRIS_LED_DISK_READ(0);
|
|
|
|
CRIS_LED_DISK_WRITE(0);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#if defined (CONFIG_ETRAX_CSP0_LEDS)
|
2008-01-25 14:42:41 +00:00
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
CRIS_LED_BIT_SET(i);
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
2005-07-27 18:44:34 +00:00
|
|
|
/* The I/O interface allocation watcher will be called when
|
|
|
|
* registering it. */
|
|
|
|
if (cris_io_interface_register_watcher(ioif_watcher)){
|
2008-01-25 14:42:41 +00:00
|
|
|
printk(KERN_WARNING "gpio_init: Failed to install IO "
|
|
|
|
"if allocator watcher\n");
|
2005-07-27 18:44:34 +00:00
|
|
|
}
|
|
|
|
|
2008-01-25 14:42:41 +00:00
|
|
|
printk(KERN_INFO "ETRAX 100LX GPIO driver v2.5, (c) 2001-2008 "
|
|
|
|
"Axis Communications AB\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
/* We call etrax_gpio_wake_up_check() from timer interrupt and
|
|
|
|
* from cpu_idle() in kernel/process.c
|
|
|
|
* The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
|
|
|
|
* in some tests.
|
2008-01-25 14:42:41 +00:00
|
|
|
*/
|
|
|
|
res = request_irq(TIMER0_IRQ_NBR, gpio_poll_timer_interrupt,
|
|
|
|
IRQF_SHARED | IRQF_DISABLED, "gpio poll", gpio_name);
|
|
|
|
if (res) {
|
2005-04-16 22:20:36 +00:00
|
|
|
printk(KERN_CRIT "err: timer0 irq for gpio\n");
|
2008-01-25 14:42:41 +00:00
|
|
|
return res;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-02-06 13:52:40 +00:00
|
|
|
res = request_irq(PA_IRQ_NBR, gpio_interrupt,
|
2008-01-25 14:42:41 +00:00
|
|
|
IRQF_SHARED | IRQF_DISABLED, "gpio PA", gpio_name);
|
|
|
|
if (res)
|
2005-04-16 22:20:36 +00:00
|
|
|
printk(KERN_CRIT "err: PA irq for gpio\n");
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this makes sure that gpio_init is called during kernel boot */
|
|
|
|
module_init(gpio_init);
|
2008-02-06 13:52:40 +00:00
|
|
|
|