platform-devices-drivers

了解platform-device及platform-drivers用法及定義


更新記錄

item note
20160629 第一版

目錄


platform

  • 來源, linux-3.0.y/Documentation/driver-model/platform.txt
  • [platform.txt]
  • 註冊設備:

    • platform_device_register(): 註冊設備硬件,告訴kernel,當前有什麼設備
    • platform_driver_register(): 註冊設備的驅動程序
  • 程序如下:

    • 當個設備connect後
    • device和driver的兩條綫上都有匹配(由name來匹配)
    • 匹配上(即binding),並且開始使用driver的probe等函數進行進行硬件初始化工作

platform device

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[rectangle setX: 10 y: 10 width: 20 height: 20];

struct platform_device {
const char * name;
int id;
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;
};
  • name : 在做driver及device比對時用(即是參考此name)
    • platform_device.name : 用來作driver matching
  • id : 當name比較相同時,在probe還可以參考id
    • platform_device.id : the device instance number
  • resource: SOC的IOBASE/IRQ為固定的,由此設定

platform driver

1
2
3
4
5
6
7
8
9
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;
};
  • 提供callback function: probe, remove等

platform api

1
2
3
4
extern int platform_device_register(struct platform_device   *);
extern void platform_device_unregister(struct platform_device *);
extern int platform_driver_register(struct platform_driver *);
extern void platform_driver_unregister(struct platform_driver *);
  • Whenever a device a registered. the driver for that bus are checked for matches.
  • When ad driver is regiestered using platform_device_register.
    • all unbund device on that bus are checked for matches.
  • Register a driver using platform_driver_probe.
  • 當device上線時第一次動作為probe

platfrom_device_register flow

[platform device register flow]

platfrom_driver_register flow

[platform driver register flow]

hi3521 stmmac example

stmmac module

  • 當module init時註冊設備
[stmmac module]

stmmac init module

  • 先platform_device_register
  • 再platform_drvier_register
[init module]

stmmac platform device

  • phy device data
    • 設定相關phy0_id及interface
[phy device data]
  • ethernet device data
    • 設定相關gmac的IRQ及IOBASE
[ethernet device data]

stmmac platfrom driver

  • 設定相關probe及remove
[platfrom driver]

參考來源