I2C子系统01 - 相关结构体

相关结构体

I2C驱动

struct i2c_driver

路径: include/linux/i2c.h

c 复制代码
struct i2c_driver {
	unsigned int class;		//I2C设备的类型
	/* Standard driver model interfaces 标准驱动模型接口 */
	int (*probe)(struct i2c_client *, const struct i2c_device_id *);
	int (*remove)(struct i2c_client *);
	
	/* New driver model interface to aid the seamless removal of the
	 * current probe()'s, more commonly unused than used second parameter.
	 */
	int (*probe_new)(struct i2c_client *);

	/* driver model interfaces that don't relate to enumeration  */
	void (*shutdown)(struct i2c_client *);

	/* Alert callback, for example for the SMBus alert protocol. */
	void (*alert)(struct i2c_client *, enum i2c_alert_protocol protocol,
		      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;   /* Device driver model driver */
	const struct i2c_device_id *id_table;  /* 这个驱动所支持的I2C设备列表 */

	/* Device detection callback for automatic device creation */
	int (*detect)(struct i2c_client *, struct i2c_board_info *);
	const unsigned short *address_list;  /* 在detect时的地址范围 */
	struct list_head clients;	/* 探测到的从设备列表 */

	bool disable_i2c_core_irq_mapping; /* Tell the i2c-core to not do irq-mapping  */
};
I2C客户端

struct i2c_client

路径: include/linux/i2c.h

c 复制代码
struct i2c_client {
	unsigned short flags;		/* div., see below		*/
	unsigned short addr;		/* chip address - NOTE: 7bit 连接到I2C控制器上的设备地址	*/
					/* addresses are stored in the	*/
					/* _LOWER_ 7 bits		*/
	char name[I2C_NAME_SIZE];		/* 设备的名字,通常指示着设备的类型 */ 
	struct i2c_adapter *adapter;	/* the adapter we sit on	*/
	struct device dev;		/* the device structure		*/
	int init_irq;			/* irq set at initialization  初始的中断号	*/
	int irq;			/* irq issued by device	映射之后的中断号	*/
	struct list_head detected;
#if IS_ENABLED(CONFIG_I2C_SLAVE)
	i2c_slave_cb_t slave_cb;	/* callback for slave mode	*/
#endif
};
I2C控制器

struct i2c_adapter

路径: include/linux/i2c.h

c 复制代码
struct i2c_adapter {
	struct module *owner;		 	/* THIS_MODULE指向当前模块 */
	unsigned int class;		  	/* classes to allow probing for 支持的设备类型 */
	const struct i2c_algorithm *algo; /* the algorithm to access the bus 控制器的传输方法*/
	void *algo_data;	/* algorithm私有数据 */

	/* data fields that are valid for all devices	*/
	const struct i2c_lock_operations *lock_ops;		 /* 特殊锁定行为支持 */
	struct rt_mutex bus_lock;	 /* 多设备同时访问总线锁 */
	struct rt_mutex mux_lock; 	/* 包含I2C总线多路复用器的同步访问 */  

	int timeout;			/* 超时时间 in jiffies */
	int retries;			/* 重试次数 */
	struct device dev;		/* the adapter device */

	int nr;			/* 控制器编号 /dev/i2c-x (x=nr) */
	char name[48];	/* 控制器名称 /sys/bus/i2c/devices/i2c-x/name  */
	struct completion dev_released;   /* 用于同步释放控制器资源的完成机制 */

	struct mutex userspace_clients_lock;   /* 保护并发操作 */
	struct list_head userspace_clients;	 /* 维护一个I2C客户端链表 */

	struct i2c_bus_recovery_info *bus_recovery_info;  /* 总线死锁时恢复手段 */
	const struct i2c_adapter_quirks *quirks;   /* 控制器某些特殊限制或特性 */

	struct irq_domain *host_notify_domain;  /* 中断路由 */
};
I2C传输方法

struct i2c_algorithm

路径: include/linux/i2c.h

c 复制代码
struct i2c_algorithm {
	/* If an adapter algorithm can't do I2C-level access, set master_xfer
	   to NULL. If an adapter algorithm can do SMBus access, set
	   smbus_xfer. If set to NULL, the SMBus protocol is simulated
	   using common I2C messages */
	/* 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);  /* SMBus 传输函数 */

	/* To determine what the adapter supports 功能标志函数 */
	u32 (*functionality) (struct i2c_adapter *);

#if IS_ENABLED(CONFIG_I2C_SLAVE)
	 /* Register given client to I2C slave mode of this adapter */
	int (*reg_slave)(struct i2c_client *client);
	/* Unregister given client from I2C slave mode of this adapter */
	int (*unreg_slave)(struct i2c_client *client);
#endif
};
总结

上述各项结构体与I2C总线之间的关系如下图所示:

  • i2c_adapter在物理层面代表一个I2C控制器,i2c_algorithm代表该控制器支持的传输方法;
  • i2c_client代表每一个I2C设备,i2c_driver为一套驱动方法,两者关系为多对1;
  • i2c_adapter和i2c_client与物理连接一致,即i2c_client表示连着在这个控制器下面的设备,而i2c_adapter代表为设备所连接的控制器;
相关推荐
wen__xvn2 小时前
高斯泼溅水下三维建模
笔记
d111111111d2 小时前
STM32内核锁死补救方法-STM32F411CEU6
笔记·stm32·单片机·嵌入式硬件·学习
想你依然心痛2 小时前
Spark大数据分析与实战笔记(第六章 Kafka分布式发布订阅消息系统-02)
笔记·分布式·spark
孙严Pay2 小时前
代付业务解析
笔记·科技·计算机网络·其他·微信
星轨初途3 小时前
牛客小白月赛126
开发语言·c++·经验分享·笔记·算法
じ☆冷颜〃3 小时前
基于多数据结构融合的密码学性能增强框架
数据结构·经验分享·笔记·python·密码学
崎岖Qiu3 小时前
【设计模式笔记24】:JDK源码分析-Comparator中的「策略模式」
java·笔记·设计模式·jdk·策略模式
强子感冒了3 小时前
Java List学习笔记:ArrayList与LinkedList的实现源码分析
java·笔记·学习
YJlio3 小时前
PsPing 学习笔记(14.2):TCP Ping——端口连通性与服务在线性秒级体检
笔记·学习·tcp/ip