1# SPDX-License-Identifier: GPL-2.0 2# (C) Copyright 2023, Advanced Micro Devices, Inc. 3 4import pytest 5import random 6import re 7 8""" 9Note: This test relies on boardenv_* containing configuration values to define 10the i2c device info including the bus list and eeprom address/value. This test 11will be automatically skipped without this. 12 13For example: 14 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. 19env__i2c_device_test = { 20 'bus_list': [0, 2, 5, 12, 16, 18], 21 'probe_all': False, 22} 23 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 27env__i2c_eeprom_device_test = { 28 'bus': 3, 29 'eeprom_addr': 0x54, 30 'eeprom_val': '30 31', 31} 32""" 33 34def get_i2c_test_env(u_boot_console): 35 f = u_boot_console.config.env.get("env__i2c_device_test", None) 36 if not f: 37 pytest.skip("No I2C device to test!") 38 else: 39 bus_list = f.get("bus_list", None) 40 if not bus_list: 41 pytest.skip("I2C bus list is not provided!") 42 probe_all = f.get("probe_all", False) 43 return bus_list, probe_all 44 45@pytest.mark.buildconfigspec("cmd_i2c") 46def 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 52 53@pytest.mark.buildconfigspec("cmd_i2c") 54def 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 59 60@pytest.mark.buildconfigspec("cmd_i2c") 61def 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 70 71@pytest.mark.buildconfigspec("cmd_i2c") 72def test_i2c_eeprom(u_boot_console): 73 f = u_boot_console.config.env.get("env__i2c_eeprom_device_test", None) 74 if not f: 75 pytest.skip("No I2C eeprom to test!") 76 77 bus = f.get("bus", 0) 78 if bus < 0: 79 pytest.fail("No bus specified via env__i2c_eeprom_device_test!") 80 81 addr = f.get("eeprom_addr", -1) 82 if addr < 0: 83 pytest.fail("No eeprom address specified via env__i2c_eeprom_device_test!") 84 85 value = f.get("eeprom_val") 86 if not value: 87 pytest.fail( 88 "No eeprom configured value provided via env__i2c_eeprom_device_test!" 89 ) 90 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 96 97@pytest.mark.buildconfigspec("cmd_i2c") 98def 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 104 105 # Get all the bus list 106 if probe: 107 buses = re.findall("Bus (.+?):", response) 108 bus_list = [int(x) for x in buses] 109 110 for dev in bus_list: 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 117