Chinaunix首页 | 论坛 | 博客
  • 博客访问: 327379
  • 博文数量: 95
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 157
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-26 20:03
文章分类

全部博文(95)

文章存档

2017年(15)

2015年(17)

2014年(63)

分类: LINUX

2017-10-12 11:19:46

一,添加I2C平台设备
linux
的设备模型将设备分成了driverdevice两大部分,driver即是设备的驱动,device即是硬件设备的具体描述,比如说:设备所拥有的中断,地址等,这里分析一下I2C驱动中device,也即i2c_client的注册,driver 也即是i2c_driver的注册,
i2c_client 的注册,是由

点击(此处)折叠或打开

  1. int __init
  2. i2c_register_board_info(int busnum,
  3.     struct i2c_board_info const *info, unsigned len)
函数 注册一个 i2c_board_info结构体来完成的。busnum 是总线编号,len设备个数

点击(此处)折叠或打开

  1. struct i2c_board_info {
  2.     char        type[I2C_NAME_SIZE];
  3.     unsigned short    flags;
  4.     unsigned short    addr;
  5.     void        *platform_data;
  6.     struct dev_archdata    *archdata;
  7.     struct device_node *of_node;
  8.     int        irq;
  9. };
其中最主要的两个参数是type即驱动设备名称,这个名字要与后面提到的i2c_device_id结构体中的名字一致,addr从设备地址。
到此I2C平台设备添加完成。
二,注册I2C驱动
1,初始化一个i2c_driver结构体,定义如下

点击(此处)折叠或打开

  1. struct i2c_driver {
  2.     unsigned int class;

  3.     /* Notifies the driver that a new bus has appeared or is about to be
  4.      * removed. You should avoid using this, it will be removed in a
  5.      * near future.
  6.      */
  7.     int (*attach_adapter)(struct i2c_adapter *) __deprecated;
  8.     int (*detach_adapter)(struct i2c_adapter *) __deprecated;

  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.     struct device_driver driver;
  27.     const struct i2c_device_id *id_table;

  28.     /* Device detection callback for automatic device creation */
  29.     int (*detect)(struct i2c_client *, struct i2c_board_info *);
  30.     const unsigned short *address_list;
  31.     struct list_head clients;
  32. };
.driver.name  驱动名字。不必和device的name相同。另外几个主要成员 是probe,remove,id_table,

struct i2c_device_id *id_table 定义实例如下
struct i2c_device_id static const struct i2c_device_id rx8010_id[] = {
    { "rx8010", 0 },// 此名称必须与 i2c_board_info的type一致,系统依据此名称匹配设备和驱动
    { }
};id_table

调用函数 i2c_register_driver(THIS_MODULE, driver) 完成驱动注册。
阅读(1447) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~