Linux之I2C设备总结
我做的是TI的davinci系列的开发,一前I2C并没有深究具体的驱动实现,现在,做了一段时间,尤其是项目中需要外加一颗I2C的RTC芯片来做时钟的支持,所以在此详细总结一下:
注:Linux的版本为2.6.18
1 加载I2C设备的方法:
make menuconfig中-->Device Drivers-->I2C support-->
<*>I2C support
<*> I2C device interface
I2C Algorithms-->中选取 <*>I2C bit-banging interfaces
I2C Hardware Bus support -->中选取自己板上支持的I2C总线驱动
其他项目可以不选。
之后,编译内核,输出uImage。
b.板级加载I2C:
在板级的Linux启动代码中添加I2C,方法如下:
i.找到.init_machine对应的初始化函数(如:.init_machine = davinci_init,),
ii.添加I2C硬件管脚设置
iii.确定有platform设备的初始化函数,并找到对应的数组,(如:platform_add_devices(xxx_devices, ARRAY_SIZE(xxx_devices));)
iv.在xxx_devices中添加I2C设备,如下:
static struct platfor_device *xxx_devices[] __initdata = {
&serial_device,
&....,
&i2c_device,//Add here
};
v."i2c_device"的添加,如下:
static struct resource i2c_resources[] = {
{
.start = XXX_I2C_BASE,
.end = XXX_I2C_BASE + 0x40,
.flags = IORESOURCE_MEM,
},
{
.start = IRQ_I2C,
.flags = IORESOURCE_IRQ,
},
};
static struct xxx_i2c_platform_data xxx_i2c_data = {
.bus_freq = 20,
.bus_delay = 100,
};
static struct platform_device i2c_device = {
.name = "i2c_xxx",
.id = 1,
.dev = {
.platform_data = &xxx_i2c_data,
},
.num_resources = ARRAY_SIZE(i2c_resources),
.resource = i2c_resources,
};
到这里device端的就完成了,别忘了确保编译通过啊!
C.“driver”端的修改: