硬编码式的驱动开发带来的问题:
struct device来表示一个具体设备,主要提供具体设备相关的资源(如寄存器地址、GPIO、中断等等)
struct device_driver来表示一个设备驱动,一个驱动可以支持多个操作逻辑相同的设备
带来的问题-------怎样将二者进行关联(匹配)?
硬件上同一总线上的设备遵循一致的时序通信,在其基础上增加管理设备和驱动的软件功能
于是引入总线(bus),各种总线的核心框架由内核来实现,通信时序一般由SOC供应商支持
内核中用struct bus_type来表示一种总线,总线可以是实际存在的总线,也可以是虚拟总线:
理解方式:
设备:提供硬件资源——男方
驱动:提供驱动代码——女方
总线:匹配设备和驱动——婚介所:提供沟通机制,完成拉郎配
初期方案,各种device需要编码方式注册进内核中的设备管理结构中,为了进一步减少这样的编码,引进设备树
2.1 struct device
struct device
{
struct bus_type *bus; //总线类型
dev_t devt; //设备号
struct device_driver *driver; //设备驱动
struct device_node *of_node;//设备树中的节点,重要
void (*release)(struct device *dev);//删除设备,重要
//.......
};
2.2 struct device_driver
struct device_driver
{
const char *name; //驱动名称,匹配device用,重要
struct bus_type *bus; //总线类型
struct module *owner; //模块THIS_MODULE
const struct of_device_id *of_match_table;//用于设备树匹配 of_match_ptr(某struct of_device_id对象地址) 重要
//......
};
struct of_device_id
{
char name[32];//设备名
char type[32];//设备类型
char compatible[128]; //用于device和driver的match,重点
};
//用到结构体数组,一般不指定大小,初始化时最后加{}表示数组结束
platform是一种虚拟总线,主要用来管理那些不需要时序通信的设备
基本结构图:
struct platform_device
{
const char *name; //匹配用的名字
int id;//设备id,用于在该总线上同名的设备进行编号,如果只有一个设备,则为-1
struct device dev; //设备模块必须包含该结构体
struct resource *resource;//资源结构体 指向资源数组
u32 num_resources;//资源的数量 资源数组的元素个数
const struct platform_device_id *id_entry;//设备八字
};
struct platform_device_id
{
char name[20];//匹配用名称
kernel_ulong_t driver_data;//需要向驱动传输的其它数据
};
struct resource
{
resource_size_t start; //资源起始位置
resource_size_t end; //资源结束位置
const char *name;
unsigned long flags; //区分资源是什么类型的
};
#define IORESOURCE_MEM 0x00000200
#define IORESOURCE_IRQ 0x00000400
/*
flags 指资源类型,我们常用的是 IORESOURCE_MEM、IORESOURCE_IRQ 这两种。start 和 end 的含义会随着 flags而变更,如
a -- flags为IORESOURCE_MEM 时,start 、end 分别表示该platform_device占据的内存的开始地址和结束值;注意不同MEM的地址值不能重叠
b -- flags为 IORESOURCE_IRQ 时,start 、end 分别表示该platform_device使用的中断号的开始地址和结束值
*/
/**
*注册:把指定设备添加到内核中平台总线的设备列表,等待匹配,匹配成功则回调驱动中probe;
*/
int platform_device_register(struct platform_device *);
/**
*注销:把指定设备从设备列表中删除,如果驱动已匹配则回调驱动方法和设备信息中的release;
*/
void platform_device_unregister(struct platform_device *);
struct resource *platform_get_resource(struct platform_device *dev,unsigned int type, unsigned int num);
/*
功能:获取设备资源
参数:dev:平台驱动
type:获取的资源类型
num:对应类型资源的序号(如第0个MEM、第2个IRQ等,不是数组下标)
返回值:成功:资源结构体首地址,失败:NULL
*/
struct platform_driver
{
int (*probe)(struct platform_device *);//设备和驱动匹配成功之后调用该函数
int (*remove)(struct platform_device *);//设备卸载了调用该函数
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;//内核里所有的驱动必须包含该结构体
const struct platform_device_id *id_table; //能够支持的设备八字数组,用到结构体数组,一般不指定大小,初始化时最后加{}表示数组结束
};
int platform_driver_register(struct platform_driver*pdrv);
/*
功能:注册平台设备驱动
参数:pdrv:平台设备驱动结构体
返回值:成功:0
失败:错误码
*/
void platform_driver_unregister(struct platform_driver*pdrv);
2.1 名称匹配:一个驱动只对应一个设备 ----- 优先级最低
2.2 id匹配(可想象成八字匹配):一个驱动可以对应多个设备 ------优先级次低
device模块中,id的name成员必须与struct platform_device中的name成员内容一致
因此device模块中,struct platform_device中的name成员必须指定
driver模块中,struct platform_driver成员driver的name成员必须指定,但与device模块中name可以不相同
2.3 设备树匹配:内核启动时根据设备树自动产生的设备 ------ 优先级最高
使用compatible属性进行匹配,注意设备树中compatible属性值不要包含空白字符
id_table可不设置,但struct platform_driver成员driver的name成员必须设置
/*platform device框架*/
#include
#include
#include
#include
//定义资源数组
static void device_release(struct device *dev)
{
printk("platform: device release\n");
}
struct platform_device test_device = {
.id = -1,
.name = "test_device",//必须初始化
.dev.release = device_release,
};
static int __init platform_device_init(void)
{
platform_device_register(&test_device);
return 0;
}
static void __exit platform_device_exit(void)
{
platform_device_unregister(&test_device);
}
module_init(platform_device_init);
module_exit(platform_device_exit);
MODULE_LICENSE("Dual BSD/GPL");
/*platform driver框架*/
#include
#include
#include
#include
static int driver_probe(struct platform_device *dev)
{
printk("platform: match ok!\n");
return 0;
}
static int driver_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}
struct platform_driver test_driver = {
.probe = driver_probe,
.remove = driver_remove,
.driver = {
.name = "test_device", //必须初始化
},
};
static int __init platform_driver_init(void)
{
platform_driver_register(&test_driver);
return 0;
}
static void __exit platform_driver_exit(void)
{
platform_driver_unregister(&test_driver);
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);
MODULE_LICENSE("Dual BSD/GPL");
设备中增加资源,驱动中访问资源
mychar_led.h
#ifndef MYCHAR_LED_H
#define MYCHAR_LED_H
#include
#define MYCHAR_LED_MAGIC 'c'
#define MYCHAR_LED_OFF _IO(MYCHAR_LED_MAGIC, 0)
#define MYCHAR_LED_ON _IO(MYCHAR_LED_MAGIC, 1)
#endif
fs4412leds_device.c
#include
#include
#include
#define GPX1CON 0x11000C20
#define GPX1DAT 0x11000C24
#define GPX2CON 0x11000C40
#define GPX2DAT 0x11000C44
#define GPF3CON 0x114001E0
#define GPF3DAT 0x114001E4
void fs4412leds_dev_release (struct device *pdev) {
printk("fs4412leds_dev_release is called\n");
}
struct resource fs4412leds_dev_res [] =
{
[0] = {.start = GPX1CON, .end = GPX1CON + 3, .name = "GPX1CON", .flags = IORESOURCE_MEM},
[0] = {.start = GPX1DAT, .end = GPX1DAT + 3, .name = "GPX1DAT", .flags = IORESOURCE_MEM},
[0] = {.start = GPX2CON, .end = GPX2CON + 3, .name = "GPX2CON", .flags = IORESOURCE_MEM},
[0] = {.start = GPX2DAT, .end = GPX2DAT + 3, .name = "GPX2DAT", .flags = IORESOURCE_MEM},
[0] = {.start = GPF3CON, .end = GPF3CON + 3, .name = "GPF3CON", .flags = IORESOURCE_MEM},
[0] = {.start = GPF3DAT, .end = GPF3DAT + 3, .name = "GPF3DAT", .flags = IORESOURCE_MEM},
};
struct platform_device_id fs4412leds_id =
{
.name = "fs4412leds",
};
struct platform_device fs4412leds_device =
{
.name = "fs4412leds",
.dev.release = fs4412leds_dev_release,
.resource = fs4412leds_dev_res,
.num_resources = ARRAY_SIZE(fs4412leds_dev_res),
.id_entry = &fs4412leds_id,
};
int __init fs4412leds_device_init(void)
{
platform_device_register(&fs4412leds_device);
return 0;
}
void __exit fs4412leds_device_exit(void)
{
platform_device_unregister(&fs4412leds_device);
}
MODULE_LICENSE("GPL");
module_init(fs4412leds_device_init);
module_exit(fs4412leds_device_exit);
fs4412leds_driver.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mychar_led.h"
#define BUF_LEN 100
int major = 11; //主设备号
int minor = 0; //次设备号
int char_num = 1; //设备号数量
struct myled_dev
{
struct cdev mydev;
volatile unsigned long * pled2_con;
volatile unsigned long * pled2_dat;
volatile unsigned long * pled3_con;
volatile unsigned long * pled3_dat;
volatile unsigned long * pled4_con;
volatile unsigned long * pled4_dat;
volatile unsigned long * pled5_con;
volatile unsigned long * pled5_dat;
};
struct myled_dev *pgmydev = NULL;
int myled_open (struct inode *pnode, struct file *pfile)//打开设备
{
pfile->private_data = (void *) (container_of(pnode->i_cdev, struct myled_dev, mydev));
return 0;
}
int myled_close(struct inode *pnode, struct file *pfile)//关闭设备
{
return 0;
}
void led_on(struct myled_dev *pmydev, int ledno) {
switch(ledno) {
case 2:
writel(readl(pmydev->pled2_dat) | (0x1 << 7), pmydev->pled2_dat);
break;
case 3:
writel(readl(pmydev->pled3_dat) | (0x1 << 0), pmydev->pled3_dat);
break;
case 4:
writel(readl(pmydev->pled4_dat) | (0x1 << 4), pmydev->pled4_dat);
break;
case 5:
writel(readl(pmydev->pled5_dat) | (0x1 << 5), pmydev->pled5_dat);
break;
}
}
void led_off(struct myled_dev *pmydev, int ledno) {
switch(ledno) {
case 2:
writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)), pmydev->pled2_dat);
break;
case 3:
writel(readl(pmydev->pled3_dat) & (~(0x1 << 0)), pmydev->pled3_dat);
break;
case 4:
writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)), pmydev->pled4_dat);
break;
case 5:
writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)), pmydev->pled5_dat);
break;
}
}
long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
struct myled_dev *pmydev = (struct myled_dev *)pfile->private_data;
if(arg < 2 || arg > 5) {
return -1;
}
switch(cmd) {
case MYCHAR_LED_OFF:
led_off(pmydev, arg);
break;
case MYCHAR_LED_ON:
led_on(pmydev, arg);
break;
default:
return -1;
}
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = myled_open,
.release = myled_close,
.unlocked_ioctl = myled_ioctl,
};
void ioremap_ledreg(struct myled_dev *pmydev, struct platform_device *p_pltdev) {
struct resource *pres = NULL;
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 2);
pmydev->pled2_con = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 3);
pmydev->pled2_dat = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 0);
pmydev->pled3_con = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 1);
pmydev->pled3_dat = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 4);
pmydev->pled4_con = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 5);
pmydev->pled4_dat = ioremap(pres->start, 4);
pmydev->pled5_con = pmydev->pled4_con;
pmydev->pled5_dat = pmydev->pled4_dat;
}
void set_output_ledconreg(struct myled_dev *pmydev) {
writel((readl(pmydev->pled2_con) & (~(0xF << 28))) | (0x1 << 28), pmydev->pled2_con);
writel((readl(pmydev->pled3_con) & (~(0xF << 0))) | (0x1), pmydev->pled3_con);
writel((readl(pmydev->pled4_con) & (~(0xF << 16))) | (0x1 << 16), pmydev->pled4_con);
writel((readl(pmydev->pled5_con) & (~(0xF << 20))) | (0x1 << 20), pmydev->pled5_con);
writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)), pmydev->pled2_dat);
writel(readl(pmydev->pled3_dat) & (~(0x1 << 0)), pmydev->pled3_dat);
writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)), pmydev->pled4_dat);
writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)), pmydev->pled5_dat);
}
void iounmap_ledreg(struct myled_dev *pmydev) {
iounmap(pmydev->pled2_con);
pmydev->pled2_con = NULL;
iounmap(pmydev->pled2_dat);
pmydev->pled2_dat = NULL;
iounmap(pmydev->pled3_con);
pmydev->pled3_con = NULL;
iounmap(pmydev->pled3_dat);
pmydev->pled3_dat = NULL;
iounmap(pmydev->pled4_con);
pmydev->pled4_con = NULL;
iounmap(pmydev->pled4_dat);
pmydev->pled4_dat = NULL;
pmydev->pled5_con = NULL;
pmydev->pled5_dat = NULL;
}
int fs4412leds_driver_probe(struct platform_device *p_pltdev)
{
int ret = 0;
dev_t devno = MKDEV(major, minor);
/* 手动申请设备号 */
ret = register_chrdev_region(devno, char_num, "myled");
if (ret) {
/* 动态申请设备号 */
ret = alloc_chrdev_region(&devno, minor, char_num, "myled");
if(ret){
printk("get devno failed\n");
return -1;
}
/*申请成功 更新设备号*/
major = MAJOR(devno);
}
pgmydev = (struct myled_dev *)kmalloc(sizeof(struct myled_dev), GFP_KERNEL);
if(NULL == pgmydev) {
unregister_chrdev_region(devno, char_num);
printk("kmalloc for 'struct myled_dev' failed\n");
return -1;
}
memset(pgmydev, 0, sizeof(struct myled_dev));
/* 给struct cdev对象指定操作函数集 */
cdev_init(&pgmydev->mydev, &myops);
/* 将struct cdev对象添加到内核对应的数据结构中 */
pgmydev->mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev, devno, char_num);
/* ioremap */
ioremap_ledreg(pgmydev, p_pltdev);
/* con-register set output */
set_output_ledconreg(pgmydev);
return 0;
}
int fs4412leds_driver_remove(struct platform_device *p_pltdev)
{
dev_t devno = MKDEV(major, minor);
/* iounmap */
iounmap_ledreg(pgmydev);
/* 从内核中移除一个字符设备 */
cdev_del(&pgmydev->mydev);
/* 回收设备号 */
unregister_chrdev_region(devno, char_num);
kfree(pgmydev);
pgmydev = NULL;
return 0;
}
struct platform_driver fs4412leds_driver =
{
.driver.name = "fs4412leds",
.probe = fs4412leds_driver_probe,
.remove = fs4412leds_driver_remove,
};
int __init fs4412leds_driver_init(void)
{
platform_driver_register(&fs4412leds_driver);
return 0;
}
void __exit fs4412leds_driver_exit(void)
{
platform_driver_unregister(&fs4412leds_driver);
}
MODULE_LICENSE("GPL");
module_init(fs4412leds_driver_init);
module_exit(fs4412leds_driver_exit);
id匹配(可想象成八字匹配):一个驱动可以对应多个设备 ------优先级次低
注意事项:
/*platform device框架*/
#include
#include
#include
#include
//定义资源数组
static void device_release(struct device *dev)
{
printk("platform: device release\n");
}
struct platform_device_id test_id = {
.name = "test_device",
};
struct platform_device test_device = {
.name = "test_device",//必须初始化
.dev.release = device_release,
.id_entry = &test_id,
};
static int __init platform_device_init(void)
{
platform_device_register(&test_device);
return 0;
}
static void __exit platform_device_exit(void)
{
platform_device_unregister(&test_device);
}
module_init(platform_device_init);
module_exit(platform_device_exit);
MODULE_LICENSE("Dual BSD/GPL");
/*platform driver框架*/
#include
#include
#include
#include
static int driver_probe(struct platform_device *dev)
{
printk("platform: match ok!\n");
return 0;
}
static int driver_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}
struct platform_device_id testdrv_ids[] =
{
[0] = {.name = "test_device"},
[1] = {.name = "abcxyz"},
[2] = {}, //means ending
};
struct platform_driver test_driver = {
.probe = driver_probe,
.remove = driver_remove,
.driver = {
.name = "xxxxx", //必须初始化
},
.id_table = testdrv_ids,
};
static int __init platform_driver_init(void)
{
platform_driver_register(&test_driver);
return 0;
}
static void __exit platform_driver_exit(void)
{
platform_driver_unregister(&test_driver);
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);
MODULE_LICENSE("Dual BSD/GPL");
用到结构体数组,一般不指定大小,初始化时最后加{}表示数组结束
设备中增加资源,驱动中访问资源
fs4412leds_device.c
#include
#include
#include
#define GPX1CON 0x11000C20
#define GPX1DAT 0x11000C24
#define GPX2CON 0x11000C40
#define GPX2DAT 0x11000C44
#define GPF3CON 0x114001E0
#define GPF3DAT 0x114001E4
void fs4412leds_dev_release (struct device *pdev) {
printk("fs4412leds_dev_release is called\n");
}
struct resource fs4412leds_dev_res [] =
{
[0] = {.start = GPX1CON, .end = GPX1CON + 3, .name = "GPX1CON", .flags = IORESOURCE_MEM},
[0] = {.start = GPX1DAT, .end = GPX1DAT + 3, .name = "GPX1DAT", .flags = IORESOURCE_MEM},
[0] = {.start = GPX2CON, .end = GPX2CON + 3, .name = "GPX2CON", .flags = IORESOURCE_MEM},
[0] = {.start = GPX2DAT, .end = GPX2DAT + 3, .name = "GPX2DAT", .flags = IORESOURCE_MEM},
[0] = {.start = GPF3CON, .end = GPF3CON + 3, .name = "GPF3CON", .flags = IORESOURCE_MEM},
[0] = {.start = GPF3DAT, .end = GPF3DAT + 3, .name = "GPF3DAT", .flags = IORESOURCE_MEM},
};
struct platform_device_id fs4412leds_id =
{
.name = "fs4412leds",
};
struct platform_device fs4412leds_device =
{
.name = "fs4412leds",
.dev.release = fs4412leds_dev_release,
.resource = fs4412leds_dev_res,
.num_resources = ARRAY_SIZE(fs4412leds_dev_res),
.id_entry = &fs4412leds_id,
};
int __init fs4412leds_device_init(void)
{
platform_device_register(&fs4412leds_device);
return 0;
}
void __exit fs4412leds_device_exit(void)
{
platform_device_unregister(&fs4412leds_device);
}
MODULE_LICENSE("GPL");
module_init(fs4412leds_device_init);
module_exit(fs4412leds_device_exit);
fs4412leds_driver.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mychar_led.h"
#define BUF_LEN 100
int major = 11; //主设备号
int minor = 0; //次设备号
int char_num = 1; //设备号数量
struct myled_dev
{
struct cdev mydev;
volatile unsigned long * pled2_con;
volatile unsigned long * pled2_dat;
volatile unsigned long * pled3_con;
volatile unsigned long * pled3_dat;
volatile unsigned long * pled4_con;
volatile unsigned long * pled4_dat;
volatile unsigned long * pled5_con;
volatile unsigned long * pled5_dat;
};
struct myled_dev *pgmydev = NULL;
int myled_open (struct inode *pnode, struct file *pfile)//打开设备
{
pfile->private_data = (void *) (container_of(pnode->i_cdev, struct myled_dev, mydev));
return 0;
}
int myled_close(struct inode *pnode, struct file *pfile)//关闭设备
{
return 0;
}
void led_on(struct myled_dev *pmydev, int ledno) {
switch(ledno) {
case 2:
writel(readl(pmydev->pled2_dat) | (0x1 << 7), pmydev->pled2_dat);
break;
case 3:
writel(readl(pmydev->pled3_dat) | (0x1 << 0), pmydev->pled3_dat);
break;
case 4:
writel(readl(pmydev->pled4_dat) | (0x1 << 4), pmydev->pled4_dat);
break;
case 5:
writel(readl(pmydev->pled5_dat) | (0x1 << 5), pmydev->pled5_dat);
break;
}
}
void led_off(struct myled_dev *pmydev, int ledno) {
switch(ledno) {
case 2:
writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)), pmydev->pled2_dat);
break;
case 3:
writel(readl(pmydev->pled3_dat) & (~(0x1 << 0)), pmydev->pled3_dat);
break;
case 4:
writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)), pmydev->pled4_dat);
break;
case 5:
writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)), pmydev->pled5_dat);
break;
}
}
long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
struct myled_dev *pmydev = (struct myled_dev *)pfile->private_data;
if(arg < 2 || arg > 5) {
return -1;
}
switch(cmd) {
case MYCHAR_LED_OFF:
led_off(pmydev, arg);
break;
case MYCHAR_LED_ON:
led_on(pmydev, arg);
break;
default:
return -1;
}
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = myled_open,
.release = myled_close,
.unlocked_ioctl = myled_ioctl,
};
void ioremap_ledreg(struct myled_dev *pmydev, struct platform_device *p_pltdev) {
struct resource *pres = NULL;
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 2);
pmydev->pled2_con = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 3);
pmydev->pled2_dat = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 0);
pmydev->pled3_con = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 1);
pmydev->pled3_dat = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 4);
pmydev->pled4_con = ioremap(pres->start, 4);
pres = platform_get_resource(p_pltdev, IORESOURCE_MEM, 5);
pmydev->pled4_dat = ioremap(pres->start, 4);
pmydev->pled5_con = pmydev->pled4_con;
pmydev->pled5_dat = pmydev->pled4_dat;
}
void set_output_ledconreg(struct myled_dev *pmydev) {
writel((readl(pmydev->pled2_con) & (~(0xF << 28))) | (0x1 << 28), pmydev->pled2_con);
writel((readl(pmydev->pled3_con) & (~(0xF << 0))) | (0x1), pmydev->pled3_con);
writel((readl(pmydev->pled4_con) & (~(0xF << 16))) | (0x1 << 16), pmydev->pled4_con);
writel((readl(pmydev->pled5_con) & (~(0xF << 20))) | (0x1 << 20), pmydev->pled5_con);
writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)), pmydev->pled2_dat);
writel(readl(pmydev->pled3_dat) & (~(0x1 << 0)), pmydev->pled3_dat);
writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)), pmydev->pled4_dat);
writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)), pmydev->pled5_dat);
}
void iounmap_ledreg(struct myled_dev *pmydev) {
iounmap(pmydev->pled2_con);
pmydev->pled2_con = NULL;
iounmap(pmydev->pled2_dat);
pmydev->pled2_dat = NULL;
iounmap(pmydev->pled3_con);
pmydev->pled3_con = NULL;
iounmap(pmydev->pled3_dat);
pmydev->pled3_dat = NULL;
iounmap(pmydev->pled4_con);
pmydev->pled4_con = NULL;
iounmap(pmydev->pled4_dat);
pmydev->pled4_dat = NULL;
pmydev->pled5_con = NULL;
pmydev->pled5_dat = NULL;
}
int fs4412leds_driver_probe(struct platform_device *p_pltdev)
{
int ret = 0;
dev_t devno = MKDEV(major, minor);
/* 手动申请设备号 */
ret = register_chrdev_region(devno, char_num, "myled");
if (ret) {
/* 动态申请设备号 */
ret = alloc_chrdev_region(&devno, minor, char_num, "myled");
if(ret){
printk("get devno failed\n");
return -1;
}
/*申请成功 更新设备号*/
major = MAJOR(devno);
}
pgmydev = (struct myled_dev *)kmalloc(sizeof(struct myled_dev), GFP_KERNEL);
if(NULL == pgmydev) {
unregister_chrdev_region(devno, char_num);
printk("kmalloc for 'struct myled_dev' failed\n");
return -1;
}
memset(pgmydev, 0, sizeof(struct myled_dev));
/* 给struct cdev对象指定操作函数集 */
cdev_init(&pgmydev->mydev, &myops);
/* 将struct cdev对象添加到内核对应的数据结构中 */
pgmydev->mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev, devno, char_num);
/* ioremap */
ioremap_ledreg(pgmydev, p_pltdev);
/* con-register set output */
set_output_ledconreg(pgmydev);
return 0;
}
int fs4412leds_driver_remove(struct platform_device *p_pltdev)
{
dev_t devno = MKDEV(major, minor);
/* iounmap */
iounmap_ledreg(pgmydev);
/* 从内核中移除一个字符设备 */
cdev_del(&pgmydev->mydev);
/* 回收设备号 */
unregister_chrdev_region(devno, char_num);
kfree(pgmydev);
pgmydev = NULL;
return 0;
}
struct platform_device_id fs4412ledsdrv_ids[] =
{
[0] = {.name = "fs4412leds"},
[1] = {.name = "abcxyz"},
[2] = {}, //means ending
};
struct platform_driver fs4412leds_driver =
{
.driver.name = "fs4412leds",
.probe = fs4412leds_driver_probe,
.remove = fs4412leds_driver_remove,
.id_table = fs4412ledsdrv_ids,
};
int __init fs4412leds_driver_init(void)
{
platform_driver_register(&fs4412leds_driver);
return 0;
}
void __exit fs4412leds_driver_exit(void)
{
platform_driver_unregister(&fs4412leds_driver);
}
MODULE_LICENSE("GPL");
module_init(fs4412leds_driver_init);
module_exit(fs4412leds_driver_exit);
设备树匹配:内核启动时根据设备树自动产生的设备 ------ 优先级最高
注意事项:
/*platform driver框架*/
#include
#include
#include
#include
static int driver_probe(struct platform_device *dev)
{
printk("platform: match ok!\n");
return 0;
}
static int driver_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}
struct platform_device_id testdrv_ids[] =
{
[0] = {.name = "test_device"},
[1] = {.name = "abcxyz"},
[2] = {}, //means ending
};
struct of_device_id test_of_ids[] =
{
[0] = {.compatible = "xyz,abc"},
[1] = {.compatible = "qwe,opq"},
[2] = {},
};
struct platform_driver test_driver = {
.probe = driver_probe,
.remove = driver_remove,
.driver = {
.name = "xxxxx", //必须初始化
.of_match_table = test_of_ids,
},
};
static int __init platform_driver_init(void)
{
platform_driver_register(&test_driver);
return 0;
}
static void __exit platform_driver_exit(void)
{
platform_driver_unregister(&test_driver);
}
module_init(platform_driver_init);
module_exit(platform_driver_exit);
MODULE_LICENSE("Dual BSD/GPL");
fs4412leds_driver.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mychar_led.h"
#define BUF_LEN 100
int major = 11; //主设备号
int minor = 0; //次设备号
int char_num = 1; //设备号数量
struct myled_dev
{
struct cdev mydev;
unsigned int led2gpio;
unsigned int led3gpio;
unsigned int led4gpio;
unsigned int led5gpio;
};
struct myled_dev *pgmydev = NULL;
int myled_open (struct inode *pnode, struct file *pfile)//打开设备
{
pfile->private_data = (void *) (container_of(pnode->i_cdev, struct myled_dev, mydev));
return 0;
}
int myled_close(struct inode *pnode, struct file *pfile)//关闭设备
{
return 0;
}
void led_on(struct myled_dev *pmydev, int ledno) {
switch(ledno) {
case 2:
gpio_set_value(pmydev->led2gpio, 1);
break;
case 3:
gpio_set_value(pmydev->led3gpio, 1);
break;
case 4:
gpio_set_value(pmydev->led4gpio, 1);
break;
case 5:
gpio_set_value(pmydev->led5gpio, 1);
break;
}
}
void led_off(struct myled_dev *pmydev, int ledno) {
switch(ledno) {
case 2:
gpio_set_value(pmydev->led2gpio, 0);
break;
case 3:
gpio_set_value(pmydev->led3gpio, 0);
break;
case 4:
gpio_set_value(pmydev->led4gpio, 0);
break;
case 5:
gpio_set_value(pmydev->led5gpio, 0);
break;
}
}
long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
{
struct myled_dev *pmydev = (struct myled_dev *)pfile->private_data;
if(arg < 2 || arg > 5) {
return -1;
}
switch(cmd) {
case MYCHAR_LED_OFF:
led_off(pmydev, arg);
break;
case MYCHAR_LED_ON:
led_on(pmydev, arg);
break;
default:
return -1;
}
return 0;
}
struct file_operations myops = {
.owner = THIS_MODULE,
.open = myled_open,
.release = myled_close,
.unlocked_ioctl = myled_ioctl,
};
/* 得到GPIO编号,并向内核申请GPIO */
void request_leds_gpio(struct myled_dev *pmydev, struct device_node *pnode) {
pmydev->led2gpio = of_get_named_gpio(pnode, "led2-gpio", 0);
gpio_request(pmydev->led2gpio, "led2");
pmydev->led3gpio = of_get_named_gpio(pnode, "led3-gpio", 0);
gpio_request(pmydev->led3gpio, "led3");
pmydev->led4gpio = of_get_named_gpio(pnode, "led4-gpio", 0);
gpio_request(pmydev->led4gpio, "led4");
pmydev->led5gpio = of_get_named_gpio(pnode, "led5-gpio", 0);
gpio_request(pmydev->led5gpio, "led5");
}
/* 将引脚设置成输出模式,同时设置默认值为低电平 0 */
void set_leds_gpio_output(struct myled_dev *pmydev) {
gpio_direction_output(pmydev->led2gpio, 0);
gpio_direction_output(pmydev->led3gpio, 0);
gpio_direction_output(pmydev->led4gpio, 0);
gpio_direction_output(pmydev->led5gpio, 0);
}
void free_leds_gpio(struct myled_dev *pmydev) {
gpio_free(pmydev->led2gpio);
gpio_free(pmydev->led3gpio);
gpio_free(pmydev->led4gpio);
gpio_free(pmydev->led5gpio);
}
int myled_probe(struct platform_device *p_pltdev)
{
int ret = 0;
dev_t devno = MKDEV(major, minor);
struct device_node * pnode = NULL;
/* 手动申请设备号 */
ret = register_chrdev_region(devno, char_num, "myled");
if (ret) {
/* 动态申请设备号 */
ret = alloc_chrdev_region(&devno, minor, char_num, "myled");
if(ret){
printk("get devno failed\n");
return -1;
}
/*申请成功 更新设备号*/
major = MAJOR(devno);
}
pgmydev = (struct myled_dev *)kmalloc(sizeof(struct myled_dev), GFP_KERNEL);
if(NULL == pgmydev) {
unregister_chrdev_region(devno, char_num);
printk("kmalloc for 'struct myled_dev' failed\n");
return -1;
}
memset(pgmydev, 0, sizeof(struct myled_dev));
/* 给struct cdev对象指定操作函数集 */
cdev_init(&pgmydev->mydev, &myops);
/* 将struct cdev对象添加到内核对应的数据结构中 */
pgmydev->mydev.owner = THIS_MODULE;
cdev_add(&pgmydev->mydev, devno, char_num);
pnode = p_pltdev->dev.of_node;
/* ioremap */
request_leds_gpio(pgmydev, pnode);
/* con-register set output */
set_leds_gpio_output(pgmydev);
return 0;
}
int myled_remove(struct platform_device *p_pltdev)
{
dev_t devno = MKDEV(major, minor);
/* iounmap */
free_leds_gpio(pgmydev);
/* 从内核中移除一个字符设备 */
cdev_del(&pgmydev->mydev);
/* 回收设备号 */
unregister_chrdev_region(devno, char_num);
kfree(pgmydev);
pgmydev = NULL;
return 0;
}
struct of_device_id myleddev_of_ids[] =
{
[0] = {.compatible = "fs4412,led2-5"},
[1] = {.compatible = "fs4412,key2"},
[2] = {},
};
struct platform_driver myled_driver =
{
.driver = {
.name = "fs4412leds",
.of_match_table = myleddev_of_ids,
},
.probe = myled_probe,
.remove = myled_remove,
};
int __init myled_init(void) {
platform_driver_register(&myled_driver);
return 0;
}
void __exit myled_exit(void) {
platform_driver_unregister(&myled_driver);
}
MODULE_LICENSE("GPL");
module_init(myled_init);
module_exit(myled_exit);
struct platform_driver xxx = {
...
};
module_platform_driver(xxx);
//最终展开后就是如下形式:
static int __init xxx_init(void)
{
return platform_driver_register(&xxx);
}
module_init(xxx_init);
static void __exit xxx_init(void)
{
return platform_driver_unregister(&xxx);
}
module_exit(xxx_exit)
;