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;
- /* initialize reference counting for all cells */
- 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部分内容,但其中成员由本函数初始化:
- /*
- * This struct describes the MFD part ("cell").
- * After registration the copy of this structure will become the platform data
- * of the resulting platform_device
- */
- struct mfd_cell { //个人理解:注册mfd_cell后等效为platform_device
- const char *name;
- int id;
- /* refcounting for multiple drivers to use a single cell */
- 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);
- /* platform data passed to the sub devices drivers */
- void *platform_data;
- size_t pdata_size;
- /*
- * These resources can be specified relative to the parent device.
- * For accessing hardware you should use resources from the platform dev
- */
- int num_resources;
- const struct resource *resources;
- /* don't check for resource conflicts */
- bool ignore_resource_conflicts;
- /*
- * Disable runtime PM callbacks for this subdevice - see
- * pm_runtime_no_callbacks().
- */
- 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); //申请pdev内存并初始化name和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) { //重新分配pdev->dev. platform_data内存并将cell->platform_data赋给它。
- ret = platform_device_add_data(pdev,
- cell->platform_data, cell->pdata_size);
- if (ret)
- goto fail_res;
- }
- ret = mfd_platform_add_cell(pdev, cell); //重新分配pdev->mfd_cell内存并将cell赋给它。
- if (ret)
- goto fail_res;
- //初始化cell->num_resources 个数量的res将它赋给pdev->resource
- for (r = 0; r < cell->num_resources; r++) {
- res[r].name = cell->resources[r].name;
- res[r].flags = cell->resources[r].flags;
- /* Find out base to use */
- if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
- res[r].parent = mem_base;
- res[r].start = mem_base->start + //cell中的每个内存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中的irq都要加上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);//将多个res赋给pdev
- if (ret)
- goto fail_res;
- ret = platform_device_add(pdev); //添加一个platform_device到系统,这时在dev的驱动中会使用这些res数据。
- 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;
- }