i2c.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * I2C test
  8. *
  9. * For verifying the I2C bus, a full I2C bus scanning is performed.
  10. *
  11. * #ifdef CONFIG_SYS_POST_I2C_ADDRS
  12. * The test is considered as passed if all the devices and only the devices
  13. * in the list are found.
  14. * #ifdef CONFIG_SYS_POST_I2C_IGNORES
  15. * Ignore devices listed in CONFIG_SYS_POST_I2C_IGNORES. These devices
  16. * are optional or not vital to board functionality.
  17. * #endif
  18. * #else [ ! CONFIG_SYS_POST_I2C_ADDRS ]
  19. * The test is considered as passed if any I2C device is found.
  20. * #endif
  21. */
  22. #include <common.h>
  23. #include <log.h>
  24. #include <post.h>
  25. #include <i2c.h>
  26. #if CONFIG_POST & CONFIG_SYS_POST_I2C
  27. static int i2c_ignore_device(unsigned int chip)
  28. {
  29. #ifdef CONFIG_SYS_POST_I2C_IGNORES
  30. const unsigned char i2c_ignore_list[] = CONFIG_SYS_POST_I2C_IGNORES;
  31. int i;
  32. for (i = 0; i < sizeof(i2c_ignore_list); i++)
  33. if (i2c_ignore_list[i] == chip)
  34. return 1;
  35. #endif
  36. return 0;
  37. }
  38. int i2c_post_test (int flags)
  39. {
  40. unsigned int i;
  41. #ifndef CONFIG_SYS_POST_I2C_ADDRS
  42. /* Start at address 1, address 0 is the general call address */
  43. for (i = 1; i < 128; i++) {
  44. if (i2c_ignore_device(i))
  45. continue;
  46. if (i2c_probe (i) == 0)
  47. return 0;
  48. }
  49. /* No devices found */
  50. return -1;
  51. #else
  52. unsigned int ret = 0;
  53. int j;
  54. unsigned char i2c_addr_list[] = CONFIG_SYS_POST_I2C_ADDRS;
  55. /* Start at address 1, address 0 is the general call address */
  56. for (i = 1; i < 128; i++) {
  57. if (i2c_ignore_device(i))
  58. continue;
  59. if (i2c_probe(i) != 0)
  60. continue;
  61. for (j = 0; j < sizeof(i2c_addr_list); ++j) {
  62. if (i == i2c_addr_list[j]) {
  63. i2c_addr_list[j] = 0xff;
  64. break;
  65. }
  66. }
  67. if (j == sizeof(i2c_addr_list)) {
  68. ret = -1;
  69. post_log("I2C: addr %02x not expected\n", i);
  70. }
  71. }
  72. for (i = 0; i < sizeof(i2c_addr_list); ++i) {
  73. if (i2c_addr_list[i] == 0xff)
  74. continue;
  75. if (i2c_ignore_device(i2c_addr_list[i]))
  76. continue;
  77. post_log("I2C: addr %02x did not respond\n", i2c_addr_list[i]);
  78. ret = -1;
  79. }
  80. return ret;
  81. #endif
  82. }
  83. #endif /* CONFIG_POST & CONFIG_SYS_POST_I2C */