Compare commits

..

No commits in common. "40b2bc2385d878184b5babab520427c52707327d" and "3db1e5f14a092512f84afb87140bc88e132d4eb0" have entirely different histories.

View File

@ -37,16 +37,17 @@ struct cst816x_data {
int io_cst816d_reset; // 保存获取得到引脚编号 int io_cst816d_reset; // 保存获取得到引脚编号
static dev_t cst816x_devno; //定义字符设备的设备号 static dev_t mpu6050_devno; //定义字符设备的设备号
static struct cdev cst816x_chr_dev; //定义字符设备结构体chr_dev static struct cdev mpu6050_chr_dev; //定义字符设备结构体chr_dev
struct class *class_cst816x; //保存创建的类 struct class *class_mpu6050; //保存创建的类
struct device *device_cst816x; // 保存创建的设备 struct device *device_mpu6050; // 保存创建的设备
struct device_node *mpu6050_device_node; //rgb_led的设备树节点结构体
static struct workqueue_struct *button_wq; static struct workqueue_struct *button_wq;
static struct work_struct button_work; static struct work_struct button_work;
/*------------------IIC设备内容----------------------*/ /*------------------IIC设备内容----------------------*/
struct i2c_client *cst816x_client = NULL; //保存cst816设备对应的i2c_client结构体,匹配成功后由.prob函数带回。 struct i2c_client *mpu6050_client = NULL; //保存mpu6050设备对应的i2c_client结构体,匹配成功后由.prob函数带回。
static struct input_dev *cst816x_i_dev; static struct input_dev *cst816x_i_dev;
@ -78,36 +79,36 @@ static int i2c_write_16bit_address(struct i2c_client *client, u16 address, u8 da
return 0; return 0;
} }
/*通过i2c 向cst816x写入数据 /*通过i2c 向mpu6050写入数据
*cst816x_client:cst816x的i2c_client结构体。 *mpu6050_client:mpu6050的i2c_client结构体。
*address, 要读取的地址, *address, 要读取的地址,
*data,保存读取得到的数据 *data,保存读取得到的数据
*length,读长度 *length,读长度
*返回值,错误,-1。成功,0 *返回值,错误,-1。成功,0
*/ */
static int i2c_read_cst816x(struct i2c_client *cst816x_client, u8 address, void *data, u32 length) static int i2c_read_cst816x(struct i2c_client *mpu6050_client, u8 address, void *data, u32 length)
{ {
int error = 0; int error = 0;
u8 address_data = address; u8 address_data = address;
struct i2c_msg cst816x__msg[2]; struct i2c_msg mpu6050_msg[2];
/*设置读取位置msg*/ /*设置读取位置msg*/
cst816x__msg[0].addr = cst816x_client->addr; //cst816x在 iic 总线上的地址 mpu6050_msg[0].addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
cst816x__msg[0].flags = 0; //标记为发送数据 mpu6050_msg[0].flags = 0; //标记为发送数据
cst816x__msg[0].buf = &address_data; //写入的首地址 mpu6050_msg[0].buf = &address_data; //写入的首地址
cst816x__msg[0].len = 1; //写入长度 mpu6050_msg[0].len = 1; //写入长度
/*设置读取位置msg*/ /*设置读取位置msg*/
cst816x__msg[1].addr = cst816x_client->addr; //cst816x在 iic 总线上的地址 mpu6050_msg[1].addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
cst816x__msg[1].flags = I2C_M_RD; //标记为读取数据 mpu6050_msg[1].flags = I2C_M_RD; //标记为读取数据
cst816x__msg[1].buf = data; //读取得到的数据保存位置 mpu6050_msg[1].buf = data; //读取得到的数据保存位置
cst816x__msg[1].len = length; //读取长度 mpu6050_msg[1].len = length; //读取长度
error = i2c_transfer(cst816x_client->adapter, cst816x__msg, 2); error = i2c_transfer(mpu6050_client->adapter, mpu6050_msg, 2);
if (error != 2) if (error != 2)
{ {
printk(KERN_EMERG "\n i2c_read_cst816x error \n"); printk(KERN_EMERG "\n i2c_read_mpu6050 error \n");
return -1; return -1;
} }
return 0; return 0;
@ -115,36 +116,36 @@ static int i2c_read_cst816x(struct i2c_client *cst816x_client, u8 address, void
/*通过i2c 向设备读取数据 /*通过i2c 向设备读取数据
* cst816x_client:设备的 i2c_client 结构体。 * mpu6050_client:设备的 i2c_client 结构体。
* address:要读取的 16 位寄存器地址。 * address:要读取的 16 位寄存器地址。
* data:保存读取到的数据。 * data:保存读取到的数据。
* length:读取长度。 * length:读取长度。
* 返回值:错误,-1;成功,0。 * 返回值:错误,-1;成功,0。
*/ */
static int i2c_read_cst816x_16bit(struct i2c_client *cst816x_client, u16 address, void *data, u32 length) static int i2c_read_cst816x_16bit(struct i2c_client *mpu6050_client, u16 address, void *data, u32 length)
{ {
int error = 0; int error = 0;
u8 address_data[2]; // 存储 16 位地址(高字节 + 低字节) u8 address_data[2]; // 存储 16 位地址(高字节 + 低字节)
struct i2c_msg cst816x__msg[2]; // I2C 消息数组 struct i2c_msg mpu6050_msg[2]; // I2C 消息数组
/* 设置 16 位地址 */ /* 设置 16 位地址 */
address_data[0] = (address >> 8) & 0xFF; // 高字节 address_data[0] = (address >> 8) & 0xFF; // 高字节
address_data[1] = address & 0xFF; // 低字节 address_data[1] = address & 0xFF; // 低字节
/* 设置写入地址的消息 */ /* 设置写入地址的消息 */
cst816x__msg[0].addr = cst816x_client->addr; // 设备的 I2C 地址 mpu6050_msg[0].addr = mpu6050_client->addr; // 设备的 I2C 地址
cst816x__msg[0].flags = 0; // 写操作 mpu6050_msg[0].flags = 0; // 写操作
cst816x__msg[0].buf = address_data; // 地址缓冲区 mpu6050_msg[0].buf = address_data; // 地址缓冲区
cst816x__msg[0].len = 2; // 地址长度为 2 字节 mpu6050_msg[0].len = 2; // 地址长度为 2 字节
/* 设置读取数据的消息 */ /* 设置读取数据的消息 */
cst816x__msg[1].addr = cst816x_client->addr; // 设备的 I2C 地址 mpu6050_msg[1].addr = mpu6050_client->addr; // 设备的 I2C 地址
cst816x__msg[1].flags = I2C_M_RD; // 读操作 mpu6050_msg[1].flags = I2C_M_RD; // 读操作
cst816x__msg[1].buf = data; // 数据保存缓冲区 mpu6050_msg[1].buf = data; // 数据保存缓冲区
cst816x__msg[1].len = length; // 读取长度 mpu6050_msg[1].len = length; // 读取长度
/* 执行 I2C 传输 */ /* 执行 I2C 传输 */
error = i2c_transfer(cst816x_client->adapter, cst816x__msg, 2); error = i2c_transfer(mpu6050_client->adapter, mpu6050_msg, 2);
if (error != 2) if (error != 2)
{ {
printk(KERN_ERR "i2c_read_cst816x_16bit: i2c_transfer error\n"); printk(KERN_ERR "i2c_read_cst816x_16bit: i2c_transfer error\n");
@ -156,13 +157,13 @@ static int i2c_read_cst816x_16bit(struct i2c_client *cst816x_client, u16 address
/* 通过 I2C 向设备发送多字节数据(支持大数据量)。 /* 通过 I2C 向设备发送多字节数据(支持大数据量)。
* cst816x_client: 设备的 i2c_client 结构体。 * mpu6050_client: 设备的 i2c_client 结构体。
* address: 数据要写入的寄存器地址(16 位)。 * address: 数据要写入的寄存器地址(16 位)。
* data: 要发送的数据缓冲区。 * data: 要发送的数据缓冲区。
* length: 要发送的数据长度(最大 512 字节)。 * length: 要发送的数据长度(最大 512 字节)。
* 返回值: 成功返回 0,失败返回 -1。 * 返回值: 成功返回 0,失败返回 -1。
*/ */
static int i2c_write_cst816x_512bytes(struct i2c_client *cst816x_client, u16 address, const void *data, u32 length) static int i2c_write_cst816x_512bytes(struct i2c_client *mpu6050_client, u16 address, const void *data, u32 length)
{ {
int error = 0; int error = 0;
u32 chunk_size = 32; // 每次传输的最大字节数(根据硬件支持设置) u32 chunk_size = 32; // 每次传输的最大字节数(根据硬件支持设置)
@ -184,7 +185,7 @@ static int i2c_write_cst816x_512bytes(struct i2c_client *cst816x_client, u16 add
} }
/* 初始化 I2C 消息 */ /* 初始化 I2C 消息 */
msg.addr = cst816x_client->addr; msg.addr = mpu6050_client->addr;
msg.flags = 0; // 写操作 msg.flags = 0; // 写操作
while (offset < length) { while (offset < length) {
@ -202,7 +203,7 @@ static int i2c_write_cst816x_512bytes(struct i2c_client *cst816x_client, u16 add
msg.len = bytes_to_send + 2; msg.len = bytes_to_send + 2;
/* 执行 I2C 传输 */ /* 执行 I2C 传输 */
error = i2c_transfer(cst816x_client->adapter, &msg, 1); error = i2c_transfer(mpu6050_client->adapter, &msg, 1);
if (error != 1) { if (error != 1) {
printk(KERN_ERR "i2c_write_cst816x_512bytes: i2c_transfer error at offset %u\n", offset); printk(KERN_ERR "i2c_write_cst816x_512bytes: i2c_transfer error at offset %u\n", offset);
kfree(buffer); // 释放动态缓冲区 kfree(buffer); // 释放动态缓冲区
@ -220,13 +221,13 @@ static int i2c_write_cst816x_512bytes(struct i2c_client *cst816x_client, u16 add
} }
/* 通过 I2C 向设备写入任意字节长度的数据。 /* 通过 I2C 向设备写入任意字节长度的数据。
* cst816x_client: 设备的 i2c_client 结构体。 * mpu6050_client: 设备的 i2c_client 结构体。
* address: 数据要写入的寄存器地址(16 位)。 * address: 数据要写入的寄存器地址(16 位)。
* data: 要发送的数据缓冲区。 * data: 要发送的数据缓冲区。
* length: 要发送的数据长度(任意长度)。 * length: 要发送的数据长度(任意长度)。
* 返回值: 成功返回 0,失败返回 -1。 * 返回值: 成功返回 0,失败返回 -1。
*/ */
static int i2c_write_cst816x_any(struct i2c_client *cst816x_client, u16 address, const void *data, u32 length) static int i2c_write_cst816x_any(struct i2c_client *mpu6050_client, u16 address, const void *data, u32 length)
{ {
int error = 0; int error = 0;
u8 *buffer; // 动态分配缓冲区 u8 *buffer; // 动态分配缓冲区
@ -251,13 +252,13 @@ static int i2c_write_cst816x_any(struct i2c_client *cst816x_client, u16 address,
memcpy(&buffer[2], data, length); // 数据拷贝 memcpy(&buffer[2], data, length); // 数据拷贝
/* 设置 I2C 消息 */ /* 设置 I2C 消息 */
msg.addr = cst816x_client->addr; msg.addr = mpu6050_client->addr;
msg.flags = 0; // 写操作 msg.flags = 0; // 写操作
msg.buf = buffer; msg.buf = buffer;
msg.len = length + 2; msg.len = length + 2;
/* 执行 I2C 传输 */ /* 执行 I2C 传输 */
error = i2c_transfer(cst816x_client->adapter, &msg, 1); error = i2c_transfer(mpu6050_client->adapter, &msg, 1);
if (error != 1) { if (error != 1) {
printk(KERN_ERR "i2c_write_cst816x_any: i2c_transfer error\n"); printk(KERN_ERR "i2c_write_cst816x_any: i2c_transfer error\n");
kfree(buffer); kfree(buffer);
@ -270,7 +271,7 @@ static int i2c_write_cst816x_any(struct i2c_client *cst816x_client, u16 address,
} }
/*字符设备操作函数集*/ /*字符设备操作函数集*/
static struct file_operations cst816x_chr_dev_fops = static struct file_operations mpu6050_chr_dev_fops =
{ {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.open = NULL, .open = NULL,
@ -293,7 +294,7 @@ static void button_work_fn(struct work_struct *work)
uint16_t touch_coordinate_x = 0; uint16_t touch_coordinate_x = 0;
uint16_t touch_coordinate_y = 0; uint16_t touch_coordinate_y = 0;
i2c_read_cst816x(cst816x_client, 0x00, &touch_tmp[0], 7); i2c_read_cst816x(mpu6050_client, 0x00, &touch_tmp[0], 7);
// 解析坐标值 // 解析坐标值
touch_coordinate_x = ((uint16_t)(touch_tmp[3] & 0x0f) << 8) + (uint16_t)touch_tmp[4]; touch_coordinate_x = ((uint16_t)(touch_tmp[3] & 0x0f) << 8) + (uint16_t)touch_tmp[4];
@ -330,22 +331,7 @@ static void btn_close(struct input_dev *i_dev)
pr_info("input device closed()\n"); pr_info("input device closed()\n");
} }
static void reset_cst816x(int reset_io)
{
gpio_direction_output(reset_io, 0);
usleep_range(500000, 550000); // 精确延时 50~55ms
gpio_direction_output(reset_io, 1);
usleep_range(500000, 550000); // 精确延时 50~55ms
}
// static uint8_t check_id(uint8_t iic_addr)
// {
// }
// static int update_cst816()
// {
// }
/*----------------平台驱动函数集-----------------*/ /*----------------平台驱动函数集-----------------*/
static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *id) static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *id)
@ -390,10 +376,10 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
if (IS_ERR(irq_gpio)) if (IS_ERR(irq_gpio))
return -ENODEV; return -ENODEV;
ret = alloc_chrdev_region(&cst816x_devno, 0, DEV_CNT, DEV_NAME); ret = alloc_chrdev_region(&mpu6050_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0) if (ret < 0)
{ {
printk("fail to alloc cst816x_devno\n"); printk("fail to alloc mpu6050_devno\n");
goto alloc_err; goto alloc_err;
} }
@ -443,11 +429,11 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
//关联字符设备结构体cdev与文件操作结构体file_operations //关联字符设备结构体cdev与文件操作结构体file_operations
cst816x_chr_dev.owner = THIS_MODULE; mpu6050_chr_dev.owner = THIS_MODULE;
cdev_init(&cst816x_chr_dev, &cst816x_chr_dev_fops); cdev_init(&mpu6050_chr_dev, &mpu6050_chr_dev_fops);
// 添加设备至cdev_map散列表中 // 添加设备至cdev_map散列表中
ret = cdev_add(&cst816x_chr_dev, cst816x_devno, DEV_CNT); ret = cdev_add(&mpu6050_chr_dev, mpu6050_devno, DEV_CNT);
if (ret < 0) if (ret < 0)
{ {
printk("fail to add cdev\n"); printk("fail to add cdev\n");
@ -455,56 +441,63 @@ static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *
} }
/*创建类 */ /*创建类 */
class_cst816x = class_create(THIS_MODULE, DEV_NAME); class_mpu6050 = class_create(THIS_MODULE, DEV_NAME);
/*创建设备 DEV_NAME 指定设备名,*/ /*创建设备 DEV_NAME 指定设备名,*/
device_cst816x = device_create(class_cst816x, NULL, cst816x_devno, NULL, DEV_NAME); device_mpu6050 = device_create(class_mpu6050, NULL, mpu6050_devno, NULL, DEV_NAME);
cst816x_client = client; mpu6050_client = client;
reset_cst816x(io_cst816d_reset);//复位chip gpio_direction_output(io_cst816d_reset, 0);
// usleep_range(10000, 12000); // 精确延时 50~55ms
usleep_range(500000, 550000); // 精确延时 50~55ms
gpio_direction_output(io_cst816d_reset, 1);
usleep_range(500000, 550000); // 精确延时 50~55ms
cst816x_client->addr = 0x15; mpu6050_client->addr = 0x15;
if(i2c_read_cst816x(cst816x_client, 0xA7, &chip_id, 1)!=0) if(i2c_read_cst816x(mpu6050_client, 0xA7, &chip_id, 1)!=0)
{ {
printk(KERN_EMERG "\n We need to update the firmware on the Touch chip \n"); printk(KERN_EMERG "\n We need to update the firmware on the Touch chip \n");
cst816x_client->addr = 0x6A; mpu6050_client->addr = 0x6A;
gpio_direction_output(io_cst816d_reset, 0); gpio_direction_output(io_cst816d_reset, 0);
usleep_range(10000, 12000); // 精确延时 50~55ms usleep_range(10000, 12000); // 精确延时 50~55ms
gpio_direction_output(io_cst816d_reset, 1); gpio_direction_output(io_cst816d_reset, 1);
usleep_range(10000, 12000); // 精确延时 50~55ms usleep_range(10000, 12000); // 精确延时 50~55ms
chip_id = 0xab; chip_id = 0xab;
i2c_write_16bit_address(cst816x_client, 0xA001 , chip_id); i2c_write_16bit_address(mpu6050_client, 0xA001 , chip_id);
usleep_range(2000, 3000); // 精确延时 50~55ms usleep_range(2000, 3000); // 精确延时 50~55ms
i2c_read_cst816x_16bit(cst816x_client, 0xA003, &chip_id, 1); i2c_read_cst816x_16bit(mpu6050_client, 0xA003, &chip_id, 1);
// usleep_range(10000, 12000); // 精确延时 50~55ms // usleep_range(10000, 12000); // 精确延时 50~55ms
// i2c_read_cst816x(cst816x_client, 0xab, &chip_id, 1); // i2c_read_cst816x(mpu6050_client, 0xab, &chip_id, 1);
usleep_range(2000, 3000); // 精确延时 50~55ms usleep_range(2000, 3000); // 精确延时 50~55ms
chip_id = 0xab; chip_id = 0xab;
i2c_write_16bit_address(cst816x_client, 0xA001 , chip_id); i2c_write_16bit_address(mpu6050_client, 0xA001 , chip_id);
usleep_range(2000, 3000); // 精确延时 50~55ms usleep_range(2000, 3000); // 精确延时 50~55ms
for(i=0;i<30;i++) for(i=0;i<30;i++)
{ {
buff[0] = startaddr&0xff; buff[0] = startaddr&0xff;
buff[1] = startaddr>>8; buff[1] = startaddr>>8;
i2c_write_cst816x_any(cst816x_client, 0xA014,&buff[0],2); i2c_write_cst816x_any(mpu6050_client, 0xA014,&buff[0],2);
usleep_range(2000, 3000); // 精确延时 50~55ms usleep_range(2000, 3000); // 精确延时 50~55ms
i2c_write_cst816x_512bytes(cst816x_client,0xA018,&app_bin[6+startaddr],512); i2c_write_cst816x_512bytes(mpu6050_client,0xA018,&app_bin[6+startaddr],512);
usleep_range(4000,5000); // 精确延时 50~55ms usleep_range(4000,5000); // 精确延时 50~55ms
chip_id = 0xee; chip_id = 0xee;
i2c_write_16bit_address(cst816x_client, 0xA004 , chip_id); i2c_write_16bit_address(mpu6050_client, 0xA004 , chip_id);
usleep_range(100000, 120000); // 精确延时 50~55ms usleep_range(100000, 120000); // 精确延时 50~55ms
startaddr+=512; startaddr+=512;
} }
gpio_direction_output(io_cst816d_reset, 0);
usleep_range(10000, 12000); // 精确延时 50~55ms
gpio_direction_output(io_cst816d_reset, 1);
usleep_range(500000, 550000); // 精确延时 50~55ms
usleep_range(10000, 12000); // 精确延时 50~55ms
reset_cst816x(io_cst816d_reset);//复位chip i2c_read_cst816x(mpu6050_client, 0xA7, &chip_id, 1);
i2c_read_cst816x(cst816x_client, 0xA7, &chip_id, 1);
printk(KERN_EMERG "\n chip_id:%X \n",chip_id); printk(KERN_EMERG "\n chip_id:%X \n",chip_id);
}else{ }else{
@ -533,7 +526,7 @@ err_free_input:
return ret; return ret;
add_err: add_err:
// 添加设备失败时,需要注销设备号 // 添加设备失败时,需要注销设备号
unregister_chrdev_region(cst816x_devno, DEV_CNT); unregister_chrdev_region(mpu6050_devno, DEV_CNT);
printk(KERN_EMERG "\n error! \n"); printk(KERN_EMERG "\n error! \n");
alloc_err: alloc_err:
@ -552,7 +545,6 @@ static int cst816d_remove(struct i2c_client *client)
flush_workqueue(button_wq); flush_workqueue(button_wq);
destroy_workqueue(button_wq); destroy_workqueue(button_wq);
reset_cst816x(io_cst816d_reset);//复位chip
/* 1. 注销输入子系统 */ /* 1. 注销输入子系统 */
input_unregister_device(priv->button_input_dev); input_unregister_device(priv->button_input_dev);
@ -563,10 +555,10 @@ static int cst816d_remove(struct i2c_client *client)
gpiod_put(priv->Interrupt_gpiod); gpiod_put(priv->Interrupt_gpiod);
/* 4. 清除字符设备相关资源 */ /* 4. 清除字符设备相关资源 */
device_destroy(class_cst816x, cst816x_devno); device_destroy(class_mpu6050, mpu6050_devno);
class_destroy(class_cst816x); class_destroy(class_mpu6050);
cdev_del(&cst816x_chr_dev); cdev_del(&mpu6050_chr_dev);
unregister_chrdev_region(cst816x_devno, DEV_CNT); unregister_chrdev_region(mpu6050_devno, DEV_CNT);
return 0; return 0;
} }
@ -591,19 +583,19 @@ static const struct i2c_device_id gtp_device_id[] = {
{}}; {}};
/*定义设备树匹配表*/ /*定义设备树匹配表*/
static const struct of_device_id cst816x_of_match_table[] = { static const struct of_device_id mpu6050_of_match_table[] = {
{.compatible = "Yura,cst816d"}, {.compatible = "Yura,cst816d"},
{/* sentinel */}}; {/* sentinel */}};
/*定义i2c总线设备结构体*/ /*定义i2c总线设备结构体*/
struct i2c_driver cst816x_driver = { struct i2c_driver mpu6050_driver = {
.probe = cst816d_probe, .probe = cst816d_probe,
.remove = cst816d_remove, .remove = cst816d_remove,
.id_table = gtp_device_id, .id_table = gtp_device_id,
.driver = { .driver = {
.name = "Yura,cst816d", .name = "Yura,cst816d",
.owner = THIS_MODULE, .owner = THIS_MODULE,
.of_match_table = cst816x_of_match_table, .of_match_table = mpu6050_of_match_table,
}, },
}; };
@ -611,25 +603,25 @@ struct i2c_driver cst816x_driver = {
/* /*
*驱动初始化函数 *驱动初始化函数
*/ */
static int __init cst816x_driver_init(void) static int __init mpu6050_driver_init(void)
{ {
int ret; int ret;
pr_info("cst816x_driver_init\n"); pr_info("mpu6050_driver_init\n");
ret = i2c_add_driver(&cst816x_driver); ret = i2c_add_driver(&mpu6050_driver);
return ret; return ret;
} }
/* /*
*驱动注销函数 *驱动注销函数
*/ */
static void __exit cst816x_driver_exit(void) static void __exit mpu6050_driver_exit(void)
{ {
pr_info("cst816x_driver_exit\n"); pr_info("mpu6050_driver_exit\n");
i2c_del_driver(&cst816x_driver); i2c_del_driver(&mpu6050_driver);
} }
module_init(cst816x_driver_init); module_init(mpu6050_driver_init);
module_exit(cst816x_driver_exit); module_exit(mpu6050_driver_exit);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");