linux设备树之gpio
程序员文章站
2024-01-23 15:03:10
...
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/platform_device.h>
#include <asm/io.h>
/* DTS
myled{
compatible = "led";
/* led2-5: gpx2_7 gpx1_0 gpf3_4 gpf3_5 *//*
gpios = <&gpx2 7 0>, <&gpx1 0 0>, <&gpf3 4 0>, <&gpf3 5 0>;
};
*/
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("a simple driver example!");
//create a platform driver
struct resource *res;
int led_pin;
int led_probe(struct platform_device *pdev)
{
int i = 0;
char name_buf[10] = {0};
printk("probe !\n");
// 获取DTS:gpios = <&gpx2 7 0>, <&gpx1 0 0>, <&gpf3 4 0>, <&gpf3 5 0>;(正式)
for(i = 0; i < 4; i++) {
// 从DTB解析管脚
led_pin = of_get_gpio(pdev->dev.of_node, i);
printk("led-gpio: %d\n", led_pin);
sprintf(name_buf, "led%d-pin", i);
devm_gpio_request(&pdev->dev, led_pin, name_buf);
// gpio_request(led_pin, name_buf);
gpio_direction_output(led_pin, 1);
gpio_set_value(led_pin, 1);
}
#if 0
// 获取DTS:reg = <0x11000c40 4>;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
printk("baseaddr: %#x\n", res->start);
// 获取DTS:pin = <7>; (非正式)
res = platform_get_resource_byname(pdev, 0, "pin");
printk("pinnum: %#x\n", res->start);
#endif
return 0;
}
int led_remove(struct platform_device *pdev)
{
printk("remove !\n");
return 0;
}
struct of_device_id led_table[] = {
{.compatible = "led"},
{}
};
struct platform_driver led_driver = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "11000c40.led_node",
.of_match_table = led_table
}
};
static int led_init(void)
{
printk("module install\n");
//add into platform bus
platform_driver_register(&led_driver);
return 0;
}
static void led_exit(void)
{
printk("module release\n");
//del from platform bus
platform_driver_unregister(&led_driver);
}
module_init(led_init);
module_exit(led_exit);
上一篇: c# timer定时执行任务
下一篇: 反转比特位(文章最后有干货)