blob: e9cf9f7819a73cadb9940e86c2289eff937adadb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7df766e2014-12-10 08:55:55 -07002/*
3 * Copyright (C) 2013 Google, Inc
4 *
Simon Glass7df766e2014-12-10 08:55:55 -07005 * Note: Test coverage does not include 10-bit addressing
6 */
7
Simon Glass7df766e2014-12-10 08:55:55 -07008#include <dm.h>
9#include <fdtdec.h>
10#include <i2c.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050011#include <asm/state.h>
12#include <asm/test.h>
Simon Glass7df766e2014-12-10 08:55:55 -070013#include <dm/device-internal.h>
14#include <dm/test.h>
15#include <dm/uclass-internal.h>
Simon Glass7df766e2014-12-10 08:55:55 -070016#include <dm/util.h>
Robert Beckett1fe8a492019-10-28 17:44:58 +000017#include <hexdump.h>
Simon Glass75c4d412020-07-19 10:15:37 -060018#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glass7df766e2014-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 Hershberger3a77be52015-05-20 14:27:27 -050025static int dm_test_i2c_find(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -070026{
27 struct udevice *bus, *dev;
28 const int no_chip = 0x10;
29
Simon Glass7df766e2014-12-10 08:55:55 -070030 /*
Simon Glass18230342016-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 Glass7df766e2014-12-10 08:55:55 -070033 */
34 ut_assertok(uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus));
Simon Glass7d722762015-01-12 18:02:07 -070035 ut_assertok(dm_i2c_probe(bus, chip, 0, &dev));
Simon Glass17b56f62018-11-18 08:14:34 -070036 ut_asserteq(-ENOENT, dm_i2c_probe(bus, no_chip, 0, &dev));
Simon Glass7df766e2014-12-10 08:55:55 -070037 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_I2C, 1, &bus));
38
39 return 0;
40}
Simon Glass974dccd2020-07-28 19:41:12 -060041DM_TEST(dm_test_i2c_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070042
Joe Hershberger3a77be52015-05-20 14:27:27 -050043static int dm_test_i2c_read_write(struct unit_test_state *uts)
Simon Glass7df766e2014-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 Glassa2723ae2015-01-25 08:26:55 -070049 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glass7d722762015-01-12 18:02:07 -070050 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -060051 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glass7d722762015-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 Glassa3186e62020-05-10 12:52:45 -060054 ut_asserteq_mem(buf, "\0\0AB\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -070055
56 return 0;
57}
Simon Glass974dccd2020-07-28 19:41:12 -060058DM_TEST(dm_test_i2c_read_write, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070059
Joe Hershberger3a77be52015-05-20 14:27:27 -050060static int dm_test_i2c_speed(struct unit_test_state *uts)
Simon Glass7df766e2014-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 Glass4c70ed92015-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 Glassa2723ae2015-01-25 08:26:55 -070069 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glasse4e8ff22015-02-05 21:41:32 -070070 ut_assertok(dm_i2c_set_bus_speed(bus, 100000));
Simon Glass7d722762015-01-12 18:02:07 -070071 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glasse4e8ff22015-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 Glass7d722762015-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 Glass4c70ed92015-04-20 12:37:15 -060076 sandbox_i2c_set_test_mode(bus, false);
Simon Glass7df766e2014-12-10 08:55:55 -070077
78 return 0;
79}
Simon Glass974dccd2020-07-28 19:41:12 -060080DM_TEST(dm_test_i2c_speed, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070081
Joe Hershberger3a77be52015-05-20 14:27:27 -050082static int dm_test_i2c_offset_len(struct unit_test_state *uts)
Simon Glass7df766e2014-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 Glassa2723ae2015-01-25 08:26:55 -070088 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glass7df766e2014-12-10 08:55:55 -070089 ut_assertok(i2c_set_chip_offset_len(dev, 1));
Simon Glass7d722762015-01-12 18:02:07 -070090 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glass7df766e2014-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 Glass974dccd2020-07-28 19:41:12 -060097DM_TEST(dm_test_i2c_offset_len, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -070098
Joe Hershberger3a77be52015-05-20 14:27:27 -050099static int dm_test_i2c_probe_empty(struct unit_test_state *uts)
Simon Glass7df766e2014-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 Glass4c70ed92015-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 Glass7d722762015-01-12 18:02:07 -0700107 ut_assertok(dm_i2c_probe(bus, SANDBOX_I2C_TEST_ADDR, 0, &dev));
Simon Glass4c70ed92015-04-20 12:37:15 -0600108 sandbox_i2c_set_test_mode(bus, false);
Simon Glass7df766e2014-12-10 08:55:55 -0700109
110 return 0;
111}
Simon Glass974dccd2020-07-28 19:41:12 -0600112DM_TEST(dm_test_i2c_probe_empty, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -0700113
Joe Hershberger3a77be52015-05-20 14:27:27 -0500114static int dm_test_i2c_bytewise(struct unit_test_state *uts)
Simon Glass7df766e2014-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 Glassa2723ae2015-01-25 08:26:55 -0700121 ut_assertok(i2c_get_chip(bus, chip, 1, &dev));
Simon Glass7d722762015-01-12 18:02:07 -0700122 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600123 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700124
125 /* Tell the EEPROM to only read/write one register at a time */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200126 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Simon Glass7df766e2014-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 Glass7d722762015-01-12 18:02:07 -0700131 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600132 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glass7df766e2014-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 Glass7d722762015-01-12 18:02:07 -0700136 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600137 ut_asserteq_mem(buf, "\0\0\0\0\0", sizeof(buf));
Simon Glass7df766e2014-12-10 08:55:55 -0700138
139 /* This will only write A */
140 ut_assertok(i2c_set_chip_flags(dev, 0));
Simon Glass7d722762015-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 Glassa3186e62020-05-10 12:52:45 -0600143 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glass7df766e2014-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 Glass7d722762015-01-12 18:02:07 -0700147 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600148 ut_asserteq_mem(buf, "\0\0A\0\0\0", sizeof(buf));
Simon Glass7df766e2014-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 Glass7d722762015-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 Glassa3186e62020-05-10 12:52:45 -0600154 ut_asserteq_mem(buf, "\0\xff\xff\xff\xff", sizeof(buf));
Simon Glass7df766e2014-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 Glass7d722762015-01-12 18:02:07 -0700158 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Simon Glassa3186e62020-05-10 12:52:45 -0600159 ut_asserteq_mem(buf, "\0\0AB\0\0", sizeof(buf));
Simon Glass7df766e2014-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 Glass974dccd2020-07-28 19:41:12 -0600167DM_TEST(dm_test_i2c_bytewise, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7df766e2014-12-10 08:55:55 -0700168
Joe Hershberger3a77be52015-05-20 14:27:27 -0500169static int dm_test_i2c_offset(struct unit_test_state *uts)
Simon Glass7df766e2014-12-10 08:55:55 -0700170{
171 struct udevice *eeprom;
172 struct udevice *dev;
173 uint8_t buf[5];
174
Simon Glassa2723ae2015-01-25 08:26:55 -0700175 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
Simon Glass7df766e2014-12-10 08:55:55 -0700176
177 /* Do a transfer so we can find the emulator */
Simon Glass7d722762015-01-12 18:02:07 -0700178 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200179 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Simon Glass7df766e2014-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 Glass7d722762015-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 Beckett1fe8a492019-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 Glass7df766e2014-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 Glass7d722762015-01-12 18:02:07 -0700192 ut_assertok(dm_i2c_write(dev, 2, (uint8_t *)"AB", 2));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000193 ut_asserteq(2, sanbox_i2c_eeprom_get_prev_offset(eeprom));
Simon Glass7d722762015-01-12 18:02:07 -0700194 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Robert Beckett1fe8a492019-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));
Simon Glass7df766e2014-12-10 08:55:55 -0700197
Robert Beckett1fe8a492019-10-28 17:44:58 +0000198 /* Offset length 2 boundary - check model wrapping */
Simon Glass7df766e2014-12-10 08:55:55 -0700199 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
200 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Robert Beckett1fe8a492019-10-28 17:44:58 +0000201 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 Glass7df766e2014-12-10 08:55:55 -0700210
Robert Beckett1fe8a492019-10-28 17:44:58 +0000211 /* Offset length 2 */
Simon Glass7df766e2014-12-10 08:55:55 -0700212 sandbox_i2c_eeprom_set_offset_len(eeprom, 2);
213 ut_assertok(i2c_set_chip_offset_len(dev, 2));
Robert Beckett1fe8a492019-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));
218
219 /* Offset length 3 */
220 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 Glass7df766e2014-12-10 08:55:55 -0700226
227 /* Offset length 4 */
Robert Beckett1fe8a492019-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 Glass7df766e2014-12-10 08:55:55 -0700234
235 /* Restore defaults */
236 sandbox_i2c_eeprom_set_offset_len(eeprom, 1);
237
238 return 0;
239}
Simon Glass974dccd2020-07-28 19:41:12 -0600240DM_TEST(dm_test_i2c_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Robert Beckettf695f6e2019-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 Suchanekac12a2f2022-10-12 21:57:59 +0200252 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Robert Beckettf695f6e2019-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}
304
Simon Glass974dccd2020-07-28 19:41:12 -0600305DM_TEST(dm_test_i2c_addr_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Sebastian Reichel8e841fb2021-07-15 17:39:59 +0200306
307static int dm_test_i2c_reg_clrset(struct unit_test_state *uts)
308{
309 struct udevice *eeprom;
310 struct udevice *dev;
311 u8 buf[5];
312
313 ut_assertok(i2c_get_chip_for_busnum(busnum, chip, 1, &dev));
314
315 /* Do a transfer so we can find the emulator */
316 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200317 ut_assertok(uclass_first_device_err(UCLASS_I2C_EMUL, &eeprom));
Sebastian Reichel8e841fb2021-07-15 17:39:59 +0200318
319 /* Dummy data for the test */
320 ut_assertok(dm_i2c_write(dev, 0, "\xff\x00\xff\x00\x10", 5));
321
322 /* Do some clrset tests */
323 ut_assertok(dm_i2c_reg_clrset(dev, 0, 0xff, 0x10));
324 ut_assertok(dm_i2c_reg_clrset(dev, 1, 0x00, 0x11));
325 ut_assertok(dm_i2c_reg_clrset(dev, 2, 0xed, 0x00));
326 ut_assertok(dm_i2c_reg_clrset(dev, 3, 0xff, 0x13));
327 ut_assertok(dm_i2c_reg_clrset(dev, 4, 0x00, 0x14));
328
329 ut_assertok(dm_i2c_read(dev, 0, buf, 5));
330 ut_asserteq_mem("\x10\x11\x12\x13\x14", buf, sizeof(buf));
331
332 return 0;
333}
334DM_TEST(dm_test_i2c_reg_clrset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);