platform 机制不复杂,但是比较不好理解,它由platform device 和platform driver两部分组成。
I2C的platform device 初始化在mach-smdk2440.c文件中进行
/arch/arm/mach-s3c2440/mach-smdk2440.c
static void __init smdk2440_machine_init(void)
{
s3c24xx_fb_set_platdata(&smdk2440_fb_info);
s3c_i2c0_set_platdata(NULL); (1.0)
platform_add_devices(smdk2440_devices, ARRAY_SIZE(smdk2440_devices)); (2.0)
smdk_machine_init();
}
(1.0)构建platform device的设备信息 s3c_device_i2c0, 它将I2C 适配器默认的信息存到内核中。具体信息看下面代码:
arch/arm/plat-s3c/dev-i2c0.c
static struct resource s3c_i2c_resource[] = {(1.1)芯片I2C寄存器开始地址
[0] = {
.start = S3C_PA_IIC, (1.1)
.end = S3C_PA_IIC + SZ_4K - 1, (1.2)
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_IIC,
.end = IRQ_IIC,
.flags = IORESOURCE_IRQ,
},
};
struct platform_device s3c_device_i2c0 = {
.name = "s3c2410-i2c", (1.3)
#ifdef CONFIG_S3C_DEV_I2C1
.id = 0,
#else
.id = -1,
#endif
.num_resources = ARRAY_SIZE(s3c_i2c_resource),
.resource = s3c_i2c_resource,
};
static struct s3c2410_platform_i2c default_i2c_data0 __initdata = {
.flags= 0,
.slave_addr= 0x10, (1.4)
.frequency= 100*1000, (1.5)
.sda_delay= 100,
};
void __init s3c_i2c0_set_platdata(struct s3c2410_platform_i2c *pd)
{
struct s3c2410_platform_i2c *npd;
if (!pd)
pd = &default_i2c_data0;
npd = kmemdup(pd, sizeof(struct s3c2410_platform_i2c), GFP_KERNEL);
if (!npd)
printk(KERN_ERR "%s: no memory for platform data\n", __func__);
else if (!npd->cfg_gpio)
npd->cfg_gpio = s3c_i2c0_cfg_gpio;
s3c_device_i2c0.dev.platform_data = npd;
}
(1.2)芯片I2C寄存器结束地址
(1.3)默认的platform device 的名字s3c2410-i2c,用来与platform driver 匹配用。
(1.4)默认的从设备的设备地址
(1.5)默认的I2C时钟频率
(2.0)调用platform_add_devices 添加I2C平台设备platform device。
初始化到这里,platform device并没有和驱动绑定上,因为i2c platform driver 还没有被初始化。
说明:
1.分析的内核版本是linux2.6.32.2
2.开发板为友善之臂的mini2440, 用的是ARM9(S3C2440A)处理器
3.链接的IIC设备是EEPROM(AT24C02)
4.按照内核I2C子系统的注册顺序分析。