platform設備驅動框架詳解

platform設備驅動框架詳解


    前面我們講了設備驅動的分離,並且引出了總線(bus)、驅動(driver)和設備(device)模型,比如 I2C、SPI、USB 等總線。但是在 SOC 中有些外設是沒有總線這個概念的,但是又要使用總線、驅動和設備模型該怎麼辦呢?爲了解決此問題,Linux 提出了 platform 這個虛擬總線,相應的就有 platform_driver 和 platform_device。

一、platform總線

1. struct bus_type 結構體

    Linux 系統內核使用 bus_type 結構體表示總線,此結構體定義在文件***include/linux/device.h***, bus_type 結構體內容如下:

	struct bus_type {
	const char *name; /* 總線名字 */
	const char *dev_name;
	struct device *dev_root;
	struct device_attribute *dev_attrs;
	const struct attribute_group **bus_groups;/* 總線屬性 */
	const struct attribute_group **dev_groups;/* 設備屬性 */
	const struct attribute_group **drv_groups;/* 驅動屬性 */
	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	int (*remove)(struct device *dev);
	void (*shutdown)(struct device *dev);
	int (*online)(struct device *dev);
	int (*offline)(struct device *dev);
	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);
	const struct dev_pm_ops *pm;
	const struct iommu_ops *iommu_ops;
	struct subsys_private *p;
	struct lock_class_key lock_key;
};

    match 函數,此函數很重要,單詞 match 的意思就是“匹配、相配”,因此此函數就是完成設備和驅動之間匹配的,總線就是使用 match 函數來根據註冊的設備來查找對應的驅動,或者根據註冊的驅動來查找相應的設備,因此每一條總線都必須實現此函數。 match 函數有兩個參數:dev 和 drv,這兩個參數分別爲 device 和 device_driver 類型,也就是設備和驅動。

2. struct bus_type 結構體

    platform 總線是 bus_type 的一個具體實例,定義在文件 drivers/base/platform.c, platform 總線定義如下:

struct bus_type platform_bus_type = {
	.name = "platform",
	.dev_groups = platform_dev_groups,
	.match = platform_match,
	.uevent = platform_uevent,
	.pm = &platform_dev_pm_ops,
};

    platform_bus_type 就是 platform 平臺總線,其中 platform_match 就是匹配函數。

二、platform驅動

1. struct platform_driver 結構體

    platform_driver 結 構 體 表 示***platforminclude/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 (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};

    probe 函數,當驅動與設備匹配成功以後 probe 函數就會執行,非常重要的函數!!一般驅動的提供者會編寫,如果自己要編寫一個全新的驅動,那麼 probe 就需要自行實現。
    driver 成員,爲 device_driver 結構體變量,Linux 內核裏面大量使用到了面向對象的思維, device_driver 相當於基類,提供了最基礎的驅動框架。 plaform_driver 繼承了這個基類,然後在此基礎上又添加了一些特有的成員變量。
    在編寫 platform 驅動的時候,首先定義一個 platform_driver 結構體變量,然後實現結構體中的各個成員變量,重點是實現匹配方法以及 probe 函數。當驅動和設備匹配成功以後 probe函數就會執行,具體的驅動程序在 probe 函數裏面編寫,比如字符設備驅動等等。

2. 驅動註冊相關API

    當我們定義並初始化好 platform_driver 結構體變量以後,需要在驅動入口函數裏面調用***platform_driver_register*** 函數向 Linux 內核註冊一個 platform 驅動,platform_driver_register 函數原型如下所示:

int platform_driver_register (struct platform_driver *driver)

函數參數和返回值含義如下:
driver: 要註冊的 platform 驅動。
返回值: 負數,失敗;0,成功。
還 需 要 在 驅 動 卸 載 函 數 中 通 過 platform_driver_unregister 函 數 卸 載platform 驅 動 ,platform_driver_unregister 函數原型如下:

void platform_driver_unregister(struct platform_driver *drv)

函數參數和返回值含義如下:
drv: 要卸載的 platform 驅動。
返回值: 無。

3. platfrom驅動框架

platform 驅動框架如下所示:

/* 設備結構體 */
struct xxx_dev{
	struct cdev cdev;
	/* 設備結構體其他具體內容 */
};
struct xxx_dev xxxdev;

static int xxx_open(struct inode *inode, struct file *filp)
{
	/* 函數具體內容 */
	return 0;
}
static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
	/* 函數具體內容 */
	return 0;
}

/*
 * 字符設備驅動操作集
 */
static struct file_operations xxx_fops = {
	.owner = THIS_MODULE,
	.open = xxx_open,
	.write = xxx_write,
};

/*
 * platform 驅動的 probe 函數
 * 驅動與設備匹配成功以後此函數就會執行
 */
static int xxx_probe(struct platform_device *dev)
{
	...
	cdev_init(&xxxdev.cdev, &xxx_fops); /* 註冊字符設備驅動 */
	/* 函數具體內容 */
	return 0;
}
static int xxx_remove(struct platform_device *dev)
{
	...
	cdev_del(&xxxdev.cdev);/*刪除 cdev */
	/* 函數具體內容 */
	return 0;
}
/* 匹配列表 */
static const struct of_device_id xxx_of_match[] = {
	{ .compatible = "xxx-gpio" },
	{ /* Sentinel */ }
};
/*
 * platform 平臺驅動結構體
 */
static struct platform_driver xxx_driver = {
	.driver = {
		.name = "xxx",
		.of_match_table = xxx_of_match,
	}, 
	.probe = xxx_probe,
	.remove = xxx_remove,
};

/* 驅動模塊加載 */
static int __init xxxdriver_init(void)
{
	return platform_driver_register(&xxx_driver);
}

/* 驅動模塊卸載 */
static void __exit xxxdriver_exit(void)
{
	platform_driver_unregister(&xxx_driver);
}
module_init(xxxdriver_init);
module_exit(xxxdriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("eurphan");

三、platform設備

1. struct platform_device 結構體

    platform 驅動已經準備好了,我們還需要 platform 設備,否則的話單單一個驅動也做不了什麼。platform_device 這個結構體表示 platform 設備,這裏我們要注意,如果內核支持設備樹的話就不要再使用 platform_device 來描述設備了,因爲改用設備樹去描述了。當然了,你如果一定要用 platform_device 來描述設備信息的話也是可以的。platform_device 結構體定義在文件***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;
};

name 表示設備名字,要和所使用的 platform 驅動的 name 字段相同,否則的話設備就無法匹配到對應的驅動。比如對應的 platform 驅動的 name 字段爲“xxx-gpio”,那麼此 name字段也要設置爲“xxx-gpio”。
num_resources 表示資源數量,一般爲第 28 行 resource 資源的大小。
resource 表示資源,也就是設備信息,比如外設寄存器等。

2. struct resource 結構體

Linux 內核使用 resource結構體表示資源。resource 結構體內容如下:

struct resource {
	resource_size_t start;
	resource_size_t end;
	const char *name;
	unsigned long flags;
	struct resource *parent, *sibling, *child;
};

start 和 end 分別表示資源的起始和終止信息,對於內存類的資源,就表示內存起始和終止地 址 ,
name 表 示 資 源 名 字 ,
flags 表 示 資 源 類 型 , 可 選 的 資 源 類 型 都 定 義 在 了 文 件***include/linux/ioport.h*** 裏面,如下所示:

#define IORESOURCE_BITS 		0x000000ff /* Bus-specific bits */
#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
......
/* PCI control bits.Shares IORESOURCE_BITS with above PCI ROM.*/
#define IORESOURCE_PCI_FIXED	(1<<4)
/* Do not move resource */
3.platform設備註冊相關API

    在以前不支持設備樹的 Linux 版本中,用戶需要編寫 platform_device 變量來描述設備信息,
然後使用 platform_device_register 函數將設備信息註冊到 Linux 內核中,此函數原型如下所示:

int platform_device_register(struct platform_device *pdev)

函數參數和返回值含義如下:
pdev: 要註冊的 platform 設備。
返回值: 負數,失敗;0,成功。
    如果不再使用 platform 的話可以通過 platform_device_unregister 函數註銷掉相應的 platform
設備,platform_device_unregister 函數原型如下:

void platform_device_unregister(struct platform_device *pdev)

函數參數和返回值含義如下:
pdev: 要註銷的 platform 設備。
返回值: 無。

4.platform設備信息框架

platform 設備信息框架如下所示:

/* 寄存器地址定義*/ 
#define PERIPH1_REGISTER_BASE (0X20000000) /* 外設 1 寄存器首地址 */

#define PERIPH2_REGISTER_BASE (0X020E0068) /* 外設 2 寄存器首地址 */
#define REGISTER_LENGTH 	  4

/* 資源 */
static struct resource xxx_resources[] = {
	[0] = {
		.start = PERIPH1_REGISTER_BASE,
		.end = (PERIPH1_REGISTER_BASE + REGISTER_LENGTH - 1),
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = PERIPH2_REGISTER_BASE,
		.end = (PERIPH2_REGISTER_BASE + REGISTER_LENGTH - 1),
		.flags = IORESOURCE_MEM,
	},
};

/* platform 設備結構體 */
static struct platform_device xxxdevice = {
	.name = "xxx-gpio",
	.id = -1,
	.num_resources = ARRAY_SIZE(xxx_resources),
	.resource = xxx_resources,
 };

/* 設備模塊加載 */
static int __init xxxdevice_init(void)
{
	return platform_device_register(&xxxdevice);
}

/* 設備模塊註銷 */
static void __exit xxx_resourcesdevice_exit(void)
{
	platform_device_unregister(&xxxdevice);
}

    上述示例代碼主要是在不支持設備樹的 Linux 版本中使用的,當 Linux 內核支持了設備樹以後就不需要用戶手動去註冊 platform 設備了。因爲設備信息都放到了設備樹中去描述,Linux 內核啓動的時候會從設備樹中讀取設備信息,然後將其組織成 platform_device 形式,至於設備樹到platform_device 的具體過程就不去詳細的追究了,感興趣的可以去看一下,網上也有很多博客詳細的講解了整個過程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章