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;
}

Linux I2C设备驱动编写(二)

在(一)中简述了Linux I2C子系统的三个主要成员i2c_adapter、i2c_driver、i2c_client。三者的关系也在上一节进行了描述。应该已经算是对Linux I2C子系统有了初步...
  • airk000
  • airk000
  • 2014年03月16日 23:26
  • 17921

编写i2c驱动-基于Linux3.10

很多芯片的控制方法均使用了i2c的方式,EEPROM,音频芯片AK4951,索尼、松下、豪威的图像传感器、电机驱动等均采用i2c的控制方式。彻底的弄懂i2c设备驱动,对于理解其它驱动非常有帮助,从投入...
  • shichaog
  • shichaog
  • 2014年11月16日 08:52
  • 1891

v4l2视频采集驱动框架(vfe, camera i2c driver,v4l2_subdev等之间的联系)

2014年的博文就从这篇文章开始吧,又一次回到linux,过去的一年从dm3730再到dm6437,这次来到了全志的A31 4核处理器,每一次都是全新的事物,但是偶然间还是可以感受到对新事物的消化能...
  • sanmaoljh
  • sanmaoljh
  • 2016年04月12日 15:31
  • 1989

i2c驱动调试经验

http://blog.csdn.net/cmm20071020/article/details/7179958
  • u013615357
  • u013615357
  • 2014年04月03日 14:44
  • 958

Openwrt环境下I2C操作

一、          环境介绍 软件环境:openwrt稳定发行版 barrier_breaker 硬件环境:主控芯片 MT7620n   eeprom 24c02   本文主要介绍,在openwr...
  • stone8761
  • stone8761
  • 2014年11月09日 12:21
  • 4038

i2c设备与驱动匹配过程

linux下i2c驱动笔记 1. 几个基本概念 1.1. 设备模型 由 总线(bus_type) + 设备(device) + 驱动(device_d...
  • u013952558
  • u013952558
  • 2015年12月04日 15:47
  • 2670

Linux I2C设备驱动编写(三)-实例分析AM3359

TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格支持标准模式(100K bits/s)和快速模式(400K bits/s)多路接收、发送模式支...
  • airk000
  • airk000
  • 2014年03月18日 15:11
  • 15227

Linux 内核I2C总线架构

总线是将设备与驱动联系在一起的纽带。 如果一个设备与驱动彼此绑在了一起,通过sys目录下的文件信息能看出其绑定的驱动/设备对象。 如:~# ls /sys/bus/i2c/drivers/ad-7...
  • linchuanzhi_886
  • linchuanzhi_886
  • 2015年03月24日 18:33
  • 1116

Linux I2C设备驱动编写(一)

在Linux驱动中I2C系统中主要包含以下几个成员: I2C adapter 即I2C适配器 I2C driver 某个I2C设备的设备驱动,可以以driver理解。 I2C client 某个I2...
  • airk000
  • airk000
  • 2014年03月16日 23:24
  • 33198

I2C子系统驱动架构 - 驱动框架

文章系列I2C子系统驱动架构 - 简介I2C子系统驱动架构 - 驱动框架I2C子系统驱动架构 - 具体实现 基于linux内核4.6.3版本介绍 I2C驱动框架I2C驱动框架图如下所示,用户空间上...
  • l289123557
  • l289123557
  • 2016年07月03日 18:15
  • 5035
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:I2C驱动的另类注册方法
举报原因:
原因补充:

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