您的位置:首页 > 运维架构 > Linux

【Linux驱动】I2C驱动编写要点

2015-10-04 15:16 519 查看
继续上一篇博文没讲完的内容“针对 RepStart 型i2c设备的驱动模型”,其中涉及的内容有:i2c_client 的注册、i2c_driver 的注册、驱动程序的编写。

一、i2c 设备的注册分析:在新版本内核的i2c驱动模型中,支持多种方式来注册 i2c 设备,在Documentation/i2c/instantiating-devices文件中有讲到,在内核中对应的抽象数据结构就是 struct i2c_client。

(1)Declare the I2C devices by bus number 以i2c总线号来声明设备:主要适用于嵌入式系统设备,系统外的设备比较固定。

通过 struct i2c_board_info 结构来声明,使用 i2c_register_board_info() 函数来注册。

//在include/linux/i2c.h中
struct i2c_board_info {
char              type[I2C_NAME_SIZE]; //设备名称
unsigned short    flags;
unsigned short    addr;  //设备地址
void              *platform_data;
struct dev_archdata     *archdata;
struct device_node      *of_node;
struct acpi_dev_node acpi_node;
int       irq;
};

//在drivers/i2c/i2c-boardinfo.c中
LIST_HEAD(__i2c_board_list);
EXPORT_SYMBOL_GPL(__i2c_board_list);

int __init i2c_register_board_info(int busnum,struct i2c_board_info const *info, unsigned len)
{
int status;

/* dynamic bus numbers will be assigned after the last static one */
if (busnum >= __i2c_first_dynamic_bus_num)
__i2c_first_dynamic_bus_num = busnum + 1;
//__i2c_first_dynamic_bus_num是个全局变量,初始值为0,因此,他的值始终比busnum大1!这一点可以解决上一篇博文中提出的一个问题。

for (status = 0; len; len--, info++) {
struct i2c_devinfo    *devinfo;

devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
if (!devinfo) {
pr_debug("i2c-core: can't register boardinfo!\n");
status = -ENOMEM;
break;
}

devinfo->busnum = busnum;
devinfo->board_info = *info;
list_add_tail(&devinfo->list, &__i2c_board_list);//将这个 i2c_board_info 添加到该总线的 __i2c_board_list i2c设备链表中
}
return status;
}


问题思考:board_list 链表结构是怎样的?是一条 i2c总线一条链表还是类似于哈希链表?
寻找答案:答案是一条i2c设备链表,里边链着的是所有i2c总线上的设备,分析i2c-core.c中的函数:

i2c_scan_static_board_info(struct i2c_adapter *adapter)
list_for_each_entry(devinfo, &__i2c_board_list, list) {
if (devinfo->busnum == adapter->nr && !i2c_new_device(adapter,&devinfo->board_info))
dev_err(&adapter->dev,"Can't create device at 0x%02x\n",devinfo->board_info.addr);
}
//遍历__i2c_board_list这整个链表,比较其中每一个结点的 busnum 与 adapter->nr是否相等


看看这种方法的具体应用:以 at24cxx 为例来分析。

//在arch/arm/mach-s5pv210/mach-smdkv210.c中
static struct i2c_board_info smdkv210_i2c_devs0[] __initdata = {
{ I2C_BOARD_INFO("tq210-at24cxx", 0x50), }, /* 目标i2c外设 */
{ I2C_BOARD_INFO("wm8580", 0x1b), },
};
smdkv210_machine_init()
{
...
i2c_register_board_info(0, smdkv210_i2c_devs0,ARRAY_SIZE(smdkv210_i2c_devs0));//注册i2c-0总线上的i2c设备到设备链表
i2c_register_board_info(1, smdkv210_i2c_devs1,ARRAY_SIZE(smdkv210_i2c_devs1));//注册i2c-1总线上的i2c设备到设备链表
i2c_register_board_info(2, smdkv210_i2c_devs2,ARRAY_SIZE(smdkv210_i2c_devs2));//注册i2c-2总线上的i2c设备到设备链表
...
}


我们在前一篇博文中分析platform_driver.probe函数(s3c24xx_i2c_probe)的时候得知函数的调用关系是:

i2c_add_numbered_adapter -> i2c_register_adapter -> i2c_scan_static_board_info -> i2c_new_device,即在i2c_adapter注册好之后内核会去尝试从__i2c_board_list链表中搜索匹配的i2c设备。这种方法有个缺陷就是:必须在调用 i2c_register_adapter 来注册 i2c_adapter 之前就把i2c设备链表注册好,因此,不适合使用 insmod 来动态注册i2c设备。

(2)Instantiate the devices explicitly 立即检测 i2c设备:

这种方法就是“认为i2c设备一定肯定存在”后直接使用 i2c_new_device函数来注册i2c_client。看看怎样做,再来详细跟进这个函数:

struct i2c_client *i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client    *client;
int            status;

client = kzalloc(sizeof *client, GFP_KERNEL);

// 设置这个client
client->adapter = adap;
client->dev.platform_data = info->platform_data;
......
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
strlcpy(client->name, info->type, sizeof(client->name));
/* Check for address validity */
status = i2c_check_client_addr_validity(client); // 地址合法性
......
/* Check for address business */
status = i2c_check_addr_busy(adap, client->addr);// 地址是否被复用
.....
client->dev.bus = &i2c_bus_type;
client->dev.type = &i2c_client_type;
.....
status = device_register(&client->dev); //注册i2c_client设备
......
return client; //返回i2c_client结构,方便在字符设备编程中的使用!!!
......
}


但是使用这个函数前从其参数可以发现需要准备一些材料:i2c_adapter 和 i2c_board_list ,i2c_adapter 需要用核心层的 i2c_get_adapter() 函数来获得, i2c_board_list 的话自己现场构造一个。

其实到这里就可以看穿其真实面目:把之前放在注册 i2c_adapter之后的任务:遍历 i2c_board_list 链表来注册 i2c_client 版移到这里来实现,这样就可以使用 “insmod” 来动态装载i2c设备了。

(3)Probe an I2C bus for certain devices 通过probe来探测确定在i2c总线上的设备:

使用核心层的 i2c_new_probed_device函数来实现:

i2c_new_probed_device(struct i2c_adapter *adap,struct i2c_board_info *info,unsigned short const *addr_list,int (*probe)(struct i2c_adapter *, unsigned short addr))
{
if (!probe)
probe = i2c_default_probe;
......
/* Test address responsiveness */
for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
......
if (probe(adap, addr_list[i])) //真实的发地址去探测
break; //成功就跳出for循环往后进行注册i2c设备
}
if (addr_list[i] == I2C_CLIENT_END) { //看看是不是探测地址用完了,用完了函数就返回,不往下注册设备。
dev_dbg(&adap->dev, "Probing failed, no device found\n");
return NULL;
}
info->addr = addr_list[i];
return i2c_new_device(adap, info);//注册设备:创建 i2c_client 并注册
}


其实这种方法是2.6或之前的内核做法,传闻说这种探测机制会有副作用不建议使用,具体什么副作用就不知道了。

(4)Instantiate from user-space 从用户空间来创建i2c设备:

这种方法最最......好吧,竟然找不到一个词汇来形容对它的这种感觉。

直接使用指令来完成:

echo eeprom 0x50 > /sys/bus/i2c/devices/i2c-0/new_device

大体原理可以意会到一些,具体的原理可以看看内核文档的介绍。

二、i2c 设备驱动的分析和实例编写

不管上边的哪一种方法注册 i2c_client ,对我的i2c设备驱动的设计都没有太大的出入。下边给出是在第一种方式下注册的 i2c_client 时的驱动编写,分析的内容已经容纳在程序的注释中。

#include <linux/module.h>
#include <linux/input.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <plat/gpio-cfg.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/device.h>
#include <linux/notifier.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

static int major;
static struct class    *cls;
static struct device *i2c_dev;
struct i2c_client       *at24cxx_client ;

static unsigned write_timeout = 25;

int at24cxx_open(struct inode *inode, struct file *file)
{
printk("at24cxx_open\n");
return 0;
}

static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset)
{
unsigned char    address;
unsigned char    data;
int ret;
struct i2c_msg msg[2];

unsigned long timeout, read_time;

/* address = buf[0]
* data    = buf[1]
*/
if (size != 1)
return -EINVAL;

ret = copy_from_user(&address, buf, 1);
printk("read addr:%d\n",address);
/* 数据传输三要素: 源,目的,长度 */
/* 读AT24CXX时,要先把要读的存储空间的地址发给它 */
msg[0].addr  = at24cxx_client->addr;  /* 目的 */
msg[0].buf   = &address;                         /* 读的源头 */
msg[0].len   = 1;                                     /* 地址=1 byte */
//msg[0].flags = at24cxx_client->flags & I2C_M_TEN; /* 表示写 */

/* 然后启动读操作 */
msg[1].addr  = at24cxx_client->addr;  /* 源 */
msg[1].buf   = &data;                              /* 目的 */
msg[1].len   = 1;                                      /* 数据=1 byte */
msg[1].flags = at24cxx_client->flags & I2C_M_TEN;
msg[1].flags |= I2C_M_RD;                     /* 表示读 */

timeout = jiffies + msecs_to_jiffies(write_timeout);
do{
read_time = jiffies;
ret = i2c_transfer(at24cxx_client->adapter, msg, 2);
msleep(1);
} while (time_before(read_time, timeout));
printk("ret=%d\n",ret);
if (ret  >= 0)
{
ret = copy_to_user(buf, &data, 1);
return ret;
}
else
return -1;

}

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;
printk("at24cxx_write\n");
ret = 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 = at24cxx_client->flags & I2C_M_TEN;                     /* 表示写:i2c_transfer函数就知道将buf[0]当做写地址,后边的是写地址 */

ret = i2c_transfer(at24cxx_client->adapter, msg, 1);
if (ret == 1)
return 2;
else
return -EIO;
}

long at24cxx__ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

return 0;
}

static struct file_operations at24cxx_fops = {
.owner = THIS_MODULE,
.open   = at24cxx_open,
.read    = at24cxx_read,
.write   = at24cxx_write,
.unlocked_ioctl = at24cxx__ioctl,
};

static int at24cxx_probe(struct i2c_client *client, const struct i2c_device_id *dev_id)
{
printk("at24cxx_probe\n");

/* 注册字符设备驱动:通用模型中这个任务是放在xxx_init()函数来实现 */
major = register_chrdev(0, "at24cxx", &at24cxx_fops);

cls = class_create(THIS_MODULE, "at24cxx");
i2c_dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx");  /* /dev/at24cxx */

/* 拽住要操作的i2c_client,要它成为读写的操作对象 */
at24cxx_client = client;

return 0;
}

int at24cxx_remove(struct i2c_client *client)
{
device_unregister(i2c_dev);
class_destroy(cls);
unregister_chrdev(major, "at24cxx");

return 0;
}

static const struct i2c_device_id at24cxx_id[] = {
{ "tq210-at24cxx", 0 },   /* i2c设备的设备名:这个驱动根据这个名在i2c总线中来找匹配的client */
{ }
};

static struct i2c_driver at24cxx_driver = {
.driver = {
.name= "at24cxx",
.owner = THIS_MODULE,
},
.probe    = at24cxx_probe,    /* 当有i2c_client和i2c_driver匹配时调用 */
.remove = at24cxx_remove, /* 注销时调用 */
.id_table = at24cxx_id,             /* 匹配规则 */
};

static int at24cxx_init(void)
{
printk("at24cxx_init\n");
i2c_add_driver(&at24cxx_driver);
return 0;
}

static void at24cxx_exit(void)
{
printk("at24cxx_exit\n");
i2c_del_driver(&at24cxx_driver);
}

module_init(at24cxx_init);
module_exit(at24cxx_exit);
MODULE_LICENSE("GPL");


应用层测试程序:

#include <stdio.h>
#include <linux/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>

int main()
{
int  fd;
unsigned char wr_buf[2];
unsigned char rd_buf;
int ret;

fd = open("/dev/at24cxx",O_RDWR);
if(!fd)
{
printf("open error.\n");
return -1;
}
wr_buf[0] = 0x10;//wr_addr
wr_buf[1] = 0x66;//wr_val
ret = write(fd,wr_buf,2); //2bytes
printf("write %dbytes.\n",ret);

rd_buf = 0x10;//rd_addr
ret = read(fd,&rd_buf,1); // rd_val -> rd_buf[0]
printf("read addr:0x00 -> data:%d\n",rd_buf);
close(fd);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: