I2C设备linux驱动 [英] I2C device linux driver

查看:23
本文介绍了I2C设备linux驱动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为i2c设备制作字符设备,具有打开、关闭、ioctl等功能?过去两周我一直在寻找有关它的信息,但找不到任何工作.我在 Essential Linux Device Drivers 中找到了一些信息,但它是为 2.6 内核编写的,我使用 3.4.79(我正在尝试为 cubian distr 上的 cubieboard2 编写这个驱动程序)所以这本书有许多不推荐使用的功能,我试图像那里那样写我的驱动程序,但它仍然不起作用(当我正在使用 cat 字符设备时,给我内核错误).谁能解释一下如何处理它,或者至少给我一个可行的例子.

How to make a character device for i2c device, with open, close, ioctl etc. functions? I was looking for information about it last two weeks and couldn't find anything working. I found some information in Essential Linux Device Drivers, but it was written for 2.6 kernel, and i use 3.4.79 (i'm trying to write this driver for cubieboard2 on cubian distr) so this book has many deprecated functions, i tried to write my driver like there, but it still don't work (give me kernel errors while i'm truing to cat character device). Can anyone explain me what to do with it, or at least give me a working example.

所以,这是我的代码:http://pastebin.com/T7PBTpym

我只是尝试让 i2c 总线正常工作,但是在我的示波器上,我在 i2c 线上看不到任何内容,而在编译时我没有发现任何错误.我也找不到如何将设备驱动程序附加到 i2c 总线号.因为我的 cubieboard2 上有 4 个 i2c 总线,例如如何将设备驱动程序连接到 i2c-1 总线?

i just try to get i2c bus works, but on my oscilloscope i don't see anything on i2c lines, while i get no errors when i compile it. Also i can't find how to attach device driver to i2c bus number. because i have 4 i2c buses on my cubieboard2, and how to attach device driver to i2c-1 bus, for example?

推荐答案

谢谢各位.几个小时前我找到了我想要的东西.所以我看到有三种类型的驱动程序.一个应该被插入到内核中,你需要重建你的内核才能使用它们.还有一些驱动程序可以通过 sysfs 使用设备属性(它们出现在 /sys/bus/i2c/driver/your_driver/ 中).而且,第三 - 我的类型 - 看起来像字符设备的驱动程序.实际上,您可以将它们组合起来.

Thank you, guys. I've found what I want a couple hours ago. So as I see there are three types of drivers. One is supposed to be inserted into the kernel, and you need to rebuild your kernel to use them. There are also drivers that you can use through the sysfs using device attributes (they appear in /sys/bus/i2c/driver/your_driver/). And, third - my type - drivers that look like character devices. Actually, you can combine them.

因此,如果您想使用最后一种类型的设备,则很难找到正确的示例,因为几乎所有示例都专门针对前两种类型的驱动程序.无论如何,如果要创建字符设备,则需要从file_operations 结构中描述函数.但是所有的函数都像i2c_transferi2c_smbus_read_byte.等(完整列表)需要struct i2c_adapterstruct i2c_client.还有两个问题,如何获得结构,以及如何将驱动程序连接到适当的 i2c 总线,例如 i2c-2?

So, if you want to use last type of devices it'll be a bit difficult to find correct examples, because almost all examples are devoted to the first two types of drivers. Anyway, if you want to create a character device, you need to describe functions from the file_operations structure. But all functions like i2c_transfer, i2c_smbus_read_byte. Etc. (full list) required eitherstruct i2c_adapter or struct i2c_client. And there two questions, how to get there structures, and how to attach driver to the appropriate i2c bus, such as i2c-2?

所以有一个函数没有在这里描述:i2c_get_adapter.作为参数,您需要传递 i2c 总线编号.它返回一个指向 i2c_adapter 结构的链接.您可以通过 i2c_new_dummy 函数将 i2c_adapter 和从地址作为参数传递给它来获得 i2c_client 结构.

So there is a function that is not described here: i2c_get_adapter. As a parameter, you need to pass the i2c bus number. It returns a link to a i2c_adapter structure. The i2c_client structure you can get with i2c_new_dummy function passing i2c_adapter and slave address to it as parameters.

之后可以使用i2c_transferi2c_smbus_read_byte等函数,最后可以描述file_operations结构函数,并发布驱动,代表您的 i2c 设备,无需使用 sysfs 和重建内核.

After that you can use functions such as i2c_transfer, i2c_smbus_read_byte etc. In the end you can describe file_operations structure functions, and release driver, representing your i2c device, without using sysfs and rebuilding your kernel.

结果代码如下:

u8 ret; 
struct i2c_client * my_client; 
struct i2c_adapter * my_adap = i2c_get_adapter(1); // 1 means i2c-1 bus
my_client = i2c_new_dummy (my_adap, 0x69); // 0x69 - slave address on i2c bus
i2c_smbus_write_byte(my_client, 0x0f); 
ret = i2c_smbus_read_byte(my_client);

您可以直接在 file_operations 结构的函数中使用此代码.

You can use this code directly in your functions for the file_operations structure.

希望这些信息对像我这样的初学者有用.

Hope this info will be useful for beginners like me.

这篇关于I2C设备linux驱动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆