歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux設備模型之i2c子系統

Linux設備模型之i2c子系統

日期:2017/2/28 15:58:18   编辑:Linux教程
I2c子系統將i2c控制器(i2c寄存器所在的那塊電路)抽象出來,用adapter(適配器)這個結構來描述,可以說一個適配器就代表一條i2c總線,而掛接在i2c總線上的設備是用client這個結構體來表述,另外i2c_bus上的設備鏈表掛接的不單單是連接的這條i2c上的client,同樣adapter也作為一個設備掛在其所在的i2c_bus,也就是說控制器和設備都作為i2c_bus上的設備連接在設備鏈表,他們用內嵌的device的type這個成員來區分,適配器的類型為i2c_adapter_type,client的類型為i2c_client_type。

一、i2c相關的描述結構

首先看一下i2c子系統給adapter定義的描述結構:

[cpp]

  1. struct i2c_adapter {
  2. struct module *owner;
  3. unsigned int id;
  4. unsigned int class// 適配器支持的類型,如傳感器,eeprom等
  5. const struct i2c_algorithm *algo; //該適配器的通信函數
  6. void *algo_data;
  7. /* data fields that are valid for all devices */
  8. struct rt_mutex bus_lock;
  9. int timeout; //超時時間限定
  10. int retries; //通信重復次數限定
  11. /*
  12. * 內嵌的標准device,其中dev->type標識該設備
  13. * 是個adapter,其值為i2c_adapter_type
  14. */
  15. struct device dev;
  16. int nr; //適配器編號也是bus編號,第幾條i2c總線
  17. char name[48]; //名字
  18. struct completion dev_released;
  19. struct mutex userspace_clients_lock;
  20. struct list_head userspace_clients;
  21. };

再來看一下client的描述結構:

[cpp]

  1. struct i2c_client {
  2. unsigned short flags; //設備的標志,如喚醒標志等等
  3. /* chip address - NOTE: 7bit */
  4. /* addresses are stored in the */
  5. /* _LOWER_ 7 bits */
  6. unsigned short addr; //設備的地址
  7. char name[I2C_NAME_SIZE]; //設備的名字
  8. struct i2c_adapter *adapter; //設備所屬的適配器
  9. struct i2c_driver *driver; //設備的driver
  10. /*
  11. * 內嵌的標准device模型,其中dev->type標識該設備
  12. * 是個client,其值為i2c_client_type
  13. */
  14. struct device dev; /* the device structure */
  15. int irq; //中斷號
  16. struct list_head detected; //掛接點,掛接在adapter
  17. };


下面是driver的表述結構i2c_driver:

[cpp]

  1. struct i2c_driver {
  2. unsigned int class; //支持的類型,與adapter的class相對
  3. /* Notifies the driver that a new bus has appeared or is about to be
  4. * removed. You should avoid using this if you can, it will probably
  5. * be removed in a near future.
  6. */
  7. int (*attach_adapter)(struct i2c_adapter *); //舊式探測函數
  8. int (*detach_adapter)(struct i2c_adapter *);
  9. /* Standard driver model interfaces */
  10. int (*probe)(struct i2c_client *, const struct i2c_device_id *);
  11. int (*remove)(struct i2c_client *);
  12. /* driver model interfaces that don't relate to enumeration */
  13. void (*shutdown)(struct i2c_client *);
  14. int (*suspend)(struct i2c_client *, pm_message_t mesg);
  15. int (*resume)(struct i2c_client *);
  16. /* Alert callback, for example for the SMBus alert protocol.
  17. * The format and meaning of the data value depends on the protocol.
  18. * For the SMBus alert protocol, there is a single bit of data passed
  19. * as the alert response's low bit ("event flag").
  20. */
  21. void (*alert)(struct i2c_client *, unsigned int data);
  22. /* a ioctl like command that can be used to perform specific functions
  23. * with the device.
  24. */
  25. int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
  26. /*
  27. * 內嵌的標准driver,driver的of_match_table成員也用於標識其支持
  28. * 的設備,並且優先級高於id_table
  29. */
  30. struct device_driver driver;
  31. const struct i2c_device_id *id_table; //支持的client信息表
  32. /* Device detection callback for automatic device creation */
  33. int (*detect)(struct i2c_client *, struct i2c_board_info *); //探測函數
  34. const unsigned short *address_list; //driver支持的client地址
  35. struct list_head clients; //掛接其探測到的支持的設備
  36. ;

另外client端有一條全局鏈表,用於串聯所有i2c的client設備,為__i2c_board_list,也就是說client可以靜態注冊亦可動態
被探測,靜態注冊掛接在該鏈表上的結構為:

[cpp]

  1. struct i2c_devinfo {
  2. struct list_head list; //連接指針指向前後設備
  3. int busnum; //所在bus的編號
  4. struct i2c_board_info board_info; //板級平台信息相關的結構體
  5. };
  6. //其中 i2c_board_info結構的源碼為:
  7. struct i2c_board_info {
  8. char type[I2C_NAME_SIZE]; //名字
  9. unsigned short flags; //標志
  10. unsigned short addr; //地址
  11. void *platform_data; //私有特殊數據
  12. struct dev_archdata *archdata;
  13. #ifdef CONFIG_OF
  14. struct device_node *of_node; //節點
  15. #endi
  16. int irq; //中斷號
  17. };

i2c_devinfo結構靜態注冊的信息最後都會被整合集成到client中,形成一個標准的i2c_client設備並注冊。

Copyright © Linux教程網 All Rights Reserved