公眾號(hào):嵌入式攻城獅(ID:andyxi_linux)
作者:安迪西
本文介紹如何使用platform驅(qū)動(dòng)框架來(lái)編寫一個(gè)LED燈驅(qū)動(dòng),采用自定義platform_device的方式來(lái)編寫LED的設(shè)備信息。需要編寫一個(gè)驅(qū)動(dòng)模塊(platform驅(qū)動(dòng)程序)和一個(gè)設(shè)備模塊(platform設(shè)備信息)。當(dāng)這兩個(gè)模塊都加載成功后就會(huì)匹配成功,然后platform驅(qū)動(dòng)模塊中的probe函數(shù)就會(huì)執(zhí)行
1. platform設(shè)備程序編寫
platform設(shè)備信息框架如下圖所示
新建設(shè)備文件leddevice.c,并根據(jù)上面的框架圖,編寫設(shè)備信息程序
/* 寄存器地址定義 */
#define CCM_CCGR1_BASE (0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIR_BASE (0X0209C004)
#define REGISTER_LENGTH 4
/* 釋放 flatform 設(shè)備模塊 */
static void led_release(struct device *dev) {
printk("led device released!rn");
}
/* 設(shè)備資源信息,即LED0所使用的所有寄存器 */
static struct resource led_resources[] = {
[0] = {
.start = CCM_CCGR1_BASE,
.end = (CCM_CCGR1_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[1] = {
.start = SW_MUX_GPIO1_IO03_BASE,
.end = (SW_MUX_GPIO1_IO03_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[2] = {
.start = SW_PAD_GPIO1_IO03_BASE,
.end = (SW_PAD_GPIO1_IO03_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[3] = {
.start = GPIO1_DR_BASE,
.end = (GPIO1_DR_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
[4] = {
.start = GPIO1_GDIR_BASE,
.end = (GPIO1_GDIR_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,
},
};
/* platform 設(shè)備結(jié)構(gòu)體 */
static struct platform_device leddevice = {
.name = "imx6ul-led",
.id = -1,
.dev = {
.release = &led_release,
},
.num_resources = ARRAY_SIZE(led_resources),
.resource = led_resources,
};
/* 設(shè)備模塊加載 */
static int __init leddevice_init(void) {
return platform_device_register(&leddevice);
}
/* 設(shè)備模塊注銷 */
static void __exit leddevice_exit(void) {
platform_device_unregister(&leddevice);
}
module_init(leddevice_init);
module_exit(leddevice_exit);
MODULE_LICENSE("GPL");
2. platform驅(qū)動(dòng)程序編寫
platform驅(qū)動(dòng)框架如下圖所示:platform驅(qū)動(dòng)并不是獨(dú)立于字符設(shè)備、塊設(shè)備和網(wǎng)絡(luò)設(shè)備驅(qū)動(dòng)之外的另一種類型驅(qū)動(dòng),它只是為了驅(qū)動(dòng)的分離與分層而提出來(lái)的一種框架,其驅(qū)動(dòng)的具體實(shí)現(xiàn)還是需要字符設(shè)備、塊設(shè)備或網(wǎng)絡(luò)設(shè)備驅(qū)動(dòng)
新建設(shè)備文件leddriver.c,并根據(jù)上面的框架圖,編寫驅(qū)動(dòng)程序
? 宏定義及設(shè)備結(jié)構(gòu)體變量定義
/* 寄存器名 */
static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;
/* leddev 設(shè)備結(jié)構(gòu)體 */
struct leddev_dev {
dev_t devid; //設(shè)備號(hào)
struct cdev cdev; //cdev
struct class *class; //類
struct device *device; //設(shè)備
int major; //設(shè)備號(hào)
};
struct leddev_dev leddev; //led 設(shè)備
? 字符設(shè)備驅(qū)動(dòng)操作集函數(shù)實(shí)現(xiàn)
/* LED 打開(kāi)或關(guān)閉 */
void led0_switch(u8 sta) {
u32 val = 0;
if(sta == LEDON){
val = readl(GPIO1_DR);
val &= ~(1 << 3);
writel(val, GPIO1_DR);
}else if(sta == LEDOFF){
val = readl(GPIO1_DR);
val|= (1 << 3);
writel(val, GPIO1_DR);
}
}
/* 打開(kāi)設(shè)備 */
static int led_open(struct inode *inode, struct file *filp) {
filp->private_data = &leddev; // 設(shè)置私有數(shù)據(jù)
return 0;
}
/* 向設(shè)備寫數(shù)據(jù) */
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]; //獲取狀態(tài)值
if(ledstat == LEDON) {
led0_switch(LEDON); //打開(kāi)LED燈
}else if(ledstat == LEDOFF) {
led0_switch(LEDOFF); //關(guān)閉LED燈
}
return 0;
}
/* 設(shè)備操作函數(shù) */
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
? probe函數(shù)中,初始化IO并注冊(cè)字符設(shè)備
/* platform驅(qū)動(dòng)的 probe 函數(shù) */
static int led_probe(struct platform_device *dev) {
int i = 0;
int ressize[5];
u32 val = 0;
struct resource *ledsource[5];
printk("led driver and device has matched!rn");
/* 1、獲取資源 */
for (i = 0; i < 5; i++) {
ledsource[i] = platform_get_resource(dev, IORESOURCE_MEM, i);
if (!ledsource[i]) {
dev_err(&dev->dev, "No MEM resource for always onn");
return -ENXIO;
}
ressize[i] = resource_size(ledsource[i]);
}
/* 2、初始化 LED */
/* 寄存器地址映射 */
IMX6U_CCM_CCGR1 = ioremap(ledsource[0]->start, ressize[0]);
SW_MUX_GPIO1_IO03 = ioremap(ledsource[1]->start, ressize[1]);
SW_PAD_GPIO1_IO03 = ioremap(ledsource[2]->start, ressize[2]);
GPIO1_DR = ioremap(ledsource[3]->start, ressize[3]);
GPIO1_GDIR = ioremap(ledsource[4]->start, ressize[4]);
val = readl(IMX6U_CCM_CCGR1);
val &= ~(3 << 26); /* 清除以前的設(shè)置 */
val |= (3 << 26); /* 設(shè)置新值 */
writel(val, IMX6U_CCM_CCGR1);
/* 設(shè)置 GPIO1_IO03 復(fù)用功能,將其復(fù)用為 GPIO1_IO03 */
writel(5, SW_MUX_GPIO1_IO03);
writel(0x10B0, SW_PAD_GPIO1_IO03);
/* 設(shè)置 GPIO1_IO03 為輸出功能 */
val = readl(GPIO1_GDIR);
val &= ~(1 << 3); /* 清除以前的設(shè)置 */
val |= (1 << 3); /* 設(shè)置為輸出 */
writel(val, GPIO1_GDIR);
/* 默認(rèn)關(guān)閉 LED1 */
val = readl(GPIO1_DR);
val |= (1 << 3) ;
writel(val, GPIO1_DR);
/* 注冊(cè)字符設(shè)備驅(qū)動(dòng) */
/*1、創(chuàng)建設(shè)備號(hào) */
if (leddev.major) { /* 定義了設(shè)備號(hào) */
leddev.devid = MKDEV(leddev.major, 0);
register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);
} else { /* 沒(méi)有定義設(shè)備號(hào) */
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、添加一個(gè) cdev */
cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
/* 4、創(chuàng)建類 */
leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
if (IS_ERR(leddev.class)) {
return PTR_ERR(leddev.class);
}
/* 5、創(chuàng)建設(shè)備 */
leddev.device=device_create(leddev.class,NULL,leddev.devid,NULL,LEDDEV_NAME);
if (IS_ERR(leddev.device)) {
return PTR_ERR(leddev.device);
}
return 0;
}? remove函數(shù)中,刪除設(shè)備/* platform驅(qū)動(dòng)的 remove 函數(shù) */
static int led_remove(struct platform_device *dev) {
iounmap(IMX6U_CCM_CCGR1);
iounmap(SW_MUX_GPIO1_IO03);
iounmap(SW_PAD_GPIO1_IO03);
iounmap(GPIO1_DR);
iounmap(GPIO1_GDIR);
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驅(qū)動(dòng)結(jié)構(gòu)體,name變量要與platform設(shè)備結(jié)構(gòu)體的name保持一致,用于和設(shè)備匹配
/* platform 驅(qū)動(dòng)結(jié)構(gòu)體 */
static struct platform_driver led_driver = {
.driver = {
.name = "imx6ul-led", /* 驅(qū)動(dòng)名字,用于和設(shè)備匹配 */
},
.probe = led_probe,
.remove = led_remove,
};
? 驅(qū)動(dòng)入口和出口函數(shù)中,注冊(cè)和卸載platform驅(qū)動(dòng)
/* 驅(qū)動(dòng)模塊加載函數(shù) */
static int __init leddriver_init(void) {
return platform_driver_register(&led_driver);
}
/* 驅(qū)動(dòng)模塊卸載函數(shù) */
static void __exit leddriver_exit(void) {
platform_driver_unregister(&led_driver);
}
module_init(leddriver_init);
module_exit(leddriver_exit);
MODULE_LICENSE("GPL");
3. 測(cè)試程序編寫
新建測(cè)試文件platformledAPP.c,并編寫程序
int main(int argc, char *argv[]) {
int fd, retvalue;
char *filename;
unsigned char databuf[2];
if(argc != 3){
printf("Error Usage!rn");
return -1;
}
filename = argv[1];
/* 打開(kāi) led 驅(qū)動(dòng) */
fd = open(filename, O_RDWR);
if(fd < 0){
printf("file %s open failed!rn", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]); /* 要執(zhí)行的操作:打開(kāi)或關(guān)閉 */
retvalue = write(fd, databuf, sizeof(databuf));
if(retvalue < 0){
printf("LED Control Failed!rn");
close(fd);
return -1;
}
retvalue = close(fd); /* 關(guān)閉文件 */
if(retvalue < 0){
printf("file %s close failed!rn", argv[1]);
return -1;
}
return 0;
}
4. 編譯測(cè)試
? 編譯驅(qū)動(dòng)程序:當(dāng)前目錄下創(chuàng)建Makefile文件,并make編譯
KERNELDIR := /home/andyxi/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga_andyxi
CURRENT_PATH := $(shell pwd)
obj-m := leddevice.o leddriver.o
build: kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
? 編譯測(cè)試程序:無(wú)需內(nèi)核參與,直接編譯即可
arm-linux-gnueabihf-gcc platformledApp.c -o platformledApp
? 將兩個(gè)驅(qū)動(dòng)文件和測(cè)試文件拷貝至rootfs/lib/modules/4.1.15后加載驅(qū)動(dòng),加載成功后,總線就會(huì)進(jìn)行匹配
depmod #第一次加載驅(qū)動(dòng)時(shí),需使用“depmod”命令
modprobe leddevice.ko
modprobe leddriver.ko
? 根文件系統(tǒng)中/sys/bus/platform/目錄下保存著當(dāng)前platform總線下的設(shè)備和驅(qū)動(dòng),子目錄devices為platform設(shè)備,子目錄drivers為platform驅(qū)動(dòng),設(shè)備模塊和驅(qū)動(dòng)模塊加載成功后,會(huì)在相應(yīng)目錄下找到對(duì)應(yīng)的設(shè)備和驅(qū)動(dòng)
? 使用以下命令打開(kāi)或者關(guān)閉LED燈
./platformledAPP /dev/platled 1
./platformledAPP /dev/platled 0