花了近一个星期的时间学习I2C的驱动,将心得描述如下。I2C是一个典型的较简单的子系统,比较适合学习,因为I2C协议要比PCI等等简单得多。
在mini2440上用I2C连了一块EEPROM的芯片。因此,在mini2440上面说I2C的驱动,主要是说怎么样通过I2C在这块EERPOM上面读写数据。有点像是这个EEPROM的驱动。
先分析几个相关的结构体。其实有时觉得C语言就是以结构体为中心的,基本上把重要的结构体看明白了,程序也就看懂了。
struct s3c24xx_i2c {
spinlock_t lock;
wait_queue_head_t wait;
unsigned int suspended:1;
struct i2c_msg *msg;
unsigned int msg_num;
unsigned int msg_idx;
unsigned int msg_ptr;
unsigned int tx_setup;
unsigned int irq;
enum s3c24xx_i2c_state state;
unsigned long clkrate;
void __iomem *regs;
struct clk *clk;
struct device *dev;
struct resource *ioarea;
struct i2c_adapter adap;
#ifdef CONFIG_CPU_FREQ
struct notifier_block freq_transition;
#endif
};
这个结构体其实是代表着i2c所连着的设备。其中state代表设备的状态,resource代表设备相关的IO资源,dev是所有设备都包括的。adap其实代表的就是这个设备连着的I2C总线,I2C总线本身就是一个dev。因为在struct i2c_adapter里面包含有struct device结构体。如下:
/*
* i2c_adapter is the structure used to identify a physical i2c bus along
* with the access algorithms necessary to access it.
*/
struct i2c_adapter {
struct module *owner;
unsigned int id;
unsigned int class; /* classes to allow probing for */
const struct i2c_algorithm *algo; /* the algorithm to access the bus */
void *algo_data;
/* data fields that are valid for all devices */
u8 level; /* nesting level for lockdep */
struct mutex bus_lock;
int timeout; /* in jiffies */
int retries;
struct device dev; /* the adapter device */
int nr;
char name[48];
struct completion dev_released;
};
#define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)