1.设备注册
注册设备的结构体platform_device位于linux目录中的include/linux,使用命令“vim include/linux/platform_device.h”打开”platform_device”所在文件。
struct platform_device {
const char * name;
int id;
struct device dev;
u32 num_resources;
struct resource * resource;
const struct platform_device_id *id_entry;
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
name代表设备名称,通过ls sys/devices会显示;id表示设备id,用于插入总线并且具有相同name的设备编号,若只有一个设备那么就为-1;dev为结构体中内嵌的device结构体;u32代表设备使用的资源数据;resource代表设备使用的资源数组。结构体的参数不一定全部使用,对于较为常用的有设备名name和设备编号id,以及资源数组*resource。
2.添加设备到平台总线
结合迅为开发板将设备添加到平台总线上作一个详细介绍:
平台文件位于linux目录下 arch/arm/mach-exynos中,使用”vim arch/arm/mach-exynos/mach-itop4412.c”打开平台文件。对于驱动程序一般可以参考已有的驱动程序,由于led的驱动程序相对简单,在文件中查找”LEDS_CTL”,仿照这段代码加”hello_ctl”:
#ifdef CONFIG_HELLO_CTL
struct platform_device s3c_device_hello_ctl={
.name ="hello",
.id =-1,
};
#endif
同时在文件mach_itop4412.c中再搜索LEDS_CTL,查找设备初始化代码,仿照添加即可:
#ifdef CONFIG_HELLO_CTL
&s3c_device_hello_ctl,
#endif
同时还需要确定一下,是否有”HELLO_CTL”宏定义,只有定义这个宏,在生成内核的时候才能把该驱动编译进内核。使用“vim drivers/char/Kconfig”仿照LEDS_CTL添加HELLO_CTL:
config HELLO_CTL
tristate "Enable HELLO config"
default y
help
Enable Hello config
接着在menuconfig中将其配置好,使用命令make menuconfig,进入Device Drivers/Character devices,空格选择Enable HELLO config,配置上宏定义”HELLO_CTL”,配置后保存退出,重新编译内核,烧写到开发板。