• Linux之I2C驱动


    目录

    一、I2C 驱动框架简介

    1.I2C 总线驱动

    2.I2C 设备驱动

    3.I2C 核心

    二、I2C 设备驱动流程

    1.设备信息描述

    ①不使用设备树

    ②使用设备树

    2. I2C 设备数据收发和处理


    一、I2C 驱动框架简介

    在 Linux 内核中 I2C 的体系结构分为 3 个部分:

    I2C 核心: I2C 核心提供了 I2C 总线驱动和设备驱动的注册、 注销方法


    I2C 总线驱动: I2C 总线驱动是对 I2C 硬件体系结构中适配器端的实现, 适配器可由CPU 控制, 甚至可以直接集成在 CPU 内部。I2C 总线驱动就是 SOC 的 I2C 控制器驱动,也叫做 I2C 适配器驱动
     

    I2C 设备驱动: I2C 设备驱动是对 I2C 硬件体系结构中设备端的实现设备一般挂接在受 CPU 控制的 I2C 适配器上, 通过 I2C 适配器与 CPU 交换数据

    1.I2C 总线驱动

    I2C 总线和 platform 总线类似, 区别在于platform 总线是虚拟的一条总线, 而 I2C 总线是实际
    存在的。 对于使用 I2C 通信的设备, 在驱动中直接使用 I2C 总结即可。 I2C 总线驱动的重点是 I2C 适配器驱动, 主要涉及到两个结构体: i2c_adapter 和 i2c_algorithm。 在 Linux 内核中用 i2c_adapter 结构体来表示 I2C 适配器。 i2c_adapter 结构体定义在 include/linux/i2c.h 文件中

    1. struct i2c_adapter {
    2. struct module *owner;
    3. unsigned int class; /* classes to allow probing for */
    4. const struct i2c_algorithm *algo; /* 总线访问算法 */
    5. void *algo_data;
    6. /* data fields that are valid for all devices */
    7. struct rt_mutex bus_lock;
    8. int timeout; /* in jiffies */
    9. int retries;
    10. struct device dev; /* the adapter device */
    11. int nr;
    12. char name[48];
    13. struct completion dev_released;
    14. struct mutex userspace_clients_lock;
    15. struct list_head userspace_clients;
    16. struct i2c_bus_recovery_info *bus_recovery_info;
    17. const struct i2c_adapter_quirks *quirks;
    18. };

    i2c_algorithm 类型的指针变量 algo, 对于一个 I2C 适配器, 要对外提供读写 API 函数, 设备驱动程序可以使用这些 API 函数来完成读写操作。 i2c_algorithm 是 I2C 适配器与 IIC 设备进行通信的方法

    i2c_algorithm 结构体定义在 include/linux/i2c.h 文件中

    1. struct i2c_algorithm
    2. {
    3. ......
    4. int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,int num);
    5. int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
    6. unsigned short flags, char read_write,
    7. u8 command, int size, union i2c_smbus_data *data);
    8. /* To determine what the adapter supports */
    9. u32 (*functionality) (struct i2c_adapter *);
    10. ......
    11. };

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

    I2C 总线驱动(I2C 适配器驱动)的主要工作就是初始化 i2c_adapter 结构体变量,然后设置 i2c_algorithm 中的 master_xfer 函数。最后通过 i2c_add_numbered_adapteri2c_add_adapter 这两个函数向系统注册i2c_adapter

    1. int i2c_add_adapter(struct i2c_adapter *adapter)
    2. int i2c_add_numbered_adapter(struct i2c_adapter *adap)

    adapter 或 adap:要添加到 Linux 内核中的 i2c_adapter,即I2C 适配器
    返回值: 0,成功;负值,失败

    这两个函数的区别在于 i2c_add_adapter 使用动态的总线号,而 i2c_add_numbered_adapter
    使用静态总线号

    如果要删除 I2C 适配器使用 i2c_del_adapter 函数即可

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

    一般 SOC 的 I2C 总线驱动都是由半导体厂商编写的,所以大多数只要专注于 I2C 设备驱动即可。

    2.I2C 设备驱动

    在 I2C 设备驱动中主要有两个重要的结构体: i2c_client 和 i2c_driveri2c_client 是描述设备信息的,i2c_driver 描述驱动内容。 i2c_client 结构体定义在include/linux/i2c.h 文件中

    1. struct i2c_client {
    2. unsigned short flags; /* 标志 */
    3. unsigned short addr; /* 芯片地址, 7 位,存在低 7 位*/
    4. ......
    5. char name[I2C_NAME_SIZE]; /* 名字 */
    6. struct i2c_adapter *adapter; /* 对应的 I2C 适配器 */
    7. struct device dev; /* 设备结构体 */
    8. int irq; /* 中断 */
    9. struct list_head detected;
    10. ......
    11. };

    当驱动和设备匹配成功后,每检测到一个 I2C 设备就会给这个 I2C 设备分配一个i2c_client这个ic_client存储着这个设备的所有信息,如芯片地址

    i2c_driver 类似 platform_driver,是编写 I2C 设备驱动重点要处理的内容, i2c_driver 结构体定义在 include/linux/i2c.h 文件中

    1. struct i2c_driver {
    2. unsigned int class;
    3. /* Notifies the driver that a new bus has appeared. You should
    4. * avoid using this, it will be removed in a near future.
    5. */
    6. int (*attach_adapter)(struct i2c_adapter *) __deprecated;
    7. /* Standard driver model interfaces */
    8. int (*probe)(struct i2c_client *, const struct i2c_device_id *);
    9. int (*remove)(struct i2c_client *);
    10. /* driver model interfaces that don't relate to enumeration */
    11. void (*shutdown)(struct i2c_client *);
    12. /* Alert callback, for example for the SMBus alert protocol.
    13. * The format and meaning of the data value depends on the
    14. * protocol.For the SMBus alert protocol, there is a single bit
    15. * of data passed as the alert response's low bit ("eventflag"). */
    16. void (*alert)(struct i2c_client *, unsigned int data);
    17. /* a ioctl like command that can be used to perform specific
    18. * functions with the device.
    19. */
    20. int (*command)(struct i2c_client *client, unsigned int cmd,void *arg);
    21. struct device_driver driver;
    22. const struct i2c_device_id *id_table;
    23. /* Device detection callback for automatic device creation */
    24. int (*detect)(struct i2c_client *, struct i2c_board_info *);
    25. const unsigned short *address_list;
    26. struct list_head clients;
    27. };

    当 I2C 设备和驱动匹配成功以后 probe 函数就会执行。device_driver 驱动结构体如果使用设备树,需要设置 device_driver 的of_match_table 成员变量,即驱动的兼容(compatible)性。 未使用设备树的设备需要设置id_table 设备匹配 ID 表

    在编写 I2C 设备的驱动时, 主要是创建 i2c_driver 结构体, 并实现里面的内容。 i2c_driver 结构体创建完成后, 使用i2c_register_driver 函数向 Linux 内核中注册 i2c 设备

    int i2c_register_driver(struct module *owner,struct i2c_driver *driver)
    owner: 一般为 THIS_MODULE

    driver:要注册的 i2c_driver
    返回值: 0,成功;负值,失败

    i2c_add_driver 也常常用于注册 i2c_driver, i2c_add_driver 是一个宏定义,

    1. #define i2c_add_driver(driver) \
    2. i2c_register_driver(THIS_MODULE, driver)

    i2c_add_driver是对i2c_register_driver做了一个简单的封装,只有一个参数,要注册的i2c_driver。

    注销 I2C 设备驱动的时需要用到i2c_del_driver 函数将前面注册的 i2c_driver 从 Linux 内核中注销掉

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

    i2c_driver注册模板:

    1. /* i2c 驱动的 probe 函数 */
    2. static int xxx_probe(struct i2c_client *client,const struct i2c_device_id *id)
    3. {
    4. /* 函数具体程序,一般字符设备驱动框架*/
    5. return 0;
    6. }
    7. /* i2c 驱动的 remove 函数 */
    8. static int xxx_remove(struct i2c_client *client)
    9. {
    10. /* 函数具体程序 */
    11. return 0;
    12. }
    13. /* 传统匹配方式 ID 列表 */
    14. static const struct i2c_device_id xxx_id[] = {
    15. {"xxx", 0},
    16. {}
    17. };
    18. /* 设备树匹配列表 */
    19. static const struct of_device_id xxx_of_match[] = {
    20. { .compatible = "xxx" },
    21. { /* Sentinel */ }
    22. };
    23. /* i2c 驱动结构体 */
    24. static struct i2c_driver xxx_driver = {
    25. .probe = xxx_probe,
    26. .remove = xxx_remove,
    27. .driver = {
    28. .owner = THIS_MODULE,
    29. .name = "xxx",
    30. .of_match_table = xxx_of_match, //使用设备树
    31. },
    32. .id_table = xxx_id, //未使用设备树
    33. };
    34. /* 驱动入口函数 */
    35. static int __init xxx_init(void)
    36. {
    37. int ret = 0;
    38. ret = i2c_add_driver(&xxx_driver);
    39. return ret;
    40. }
    41. /* 驱动出口函数 */
    42. static void __exit xxx_exit(void)
    43. {
    44. i2c_del_driver(&xxx_driver);
    45. }
    46. module_init(xxx_init);
    47. module_exit(xxx_exit);

    3.I2C 核心

    在 I2C 核心层完成的是 I2C 设备和I2C 驱 动的匹配过程。I2C 核心部分的文件是drivers/i2c/i2c-core.c。

    I2C 核心层提供了一些与硬件无关的 API 函数。

    ①i2c_adapter 注册/注销函数

    int i2c_add_adapter(struct i2c_adapter *adapter)

    int i2c_add_numbered_adapter(struct i2c_adapter *adap)

    void i2c_del_adapter(struct i2c_adapter * adap)

    ②i2c_driver 注册/注销函数

    int i2c_register_driver(struct module *owner, struct i2c_driver *driver)

    int i2c_add_driver (struct i2c_driver *driver)

    void i2c_del_driver(struct i2c_driver *driver)

    设备和驱动的匹配过程是在 I2C 总线完成的, I2C 总线的数据结构为 i2c_bus_type, 定义在
    drivers/i2c/i2c-core.c 文件

    1. struct bus_type i2c_bus_type = {
    2. .name = "i2c",
    3. .match = i2c_device_match,
    4. .probe = i2c_device_probe,
    5. .remove = i2c_device_remove,
    6. .shutdown = i2c_device_shutdown,
    7. };

    match 成员变量是 I2C 总线上设备和驱动匹配函数,即i2c_device_match函数。

    1. static int i2c_device_match(struct device *dev, struct device_driver *drv)
    2. {
    3. struct i2c_client *client = i2c_verify_client(dev);
    4. struct i2c_driver *driver;
    5. if (!client)
    6. return 0;
    7. /* Attempt an OF style match */
    8. if (of_driver_match_device(dev, drv))
    9. return 1;
    10. /* Then ACPI style match */
    11. if (acpi_driver_match_device(dev, drv))
    12. return 1;
    13. driver = to_i2c_driver(drv);
    14. /* match on an id table if there is one */
    15. if (driver->id_table)
    16. return i2c_match_id(driver->id_table, client) != NULL;
    17. return 0;
    18. }

    of_driver_match_device 函数用于完成设备树设备和驱动匹配。比较 I2C 设备节点的 compatible 属性和 of_device_id 中的 compatible 属性是否相等,如果相当的话就表示 I2C设备和驱动匹配。

    acpi_driver_match_device 函数用于 ACPI 形式的匹配。

    i2c_match_id 函数用于无设备树的 I2C 设备和驱动匹配过程。比较 I2C设备名字和 i2c_device_id 的 name 字段是否相等,相等的话就说明 I2C 设备和驱动匹配。


    二、I2C 设备驱动流程

    I2C 总线驱动一般是不用用户自己编写的, 但是I2C 设备驱动需要用户编写。 每个设备的驱动都
    不相同, 但是编写流程是一样的。

    1.设备信息描述

    ①不使用设备树

    当开始编写 I2C 设备驱动时,首先要添加设备信息在不使用设备树时, 使用平台文件时添加I2C 设备信息

    在平台文件中通过 i2c_board_info 结构体来描述一个具体的 I2C 设备

    1. struct i2c_board_info {
    2. char type[I2C_NAME_SIZE]; /* I2C 设备名字 */
    3. unsigned short flags; /* 标志 */
    4. unsigned short addr; /* I2C 器件地址 */
    5. void *platform_data;
    6. struct dev_archdata *archdata;
    7. struct device_node *of_node;
    8. struct fwnode_handle *fwnode;
    9. int irq;
    10. };

    其中 type 和 addr 这两个成员变量是必须要设置的, 一个是 I2C 设备的名字, 一个是 I2C 设备的器件地址

    打开 arch/arm/mach-imx/mach-mx27_3ds.c 文件, 此文件中关于 OV2640 的 I2C 设备信息描述如下:

    1. static struct i2c_board_info mx27_3ds_i2c_camera = {
    2. I2C_BOARD_INFO("ov2640", 0x30),
    3. };

    I2C_BOARD_INFO 来完成 mx27_3ds_i2c_camera 的初始化工作, I2C_BOARD_INFO 是一个宏

    1. #define I2C_BOARD_INFO(dev_type, dev_addr) \
    2. .type = dev_type, .addr = (dev_addr)

    I2C_BOARD_INFO 宏是设置 i2c_board_info 的 type 和 addr 这两个成员变量

    ②使用设备树

    在使用设备树文件时, 只需要在设备树文件中添加相应的 I2C 设备节点就可以了。NXP 官方的在 I2C1 上接了 mag3110 这个磁力计芯片, 因此必须在 i2c1 节点下创建mag3110 子节点, 然后在这个子节点内描述 mag3110 这个芯片的相关信息。

    1. &i2c1 {
    2. clock-frequency = <100000>;
    3. pinctrl-names = "default";
    4. pinctrl-0 = <&pinctrl_i2c1>;
    5. status = "okay";
    6. mag3110@0e {
    7. compatible = "fsl,mag3110";
    8. reg = <0x0e>;
    9. position = <2>;
    10. };
    11. ......
    12. };

    向 i2c1 添加 mag3110 子节点,mag3110@0e是子节点名字, @后面的0e是I2C 器件地址。compatible 属性值为fsl,mag3110。reg属性也是设置I2C的器件地址的I2C 设备节点的创建主要是是 compatible 属性和 reg属性的设置, 一个用于匹配驱动, 一个用于设置器件地址

    2. I2C 设备数据收发和处理

    在 I2C 设备驱动中首先要完成 i2c_driver 结构体的创建、 初始化和注册, 当设备和驱动匹配成功后,就会执行 probe 函数, probe 函数中就是执行字符设备驱动的一套流程。

    一般需要在probe函数里面初始化 I2C 设备,要初始化 I2C 设备需要使用 i2c_transfer 函数对 I2C 设备寄存器进行读写操作。 i2c_transfer 函数会调用 I2C 适配器中 i2c_algorithm里面的 master_xfer 函数, 对于 I.MX6U 而言是 i2c_imx_xfer 这个函数。

    int i2c_transfer(struct i2c_adapter *adap,struct i2c_msg *msgs,int num)

    adap: 所使用的 I2C 适配器, i2c_client 会保存其对应的 i2c_adapter
    msgs: I2C 要发送的一个或多个消息
    num: 消息数量,msgs 的数量
    返回值: 负值,失败,其他非负值,发送的 msgs 数量

    msgs参数是一个 i2c_msg 类型的指针参数,Linux 内 核 使 用 i2c_msg 结 构 体 来 描 述 一 个 消 息。

    1. struct i2c_msg {
    2. __u16 addr; /* 从机地址 */
    3. __u16 flags; /* 标志 */
    4. #define I2C_M_TEN 0x0010
    5. #define I2C_M_RD 0x0001
    6. #define I2C_M_STOP 0x8000
    7. #define I2C_M_NOSTART 0x4000
    8. #define I2C_M_REV_DIR_ADDR 0x2000
    9. #define I2C_M_IGNORE_NAK 0x1000
    10. #define I2C_M_NO_RD_ACK 0x0800
    11. #define I2C_M_RECV_LEN 0x0400
    12. __u16 len; /* 消息(本 msg)长度 */
    13. __u8 *buf; /* 消息数据 */
    14. };

    使用i2c_transfer函数发送数据前,要构建好 i2c_msg,使用 i2c_transfer 进行 I2C 数据收发的模板:

    1. /* 设备结构体 */
    2. struct xxx_dev {
    3. ......
    4. void *private_data; /* 私有数据,一般会设置为 i2c_client */
    5. };
    6. /*
    7. * @description : 读取 I2C 设备多个寄存器数据
    8. * @param – dev : I2C 设备
    9. * @param – reg : 要读取的寄存器首地址
    10. * @param – val : 读取到的数据
    11. * @param – len : 要读取的数据长度
    12. * @return : 操作结果
    13. */
    14. static int xxx_read_regs(struct xxx_dev *dev, u8 reg, void *val,
    15. int len)
    16. {
    17. int ret;
    18. struct i2c_msg msg[2];
    19. struct i2c_client *client = (struct i2c_client *)
    20. dev->private_data;
    21. /* msg[0],第一条写消息,发送要读取的寄存器首地址 */
    22. msg[0].addr = client->addr; /* I2C 器件地址 */
    23. msg[0].flags = 0; /* 标记为发送数据 */
    24. msg[0].buf = ® /* 读取的首地址 */
    25. msg[0].len = 1; /* reg 长度 */
    26. /* msg[1],第二条读消息,读取寄存器数据 */
    27. msg[1].addr = client->addr; /* I2C 器件地址 */
    28. msg[1].flags = I2C_M_RD; /* 标记为读取数据 */
    29. msg[1].buf = val; /* 读取数据缓冲区 */
    30. msg[1].len = len; /* 要读取的数据长度 */
    31. ret = i2c_transfer(client->adapter, msg, 2);
    32. if(ret == 2)
    33. {
    34. ret = 0;
    35. }
    36. else
    37. {
    38. ret = -EREMOTEIO;
    39. }
    40. return ret;
    41. }
    42. /*
    43. * @description : 向 I2C 设备多个寄存器写入数据
    44. * @param – dev : 要写入的设备结构体
    45. * @param – reg : 要写入的寄存器首地址
    46. * @param – buf : 要写入的数据缓冲区
    47. * @param – len : 要写入的数据长度
    48. * @return : 操作结果
    49. */
    50. static s32 xxx_write_regs(struct xxx_dev *dev, u8 reg, u8 *buf,u8 len)
    51. {
    52. u8 b[256];
    53. struct i2c_msg msg;
    54. struct i2c_client *client = (struct i2c_client *)
    55. dev->private_data;
    56. b[0] = reg; /* 寄存器首地址 */
    57. memcpy(&b[1],buf,len); /* 将要发送的数据拷贝到数组 b 里面 */
    58. msg.addr = client->addr; /* I2C 器件地址 */
    59. msg.flags = 0; /* 标记为写数据 */
    60. msg.buf = b; /* 要发送的数据缓冲区 */
    61. msg.len = len + 1; /* 要发送的数据长度 */
    62. return i2c_transfer(client->adapter, &msg, 1);
    63. }

    在设备结构体里面添加一个执行void的指针成员变量private_data,此成员变量用于保存设备的私有数据,在 I2C 设备驱动中一般将其指向 I2C 设备对应的i2c_client

     

    xxx_read_regs 函数用于读取 I2C 设备多个寄存器数据,然后定义了一个i2c_msg 数组, 2 个数组元素。如图,I2C写时序图,因为 I2C 读取数据的时候要先发送要读取的寄存器地址,然后再读取数据,所以需要准备两个 i2c_msg。一个用于发送寄存器地址,一个用于读取寄存器值。

    对于 msg[0], 将 flags设置为 0 表示写数据。 msg[0]的 addr 是 I2C 设备的器件地址, msg[0]的 buf 成员变量就是要读取的寄存器地址。 对于 msg[1], 将 flags 设置为 I2C_M_RD, 表示读取数据。 msg[1]的 buf 成员变量用于保存读取到的数据, len 成员变量就是要读取的数据长度。 调用 i2c_transfer 函数完成 I2C 数据读操作。

    xxx_write_regs 函数用于向 I2C 设备多个寄存器写数据。 数组 b 用于存放寄存器首地址和要发送的数据,msg 的 addr设置为 I2C 器件地址。然后设置 msg 的 flags 为 0, 也就是写数据。设置要发送的数据, 也就是数组 b。设置 msg 的 len 为 len+1, 因为要加上一个字节的寄存器地址。 最后通过 i2c_transfer 函数完成向 I2C 设备的写操作。

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

    int i2c_master_send(const struct i2c_client *client,const char *buf,int count)

    client: I2C 设备对应的 i2c_client
    buf:要发送的数据
    count: 要发送的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无
    符号 16 位)类型的数据。
    返回值: 负值,失败,其他非负值,发送的字节数

     I2C 数据接收函数为 i2c_master_recv

    int i2c_master_recv(const struct i2c_client *client,char *buf,int count)
    client: I2C 设备对应的 i2c_client
    buf:要接收的数据

    count: 要接收的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无
    符号 16 位)类型的数据
    返回值: 负值,失败,其他非负值,发送的字节数


     

  • 相关阅读:
    电脑怎么截图,4种简单常用的截图方法
    【JAVA】List
    操作系统——进程和线程相关知识
    开源思维导图白板工具
    Windows电脑如何手动设置IP地址和DNS?
    温湿度传感器为什么会有这么多型号,应该如何挑选?
    三、流程控制及循环《2022 solidity8.+ 版本教程到实战》
    香港服务器在大陆连不上怎么回事?
    文章摘要智能提取【基于BERT技术】
    记录在windows下安装MySQL所遇到的各种坑
  • 原文地址:https://blog.csdn.net/qq_53144843/article/details/127089880