目录
2.2.1dtb中的每一个节点都被转换为device_node结构体
2.4 哪些设备树节点会被转化为platform_device呢?
2.5platform_device和platform_driver如何进行配对的?
一.功能介绍
之前都是使用在文件中定义引脚或者是定义寄存器地址,今天尝试使用设备树去存储引脚。
总结4种写驱动程序的方法
资源和驱动在同一个文件里面(这是最开始点灯的方法,驱动和资源都放在同一文件中,字符驱动设备注册/注销/分配也都在这一个文件夹里面)
分层为上层驱动程序和下层驱动程序。驱动程序下层进行分离,分离成资源和驱动两个文件。
驱动上层实现硬件无关的操作,比如注册字符设备驱动。
驱动下层实现硬件相关的操作,比如board_A.c实现单板A的LED操作。
资源用platform_device指定,驱动在platform_driver实现(匹配过程是device去总线上面匹配driver)
资源用设备树指定在platform_driver实现 ,将设备信息存储在设备树中,设备树生成platform_device节点。使用API来获取设备树上的信息来达到目的。
二.设备树简介
2.1概念
Linux设备树(Device tree)是一种描述硬件设备和系统配置的数据结构和文件格式。它是硬件描述上具有平台无关性的中间层,用于在Linux内核启动过程中传递和解析设备的相关信息。设备树的源文件(后缀是.dts或是.dtsi(可以被其它文件.dts/.dtsi包含)),存储地址、中断信息、设备驱动等硬件相关信息。设备树从根节点开始,根节点是设备树的起点,子节点可以进一步包含多个子节点允许多层嵌套。
2.2设备树如何被启动?
设备树的启动和解析是在内核引导过程中完成的:
-
引导加载程序(Bootloader)加载设备树文件:在启动过程中,引导加载程序(如U-Boot、GRUB)负责加载设备树文件到内存中。通常,设备树文件被嵌入在引导加载程序或操作系统镜像中,或者以外部文件的形式存储在存储设备上。引导加载程序会将设备树二进制文件加载到特定的内存位置。
-
内核启动时传递设备树信息:引导加载程序在启动内核时,会将设备树的内存地址通过某个机制(如设备树指针寄存器)传递给内核,以便内核能够找到设备树并进行解析。
-
内核解析设备树:内核在启动过程中,会通过设备树的地址找到设备树数据,并进行解析。内核的设备树子系统会读取设备树数据,并根据其中描述的设备信息和配置数据来初始化设备和资源。
-
设备匹配和驱动绑定:内核在解析设备树时,会将设备描述与相应的驱动程序进行匹配。设备节点中的属性会与设备驱动程序中的匹配条件进行比较,如果匹配成功,则将设备与驱动程序进行绑定。
-
设备初始化和资源分配:一旦设备与驱动程序绑定成功,内核会调用相应的驱动程序代码来初始化设备,并根据设备树中的资源描述分配和管理设备所需的资源,如内存、中断等。
2.3内核对设备树的处理
从源文件.dts开始,设备树的处理过程为:
①:dts在PC机上被编译为dtb文件;
②:u-boot把dtb文件传给内核;
③:内核解析dtb文件,把每一个节点都转换为device_node结构体;
④:对于某些device_node结构体,会被转换为platform_device结构体。(有些不需要生成platform_device例如IIC的子节点交给IIC总线来进行控制)
2.2.1dtb中的每一个节点都被转换为device_node结构体
struct device_node {
const char *name;
const char *type;
phandle phandle;
const char *full_name;
struct fwnode_handle fwnode;
struct property *properties;
struct property *deadprops; /* removed properties */
struct device_node *parent;
struct device_node *child;
struct device_node *sibling;
struct kobject kobj;
unsigned long _flags;
void *data;
#if defined(CONFIG_SPARC)
const char *path_component_name;
unsigned int unique_id;
struct of_irq_controller *irq_trans;
#endif
};
根节点被保存在全局变量 of_root 中,从 of_root 开始可以访问到任意节点。
2.4 哪些设备树节点会被转化为platform_device呢?
①:根节点下面含有compatile属性的子节点(这里面的mytest节点含有compatile属性能被转换,但是这个节点的子节点能否转化需要进一步判断)
②:含有特定compatile属性的节点的子节点
如果一个节点的 compatile 属性,它的值是这 4 者之一:"simple-bus","simple-mfd","isa","arm,amba-bus", 那 么 它 的 子 结 点 ( 需 含compatile 属性)也可以转换为 platform_device。
③:总线IIC、SPI节点下的子节点:不转化为platform_device,应该将他们的控制权交给总线控制器。
/{
mytest {
compatile = "mytest", "simple-bus";
mytest@0 {
compatile = "mytest_0";
};
};
i2c {
compatile = "samsung,i2c";
at24c02 {
compatile = "at24c02";
};
};
spi {
compatile = "samsung,spi";
flash@0 {
compatible = "winbond,w25q32dw";
spi-max-frequency = <25000000>;
reg = <0>;
};
};
};
/mytest 会被转换为 platform_device, 因为它兼容"simple-bus";
它的子节点/mytest/mytest@0 也会被转换为 platform_device
⚫ /i2c 节点一般表示 i2c 控制器, 它会被转换为 platform_device, 在内核
中有对应的 platform_driver;
⚫ /i2c/at24c02 节点不会被转换为 platform_device, 它被如何处理完全由
父节点的 platform_driver 决定, 一般是被创建为一个 i2c_client。
⚫ 类似的也有/spi 节点, 它一般也是用来表示 SPI 控制器, 它会被转换为
platform_device, 在内核中有对应的 platform_driver;
⚫ /spi/flash@0 节点不会被转换为 platform_device, 它被如何处理完全由
父节点的 platform_driver 决定, 一般是被创建为一个 spi_device。
怎么转化为platform_device
内核处理设备树的函数调用过程,这里不需要去分析,得到的结论是:
platform_device中含有resource,它来自device_node的reg,interrupts属性;
platform_device.dev.of_node指向device_node,可以通过它获得其它属性。
2.5platform_device和platform_driver如何进行配对的?
在前一篇文章之中,有对配对过程进行讲解,这里大体相同,但是由设备树转换而成的platform_device有一个of_node。
最先比较
platform_device.driver_override 和 platform_driver.driver.name可以设置 platform_device 的 driver_override,强制选择某个 platform_driver。若定义了driver_override ,最先使用它去比较。
比较设备树(比较of_node结构体)
platform_device.dev.of_node 和 platform_driver.driver.of_match_table。
由设备树节点转换得来的 platform_device 中,含有一个结构体:of_node。
它的类型如下:
使用设备树信息来判断 dev 和 drv 是否配对时:
首先,如果 of_match_table 中含有 compatible 值,就跟 dev 的 compatile
属性比较,若一致则成功,否则返回失败;
其次,如果 of_match_table 中含有 type 值,就跟 dev 的 device_type 属性
比较,若一致则成功,否则返回失败;
最后,如果 of_match_table 中含有 name 值,就跟 dev 的 name 属性比
较,若一致则成功,否则返回失败。
而设备树中建议不再使用 devcie_type 和 name 属性,所以基本上只使用设
备节点的 compatible 属性来寻找匹配的 platform_driver。
然后比较
platform_device. name 和 platform_driver.id_table[i].name Platform_driver.id_table 是“platform_device_id”指针,表示该 drv 支持若干个 device,它里面列出了各个 device 的{.name, .driver_data},其中的“name”表示该drv 支持的设备的名字,driver_data 是些提供给该 device 的私有数据
最后比较
platform_device.name 和 platform_driver.driver.name platform_driver.id_table 可能为空,这时可以根据 platform_driver.driver.name 来寻找同名的 platform_device。
三.设备树语法
设备树源文件的后缀是.dts,通过DTC工具编译生成dtb可用的设备树文件
范例:
[label:] node-name[@unit-address] {
[properties definitions]
[child nodes]
};
实例:
/dts-v1/;
/ {
uart0: uart@fe001000 {
compatible="ns16550";
reg=<0xfe001000 0x100>;
};
};
label:node-name@uint-address//标准格式
label 是标号,可以省略
为什么要引入label呢,这是为了方便访问节点,
可以直接通过&label 来访问这个节点,
比如通过&cpu0 就可以访问“cpu@0”这个节点,
而不需要输入完整的节点名字。
再比如节点 “intc:interrupt-controller@00a01000”,
节点 label 是 intc,而节点名字就很长了,
为“ interruptcontroller@00a01000”。
很明显通过&intc 来访问“interrupt-controller@00a01000”这个节点要方便很多!
应用实例:
/dts-v1/; // 设备树文件版本
/ { // 根节点开始
model = "MyDevice"; // 设备型号为"MyDevice"
compatible = "mydevice,example"; // 设备与"mydevice,example"兼容
memory { // 描述设备的一段物理内存区域
device_type = "memory"; // 设备类型为memory
reg = <0x0 0x20000000>; // 内存区域起始地址和大小
};
led1 { // 描述一个LED灯设备
compatible = "gpio-leds"; // LED灯驱动程序与"gpio-leds"兼容
gpios = <&gpio 0 1>; // LED所连接的GPIO控制器和引脚号
default-state = "off"; // LED的初始状态为off
};
};
四.代码部分
思路就是通过更改设备树,通过设备树生成platform_device去和驱动匹配,并读取设备树上的硬件信息,然后操作硬件。
上层应用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
/*
* ./ledtest /dev/100ask_led0 on
* ./ledtest /dev/100ask_led0 off
*/
int main(int argc, char **argv)
{
int fd;
char status;
/* 1. 判断参数 */
if (argc != 3)
{
printf("Usage: %s <dev> <on | off>\n", argv[0]);
return -1;
}
/* 2. 打开文件 */
fd = open(argv[1], O_RDWR);
if (fd == -1)
{
printf("can not open file %s\n", argv[1]);
return -1;
}
/* 3. 写文件 */
if (0 == strcmp(argv[2], "on"))
{
status = 1;
write(fd, &status, 1);
}
else
{
status = 0;
write(fd, &status, 1);
}
close(fd);
return 0;
}
上层驱动leddrv.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include "leddrv.h"
#include "led_opr.h"
#define LED_NUM 2
/* 1. 确定主设备号 */
static int major = 0;
static struct class *led_class;
struct led_operations *p_led_opr;
#define MIN(a, b) (a < b ? a : b)
/*个人理解这里为什么要在这里单独再写一个API去注册设备
major和led_class设备类是在上层驱动定义的,要在下层用的话太麻烦
*/
void led_class_create_device(int minor)
{
device_create(led_class, NULL, MKDEV(major, minor), NULL, "100ask_led%d", minor); /* /dev/100ask_led0,1,... */
}
void led_class_destroy_device(int minor)
{
device_destroy(led_class, MKDEV(major, minor));
}
void register_led_operations(struct led_operations *opr)
{
p_led_opr = opr;//这里就是避免发生交叉依赖
}
EXPORT_SYMBOL(led_class_create_device);//导出到内核的全局符号表中,让其它的内核模块也能使用这个函数
EXPORT_SYMBOL(led_class_destroy_device);
EXPORT_SYMBOL(register_led_operations);
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
char status;
struct inode *inode = file_inode(file);
int minor = iminor(inode);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_from_user(&status, buf, 1);
/* 根据次设备号和status控制LED */
p_led_opr->ctl(minor, status);
return 1;
}
static int led_drv_open (struct inode *node, struct file *file)
{
int minor = iminor(node);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
/* 根据次设备号初始化LED */
p_led_opr->init(minor);
return 0;
}
static int led_drv_close (struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
/* 2. 定义自己的file_operations结构体 */
static struct file_operations led_drv = {
.owner = THIS_MODULE,
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
};
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init led_init(void)
{
int err;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */
led_class = class_create(THIS_MODULE, "100ask_led_class");
err = PTR_ERR(led_class);
if (IS_ERR(led_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "100ask_led");
return -1;
}
// p_led_opr = get_board_led_opr();
return 0;
}
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
static void __exit led_exit(void)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
class_destroy(led_class);
unregister_chrdev(major, "100ask_led");
}
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
下层驱动:chip_demo_gpio.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>
#include "leddrv.h"
#include "led_opr.h"
#include "led_resource.h"
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/module.h>
#include <asm/io.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
//static struct led_resource *led_rsc = NULL;
static int g_ledpins[100];
static int g_ledcnt = 0;
static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */
{
printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, GROUP(g_ledpins[which]));
switch (GROUP(g_ledpins[which]))
{
case 0:
{
printk("set pin of group 0 ....\n");
break;
}
case 1:
{
printk("set pin of group 1 ....\n");
break;
}
case 2:
{
printk("set pin of group 2 ....\n");
break;
}
case 3 :
{
printk("set pin of group 3 ....\n");
break;
}
case 5:
{
printk("set pin of group 5 ...\n");
break;
}
}
return 0;
}
static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, PIN(g_ledpins[which]), status ? "on" : "off");
switch (PIN(g_ledpins[which]))
{
case 0:
{
printk("set pin of pin 0 ....\n");
break;
}
case 1:
{
printk("set pin of pin 1 ....\n");
break;
}
case 2:
{
printk("set pin of pin 2 ....\n");
break;
}
case 3 :
{
printk("set pin of pin 3 ....\n");
break;
}
case 5:
{
printk("set pin of pin 5 ...\n");
break;
}
case 8:
{
printk("set pin 8\n");
break;
}
}
return 0;
}
static struct led_operations board_demo_led_opr = {
.init = board_demo_led_init,
.ctl = board_demo_led_ctl,
};
struct led_operations *get_board_led_opr(void)
{
return &board_demo_led_opr;
}
/*系统检测到驱动和设备都注册了之后会调用这个函数如果一个驱动对应多个设备,
那么每当有一个设备注册了probe函数就会执行一次
*/
static int chip_demo_gpio_led_probe(struct platform_device *pdev)
{
struct device_node *np;
int led_pin;
int err;
np = pdev->dev.of_node;
if(!np)//判断结构体是否为空,判断设备树是否生成node节点
{
return -1;
}
//根据np从设备树中的这个节点读取一个pin属性并将它转化为32位的无符号整数
//并且将结果保存到led_pin中
err = of_property_read_u32(np, "pin" ,&led_pin);
g_ledpins[g_ledcnt] = led_pin;
led_class_create_device(g_ledcnt);
g_ledcnt++;
return 0;
}
/*删除模块时会自动执行这个函数,内核自动调用*/
static int chip_demo_gpio_led_remove(struct platform_device *pdev)
{
int i = 0;
struct device_node *np;
int err;
int led_pin;
//由设备树节点转换得来的 platform_device 中,含有一个结构体:of_node。
np = pdev->dev.of_node;//获取of_node结构体
if(!np)//判断该结构体是否为空,判断设备树是否生成了设备节点
{
return -1;
}
//根据np从设备树中的这个节点读取一个pin属性并将它转化为32位的无符号整数
//并且将结果保存到led_pin中
err = of_property_read_u32(np, "pin", &led_pin);//简单来说就是读取引脚
for(i = 0;i < g_ledcnt;i++)
{
if(g_ledpins[i] == led_pin )//判断引脚是否相同
{
led_class_destroy_device(i);//销毁设备
g_ledpins[i] = -1;//将标志位置一
break;
}
}
for(i = 0;i < g_ledcnt; i++)
{
if(g_ledpins[i] != -1)
{
break;
}
}
if(i == g_ledcnt)//说明上面的for循环里面都没走,g_ledpins[i]都等于-1也就是所有引脚都用了
{
g_ledcnt = 0;
}
return 0;
}
static const struct of_device_id ask100_leds[] = {
{ .compatible = "100as,leddrv"},//确保compatible和设备树compatible相同
{},
};
static struct platform_driver chip_demo_gpio_driver = {
.probe = chip_demo_gpio_led_probe,
.remove = chip_demo_gpio_led_remove,
.driver = {
.name = "100ask_led",
.of_match_table = ask100_leds,//想要和设备树driver进行匹配需要定义这个
},
};
static int chip_demo_gpio_drv_init(void)
{
int err;
err= platform_driver_register(&chip_demo_gpio_driver);
register_led_operations(&board_demo_led_opr);
return 0;
}
static void chip_demo_gpio_dev_exit(void)
{
platform_driver_unregister(&chip_demo_gpio_driver);//注销设备
}
module_init(chip_demo_gpio_drv_init);
module_exit(chip_demo_gpio_dev_exit);
MODULE_LICENSE("GPL");
设备树文件
#define GROUP_PIN(g, p) ((g << 16) | (p))
/ {
100ask_led@0 {
compatible = "100as,leddrv";
pin = <GROUP_PIN(3, 1)>;
};
100ask_led@1 {
compatible = "100as,leddrv";
pin = <GROUP_PIN(5, 8)>;
};
};
头文件以及Makefile
leddrv.h
#ifndef _LEDDRV_H
#define _LEDDRV_H
#include "led_opr.h"
void led_class_create_device(int minor);
void led_class_destroy_device(int minor);
void register_led_operations(struct led_operations *opr);
//void led_device_create(int minor);
//void led_device_destory(int minor);
#endif /* _LEDDRV_H */
led_opr.h
#ifndef _LED_OPR_H
#define _LED_OPR_H
struct led_operations {
int (*init) (int which); /* 初始化LED, which-哪个LED */
int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
};
struct led_operations *get_board_led_opr(void);
#endif
Makefile
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
# 请参考各开发板的高级用户使用手册
KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o ledtest ledtest.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
rm -f ledtest
# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o
obj-m += leddrv.o chip_demo_gpio.o
五.踩坑
任意驱动程序里,都可以直接访问设备树。
bus_type中有设备和驱动这两条总线,同时bus_type也包含设备和驱动的匹配规则
想要获取设备树里面信息(reg, gpio, interrupts)调用指定API来获取这些信息
驱动程序里面platform_driver结构体里面需要有.of_match_table成员才能够支持设备树
如果一个驱动支持多个设备,那么它的probe函数会被调用多次,每次调用都对应一个新的设备。当系统检测到新的设备时,会遍历所有已注册的驱动,尝试将其与该设备进行匹配。如果找到了匹配的驱动,就会调用该驱动的probe函数进行设备的初始化和配置,直到找到一个成功匹配的驱动为止。
在probe函数中,驱动可以根据不同的设备进行不同的初始化和配置,以适应不同的硬件需求。通常情况下,驱动会根据设备的特征和属性进行判断和配置,以确保设备能够正常工作。
需要注意的是,如果多个设备共享相同的硬件资源(如GPIO、I2C总线等),那么驱动需要进行适当的资源管理,以避免冲突和错误。在probe函数中,驱动可以申请并分配资源,设置中断处理程序等操作,以确保多个设备可以正常共享硬件资源。
总线注册:
device:编写过设备树之后,开机之后自动注册
driver:使用模块在入口函数中注册总线驱动
匹配之后在驱动程序中就可以拿到设备树中的硬件信息,通过设备树中的寄存器地址进行映射,然后操作寄存器