blob: 40f1f26b671ed40f89acf5ebbb4a40340d0480b3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassecc2ed52014-12-10 08:55:55 -07002/*
3 * Copyright (C) 2013 Google, Inc
4 *
Simon Glassecc2ed52014-12-10 08:55:55 -07005 * Note: Test coverage does not include 10-bit addressing
6 */
7
Simon Glassecc2ed52014-12-10 08:55:55 -07008#include <dm.h>
9#include <fdtdec.h>
10#include <i2c.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050011#include <asm/state.h>
12#include <asm/test.h>
Simon Glassecc2ed52014-12-10 08:55:55 -070013#include <dm/device-internal.h>
14#include <dm/test.h>
15#include <dm/uclass-internal.h>
Simon Glassecc2ed52014-12-10 08:55:55 -070016#include <dm/util.h>
Robert Beckett22e93512019-10-28 17:44:58 +000017#include <hexdump.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060018#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glassecc2ed52014-12-10 08:55:55 -070020
21static const int busnum;
22static const int chip = 0x2c;
23
24/* Test that we can find buses and chips */
Joe Hershbergere721b882015-05-20 14:27:27 -050025static int dm_test_i2c_find(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070026{
27 struct udevice *bus, *dev;
28 const int no_chip = 0x10;
29
Simon Glassecc2ed52014-12-10 08:55:55 -070030 /*
Simon Glass91195482016-07-05 17:10:10 -060031 * The post_bind() method will bind devices to chip selects. Check
32 * this then remove the emulation and the slave device.
Simon Glassecc2ed52014-12-10 08:55:55 -070033 */
34 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070035 ut_assertok(dm_i2c_probe(bus, chip, 0, &dev));
Simon Glass031a6502018-11-18 08:14:34 -070036 ut_asserteq(-ENOENT, dm_i2c_probe(bus, no_chip, 0, &dev));
Simon Glassecc2ed52014-12-10 08:55:55 -070037 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus));
38
39 return 0;
40}
Simon Glass725c4382024-08-22 07:57:48 -060041DM_TEST(dm_test_i2c_find, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassecc2ed52014-12-10 08:55:55 -070042
Joe Hershbergere721b882015-05-20 14:27:27 -050043static int dm_test_i2c_read_write(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070044{
45 struct udevice *bus, *dev;
46 uint8_t buf[5];
47
48 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass25ab4b02015-01-25 08:26:55 -070049 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070050 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -060051 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070052 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
53 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -060054 ut_asserteq_mem(buf, "\0\0AB\0", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -070055
56 return 0;
57}
Simon Glass725c4382024-08-22 07:57:48 -060058DM_TEST(dm_test_i2c_read_write, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassecc2ed52014-12-10 08:55:55 -070059
Joe Hershbergere721b882015-05-20 14:27:27 -050060static int dm_test_i2c_speed(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070061{
62 struct udevice *bus, *dev;
63 uint8_t buf[5];
64
65 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass182bf922015-04-20 12:37:15 -060066
67 /* Use test mode so we create the required errors for invalid speeds */
68 sandbox_i2c_set_test_mode(bus, true);
Simon Glass25ab4b02015-01-25 08:26:55 -070069 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassca88b9b2015-02-05 21:41:32 -070070 ut_assertok(dm_i2c_set_bus_speed(bus, 100000));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070071 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassca88b9b2015-02-05 21:41:32 -070072 ut_assertok(dm_i2c_set_bus_speed(bus, 400000));
73 ut_asserteq(400000, dm_i2c_get_bus_speed(bus));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070074 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
75 ut_asserteq(-EINVAL, dm_i2c_write(dev, 0, buf, 5));
Simon Glass182bf922015-04-20 12:37:15 -060076 sandbox_i2c_set_test_mode(bus, false);
Simon Glassecc2ed52014-12-10 08:55:55 -070077
78 return 0;
79}
Simon Glass725c4382024-08-22 07:57:48 -060080DM_TEST(dm_test_i2c_speed, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassecc2ed52014-12-10 08:55:55 -070081
Joe Hershbergere721b882015-05-20 14:27:27 -050082static int dm_test_i2c_offset_len(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -070083{
84 struct udevice *bus, *dev;
85 uint8_t buf[5];
86
87 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass25ab4b02015-01-25 08:26:55 -070088 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassecc2ed52014-12-10 08:55:55 -070089 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glassf9a4c2d2015-01-12 18:02:07 -070090 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassecc2ed52014-12-10 08:55:55 -070091
92 /* This is not supported by the uclass */
93 ut_asserteq(-EINVAL, i2c_set_chip_offset_len(dev, 5));
94
95 return 0;
96}
Simon Glass725c4382024-08-22 07:57:48 -060097DM_TEST(dm_test_i2c_offset_len, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassecc2ed52014-12-10 08:55:55 -070098
Joe Hershbergere721b882015-05-20 14:27:27 -050099static int dm_test_i2c_probe_empty(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -0700100{
101 struct udevice *bus, *dev;
102
103 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass182bf922015-04-20 12:37:15 -0600104
105 /* Use test mode so that this chip address will always probe */
106 sandbox_i2c_set_test_mode(bus, true);
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700107 ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev));
Simon Glass182bf922015-04-20 12:37:15 -0600108 sandbox_i2c_set_test_mode(bus, false);
Simon Glassecc2ed52014-12-10 08:55:55 -0700109
110 return 0;
111}
Simon Glass725c4382024-08-22 07:57:48 -0600112DM_TEST(dm_test_i2c_probe_empty, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassecc2ed52014-12-10 08:55:55 -0700113
Joe Hershbergere721b882015-05-20 14:27:27 -0500114static int dm_test_i2c_bytewise(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -0700115{
116 struct udevice *bus, *dev;
117 struct udevice *eeprom;
118 uint8_t buf[5];
119
120 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass25ab4b02015-01-25 08:26:55 -0700121 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700122 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600123 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700124
125 /* Tell the EEPROM to only read/write one register at a time */
Michal Suchanekc726fc02022-10-12 21:57:59 +0200126 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700127 ut_assertnonnull(eeprom);
128 sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_SINGLE_BYTE);
129
130 /* Now we only get the first byte - the rest will be 0xff */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700131 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600132 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700133
134 /* If we do a separate transaction for each byte, it works */
135 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700136 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600137 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700138
139 /* This will only write A */
140 ut_assertok(i2c_set_chip_flags(dev, 0));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700141 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
142 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600143 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700144
145 /* Check that the B was ignored */
146 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_RD_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700147 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600148 ut_asserteq_mem(buf, "\0\0A\0\0\0", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700149
150 /* Now write it again with the new flags, it should work */
151 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700152 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
153 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600154 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700155
156 ut_assertok(i2c_set_chip_flags(dev, DM_I2C_CHIP_WR_ADDRESS |
157 DM_I2C_CHIP_RD_ADDRESS));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700158 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassf91f3662020-05-10 12:52:45 -0600159 ut_asserteq_mem(buf, "\0\0AB\0\0", sizeof(buf));
Simon Glassecc2ed52014-12-10 08:55:55 -0700160
161 /* Restore defaults */
162 sandbox_i2c_eeprom_set_test_mode(eeprom, SIE_TEST_MODE_NONE);
163 ut_assertok(i2c_set_chip_flags(dev, 0));
164
165 return 0;
166}
Simon Glass725c4382024-08-22 07:57:48 -0600167DM_TEST(dm_test_i2c_bytewise, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassecc2ed52014-12-10 08:55:55 -0700168
Joe Hershbergere721b882015-05-20 14:27:27 -0500169static int dm_test_i2c_offset(struct unit_test_state *uts)
Simon Glassecc2ed52014-12-10 08:55:55 -0700170{
171 struct udevice *eeprom;
172 struct udevice *dev;
173 uint8_t buf[5];
174
Simon Glass25ab4b02015-01-25 08:26:55 -0700175 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
Simon Glassecc2ed52014-12-10 08:55:55 -0700176
177 /* Do a transfer so we can find the emulator */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700178 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Michal Suchanekc726fc02022-10-12 21:57:59 +0200179 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700180
181 /* Offset length 0 */
182 sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
183 ut_assertok(i2c_set_chip_offset_len(dev, 0));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700184 ut_assertok(dm_i2c_write(dev, 10 /* ignored */, (uint8_t *)"AB", 2));
185 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Robert Beckett22e93512019-10-28 17:44:58 +0000186 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
187 ut_asserteq(0, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700188
189 /* Offset length 1 */
190 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
191 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700192 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
Robert Beckett22e93512019-10-28 17:44:58 +0000193 ut_asserteq(2, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700194 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Robert Beckett22e93512019-10-28 17:44:58 +0000195 ut_asserteq_mem("ABAB\0", buf, sizeof(buf));
196 ut_asserteq(0, sanbox_i2c_eeprom_get_prev_offset(eeprom));
197
198 /* Offset length 2 boundary - check model wrapping */
199 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
200 ut_assertok(i2c_set_chip_offset_len(dev, 2));
201 ut_assertok(dm_i2c_write(dev, 0xFF, (uint8_t *)"A", 1));
202 ut_asserteq(0xFF, sanbox_i2c_eeprom_get_prev_offset(eeprom));
203 ut_assertok(dm_i2c_write(dev, 0x100, (uint8_t *)"B", 1));
204 ut_asserteq(0x100, sanbox_i2c_eeprom_get_prev_offset(eeprom));
205 ut_assertok(dm_i2c_write(dev, 0x101, (uint8_t *)"C", 1));
206 ut_asserteq(0x101, sanbox_i2c_eeprom_get_prev_offset(eeprom));
207 ut_assertok(dm_i2c_read(dev, 0xFF, buf, 5));
208 ut_asserteq_mem("ABCAB", buf, sizeof(buf));
209 ut_asserteq(0xFF, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700210
211 /* Offset length 2 */
212 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
213 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Robert Beckett22e93512019-10-28 17:44:58 +0000214 ut_assertok(dm_i2c_write(dev, 0x2020, (uint8_t *)"AB", 2));
215 ut_assertok(dm_i2c_read(dev, 0x2020, buf, 5));
216 ut_asserteq_mem("AB\0\0\0", buf, sizeof(buf));
217 ut_asserteq(0x2020, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700218
219 /* Offset length 3 */
Robert Beckett22e93512019-10-28 17:44:58 +0000220 sandbox_i2c_eeprom_set_offset_len(eeprom, 3);
221 ut_assertok(i2c_set_chip_offset_len(dev, 3));
222 ut_assertok(dm_i2c_write(dev, 0x303030, (uint8_t *)"AB", 2));
223 ut_assertok(dm_i2c_read(dev, 0x303030, buf, 5));
224 ut_asserteq_mem("AB\0\0\0", buf, sizeof(buf));
225 ut_asserteq(0x303030, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700226
227 /* Offset length 4 */
Robert Beckett22e93512019-10-28 17:44:58 +0000228 sandbox_i2c_eeprom_set_offset_len(eeprom, 4);
229 ut_assertok(i2c_set_chip_offset_len(dev, 4));
230 ut_assertok(dm_i2c_write(dev, 0x40404040, (uint8_t *)"AB", 2));
231 ut_assertok(dm_i2c_read(dev, 0x40404040, buf, 5));
232 ut_asserteq_mem("AB\0\0\0", buf, sizeof(buf));
233 ut_asserteq(0x40404040, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glassecc2ed52014-12-10 08:55:55 -0700234
235 /* Restore defaults */
236 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
237
238 return 0;
239}
Simon Glass725c4382024-08-22 07:57:48 -0600240DM_TEST(dm_test_i2c_offset, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Robert Beckett951674a2019-10-28 17:44:59 +0000241
242static int dm_test_i2c_addr_offset(struct unit_test_state *uts)
243{
244 struct udevice *eeprom;
245 struct udevice *dev;
246 u8 buf[5];
247
248 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
249
250 /* Do a transfer so we can find the emulator */
251 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Michal Suchanekc726fc02022-10-12 21:57:59 +0200252 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Robert Beckett951674a2019-10-28 17:44:59 +0000253
254 /* Offset length 0 */
255 sandbox_i2c_eeprom_set_offset_len(eeprom, 0);
256 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
257 ut_assertok(i2c_set_chip_offset_len(dev, 0));
258 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
259 ut_assertok(dm_i2c_write(dev, 0x3, (uint8_t *)"AB", 2));
260 ut_assertok(dm_i2c_read(dev, 0x3, buf, 5));
261 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
262 ut_asserteq(0x3, sanbox_i2c_eeprom_get_prev_offset(eeprom));
263 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
264
265 /* Offset length 1 */
266 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
267 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
268 ut_assertok(i2c_set_chip_offset_len(dev, 1));
269 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
270 ut_assertok(dm_i2c_write(dev, 0x310, (uint8_t *)"AB", 2));
271 ut_assertok(dm_i2c_read(dev, 0x310, buf, 5));
272 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
273 ut_asserteq(0x310, sanbox_i2c_eeprom_get_prev_offset(eeprom));
274 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
275
276 /* Offset length 2 */
277 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
278 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
279 ut_assertok(i2c_set_chip_offset_len(dev, 2));
280 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
281 ut_assertok(dm_i2c_write(dev, 0x32020, (uint8_t *)"AB", 2));
282 ut_assertok(dm_i2c_read(dev, 0x32020, buf, 5));
283 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
284 ut_asserteq(0x32020, sanbox_i2c_eeprom_get_prev_offset(eeprom));
285 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
286
287 /* Offset length 3 */
288 sandbox_i2c_eeprom_set_offset_len(eeprom, 3);
289 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0x3);
290 ut_assertok(i2c_set_chip_offset_len(dev, 3));
291 ut_assertok(i2c_set_chip_addr_offset_mask(dev, 0x3));
292 ut_assertok(dm_i2c_write(dev, 0x3303030, (uint8_t *)"AB", 2));
293 ut_assertok(dm_i2c_read(dev, 0x3303030, buf, 5));
294 ut_asserteq_mem("AB\0\0\0\0", buf, sizeof(buf));
295 ut_asserteq(0x3303030, sanbox_i2c_eeprom_get_prev_offset(eeprom));
296 ut_asserteq(chip | 0x3, sanbox_i2c_eeprom_get_prev_addr(eeprom));
297
298 /* Restore defaults */
299 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
300 sandbox_i2c_eeprom_set_chip_addr_offset_mask(eeprom, 0);
301
302 return 0;
303}
Simon Glass725c4382024-08-22 07:57:48 -0600304DM_TEST(dm_test_i2c_addr_offset, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Sebastian Reichel2aefa6e2021-07-15 17:39:59 +0200305
306static int dm_test_i2c_reg_clrset(struct unit_test_state *uts)
307{
308 struct udevice *eeprom;
309 struct udevice *dev;
310 u8 buf[5];
311
312 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
313
314 /* Do a transfer so we can find the emulator */
315 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Michal Suchanekc726fc02022-10-12 21:57:59 +0200316 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Sebastian Reichel2aefa6e2021-07-15 17:39:59 +0200317
318 /* Dummy data for the test */
319 ut_assertok(dm_i2c_write(dev, 0, "\xff\x00\xff\x00\x10", 5));
320
321 /* Do some clrset tests */
322 ut_assertok(dm_i2c_reg_clrset(dev, 0, 0xff, 0x10));
323 ut_assertok(dm_i2c_reg_clrset(dev, 1, 0x00, 0x11));
324 ut_assertok(dm_i2c_reg_clrset(dev, 2, 0xed, 0x00));
325 ut_assertok(dm_i2c_reg_clrset(dev, 3, 0xff, 0x13));
326 ut_assertok(dm_i2c_reg_clrset(dev, 4, 0x00, 0x14));
327
328 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
329 ut_asserteq_mem("\x10\x11\x12\x13\x14", buf, sizeof(buf));
330
331 return 0;
332}
Simon Glass725c4382024-08-22 07:57:48 -0600333DM_TEST(dm_test_i2c_reg_clrset, UTF_SCAN_PDATA | UTF_SCAN_FDT);