platform總線(一)
轉載:https://www.jianshu.com/p/4c3351ffcccb
在linux2.6設備模型中,關心總線,設備,驅動這三個實體,總線將設備和驅動綁定,在系統每註冊一個設備的時候,會尋找與之匹配的驅動。相反,在系統每註冊一個驅動的時候,尋找與之匹配的設備,匹配是由總線來完成的。
硬件資源用專門的模塊維護,驅動用專門的模塊維護,使用platform總線連接,設備用 platform_device 表示;驅動用platform_driver 進行註冊。
對於依附在USB、PCI、I2C、SPI等物理總線來 這些都不是問題。但是在嵌入式系統裏面,在Soc系統中集成的獨立外設控制器,掛接在Soc內存空間的外設等卻不依附在此類總線。基於這一背景,Linux發明了一種虛擬總線,稱爲platform。
platform總線相關代碼:driver\base\platform.c
相關結構體定義:include\linux\platform_device.h
一、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;
};
驅動程序的描述結構體:
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;
};
struct platform_driver pdrv={
.probe = hello_probe,
.remove = hello_remove,
.driver.name = "chuanpu", //設備名稱必須匹配
};
接口函數(driver\base\platform.c)
int platform_driver_register(struct platform_driver *); // 用來註冊我們的設備驅動
void platform_driver_unregister(struct platform_driver *); // 用來卸載我們的設備驅動
二、platform_device
struct platform_device {
const char *name; //設備的名字
int id; //ID 是用來區分如果設備名字相同的時候
bool id_auto;
struct device dev; //設備結構
u32 num_resources; //resource結構個數
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;
};
resource結構體存入了最爲重要的設備資源信息
struct resource {
resource_size_t start; //資源起始地址
resource_size_t end; //資源結束地址
const char *name;
unsigned long flags; //資源類型
struct resource *parent, *sibling, *child;
};
我們通常關心start、end 和flags 這3 個字段,分別標明資源的開始值、結束值和類型,flags 可以爲IORESOURCE_IO、IORESOURCE_MEM、IORESOURCE_IRQ、IORESOURCE_DMA 等
#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
#define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */
#define IORESOURCE_MEM 0x00000200
#define IORESOURCE_REG 0x00000300 /* Register offsets */
#define IORESOURCE_IRQ 0x00000400
#define IORESOURCE_DMA 0x00000800
#define IORESOURCE_BUS 0x00001000
例如 fs4412上的beep驅動
struct resource beep_res[] ={
[0]={
.start = 0x114000a0,
.end = 0x114000a0+0x3,
.flags = IORESOURCE_MEM,
},
[1]={
.start = 0x139d0000,
.end = 0x139d0000+0x13,
.flags = IORESOURCE_MEM,
},
};
struct platform_device pdev={
.name = "chuanpu", //設備名稱
.id = -1,
.dev.release = hello_release, //必須實現
.num_resources = ARRAY_SIZE(beep_res),
.resource = beep_res,
};
接口函數(driver\base\platform.c)
int platform_device_register(struct platform_device *); // 用來註冊我們的設備
void platform_device_unregister(struct platform_device *); // 用來卸載我們的設備
三、platform下的驅動架構
硬件資源從probe函數從platform_device傳遞,不應該用宏定義死硬件地址
int hello_probe(struct platform_device *pdev)
{
//當platform_device 和platform_driver 匹配成功之後,纔會調用probe,
1. 申請設備號
2. cdev
3. class
4. ioremap
5. beep初始化
}
int hello_remove(struct platform_device *pdev)
{
釋放資源
}
struct platform_driver pdrv={
.probe = hello_probe,
.remove = hello_remove,
.driver.name = "chuanpu",
};
static int hello_init(void)
{
return platform_driver_register(&pdrv);
}
static void hello_exit(void)
{
platform_driver_unregister(&pdrv);
}