1.前言
开发板:正点原子阿尔法 本文示例会结合前面知识,pinctrl与gpio,设备树,工作队列,定时器,sysfs,platform驱动做一个简单总结,会有一些不合理的地方,实现1s翻转一次led的状态
2.驱动代码
led.h
#ifndef LED_DRIVER_REG_H
#define LED_DRIVER_REG_H
#include <linux/types.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <asm/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#define DEVICE_NAME "led_driver"
#define DEVICE_CNT 1
#define LED_ON 1
#define LED_OFF 0
#define HIGH_LEVEL 1
#define LOW_LEVEL 0
struct led_device
{
dev_t devid;
struct cdev cdev;
struct class *class;
struct device *device;
int major;
int minor;
int led_gpio;
struct device_node* dev_node;
struct timer_list timer;
struct work_struct led_work;
};
struct led_device led_device;
#endif
led.c
#include "led.h"
static void led_switch(int led_status)
{
switch(led_status)
{
case LED_ON:
gpio_set_value(led_device.led_gpio,LOW_LEVEL);
break;
case LED_OFF:
gpio_set_value(led_device.led_gpio,HIGH_LEVEL);
break;
default:
gpio_set_value(led_device.led_gpio,HIGH_LEVEL);
break;
}
}
static ssize_t led_enable_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t len)
{
bool iLedEnable;
u32 ret;
ret = strtobool(buf, &iLedEnable);
if(ret < 0)
{
printk("strtobool failed\n");
return ret;
}
printk("led led_enable_store is=%d\n",iLedEnable);
led_switch(!!iLedEnable);
return len;
}
static DEVICE_ATTR(led_enable, S_IWUSR, NULL,led_enable_store);
static struct attribute *atk_imx6ul_led_sysfs_attrs[] = {
&dev_attr_led_enable.attr,
NULL,
};
static const struct attribute_group dev_attr_grp = {
.attrs = atk_imx6ul_led_sysfs_attrs,
NULL,
};
static int imx6ull_led_open(struct inode *inode, struct file *file)
{
file->private_data = &led_device;
return 0;
}
static int imx6ull_led_close(struct inode *inode, struct file *file)
{
file->private_data = &led_device;
return 0;
}
static ssize_t imx6ull_led_read(struct file *file, char __user *buf, size_t cnt, loff_t * loff)
{
return 0;
}
static ssize_t imx6ull_led_write(struct file *file, const char __user *buf, size_t cnt, loff_t *loff)
{
u32 ret;
unsigned char cALedbuf[1];
unsigned char iLedSta;
ret = copy_from_user(cALedbuf,buf,cnt);
if(ret < 0)
{
printk("copy from user failed\n");
return -EINVAL;
}
iLedSta = cALedbuf[0];
printk("++klz write led ,led status %d\n",iLedSta);
led_switch(!!iLedSta);
return 0;
}
static struct file_operations led_device_fops = {
.owner = THIS_MODULE,
.read = imx6ull_led_read,
.write = imx6ull_led_write,
.open = imx6ull_led_open,
.release = imx6ull_led_close,
};
static int led_parse_dt(void)
{
int ret;
led_device.dev_node = of_find_compatible_node(NULL,NULL,"klz-led");
if(led_device.dev_node == NULL)
{
printk("led device node find failed\n");
return -1;
}
led_device.led_gpio = of_get_named_gpio(led_device.dev_node, "led-gpio", 0);
if(led_device.led_gpio < 0)
{
printk("failed to get gpio\n");
return -1;
}
ret = gpio_request(led_device.led_gpio, "klz-led");
if(ret != 0)
{
printk("gpio request failed\n");
return -1;
}
ret = gpio_direction_output(led_device.led_gpio,HIGH_LEVEL);
if(ret != 0)
{
printk("gpio direction output failed\n");
return -1;
}
return 0;
}
static void led_exchange_work(struct work_struct *work)
{
struct led_device *data = container_of(work, struct led_device, led_work);
gpio_set_value((*data).led_gpio,HIGH_LEVEL);
msleep(1000);
gpio_set_value((*data).led_gpio,LOW_LEVEL);
}
static void led_exchange_timer(unsigned long handle)
{
schedule_work(&led_device.led_work);
mod_timer(&led_device.timer, jiffies +msecs_to_jiffies(2000));
}
static int led_driver_probe(struct platform_device *pdev)
{
u32 ret;
printk("klz");
printk("led driver and device was matched!\r\n");
ret = led_parse_dt();
if (ret < 0) {
printk("led parse error");
return ret;
}
if(led_device.major)
{
led_device.devid = MKDEV(led_device.major,0);
ret = register_chrdev_region(led_device.devid, DEVICE_CNT, DEVICE_NAME);
if(ret < 0)
{
printk("register_chrdev_region failed\n");
return ret;
}
}else
{
alloc_chrdev_region(&led_device.devid, 0, DEVICE_CNT, DEVICE_NAME);
led_device.major = MAJOR(led_device.devid);
led_device.minor = MINOR(led_device.devid);
}
led_device.cdev.owner = THIS_MODULE;
cdev_init(&led_device.cdev,&led_device_fops);
ret = cdev_add(&led_device.cdev,led_device.devid,DEVICE_CNT);
if(ret < 0)
{
printk("cdev_add device failed\n");
goto fail_cdev_add;
}
led_device.class = class_create(THIS_MODULE,DEVICE_NAME);
if(IS_ERR(led_device.class))
{
printk("class creat failed\n");
goto fail_class_create;
}
led_device.device = device_create(led_device.class,NULL,led_device.devid,NULL,DEVICE_NAME);
if(IS_ERR(led_device.device))
{
printk("device class failed\n");
goto fail_device_create;
}
ret = sysfs_create_group(&led_device.device->kobj,&dev_attr_grp);
if(ret)
{
printk("failed to create sys files\n");
goto fail_sys_create;
}
INIT_WORK(&led_device.led_work,led_exchange_work);
setup_timer(&led_device.timer,led_exchange_timer,(unsigned long)&led_device);
mod_timer(&led_device.timer, jiffies +msecs_to_jiffies(0));
dev_set_drvdata(&pdev->dev, &led_device);
printk("%s:probe success\n",__func__);
return 0;
fail_cdev_add:
unregister_chrdev_region(led_device.devid,DEVICE_CNT);
return -1;
fail_class_create:
cdev_del(&led_device.cdev);
unregister_chrdev_region(led_device.devid,DEVICE_CNT);
return -1;
fail_device_create:
cdev_del(&led_device.cdev);
unregister_chrdev_region(led_device.devid,DEVICE_CNT);
class_destroy(led_device.class);
return -1;
fail_sys_create:
sysfs_remove_group(&led_device.device->kobj,&dev_attr_grp);
cdev_del(&led_device.cdev);
unregister_chrdev_region(led_device.devid,DEVICE_CNT);
class_destroy(led_device.class);
return -1;
}
static int led_driver_remove(struct platform_device *pdev)
{
gpio_set_value(led_device.led_gpio,1);
sysfs_remove_group(&led_device.device->kobj, &dev_attr_grp);
cdev_del(&led_device.cdev);
unregister_chrdev_region(led_device.devid,DEVICE_CNT);
device_destroy(led_device.class, led_device.devid);
class_destroy(led_device.class);
del_timer_sync(&led_device.timer);
cancel_work_sync(&led_device.led_work);
gpio_free(led_device.led_gpio);
return 0;
}
static const struct of_device_id led_of_match[] = {
{ .compatible = "klz-led" },
{ }
};
static struct platform_driver led_driver = {
.driver = {
.name = "klz-led",
.of_match_table = led_of_match,
},
.probe = led_driver_probe,
.remove = led_driver_remove,
};
module_platform_driver(led_driver);
MODULE_AUTHOR("klz <1255713178@qq.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("led driver of atk imx6ull");
Makefile:
KERNELDIR := /home/klz/linux/linux-4.1.15
CURRENT_PATH := $(shell pwd)
obj-m := led.o
build: kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
copy:
sudo cp *.ko /home/klz/linux/nfs/rootfs/lib/modules/4.1.15
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
执行make && make copy拷贝到根文件系统中,
3.设备树
在根结点设备树添加,注意屏蔽其他用到一样结点
klzled{
#address-cells = <1>;
#size-cells = <1>;
compatible = "klz-led";
status = "okay";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_led>;
led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;
};
pinctrl下,注意屏蔽其他用到一样结点
pinctrl_led: ledgrp {
fsl,pins = <
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x10B0
>;
};
在Linux源码目录下执行
make dtbs
拷贝到根文件系统
cp arch/arm/boot/dts/imx6ull-alientek-emmc.dtb ../tftpboot/
4.测试
将编译出来 led.ko 拷贝到 rootfs/lib/modules/4.1.15 目录中,重启开发板,进入到目录 lib/modules/4.1.15 中,输入如下命令加载 led.ko 这个驱动模块
depmod
modprobe led.ko
或者
insmod led.ko
驱动模块加载完成以后到/sys/bus/platform/drivers/目录下查看驱动是否存在,我们在led.c 中设置 led_driver (platform_driver 类型)的 name 字段为“ klz-led”,因此会在/sys/bus/platform/drivers/目录下存在名为“klz-led”这个文件 同理,在/sys/bus/platform/devices/目录下也存在 led 的设备文件,也就是设备树中 klzled 这 个节点 驱动和模块都存在,当驱动和设备匹配成功以后就会输出如图 如果要卸载驱动的话输入如下命令即可:
rmmod led.ko
5.总结
1.使用sysfs_create_files会有警告,所以换成sysfs_create_group 2.sysfs_remove_group卸载在device卸载后面,不然会报空指针
|