Chinaunix首页 | 论坛 | 博客
  • 博客访问: 852433
  • 博文数量: 189
  • 博客积分: 4310
  • 博客等级: 上校
  • 技术积分: 1925
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-27 08:56
文章分类

全部博文(189)

文章存档

2015年(1)

2013年(2)

2012年(1)

2011年(39)

2010年(98)

2009年(48)

分类: LINUX

2009-11-27 09:45:34

1. Linux 的I2C 驱动架构
Linux 中I2C 总线的驱动分为两个部分,总线驱动(BUS)和设备驱动(DEVICE)。其中总
线驱动的职责,是为系统中每个I2C 总线增加相应的读写方法。但是总线驱动本身并不会进行任
何的通讯,它只是存在在那里,等待设备驱动调用其函数。
设备驱动则是与挂在I2C 总线上的具体的设备通讯的驱动。通过I2C 总线驱动提供的函数,
设备驱动可以忽略不同总线控制器的差异,不考虑其实现细节地与硬件设备通讯。
1.1. 总线驱动
在系统开机时,首先装载的是I2C 总线驱动。一个总线驱动用于支持一条特定的I2C 总线
的读写。一个总线驱动通常需要两个模块,一个struct i2c_adapter 和一个struct i2c_algorith
m 来描述:
static struct i2c_adapter pb1550_board_adapter =
{
name: "pb1550 adapter",
id: I2C_HW_AU1550_PSC,
algo: NULL,
algo_data: &pb1550_i2c_info,
inc_use: pb1550_inc_use,
dec_use: pb1550_dec_use,
client_register: pb1550_reg,
client_unregister: pb1550_unreg,
client_count: 0,
};
这个样例挂接了一个叫做“pb1550 adapter”的驱动。但这个模块并未提供读写函数,具体
的读写方法由第二个模块,struct i2c_algorithm 提供。
static struct i2c_algorithm au1550_algo =
{
.name = "Au1550 algorithm",
.id = I2C_ALGO_AU1550,
.master_xfer = au1550_xfer,
.functionality = au1550_func,
};
i2c_adap->algo = &au1550_algo;
这个样例给上述总线驱动增加了读写“算法”。通常情况下每个I2C 总线驱动都定义一个自己
的读写算法,但鉴于有些总线使用相同的算法,因而可以共用同一套读写函数。本例中的驱动定
义了自己的读写算法模块,起名叫“Au1550 algorithm”。
全部填妥后,通过调用:
i2c_add_adapter(i2c_adap);
将这两个模块注册到操作系统里,总线驱动就算装上了。对于AMD au1550,这部分已经
由AMD 提供了。
1.2. 设备驱动
如前所述,总线驱动只是提供了对一条总线的读写机制,本身并不会去做通信。通信是由I
2C 设备驱动来做的,设备驱动透过I2C 总线同具体的设备进行通讯。一个设备驱动有两个模块
来描述,struct i2c_driver 和struct i2c_client。
Linux 的I2C 驱动架构
当系统开机、I2C 总线驱动装入完成后,就可以装入设备驱动了。首先装入如下结构:
static struct i2c_driver driver =
{
.name = "i2c TV tuner driver",
.id = I2C_DRIVERID_TUNER,
.flags = I2C_DF_NOTIFY,
.attach_adapter = tuner_probe,
.detach_client = tuner_detach,
.command = tuner_command,
};
i2c_add_driver(&driver);
这个i2c_driver 一旦装入完成,其中的attach_adapter 函数就会被调用。在其中可以遍历
系统中的每个i2c 总线驱动,探测想要访问的设备:
static int tuner_probe(struct i2c_adapter *adap)
{
return i2c_probe(adap, &addr_data, tuner_attach);
}
注意探测可能会找到多个设备,因而不仅一个I2C 总线可以挂多个不同类型的设备,一个
设备驱动也可以同时为挂在多个不同I2C 总线上的设备服务。
每当设备驱动探测到了一个它能支持的设备,它就创建一个struct i2c_client 来标识这个设
备:
new_client->addr = address;
new_client->adapter = adapter;
new_client->driver = &driver;
/* Tell the I2C layer a new client has arrived */
err = i2c_attach_client(new_client);
if (err)
goto error;
可见,一个i2c_client 代表着位于adapter 总线上,地址为address,使用driver 来驱动的
一个设备。它将总线驱动与设备驱动,以及设备地址绑定在了一起。一个i2c_client 就代表着一
个I2C 设备。
当得到 I2C 设备后,就可以直接对此设备进行读写:
/*
* The master routines are the ones normally used to transmit data to devices
* on a bus (or read from them). Apart from two basic transfer functions to
* transmit one message at a time, a more complex version can be used to
* transmit an arbitrary number of messages without interruption.
*/
extern int i2c_master_send(struct i2c_client *,const char* ,int);
extern int i2c_master_recv(struct i2c_client *,char* ,int);
与通常意义上的读写函数一样,这两个函数对i2c_client 指针指定的设备,读写int 个char。
返回值为读写的字节数。对于我们现有的SLIC 的驱动,只要将最后要往总线上进行读写的数据
引出传输到这两个函数中,移植工作就算完成了,我们将得到一个Linux 版的I2C 设备驱动。
阅读(955) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~