1.在设备树根节点下添加设备节点
#define GROUP_PIN(g,p) ((g<<16) | (p))
/ {
100ask_led@0 {
compatible = "100as,leddrv";
pin = <GROUP_PIN(3, 1)>;
};
100ask_led@1 {
compatible = "100as,leddrv";
pin = <GROUP_PIN(5, 8)>;
};
};
(1)在设备树根节点下添加子节点,里面包含引脚信息和compatible属性,用于注册platform_device后与platform_driver匹配。
(2)用新的设备树文件后,内核设备树节点下会多出我们自己定义的设备树节点。
(3) 内核解析设备树文件后,会在platform_device下增加对应的设备节点,此时未安装驱动,节点下没有driver属性
(4) 安装设备驱动后,compatible属性对应后会生成一个转向platform_driver下面的driver属性
2.上层leddrv.c
1.定义主设备号
2.定义自己的file_operation结构体
3.通过入口函数init函数去内核注册这个结构体,register_chrdev和class_create
4.实现file_operation结构体中的open/write函数,
在其中再led_operations调用底层chip.c中的open函数和ctrl函数
5.出口函数exit,与init函数初始化顺序相反
6.其他项。module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
3.底层chip.c
1.定义led_operations结构体
2.定义platform_driver结构体,并实现其中的of_match_table
函数,在其成员中定义compatible属性与device一致
3.入口函数init中注册platform_driver结构体,并调用自定义函数register_led_operations(&board_demo_led_opr);将led_operations对象传到上层leddrv.c中,这样上层才能调用底层的open函数和ctrl函数
4.实现led_operations结构体中的open、ctrl函数,操控硬件寄存器实现对应功能。
5.出口函数exit
6.其他
static int board_demo_led_init (int which) /* 鍒濆鍖朙ED, which-鍝釜LED */
{
//printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
printk("init gpio: group %d, pin %d\n", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
switch(GROUP(g_ledpins[which]))
{
case 0:
{
printk("init pin of group 0 ...\n");
break;
}
case 1:
{
printk("init pin of group 1 ...\n");
break;
}
case 2:
{
printk("init pin of group 2 ...\n");
break;
}
case 3:
{
printk("init pin of group 3 ...\n");
break;
}
}
return 0;
}
static int board_demo_led_ctl (int which, char status) /* 鎺у埗LED, which-鍝釜LED, status:1-浜�,0-鐏� */
{
//printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
switch(GROUP(g_ledpins[which]))
{
case 0:
{
printk("set pin of group 0 ...\n");
break;
}
case 1:
{
printk("set pin of group 1 ...\n");
break;
}
case 2:
{
printk("set pin of group 2 ...\n");
break;
}
case 3:
{
printk("set pin of group 3 ...\n");
break;
}
}
return 0;
}
static struct led_operations board_demo_led_opr = {
.init = board_demo_led_init,
.ctl = board_demo_led_ctl,
};
struct led_operations *get_board_led_opr(void)
{
return &board_demo_led_opr;
}
static int chip_demo_gpio_probe(struct platform_device *pdev)
{
struct device_node *np;
int err = 0;
int led_pin;
np = pdev->dev.of_node;
if (!np)
return -1;
err = of_property_read_u32(np, "pin", &led_pin);
g_ledpins[g_ledcnt] = led_pin;
led_class_create_device(g_ledcnt);
g_ledcnt++;
return 0;
}
static int chip_demo_gpio_remove(struct platform_device *pdev)
{
int i = 0;
int err;
struct device_node *np;
int led_pin;
np = pdev->dev.of_node;
if (!np)
return -1;
err = of_property_read_u32(np, "pin", &led_pin);
for (i = 0; i < g_ledcnt; i++)
{
if (g_ledpins[i] == led_pin)
{
led_class_destroy_device(i);
g_ledpins[i] = -1;
break;
};
}
for (i = 0; i < g_ledcnt; i++)
{
if (g_ledpins[i] != -1)
break;
}
if (i == g_ledcnt)
g_ledcnt = 0;
return 0;
}
static const struct of_device_id ask100_leds[] = {
{ .compatible = "100as,leddrv" },
{ },
};
static struct platform_driver chip_demo_gpio_driver = {
.probe = chip_demo_gpio_probe,
.remove = chip_demo_gpio_remove,
.driver = {
.name = "100ask_led",
.of_match_table = ask100_leds,
},
};
static int __init chip_demo_gpio_drv_init(void)
{
int err;
err = platform_driver_register(&chip_demo_gpio_driver);
register_led_operations(&board_demo_led_opr);
return 0;
}
static void __exit lchip_demo_gpio_drv_exit(void)
{
platform_driver_unregister(&chip_demo_gpio_driver);
}
module_init(chip_demo_gpio_drv_init);
module_exit(lchip_demo_gpio_drv_exit);
MODULE_LICENSE("GPL");
当注册的platform_driver与总线上某一个platform_devicecompatible属性对应后,会调用latform_driver中的probe函数,从platform_driver获取资源引脚并进行存储,这样在open、ctrl函数中就知道具体操作哪个引脚。