最近看了韦东山驱动视频的i2c部分,总结出相关知识,分享给大家
在内核中有两种方式的i2c设备驱动的编写方法,一种legacy方式,一种是newstyle方式。韦东山视频和宋宝华书籍里讲解的都是legacy方式,但是在新版本内核中,legacy方式的i2c设备驱动已经编译不过去了,因为几个主要的内核函数都已经不存在了,即内核不再支持legacy方式的i2c设备驱动。下面讲解下newstyle方式的i2c设备驱动。
<一>定义并填充i2c_driver:
<二>模块初始化函数
分析i2c_add_driver():
分析driver_register():
i2c总线i2c_bus_type的定义如下:
< 三 > 注册 i2c 设备相关信息
对于newstyle方式,需要通过i2c_register_board_info()函数注册i2c_board_info,向内核提供i2c设备的相关信息。
在arch/arm/mach-s3c2440/mach-mini2440.c添加如下代码:
分析i2c_register_board_info():
搜索__i2c_board_list可知:
分析device_register():
ps:上述这一套分析适用于所有符合总线设备驱动模型的驱动,如usb总线,平台总线,pci总线,i2c总线等
<四>i2c_driver的probe()函数
正常的注册字符设备即可,即:
(1)分配设备号:alloc_chrdev_region()
(2)构造file_operations
(3)分配设置注册cdev:cdev_init(&cdev,&file_operations); cdev_add()
<五>file_operations的read()和write()函数
(1)read:
不是所有的I2C或者SMBus适配器实现了I2C规范上的所有功能,因此当访问I2C适配器时,
并不能完全假定适配器提供了你所需的功能。需要有一种检测适配器是否提供
了所需功能的方法。
对于不断更新的I2C适配器功能常量列表,参考<linux/i2c.h>
I2C_FUNC_I2C 无格式i2c-level命令
I2C_FUNC_10BIT_ADDR 处理10-bit地址的扩展
I2C_FUNC_SMBUS_READ_BYTE 处理SMBus read_byte命令
I2C_FUNC_SMBUS_WRITE_BYTE 处理SMBus write_byte命令
I2C_FUNC_SMBUS_READ_BYTE_DATA 处理SMBus read_byte_data命令
I2C_FUNC_SMBUS_WRITE_BYTE_DATA处理SMBus write_byte_data命令
I2C_FUNC_SMBUS_READ_WORD_DATA处理SMBus read_word_data命令
I2C_FUNC_SMBUS_WRITE_WORD_DATA处理SMBus write_word_data命令
...
分析i2c_smbus_read_byte_data(I2C_SMBUS_BYTE_DATA):
(2)write:
分析i2c_smbus_write_byte_data(I2C_SMBUS_BYTE_DATA):
ps:附件中有legacy和new style的at24cxx的驱动代码和测试代码,适用于mini2440
在内核中有两种方式的i2c设备驱动的编写方法,一种legacy方式,一种是newstyle方式。韦东山视频和宋宝华书籍里讲解的都是legacy方式,但是在新版本内核中,legacy方式的i2c设备驱动已经编译不过去了,因为几个主要的内核函数都已经不存在了,即内核不再支持legacy方式的i2c设备驱动。下面讲解下newstyle方式的i2c设备驱动。
<一>定义并填充i2c_driver:
|
<二>模块初始化函数
|
分析i2c_add_driver():
|
分析driver_register():
|
i2c总线i2c_bus_type的定义如下:
|
< 三 > 注册 i2c 设备相关信息
对于newstyle方式,需要通过i2c_register_board_info()函数注册i2c_board_info,向内核提供i2c设备的相关信息。
在arch/arm/mach-s3c2440/mach-mini2440.c添加如下代码:
|
分析i2c_register_board_info():
|
搜索__i2c_board_list可知:
|
分析device_register():
|
ps:上述这一套分析适用于所有符合总线设备驱动模型的驱动,如usb总线,平台总线,pci总线,i2c总线等
<四>i2c_driver的probe()函数
正常的注册字符设备即可,即:
(1)分配设备号:alloc_chrdev_region()
(2)构造file_operations
(3)分配设置注册cdev:cdev_init(&cdev,&file_operations); cdev_add()
<五>file_operations的read()和write()函数
(1)read:
|
不是所有的I2C或者SMBus适配器实现了I2C规范上的所有功能,因此当访问I2C适配器时,
并不能完全假定适配器提供了你所需的功能。需要有一种检测适配器是否提供
了所需功能的方法。
对于不断更新的I2C适配器功能常量列表,参考<linux/i2c.h>
I2C_FUNC_I2C 无格式i2c-level命令
I2C_FUNC_10BIT_ADDR 处理10-bit地址的扩展
I2C_FUNC_SMBUS_READ_BYTE 处理SMBus read_byte命令
I2C_FUNC_SMBUS_WRITE_BYTE 处理SMBus write_byte命令
I2C_FUNC_SMBUS_READ_BYTE_DATA 处理SMBus read_byte_data命令
I2C_FUNC_SMBUS_WRITE_BYTE_DATA处理SMBus write_byte_data命令
I2C_FUNC_SMBUS_READ_WORD_DATA处理SMBus read_word_data命令
I2C_FUNC_SMBUS_WRITE_WORD_DATA处理SMBus write_word_data命令
...
分析i2c_smbus_read_byte_data(I2C_SMBUS_BYTE_DATA):
|
(2)write:
|
分析i2c_smbus_write_byte_data(I2C_SMBUS_BYTE_DATA):
|
ps:附件中有legacy和new style的at24cxx的驱动代码和测试代码,适用于mini2440