linux iic驱动编程之二 向总线注册设备(1)

来源:互联网 发布:mysql client android 编辑:程序博客网 时间:2024/05/29 19:00

linux iic驱动编程之二 向总想注册设备

前言:本人是接触linux驱动开发的初学者,如有什么错误,希望您能够指出来,万分感谢,希望大家一起学习,一起进步!!

linux iic 驱动编程向总线注册设备之方法一 静态注册 ,此方法不能动态的加载。

Method 1: Declare the I2C devices by bus number
-----------------------------------------------

This method is appropriate when the I2C bus is a system bus as is the case
for many embedded systems. On such systems, each I2C bus has a number
which is known in advance. It is thus possible to pre-declare the I2C
devices which live on this bus. This is done with an array of struct
i2c_board_info which is registered by calling i2c_register_board_info().

方法1:申明(注册)一个iic设备通过总线号。
这种方法应用于许多的嵌入式系统,使用的条件是iic总线也是更系统总线一样。在这种系统的情况下,我们能够提前知道每一iic总想在系统的总线的总想号。能够在总线生效提前声明一个iic设备(iic设备注册iic总线到内核)。完成以下需要一个结构体数组 struct i2c_board_info 被i2c_register_board_info().函数调用用于注册到内核相应的总线上。

其中具体的结构体和函数如下 struct i2c_board_info 和函数 i2c_register_board_info().

struct i2c_board_info {
char  type[I2C_NAME_SIZE]; //名称
unsigned short flags;
unsigned shortaddr;          //地址
void *platform_data; //设备的私有数据
struct dev_archdata*archdata;
struct device_node *of_node;
int irq;
};

int __init i2c_register_board_info(int busnum,struct i2c_board_info const *info, unsigned len)
busnum : iic外部设备注册那一条iic总线上。
i2c_board_info :结构体的地址
len :结构体数组的大小 常用 ARRAY_SIZE 来求取具体的大小。

I2C_BOARD_INFO(dev_type, dev_addr) 
dev_type : iic设备的名称 用于和该设备驱动的匹配
dev_addr :从机的地址

iic设备注册到总线的例子,注意的是,此方法不能动态的加载。

static struct i2c_board_info __initdata h4_i2c_board_info[] = {
{
I2C_BOARD_INFO("isp1301_omap", 0x2d),
.irq = OMAP_GPIO_IRQ(125),
},
{ /* EEPROM on mainboard */
I2C_BOARD_INFO("24c01", 0x52),
.platform_data= &m24c01,
},
{ /* EEPROM on cpu card */
I2C_BOARD_INFO("24c01", 0x57),
.platform_data= &m24c01,
},
};


static void __init omap_h4_init(void)
{
(...)
i2c_register_board_info(1, h4_i2c_board_info,
ARRAY_SIZE(h4_i2c_board_info));
(...)
}

最后需要注意的地方:
The above code declares 3 devices on I2C bus 1, including their respective
addresses and custom data needed by their drivers. When the I2C bus in
question is registered, the I2C devices will be instantiated automatically
by i2c-core.


The devices will be automatically unbound and destroyed when the I2C bus
they sit on goes away (if ever.)

以讯为的4412开发板为例:注册一个mpu6050的iic外部设备 在4412的平台文件里面添加
 #if !defined(CONFIG_CAN_MCP251X) && !defined(CONFIG_SPI_RC522)
static struct i2c_board_info i2c_devs6[] __initdata = {
        {
            I2C_BOARD_INFO("mpu6050", 0x68),
       },
 };      
#endif  











原创粉丝点击