首页 > 系统相关 >01-[Linux][GPIO]GPIO编程示例代码

01-[Linux][GPIO]GPIO编程示例代码

时间:2023-07-26 13:34:01浏览次数:34  
标签:sample 01 input 示例 high low GPIO gpio gsample

基于MTK平台的Android Linux驱动

1、DTS配置如下

gpio_sample: gpio_sample {
        compatible = "mediatek,gpio-sample";
        input,high-gpio = <&pio 77 GPIO_ACTIVE_HIGH>;
        input,low-gpio = <&pio 70 GPIO_ACTIVE_HIGH>;
        output-gpio = <&pio 78 GPIO_ACTIVE_HIGH>;
        irq-gpio = <&pio 10 GPIO_ACTIVE_HIGH>;
        status = "okay";
};

2、驱动文件如下

#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/fcntl.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>


// #define USE_DEVM_GPIO_API
#define USE_OF_GPIO_API

struct gpio_sample {
#ifdef USE_DEVM_GPIO_API
	struct gpio_desc *input_low;
	struct gpio_desc *input_high;
	struct gpio_desc *output;
#endif

#ifdef USE_OF_GPIO_API
	int input_low;
	int input_high;
	int output;
#endif
};

static int gpio_sample_dts_parse(struct device *dev,
				 struct gpio_sample *gsample)
{
	int ret = 0;
#ifdef USE_DEVM_GPIO_API
	/* 1.input */
	gsample->input_high = devm_gpiod_get(dev, "input,high", GPIOD_IN);
	if (IS_ERR(gsample->input_low))
		return PTR_ERR(gsample->input_high);

	gsample->input_low = devm_gpiod_get(dev, "input,low", GPIOD_IN);
	if (IS_ERR(gsample->input_low))
		return PTR_ERR(gsample->input_low);

	/* 2.output */
	gsample->output = devm_gpiod_get(dev, "output", GPIOD_OUT_LOW);
	if (IS_ERR(gsample->output))
		return PTR_ERR(gsample->output);

#endif /* USE_DEVM_GPIO_API */

#ifdef USE_OF_GPIO_API
	// input low
	gsample->input_low = of_get_named_gpio(dev->of_node,
					       "input,low-gpio", 0);
	if (!gpio_is_valid(gsample->input_low)) {
		pr_err("596 Missing dt property: input,low-gpio\n");
		return -EINVAL;
	}
	ret = devm_gpio_request_one(dev, gsample->input_low, GPIOF_IN, 
				    "input_low_gpio");
	if (ret) {
		pr_err("596 failed to get input_low gpio\n");
		return ret;
	}

	// input high
	gsample->input_high = of_get_named_gpio(dev->of_node,
						"input,high-gpio", 0);
	if (!gpio_is_valid(gsample->input_low)) {
		pr_err("596 Missing dt property: input,high-gpio\n");
		return -EINVAL;
	}
	ret = devm_gpio_request_one(dev, gsample->input_high, GPIOF_IN,
				    "input_high_gpio");
	if (ret) {
		pr_err("596 failed to get input_high gpio\n");
		return ret;
	}
	pr_info("596 gsample->input_low = %d, gsample->input_high = %d\n",
		gsample->input_low, gsample->input_high);

	// output gpio
	gsample->output = of_get_named_gpio(dev->of_node, "output-gpio", 0);
	if (!gpio_is_valid(gsample->output)) {
		pr_err("596 Missing dt property: output-gpio\n");
		return -EINVAL;
	}
	ret = devm_gpio_request_one(dev, gsample->output, GPIOF_OUT_INIT_LOW,
				    "output_gpio");
	if (ret) {
		pr_err("596 failed to get output gpio\n");
		return ret;
	}
	
#endif /* USE_OF_GPIO_API */

	return 0;
}

static int gpio_sample_operation(struct gpio_sample *gsample)
{
	int in_low_val = 0, in_high_val = 0;

#ifdef USE_DEVM_GPIO_API
	in_low_val = gpiod_get_value(gsample->input_low);
	in_high_val = gpiod_get_value(gsample->input_high);

	// set gpio output high
	gpiod_set_value(gsample->output, 1);
#endif /* USE_DEVM_GPIO_API */

#ifdef USE_OF_GPIO_API
	if (gpio_is_valid(gsample->input_low)) {
		in_low_val = gpio_get_value(gsample->input_low);
	}

	if (gpio_is_valid(gsample->input_high)) {
		in_high_val = gpio_get_value(gsample->input_high);
	}

	gpio_set_value(gsample->output, 1);
#endif
	pr_info("596 gpio_sample_get_value in_low_val = %d," 
		"in_high_val = %d\n", in_low_val, in_high_val);

	return 0;
}

static int gpio_sample_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct gpio_sample *gsample;
	int ret = 0;

	gsample = devm_kzalloc(dev, sizeof(*gsample), GFP_KERNEL);
	if (!gsample)
		return -ENOMEM;
	
	/* 1. Parse dts and init */
	ret = gpio_sample_dts_parse(dev, gsample);
	if (ret) {
		pr_err("dts parse err!");
		return ret;
	}

	/* 2. Get gpio value */
	gpio_sample_operation(gsample);

	return 0;
}

static int gpio_sample_remove(struct platform_device *dev)
{
	pr_info("596 gpio_sample_remove enter.\n");
	return 0;
}

const struct of_device_id gpio_sample_table[] = {
	{ .compatible = "mediatek,gpio-sample" },
	{}
};

static struct platform_driver gpio_sample_driver = {
	.probe = gpio_sample_probe,
	.remove = gpio_sample_remove,
	.driver = {
		.name = "gpio_sample",
		.of_match_table = gpio_sample_table,
	},
};

module_platform_driver(gpio_sample_driver);

MODULE_LICENSE("GPL");

标签:sample,01,input,示例,high,low,GPIO,gpio,gsample
From: https://www.cnblogs.com/hkcs596/p/17582212.html

相关文章

  • Unsupervised Learning of Depth and Ego-Motion from Video(CVPR2017)论文阅读
    深度估计问题 从输入的单目或双目图像,计算图像物体与摄像头之间距离(输出距离图),双目的距离估计应该是比较成熟和完善,但往单目上考虑主要还是成本的问题,所以做好单目的深度估计有一定的意义。单目的意思是只有一个摄像头,同一个时间点只有一张图片。就象你闭上一只眼睛,只用一......
  • 替代GSV6201方案 集睿致远芯片CS5466 Type-c转HDMI8K高刷方案 CS5466完美代替RTD2173
    GSV6201基石是国内首款TPYEC转HDMI8K芯片。随着视频采集及显示设备日新月异的发展,用户对于高画质及低延时的观感体验追求越来越高,HDMI2.1传输技术的出现让这一切成为可能;它可以在动态帧率变化、高动态范围(HDR)和更多的音频传输方式比如eARC等方面实现提升,可以JIA一下幺三6玖二二72......
  • SerfJ REST框架的示例代码
    [1].[代码]web.xml01 <servlet>02 <servlet-name>RestServlet</servlet-name>03 <servlet-class>net.sf.serfj.RestServlet</servlet-class>04 <load-on-startup>5</load-on-startup>05 </servlet>06 0......
  • 013 学习笔记--锁
    锁:全局锁:锁定数据库中的所有表表级锁:每次操作锁住整张表行级锁:每次操作锁住对应的行数据1.概述锁是计算机协调多个进程或线程并发访问某一资源的机制。在数据库中,除传统的计算资源(CUP、RAM、IO)的争用以外,数据也是一种供许多用户共享的资源。如何保证数据并发访问的一致性......
  • 提示工程101|与 AI 交谈的技巧和艺术
    随着ChatGPT的问世,人工智能(AI)新时代也正式开启。ChatGPT是一种语言模型。它与用户进行对话交互,以便用户输入问题或提示,模型响应,然后对话可以继续来回进行,类似于在消息传递应用程序上向实际人员发送消息的方式。随着对AI的需求不断增长,为AI模型提供信息的能力也变得同样重要......
  • python学习01:Python基础语法与数据类型
    一、Python注释通常用于解释代码,这段打开主要是想表达什么意思,注释后的代码不会再代码中运行,例如:#打印HelloWorldprint("HelloWorld")注释的方式:#python注释(快捷键:Ctrl+/(选中你想注释的代码就可全部注释掉))=========>单行注释''''print('hello') ''''''�......
  • 01 linux基础(1)
    环境安装解压,从vmware打开虚拟机。设置密码:1打开终端:ctrl+alt+tlinux介绍Linux的发展1)1969年,由kenthompson在AT&T贝尔实验室实现的。使用的是汇编语言。2)1970年,KenThompson和DennisRitchie是使用C语言对整个系统进行了再加工和编写,是的Unix能够很容易的移植到其他硬件的......
  • 题解 BZOJ4543【[POI2014] HOT-Hotels】
    长链剖分优化DP板子题了,但是虽然是板子这个转移方程也很难想。problem树。求\(\sum_{1\leqi<j<k\leqn}[dist(i,j)=dist(i,k)=dist(j,k)].\)。\(n\leq10^5\)。solution与重链剖分相似,长链剖分是将树剖成很多条长链。我们定义长儿子,为一个点的儿子中子树深度最大的一个儿......
  • 01_HTML
    HTML1.什么是HTMLHTML全称:HyperTextMarkupLanguage(超文本标记语言)超文本:页面内可以包含图片、链接,甚至音乐、程序等非文字元素(超出文本的范畴);标记:标签,不同的标签实现不同的功能语言:人与计算机的交互工具2.HTML书写规范HTML元素标签用<>括起来,通常情况下<>表示开始......
  • MURF20100CTR-ASEMI快恢复对管封装、尺寸、参数
    编辑:llMURF20100CTR-ASEMI快恢复对管封装、尺寸、参数型号:MURF20100CTR品牌:ASEMI芯片个数:2芯片尺寸:102MIL*2封装:TO-220F恢复时间:50ns工作温度:-50°C~150°C浪涌电流:200A正向电流:20A反向耐压:1000V正向压降:1.10V引脚数量:2漏电流:>10uaMURF20100CTR二极管特性:MURF20100......