1. 程式人生 > >Linux Platform devices 平臺裝置驅動

Linux Platform devices 平臺裝置驅動

    裝置匯流排驅動模型:http://blog.csdn.net/lizuobin2/article/details/51570196

    本文主要參考:http://www.wowotech.net/device_model/platform_device.html

    platform平臺裝置驅動是基於裝置匯流排驅動模型的,它只不過是將 device 進一步封裝成為 platform_device,將 device_driver 進一步封裝成為 platform_device_driver,前面已經分析過裝置匯流排驅動模型,關於device 與 device_driver 的註冊過程以及它們在sysfs檔案系統中的層次關係就不在分析,本文重點分析platform平臺裝置驅動與裝置匯流排驅動模型相比較新增添的那些東西。


    在Linux裝置模型的抽象中,存在著一類稱作“Platform Device”的裝置,核心是這樣描述它們的(Documentation/driver-model/platform.txt):

    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裝置包括:基於埠的裝置(已不推薦使用,保留下來只為相容舊裝置,legacy);連線物理匯流排的橋裝置;整合在SOC平臺上面的控制器;連線在其它bus上的裝置(很少見)。等等。
    這些裝置有一個基本的特徵:可以通過CPU bus直接定址(例如在嵌入式系統常見的“暫存器”)。因此,由於這個共性,核心在裝置模型的基礎上(device和device_driver),對這些裝置進行了更進一步的封裝,抽象出paltform bus、platform device和platform driver,以便驅動開發人員可以方便的開發這類裝置的驅動。
    可以說,paltform裝置對Linux驅動工程師是非常重要的,因為我們編寫的大多數裝置驅動,都是為了驅動plaftom裝置。

platform_bus_type
    我們知道,在裝置匯流排驅動模型的中,BUS像一個月老一樣,通過它的match函式,將註冊到bus中的device與driver進行配對,那麼每一個不同的bus 都有自己的match函式,我們來看看platform_bus_type.

struct bus_type platform_bus_type = {
	.name		= "platform",
	.dev_attrs	= platform_dev_attrs,
	.match		= platform_match,
	.uevent		= platform_uevent,
	.pm		= &platform_dev_pm_ops,
};
static int platform_match(struct device *dev, struct device_driver *drv)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct platform_driver *pdrv = to_platform_driver(drv);

	/* match against the id table first */
	if (pdrv->id_table)
		return platform_match_id(pdrv->id_table, pdev) != NULL;

	/* fall-back to driver name match */
	return (strcmp(pdev->name, drv->name) == 0);
}

    如果platform_device_driver中定義了id_table,則呼叫 platform_match_id 進行匹配

    舉個例子:

static struct platform_device_id s3c24xx_driver_ids[] = {
	{
		.name		= "s3c2410-i2c",
		.driver_data	= TYPE_S3C2410,
	}, {
		.name		= "s3c2440-i2c",
		.driver_data	= TYPE_S3C2440,
	}, { },
};
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,
};
static const struct platform_device_id *platform_match_id(struct platform_device_id *id, struct platform_device *pdev)
{
	while (id->name[0]) {
		if (strcmp(pdev->name, id->name) == 0) {
			pdev->id_entry = id;
			return id;
		}
		id++;
	}
	return NULL;
}
    顯然,platform_match_id 的作用就是遍歷整個 Id_table 陣列,尋找是否有與 platform_device->name 同名的,如果有,則返回這個 Platform_device_id ,使用Id_table 打破了原本裝置匯流排驅動模型,一個 device 只能用與一個 device_driver 配對的侷限性。現在一個platform_device_driver 可以與多個platform_device配對。

    如果沒有,則只是根據 platform_device_driver->name 與 platform_device->name 進行比較,這也就是老師為啥在寫平臺裝置驅動程式的時候經常說,“將驅動註冊到核心中去,如果有同名裝置,則呼叫driver->probe函式....”。

pletform_device 中的 id 的作用:

    if (pdev->id != -1)      /* 如果不是-1 對name編號 */  
        dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);  
    else                             /* -1時直接是名字 */
        dev_set_name(&pdev->dev, pdev->name); 


從device封裝而來的platform_device
struct platform_device {
	const char	* name;
	int		id;
	struct device	dev;
	u32		num_resources;
	struct resource	* resource;

	struct platform_device_id	*id_entry;

	/* arch specific additions */
	struct pdev_archdata	archdata;
};	
    name,裝置的名稱,該名稱在設備註冊時,會拷貝到dev.init_name中。
    dev,真正的裝置,通過 container_of ,就能找到整個platform_device ,訪問其它成員,如後面要提到的 resource 
    num_resources、resource,該裝置的資源描述,由struct resource(include/linux/ioport.h)結構抽象。 
    在Linux中,系統資源包括I/O、Memory、Register、IRQ、DMA、Bus等多種型別。這些資源大多具有獨佔性,不允許多個裝置同時使用,因此Linux核心提供了一些API,用於分配、管理這些資源。 
    當某個裝置需要使用某些資源時,只需利用struct resource組織這些資源(如名稱、型別、起始、結束地址等),並儲存在該裝置的resource指標中即可。然後在裝置probe時,裝置需求會呼叫資源管理介面,分配、使用這些資源。而核心的資源管理邏輯,可以判斷這些資源是否已被使用、是否可被使用等等。
struct resource {
	resource_size_t start;
	resource_size_t end;
	const char *name;
	unsigned long flags;
	struct resource *parent, *sibling, *child;
};
static struct resource led_resource[] = {	//jz2440的引數,驅動未測試 
	[0] = {
		.start = 0x56000010,
		.end   = 0x56000010 + 8 - 1,
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = 5,
		.end   = 5,
		.flags = IORESOURCE_IRQ,
	},
};
static struct platform_device led_dev = {
	.name = "myled",	//裝置名字 與 驅動相匹配
	.id	  = -1,
	.num_resources = ARRAY_SIZE(led_resource),
	.resource = led_resource,
	
	.dev = {
		.release = led_release,
		//.devt = MKDEV(252, 1),
	},
};
從 device_driver 封裝而來的platform_device_dirver
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;
	struct platform_device_id *id_table;
};
int platform_driver_register(struct platform_driver *drv)
{
	drv->driver.bus = &platform_bus_type;
	if (drv->probe)
		drv->driver.probe = platform_drv_probe;
	if (drv->remove)
		drv->driver.remove = platform_drv_remove;
	if (drv->shutdown)
		drv->driver.shutdown = platform_drv_shutdown;

	return driver_register(&drv->driver);
}
    struct platform_driver結構和struct device_driver非常類似,上邊的platform_drv_probe、platform_drv_remove、platform_drv_shutdown,只不過稍作轉換呼叫platform_driver中的probe、remove、shutdown函式,舉個例子稍微看一下
static int platform_drv_probe(struct device *_dev)
{
	struct platform_driver *drv = to_platform_driver(_dev->driver);
	struct platform_device *dev = to_platform_device(_dev);

	return drv->probe(dev);
}
Platform Device提供的API
/* include/linux/platform_device.h */
extern int platform_device_register(struct platform_device *);
extern void platform_device_unregister(struct platform_device *);
 
extern void arch_setup_pdev_archdata(struct platform_device *);
extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
extern int platform_get_irq(struct platform_device *, unsigned int);
extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, const char *);
extern int platform_get_irq_byname(struct platform_device *, const char *);
extern int platform_add_devices(struct platform_device **, int);
 
extern struct platform_device *platform_device_register_full(const struct platform_device_info *pdevinfo);
 
static inline struct platform_device *platform_device_register_resndata(
                struct device *parent, const char *name, int id,
                const struct resource *res, unsigned int num,
                const void *data, size_t size)
 
static inline struct platform_device *platform_device_register_simple(
                const char *name, int id,
                const struct resource *res, unsigned int num)
 
static inline struct platform_device *platform_device_register_data(
                struct device *parent, const char *name, int id,
                const void *data, size_t size)
 
extern struct platform_device *platform_device_alloc(const char *name, int id);
extern int platform_device_add_resources(struct platform_device *pdev,
                                         const struct resource *res,
                                         unsigned int num);
extern int platform_device_add_data(struct platform_device *pdev,
                                    const void *data, size_t size);
extern int platform_device_add(struct platform_device *pdev);
extern void platform_device_del(struct platform_device *pdev);
extern void platform_device_put(struct platform_device *pdev);
    platform_device_register、platform_device_unregister,Platform裝置的註冊/登出介面,和底層的device_register等介面類似。
    arch_setup_pdev_archdata,設定platform_device變數中的archdata指標。
    platform_get_resource、platform_get_irq、platform_get_resource_byname、platform_get_irq_byname,通過這些介面,可以獲取platform_device變數中的resource資訊,以及直接獲取IRQ的number等等。
    platform_device_register_full、platform_device_register_resndata、platform_device_register_simple、platform_device_register_data,其它形式的設備註冊。呼叫者只需要提供一些必要的資訊,如name、ID、resource等,Platform模組就會自動分配一個struct platform_device變數,填充內容後,註冊到核心中。
    platform_device_alloc,以name和id為引數,動態分配一個struct platform_device變數。
    platform_device_add_resources,向platform device中增加資源描述。
    platform_device_add_data,向platform device中新增自定義的資料(儲存在pdev->dev.platform_data指標中)。
    platform_device_add、platform_device_del、platform_device_put,其它操作介面。

Platform Driver提供的API
    platform_driver_registe、platform_driver_unregister,platform driver的註冊、登出介面。
    platform_driver_probe,主動執行probe動作。
    platform_set_drvdata、platform_get_drvdata,設定或者獲取driver儲存在device變數中的私有資料。

懶人API
 extern struct platform_device *platform_create_bundle(
       struct platform_driver *driver, int (*probe)(struct platform_device *),
       struct resource *res, unsigned int n_res,
       const void *data, size_t size);
    只要提供一個platform_driver(要把driver的probe介面顯式的傳入),並告知該裝置佔用的資源資訊,platform模組就會幫忙分配資源,並執行probe操作。對於那些不需要熱拔插的裝置來說,這種方式是最省事的了。  

簡單一例:

    開發板:Mini2440

    核心版本:2.6.32.2

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/sched.h> 
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <linux/input.h>
#include <linux/platform_device.h>
// 裝置資源
static struct resource led_resource[] = {	//jz2440的引數,驅動未測試 
	[0] = {
		.start = 0x56000010,
		.end   = 0x56000010 + 8 - 1,
		.flags = IORESOURCE_MEM,
	},
	[1] = {
		.start = 5,
		.end   = 5,
		.flags = IORESOURCE_IRQ,
	},
};

static void led_release(struct device *dev){

}

// 建立一個裝置
static struct platform_device led_dev = {
	.name = "myled",	//裝置名字 與 驅動相匹配
	.id	  = -1,
	.num_resources = ARRAY_SIZE(led_resource),
	.resource = led_resource,
	
	.dev = {
		.release = led_release,
		//.devt = MKDEV(252, 1),
	},
};

static int led_dev_init(void){

	//向bus註冊led_dev match drv連結串列進行配對
	platform_device_register(&led_dev);
	return 0;
}

static void led_dev_exit(void){
	platform_device_unregister(&led_dev);
}

module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
	

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/sched.h> 
#include <linux/irq.h>
#include <asm/uaccess.h>

#include <linux/platform_device.h>
#include <linux/io.h>

static int major;

static struct class *cls;
static struct device *dev;

static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file){

	*gpio_con &= ~(0x03 << (pin*2));
	*gpio_con |=  (0x01 << (pin*2));
	return 0;
}

static ssize_t led_write(struct file *file, const char __user *buf,
	size_t count, loff_t *ppos){

	int val;
	copy_from_user(&val, buf, count);

	if(val == 1){
		
		*gpio_dat &= ~(1<<pin);
	}else{
	
		*gpio_dat &=  (1<<pin);
	}

	return 0;
}

static struct file_operations led_fops = {

	.owner = THIS_MODULE,
	.open  = led_open,
	.write = led_write,
};

static int led_probe(struct platform_device *pdev){

	struct resource *res;
	// 最後一個引數 0 表示第1個該型別的資源
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	gpio_con = ioremap(res->start, res->end - res->start + 1);
	gpio_dat = gpio_con + 1;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	pin = res->start;

	printk("led_probe, found led\n");

	// 註冊裝置驅動 建立裝置節點
	major = register_chrdev(0, "myled", &led_fops);
	// 建立類
	cls = class_create(THIS_MODULE, "myled");
	// 建立裝置節點
	dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "led");

	return 0;
}

static int led_remove(struct platform_device *pdev){

	printk("led_remove, remove led\n");	
	// 刪除裝置節點
	device_unregister(dev);
	// 銷燬類
	class_destroy(cls);
	// 取消註冊裝置驅動
	unregister_chrdev(major, "myled");
	// 取消記憶體對映
	iounmap(gpio_con);

	return 0;
}

struct platform_driver led_drv = {

	.probe 	= led_probe,	//匹配到dev之後呼叫probe
	.remove = led_remove,
	.driver = {
		.name = "myled",
	},
};

static int led_drv_init(void){

	platform_driver_register(&led_drv);
	return 0;
}

static void led_drv_exit(void){
	
	platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");