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

📄 i2c-algo-ite.c

📁 IXP425 平台下嵌入式LINUX的I2Cz总线的驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
			timeout = wait_for_pin(adap, &status);			if(timeout) {				iic_stop(adap);				printk("iic_readbytes:  %s read timeout.\n", i2c_adap->name);				return (-1);			}#ifndef STUB_I2C			if (status & ITE_I2CHSR_DB) {				iic_stop(adap);				printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);				return (-1);			}#endif			timeout = wait_for_fe(adap, &status);			if(timeout) {				iic_stop(adap);				printk("iic_readbytes:  %s FIFO is empty\n", i2c_adap->name);				return (-1); 			}			for(j=0; j<32/2; j++) {				tmp.word = iic_inw(adap, ITE_I2CFDR);				buf[rdcount++] = tmp.byte[1];				buf[rdcount++] = tmp.byte[0];			}			/* status FIFO underrun */			iic_inw(adap, ITE_I2CFSR);		}	}	if(remainder) {		remainder=(remainder+1)/2 * 2;		iic_outw(adap, ITE_I2CFBCR, remainder);		if (sread)			iic_outw(adap, ITE_I2CHCR, ITE_SREAD);		else		iic_outw(adap, ITE_I2CHCR, ITE_READ);		/* Issue READ command */		timeout = wait_for_pin(adap, &status);		if(timeout) {			iic_stop(adap);			printk("iic_readbytes:  %s read timeout.\n", i2c_adap->name);			return (-1);		}#ifndef STUB_I2C		if (status & ITE_I2CHSR_DB) {			iic_stop(adap);			printk("iic_readbytes: %s read error - no ack.\n", i2c_adap->name);			return (-1);		}#endif		timeout = wait_for_fe(adap, &status);		if(timeout) {			iic_stop(adap);			printk("iic_readbytes:  %s FIFO is empty\n", i2c_adap->name);			return (-1);		}         		for(i=0; i<(remainder+1)/2; i++) {			tmp.word = iic_inw(adap, ITE_I2CFDR);			buf[rdcount++] = tmp.byte[1];			buf[rdcount++] = tmp.byte[0];		}		/* status FIFO underrun */		iic_inw(adap, ITE_I2CFSR);	}	iic_stop(adap);	return rdcount;}/* This function implements combined transactions.  Combined * transactions consist of combinations of reading and writing blocks of data. * Each transfer (i.e. a read or a write) is separated by a repeated start * condition. */#if 0static int iic_combined_transaction(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num) {   int i;   struct i2c_msg *pmsg;   int ret;   DEB2(printk("Beginning combined transaction\n"));   for(i=0; i<(num-1); i++) {      pmsg = &msgs[i];      if(pmsg->flags & I2C_M_RD) {         DEB2(printk("  This one is a read\n"));         ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);      }      else if(!(pmsg->flags & I2C_M_RD)) {         DEB2(printk("This one is a write\n"));         ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_COMBINED_XFER);      }   }   /* Last read or write segment needs to be terminated with a stop */   pmsg = &msgs[i];   if(pmsg->flags & I2C_M_RD) {      DEB2(printk("Doing the last read\n"));      ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);   }   else if(!(pmsg->flags & I2C_M_RD)) {      DEB2(printk("Doing the last write\n"));      ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);   }   return ret;}#endif/* Whenever we initiate a transaction, the first byte clocked * onto the bus after the start condition is the address (7 bit) of the * device we want to talk to.  This function manipulates the address specified * so that it makes sense to the hardware when written to the IIC peripheral. * * Note: 10 bit addresses are not supported in this driver, although they are * supported by the hardware.  This functionality needs to be implemented. */static inline int iic_doAddress(struct i2c_algo_iic_data *adap,                                struct i2c_msg *msg, int retries) {	unsigned short flags = msg->flags;	unsigned int addr;	int ret;/* Ten bit addresses not supported right now */	if ( (flags & I2C_M_TEN)  ) { #if 0		addr = 0xf0 | (( msg->addr >> 7) & 0x03);		DEB2(printk("addr0: %d\n",addr));		ret = try_address(adap, addr, retries);		if (ret!=1) {			printk("iic_doAddress: died at extended address code.\n");			return -EREMOTEIO;		}		iic_outw(adap,msg->addr & 0x7f);		if (ret != 1) {			printk("iic_doAddress: died at 2nd address code.\n");			return -EREMOTEIO;		}		if ( flags & I2C_M_RD ) {			i2c_repstart(adap);			addr |= 0x01;			ret = try_address(adap, addr, retries);			if (ret!=1) {				printk("iic_doAddress: died at extended address code.\n");				return -EREMOTEIO;			}		}#endif	} else {		addr = ( msg->addr << 1 );#if 0		if (flags & I2C_M_RD )			addr |= 1;		if (flags & I2C_M_REV_DIR_ADDR )			addr ^= 1;#endif		if (iic_inw(adap, ITE_I2CSAR) != addr) {			iic_outw(adap, ITE_I2CSAR, addr);			ret = try_address(adap, addr, retries);			if (ret!=1) {				printk("iic_doAddress: died at address code.\n");				return -EREMOTEIO;			}		}  }	return 0;}/* Description: Prepares the controller for a transaction (clearing status * registers, data buffers, etc), and then calls either iic_readbytes or * iic_sendbytes to do the actual transaction. * * still to be done: Before we issue a transaction, we should * verify that the bus is not busy or in some unknown state. */static int iic_xfer(struct i2c_adapter *i2c_adap,		    struct i2c_msg msgs[], 		    int num){	struct i2c_algo_iic_data *adap = i2c_adap->algo_data;	struct i2c_msg *pmsg;	int i = 0;	int ret, timeout;    	pmsg = &msgs[i];	if(!pmsg->len) {		DEB2(printk("iic_xfer: read/write length is 0\n");)		return -EIO;	}	if(!(pmsg->flags & I2C_M_RD) && (!(pmsg->len)%2) ) {		DEB2(printk("iic_xfer: write buffer length is not odd\n");)		return -EIO; 	}	/* Wait for any pending transfers to complete */	timeout = wait_for_bb(adap);	if (timeout) {		DEB2(printk("iic_xfer: Timeout waiting for host not busy\n");)		return -EIO;	}	/* Flush FIFO */	iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);	/* Load address */	ret = iic_doAddress(adap, pmsg, i2c_adap->retries);	if (ret)		return -EIO;#if 0	/* Combined transaction (read and write) */	if(num > 1) {           DEB2(printk("iic_xfer: Call combined transaction\n"));           ret = iic_combined_transaction(i2c_adap, msgs, num);  }#endif	DEB3(printk("iic_xfer: Msg %d, addr=0x%x, flags=0x%x, len=%d\n",		i, msgs[i].addr, msgs[i].flags, msgs[i].len);)	if(pmsg->flags & I2C_M_RD) 		/* Read */		ret = iic_readbytes(i2c_adap, pmsg->buf, pmsg->len, 0);	else {													/* Write */ 		udelay(1000);		ret = iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len);	}	if (ret != pmsg->len)		DEB3(printk("iic_xfer: error or fail on read/write %d bytes.\n",ret)); 	else		DEB3(printk("iic_xfer: read/write %d bytes.\n",ret));	return ret;}/* Implements device specific ioctls.  Higher level ioctls can * be found in i2c-core.c and are typical of any i2c controller (specifying * slave address, timeouts, etc).  These ioctls take advantage of any hardware * features built into the controller for which this algorithm-adapter set * was written.  These ioctls allow you to take control of the data and clock * lines and set the either high or low, * similar to a GPIO pin. */static int algo_control(struct i2c_adapter *adapter, 	unsigned int cmd, unsigned long arg){  struct i2c_algo_iic_data *adap = adapter->algo_data;  struct i2c_iic_msg s_msg;  char *buf;	int ret;  if (cmd == I2C_SREAD) {		if(copy_from_user(&s_msg, (struct i2c_iic_msg *)arg, 				sizeof(struct i2c_iic_msg))) 			return -EFAULT;		buf = kmalloc(s_msg.len, GFP_KERNEL);		if (buf== NULL)			return -ENOMEM;		/* Flush FIFO */		iic_outw(adap, ITE_I2CFCR, ITE_I2CFCR_FLUSH);		/* Load address */		iic_outw(adap, ITE_I2CSAR,s_msg.addr<<1);		iic_outw(adap, ITE_I2CSSAR,s_msg.waddr & 0xff);		ret = iic_readbytes(adapter, buf, s_msg.len, 1);		if (ret>=0) {			if(copy_to_user( s_msg.buf, buf, s_msg.len) ) 				ret = -EFAULT;		}		kfree(buf);	}	return 0;}static u32 iic_func(struct i2c_adapter *adap){	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR | 	       I2C_FUNC_PROTOCOL_MANGLING; }/* -----exported algorithm data: -------------------------------------	*/static struct i2c_algorithm iic_algo = {	"ITE IIC algorithm",	I2C_ALGO_IIC,	iic_xfer,		/* master_xfer	*/	NULL,				/* smbus_xfer	*/	NULL,				/* slave_xmit		*/	NULL,				/* slave_recv		*/	algo_control,			/* ioctl		*/	iic_func,			/* functionality	*/};/*  * registering functions to load algorithms at runtime  */int i2c_iic_add_bus(struct i2c_adapter *adap){	int i;	short status;	struct i2c_algo_iic_data *iic_adap = adap->algo_data;	if (iic_test) {		int ret = test_bus(iic_adap, adap->name);		if (ret<0)			return -ENODEV;	}	DEB2(printk("i2c-algo-ite: hw routines for %s registered.\n",	            adap->name));	/* register new adapter to i2c module... */	adap->id |= iic_algo.id;	adap->algo = &iic_algo;	adap->timeout = 100;	/* default values, should	*/	adap->retries = 3;		/* be replaced by defines	*/	adap->flags = 0;#ifdef MODULE	MOD_INC_USE_COUNT;#endif	i2c_add_adapter(adap);	iic_init(iic_adap);	/* scan bus */	/* By default scanning the bus is turned off. */	if (iic_scan) {		printk(KERN_INFO " i2c-algo-ite: scanning bus %s.\n",		       adap->name);		for (i = 0x00; i < 0xff; i+=2) {			iic_outw(iic_adap, ITE_I2CSAR, i);			iic_start(iic_adap);			if ( (wait_for_pin(iic_adap, &status) == 0) && 			    ((status & ITE_I2CHSR_DNE) == 0) ) { 				printk(KERN_INFO "\n(%02x)\n",i>>1); 			} else {				printk(KERN_INFO "."); 				iic_reset(iic_adap);			}			udelay(iic_adap->udelay);		}	}	return 0;}int i2c_iic_del_bus(struct i2c_adapter *adap){	int res;	if ((res = i2c_del_adapter(adap)) < 0)		return res;	DEB2(printk("i2c-algo-ite: adapter unregistered: %s\n",adap->name));#ifdef MODULE	MOD_DEC_USE_COUNT;#endif	return 0;}int __init i2c_algo_iic_init (void){	printk(KERN_INFO "ITE iic (i2c) algorithm module\n");	return 0;}void i2c_algo_iic_exit(void){	return;}EXPORT_SYMBOL(i2c_iic_add_bus);EXPORT_SYMBOL(i2c_iic_del_bus);/* The MODULE_* macros resolve to nothing if MODULES is not defined * when this file is compiled. */MODULE_AUTHOR("MontaVista Software <www.mvista.com>");MODULE_DESCRIPTION("ITE iic algorithm");MODULE_LICENSE("GPL");MODULE_PARM(iic_test, "i");MODULE_PARM(iic_scan, "i");MODULE_PARM(i2c_debug,"i");MODULE_PARM_DESC(iic_test, "Test if the I2C bus is available");MODULE_PARM_DESC(iic_scan, "Scan for active chips on the bus");MODULE_PARM_DESC(i2c_debug,        "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");/* This function resolves to init_module (the function invoked when a module * is loaded via insmod) when this file is compiled with MODULES defined. * Otherwise (i.e. if you want this driver statically linked to the kernel), * a pointer to this function is stored in a table and called * during the intialization of the kernel (in do_basic_setup in /init/main.c)  * * All this functionality is complements of the macros defined in linux/init.h */module_init(i2c_algo_iic_init);/* If MODULES is defined when this file is compiled, then this function will * resolved to cleanup_module. */module_exit(i2c_algo_iic_exit);

⌨️ 快捷键说明

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