20200409-02 platform基於設備樹配套 led 例程

設備樹

/{
	gpioled {
	        compatible = "atkalpha-gpioled";
	        #address-cells = <1>;
	        #size-cells = <1>;
	        pinctrl-names = "default";
	        pinctrl-0 = <&pinctrl_led>;
	        led-gpio = <&gpio4 14 GPIO_ACTIVE_LOW>;
	        status = "okay";
	};

};
&iomuxc {
	pinctrl_led: ledgrp {
	        fsl,pins = <
	                MX6UL_PAD_NAND_CE1_B__GPIO4_IO14        0x40017059
	        >;
	};
};

驅動

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define LEDDEV_CNT 1 //個數
#define LEDDEV_NAME "dstplatled"
#define LEDOFF 0
#define LEDON  1

struct leddev_dev {
    dev_t devid;    //設備號
    struct cdev cdev;       //cdev
    struct class *class;    //類
    struct device *device;  //設備
    int major;              //主設備號
    struct device_node *nd; //設備節點
    int led0;           //led gpio 編號
};

struct leddev_dev leddev;

void led0_switch(u8 sta)
{
    if (LEDON == sta) {
        gpio_set_value(leddev.led0, 1);
    } else {
        gpio_set_value(leddev.led0, 0);
    }
}

/*!
 * \brief led_open: 打開設備
 * \param inode: 傳遞給驅動的 inode
 * \param filp: 設備文件,file 結構體中的 private_data 一般需要在 open
 *  的時候將其指向設備結構體
 * \return 0:success 其他: fault
 */
static int led_open(struct inode* inode, struct file* filp)
{
    filp->private_data = &leddev;
    return 0;
}

static ssize_t led_write(struct file* filp, const char __user* buf,
                         size_t cnt, loff_t* offt)
{
    int retvalue = 0;
    unsigned char databuf[2];
    unsigned char ledstat = 0;

    retvalue = copy_from_user(databuf, buf, cnt);
    if (0 > retvalue) {
        printk("kernel write failed ! \n");
        return -EFAULT;
    }

    ledstat = databuf[0];
    if (LEDON == ledstat) {
        led0_switch(LEDON);
    } else {
        led0_switch(LEDOFF);
    }
    return 0;
}

static struct file_operations led_fops = {
    .owner = THIS_MODULE,
    .open = led_open,
    .write = led_write,
};

static int led_probe(struct platform_device* dev)
{
    printk("led driver and device was match! \n");

    //設置進程號
    if (leddev.major) {
        leddev.devid = MKDEV(leddev.major, 0);
        register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);
    } else {
        alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
        leddev.major = MAJOR(leddev.devid);
    }

    //註冊設備
    cdev_init(&leddev.cdev, &led_fops);
    cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);

    //創建類
    leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
    if (IS_ERR(leddev.class)) {
        return PTR_ERR(leddev.class);
    }
    //創建設備
    leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);
    if (IS_ERR(leddev.device)) {
        return PTR_ERR(leddev.device);
    }
    //初始化IO
    leddev.nd = of_find_node_by_path("/gpioled");
    if (NULL == leddev.nd) {
        printk("gpioled node nost find! \n");
        return -EINVAL;
    }
    leddev.led0 = of_get_named_gpio(leddev.nd, "led-gpio", 0);
    if (leddev.led0 < 0) {
        printk("can't get led-gpio \n");
        return -EINVAL;
    }

    gpio_request(leddev.led0, "led0");
    gpio_direction_output(leddev.led0, 1); //默認拉高
    return 0;
}

static int led_remove(struct platform_device *dev)
{
    gpio_set_value(leddev.led0, 0); //拉低

    cdev_del(&leddev.cdev);
    unregister_chrdev_region(leddev.devid, LEDDEV_CNT);
    device_destroy(leddev.class, leddev.devid);
    class_destroy(leddev.class);
    return 0;
}

//匹配表
static const struct of_device_id led_of_match[] = {
    { .compatible = "atkalpha-gpioled" },
    {   }
};
//platform 驅動結構體
static struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ul-led",           //驅動名稱,用於設備匹配
        .of_match_table = led_of_match, //設備樹匹配表
    },
    .probe = led_probe,
    .remove = led_remove,
};

static int __init led_init(void)
{
    return platform_driver_register(&led_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
}




module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zzz");

測試程序

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#define LEDOFF 0
#define LEDON  1

int main(int argc, char* argv[])
{
    int fd, retvalue;
    char* filename;
    unsigned char databuf[1];

    if (argc != 3) {
        printf("Error Usage! \n");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if (fd < 0) {
        printf("file %s open failed ! \n", argv[1]);
        return -1;
    }

    databuf[0] = atoi(argv[2]);

    retvalue = write(fd, databuf, sizeof(databuf));
    if (retvalue < 0) {
        printf("LED Control Failed ! \n");
        return -1;
    }

    retvalue = close(fd);
    if (retvalue < 0) {
        printf("file %s close failed !\n", argv[1]);
        return -1;
    }
    return 0;
}

Makefile

KERNELDIR := /home/x/workspace/01_linux_kernel/test/imx_4.1.15_2.0.0_ga_rc3/
CURRENT_PATH := $(shell pwd)
obj-m := devicetree_driver.o

build: kernel_modules

kernel_modules:
        $(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- EXTRA_CFLAGS=-fno-pic -C $(KERNELDIR) M=`pwd` modules 

clean:
        $(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=`pwd` clean


編譯

測試App // arm-linux-gnueabihf-gcc driver_test.c -o driver_test
驅動    // make -j32

執行

insmod devicetree_driver.ko
./driver_test /dev/dtsplatled 1
./driver_test /dev/dtsplatled 0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章