⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpio.c

📁 优龙2410linux2.6.8内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: gpio.c,v 1.11 2004/05/14 07:58:03 starvik Exp $ * * Etrax general port I/O device * * Copyright (c) 1999, 2000, 2001, 2002 Axis Communications AB * * Authors:    Bjorn Wesen      (initial version) *             Ola Knutsson     (LED handling) *             Johan Adolfsson  (read/set directions, write, port G) * * $Log: gpio.c,v $ * Revision 1.11  2004/05/14 07:58:03  starvik * Merge of changes from 2.4 * * Revision 1.9  2003/09/11 07:29:48  starvik * Merge of Linux 2.6.0-test5 * * Revision 1.8  2003/07/04 08:27:37  starvik * Merge of Linux 2.5.74 * * Revision 1.7  2003/01/10 07:44:07  starvik * init_ioremap is now called by kernel before drivers are initialized * * Revision 1.6  2002/12/11 13:13:57  starvik * Added arch/ to v10 specific includes * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer) * * Revision 1.5  2002/11/20 11:56:11  starvik * Merge of Linux 2.5.48 * * Revision 1.4  2002/11/18 10:10:05  starvik * Linux 2.5 port of latest gpio.c from Linux 2.4 * * Revision 1.20  2002/10/16 21:16:24  johana * Added support for PA high level interrupt. * That gives 2ms response time with iodtest for high levels and 2-12 ms * response time on low levels if the check is not made in * process.c:cpu_idle() as well. * * Revision 1.19  2002/10/14 18:27:33  johana * Implemented alarm handling so select() now works. * Latency is around 6-9 ms with a etrax_gpio_wake_up_check() in * cpu_idle(). * Otherwise I get 15-18 ms (same as doing the poll in userspace - * but less overhead). * TODO? Perhaps we should add the check in IMMEDIATE_BH (or whatever it * is in 2.4) as well? * TODO? Perhaps call request_irq()/free_irq() only when needed? * Increased version to 2.5 * * Revision 1.18  2002/10/11 15:02:00  johana * Mask inverted 8 bit value in setget_input(). * * Revision 1.17  2002/06/17 15:53:01  johana * Added IO_READ_INBITS, IO_READ_OUTBITS, IO_SETGET_INPUT and IO_SETGET_OUTPUT * that take a pointer as argument and thus can handle 32 bit ports (G) * correctly. * These should be used instead of IO_READBITS, IO_SETINPUT and IO_SETOUTPUT. * (especially if Port G bit 31 is used) * * Revision 1.16  2002/06/17 09:59:51  johana * Returning 32 bit values in the ioctl return value doesn't work if bit * 31 is set (could happen for port G), so mask it of with 0x7FFFFFFF. * A new set of ioctl's will be added. * * Revision 1.15  2002/05/06 13:19:13  johana * IO_SETINPUT returns mask with bit set = inputs for PA and PB as well. * * Revision 1.14  2002/04/12 12:01:53  johana * Use global r_port_g_data_shadow. * Moved gpio_init_port_g() closer to gpio_init() and marked it __init. * * Revision 1.13  2002/04/10 12:03:55  johana * Added support for port G /dev/gpiog (minor 3). * Changed indentation on switch cases. * Fixed other spaces to tabs. * * Revision 1.12  2001/11/12 19:42:15  pkj * * Corrected return values from gpio_leds_ioctl(). * * Fixed compiler warnings. * * Revision 1.11  2001/10/30 14:39:12  johana * Added D() around gpio_write printk. * * Revision 1.10  2001/10/25 10:24:42  johana * Added IO_CFG_WRITE_MODE ioctl and write method that can do fast * bittoggling in the kernel. (This speeds up programming an FPGA with 450kB * from ~60 seconds to 4 seconds). * Added save_flags/cli/restore_flags in ioctl. * * Revision 1.9  2001/05/04 14:16:07  matsfg * Corrected spelling error * * Revision 1.8  2001/04/27 13:55:26  matsfg * Moved initioremap. * Turns off all LEDS on init. * Added support for shutdown and powerbutton. * * Revision 1.7  2001/04/04 13:30:08  matsfg * Added bitset and bitclear for leds. Calls init_ioremap to set up memmapping * * Revision 1.6  2001/03/26 16:03:06  bjornw * Needs linux/config.h * * Revision 1.5  2001/03/26 14:22:03  bjornw * Namechange of some config options * * Revision 1.4  2001/02/27 13:52:48  bjornw * malloc.h -> slab.h * * Revision 1.3  2001/01/24 15:06:48  bjornw * gpio_wq correct type * * Revision 1.2  2001/01/18 16:07:30  bjornw * 2.4 port * * Revision 1.1  2001/01/18 15:55:16  bjornw * Verbatim copy of etraxgpio.c from elinux 2.0 added * * */#include <linux/config.h>#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>#include <asm/arch/svinto.h>#include <asm/io.h>#include <asm/system.h>#include <asm/irq.h>#define GPIO_MAJOR 120  /* experimental MAJOR number */#define D(x)#if 0static int dp_cnt;#define DP(x) do { dp_cnt++; if (dp_cnt % 1000 == 0) x; }while(0)#else#define DP(x)#endif	static char gpio_name[] = "etrax gpio";#if 0static wait_queue_head_t *gpio_wq;#endifstatic int gpio_ioctl(struct inode *inode, struct file *file,		      unsigned int cmd, unsigned long arg);static ssize_t gpio_write(struct file * file, const char * buf, size_t count,                          loff_t *off);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 */static struct gpio_private *alarmlist = 0;static int gpio_some_alarms = 0; /* Set if someone uses alarm */static unsigned long gpio_pa_irq_enabled_mask = 0;/* Port A and B use 8 bit access, but Port G is 32 bit */#define NUM_PORTS (GPIO_MINOR_B+1)static volatile unsigned char *ports[NUM_PORTS] = { 	R_PORT_PA_DATA, 	R_PORT_PB_DATA,};static volatile unsigned char *shads[NUM_PORTS] = {	&port_pa_data_shadow, 	&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#endifstatic unsigned char changeable_dir[NUM_PORTS] = { 	CONFIG_ETRAX_PA_CHANGEABLE_DIR,	CONFIG_ETRAX_PB_CHANGEABLE_DIR };static unsigned char changeable_bits[NUM_PORTS] = { 	CONFIG_ETRAX_PA_CHANGEABLE_BITS,	CONFIG_ETRAX_PB_CHANGEABLE_BITS };static volatile unsigned char *dir[NUM_PORTS] = { 	R_PORT_PA_DIR, 	R_PORT_PB_DIR };static volatile unsigned char *dir_shadow[NUM_PORTS] = {	&port_pa_dir_shadow, 	&port_pb_dir_shadow };/* Port G is 32 bit, handle it special, some bits are both inputs    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)static unsigned int gpio_poll(struct file *file,	  poll_table *wait){	unsigned int mask = 0;	struct gpio_private *priv = (struct gpio_private *)file->private_data;	unsigned long data;	poll_wait(file, &priv->alarm_wq, wait);	if (priv->minor == GPIO_MINOR_A) {		unsigned long flags;		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);		save_flags(flags); cli();		gpio_pa_irq_enabled_mask |= tmp;		*R_IRQ_MASK1_SET = tmp;		restore_flags(flags);	} else if (priv->minor == GPIO_MINOR_B)		data = *R_PORT_PB_DATA;	else if (priv->minor == GPIO_MINOR_G)		data = *R_PORT_G_DATA;	else		return 0;		if ((data & priv->highalarm) ||	    (~data & priv->lowalarm)) {		mask = POLLIN|POLLRDNORM;	}		DP(printk("gpio_poll ready: mask 0x%08X\n", mask));	return mask;}int etrax_gpio_wake_up_check(void){	struct gpio_private *priv = alarmlist;	unsigned long data = 0;        int ret = 0;	while (priv) {		if (USE_PORTS(priv)) {			data = *priv->port;		} else if (priv->minor == GPIO_MINOR_G) {			data = *R_PORT_G_DATA;		}		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;	}        return ret;}static irqreturn_tgpio_poll_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs){	if (gpio_some_alarms) {		etrax_gpio_wake_up_check();                return IRQ_HANDLED;	}        return IRQ_NONE;}static irqreturn_tgpio_pa_interrupt(int irq, void *dev_id, struct pt_regs *regs){	unsigned long tmp;	/* 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;	if (gpio_some_alarms) {		return IRQ_RETVAL(etrax_gpio_wake_up_check());	}        return IRQ_NONE;}static ssize_t gpio_write(struct file * file, const char * buf, size_t count,                          loff_t *off){	struct gpio_private *priv = (struct gpio_private *)file->private_data;	unsigned char data, clk_mask, data_mask, write_msb;	unsigned long flags;	ssize_t retval = count;	if (priv->minor !=GPIO_MINOR_A && priv->minor != GPIO_MINOR_B) {		return -EFAULT;	}    	if (verify_area(VERIFY_READ, buf, count)) {		return -EFAULT;	}	clk_mask = priv->clk_mask;	data_mask = priv->data_mask;	/* It must have been configured using the IO_CFG_WRITE_MODE */	/* Perhaps a better error code? */	if (clk_mask == 0 || data_mask == 0) {		return -EPERM;	}	write_msb = priv->write_msb;	D(printk("gpio_write: %lu to data 0x%02X clk 0x%02X msb: %i\n",count, data_mask, clk_mask, write_msb));	while (count--) {		int i;		data = *buf++;		if (priv->write_msb) {			for (i = 7; i >= 0;i--) {				local_irq_save(flags); local_irq_disable();				*priv->port = *priv->shadow &= ~clk_mask;				if (data & 1<<i)					*priv->port = *priv->shadow |= data_mask;				else					*priv->port = *priv->shadow &= ~data_mask;			/* For FPGA: min 5.0ns (DCC) before CCLK high */				*priv->port = *priv->shadow |= clk_mask;				local_irq_restore(flags);			}		} else {			for (i = 0; i <= 7;i++) {				local_irq_save(flags); local_irq_disable();				*priv->port = *priv->shadow &= ~clk_mask;				if (data & 1<<i)					*priv->port = *priv->shadow |= data_mask;				else					*priv->port = *priv->shadow &= ~data_mask;			/* For FPGA: min 5.0ns (DCC) before CCLK high */				*priv->port = *priv->shadow |= clk_mask;				local_irq_restore(flags);			}		}	}	return retval;}static intgpio_open(struct inode *inode, struct file *filp){	struct gpio_private *priv;	int p = MINOR(inode->i_rdev);	if (p > GPIO_MINOR_LAST)		return -EINVAL;	priv = (struct gpio_private *)kmalloc(sizeof(struct gpio_private), 					      GFP_KERNEL);	if (!priv)		return -ENOMEM;	priv->minor = p;	/* initialize the io/alarm struct and link it into our alarmlist */	priv->next = alarmlist;	alarmlist = priv;	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);	filp->private_data = (void *)priv;	return 0;}static intgpio_release(struct inode *inode, struct file *filp){	struct gpio_private *p = alarmlist;	struct gpio_private *todel = (struct gpio_private *)filp->private_data;		/* 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;			return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -