linux I2C 驅動之----i2c驅動的註冊過程(i2c_register_driver->driver_register(&driver->driver)->driver_find)__linux

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.