1. 程式人生 > >Linux I2C(一)之常用的幾種例項化(i2c_client )

Linux I2C(一)之常用的幾種例項化(i2c_client )

前言:

因為工作是音訊驅動,所以經常涉及到I2C、I2S等常用的匯流排,想將I2C相關的東西總結一下,讓自己更加了解I2C。

  • 基於:Linux3.10

方式一:

使用arch/arm/mach-s3c24xx/mach-mini2440.c舉例:

static struct i2c_board_info mini2440_i2c_devs[] __initdata = {
    {
/* 遇到與”24c08一樣的名稱”的驅動就會與之繫結,0x50是I2C裝置的地址 */
        I2C_BOARD_INFO("24c08", 0x50),
        .platform_data = &at24c08,
    },
};

/* 這裡的0代表:i2c-0匯流排 */
i2c_register_board_info(0, mini2440_i2c_devs, ARRAY_SIZE(mini2440_i2c_devs));
  • 到這裡我們可以說就完成了第一種方式的例項化。

  • 使用i2c_register_board_info去例項化必須知道我們使用的I2C裝置是掛載到哪個總線上,並知道裝置的地址。

  • 在Linux啟動的時候會將資訊進行收集,i2c介面卡會掃描已經靜態註冊的i2c_board_info,通過呼叫i2c_register_board_info函式將包含所有I2C裝置的i2c_board_info資訊的i2c_devinfo變數加入到__i2c_board_list連結串列中,並呼叫i2c_new_device為其例項化一個i2c_client。在驅動載入的時候遇到同名的i2c_board_info就會將i2c_client和driver繫結,並且執行driver的probe函式。

  • 這種方式一般放在平臺的程式碼中。

struct i2c_board_info :

/**
 * struct i2c_board_info - template for device creation
 * @type: chip type, to initialize i2c_client.name
 * @flags: to initialize i2c_client.flags
 * @addr: stored in i2c_client.addr
 * @platform_data: stored in i2c_client.dev.platform_data
 * @archdata: copied into
i2c_client.dev.archdata * @of_node: pointer to OpenFirmware device node * @acpi_node: ACPI device node * @irq: stored in i2c_client.irq * * I2C doesn't actually support hardware probing, although controllers and * devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's * a device at a given address. Drivers commonly need more information than * that, such as chip type, configuration, associated IRQ, and so on. * * i2c_board_info is used to build tables of information listing I2C devices * that are present. This information is used to grow the driver model tree. * For mainboards this is done statically using i2c_register_board_info(); * bus numbers identify adapters that aren't yet available. For add-on boards, * i2c_new_device() does this dynamically with the adapter already known. */ 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; struct acpi_dev_node acpi_node; int irq; };

i2c_register_board_info:

/**
 * i2c_register_board_info - statically declare I2C devices
 * @busnum: identifies the bus to which these devices belong
 * @info: vector of i2c device descriptors
 * @len: how many descriptors in the vector; may be zero to reserve
 *  the specified bus number.
 *
 * Systems using the Linux I2C driver stack can declare tables of board info
 * while they initialize.  This should be done in board-specific init code
 * near arch_initcall() time, or equivalent, before any I2C adapter driver is
 * registered.  For example, mainboard init code could define several devices,
 * as could the init code for each daughtercard in a board stack.
 *
 * The I2C devices will be created later, after the adapter for the relevant
 * bus has been registered.  After that moment, standard driver model tools
 * are used to bind "new style" I2C drivers to the devices.  The bus number
 * for any device declared using this routine is not available for dynamic
 * allocation.
 *
 * The board info passed can safely be __initdata, but be careful of embedded
 * pointers (for platform_data, functions, etc) since that won't be copied.
 */
int i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned len)
{
    int status;

    down_write(&__i2c_board_lock);

    /* dynamic bus numbers will be assigned after the last static one */
    if (busnum >= __i2c_first_dynamic_bus_num)
        __i2c_first_dynamic_bus_num = busnum + 1;

    for (status = 0; len; len--, info++) {
        struct i2c_devinfo  *devinfo;

        devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
        if (!devinfo) {
            pr_debug("i2c-core: can't register boardinfo!\n");
            status = -ENOMEM;
            break;
        }

        devinfo->busnum = busnum;
        devinfo->board_info = *info;
        list_add_tail(&devinfo->list, &__i2c_board_list);
    }

    up_write(&__i2c_board_lock);

    return status;
}

方式二:

使用arch/arm/mach-ux500/board-mop500-uib.c舉例:

void mop500_uib_i2c_add(int busnum, struct i2c_board_info *info,
        unsigned n)
{
    struct i2c_adapter *adap;
    struct i2c_client *client;
    int i;
/* 獲得一個匯流排,當然必須知道我們裝置要掛載在哪個總線上,busnum就是匯流排編號 */
    adap = i2c_get_adapter(busnum);
    if (!adap) {
        pr_err("failed to get adapter i2c%d\n", busnum);
        return;
    }

    for (i = 0; i < n; i++) {
/* 將i2c_board_info所描述的器件與介面卡進行關聯,並例項化i2c_client */
        client = i2c_new_device(adap, &info[i]);
        if (!client)
            pr_err("failed to register %s to i2c%d\n",
                    info[i].type, busnum);
    }
/* 與 i2c_get_adapter對應,釋放資源 */
    i2c_put_adapter(adap);
}
  • 別忘了在登出驅動或者出錯的情況下呼叫i2c_unregister_device(struct i2c_client *client)去釋放資源。

  • 這種方式與方式一的差別是不需要在編譯核心的時候就要知道裝置掛載哪個總線上、裝置的地址是什麼。靈活性變強了。

方式三:

如果連i2c裝置的地址不知道,我們可以提供一個地址列表供系統探測。

使用drivers/media/pci/bt8xx/bttv-input.c舉例:

/* Instantiate the I2C IR receiver device, if present */
void init_bttv_i2c_ir(struct bttv *btv)
{
/* 這裡就是地址列表 */
    const unsigned short addr_list[] = {
        0x1a, 0x18, 0x64, 0x30, 0x71,
        I2C_CLIENT_END
    };
    struct i2c_board_info info;
    struct i2c_client *i2c_dev;

    if (0 != btv->i2c_rc)
        return;
/* 用於存放i2c_board_info資訊 */
    memset(&info, 0, sizeof(struct i2c_board_info));
    memset(&btv->init_data, 0, sizeof(btv->init_data));
    strlcpy(info.type, "ir_video", I2C_NAME_SIZE);

    switch (btv->c.type) {
    case BTTV_BOARD_PV951:
        btv->init_data.name = "PV951";
        btv->init_data.get_key = get_key_pv951;
        btv->init_data.ir_codes = RC_MAP_PV951;
        info.addr = 0x4b;
        break;
    }

    if (btv->init_data.name) {
        info.platform_data = &btv->init_data;
        i2c_dev = i2c_new_device(&btv->c.i2c_adap, &info);
    } else {
        /*
         * The external IR receiver is at i2c address 0x34 (0x35 for
         * reads).  Future Hauppauge cards will have an internal
         * receiver at 0x30 (0x31 for reads).  In theory, both can be
         * fitted, and Hauppauge suggest an external overrides an
         * internal.
         * That's why we probe 0x1a (~0x34) first. CB
         */
/* 這樣就會在指定的總線上匹配addr_list中地址,將第一個匹配正確的地址儲存到info->addr,然後使用i2c_device_new來例項化i2c_client */
        i2c_dev = i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
    }
    if (NULL == i2c_dev)
        return;

#if defined(CONFIG_MODULES) && defined(MODULE)
    request_module("ir-kbd-i2c");
#endif
}

i2c_new_probed_device:

struct i2c_client *i2c_new_probed_device(struct i2c_adapter *adap,
              struct i2c_board_info *info,
              unsigned short const *addr_list,
              int (*probe)(struct i2c_adapter *, unsigned short addr))
{
    int i;

    if (!probe)
        probe = i2c_default_probe;

    for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
        /* Check address validity */
        if (i2c_check_addr_validity(addr_list[i]) < 0) {
            dev_warn(&adap->dev, "Invalid 7-bit address "
                 "0x%02x\n", addr_list[i]);
            continue;
        }

        /* Check address availability */
        if (i2c_check_addr_busy(adap, addr_list[i])) {
            dev_dbg(&adap->dev, "Address 0x%02x already in "
                "use, not probing\n", addr_list[i]);
            continue;
        }

        /* Test address responsiveness */
        if (probe(adap, addr_list[i]))
            break;
    }

    if (addr_list[i] == I2C_CLIENT_END) {
        dev_dbg(&adap->dev, "Probing failed, no device found\n");
        return NULL;
    }

    info->addr = addr_list[i];
    return i2c_new_device(adap, info);
}

方式四:

從使用者空間著手,在/sys/bus/i2c/devices/i2c-0(i2c匯流排編號)下存在new_device(建立i2c_client)和delete_device(刪除i2c_client)。

new_device方法:
echo [name] [addr:0x20] > /sys/bus/i2c/devices/i2c-0/new_device
delete_device方法:
echo [addr:0x20] > /sys/bus/i2c/devices/i2c-0/delete_device

方式五:

在dtsi中有:

/*@後面是裝置的起始地址*/
     &i2c-0@fe {
         /* i2c_client的name = "hall-i2c" */
         compatible = "qcom, hall-i2c";
         reg = <fe>;
         interrupts = <70>;
/* 如果設定成disabled,在初始化的時候就不會被例項化,可以在linux內建文件檢視更多 */
         status = "disabled";
     };
  • 其中:i2c-0中的0是匯流排編號,reg是裝置地址,interrupts是中斷號。

  • 在初始化的時候i2c匯流排會呼叫qup_i2c_probe(),接著呼叫of_i2c_register_devices對dtsi上所描述的裝置進行例項化。並建立相應的sys檔案:sys/bus/i2c/devices/0-00fe。

驅動部分程式碼:

/* 即使在dtsi中沒有compatible 與驅動中的相對應,只要有i2c_client有與下面列表有對應的也會觸發xxx_probe()函式。*/
static const struct i2c_device_id hall_id[] = {
    { "hall-i2c", 0 },          
    { "lm82", 0 },
    { }
};
MODULE_DEVICE_TABLE(i2c, hall_id);
  • 上面的lm83_id[] 列出的就是本驅動支援的i2c_client,哪一個i2c_client的名字與此處匹配就會呼叫xxx_probe()。具體的匹配函式是i2c_match_id。
static const struct of_device_id i2c_of_match[] = {
    {
        .compatible = "qcom,hall-i2c",
        .data = &ppdata,
    },
    { },
};

static struct platform_driver omap_i2c_driver = {
    .probe        = xxx_probe,
    .remove        = xxx_remove,
    .id_table   = hall_id,
    .driver        = {
        .name    = "qcom_test",
        .owner    = THIS_MODULE,
        .of_match_table = of_match_ptr(i2c_of_match),
    },
};

static int __init hall_i2c_init(void) 
{ 
    return i2c_add_driver(&omap_i2c_driver);    
} 
  • 在驅動載入的時候i2c_of_match與dtsi中的”qcom,hall-i2c”匹配成功就會呼叫xxx_probe函式。並發現/sys/bus/i2c/devices/0-00fe/subsystem/drivers/qcom_test,沒錯qcom_test就是我們驅動中的name。

of_i2c_register_devices:

void of_i2c_register_devices(struct i2c_adapter *adap)
{
    void *result;
    struct device_node *node;

    /* Only register child devices if the adapter has a node pointer set */
    if (!adap->dev.of_node)
        return;

    dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");

    for_each_available_child_of_node(adap->dev.of_node, node) {
        struct i2c_board_info info = {};
        struct dev_archdata dev_ad = {};
        const __be32 *addr;
        int len;

        dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);

        if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
            dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
                node->full_name);
            continue;
        }

        addr = of_get_property(node, "reg", &len);
        if (!addr || (len < sizeof(int))) {
            dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
                node->full_name);
            continue;
        }

        info.addr = be32_to_cpup(addr);
        if (info.addr > (1 << 10) - 1) {
            dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
                info.addr, node->full_name);
            continue;
        }

        info.irq = irq_of_parse_and_map(node, 0);
        info.of_node = of_node_get(node);
        info.archdata = &dev_ad;

        if (of_get_property(node, "wakeup-source", NULL))
            info.flags |= I2C_CLIENT_WAKE;

        request_module("%s%s", I2C_MODULE_PREFIX, info.type);

        result = i2c_new_device(adap, &info);
        if (result == NULL) {
            dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
                    node->full_name);
            of_node_put(node);
            irq_dispose_mapping(info.irq);
            continue;
        }
    }
}

方式六

方式三在探測到第一個可用的地址就停止探測了,且只能在一個總線上去探測。如果之前並不確定匯流排的編號,或者一次探測多個i2c裝置,我們就可以用方式六了。

使用/drivers/hwmon/adm1026.c舉例:

/* Addresses to scan ,地址列表,I2C_CLIENT_END結束標誌*/
static const unsigned short normal_i2c[] = {
    0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};

/* 本驅動所支援的i2c_client */
static const struct i2c_device_id adm1026_id[] = {
    { "adm1026", 0 },
    { }
};
MODULE_DEVICE_TABLE(i2c, adm1026_id);

static struct i2c_driver adm1026_driver = {
    .class      = I2C_CLASS_HWMON,
    .driver = {
        .name   = "adm1026",
    },
    .probe      = adm1026_probe,
    .remove     = adm1026_remove,
    .id_table   = adm1026_id,
/* 回撥函式:用於自主檢測,符合就返回0,不滿足就返回-ENODEV */
    .detect     = adm1026_detect,
    .address_list   = normal_i2c,
};

回撥函式adm1026_detect:

/* Return 0 if detection is successful, -ENODEV otherwise */
static int adm1026_detect(struct i2c_client *client,
              struct i2c_board_info *info)
{
    struct i2c_adapter *adapter = client->adapter;
    int address = client->addr;
    int company, verstep;

    if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
        /* We need to be able to do byte I/O */
        return -ENODEV;
    };

    /* Now, we do the remaining detection. */

    company = adm1026_read_value(client, ADM1026_REG_COMPANY);
    verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);

    dev_dbg(&adapter->dev,
        "Detecting device at %d,0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
        i2c_adapter_id(client->adapter), client->addr,
        company, verstep);

    /* Determine the chip type. */
    dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
        i2c_adapter_id(adapter), address);
    if (company == ADM1026_COMPANY_ANALOG_DEV
        && verstep == ADM1026_VERSTEP_ADM1026) {
        /* Analog Devices ADM1026 */
    } else if (company == ADM1026_COMPANY_ANALOG_DEV
        && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
        dev_err(&adapter->dev,
            "Unrecognized stepping 0x%02x. Defaulting to ADM1026.\n",
            verstep);
    } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
        dev_err(&adapter->dev,
            "Found version/stepping 0x%02x. Assuming generic ADM1026.\n",
            verstep);
    } else {
        dev_dbg(&adapter->dev, "Autodetection failed\n");
        /* Not an ADM1026... */
        return -ENODEV;
    }

    strlcpy(info->type, "adm1026", I2C_NAME_SIZE);

    return 0;
}
  • i2c_driver註冊的時候,i2c_core會在所有已經註冊的i2c_adapter上探測address_list中的所有地址,硬體探測成功之後後呼叫i2c_driver的detect(這裡是adm1026_detect)回撥函式,如果符合我們的要求那麼久填充引數info(struct i2c_board_info *info),這裡填充了info->type(也就是name),至於info->addr等i2c_core會自動賦值依據我們給出的address_list。接著會根據detect填充的info建立一個i2c_client。

  • 如果多個個總線上有相同的地址的裝置,那麼會分別建立多個個i2c_client。如果address_list中的多個地址都有裝置佔用,那麼會建立多個i2c_client。