一。平台设备 platform_device
结构
struct platform_device {
const char * name; /*平台设备的名字,与驱动的名字对应*/
int id; /*一般为-1*/
struct device dev; /*设备结构,platform_device 派生于device*/
u32 num_resources; /*设备使用的资源数*/
struct resource * resource; /*指向资源的数组,数量由num_resources指定*/
};
资源
struct resource {
resource_size_t start; /*资源的起始地址*/
resource_size_t end; /*资源结束地址*/
const char *name; /*资源名字*/
unsigned long flags; /*资源类型*/
struct resource *parent, *sibling, *child; /*用于构建资源的树形结构*/
};
用例:
static struct resource s3c_adc_resource[] = {
[0] = {
.start = SAMSUNG_PA_ADC,
.end = SAMSUNG_PA_ADC + SZ_8K - 1,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_ADC,
.end = IRQ_ADC,
.flags = IORESOURCE_IRQ,
},
};
struct platform_device s3c_device_adc = {
.name = "samsung-adc",
.id = -1,
.num_resources = ARRAY_SIZE(s3c_adc_resource),
.resource = s3c_adc_resource,
};
注册设备多个设备:
int platform_add_devices(struct platform_device **devs, int num)
如果是注册单个设备直接使用 platform_device_register
int platform_device_register(struct platform_device *pdev)
二。平台驱动 platform_drive
结构
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;
};
struct device_driver {
const char
*name; /*驱动的名字要与设备对应*/
struct bus_type *bus;
struct module
*owner; /*THIS_MODULE*/
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;
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;
};
用例:
static struct platform_driver serial8250_isa_driver = {
.probe = serial8250_probe,
.remove = __devexit_p(serial8250_remove),
.suspend = serial8250_suspend,
.resume = serial8250_resume,
.driver = {
.name = "serial8250",
.owner = THIS_MODULE,
},
};
注册设备
int platform_driver_register(struct platform_driver *);
注销设备
void platform_driver_unregister(struct platform_driver *);
平台驱动编写:
编写平台驱动结构
static struct platform_driver serial8250_isa_driver{
}
init中:
1.创建平台设备:
struct platform_device *platform_device_alloc(const char *name, int id)
输入参数:设备名字 -1
成功返回struct platform_device *
失败返回NULL
2.注册平台设备
int platform_device_add(struct platform_device *pdev)
3.注册平台驱动
int platform_driver_register(struct platform_driver *drv)
如果创建失败则删除设备
void platform_device_del(struct platform_device *pdev)
void platform_device_put(struct platform_device *pdev)
实际上这两个函数做的工作就是 platform_device_unregister
exit中:
1.注销驱动
void platform_driver_unregister(struct platform_driver *drv)
2.注销设备
void platform_device_unregister(struct platform_device *pdev)
在probe中
static int __devinit dw8250_probe(struct platform_device *pdev)
获取平台设备的资源:
struct resource *platform_get_resource(struct platform_device *dev,
unsigned int type, unsigned int num)
参数:
* @dev: platform device
* @type: resource type
* @num: resource index
常用资源类型:
IORESOURCE_MEM 内存资源
IORESOURCE_IRQ 中断资源
成功返回指向资源指针struct resource *