mfd: multifunction device drivers---多功能设备驱动开发;
A product or device that has multiple functions. An example of this might be a printer that also makes copies, faxes, and scans. Another example is a CD or DVD that might contain multiple applications on the same disk; this may be a Mac and PC version of the same software or media meant to be played on more than one platform. Also called multi function product (MFP), all-in-one.
源码主要是做了一些platform_device的注册和添加删除工作。
- int mfd_add_devices(struct device *parent, int id,
- struct mfd_cell *cells, int n_devs,
- struct resource *mem_base,
- int irq_base)
- {
- int i;
- int ret = 0;
- atomic_t *cnts;
-
-
- cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
- if (!cnts)
- return -ENOMEM;
-
- for (i = 0; i < n_devs; i++) {
- atomic_set(&cnts[i], 0);
- cells[i].usage_count = &cnts[i];
- ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base); 调用mfd_add_device()
- if (ret)
- break;
- }
-
- if (ret)
- mfd_remove_devices(parent);
-
- return ret;
- }
- EXPORT_SYMBOL(mfd_add_devices);
在这个函数中,参数cells是数组,个数为参数n_devs。用户调用此函数前初始化了cells部分内容,但其中成员由本函数初始化:
- struct mfd_cell { //个人理解:注册mfd_cell后等效为platform_device
- const char *name;
- int id;
-
-
- atomic_t *usage_count;
- int (*enable)(struct platform_device *dev);
- int (*disable)(struct platform_device *dev);
-
- int (*suspend)(struct platform_device *dev);
- int (*resume)(struct platform_device *dev);
-
-
- void *platform_data;
- size_t pdata_size;
-
-
- int num_resources;
- const struct resource *resources;
-
-
- bool ignore_resource_conflicts;
-
-
- bool pm_runtime_no_callbacks;
- };
再来看mfd_add_device()
- static int mfd_add_device(struct device *parent, int id,
- const struct mfd_cell *cell,
- struct resource *mem_base,
- int irq_base)
- {
- struct resource *res;
- struct platform_device *pdev;
- int ret = -ENOMEM;
- int r;
-
- pdev = platform_device_alloc(cell->name, id + cell->id);
- if (!pdev)
- goto fail_alloc;
-
- res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
- if (!res)
- goto fail_device;
-
- pdev->dev.parent = parent;
- pdev->dev.type = &mfd_dev_type;
-
- if (cell->pdata_size) {
- ret = platform_device_add_data(pdev,
- cell->platform_data, cell->pdata_size);
- if (ret)
- goto fail_res;
- }
-
- ret = mfd_platform_add_cell(pdev, cell);
- if (ret)
- goto fail_res;
-
- for (r = 0; r < cell->num_resources; r++) {
- res[r].name = cell->resources[r].name;
- res[r].flags = cell->resources[r].flags;
-
-
- if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
- res[r].parent = mem_base;
- res[r].start = mem_base->start +
- cell->resources[r].start;
- res[r].end = mem_base->start +
- cell->resources[r].end;
- } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
- res[r].start = irq_base +
- cell->resources[r].start;
- res[r].end = irq_base +
- cell->resources[r].end;
- } else {
- res[r].parent = cell->resources[r].parent;
- res[r].start = cell->resources[r].start;
- res[r].end = cell->resources[r].end;
- }
-
- if (!cell->ignore_resource_conflicts) {
- ret = acpi_check_resource_conflict(&res[r]);
- if (ret)
- goto fail_res;
- }
- }
-
- ret = platform_device_add_resources(pdev, res, cell->num_resources);
- if (ret)
- goto fail_res;
-
- ret = platform_device_add(pdev);
- if (ret)
- goto fail_res;
-
- if (cell->pm_runtime_no_callbacks)
- pm_runtime_no_callbacks(&pdev->dev);
-
- kfree(res);
-
- return 0;
-
- fail_res:
- kfree(res);
- fail_device:
- platform_device_put(pdev);
- fail_alloc:
- return ret;
- }
drivers/mfd/Mfd-core.c
原文:http://www.cnblogs.com/Ph-one/p/5785054.html