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