linux platform匹配机制,Linux驱动中的platform总线详解

platform总线是学习linux驱动必须要掌握的一个知识点。

一、概念

嵌入式系统中有很多的物理总线:I2c、SPI、USB、uart、PCIE、APB、AHB

linux从2.6起就加入了一套新的驱动管理和注册的机制platform平台总线,是一条虚拟的总线,并不是一个物理的总线。

相比 PCI、USB,它主要用于描述SOC上的片上资源。platform 所描述的资源有一个共同点:在CPU 的总线上直接取址。

平台设备会分到一个名称(用在驱动绑定中)以及一系列诸如地址和中断请求号(IRQ)之类的资源。

设备用platform_device表示,驱动用platform_driver进行注册。

与传统的bus/device/driver机制相比,platform由内核进行统一管理,在驱动中使用资源,提高了代码的安全性和可移植性。

二、platform

1. platform总线两个最重要的结构体

platform维护的所有的驱动都必须要用该结构体定义:

platform_driver

struct platform_driver {

int (*probe)(struct platform_device *); //

int (*remove)(struct platform_device *);

void (*shutdown)(struct platform_device *);

int (*suspend)(struct platform_device *, pm_message_t state);

int (*resume)(struct platform_device *);

struct device_driver driver;

const struct platform_device_id *id_table;

bool prevent_deferred_probe;

};

该结构体,用于注册驱动到platform总线,

成员含义

probe当驱动和硬件信息匹配成功之后,就会调用probe函数,驱动所有的资源的注册和初始化全部放在probe函数中

remove硬件信息被移除了,或者驱动被卸载了,全部要释放,释放资源的操作就放在该函数中

struct device_driver driver内核维护的所有的驱动必须包含该成员,通常driver-》name用于和设备进行匹配

const struct platform_device_id *id_table往往一个驱动可能能同时支持多个硬件,这些硬件的名字都放在该结构体数组中

我们编写驱动的时候往往需要填充以上几个成员

platform_device

platform总线用于描述设备硬件信息的结构体,包括该硬件的所有资源(io,memory、中断、DMA等等)。

struct platform_device {

const char *name;

int id;

bool id_auto;

struct device dev;

u32 num_resources;

struct resource *resource;

const struct platform_device_id *id_entry;

/* MFD cell pointer */

struct mfd_cell *mfd_cell;

/* arch specific additions */

struct pdev_archdata archdata;

};

成员含义

const char*name设备的名字,用于和驱动进行匹配的

struct devicedev内核中维护的所有的设备必须包含该成员,

u32num_resources资源个数

struct resource*resource描述资源

struct devicedev-》release()必须实现,

其中描述硬件信息的成员struct resource

0x139d0000

struct resource {

resource_size_t start; //表示资源的起始值,

resource_size_t end; //表示资源的最后一个字节的地址, 如果是中断,end和satrt相同

const char *name; // 可不写

unsigned long flags; //资源的类型

struct resource *parent, *sibling, *child;

};

flags的类型说明

#define IORESOURCE_MEM 0x00000200 //内存

#define IORESOURCE_IRQ 0x00000400 //中断

内核管理的所有的驱动,都必须包含一个叫struct device_driver成员, //男性描述的硬件,必须包含struct device结构体成员。 //女性

struct device_driver {

const char *name;

struct bus_type *bus;

struct module *owner;

const char *mod_name; /* used for built-in modules */

bool suppress_bind_attrs; /* disables bind/unbind via sysfs */

const struct of_device_id *of_match_table;

const struct acpi_device_id *acpi_match_table;

int (*probe) (struct device *dev);

int (*remove) (struct device *dev);

void (*shutdown) (struct device *dev);

int (*suspend) (struct device *dev, pm_message_t state);

int (*resume) (struct device *dev);

const struct attribute_group **groups;

const struct dev_pm_ops *pm;

struct driver_private *p;

};

其中:

const char *name;

用于和硬件进行匹配。

内核描述硬件,必须包含struct device结构体成员:

struct device {

struct device *parent;

struct device_private *p;

struct kobject kobj;

const char *init_name; /* initial name of the device */

const struct device_type *type;

struct mutex mutex; /* mutex to synchronize calls to

* its driver.

*/

struct bus_type *bus; /* type of bus device is on */

struct device_driver *driver; /* which driver has allocated this

device */

void *platform_data; /* Platform specific data, device

core doesn‘t touch it */

struct dev_pm_info power;

struct dev_pm_domain *pm_domain;

#ifdef CONFIG_PINCTRL

struct dev_pin_info *pins;

#endif

#ifdef CONFIG_NUMA

int numa_node; /* NUMA node this device is close to */

#endif

u64 *dma_mask; /* dma mask (if dma’able device) */

u64 coherent_dma_mask;/* Like dma_mask, but for

alloc_coherent mappings as

not all hardware supports

64 bit addresses for consistent

allocations such descriptors. */

struct device_dma_parameters *dma_parms;

struct list_head dma_pools; /* dma pools (if dma‘ble) */

struct dma_coherent_mem *dma_mem; /* internal for coherent mem

override */

#ifdef CONFIG_DMA_CMA

struct cma *cma_area; /* contiguous memory area for dma

allocations */

#endif

/* arch specific additions */

struct dev_archdata archdata;

struct device_node *of_node; /* associated device tree node */

struct acpi_dev_node acpi_node; /* associated ACPI device node */

dev_t devt; /* dev_t, creates the sysfs “dev” */

u32 id; /* device instance */

spinlock_t devres_lock;

struct list_head devres_head;

struct klist_node knode_class;

struct class *class;

const struct attribute_group **groups; /* optional groups */

void (*release)(struct device *dev);

struct iommu_group *iommu_group;

bool offline_disabled:1;

bool offline:1;

};

其中:

void (*release)(struct device *dev);

不能为空。

2. 如何注册

要用注册一个platform驱动的步骤

1)注册驱动platform_device_register

/**

* platform_device_register - add a platform-level device

* @pdev: platform device we’re adding

*/

int platform_device_register(struct platform_device *pdev)

{

device_initialize(&pdev-》dev);

arch_setup_pdev_archdata(pdev);

return platform_device_add(pdev);

}

2) 注册设备platform_driver_register

#define platform_driver_register(drv)

__platform_driver_register(drv, THIS_MODULE)

三、举例

1. 开发步骤

platform 总线下驱动的开发步骤是:

设备

需要实现的结构体是:platform_device 。

1)初始化 resource 结构变量

2)初始化 platform_device 结构变量

3)向系统注册设备:platform_device_register。

以上三步,必须在设备驱动加载前完成,即执行platform_driver_register()之前,原因是驱动注册时需要匹配内核中所有已注册的设备名。

platform_driver_register()中添加device到内核最终还是调用的device_add函数。

Platform_device_add和device_add最主要的区别是多了一步insert_resource(p, r),即将platform资源(resource)添加进内核,由内核统一管理。

驱动

驱动注册中,需要实现的结构体是:platform_driver 。

在驱动程序的初始化函数中,调用了platform_driver_register()注册 platform_driver 。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值