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

📄 i2c-ov9640.c

📁 linux I2C driver for pxa cpu, ov9640 camera. Also include a doc about linux I2C driver
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (res<=0) {printk("%s:write cmd address fail\n",__FUNCTION__);return res;}
	else{printk("%s :write cmd success\n",__FUNCTION__);}
	
	res=i2c_transfer(g_client->adapter,&msgs[1],6);
	if (res<=0) {printk("%s:write cmd container fail\n",__FUNCTION__);return res;}
	else{printk("%s :write cmd container success\n",__FUNCTION__);}


	//res=i2c_smbus_write_byte_data(g_client, addr, value );
	#if 0
	//addr=addr<<1;//add by undiso
	char 	buf[2]={addr,value};
//	i2c_ov9640_inc_use(g_client);
	res = i2c_master_send(g_client, buf, 2);
//	i2c_ov9640_dec_use(g_client);
	if (res >0) {res =0; printk("%s send success\n", __FUNCTION__);}
	else {res =-1;printk("%s  send fail\n",__FUNCTION__);}
	DPRINTK(KERN_INFO "In funtion %s addr:%x value:%xreturn %d \n", __FUNCTION__, addr,value,res);
	return res;
	#endif
}	
#endif


#if 0
int i2c_ov9640_read(struct i2c_client *client, u8 reg)
{
	unsigned char msgbuf=0;
	printk("%s\n",__FUNCTION__);
	DPRINTK("in function %s\n",__FUNCTION__);
	i2c_master_recv(client,&msgbuf,1);
	return msgbuf;

//    return i2c_smbus_read_word_data(client,reg);
//  return i2c_smbus_read_byte_data(client,reg);
}
#endif

#if 0
int i2c_ov9640_write(struct i2c_client *client, u8 reg, u16 value)
{
printk("%s\n",__FUNCTION__);
    return i2c_smbus_write_word_data(client,reg,value);
}
#endif

#if 1

int i2c_ov9640_attach_adapter(struct i2c_adapter *adap)
{
	DPRINTK("In function %s.\n", __FUNCTION__);
	return i2c_probe(adap,&addr_data,i2c_ov9640_detect_client);
}
#endif

int i2c_ov9640_detect_client(struct i2c_adapter *adapter, int address, unsigned short flags, int kind)
{
    struct i2c_client *new_client;
    int err = 0;
/*colin add for download firmware*/
	//struct file *filp;
//	char buf[120];
//	int len;
//	mm_segment_t  oldfs;
/*colin add for download firmware*/
printk("%s\n",__FUNCTION__);

//    struct ov9640_data *data;

    /*check if */
    if(g_client != NULL) {
      err = -ENXIO;
      goto ERROR0;
    }
 

	DPRINTK(KERN_INFO "In funtion %s. address=0X%X\n", __FUNCTION__, address);
    /* Let's see whether this adapter can support what we need.
       Please substitute the things you need here!  */
	if ( !i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA) ) {
		DPRINTK(KERN_INFO "Word op is not permited.\n");
		goto ERROR0;
	}

    /* OK. For now, we presume we have a valid client. We now create the
       client structure, even though we cannot fill it completely yet.
       But it allows us to access several i2c functions safely */
    
    /* Note that we reserve some space for ov9640_data too. If you don't
       need it, remove it. We do it here to help to lessen memory
       fragmentation. */

    new_client=kzalloc(sizeof(struct i2c_client),GFP_KERNEL );

    if ( !new_client )  {
      err = -ENOMEM;
      goto ERROR0;
    }

	memset(new_client,0,sizeof(struct i2c_client));
//	data = (struct ov9640_data *) (new_client + 1);
/*
	if(!(data =kzalloc(sizeof( struct ov9640_data),GFP_KERNEL))) 
	{
		printk(KERN_INFO"alloc memory fail\n");
		return -ENOMEM;
	}*/
	new_client->addr = address;	
	new_client->adapter = adapter;
	new_client->driver = &ov9640_driver;
	new_client->flags = 0;

    g_client = new_client;

    /* Now, we do the remaining detection. If no `force' parameter is used. */

    /* First, the generic detection (if any), that is skipped if any force
       parameter was used. */

	if (kind <= 0) {
                 char res = -1;
		mdelay(2000);
		 ov9640_read(REV,&res);
		/* The below is of course bogus */
		DPRINTK("I2C: Probe ov9640 chip..addr=0x%x, REV=%d, res=0x%x\n", address, REV, res);
                /*ov9640 chip id is 0x9648
                 if(res != OV9640_CHIP_ID) {
			DPRINTK(KERN_WARNING "Failed.product id =%d \n",res);
			goto ERROR1;
		 }		 
		else {
                       DPRINTK("OV9640 chip id is 0X%04X\n", OV9640_CHIP_ID);
			if ( ov9640_id == 0 )
				DPRINTK(" detected.\n");
		}*/
	}

	strcpy(new_client->name, "ov9640");
    /* Automatically unique */
  

    /* Tell the i2c layer a new client has arrived */
    if ((err = i2c_attach_client(new_client)))
      goto ERROR3;

    /* This function can write default values to the client registers, if
       needed. */
	/*	ov9640_init_client(new_client);	*/
/**/
/*	filp=filp_open("/JFFS2/camera/a.txt",O_RDONLY,0);
	printk(KERN_INFO"open file succeed!\n");
	if(IS_ERR(filp)) 
	{
		printk(KERN_INFO"open file error!\n");
	}
	if(filp->f_op->read ==NULL) 
	{
		printk(KERN_INFO"no read fun!\n");
	}

	oldfs=get_fs();
	set_fs(KERNEL_DS);
	len=100;

	filp->f_op->read(filp,buf,len,&filp->f_pos);

	set_fs(oldfs);

	fput(filp);


	printk(KERN_INFO"read file succeed! the file is %s\n",buf);

*/


    return 0;

    /* OK, this is not exactly good programming practice, usually. But it is
       very code-efficient in this case. */

ERROR3:
ERROR1:
      kfree(new_client);
      g_client = NULL;
ERROR0:
      return err;
}

int i2c_ov9640_detach_client(struct i2c_client *client)
{
	int err;
printk("%s\n",__FUNCTION__);
    /* Try to detach the client from i2c space */
    if ((err = i2c_detach_client(client))) {
      DPRINTK("ov9640.o: Client deregistration failed, client not detached.\n");
      return err;
    }

    kfree(client); /* Frees client data too, if allocated at the same time */
    g_client = NULL;
    return 0;
}

/* 	Keep track of how far we got in the initialization process. If several
	things have to initialized, and we fail halfway, only those things
	have to be cleaned up! */
static int ov9640_initialized = 0;

int i2c_ov9640_init(void)
{
	int res;

	if (ov9640_initialized) 
		return 0;

	
	#if 0

 //   ov9640_gpio_init();
       pxa_gpio_mode(81 | GPIO_ALT_FN_2_IN);	/* CIF_DD[0] */
	pxa_gpio_mode(55 | GPIO_ALT_FN_1_IN);	/* CIF_DD[1] */
	pxa_gpio_mode(51 | GPIO_ALT_FN_1_IN);	/* CIF_DD[2] */
	pxa_gpio_mode(50 | GPIO_ALT_FN_1_IN);	/* CIF_DD[3] */
	pxa_gpio_mode(52 | GPIO_ALT_FN_1_IN);	/* CIF_DD[4] */
	pxa_gpio_mode(48 | GPIO_ALT_FN_1_IN);	/* CIF_DD[5] */
	pxa_gpio_mode(17 | GPIO_ALT_FN_2_IN);	/* CIF_DD[6] */
	pxa_gpio_mode(108 | GPIO_ALT_FN_1_IN);	/* CIF_DD[7] */
	pxa_gpio_mode(53 | GPIO_ALT_FN_2_OUT);	/* CIF_MCLK */
	pxa_gpio_mode(54 | GPIO_ALT_FN_3_IN);	/* CIF_PCLK */
	pxa_gpio_mode(85 | GPIO_ALT_FN_3_IN);	/* CIF_LV */
	pxa_gpio_mode(84 | GPIO_ALT_FN_3_IN);	/* CIF_FV */




	mdelay(200);
	ci_init();
	mdelay(200);
	ci_set_mode(CI_MODE_MP,CI_DATA_WIDTH8);
	mdelay(200);
	ov9640_set_clock(0,1,1,2600);

	mdelay(80);


	ci_set_polarity(0,0,0);
	mdelay(200);
	ci_set_fifo(0,CI_FIFO_THL_32,1,1);
	mdelay(200);
  	GAFR0_L &= (~(0x3 << 4));
    	GPSR0 |= (1 << 2 );
	GPDR0 |= (1 << 2 );

		
    	GAFR2_U &=  (~(0x3 << 14));
	GPSR2 |= (1 << 23);	//set high	
	GPDR2 |= (1 << 23);//set as output
	pcf8574_write(0x2A,0x10);//camera power down
 	mdelay(200);

	pcf8574_write(0x2A,0xef);//camera power up

	mdelay(200);

	pcf8574_write(0x2A,0xdf);//camera resset
	mdelay(100);
	pcf8574_write(0x2A,0x20);//camera reset
	mdelay(100);
/*	GAFR1_U &= (~(0x3 << 10));
	GPDR1 |= (1 <<21 );

    mdelay(100);
	*/
	ci_enable(1);

	mdelay(100);

#endif

	DPRINTK("%s:I2C: driver for device ov9640.\n",__FUNCTION__);
	if ( (res = i2c_add_driver(&ov9640_driver)) ) {
		DPRINTK("ov9640: Driver registration failed, module not inserted.\n");
		i2c_ov9640_cleanup();
		return res;
	}
	ov9640_initialized ++;

        if(g_client != NULL)
   	   DPRINTK("%s,I2C: driver for device %s registed!.\n",__FUNCTION__, g_client->name);
        else 
           DPRINTK("%s: I2C: driver for device unregisted!.\n",__FUNCTION__);
	return 0;
}

int i2c_ov9640_cleanup(void)
{
	int res;
printk("%s\n",__FUNCTION__);
	if (ov9640_initialized == 1) {
		if ((res = i2c_del_driver(&ov9640_driver))) {
			DPRINTK("ov9640: Driver registration failed, module not removed.\n");
			return res;
		}
		ov9640_initialized --;
	}
	return 0;
}

//EXPORT_SYMBOL(ov9640_read_status);
EXPORT_SYMBOL(i2c_ov9640_init);
EXPORT_SYMBOL(ov9640_write);
EXPORT_SYMBOL(ov9640_write_new);//undiso 070711
EXPORT_SYMBOL(ov9640_read);
EXPORT_SYMBOL(ov9640_read_new);//undiso 070710
EXPORT_SYMBOL(ov9640_read_status);
EXPORT_SYMBOL(i2c_ov9640_cleanup);
//module_init(i2c_ov9640_init);
//module_exit(i2c_ov9640_cleanup);
MODULE_LICENSE("GPL");

⌨️ 快捷键说明

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