From 862c27fc9920c83318c784bfdadf43a65df1ec8f Mon Sep 17 00:00:00 2001 From: hc <hc@nodka.com> Date: Wed, 30 Aug 2023 01:56:27 +0000 Subject: [PATCH] 增加sysfs接口 --- kernel/arch/arm64/boot/dts/rockchip/NK-R36S0.dtsi | 3 + kernel/drivers/misc/nkio/nk_io_core.c | 100 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 93 insertions(+), 10 deletions(-) diff --git a/kernel/arch/arm64/boot/dts/rockchip/NK-R36S0.dtsi b/kernel/arch/arm64/boot/dts/rockchip/NK-R36S0.dtsi index e726885..92a194c 100755 --- a/kernel/arch/arm64/boot/dts/rockchip/NK-R36S0.dtsi +++ b/kernel/arch/arm64/boot/dts/rockchip/NK-R36S0.dtsi @@ -93,6 +93,9 @@ compatible = "nk_io_control"; pinctrl-names = "default"; pinctrl-0 = <&nk_io_gpio>; + + //gpio_op0 = <&gpio1 RK_PD0 GPIO_ACTIVE_HIGH>; + vcc_5v { gpio_num = <&gpio1 RK_PA4 GPIO_ACTIVE_HIGH>; //VCC5_IO_EN_GPIO1_A4_3V3 gpio_function = <0>; diff --git a/kernel/drivers/misc/nkio/nk_io_core.c b/kernel/drivers/misc/nkio/nk_io_core.c index 1e06855..e17fb86 100755 --- a/kernel/drivers/misc/nkio/nk_io_core.c +++ b/kernel/drivers/misc/nkio/nk_io_core.c @@ -12,15 +12,18 @@ #include <asm/uaccess.h> #include <linux/string.h> #include <linux/uaccess.h> +#include <linux/device.h> #define GPIO_FUNCTION_OUTPUT 0 #define GPIO_FUNCTION_INPUT 1 #define GPIO_FUNCTION_RESET 3 - +#define HIGH "1" +#define LOW "0" + static int flash_flag = 0; struct ndj_gpio { - int gpio_num; //gpui num + int gpio_num; //gpio num int action; //gpio flag int gpio_event; //input only int send_mode; //input only @@ -31,12 +34,66 @@ struct ndj_gpio_data { struct ndj_gpio ndj_gpio_num[20]; - struct input_dev *input; struct timer_list mytimer; int gpio_dts_num; + unsigned int gpio_op0; + unsigned int gpio_op1; }; - + static struct ndj_gpio_data *gpio_data = NULL; + +/* creat sysfs gpio api node*/ +#if 0 +static ssize_t gpio_op0_show(struct class *class,struct class_attribute *attr, char *buf) +{ + return sprintf(buf, "%d\n", gpio_get_value(gpio_data->gpio_op0)); +} + +static ssize_t gpio_op0_store(struct class *class,struct class_attribute *attr, const char *buf, size_t size) +{ + if(!strncmp(buf, HIGH, strlen(HIGH))) { + gpio_set_value(gpio_data->gpio_op0, 1); + + } else if(!strncmp(buf, LOW, strlen(LOW))) { + gpio_set_value(gpio_data->gpio_op0, 0); + } + return size; +} + +static ssize_t gpio_op1_show(struct class *class,struct class_attribute *attr, char *buf) +{ + return sprintf(buf, "%d\n", gpio_get_value(gpio_data->gpio_op1)); +} + +static ssize_t gpio_op1_store(struct class *class,struct class_attribute *attr, const char *buf, size_t size) +{ + if(!strncmp(buf, HIGH, strlen(HIGH))) { + gpio_set_value(gpio_data->gpio_op1, 1); + + } else if(!strncmp(buf, LOW, strlen(LOW))) { + gpio_set_value(gpio_data->gpio_op1, 0); + } + return size; +} + +static CLASS_ATTR_RW(gpio_op0); +static CLASS_ATTR_RW(gpio_op1); + +static struct attribute *ndj_gpio_attrs[] = { + &class_attr_gpio_op0.attr, + &class_attr_gpio_op1.attr, + NULL, +}; + +ATTRIBUTE_GROUPS(ndj_gpio); + +/** Device model classes */ +struct class ndj_io_class = { + .name = "io_control", //sysfs directory + .class_groups = ndj_gpio_groups, +}; + +#endif static int event_flag = 0; static int open_now = 0; static char* file_name = NULL; @@ -113,8 +170,6 @@ char gpio_name_num[20]; int gpio_in_cnt = 0; int cnt =0; - int lvds_index = 0; - int gpio0, gpio1, gpio2, gpio3; int i=0; enum of_gpio_flags flags; @@ -130,6 +185,23 @@ if (gpio_data->gpio_dts_num == 0){ dev_info(&pdev->dev, "no gpio defined\n"); } + + #if 0 + /*gpio_op0*/ + ret = of_get_named_gpio_flags(np, "gpio_op0", 0, &gpio_flags); + if (ret < 0) { + printk("%s() Can not read property gpio_op0\n", __func__); + return -ENODEV; + } + gpio_data->gpio_op0 = ret; + ret = gpio_request(gpio_data->gpio_op0, "gpio_op0"); + if(ret < 0){ + printk("%s() gpio_op0 request ERROR\n", __func__); + return -ENODEV; + } + + gpio_direction_output(gpio_data->gpio_op0,!gpio_flags); +#endif /* create node */ root_entry_gpio = proc_mkdir("ndj_gpio", NULL); @@ -161,9 +233,9 @@ gpio_direction_input(gpio_data->ndj_gpio_num[gpio_cnt].gpio_num); //gpio_direction_output(gpio_data->ndj_gpio_num[gpio_cnt].gpio_num,!gpio_data->ndj_gpio_num[gpio_cnt].action); - event_flag = gpio_flags; - of_property_read_u32(child_np, "send_mode", &(gpio_data->ndj_gpio_num[gpio_cnt].send_mode)); - of_property_read_u32(child_np, "gpio_event", &(gpio_data->ndj_gpio_num[gpio_cnt].gpio_event)); + //event_flag = gpio_flags; + //of_property_read_u32(child_np, "send_mode", &(gpio_data->ndj_gpio_num[gpio_cnt].send_mode)); + //of_property_read_u32(child_np, "gpio_event", &(gpio_data->ndj_gpio_num[gpio_cnt].gpio_event)); gpio_in_cnt++; } break; @@ -200,7 +272,14 @@ gpio_cnt++; } - platform_set_drvdata(pdev, gpio_data); + platform_set_drvdata(pdev, gpio_data); + +#if 0 + ret= class_register(&ndj_io_class); + if(ret < 0) { + return -EINVAL; + } +#endif return 0; } @@ -223,6 +302,7 @@ static int ndj_gpio_remove(struct platform_device *pdev) { +// class_unregister(&ndj_io_class); return 0; } -- Gitblit v1.6.2