blob: 69b11930ce7d00cb8ece89025baeda1dd55fd029 [file] [log] [blame]
Love Kumarc010f122024-01-02 12:17:07 +05301# 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
Simon Glassddba5202025-02-09 09:07:14 -070034def get_i2c_test_env(ubman):
35 f = ubman.config.env.get("env__i2c_device_test", None)
Love Kumarc010f122024-01-02 12:17:07 +053036 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")
Simon Glassddba5202025-02-09 09:07:14 -070046def test_i2c_bus(ubman):
47 bus_list, probe = get_i2c_test_env(ubman)
Love Kumarc010f122024-01-02 12:17:07 +053048 bus = random.choice(bus_list)
49 expected_response = f"Bus {bus}:"
Simon Glassddba5202025-02-09 09:07:14 -070050 response = ubman.run_command("i2c bus")
Love Kumarc010f122024-01-02 12:17:07 +053051 assert expected_response in response
52
53@pytest.mark.buildconfigspec("cmd_i2c")
Simon Glassddba5202025-02-09 09:07:14 -070054def test_i2c_dev(ubman):
55 bus_list, probe = get_i2c_test_env(ubman)
Love Kumarc010f122024-01-02 12:17:07 +053056 expected_response = "Current bus is"
Simon Glassddba5202025-02-09 09:07:14 -070057 response = ubman.run_command("i2c dev")
Love Kumarc010f122024-01-02 12:17:07 +053058 assert expected_response in response
59
60@pytest.mark.buildconfigspec("cmd_i2c")
Simon Glassddba5202025-02-09 09:07:14 -070061def test_i2c_probe(ubman):
62 bus_list, probe = get_i2c_test_env(ubman)
Love Kumarc010f122024-01-02 12:17:07 +053063 bus = random.choice(bus_list)
64 expected_response = f"Setting bus to {bus}"
Simon Glassddba5202025-02-09 09:07:14 -070065 response = ubman.run_command(f"i2c dev {bus}")
Love Kumarc010f122024-01-02 12:17:07 +053066 assert expected_response in response
67 expected_response = "Valid chip addresses:"
Simon Glassddba5202025-02-09 09:07:14 -070068 response = ubman.run_command("i2c probe")
Love Kumarc010f122024-01-02 12:17:07 +053069 assert expected_response in response
70
71@pytest.mark.buildconfigspec("cmd_i2c")
Simon Glassddba5202025-02-09 09:07:14 -070072def test_i2c_eeprom(ubman):
73 f = ubman.config.env.get("env__i2c_eeprom_device_test", None)
Love Kumarc010f122024-01-02 12:17:07 +053074 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
Simon Glassddba5202025-02-09 09:07:14 -070092 ubman.run_command("i2c dev %x" % bus)
93 ubman.run_command("i2c probe")
94 output = ubman.run_command("i2c md %x 0 5" % addr)
Love Kumarc010f122024-01-02 12:17:07 +053095 assert value in output
96
97@pytest.mark.buildconfigspec("cmd_i2c")
Simon Glassddba5202025-02-09 09:07:14 -070098def test_i2c_probe_all_buses(ubman):
99 bus_list, probe = get_i2c_test_env(ubman)
Love Kumarc010f122024-01-02 12:17:07 +0530100 bus = random.choice(bus_list)
101 expected_response = f"Bus {bus}:"
Simon Glassddba5202025-02-09 09:07:14 -0700102 response = ubman.run_command("i2c bus")
Love Kumarc010f122024-01-02 12:17:07 +0530103 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}"
Simon Glassddba5202025-02-09 09:07:14 -0700112 response = ubman.run_command(f"i2c dev {dev}")
Love Kumarc010f122024-01-02 12:17:07 +0530113 assert expected_response in response
114 expected_response = "Valid chip addresses:"
Simon Glassddba5202025-02-09 09:07:14 -0700115 response = ubman.run_command("i2c probe")
Love Kumarc010f122024-01-02 12:17:07 +0530116 assert expected_response in response