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