i2c的操作在内核中是当做字符设备来操作的,相关初始化在由i2c_dev_init函数来初始化。
并且i2c adapter的驱动通过i2cdev_driver这个通用驱动的attach方法来实现注册的。
下面具体分析整个过程。
1.i2c_dev_init函数
static int __init i2c_dev_init(void) { 。。。 。。。 res = register_chrdev(I2C_MAJOR, "i2c", &i2cdev_fops); if (res) goto out; i2c_dev_class = class_create(THIS_MODULE, "i2c-dev"); if (IS_ERR(i2c_dev_class)) { res = PTR_ERR(i2c_dev_class); goto out_unreg_chrdev; } res = i2c_add_driver(&i2cdev_driver); 。。。 。。。 }
函数首先调用register_chardev函数向内核注册主备号为I2C_MAJOR、操作集为i2cdev_fops的字符设备。
register_chrdev函数最终会向系统注册主设备为I2C_MAJOR,此设备号为0~255的设备。这表示系统最多
可以容纳256个i2c adapter,adapter的字符操作方法i2cdev_fops如下:
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, };
当read()、write()、open()、close()、ioctl()等系统调用发生时就会调用到这些函数。
但是i2cdev_fops其实是通用的的操作,应为不同的adapter对应的操作方法肯定有区别
所以这里的fops只是具体adapter操作方法的一层外壳,具体稍后分析。
字符设备注册完毕后通过class_create()函数初始化一个类i2c_dev_class,这个类稍后需要使用,用于在
/dev/i2c-0下自动创建设备,后面分析。
类初始化完毕后,然后调用函数i2c_add_driver函数注册i2c driver。这里所说的i2c其实对应的是系统中所有的i2c类设备。
通过i2c driver中的attach_adapter方法来实现将adapter和对应的驱动绑定。
static struct i2c_driver i2cdev_driver = { .driver = { .name = "dev_driver", }, .attach_adapter = i2cdev_attach_adapter, .detach_adapter = i2cdev_detach_adapter, };此处注意attach_adapter这个方法,/dev目录下的设备创建是在通过执行此函数实现的。
下面具体分析i2c_add_driver注册i2cdev_driver的过程
2.i2c_add_driver
i2c_add_driver函数只是对i2c_register_driver做了简单的封装,下面直接分析i2c_register_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))){ printk("Can't register until after driver model init\n"); 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, __process_new_driver); mutex_unlock(&core_lock); return 0; }
函数通过
driver->driver.bus = &i2c_bus_type;可见此驱动通过函数driver_register()之后 同样会被注册到了i2c总线上。
值得一提的是此处i2cdev_driver中的attach_adapter的执行机会很大,
通过bus_for_each_dev()函数
bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);会尝试和i2c总线上所有的dev进行一次匹配,只要获取的dev为adapter时,就可执行后续操作。
此处的bus_for_each_dev函数主要功能就是循环查询一遍i2c总线上所有的dev,包括adapter device和client device。
然后依次将dev和driver作为__process_new_driver的参数并执行__process_new_driver函数,但是只有adapter device
才会执行后续的操作,否则返回继续轮询i2c总线上的dev。
static int __process_new_driver(struct device *dev, void *data) { if (dev->type != &i2c_adapter_type) return 0; return i2c_do_add_adapter(data, to_i2c_adapter(dev)); }
后面的代码,假如不是则立即返回继续摘取下个dev然后循环执行__process_new_driver。因为对
我们来说,我们只需要注册adapter的驱动就可以了,i2c的所有操作是通过主机来完成的,从机只是
被动接受。由于之前已经通过i2cadd_numbered_adapter()注册过adapter到总线i2c_bus_type,
所以此处有机会执行后面的i2c_do_add_adapter函数。
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) { /* We ignore the return code; if it fails, too bad */ driver->attach_adapter(adap); } return 0; }
由于此处驱动并未初始化driver->detect,所以i2c_detect函数未执行有效操作就会退出。
接着通过传统方式执行driver->attach_adapter方法。
tatic int i2cdev_attach_adapter(struct i2c_adapter *adap) { struct i2c_dev *i2c_dev; int res; i2c_dev = get_free_i2c_dev(adap); if (IS_ERR(i2c_dev)) return PTR_ERR(i2c_dev); /* register this i2c device with the driver core */ i2c_dev->dev = device_create(i2c_dev_class, &adap->dev, MKDEV(I2C_MAJOR, adap->nr), NULL, "i2c-%d", adap->nr); if (IS_ERR(i2c_dev->dev)) { res = PTR_ERR(i2c_dev->dev); goto error; } res = device_create_file(i2c_dev->dev, &dev_attr_name); if (res) goto error_destroy; pr_debug("i2c-dev: adapter [%s] registered as minor %d\n", adap->name, adap->nr); return 0; error_destroy: device_destroy(i2c_dev_class, MKDEV(I2C_MAJOR, adap->nr)); error: return_i2c_dev(i2c_dev); return res; }
可见attach_adapter函数的作用就是调用device_create()函数 通过之前class_create的类信息在/dev下自动创建设备文件。
并且此设备的设备号是由固定的主设备号I2C_MAJOR 和 从设备号组成的,从设备号取的就是adapter的nr,此处为0。
并且可以推断出系统最多可以容纳0~255 总共256个i2c adapter。
到此i2c部分的初始化就完成了,可以通过read write来操作设备了。
补充:上面说的新方法好像在驱动里面就会detect client,然后把所有检测到的client放到一条链表里。