I2C設備驅動程序

I2C設備驅動程序

I2C設備驅動程序框架
1. 分配一個i2c_driver結構體
2. 設置
      attach_adapter // 它直接調用 i2c_probe(adap, 設備地址, 發現這個設備後要調用的函數);
      detach_client  // 卸載這個驅動後,如果之前發現能夠支持的設備,則調用它來清理
      
3. 註冊:i2c_add_driver

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

static unsigned short ignore[]      = { I2C_CLIENT_END };
static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 地址值是7位 */
                                        /* 改爲0x60的話, 由於不存在設備地址爲0x60的設備, 所以at24cxx_detect不被調用 */

static unsigned short force_addr[] = {ANY_I2C_BUS, 0x60, I2C_CLIENT_END};
static unsigned short * forces[] = {force_addr, NULL};

static struct i2c_client_address_data addr_data = {
.normal_i2c = normal_addr,  /* 要發出S信號和設備地址並得到ACK信號,才能確定存在這個設備 */
.probe = ignore,
.ignore = ignore,
//.forces     = forces, /* 強制認爲存在這個設備 */
};

static struct i2c_driver at24cxx_driver;
static int major;
static struct class *cls;
struct i2c_client *at24cxx_client;

static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset)
{
unsigned char address;
unsigned char data;
struct i2c_msg msg[2];
int ret;

/* address = buf[0] 
* data    = buf[1]
*/
if (size != 1)
return -EINVAL;

copy_from_user(&address, buf, 1);

/* 數據傳輸三要素: 源,目的,長度 */

/* 讀AT24CXX時,要先把要讀的存儲空間的地址發給它 */
msg[0].addr  = at24cxx_client->addr;  /* 目的 */
msg[0].buf   = &address;              /* 源 */
msg[0].len   = 1;                     /* 地址=1 byte */
msg[0].flags = 0;                     /* 表示寫 */

/* 然後啓動讀操作 */
msg[1].addr  = at24cxx_client->addr;  /* 源 */
msg[1].buf   = &data;                 /* 目的 */
msg[1].len   = 1;                     /* 數據=1 byte */
msg[1].flags = I2C_M_RD;                     /* 表示讀 */

ret = i2c_transfer(at24cxx_client->adapter, msg, 2);
if (ret == 2)
{
copy_to_user(buf, &data, 1);
return 1;
}
else
return -EIO;
}

static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
unsigned char val[2];
struct i2c_msg msg[1];
int ret;

/* address = buf[0] 
* data    = buf[1]
*/
if (size != 2)
return -EINVAL;

copy_from_user(val, buf, 2);

/* 數據傳輸三要素: 源,目的,長度 */
msg[0].addr  = at24cxx_client->addr;  /* 目的 */
msg[0].buf   = val;                   /* 源 */
msg[0].len   = 2;                     /* 地址+數據=2 byte */
msg[0].flags = 0;                     /* 表示寫 */

ret = i2c_transfer(at24cxx_client->adapter, msg, 1);
if (ret == 1)
return 2;
else
return -EIO;
}

static struct file_operations at24cxx_fops = {
.owner = THIS_MODULE,
.read  = at24cxx_read,
.write = at24cxx_write,
};

static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind)
{
printk("at24cxx_detect\n");

/* 構構一個i2c_client結構體: 以後收改數據時會用到它 */
at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
at24cxx_client->addr    = address;
at24cxx_client->adapter = adapter;
at24cxx_client->driver  = &at24cxx_driver;
strcpy(at24cxx_client->name, "at24cxx");
i2c_attach_client(at24cxx_client);

major = register_chrdev(0, "at24cxx", &at24cxx_fops);

cls = class_create(THIS_MODULE, "at24cxx");
class_device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */

return 0;
}

static int at24cxx_attach(struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, at24cxx_detect);
}

static int at24cxx_detach(struct i2c_client *client)
{
printk("at24cxx_detach\n");
class_device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "at24cxx");

i2c_detach_client(client);
kfree(i2c_get_clientdata(client));

return 0;
}

/* 1. 分配一個i2c_driver結構體 */
/* 2. 設置i2c_driver結構體 */
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = "at24cxx",
},
.attach_adapter = at24cxx_attach,
.detach_client  = at24cxx_detach,
};

static int at24cxx_init(void)
{
i2c_add_driver(&at24cxx_driver);
return 0;
}

static void at24cxx_exit(void)
{
i2c_del_driver(&at24cxx_driver);
}

module_init(at24cxx_init);
module_exit(at24cxx_exit);
MODULE_LICENSE("GPL");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章