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

📄 i2c-ov9640.c

📁 linux I2C driver for pxa cpu, ov9640 camera. Also include a doc about linux I2C driver
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <asm/hardware.h>
#include <asm/types.h>
#include <linux/delay.h>
#include "/undiso/MNH/linux-2.6.17.14/drivers/media/video/camera.h"
#include <asm/arch/pxa-regs.h>

#include "i2c-ov9640.h"

#define DEBUG 1
#define DPRINTK(fmt,args...)	do { if (DEBUG) printk("in function %s "fmt,__FUNCTION__,##args);} while(0)
extern int i2c_adapter_id(struct i2c_adapter *adap);

int  i2c_ov9640_cleanup(void);
//void i2c_ov9640_inc_use (struct i2c_client *client);
//void i2c_ov9640_dec_use (struct i2c_client *client);
int  i2c_ov9640_attach_adapter(struct i2c_adapter *adapter);
int  i2c_ov9640_detect_client(struct i2c_adapter *, int, unsigned short, int);
int  i2c_ov9640_detach_client(struct i2c_client *client);

struct i2c_driver ov9640_driver  = 
{
	.driver=
	{
		.name=			"ov9640 driver",	            /* name           */
		.owner= THIS_MODULE,
	},
	id:			I2C_DRIVERID_OV9640,         	    /* id             */
//	flags:			I2C_NO_NOTIFY,        		    /* flags          */
	.attach_adapter=		i2c_ov9640_attach_adapter,       /* attach_adapter */
	.detach_client=         i2c_ov9640_detach_client,        /* detach_client  */
	command:		NULL,
//	inc_use:		&i2c_ov9640_inc_use,
//	dec_use:		&i2c_ov9640_dec_use
};

extern  struct i2c_adapter *i2cdev_adaps[];
/* Unique ID allocation */
static int ov9640_id = 0;
struct i2c_client *g_client = NULL;
static unsigned short normal_i2c[] = {OV9640_SLAVE_ADDR ,I2C_CLIENT_END };
static unsigned short normal_i2c_range[] = { I2C_CLIENT_END }; 
//I2C_CLIENT_INSMOD;
static unsigned short *forces[]={NULL};

static struct i2c_client_address_data addr_data=
{
	.normal_i2c=normal_i2c,
	.probe=normal_i2c_range,
	.ignore=normal_i2c_range,
	.forces=forces,
};

/* 
 * This call returns a unique low identifier for each registered adapter,
 * or -1 if the adapter was not registered.
 */
/*
void i2c_ov9640_inc_use (struct i2c_client *client)
{
//	MOD_INC_USE_COUNT;
#ifdef MODULE
#endif
}

void i2c_ov9640_dec_use (struct i2c_client *client)
{
//	MOD_DEC_USE_COUNT;
#ifdef MODULE
#endif
}*/
#if 1
int ov9640_read(u8 addr, u8 *pvalue)
{	
	//printk("%s\n",__FUNCTION__);
	int	res=0;
	char 	buf=0;
	static unsigned char buffer[1]={0x30};
	struct i2c_msg msgs[2] ={ 
		{ 0x30, 0, 1,buffer },
		{ 0x30, 1, 1,&buf }};

	if( g_client == NULL )	
		return -1;
//	i2c_ov9640_inc_use(g_client);
//	msgs[0].addr=msgs[1].addr=g_client->addr;
	res=i2c_transfer(g_client->adapter,msgs,2);
	if (res<=0) 
		goto out;
	//res=i2c_transfer(g_client->adapter,&msgs[1],1);
	//if (res<=0) 
		//goto out;
	printk("%s--buf value=%x\n",__FUNCTION__, buf);
	*pvalue=buf;

	printk("%s-- pvalue=%x,pvalue addr=%d\n",__FUNCTION__,*pvalue,&pvalue);
	return res;
//	i2c_ov9640_dec_use(g_client);
out:
	DPRINTK(KERN_INFO "In funtion %s addr:%x,value=%x\n", __FUNCTION__, addr,*pvalue);
	if (res<=0) DPRINTK("res = %d \n",res);
	return res;
}
#endif

#if 1
int ov9640_read_new(u8 addr,u8 subaddr, u8 *pvalue)
{	
	//printk("%s\n",__FUNCTION__);
	int	res=0;
	char 	buf=0;
	 char buffer[1]={subaddr};
	struct i2c_msg msgs[2] ={ 
		{ 0x30, 0|I2C_M_NOSTART, 1,buffer },
		{ 0x30, 1, 1,&buf }};

	if( g_client == NULL )	
		return -1;
//	i2c_ov9640_inc_use(g_client);
//	msgs[0].addr=msgs[1].addr=g_client->addr;
	res=i2c_transfer(g_client->adapter,msgs,2);
	if (res<=0) 
		goto out;
	//res=i2c_transfer(g_client->adapter,&msgs[1],1);
	//if (res<=0) 
		//goto out;
	printk("%s--subaddr=%x  ;buf value=%x\n",__FUNCTION__, subaddr,buf);
	*pvalue=buf;

//	printk("%s-- pvalue=%x,pvalue addr=%d\n",__FUNCTION__,*pvalue,&pvalue);
	return res;
//	i2c_ov9640_dec_use(g_client);
out:
	DPRINTK(KERN_INFO "In funtion %s addr:%x,value=%x\n", __FUNCTION__, addr,*pvalue);
	if (res<=0) DPRINTK("res = %d \n",res);
	return res;
}
#endif
#if 0
static int x1205_get_status(struct i2c_client *client, unsigned char *sr)
{
	static unsigned char sr_addr[2] = { 0, X1205_REG_SR };

	struct i2c_msg msgs[] = {
		{ client->addr, 0, 2, sr_addr },	/* setup read ptr */
		{ client->addr, I2C_M_RD, 1, sr },	/* read status */
	};

	/* read status register */
	if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
		dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
		return -EIO;
	}

	return 0;
}
#endif

int ov9640_write(u8 addr, u8 *value,int size)
{

	int	res = 0;
	
	if( g_client == NULL )
		return -1;
	
	char buffer = addr;
	struct i2c_msg msgs[1] = //{
		//{ addr, 0, 1, &buffer},
		{ addr, 0, size, value };//};
		
	printk("%s\n",__FUNCTION__);	
	
//	msgs[0].addr  = g_client->addr;

	//res=i2c_transfer(g_client->adapter,&msgs[0],1);
	//if (res<=0){printk("%s---address transfer FAIL!!!res=%d\n",__FUNCTION__,res); return res;}
	//else {printk("%s---address transfer success!!!res=%d\n",__FUNCTION__,res);}

	res = i2c_transfer(g_client->adapter,msgs,1);

	if (res <= 0) { printk("%s---message transfer FAIL!!!res=%d\n",__FUNCTION__,res); return res;}
	else {printk("%s---address transfer success res=%d!!!\n",__FUNCTION__,res);}
	
	//res=i2c_smbus_write_byte_data(g_client, addr, value );
	
/*
	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;

}
/*****************undiso************************/
int ov9640_write_new(u8 addr, u8 subaddr,int value)
{

	int	res = 0;
	
	if( g_client == NULL )
		return -1;
	
	char buffer[2] = {subaddr,value};
	struct i2c_msg msgs[1] = //{
		//{ addr, 0, 1, &buffer},
		{ addr, 0, 2, &buffer };//};
		
	printk("%s\n",__FUNCTION__);	
	
//	msgs[0].addr  = g_client->addr;

	//res=i2c_transfer(g_client->adapter,&msgs[0],1);
	//if (res<=0){printk("%s---address transfer FAIL!!!res=%d\n",__FUNCTION__,res); return res;}
	//else {printk("%s---address transfer success!!!res=%d\n",__FUNCTION__,res);}

	res = i2c_transfer(g_client->adapter,msgs,1);

	if (res <= 0) { printk("%s---message transfer FAIL!!!res=%d\n",__FUNCTION__,res); return res;}
	else {printk("%s---address transfer success res=%d!!!\n",__FUNCTION__,res);}
	
	//res=i2c_smbus_write_byte_data(g_client, addr, value );
	
/*
	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;

}

/********undiso*********/

int ov9640_read_status(u8 addr, u8 *pvalue)

{
    int res = 0;
	int i = 0;

	char buf[8] = {0,0,0,0,0,0,0,0};
    char buffer[1] = {0xfe};

	struct i2c_msg msgs[2] = {
       { 0x30, 0, 1, buffer },
       { 0x30, 1, 8, buf }    };

    printk("%s\n",__FUNCTION__);

	if( g_client == NULL )   
       return -1;

//  i2c_ov9640_inc_use(g_client);

  //  msgs[0].addr=g_client->addr;

    res=i2c_transfer(g_client->adapter,msgs,2);

    if (res<=0) 
       goto out;
	else
		printk("%s--read status ADDRESS success!!\n",__FUNCTION__);

   // res=i2c_transfer(g_client->adapter,&msgs[1],1);

 //   if (res<=0) 

     //  goto out;
	//else
	//	printk("%s--read status success!!",__FUNCTION__);
    for( i = 0; i < 8; i++)
    pvalue[i] = buf[i];
	
//	printk("%s---0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x\n",__FUNCTION__,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);

//  i2c_ov9640_dec_use(g_client);

    return res;

out:

    DPRINTK(KERN_INFO "In funtion %s addr:%x,value=%x\n", __FUNCTION__, addr,*pvalue);

    if (res<=0) DPRINTK("res = %d \n",res);

    return res;

}

#if 0
int ov9640_write_cmd(u8 addr, u8 *value)
{
	int	res=0;
	int i;
	if( g_client == NULL )
		return -1;
	printk("%s\n",__FUNCTION__);
	u8 *pvalue=value;
	char   buf[6]={0,0,0,0,0,3};
	for(i=0;i<5;i++)
		{
		buf[i]=&pvalue;
		pvalue++;
		
		}
	struct i2c_msg msgs[2] = {
		{ 0, 0, 1, &addr },
		{ 0, 0, 1, &value }};
	msgs[0].addr=msgs[1].addr=g_client->addr;
	res=i2c_transfer(g_client->adapter,&msgs[0],1);

⌨️ 快捷键说明

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