基于platform驱动模型完成LED驱动的编写

添加设备树文件信息

复制代码
myplatform{
    compatible="hqyj,myplatform";//厂商信息,用于驱动端进行匹配
    interrupt-parent=<&gpiof>;
//关联中断父节点
    interrupts=<9 0>;
//和中断父节点的关系描述符
    led1-gpio=<&gpioe 10 0>;
led2-gpio=<&gpiof 10 0>;
led3-gpio=<&gpioe 8 0>;
//gpio管脚描述符信息
    reg=<0X12345678 0x50>;
//地址信息
    };

pdrv.c

复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono[3];
int pdrv_probe(struct platform_device *pdev) // 当驱动和设备匹配成功后执行
{
    // 获取MEM类型的资源
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (res == NULL)
    {
        printk("获取MEM类型的资源失败\n");
        return -ENXIO;
    }
    // 获取中断类型资源
    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0)
    {
        printk("获取中断类型资源失败\n");
        return -ENXIO;
    }
    printk("MEM类型资源为%x\n", res->start);
    printk("中断类型资源为%d\n", irqno);
    // 获取gpio信息
    gpiono[0] = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_HIGH, NULL);
    if (IS_ERR(gpiono[0]))
    {
        printk("解析GPIO 0信息失败\n");
        return -PTR_ERR(gpiono[0]);
    }

    gpiono[1] = gpiod_get_from_of_node(pdev->dev.of_node, "led2-gpio", 0, GPIOD_OUT_HIGH, NULL);
    if (IS_ERR(gpiono[1]))
    {
        printk("解析GPIO 1信息失败\n");
        return -PTR_ERR(gpiono[1]);
    }

    gpiono[2] = gpiod_get_from_of_node(pdev->dev.of_node, "led3-gpio", 0, GPIOD_OUT_HIGH, NULL);
    if (IS_ERR(gpiono[2]))
    {
        printk("解析GPIO 2信息失败\n");
        return -PTR_ERR(gpiono[2]);
    }

    printk("解析GPIO信息成功,LED亮\n");
    return 0;
}

int pdrv_remove(struct platform_device *pdev) // 当设备和驱动分离时执行
{
    // 释放GPIO信息]

    gpiod_set_value(gpiono[0], 0);
    gpiod_put(gpiono[0]);

    gpiod_set_value(gpiono[1], 0);
    gpiod_put(gpiono[1]);

    gpiod_set_value(gpiono[2], 0);
    gpiod_put(gpiono[2]);
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
// 构建设备树匹配表
struct of_device_id oftable[] = {
    {
        .compatible = "hqyj,myplatform",
    },
    {
        .compatible = "hqyj,myplatform1",
    },
    {}, // 防止数组越界
};
// 1.分配驱动信息对象
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "bbbbb", // 驱动名
        .of_match_table = oftable,
    },

};
// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
相关推荐
AndyHeee12 天前
【SVC、PendSV(系统异常) 与 外设 IRQ 、NVIC笔记】
arm开发
暮云星影12 天前
瑞芯微rk3588利用Rockchip NPU运行大语言模型(LLM)
arm开发·人工智能·语言模型·自然语言处理
techdashen12 天前
绕过系统 ICMP:用 rawsock、Npcap 和 WMI 找到默认网卡
开发语言·arm开发·rust
振南的单片机世界12 天前
ARM中断比51快在哪?硬件压栈+NVIC集中管理
arm开发·stm32·单片机·嵌入式硬件
墨绿色的摆渡人12 天前
论文笔记(一百三十七)Learning Dual-Arm Push and Grasp Synergy in Dense Clutter
arm开发·论文阅读
暮云星影13 天前
全志linux开发屏幕适配(一)屏幕参数设置说明
linux·arm开发
m0_5474866613 天前
《ARM Cortex-M4嵌入式应用技术——基于STM32F407、STM32CubeMX与Proteus》全套PPT课件
arm开发·stm32·proteus
Lanceli_van13 天前
SQLite 3.45.2(sqlite-autoconf-3450200)ARM 交叉编译完整步骤
arm开发·sqlite
暮云星影13 天前
全志linux开发屏幕适配(二)`HDMI`驱动适配说明
linux·arm开发·驱动开发
暮云星影13 天前
瑞芯微rk3566开发FIT Secure Boot
linux·arm开发·驱动开发·安全