修改部分变量名称

This commit is contained in:
Yu Xifeng 2024-11-19 05:34:32 +00:00
parent a373e412c3
commit a0196904bb
2 changed files with 47 additions and 47 deletions

View File

@ -18,47 +18,47 @@
#include <linux/platform_device.h> #include <linux/platform_device.h>
/*------------------字符设备内容----------------------*/ /*------------------字符设备内容----------------------*/
#define DEV_NAME "wifi_power_en" #define DEV_NAME "cst816d"
#define DEV_CNT (1) #define DEV_CNT (1)
//定义字符设备的设备号 //定义字符设备的设备号
static dev_t led_devno; static dev_t cst816d_devno;
//定义字符设备结构体chr_dev //定义字符设备结构体chr_dev
static struct cdev led_chr_dev; static struct cdev cst816d_chr_dev;
struct class *class_led; //保存创建的类 struct class *class_cst816d; //保存创建的类
struct device *device; // 保存创建的设备 struct device *device; // 保存创建的设备
struct device_node *led_device_node; //led的设备树节点 struct device_node *cst816d_device_node; //cst816d的设备树节点
int led; // 保存获取得到引脚编号 int io_cst816d_reset; // 保存获取得到引脚编号
/*字符设备操作函数集,open函数*/ /*字符设备操作函数集,open函数*/
static int led_chr_dev_open(struct inode *inode, struct file *filp) static int cst816d_chr_dev_open(struct inode *inode, struct file *filp)
{ {
printk("open \n"); printk("open \n");
return 0; return 0;
} }
/*字符设备操作函数集,write函数*/ /*字符设备操作函数集,write函数*/
static ssize_t led_chr_dev_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt) static ssize_t cst816d_chr_dev_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{ {
unsigned char write_data; //用于保存接收到的数据 unsigned char write_data; //用于保存接收到的数据
int error = copy_from_user(&write_data, buf, cnt); int error = copy_from_user(&write_data, buf, cnt);
if(error < 0) { if(error < 0) {
printk("led_test write failed \n"); printk("cst816d_test write faicst816d \n");
return -1; return -1;
} }
/*设置GPIO 输出电平 具体引脚根据板卡原理图*/ /*设置GPIO 输出电平 具体引脚根据板卡原理图*/
if(write_data) if(write_data)
{ {
gpio_direction_output(led, 1); // 引脚输出高电平,红灯灭 gpio_direction_output(io_cst816d_reset, 1); // 引脚输出高电平,红灯灭
} }
else else
{ {
gpio_direction_output(led, 0); // 引脚输出底电平,红灯亮 gpio_direction_output(io_cst816d_reset, 0); // 引脚输出底电平,红灯亮
} }
return 0; return 0;
@ -66,15 +66,15 @@ static ssize_t led_chr_dev_write(struct file *filp, const char __user *buf, size
/*字符设备操作函数集*/ /*字符设备操作函数集*/
static struct file_operations led_chr_dev_fops = static struct file_operations cst816d_chr_dev_fops =
{ {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.open = led_chr_dev_open, .open = cst816d_chr_dev_open,
.write = led_chr_dev_write, .write = cst816d_chr_dev_write,
}; };
/*----------------平台驱动函数集-----------------*/ /*----------------平台驱动函数集-----------------*/
static int led_probe(struct platform_device *pdv) static int cst816d_probe(struct platform_device *pdv)
{ {
int ret = 0; //用于保存申请设备号的结果 int ret = 0; //用于保存申请设备号的结果
@ -82,37 +82,37 @@ static int led_probe(struct platform_device *pdv)
printk("match successed\n"); printk("match successed\n");
/*获取RGB的设备树节点*/ /*获取RGB的设备树节点*/
led_device_node = of_find_node_by_path("/wifi-pwrseq"); cst816d_device_node = of_find_node_by_path("/cst816t@15");
if(led_device_node == NULL) if(cst816d_device_node == NULL)
{ {
printk(KERN_EMERG "\t get led_test failed! \n"); printk(KERN_EMERG "\t get cst816t fail! \n");
} }
led = of_get_named_gpio(led_device_node, "reset-gpios", 0); io_cst816d_reset = of_get_named_gpio(cst816d_device_node, "reset-gpios", 0);
printk("led = %d \n",led); printk("cst816d = %d \n",io_cst816d_reset);
/*设置gpio输出高电平*/ /*设置gpio输出高电平*/
gpio_direction_output(led, 1); gpio_direction_output(io_cst816d_reset, 1);
/*---------------------注册 字符设备部分-----------------*/ /*---------------------注册 字符设备部分-----------------*/
//第一步 //第一步
//采用动态分配的方式,获取设备编号,次设备号为0, //采用动态分配的方式,获取设备编号,次设备号为0,
//DEV_CNT为1,当前只申请一个设备编号 //DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&led_devno, 0, DEV_CNT, DEV_NAME); ret = alloc_chrdev_region(&cst816d_devno, 0, DEV_CNT, DEV_NAME);
if(ret < 0){ if(ret < 0){
printk("fail to alloc led_devno\n"); printk("fail to alloc cst816d_devno\n");
goto alloc_err; goto alloc_err;
} }
//第二步 //第二步
//关联字符设备结构体cdev与文件操作结构体file_operations //关联字符设备结构体cdev与文件操作结构体file_operations
led_chr_dev.owner = THIS_MODULE; cst816d_chr_dev.owner = THIS_MODULE;
cdev_init(&led_chr_dev, &led_chr_dev_fops); cdev_init(&cst816d_chr_dev, &cst816d_chr_dev_fops);
//第三步 //第三步
//添加设备至cdev_map散列表中 //添加设备至cdev_map散列表中
ret = cdev_add(&led_chr_dev, led_devno, DEV_CNT); ret = cdev_add(&cst816d_chr_dev, cst816d_devno, DEV_CNT);
if(ret < 0) if(ret < 0)
{ {
printk("fail to add cdev\n"); printk("fail to add cdev\n");
@ -121,16 +121,16 @@ static int led_probe(struct platform_device *pdv)
//第四步 //第四步
/*创建类 */ /*创建类 */
class_led = class_create(THIS_MODULE, DEV_NAME); class_cst816d = class_create(THIS_MODULE, DEV_NAME);
/*创建设备*/ /*创建设备*/
device = device_create(class_led, NULL, led_devno, NULL, DEV_NAME); device = device_create(class_cst816d, NULL, cst816d_devno, NULL, DEV_NAME);
return 0; return 0;
add_err: add_err:
//添加设备失败时,需要注销设备号 //添加设备失败时,需要注销设备号
unregister_chrdev_region(led_devno, DEV_CNT); unregister_chrdev_region(cst816d_devno, DEV_CNT);
printk("\n error! \n"); printk("\n error! \n");
alloc_err: alloc_err:
@ -138,29 +138,29 @@ alloc_err:
} }
static const struct of_device_id led_ids[] = { static const struct of_device_id cst816d_ids[] = {
{ .compatible = "Yura,aic8800_usb_power_en"}, { .compatible = "Yura,cst816t"},
{ /* sentinel */ } { /* sentinel */ }
}; };
/*定义平台设备结构体*/ /*定义平台设备结构体*/
struct platform_driver led_platform_driver = { struct platform_driver cst816d_platform_driver = {
.probe = led_probe, .probe = cst816d_probe,
.driver = { .driver = {
.name = "leds-platform", .name = "cst816ds-platform",
.owner = THIS_MODULE, .owner = THIS_MODULE,
.of_match_table = led_ids, .of_match_table = cst816d_ids,
} }
}; };
/* /*
*驱动初始化函数 *驱动初始化函数
*/ */
static int __init led_platform_driver_init(void) static int __init cst816d_platform_driver_init(void)
{ {
int DriverState; int DriverState;
DriverState = platform_driver_register(&led_platform_driver); DriverState = platform_driver_register(&cst816d_platform_driver);
printk(KERN_EMERG "\tDriverState is %d\n",DriverState); printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
return 0; return 0;
@ -169,21 +169,21 @@ static int __init led_platform_driver_init(void)
/* /*
*驱动注销函数 *驱动注销函数
*/ */
static void __exit led_platform_driver_exit(void) static void __exit cst816d_platform_driver_exit(void)
{ {
printk(KERN_EMERG "led_test exit!\n"); printk(KERN_EMERG "cst816d_test exit!\n");
/*删除设备*/ /*删除设备*/
device_destroy(class_led, led_devno); //清除设备 device_destroy(class_cst816d, cst816d_devno); //清除设备
class_destroy(class_led); //清除类 class_destroy(class_cst816d); //清除类
cdev_del(&led_chr_dev); //清除设备号 cdev_del(&cst816d_chr_dev); //清除设备号
unregister_chrdev_region(led_devno, DEV_CNT); //取消注册字符设备 unregister_chrdev_region(cst816d_devno, DEV_CNT); //取消注册字符设备
platform_driver_unregister(&led_platform_driver); platform_driver_unregister(&cst816d_platform_driver);
} }
module_init(led_platform_driver_init); module_init(cst816d_platform_driver_init);
module_exit(led_platform_driver_exit); module_exit(cst816d_platform_driver_exit);
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");

Binary file not shown.