一、概述

mfd是Multifunction device的简称,即多功能设备,是许多有共性的设备的集合,mfd由核心层(core)以及其下的“子设备”组成。从下文将会看到,mfd只是将设备注册到platform总线——因此,其子设备属于platform设备。它并没有对涉及到的设备或驱动做实质性改变。但是,因为某些设备的共性,所以可以在mfd中提供共同的函数给其下子设备进行调用。

本文提到的hisi_fmc驱动就是如此:

下面就分析mfd设备注册过程,并结合1个实例讲解。

内核配置(make menuconfig)信息如下:

在里面可以选中自己需要的器件;

.config文件中配置CONFIG_MFD_CORE=y

二、mfd设备添加

mfd核心代码位于drivers/mfd/mfd-core.c文件中。对外提供添加设备和删除设备的接口:mfd_add_devices、mfd_remove_devices。设备添加函数原型如下:

int mfd_add_devices(struct device *parent, int id,
const struct mfd_cell *cells, int n_devs,
struct resource *mem_base,
int irq_base, struct irq_domain *domain)
  • id:即设备ID号。它指示着设备的个数。一般可以设置为-1。即表示系统有且仅有一个这样的设备。如果有多个foo设备,则需要使用id来区别。

在/sys/bus/platform/devices目录下会产生foo.0,foo.1等设备。详情可以看platform设备添加函数过程。

  • cells:即mfd_cell结构体数组,n_devs为其数组大小,即设备数量。

  • mem_base:资源resource结构体。如果没有,可置为NULL。

描述mfd设备单元称为“cell”,mfd_cell定义如下:

/*
* 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 {
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;
/*
* Device Tree compatible string
* See: Documentation/devicetree/usage-model.txt Chapter 2.2 for details
*/
const char *of_compatible; /*
* 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; /* A list of regulator supplies that should be mapped to the MFD
* device rather than the child device when requested
*/
const char * const *parent_supplies;
int num_parent_supplies;
};

部分常见的成员介绍如下:

  • name:设备平台。
  • platform_data:平台私有数据指针,数据大小使用pdata_size表示。
  • resources:资源结构体,资源数量使用num_resources表示。
  • ignore_resource_conflicts:为true表示不检查资源冲突。
  • of_compatible:设备树匹配compatible的字符串(具体参考Documentation/devicetree/usage-model.txt Chapter 2.2)这个根据我的理解,是用于platform device的,只是写在了mfd设备上;

至此,mfd设备的添加就完成了,最终调用驱动的probe函数。从这个过程中知道,mfd实质上就是封装一个接口,将一些可以归纳到一起的platform设备注册到platform总线上。它就是一个收纳盒子。里面的设备该是怎样处理就怎样处理。

三、mfd实例

下面介绍hisi_fmc驱动的实例:


static int hisi_fmc_probe(struct platform_device *pdev)
{
struct hisi_fmc *fmc;
struct resource *res;
struct device *dev = &pdev->dev;
int ret; pr_err("hisi_fmc_probe successfully!\n");
fmc = devm_kzalloc(dev, sizeof(*fmc), GFP_KERNEL);
if (!fmc)
return -ENOMEM; res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "control");
fmc->regbase = devm_ioremap_resource(dev, res);
if (IS_ERR(fmc->regbase))
return PTR_ERR(fmc->regbase); res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "memory");
fmc->iobase = devm_ioremap_resource(dev, res);
if (IS_ERR(fmc->iobase))
return PTR_ERR(fmc->iobase); fmc->clk = devm_clk_get(dev, NULL);
if (IS_ERR(fmc->clk))
return PTR_ERR(fmc->clk); if (of_property_read_u32(dev->of_node, "max-dma-size", &fmc->dma_len)) {
dev_err(dev, "Please set the suitable max-dma-size value !!!\n");
return -ENOMEM;
} ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
if (ret) {
dev_warn(dev, "Unable to set dma mask\n");
return ret;
} fmc->buffer = dmam_alloc_coherent(dev, fmc->dma_len,
&fmc->dma_buffer, GFP_KERNEL);
if (IS_ERR(fmc->buffer))
return PTR_ERR(fmc->buffer); mutex_init(&fmc->lock); platform_set_drvdata(pdev, fmc); ret = mfd_add_devices(dev, 0, hisi_fmc_devs,
ARRAY_SIZE(hisi_fmc_devs), NULL, 0, NULL);
if (ret) {
dev_err(dev, "add mfd devices failed: %d\n", ret);
return ret;
} return 0;
}
  1. 读取fmc的reg_base、io_base;
  2. 获取最大的max-dma-size
  3. 添加mfd设备
ret = mfd_add_devices(dev, 0, hisi_fmc_devs,
ARRAY_SIZE(hisi_fmc_devs), NULL, 0, NULL);

多功能设备mfd驱动的更多相关文章

  1. i2c总线,设备,驱动之间的关系

    ------ 总线上先添加好所有具体驱动,i2c.c遍历i2c_boardinfo链表,依次建立i2c_client, 并对每一个i2c_client与所有这个线上的驱动匹配,匹配上,就调用这个驱动的 ...

  2. linux设备驱动归纳总结(九):1.platform总线的设备和驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-111745.html linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxx ...

  3. linux设备驱动归纳总结(八):1.总线、设备和驱动【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-109733.html linux设备驱动归纳总结(八):1.总线.设备和驱动 xxxxxxxxxxxx ...

  4. Linux下实现流水灯等功能的LED驱动代码及测试实例

    驱动代码: #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> ...

  5. platform总线,设备,驱动的注册

    linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  6. SPI设备的驱动

    主要包括两个SPI设备步骤:register_chrdevspi_register_driver关键点1:spi_board_info可以去已经运行的板子下面找例子:/sys/bus/spi/driv ...

  7. I2C总线、设备、驱动

    I2C总线.设备.驱动 框架 I2C驱动框架可分为3个部分,分别是:I2C核心层.I2C总线驱动层(适配器层)以及I2C设备驱动层: I2C核心层 提供了统一的I2C操作函数,主要有两套函数smbus ...

  8. 【Linux开发】linux设备驱动归纳总结(九):1.platform总线的设备和驱动

    linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  9. 【Linux开发】linux设备驱动归纳总结(八):1.总线、设备和驱动

    linux设备驱动归纳总结(八):1.总线.设备和驱动 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

随机推荐

  1. String的replaceAll()用法详解

    使用replaceAll实现字符串替换,即把字符串某些字符全部替换成别的 // 将str中的所有数字替换为"数字"二字 String str = "abc123bcd45 ...

  2. mysql 从一个表中查数据,插入另一个表

    其实很简单,只是为了忘记,做个记录,用的时候方便. 不管是在网站开发还是在应用程序开发中,我们经常会碰到需要将MySQL或MS SQLServer某个表的数据批量导入到另一个表的情况,甚至有时还需要指 ...

  3. Tiny4412中断介绍

    通过几天裸板驱动开发,今天对ARM的中断做一些简单总结,前面我们已经了解了ARM的7种异常模式,中断是异常模式的一种,在ARM中异常事件发生将会触发中断,但是,所有的中断都不能直接访问cpu,而是都统 ...

  4. FMDB的简单实用

    一.FMDB 的框架引入点击此处去GitHub下载 二.FMDB 的优缺点 优点:使用起来更加面向对象,省去了很多麻烦.冗余的C语言代码:对比苹果自带的Core Data框架,更加轻量级和灵活:提供了 ...

  5. Linux共享库、静态库、动态库详解

    1. 介绍 使用GNU的工具我们如何在Linux下创建自己的程序函数库?一个“程序函数库”简单的说就是一个文件包含了一些编译好的代码和数据,这些编译好的代码和数据可以在事后供其他的程序使用.程序函数库 ...

  6. Python题库

    Date:2018-05-08 1.Given: an array containing hashes of names Return: a string formatted as a list of ...

  7. SpringMVC接口测试异常:Can not deserialize instance of int out of START_OBJECT token

    之前使用springmvc搭建了restful风格的接口服务,在使用mockmvc进行集成测试的时候出现了异常:Can not deserialize instance of int out of S ...

  8. spring boot sso

    https://hellokoding.com/hello-single-sign-on-sso-with-json-web-token-jwt-spring-boot/ https://github ...

  9. spring创建bean及数据注入

    通过spring的IoC可以实现由配置文件来创建类的对象,可以降低类鱼类之间的耦合, 通常我们都是在代码中控制对象的生成和属性注入,而使用IoC后,就可以将设计好的类交给IoC容器,让容器去控制对象的 ...

  10. 关于Kafka配额的讨论(2)

    继续前一篇的讨论.前文中提到了两大类配额管理:基于带宽的以及基于CPU线程使用时间的.本文着重探讨基于CPU线程时间的配额管理. 定义 这类配额管理被称为请求配额(request quota),管理起 ...