Linux I2C驱动

一、Linux 设备模型

在这里插入图片描述

二、Linux Kernel I2C驱动

1.简介

  • I2C总线是一个常用的串行通信,用于连接各种外设、传感器等器件。
  • 在裸机系统中,I2C驱动可分为host(主机)驱动和device(设备)驱动。
  • 在Linux系统中,可分为i2c总线驱动和i2c设备驱动, SOC 的 I2C 总线驱动都是由半导体厂商编写的,总线驱动一般不用去修改。在添加外设、传感器进行device驱动开发时,可以直接调用总线驱动提供的API函数完成read/write/ioctl等操作。符合Linux驱动的分离与分层的思想。
  • i2c驱动框架和子系统
    Linux系统定义了I2C驱动体系结构,在Linux内核中的I2C子系统中,I2C驱动共有3部分组成:I2C核心、I2C总线驱动、I2C设备驱动,这三部分组成了I2C的框架;I2C子系统中有4个重要内容:I2C总线、I2C设备、I2C驱动、I2C适配器;

2.i2c驱动框架

在这里插入图片描述

  1. i2c核心:提供i2c总线驱动和设备驱动的注册、注销方法。
  2. i2c总线驱动:是对i2c硬件体系结构中适配器端的实现,适配器可由CPU控制,甚至直接集成在CPU内部。包含i2c适配器数据结构i2c_adapter、algorithm数据结构i2c_algorithm和控制i2c适配器的产生的通信的函数。经过i2c总线驱动的代码,可以控制i2c适配器以主控的方式产生开始位,停止位、读写周期,以主从设备方式被读写、产生ACK等。
  3. i2c设备驱动:是对i2c硬件体现结构中设备端的实现,设备一般挂接在受CPU控制的i2c设配器上,通过i2c适配器与cpu交换数据。

3. I2C子系统

Linux内核中的I2C驱动代码位于:drivers/i2c目录;
I2C驱动中有4个重要内容:I2C总线、I2C设备、I2C驱动、I2C适配器;
I2C总线:维护I2C驱动和I2C设备两个链表,管理I2C驱动和I2C设备的匹配、删除等;
I2C设备:具体硬件设备的一个抽象;
I2C驱动:对应I2C设备的驱动程序;
I2C适配器:用于I2C驱动和I2C设备间通信,是SOC上I2C控制器的一个抽象;
I2C总线上有两个链表,分别是i2c_driver和i2c_client三链表;当任何一个driver或client注册时,I2C总线都会调用match函数,对 client.name 和driver.id_table.name进行遍历匹配;如果driver.id_table中所有的id都匹配不成功,说明client没有找到对应的driver;如果匹配成功,说明client和driver是配套的,那么I2C总线就会调用自己的probe函数,然后probe函数调用driver中提供的probe函数,driver中的probe函数会对设备进行硬件初始化和后续工作;

//1. i2c_add_adapter // 注册适配器
//2.i2c_add_driver // 注册驱动程序
//3.i2c_new_device // 注册客户端

 //drivers/i2c/i2c-core.c
//drivers/i2c/i2c-core-base.c
/**
* i2c_add_adapter - declare i2c adapter, use dynamic bus number
* @adapter: the adapter to add
* Context: can sleep
*
* This routine is used to declare an I2C adapter when its bus number
* doesn't matter or when its bus number is specified by an dt alias.
* Examples of bases when the bus number doesn't matter: I2C adapters
* dynamically added by USB links or PCI plugin cards.
*
* When this returns zero, a new bus number was allocated and stored
* in adap->nr, and the specified adapter became available for clients.
* Otherwise, a negative errno value is returned.
*/
int i2c_add_adapter(struct i2c_adapter *adapter)
EXPORT_SYMBOL(i2c_add_adapter);

/**
* i2c_del_adapter - unregister I2C adapter
* @adap: the adapter being unregistered
* Context: can sleep
*
* This unregisters an I2C adapter which was previously registered
* by @i2c_add_adapter or @i2c_add_numbered_adapter.
*/
void i2c_del_adapter(struct i2c_adapter *adap){...}
EXPORT_SYMBOL(i2c_del_adapter);

/*
* An i2c_driver is used with one or more i2c_client (device) nodes to access
* i2c slave chips, on a bus instance associated with some i2c_adapter.
*/
int i2c_register_driver(struct module *owner, struct i2c_driver *driver){....}

/**
* i2c_del_driver - unregister I2C driver
* @driver: the driver being unregistered
* Context: can sleep
*/
void i2c_del_driver(struct i2c_driver *driver)
EXPORT_SYMBOL(i2c_add_adapter);
/**
* i2c_new_device - instantiate an i2c device
* @adap: the adapter managing the device
* @info: describes one I2C device; bus_num is ignored
* Context: can sleep
*
* Create an i2c device. Binding is handled through driver model
* probe()/remove() methods.  A driver may be bound to this device when we
* return from this function, or any later moment (e.g. maybe hotplugging will
* load the driver module).  This call is not appropriate for use by mainboard
* initialization logic, which usually runs during an arch_initcall() long
* before any i2c_adapter could exist.
*
* This returns the new i2c client, which may be saved for later use with
* i2c_unregister_device(); or NULL to indicate an error.
*/
struct i2c_client* i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
EXPORT_SYMBOL_GPL(i2c_new_device);

I2C子系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ai_Sj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值