一、linux驱动开发-13.1-I2C

 一、I2C驱动框架简介

 

       linux内核将I2C驱动分为两部分:

       ①、I2C总线驱动,针对SOC的I2C控制器

       ②、I2C设备驱动,针对具体I2C从设备

1.1、I2C总线驱动

       I2C总线驱动的重点就是SOC的I2C控制器驱动,有两个重要的数据结构:i2c_adapter 和 i2c_algorithm,Linux 内核将 SOC 的 I2C 适配器(控制器)抽象成 i2c_adapter。

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;

	struct i2c_bus_recovery_info *bus_recovery_info;
	const struct i2c_adapter_quirks *quirks;
};

        i2c_algorithm提供了适配器与I2C从设备通信的方法:

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);

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

#if IS_ENABLED(CONFIG_I2C_SLAVE)
	int (*reg_slave)(struct i2c_client *client);
	int (*unreg_slave)(struct i2c_client *client);
#endif
};

       master_xfer 就是 I2C 适配器的传输函数,可以通过此函数来完成与 IIC 设备之
间的通信。

       因此,I2C总线驱动,主要工作就是初始化i2c_adapter结构体变量,然后设置 i2c_algorithm 中的 master_xfer 函数。完成以后通过 i2c_add_numbered_adapter或 i2c_add_adapter 这两个函数向系统注册设置好的 i2c_adapter;如果要删除,使用 i2c_del_adapter

/*
@adapter:要添加的I2C适配器

@return:0,成功
        <0,失败
*/
int i2c_add_adapter(struct i2c_adapter *adapter)
int i2c_add_numbered_adapter(struct i2c_adapter *adap)

/*
@adap:要删除的I2C适配器
*/
void i2c_del_adapter(struct i2c_adapter * adap)

1.2、I2C设备驱动

       I2C 设备驱动重点关注两个数据结构:i2c_client 和 i2c_driver,i2c_client 就是描述设备信息的,i2c_driver 描述驱动内容,类似于 platform_driver。

1.2.1、i2c_client

       一个设备对应一个 i2c_client,每检测到一个 I2C 设备就会给这个 I2C 设备分配一个
i2c_client。

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 device dev;		/* the device structure		*/
	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
};

1.2.2、i2c_driver

       i2c_driver 类似 platform_driver,是我们编写 I2C 设备驱动重点要处理的内容

struct i2c_driver {
	unsigned int class;

	/* Notifies the driver that a new bus has appeared. You should avoid
	 * using this, it will be removed in a near future.
	 */
	int (*attach_adapter)(struct i2c_adapter *) __deprecated;

	/* Standard driver model interfaces */
	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 *);

	/* Alert callback, for example for the SMBus alert protocol.
	 * The format and meaning of the data value depends on the protocol.
	 * For the SMBus alert protocol, there is a single bit of data passed
	 * as the alert response's low bit ("event flag").
	 */
	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;
};

①、probe:当 I2C 设备和驱动匹配成功以后 probe 函数就会执行

②、driver:如果使用设备树的话,需要设置 device_driver 的of_match_table 成员变量,也就是驱动的兼容(compatible)属性

③、id_table 是传统的、未使用设备树的设备匹配 ID 表

       重点工作就是构建 i2c_driver,构建完成以后需要向Linux 内核注册这个 i2c_driver,i2c_driver 注册函数为 int i2c_register_driver,或 i2c_add_driver 

/*
@owner:一般为THIS_MODULE
@driver:要注册的i2c_driver

@return:0,成功
        <0,失败
*/
int i2c_register_driver(struct module *owner,
                        struct i2c_driver *driver)


#define i2c_add_driver(driver)     i2c_register_driver(THIS_MODULE, driver)

       注销的时候:

/*
@driver:要注销的i2c_driver
*/
void i2c_del_driver(struct i2c_driver *driver)

二、驱动编写流程

2.1、设备信息描述

2.1.1、未使用设备树

2.1.2、使用设备树

2.2、设备数据收发

/*
@adap:所使用的I2C适配器,i2c_client会保存对应的i2c_adapter
@msgs:I2C要发送的一个或多个消息
@num:消息数量,也就是msgs的数量

@return:< 0,失败
         >=0,发送的msgs数量
*/
int i2c_transfer(struct i2c_adapter *adap,
                 struct i2c_msg *msgs,
                 int num)

       另外还有两个API分别用于I2C数据的收发,这两个函数最终都会调用i2c_transfer。

       数据发送函数:

/*
@lient :I2C 设备对应的 i2c_client。
@buf:要发送的数据。
@count :要发送的数据字节数

@return :< 0,失败
         >= 0,发送的字节数
*/
int i2c_master_send(const struct i2c_client *client,
                    const char *buf,
                    int count)

       数据接收函数:

/*
@client :I2C 设备对应的 i2c_client。
@buf:要接收的数据。
@count :要接收的数据字节数

@return : < 0,失败
          >= 0,发送的字节数
*/
int i2c_master_recv(const struct i2c_client *client,
                    char *buf,
                    int count)

问题:

我只有设备树匹配时,加载驱动后,probe未匹配,加上传统匹配方式后就能匹配,?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值