dev_led:
#include<linux/init.h>
#include<linux/module.h>#include<linux/ioport.h>#include<linux/platform_device.h>#include"led.h"/*1.构建资源*/
struct resource s5pc_led_resource[]={ [0]={ .start =S5PC100_PA_LED, //0xE03001C0 .end =S5PC100_PA_LED+SZ_8-1, .flags =IORESOURCE_MEM, },};static void led_release(struct device *dev)
{ }/*2.构建一个平台设备结构体*/
struct platform_device s5pc_led_device ={ .name ="s5pc-led", .id =-1, .num_resources =ARRAY_SIZE(s5pc_led_resource), .resource =s5pc_led_resource, .dev ={ .release =led_release, },};static int __init led_dev_init(void)
{ /*3.注册平台设备 *将平台设备注册到平台总线的设备链表里面了 */ platform_device_register(&s5pc_led_device); return 0;}static void __exit led_dev_exit(void)
{ platform_device_unregister(&s5pc_led_device);}module_init(led_dev_init);
module_exit(led_dev_exit);MODULE_LICENSE("GPL");
led_drv:
#include<linux/init.h>
#include<linux/module.h>#include<linux/fs.h> //file_operations#include<linux/device.h> //device_creat class_creat#include<linux/cdev.h> //struct cdev#include<linux/slab.h> //kamalloc#include<asm/io.h> //ioremap writel readl#include<asm/uaccess.h> //copy_to_user copy_from_user#include<linux/platform_device.h> //平台#include "led.h"/*构建本地结构体*/
struct led_device{ struct cdev *cdev; void __iomem *led_base; dev_t devno; struct class *led_class;};static struct led_device *s5pc_led_dev;
static int led_open(struct inode *inode, struct file *file)
{ printk(KERN_INFO "led open!\n");return 0;
}static ssize_t led_write(struct file *file, const char __user *buf, size_t size, loff_t *opps)
{ int ret=0; int val; /*获取用户空间的数据 *如果拷贝成功,则返回0 *失败返回剩下的没有拷贝成功的字节数>0 */ ret =copy_from_user(&val,buf,size);if(val){
/*点灯*/ writel((readl(s5pc_led_dev->led_base+S5PC100_GPG3DAT)|(0x1<<0)),s5pc_led_dev->led_base+S5PC100_GPG3DAT); }else{ /*灭灯*/ writel((readl(s5pc_led_dev->led_base+S5PC100_GPG3DAT)&~(0x1<<0)),s5pc_led_dev->led_base+S5PC100_GPG3DAT); } return ret?-EFAULT:size;} struct file_operations led_fops={ .owner =THIS_MODULE, .open =led_open, .write =led_write,};/*3.实现probe函数,在设备资源与设备驱动匹配成功的时候,才会调用*/
static int s5pc_led_probe(struct platform_device *pdev){ int ret; struct resource *res;s5pc_led_dev=kmalloc(sizeof(struct led_device), GFP_KERNEL);
/*3.1 获取资源*/ res=platform_get_resource(pdev,IORESOURCE_MEM,0); /*3.2 映射*/ s5pc_led_dev->led_base=ioremap(res->start, res->end-res->start+1);/*3.3 初始化设备,操作寄存器,将GPG3[0]设置为输出*/
writel((readl(s5pc_led_dev->led_base+S5PC100_GPG3CON)&~(0xf<<0))|(0x1<<0),s5pc_led_dev->led_base+S5PC100_GPG3CON);/*3.4 给用户提供接口*/
s5pc_led_dev->devno =MKDEV(LED_MAJOR, 0); ret=register_chrdev_region(s5pc_led_dev->devno,1,"leds");s5pc_led_dev->cdev=cdev_alloc();
cdev_init(s5pc_led_dev->cdev,&led_fops); cdev_add(s5pc_led_dev->cdev,s5pc_led_dev->devno,1);s5pc_led_dev->led_class=class_create(THIS_MODULE, "led_class");
device_create(s5pc_led_dev->led_class,NULL,s5pc_led_dev->devno,NULL,"led"); return 0;} /*4.实现remove函数*/static int s5pc_led_remove(struct platform_device *pdev){ unregister_chrdev_region(s5pc_led_dev->devno,1); cdev_del(s5pc_led_dev->cdev); iounmap(s5pc_led_dev->led_base); device_destroy(s5pc_led_dev->led_class,s5pc_led_dev->devno); class_destroy(s5pc_led_dev->led_class); return 0;}/*1.构建平台驱动结构体*/
struct platform_driver s5pc_led_driver={ .probe =s5pc_led_probe, .remove =s5pc_led_remove, .driver ={ .owner =THIS_MODULE, .name ="s5pc-led", },};static int __init led_dev_init(void)
{ /*2.注册平台驱动 *将平台驱动注册到平台总线的驱动链表中 */ platform_driver_register(&s5pc_led_driver); return 0;}static void __exit led_dev_exit(void)
{ platform_driver_unregister(&s5pc_led_driver);}module_init(led_dev_init);
module_exit(led_dev_exit);MODULE_LICENSE("GPL");test:
#include <stdio.h>
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>/*./led_test on 点灯
*./led_test off 灭灯 */int main(int argc,char **argv){ int fd; int buf=0; /*1.打开设备/打开驱动/打开设备文件*/ fd =open("/dev/led",O_RDWR); if(fd<0){ perror("open failed"); exit(1); }/*2.操作硬件*/
if(strcmp(argv[1],"on")==0){ buf =1; }else{ buf =0; }if(write(fd,&buf,4)!=4){
perror("write failed"); exit(1); } close(fd); return 0;}