添加设备树文件信息
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");