RK3288开发板PopMetal上的GPIO驱动实例

时间:2024-03-15 20:32:03

RK3288开发板该驱动需要涉及到的知识点:1,DTS设备树的作用,2,platform虚拟总线驱动的编写。
第一步,添加DTS节点
在/kernel/arch/arm/boot/dts/rockchip.dts下添加如下内容。
下图rockchip-leds-gpio这部分的内容,修改保存,
 RK3288开发板PopMetal上的GPIO驱动实例
第二步,在kernel/drivers下创建个LED文件夹,然后加入如下几个文件驱动文件leds.c,Makefile和Kconfig.如下图
源码:
/***********************************************************************************
* driver for led0
*
**********************************************************************************/
#include <linux/miscdevice.h>
#include <linux/input.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/of_platform.h>
static int leds_probe(struct platform_device *pdev)
{  int ret =-1
int i
int led
enum of_gpio_flags flag
struct device_node *led_node = pdev->dev.of_node
led = of_get_named_gpio_flags(led_node,”led-gpios”,0,&flag)
printk(“get gpio id successful\n”)
if(!gpio_is_valid(led)){
  printk(“invalid led-gpios: %d\n”,led)
  return -1
}
if(gpio_request(led,”led_gpio”)){
printk(“led gpio request failed!\n”)
return ret
}
  gpio_direction_output(led,1)
for(i=0 i < 10 i++)
{
  gpio_set_value(led,1)
  mdelay(500)
  gpio_set_value(led,0)
  mdelay(500)
  printk(“it’s %d\n”,i)
}
return 0
}
static int leds_remove(struct platform_device *pdev)
{
        return 0
}
static struct of_device_id leds_of_match[] = {
        { .compatible = “rockchip-leds-gpio” },
        { }
}
MODULE_DEVICE_TABLE(of, leds_of_match)
static struct platform_driver leds_driver = {
        .driver         = {
                .name           = “leds-drivers”,
                .owner          = THIS_MODULE,
                .of_match_table = of_match_ptr(leds_of_match),
        },
        .probe          = leds_probe,
        .remove         = leds_remove,
};
/*static int __init leds_init(void)
{
    printk(KERN_INFO “Enter %s\n”, __FUNCTION__)
    return platform_driver_register(&leds_driver)
    return 0
}
static void __exit leds_exit(void)
{
platform_driver_unregister(&leds_driver)
    printk(“close leds\n”)
}*/module_platform_driver(leds_driver)
module_platform_driver(leds_driver)
MODULE_DESCRIPTION(“leds Driver”)
MODULE_LICENSE(“GPL”)
MODULE_ALIAS(“platform:leds-drivers”)
/***********************************************************************************
* driver for led0
*
**********************************************************************************/
Kconfig:

RK3288开发板PopMetal上的GPIO驱动实例

Makefile:
RK3288开发板PopMetal上的GPIO驱动实例
第三步,修改drivers下的Kconfig和Makefile,修改内容如下
在Kconfig末尾添加:source “drivers/led/Kconfig”
在Makefile末尾添加: obj-$(CONFIG_LED0_TEST)  +=led/
第四步,编译新的kernel与resource并烧写进板子里,
然后DTS中定义的引脚就会按照驱动的内容,进行高低电平的变化。