1. 程式人生 > >x4412 基於裝置樹的 hello_world驅動

x4412 基於裝置樹的 hello_world驅動

首先在exynos4412-x4412.dts檔案中新增HelloWorld節點,如下:

HelloWorld {
         compatible = "x4412, hello_world";
         status = "okay";
};

然後新建一個 hello_world.c 原始檔,具體原始碼如下:

/*
* Hello World Driver for X4412
*
* Copyright (c) 2018
* Author: YYH
* This is a simple module driver example for x4412 base on dts
*
*/

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of.h>

static int hello_world_probe(struct platform_device *pdev)
{
    pr_notice("Driver probe : %s\n", __func__);
    return 0;
}

static int hello_world_remove(struct platform_device *pdev)
{
    pr_notice("Driver remove :  %s\n", __func__);
    return 0;
}

static void hello_world_shutdown(struct platform_device *pdev)
{
    pr_notice("Driver shutdown :  %s\n", __func__);
    return ;
}

static int hello_world_suspend(struct platform_device *pdev, pm_message_t state)
{
    pr_notice("Driver suspend :  %s\n", __func__);
    return 0;
}

static int hello_world_resume(struct platform_device *pdev)
{
    pr_notice("Driver resume :  %s\n", __func__);
    return 0;
}

static const struct of_device_id tiny4412_hello_world_dt_match[] = {
{ 
    .compatible = "x4412,hello_world" },
    {},
};

static struct platform_driver x4412_hello_world_driver = {
    .probe = hello_world_probe,
    .remove = hello_world_remove,
    .shutdown = hello_world_shutdown,
    .suspend = hello_world_suspend,
    .resume = hello_world_resume,
    .driver = {
        .name = "hello world",
        .of_match_table = tiny4412_hello_world_dt_match,
    },

};

module_platform_driver(x4412_hello_world_driver);
MODULE_AUTHOR("YYH");
MODULE_LICENSE("GPL v2");

最後新建一個Makefile檔案用來編譯生成hello_world.ko檔案。具體內容如下

obj-m := hello_world.o

PWD := $(shell pwd)
KDIR := /home/yyh/Desktop/farsight/linux-4.9.123

all:
    make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=/home/yyh/Desktop/farsight/gcc4.6/gcc-4.6.4/bin/arm-none-linux-gnueabi-

clean:
    rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions