当前位置: 首页 > article >正文

基于高通MSM8953平台android9.0的GPIO驱动开发

2.1、注册设备:

2.1.1、添加编译选项:

1)、修改kernel/msm-4.9/drivers/leds下Makefile文件:

        obj-$(CONFIG_LED_GPIO) += led_gpio.o

2)、修改kernel/msm-4.9/drivers/leds下Kconfig文件:

        config LED_GPIO

                tristate "led_gpio"

2.1.2、修改驱动配置文件,驱动编译进系统:

        修改kernel/msm-4.9/arch/arm64/configs下的msm8953_defconfig文件:

                CONFIG_LED_GPIO=y

2.2、注册驱动:

2.2.1、修改dts设备树文件:

1)、文件名:msm8953-mtp-dtsi。

2)、文件路径:kernel/msm-4.9/arch/arm64/boot/dts/qcom/msm8953-mtp-dtsi

3)、在&soc下添加:

&soc {

led_gpio {

compatible = "led_gpio";

         led_gpio,gpio97 = <&tlmm 97 0x1>;

};

};

2.2.2、添加驱动:

在kernel/msm-4.9/drivers/leds下添加led_gpio.c文件:

#include <linux/types.h>

#include <linux/pm.h>

#include <linux/gpio.h>

#include <linux/slab.h>

#include <linux/init.h>

#include <linux/platform_device.h>

#include <linux/fsl_devices.h>

#include <asm/setup.h>

#include <linux/of.h>

#include <linux/of_gpio.h>

#include <linux/stat.h>

#include <linux/module.h>

#include <linux/err.h>

#include <linux/spinlock.h>

#include <linux/err.h>

#include <linux/regulator/consumer.h>

int led_gpio97 = -1;

static struct class * led_gpios_class = NULL;

static struct device * led_gpio97_dev = NULL;

#define CTL_LED_ON "1"

#define CTL_LED_OFF "0"

//使用 cat命令时候调用此函数

static ssize_t led_gpio97_show(struct device *dev,struct device_attribute *attr, char *buf)

{

int gpio_state = gpio_get_value(led_gpio97);

sprintf(buf, "%d\n", gpio_state);

return strlen(buf);

}

//使用 echo 命令时候调用此函数

static ssize_t led_gpio97_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)

{

if(!strncmp(buf, CTL_LED_ON, strlen(CTL_LED_ON))) {

gpio_set_value(led_gpio97, 1);

} else if(!strncmp(buf, CTL_LED_OFF, strlen(CTL_LED_OFF))) {

gpio_set_value(led_gpio97, 0);

}

return count;

}

static struct device_attribute led_gpio97_dev_attr = {

.attr = {

.name = "state",

.mode = S_IRWXU|S_IRWXG|S_IRWXO,

},

.show = led_gpio97_show,

.store = led_gpio97_store,

};

static int led_gpio_probe(struct platform_device *pdev)

{

int ret = 0;

printk("enter led_gpio_probe \n");

//寻找设备树dts下的 "led_gpio,gpio97" 名称对应的GPIO口

led_gpio97 = of_get_named_gpio(pdev->dev.of_node, "led_gpio,gpio97", 0);

if (led_gpio97 < 0)

printk("led_gpio97 is not available \n");

//对应修改的dts的 label

ret = gpio_request(led_gpio97, "led_gpio");

if(0 != ret) {

printk("gpio request %d failed.", led_gpio97);

goto fail1;

}

gpio_direction_output(led_gpio97, 0); //设置gpio为输出

//创建led_gpios节点

led_gpios_class = class_create(THIS_MODULE, "led_gpios");

if(IS_ERR(led_gpios_class))

{

ret = PTR_ERR(led_gpios_class);

printk("Failed to create class.\n");

return ret;

}

//创建led_gpio97 的驱动设备

led_gpio0_dev=device_create(led_gpios_class, NULL, 0, NULL, "led_gpio7");

if (IS_ERR(led_gpio97_dev))

{

ret = PTR_ERR(test_gpios_class);

printk("Failed to create device(led_gpio97_dev)!\n");

return ret;

}

//根据 led_gpio0_dev_attr 配置,生成相应的文件

ret = device_create_file(led_gpio97_dev, & led_gpio97_dev_attr);

if(ret)

{

pr_err("%s: led_gpio97 creat sysfs failed\n",__func__);

return ret;

}

printk("enter led_gpio_probe, ok \n");

fail1:

return ret;

}

static int led_gpio_remove(struct platform_device *pdev)

{

device_destroy(led_gpios_class, 0);

class_destroy(led_gpios_class);

device_remove_file(led_gpio97_dev, & led_gpio97_dev_attr);

return 0;

}

static int led_gpio_suspend(struct platform_device *pdev,pm_message_t state)

{

return 0;

}

static int led_gpio_resume(struct platform_device *pdev)

{

return 0;

}

static struct of_device_id led_gpio_dt_match[] = {

{ .compatible = "led_gpio",},

{ },

};

  

MODULE_DEVICE_TABLE(of, led_gpio_dt_match);

//驱动的方法与函数对应

static struct platform_driver gpio_led_driver = {

.driver = {

.name = "led_gpio",

.owner = THIS_MODULE,

.of_match_table = of_match_ptr(led_gpio_dt_match),

},

.probe = led_gpio_probe,

.remove = led_gpio_remove,

.suspend = led_gpio_suspend,

.resume = led_gpio_resume,

};

  

static __init int gpio_led_init(void)

{

return platform_driver_register(&gpio_led_driver);

}

static void __exit gpio_led_exit(void)

{

platform_driver_unregister(&gpio_led_driver);

}

  

module_init(gpio_led_init);

module_exit(gpio_led_exit);

MODULE_LICENSE("GPL");

2.3、编译系统:

source build/envsetup.sh

lunch msm8953_64-userdebug

make -j8


http://www.kler.cn/news/163332.html

相关文章:

  • Hbase JAVA API 增删改查操作
  • 【电子取证篇】汽车取证数据提取与汽车取证实例浅析(附标准下载)
  • imazing正在查找最新的apple mobile device组件
  • SpringBoot AOP切面实现对自定义注解的属性动态修改
  • 无重复字符的最长子串(LeetCode 3)
  • 记录一下Mac配置SpringBoot开发环境
  • “华为杯”研究生数学建模竞赛2016年-【华为杯】A题:无人机在抢险救灾中的优化运用(附获奖论文及MATLAB代码实现)
  • perf与火焰图-性能分析工具
  • IntelliJ IDEA使用Eval Reset
  • Unity使用打成图集的Sprite作为模型贴图使用的问题
  • ubuntu server 20.04 备份和恢复 系统 LTS
  • 小红书用户采集工具:掌握策略,轻松吸引潜在客户
  • 【flink番外篇】1、flink的23种常用算子介绍及详细示例(1)- map、flatmap和filter
  • 【链表Linked List】力扣-82 删除链表中的重复元素II
  • velocity-engine-core是什么?Velocity模板引擎的使用
  • pr抖音素材42个手机竖屏抖音视频转场特效PR剪辑模板
  • 浅谈5G基站节能及数字化管理解决方案的设计与应用-安科瑞 蒋静
  • 智慧城市是什么?为什么要建智慧城市?
  • 8_企业架构缓存中间件分布式memcached
  • 云原生系列1
  • 力扣(LeetCode)1038. 从二叉搜索树到更大和树(C++)
  • 卷积之后通道数为什么变了
  • java实现冒泡排序算法
  • 做题笔记:SQL Sever 方式做牛客SQL的题目--SQL156
  • 什么是Nginx反向代理?Nginx反向代理配置指南
  • Centos图形化界面封装OpenStack Centos镜像
  • Kubernetes(K8s)数据存储-09
  • c/c++中一些不常用但有用的知识
  • 【数据结构】插入排序,希尔排序,选择排序,堆排序,冒泡排序
  • 限流算法,基于go的gRPC 实现的