platform (正点原子笔记)

platform总线的匹配方式

Linux系统内核使用bus_type结构体表示总线,定义在件include/linux/bus.h中

1 struct bus_type {
2 const char *name; /* 总线名字 */
3 const char *dev_name; 
4 struct device *dev_root;
5 struct device_attribute *dev_attrs;
6 const struct attribute_group **bus_groups; /* 总线属性 */
7 const struct attribute_group **dev_groups; /* 设备属性 */
8 const struct attribute_group **drv_groups; /* 驱动属性 */
9 
10 int (*match)(struct device *dev, struct device_driver *drv);
11 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
12 int (*probe)(struct device *dev);
13 int (*remove)(struct device *dev);
14 void (*shutdown)(struct device *dev);
15 
16 int (*online)(struct device *dev);
17 int (*offline)(struct device *dev);
18 int (*suspend)(struct device *dev, pm_message_t state);
19 int (*resume)(struct device *dev);
20 const struct dev_pm_ops *pm;
21 const struct iommu_ops *iommu_ops;
22 struct subsys_private *p;
23 struct lock_class_key lock_key;
24 };

总线使用 match 函数来根据注册的设备来查找对应的驱动,或者根据注册的驱动来查找相应的设备,因此每一条总线都必须实现此函数。match 函数有两个参数:dev 和 drv,这两个参数分别为 device 和 device_driver 类型,也就是设备和驱动。

platform 总线是 bus_type 的一个具体实例,定义在文件 drivers/base/platform.c,platform 总线定义如下:

1 struct bus_type platform_bus_type = {
2 .name = "platform",
3 .dev_groups = platform_dev_groups,
4 .match = platform_match,
5 .uevent = platform_uevent,
6 .pm = &platform_dev_pm_ops,
7 };

platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数

1 static int platform_match(struct device *dev,struct device_driver *drv)
2 {
3     struct platform_device *pdev = to_platform_device(dev);
4     struct platform_driver *pdrv = to_platform_driver(drv);
5 
6     /*When driver_override is set,only bind to the matching driver*/
7     if (pdev->driver_override)
8         return !strcmp(pdev->driver_override, drv->name);
9 
10     /* Attempt an OF style match first */
11     if (of_driver_match_device(dev, drv))
12         return 1;
13
14     /* Then try ACPI style match */
15     if (acpi_driver_match_device(dev, drv))
16         return 1;
17
18     /* Then try to match against the id table */
19     if (pdrv->id_table)
20         return platform_match_id(pdrv->id_table, pdev) != NULL;
21
22     /* fall-back to driver name match */
23     return (strcmp(pdev->name, drv->name) == 0);
24 }

如上函数所示,驱动和设备的匹配有四种方法:

第 11~12 行,第一种匹配方式, OF 类型的匹配,of_driver_match_device 函数定义在文件include/linux/of_device.h 中。device_driver 结构体(表示设备驱动)中有个名为of_match_table的成员变量,此成员变量保存着驱动的compatible匹配表,设备树中的每个设备节点的 compatible 属性会和 of_match_table 表中的所有成员比较,查看是否有相同的条目,如果有的话就表示设备和此驱动匹配,设备和驱动匹配成功以后 probe 函数就会执行。
第 15~16 行,第二种匹配方式,ACPI 匹配方式。
第 19~20 行,第三种匹配方式,id_table 匹配,每个 platform_driver 结构体有一个 id_table成员变量,顾名思义,保存了很多 id 信息。这些 id 信息存放着这个 platformd 驱动所支持的驱动类型。
第 23 行,第四种匹配方式,如果第三种匹配方式的 id_table 不存在的话就直接比较驱动和设备的 name 字段,看看是不是相等,如果相等的话就匹配成功

对于支持设备树的 Linux 版本号,一般设备驱动为了兼容性都支持设备树无设备树两种匹配式。也就是第一种匹配方式一般都会存在,第三种和第四种只要存在一种就可以,一般用的最多的还是第四种,也就是直接比较驱动和设备的 name 字段。

 platform 驱动

platform_driver 结 构 体 表 示 platform 驱动,

1 struct platform_driver {
2     int (*probe)(struct platform_device *);
3     int (*remove)(struct platform_device *);
4     void (*shutdown)(struct platform_device *);
5     int (*suspend)(struct platform_device *, pm_message_t state);
6     int (*resume)(struct platform_device *);
7     struct device_driver driver;
8     const struct platform_device_id *id_table;
9     bool prevent_deferred_probe;
10 };

第 7 行,driver 成员,为 device_driver 结构体变量,Linux 内核里面大量使用到了面向对象
的思维,device_driver 相当于基类,提供了最基础的驱动框架。plaform_driver 继承了这个基类,
然后在此基础上又添加了一些特有的成员变量。

1 struct device_driver {
2 const char *name;
3 struct bus_type *bus;
4 
5 struct module *owner;
6 const char *mod_name; /* used for built-in modules */
7 
8 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
9 
10 const struct of_device_id *of_match_table;
11 const struct acpi_device_id *acpi_match_table;
12
13 int (*probe) (struct device *dev);
14 int (*remove) (struct device *dev);
15 void (*shutdown) (struct device *dev);
16 int (*suspend) (struct device *dev, pm_message_t state);
17 int (*resume) (struct device *dev);
18 const struct attribute_group **groups;
19
20 const struct dev_pm_ops *pm;
21
22 struct driver_private *p;
23 };

第 10 行,of_match_table 就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹配项都为 of_device_id 结构体类型

1 struct of_device_id {
2 char name[32];
3 char type[32];
4 char compatible[128];
5 const void *data;
6 };

编写 platform 驱动的时候,1.首先定义一个 platform_driver 结构体变量,2.然后实现结构体中的各个成员变量,重点是实现匹配方法以及 probe 函数。3.需要在驱动入口函数里面调用
platform_driver_register 函数向 Linux 内核注册一个 platform 驱动。

int platform_driver_register (struct platform_driver *driver)

void platform_driver_unregister(struct platform_driver *drv)

示例:

1 struct xxx_dev{
2     struct cdev cdev;
3 /* 设备结构体其他具体内容 */
4 };
5 
6 struct xxx_dev xxxdev; /* 定义个设备结构体变量 */
7 
8 static int xxx_open(struct inode *inode, struct file *filp)
9 { 
10     /* 函数具体内容 */
11     return 0;
12 }
13
14 static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
15 {
16     /* 函数具体内容 */
17     return 0;
18 }
19
20 /*
21 * 字符设备驱动操作集
22 */
23 static struct file_operations xxx_fops = {
24     .owner = THIS_MODULE,
25     .open = xxx_open,
26     .write = xxx_write,
27 };
28
29 /*
30 * platform 驱动的 probe 函数
31 * 驱动与设备匹配成功以后此函数就会执行
32 */
33 static int xxx_probe(struct platform_device *dev)
34 { 
35     ......
36     cdev_init(&xxxdev.cdev, &xxx_fops); /* 注册字符设备驱动 */
37     /* 函数具体内容 */
38     return 0;
39 }
40
41 static int xxx_remove(struct platform_device *dev)
42 {
43     ......
44     cdev_del(&xxxdev.cdev);/* 删除 cdev */
45     /* 函数具体内容 */
46     return 0;
47 }
48
49 /* 匹配列表 */
50 static const struct of_device_id xxx_of_match[] = {
51     { .compatible = "xxx-gpio" },
52     { /* Sentinel */ }
53 };
54
55 /* 
56 * platform 平台驱动结构体
57 */
58 static struct platform_driver xxx_driver = {
59     .driver = {
60     .name = "xxx",
61     .of_match_table = xxx_of_match,
62 },
63     .probe = xxx_probe,
64     .remove = xxx_remove,
65 };
66 
67 /* 驱动模块加载 */
68 static int __init xxxdriver_init(void)
69 {
70     return platform_driver_register(&xxx_driver);
71 }
72
73 /* 驱动模块卸载 */
74 static void __exit xxxdriver_exit(void)
75 {
76     platform_driver_unregister(&xxx_driver);
77 }
78
79 module_init(xxxdriver_init);
80 module_exit(xxxdriver_exit);
81 MODULE_LICENSE("GPL");

总体来说,platform 驱动还是传统的字符设备驱动、块设备驱动或网络设备驱动,只是套上了一张“platform”的皮,目的是为了使用总线、驱动和设备这个驱动模型来实现驱动的分离与分层。

platform 设备

如果内核支持设备树的话就不要再使用 platform_device 来描述设备了,因为改用设备树去描述了。当然了,你如果一定要用 platform_device 来描述设备信息的话也是可以的。

22 struct platform_device {
23 const char *name; 
24 int id; 
25 bool id_auto;
26 struct device dev;
27 u32 num_resources; 
28 struct resource *resource;
29
30 const struct platform_device_id *id_entry;
31 char *driver_override; /* Driver name to force a match */
32
33 /* MFD cell pointer */
34 struct mfd_cell *mfd_cell;
35
36 /* arch specific additions */
37 struct pdev_archdata archdata;
38 };

第 23 行,name 表示设备名字,要和所使用的 platform 驱动的 name 字段相同,否则的话设备就无法匹配到对应的驱动。比如对应的 platform 驱动的 name 字段为“xxx-gpio”,那么此 name字段也要设置为“xxx-gpio”。
第 27 行,num_resources 表示资源数量,一般为第 28 行 resource 资源的大小。
第 28 行,resource 表示资源,也就是设备信息,比如外设寄存器等。Linux 内核使用 resource结构体表示资源,resource 结构体内容如下:

18 struct resource {
19 resource_size_t start;
20 resource_size_t end;
21 const char *name;
22 unsigned long flags;
23 struct resource *parent, *sibling, *child;
24 };
//start 和 end 分别表示资源的起始和终止信息,
//对于内存类的资源,就表示内存起始和终止地址,name 表示资源名字,flags 表示资源类型

资源类型

29 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
30 
31 #define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
32 #define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */
33 #define IORESOURCE_MEM 0x00000200
34 #define IORESOURCE_REG 0x00000300 /* Register offsets */
35 #define IORESOURCE_IRQ 0x00000400
36 #define IORESOURCE_DMA 0x00000800
37 #define IORESOURCE_BUS 0x00001000
......
104 /* PCI control bits. Shares IORESOURCE_BITS with above PCI ROM. */
105 #define IORESOURCE_PCI_FIXED (1<<4) /* Do not move resource */

在不支持设备树的Linux 版本中,用户需要编写platform_device变量来描述设备信息,然后使用 platform_device_register 函数将设备信息注册到 Linux 内核中

int platform_device_register(struct platform_device *pdev)
void platform_device_unregister(struct platform_device *pdev)

示例

1 /* 寄存器地址定义*/
2 #define PERIPH1_REGISTER_BASE (0X20000000) /* 外设 1 寄存器首地址 */ 
3 #define PERIPH2_REGISTER_BASE (0X020E0068) /* 外设 2 寄存器首地址 */
4 #define REGISTER_LENGTH 4
5 
6 /* 资源 */
7 static struct resource xxx_resources[] = {
8     [0] = {
9     .start = PERIPH1_REGISTER_BASE,
10     .end = (PERIPH1_REGISTER_BASE + REGISTER_LENGTH - 1),
11     .flags = IORESOURCE_MEM,
12     }, 
13     [1] = {
14     .start = PERIPH2_REGISTER_BASE,
15     .end = (PERIPH2_REGISTER_BASE + REGISTER_LENGTH - 1),
16     .flags = IORESOURCE_MEM,
17     },
18 };
19
20 /* platform 设备结构体 */
21 static struct platform_device xxxdevice = {
22     .name = "xxx-gpio",
23     .id = -1,
24     .num_resources = ARRAY_SIZE(xxx_resources),
25     .resource = xxx_resources,
26 };
27 
28 /* 设备模块加载 */
29 static int __init xxxdevice_init(void)
30 {
31     return platform_device_register(&xxxdevice);
32 }
33
34 /* 设备模块注销 */
35 static void __exit xxx_resourcesdevice_exit(void)
36 {
37     platform_device_unregister(&xxxdevice);
38 }
39
40 module_init(xxxdevice_init);
41 module_exit(xxxdevice_exit);
42 MODULE_LICENSE("GPL");

根文件系统中/sys/bus/platform/目录下保存着当前板子 platform 总线下的设备和驱动,其中devices 子目录为 platform 设备,drivers 子目录为 plartofm 驱动。

device与driver 的匹配示例

示例:

88 static struct platform_device leddevice = {
89 .name = "imx6ul-led",
90 .id = -1,
91 .dev = {
92 .release = &led_release,
93 },
94 .num_resources = ARRAY_SIZE(led_resources),
95 .resource = led_resources,
};
与
229 static struct platform_driver led_driver = {
230 .driver = {
231 .name = "imx6ul-led", /* 驱动名字,用于和设备匹配 */
232 },
233 .probe = led_probe,
234 .remove = led_remove,
235 };

看/sys/bus/platform/devices/目录,看看我们的设备是否存在,leddevice(platform_device 类型)的name 字段为“imx6ul-led”;在/sys/bus/platform/drivers/目录下存在一个名字“imx6ul-led”的文件,否则说明我们的设备模块加载失败。

设备树的platform的匹配方式

在使用设备树的时候,设备的描述被放到了设备树中,因此 platform_device 就不需要我们去编写了,我们只需要实现 platform_driver 即可。

1、在设备树中创建设备节点

1 gpioled {
2 #address-cells = <1>;
3 #size-cells = <1>;
4 compatible = "atkalpha-gpioled";
5 pinctrl-names = "default";
6 pinctrl-0 = <&pinctrl_led>;
7 led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;
8 status = "okay";
9 };

2、编写 platform 驱动的时候要注意兼容属性

在使用设备树的时候 platform 驱动会通过 of_match_table 来保存兼容性值,也就是表明此驱动兼容哪些设备。

1 static const struct of_device_id leds_of_match[] = {
2     { .compatible = "atkalpha-gpioled" }, /* 兼容属性 */
3     { /* Sentinel */ }
4 };
5 
6 MODULE_DEVICE_TABLE(of, leds_of_match);
7 
8 static struct platform_driver leds_platform_driver = {
9     .driver = {
10     .name = "imx6ul-led",  //驱动在节点里面的名字
11     .of_match_table = leds_of_match,
12     },
13     .probe = leds_probe,
14     .remove = leds_remove,
15 };

3.编写 platform 驱动

33 #define LEDDEV_CNT 1 /* 设备号长度 */
34 #define LEDDEV_NAME "dtsplatled" /* 设备名字 */
35 #define LEDOFF 0
36 #define LEDON 1
37 
38 /* leddev 设备结构体 */
39 struct leddev_dev{
40 dev_t devid; /* 设备号 */
41 struct cdev cdev; /* cdev */
42 struct class *class; /* 类 */
43 struct device *device; /* 设备 */
44 int major; /* 主设备号 */ 
45 struct device_node *node; /* LED 设备节点 */
46 int led0; /* LED 灯 GPIO 标号 */
47 };
48 
49 struct leddev_dev leddev; /* led 设备 */
56 void led0_switch(u8 sta)
57 {
58 if (sta == LEDON )
59 gpio_set_value(leddev.led0, 0);
60 else if (sta == LEDOFF)
61 gpio_set_value(leddev.led0, 1);
62 }
71 static int led_open(struct inode *inode, struct file *filp)
72 {
73 filp->private_data = &leddev; /* 设置私有数据 */
74 return 0;
75 }
94 printk("kernel write failed!\r\n");
95 return -EFAULT;
96 }
97 
98 ledstat = databuf[0];
99 if (ledstat == LEDON) {
100 led0_switch(LEDON);
101 } else if (ledstat == LEDOFF) {
102 led0_switch(LEDOFF);
103 }
104 return 0;
105 }
106
107 /* 设备操作函数 */
108 static struct file_operations led_fops = {
109 .owner = THIS_MODULE,
110 .open = led_open,
111 .write = led_write,
112 };
120 static int led_probe(struct platform_device *dev)
121 { 
122 printk("led driver and device was matched!\r\n");
123 /* 1、设置设备号 */
124 if (leddev.major) {
125 leddev.devid = MKDEV(leddev.major, 0);
126 register_chrdev_region(leddev.devid, LEDDEV_CNT,
LEDDEV_NAME);
127 } else {
128 alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT,
LEDDEV_NAME);
129 leddev.major = MAJOR(leddev.devid);
130 }
131
132 /* 2、注册设备 */
133 cdev_init(&leddev.cdev, &led_fops);
134 cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
135
136 /* 3、创建类 */
137 leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
138 if (IS_ERR(leddev.class)) {
139 return PTR_ERR(leddev.class);
140 }
141
142 /* 4、创建设备 */
143 leddev.device = device_create(leddev.class, NULL, leddev.devid,
NULL, LEDDEV_NAME);
144 if (IS_ERR(leddev.device)) {
145 return PTR_ERR(leddev.device);
146 }
147
148 /* 5、初始化 IO */ 
149 leddev.node = of_find_node_by_path("/gpioled");
150 if (leddev.node == NULL){
151 printk("gpioled node nost find!\r\n");
152 return -EINVAL;
153 }
154 
155 leddev.led0 = of_get_named_gpio(leddev.node, "led-gpio", 0);
156 if (leddev.led0 < 0) {
157 printk("can't get led-gpio\r\n");
158 return -EINVAL;
159 }
160
161 gpio_request(leddev.led0, "led0");
162 gpio_direction_output(leddev.led0, 1); /*设置为输出,默认高电平 */
163 return 0;
164 }
171 static int led_remove(struct platform_device *dev)
172 {
173 gpio_set_value(leddev.led0, 1); /* 卸载驱动的时候关闭 LED */
174
175 cdev_del(&leddev.cdev); /* 删除 cdev */
176 unregister_chrdev_region(leddev.devid, LEDDEV_CNT);
177 device_destroy(leddev.class, leddev.devid);
178 class_destroy(leddev.class);
179 return 0;
180 }
183 static const struct of_device_id led_of_match[] = {
184 { .compatible = "atkalpha-gpioled" },
185 { /* Sentinel */ }
186 };
189 static struct platform_driver led_driver = {
190 .driver = {
191 .name = "imx6ul-led", /* 驱动名字,用于和设备匹配 */
192 .of_match_table = led_of_match, /* 设备树匹配表 */
193 },
194 .probe = led_probe,
195 .remove = led_remove,
196 };
203 static int __init leddriver_init(void)
204 {
205 return platform_driver_register(&led_driver);
206 }
213 static void __exit leddriver_exit(void)
214 {
215 platform_driver_unregister(&led_driver);
216 }
217
218 module_init(leddriver_init);

设置 led_driver (platform_driver 类型)的 name 字段为“imx6ul-led”,在/sys/bus/platform/drivers/目录下存在名为“imx6ul-led”这个文件;

 

同理,在/sys/bus/platform/devices/目录下也存在 led 的设备文件,也就是设备树中 gpioled
个节点。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值