设备树
/{
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