S3C2440驱动移植——AT24C02(EEPROM)移植

来源:互联网 发布:大数据对贵州的影响 编辑:程序博客网 时间:2024/06/08 17:49

转载自 http://blog.csdn.net/yj4231/article/details/18182775

本文将介绍Linux中AT24C02驱动。AT24C02是一种EEPROM,使用I2C接口来访问。

在开发板中,使用I2C控制器0和AT24C02连接,这里就不给出原理图了,如需要,可以搜索TQ2440开发板的原理图。

目标平台:TQ2440 

CPU:s3c2440

内核版本:2.6.32

本文所有的代码均位于内核源码:linux/drivers/misc/eeprom/at24.c中。

1. 模块注册和注销

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static int __init at24_init(void)  
  2. {  
  3.     /* 将io_limit向下圆整到最近的2的幂*/  
  4.     io_limit = rounddown_pow_of_two(io_limit);  
  5.     return i2c_add_driver(&at24_driver); /* i2c 驱动注册*/  
  6. }  
  7. module_init(at24_init);  
  8.   
  9. static void __exit at24_exit(void)  
  10. {  
  11.     i2c_del_driver(&at24_driver);  
  12. }  
  13. module_exit(at24_exit);  
  14.   
  15. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");  
  16. MODULE_AUTHOR("David Brownell and Wolfram Sang");  
  17. MODULE_LICENSE("GPL");  


注册函数很简单。io_limit为写入时允许一次写入的最大字节,该参数为驱动模块参数,可由用户设置,默认值为128字节。

首先对io_limit向下圆整到最近的2的幂,接着直接调用了i2c_add_driver来注册一个i2c驱动。

注销函数更简单。注销之前注册的i2c驱动。


2. 设备驱动绑定

熟悉I2C驱动架构的可能会知道I2C驱动的match函数,该函数将使用id表(struct i2c_device_id)和i2c设备(struct i2c_client)进行匹配,判断是否有name字段相同,如果相同则匹配完成,即可完成设备和驱动的绑定,接着便会调用驱动提供的probe方法。我们来看下驱动提供的id表。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static struct i2c_driver at24_driver = {  
  2.     .driver = {  
  3.         .name = "at24",  
  4.         .owner = THIS_MODULE,  
  5.     },  
  6.     .probe = at24_probe,  
  7.     .remove = __devexit_p(at24_remove),  
  8.     .id_table = at24_ids,  
  9. };  
驱动提供的id为at24_ids,如下:


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static const struct i2c_device_id at24_ids[] = {  
  2.     /* needs 8 addresses as A0-A2 are ignored */  
  3.     { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },  
  4.     /* old variants can't be handled with this generic entry! */  
  5.     { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },  
  6.     { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },  
  7.     /* spd is a 24c02 in memory DIMMs */  
  8.     { "spd", AT24_DEVICE_MAGIC(2048 / 8,  
  9.         AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },  
  10.     { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },  
  11.     /* 24rf08 quirk is handled at i2c-core */  
  12.     { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },  
  13.     { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },  
  14.     { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },  
  15.     { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },  
  16.     { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },  
  17.     { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },  
  18.     { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },  
  19.     { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },  
  20.     { "at24", 0 },  
  21.     { /* END OF LIST */ }  
  22. };  

结构体成员的第一个参数即为name,表示设备的名字。第二个参数,在该驱动中,为一个幻术(magic),通过AT24_DEVICE_MAGIC宏计算。

宏第一个参数为eeprom的大小,第二参数为一些标志位。我们看下这个宏:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #define AT24_SIZE_BYTELEN 5  
  2. #define AT24_SIZE_FLAGS 8  
  3.   
  4. /* create non-zero magic value for given eeprom parameters */  
  5. #define AT24_DEVICE_MAGIC(_len, _flags)         \  
  6.     ((1 << AT24_SIZE_FLAGS | (_flags))        \  
  7.         << AT24_SIZE_BYTELEN | ilog2(_len))  

在这个表中,针对这里讲解的24c02,其大小为256字节,标志位为空。


3.probe函数

    当i2c总线完成设备驱动绑定后,就会调用probe方法了。具体看下这个函数。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)  
  2. {  
  3.     struct at24_platform_data chip;  
  4.     bool writable;  
  5.     bool use_smbus = false;  
  6.     struct at24_data *at24;  
  7.     int err;  
  8.     unsigned i, num_addresses;  
  9.     kernel_ulong_t magic;  
  10.   
  11.     /* 获取板级设备信息*/  
  12.     if (client->dev.platform_data) {  
  13.         chip = *(struct at24_platform_data *)client->dev.platform_data;  
  14.     } else {  
  15.         /* 没有板级设备信息,也没有driver_data,直接出错*/  
  16.         if (!id->driver_data) {  
  17.             err = -ENODEV;  
  18.             goto err_out;  
  19.         }  
  20.         magic = id->driver_data;  
  21.         chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));  
  22.         magic >>= AT24_SIZE_BYTELEN;  
  23.         chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);  
  24.         /* 
  25.          * This is slow, but we can't know all eeproms, so we better 
  26.          * play safe. Specifying custom eeprom-types via platform_data 
  27.          * is recommended anyhow. 
  28.          */  
  29.         chip.page_size = 1;  
  30.   
  31.         chip.setup = NULL;  
  32.         chip.context = NULL;  
  33.     }  
  34.   
  35.     /* 检查参数, 
  36.         byte_len和page_size必须为2的幂,不是则打印警告*/  
  37.     if (!is_power_of_2(chip.byte_len))  
  38.         dev_warn(&client->dev,  
  39.             "byte_len looks suspicious (no power of 2)!\n");  
  40.     if (!is_power_of_2(chip.page_size))  
  41.         dev_warn(&client->dev,  
  42.             "page_size looks suspicious (no power of 2)!\n");  
  43.   
  44.     /* Use I2C operations unless we're stuck with SMBus extensions. */  
  45.     /* 检查是否支持I2C协议, 
  46.         如果不支持,则检查是否使用SMBUS协议*/  
  47.     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {  
  48.         /* 不支持I2C协议,但是使用16位地址,出错*/  
  49.         if (chip.flags & AT24_FLAG_ADDR16) {  
  50.             err = -EPFNOSUPPORT;  
  51.             goto err_out;  
  52.         }  
  53.         /*  不支持I2C协议,使用8位地址, 
  54.            但是不支持I2C_FUNC_SMBUS_READ_I2C_BLOCK,出错*/  
  55.         if (!i2c_check_functionality(client->adapter,  
  56.                 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {  
  57.             err = -EPFNOSUPPORT;  
  58.             goto err_out;  
  59.         }  
  60.         use_smbus = true/*使用 SMBUS协议*/  
  61.     }  
  62.   
  63.     /*是否使用8个地址,根据id表, 
  64.         目前只有AT24C00使用8个地址,其他都为1个*/  
  65.     if (chip.flags & AT24_FLAG_TAKE8ADDR)  
  66.         num_addresses = 8;  
  67.     else  
  68.         /* 24C02需要1个地址,24C04为2个,以此类推*/  
  69.         num_addresses = DIV_ROUND_UP(chip.byte_len,  
  70.             (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);  
  71.   
  72.     /* 分配struct at24_data,同时根据地址个数分配struct i2c_client*/  
  73.     at24 = kzalloc(sizeof(struct at24_data) +  
  74.         num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);  
  75.     if (!at24) {  
  76.         err = -ENOMEM;  
  77.         goto err_out;  
  78.     }  
  79.   
  80.     /* 初始化struct at24_data*/  
  81.     mutex_init(&at24->lock);  
  82.     at24->use_smbus = use_smbus;  
  83.     at24->chip = chip;  
  84.     at24->num_addresses = num_addresses;  
  85.   
  86.     /* 
  87.      * Export the EEPROM bytes through sysfs, since that's convenient. 
  88.      * By default, only root should see the data (maybe passwords etc) 
  89.      */  
  90.      /* 设置bin_attribute字段,二进制文件名为eeprom, 
  91.          通过它即可读写设备 */  
  92.     at24->bin.attr.name = "eeprom";  
  93.     at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;  
  94.     at24->bin.read = at24_bin_read;  
  95.     at24->bin.size = chip.byte_len;  
  96.   
  97.     at24->macc.read = at24_macc_read;  /***  先忽略***/  
  98.   
  99.     /* 判断设备是否可写*/  
  100.     writable = !(chip.flags & AT24_FLAG_READONLY);  
  101.     if (writable) {  
  102.         if (!use_smbus || i2c_check_functionality(client->adapter,  
  103.                 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {  
  104.   
  105.             unsigned write_max = chip.page_size;  
  106.   
  107.             at24->macc.write = at24_macc_write;  /***  先忽略***/  
  108.   
  109.             at24->bin.write = at24_bin_write;    /* 写函数*/  
  110.             at24->bin.attr.mode |= S_IWUSR;    /* 文件拥有者可写*/  
  111.   
  112.             if (write_max > io_limit)  /* 一次最多写io_limit个字节*/  
  113.                 write_max = io_limit;  
  114.             /* 如果使用smbus,对write_max检查*/  
  115.             if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)  
  116.                 write_max = I2C_SMBUS_BLOCK_MAX;  
  117.             at24->write_max = write_max;    
  118.   
  119.             /* buffer (data + address at the beginning) */  
  120.             /* 分配写缓冲区,多余两个字节用于保存寄存器地址*/  
  121.             at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);  
  122.             if (!at24->writebuf) {  
  123.                 err = -ENOMEM;  
  124.                 goto err_struct;  
  125.             }  
  126.         } else {  
  127.             dev_warn(&client->dev,  
  128.                 "cannot write due to controller restrictions.");  
  129.         }  
  130.     }  
  131.   
  132.     at24->client[0] = client;  /* 保存i2c设备client*/  
  133.   
  134.     /* use dummy devices for multiple-address chips */  
  135.     /* 为其余设备地址注册一个dummy设备*/  
  136.     for (i = 1; i < num_addresses; i++) {  
  137.         at24->client[i] = i2c_new_dummy(client->adapter,  
  138.                     client->addr + i);     /* 设备地址每次加1 */  
  139.         if (!at24->client[i]) {  
  140.             dev_err(&client->dev, "address 0x%02x unavailable\n",  
  141.                     client->addr + i);  
  142.             err = -EADDRINUSE;  
  143.             goto err_clients;  
  144.         }  
  145.     }  
  146.   
  147.     /* 创建二进制属性*/  
  148.     err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);  
  149.     if (err)  
  150.         goto err_clients;  
  151.   
  152.     i2c_set_clientdata(client, at24);  /* 保存驱动数据*/  
  153.   
  154.     /* 打印设备信息*/  
  155.     dev_info(&client->dev, "%zu byte %s EEPROM %s\n",  
  156.         at24->bin.size, client->name,  
  157.         writable ? "(writable)" : "(read-only)");  
  158.     dev_dbg(&client->dev,  
  159.         "page_size %d, num_addresses %d, write_max %d%s\n",  
  160.         chip.page_size, num_addresses,  
  161.         at24->write_max,  
  162.         use_smbus ? ", use_smbus" : "");  
  163.   
  164.     /* export data to kernel code */  
  165.     if (chip.setup)  
  166.         chip.setup(&at24->macc, chip.context);  
  167.   
  168.     return 0;  
  169.   
  170. err_clients:  
  171.     for (i = 1; i < num_addresses; i++)  
  172.         if (at24->client[i])  
  173.             i2c_unregister_device(at24->client[i]);  
  174.   
  175.     kfree(at24->writebuf);  
  176. err_struct:  
  177.     kfree(at24);  
  178. }  

驱动首先获取板级设备信息(client->dev.platform_data),我们假设驱动移植时,添加了该板级设备信息。

判断是使用I2C协议还是SMBus协议。在这里,I2C adpater使用I2C协议。

然后,判断设备需要多少个i2c设备地址。

这里补充下:根据at24c02的datasheet,设备地址的第1位到第3位,将根据不同的设备来进行设置。

例如,如果是at24c04,则设备地址的第1位将用来表示寄存器地址,因为内存大小为512字节,而寄存器地址只有8位(256字节),

需要额外的一位用来表示512字节,因此使用了设备地址当中的一位来实现此目的。具体的请看datasheet。

这里使用at24c02,num_addresses将为1。

接着分配struct at24_data和struct i2c_client指针数组空间。

然后对struct at24_data进行了初始化工作。

接着,对二进制属性进行了配置。名字为eeprom,同时配置了其读方法(at24_bin_read),如果设备可写,还将配置其写方法(at24_bin_write)。

接下来很重要的一步,如果设备使用多个地址,则需要为所有地址(除了第一个地址)分配一个dummy device,这样这些地址就不会被其他的I2C设备占用了。

最后,向sys文件系统注册了二进制属性文件,通过该二进制文件,用户即可访问该设备。

注意:驱动使用了struct memory_accessor的东东,对这个东东不是太了解,所以先忽略,这个东西不影响驱动整体的架构。


4.设备访问方法

   从第3结的分析可知,驱动并没有注册任何字符设备或者杂项设备,只是向sys文件系统注册了一个二进制属性文件。因此要访问设备,必须通过该文件的读写函数来。

读写函数在probe函数中指定为at24_bin_write和at24_bin_read,我们来分别看下。

4.1 写函数(at24_bin_write)

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,  
  2.         char *buf, loff_t off, size_t count)  
  3. {  
  4.     struct at24_data *at24;  
  5.   
  6.         /* 通过kobj获取device,再获取driver_data */  
  7.     at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));  
  8.     return at24_write(at24, buf, off, count);  
  9. }  
该函数首先通过kobj获取了struct device的指针,再获取了at24。

接着直接调用了at24_write。如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. tatic ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,  
  2.               size_t count)  
  3. {  
  4.     ssize_t retval = 0;  
  5.   
  6.     if (unlikely(!count))  
  7.         return count;  
  8.   
  9.     /* 
  10.      * Write data to chip, protecting against concurrent updates 
  11.      * from this host, but not from other I2C masters. 
  12.      */  
  13.       /* 访问设备前,加锁*/  
  14.     mutex_lock(&at24->lock);  
  15.   
  16.     while (count) {  
  17.         ssize_t status;  
  18.   
  19.         status = at24_eeprom_write(at24, buf, off, count);  
  20.         if (status <= 0) {  
  21.             if (retval == 0)  
  22.                 retval = status;  
  23.             break;  
  24.         }  
  25.         buf += status;  
  26.         off += status;  
  27.         count -= status;  
  28.         retval += status;  
  29.     }  
  30.   
  31.     mutex_unlock(&at24->lock);  
  32.   
  33.     return retval;  
  34. }  

该函数不复杂。在访问设备前,首先加锁互斥体,以防止竞态。然后根据count来调用at24_eeprom_write函数将数据写入设备。

写入成功后,更新偏移量等信息,如果还需要写入,则再次调用at24_eeprom_write函数。

看下at24_eeprom_write函数:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * Note that if the hardware write-protect pin is pulled high, the whole 
  3.  * chip is normally write protected. But there are plenty of product 
  4.  * variants here, including OTP fuses and partial chip protect. 
  5.  * 
  6.  * We only use page mode writes; the alternative is sloooow. This routine 
  7.  * writes at most one page. 
  8.  */  
  9. static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,  
  10.         unsigned offset, size_t count)  
  11. {  
  12.     struct i2c_client *client;  
  13.     struct i2c_msg msg;  
  14.     ssize_t status;  
  15.     unsigned long timeout, write_time;  
  16.     unsigned next_page;  
  17.   
  18.     /* Get corresponding I2C address and adjust offset */  
  19.     client = at24_translate_offset(at24, &offset);  
  20.   
  21.     /* write_max is at most a page */  
  22.     /* 检查写入的字节数*/  
  23.     if (count > at24->write_max)  
  24.         count = at24->write_max;  
  25.   
  26.     /* Never roll over backwards, to the start of this page */  
  27.     /* 写入不会超过下一页的边界*/  
  28.     next_page = roundup(offset + 1, at24->chip.page_size);  
  29.     /* 根据页大小调整count*/  
  30.     if (offset + count > next_page)  
  31.         count = next_page - offset;  
  32.   
  33.     /* If we'll use I2C calls for I/O, set up the message */  
  34.     /* 使用I2C协议,需要填充msg*/  
  35.     if (!at24->use_smbus) {  
  36.         int i = 0;  
  37.   
  38.         msg.addr = client->addr;  /*设备地址*/  
  39.         msg.flags = 0;  
  40.   
  41.         /* msg.buf is u8 and casts will mask the values */  
  42.         /* 使用writebuf作为发送缓冲区 */  
  43.         msg.buf = at24->writebuf;  
  44.           
  45.         /* 根据是8位还是16位地址,msg.buf的前一(两)个字节 
  46.              为设备内部的寄存器地址*/  
  47.         if (at24->chip.flags & AT24_FLAG_ADDR16)  
  48.             msg.buf[i++] = offset >> 8;  /* 16位地址,先写高位地址*/  
  49.   
  50.         msg.buf[i++] = offset;  
  51.         /* 复制需要发送的数据 */  
  52.         memcpy(&msg.buf[i], buf, count);  
  53.         msg.len = i + count;   /* 发送长度为数据长度加上地址长度*/  
  54.     }  
  55.   
  56.     /* 
  57.      * Writes fail if the previous one didn't complete yet. We may 
  58.      * loop a few times until this one succeeds, waiting at least 
  59.      * long enough for one entire page write to work. 
  60.      */  
  61.     timeout = jiffies + msecs_to_jiffies(write_timeout);  
  62.     do {  
  63.         write_time = jiffies;  
  64.         if (at24->use_smbus) {  
  65.             /* 使用SMBus协议发送*/  
  66.             status = i2c_smbus_write_i2c_block_data(client,  
  67.                     offset, count, buf);  
  68.             if (status == 0)  
  69.                 status = count;  
  70.         } else {  
  71.             /* 使用I2C协议发送*/  
  72.             status = i2c_transfer(client->adapter, &msg, 1);  
  73.             if (status == 1)  
  74.                 status = count;  
  75.         }  
  76.         dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",  
  77.                 count, offset, status, jiffies);  
  78.   
  79.         if (status == count)  
  80.             return count;  /* 已全部写入,返回*/  
  81.   
  82.         /* REVISIT: at HZ=100, this is sloooow */  
  83.         msleep(1);  
  84.     } while (time_before(write_time, timeout));  /* 使用timeout */  
  85.   
  86.     return -ETIMEDOUT;  /* 超时,返回错误*/  
  87. }  

该函数首先调用了at24_translate_offset函数,来获取地址对应的client:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * This routine supports chips which consume multiple I2C addresses. It 
  3.  * computes the addressing information to be used for a given r/w request. 
  4.  * Assumes that sanity checks for offset happened at sysfs-layer. 
  5.  */  
  6. static struct i2c_client *at24_translate_offset(struct at24_data *at24,  
  7.         unsigned *offset)  
  8. {  
  9.     unsigned i;  
  10.   
  11.     /* 有多个I2C设备地址,根据offset获取该地址对应的client*/  
  12.     if (at24->chip.flags & AT24_FLAG_ADDR16) {  
  13.         i = *offset >> 16;  
  14.         *offset &= 0xffff;  
  15.     } else {  
  16.         i = *offset >> 8;  
  17.         *offset &= 0xff;  
  18.     }  
  19.   
  20.     return at24->client[i];  
  21. }  

然后,对写入的字节数(count)进行了调整。

随后,如果使用I2C协议,则要组建msg用于发送。

最后,根据使用I2C还是SMBus协议,调用相应的发送函数来发送数据。

注意的是,这里使用了超时,超时时间write_timeout为驱动模块参数,可由用户设置,默认为25ms。如果发送超时了,while循环将终止。

至此,at24c02的写入过程就结束了。

4.2 读函数(at24_bin_read)

  写函数和读函数非常相似,只是在使用I2C协议时,组建的msg有所不同。同样读函数也使用了超时。

  因此,这里仅仅给出代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 
  2.  * This routine supports chips which consume multiple I2C addresses. It 
  3.  * computes the addressing information to be used for a given r/w request. 
  4.  * Assumes that sanity checks for offset happened at sysfs-layer. 
  5.  */  
  6. static struct i2c_client *at24_translate_offset(struct at24_data *at24,  
  7.         unsigned *offset)  
  8. {  
  9.     unsigned i;  
  10.   
  11.     /* 有多个I2C设备地址,根据offset获取该地址对应的client*/  
  12.     if (at24->chip.flags & AT24_FLAG_ADDR16) {  
  13.         i = *offset >> 16;  
  14.         *offset &= 0xffff;  
  15.     } else {  
  16.         i = *offset >> 8;  
  17.         *offset &= 0xff;  
  18.     }  
  19.   
  20.     return at24->client[i];  
  21. }  
  22.   
  23. static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,  
  24.         unsigned offset, size_t count)  
  25. {  
  26.     struct i2c_msg msg[2];  
  27.     u8 msgbuf[2];  
  28.     struct i2c_client *client;  
  29.     unsigned long timeout, read_time;  
  30.     int status, i;  
  31.   
  32.     memset(msg, 0, sizeof(msg));  
  33.   
  34.     /* 
  35.      * REVISIT some multi-address chips don't rollover page reads to 
  36.      * the next slave address, so we may need to truncate the count. 
  37.      * Those chips might need another quirk flag. 
  38.      * 
  39.      * If the real hardware used four adjacent 24c02 chips and that 
  40.      * were misconfigured as one 24c08, that would be a similar effect: 
  41.      * one "eeprom" file not four, but larger reads would fail when 
  42.      * they crossed certain pages. 
  43.      */  
  44.   
  45.     /* 
  46.      * Slave address and byte offset derive from the offset. Always 
  47.      * set the byte address; on a multi-master board, another master 
  48.      * may have changed the chip's "current" address pointer. 
  49.      */  
  50.     client = at24_translate_offset(at24, &offset);  
  51.   
  52.     if (count > io_limit)  
  53.         count = io_limit;  
  54.   
  55.     if (at24->use_smbus) {  
  56.         /* Smaller eeproms can work given some SMBus extension calls */  
  57.         if (count > I2C_SMBUS_BLOCK_MAX)  
  58.             count = I2C_SMBUS_BLOCK_MAX;  
  59.     } else {  
  60.         /* 使用I2C协议,需要填充msg*/  
  61.         /* 
  62.          * When we have a better choice than SMBus calls, use a 
  63.          * combined I2C message. Write address; then read up to 
  64.          * io_limit data bytes. Note that read page rollover helps us 
  65.          * here (unlike writes). msgbuf is u8 and will cast to our 
  66.          * needs. 
  67.          */  
  68.         i = 0;  
  69.         if (at24->chip.flags & AT24_FLAG_ADDR16)  
  70.             msgbuf[i++] = offset >> 8;  
  71.         msgbuf[i++] = offset;  
  72.   
  73.         msg[0].addr = client->addr;  
  74.         msg[0].buf = msgbuf;  
  75.         msg[0].len = i;  
  76.   
  77.         msg[1].addr = client->addr;  
  78.         msg[1].flags = I2C_M_RD;  /* 读模式*/  
  79.         msg[1].buf = buf;  
  80.         msg[1].len = count;  
  81.     }  
  82.   
  83.     /* 
  84.      * Reads fail if the previous write didn't complete yet. We may 
  85.      * loop a few times until this one succeeds, waiting at least 
  86.      * long enough for one entire page write to work. 
  87.      */  
  88.     timeout = jiffies + msecs_to_jiffies(write_timeout);  
  89.     do {  
  90.         read_time = jiffies;  
  91.         if (at24->use_smbus) {  
  92.             status = i2c_smbus_read_i2c_block_data(client, offset,  
  93.                     count, buf);  
  94.         } else {  
  95.             status = i2c_transfer(client->adapter, msg, 2);  
  96.             if (status == 2)  
  97.                 status = count;  
  98.         }  
  99.         dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",  
  100.                 count, offset, status, jiffies);  
  101.   
  102.         if (status == count)  
  103.             return count;  
  104.   
  105.         /* REVISIT: at HZ=100, this is sloooow */  
  106.         msleep(1);  
  107.     } while (time_before(read_time, timeout));  /* 使用timeout */  
  108.   
  109.     return -ETIMEDOUT;  
  110. }  
  111.   
  112. static ssize_t at24_read(struct at24_data *at24,  
  113.         char *buf, loff_t off, size_t count)  
  114. {  
  115.     ssize_t retval = 0;  
  116.   
  117.     if (unlikely(!count))  
  118.         return count;  
  119.   
  120.     /* 
  121.      * Read data from chip, protecting against concurrent updates 
  122.      * from this host, but not from other I2C masters. 
  123.      */  
  124.      /* 访问设备前,加锁*/  
  125.     mutex_lock(&at24->lock);   
  126.   
  127.     while (count) {  
  128.         ssize_t status;  
  129.   
  130.         status = at24_eeprom_read(at24, buf, off, count);  
  131.         if (status <= 0) {  
  132.             if (retval == 0)  
  133.                 retval = status;  
  134.             break;  
  135.         }  
  136.         buf += status;  
  137.         off += status;  
  138.         count -= status;  
  139.         retval += status;  
  140.     }  
  141.   
  142.     mutex_unlock(&at24->lock);  
  143.   
  144.     return retval;  
  145. }  
  146.   
  147. static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,  
  148.         char *buf, loff_t off, size_t count)  
  149. {  
  150.     struct at24_data *at24;  
  151.   
  152.     /* 通过kobj获取device,再获取driver_data */  
  153.     at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));  
  154.     return at24_read(at24, buf, off, count);  
  155. }  

5. 总结

    本文主要对at24c02的驱动架构进行了分析。该驱动基于i2c总线架构,提供了id表来帮助设备驱动的绑定,该驱动支持AT24CXX等多个系列,不仅仅是at24c02。

其次,该驱动并没有注册任何字符设备或者杂项设备,而是通过sys文件系统的二进制属性文件来对设备进行访问。此外,驱动同时支持I2C协议和SMBus协议来访问设备。


有关驱动的移植可以参考:

S3C2440驱动移植——AT24C02(EEPROM)移植

0 0