前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >26.3.4.2内核下的I2C驱动(一)

26.3.4.2内核下的I2C驱动(一)

作者头像
嵌入式与Linux那些事
发布2021-05-20 15:47:16
1.1K0
发布2021-05-20 15:47:16
举报

1.硬件协议

1.1mini2440 I2C设备如图所示

在这里插入图片描述
在这里插入图片描述

1.2 I2C协议如下:

在这里插入图片描述
在这里插入图片描述

1.3启动和停止信号如下图

在这里插入图片描述
在这里插入图片描述

1.4 I2C总线数据传输时序:

在这里插入图片描述
在这里插入图片描述

1.5 2440只负责发送,不知道数据含义,只有外设知道。如何写一个字节?参考AT24CXX手册 。

在这里插入图片描述
在这里插入图片描述

注意:I2C设备读之前先写!

在这里插入图片描述
在这里插入图片描述

2.软件框架

2.1总线-设备-驱动模型如下图所示

  把某个结构体,左右的放入链表,一一比较,匹配调用probe函数   设备链表 driver链表, 总线提供了match函数

在这里插入图片描述
在这里插入图片描述

linux-2.6.22.6\Documentation\i2c\instantiating-devices

2.2如何构造I2C_CLIENT?设备的4种构建方法

2.1方法一:

在这里插入图片描述
在这里插入图片描述

mach-mini2440.c

代码语言:javascript
复制
static struct i2c_board_info mini2440_i2c_devs[] __initdata = {
	{
		I2C_BOARD_INFO("24c08", 0x50),//名字:24c08 地址:0x50。知道名字就注册进去,知道地址就知道访问谁
		.platform_data = &at24c08,
	},
};
代码语言:javascript
复制
	i2c_register_board_info(0, mini2440_i2c_devs,
				ARRAY_SIZE(mini2440_i2c_devs));//注册结构体

进入i2c_register_board_info

代码语言:javascript
复制
i2c_register_board_info(int busnum,
	struct i2c_board_info const *info, unsigned len)
{
	int status;

	down_write(&__i2c_board_lock);

	/* 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;

	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_register_board_info结构体放入链表
	}

	up_write(&__i2c_board_lock);

	return status;
}

  什么时候调用链表呢? i2c-core.c

代码语言:javascript
复制
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
{
	struct i2c_devinfo	*devinfo;

	down_read(&__i2c_board_lock);
	list_for_each_entry(devinfo, &__i2c_board_list, list) {//链表里面的每一个成员调用i2c_new_device,构造I2C_CLIENT
		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);
	}
	up_read(&__i2c_board_lock);
}

i2c_new_device.c

代码语言:javascript
复制
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
	struct i2c_client	*client;//定义client
	int			status;

	client = kzalloc(sizeof *client, GFP_KERNEL);
	if (!client)
		return NULL;

	client->adapter = adap;

	client->dev.platform_data = info->platform_data;

	if (info->archdata)
		client->dev.archdata = *info->archdata;

	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);
	if (status) {
		dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
			client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
		goto out_err_silent;
	}

	/* Check for address business */
	status = i2c_check_addr_busy(adap, client->addr);
	if (status)
		goto out_err;
	/*设置*/
	client->dev.parent = &client->adapter->dev;
	client->dev.bus = &i2c_bus_type;
	client->dev.type = &i2c_client_type;
	client->dev.of_node = info->of_node;

	/* For 10-bit clients, add an arbitrary offset to avoid collisions */
	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
		     client->addr | ((client->flags & I2C_CLIENT_TEN)
				     ? 0xa000 : 0));
	status = device_register(&client->dev);//注册
	if (status)
		goto out_err;

	dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
		client->name, dev_name(&client->dev));

	return client;

out_err:
	dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
		"(%d)\n", client->name, client->addr, status);
out_err_silent:
	kfree(client);
	return NULL;
}

  使用限制:必须在 i2c_register_adapter 之前 i2c_register_board_info   所以:不适合我们动态加载insmod

2.2方法二:直接i2c_new_device, i2c_new_probed_device

在这里插入图片描述
在这里插入图片描述

  i2c_new_device, i2c_new_probed_device有什么区别呢?写代码测试下 At24cxx_dev.c

代码语言:javascript
复制
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>


static struct i2c_board_info at24cxx_info = {	
	I2C_BOARD_INFO("at24c08", 0x50),//芯片手册 1010000
};

static struct i2c_client *at24cxx_client;

static int at24cxx_dev_init(void)
{
	struct i2c_adapter *i2c_adap;

	i2c_adap = i2c_get_adapter(0);//获得适配器
	at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
	i2c_put_adapter(i2c_adap);
	
	return 0;
}

static void at24cxx_dev_exit(void)
{
	i2c_unregister_device(at24cxx_client);
}


module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE("GPL");

At24cxx_drv.c

代码语言:javascript
复制
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>


static int __devinit at24cxx_probe(struct i2c_client *client,
				  const struct i2c_device_id *id)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static int __devexit at24cxx_remove(struct i2c_client *client)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static const struct i2c_device_id at24cxx_id_table[] = {
	{ "at24c08", 0 },
	{}
};


/* 1. 分配/设置i2c_driver */
static struct i2c_driver at24cxx_driver = {
	.driver	= {
		.name	= "100ask",
		.owner	= THIS_MODULE,
	},
	.probe		= at24cxx_probe,
	.remove		= __devexit_p(at24cxx_remove),
	.id_table	= at24cxx_id_table,
};

static int at24cxx_drv_init(void)
{
	/* 2. 注册i2c_driver */
	i2c_add_driver(&at24cxx_driver);
	
	return 0;
}

static void at24cxx_drv_exit(void)
{
	i2c_del_driver(&at24cxx_driver);
}


module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");

  编译安装驱动之后,可以正常识别。

在这里插入图片描述
在这里插入图片描述

  将地址改为0X60后,仍然能正常识别

在这里插入图片描述
在这里插入图片描述

二者区别为:    i2c_new_device : 认为设备肯定存在   _new_probed_device :对于"已经识别出来的设备"(probed_device),才会创建(“new”)   i2c_new_probed_device调用:    probe(adap, addr_list[i]) /* 确定设备是否真实存在 */    info->addr = addr_list[i];    i2c_new_device(adap, info);

2.3 第三种 从用户空间实例化

在这里插入图片描述
在这里插入图片描述

实例化过程:   echo at24c08 0x50 > /sys/class/i2c-adapter/i2c-0/new_device

  导致i2c_new_device被调用

  删除设备   echo 0x50 > /sys/class/i2c-adapter/i2c-0/delete_device

  导致i2c_unregister_device 测试:   删除左边的devices,只留下右边的driver

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.4第四种 从I2C总线上分辨出某个设备

在这里插入图片描述
在这里插入图片描述

  前面的3种方法都要事先确定适配器(I2C总线,I2C控制器)    如果我事先并不知道这个I2C设备在哪个适配器上,怎么办?去class表示的所有的适配器上查找    如果适配器上一些I2C设备的地址是一样,怎么继续分辨它是哪一款?用detect函数

代码语言:javascript
复制
static struct i2c_driver at24cxx_driver = {
	.class  = I2C_CLASS_HWMON, /* 表示去哪些适配器上找设备 */
	.driver	= {
		.name	= "100ask",
		.owner	= THIS_MODULE,
	},
	.probe		= at24cxx_probe,
	.remove		= __devexit_p(at24cxx_remove),
	.id_table	= at24cxx_id_table,
	.detect     = at24cxx_detect,  /* 用这个函数来检测设备确实存在 */
	.address_list	= addr_list,   /* 这些设备的地址 */
};

  去"class表示的这一类"I2C适配器,用"detect函数"来确定能否找到"address_list里的设备", 如果能找到就调用i2c_new_device来注册i2c_client, 这会和i2c_driver的id_table比较, 如果匹配,调用probe

At24cxx.c

代码语言:javascript
复制
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>


static int __devinit at24cxx_probe(struct i2c_client *client,
				  const struct i2c_device_id *id)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static int __devexit at24cxx_remove(struct i2c_client *client)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static const struct i2c_device_id at24cxx_id_table[] = {
	{ "at24c08", 0 },
	{}
};

static int at24cxx_detect(struct i2c_client *client,
		       struct i2c_board_info *info)
{
	/* 能运行到这里, 表示该addr的设备是存在的
	 * 但是有些设备单凭地址无法分辨(A芯片的地址是0x50, B芯片的地址也是0x50)
	 * 还需要进一步读写I2C设备来分辨是哪款芯片
	 * detect就是用来进一步分辨这个芯片是哪一款,并且设置info->type
	 */
	
	printk("at24cxx_detect : addr = 0x%x\n", client->addr);

	/* 进一步判断是哪一款 */
	
	strlcpy(info->type, "at24c08", I2C_NAME_SIZE);
	return 0;
}

static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };

/* 1. 分配/设置i2c_driver */
static struct i2c_driver at24cxx_driver = {
	.class  = I2C_CLASS_HWMON, /* 表示去哪些适配器上找设备 */
	.driver	= {
		.name	= "100ask",
		.owner	= THIS_MODULE,
	},
	.probe		= at24cxx_probe,
	.remove		= __devexit_p(at24cxx_remove),
	.id_table	= at24cxx_id_table,
	.detect     = at24cxx_detect,  /* 用这个函数来检测设备确实存在 */
	.address_list	= addr_list,   /* 这些设备的地址 */
};

static int at24cxx_drv_init(void)
{
	/* 2. 注册i2c_driver */
	i2c_add_driver(&at24cxx_driver);
	
	return 0;
}

static void at24cxx_drv_exit(void)
{
	i2c_del_driver(&at24cxx_driver);
}


module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");
在这里插入图片描述
在这里插入图片描述

  probe函数被调用,证明左边devices和右边driver中设备名字相同。我们想搞清楚整个过程,必须 分析i2c_register_driver过程。下面是程序分析的框架: i2c-core.c.c

代码语言:javascript
复制
i2c_add_driver
	i2c_register_driver
		a. at24cxx_driver放入i2c_bus_type的drv链表
		   并且从dev链表里取出能匹配的i2c_client并调用probe
		driver_register
			
		
		b. 对于每一个适 配器,调用__process_new_driver
		   对于每一个适配器,调用它的函数确定address_list里的设备是否存在
		   如果存在,再调用detect进一步确定、设置(因为有些设备地址相同,需要进一步确认,这一类下面的哪一种),然后i2c_new_device
		/* Walk the adapters that are already present */
		i2c_for_each_dev(driver, __process_new_driver);
			__process_new_driver
				i2c_do_add_adapter
					/* Detect supported devices on that bus, and instantiate them */
					i2c_detect(adap, driver);
						for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
							err = i2c_detect_address(temp_client, driver);
										/* 判断这个设备是否存在:简单的发出S信号确定有ACK */
										if (!i2c_default_probe(adapter, addr))
											return 0;
										
										memset(&info, 0, sizeof(struct i2c_board_info));
										info.addr = addr;	
										
										// 设置info.type
										err = driver->detect(temp_client, &info);
					
										i2c_new_device

3.完善设备驱动程序

At24cxx_drv.c

代码语言:javascript
复制
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <asm/uaccess.h>


static int major;
static struct class *class;
static struct i2c_client *at24cxx_client;

/* 传入: buf[0] : addr
 * 输出: buf[0] : data
 */
static ssize_t at24cxx_read(struct file * file, char __user *buf, size_t count, loff_t *off)
{
	unsigned char addr, data;
	
	copy_from_user(&addr, buf, 1);
	data = i2c_smbus_read_byte_data(at24cxx_client, addr);//i2c_smbus_read_byte_data 核心层提供的读写函数,和之前写的I2C驱动不太一样 之前是构造了两个i2c_msg来进行读写
	copy_to_user(buf, &data, 1);
	return 1;
}

/* buf[0] : addr
 * buf[1] : data
 */
static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
{
	unsigned char ker_buf[2];
	unsigned char addr, data;

	copy_from_user(ker_buf, buf, 2);
	addr = ker_buf[0];
	data = ker_buf[1];

	printk("addr = 0x%02x, data = 0x%02x\n", addr, data);

	if (!i2c_smbus_write_byte_data(at24cxx_client, addr, data))
		return 2;
	else
		return -EIO;	
}

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

static int __devinit at24cxx_probe(struct i2c_client *client,
				  const struct i2c_device_id *id)
{
	at24cxx_client = client;
		
	//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	major = register_chrdev(0, "at24cxx", &at24cxx_fops);
	class = class_create(THIS_MODULE, "at24cxx");
	device_create(class, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */
	
	return 0;
}

static int __devexit at24cxx_remove(struct i2c_client *client)
{
	//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	device_destroy(class, MKDEV(major, 0));
	class_destroy(class);
	unregister_chrdev(major, "at24cxx");
		
	return 0;
}

static const struct i2c_device_id at24cxx_id_table[] = {
	{ "at24c08", 0 },
	{}
};


/* 1. 分配/设置i2c_driver */
static struct i2c_driver at24cxx_driver = {
	.driver	= {
		.name	= "100ask",
		.owner	= THIS_MODULE,
	},
	.probe		= at24cxx_probe,
	.remove		= __devexit_p(at24cxx_remove),
	.id_table	= at24cxx_id_table,
};

static int at24cxx_drv_init(void)
{
	/* 2. 注册i2c_driver */
	i2c_add_driver(&at24cxx_driver);
	
	return 0;
}

static void at24cxx_drv_exit(void)
{
	i2c_del_driver(&at24cxx_driver);
}


module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");

At24cxx_dev.c

代码语言:javascript
复制
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>


static struct i2c_board_info at24cxx_info = {	
	I2C_BOARD_INFO("at24c08", 0x50),
};

static struct i2c_client *at24cxx_client;

static int at24cxx_dev_init(void)
{
	struct i2c_adapter *i2c_adap;

	i2c_adap = i2c_get_adapter(0);
	at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
	i2c_put_adapter(i2c_adap);
	
	return 0;
}

static void at24cxx_dev_exit(void)
{
	i2c_unregister_device(at24cxx_client);
}


module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE("GPL");

I2c_test.c

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


/* i2c_test r addr
 * i2c_test w addr val
 */

void print_usage(char *file)
{
	printf("%s r addr\n", file);
	printf("%s w addr val\n", file);
}

int main(int argc, char **argv)
{
	int fd;
	unsigned char buf[2];
	
	if ((argc != 3) && (argc != 4))
	{
		print_usage(argv[0]);
		return -1;
	}

	fd = open("/dev/at24cxx", O_RDWR);
	if (fd < 0)
	{
		printf("can't open /dev/at24cxx\n");
		return -1;
	}

	if (strcmp(argv[1], "r") == 0)
	{
		buf[0] = strtoul(argv[2], NULL, 0);
		read(fd, buf, 1);
		printf("data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);
	}
	else if ((strcmp(argv[1], "w") == 0) && (argc == 4))
	{
		buf[0] = strtoul(argv[2], NULL, 0);
		buf[1] = strtoul(argv[3], NULL, 0);
		if (write(fd, buf, 2) != 2)
			printf("write err, addr = 0x%02x, data = 0x%02x\n", buf[0], buf[1]);
	}
	else
	{
		print_usage(argv[0]);
		return -1;
	}
	
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.硬件协议
    • 1.1mini2440 I2C设备如图所示
      • 1.2 I2C协议如下:
        • 1.3启动和停止信号如下图
          • 1.4 I2C总线数据传输时序:
            • 1.5 2440只负责发送,不知道数据含义,只有外设知道。如何写一个字节?参考AT24CXX手册 。
            • 2.软件框架
              • 2.1总线-设备-驱动模型如下图所示
                • 2.2如何构造I2C_CLIENT?设备的4种构建方法
                  • 2.1方法一:
                    • 2.2方法二:直接i2c_new_device, i2c_new_probed_device
                      • 2.3 第三种 从用户空间实例化
                        • 2.4第四种 从I2C总线上分辨出某个设备
                        • 3.完善设备驱动程序
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档