打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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

  1. <span style="font-size:14px;">struct i2c_driver {  
  2.     unsigned int class;  
  3.   
  4.     int (*attach_adapter)(struct i2c_adapter *) __deprecated;  
  5.     int (*detach_adapter)(struct i2c_adapter *) __deprecated;  
  6.   
  7.     int (*probe)(struct i2c_client *, const struct i2c_device_id *);  
  8.     int (*remove)(struct i2c_client *);  
  9.   
  10.     /* driver model interfaces that don't relate to enumeration  */  
  11.     void (*shutdown)(struct i2c_client *);  
  12.     int (*suspend)(struct i2c_client *, pm_message_t mesg);  
  13.     int (*resume)(struct i2c_client *);  
  14.   
  15.     void (*alert)(struct i2c_client *, unsigned int data);  
  16.   
  17.     /* a ioctl like command that can be used to perform specific functions 
  18.      * with the device. 
  19.      */  
  20.     int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);  
  21.   
  22.     struct device_driver driver;  
  23.     const struct i2c_device_id *id_table;  
  24.   
  25.     /* Device detection callback for automatic device creation */  
  26.     int (*detect)(struct i2c_client *, struct i2c_board_info *);  
  27.     const unsigned short *address_list;  
  28.     struct list_head clients;  
  29. };</span>  
这里简单地把源码里的简介翻译翻译:

首先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的操作是由管理设备的驱动程序里面定义的

  1. <span style="font-size:14px;">struct i2c_client {  
  2.     unsigned short flags;       /* div., see below      */  
  3.     unsigned short addr;        /* chip address - NOTE: 7bit    */  
  4.                     /* addresses are stored in the  */  
  5.                     /* _LOWER_ 7 bits       */  
  6.     char name[I2C_NAME_SIZE];  
  7.     struct i2c_adapter *adapter;    /* the adapter we sit on    */  
  8.     struct i2c_driver *driver;  /* and our access routines  */  
  9.     struct device dev;      /* the device structure     */  
  10.     int irq;            /* irq issued by device     */  
  11.     struct list_head detected;  
  12. };</span>  

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

首先,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总线)连同访问它必要的算法的一个结构

  1. <span style="font-size:14px;">struct i2c_adapter {  
  2.     struct module *owner;  
  3.     unsigned int class;       /* classes to allow probing for */  
  4.     const struct i2c_algorithm *algo; /* the algorithm to access the bus */  
  5.     void *algo_data;  
  6.   
  7.     /* data fields that are valid for all devices   */  
  8.     struct rt_mutex bus_lock;  
  9.   
  10.     int timeout;            /* in jiffies */  
  11.     int retries;  
  12.     struct device dev;      /* the adapter device */  
  13.   
  14.     int nr;  
  15.     char name[48];  
  16.     struct completion dev_released;  
  17.   
  18.     struct mutex userspace_clients_lock;  
  19.     struct list_head userspace_clients;  
  20. };</span>  

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

  1. <span style="font-size:14px;">struct i2c_algorithm {  
  2.     /* master_xfer should return the number of messages successfully 
  3.        processed, or a negative value on error */  
  4.     int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,  
  5.                int num);  
  6.     int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,  
  7.                unsigned short flags, char read_write,  
  8.                u8 command, int size, union i2c_smbus_data *data);  
  9.   
  10.     /* To determine what the adapter supports */  
  11.     u32 (*functionality) (struct i2c_adapter *);  
  12. };</span>  

下面主要说说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驱动模型写一个具体的驱动的

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
I2C 的 4 个结构体
I2C
linux内核I2C体系结构(注意结构体原型)
i2c设备驱动实例分析
Linux I2C驱动完全分析(一)
I2C驱动架构
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服