Linux下i2c驱动的加载过程,分为i2c设备层、i2c adapter层与i2c核心层
i2c设备驱动层也就是我们为特定i2c设备编写的驱动,下面是我自己理解的i2c驱动的注册过程
在我们写的i2c设备驱动中,我们会调用i2c_add_driver()开始i2c设备驱动的注册,该函数调用
i2c_register_driver完成所有注册操作
static inline int i2c_add_driver(struct i2c_driver *driver)
{
return i2c_register_driver(THIS_MODULE, driver);
}
i2c_register_driver会调用driver_register() 来将设备驱动添加到总线的设备驱动链表中:
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
{
int res;
/* Can't register until after driver model init */
if (unlikely(WARN_ON(!i2c_bus_type.p)))
return -EAGAIN;
driver->driver.owner = owner;
driver->driver.bus = &i2c_bus_type;
/* When registration returns, the driver core
* will have called probe() for all matching-but-unbound devices.
*/
res = driver_register(&driver->driver);
if (res)
return res;
pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
INIT_LIST_HEAD(&driver->clients);
/* Walk the adapters that are already present */
mutex_lock(&core_lock);
bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
mutex_unlock(&core_lock);
return 0;
}
在driver_register中,通过driver_find来判断驱动是否已经注册,然后会调用
bus_add_drive
将设备驱动添加到总线上
int driver_register(struct device_driver *drv)
{
int ret;
struct device_driver *other;
BUG_ON(!drv->bus->p);
if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown))
printk(KERN_WARNING "Driver '%s' needs updating - please use "
"bus_type methods\n", drv->name);
other = driver_find(drv->name, drv->bus);
if (other) {
put_driver(other);
printk(KERN_ERR "Error: Driver '%s' is already registered, "
"aborting...\n", drv->name);
return -EBUSY;
}
ret = bus_add_driver(drv);
if (ret)
return ret;
ret = driver_add_groups(drv, drv->groups);
if (ret)
bus_remove_driver(drv);
return ret;
}
在bus_add_driver中初始化priv->klist_devices的值,并将priv赋值给drv->p
**************************************************************************
1、在arch/arm/mach-xxx/ 自己的平台文件里添加i2c信息,美其名曰:i2c_board_info
例如:
static struct i2c_board_info __initdata xxxi2c_board_info[] = {
{
I2C_BOARD_INFO("abcd1", 0x20), /* 字符串要与后面的匹配,0x20是从设备地址 */
.platform_data = 0,
},
{
I2C_BOARD_INFO("abcd2", 0x21),
.platform_data = 0,
},
};
然后调用i2c_register_board_info(1, xxxi2c_board_info, ARRAY_SIZE(xxxi2c_board_info));
第一个参数是0还是1,我还不知道:-(
2、在另外一个设备驱动文件里,比如你放到/driver/char下做字符设备,一般是module_init(func_init())形式,则调用i2c_add_driver()即可,有几个要定义:
static const struct i2c_device_id xxx_led_id[] = {
{ "abcd1", 0 }, /* 该名称必须与BOARD_INFO的匹配才会调用probe函数 */
{ "abcd2", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, xxx_led_id);
static struct i2c_driver xxx_led_driver = {
.driver = {
.name = "yourname", /* 该名字不需要与别的匹配 */
.owner = THIS_MODULE,
},
.probe = xxx_led_probe,
.remove = xxx_remove,
.id_table = xxx_led_id,
};
看到了吧,struct i2c_device_id里面的字符串与 I2C_BOARD_INFO里面的匹配后,xxx_led_probe才会调用。
如果不想用同一个probe,那就在写一个struct i2c_device_id和struct i2c_driver