Linux3.5内核中,IIC。
i2c_add_driver
i2c_register_driver
a. at24cxx_driver放入i2c_bus_type的drv链表
并且从dev链表里取出能匹配的i2c_client并调用probe
driver_register b. 对于每一个适配器,调用__process_new_driver
对于每一个适配器,调用它的函数确定address_list里的设备是否存在
如果存在,再调用detect进一步确定、设置,然后i2c_new_device
/* Walk the adapters that are already present */
i2c_for_each_dev(driver, __process_new_driver);
__process_new_driver
i2c_do_add_adapter
/* Detect supported devices on that bus, and instantiate them */
i2c_detect(adap, driver);
for (i = ; address_list[i] != I2C_CLIENT_END; i += ) {
err = i2c_detect_address(temp_client, driver);
/* 判断这个设备是否存在:简单的发出S信号确定有ACK */
if (!i2c_default_probe(adapter, addr))
return ; memset(&info, , sizeof(struct i2c_board_info));
info.addr = addr; // 设置info.type
err = driver->detect(temp_client, &info); i2c_new_device
使用读写IIC器件的函数接口:
/**
*I2c_Master_发送-在主传输模式下发出单个I2c消息
*@客户端:从设备的句柄
*@buf:将写入从系统的数据
*@计数:要写入的字节数,必须小于64K,因为msg.len是u16
*返回负的errno,或者返回写入的字节数。
*/ /**
* i2c_master_send - issue a single I2C message in master transmit mode
* @client: Handle to slave device
* @buf: Data that will be written to the slave
* @count: How many bytes to write, must be less than 64k since msg.len is u16
*
* Returns negative errno, or else the number of bytes written.
*/
int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
{
int ret;
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg; msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.len = count;
msg.buf = (char *)buf; ret = i2c_transfer(adap, &msg, ); /*
* If everything went ok (i.e. 1 msg transmitted), return #bytes
* transmitted, else error code.
*/
return (ret == ) ? count : ret;
}
/**
*I2c_Master_Recv-在主接收模式下发出单个I2c消息
*@客户端:从设备的句柄
*@buf:从从属服务器读取数据的存储位置
*@计数:要读取的字节数,必须小于64K,因为msg.len是u16
*返回负的errno,否则返回读取的字节数。
*/ /**
* i2c_master_recv - issue a single I2C message in master receive mode
* @client: Handle to slave device
* @buf: Where to store data read from slave
* @count: How many bytes to read, must be less than 64k since msg.len is u16
*
* Returns negative errno, or else the number of bytes read.
*/
int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
{
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;
int ret; msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.flags |= I2C_M_RD;
msg.len = count;
msg.buf = buf; ret = i2c_transfer(adap, &msg, ); /*
* If everything went ok (i.e. 1 msg received), return #bytes received,
* else error code.
*/
return (ret == ) ? count : ret;
}
自己写的时候:
/**
*结构I2c_Board_信息-设备创建模板
*@类型:芯片类型,初始化i2c_client.name
*@标志:初始化i2c_client.flags
*@地址:存储在I2c_client.addr中
*@平台数据:存储在i2c_client.dev.platform_data
*@archdata:复制到i2c_client.dev.archdata
*@节点:指向OpenFirmware设备节点的指针
*@IRQ:存储在i2c_client.irq中
*
*I2c实际上不支持硬件探测,尽管控制器和
*设备可能能够使用I2c总线快速判断是否存在
*位于给定地址的设备。司机通常需要更多的信息
*如芯片类型、配置、相关IRQ等。
*i2c_board_info用于构建列出i2c设备的信息表。
*那是存在的。此信息用于增长驱动程序模型树。
*对于主板,这是使用i2c_register_board_info()静态完成的。
*总线号标识尚未提供的适配器。对于附加板,
*I2c_new_device()使用已知的适配器动态执行此操作。
*/
/**
* struct i2c_board_info - template for device creation
* @type: chip type, to initialize i2c_client.name
* @flags: to initialize i2c_client.flags
* @addr: stored in i2c_client.addr
* @platform_data: stored in i2c_client.dev.platform_data
* @archdata: copied into i2c_client.dev.archdata
* @of_node: pointer to OpenFirmware device node
* @irq: stored in i2c_client.irq
*
* I2C doesn't actually support hardware probing, although controllers and
* devices may be able to use I2C_SMBUS_QUICK to tell whether or not there's
* a device at a given address. Drivers commonly need more information than
* that, such as chip type, configuration, associated IRQ, and so on.
*
* i2c_board_info is used to build tables of information listing I2C devices
* that are present. This information is used to grow the driver model tree.
* For mainboards this is done statically using i2c_register_board_info();
* bus numbers identify adapters that aren't yet available. For add-on boards,
* i2c_new_device() does this dynamically with the adapter already known.
*/
struct i2c_board_info {
char type[I2C_NAME_SIZE];
unsigned short flags;
unsigned short addr;
void *platform_data;
struct dev_archdata *archdata;
struct device_node *of_node;
int irq;
};
写好 i2c_register_board_info smdk4x12_i2c_devs = { };
i2c_register_board_info(i2c_register_board_info); //注册硬件信息。
platform_add_devices(smdk4x12_devices) //smdk4x12_devices是放的所有platform_device结构体数组。