include/linux/i2c.h
struct i2c_msg;
struct i2c_algorithm;
struct i2c_adapter;
struct i2c_client;
struct i2c_driver;
union i2c_smbus_data;
I2C驱动主要包含三部分:I2C核心、I2C总线驱动、I2C设备驱动,它们主要的数据结构在目录:/include/linux/i2c.h
struct i2c_driver
/*
* A driver is capable of handling one or more physical devices present on
* I2C adapters. This information is used to inform the driver of adapter
* events.
*
* The driver.owner field should be set to the module owner of this driver.
* The driver.name field should be set to the name of this driver.
*/
struct i2c_driver {
int id;
unsigned int class; /* Notifies the driver that a new bus has appeared. This routine
* can be used by the driver to test if the bus meets its conditions
* & seek for the presence of the chip(s) it supports. If found, it
* registers the client(s) that are on the bus to the i2c admin. via
* i2c_attach_client. (LEGACY I2C DRIVERS ONLY)
*/
int (*attach_adapter)(struct i2c_adapter *);
int (*detach_adapter)(struct i2c_adapter *); /* tells the driver that a client is about to be deleted & gives it
* the chance to remove its private data. Also, if the client struct
* has been dynamically allocated by the driver in the function above,
* it must be freed here. (LEGACY I2C DRIVERS ONLY)
*/
int (*detach_client)(struct i2c_client *); /* Standard driver model interfaces, for "new style" i2c drivers.
* With the driver model, device enumeration is NEVER done by drivers;
* it's done by infrastructure. (NEW STYLE DRIVERS ONLY)
*/
int (*probe)(struct i2c_client *);
int (*remove)(struct i2c_client *); /* driver model interfaces that don't relate to enumeration */
void (*shutdown)(struct i2c_client *);
int (*suspend)(struct i2c_client *, pm_message_t mesg);
int (*resume)(struct i2c_client *); /* a ioctl like command that can be used to perform specific functions
* with the device.
*/
int (*command)(struct i2c_client *client,unsigned int cmd, void *arg); struct device_driver driver;
struct list_head list;
};
struct i2c_client
/**
* struct i2c_client - represent an I2C slave device
* @addr: Address used on the I2C bus connected to the parent adapter.
* @name: Indicates the type of the device, usually a chip name that's
* generic enough to hide second-sourcing and compatible revisions.
* @dev: Driver model device node for the slave.
* @driver_name: Identifies new-style driver used with this device; also
* used as the module name for hotplug/coldplug modprobe support.
*
* An i2c_client identifies a single device (i.e. chip) connected to an
* i2c bus. The behaviour is defined by the routines of the driver.
*/
struct i2c_client {
unsigned short flags; /* div., see below */
unsigned short addr; /* chip address - NOTE: 7bit */
/* addresses are stored in the */
/* _LOWER_ 7 bits */
char name[I2C_NAME_SIZE];
struct i2c_adapter *adapter; /* the adapter we sit on */
struct i2c_driver *driver; /* and our access routines */
int usage_count; /* How many accesses currently */
/* to the client */
struct device dev; /* the device structure */
int irq; /* irq issued by device (or -1) */
char driver_name[KOBJ_NAME_LEN];
struct list_head list;
struct completion released;
};
struct i2c_algorithm
/*
* The following structs are for those who like to implement new bus drivers:
* i2c_algorithm is the interface to a class of hardware solutions which can
* be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
* to name two of the most common.
*/
struct i2c_algorithm {
/* If an adapter algorithm can't do I2C-level access, set master_xfer
to NULL. If an adapter algorithm can do SMBus access, set
smbus_xfer. If set to NULL, the SMBus protocol is simulated
using common I2C messages */
/* master_xfer should return the number of messages successfully
processed, or a negative value on error */
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); /* --- ioctl like call to set div. parameters. */
int (*algo_control)(struct i2c_adapter *, unsigned int, unsigned long); /* To determine what the adapter supports */
u32 (*functionality) (struct i2c_adapter *);
};
struct i2c_adapter
/*
* i2c_adapter is the structure used to identify a physical i2c bus along
* with the access algorithms necessary to access it.
*/
struct i2c_adapter {
struct module *owner;
unsigned int id;
unsigned int class;
const struct i2c_algorithm *algo; /* the algorithm to access the bus */
void *algo_data; /* --- administration stuff. */
int (*client_register)(struct i2c_client *);
int (*client_unregister)(struct i2c_client *); /* data fields that are valid for all devices */
u8 level; /* nesting level for lockdep */
struct mutex bus_lock;
struct mutex clist_lock; int timeout;
int retries;
struct device dev; /* the adapter device */ int nr;
struct list_head clients;
struct list_head list;
char name[];
struct completion dev_released;
}
I2C核心
I2C核心提供了I2C总线驱动和设备驱动的注册、注销方法,I2C通信方法(algorithm)上层的与具体适配器无关的代码以及探测设备、检测设备地址的上层代码等。
I2C总线驱动
I2C总线驱动是对I2C硬件体系结构中适配器段端的实现,适配器可由CPU控制,甚至可以直接集成在CPU内部。I2C总线驱动主要包括I2C适配器数据结构i2c_adapter、I2C适配器的Algorithm数据结构i2c_algorithm和控制I2C适配器产生通信信号的函数。经由I2C总线驱动的代码,我们可以控制I2C适配器以主控方式产生开始位、停止位、读写周期,以及从设备方式读写、产生ACK等。
I2C设备驱动
I2C设备驱动即客户驱动时对I2C硬件体系结构中设备端的实现,设备一般挂接在受CPU控制的I2C适配器上,通过I2C适配器与CPU交换数据。I2C设备驱动主要包含数据结构i2c_driver和i2c_client,我们需要具体设备实现其中的成员函数。
在linux2.6内核中,所有设备都在sysfs文件系统中显示,在sysfs虚拟文件系统中存放了驱动挂载的总线以及device、driver,当我们注册一个driver后,内核会将我们注册的这个driver添加到这类驱动总线上这类总线的拥有一个共同的类似于一个基类kobject,而kset就是koject的一个集合。我们在写驱动的时候一般不会去分析kobject、kset,毕竟他们在内核里面是非常顶层的软件抽象层,但是对于内核整个驱动框架,却不能不分析这类抽象层,下图是我在树莓派所做的截图:
我们可以看到在sys文件目录下面有bus、class等,进入bus后会看到各种设备驱动,如在I2C中我们可以看到device、drivers,当然这些目录下面都没有什么内容应为sysfs是一个虚拟文件系统主要是记录各个进程和内核方面的信息。我们的驱动设备如何和虚拟文件系统产生关系了呢,就是kobject在这儿起了作用,我们的device、driver最终都会挂载一个总线上,后面我们会看到sysfs申请内存为device或者driver建立节点。
同样注册一个device后也会挂载在总线上。其实I2C我们也可以看成设备-总线-驱动模型,
i2c_register_driver(THIS_MODULE, driver)
/*
* An i2c_driver is used with one or more i2c_client (device) nodes to access
* i2c slave chips, on a bus instance associated with some i2c_adapter. There
* are two models for binding the driver to its device: "new style" drivers
* follow the standard Linux driver model and just respond to probe() calls
* issued if the driver core sees they match(); "legacy" drivers create device
* nodes themselves.
*/ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
{
int res; /* new style driver methods can't mix with legacy ones */
if (is_newstyle_driver(driver)) {
if (driver->attach_adapter || driver->detach_adapter
|| driver->detach_client) {
printk(KERN_WARNING
"i2c-core: driver [%s] is confused\n",
driver->driver.name);
return -EINVAL;
}
} /* add the driver to the list of i2c drivers in the driver core */
driver->driver.owner = owner;
driver->driver.bus = &i2c_bus_type; /* for new style drivers, 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; mutex_lock(&core_lists); list_add_tail(&driver->list,&drivers);
pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); /* legacy drivers scan i2c busses directly */
if (driver->attach_adapter) {
struct i2c_adapter *adapter; list_for_each_entry(adapter, &adapters, list) {
driver->attach_adapter(adapter);
}
} mutex_unlock(&core_lists);
return ;
}
driver_register(&driver->driver);
/**
* driver_register - register driver with bus
* @drv: driver to register
*
* We pass off most of the work to the bus_add_driver() call,
* since most of the things we have to do deal with the bus
* structures.
*/
int driver_register(struct device_driver * drv)
{
if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown)) {
printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
}
klist_init(&drv->klist_devices, NULL, NULL);
return bus_add_driver(drv);
}
klist_init(&drv->klist_devices, NULL, NULL);
/**
* driver_register - register driver with bus
* @drv: driver to register
*
* We pass off most of the work to the bus_add_driver() call,
* since most of the things we have to do deal with the bus
* structures.
*/
int driver_register(struct device_driver * drv)
{
if ((drv->bus->probe && drv->probe) ||
(drv->bus->remove && drv->remove) ||
(drv->bus->shutdown && drv->shutdown)) {
printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
}
klist_init(&drv->klist_devices, NULL, NULL);
return bus_add_driver(drv);
}
bus_add_driver(drv);
/**
* bus_add_driver - Add a driver to the bus.
* @drv: driver.
*
*/
int bus_add_driver(struct device_driver *drv)
{
struct bus_type * bus = get_bus(drv->bus);
int error = ; if (!bus)
return -EINVAL; pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
error = kobject_set_name(&drv->kobj, "%s", drv->name);
if (error)
goto out_put_bus;
drv->kobj.kset = &bus->drivers;
if ((error = kobject_register(&drv->kobj)))
goto out_put_bus; if (drv->bus->drivers_autoprobe) {
error = driver_attach(drv);
if (error)
goto out_unregister;
}
klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
module_add_driver(drv->owner, drv); error = driver_add_attrs(bus, drv);
if (error) {
/* How the hell do we get out of this pickle? Give up */
printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
__FUNCTION__, drv->name);
}
error = add_bind_files(drv);
if (error) {
/* Ditto */
printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
__FUNCTION__, drv->name);
} return error;
out_unregister:
kobject_unregister(&drv->kobj);
out_put_bus:
put_bus(bus);
return error;
}
kobject_register(&drv->kobj)
/**
* kobject_register - initialize and add an object.
* @kobj: object in question.
*/ int kobject_register(struct kobject * kobj)
{
int error = -EINVAL;
if (kobj) {
kobject_init(kobj);
error = kobject_add(kobj);
if (!error)
kobject_uevent(kobj, KOBJ_ADD);
}
return error;
}
kobject_add(kobj);
/**
* kobject_add - add an object to the hierarchy.
* @kobj: object.
*/
int kobject_add(struct kobject * kobj)
{
return kobject_shadow_add(kobj, NULL);
}
kobject_shadow_add(kobj, NULL);
/**
* kobject_shadow_add - add an object to the hierarchy.
* @kobj: object.
* @shadow_parent: sysfs directory to add to.
*/ int kobject_shadow_add(struct kobject * kobj, struct dentry *shadow_parent)
{
int error = ;
struct kobject * parent; if (!(kobj = kobject_get(kobj)))
return -ENOENT;
if (!kobj->k_name)
kobj->k_name = kobj->name;
if (!*kobj->k_name) {
pr_debug("kobject attempted to be registered with no name!\n");
WARN_ON();
kobject_put(kobj);
return -EINVAL;
}
parent = kobject_get(kobj->parent); pr_debug("kobject %s: registering. parent: %s, set: %s\n",
kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
kobj->kset ? kobj->kset->kobj.name : "<NULL>" ); if (kobj->kset) {
spin_lock(&kobj->kset->list_lock); if (!parent)
parent = kobject_get(&kobj->kset->kobj); list_add_tail(&kobj->entry,&kobj->kset->list);
spin_unlock(&kobj->kset->list_lock);
kobj->parent = parent;
} error = create_dir(kobj, shadow_parent);
if (error) {
/* unlink does the kobject_put() for us */
unlink(kobj);
kobject_put(parent); /* be noisy on error issues */
if (error == -EEXIST)
printk(KERN_ERR "kobject_add failed for %s with "
"-EEXIST, don't try to register things with "
"the same name in the same directory.\n",
kobject_name(kobj));
else
printk(KERN_ERR "kobject_add failed for %s (%d)\n",
kobject_name(kobj), error);
dump_stack();
} return error;
}
create_dir(kobj, shadow_parent);
static int create_dir(struct kobject * kobj, struct dentry *shadow_parent)
{
int error = ;
if (kobject_name(kobj)) {
error = sysfs_create_dir(kobj, shadow_parent);
if (!error) {
if ((error = populate_dir(kobj)))
sysfs_remove_dir(kobj);
}
}
return error;
}
sysfs_create_dir(kobj, shadow_parent);
/**
* sysfs_create_dir - create a directory for an object.
* @kobj: object we're creating directory for.
* @shadow_parent: parent parent object.
*/ int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
{
struct dentry * dentry = NULL;
struct dentry * parent;
int error = ; BUG_ON(!kobj); if (shadow_parent)
parent = shadow_parent;
else if (kobj->parent)
parent = kobj->parent->dentry;
else if (sysfs_mount && sysfs_mount->mnt_sb)
parent = sysfs_mount->mnt_sb->s_root;
else
return -EFAULT; error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
if (!error)
kobj->dentry = dentry;
return error;
}
create_dir(kobj,parent,kobject_name(kobj),&dentry);
static int create_dir(struct kobject * k, struct dentry * p,
const char * n, struct dentry ** d)
{
int error;
umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; mutex_lock(&p->d_inode->i_mutex);
*d = lookup_one_len(n, p, strlen(n));
if (!IS_ERR(*d)) {
if (sysfs_dirent_exist(p->d_fsdata, n))
error = -EEXIST;
else
error = sysfs_make_dirent(p->d_fsdata, *d, k, mode,
SYSFS_DIR);
if (!error) {
error = sysfs_create(*d, mode, init_dir);
if (!error) {
inc_nlink(p->d_inode);
(*d)->d_op = &sysfs_dentry_ops;
d_rehash(*d);
}
}
if (error && (error != -EEXIST)) {
struct sysfs_dirent *sd = (*d)->d_fsdata;
if (sd) {
list_del_init(&sd->s_sibling);
sysfs_put(sd);
}
d_drop(*d);
}
dput(*d);
} else
error = PTR_ERR(*d);
mutex_unlock(&p->d_inode->i_mutex);
return error;
}
sysfs_create(*d, mode, init_dir);
int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
{
int error = ;
struct inode * inode = NULL;
if (dentry) {
if (!dentry->d_inode) {
struct sysfs_dirent * sd = dentry->d_fsdata;
if ((inode = sysfs_new_inode(mode, sd))) {
if (dentry->d_parent && dentry->d_parent->d_inode) {
struct inode *p_inode = dentry->d_parent->d_inode;
p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
}
goto Proceed;
}
else
error = -ENOMEM;
} else
error = -EEXIST;
} else
error = -ENOENT;
goto Done; Proceed:
if (init)
error = init(inode);
if (!error) {
d_instantiate(dentry, inode);
if (S_ISDIR(mode))
dget(dentry); /* pin only directory dentry in core */
} else
iput(inode);
Done:
return error;
}
sysfs_create(*d, mode, init_dir);
int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
{
int error = ;
struct inode * inode = NULL;
if (dentry) {
if (!dentry->d_inode) {
struct sysfs_dirent * sd = dentry->d_fsdata;
if ((inode = sysfs_new_inode(mode, sd))) {
if (dentry->d_parent && dentry->d_parent->d_inode) {
struct inode *p_inode = dentry->d_parent->d_inode;
p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
}
goto Proceed;
}
else
error = -ENOMEM;
} else
error = -EEXIST;
} else
error = -ENOENT;
goto Done; Proceed:
if (init)
error = init(inode);
if (!error) {
d_instantiate(dentry, inode);
if (S_ISDIR(mode))
dget(dentry); /* pin only directory dentry in core */
} else
iput(inode);
Done:
return error;
}
sysfs_new_inode(mode, sd))
struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd)
{
struct inode * inode = new_inode(sysfs_sb);
if (inode) {
inode->i_blocks = ;
inode->i_mapping->a_ops = &sysfs_aops;
inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
inode->i_op = &sysfs_inode_operations;
inode->i_ino = sd->s_ino;
lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); if (sd->s_iattr) {
/* sysfs_dirent has non-default attributes
* get them for the new inode from persistent copy
* in sysfs_dirent
*/
set_inode_attr(inode, sd->s_iattr);
} else
set_default_inode_attr(inode, mode);
}
return inode;
}
new_inode(sysfs_sb);
/**
* new_inode - obtain an inode
* @sb: superblock
*
* Allocates a new inode for given superblock.
*/
struct inode *new_inode(struct super_block *sb)
{
/*
* On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
* error if st_ino won't fit in target struct field. Use 32bit counter
* here to attempt to avoid that.
*/
static unsigned int last_ino;
struct inode * inode; spin_lock_prefetch(&inode_lock); inode = alloc_inode(sb);
if (inode) {
spin_lock(&inode_lock);
inodes_stat.nr_inodes++;
list_add(&inode->i_list, &inode_in_use);
list_add(&inode->i_sb_list, &sb->s_inodes);
inode->i_ino = ++last_ino;
inode->i_state = ;
spin_unlock(&inode_lock);
}
return inode;
}
alloc_inode(sb);
static struct inode *alloc_inode(struct super_block *sb)
{
static const struct address_space_operations empty_aops;
static struct inode_operations empty_iops;
static const struct file_operations empty_fops;
struct inode *inode; if (sb->s_op->alloc_inode)
inode = sb->s_op->alloc_inode(sb);
else
inode = (struct inode *) kmem_cache_alloc(inode_cachep, GFP_KERNEL); if (inode) {
struct address_space * const mapping = &inode->i_data; inode->i_sb = sb;
inode->i_blkbits = sb->s_blocksize_bits;
inode->i_flags = ;
atomic_set(&inode->i_count, );
inode->i_op = &empty_iops;
inode->i_fop = &empty_fops;
inode->i_nlink = ;
atomic_set(&inode->i_writecount, );
inode->i_size = ;
inode->i_blocks = ;
inode->i_bytes = ;
inode->i_generation = ;
#ifdef CONFIG_QUOTA
memset(&inode->i_dquot, , sizeof(inode->i_dquot));
#endif
inode->i_pipe = NULL;
inode->i_bdev = NULL;
inode->i_cdev = NULL;
inode->i_rdev = ;
inode->dirtied_when = ;
if (security_inode_alloc(inode)) {
if (inode->i_sb->s_op->destroy_inode)
inode->i_sb->s_op->destroy_inode(inode);
else
kmem_cache_free(inode_cachep, (inode));
return NULL;
} mapping->a_ops = &empty_aops;
mapping->host = inode;
mapping->flags = ;
mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
mapping->assoc_mapping = NULL;
mapping->backing_dev_info = &default_backing_dev_info; /*
* If the block_device provides a backing_dev_info for client
* inodes then use that. Otherwise the inode share the bdev's
* backing_dev_info.
*/
if (sb->s_bdev) {
struct backing_dev_info *bdi; bdi = sb->s_bdev->bd_inode_backing_dev_info;
if (!bdi)
bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
mapping->backing_dev_info = bdi;
}
inode->i_private = NULL;
inode->i_mapping = mapping;
}
return inode;
}
kmem_cache_alloc(inode_cachep, GFP_KERNEL);这儿就是最底层为sys虚拟文件系统分配内存的函数,就是采用了高速页缓存方法,在内存中为节点分配了一块内存。
**
* kmem_cache_alloc - Allocate an object
* @cachep: The cache to allocate from.
* @flags: See kmalloc().
*
* Allocate an object from this cache. The flags are only relevant
* if the cache has no available objects.
*/
void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
{
return __cache_alloc(cachep, flags, __builtin_return_address());
}
上面是关于sysfs虚拟文件系统的分析,与i2c驱动没有太大的关系,但是我们可以看出来i2c的driver和device最后都是挂在总线上的,这些都是可以归纳为:总线---设备-----驱动模型的,我们可以看出来从i2c_add_driver()函数开始内核不光是在建立一个driver同时也在忙着将这个dirver挂在这个总线上,前面贴出的代码描述了这个过程,下面在看看流程:
i2c_add_driver()----------->i2c_register_driver()
|
|
driver->driver.bus = &i2c_bus_type
struct bus_type i2c_bus_type = {
.name = "i2c",
.dev_attrs = i2c_dev_attrs,
.match = i2c_device_match,
.uevent = i2c_device_uevent,
.probe = i2c_device_probe,
.remove = i2c_device_remove,
.shutdown = i2c_device_shutdown,
.suspend = i2c_device_suspend,
.resume = i2c_device_resume,
};
i2c_driver与i2c_client关系:
i2c_driver对应于一套驱动方法,主要成员函数是probe()、remove()、suspend()、resume()。
i2c_client对应于真实的物理设备,每个I2C设备都需要一个i2c_client来描述,i2c_driver与i2c_client的关系是一对多,一个i2c_drvier可以支持多个同类型的i2c_client。
i2c_adpater与i2c_client关系
i2c_adpater与i2c_client的关系与I2C硬件体系中适配器和设备的关系一致,即i2c_client依附于i2c_adapter。一个适配器可以连接多个I2C设备,所以一个i2c_adapter也可以被多个i2c_client依附,i2c_adapter中包括依附于它的i2c_client的链表。
刚开始没搞懂其实i2c_client、i2c_adpater这两个数据结构都是内核抽象出来的便于对多平台的适应,每一个i2c_adpater对应于一条总线,i2c_client每一个对应于一个具体设备。
网上的解释:简单点了, 你的开发板上有几个I2C接口,就有几个adapter , 也就是有几条I2C bus , I2C CLIENT 对应的就是你的外围I2C 设备,有几个就有几个CLIENT , 把这些设备插入开发板, 对应其中的一条BUS, 那么相应的就对应了其中的一个ADAPTER , 接下来的就是 CLIENT 与 ADAPTER 勾搭成对了, 后面就是做该做的事了
在编写I2C驱动的时候我们需要实现两个方面的内容:
1、提供I2C适配器的硬件驱动,探测、初始化I2C适配器、驱动CPU控制的I2C适配器从硬件上产生各种信号及处理I2C中断。
2、提供I2C适配器的算法,用具体适配器的xxx_dfer()函数填充i2c_algorithm的master_xfer指针。
3、对I2C core的接口,必须实现 struct i2c_drvier数据结构中的几个特定的功能函数。这些函数是I2C驱动与I2C总线物理层(I2C控制器)和I2C设备器件之间通信的基础。
4、 对用户应用层的接口,必须实现struct file_operation数据结构中的一些特定功能的函数,如 open ,release , read ,write,lseek等函数。以上两类接口中,对I2C core的接口是对I2C设备访问的基础,实现对I2C总线具体的访问方法;对用户应用层的接口则是方便应用程序开发,实现设备特定功能的必不可少的部分。例如,如果是字符设备,就实现文件操作接口,实现具体设备yyy的yyy_read()、yyy_write()和yyy_ioctl()函数等;如果是声卡,就实现ALSA驱动。 下面的函数流程就是i2c注册的过程,在注册的过程中会把i2c添加到总线上去,同时完成match过程(目前没有弄清楚的流程),难道是在这儿进行了dev和driver的比较?
i2c_add_driver()---------->i2c_register_driver()------------>driver_register()------------------->bus_add_driver()-------->driver_attach()--------->bus_for_each_dev()-------------->__driver_attach()----------------->driver_probe_device()----------------->int (*match)(struct device * dev, struct device_driver * drv)
总结一下I2C驱动的流程:
1、首先,在i2c_client_address_data的normal_i2c属性中定义好我们设备的设备地址。
2、接下来,i2c_driver就出场了,它的功能是定义i2c设备的名字,探测函数,卸载函数三个属性。
3、当程序在入口函数中注册i2c-driver驱动之后,系统就会根据我们第一步中定义的设备地址,调用attach_adapter函数进行匹配设备地址是否支持,在attach_adapter函数中主要的功能是在调用i2c_probe函数,当系统检测到设备地址匹配时,就会进入i2c_probe函数中干一些重要的事,接着就进入i2c-probe传入的at24cxx_detect函数中实现我们自己的事。 其实总结一下就下面一个流程:at24cxx_attach_adapter -> i2c_probe -> at24cxx_detect
当我们卸载设备时,会自动调用i2c_driver中定义的卸载函数at24cxx_detach_adapter进行卸载设备。
最后一步自然就是在出口函数中卸载i2c-driver驱动。
因为I2C只是一种通信的方式,所以在完成前面的工作后我们需要完成我们要在I2C这种通信方式山所做的工作,我们一般在at24cxx_detect()中完成设备register_chrdev()的注册,然后填写read()、write()等函数。
没有弄得很清楚的就是为什么没有注册i2c_add_adapter()---------->i2c_register_adapter(),适配器和device的匹配是在driver中完成的吗?
参考的比较经典的博文:http://www.cnblogs.com/lihaiyan/p/4452875.html
http://blog.csdn.net/chocolate001/article/details/7470873
http://blog.sina.com.cn/s/blog_63f31f340101byb2.html
http://www.embedu.org/Column/Column213.htm