STM32MP157驱动开发——platform设备驱动(中)
相关文章:STM32MP157驱动开发——platform设备驱动(上)
0.前言
在上一节中,对platform平台设备驱动的原理以及相关知识进行了一些了解。本节就使用 platform 驱动框架来编写一个 LED 设备驱动,其中包括一个驱动模块
和一个设备模块
,驱动模块是 platform 驱动程序,设备模块是 platform 的设备信息,当这两个模块都加载成功以后就会匹配成功,然后 platform驱动模块中的 probe 函数就会执行,probe 函数中就是传统的字符设备驱动。注:这种驱动开发方式是无设备树的Linux内核所使用的,在使用设备树模式的新版本Linux内核中不再使用,在此只是为了理解以及兼顾旧版本的项目使用,另外,一些RTOS中的驱动开发也会用这种方式。
一、platform设备模块——设备信息解析
leddevice.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 <inux/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 PERIPH_BASE (0x40000000)
#define MPU_AHB4_PERIPH_BASE (PERIPH_BASE + 0x10000000)
#define RCC_BASE (MPU_AHB4_PERIPH_BASE + 0x0000)
#define RCC_MP_AHB4ENSETR (RCC_BASE + 0XA28)
#define GPIOI_BASE (MPU_AHB4_PERIPH_BASE + 0xA000)
#define GPIOI_MODER (GPIOI_BASE + 0x0000)
#define GPIOI_OTYPER (GPIOI_BASE + 0x0004)
#define GPIOI_OSPEEDR (GPIOI_BASE + 0x0008)
#define GPIOI_PUPDR (GPIOI_BASE + 0x000C)
#define GPIOI_BSRR (GPIOI_BASE + 0x0018)
#define REGISTER_LENGTH 4
/*设备资源信息,也就是LED0所使用的寄存器*/
static struct resource led_resources[] = {
[0] = {
.start = RCC_MP_AHB4ENSETR,
.end = (RCC_MP_AHB4ENSETR + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[1] = {
.start = GPIOI_MODER,
.end = (GPIOI_MODER + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[2] = {
.start = GPIOI_OTYPER,
.end = (GPIOI_OTYPER + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[3] = {
.start = GPIOI_OSPEEDR,
.end = (GPIOI_OSPEEDR + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[4] = {
.start = GPIOI_PUPDR,
.end = (GPIOI_PUPDR + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[5] = {
.start = GPIOI_BSRR,
.end = (GPIOI_BSRR + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
};
/*release程序,释放 flatform 设备模块的时候此函数会执行*/
static void led_release(struct device *dev)
{
printk("led device released!\r\n");
}
/*platform 设备结构体*/
static struct platform_device leddevice = {
.name = "stm32mp1-led",
.id = -1,
.dev = {
.release = &led_release,
},
.num_resources = ARRAY_SIZE(led_resources),
.resource = led_resources,
};
/*设备模块加载*/
static int __init leddevice_init(void)
{
return platform_device_register(&leddevice);
}
/*设备模块注销*/
static void __exit leddevice_exit(void)
{
platform_device_unregister(&leddevice);
}
module_init(leddevice_init);
module_exit(leddevice_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("amonter");
MODULE_INFO(intree, "Y");
① led_resources 数组,也就是设备资源,描述了 LED 所要使用到的寄存器信息,也就是 IORESOURCE_MEM 资源
②platform 设备结构体变量 leddevice,这里要注意 name 字段为“stm32mp1-led”,所以稍后编写 platform 驱动中的 name 字段也要为“stm32mp1-led”,否则设备和驱动会匹配失败
③设备模块加载函数 leddevice_init,在此函数里面通过 platform_device_register 向 Linux 内核注册 leddevice 这个 platform 设备
④设备模块卸载函数 leddevice_exit,在此函数里面通过 platform_device_unregister 从 Linux内核中删除掉 leddevice 这个 platform 设备
二、platform驱动模块——加载设备
leddriver.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
/* 映射后的寄存器虚拟地址指针 */
static void __iomem *MPU_AHB4_PERIPH_RCC_PI;
static void __iomem *GPIOI_MODER_PI;
static void __iomem *GPIOI_OTYPER_PI;
static void __iomem *GPIOI_OSPEEDR_PI;
static void __iomem *GPIOI_PUPDR_PI;
static void __iomem *GPIOI_BSRR_PI;
/* leddev 设备结构体 */
struct leddev_dev
{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
};
struct leddev_dev leddev;
/*LED打开、关闭*/
void led_switch(u8 sta)
{
u32 val = 0;
if (sta == LEDON)
{
val = readl(GPIOI_BSRR_PI);
val |= (1 << 16);
writel(val, GPIOI_BSRR_PI);
}
else if (sta == LEDOFF)
{
val = readl(GPIOI_BSRR_PI);
val |= (1 << 0);
writel(val, GPIOI_BSRR_PI);
}
}
/*取消映射*/
void led_unmap(void)
{
/* 取消映射 */
iounmap(MPU_AHB4_PERIPH_RCC_PI);
iounmap(GPIOI_MODER_PI);
iounmap(GPIOI_OTYPER_PI);
iounmap(GPIOI_OSPEEDR_PI);
iounmap(GPIOI_PUPDR_PI);
iounmap(GPIOI_BSRR_PI);
}
/*打开设备*/
static int led_open(struct inode *inode, struct file *filp)
{
return 0;
}
/*向设备写数据*/
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)
{
printk("kernel write failed!\r\n");
return -EFAULT;
}
ledstat = databuf[0]; /* 获取状态值 */
if (ledstat == LEDON)
{
led_switch(LEDON); /* 打开 LED 灯 */
}
else if (ledstat == LEDOFF)
{
led_switch(LEDOFF); /* 关闭 LED 灯 */
}
return 0;
}
/* 设备操作函数 */
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
/*probe函数,即挂载设备函数*/
static int led_probe(struct platform_device *dev)
{
int i = 0, ret;
int ressize[6];
u32 val = 0;
struct resource *ledsource[6];
printk("led driver and device has matched!\r\n");
/* 1、获取资源 */
for (i = 0; i < 6; i++)
{
ledsource[i] = platform_get_resource(dev, IORESOURCE_MEM, i);
if (!ledsource[i])
{
dev_err(&dev->dev, "No MEM resource for always on\n");
return -ENXIO;
}
ressize[i] = resource_size(ledsource[i]);
}
/* 2、初始化 LED */
/* 寄存器地址映射 */
MPU_AHB4_PERIPH_RCC_PI = ioremap(ledsource[0]->start, ressize[0]);
GPIOI_MODER_PI = ioremap(ledsource[1]->start, ressize[1]);
GPIOI_OTYPER_PI = ioremap(ledsource[2]->start, ressize[2]);
GPIOI_OSPEEDR_PI = ioremap(ledsource[3]->start, ressize[3]);
GPIOI_PUPDR_PI = ioremap(ledsource[4]->start, ressize[4]);
GPIOI_BSRR_PI = ioremap(ledsource[5]->start, ressize[5]);
/* 3、使能 PI 时钟 */
val = readl(MPU_AHB4_PERIPH_RCC_PI);
val &= ~(0X1 << 8); /* 清除以前的设置 */
val |= (0X1 << 8); /* 设置新值 */
writel(val, MPU_AHB4_PERIPH_RCC_PI);
/* 4、设置 PI0 通用的输出模式。 */
val = readl(GPIOI_MODER_PI);
val &= ~(0X3 << 0); /* bit0:1 清零 */
val |= (0X1 << 0); /* bit0:1 设置 01 */
writel(val, GPIOI_MODER_PI);
/* 5、设置 PI0 为推挽模式。 */
val = readl(GPIOI_OTYPER_PI);
val &= ~(0X1 << 0); /* bit0 清零,设置为上拉*/
writel(val, GPIOI_OTYPER_PI);
/* 6、设置 PI0 为高速。 */
val = readl(GPIOI_OSPEEDR_PI);
val &= ~(0X3 << 0); /* bit0:1 清零 */
val |= (0x2 << 0); /* bit0:1 设置为 10 */
writel(val, GPIOI_OSPEEDR_PI);
/* 7、设置 PI0 为上拉。 */
val = readl(GPIOI_PUPDR_PI);
val &= ~(0X3 << 0); /* bit0:1 清零 */
val |= (0x1 << 0); /*bit0:1 设置为 01 */
writel(val, GPIOI_PUPDR_PI);
/* 8、默认关闭 LED */
val = readl(GPIOI_BSRR_PI);
val |= (0x1 << 0);
writel(val, GPIOI_BSRR_PI);
/* 注册字符设备驱动 */
/* 1、申请设备号 */
ret = alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
if (ret < 0)
goto fail_map;
/* 2、初始化 cdev */
leddev.cdev.owner = THIS_MODULE;
cdev_init(&leddev.cdev, &led_fops);
/* 3、添加一个 cdev */
ret = cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
if (ret < 0)
goto del_unregister;
/* 4、创建类 */
leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
if (IS_ERR(leddev.class))
{
goto del_cdev;
}
/* 5、创建设备 */
leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);
if (IS_ERR(leddev.device))
{
goto destroy_class;
}
return 0;
destroy_class:
class_destroy(leddev.class);
del_cdev:
cdev_del(&leddev.cdev);
del_unregister:
unregister_chrdev_region(leddev.devid, LEDDEV_CNT);
fail_map:
led_unmap();
return -EIO;
}
/*remove函數*/
static int led_remove(struct platform_device *dev)
{
led_unmap(); /* 取消映射 */
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 = "stm32mp1-led", /* 驱动名字,用于和设备匹配 */
},
.probe = led_probe,
.remove = led_remove,
};
/*驱动模块加载函数*/
static int __init leddriver_init(void)
{
return platform_driver_register(&led_driver);
}
/*驱动模块卸载函数*/
static void __exit leddriver_exit(void)
{
platform_driver_unregister(&led_driver);
}
module_init(leddriver_init);
module_exit(leddriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("amonter");
MODULE_INFO(intree, "Y");
①在设备操作函数集 led_fops 之前,为传统的字符设备驱动
②probe 函数,当设备和驱动匹配以后此函数就会执行,当匹配成功以后会在终端上输出“led driver and device has matched!”。在 probe 函数里面初始化 LED、注册字符设备驱动。也就是将原来在驱动加载函数里面做的工作全部放到 probe 函数里面完成
③remove 函数,当卸载 platform 驱动的时候此函数就会执行。在此函数里面释放内存、注销字符设备等。也就是将原来驱动卸载函数里面的工作全部都放到 remove 函数中完成
④platform_driver 驱动结构体,注意 name 字段为"stm32mp1-led",和 leddevice.c 文件里面设置的设备 name 字段一致
⑤驱动模块加载函数,在此函数里面通过 platform_driver_register 向 Linux 内核注册 led_driver 驱动
⑥驱动模块卸载函数,在此函数里面通过 platform_driver_unregister 从 Linux内核卸载 led_driver 驱动
三、测试App
测试程序的功能比较简单,只需要操作打开和关闭LED。
led_App.c:
#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!\r\n");
return -1;
}
filename = argv[1];
/* 打开 led 驱动 */
fd = open(filename, O_RDWR);
if (fd < 0)
{
printf("file %s open failed!\r\n", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]); /* 要执行的操作:打开或关闭 */
retvalue = write(fd, databuf, sizeof(databuf));
if (retvalue < 0)
{
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
retvalue = close(fd); /* 关闭文件 */
if (retvalue < 0)
{
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}
四、编译及运行
与前面章节的Makefile不同,本节使用的驱动,Makefile文件需要添加两个目标输出文件,所以将obj-m 变量的值改为“leddevice.o leddriver.o”:
编译后会生成两个名为“leddevice.ko leddriver.ko”的驱动模块文件,另外再使用交叉编译工具链编译出测试App文件,即可用于驱动的运行及测试。
根文件系统中/sys/bus/platform/目录下保存着当前板子 platform 总线下的设备和驱动,其中 devices 子目录为 platform 设备,drivers 子目录为 plartofm 驱动。进入 devices 目录,就可以找到名为“stm32mp1-led”的设备,同样的,在 drivers 目录下,一定也会有一个“stm32mp1-led”的驱动文件,否则就会匹配失败。
使用测试 App 对 /dev/platled 设备操作就可以控制LED的亮灭:./led_App /dev/platled 1
,卸载驱动时需要分别对驱动和设备进行卸载。