linux_driver/cst816d_driver/cst816d.c

580 lines
17 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/platform_device.h> /*平台设备*/
#include "cst816_update.h"
/*------------------字符设备内容----------------------*/
#define DEV_NAME "cst816d"
#define DEV_CNT (1)
struct cst816x_data {
struct gpio_desc *Interrupt_gpiod; //GPIO
struct input_dev *button_input_dev; //按键输入子系统结构体
struct platform_device *pdev; //平台设备结构体
int irq; //中断irq
};
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的设备树节点结构体
/*------------------IIC设备内容----------------------*/
struct i2c_client *mpu6050_client = NULL; //保存mpu6050设备对应的i2c_client结构体,匹配成功后由.prob函数带回。
static int i2c_write_16bit_address(struct i2c_client *client, u16 address, u8 data)
{
int error;
u8 write_data[3]; // 两个字节的地址 + 一个字节的数据
struct i2c_msg send_msg;
/* 设置要发送的数据 */
write_data[0] = (address >> 8) & 0xFF; // 高字节
write_data[1] = address & 0xFF; // 低字节
write_data[2] = data; // 数据
/* 构造 I2C 消息 */
send_msg.addr = client->addr; // I2C 从设备地址
send_msg.flags = 0; // 写操作
send_msg.buf = write_data; // 数据缓冲区
send_msg.len = 3; // 数据长度(2 字节地址 + 1 字节数据)
/* 发送数据 */
error = i2c_transfer(client->adapter, &send_msg, 1);
if (error != 1)
{
printk(KERN_ERR "i2c_transfer error\n");
return -1;
}
return 0;
}
/*通过i2c 向mpu6050写入数据
*mpu6050_client:mpu6050的i2c_client结构体。
*address, 要读取的地址,
*data,保存读取得到的数据
*length,读长度
*返回值,错误,-1。成功,0
*/
static int i2c_read_cst816x(struct i2c_client *mpu6050_client, u8 address, void *data, u32 length)
{
int error = 0;
u8 address_data = address;
struct i2c_msg mpu6050_msg[2];
printk(KERN_EMERG "\n i2c_read addr:%X \n",mpu6050_client->addr);
/*设置读取位置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; //写入长度
/*设置读取位置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; //读取长度
error = i2c_transfer(mpu6050_client->adapter, mpu6050_msg, 2);
if (error != 2)
{
printk(KERN_EMERG "\n i2c_read_mpu6050 error \n");
return -1;
}
return 0;
}
/*通过i2c 向设备读取数据
* mpu6050_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)
{
int error = 0;
u8 address_data[2]; // 存储 16 位地址(高字节 + 低字节)
struct i2c_msg mpu6050_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 字节
/* 设置读取数据的消息 */
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; // 读取长度
/* 执行 I2C 传输 */
error = i2c_transfer(mpu6050_client->adapter, mpu6050_msg, 2);
if (error != 2)
{
printk(KERN_ERR "i2c_read_cst816x_16bit: i2c_transfer error\n");
return -1;
}
return 0;
}
/* 通过 I2C 向设备发送多字节数据(支持大数据量)。
* mpu6050_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)
{
int error = 0;
u32 chunk_size = 32; // 每次传输的最大字节数(根据硬件支持设置)
u32 offset = 0; // 当前传输偏移
u8 *buffer; // 动态分配缓冲区
struct i2c_msg msg;
/* 检查长度是否超出限制 */
if (length > 512) {
printk(KERN_ERR "i2c_write_cst816x_512bytes: length exceeds 512 bytes\n");
return -1;
}
/* 分配动态缓冲区 */
buffer = kmalloc(chunk_size + 2, GFP_KERNEL);
if (!buffer) {
printk(KERN_ERR "i2c_write_cst816x_512bytes: failed to allocate buffer\n");
return -1;
}
/* 初始化 I2C 消息 */
msg.addr = mpu6050_client->addr;
msg.flags = 0; // 写操作
while (offset < length) {
u32 bytes_to_send = min(chunk_size, length - offset); // 计算本次要发送的字节数
/* 设置 16 位寄存器地址 */
buffer[0] = (address >> 8) & 0xFF; // 高字节
buffer[1] = address & 0xFF; // 低字节
/* 复制当前块的数据到缓冲区 */
memcpy(&buffer[2], (u8 *)data + offset, bytes_to_send);
/* 设置消息 */
msg.buf = buffer;
msg.len = bytes_to_send + 2;
/* 执行 I2C 传输 */
error = i2c_transfer(mpu6050_client->adapter, &msg, 1);
if (error != 1) {
printk(KERN_ERR "i2c_write_cst816x_512bytes: i2c_transfer error at offset %u\n", offset);
kfree(buffer); // 释放动态缓冲区
return -1;
}
/* 更新偏移和寄存器地址(如果地址需递增) */
offset += bytes_to_send;
address += bytes_to_send; // 若设备地址自增
}
/* 释放动态缓冲区 */
kfree(buffer);
return 0;
}
/* 通过 I2C 向设备写入任意字节长度的数据。
* mpu6050_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)
{
int error = 0;
u8 *buffer; // 动态分配缓冲区
struct i2c_msg msg;
/* 检查输入参数合法性 */
if (!data || length == 0) {
printk(KERN_ERR "i2c_write_cst816x_any: invalid data or length\n");
return -1;
}
/* 分配缓冲区,包含寄存器地址(2 字节)+ 数据长度 */
buffer = kmalloc(length + 2, GFP_KERNEL);
if (!buffer) {
printk(KERN_ERR "i2c_write_cst816x_any: failed to allocate buffer\n");
return -1;
}
/* 设置缓冲区内容 */
buffer[0] = (address >> 8) & 0xFF; // 高字节
buffer[1] = address & 0xFF; // 低字节
memcpy(&buffer[2], data, length); // 数据拷贝
/* 设置 I2C 消息 */
msg.addr = mpu6050_client->addr;
msg.flags = 0; // 写操作
msg.buf = buffer;
msg.len = length + 2;
/* 执行 I2C 传输 */
error = i2c_transfer(mpu6050_client->adapter, &msg, 1);
if (error != 1) {
printk(KERN_ERR "i2c_write_cst816x_any: i2c_transfer error\n");
kfree(buffer);
return -1;
}
/* 释放缓冲区 */
kfree(buffer);
return 0;
}
/*字符设备操作函数集*/
static struct file_operations mpu6050_chr_dev_fops =
{
.owner = THIS_MODULE,
.open = NULL,
.read = NULL,
.release = NULL,
};
/*按键中断处理函数*/
static irqreturn_t button_input_irq_hander(int irq, void *dev_id)
{
// struct button_data *priv = dev_id;
// int button_satus;
// /*读取按键引脚的电平,根据读取得到的结果输入按键状态*/
// button_satus = (gpiod_get_value(priv->button_input_gpiod) & 1);
// if(button_satus)
// {
// input_report_key(priv->button_input_dev, BTN_0, 1);
// input_sync(priv->button_input_dev);
// }
// else
// {
// input_report_key(priv->button_input_dev, BTN_0, 0);
// input_sync(priv->button_input_dev);
// }
printk(KERN_EMERG "\t button_input_irq_hander \n");
return IRQ_HANDLED;
}
static int btn_open(struct input_dev *i_dev)
{
pr_info("input device opened()\n");
return 0;
}
static void btn_close(struct input_dev *i_dev)
{
pr_info("input device closed()\n");
}
/*----------------平台驱动函数集-----------------*/
static int cst816d_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct device_node *node=client->dev.of_node;
struct input_dev *i_dev;
struct cst816x_data *priv;
struct gpio_desc *irq_gpio;
int ret = -1; //保存错误状态码
char chip_id = 0;
int i=0;
u_int8_t buff[24];
u_int16_t startaddr = 0;
u_int16_t update_len = app_bin[3];
update_len <<= 8;
update_len |= app_bin[2];
printk(KERN_EMERG "\t match successed \n");
/* 分配输入设备 */
i_dev = input_allocate_device();
if (!i_dev) {
pr_err("CST816D: Failed to allocate input device\n");
return -ENOMEM;
}
/* 分配私有数据 */
priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
if (!priv) {
ret = -ENOMEM;
goto err_free_input;
}
i2c_set_clientdata(client, priv); // 绑定私有数据
io_cst816d_reset = of_get_named_gpio(node, "reset-gpios", 0);
printk(KERN_EMERG "cst816d = %d \n",io_cst816d_reset);
irq_gpio = devm_gpiod_get(&client->dev, "intirq", GPIOD_IN);
if (IS_ERR(irq_gpio))
return -ENODEV;
ret = alloc_chrdev_region(&mpu6050_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0)
{
printk("fail to alloc mpu6050_devno\n");
goto alloc_err;
}
i_dev->open = btn_open;
i_dev->close = btn_close;
i_dev->name = "key input";
i_dev->dev.parent = &client->dev;
priv->button_input_dev = i_dev;
priv->irq = gpiod_to_irq(irq_gpio);
priv->Interrupt_gpiod = irq_gpio;
set_bit(EV_KEY, i_dev->evbit); /*设置要使用的输入事件类型*/
set_bit(BTN_0, i_dev->keybit); /* 按钮 0*/
ret = input_register_device(priv->button_input_dev);
if (ret) {
pr_err("Failed to register inputdevice\n");
goto err_input;
}
ret = request_any_context_irq(priv->irq, button_input_irq_hander,IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button", priv);
if (ret < 0) {
dev_err(&client->dev,"请求gpio中断失败\n");
goto err_btn;
}
//关联字符设备结构体cdev与文件操作结构体file_operations
mpu6050_chr_dev.owner = THIS_MODULE;
cdev_init(&mpu6050_chr_dev, &mpu6050_chr_dev_fops);
// 添加设备至cdev_map散列表中
ret = cdev_add(&mpu6050_chr_dev, mpu6050_devno, DEV_CNT);
if (ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
/*创建类 */
class_mpu6050 = class_create(THIS_MODULE, DEV_NAME);
/*创建设备 DEV_NAME 指定设备名,*/
device_mpu6050 = device_create(class_mpu6050, NULL, mpu6050_devno, NULL, DEV_NAME);
mpu6050_client = client;
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
mpu6050_client->addr = 0x15;
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");
mpu6050_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);
usleep_range(2000, 3000); // 精确延时 50~55ms
i2c_read_cst816x_16bit(mpu6050_client, 0xA003, &chip_id, 1);
// usleep_range(10000, 12000); // 精确延时 50~55ms
// i2c_read_cst816x(mpu6050_client, 0xab, &chip_id, 1);
usleep_range(2000, 3000); // 精确延时 50~55ms
chip_id = 0xab;
i2c_write_16bit_address(mpu6050_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);
usleep_range(2000, 3000); // 精确延时 50~55ms
i2c_write_cst816x_512bytes(mpu6050_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);
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);
printk(KERN_EMERG "\n chip_id:%X \n",chip_id);
}else{
printk(KERN_EMERG "\n chip_id:%X \n",chip_id);
}
printk(KERN_EMERG "\n cst816d ok! \n");
return 0;
err_btn:
gpiod_put(priv->Interrupt_gpiod);
err_input:
input_free_device(priv->button_input_dev);
err_free_input:
input_free_device(i_dev);
return ret;
add_err:
// 添加设备失败时,需要注销设备号
unregister_chrdev_region(mpu6050_devno, DEV_CNT);
printk(KERN_EMERG "\n error! \n");
alloc_err:
return -1;
}
static int cst816d_remove(struct i2c_client *client)
{
struct cst816x_data *priv = i2c_get_clientdata(client); // 获取驱动私有数据
if (!priv)
return -EINVAL;
/* 1. 注销输入子系统 */
input_unregister_device(priv->button_input_dev);
/* 2. 释放中断 */
free_irq(priv->irq, priv);
/* 3. 释放 GPIO */
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);
return 0;
}
static const struct of_device_id cst816x_of_match[] = {
{ .compatible = "Yura,cst816t"},
{ /* sentinel */ }
};
static const struct i2c_device_id cst816x_id[] = {
{ .name = "cst816s", 0 },
{ }
};
// module_i2c_driver(cst816d_driver);
/*定义ID 匹配表*/
static const struct i2c_device_id gtp_device_id[] = {
{"Yura,cst816d", 0},
{}};
/*定义设备树匹配表*/
static const struct of_device_id mpu6050_of_match_table[] = {
{.compatible = "Yura,cst816d"},
{/* sentinel */}};
/*定义i2c总线设备结构体*/
struct i2c_driver mpu6050_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,
},
};
/*
*驱动初始化函数
*/
static int __init mpu6050_driver_init(void)
{
int ret;
pr_info("mpu6050_driver_init\n");
ret = i2c_add_driver(&mpu6050_driver);
return ret;
}
/*
*驱动注销函数
*/
static void __exit mpu6050_driver_exit(void)
{
pr_info("mpu6050_driver_exit\n");
i2c_del_driver(&mpu6050_driver);
}
module_init(mpu6050_driver_init);
module_exit(mpu6050_driver_exit);
MODULE_LICENSE("GPL");