1、直接操作寄存器
直接把硬件资源(比如LED灯所用到的寄存器)和驱动程序写在一个.c文件里
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <asm/io.h>
/* 1. 确定主设备号 */
#define CHAR_DEV_NAME "atk_led"
static int major = 0;
static struct class *led_class;
/* registers */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
// GPIO5_GDIR 地址:0x020AC004
static volatile unsigned int *GPIO5_GDIR;
//GPIO5_DR 地址:0x020AC000
static volatile unsigned int *GPIO5_DR;
static int led_drv_open (struct inode *node, struct file *file)
{
/* enable gpio5:GPIO5 默认使能
* configure gpio5_io3 as gpio
* configure gpio5_io3 as output
*/
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &=~0xf;//清空
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |=0x5;//configure gpio5_io3 as gpio
*GPIO5_GDIR|=(1<<3);//configure gpio5_io3 as output
return 0;
}
static ssize_t led_drv_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
char c;
int ret;
ret=copy_from_user(&c, buf,1);
if(c)
{
*GPIO5_DR &=~(1<<3);
}
else
{
*GPIO5_DR |=(1<<3);
}
return 1;
}
/* 2. 定义自己的file_operations结构体 */
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int __init led_init(void)
{
int err;
major = register_chrdev(0, CHAR_DEV_NAME, &led_drv);//CHAR_DEV_NAME 决定cat /proc/devices里的name
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3=ioremap(0x02290000 + 0x14,4);
// GPIO5_GDIR 地址:0x020AC004
GPIO5_GDIR=ioremap(0x020AC004,4);
//GPIO5_DR 地址:0x020AC000
GPIO5_DR=ioremap(0x020AC000,4);
//用于将设备驱动程序和设备模型关联起来
led_class = class_create(THIS_MODULE, "atk_led_class");//在“/sys/class”目录下创建一个子目录“led_class”
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
printk("%s line %d\n", __FUNCTION__, __LINE__);
unregister_chrdev(major, CHAR_DEV_NAME);
return -1;
}
//在成功创建设备节点之后,用户空间可以通过打开 /dev/led 文件来访问该设备节点,从而与内核中的设备进行交互。
device_create(led_class, NULL, MKDEV(major, 0), NULL, "atk_led"); //决定/sys/class/led_class和/dev/目录下创建一个文件“led”:
return 0;
}
static void __exit led_exit(void)
{
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_DR);
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
unregister_chrdev(major, CHAR_DEV_NAME);
}
//一个驱动程序有入口函数、出口函数
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
2、总线驱动模型
led_dev.c
#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_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
/*
* 寄存器地址定义
*/
#define IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 (0x02290014)
#define GPIO5_DR_BASE (0x020AC000)
#define GPIO5_GDIR_BASE (0x020AC004)
#define REGISTER_LENGTH 4
/* @description : 释放flatform设备模块的时候此函数会执行
* @param - dev : 要释放的设备
* @return : 无
*/
static void led_release(struct device *dev)
{
printk("led device released!\r\n");
}
/*
* 设备资源信息,也就是LED0所使用的所有寄存器
*/
static struct resource led_resources[] = {
[0] = {
.start = IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3,
.end = (IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM, //表示资源类型
},
[2] = {
.start = GPIO5_DR_BASE,
.end = (GPIO5_DR_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[3] = {
.start = GPIO5_GDIR_BASE,
.end = (GPIO5_GDIR_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
};
/*
* platform设备结构体:描述设备信息
*/
static struct platform_device led_device = {
.name = "imx6ul-led", //name 表示设备名字,要和所使用的 platform 驱动的 name 字段相同
.id = -1, //表示没有id
.dev = {
.release = &led_release,
},
.num_resources = ARRAY_SIZE(led_resources), //表示资源数量
.resource = led_resources, //是设备信息,比如外设寄存器
};
static int __init led_device_init(void)
{
return platform_device_register(&led_device);//设备信息注册到 Linux 内核中
}
/*
* @description : 设备模块注销
* @param : 无
* @return : 无
*/
static void __exit led_device_exit(void)
{
platform_device_unregister(&led_device);
}
module_init(led_device_init);
module_exit(led_device_exit);
MODULE_LICENSE("GPL");
led_drv.c
#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_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.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 "platled" /* 设备名字 */
#define LEDOFF 0
#define LEDON 1
/* registers */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
// GPIO5_GDIR 地址:0x020AC004
static volatile unsigned int *GPIO5_GDIR;
//GPIO5_DR 地址:0x020AC000
static volatile unsigned int *GPIO5_DR;
/* leddev设备结构体 */
struct leddev_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
};
struct leddev_dev leddev; /* led设备 */
/*
* @description : LED打开/关闭
* @param - sta : LEDON(0) 打开LED,LEDOFF(1) 关闭LED
* @return : 无
*/
void led0_switch(u8 sta)
{
u32 val = 0;
if(sta == LEDON){
val = readl(GPIO5_DR);
val &= ~(1 << 3);
writel(val, GPIO5_DR);
}else if(sta == LEDOFF){
val = readl(GPIO5_DR);
val|= (1 << 3);
writel(val, GPIO5_DR);
}
}
/*
* @description : 打开设备
* @param - inode : 传递给驱动的inode
* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量
* 一般在open的时候将private_data指向设备结构体。
* @return : 0 成功;其他 失败
*/
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &leddev; /* 设置私有数据 */
return 0;
}
/*
* @description : 向设备写数据
* @param - filp : 设备文件,表示打开的文件描述符
* @param - buf : 要写给设备写入的数据
* @param - cnt : 要写入的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 写入的字节数,如果为负值,表示写入失败
*/
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[1];
unsigned char ledstat;
retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
return -EFAULT;
}
ledstat = databuf[0]; /* 获取状态值 */
if(ledstat == LEDON) {
led0_switch(LEDON); /* 打开LED灯 */
}else if(ledstat == LEDOFF) {
led0_switch(LEDOFF); /* 关闭LED灯 */
}
return 0;
}
/* 设备操作函数 */
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
/*
* @description : flatform驱动的probe函数,当驱动与
* 设备匹配以后此函数就会执行
* @param - dev : platform设备
* @return : 0,成功;其他负值,失败
*/
static int led_probe(struct platform_device *dev)
{
int i = 0;
int ressize[5];
struct resource *ledsource[3];
printk("led driver and device has matched!\r\n");
/* 1、获取资源 */
for (i = 0; i < 3; i++) {
ledsource[i] = platform_get_resource(dev, IORESOURCE_MEM, i); /* 依次MEM类型资源 */
if (!ledsource[i]) {
dev_err(&dev->dev, "No MEM resource for always on\n");
return -ENXIO;
}
ressize[i] = resource_size(ledsource[i]);
}
/* 2、初始化LED */
/* 寄存器地址映射 */
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(ledsource[0]->start, ressize[0]);
GPIO5_DR = ioremap(ledsource[1]->start, ressize[1]);
GPIO5_GDIR = ioremap(ledsource[2]->start, ressize[2]);
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &=~0xf;//清空
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |=0x5;//configure gpio5_io3 as gpio
*GPIO5_GDIR|=(1<<3);//configure gpio5_io3 as output
/* 注册字符设备驱动 */
/*1、创建设备号 */
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); /* 获取分配号的主设备号 */
}
/* 2、初始化cdev */
leddev.cdev.owner = THIS_MODULE;
cdev_init(&leddev.cdev, &led_fops);
/* 3、添加一个cdev */
cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
/* 4、创建类 */
leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
if (IS_ERR(leddev.class)) {
return PTR_ERR(leddev.class);
}
/* 5、创建设备 */
leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);
if (IS_ERR(leddev.device)) {
return PTR_ERR(leddev.device);
}
return 0;
}
/*
* @description : platform驱动的remove函数,移除platform驱动的时候此函数会执行
* @param - dev : platform设备
* @return : 0,成功;其他负值,失败
*/
static int led_remove(struct platform_device *dev)
{
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_DR);
cdev_del(&leddev.cdev);/* 删除cdev */
unregister_chrdev_region(leddev.devid, LEDDEV_CNT); /* 注销设备号 */
device_destroy(leddev.class, leddev.devid);
class_destroy(leddev.class);
return 0;
}
/* platform驱动结构体 */
static struct platform_driver led_driver = {
.driver = {
.name = "imx6ul-led", /* 驱动名字,用于和设备匹配 */
},
.probe = led_probe,
.remove = led_remove,
};
/*
* @description : 驱动模块加载函数
* @param : 无
* @return : 无
*/
static int __init led_driver_init(void)
{
return platform_driver_register(&led_driver);
}
/*
* @description : 驱动模块卸载函数
* @param : 无
* @return : 无
*/
static void __exit led_driver_exit(void)
{
platform_driver_unregister(&led_driver);
}
module_init(led_driver_init);
module_exit(led_driver_exit);
MODULE_LICENSE("GPL");
3、设备树
需要在设备树文件根节点添加led相关信息
100ask_imx6ull_mini_led{
#address-cells = <1>;
#size-cells = <1>;
compatible = "100ask_imx6ull_mini_led0";
status = "okay";
reg = <0x02290014 0x04 /*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3*/
0x020AC004 0x04 /*GPIO5_GDIR*/
0x020AC000 0x04>;/*GPIO5_DR*/
};
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <asm/io.h>
#include <linux/property.h>
#include <linux/of.h>
/* registers */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
// GPIO5_GDIR 地址:0x020AC004
static volatile unsigned int *GPIO5_GDIR;
//GPIO5_DR 地址:0x020AC000
static volatile unsigned int *GPIO5_DR;
struct dtsled_dev
{
struct class *class; /* 类 */
struct device*device; /* 设备 */
int major; /* 主设备号 */
struct device_node *nd; /* 设备节点 */
};
struct dtsled_dev dtsled; /* led 设备 */
static int led_drv_open(struct inode *inode, struct file *filp)
{
filp->private_data = &dtsled; /* 设置私有数据 */
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &=~0xf;//清空
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |=0x5;//configure gpio5_io3 as gpio
*GPIO5_GDIR|=(1<<3);//configure gpio5_io3 as output
return 0;
}
static ssize_t led_drv_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
char c;
int ret;
ret=copy_from_user(&c, buf,1);
if(c)
{
*GPIO5_DR &=~(1<<3);
}
else
{
*GPIO5_DR |=(1<<3);
}
return 1;
}
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.write = led_drv_write,
};
static int __init dtsled_init(void)
{
u32 val = 0;
int ret;
u32 regdata[14];
const char *str;
struct property *proper;
/* 获取设备树中的属性数据 */
/* 1、获取设备节点: alphaled */
dtsled.nd = of_find_node_by_path("/100ask_imx6ull_mini_led");
if(dtsled.nd == NULL) {
printk("100ask_imx6ull_mini_led node can not found!\r\n");
return -EINVAL;
} else {
printk("100ask_imx6ull_mini_led node has been found!\r\n");
}
/* 2、获取 compatible 属性内容 */
proper = of_find_property(dtsled.nd, "compatible", NULL);
if(proper == NULL) {
printk("compatible property find failed\r\n");
} else {
printk("compatible = %s\r\n", (char*)proper->value);
}
/* 3、获取 status 属性内容 */
ret = of_property_read_string(dtsled.nd, "status", &str);
if(ret < 0){
printk("status read failed!\r\n");
} else {
printk("status = %s\r\n",str);
}
/* 4、获取 reg 属性内容 */
ret = of_property_read_u32_array(dtsled.nd, "reg", regdata, 6);
if(ret < 0) {
printk("reg property read failed!\r\n");
} else {
u8 i = 0;
printk("reg data:\r\n");
for(i = 0; i < 6; i++)
printk("%#X ", regdata[i]);
printk("\r\n");
}
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3=ioremap(regdata[0],regdata[1]);
// GPIO5_GDIR 地址:0x020AC004
GPIO5_GDIR=ioremap(regdata[2],regdata[3]);
//GPIO5_DR 地址:0x020AC000
GPIO5_DR=ioremap(regdata[4],regdata[5]);
dtsled.major=register_chrdev(0, "100ask_led0", &led_drv);//CHAR_DEV_NAME 决定cat /proc/devices里的name
//用于将设备驱动程序和设备模型关联起来
dtsled.class = class_create(THIS_MODULE, "100ask_led_class");//在“/sys/class”目录下创建一个子目录“led_class”
if (IS_ERR(dtsled.class)) {
unregister_chrdev(dtsled.major, "100ask_led");
return -1;
}
//在成功创建设备节点之后,用户空间可以通过打开 /dev/led 文件来访问该设备节点,从而与内核中的设备进行交互。
dtsled.device=device_create(dtsled.class, NULL, MKDEV(dtsled.major, 0), NULL, "100ask_led0"); //决定/sys/class/led_class和/dev/目录下创建一个文件“led”:
return 0;
}
static void __exit dtsled_exit(void)
{
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_DR);
device_destroy(dtsled.class, MKDEV(dtsled.major, 0));
class_destroy(dtsled.class);
unregister_chrdev(dtsled.major, "100ask_led0");
}
//一个驱动程序有入口函数、出口函数
module_init(dtsled_init);
module_exit(dtsled_exit);
MODULE_LICENSE("GPL");