【Linux驱动开发】I2C 驱动实验(AP3216C驱动)

一、I2C 驱动框架简介

Linux 内核也将 I2C 驱动分为两部分:
I2C 总线驱动: SOC I2C 控制器驱动,也叫做 I2C 适配器驱动。
I2C 设备驱动: 针对具体的 I2C 设备而编写的驱动。
 
 

1、I2C 总线驱动

  • I2C 总线驱动重点是 I2C 适配器驱动,要用到两个重要的数据结构:i2c_adapter i2c_algorithm
  • Linux 内核将 SOC I2C 适配器(控制器) 抽象成 i2c_adapteri2c_adapter 结构体定义在 include/linux/i2c.h 文件中。
  • i2c_algorithm 就是 I2C 适配器与 IIC 设备进行通信的方法。
  • i2c_algorithm 结构体定义在 include/linux/i2c.h 文件中。
  • master_xfer I2C 适配器的传输函数,可以通过此函数来完成与 IIC 设备之间的通信。
  • smbus_xfer SMBUS 总线的传输函数。
  • I2C 总线驱动主要工作就是初始化 i2c_adapter 结构体变量,然后设置 i2c_algorithm 中的 master_xfer 函数。
  • 使用 i2c_add_numbered_adapter 或 i2c_add_adapter 这两个函数向系统注册设置好的 i2c_adapter。
注册  i2c_adapter
int i2c_add_adapter (struct i2c_adapter *adapter)
int i2c_add_numbered_adapter (struct i2c_adapter *adap)
i2c_add_adapter 使用动态的总线号
i2c_add_numbered_adapter 使用静态总线号。
adapter adap :要添加到 Linux 内核中的 i2c_adapter ,也就是 I2C 适配器。
返回值: 0 ,成功;负值,失败
注销 i2c_adapter
void i2c_del_adapter (struct i2c_adapter * adap)
adap :要删除的 I2C 适配器。
返回值: 无。

 

2、I2C 设备驱动

  • I2C 设备驱动重点关注两个数据结构:i2c_client i2c_driver
  • i2c_client 是描述设备信息的,i2c_driver 是描述驱动内容。
  • i2c_client 结构体定义在 include/linux/i2c.h一个设备对应一个 i2c_client。
  • i2c_driver 结构体定义在 include/linux/i2c.h 文件中。
注册 i2c_driver
int i2c_register_driver (struct module *owner, struct i2c_driver *driver)
owner 一般为 THIS_MODULE
driver :要注册的 i2c_driver
返回值: 0 ,成功;负值,失败。
i2c_add_driver 也用于注册 i2c_driver
注销 i2c_driver
void i2c_del_driver (struct i2c_driver *driver)
driver :要注销的 i2c_driver
返回值: 无。

 

3、I2C 设备和驱动匹配过程

I2C 设备和驱动的匹配过程是由 I2C 核心来完成的, drivers/i2c/i2c-core.c 就是 I2C 的核心部分。
I2C 核心的 API 函数:
i2c_adapter 注册/注销函数
int i2c_add_adapter(struct i2c_adapter *adapter)
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
void i2c_del_adapter(struct i2c_adapter * adap)
i2c_driver 注册/注销函数
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
int i2c_add_driver (struct i2c_driver *driver)
void i2c_del_driver(struct i2c_driver *driver)
 
I2C 总线的数据结构为 i2c_bus_type ,定义在 drivers/i2c/i2c-core.c 文件。
of_driver_match_device 函数

用于完成设备树设备和驱动匹配。

比较 I2C 设备节点的 compatible 属性和 of_device_id 中的 compatible 属性是否相等。

acpi_driver_match_device 函数用于 ACPI 形式的匹配。
i2c_match_id 函数

用于传统的、无设备树的 I2C 设备和驱动匹配过程。

比较 I2C 设备名字和 i2c_device_id name 字段是否相等。

 

二、I.MX6U I2C 适配器驱动分析

I.MX6U I2C 适配器驱动文件为 drivers/i2c/busses/i2c-imx.c
i2c_imx_func 函数用于返回此I2C适配器支持什么样的通信协议。
i2c_imx_xfer 函数完成与 I2C 设备通信。
i2c_imx_start 函数开启 I2C 通信。
i2c_imx_read 函数从 I2C 设备读数据。
i2c_imx_dma_write 函数向 I2C 设备写数据(使用 DMA)。
i2c_imx_write 函数向 I2C 设备写数据(不使用 DMA)。
i2c_imx_stop 函数停止 I2C 通信。

i2c_imx_starti2c_imx_read

i2c_imx_write、i2c_imx_stop 函数

I2C 寄存器的具体操作函数。

 

三、I2C 设备驱动编写流程

1、I2C 设备信息描述

  • 在设备树 i2c1 节点下创建对应设备的子节点,在子节点内描述这个设备的相关信息。
  • I2C 设备节点的创建重点是 compatible 属性和 reg 属性的设置,一个用于匹配驱动,一个用于设置器件地址。

2、I2C 设备数据收发处理流程

读写 I2C 设备寄存器
int i2c_transfer (struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
adap 所使用的 I2C 适配器, i2c_client 会保存其对应的 i2c_adapter
msgs I2C 要发送的一个或多个消息。
num 消息数量,也就是 msgs 的数量。
返回值: 负值,失败,其他非负值,发送的 msgs 数量。
  • 使用 i2c_transfer 函数发送数据之前要先构建好 i2c_msg。
  • msgs 这个参数,是一个 i2c_msg 类型的指针参数,I2C 进行数据收发就是消息的传递,Linux 内核使用 i2c_msg 结构体来描述一个消息,i2c_msg 结构体定义 在 include/uapi/linux/i2c.h 文件中。
I2C 数据发送函数
int i2c_master_send (const struct i2c_client *client, const char *buf, int count)
client I2C 设备对应的 i2c_client
buf :要发送的数据。
count 要发送的数据字节数,要小于 64KB ,  i2c_msg len 成员变量是一个 u16( 无符号 16 ) 类型数据。
返回值: 负值,失败,其他非负值,发送的字节数。
I2C 数据接收函数
int i2c_master_recv (const struct i2c_client *client, char *buf, int count)
client I2C 设备对应的 i2c_client
buf :要接收的数据。
count 要接收的数据字节数,要小于 64KB i2c_msg len 成员变量是一个 u16( 无符号 16 ) 类型数据。
返回值: 负值,失败,其他非负值,发送的字节数。

 

四、实验程序编写

1、修改设备树

(1)IO 修改或添加

  • 开发板上的 I2C1 接口使用到了 UART4_TXD UART4_RXD。
  • 如果要用到 AP3216C 的中断功能的话还需要初始化 AP_INT 对应的 GIO1_IO01。
  • 将 UART4_TXD UART4_RXD 这两个IO分别复用为 I2C1_SCL I2C1_SDA,电气属性都设置为 0x4001b8b0
pinctrl_i2c1: i2c1grp { 
		fsl,pins = < 
			MX6UL_PAD_UART4_TX_DATA__I2C1_SCL 0x4001b8b0
			MX6UL_PAD_UART4_RX_DATA__I2C1_SDA 0x4001b8b0
		>;
};

(2)i2c1 节点删除原有的节点,增加 ap3216c 子节点

&i2c1 {
	clock-frequency = <100000>;
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_i2c1>;
	status = "okay";
	/*
	mag3110@0e {
		compatible = "fsl,mag3110";
		reg = <0x0e>;
		position = <2>;
	};

	fxls8471@1e {
		compatible = "fsl,fxls8471";
		reg = <0x1e>;
		position = <0>;
		interrupt-parent = <&gpio5>;
		interrupts = <0 8>;
	};
	*/

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

};
(3)测试
“make dtbs”编译设备树,使用新的设备树启动内核。
在 /sys/bus/i2c/devices 目录下存放着所有 I2C 设备。

名为“0-001e”的子目录就是新添加的 I2C 设备 ap3216c。

 

2、AP3216C 驱动编写

AP3216C 是环境光传感器。

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_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include "ap3216creg.h"
/***************************************************************
文件名		: ap3216c.c
描述	   	: AP3216C驱动程序
***************************************************************/
#define AP3216C_CNT	1
#define AP3216C_NAME	"ap3216c"

struct ap3216c_dev {
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;	/* 类 		*/
	struct device *device;	/* 设备 	 */
	struct device_node	*nd; /* 设备节点 */
	int major;			/* 主设备号 */
	void *private_data;	/* 私有数据 */
	unsigned short ir, als, ps;		/* 三个光传感器数据 */
};

static struct ap3216c_dev ap3216cdev;

/*
 * @description	: 从ap3216c读取多个寄存器数据
 * @param - dev:  ap3216c设备
 * @param - reg:  要读取的寄存器首地址
 * @param - val:  读取到的数据
 * @param - len:  要读取的数据长度
 * @return 		: 操作结果
 */
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{
	int ret;
	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;						/* reg长度*/

	/* msg[1]读取数据 */
	msg[1].addr = client->addr;			/* ap3216c地址 */
	msg[1].flags = I2C_M_RD;			/* 标记为读取数据*/
	msg[1].buf = val;					/* 读取数据缓冲区 */
	msg[1].len = len;					/* 要读取的数据长度*/

	ret = i2c_transfer(client->adapter, msg, 2);
	if(ret == 2) {
		ret = 0;
	} else {
		printk("i2c rd failed=%d reg=%06x len=%d\n",ret, reg, len);
		ret = -EREMOTEIO;
	}
	return ret;
}

/*
 * @description	: 向ap3216c多个寄存器写入数据
 * @param - dev:  ap3216c设备
 * @param - reg:  要写入的寄存器首地址
 * @param - val:  要写入的数据缓冲区
 * @param - len:  要写入的数据长度
 * @return 	  :   操作结果
 */
static s32 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);		/* 将要写入的数据拷贝到数组b里面 */
		
	msg.addr = client->addr;	/* ap3216c地址 */
	msg.flags = 0;				/* 标记为写数据 */

	msg.buf = b;				/* 要写入的数据缓冲区 */
	msg.len = len + 1;			/* 要写入的数据长度 */

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

/*
 * @description	: 读取ap3216c指定寄存器值,读取一个寄存器
 * @param - dev:  ap3216c设备
 * @param - reg:  要读取的寄存器
 * @return 	  :   读取到的寄存器值
 */
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg)
{
	u8 data = 0;

	ap3216c_read_regs(dev, reg, &data, 1);
	return data;

#if 0
	struct i2c_client *client = (struct i2c_client *)dev->private_data;
	return i2c_smbus_read_byte_data(client, reg);
#endif
}

/*
 * @description	: 向ap3216c指定寄存器写入指定的值,写一个寄存器
 * @param - dev:  ap3216c设备
 * @param - reg:  要写的寄存器
 * @param - data: 要写入的值
 * @return   :    无
 */
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);
}

/*
 * @description	: 读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!
 *				: 如果同时打开ALS,IR+PS的话两次数据读取的时间间隔要大于112.5ms
 * @param - ir	: ir数据
 * @param - ps 	: ps数据
 * @param - ps 	: als数据 
 * @return 		: 无。
 */
void ap3216c_readdata(struct ap3216c_dev *dev)
{
	unsigned char i =0;
    unsigned char buf[6];
	
	/* 循环读取所有传感器数据 */
    for(i = 0; i < 6; i++)	
    {
        buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW + i);	
    }

    if(buf[0] & 0X80) 	/* IR_OF位为1,则数据无效 */
		dev->ir = 0;					
	else 				/* 读取IR传感器的数据   		*/
		dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0X03); 			
	
	dev->als = ((unsigned short)buf[3] << 8) | buf[2];	/* 读取ALS传感器的数据 			 */  
	
    if(buf[4] & 0x40)	/* IR_OF位为1,则数据无效 			*/
		dev->ps = 0;    													
	else 				/* 读取PS传感器的数据    */
		dev->ps = ((unsigned short)(buf[5] & 0X3F) << 4) | (buf[4] & 0X0F); 
}

/*
 * @description		: 打开设备
 * @param - inode 	: 传递给驱动的inode
 * @param - filp 	: 设备文件,file结构体有个叫做private_data的成员变量
 * 					  一般在open的时候将private_data指向设备结构体。
 * @return 			: 0 成功;其他 失败
 */
static int ap3216c_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &ap3216cdev;

	/* 初始化AP3216C */
	ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x04);		/* 复位AP3216C 			*/
	mdelay(50);														/* AP3216C复位最少10ms 	*/
	ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0X03);		/* 开启ALS、PS+IR 		*/
	return 0;
}

/*
 * @description		: 从设备读取数据 
 * @param - filp 	: 要打开的设备文件(文件描述符)
 * @param - buf 	: 返回给用户空间的数据缓冲区
 * @param - cnt 	: 要读取的数据长度
 * @param - offt 	: 相对于文件首地址的偏移
 * @return 			: 读取的字节数,如果为负值,表示读取失败
 */
static ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
	short data[3];
	long err = 0;

	struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;
	
	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;
}

/*
 * @description		: 关闭/释放设备
 * @param - filp 	: 要关闭的设备文件(文件描述符)
 * @return 			: 0 成功;其他 失败
 */
static int ap3216c_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/* AP3216C操作函数 */
static const struct file_operations ap3216c_ops = {
	.owner = THIS_MODULE,
	.open = ap3216c_open,
	.read = ap3216c_read,
	.release = ap3216c_release,
};

 /*
  * @description     : i2c驱动的probe函数,当驱动与
  *                    设备匹配以后此函数就会执行
  * @param - client  : i2c设备
  * @param - id      : i2c设备ID
  * @return          : 0,成功;其他负值,失败
  */
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	/* 1、构建设备号 */
	if (ap3216cdev.major) {
		ap3216cdev.devid = MKDEV(ap3216cdev.major, 0);
		register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME);
	} else {
		alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);
		ap3216cdev.major = MAJOR(ap3216cdev.devid);
	}

	/* 2、注册设备 */
	cdev_init(&ap3216cdev.cdev, &ap3216c_ops);
	cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT);

	/* 3、创建类 */
	ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME);
	if (IS_ERR(ap3216cdev.class)) {
		return PTR_ERR(ap3216cdev.class);
	}

	/* 4、创建设备 */
	ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid, NULL, AP3216C_NAME);
	if (IS_ERR(ap3216cdev.device)) {
		return PTR_ERR(ap3216cdev.device);
	}

	ap3216cdev.private_data = client;

	return 0;
}

/*
 * @description     : i2c驱动的remove函数,移除i2c驱动的时候此函数会执行
 * @param - client 	: i2c设备
 * @return          : 0,成功;其他负值,失败
 */
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;
}

/* 传统匹配方式ID列表 */
static const struct i2c_device_id ap3216c_id[] = {
	{"iot,ap3216c", 0},  
	{}
};

/* 设备树匹配列表 */
static const struct of_device_id ap3216c_of_match[] = {
	{ .compatible = "iot,ap3216c" },
	{ /* Sentinel */ }
};

/* i2c驱动结构体 */	
static struct i2c_driver ap3216c_driver = {
	.probe = ap3216c_probe,
	.remove = ap3216c_remove,
	.driver = {
			.owner = THIS_MODULE,
		   	.name = "ap3216c",
		   	.of_match_table = ap3216c_of_match, 
		   },
	.id_table = ap3216c_id,
};
		   
/*
 * @description	: 驱动入口函数
 * @param 		: 无
 * @return 		: 无
 */
static int __init ap3216c_init(void)
{
	int ret = 0;

	ret = i2c_add_driver(&ap3216c_driver);
	return ret;
}

/*
 * @description	: 驱动出口函数
 * @param 		: 无
 * @return 		: 无
 */
static void __exit ap3216c_exit(void)
{
	i2c_del_driver(&ap3216c_driver);
}

/* module_i2c_driver(ap3216c_driver) */

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

ap3216cApp.c

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
/***************************************************************
文件名		: ap3216cApp.c
描述	   	: ap3216c设备测试APP。
使用方法	 :./ap3216cApp /dev/ap3216c
***************************************************************/

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd;
	char *filename;
	unsigned short databuf[3];
	unsigned short ir, als, ps;
	int ret = 0;

	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) {
		ret = read(fd, databuf, sizeof(databuf));
		if(ret == 0) { 			/* 数据读取成功 */
			ir =  databuf[0]; 	/* ir传感器数据 */
			als = databuf[1]; 	/* als传感器数据 */
			ps =  databuf[2]; 	/* ps传感器数据 */
			printf("ir = %d, als = %d, ps = %d\r\n", ir, als, ps);
		}
		usleep(200000); /*100ms */
	}
	close(fd);	/* 关闭文件 */	
	return 0;
}

 ap3216creg.h

#ifndef AP3216C_H
#define AP3216C_H
/***************************************************************
文件名		: ap3216creg.h
描述	   	: AP3216C寄存器地址描述头文件
***************************************************************/

#define AP3216C_ADDR    	0X1E	/* AP3216C器件地址  */

/* AP3316C寄存器 */
#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

 Makefile

KERNELDIR := /home/pjw/linux/kernel/iot_kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga
 
CURRENT_PATH := $(shell pwd)
 
obj-m := dtsled.o
 
build: kernel_modules
 
kernel_modules:
    $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
 
clean:
    $(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean

 

五、测试:

1、将ap3216c.ko和 ap3216cApp 这两个文件拷贝到 rootfs/lib/modules 目录
2、主板终端进入到目录 lib/modules/4.1.15 中,输入如下命令加载 ap3216c.ko 驱动模块:

depmod                 //第一次加载驱动的时候需要运行此命令
modprobe ap3216c.ko     //加载驱动

3、查看模块是否加载成功

使用“lsmod”命令查看当前系统中存在的模块

4、驱动模块加载成功以后使用 ap3216cApp 来测试:

./ap3216cApp /dev/ap3216c

从 AP3216C 中读取数据:

以下是一个简单的示例代码,展示了如何在Linux系统上编写i2c驱动程序以控制AP3216C传感器: ```c #include <linux/i2c-dev.h> #include <linux/i2c.h> #define AP3216C_I2C_ADDR 0x1e static int ap3216c_read(struct i2c_client *client, u8 reg, u8 *data, int len) { struct i2c_msg msgs[2] = { { .addr = client->addr, .flags = 0, .len = 1, .buf = &reg, }, { .addr = client->addr, .flags = I2C_M_RD, .len = len, .buf = data, }, }; return i2c_transfer(client->adapter, msgs, 2); } static int ap3216c_write(struct i2c_client *client, u8 reg, u8 *data, int len) { u8 *buf = kzalloc(len + 1, GFP_KERNEL); int ret; if (!buf) return -ENOMEM; buf[0] = reg; memcpy(buf + 1, data, len); struct i2c_msg msg = { .addr = client->addr, .flags = 0, .len = len + 1, .buf = buf, }; ret = i2c_transfer(client->adapter, &msg, 1); kfree(buf); return ret; } static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id) { int ret; u8 data[2]; ret = ap3216c_read(client, 0x00, data, 2); if (ret < 0) { dev_err(&client->dev, "Failed to read AP3216C chip ID\n"); return ret; } dev_info(&client->dev, "AP3216C chip ID: %x\n", data[1]); /* TODO: Write initialization sequence here */ return 0; } static const struct i2c_device_id ap3216c_id[] = { { "ap3216c", 0 }, { } }; MODULE_DEVICE_TABLE(i2c, ap3216c_id); static struct i2c_driver ap3216c_driver = { .driver = { .name = "ap3216c", }, .probe = ap3216c_probe, .id_table = ap3216c_id, }; module_i2c_driver(ap3216c_driver); ``` 这个示例代码定义了两个函数:`ap3216c_read`和`ap3216c_write`,分别用于读和写AP3216C寄存器。该驱动程序在初始化过程中会通过I2C总线读取AP3216C芯片的ID,并可以在该函数中添加其他初始化代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值