linux dts gpio kernel example

1. dts configuration

./kernel_imx/arch/arm/boot/dts/imx6sl-evk.dts

......
&iomuxc {
        pinctrl-names = "default", "sleep";
        pinctrl-0 = <&pinctrl_hog>;
        pinctrl-1 = <&pinctrl_hog_sleep>;

        hog {
                pinctrl_hog: hoggrp {
                        fsl,pins = <
                                MX6SL_PAD_KEY_ROW7__GPIO4_IO07    0x17059
                                MX6SL_PAD_KEY_COL7__GPIO4_IO06    0x17059
                                MX6SL_PAD_SD2_DAT7__GPIO5_IO00    0x17059
                                MX6SL_PAD_SD2_DAT6__GPIO4_IO29    0x17059
                                MX6SL_PAD_REF_CLK_32K__GPIO3_IO22 0x17059
                                MX6SL_PAD_FEC_TX_CLK__GPIO4_IO21  0x80000000
                                MX6SL_PAD_KEY_ROW5__GPIO4_IO03    0x110b0
                                MX6SL_PAD_EPDC_VCOM0__GPIO2_IO03  0x80000000
                                MX6SL_PAD_EPDC_PWRSTAT__GPIO2_IO13 0x80000000
                                MX6SL_PAD_EPDC_PWRCTRL0__GPIO2_IO07 0x4001b0b0 /*luke modify 20161125*/
                                MX6SL_PAD_EPDC_PWRWAKEUP__GPIO2_IO14 0x80000000
                                MX6SL_PAD_EPDC_PWRINT__GPIO2_IO12 0x80000000
                                MX6SL_PAD_EPDC_PWRCTRL3__GPIO2_IO10 0x170b0
                                MX6SL_PAD_EPDC_PWRCTRL2__GPIO2_IO09 0x80000000
                                MX6SL_PAD_KEY_COL6__GPIO4_IO04    0x110b0
                                MX6SL_PAD_ECSPI2_MISO__GPIO4_IO14 0x17000
                                MX6SL_PAD_ECSPI2_MOSI__GPIO4_IO13 0x17000
                                MX6SL_PAD_ECSPI2_SS0__GPIO4_IO15  0x17000
                                MX6SL_PAD_FEC_RX_ER__GPIO4_IO19   0x1b0b0
                                MX6SL_PAD_LCD_RESET__GPIO2_IO19 0x1b0b0
                                MX6SL_PAD_KEY_COL4__GPIO4_IO00  0x80000000
                                MX6SL_PAD_KEY_COL5__GPIO4_IO02  0x80000000
                        >;
                };

                pinctrl_hog_sleep: hoggrp_sleep {
                        fsl,pins = <
                                MX6SL_PAD_KEY_ROW5__GPIO4_IO03    0x3080
                                MX6SL_PAD_KEY_COL6__GPIO4_IO04    0x3080
                                MX6SL_PAD_LCD_RESET__GPIO2_IO19   0x3080
                        >;
                };
        };
};
......

./kernel_imx/arch/arm/boot/dts/imx6sl-evk-common.dtsi

......
      /*luke add hello gpio test 20161125*/
       hello-led {
                 compatible = "firefly,hello_led";
                 led = <&gpio2 7 0>;
                 status = "okay";
         }; 
......

2. kernel module

2.1 add .c file

./kernel_imx/drivers/hello_led_module/hello.c

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h> 
#include <linux/delay.h>
#include <linux/gpio.h>

#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#endif


#define GPIO_LOW 0
#define GPIO_HIGH 1

static int firefly_hello_probe(struct platform_device *pdev)
{
    int ret = -1;
	int i;
    int gpio,flag;
	struct device_node *hello_node = pdev->dev.of_node;

	printk(KERN_INFO "%s() enter line:%d\n",__FUNCTION__,__LINE__);

	gpio = of_get_named_gpio_flags(hello_node,"led", 0,&flag);
	if (!gpio_is_valid(gpio)){
		printk(KERN_INFO "hello: invalid gpio : %d\n",gpio);
		return -1;
	} 
    ret = gpio_request(gpio, "hello_led");
	if (ret != 0) {
		gpio_free(gpio);
		printk(KERN_INFO "hello: invalid request : %d\n",gpio);
		return -EIO;
	}

	gpio_direction_output(gpio, GPIO_HIGH);

	for(i=0; i < 10; i++)
	{
		gpio_set_value(gpio,GPIO_LOW);
		mdelay(500);
		gpio_set_value(gpio,GPIO_HIGH);
		mdelay(500);
	}
    	
	printk(KERN_INFO "%s() exit line:%d\n",__FUNCTION__,__LINE__);
	
	return 0;  //return Ok
}


static int firefly_hello_remove(struct platform_device *pdev)
{ 
	struct device_node *hello_node = pdev->dev.of_node;
        int gpio,flag;
	gpio = of_get_named_gpio_flags(hello_node,"led", 0,&flag);
	if (!gpio_is_valid(gpio)){
		printk(KERN_INFO "hello: invalid gpio : %d\n",gpio);
		return -1;
	} 
        gpio_free(gpio);
    return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id of_firefly_hello_match[] = {
	{ .compatible = "firefly,hello_led" },
	{ /* Sentinel */ }
};
#endif

static struct platform_driver firefly_hello_driver = {
	.probe		= firefly_hello_probe,
	.remove		= firefly_hello_remove,
	.driver		= {
		.name	= "firefly_hello",
		.owner	= THIS_MODULE,
#ifdef CONFIG_OF
		.of_match_table	= of_match_ptr(of_firefly_hello_match),
#endif
	},

};

static int __init hello_init(void)
{
    printk(KERN_INFO "Enter %s\n", __FUNCTION__);
    return platform_driver_register(&firefly_hello_driver);
}

static void __exit hello_exit(void)
{
	platform_driver_unregister(&firefly_hello_driver);
    printk("Exit Hello world\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("sai <271319925@qq.com>");
MODULE_DESCRIPTION("Firefly hello driver");
MODULE_LICENSE("GPL");

ps: we can also ignore dev_node and of_get_named_gpio_flags() if use 37(GPIO2_IO07=32+7=39) directly.

2.2 add Makefile

./kernel_imx/drivers/hello_led_module/Makefile

PWD = $(shell pwd)
KERNEL_SRC = /home/xxx/Projects/Android/android4.4.3/kernel_imx/
CROSS_COMPILE = /home/xxx/Projects/Android/android4.4.3/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin/arm-linux-androideabi-

obj-m := hello.o
all:
        make ARCH=arm CFLAGS_MODULE=-fno-pic  CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_SRC) M=$(PWD) modules
clean:
        make ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_SRC) M=$(PWD) clean


2.3 compile

cd ./kernel_imx/driver/hello_module

make

3. result



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值