LINUX的IIC驱动从这开始(二)

LINUX下IIC驱动体系中的四个主要数据结构以及相互之间的关系

struct i2c_driver {
	unsigned int class;

	int (*attach_adapter)(struct i2c_adapter *) __deprecated;
	int (*detach_adapter)(struct i2c_adapter *) __deprecated;

	int (*probe)(struct i2c_client *, const struct i2c_device_id *);
	int (*remove)(struct i2c_client *);

	/* driver model interfaces that don't relate to enumeration  */
	void (*shutdown)(struct i2c_client *);
	int (*suspend)(struct i2c_client *, pm_message_t mesg);
	int (*resume)(struct i2c_client *);

	void (*alert)(struct i2c_client *, unsigned int data);

	/* a ioctl like command that can be used to perform specific functions
	 * with the device.
	 */
	int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

	struct device_driver driver;
	const struct i2c_device_id *id_table;

	/* Device detection callback for automatic device creation */
	int (*detect)(struct i2c_client *, struct i2c_board_info *);
	const unsigned short *address_list;
	struct list_head clients;
};
这里简单地把源码里的简介翻译翻译:

首先i2c_driver代表一个i2c设备驱动,@class:表示我们将注册的是那种设备(探测时用);@attach_adapter:添加总线时,告诉驱动的回调函数(以后可能要弃用);@detach_adapter:总线移除时,调用的回调函数(以后可能要弃用);@probe:绑定设备时的回调函数;@remove:解除绑定时调用的回调函数;@shutdown:设备关闭时调用的回调函数;@suspend:设备挂起时调用的回调函数;@resume:设备恢复时调用的回调函数;@alert:警惕回调函数(翻译可能不准确);@command:实现特殊功能的回调函数;@driver:设备驱动模型中的驱动;@id_table:这个IIC驱动支持的设备链表;@detect:检测设备的回调函数;@address_list:检测的IIC设备的地址;@clients:检测到的我们写的client(仅仅iic核心会用)

一个连接到i2c_bus总线上的设备用i2c_client表示,赋予linux的操作是由管理设备的驱动程序里面定义的

struct i2c_client {
	unsigned short flags;		/* div., see below		*/
	unsigned short addr;		/* chip address - NOTE: 7bit	*/
					/* addresses are stored in the	*/
					/* _LOWER_ 7 bits		*/
	char name[I2C_NAME_SIZE];
	struct i2c_adapter *adapter;	/* the adapter we sit on	*/
	struct i2c_driver *driver;	/* and our access routines	*/
	struct device dev;		/* the device structure		*/
	int irq;			/* irq issued by device		*/
	struct list_head detected;
};

下面是源码里的简介翻译:

首先,i2c_client代表一个IIC从设备;@flags:就是一个标示, I2C_CLIENT_TEN表示IIC从设备使用的芯片地址是10bit的,I2C_CLIENT_PEC表示设备使用SMBus错误检查;@addr:从设备在连接到相应适配器总线上使用的地址;@name:设备的名字;@adapter:挂接设备的适配器;@driver:访问设备的驱动;@irq:表明由设备产生的中断;@detected:一个i2c_driver支持的client的数量或i2c核心的用户空间设备的链表。

i2c_adapter就是一个用于标识物理总线(也就是IIC总线)连同访问它必要的算法的一个结构

struct i2c_adapter {
	struct module *owner;
	unsigned int class;		  /* classes to allow probing for */
	const struct i2c_algorithm *algo; /* the algorithm to access the bus */
	void *algo_data;

	/* data fields that are valid for all devices	*/
	struct rt_mutex bus_lock;

	int timeout;			/* in jiffies */
	int retries;
	struct device dev;		/* the adapter device */

	int nr;
	char name[48];
	struct completion dev_released;

	struct mutex userspace_clients_lock;
	struct list_head userspace_clients;
};

i2c_algorithm是为一类使用相同总线算法寻址的一个接口。当适配器不能使用i2c访问设备时,把master_xfer设置为NULL;如果一个适配器可以做SMBus访问时,设置smbus_xfer,如果把smbus_xfer设置成NULL,SMBus协议使用通用I2C模拟的消息。

struct i2c_algorithm {
	/* master_xfer should return the number of messages successfully
	   processed, or a negative value on error */
	int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
			   int num);
	int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
			   unsigned short flags, char read_write,
			   u8 command, int size, union i2c_smbus_data *data);

	/* To determine what the adapter supports */
	u32 (*functionality) (struct i2c_adapter *);
};

下面主要说说i2c_adapter和i2c_algorithm、i2c_driver和i2c_client的关系

i2c_adapter和i2c_algorithm的关系:

i2c_adapter对应与物理上的一个适配器,而i2c_algorithm对应一套通信方法,一个i2c适配器需要i2c_algorithm中提供的通信函数来控制适配器上产生特定的访问周期。缺少i2c_algorithm的i2c_adapter什么也做不了,因此i2c_adapter中包含其使用i2c_algorithm的指针。

i2c_driver和i2c_client的关系:

i2c_driver包含访问一些种类设备的通用代码,i2c_client表示一个独立的设备,驱动和设备是一对多的关系。每个探测到的设备通过在client数据结构中得到自己的数据

主要的数据结构介绍完,那咱们写一个iic设备驱动程序,看看到底是怎么按iic驱动模型写一个具体的驱动的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值