platform设备驱动全透析

合集下载

Platformdevicedriver注册过程

Platformdevicedriver注册过程

Platformdevicedriver注册过程Platform是⼀种虚拟总线,Platform机制将设备本⾝的资源注册进内核,有内核统⼀管理,在驱动程序使⽤这些资源时使⽤统⼀的接⼝,这样提⾼了程序的可移植性。

Linux的⼤部分设备驱动都可以使⽤platform 机制,⽤platform device 表⽰设备,⽤platform driver 表⽰驱动。

Platform总线的定义如下:struct bus_type platform_bus_type = { .name = "platform",//总线名字,总线注册后新建⽬录sys/bus/platform .dev_groups = platform_dev_groups, .match = platform_match,//当有总线或者设备注册到platform总线时,内核⾃动调⽤match函数,判断设备和驱动的name是否⼀致。

.uevent = platform_uevent, .pm = &platform_dev_pm_ops,};Platform device定义如下:struct platform_device { const char *name;//device名字,应该与platform driver对应。

注册后,会在/sys/device/⽬录下创建⼀个以name命名的⽬录,并且创建软连接到/sys/bus/platform/device下。

int id; bool id_auto; struct device dev;//基本的device结构 u32 num_resources;//资源数 struct resource *resource;//资源 const struct platform_device_id *id_entry; char *driver_override; /* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell; /* arch specific additions */ struct pdev_archdata archdata;};Platform driver定义如下:struct platform_driver { int (*probe)(struct platform_device *);//platform driver注册时,如果总线上已有匹配的device,将调⽤probe函数。

linux中platform_device与platform_driver小结

linux中platform_device与platform_driver小结

linux 内核驱动--Platform Device和Platform_driver注册过程从Linux 2.6 起引入了一套新的驱动管理和注册机制:Platform_device 和Platform_driver 。

Linux 中大部分的设备驱动,都可以使用这套机制, 设备用Platform_device 表示,驱动用Platform_driver 进行注册。

Linux platform driver 机制和传统的device driver 机制( 通过driver_register 函数进行注册) 相比,一个十分明显的优势在于platform 机制将设备本身的资源注册进内核,由内核统一管理,在驱动程序中使用这些资源时通过platform device 提供的标准接口进行申请并使用。

这样提高了驱动和资源管理的独立性,并且拥有较好的可移植性和安全性( 这些标准接口是安全的) 。

Platform 机制的本身使用并不复杂,由两部分组成:platform_device 和platfrom_driver 。

通过Platform 机制开发发底层驱动的大致流程为: 定义platform_add_devices 注册platform_device, 定义platform_add_driver 注册platform_driver 。

1、platform_device注册过程:首先要确认的就是设备的资源信息,例如设备的地址,中断号等。

在 2.6 内核中platform 设备用结构体platform_device 来描述,该结构体定义在kernel/include/linux/platform_device.h 中,struct platform_device {const char * name;int id;struct device dev;u32 num_resources;struct resource * resource ;struct platform_device_id *id_entry;};该结构一个重要的元素是resource ,该元素存入了最为重要的设备资源信息,定义在kernel/include/linux/ioport.h 中,struct resource {resource_size_t start;resource_size_t end;const char *name;unsigned long flags;struct resource *parent, *sibling, *child;};下面举s3c6410 平台的i2c 驱动作为例子来说明:static struct platform_device *smdk6410_devices [] __initdata = {#ifdef CONFIG_SMDK6410_SD_CH0&s3c_device_hsmmc0,#endif#ifdef CONFIG_SMDK6410_SD_CH1&s3c_device_hsmmc1,#endif&s3c_device_i2c0 ,&s3c_device_i2c1,&s3c_device_fb,&s3c_device_usb,&s3c_device_usb_hsotg,&smdk6410_lcd_powerdev,&smdk6410_smsc911x,};把一个或几个设备资源放在一起,便于集中管理,其中IIC设备platform_device 如下:struct platform_device s3c_device_i2c0 = {.name = "s3c2410-i2c",#ifdef CONFIG_S3C_DEV_I2C1.id = 0,#else.id = -1,#endif.num_resources = ARRAY_SIZE(s3c_i2c_resource ),.resource = s3c_i2c_resource,};具体resource如下:static struct resource s3c_i2c_resource [] = {[0] = {.start = S3C_PA_IIC,.end = S3C_PA_IIC + SZ_4K - 1,.flags = IORESOURCE_MEM,},[1] = {.start = IRQ_IIC,.end = IRQ_IIC,.flags = IORESOURCE_IRQ,},};这里定义了两组resource ,它描述了一个I2C 设备的资源,第 1 组描述了这个I2C 设备所占用的总线地址范围,IORESOURCE_MEM 表示第 1 组描述的是内存类型的资源信息,第 2 组描述了这个I2C 设备的中断号,IORESOURCE_IRQ 表示第2 组描述的是中断资源信息。

LinuxALSA声卡驱动之八:ASoC架构中的Platform

LinuxALSA声卡驱动之八:ASoC架构中的Platform

LinuxALSA声卡驱动之⼋:ASoC架构中的Platform#define SND_SOC_DAIFMT_AC97 6 /* AC97 */#define SND_SOC_DAIFMT_PDM 7 /* Pulse density modulation */ bit 4-7 ⽤于设置接⼝时钟的开关特性:1. #def ine SND_SOC_DAIFMT_CONT (1 << 4) /* cont inuous clock */2. #def ine SND_SOC_DAIFMT_GAT ED (2 << 4) /* clock is gat ed */bit 8-11 ⽤于设置接⼝时钟的相位1. #def ine SND_SOC_DAIFMT_NB_NF (1 << 8) /* normal bit clock + f rame */2. #def ine SND_SOC_DAIFMT_NB_IF (2 << 8) /* normal BCLK + inv FRM */3. #def ine SND_SOC_DAIFMT_IB_NF (3 << 8) /* invert BCLK + nor FRM */4. #def ine SND_SOC_DAIFMT_IB_IF (4 << 8) /* invert BCLK + FRM */bit 12-15 ⽤于设置接⼝主从格式:1. #def ine SND_SOC_DAIFMT_CBM_CFM (1 << 12) /* codec clk & FRM mast er */2. #def ine SND_SOC_DAIFMT_CBS_CFM (2 << 12) /* codec clk slave & FRM mast er */3. #def ine SND_SOC_DAIFMT_CBM_CFS (3 << 12) /* codec clk mast er & f rame slave */4. #def ine SND_SOC_DAIFMT_CBS_CFS (4 << 12) /* codec clk & FRM slave */5. snd_soc_platform_driver中的ops字段该ops字段是⼀个snd_pcm_ops结构,实现该结构中的各个回调函数是socplat f orm驱动的主要⼯作,他们基本都涉及dma操作以及dma buf f er的管理等⼯作。

platform_device 和 platform_driver 流程简介

platform_device 和 platform_driver 流程简介

作者:admin来源:ChinaUnix博客首先介绍一下注册一个驱动的步骤:1、定义一个platform_driver结构2、初始化这个结构,指定其probe、remove等函数,并初始化其中的driver变量3、实现其probe、remove等函数看platform_driver结构,定义于include/linux/platform_device.h文件中: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 (*suspend_late)(struct platform_device *, pm_message_t state);int (*resume_early)(struct platform_device *);int (*resume)(struct platform_device *);struct device_driver driver;};可见,它包含了设备操作的几个功能函数,同样重要的是,它还包含了一个device_driver 结构。

刚才提到了驱动程序中需要初始化这个变量。

下面看一下这个变量的定义,位于include/linux/device.h中:struct device_driver {const char * name;struct bus_type * bus;struct kobject kobj;struct klist klist_devices;struct klist_node knode_bus;struct module * owner;const char * mod_name; /* used for built-in modules */struct module_kobject * mkobj;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);};需要注意这两个变量:name和owner。

platform设备的添加流程

platform设备的添加流程

platform设备的添加流程
在嵌入式开发中,很多设备都需要使用 platform 设备来进行驱动程序的编写。

不同的设备所需的 platform 设备是不同的,因此需要针对具体设备来进行添加。

本文将介绍 platform 设备的添加流程。

1. 设备的准备工作
在进行 platform 设备的添加前,需要先进行以下设备的准备工作:
1.设备的硬件连接:将要添加的设备连接到开发板上,并将设备与板上
所需引脚连接好。

2.设备的驱动程序:根据设备的硬件信息,编写并编译设备的驱动程序。

3.设备的设备树文本文件:将设备的硬件信息以及驱动程序的信息编写
到设备树文本文件中,并将设备树文本文件编译成设备树二进制文件。

2. platform 设备的添加步骤
完成设备的准备工作后,可以进行 platform 设备的添加步骤。

2.1. 添加 platform device 节点
首先,需要在设备树中添加 platform 设备节点。

节点的添加形式如下:
``` platform { compatible =。

platform driver使用方法

platform driver使用方法

platform driver使用方法一、Platform Driver的基本概念Platform Driver是Linux内核中的一种设备驱动程序,用于管理特定硬件平台上的设备。

它通过与设备的Platform Device进行匹配,并提供设备的初始化、注册和卸载等功能。

Platform Driver通常由两部分组成:Platform Driver的结构体和Platform Driver的注册函数。

1. Platform Driver的结构体Platform Driver的结构体是一个包含了与设备驱动相关信息的数据结构,它通常包含了设备的名称、设备的ID、设备的资源信息等。

在编写Platform Driver时,需要定义一个Platform Driver的结构体,并在其中填写相关信息。

2. Platform Driver的注册函数Platform Driver的注册函数用于将Platform Driver的结构体与对应的硬件设备进行匹配,并注册到Linux内核中。

在注册函数中,需要填写Platform Driver的结构体,并调用相应的函数进行注册。

二、Platform Driver的使用步骤使用Platform Driver的步骤一般包括以下几个步骤:1. 定义Platform Driver的结构体需要定义一个Platform Driver的结构体,并填写相关信息。

在结构体中,需要包含设备的名称、设备的ID、设备的资源信息等。

2. 实现Platform Driver的初始化函数在定义了Platform Driver的结构体后,需要实现Platform Driver 的初始化函数。

在初始化函数中,可以进行设备的初始化操作,如初始化设备的寄存器、分配设备的内存等。

3. 实现Platform Driver的probe函数Platform Driver的probe函数用于设备的注册和初始化。

在probe函数中,需要填写设备的相关信息,并调用相应的函数进行设备的注册和初始化。

platform

platform

Platform Devices and Drivers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ See <linux/platform_device.h> for the driver model interface to the platform bus: platform_device, and platform_driver. This pseudo-bus is used to connect devices on busses with minimal infrastructure, like those used to integrate peripherals on many system-on-chip processors, or some "legacy" PC interconnects; as opposed to large formally specified ones like PCI or USB.Platform devices ~~~~~~~~~~~~~~~~ Platform devices are devices that typically appear as autonomous entities in the system. This includes legacy port-based devices and host bridges to peripheral buses, and most controllers integrated into system-on-chip platforms. What they usually have in common is direct addressing from a CPU bus. Rarely, a platform_device will be connected through a segment of some other kind of bus; but its registers will still be directly addressable.Platform devices are given a name, used in driver binding, and a list of resources such as addresses and IRQs.struct platform_device { const char *name;u32 id;struct device dev;u32 num_resources;struct resource *resource;};Platform drivers ~~~~~~~~~~~~~~~~ Platform drivers follow the standard driver model convention, where discovery/enumeration is handled outside the drivers, and drivers provide probe() and remove() methods. They support power management and shutdown notifications using the standard conventions.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 (*suspend_late)(struct platform_device *, pm_message_t state);int (*resume_early)(struct platform_device *);int (*resume)(struct platform_device *);struct device_driver driver;};Note that probe() should general verify that the specified device hardware actually exists; sometimes platform setup code can't be sure. The probing can use device resources, including clocks, and device platform_data.Platform drivers register themselves the normal way:int platform_driver_register(struct platform_driver *drv);Or, in common situations where the device is known not to be hot-pluggable, the probe() routine can live in an init section to reduce the driver's runtime memory footprint:int platform_driver_probe(struct platform_driver *drv, int (*probe)(structplatform_device *))Device Enumeration ~~~~~~~~~~~~~~~~~~ As a rule, platform specific (and often board-specific) setup code will register platform devices:int platform_device_register(struct platform_device *pdev);int platform_add_devices(struct platform_device **pdevs, int ndev);The general rule is to register only those devices that actually exist, but in some cases extra devices might be registered. For example, a kernel might be configured to work with an external network adapter that might not be populated on all boards, or likewise to work with an integrated controller that some boards might not hook up to any peripherals.In some cases, boot firmware will export tables describing the devices that are populated on a given board. Without such tables, often the only way for system setup code to set up the correct devices is to build a kernel for a specific target board. Such board-specific kernels are common with embedded and custom systems development.In many cases, the memory and IRQ resources associated with the platform device are not enough to let the device's driver work. Board setup code will often provide additional information using the device's platform_data field to hold additional information.Embedded systems frequently need one or more clocks for platform devices, which are normally kept off until they're actively needed (to save power). System setup also associates those clocks with the device, so that that calls to clk_get(&pdev->dev, clock_name) return them as needed.Legacy Drivers: Device Probing ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Some drivers are not fully converted to the driver model, because they take on a non-driver role: the driver registers its platform device, rather than leaving that for system infrastructure. Such drivers can't be hotplugged or coldplugged, since those mechanisms require device creation to be in a different system component than the driver.The only "good" reason for this is to handle older system designs which, like original IBM PCs, rely on error-prone "probe-the-hardware" models for hardware configuration. Newer systems have largely abandoned that model, in favor of bus-level support for dynamic configuration (PCI, USB), or device tables provided by the boot firmware (e.g. PNPACPI on x86). There are too many conflicting options about what might be where, and even educated guesses by an operating system will be wrong often enough to make trouble.This style of driver is discouraged. If you're updating such a driver, please try to move the device enumeration to a more appropriate location, outside the driver. This will usually be cleanup, since such drivers tend to already have "normal" modes, such as ones using device nodes that were created by PNP or by platform device setup. None the less, there are some APIs to support such legacy drivers. Avoid using these calls except with such hotplug-deficient drivers.struct platform_device *platform_device_alloc( const char *name, int id);Y ou can use platform_device_alloc() to dynamically allocate a device, which you willthen initialize with resources and platform_device_register(). A better solution is usually:struct platform_device *platform_device_register_simple( const char *name, int id, struct resource *res, unsigned int nres);Y ou can use platform_device_register_simple() as a one-step call to allocate and register a device.Device Naming and Driver Binding ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The platform_device.dev.bus_id is the canonical name for the devices. It's built from two components:* platform_ ... which is also used to for driver matching.* platform_device.id ... the device instance number, or else "-1" to indicate there's only one.These are concatenated, so name/id "serial"/0 indicates bus_id "serial.0", and "serial/3" indicates bus_id "serial.3"; both would use the platform_driver named "serial". While "my_rtc"/-1 would be bus_id "my_rtc" (no instance id) and use the platform_driver called "my_rtc".Driver binding is performed automatically by the driver core, invoking driver probe() after finding a match between device and driver. If the probe() succeeds, the driver and device are bound as usual. There are three different ways to find such a match: - Whenever a device is registered, the drivers for that bus are checked for matches. Platform devices should be registered veryearly during system boot.- When a driver is registered using platform_driver_register(), all unbound devices on that bus are checked for matches. Driversusually register later during booting, or by module loading.- Registering a driver using platform_driver_probe() works just like using platform_driver_register(), except that the driver won'tbe probed later if another device registers. (Which is OK, since this interface is only for use with non-hotpluggable devices.)Early Platform Devices and Drivers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The early platform interfaces provide platform data to platform device drivers early on during the system boot. The code is built on top of the early_param() command line parsing and can be executed very early on.Example: "earlyprintk" class early serial console in 6 steps1. Registering early platform device data ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The architecture code registers platform device data using the function early_platform_add_devices(). In the case of early serial console this should be hardware configuration for the serial port. Devices registered at this point will later on be matched against early platform drivers.2. Parsing kernel command line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The architecture code calls parse_early_param() to parse the kernel command line. This will execute all matching early_param() callbacks. User specified early platform devices will be registered at this point. For the early serial console case the user can specify port on the kernel command line as "earlyprintk=serial.0" where "earlyprintk"is the class string, "serial" is the name of the platform driver and 0 is the platform device id. If the id is -1 then the dot and the id can be omitted.3. Installing early platform drivers belonging to a certain class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ The architecture code may optionally force registration of all early platform drivers belonging to a certain class using the function early_platform_driver_register_all(). User specified devices from step 2 have priority over these. This step is omitted by the serial driver example since the early serial driver code should be disabled unless the user has specified port on the kernel command line.4. Early platform driver registration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Compiled-in platform drivers making use of early_platform_init() are automatically registered during step 2 or 3. The serial driver example should use early_platform_init("earlyprintk", &platform_driver).5. Probing of early platform drivers belonging to a certain class ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ The architecture code calls early_platform_driver_probe() to match registered early platform devices associated with a certain class with registered early platform drivers. Matched devices will get probed(). This step can be executed at any point during the early boot. As soon as possible may be good for the serial port case.6. Inside the early platform driver probe() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The driver code needs to take special care during early boot, especially when it comes to memory allocation and interrupt registration. The code in the probe() function can use is_early_platform_device() to check if it is called at early platform device or at the regular platform device time. The early serial driver performs register_console() at this point.For further information, see <linux/platform_device.h>.。

基于嵌入式Linux的platform驱动设计与应用

基于嵌入式Linux的platform驱动设计与应用

D01:10.19392/ki.1671-7341.201825001基于嵌入式Linux的platform驱动设计与应用王晓君王星!李玉莹河北科技大学河北石家庄050000摘要:如今的驱动程序已经无法满足越来越多的高度集中的片上系统S0C的要求。

本文主要叙述了基于嵌入式Linux的platform驱动程序的设计方法和应用开发过程。

在platform总线架构下温湿度采集驱动设计的基础上,详细阐述了嵌入式Linux 下platform总线驱动设计中的platform总线、platform设备及platform驱动注册和卸载等,并验证设备驱动的正确性。

关键词:platform总线;platform设备;platform驱动中图分类号:TP316 文献标识码:APlatform的管理是在Linux基础之上完成。

platform在 Linux设备模型中管理外设资源和系统的外围设备。

引入plat­form之后更加便于管理。

platform驱动架构有利于这些设备驱 动的改写、维护与扩展,提高设备驱动开发[1]的效率。

因而,在 此基础上完成了驱动设计的温度和湿度采集的实验。

1platform总体设计Linux设备驱动负责设备的维护和实现,如:电源管理、热 插拔、对象生命周期、用户空间和内核空间[2]的交互等。

设备 驱动主要是用于驱动程序的编写和管理,但是实现相对复杂。

总线、设备、驱动就是设备驱动模型的三个主要部分,%32&其组 织关系总结为:(1)devices:描述了设备如何连接系统;(2 )drivers:系统中可用的驱动;(3 )buses:负责匹配设备与驱动,连接到每个总线上;(4 )classes:设备功能类。

的采集。

Platform总线架构需要一整套系统的的设计开发过程:首先需要对总线platformbus进行初始化,将platform_device 注册到内核,然后再把platform设备挂到platform bus(虚拟总 线)上。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档