内核模块test.c的代码:
#include <linux/gpio/consumer.h> // gpiod_set_value
#include <linux/module.h> // MODULE_AUTHOR, MODULE_LICENSE
#include <linux/of.h> // of_match_ptr
#include <linux/platform_device.h> // module_platform_driver
static int mytestdriver_probe(struct platform_device *pdev)
{
const char *str;
int ret;
struct gpio_desc *gpio;
dev_err(&pdev->dev, "probe");
gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_HIGH); // led on
if (IS_ERR(gpio))
{
dev_err(&pdev->dev, "gpio error\n");
return -1;
}
platform_set_drvdata(pdev, gpio);
ret = of_property_read_string(pdev->dev.of_node, "xxxyyy", &str);
if (ret == 0)
dev_err(&pdev->dev, "str=%s\n", str);
return 0;
}
static int mytestdriver_remove(struct platform_device *pdev)
{
struct gpio_desc *gpio;
dev_err(&pdev->dev, "remove");
gpio = platform_get_drvdata(pdev);
if (gpio != NULL)
{
gpiod_set_value(gpio, 0); // led off
platform_set_drvdata(pdev, NULL);
}
return 0;
}
static const struct of_device_id mytestdriver_match_ids[] = {
{.compatible = "mytest,myboard_20250314"},
{}
};
static struct platform_driver mytestdriver = {
.driver = {
.name = "mytestdriver",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(mytestdriver_match_ids)
},
.probe = mytestdriver_probe,
.remove = mytestdriver_remove
};
module_platform_driver(mytestdriver);
MODULE_AUTHOR("Oct1158");
MODULE_LICENSE("GPL");