Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux I2C 子系統剖析:核心、總線驅動與設備驅動

一、I2C 架構分層

Linux 的 I2C 體系分為三層:

  1. I2C core:提供設備驅動與總線驅動的介面 API、I2C 通訊方法,以及設備探測能力。
  2. I2C 總線驅動:實作硬體體系中的「適配器」,解決通訊時怎麼發資料。
  3. I2C 設備驅動:找出適配器並與 CPU 交換資料,解決向誰發、發什麼。

Linux I2C 子系統剖析:核心、總線驅動與設備驅動

在核心原始碼的 drivers/i2c 目錄下:i2c-core.c 實作核心層,i2c-dev.c 提供適配器設備功能,buses/ 放主機控制器驅動,algos/ 則是各總線適配器的通訊方法。

二、關鍵資料結構

/* 對應一個物理上的適配器 */
struct i2c_adapter {
    struct module *owner;
    unsigned int id;
    unsigned int class;
    const struct i2c_algorithm *algo;   /* 存取總線的演算法 */
    void *algo_data;
    struct rt_mutex bus_lock;
    int timeout;                        /* 單位 jiffies */
    int retries;
    struct device dev;                  /* 適配器設備 */
    int nr;
    char name[48];
    struct completion dev_released;
    struct list_head userspace_clients;
};

/* 一套 I2C 總線通訊方法 */
struct i2c_algorithm {
    int (*master_xfer)(struct i2c_adapter *adap,
                       struct i2c_msg *msgs, int num);
    int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,
                      unsigned short flags, char read_write,
                      u8 command, int size,
                      union i2c_smbus_data *data);
    u32 (*functionality)(struct i2c_adapter *);
};

/* 一套設備驅動方法 */
struct i2c_driver {
    unsigned int class;
    int (*probe)(struct i2c_client *, const struct i2c_device_id *);
    int (*remove)(struct i2c_client *);
    void (*shutdown)(struct i2c_client *);
    int (*suspend)(struct i2c_client *, pm_message_t mesg);
    int (*resume)(struct i2c_client *);
    struct device_driver driver;
    const struct i2c_device_id *id_table;   /* 支援的設備 id 表 */
    int (*detect)(struct i2c_client *, struct i2c_board_info *);
    const unsigned short *address_list;
    struct list_head clients;
};

/* 一個真實的 I2C 物理設備 */
struct i2c_client {
    unsigned short flags;
    unsigned short addr;            /* 7 位元晶片位址 */
    char name[I2C_NAME_SIZE];
    struct i2c_adapter *adapter;    /* 掛在哪個適配器上 */
    struct i2c_driver *driver;
    struct device dev;
    int irq;
    struct list_head detected;
};

/* I2C 訊息結構 */
struct i2c_msg {
    __u16 addr;         /* 從設備位址 */
    __u16 flags;        /* I2C_M_RD 表讀、I2C_M_TEN 表 10 位元位址等 */
    __u16 len;          /* 訊息長度 */
    __u8 *buf;          /* 資料指標 */
};

三、I2C 總線驅動

總線驅動是獨立存在的,掛在 platform 總線上,最終目標是註冊一個 adapter。

/* plat-s2410.c:platform_device */
struct platform_device s3c_device_i2c2 = {
    .name = "s3c2410-i2c",
    .id   = 2,
    .num_resources = ARRAY_SIZE(s3c_i2c_resource),
    .resource      = s3c_i2c_resource,   /* 暫存器位址與中斷號 */
};

/* 傳給 platform_driver 的私有資料 */
static struct s3c2410_platform_i2c default_i2c_data2 __initdata = {
    .flags     = 0,
    .bus_num   = 2,          /* 對應 i2c-2 */
    .slave_addr = 0x10,
    .frequency = 100 * 1000,
    .sda_delay = 100,
};

/* platform_driver 與 id_table */
static struct platform_driver s3c24xx_i2c_driver = {
    .probe     = s3c24xx_i2c_probe,
    .remove    = s3c24xx_i2c_remove,
    .id_table  = s3c24xx_driver_ids,
    .driver    = { .owner = THIS_MODULE, .name = "s3c-i2c" },
};

static struct platform_device_id s3c24xx_driver_ids[] = {
    { .name = "s3c2410-i2c", .driver_data = TYPE_S3C2410 },
    { .name = "s3c2440-i2c", .driver_data = TYPE_S3C2440 },
    { },
};
MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);

probe 中完成暫存器、時鐘、中斷等資源的初始化,最後註冊適配器:

static int s3c24xx_i2c_probe(struct platform_device *pdev)
{
    pdata = pdev->dev.platform_data;              /* 取得私有資料 */
    i2c->adap.algo = &s3c24xx_i2c_algorithm;      /* 綁定訊號傳輸函式 */
    init_waitqueue_head(&i2c->wait);              /* 等待佇列 */

    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    i2c->regs = ioremap(res->start, resource_size(res));
    s3c24xx_i2c_init(i2c);                        /* 初始化暫存器 */

    i2c->irq = platform_get_irq(pdev, 0);
    request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,
                dev_name(&pdev->dev), i2c);

    i2c->adap.nr = pdata->bus_num;
    i2c_add_numbered_adapter(&i2c->adap);         /* 註冊 adapter */
    i2c_scan_static_board_info(adap);             /* 掃描總線產生 client */
}

訊號傳輸與中斷的配合是重點:

static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
    .master_xfer   = s3c24xx_i2c_xfer,
    .functionality = s3c24xx_i2c_func,
};

static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
                            struct i2c_msg *msgs, int num)
{
    s3c24xx_i2c_set_master(i2c);
    i2c->msg = msgs;
    i2c->msg_num = num;
    s3c24xx_i2c_enable_irq(i2c);
    s3c24xx_i2c_message_start(i2c, msgs);   /* 送 start 訊號 */

    unsigned int addr = (msg->addr & 0x7f) << 1;   /* 注意 7 位元位址 */
    timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
}

/* 中斷:每完成一個 byte 就繼續,全部完成後喚醒等待佇列 */
static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
{
    s3c24xx_i2c_irq_nextbyte(i2c, status);
    s3c24xx_i2c_stop(i2c, 0);        /* 送 stop 訊號 */
    s3c24xx_i2c_master_complete(i2c, ret);
    wake_up(&i2c->wait);             /* 喚醒 xfer */
    s3c24xx_i2c_disable_irq(i2c);
}

四、I2C 設備驅動與 client 的誕生

設備驅動要匹配的是 i2c_client,而 client 是在註冊 adapter 時掃描 __i2c_board_list 依 i2c_board_info 動態產生的。註冊方式有兩種:

/* 方法一:在 BSP 中靜態註冊 */
i2c_register_board_info(0, i2c_devs0, ARRAY_SIZE(i2c_devs0));

/* 方法二:核心支援設備樹時,在 dts 的 i2c 節點下描述 */
struct i2c_board_info {
    char type[I2C_NAME_SIZE];   /* 名字,如 "lm75" */
    unsigned short flags;
    unsigned short addr;        /* 7 位元位址 */
    void *platform_data;
    int irq;
};

client 產生的路徑如下:

static int i2c_register_adapter(struct i2c_adapter *adap)
{
    i2c_scan_static_board_info(adap);
    list_for_each_entry(devinfo, &__i2c_board_list, list)
        i2c_new_device(adapter, &devinfo->board_info);
}

struct i2c_client *i2c_new_device(struct i2c_adapter *adap,
                                  struct i2c_board_info const *info)
{
    client->adapter = adap;
    client->dev.platform_data = info->platform_data;
    client->dev.bus  = &i2c_bus_type;
    client->dev.type = &i2c_client_type;
    device_register(&client->dev);   /* 掛到 i2c 總線上 */
}

最後在設備驅動中提供 id_table 完成匹配,並在 probe 裡註冊一組 fops 供上層讀寫:

static struct i2c_device_id id_tables[] = {
    { "lm75",  0x1111 },
    { "lm75a", 0x1112 },
};

static struct i2c_driver lm75_drv = {
    .probe     = lm75_drv_probe,   /* 在 probe 中註冊 fops */
    .remove    = lm75_drv_remove,  /* 必須實作 */
    .driver    = { .name = "lm75" },
    .id_table  = id_tables,
};

整體脈絡可以記成一句話:總線驅動負責「會不會講 I2C」,設備驅動負責「要跟誰講什麼」,而 core 提供兩者相遇的介面與 client 的配對機制。

Copyright © Linux教程網 All Rights Reserved