编写MTK6737平台的GPIO驱动例程(三)

article/2025/11/3 2:36:24

在原先的设备驱动文件上增加上更加方便的交互方式那就是sysfs接口,使用device_create_file用于在sys下创建设备的属性节点。

注意一下device_attribute可以使用一下两种方法初始化

 

1、使用DEVICE_ATTR初始化结构体device_attribute,下面代码示例:

static DEVICE_ATTR(demo, (S_IRUGO | S_IWUSR | S_IWGRP),demo_show,demo_store);
static DEVICE_ATTR(demo, 0444,demo_show,NULL);

其中参数:mode可以直接定义成只读0444,只写0222,或者读写都行的0666。。 
_show和_store函数当没有的时候用NULL赋值,对函数的名称和内容没有具体要求,甚至可以和别的属性相同。

2、也可以使用直接赋值的方法初始化device_attribute结构体,下面代码示例:

static struct device_attribute dev_attr_demo = {.attr = {.name = "demo", .mode = (S_IRUGO | S_IWUSR) },.show = demo_show,.store = demo_store,
};

下面上代码,其实就是增加了这么一个接口操作立马很容易了,你就可以不用写测试程序了。

#include <linux/slab.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <generated/autoconf.h>
#include <linux/platform_device.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/atomic.h>#include "mt-plat/mtgpio.h"
#include <linux/types.h>
#include <mt-plat/mt_gpio.h>
#include <mt-plat/mt_gpio_core.h>
#include <mach/gpio_const.h>#include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/seq_file.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/kobject.h>
#include <linux/regulator/consumer.h>/* 生命函数定义 */
static int mygpio_probe(struct platform_device *pdev);
static int mygpio_remove(struct platform_device *pdev);static ssize_t my_gpio96_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);struct pinctrl *pinctrlio96;
struct pinctrl_state *pio96_output0, *pio96_output1;static const struct of_device_id mygpio_of_match[] = {{ .compatible = "mykgpio", },{},
};static struct platform_driver mygpio_driver = {.remove = mygpio_remove,.probe = mygpio_probe,.driver = {.name = "myGPIO",.owner = THIS_MODULE,.of_match_table = mygpio_of_match,},
};static struct device_attribute mygpio96_attr = {.attr = {.name = "mygpio96",.mode = 0222,				/*mode可以直接定义成只读0444,只写0222,或者读写都行的0666。*/},.store  = &my_gpio96_store,
};/* 设置管教的状态  level=1 输出高电平 level=0 输出低电平*/
void my673x_gpio_output(int level)
{printk("[myGPIO]my673x_gpio_output level = %d\n", level);/* 设置名字为"my_state_io96_output0"这个pinctrl对应引脚的pin state */if (level==1)pinctrl_select_state(pinctrlio96, pio96_output1);if (level==0)pinctrl_select_state(pinctrlio96, pio96_output0);
}//属性文件的store方法(也就是写)
static ssize_t my_gpio96_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{int temp=0;sscanf(buf, "%d", &temp); 	/*将buf中的值以%d的形式赋值给temp*/printk("[myGPIO]my673x_gpio_output level = %d \n", temp);my673x_gpio_output(temp);return count;
}static int mygpio_misc_open(struct inode *inode, struct file *file)
{printk("MyGPIO OPen. \r\n");return 0;
}static int mygpio_misc_release(struct inode *inode, struct file *file)
{printk("MyGPIO Release. \r\n");return 0;
}static long mygpio_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{printk("MyGPIO Ioctl. \r\n");printk("MyGPIO cmd=%d \r\n", cmd);/* 根据命令执行相应的操作 */switch(cmd) {/* 输出GPIO96高电平 */case 1:my673x_gpio_output(1);break;/* 输出GPIO96低电平 */case 0:my673x_gpio_output(0);break;default:return -EINVAL;}return 0;
}static const struct file_operations mygpio_fops = {/* .owner = THIS_MODULE, */.open = mygpio_misc_open,.release = mygpio_misc_release,.unlocked_ioctl = mygpio_unlocked_ioctl,
};static struct miscdevice mygpio_misc_device = {.minor = MISC_DYNAMIC_MINOR,		//动态设备号.name = "myGPIO",.fops = &mygpio_fops,
};/* My GPIO probe */
static int mygpio_probe(struct platform_device *pdev)
{int ret = 0;printk("MyGPIO Probe. \r\n");/* 注册一个misc设备 */ret = misc_register(&mygpio_misc_device);if (ret != 0 )printk("myGPIO: mygpio_device register failed\n");/* 在/sys/devices/节点下创建名字是“mygpio96”设备节点 *//* 所生成的设备节点:/sys/devices/soc/soc:mygpio@10212000 *//* echo 0 > mygpio96 灯亮 | echo 1 > mygpio96 灯灭 */if (device_create_file(&pdev->dev, &mygpio96_attr))		//关联到哪个设备printk("[MyGPIO] Unable to create sysfs entry: '%s'\n", mygpio96_attr.attr.name);/* 获取pin control state holder 的句柄 */pinctrlio96 = devm_pinctrl_get(&pdev->dev);if (IS_ERR(pinctrlio96)) {ret = PTR_ERR(pinctrlio96);printk("fwq Cannot find mygpio pinctrlio96!\n");return ret;}/* dts中 pinctrl-names = "my_state_io96_output0", "my_state_io96_output1"; *//* 得到设备树中名字为 my_state_io96_output0和 my_state_io96_output1对应的pin state */pio96_output0 = pinctrl_lookup_state(pinctrlio96, "my_state_io96_output0");if (IS_ERR(pio96_output0)) {ret = PTR_ERR(pio96_output0);printk("fwq Cannot find touch pinctrl my_state_io96_output0!\n");return ret;}pio96_output1 = pinctrl_lookup_state(pinctrlio96, "my_state_io96_output1");if (IS_ERR(pio96_output1)) {ret = PTR_ERR(pio96_output1);printk("fwq Cannot find touch pinctrl my_state_io96_output1!\n");return ret;}return ret;
}static int mygpio_remove(struct platform_device *pdev)
{int err;printk("MyGPIO remove. \r\n");err = misc_deregister(&mygpio_misc_device);if (err)printk("deregister gpio\n");return err;
}static int __init my_gpio_init(void)
{int ret = 0;printk("Register MyGPIO platform_driver. \r\n");ret = platform_driver_register(&mygpio_driver);if(ret != 0 )printk("unable to register MyGPIO driver.\n");return ret;
}/*---------------------------------------------------------------------------*/
static void __exit my_gpio_exit(void)
{platform_driver_unregister(&mygpio_driver);
}subsys_initcall(my_gpio_init);
/*module_init(my_gpio_init);*/
module_exit(my_gpio_exit);MODULE_AUTHOR("zue");
MODULE_DESCRIPTION("MY General Purpose Driver (GPIO)");
MODULE_LICENSE("GPL v2");

在设备注册后会在/sys/devices/soc/soc:mygpio@10212000目录下生成一个设备节点,其实也可以使用find命令直接搜索你注册设备节点名称。

可以看到连接在96号管脚的LED会随着echo的1或者0灭或者亮。这样测试LED驱动就不用再次写测试程序了。

 


http://chatgpt.dhexx.cn/article/l88Y66an.shtml

相关文章

23.mtk6737上报键值更改

1.dws修改按键上报 2.找底层上报的对应键值标&#xff08;实体按键&#xff09; \alps_o1_mp1\update\alps\kst\drv\dct\dct\old_dct\Keypad_YuSu.cmp 3.映射键值给上层用 alps_o1_mp1\update\alps\device\mediateksample\k37mv1_bsp\mtk-kpd.kl 4上层按键的定义在\alps\fra…

MTK6737处理器,MT6737芯片技术资料下载

MTK6737处理器&#xff0c;MT6737芯片技术资料下载 了解MTK的都知道&#xff0c;联发科的芯片资料并不是很难寻得&#xff01;如果你还不知道在哪里可以下载的话&#xff0c;说明你已经不需要这些资料了&#xff0c;或者你用错了方式。既然讲到联发科&#xff0c;那就稍稍透露一…

andorid6.0 mtk6737平台 ctp调试方法

mtk平台上ctp调试很简单&#xff1a;搞定6根线就可以了&#xff0c;分别是地线&#xff0c;中断线&#xff0c;电源线&#xff0c;I2C线&#xff08;2根&#xff09;&#xff0c;reset复位线。 这里地线就不讨论了。从原理图可以知道CTP是i2c 设备&#xff0c;保证I2C通讯正常前…

MTK 6737 android6.0 user版本打开root权限(adb root权限和 apk root权限)

系列文章 MTK 6735/6739/6755/6763 android8.1 user版本打开root权限(adb root权限和 apk root权限) 相比较 android8.1 而言,6.0 的要简单很多 1、首先 6.0 不需要关闭 DM-verity,只需开发 adb root 后就能成功 remount,对 system 分区 rw 操作 2、6.0 无需添加在 init…

android studio无法识别mtk,Android6.0 MTK6737 长按 Power 没有真正关机 (MTK IPO 功能) · Younix’s Studio...

8种机械键盘轴体对比 本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f; Hardware:MT6737 DeviceOS:Android6.0 Kernel: Linux3.18 HostOS: Ubuntu16.04 正常开机 45s. 长按 Power 键进行关机后, 在 5s 内立即按住 Power 键进行开机 虽…

mtk android高级工具,Android6.0 MTK6737 DCT 工具介绍 · Younix’s Studio

8种机械键盘轴体对比 本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f; OS:Android6.0 Hardware:MTK6737 [TOC] MTK 的 DCT Driver Customization Tool MTK 为 GPIO、I2C 等配置制作了一个工具叫做 DCT &#xff0c; 可以直接在 UI 里…

[Android6.0][MTK6737] 启动流程分析

OS:Android6.0 Hardware:MTK6737 一、启动流程概览 1. BootRom2. Preloader3. LK4. Kernel5. Android 二、Bootloader 引导 2.1 preloader 启动过程 2.1.1 preloader 的功能preloader 中的硬件部分2.1.2 preloader 启动代码 2.2 LK 启动过程 2.2.1 LK 中的上电情景2.2.2 LK 中…

4G全网通MTK6737开发板-安卓核心板通信LTE模块方案

新移科技的XY001开发板(安卓智能核心板评估套件)&#xff0c;主要用于新移科技3G/4G/5G智能核心板的功能开发与测试。适用&#xff1a;XY6761、XY6853、XY8735 、XY6580、 XY6762、XY6763、XY6765、XY6771、XY8788 、XY6737全系列核心板。 核心板产品介绍&#xff1a; MT6737…

MTK6737功能展示

MT6737核心板处理器: QL168(6737) 具备低功耗特色的四核系统单芯片,是一款4G LTE 主要针对移动、联通、电信版本的全网通模块,SMT 封装。其性能稳定,外形紧凑,性价比高,功能强大,拥有良好的可扩展性和设计灵活性,能适用于各种产品设计需求 模块内置64 位4 核A53 架构…

HTML+CSS做登录注册界面

在登录界面点注册会进入注册界面 登录界面代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title></head> <body><div style"width:500px;float: …

HTML+CSS 登录界面设计

一、界面源代码 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>登录</title><style>bo…

HTML(css+div)登录界面

&#xff08;1&#xff09;这是当时做的一个课程设计&#xff0c;很多人想要图片&#xff0c;在这里我把用到的所有图片资源分享下 &#xff08;2&#xff09;链接&#xff1a;https://pan.baidu.com/s/1nUX1DQe_aDZQMf2Zbblm_A 提取码&#xff1a;twcp <!doctype…

HTML登录界面的实现详解

前言序锦 https://blog.csdn.net/HuaCode/article/details/81413387前阵子参加学校的暑期实习&#xff0c;我作为项目负责人&#xff0c;除了进行统筹规划&#xff0c;整体运营以及进度安排等工作外呢&#xff0c;我还负责了前端模块&#xff0c;参与并编写了前端页面&#xff…

html后台登录界面网页设计界面

一款简单的html后台登录界面网页设计界面带给大家。 效果图&#xff1a; html代码&#xff1a; <!DOCTYPE html> <html> <head><title>某某公司后台登录系统</title><link rel"stylesheet" href"css/style.css">&l…

HTML 实现好看的登录注册界面(一)

1. 效果图展示 2. 代码 2.1 HTML部分 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta http-equiv"X-UA-Compatible" content"IEedge" /><meta name"viewport" con…

HTML制作用户登录界面

1、index代码 <!doctype html> <html><head><meta charset"utf-8"><title>login</title><link rel"stylesheet" type"text/css" href"css/index.css"/></head><body><div&…

登录界面html源代码_网页制作:一个简易美观的登录界面

登录界面html源代码_网页制作&#xff1a;一个简易美观的登录界面 效果图 在我们做一个页面之前&#xff0c;要先想好他的一个整体布局&#xff0c;也就是我们这里面的login.html主页面&#xff0c;大致结构如下&#xff1a; 接下来&#xff0c;我们先上代码&#xff0c;看一…

HTML+CSS登陆界面实例

登录界面截图 项目代码仓库地址 项目的代码放在了github的代码仓库当中&#xff1a;点我 项目访问地址 将登录界面项目部署在了github上面&#xff1a;点我 项目代码解析 项目的界面简析 主要部分是Login的模块&#xff0c;包括username文本框和password文本框以及Login的…

HTML+CSS登录注册界面

目录 一、效果演示二、代码实现2.1、HTML主页面2.2、CSS代码2.3、JS代码 一、效果演示 [注]: 这里需要我们创建三个文件&#xff0c;一个是index.html文件&#xff0c;一个是style.css文件还有一个是login.js文件&#xff0c;把代码copy上去 只要路径找好就能正常运行了。 演示…

HTML+CSS登录界面

今天学习了一个HTML+CSS登录界面 效果图如下: 背景图片可以选取自己喜欢的,以下是实现代码: login.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet&quo…