本文共 9230 字,大约阅读时间需要 30 分钟。
I2C核心是内核自带的,一般不需要修改,它提供了I2C总线驱动和设备驱动的注册、注销方法,I2C通信方法上层的、与具体适配器无关的代码以及探测设备、检测设备地址的上层代码等。 adapter驱动是硬件相关的,不同的soc有不同的adapter驱动,它操作IIC控制器,adapter的i2c_algorithm成员实现了一套通信方法,adapter利用i2c_algorithm 中提供的通信函数来控制适配器上产生特定的通信信号,再根据IIC协议就可实现IIC控制器读写IIC设备的读写接口。 当内核提供了I2C核心和adapter(适配器,或IIC控制器,或I2C总线)驱动后,我们要想读写一个外接的IIC设备驱动,只需要提供这个外接设备的IIC设备地址和起个名字即可,利用这个设备地址和名字构造一个i2c_client结构对象,把这个i2c_client挂在adapter总线(I2C总线)上,以后就通过这个与外接IIC设备绑定了的i2c_client结构体来读写这个IIC设备。IIC设备最终以字符设备体现,我们是在IIC驱动的机制下搞字符设备的那套东西,I2C核心负责管理IIC设备和驱动,字符设备才负责具体的有意义的读写,因为adapter驱动虽然实现了读写,但不知道具体数据的含义,在字符设备部分,我们要根据IIC协议,构造有意义的数据进行有意义的读写,这个字符设备的读写,最终还是依赖于adapter驱动提供的函数来操作寄存器。
I2C设备驱动的模型(at24cxx为例):
static unsigned short ignore[] = { I2C_CLIENT_END };static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 地址值是7位 */static struct i2c_client_address_data addr_data = { .normal_i2c = normal_addr, /* 要发出S信号和设备地址并得到ACK信号,才能确定存在这个设备 */ .probe = ignore, .ignore = ignore, //.forces /* 强制认为存在这个设备 */};static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind){ printk("at24cxx_detect\n"); return 0;}static int at24cxx_attach(struct i2c_adapter *adapter){ return i2c_probe(adapter, &addr_data, at24cxx_detect);//设置的addr_data用在这里}static int at24cxx_detach(struct i2c_client *client){ printk("at24cxx_detach\n"); return 0;}/* 1. 分配一个i2c_driver结构体 *//* 2. 设置i2c_driver结构体 */static struct i2c_driver at24cxx_driver = { .driver = { .name = "at24cxx", }, .attach_adapter = at24cxx_attach, .detach_client = at24cxx_detach,};static int at24cxx_init(void){ i2c_add_driver(&at24cxx_driver); return 0;}static void at24cxx_exit(void){ i2c_del_driver(&at24cxx_driver);}module_init(at24cxx_init);module_exit(at24cxx_exit);MODULE_LICENSE("GPL");重点就是搞两个个结构体,和实现两个函数at24cxx_attach()和at24cxx_detect: static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 地址值是7位 */ static struct i2c_client_address_data addr_data = { }; static struct i2c_driver at24cxx_driver = { }; 实现at24cxx_attach(): static int at24cxx_attach(struct i2c_adapter *adapter) { return i2c_probe(adapter, &addr_data, at24cxx_detect);//设置的addr_data用在这里 } i2c_probe()函数会发出信号检测,成功后,会调用它的第三个参数at24cxx_detect,所以还要实现i2c_probe()的第三个参数:at24cxx_detect()函数 在at24cxx_detect函数中,可以干自己喜欢干的事情,如注册字符设备等。 调用的过程: at24cxx_init i2c_add_driver(&at24cxx_driver); at24cxx_driver.attach_adapter,即at24cxx_attach, i2c_probe(adapter, &addr_data, at24cxx_detect);
at24cxx_detect()
上面就是在2.6内核下,及I2C核心和adapter驱动的前提下写IIC设备驱动的机制,我们要实现一个具体的I2C设备的驱动,就在这个机制下写。下面把I2C设备at24cxx作为字符设备来写它的驱动,就是在上面的框架下,在at24cxx_detect()函数里注册字符设备,重点是实现read、write函数和构造i2c_client结构体。
读写这个字符设备,归根结底是要操作到IIC控制器,怎么操作IIC控制器?这就要用到i2c_client结构,这个i2c_client是挂 在adapter上的,I2C核心会给它检测、匹配adapter。 (I2C核心是通过i2c_client结构来管理I2C设备,一个i2c_client对一个一个I2C外设,所以我们要构造一个i2c_client结构对象)static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind){ printk("at24cxx_detect\n"); /* 构构一个i2c_client结构体: 以后收改数据时会用到它 */ at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); at24cxx_client->addr = address; at24cxx_client->adapter = adapter; at24cxx_client->driver = &at24cxx_driver; strcpy(at24cxx_client->name, "at24cxx"); i2c_attach_client(at24cxx_client); major = register_chrdev(0, "at24cxx", &at24cxx_fops); cls = class_create(THIS_MODULE, "at24cxx"); class_device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */ return 0;}其中: at24cxx_client->addr = address; //I2C设备的地址 at24cxx_client->adapter = adapter; //client所在的适配器 //i2c_add_driver的时候会调用到list_for_each_entry(adapter, &adapters, list)找到的一个是配器 at24cxx_client->driver = &at24cxx_driver;//我们自己构造的at24cxx_driver strcpy(at24cxx_client->name, "at24cxx");
i2c_attach_client(at24cxx_client);
利用msg、i2c_client、i2c_transfer()实现字符设备的读写函数read、write,i2c_transfer()最终会操作IIC控制器。msg[]是消息结构数据,每个消息结构含addr,buf,len,flags(读还是写)
这样发送消息:i2c_transfer(at24cxx_client->adapter, msg, 1);//第三个参数是消息个数 把所有消息结构数据构造好后,调用一次i2c_transfer()一次性发送所有消息 下面的字符设备读写函数的实现,一定要搞清楚msg在不同情况下,其各成员的具体含义,以便正确的构造msg。static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset){ unsigned char val[2]; struct i2c_msg msg[1]; int ret; /* address = buf[0] * data = buf[1] */ if (size != 2) return -EINVAL; copy_from_user(val, buf, 2); /* 数据传输三要素: 源,目的,长度 */ msg[0].addr = at24cxx_client->addr; /* 目的,要写的从机地址 */ msg[0].buf = val; /* 源,要写的内容,内容是要读的数据在at24cxx上的存储空间地址 */ msg[0].len = 2; /* 地址+数据=2 byte */ msg[0].flags = 0; /* 表示写 */ ret = i2c_transfer(at24cxx_client->adapter, msg, 1); if (ret == 1) return 2; else return -EIO;}static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset){ unsigned char address; unsigned char data; struct i2c_msg msg[2]; int ret; /* address = buf[0] * data = buf[1] */ if (size != 1) return -EINVAL; copy_from_user(&address, buf, 1); /* 数据传输三要素: 源,目的,长度 */ /* 读AT24CXX时,要先把要读的存储空间的地址发给它 */ msg[0].addr = at24cxx_client->addr; /* 目的,要写的从机地址 */ msg[0].buf = &address; /* 源,要写的内容,内容是要读的数据在at24cxx上的存储空间地址 */ msg[0].len = 1; /* 地址=1 byte */ msg[0].flags = 0; /* 表示写 */ /* 然后启动读操作 */ msg[1].addr = at24cxx_client->addr; /* 源,从机地址,数据地址由msg[0]提供,这里称为源,不太严谨,因为这个源是由从机地址和数据存储地址一起构成 */ msg[1].buf = &data; /* 目的,要读到哪里去 */ msg[1].len = 1; /* 数据=1 byte */ msg[1].flags = I2C_M_RD; /* 表示读 */ ret = i2c_transfer(at24cxx_client->adapter, msg, 2); if (ret == 2) { copy_to_user(buf, &data, 1); return 1; } else return -EIO;}
下面给出at24cxx接在s3c2440的IIC控制器上的驱动的完整的源代码给大家参考,不明白的地方抄一抄,改一改即可:
#include再强调一下,这样的驱动的前提是内核提供了I2C核心和adapter驱动,如果没有提供对应平台上的adapter驱动,就需要我们自己实现adapter驱动,就要根据I2C协议操作寄存器和中断,实现收发函数。#include #include #include #include #include #include #include #include static unsigned short ignore[] = { I2C_CLIENT_END };static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 地址值是7位 */ /* 改为0x60的话, 由于不存在设备地址为0x60的设备, 所以at24cxx_detect不被调用 */static unsigned short force_addr[] = {ANY_I2C_BUS, 0x60, I2C_CLIENT_END};static unsigned short * forces[] = {force_addr, NULL}; static struct i2c_client_address_data addr_data = { .normal_i2c = normal_addr, /* 要发出S信号和设备地址并得到ACK信号,才能确定存在这个设备 */ .probe = ignore, .ignore = ignore, //.forces = forces, /* 强制认为存在这个设备 */};static struct i2c_driver at24cxx_driver;static int major;static struct class *cls;struct i2c_client *at24cxx_client;static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset){ unsigned char address; unsigned char data; struct i2c_msg msg[2]; int ret; /* address = buf[0] * data = buf[1] */ if (size != 1) return -EINVAL; copy_from_user(&address, buf, 1); /* 数据传输三要素: 源,目的,长度 */ /* 读AT24CXX时,要先把要读的存储空间的地址发给它 */ msg[0].addr = at24cxx_client->addr; /* 目的 */ msg[0].buf = &address; /* 源 */ msg[0].len = 1; /* 地址=1 byte */ msg[0].flags = 0; /* 表示写 */ /* 然后启动读操作 */ msg[1].addr = at24cxx_client->addr; /* 源 */ msg[1].buf = &data; /* 目的 */ msg[1].len = 1; /* 数据=1 byte */ msg[1].flags = I2C_M_RD; /* 表示读 */ ret = i2c_transfer(at24cxx_client->adapter, msg, 2); if (ret == 2) { copy_to_user(buf, &data, 1); return 1; } else return -EIO;}static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset){ unsigned char val[2]; struct i2c_msg msg[1]; int ret; /* address = buf[0] * data = buf[1] */ if (size != 2) return -EINVAL; copy_from_user(val, buf, 2); /* 数据传输三要素: 源,目的,长度 */ msg[0].addr = at24cxx_client->addr; /* 目的 */ msg[0].buf = val; /* 源 */ msg[0].len = 2; /* 地址+数据=2 byte */ msg[0].flags = 0; /* 表示写 */ ret = i2c_transfer(at24cxx_client->adapter, msg, 1); if (ret == 1) return 2; else return -EIO;}static struct file_operations at24cxx_fops = { .owner = THIS_MODULE, .read = at24cxx_read, .write = at24cxx_write,};static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind){ printk("at24cxx_detect\n"); /* 构构一个i2c_client结构体: 以后收改数据时会用到它 */ at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); at24cxx_client->addr = address; at24cxx_client->adapter = adapter; at24cxx_client->driver = &at24cxx_driver; strcpy(at24cxx_client->name, "at24cxx"); i2c_attach_client(at24cxx_client); major = register_chrdev(0, "at24cxx", &at24cxx_fops); cls = class_create(THIS_MODULE, "at24cxx"); class_device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */ return 0;}static int at24cxx_attach(struct i2c_adapter *adapter){ return i2c_probe(adapter, &addr_data, at24cxx_detect);}static int at24cxx_detach(struct i2c_client *client){ printk("at24cxx_detach\n"); class_device_destroy(cls, MKDEV(major, 0)); class_destroy(cls); unregister_chrdev(major, "at24cxx"); i2c_detach_client(client); kfree(i2c_get_clientdata(client)); return 0;}/* 1. 分配一个i2c_driver结构体 *//* 2. 设置i2c_driver结构体 */static struct i2c_driver at24cxx_driver = { .driver = { .name = "at24cxx", }, .attach_adapter = at24cxx_attach, .detach_client = at24cxx_detach,};static int at24cxx_init(void){ i2c_add_driver(&at24cxx_driver); return 0;}static void at24cxx_exit(void){ i2c_del_driver(&at24cxx_driver);}module_init(at24cxx_init);module_exit(at24cxx_exit);MODULE_LICENSE("GPL");
转载地址:http://rpkjn.baihongyu.com/