platform_device
内核版本:linux-4.9
在3.x版本内核中platform_device不再静态定义,而是通过device tree来动态生成,例如(arch/arm/mach-s3c24xx/mach-sc2416-dt.c):
[cpp] view plain copy
- static void __init s3c2416_dt_machine_init(void)
- {
- of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
- s3c_pm_init();
- }
[cpp] view plain copy
- int of_platform_populate(struct device_node *root,
- const struct of_device_id *matches,
- const struct of_dev_auxdata *lookup,
- struct device *parent)
- {
- struct device_node *child;
- int rc = 0;
- root = root ? of_node_get(root) : of_find_node_by_path("/");
- if (!root)
- return -EINVAL;
- for_each_child_of_node(root, child) {
- rc = of_platform_bus_create(child, matches, lookup, parent, true);
- if (rc)
- break;
- }
- of_node_put(root);
- return rc;
- }
如果传递进来的参数root为NULL,那么需要通过of_find_node_by_path函数找到device tree中的根节点。
得到根节点之后呢,就需要通过这个根节点来遍历device tree中的节点了。得到一个子节点之后,调用of_platform_bus_create函数:
[cpp] view plain copy
- static int of_platform_bus_create(struct device_node *bus,
- const struct of_device_id *matches,
- const struct of_dev_auxdata *lookup,
- struct device *parent, bool strict)
- {
- const struct of_dev_auxdata *auxdata;
- struct device_node *child;
- struct platform_device *dev;
- const char *bus_id = NULL;
- void *platform_data = NULL;
- int rc = 0;
- /* Make sure it has a compatible property */
- if (strict && (!of_get_property(bus, "compatible", NULL))) {
- pr_debug("%s() - skipping %s, no compatible prop\n",
- __func__, bus->full_name);
- return 0;
- }
- auxdata = of_dev_lookup(lookup, bus);
- if (auxdata) {
- bus_id = auxdata->name;
- platform_data = auxdata->platform_data;
- }
- if (of_device_is_compatible(bus, "arm,primecell")) {
- /*
- * Don't return an ERROR here to keep compatibility with older
- * device tree files.
- */
- of_amba_device_create(bus, bus_id, platform_data, parent);
- return 0;
- }
- dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
- if (!dev || !of_match_node(matches, bus))
- return 0;
- for_each_child_of_node(bus, child) {
- pr_debug(" create child: %s\n", child->full_name);
- rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
- if (rc) {
- of_node_put(child);
- break;
- }
- }
- of_node_set_flag(bus, OF_POPULATED_BUS);
- return rc;
- }
所幸在mach-sc2416-dt.c中传递进来的lookup参数为NULL,所以of_dev_lookup这部分也就不去看了。
如果"compatible"属性值有"arm,primecell",则会调用of_amba_device_create函数去创建amba_device,这个设备暂时也不知道是一个什么设备,那么这里还是先忽略。
继续,调用of_platform_device_create_pdata函数:
[cpp] view plain copy
- static struct platform_device *of_platform_device_create_pdata(
- struct device_node *np,
- const char *bus_id,
- void *platform_data,
- struct device *parent)
- {
- struct platform_device *dev;
- if (!of_device_is_available(np) ||
- of_node_test_and_set_flag(np, OF_POPULATED))
- return NULL;
- dev = of_device_alloc(np, bus_id, parent);
- if (!dev)
- goto err_clear_flag;
- of_dma_configure(&dev->dev);
- dev->dev.bus = &platform_bus_type;
- dev->dev.platform_data = platform_data;
- /* We do not fill the DMA ops for platform devices by default.
- * This is currently the responsibility of the platform code
- * to do such, possibly using a device notifier
- */
- if (of_device_add(dev) != 0) {
- platform_device_put(dev);
- goto err_clear_flag;
- }
- return dev;
- err_clear_flag:
- of_node_clear_flag(np, OF_POPULATED);
- return NULL;
- }
首先调用of_device_is_available函数,这个函数主要是用于检测"status"属性的,如果没有"status"属性,那还好说直接返回true。如果有"status"属性,而它的值又不是"okay"或"ok",那么不好意思,返回false,否则还是返回true。所以"status"属性就是用来检测是否可用(有点拗口,其实就是用来确认是否需要创建platform设备)。
"status"属性检测完毕了,则要调用of_device_alloc函数来为platform_device分配内存了。
[cpp] view plain copy
- struct platform_device *of_device_alloc(struct device_node *np,
- const char *bus_id,
- struct device *parent)
- {
- struct platform_device *dev;
- int rc, i, num_reg = 0, num_irq;
- struct resource *res, temp_res;
- dev = platform_device_alloc("", -1);
- if (!dev)
- return NULL;
- /* count the io and irq resources */
- while (of_address_to_resource(np, num_reg, &temp_res) == 0)
- num_reg++;
- num_irq = of_irq_count(np);
- /* Populate the resource table */
- if (num_irq || num_reg) {
- res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
- if (!res) {
- platform_device_put(dev);
- return NULL;
- }
- dev->num_resources = num_reg + num_irq;
- dev->resource = res;
- for (i = 0; i < num_reg; i++, res++) {
- rc = of_address_to_resource(np, i, res);
- WARN_ON(rc);
- }
- if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
- pr_debug("not all legacy IRQ resources mAPPed for %s\n",
- np->name);
- }
- dev->dev.of_node = of_node_get(np);
- dev->dev.parent = parent;
- if (bus_id)
- dev_set_name(&dev->dev, "%s", bus_id);
- else
- of_device_make_bus_id(&dev->dev);
- return dev;
- }
内存申请了之后,还会对platform_device做一些初始化,例如IO、中断资源等等。首先是调用of_address_to_resource和of_irq_count去计算io和中断资源的个数(有注释说明)。
[cpp] view plain copy
- int of_address_to_resource(struct device_node *dev, int index,
- struct resource *r)
- {
- const __be32 *addrp;
- u64 size;
- unsigned int flags;
- const char *name = NULL;
- addrp = of_get_address(dev, index, &size, &flags);
- if (addrp == NULL)
- return -EINVAL;
- /* Get optional "reg-names" property to add a name to a resource */
- of_property_read_string_index(dev, "reg-names", index, &name);
- return __of_address_to_resource(dev, addrp, size, flags, name, r);
- }
[cpp] view plain copy
- const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,
- unsigned int *flags)
- {
- const __be32 *prop;
- unsigned int psize;
- struct device_node *parent;
- struct of_bus *bus;
- int onesize, i, na, ns;
- /* Get parent & match bus type */
- parent = of_get_parent(dev);
- if (parent == NULL)
- return NULL;
- bus = of_match_bus(parent);
- bus->count_cells(dev, &na, &ns);
- of_node_put(parent);
- if (!OF_CHECK_ADDR_COUNT(na))
- return NULL;
- /* Get "reg" or "assigned-addresses" property */
- prop = of_get_property(dev, bus->addresses, &psize);
- if (prop == NULL)
- return NULL;
- psize /= 4;
- onesize = na + ns;
- for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
- if (i == index) {
- if (size)
- *size = of_read_number(prop + na, ns);
- if (flags)
- *flags = bus->get_flags(prop);
- return prop;
- }
- return NULL;
- }
[cpp] view plain copy
- static struct of_bus *of_match_bus(struct device_node *np)
- {
- int i;
- for (i = 0; i < ARRAY_SIZE(of_busses); i++)
- if (!of_busses[i].match || of_busses[i].match(np))
- return &of_busses[i];
- BUG();
- return NULL;
- }
[cpp] view plain copy
- static struct of_bus of_busses[] = {
- #ifdef CONFIG_OF_ADDRESS_PCI
- /* PCI */
- {
- .name = "pci",
- .addresses = "assigned-addresses",
- .match = of_bus_pci_match,
- .count_cells = of_bus_pci_count_cells,
- .map = of_bus_pci_map,
- .translate = of_bus_pci_translate,
- .get_flags = of_bus_pci_get_flags,
- },
- #endif /* CONFIG_OF_ADDRESS_PCI */
- /* ISA */
- {
- .name = "isa",
- .addresses = "reg",
- .match = of_bus_isa_match,
- .count_cells = of_bus_isa_count_cells,
- .map = of_bus_isa_map,
- .translate = of_bus_isa_translate,
- .get_flags = of_bus_isa_get_flags,
- },
- /* Default */
- {
- .name = "default",
- .addresses = "reg",
- .match = NULL,
- .count_cells = of_bus_default_count_cells,
- .map = of_bus_default_map,
- .translate = of_bus_default_translate,
- .get_flags = of_bus_default_get_flags,
- },
- };
回到of_get_address函数中,调用of_get_property函数去读取哪个属性呢,就是前面的addresses值的属性,即reg属性,所以reg属性就是用来定义io地址地址信息的。而io地址的长度是通过of_get_address中的of_read_number去读取完成的,最后返回这个io地址。
回到of_address_to_resource函数中,在得到这个io地址之后,调用__of_address_to_resource函数将io地址转换成struct resource资源信息。
然后是中断资源。
[cpp] view plain copy
- int of_irq_count(struct device_node *dev)
- {
- struct of_phandle_args irq;
- int nr = 0;
- while (of_irq_parse_one(dev, nr, &irq) == 0)
- nr++;
- return nr;
- }
- int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
- {
- struct device_node *p;
- const __be32 *intspec, *tmp, *addr;
- u32 intsize, intlen;
- int i, res = -EINVAL;
- pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index);
- /* OldWorld mac stuff is "special", handle out of line */
- if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
- return of_irq_parse_oldworld(device, index, out_irq);
- /* Get the reg property (if any) */
- addr = of_get_property(device, "reg", NULL);
- /* Try the new-style interrupts-extended first */
- res = of_parse_phandle_with_args(device, "interrupts-extended",
- "#interrupt-cells", index, out_irq);
- if (!res)
- return of_irq_parse_raw(addr, out_irq);
- /* Get the interrupts property */
- intspec = of_get_property(device, "interrupts", &intlen);
- if (intspec == NULL)
- return -EINVAL;
- intlen /= sizeof(*intspec);
- pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
- /* Look for the interrupt parent. */
- p = of_irq_find_parent(device);
- if (p == NULL)
- return -EINVAL;
- /* Get size of interrupt specifier */
- tmp = of_get_property(p, "#interrupt-cells", NULL);
- if (tmp == NULL)
- goto out;
- intsize = be32_to_cpu(*tmp);
- pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
- /* Check index */
- if ((index + 1) * intsize > intlen)
- goto out;
- /* Copy intspec into irq structure */
- intspec += index * intsize;
- out_irq->np = p;
- out_irq->args_count = intsize;
- for (i = 0; i < intsize; i++)
- out_irq->args[i] = be32_to_cpup(intspec++);
- /* Check if there are any interrupt-map translations to process */
- res = of_irq_parse_raw(addr, out_irq);
- out:
- of_node_put(p);
- return res;
- }
回到of_device_alloc函数,还是通过前面的of_address_to_resource函数将io地址资源赋值给平台设备,通过of_irq_to_resource_table函数将中断号转换成中断资源信息并赋值给平台设备。
[cpp] view plain copy
- int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
- int nr_irqs)
- {
- int i;
- for (i = 0; i < nr_irqs; i++, res++)
- if (!of_irq_to_resource(dev, i, res))
- break;
- return i;
- }
- int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
- {
- int irq = irq_of_parse_and_map(dev, index);
- /* Only dereference the resource if both the
- * resource and the irq are valid. */
- if (r && irq) {
- const char *name = NULL;
- memset(r, 0, sizeof(*r));
- /*
- * Get optional "interrupt-names" property to add a name
- * to the resource.
- */
- of_property_read_string_index(dev, "interrupt-names", index,
- &name);
- r->start = r->end = irq;
- r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
- r->name = name ? name : of_node_full_name(dev);
- }
- return irq;
- }
回到of_platform_device_create_pdata函数中,平台设备已经申请好了,然后对平台设备继续进行赋值操作,例如平台设备的总线赋值为平台总线,平台设备的私有数据赋值为platform_data,最后调用of_device_add函数将平台设备注册到内核中。
总结,涉及到的属性有: "compatible" 必须 "status" 可选属性 "reg" io资源 "interrupts" 中断资源
相关阅读
platform_driver_register(),platform_device_register
inux内核设备驱动分为字符设备,快设备和网络设备这三种,没错吧?在编写驱动的时候用register_chrdev(),register_blkdev(),register_net
linux平台设备驱动架构详解 Linux Platform Device an
https://www.cnblogs.com/lifan3a/articles/5045447.htmlhttps://www.cnblogs.com/lifan3a/articles/5045447.htmllinux平台设备