1. 程式人生 > >Linux 核心I2C匯流排架構

Linux 核心I2C匯流排架構

匯流排是將裝置與驅動聯絡在一起的紐帶。
如果一個裝置與驅動彼此綁在了一起,通過sys目錄下的檔案資訊能看出其繫結的驅動/裝置物件。
如:

~# ls /sys/bus/i2c/drivers/ad-7441/ -l
lrwxrwxrwx    1 root     root             0 Jan  1 00:07 2-0070 -> ../../../../devices/platform/hisi_i2c.2/i2c-2/2-0070
--w-------    1 root     root          4096 Jan  1 00:07 bind
--w-------    1 root     root          4096
Jan 1 00:07 uevent --w------- 1 root root 4096 Jan 1 00:07 unbind

可以看出i2c總線上的名字為ad-7441的i2c_driver找到了其繫結的裝置。
同樣有:

/sys/devices/platform/hisi_i2c.2/i2c-2# ls 2-0070/ -l
lrwxrwxrwx    1 root     root             0 Jan  1 00:06 driver -> ../../../../../bus/i2c/drivers/ad-7441
-r--r--r--    1 root     root
4096 Jan 1 00:06 modalias -r--r--r-- 1 root root 4096 Jan 1 00:06 name drwxr-xr-x 2 root root 0 Jan 1 00:06 power lrwxrwxrwx 1 root root 0 Jan 1 00:06 subsystem -> ../../../../../bus/i2c -rw-r--r-- 1 root root 4096 Jan 1 00:06 uevent /sys/devices/platform/hisi_i2c
.2/i2c-2# cat 2-0082/name adv8200

由於系統中沒有i2c_add_driver 成員 id_table的name為adv8200的i2c_driver,所以,這裡的2-0082是找不到自己的另一半的。
相反,系統中定義了:

const static struct i2c_device_id slaveid[]={
            {.name="adv7441"},
            {.name="GS2970"},

};
static struct i2c_driver adv7441_driver={
    .probe=adv7441_probe,
    .id_table=slaveid,
    .driver = {
            .name   = "ad-7441",
            .owner  = THIS_MODULE,
    },
};
ret=i2c_add_driver(&adv7441_driver);

就因為這樣,ad-7441找到了driver,而8200卻找不到driver。
通常情況下,在一個已經配置了i2c_adpter裝置的系統中,如果要在核心中新增一個i2c從裝置的驅動。最常用的做法是:
1. 定義 i2c_board_info,並執行i2c_register_board_info(int busnum,
struct i2c_board_info const *info, unsigned len)。該函式會申請分配i2c_board_info
並新增到__i2c_board_list為頭的連結串列中。

struct i2c_board_info {
    char        type[I2C_NAME_SIZE];
    unsigned short  flags;
    unsigned short  addr;
    void        *platform_data;
    struct dev_archdata *archdata;
    struct device_node *of_node;
    int     irq;
};
/*  
 * I2C  slave devices 
 */
static struct i2c_board_info __initdata i2c_devs[] = {
        { I2C_BOARD_INFO("testA8", 0xA8), },
        { I2C_BOARD_INFO("adv7441", 0x70), },
        { I2C_BOARD_INFO("adv8200", 0x82), },
};
i2c_register_board_info(2, i2c_devs, ARRAY_SIZE(i2c_devs))

2.定義i2c_driver結構體,由於i2c_bus_type的match函式是根據id_table來判斷是否存在另一半的,因此i2c_driver中要定義合適的id_table欄位。並執行i2c_add_driver把driver新增到i2c總線上,還可以定義probe函式,在probe函式中對i2c裝置進行配置。。如:

static struct i2c_driver ds1307_driver = {
    .driver = {
        .name   = "rtc-ds1307",
        .owner  = THIS_MODULE,
    },
    .probe      = ds1307_probe,
    .remove     = __devexit_p(ds1307_remove),
    .id_table   = ds1307_id,
};
static const struct i2c_device_id ds1307_id[] = {
    { "ds1307", ds_1307 },
    { "ds1337", ds_1337 },
    { "ds1338", ds_1338 },
    { "ds1339", ds_1339 },
    { "ds1388", ds_1388 },
    { "ds1340", ds_1340 },
    { "ds3231", ds_3231 },
    { "m41t00", m41t00 },
    { "rx8025", rx_8025 },
    { }
};
const static struct i2c_device_id slaveid[]={
            {.name="adv7441"},
            {.name="GS2970"},

};
static struct i2c_driver adv7441_driver={
    .probe=adv7441_probe,
    .id_table=slaveid,
    .driver = {
            .name   = "ad-7441",
            .owner  = THIS_MODULE,
    },
};

那麼i2c_register_driver都做了哪些操作呢?
i2c_register_driver中的i2c_driver引數,如果i2c_driver引數定義了address_list且地址探測成功,則會自動呼叫i2c_new_device來建立裝置。
如:

/* This is the driver that will be inserted */
static struct i2c_driver ads7828_driver = {
    .class = I2C_CLASS_HWMON,
    .driver = {
        .name = "ads7828",
    },
    .probe = ads7828_probe,
    .remove = ads7828_remove,
    .id_table = ads7828_id,
    .detect = ads7828_detect,
    .address_list = normal_i2c,
};
/* Addresses to scan */
static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,I2C_CLIENT_END };
    #define I2C_CLIENT_END      0xfffeU

跟蹤核心原始碼,看i2c_register_driver的執行流程:

i2c_register_driver--》  i2c_for_each_dev(driver, __process_new_driver);---》i2c_do_add_adapter(data, to_i2c_adapter(dev));---》i2c_detect(adap, driver);---》  for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
        dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
            "addr 0x%02x\n", adap_id, address_list[i]);
        temp_client->addr = address_list[i];
        err = i2c_detect_address(temp_client, driver);
        if (unlikely(err))
            break;
    }

static int i2c_detect_address(struct i2c_client *temp_client,
                  struct i2c_driver *driver){

    if (info.type[0] == '\0') {//i2c_board_info的type欄位不為空
        dev_err(&adapter->dev, "%s detection function provided "
            "no name for 0x%x\n", driver->driver.name,
            addr);
    } else {
        struct i2c_client *client;

        /* Detection succeeded, instantiate the device */
        dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
            info.type, info.addr);
        client = i2c_new_device(adapter, &info);
        if (client)
            list_add_tail(&client->detected, &driver->clients);
        else
            dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
                info.type, info.addr);
    }

}
另外在i2c_register_adapter中執行的是
    bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);

    一個是__process_new_driver,一個是__process_new_adapter,兩個都執行了i2c_detect--》i2c_detect_address---》i2c_new_device。i2c_new_device中會給i2c_client的adpter成員賦值,然後會執行device_register,如果match函式返回真,則會執行i2c_device_probe。
    不同的地方是__process_new_adapter執行的是 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);遍歷i2c_bus上的所有driver。
    而i2c_register_driver用的是 i2c_for_each_dev(driver, __process_new_driver);---》bus_for_each_dev,遍歷的是i2c_bus上的所有device的時候,可以根據device得到adpter,因為i2c_adapter中有device dev成員。通過#define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
可以找到對應的adapter,然後adapter就可以作為i2c_detect的引數傳進去。


    struct bus_type i2c_bus_type = {
    .name       = "i2c",
    .match      = i2c_device_match,
    .probe      = i2c_device_probe,
    .remove     = i2c_device_remove,
    .shutdown   = i2c_device_shutdown,
    .pm     = &i2c_device_pm_ops,
};
    static int i2c_device_probe(struct device *dev)
{
    struct i2c_client   *client = i2c_verify_client(dev);
    struct i2c_driver   *driver;
    int status;

    if (!client)
        return 0;

    driver = to_i2c_driver(dev->driver);
    if (!driver->probe || !driver->id_table)
        return -ENODEV;
    client->driver = driver;
    }
    由此可見,在i2c_new_device的過程中,首選給client賦adapter,如果批評成功,呼叫了i2c_bus_type的probe函式後,又會給i2c_client賦i2c_driver。i2c_client的driver和adapter成員都會得到正確的賦值。

這說明在註冊i2c_driver的時候,也可以自動的建立裝置。
但不是必須這樣自動建立裝置的,有的i2c_driver中沒有定義address_list也沒有自定義attach函式,那麼就不會自動建立i2c_device.

如果不在核心裡做的話,可以實現i2c-dev.c的fops,在應用層呼叫read、write、ioctl、open系統呼叫來配置i2c裝置。
看了上面的內容,你會覺得太表面化了,或者是心裡沒底,為什麼這樣做就可以呢。下面就來解釋一下!

i2c匯流排結構可以分為3層:
1. i2c 核心層,主要是i2c-core.c,提供了i2c裝置、驅動的註冊、登出函式。I2C 通訊方法(即“ algorithm”)上層的、與具體介面卡無關的程式碼以及探測裝置、檢測裝置地址的上層程式碼等。
2. i2c裝置層。這裡說的裝置包括“主裝置從裝置”,主裝置是指i2c介面卡,一般是整合到CPU的i2c模組,也可以是GPIO模擬的i2c模組。從裝置是指我們的板子上用的支援i2c匯流排的晶片,如RTC、AD、E2PROM等。
3. I2C裝置驅動層,驅動是指從裝置的裝置。主要工作是填充I2c_driver。
我覺得上面的分層比較好理解。有的書上分三層是: 核心層、匯流排層、裝置驅動層。其中匯流排層是介面卡端完成的,而裝置驅動層是I2C硬體體系結構中裝置端的實現的,主要是填充i2c_client和driver。
這樣理解也行。可能是我對系統認識的不夠深,仍然覺得我自己的理解比較通俗。

一個常識性的知識,就是當device_add或者driver_add的時候,都會把device或者driver新增到bus的 struct subsys_private *p;成員指向的對應連結串列頭中,然後根據匯流排註冊的時候是否允許drivers_autoprobe,來進行探測。探測的時候會根據匯流排的match函式返回結果來決定是否要繫結dev和driver。那麼我們來看一下i2c_bus_type的註冊後是否允許drivers_autoprobe.

struct subsys_private {
    struct kset subsys;
    struct kset *devices_kset;
    struct list_head interfaces;
    struct mutex mutex;

    struct kset *drivers_kset;
    struct klist klist_devices;//新增到總線上的裝置連結串列
    struct klist klist_drivers;//新增到總線上的驅動連結串列
    struct blocking_notifier_head bus_notifier;
    unsigned int drivers_autoprobe:1;
    struct bus_type *bus;

    struct kset glue_dirs;
    struct class *class;
};
static int __init i2c_init(void)
{
    int retval;

    retval = bus_register(&i2c_bus_type);
    if (retval)
        return retval;
}
int __bus_register(struct bus_type *bus, struct lock_class_key *key)
{
    priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
    if (!priv)
        return -ENOMEM;

    priv->bus = bus;
    bus->p = priv;

    BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);

    retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
    if (retval)
        goto out;

    priv->subsys.kobj.kset = bus_kset;
    priv->subsys.kobj.ktype = &bus_ktype;
    **priv->drivers_autoprobe = 1;**

    retval = kset_register(&priv->subsys);
}

很明顯i2c_bus_type.p->drivers_autoprobe 是為1的。因此在新增i2c裝置和i2c驅動的時候,會自動匹配另一半。而匯流排匹配條件一般是匯流排的match成員函式返回的。那麼需要分析一下i2c匯流排的match函式:

static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
    struct i2c_client   *client = i2c_verify_client(dev);
    struct i2c_driver   *driver;

    if (!client)
        return 0;

    /* Attempt an OF style match */
    if (of_driver_match_device(dev, drv))
        return 1;

    driver = to_i2c_driver(drv);
    /* match on an id table if there is one */
    if (driver->id_table)
        return i2c_match_id(driver->id_table, client) != NULL;

    return 0;
}
static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
                        const struct i2c_client *client)
{
    while (id->name[0]) {
        if (strcmp(client->name, id->name) == 0)
            return id;
        id++;
    }
    return NULL;
}
struct bus_type i2c_bus_type = {
    .name       = "i2c",
    .match      = i2c_device_match,
    .probe      = i2c_device_probe,
    .remove     = i2c_device_remove,
    .shutdown   = i2c_device_shutdown,
    .pm     = &i2c_device_pm_ops,
};
EXPORT_SYMBOL_GPL(i2c_bus_type);

i2c_driver的id_table裡的name欄位與client欄位相同則返回真進行匹配。而clientd的name是從哪裡來的呢?是在i2c_new_devie(adpter)的時候給賦值的。而i2c_new_device。如果執行了i2c_register_board_info,那麼就會呼叫到i2c_scan_static_board_info。i2c_scan_static_board_info在掃描過程中,會嘗試執行i2c_new_device進行匹配。匹配成功了裝置與驅動就會繫結在一起。

在static int i2c_register_adapter(struct i2c_adapter *adap)中有

static int i2c_register_adapter(struct i2c_adapter *adap){
    dev_set_name(&adap->dev, "i2c-%d", adap->nr);//i2c-012
    adap->dev.bus = &i2c_bus_type;
    adap->dev.type = &i2c_adapter_type;
    res = device_register(&adap->dev);
    if (adap->nr < __i2c_first_dynamic_bus_num)
        i2c_scan_static_board_info(adap);

    /* Notify drivers */
    mutex_lock(&core_lock);
    bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
    mutex_unlock(&core_lock);

    }
static int __process_new_adapter(struct device_driver *d, void *data)
{
    return i2c_do_add_adapter(to_i2c_driver(d), data);
}
static int i2c_do_add_adapter(struct i2c_driver *driver,
                  struct i2c_adapter *adap)
{
    /* Detect supported devices on that bus, and instantiate them */
    i2c_detect(adap, driver);

    /* Let legacy drivers scan this bus for matching devices */
    if (driver->attach_adapter) {
        dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
             driver->driver.name);
        dev_warn(&adap->dev, "Please use another way to instantiate "
             "your i2c_client\n");
        /* We ignore the return code; if it fails, too bad */
        driver->attach_adapter(adap);
    }
    return 0;
}
static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
{
    const unsigned short *address_list;
    struct i2c_client *temp_client;
    int i, err = 0;
    int adap_id = i2c_adapter_id(adapter);

    address_list = driver->address_list;
    if (!driver->detect || !address_list)
        return 0;

    /* Stop here if the classes do not match */
    if (!(adapter->class & driver->class))
        return 0;

    /* Set up a temporary client to help detect callback */
    temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
    if (!temp_client)
        return -ENOMEM;
    temp_client->adapter = adapter;

    for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
        dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
            "addr 0x%02x\n", adap_id, address_list[i]);
        temp_client->addr = address_list[i];
        err = i2c_detect_address(temp_client, driver);
        if (unlikely(err))
            break;
    }

    kfree(temp_client);
    return err;
}
static int i2c_detect_address(struct i2c_client *temp_client,
                  struct i2c_driver *driver)
{
    struct i2c_board_info info;
    struct i2c_adapter *adapter = temp_client->adapter;
    int addr = temp_client->addr;
    int err;

    /* Make sure the address is valid */
    err = i2c_check_addr_validity(addr);
    if (err) {
        dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
             addr);
        return err;
    }

    /* Skip if already in use */
    if (i2c_check_addr_busy(adapter, addr))
        return 0;

    /* Make sure there is something at this address */
    if (!i2c_default_probe(adapter, addr))
        return 0;

    /* Finally call the custom detection function */
    memset(&info, 0, sizeof(struct i2c_board_info));
    info.addr = addr;
    err = driver->detect(temp_client, &info);
    if (err) {
        /* -ENODEV is returned if the detection fails. We catch it
           here as this isn't an error. */
        return err == -ENODEV ? 0 : err;
    }

    /* Consistency check */
    if (info.type[0] == '\0') {
        dev_err(&adapter->dev, "%s detection function provided "
            "no name for 0x%x\n", driver->driver.name,
            addr);
    } else {
        struct i2c_client *client;

        /* Detection succeeded, instantiate the device */
        dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
            info.type, info.addr);
        client = i2c_new_device(adapter, &info);
        if (client)
            list_add_tail(&client->detected, &driver->clients);
        else
            dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
                info.type, info.addr);
    }
    return 0;
}

static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
{
    struct i2c_devinfo  *devinfo;

    down_read(&__i2c_board_lock);
    list_for_each_entry(devinfo, &__i2c_board_list, list) {
        if (devinfo->busnum == adapter->nr
                && !i2c_new_device(adapter,
                        &devinfo->board_info))
            dev_err(&adapter->dev,
                "Can't create device at 0x%02x\n",
                devinfo->board_info.addr);
    }
    up_read(&__i2c_board_lock);
}
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
    struct i2c_client   *client;
    int         status;

    client = kzalloc(sizeof *client, GFP_KERNEL);
    if (!client)
        return NULL;

    client->adapter = adap;

    client->dev.platform_data = info->platform_data;

    if (info->archdata)
        client->dev.archdata = *info->archdata;

    client->flags = info->flags;
    client->addr = info->addr;
    client->irq = info->irq;

    **strlcpy(client->name, info->type, sizeof(client->name));**

    /* Check for address validity */
    status = i2c_check_client_addr_validity(client);
    if (status) {
        dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
            client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
        goto out_err_silent;
    }

    /* Check for address business */
    status = i2c_check_addr_busy(adap, client->addr);
    if (status)
        goto out_err;

    client->dev.parent = &client->adapter->dev;
    client->dev.bus = &i2c_bus_type;
    client->dev.type = &i2c_client_type;
    client->dev.of_node = info->of_node;

    /* For 10-bit clients, add an arbitrary offset to avoid collisions */
    dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
             client->addr | ((client->flags & I2C_CLIENT_TEN)
                     ? 0xa000 : 0));
    status = device_register(&client->dev);
    if (status)
        goto out_err;

    dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
        client->name, dev_name(&client->dev));

    return client;

out_err:
    dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
        "(%d)\n", client->name, client->addr, status);
out_err_silent:
    kfree(client);
    return NULL;
}

如果核心啟動過程中沒有執行i2c_register_board_info,那麼__i2c_first_dynamic_bus_num的值就為0,是不會執行i2c_scan_static_board_info的。
但是仍然可以在系統起來後,以模組的形式呼叫i2c_new_device。如:

static struct i2c_board_info hi_info = {
     I2C_BOARD_INFO("sensor_i2c", 0x6c),
};
static int hi_dev_init(void)
{
    struct i2c_adapter *i2c_adap;

    // use i2c0
    i2c_adap = i2c_get_adapter(0);
    sensor_client = i2c_new_device(i2c_adap, &hi_info);//可以使用這個client進行i2c設定。因為/
    *struct i2c_client {
    unsigned short flags;       /* div., see below      */
    unsigned short addr;        /* chip address - NOTE: 7bit    */
                    /* addresses are stored in the  */
                    /* _LOWER_ 7 bits       */
    char name[I2C_NAME_SIZE];
    struct i2c_adapter *adapter;    /* the adapter we sit on    */
    struct i2c_driver *driver;  /* and our access routines  */
    struct device dev;      /* the device structure     */
    int irq;            /* irq issued by device     */
    struct list_head detected;
};*/

    i2c_put_adapter(i2c_adap);
    return 0;
}

由此可以知道執行i2c_register_board_info並添加了i2c_boar_info後,在以後執行i2c_register_adapter的時候是會主動掃描__i2c_board_list中的靜態devinfo資訊。如果沒有執行i2c_register_board_info,那麼可以通過i2c_new_device以模組的形式被動建立client。在i2c_new_device的時候執行device_add->bus.match?probe:return.
在i2c_register_adapter中有

    if (adap->nr < __i2c_first_dynamic_bus_num)
        i2c_scan_static_board_info(adap);

    /* Notify drivers */
    mutex_lock(&core_lock);
    bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
    mutex_unlock(&core_lock);

即當在i2c總線上註冊介面卡的時候,會掃描i2c總線上的drv,從i2c_bus_type.p.klist_drivers中查詢驅動,遍歷到一個有效的驅動後執行__process_new_adapter(drv,adap); —>i2c_do_add_adapter—->i2c_detect(adap, driver)和driver->attach_adapter。顧名思義,其中的i2c_detect就是檢測i2c總線上可以使用的驅動是否與當前的介面卡匹配。如果定義了i2c_driver的detect和address_list才會匹配{ if (!driver->detect || !address_list)
return 0;},匹配成功則把製作的client結構體新增到driver->clients的連結串列中。不過這種方法用的很少,一般在i2c_driver中不處理detect和address資訊,因此這種Notify driver的方式很少用。

    下面總結一下:
1.首先執行的是i2c_init-》bus_register(&i2c_bus_type);
postcore_initcall(i2c_init);
系統中有了i2c匯流排,才能將i2c匯流排的裝置和驅動聯絡在一起。

2.完成i2c匯流排驅動即i2c介面卡驅動,主要是adpter的algorithm成員。填充好adpter以後,用i2c_add_numbered_adapter--》i2c_register_adapter-->主動掃描__i2c_board_list中的i2c_board_info的i2c裝置,如果有會執行i2c_new_device,並且在sys目錄下會看到想應name的目錄。--->掃描i2c總線上的所有驅動,來嘗試匹配該介面卡(現在基本不用)。
3.在裝置驅動層要完成的是定義i2c_board_info和i2c_driver。
這樣看上去,把i2c框架分成三層,i2c核心層、i2c匯流排層、i2c裝置驅動層。這樣的劃分方式應該是更合理。其中i2c核心層定義了i2c介面卡註冊登出、i2cdriver註冊登出,register_i2c_boardinfo、與具體的硬體無關的發生接收函式,如:i2c_master_send和i2c_master_recv。

載入模組呼叫核心態 I2C 讀寫程式示例:
此操作示例在核心態下通過 I2C 讀寫程式實現對 I2C 外圍裝置的讀寫操作。
步驟 1. 呼叫 I2C 核心層的函式,獲得描述一個 I2C 控制器的結構體 i2c_adap:
i2c_adap = i2c_get_adapter(2);
假設我們已經知道新增的器件掛載在 I2C 控制器 2 上,直接設定 i2c_get_adapter 的引數為 2。
步驟 2. 把 I2C 控制器和新增的 I2C 外圍裝置關聯起來,得到描述 I2C 外圍裝置的客戶端結構
體 hi_client:
hi_client = i2c_new_device(i2c_adap, &hi_info);
hi_info 結構體提供了 I2C 外圍裝置的裝置地址
步驟 3. 呼叫 I2C 核心層提供的標準讀寫函式對外圍器件進行讀寫:
ret = i2c_master_send(client, buf, count);
ret = i2c_master_recv(client, buf, count);
其中i2c_master_send都是與具體的平臺和硬體無關的介面,由i2c-core層定義,其中主要的是client,這個client對應於一個i2c裝置,i2c_client裡的adpter成員完成了i2c裝置和CPU之間的通訊。

int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
{
    int ret;
    struct i2c_adapter *adap = client->adapter;
    struct i2c_msg msg;

    msg.addr = client->addr;
#ifdef CONFIG_ARCH_HI3516A
    msg.flags = client->flags;
#else
    msg.flags = client->flags & I2C_M_TEN;
#endif
    msg.len = count;
    msg.buf = (char *)buf;

    ret = i2c_transfer(adap, &msg, 1);

    /*
     * If everything went ok (i.e. 1 msg transmitted), return #bytes
     * transmitted, else error code.
     */
    return (ret == 1) ? count : ret;
}
EXPORT_SYMBOL(i2c_master_send);

int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
    unsigned long orig_jiffies;
    int ret, try;

    /* REVISIT the fault reporting model here is weak:
     *
     *  - When we get an error after receiving N bytes from a slave,
     *    there is no way to report "N".
     *
     *  - When we get a NAK after transmitting N bytes to a slave,
     *    there is no way to report "N" ... or to let the master
     *    continue executing the rest of this combined message, if
     *    that's the appropriate response.
     *
     *  - When for example "num" is two and we successfully complete
     *    the first message but get an error part way through the
     *    second, it's unclear whether that should be reported as
     *    one (discarding status on the second message) or errno
     *    (discarding status on the first one).
     */

    if (adap->algo->master_xfer) {
#ifdef DEBUG
        for (ret = 0; ret < num; ret++) {
            dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
                "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
                ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
                (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
        }
#endif

        if (in_atomic() || irqs_disabled()) {
            ret = i2c_trylock_adapter(adap);
            if (!ret)
                /* I2C activity is ongoing. */
                return -EAGAIN;
        } else {
            i2c_lock_adapter(adap);
        }
        /* Retry automatically on arbitration loss */
        orig_jiffies = jiffies;
        for (ret = 0, try = 0; try <= adap->retries; try++) {
            ret = adap->algo->master_xfer(adap, msgs, num);
            if (ret != -EAGAIN)
                break;
            if (time_after(jiffies, orig_jiffies + adap->timeout))
                break;
        }
        i2c_unlock_adapter(adap);
        return ret;
    } else {
        dev_dbg(&adap->dev, "I2C level transfers not supported\n");
        return -EOPNOTSUPP;
    }
}
EXPORT_SYMBOL(i2c_transfer);

而adap->algo->master_xfer就是與硬體和介面卡有關的了,是驅動工程師要做的。如:
static const struct i2c_algorithm hi_i2c_algo = {
    .master_xfer    = hi_i2c_xfer,
    .functionality  = hi_i2c_func,
};
    adap->algo = &hi_i2c_algo;
    adap->dev.parent = &pdev->dev;
    adap->nr = pdev->id;
    adap->retries = CONFIG_HI_I2C_RETRIES;
    errorcode = i2c_add_numbered_adapter(adap);
static int hi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
        int num)
{
    struct hi_i2c *pinfo;
    int errorcode;
    pinfo = (struct hi_i2c *)i2c_get_adapdata(adap);
    pinfo->msgs = msgs;
    pinfo->msg_num = num;
    pinfo->msg_index = 0;
    if (msgs->flags & I2C_M_RD)
        errorcode = hi_i2c_read(pinfo);
    else
        errorcode = hi_i2c_write(pinfo);
    return errorcode;
}

以i2c-dev.c提供的介面,通過應用程式讀寫i2c裝置的方法:
使用者態 I2C 讀寫程式示例:
此操作示例在使用者態下通過 I2C 讀寫程式實現對 I2C 外圍裝置的讀寫操作。
步驟 1. 開啟 I2C 匯流排對應的裝置檔案,獲取檔案描述符:
fd = open(“/dev/i2c-2”, O_RDWR);
步驟 2. 通過 ioctl 設定外圍裝置地址、外圍裝置暫存器位寬和資料位寬
ret = ioctl(fd, I2C_SLAVE_FORCE, device_addr);
ioctl(fd, I2C_16BIT_REG, 0);
ioctl(fd, I2C_16BIT_DATA, 0);//ioctl 的第三個引數為 0 表示 8bit 位寬,為 1 表示 16bit 位寬。
步驟 3. 使用 read/wite 進行資料讀寫:
read(fd, recvbuf, reg_width);
write(fd, buf, (reg_width + data_width));

unsigned int reg_width = 1;
unsigned int data_width = 1;
unsigned int reg_step = 1;
HI_RET i2c_read(int argc, char* argv[])
{
 int fd = -1;
 int ret;
 unsigned int i2c_num, device_addr, reg_addr, reg_addr_end;
 char data;
 char recvbuf[4];
 int cur_addr;
 memset(recvbuf, 0x0, 4);
 fd = open("/dev/i2c-2", O_RDWR);
 if (fd<0)
 {
 printf("Open i2c dev error!\n");
 return -1;
 }
 ret = ioctl(fd, I2C_SLAVE_FORCE, device_addr);
 if (reg_width == 2)
 ret = ioctl(fd, I2C_16BIT_REG, 1);
 else
 ret = ioctl(fd, I2C_16BIT_REG, 0);
 if (ret < 0) {
 printf("CMD_SET_REG_WIDTH error!\n");
 close(fd);
 return -1;
 }
 if (data_width == 2)
 ret = ioctl(fd, I2C_16BIT_DATA, 1);
 else
 ret = ioctl(fd, I2C_16BIT_DATA, 0);
 if (ret < 0) {
 printf("CMD_SET_DATA_WIDTH error!\n");
 close(fd);
 return -1;
 }
 for (cur_addr = reg_addr; cur_addr < reg_addr_end + reg_width;
cur_addr += reg_step)
 {
 if (reg_width == 2) {
 recvbuf[0] = cur_addr & 0xff;
 recvbuf[1] = (cur_addr >> 8) & 0xff;
 } else
 recvbuf[0] = cur_addr & 0xff;
 ret = read(fd, recvbuf, reg_width);
 if (ret < 0) {
 printf("CMD_I2C_READ error!\n");
 close(fd);
 return -1;
 }
 if (data_width == 2) {
 data = recvbuf[0] | (recvbuf[1] << 8);
 } else
 data = recvbuf[0];
 printf("0x%x 0x%x\n", cur_addr, data);
 }
 close(fd);
 return 0;
}

i2c_write(int argc , char* argv[])
{
 int fd = -1;
 int ret =0, index = 0;
 unsigned int i2c_num, device_addr, reg_addr, reg_value;
 char buf[4];
 fd = open("/dev/i2c-2", O_RDWR);
 if(fd < 0)
 {
 printf("Open i2c dev error!\n");
 return -1;
 }
 ret = ioctl(fd, I2C_SLAVE_FORCE, device_addr);
 if (reg_width == 2)
 ret = ioctl(fd, I2C_16BIT_REG, 1);
 else
 ret = ioctl(fd, I2C_16BIT_REG, 0);
 if (data_width == 2)
 ret = ioctl(fd, I2C_16BIT_DATA, 1);
 else
 ret = ioctl(fd, I2C_16BIT_DATA, 0);
 if (reg_width == 2) {
 buf[index] = reg_addr & 0xff;
 index++;
 buf[index] = (reg_addr >> 8) & 0xff;
 index++;
 } else {
 buf[index] = reg_addr & 0xff;
 index++;
 }
 if (data_width == 2) {
 buf[index] = reg_value & 0xff;
 index++;
 buf[index] = (reg_value >> 8) & 0xff;
 index++;
 } else {
 buf[index] = reg_value & 0xff;
 index++;
 }
 write(fd, buf, (reg_width + data_width));
 if(ret < 0)
 {
 printf("I2C_WRITE error!\n");
 return -1;
 }
 close(fd);
 return 0;
}

下面簡單分析一下i2c-dev.c

static const struct file_operations i2cdev_fops = {
    .owner      = THIS_MODULE,
    .llseek     = no_llseek,
    .read       = i2cdev_read,
    .write      = i2cdev_write,
    .unlocked_ioctl = i2cdev_ioctl,
    .open       = i2cdev_open,
    .release    = i2cdev_release,
};
static int i2cdev_open(struct inode *inode, struct file *file)
{
    unsigned int minor = iminor(inode);
    struct i2c_client *client;
    struct i2c_adapter *adap;
    struct i2c_dev *i2c_dev;

    i2c_dev = i2c_dev_get_by_minor(minor);
    if (!i2c_dev)
        return -ENODEV;

    adap = i2c_get_adapter(i2c_dev->adap->nr);
    if (!adap)
        return -ENODEV;
        .......
}
static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count,
        loff_t *offset)
{
    char *tmp;
    int ret;

    struct i2c_client *client = file->private_data;

    if (count > 8192)
        count = 8192;

    tmp = kmalloc(count, GFP_KERNEL);
    if (tmp == NULL)
        return -ENOMEM;

#ifdef CONFIG_ARCH_HI3516A
    copy_from_user(tmp, buf, count);
#endif

    pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n",
        iminor(file->f_path.dentry->d_inode), count);

    ret = i2c_master_recv(client, tmp, count);
    if (ret >= 0)
        ret = copy_to_user(buf, tmp, count) ? -EFAULT : ret;
    kfree(tmp);
    return ret;
}

static ssize_t i2cdev_write(struct file *file, const char __user *buf,
        size_t count, loff_t *offset)
{
    int ret;
    char *tmp;
    struct i2c_client *client = file->private_data;

    if (count > 8192)
        count = 8192;

    tmp = memdup_user(buf, count);
    if (IS_ERR(tmp))
        return PTR_ERR(tmp);

    pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
        iminor(file->f_path.dentry->d_inode), count);

    ret = i2c_master_send(client, tmp, count);
    kfree(tmp);
    return ret;
}
static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    struct i2c_client *client = file-&