/******************************************************
* linux 内核版本:2.6.32.2
*硬件平台:s3c2440
*i2c设备:at24c08
*******************************************************/
二:设备驱动
at24c08电路图
at24c08设备地址
由电路图得知,设备地址为0x50
include/linux/i2c/at24.h文件
#ifndef _LINUX_AT24_H
#define _LINUX_AT24_H
#include <linux/types.h>
#include <linux/memory.h>
/*
* As seen through Linux I2C, differences between the most common types of I2C
* memory include:
* - How much memory is available (usually specified in bit)?
* - What write page size does it support?
* - Special flags (16 bit addresses, read_only, world readable...)?
*
* If you set up a custom eeprom type, please double-check the parameters.
* Especially page_size needs extra care, as you risk data loss if your value
* is bigger than what the chip actually supports!
*/
struct at24_platform_data {
u32 byte_len; /* size (sum of all addr) */
u16 page_size; /* for writes */
u8 flags;
#define AT24_FLAG_ADDR16 0x80 /* address pointer is 16 bit */
#define AT24_FLAG_READONLY 0x40 /* sysfs-entry will be read-only */
#define AT24_FLAG_IRUGO 0x20 /* sysfs-entry will be world-readable */
#define AT24_FLAG_TAKE8ADDR 0x10 /* take always 8 addresses (24c00) */
void (*setup)(struct memory_accessor *, void *context);
void *context;
};
#endif /* _LINUX_AT24_H */
在arch/arm/mach-s3c2440/mach-arm2440.cz中,添加代码
1 头文件
#include <linux/i2c.h>
#include <linux/i2c/at24.h>
2 平台信息
static struct at24_platform_data at24c08 = {
.byte_len = SZ_8K / 8,
.page_size = 16,
};
static struct i2c_board_info i2c_devices[] __initdata = {
{I2C_BOARD_INFO("at24c08", 0x50),
.platform_data = &at24c08} ,
};
3 注册设备
static void __init arm2440_machine_init(void)
{
#if defined (LCD_WIDTH)
s3c24xx_fb_set_platdata(&arm2440_fb_info);
#endif
s3c_i2c0_set_platdata(NULL);
//at24c08
i2c_register_board_info(0,i2c_devices,ARRAY_SIZE(i2c_devices));
//end at24c08
s3c2410_gpio_cfgpin(S3C2410_GPC(0), S3C2410_GPC0_LEND);
s3c_device_nand.dev.platform_data = &friendly_arm_nand_info;
s3c_device_sdi.dev.platform_data = &arm2440_mmc_cfg;
platform_add_devices(arm2440_devices, ARRAY_SIZE(arm2440_devices));
s3c_pm_init();
}
添加完后保存,从新编译内核,然后写at4c08驱动程序
支持的设备ID
static struct i2c_device_id at24xx_ids[]=
{
{"at24c01",0x50},
{"at24c02",0x50},
{"at24c04",0x50},
{"at24c08",0x50},
{},
};
驱动描述
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = "at24c",
},
.probe = at24cxx_probe,
.id_table = at24xx_ids,
.remove = at24xx_remove,
};
添加驱动和注册字符设备驱动
static int __init at24cxx_init(void)
{
int res;
printk(KERN_ERR "at24cxx_init\n");
res = register_chrdev(I2C_MAJOR, DEV_NAME, &i2cdev_fops);
if (res)
goto out;
i2c_dev_class = class_create(THIS_MODULE, DEV_NAME);
if (IS_ERR(i2c_dev_class)) {
res = PTR_ERR(i2c_dev_class);
goto out_unreg_chrdev;
}
res = i2c_add_driver(&at24cxx_driver);
if (res)
goto out_unreg_class;
return 0;
out_unreg_class:
class_destroy(i2c_dev_class);
out_unreg_chrdev:
unregister_chrdev(I2C_MAJOR, "at24c");
out:
printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
return res;
}
字符设备操作集合
static const struct file_operations i2cdev_fops = {
.owner = THIS_MODULE,
.read = i2cdev_read,
.write = i2cdev_write,
.unlocked_ioctl = i2cdev_ioctl,
.open = i2cdev_open,
.release = i2cdev_release,
};
探测到设备后进行处理(new style)
static int at24cxx_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int res;
struct device *dev;
printk(KERN_ERR "probe:name = %s,flag =%d,addr = %x,adapter = %d,driver = %s\n",client->name,
client->flags,client->addr,client->adapter->nr,client->driver->driver.name );
my_client = client;
/* register this i2c device with the driver core */
dev = device_create(i2c_dev_class, &my_client->dev,
MKDEV(I2C_MAJOR, 0), NULL,
DEV_NAME);
if (IS_ERR(dev)) {
res = PTR_ERR(dev);
goto error;
}
return 0;
error:
return res;
}
全局定义
#define DEV_NAME "at24c"
static struct class *i2c_dev_class;
static struct i2c_client *my_client;
读操作,需要先写读地址,然后再读数据
static ssize_t i2cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
char *tmp;
char reg_addr=0;
int ret;
printk(KERN_ERR "i2cdev_read\n");
if (count > 8192)
count = 8192;
tmp = kmalloc(count, GFP_KERNEL);
if (tmp == NULL)
return -ENOMEM;
struct i2c_msg msg[2];
struct i2c_client *client= (struct i2c_client *)file->private_data;
struct i2c_adapter *adap = client->adapter;
reg_addr = *offset;
printk(KERN_ERR"read:count = %d,offset = %ld\n",count,*offset);
msg[0].addr = client->addr;
msg[0].flags = client->flags & I2C_M_TEN;
msg[0].len = 1;
msg[0].buf = reg_addr;//read addr
msg[1].addr = client->addr;
msg[1].flags = client->flags & I2C_M_TEN;
msg[1].flags |= I2C_M_RD;
msg[1].len = count;
msg[1].buf = tmp;//read data
ret = i2c_transfer(adap, msg, 2);
printk(KERN_ERR "i2c_transfer:ret=%d\n",ret);
if (ret != 2)
goto out;
ret = copy_to_user(buf, tmp, count);
printk(KERN_ERR "copy_to_user:ret=%d\n",ret);
if(ret)
goto out;
kfree(tmp);
return count;
out:
kfree(tmp);
printk(KERN_ERR "i2cdev_read out:\n");
return -1;
}
写操作是写一个地址,对应一个数据
static ssize_t i2cdev_write(struct file *file, const char __user *buf,
size_t count, loff_t *offset)
{
int ret;
int i;
char *tmp;
char tmp_data[2];
struct i2c_client *client = file->private_data;
struct i2c_adapter *adap = client->adapter;
struct i2c_msg msg;
printk(KERN_ERR "i2cdev_write\n");
if (count > 8192)
count = 8192;
printk(KERN_ERR"write:count = %d,offset = %ld\n",count,*offset);
tmp = kmalloc(count, GFP_KERNEL);
if (tmp == NULL)
return -ENOMEM;
if (copy_from_user(tmp, buf, count)) {
kfree(tmp);
printk(KERN_ERR"copy_from_user falied\n");
return -EFAULT;
}
msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
for(i=0; i< count; i++)
{
tmp_data[0] = *offset +i;
tmp_data[1] = *(tmp+i);
msg.len = 2;
msg.buf = tmp_data;
ret = i2c_transfer(adap, &msg, 1);
if(1 != ret)
{
printk(KERN_ERR"i2cdev_write:i2c_transfer !=1 ret=%d\n",ret);
goto out;
}
}
kfree(tmp);
/* If everything went ok (i.e. 1 msg transmitted), return #bytes
transmitted, else error code. */
return (ret == 1) ? count : ret;
out:
kfree(tmp);
printk(KERN_ERR"in out ret=%d\n",ret);
return -1;
}
ioclt,只写了3个命令
static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct i2c_client *client = file->private_data;
printk(KERN_ERR "i2cdev_ioctl, cmd=0x%02x, arg=0x%02lx\n",cmd, arg);
switch (cmd) {
case I2C_TENBIT:
if (arg)
client->flags |= I2C_M_TEN;
else
client->flags &= ~I2C_M_TEN;
return 0;
case I2C_RETRIES:
client->adapter->retries = arg;
break;
case I2C_TIMEOUT:
/* For historical reasons, user-space sets the timeout
* value in units of 10 ms.
*/
client->adapter->timeout = msecs_to_jiffies(arg * 10);
break;
default:
/* NOTE: returning a fault code here could cause trouble
* in buggy userspace code. Some old kernel bugs returned
* zero in this case, and userspace code might accidentally
* have depended on that bug.
*/
return -ENOTTY;
}
return 0;
}
static int i2cdev_open(struct inode *inode, struct file *file)
{
printk(KERN_ERR "i2cdev_open\n");
file->private_data = my_client;
return 0;
}
测试结果