I2C驱动的另类注册方法

原创 2012年03月29日 09:50:04


static int tpd_detect (struct i2c_client *client, int kind, struct i2c_board_info *info)
{
    strcpy(info->type, TPD_DEVICE);
    return 0;
}


static const struct i2c_device_id tpd_id[] = {{TPD_DEVICE,0},{}};
unsigned short force[] = {0,0x7a,I2C_CLIENT_END,I2C_CLIENT_END};
static const unsigned short * const forces[] = { force, NULL };
static struct i2c_client_address_data addr_data = { .forces = forces, };

static struct i2c_driver tpd_i2c_driver = {
  .driver = {
     .name = TPD_DEVICE,
     .owner = THIS_MODULE,
  },
  .probe = tpd_probe,
  .remove = __devexit_p(tpd_remove),
  .id_table = tpd_id,
  .detect = tpd_detect,
  .address_data = &addr_data,
};

static inline int i2c_add_driver(struct i2c_driver *driver)
{    
    return i2c_register_driver(THIS_MODULE, driver);
}    


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;

    /* add the driver to the list of i2c drivers in the driver core */
    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;
}
EXPORT_SYMBOL(i2c_register_driver);



static int __attach_adapter(struct device *dev, void *data)
{
    struct i2c_adapter *adapter;
    struct i2c_driver *driver = data;

    if (dev->type != &i2c_adapter_type)
        return 0;
    adapter = to_i2c_adapter(dev);

    i2c_detect(adapter, driver);

    /* Legacy drivers scan i2c busses directly */
    if (driver->attach_adapter)
        driver->attach_adapter(adapter);

    return 0;
}



static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
{
    const struct i2c_client_address_data *address_data;
    struct i2c_client *temp_client;
    int i, err = 0;
    int adap_id = i2c_adapter_id(adapter);


static int i2c_detect_address(struct i2c_client *temp_client, int kind,
                  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 */
        /* Infinity, 20090525 { */
        //if (addr < 0x03 || addr > 0x77) {
        if (addr < 0x03 || addr > 0xff) {
        /* Infinity, 20090525 } */
        dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
             addr);
        return -EINVAL;
    }

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

    /* Make sure there is something at this address, unless forced */
    if (kind < 0) {
        if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
                   I2C_SMBUS_QUICK, NULL) < 0)
            return 0;

        /* prevent 24RF08 corruption */
        if ((addr & ~0x0f) == 0x50)
            i2c_smbus_xfer(adapter, addr, 0, 0, 0,
                       I2C_SMBUS_QUICK, NULL);
    }

    /* Finally call the custom detection function */
    memset(&info, 0, sizeof(struct i2c_board_info));
    info.addr = addr;
    err = driver->detect(temp_client, kind, &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;
}

相关文章推荐

i2c驱动注册流程实例分析

本文以触摸屏GSL3680为例详细分析驱动从注册到调用的整个流程。程序详见GSL3680目录下的gslX680.c文件。 谨以本文记录当时分析思路为之后回顾保留资料,同时纪念那在力源思创昏天黑的的加...

i2c驱动注册过程---GSL3680为例进行详析---步骤2

步骤2:分析真正的驱动注册函数:driver_register() driver_register()是将设备驱动添加到总线的设备驱动链中的函数。 函数分析: int driver_regi...

以611_I2C为例 跟踪Platform架构驱动——驱动注册篇

//*************************************************//         下面是驱动的注册//****************************...

两种方式的i2c设备驱动的编写方法

我的理念:简单实用即可,不要搞一堆源码出来,结果让人看了以后还不知道怎么用,看我的: 1、在arch/arm/mach-xxx/ 自己的平台文件里添加i2c信息,美其名曰:i2c_b...

linux 驱动调试时在线读写I2C寄存器的方法

调试I2C设备时经常需要修改寄存器的值,通常的方法是修改-》编译-》烧写-》重启,哪怕改一个寄存器也要这样折腾一下,很消耗时间,下面提供一种在线修改寄存器的方法,在终端中敲命令就可以写寄存器,不要上面...

LINUX I2C设备驱动的编写方法

作者:刘洪涛,华清远见嵌入式学院讲师。 四、在内核里写i2c设备驱动的两种方式 前文介绍了利用/dev/i2c-0在应用层完成对i2c设备的操作,但很多时候我们还是习惯为i2c设备在内核层编写驱动...

linux I2C 驱动之----i2c_driver的注册

在 linux I2C 驱动之----i2c_client 的注册中介绍了i2c_client的注册,现在再来说说i2c_driver的注册过程。        每一个驱动程序都有 module...

i2c_add_driver:i2c驱动注册流程分析

以声卡cx20709为例进行分析1.加载驱动的函数module_init(cx20709_modinit);2.用于初始化驱动的函数static int __init cx20709_modinit(...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:I2C驱动的另类注册方法
举报原因:
原因补充:

(最多只允许输入30个字)