platform 总线
platform device 代码
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/types.h>#define CHRDEVBASE_NAME "chrdevbase" /* 设备名 */
#define CHRDEVBASE_NUM 1 /* 设备数目 *//*** @brief include ioport.h**/
static struct resource my_resource[] =
{[0] ={.start = 0x80007400,.end = 0x80007500,.flags = IORESOURCE_MEM,},[1] ={.start = 0x80008400,.end = 0x80008500,.flags = IORESOURCE_MEM,},[2] ={.start = 0x10,.end = 0x10,.flags = IORESOURCE_IRQ,},[3] ={.start = 0x11,.end = 0x11,.flags = IORESOURCE_IRQ,},
};static void my_device_release(struct device *dev)
{printk("my device release\r\n");
}/*** @brief include platform_device.h**/
static struct platform_device platform_device_test = {.name = CHRDEVBASE_NAME,.id = -1,.resource = my_resource,.num_resources = ARRAY_SIZE(my_resource), /* kernel.h */.dev ={.release = my_device_release,},
};static int __init chrdevbase_init(void)
{platform_device_register(&platform_device_test);printk("k: base module init!\r\n");return 0;
}static void __exit chrdevbase_exit(void)
{platform_device_unregister(&platform_device_test);printk("k: base module exit!\r\n");
}module_init(chrdevbase_init);
module_exit(chrdevbase_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("tyustli");
MODULE_INFO(intree, "Y"); /* loading out-of-tree module taints kernel */
platform driver 代码
#include "linux/export.h"
#include "linux/ioport.h"
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/mod_devicetable.h>#define CHRDEVBASE_NAME "chrdevbase" /* 设备名 */
#define CHRDEVBASE_NUM 1 /* 设备数目 */static struct resource *my_resource;static int my_probe(struct platform_device *device)
{printk("platform driver probe\r\n");/* 第一种方式获取资源 */printk("resource num %x\r\n",device->resource[0].start); /* 资源编号,从 0 开始 */printk("resource num %x\r\n", device->resource[1].start);printk("resource num %x\r\n", device->resource[2].start);printk("resource num %x\r\n", device->resource[3].start);/* 第二种方式获取资源 */my_resource = platform_get_resource(device, IORESOURCE_MEM, 0); /* 同类型资源编号,第一个 mem 资源 */printk("get resource %x\r\n", my_resource->start);my_resource = platform_get_resource(device, IORESOURCE_MEM, 1); /* 同类型资源编号,第二个 mem 资源 */printk("get resource %x\r\n", my_resource->start);my_resource = platform_get_resource(device, IORESOURCE_IRQ, 0); /* 同类型资源编号,第一个 mem 资源 */printk("get resource %x\r\n", my_resource->start);my_resource = platform_get_resource(device, IORESOURCE_IRQ, 1); /* 同类型资源编号,第二个 mem 资源 */printk("get resource %x\r\n", my_resource->start);return 0;
}static int my_remove(struct platform_device *device)
{printk("platform driver remove\r\n");return 0;
}/*** @brief include mod_devicetable.h.**/
static const struct platform_device_id test_id_table = {.name = CHRDEVBASE_NAME, /* 次 name 的优先级比下面 platform_driver 结构体中的 name 优先级高,所以下面的结构体中名字不正确也能匹配成功。 */
};/*** @brief include platform_device.h**/
static struct platform_driver platform_driver_test = {.probe = my_probe,.remove = my_remove,.driver = {.name = "second_name",.owner = THIS_MODULE,},.id_table = &test_id_table,
};static int __init platform_driver_init(void)
{platform_driver_register(&platform_driver_test);printk("k: platform driver module init!\r\n");return 0;
}static void __exit platform_driver_exit(void)
{platform_driver_unregister(&platform_driver_test);printk("k: platform driver module exit!\r\n");
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("tyustli");
MODULE_INFO(intree, "Y"); /* loading out-of-tree module taints kernel */
查看模块 ko 文件
ls /lib/modules/6.5.7+/
my_platform_device.ko my_platform_driver.ko
platform 设备模块安装
modprobe my_platform_device
k: base module init!
查看 platform 设备
ls /sys/bus/platform/devices/chrdevbase/
driver_override power uevent
modalias subsystem
platform 驱动
编译报错
error: ‘const struct platform_device_id’ has no member named ‘name’
解决
#include <linux/mod_devicetable.h>
platform_device_id 结构体原型
struct platform_device_id {char name[PLATFORM_NAME_SIZE];kernel_ulong_t driver_data;
};
platform 驱动模块安装
modprobe my_platform_driver
platform driver probe
resource num 80007400
resource num 80008400
resource num 10
resource num 11
get resource 80007400
get resource 80008400
get resource 10
get resource 11
k: platform driver module init!
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
查看 platform 驱动
ls /sys/bus/platform/drivers/chrdevbase/
bind chrdevbase module uevent unbind
```## 退出
```c
Ctrl + a
x