1 # SPDX-License-Identifier: GPL-2.0
2 # (C) Copyright 2023, Advanced Micro Devices, Inc.
9 Note: This test relies on boardenv_* containing configuration values to define
10 the i2c device info including the bus list and eeprom address/value. This test
11 will be automatically skipped without this.
15 # Setup env__i2c_device_test to set the i2c bus list and probe_all boolean
16 # parameter. For i2c_probe_all_buses case, if probe_all parameter is set to
17 # False then it probes all the buses listed in bus_list instead of probing all
18 # the buses available.
19 env__i2c_device_test = {
20 'bus_list': [0, 2, 5, 12, 16, 18],
24 # Setup env__i2c_eeprom_device_test to set the i2c bus number, eeprom address
25 # and configured value for i2c_eeprom test case. Test will be skipped if
26 # env__i2c_eeprom_device_test is not set
27 env__i2c_eeprom_device_test = {
30 'eeprom_val': '30 31',
34 def get_i2c_test_env(u_boot_console):
35 f = u_boot_console.config.env.get("env__i2c_device_test", None)
37 pytest.skip("No I2C device to test!")
39 bus_list = f.get("bus_list", None)
41 pytest.skip("I2C bus list is not provided!")
42 probe_all = f.get("probe_all", False)
43 return bus_list, probe_all
45 @pytest.mark.buildconfigspec("cmd_i2c")
46 def test_i2c_bus(u_boot_console):
47 bus_list, probe = get_i2c_test_env(u_boot_console)
48 bus = random.choice(bus_list)
49 expected_response = f"Bus {bus}:"
50 response = u_boot_console.run_command("i2c bus")
51 assert expected_response in response
53 @pytest.mark.buildconfigspec("cmd_i2c")
54 def test_i2c_dev(u_boot_console):
55 bus_list, probe = get_i2c_test_env(u_boot_console)
56 expected_response = "Current bus is"
57 response = u_boot_console.run_command("i2c dev")
58 assert expected_response in response
60 @pytest.mark.buildconfigspec("cmd_i2c")
61 def test_i2c_probe(u_boot_console):
62 bus_list, probe = get_i2c_test_env(u_boot_console)
63 bus = random.choice(bus_list)
64 expected_response = f"Setting bus to {bus}"
65 response = u_boot_console.run_command(f"i2c dev {bus}")
66 assert expected_response in response
67 expected_response = "Valid chip addresses:"
68 response = u_boot_console.run_command("i2c probe")
69 assert expected_response in response
71 @pytest.mark.buildconfigspec("cmd_i2c")
72 def test_i2c_eeprom(u_boot_console):
73 f = u_boot_console.config.env.get("env__i2c_eeprom_device_test", None)
75 pytest.skip("No I2C eeprom to test!")
79 pytest.fail("No bus specified via env__i2c_eeprom_device_test!")
81 addr = f.get("eeprom_addr", -1)
83 pytest.fail("No eeprom address specified via env__i2c_eeprom_device_test!")
85 value = f.get("eeprom_val")
88 "No eeprom configured value provided via env__i2c_eeprom_device_test!"
91 # Enable i2c mux bridge
92 u_boot_console.run_command("i2c dev %x" % bus)
93 u_boot_console.run_command("i2c probe")
94 output = u_boot_console.run_command("i2c md %x 0 5" % addr)
95 assert value in output
97 @pytest.mark.buildconfigspec("cmd_i2c")
98 def test_i2c_probe_all_buses(u_boot_console):
99 bus_list, probe = get_i2c_test_env(u_boot_console)
100 bus = random.choice(bus_list)
101 expected_response = f"Bus {bus}:"
102 response = u_boot_console.run_command("i2c bus")
103 assert expected_response in response
105 # Get all the bus list
107 buses = re.findall("Bus (.+?):", response)
108 bus_list = [int(x) for x in buses]
111 expected_response = f"Setting bus to {dev}"
112 response = u_boot_console.run_command(f"i2c dev {dev}")
113 assert expected_response in response
114 expected_response = "Valid chip addresses:"
115 response = u_boot_console.run_command("i2c probe")
116 assert expected_response in response