免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 16361 | 回复: 20
打印 上一主题 下一主题

[驱动] EEPROM AT24C02 的 I2C 驱动问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-21 18:04 |只看该作者 |倒序浏览
小弟我要使用 AT24C02 存点数据,

- 编译了 2.6.30.4 中的 at24.c 文件,at24_init() 中调用 i2c_add_driver();
- i2c_add_driver() 依次调用以下函数:
   i2c_register_driver() -> class_for_each_device() -> __attach_adapter() -> i2c_detect():
- 在 i2c_detect() 中,运行到以下语句,
  if (!driver->detect || !address_data) { return 0; }
   函数返回为 0,

最后的结果是 i2c_add_driver() 运行成功(返回0),
但 at24_probe() 却未被执行,后续的 write/read 也全部没有被执行,
系统提示:
- s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)

请问各位大哥这是怎么回事?

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
2 [报告]
发表于 2011-02-21 18:09 |只看该作者
I2C总线上挂载这个设备了吗?
或者说供电了没?

论坛徽章:
0
3 [报告]
发表于 2011-02-21 19:32 |只看该作者
我写了一个内核调用i2c的驱动模块,加载这个模块后,可以在你的驱动中直接调用api即可
http://blog.csdn.net/sepnic/archive/2011/02/11/6178657.aspx
我当时写这个东东就因为我平台很多地方要用到i2c,这样每个地方都要i2c_register_driver,太麻烦了

论坛徽章:
0
4 [报告]
发表于 2011-02-22 09:24 |只看该作者
I2C总线上挂载这个设备了吗?
或者说供电了没?
T-Bagwell 发表于 2011-02-21 18:09



谢谢关注;
在同样一块板,在 Bootloader 中测试,是正常的,硬件应该没有问题。

我感觉这个 at24.c 好像没有完成,detect 和 address_data 这两个字段均没有填充,
导致 i2c_detect 根本没有真正执行;在 eeprom.c 中倒是实现了,但这个设备我的机器却没有;

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
5 [报告]
发表于 2011-02-22 09:27 |只看该作者
代码方便贴出来不?

论坛徽章:
0
6 [报告]
发表于 2011-02-22 09:40 |只看该作者
我写了一个内核调用i2c的驱动模块,加载这个模块后,可以在你的驱动中直接调用api即可

我当时写这个东东 ...
sep 发表于 2011-02-21 19:32



谢谢,我先试试,
这个驱动要如何调用呢? i2c_api_do_send  / i2c_api_do_recv 的前几个参数要如何写?
如果可以,是否能再贴一段调用的例子?

论坛徽章:
0
7 [报告]
发表于 2011-02-22 09:41 |只看该作者
代码方便贴出来不?
T-Bagwell 发表于 2011-02-22 09:27



代码是 2.6.30.4 中自带的,如下:
  1. /*
  2. * at24.c - handle most I2C EEPROMs
  3. *
  4. * Copyright (C) 2005-2007 David Brownell
  5. * Copyright (C) 2008 Wolfram Sang, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/log2.h>
  21. #include <linux/bitops.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c/at24.h>

  25. static int debug = 1;
  26. #define dprintk(msg...)        if (debug) { printk(KERN_DEBUG "at24: " msg); }

  27. /*
  28. * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
  29. * Differences between different vendor product lines (like Atmel AT24C or
  30. * MicroChip 24LC, etc) won't much matter for typical read/write access.
  31. * There are also I2C RAM chips, likewise interchangeable. One example
  32. * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
  33. *
  34. * However, misconfiguration can lose data. "Set 16-bit memory address"
  35. * to a part with 8-bit addressing will overwrite data. Writing with too
  36. * big a page size also loses data. And it's not safe to assume that the
  37. * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
  38. * uses 0x51, for just one example.
  39. *
  40. * Accordingly, explicit board-specific configuration data should be used
  41. * in almost all cases. (One partial exception is an SMBus used to access
  42. * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
  43. *
  44. * So this driver uses "new style" I2C driver binding, expecting to be
  45. * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
  46. * similar kernel-resident tables; or, configuration data coming from
  47. * a bootloader.
  48. *
  49. * Other than binding model, current differences from "eeprom" driver are
  50. * that this one handles write access and isn't restricted to 24c02 devices.
  51. * It also handles larger devices (32 kbit and up) with two-byte addresses,
  52. * which won't work on pure SMBus systems.
  53. */

  54. struct at24_data {
  55.         struct at24_platform_data chip;
  56.         struct memory_accessor macc;
  57.         bool use_smbus;

  58.         /*
  59.          * Lock protects against activities from other Linux tasks,
  60.          * but not from changes by other I2C masters.
  61.          */
  62.         struct mutex lock;
  63.         struct bin_attribute bin;

  64.         u8 *writebuf;
  65.         unsigned write_max;
  66.         unsigned num_addresses;

  67.         /*
  68.          * Some chips tie up multiple I2C addresses; dummy devices reserve
  69.          * them for us, and we'll use them with SMBus calls.
  70.          */
  71.         struct i2c_client *client[];
  72. };

  73. /*
  74. * This parameter is to help this driver avoid blocking other drivers out
  75. * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
  76. * clock, one 256 byte read takes about 1/43 second which is excessive;
  77. * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
  78. * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
  79. *
  80. * This value is forced to be a power of two so that writes align on pages.
  81. */
  82. static unsigned io_limit = 128;
  83. module_param(io_limit, uint, 0);
  84. MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");

  85. /*
  86. * Specs often allow 5 msec for a page write, sometimes 20 msec;
  87. * it's important to recover from write timeouts.
  88. */
  89. static unsigned write_timeout = 25;
  90. module_param(write_timeout, uint, 0);
  91. MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");

  92. #define AT24_SIZE_BYTELEN 5
  93. #define AT24_SIZE_FLAGS 8

  94. #define AT24_BITMASK(x) (BIT(x) - 1)

  95. /* create non-zero magic value for given eeprom parameters */
  96. #define AT24_DEVICE_MAGIC(_len, _flags)                 \
  97.         ((1 << AT24_SIZE_FLAGS | (_flags))                 \
  98.             << AT24_SIZE_BYTELEN | ilog2(_len))

  99. static const struct i2c_device_id at24_ids[] = {
  100.         /* needs 8 addresses as A0-A2 are ignored */
  101.         { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
  102.         /* old variants can't be handled with this generic entry! */
  103.         { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
  104.         { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
  105.         /* spd is a 24c02 in memory DIMMs */
  106.         { "spd", AT24_DEVICE_MAGIC(2048 / 8,
  107.                 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
  108.         { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
  109.         /* 24rf08 quirk is handled at i2c-core */
  110.         { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
  111.         { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
  112.         { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
  113.         { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
  114.         { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
  115.         { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
  116.         { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
  117.         { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
  118.         { "at24", 0 },
  119.         { /* END OF LIST */ }
  120. };
  121. MODULE_DEVICE_TABLE(i2c, at24_ids);

  122. /*-------------------------------------------------------------------------*/

  123. /*
  124. * This routine supports chips which consume multiple I2C addresses. It
  125. * computes the addressing information to be used for a given r/w request.
  126. * Assumes that sanity checks for offset happened at sysfs-layer.
  127. */
  128. static struct i2c_client *at24_translate_offset(struct at24_data *at24,
  129.                 unsigned *offset)
  130. {
  131.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  132.         unsigned i;

  133.         if (at24->chip.flags & AT24_FLAG_ADDR16) {
  134.                 i = *offset >> 16;
  135.                 *offset &= 0xffff;
  136.         } else {
  137.                 i = *offset >> 8;
  138.                 *offset &= 0xff;
  139.         }

  140.         return at24->client[i];
  141. }

  142. static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
  143.                 unsigned offset, size_t count)
  144. {
  145.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  146.         struct i2c_msg msg[2];
  147.         u8 msgbuf[2];
  148.         struct i2c_client *client;
  149.         int status, i;

  150.         memset(msg, 0, sizeof(msg));

  151.         /*
  152.          * REVISIT some multi-address chips don't rollover page reads to
  153.          * the next slave address, so we may need to truncate the count.
  154.          * Those chips might need another quirk flag.
  155.          *
  156.          * If the real hardware used four adjacent 24c02 chips and that
  157.          * were misconfigured as one 24c08, that would be a similar effect:
  158.          * one "eeprom" file not four, but larger reads would fail when
  159.          * they crossed certain pages.
  160.          */

  161.         /*
  162.          * Slave address and byte offset derive from the offset. Always
  163.          * set the byte address; on a multi-master board, another master
  164.          * may have changed the chip's "current" address pointer.
  165.          */
  166.         client = at24_translate_offset(at24, &offset);

  167.         if (count > io_limit)
  168.                 count = io_limit;

  169.         /* Smaller eeproms can work given some SMBus extension calls */
  170.         if (at24->use_smbus) {
  171.                 if (count > I2C_SMBUS_BLOCK_MAX)
  172.                         count = I2C_SMBUS_BLOCK_MAX;
  173.                 status = i2c_smbus_read_i2c_block_data(client, offset,
  174.                                 count, buf);
  175.                 dev_dbg(&client->dev, "smbus read %zu@%d --> %d\n",
  176.                                 count, offset, status);
  177.                 return (status < 0) ? -EIO : status;
  178.         }

  179.         /*
  180.          * When we have a better choice than SMBus calls, use a combined
  181.          * I2C message. Write address; then read up to io_limit data bytes.
  182.          * Note that read page rollover helps us here (unlike writes).
  183.          * msgbuf is u8 and will cast to our needs.
  184.          */
  185.         i = 0;
  186.         if (at24->chip.flags & AT24_FLAG_ADDR16)
  187.                 msgbuf[i++] = offset >> 8;
  188.         msgbuf[i++] = offset;

  189.         msg[0].addr = client->addr;
  190.         msg[0].buf = msgbuf;
  191.         msg[0].len = i;

  192.         msg[1].addr = client->addr;
  193.         msg[1].flags = I2C_M_RD;
  194.         msg[1].buf = buf;
  195.         msg[1].len = count;

  196.         status = i2c_transfer(client->adapter, msg, 2);
  197.         dev_dbg(&client->dev, "i2c read %zu@%d --> %d\n",
  198.                         count, offset, status);

  199.         if (status == 2)
  200.                 return count;
  201.         else if (status >= 0)
  202.                 return -EIO;
  203.         else
  204.                 return status;
  205. }

  206. static ssize_t at24_read(struct at24_data *at24,
  207.                 char *buf, loff_t off, size_t count)
  208. {
  209.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);

  210.         ssize_t retval = 0;

  211.         if (unlikely(!count))
  212.                 return count;

  213.         /*
  214.          * Read data from chip, protecting against concurrent updates
  215.          * from this host, but not from other I2C masters.
  216.          */
  217.         mutex_lock(&at24->lock);

  218.         while (count) {
  219.                 ssize_t        status;

  220.                 status = at24_eeprom_read(at24, buf, off, count);
  221.                 if (status <= 0) {
  222.                         if (retval == 0)
  223.                                 retval = status;
  224.                         break;
  225.                 }
  226.                 buf += status;
  227.                 off += status;
  228.                 count -= status;
  229.                 retval += status;
  230.         }

  231.         mutex_unlock(&at24->lock);

  232.         return retval;
  233. }

  234. static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
  235.                 char *buf, loff_t off, size_t count)
  236. {
  237.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  238.         struct at24_data *at24;

  239.         at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  240.         return at24_read(at24, buf, off, count);
  241. }


  242. /*
  243. * Note that if the hardware write-protect pin is pulled high, the whole
  244. * chip is normally write protected. But there are plenty of product
  245. * variants here, including OTP fuses and partial chip protect.
  246. *
  247. * We only use page mode writes; the alternative is sloooow. This routine
  248. * writes at most one page.
  249. */
  250. static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
  251.                 unsigned offset, size_t count)
  252. {
  253.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  254.         struct i2c_client *client;
  255.         struct i2c_msg msg;
  256.         ssize_t status;
  257.         unsigned long timeout, write_time;
  258.         unsigned next_page;

  259.         /* Get corresponding I2C address and adjust offset */
  260.         client = at24_translate_offset(at24, &offset);

  261.         /* write_max is at most a page */
  262.         if (count > at24->write_max)
  263.                 count = at24->write_max;

  264.         /* Never roll over backwards, to the start of this page */
  265.         next_page = roundup(offset + 1, at24->chip.page_size);
  266.         if (offset + count > next_page)
  267.                 count = next_page - offset;

  268.         /* If we'll use I2C calls for I/O, set up the message */
  269.         if (!at24->use_smbus) {
  270.                 int i = 0;

  271.                 msg.addr = client->addr;
  272.                 msg.flags = 0;

  273.                 /* msg.buf is u8 and casts will mask the values */
  274.                 msg.buf = at24->writebuf;
  275.                 if (at24->chip.flags & AT24_FLAG_ADDR16)
  276.                         msg.buf[i++] = offset >> 8;

  277.                 msg.buf[i++] = offset;
  278.                 memcpy(&msg.buf[i], buf, count);
  279.                 msg.len = i + count;
  280.         }

  281.         /*
  282.          * Writes fail if the previous one didn't complete yet. We may
  283.          * loop a few times until this one succeeds, waiting at least
  284.          * long enough for one entire page write to work.
  285.          */
  286.         timeout = jiffies + msecs_to_jiffies(write_timeout);
  287.         do {
  288.                 write_time = jiffies;
  289.                 if (at24->use_smbus) {
  290.                         status = i2c_smbus_write_i2c_block_data(client,
  291.                                         offset, count, buf);
  292.                         if (status == 0)
  293.                                 status = count;
  294.                 } else {
  295.                         status = i2c_transfer(client->adapter, &msg, 1);
  296.                         if (status == 1)
  297.                                 status = count;
  298.                 }
  299.                 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
  300.                                 count, offset, status, jiffies);

  301.                 if (status == count)
  302.                         return count;

  303.                 /* REVISIT: at HZ=100, this is sloooow */
  304.                 msleep(1);
  305.         } while (time_before(write_time, timeout));

  306.         return -ETIMEDOUT;
  307. }

  308. static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
  309.                           size_t count)
  310. {
  311.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  312.         ssize_t retval = 0;

  313.         if (unlikely(!count))
  314.                 return count;

  315.         /*
  316.          * Write data to chip, protecting against concurrent updates
  317.          * from this host, but not from other I2C masters.
  318.          */
  319.         mutex_lock(&at24->lock);

  320.         while (count) {
  321.                 ssize_t        status;

  322.                 status = at24_eeprom_write(at24, buf, off, count);
  323.                 if (status <= 0) {
  324.                         if (retval == 0)
  325.                                 retval = status;
  326.                         break;
  327.                 }
  328.                 buf += status;
  329.                 off += status;
  330.                 count -= status;
  331.                 retval += status;
  332.         }

  333.         mutex_unlock(&at24->lock);

  334.         return retval;
  335. }

  336. static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
  337.                 char *buf, loff_t off, size_t count)
  338. {
  339.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  340.         struct at24_data *at24;

  341.         at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
  342.         return at24_write(at24, buf, off, count);
  343. }

  344. /*-------------------------------------------------------------------------*/

  345. /*
  346. * This lets other kernel code access the eeprom data. For example, it
  347. * might hold a board's Ethernet address, or board-specific calibration
  348. * data generated on the manufacturing floor.
  349. */

  350. static ssize_t at24_macc_read(struct memory_accessor *macc, char *buf,
  351.                          off_t offset, size_t count)
  352. {
  353.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  354.         struct at24_data *at24 = container_of(macc, struct at24_data, macc);

  355.         return at24_read(at24, buf, offset, count);
  356. }

  357. static ssize_t at24_macc_write(struct memory_accessor *macc, const char *buf,
  358.                           off_t offset, size_t count)
  359. {
  360.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  361.         struct at24_data *at24 = container_of(macc, struct at24_data, macc);

  362.         return at24_write(at24, buf, offset, count);
  363. }

  364. static int at24_detect(struct i2c_client *, int kind, struct i2c_board_info *)
  365. {
  366.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  367.         return 0;
  368. }

  369. /*-------------------------------------------------------------------------*/

  370. static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
  371. {
  372.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  373.         struct at24_platform_data chip;
  374.         bool writable;
  375.         bool use_smbus = false;
  376.         struct at24_data *at24;
  377.         int err;
  378.         unsigned i, num_addresses;
  379.         kernel_ulong_t magic;

  380.         if (client->dev.platform_data) {
  381.                 chip = *(struct at24_platform_data *)client->dev.platform_data;
  382.         } else {
  383.                 if (!id->driver_data) {
  384.                         err = -ENODEV;
  385.                         goto err_out;
  386.                 }
  387.                 magic = id->driver_data;
  388.                 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
  389.                 magic >>= AT24_SIZE_BYTELEN;
  390.                 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
  391.                 /*
  392.                  * This is slow, but we can't know all eeproms, so we better
  393.                  * play safe. Specifying custom eeprom-types via platform_data
  394.                  * is recommended anyhow.
  395.                  */
  396.                 chip.page_size = 1;

  397.                 chip.setup = NULL;
  398.                 chip.context = NULL;
  399.         }

  400.         if (!is_power_of_2(chip.byte_len))
  401.                 dev_warn(&client->dev,
  402.                         "byte_len looks suspicious (no power of 2)!\n");
  403.         if (!is_power_of_2(chip.page_size))
  404.                 dev_warn(&client->dev,
  405.                         "page_size looks suspicious (no power of 2)!\n");

  406.         /* Use I2C operations unless we're stuck with SMBus extensions. */
  407.         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  408.                 if (chip.flags & AT24_FLAG_ADDR16) {
  409.                         err = -EPFNOSUPPORT;
  410.                         goto err_out;
  411.                 }
  412.                 if (!i2c_check_functionality(client->adapter,
  413.                                 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  414.                         err = -EPFNOSUPPORT;
  415.                         goto err_out;
  416.                 }
  417.                 use_smbus = true;
  418.         }

  419.         if (chip.flags & AT24_FLAG_TAKE8ADDR)
  420.                 num_addresses = 8;
  421.         else
  422.                 num_addresses =        DIV_ROUND_UP(chip.byte_len,
  423.                         (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);

  424.         at24 = kzalloc(sizeof(struct at24_data) +
  425.                 num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
  426.         if (!at24) {
  427.                 err = -ENOMEM;
  428.                 goto err_out;
  429.         }

  430.         mutex_init(&at24->lock);
  431.         at24->use_smbus = use_smbus;
  432.         at24->chip = chip;
  433.         at24->num_addresses = num_addresses;

  434.         /*
  435.          * Export the EEPROM bytes through sysfs, since that's convenient.
  436.          * By default, only root should see the data (maybe passwords etc)
  437.          */
  438.         at24->bin.attr.name = "eeprom";
  439.         at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
  440.         at24->bin.read = at24_bin_read;
  441.         at24->bin.size = chip.byte_len;

  442.         at24->macc.read = at24_macc_read;

  443.         writable = !(chip.flags & AT24_FLAG_READONLY);
  444.         if (writable) {
  445.                 if (!use_smbus || i2c_check_functionality(client->adapter,
  446.                                 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {

  447.                         unsigned write_max = chip.page_size;

  448.                         at24->macc.write = at24_macc_write;

  449.                         at24->bin.write = at24_bin_write;
  450.                         at24->bin.attr.mode |= S_IWUSR;

  451.                         if (write_max > io_limit)
  452.                                 write_max = io_limit;
  453.                         if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
  454.                                 write_max = I2C_SMBUS_BLOCK_MAX;
  455.                         at24->write_max = write_max;

  456.                         /* buffer (data + address at the beginning) */
  457.                         at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
  458.                         if (!at24->writebuf) {
  459.                                 err = -ENOMEM;
  460.                                 goto err_struct;
  461.                         }
  462.                 } else {
  463.                         dev_warn(&client->dev,
  464.                                 "cannot write due to controller restrictions.");
  465.                 }
  466.         }

  467.         at24->client[0] = client;

  468.         /* use dummy devices for multiple-address chips */
  469.         for (i = 1; i < num_addresses; i++) {
  470.                 at24->client[i] = i2c_new_dummy(client->adapter,
  471.                                         client->addr + i);
  472.                 if (!at24->client[i]) {
  473.                         dev_err(&client->dev, "address 0x%02x unavailable\n",
  474.                                         client->addr + i);
  475.                         err = -EADDRINUSE;
  476.                         goto err_clients;
  477.                 }
  478.         }

  479.         err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
  480.         if (err)
  481.                 goto err_clients;

  482.         i2c_set_clientdata(client, at24);

  483.         dev_info(&client->dev, "%zu byte %s EEPROM %s\n",
  484.                 at24->bin.size, client->name,
  485.                 writable ? "(writable)" : "(read-only)");
  486.         dev_dbg(&client->dev,
  487.                 "page_size %d, num_addresses %d, write_max %d%s\n",
  488.                 chip.page_size, num_addresses,
  489.                 at24->write_max,
  490.                 use_smbus ? ", use_smbus" : "");

  491.         /* export data to kernel code */
  492.         if (chip.setup)
  493.                 chip.setup(&at24->macc, chip.context);

  494.         return 0;

  495. err_clients:
  496.         for (i = 1; i < num_addresses; i++)
  497.                 if (at24->client[i])
  498.                         i2c_unregister_device(at24->client[i]);

  499.         kfree(at24->writebuf);
  500. err_struct:
  501.         kfree(at24);
  502. err_out:
  503.         dev_dbg(&client->dev, "probe error %d\n", err);
  504.         return err;
  505. }

  506. static int __devexit at24_remove(struct i2c_client *client)
  507. {
  508.         dprintk("%s(%d) \r\n", __FUNCTION__, __LINE__);
  509.         struct at24_data *at24;
  510.         int i;

  511.         at24 = i2c_get_clientdata(client);
  512.         sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);

  513.         for (i = 1; i < at24->num_addresses; i++)
  514.                 i2c_unregister_device(at24->client[i]);

  515.         kfree(at24->writebuf);
  516.         kfree(at24);
  517.         i2c_set_clientdata(client, NULL);
  518.         return 0;
  519. }

  520. /*-------------------------------------------------------------------------*/

  521. static struct i2c_driver at24_driver = {
  522.         .driver = {
  523.                 .name = "at24",
  524.                 .owner = THIS_MODULE,
  525.         },
  526.         .probe = at24_probe,
  527.         .remove = __devexit_p(at24_remove),
  528.         .detect = at24_detect,
  529.         .id_table = at24_ids,
  530. };

  531. static int __init at24_init(void)
  532. {
  533.         dprintk("at24_init: \r\n");
  534.        
  535.         io_limit = rounddown_pow_of_two(io_limit);
  536.         int        nadd = i2c_add_driver(&at24_driver);
  537.         dprintk("add = %d\r\n", nadd);
  538.         return nadd;
  539. }
  540. module_init(at24_init);

  541. static void __exit at24_exit(void)
  542. {
  543.         dprintk("at24_exit\r\n");
  544.        
  545.         i2c_del_driver(&at24_driver);
  546. }
  547. module_exit(at24_exit);

  548. MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
  549. MODULE_AUTHOR("David Brownell and Wolfram Sang");
  550. MODULE_LICENSE("GPL");
复制代码

论坛徽章:
0
8 [报告]
发表于 2011-02-22 09:46 |只看该作者
谢谢关注;
在同样一块板,在 Bootloader 中测试,是正常的,硬件应该没有问题。

我感觉这个 at2 ...
chengdot 发表于 2011-02-22 09:24



还有一点,原代码中,到我描述的 i2c_detect() 为止,还没有涉及到真正的硬件操作,
那句 if ((!detect) || (!address)) { return 0; } 纯粹由于代码不完成造成。

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
9 [报告]
发表于 2011-02-22 09:52 |只看该作者
本帖最后由 T-Bagwell 于 2011-02-22 09:58 编辑

如果是linux自带的代码,不用验证了,没问题
我要问的是你写的那部分代码
能否详细说明一下
I2C设备没这么麻烦吧
如果probe不上,一般就是设备没供电造成的,导致控制器挂载不上设备,再就是根本就没有这个设备

这样,用示波器在你挂载I2C总线上的设备的供电pin脚上面测量一下,看看是否供电正常,再看看clock是否正常
如果没猜错,供电应该不正常

论坛徽章:
0
10 [报告]
发表于 2011-02-22 10:03 |只看该作者
如果是linux自带的代码,不用验证了,没问题
我要问的是你写的那部分代码
能否详细说明一下
I2C设备没这 ...
T-Bagwell 发表于 2011-02-22 09:52



驱动里,我并没写什么代码;
我是在应用程序里(见下)发现设备加载不成功,才去看的驱动,

  1.         fd= open("/dev/i2c-0",  O_RDWR);
  2.         if (fd < 0) {
  3.                 printf( "Fail to open AT24C32!\n" );
  4.                 exit(1);
  5.         }
  6.         res = ioctl(fd, I2C_TENBIT, 0);
  7.         printf("I2C_TENBIT(0) = %d\n", res);
  8.         res = ioctl(fd, I2C_SLAVE_FORCE, 0x50);        //        AT24C02
  9.         printf("I2C_SLAVE_FORCE(0x51) = %d\n", res);
  10.         res = ioctl(fd, I2C_BUS_MODE, 1);
  11.         printf("I2C_BUS_MODE(1) = %d\n", res);
  12.         res = ioctl(fd, I2C_TIMEOUT, 100);
  13.         printf("I2C_TIMEOUT(100) = %d\n", res);
  14.         res = ioctl(fd, I2C_RETRIES, 2);
  15.         printf("I2C_RETRIES(2) = %d\n", res);

  16.         //        read
  17.         for (i = 2; i < 10; i++) {
  18.                 memset(buf, 0xFF, sizeof(buf));
  19.                 buf[0] = i;
  20.                 res = write(fd, buf, 1); // 失败, 返回-1, dmesg: s3c2440-i2c s3c2440-i2c: cannot get bus (error -110)
  21.                 printf("set read address to 0x%02x, result = %d\n", buf[0], res);
  22.                 if (res != 1) {
  23.                         continue;
  24.                 }
  25.                 usleep(1000);
  26.                 res = read(fd, buf + 1, 1);
  27.                 printf("read data from address 0x%02x, result = %d, value = 0x%02x\n", buf[0], res, buf[1]);
  28.         }
复制代码
你说 linux 自带的代码没问题,那么请问 probe 根本没机会调用,这是怎么回事?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP