Linux设备驱动——Linux I2C核心、总线与设备驱动
Linux I2C体系结构
- I2C核心
I2C核心提供了 I2C总线驱动和设备驱动的注册、注销的方法,I2C通信(Algorithm)方法上层的与具体适配器无关代码以及探测设备、检测设备地址的上层代码等。 - I2C总线驱动
是对 I2C 体系结构中适配器端的实现,适配器可由CPU控制,甚至可以直接集成在CPU内部。
总线驱动包含I2C适配器数据结构i2c_adapter
、I2C适配器的Algorithm数据结构i2c_algorithm
和控制I2C适配器产生通信信号的函数 - I2C设备驱动
它是对I2C硬件体系结构中设备端的实现,设备一般挂接在受CPU控制的I2C适配器上,通过I2C适配器与CPU通信.
I2C驱动主要包含数据结构i2c_driver和i2c_client,需要根据的设备实现其中的成员函数。
所有的I2C设备都在sysfs
文件系统中显示,存在/sys/bus/i2c
目录下,以适配器地址和芯片地址的形式列出。
/drivers/i2c/
下的文件介绍
i2c-core.c
:实现了I2C核心的功能以及/proc/bus/i2c*接口
i2c-dev.c
:实现I2C适配器设备文件的功能,每一个适配器被分配一个设备
设配器的主设备号位89,次设备号位:0-255
i2c_dev.c
:并不是根据具体的设备而设计的,只是提供了同用的read()、write()和ioctl()等接口,应用层可以通过这些接口访问挂接在适配器上的I2C设备的存储空间或寄存器,并控制I2C设备的工作方式
busses
文件夹:包含一些I2C主机控制器驱动,如i2c_omap.c、i2c_s3c2440c.等
algos
文件夹:实现了一些I2C总线适配器的通信方法
linux驱动中 i2c驱动架构
架构层次分类
第一层:提供i2c adapter
的硬件驱动,探测、初始化i2c adapter
(如申请i2c的io地址和中断号),驱动soc控制的i2c adapter
在硬件上产生信号(start、stop、ack)以及处理i2c中断。覆盖图中的硬件实现层
第二层:提供i2c adapter
的algorithm,用具体适配器的xxx_xferf()
函数来填充i2c_algorithm
的master_xfer
函数指针,并把赋值后的i2c_algorithm再赋值给
i2c_adapte`r的algo指针。覆盖图中的访问抽象层、i2c核心层
第三层:实现i2c设备驱动中的i2c_driver
接口,用具体的i2c device
设备的attach_adapter()、detach_adapter()
方法赋值给i2c_driver
的成员函数指针。实现设备device与总线(或者叫adapter)的挂接。覆盖图中的driver驱动层
第四层:实现i2c设备所对应的具体device的驱动,i2c_driver
只是实现设备与总线的挂接,而挂接在总线上的设备则是千差万别的,eeprom和ov2715显然不是同一类的device,所以要实现具体设备device的write()、read()、ioctl()等方法,赋值给file_operations,然后注册字符设备(多数是字符设备)。覆盖图中的driver驱动层
第一层和第二层又叫i2c总线驱动(bus),第三第四属于i2c设备驱动(device driver)。在linux驱动架构中,几乎不需要驱动开发人员再添加bus,因为linux内核几乎集成所有总线bus,如usb、pci、i2c等等。
并且总线bus中的【与特定硬件相关的代码】已由芯片提供商编写完成,例如TI davinci平台i2c总线bus与硬件相关的代码在内核目录/drivers/i2c/buses下的i2c-davinci.c源文件中;而三星的s3c-2440平台i2c总线bus为/drivers/i2c/buses/i2c-s3c2410.c
几个重要的结构体
i2c_adapter
对应于物理上的一个适配器,而i2c_algorithm
对应于一套通信方法,i2c_driver
对应于一套驱动方法,i2c_client
对应于真实的物理设备
1. i2c_driver
struct i2c_driver {
int id;
unsigned int class;
int (*attach_adapter)(struct i2c_adapter *);
int (*detach_adapter)(struct i2c_adapter *);
int (*detach_client)(struct i2c_client *);
int (*command)(struct i2c_client *client,unsigned int cmd, void *arg);
struct device_driver driver;
struct list_head list;
};
2.i2c_client
struct i2c_client {
unsigned int flags; /* div., see below */
unsigned short addr; /* chip address - NOTE: 7bit */
/* addresses are stored in the */
/* _LOWER_ 7 bits */
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 */
struct list_head list;
char name[I2C_NAME_SIZE];
struct completion released;
};
3. i2c_adapter
struct i2c_adapter {
struct module *owner;
unsigned int id;
unsigned int class;
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 */
struct mutex bus_lock;
struct mutex clist_lock;
int timeout;
int retries;
struct device dev; /* the adapter device */
struct class_device class_dev; /* the class device */
int nr;
struct list_head clients;
struct list_head list;
char name[I2C_NAME_SIZE];
struct completion dev_released;
struct completion class_dev_released;
};
4. i2c_algorithm
struct i2c_algorithm {
int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,
int num);
int (*slave_send)(struct i2c_adapter *,char*,int);
int (*slave_recv)(struct i2c_adapter *,char*,int);
u32 (*functionality) (struct i2c_adapter *);
};
i2c_adapter、i2c_algorithm、i2c_driver和i2c_client数据结构的作用及其之间的关系:
-
i2c_adapter与i2c_algorithm
i2c_adapter
对应于物理上的一个适配器,而i2c_algorithm
对应于一套通信方法。- 一个
i2c_adapter
需要i2c_algorithm
提供的通信函数来控制适配器产生特定的访问周期。
+i2c_algorithm
中的关键函数master_xfer()
用于产生I2C访问周期需要的start stop ack信号,以i2c_msg
(即I2C消息)为单位发送和接收通信数据。
i2c_msg
:调用驱动中的发送接收函数需要填充该结构体
// I2C Message - used for pure i2c transaction, also from /dev interface struct i2c_msg { __u16 addr; /* slave address */ __u16 flags; __u16 len; /* msg length */ __u8 *buf; /* pointer to msg data */ };
-
i2c_driver与i2c_client
i2c_driver
对应于一套驱动方法,struct i2c_device_id
形式的id_table
是该驱动所支持的I2C设备的ID表。i2c_client
对应于真实的物理设备,每个I2C设备都需一个i2c_client
来描述- 一个
i2c_driver
支持多个同类型的i2c_client
。
+i2c_client
的信息通常在BSP的板文件中通过i2c_board_info
填充,包括设备的ID号、地址、中断号等信息。 - 在I2C总线驱动
i2c_bus_type
的match()
函数i2c_device_match()
中,会调用i2c_match_id()
函数匹配在板文件中定义的ID和i2c_driver所支持的ID表。
-
i2c_adapter与i2c_client
- 其关系与I2C设备体系中适配器与设备的关系一致,即
i2c_client
依附于i2c_adapter
。 - 一个
i2c_adapter
可以被多个i2c_client
依附,i2c_adapter
中包含有依附它的i2c_client
的链表。
- 其关系与I2C设备体系中适配器与设备的关系一致,即
-
同一类的i2c设备device对应一个驱动driver。driver与device的关系是一对多的关系。
linux内核对i2c架构抽象了一个叫核心层core的中间件,它分离了设备驱动device driver和硬件控制的实现细节(如操作i2c的寄存器),core层不但为上面的设备驱动提供封装后的内核注册函数,而且还为下面的硬件时间提供注册接口(也就是i2c总线注册接口),可以说core层起到了承上启下的作用。
I2C核心
I2C核心提供了 I2C总线驱动和设备驱动的注册、注销的方法。
增加、删除 i2c_adapter
int i2c_add_adapter(struct i2c_adapter *adap)
{
...
device_register(&adap->dev);
device_create_file(&adap->dev, &dev_attr_name);
...
/* inform drivers of new adapters */
list_for_each(item,&drivers) {
driver = list_entry(item, struct i2c_driver, list);
if (driver->attach_adapter)
/* We ignore the return code; if it fails, too bad */
driver->attach_adapter(adap);
}
...
}
int i2c_del_adapter(struct i2c_adapter *adap)
{
...
list_for_each(item,&drivers) {
driver = list_entry(item, struct i2c_driver, list);
if (driver->detach_adapter)
if ((res = driver->detach_adapter(adap))) {
}
}
...
list_for_each_safe(item, _n, &adap->clients) {
client = list_entry(item, struct i2c_client, list);
if ((res=client->driver->detach_client(client))) {
}
}
...
device_remove_file(&adap->dev, &dev_attr_name);
device_unregister(&adap->dev);
}
增加、删除i2c_driver
int i2c_register_driver(struct module *owner, struct i2c_driver *driver);
int i2c_del_driver(struct i2c_driver *driver);
inline int i2c_add_driver(struct i2c_driver *driver);
i2c_client
依附/脱离
当一个具体的client被侦测到并被关联的时候,设备和sysfs文件将被注册。相反的,在client被取消关联的时候,sysfs文件和设备也被注销,驱动开发人员需开发i2c设备驱动时,需要调用下列函数:
int i2c_attach_client(struct i2c_client *client)
{
...
device_register(&client->dev);
device_create_file(&client->dev, &dev_attr_client_name);
...
return 0;
}
int i2c_detach_client(struct i2c_client *client)
{
...
device_remove_file(&client->dev, &dev_attr_client_name);
device_unregister(&client->dev);
...
return res;
}
I2C传输、发送和接收
i2c_transfer()
函数本身并不具备驱动适配器物理硬件完成消息交互的能力,它只是寻找到i2c_adapter
对应的i2c_algorithm
,并使用i2c_algorithm
的master_xfer()
函数真正的驱动硬件流程。
int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num);
int i2c_master_send(struct i2c_client *client,const char *buf ,int count);
int i2c_master_recv(struct i2c_client *client, char *buf ,int count);
int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
{
int ret;
if (adap->algo->master_xfer) {//如果master_xfer函数存在,则调用,否则返回错误
ret = adap->algo->master_xfer(adap,msgs,num);//这个函数在硬件相关的代码中给algorithm赋值
return ret;
} else {
return -ENOSYS;
}
}
代码调用层次图
i2c总线驱动:实现与硬件相关功能的代码集合,这部分是与平台相关的,这部分代码是这样添加到系统中的:
static struct platform_driver davinci_i2c_driver = {
.probe = davinci_i2c_probe,
.remove = davinci_i2c_remove,
.driver = {
.name = "i2c_davinci",
.owner = THIS_MODULE,
},
};
/* I2C may be needed to bring up other drivers */
static int __init davinci_i2c_init_driver(void)
{
return platform_driver_register(&davinci_i2c_driver);
}
subsys_initcall(davinci_i2c_init_driver);
static void __exit davinci_i2c_exit_driver(void)
{
platform_driver_unregister(&davinci_i2c_driver);
}
module_exit(davinci_i2c_exit_driver);
i2c适配器控制硬件发送接收数据的函数在这里赋值给i2c-algorithm
,i2c_davinci_xfer
稍加修改就可以在裸机中控制i2c适配器
static struct i2c_algorithm i2c_davinci_algo = {
.master_xfer = i2c_davinci_xfer,
.functionality = i2c_davinci_func,
};
在davinci_i2c_probe
函数中,将i2c_davinci_algo
添加到添加到algorithm系统中。
adap->algo = &i2c_davinci_algo;
I2C设备
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#ifdef CONFIG_HAS_EARLYSUSPEND
#include <linux/earlysuspend.h>
#endif
#include <linux/regulator/consumer.h>
#include <mach/gpio.h>
#include <plat/gpio-cfg.h>
#include <asm/uaccess.h>
#include <linux/miscdevice.h>
#define MAJOR_NUM 0
#define MINOR_NUM 0
static dev_t dev_no;
static cdev i2c_cdev;
static struct class *i2c_test_class;
static struct class_device *i2c_test_class_device;
static struct i2c_client *this_client;
static int i2c_tes_read_reg(struct i2c_client *client,u8 addr, u8 *pdata) {
u8 buf1[4] = { 0 };
u8 buf2[4] = { 0 };
struct i2c_msg msgs[] = {
{
.addr = client->addr, //0x38
.flags = 0, //写
.len = 1, //要写的数据的长度
.buf = buf1,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.len = 1,
.buf = buf2,
},
};
int ret;
buf1[0] = addr;
ret = i2c_transfer(client->adapter, msgs, 2);
if (ret < 0) {
pr_err("read reg (0x%02x) error, %d\n", addr, ret);
} else {
*pdata = buf2[0];
}
return ret;
}
static int i2c_tes_read_fw_reg(struct i2c_client *client,unsigned char *val)
{
int ret;
*val = 0xff;
ret = i2c_tes_read_reg(client,0xa6, val);
printk("ts reg 0xa6 val is %d\n",*val);
return ret;
}
static int i2c_open_func(struct file *filp)
{
return 0;
}
static int i2c_release_func(struct file *filp)
{
return 0;
}
static ssize_t i2c_read_func(struct file *filp, char __user *buffer, size_t count, loff_t *ppos){
int ret;
u8 reg_data;
ret = copy_from_user(®_data,buffer,1);
struct i2c_msg msgs[] = {
{
.addr = this_client->addr, //0x38
.flags = 0, //写
.len = 1, //要写的数据的长度
.buf = ®_data,
},
{
.addr = this_client->addr,
.flags = I2C_M_RD,
.len = 1,
.buf = ®_data,
},
};
ret = i2c_transfer(this_client->adapter, msgs, 2);
if (ret < 0) {
pr_err("read reg error!\n");
}
ret = copy_to_user(buffer,®_data,1);
return ret;
}
static ssize_t i2c_write_func(struct file *filp, char __user *buffer, size_t count, loff_t *ppos){
int ret;
u8 buf[2];
struct i2c_msg msgs[1];
ret = copy_from_user(&buf,buffer,2);
msgs[0].addr = this_client->addr; //0x38
msgs[0].flags = 0; //写
msgs[0].len = 2; //第一个是要写的寄存器地址,第二个是要写的内容
msgs[0].buf = buf;
ret = i2c_transfer(this_client->adapter, msgs, 1);
if (ret < 0) {
pr_err("write reg 0x%02x error!\n",buf[0]);
}
ret = copy_to_user(buffer,buf,1);
return ret;
}
//file_operation结构体
static struct file_operations i2c_ops = {
.owner = THIS_MODULE,
.open = i2c_open_func,
.release= i2c_release_func,
.write = i2c_write_func,
.read = i2c_read_func,
};
/*
static struct miscdevice i2c_dev = {
.minor = MISC_DYNAMIC_MINOR,
.fops = &i2c_ops,
.name = "i2c_control",
};
*/
static int i2c_test_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
unsigned char val;
printk("==%s:\n", __FUNCTION__);
i2c_tes_read_fw_reg(client,&val);
this_client = client;
//misc_register(&i2c_dev);
alloc_chrdev_region(&dev_no, 0, 1, "i2c_test");
cdev.init(&dev_no, &i2c_ops);
dev.owner = THIS_MODULE;
dev.ops = &fops;
cdv.add(&i2c_cdev, &dev_no, 1);
i2c_test_class = class_create(THIS_MODULE, "my_class");
i2c_test_class_device = device_create(my_class,NULL,dev_no,NULL,"yf's test")
return 0;
}
static int __devexit i2c_test_remove(struct i2c_client *client)
{
i2c_set_clientdata(client, NULL);
//misc_deregister(&i2c_dev);
device_destroy(i2c_test_class);
class_destroy(i2c_test_class,1);
cdev_del(&i2c_cdev);
unregister_chrdev_region(&dev_no, 1);
printk("==%s:\n", __FUNCTION__);
return 0;
}
static const struct i2c_device_id i2c_test_id[] = {
{ "i2c_test", 0 },
{ }
};
static struct i2c_driver i2c_test_driver = {
.probe = i2c_test_probe,
.remove = __devexit_p(i2c_test_remove),
.id_table = i2c_test_id,
.driver = {
.name = "i2c_test",
.owner = THIS_MODULE,
},
};
static void i2c_io_init(void)
{
int ret;
ret = gpio_request(EXYNOS4_GPL0(2), "TP1_EN");
if (ret) {
printk(KERN_ERR "failed to request TP1_EN for "
"I2C control\n");
//return err;
}
gpio_direction_output(EXYNOS4_GPL0(2), 1);
s3c_gpio_cfgpin(EXYNOS4_GPL0(2), S3C_GPIO_OUTPUT);
gpio_free(EXYNOS4_GPL0(2));
mdelay(5);
ret = gpio_request(EXYNOS4_GPX0(3), "GPX0_3");
if (ret) {
gpio_free(EXYNOS4_GPX0(3));
ret = gpio_request(EXYNOS4_GPX0(3), "GPX0_3");
if(ret)
{
printk("ft5xox: Failed to request GPX0_3 \n");
}
}
gpio_direction_output(EXYNOS4_GPX0(3), 0);
mdelay(200);
gpio_direction_output(EXYNOS4_GPX0(3), 1);
s3c_gpio_cfgpin(EXYNOS4_GPX0(3), S3C_GPIO_OUTPUT);
gpio_free(EXYNOS4_GPX0(3));
msleep(300);
}
static int __init i2c_test_init(void)
{
printk("==%s:\n", __FUNCTION__);
i2c_io_init();
printk("==%s:\n", __FUNCTION__);
return i2c_add_driver(&i2c_test_driver);
}
static void __exit i2c_test_exit(void)
{
printk("==%s:\n", __FUNCTION__);
i2c_del_driver(&i2c_test_driver);
}
late_initcall(i2c_test_init);
module_exit(i2c_test_exit);
MODULE_AUTHOR("xunwei_rty");
MODULE_DESCRIPTION("TsI2CTest");
MODULE_LICENSE("GPL");