// kernel/include/linux/platform_device.h
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;
char *driver_override; /* Driver name to force a match */
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
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 */
enum probe_type probe_type;
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;
};
從網路上找到關於兩者的關係:
A. platform_device_register()
|
|
---------------------------------------------------------------------------------------------------------
bus type (Platform, I2C, SPI) 在bus driver中會作match()和呼叫driver probe()的動作,若有match的話
---------------------------------------------------------------------------------------------------------
|
|
B. platform_driver_register()
這邊分成兩個部分解釋:
1. 當device透過platform_device_register()時,會透過.match提供的API在bus上找尋name相同的driver是否已經被掛載了,若成立,則會call bus或driver所提供的probe函式來做driver的初始化動作。若不成立,則只是將該device掛到bus上就return。
2. 相同的,當driver透過platform_driver_register()註冊時,也是利用.match的方式在bus上尋找name相同的device是否已經掛接在bus上,若是,則啟動probe的函式完成driver的初始化。若否,則將driver掛接在bus上就return。
綜合以上,可以得知device和driver的掛接沒有誰先誰後的副作用,唯一的先決條件就是bus driver已經被註冊好。故platform bus是第一個要備系統給註冊起來,接著的device和driver就是透過platform_device_register()和platform_driver_register()將三者牽上關係。
//----------------------------------------------------------------------------------------
platform_driver_register()與platform_device_register()
設備與驅動的兩種綁定方式:在設備註冊時進行綁定及在驅動註冊時進行綁定。以一個USB設備為例,有兩種情形:
(1)先插上USB設備並掛到總線(bus)中,然後在安裝USB驅動程序過程中從總線(bus)上遍歷各個設備,看驅動程序是否與其相匹配,如果匹配就將兩者邦定。這就是platform_driver_register()
(2)先安裝USB驅動程序,然後當有USB設備插入時,那麼就遍歷總線(bus)上的各個驅動,看兩者是否匹配,如果匹配就將其綁定。這就是 platform_device_register() 函數
//----------------------------------------------------------------------------------------
Refer to: ( 我認為 welkinchen 解釋的最好)
1. http://welkinchen.pixnet.net/blog/post/46943070-platform-device
2.