• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Linux驱动开发:IIC驱动实验完整教程

武飞扬头像
拼个世界给自己
帮助1

本实验基于正点原子ALPHT开发板上的AP3216C作为实验开展对象

基础知识
1.IIC总线驱动
  IIC总线驱动是对IIC硬件体系结构中适配器端的实现,适配器可由CPU控制,甚至可以直接集成在CPU内部。IIC适配器在内核中使用了i2c_adapter结构体,IIC适配器核心就是申请一个i2c_adapter结构体,然后设置 i2c_algorithm 中的 master_xfer 函数。
(1)i2c_adapter和 i2c_algorithm
i2c_adapter对应物理上的一个适配器,而 i2c_algorithm对应一套通信方法。它们两个缺少哪一个都是什么都做不了。

2.IIC设备驱动
  IIC设备驱动是对IIC硬件体系结构中设备端的实现,设备一般挂载在受CPU控制的IIC适配器上,通过IIC适配器与CPU交换数据。IIC设备驱动重点关注两个数据结构:i2c_client和i2c_driver。注意,i2c_client是不需要我们编写的,但是i2c_driver是重点,是需要我们去编写的,i2c_driver类似platform平台,也存在probe函数。
i2c_client和i2c_driver定义如下:
(1)i2c_client和i2c_driver
i2c_driver对应一套驱动方法,主要成员有probe、remove、suspend等;而i2c_client的信息通常在BSP的文件中通过i2c_board_info填充。

struct i2c_client {
	unsigned short flags; /* 标志 */
	unsigned short addr; /* 芯片地址, 7 位,存在低 7 位*/
	char name[I2C_NAME_SIZE]; /* 名字 */
	struct i2c_adapter *adapter; /* 对应的 I2C 适配器 */
	struct device dev; /* 设备结构体 */
	int irq; /* 中断 */
	struct list_head detected;
..
};


struct i2c_driver {
	unsigned int class;
	int (*attach_adapter)(struct i2c_adapter *) __deprecated;
	int (*probe)(struct i2c_client *, const struct i2c_device_id *);
	int (*remove)(struct i2c_client *);
	void (*shutdown)(struct i2c_client *);
	void (*alert)(struct i2/
	int (*command)(struct i2c_client *client, unsigned int cmd,
	void *arg);
	struct device_driver driver;
	const struct i2c_device_id *id_table;
	int (*detect)(struct i2c_client *, struct i2c_board_info *);
	const unsigned short *address_list;
	struct list_head clients;
};

学新通

驱动编写过程:


1.修改设备树,如修改IO结点、添加AP3216C设备结点
修改IO

		pinctrl_i2c1: i2c1grp {
			fsl,pins = <
				MX6UL_PAD_UART4_TX_DATA__I2C1_SCL 0x4001b8b0
				MX6UL_PAD_UART4_RX_DATA__I2C1_SDA 0x4001b8b0
			>;
		};

添加AP3216C设备结点

&i2c1 {
	clock-frequency = <100000>;
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_i2c1>;
	status = "okay";

	ap3216c@1e {
		compatible = "alientek,ap3216c";
		reg = <0x1e>;
	};

};

2.新建一个头文件,命名为ap3216creg.h,目的是存放寄存器,这里先创建好,暂时为空

#ifndef AP3216C_H
#define AP3216C_H



#endif 

3.新建一个ap3216c.c文件,用来编写驱动代码。这里我直接把基本框架放上来,有需要的可以直接拿去用,并且不用担心头文件缺失。

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>


/*驱动入口函数*/
static int __init ap3216c_init(void)
{
	return 0;
}

/*驱动出口函数*/
static void __exit ap3216c_exit(void)
{

}

module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kong");

学新通

4.添加i2c_driver结构体,并且在驱动入口函数中用i2c_add_driver注册该结构体,在驱动出口函数中用i2c_del_driver注销该结构体

/*i2c_driver*/
static struct i2c_driver ap3216c_driver = {

};

/*驱动入口函数*/
static int __init ap3216c_init(void)
{
	int ret =0;

	ret = i2c_add_driver(&ap3216c_driver);




	return ret;
}

/*驱动出口函数*/
static void __exit ap3216c_exit(void)
{
	i2c_del_driver(&ap3216c_driver);

}
学新通

5.完善i2c_driver结构体。其中的.of_match_table和.id_table在后面编写

/*i2c_driver*/
static struct i2c_driver ap3216c_driver = {
	.probe = ap3216c_probe,
	.remove = ap3216c_remove,
	.driver = {
		.name = "ap3216c",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(ap3216c_of_match),
	},
	.id_table = ap3216c_id,
};

6.建立ap3216c_of_match和ap3216c_id和probe函数和remove函数函数里面内容暂时不填写

/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	return 0;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{
	return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {
	{"alientek,ap3216c", 0},  //匹配名字
	{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {
	{.compatible = "alientek,ap3216c"},
	{}
};
学新通

7.完整的框架基本就构建好了,完整框架如下

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>

/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	return 0;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{
	return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {
	{"alientek,ap3216c", 0},  //匹配名字
	{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {
	{.compatible = "alientek,ap3216c"},
	{}
};

/*i2c_driver*/
static struct i2c_driver ap3216c_driver = {
	.probe = ap3216c_probe,
	.remove = ap3216c_remove,
	.driver = {
		.name = "ap3216c",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(ap3216c_of_match),
	},
	.id_table = ap3216c_id,
};

/*驱动入口函数*/
static int __init ap3216c_init(void)
{
	int ret =0;

	ret = i2c_add_driver(&ap3216c_driver);




	return ret;
}

/*驱动出口函数*/
static void __exit ap3216c_exit(void)
{
	i2c_del_driver(&ap3216c_driver);

}

module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kong");
学新通

8.继续完善probe函数,remove函数、也就是在probe函数和remove函数里面构建一套完整的字符设备驱动框架来实现i2c功能。
这里添加了一整套完整的字符设备驱动框架。

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>

#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"

struct ap3216c_dev {
	int major;
	int minor;
	dev_t devid;
	struct cdev cdev;
	struct class *class;
	struct device *device;
};

static struct ap3216c_dev ap3216cdev;

static int ap3216c_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &ap3216cdev;
	printk("open\r\n");
	return 0;
}
ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
	return 0;
}
static int ap3216c_release(struct inode *inode, struct file *filp)
{
	struct ap3216c_dev *dev = (struct ap3216cdev_dev*)filp->private_data;
	printk("release\r\n");
	return 0;
}


static const struct file_operations ap3216c_fops = {
	.owner   = THIS_MODULE,
	.open    = ap3216c_open,
	.read   = ap3216c_read,
	.release = ap3216c_release,
};
/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int ret = 0;
	printk("OKOKOKOK!!!!\r\n");
	ap3216cdev.major = 0; //由系统分配主设备号
	/*搭建字符设备驱动框架,在/dev*/
	if(ap3216cdev.major){
		ap3216cdev.devid = MKDEV(ap3216cdev.major, 0);
		ret = register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME);
	}else{
		ret = alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);
		ap3216cdev.major = MAJOR(ap3216cdev.devid);
		ap3216cdev.minor = MINOR(ap3216cdev.devid);
	}
	if(ret < 0){
		printk("ap3216cdev chrdev_region err!\r\n");
		goto fail_devid;
	}
	printk("ap3216cdev major=%d, minor=%d", ap3216cdev.major, ap3216cdev.minor);

	ap3216cdev.cdev.owner = THIS_MODULE;
	cdev_init(&ap3216cdev.cdev, &ap3216c_fops);
	ret = cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT);
	if(ret < 0){
		goto fail_cdev;
	}

	ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME);
	if(IS_ERR(ap3216cdev.class)){
		ret = PTR_ERR(ap3216cdev.class);
		goto fail_class;
	}

	ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid, NULL, AP3216C_NAME);
	if(IS_ERR(ap3216cdev.device)){
		ret = PTR_ERR(ap3216cdev.device);
		goto fail_device;
	}
	return 0;

fail_device:
	class_destroy(ap3216cdev.class);
fail_class:
	cdev_del(&ap3216cdev.cdev);
fail_cdev:
	unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);
fail_devid:
	return ret;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{
	cdev_del(&ap3216cdev.cdev);

	unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);

	device_destroy(ap3216cdev.class, ap3216cdev.devid);

	class_destroy(ap3216cdev.class);


	return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {
	{"alientek,ap3216c", 0},  //匹配名字
	{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {
	{.compatible = "alientek,ap3216c"},
	{}
};

/*i2c_driver*/
static struct i2c_driver ap3216c_driver = {
	.probe = ap3216c_probe,
	.remove = ap3216c_remove,
	.driver = {
		.name = "ap3216c",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(ap3216c_of_match),
	},
	.id_table = ap3216c_id,
};

/*驱动入口函数*/
static int __init ap3216c_init(void)
{
	int ret =0;

	ret = i2c_add_driver(&ap3216c_driver);




	return ret;
}

/*驱动出口函数*/
static void __exit ap3216c_exit(void)
{
	i2c_del_driver(&ap3216c_driver);

}

module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kong");


学新通

9.到这里,可以先进行make编译一下,并且执行应用程序,调试open、read、release、是否正确
注意,这里是应用程序如下:

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "linux/ioctl.h"
#include "linux/input.h"
/* ./keyinputApp /dev/input/event2*/

/*input_event结构体变量*/
static struct input_event inputevent;

int main(int argc, char *argv[])
{
	int fd;
	int err = 0;
	char *filename;
	int data;
	
	if (argc != 2) {
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	fd = open(filename, O_RDWR);
	if (fd < 0) {
		printf("Can't open file %s\r\n", filename);
		return -1;
	}

	err = read(fd, &data, sizeof(data));
		
	

	
	close(fd);
	return 0;
}
学新通

可以看到是没有问题的,正常打印出了open和release,如同open和release函数中设置的一样。到目前为止,都只是IIC的前期准备工作,接下来才是重头戏。
学新通
初始化AP3216C,主要是在read函数里面实现,重点就是通过IIC控制器来向AP3216C里面读取或者发送数据。主要是使用i2c_transfer这个内核的API函数来实现。
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs. int num)
adpa:也就是IIC接口,比如我们这里是接到的I2C1接口,当IIC设备和驱动匹配以后,probe函数执行,probe函数传递进来的第一个参数就是i2c_client,也就是说,i2c接口是可以从probe函数中得到。
msgs:传输的数据

10.在ap3216c_dev结构体里面添加一个成员变量,用来保存client,也就是i2c接口
学新通

11.再在probe函数里面保存client
学新通

12.读取和发送函数

/*读取AP3216C的N个寄存器值
*reg:从哪个寄存器开始读取
*val:保存读取到的数据,一般是个数组
*len:长度
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{
	


}
/*向AP3216C写N个寄存器的数据
*u8 *buf:意思是ap3216c这个芯片是八位的,如果使用芯片不同,要改变这个
*/
static int ap3216c_write_regs(struct ap3216c_dev *dev, u8 reg, u8 *buf, u8 len)
{
	
}
/*读取AP3216C一个寄存器*/
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg)
{

}
/*写一个数据*/
static void ap3216c_write_reg(struct ap3216c_dev *dev, u8 reg, u8 data)
{

}
学新通

13.完善读取和写入函数

/*读取AP3216C的N个寄存器值
*reg:从哪个寄存器开始读取
*val:保存读取到的数据,一般是个数组
*len:长度
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{
	struct i2c_msg msg[2];
	struct i2c_client *client = (struct i2c_client*)dev->private_data;
	/*msg[0]发送要读取的寄存器首地址*/
	msg[0].addr =client->addr; //从机地址,也就是AP3216C地址
	msg[0].flags = 0; //表示发送
	msg[0].buf = &reg;  //要发送的数据,也就是寄存器地址
	msg[0].len = 1;  //要发送的寄存器地址长度为1
	/*msg[1]读取数据*/
	msg[1].addr =client->addr;
	msg[1].flags = I2C_M_RD; //表示读取
	msg[1].buf = val;  //要接收的数据
	msg[1].len = len;  //要读取的寄存器地址长度为1
	return i2c_transfer(client->adapter, msg , 2);

}
/*向AP3216C写N个寄存器的数据
*u8 *buf:意思是ap3216c这个芯片是八位的,如果使用芯片不同,要改变这个
*/
static int ap3216c_write_regs(struct ap3216c_dev *dev, u8 reg, u8 *buf, u8 len)
{
	u8 b[256];
	struct i2c_msg msg[1];
	struct i2c_client *client = (struct i2c_client*)dev->private_data;

	/*构建要发送的数据,也就是寄存器首地址 实际的数据*/
	b[0]= reg;
	memcpy(&b[1] , buf, len);

	msg.addr =client->addr; //从机地址,也就是AP3216C地址
	msg.flags = 0; //表示发送
	msg.buf = b;  //要发送的数据,也就是寄存器地址 实际数据
	msg.len = len 1;  //要发送的寄存器地址长度为1和实际的数据长度

	return i2c_transfer(client->adapter, &msg , 1);
}
/*读取AP3216C一个寄存器*/
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg)
{
	u8 data = 0;
	ap3216c_read_regs(dev, reg, &data ,1);
	return data;
}
/*写一个数据*/
static void ap3216c_write_reg(struct ap3216c_dev *dev, u8 reg, u8 data)
{
	u8 buf = 0;
	buf = data;
	ap3216c_write_regs(dev, reg, &buf, 1);
}
学新通

14.开始配置寄存器,在原来创建的ap3216creg.h文件中添加如下内容

#ifndef AP3216C_H
#define AP3216C_H

/* AP3216C寄存器 */
#define AP3216C_SYSTEMCONG	0x00	/* 配置寄存器 			*/
#define AP3216C_INTSTATUS	0X01	/* 中断状态寄存器 			*/
#define AP3216C_INTCLEAR	0X02	/* 中断清除寄存器 			*/
#define AP3216C_IRDATALOW	0x0A	/* IR数据低字节 			*/
#define AP3216C_IRDATAHIGH	0x0B	/* IR数据高字节 			*/
#define AP3216C_ALSDATALOW	0x0C	/* ALS数据低字节 		*/
#define AP3216C_ALSDATAHIGH	0X0D	/* ALS数据高字节			*/
#define AP3216C_PSDATALOW	0X0E	/* PS数据低字节 			*/
#define AP3216C_PSDATAHIGH	0X0F	/* PS数据高字节 			*/

#endif 

15.回到ap3216c.c文件中,在open函数中初始化AP3216C

static int ap3216c_open(struct inode *inode, struct file *filp)
{
	unsigned char value = 0;
	filp->private_data = &ap3216cdev;
	printk("open\r\n");
	
	/*初始化ap3216c,通过IIC适配器(控制器)配置ap3216c的寄存器*/
	/*向应用返回ap3216c的数据*/
	ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x4); //复位AP3216C 
	mdelay(50);
	ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x3); //开启ALS、PS IR 

	value = ap3216c_read_reg(&ap3216cdev, AP3216C_SYSTEMCONG); /* 读取刚刚写进去的0X03 */
	printk("AP316C_SYSTEMCONG = %#x\r\n",value);	

	return 0;
}
学新通

16.编译、加载、执行应用程序
可见结果是没有问题的
学新通
17.在ap3216c_dev结构体里面添加ir,als,ps

struct ap3216c_dev {
	int major;
	int minor;
	dev_t devid;
	struct cdev cdev;
	struct class *class;
	struct device *device;
	void *private_data;   //用私有数据保存client,也就是i2c接口
	unsigned short ir,als,ps;
};

18.编写读取AP3216C函数

/*读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!
 *如果同时打开ALS,IR PS的话两次数据读取的时间间隔要大于112.5ms
 */
void ap3216c_readdata(struct ap3216c_dev *dev)
{
    unsigned char buf[6];
    unsigned char i;

	/* 循环读取所有传感器数据 */
    for(i = 0; i < 6; i  )	
    {
        buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW   i);	
    }
	if(buf[0] & 0x80){
		dev->ir = 0;
		dev->ps = 0;
	}else{
		dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0x03);
		dev->os = (((unsigned short)buf[5] & 0x3f) << 4) | (buf[4] & 0x0F);
	}

	dev->als = ((unsigned short)buf[3] << 8) | buf[2];
  
}
学新通

19.在read函数里调用18步的读取函数

ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
	long err = 0;
	short data[3];
	struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;
	/*向应用返回AP3216C的原始数据*/
	ap3216c_readdata(dev);

	data[0] = dev->ir;
	data[1] = dev->als;
	data[2] = dev->ps;

	err = copy_to_user(buf, data, sizeof(data));

	return 0;
}
学新通

20.修改应用程序。

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "linux/ioctl.h"
#include "linux/input.h"
/* ./keyinputApp /dev/input/event2*/

/*input_event结构体变量*/
static struct input_event inputevent;

int main(int argc, char *argv[])
{
	int fd;
	int err = 0;
	char *filename;
	unsigned short data[3];
	unsigned short ir,ps,als;
	if (argc != 2) {
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	fd = open(filename, O_RDWR);
	if (fd < 0) {
		printf("Can't open file %s\r\n", filename);
		return -1;
	}
	while(1){
		err = read(fd, data, sizeof(data));
		if(err == 0){
			/*读取成功*/
			ir = data[0];
			als = data[1];
			ps =data[2];
			printf("AP3216C ir=%d, als = %d, ps = %d\r\n", ir, als, ps);
		}
		usleep(200000); //延时200ms
	}
	
		
	

	
	close(fd);
	return 0;
}

学新通

21.编译、加载、执行应用程序,可看到AP3216C的数据已经出来了。至此IIC驱动实验完成!!!!!
学新通



【完整驱动代码】

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>

#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"

struct ap3216c_dev {
	int major;
	int minor;
	dev_t devid;
	struct cdev cdev;
	struct class *class;
	struct device *device;
	void *private_data;   //用私有数据保存client,也就是i2c接口
	unsigned short ir,als,ps;
};

static struct ap3216c_dev ap3216cdev;

/*读取AP3216C的N个寄存器值
*reg:从哪个寄存器开始读取
*val:保存读取到的数据,一般是个数组
*len:长度
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{
	struct i2c_msg msg[2];
	struct i2c_client *client = (struct i2c_client*)dev->private_data;
	/*msg[0]发送要读取的寄存器首地址*/
	msg[0].addr =client->addr; //从机地址,也就是AP3216C地址
	msg[0].flags = 0; //表示发送
	msg[0].buf = &reg;  //要发送的数据,也就是寄存器地址
	msg[0].len = 1;  //要发送的寄存器地址长度为1
	/*msg[1]读取数据*/
	msg[1].addr =client->addr;
	msg[1].flags = I2C_M_RD; //表示读取
	msg[1].buf = val;  //要接收的数据
	msg[1].len = len;  //要读取的寄存器地址长度为1
	return i2c_transfer(client->adapter, msg , 2);

}
/*向AP3216C写N个寄存器的数据
*u8 *buf:意思是ap3216c这个芯片是八位的,如果使用芯片不同,要改变这个
*/
static int ap3216c_write_regs(struct ap3216c_dev *dev, u8 reg, u8 *buf, u8 len)
{
	u8 b[256];
	struct i2c_msg msg;
	struct i2c_client *client = (struct i2c_client*)dev->private_data;

	/*构建要发送的数据,也就是寄存器首地址 实际的数据*/
	b[0]= reg;
	memcpy(&b[1] , buf, len);

	msg.addr =client->addr; //从机地址,也就是AP3216C地址
	msg.flags = 0; //表示发送
	msg.buf = b;  //要发送的数据,也就是寄存器地址 实际数据
	msg.len = len 1;  //要发送的寄存器地址长度为1和实际的数据长度

	return i2c_transfer(client->adapter, &msg , 1);
}

/*读取AP3216C一个寄存器*/
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg)
{
	u8 data = 0;
	ap3216c_read_regs(dev, reg, &data ,1);
	return data;
}
/*写一个数据*/
static void ap3216c_write_reg(struct ap3216c_dev *dev, u8 reg, u8 data)
{
	u8 buf = 0;
	buf = data;
	ap3216c_write_regs(dev, reg, &buf, 1);
}


/*读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!
 *如果同时打开ALS,IR PS的话两次数据读取的时间间隔要大于112.5ms
 */
void ap3216c_readdata(struct ap3216c_dev *dev)
{
    unsigned char buf[6];
    unsigned char i;

	/* 循环读取所有传感器数据 */
    for(i = 0; i < 6; i  )	
    {
        buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW   i);	
    }
	if(buf[0] & 0x80){
		dev->ir = 0;
		dev->ps = 0;
	}else{
		dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0x03);
		dev->ps = (((unsigned short)buf[5] & 0x3f) << 4) | (buf[4] & 0x0F);
	}

	dev->als = ((unsigned short)buf[3] << 8) | buf[2];
  
}

static int ap3216c_open(struct inode *inode, struct file *filp)
{
	unsigned char value = 0;
	filp->private_data = &ap3216cdev;
	
	/*初始化ap3216c,通过IIC适配器(控制器)配置ap3216c的寄存器*/
	/*向应用返回ap3216c的数据*/
	ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x4); //复位AP3216C 
	mdelay(50);
	ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x3); //开启ALS、PS IR 

	value = ap3216c_read_reg(&ap3216cdev, AP3216C_SYSTEMCONG); /* 读取刚刚写进去的0X03 */
	printk("AP316C_SYSTEMCONG = %#x\r\n",value);	

	return 0;
}
ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
	long err = 0;
	short data[3];
	struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;
	/*向应用返回AP3216C的原始数据*/
	ap3216c_readdata(dev);

	data[0] = dev->ir;
	data[1] = dev->als;
	data[2] = dev->ps;

	err = copy_to_user(buf, data, sizeof(data));

	return 0;
}
static int ap3216c_release(struct inode *inode, struct file *filp)
{
	struct ap3216c_dev *dev = (struct ap3216cdev_dev*)filp->private_data;
	printk("release\r\n");
	return 0;
}


static const struct file_operations ap3216c_fops = {
	.owner   = THIS_MODULE,
	.open    = ap3216c_open,
	.read   = ap3216c_read,
	.release = ap3216c_release,
};
/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int ret = 0;
	printk("OKOKOKOK!!!!\r\n");
	ap3216cdev.major = 0; //由系统分配主设备号
	/*搭建字符设备驱动框架,在/dev*/
	if(ap3216cdev.major){
		ap3216cdev.devid = MKDEV(ap3216cdev.major, 0);
		ret = register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME);
	}else{
		ret = alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);
		ap3216cdev.major = MAJOR(ap3216cdev.devid);
		ap3216cdev.minor = MINOR(ap3216cdev.devid);
	}
	if(ret < 0){
		printk("ap3216cdev chrdev_region err!\r\n");
		goto fail_devid;
	}
	printk("ap3216cdev major=%d, minor=%d\r\n", ap3216cdev.major, ap3216cdev.minor);

	ap3216cdev.cdev.owner = THIS_MODULE;
	cdev_init(&ap3216cdev.cdev, &ap3216c_fops);
	ret = cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT);
	if(ret < 0){
		goto fail_cdev;
	}

	ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME);
	if(IS_ERR(ap3216cdev.class)){
		ret = PTR_ERR(ap3216cdev.class);
		goto fail_class;
	}

	ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid, NULL, AP3216C_NAME);
	if(IS_ERR(ap3216cdev.device)){
		ret = PTR_ERR(ap3216cdev.device);
		goto fail_device;
	}

	ap3216cdev.private_data = client; 

	return 0;

fail_device:
	class_destroy(ap3216cdev.class);
fail_class:
	cdev_del(&ap3216cdev.cdev);
fail_cdev:
	unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);
fail_devid:
	return ret;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{
	cdev_del(&ap3216cdev.cdev);

	unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);

	device_destroy(ap3216cdev.class, ap3216cdev.devid);

	class_destroy(ap3216cdev.class);


	return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {
	{"alientek,ap3216c", 0},  //匹配名字
	{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {
	{.compatible = "alientek,ap3216c"},
	{}
};

/*i2c_driver*/
static struct i2c_driver ap3216c_driver = {
	.probe = ap3216c_probe,
	.remove = ap3216c_remove,
	.driver = {
		.name = "ap3216c",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(ap3216c_of_match),
	},
	.id_table = ap3216c_id,
};

/*驱动入口函数*/
static int __init ap3216c_init(void)
{
	int ret =0;

	ret = i2c_add_driver(&ap3216c_driver);




	return ret;
}

/*驱动出口函数*/
static void __exit ap3216c_exit(void)
{
	i2c_del_driver(&ap3216c_driver);

}

module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kong");

学新通

《正点原子第四期驱动开发第22讲》—左忠凯
《 Linux设备驱动开发详解》—宋宝华
《Linux内核设计与实现》—Robert Love

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibfhbg
系列文章
更多 icon
同类精品
更多 icon
继续加载