summaryrefslogtreecommitdiff
path: root/zephyr/emul/tcpc/emul_ps8xxx.c
blob: 57cc2c85f59e939e4e4c5000da60ea3ff5bd27f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#define DT_DRV_COMPAT cros_ps8xxx_emul

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ps8xxx_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL);

#include <zephyr/device.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/i2c_emul.h>
#include <zephyr/ztest.h>

#include "tcpm/tcpci.h"

#include "emul/emul_common_i2c.h"
#include "emul/tcpc/emul_ps8xxx.h"
#include "emul/tcpc/emul_tcpci.h"
#include "emul/emul_stub_device.h"

#include "driver/tcpm/ps8xxx.h"

#define PS8XXX_REG_MUX_IN_HPD_ASSERTION MUX_IN_HPD_ASSERTION_REG

/** Run-time data used by the emulator */
struct ps8xxx_emul_data {
	/** Common I2C data used by "hidden" ports */
	struct i2c_common_emul_data p0_data;
	struct i2c_common_emul_data p1_data;
	struct i2c_common_emul_data gpio_data;

	/** Product ID of emulated device */
	int prod_id;

	/** Chip revision used by PS8805 */
	uint8_t chip_rev;
	/** Mux usb DCI configuration */
	uint8_t dci_cfg;
	/** GPIO control register value */
	uint8_t gpio_ctrl;
	/** HW revision used by PS8815 */
	uint16_t hw_rev;
	/**
	 * Register ID to distinguish between the PS8815-A2 and PS8745-A2
	 * 0: Indicates this is an 8815-A2 chip
	 * 1: Indicates this is an 8745-A2 chip
	 */
	uint8_t reg_id;
};

/** Constant configuration of the emulator */
struct ps8xxx_emul_cfg {
	/** Common I2C configuration used by "hidden" ports */
	const struct i2c_common_emul_cfg p0_cfg;
	const struct i2c_common_emul_cfg p1_cfg;
	const struct i2c_common_emul_cfg gpio_cfg;
};

/** Check description in emul_ps8xxx.h */
void ps8xxx_emul_set_chip_rev(const struct emul *emul, uint8_t chip_rev)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	data->chip_rev = chip_rev;
}

/** Check description in emul_ps8xxx.h */
void ps8xxx_emul_set_reg_id(const struct emul *emul, enum ps8xxx_regid reg_id)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	data->reg_id = reg_id;
}

/** Check description in emul_ps8xxx.h */
void ps8xxx_emul_set_hw_rev(const struct emul *emul, uint16_t hw_rev)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	data->hw_rev = hw_rev;
}

/** Check description in emul_ps8xxx.h */
void ps8xxx_emul_set_gpio_ctrl(const struct emul *emul, uint8_t gpio_ctrl)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	data->gpio_ctrl = gpio_ctrl;
}

/** Check description in emul_ps8xxx.h */
uint8_t ps8xxx_emul_get_gpio_ctrl(const struct emul *emul)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	return data->gpio_ctrl;
}

/** Check description in emul_ps8xxx.h */
uint8_t ps8xxx_emul_get_dci_cfg(const struct emul *emul)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	return data->dci_cfg;
}

/** Check description in emul_ps8xxx.h */
int ps8xxx_emul_set_product_id(const struct emul *emul, uint16_t product_id)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	if (product_id != PS8805_PRODUCT_ID &&
	    product_id != PS8815_PRODUCT_ID) {
		LOG_ERR("Setting invalid product ID 0x%x", product_id);
		return -EINVAL;
	}

	data->prod_id = product_id;
	tcpci_emul_set_reg(emul, TCPC_REG_PRODUCT_ID, product_id);

	return 0;
}

/** Check description in emul_ps8xxx.h */
uint16_t ps8xxx_emul_get_product_id(const struct emul *emul)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	return data->prod_id;
}

/** Check description in emul_ps8xxx.h */
struct i2c_common_emul_data *
ps8xxx_emul_get_i2c_common_data(const struct emul *emul,
				enum ps8xxx_emul_port port)
{
	const struct ps8xxx_emul_cfg *cfg = emul->cfg;
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;

	switch (port) {
	case PS8XXX_EMUL_PORT_0:
		return &data->p0_data;
	case PS8XXX_EMUL_PORT_1:
		return &data->p1_data;
	case PS8XXX_EMUL_PORT_GPIO:
		if (cfg->gpio_cfg.addr != 0) {
			return &data->gpio_data;
		} else {
			return NULL;
		}
	default:
		return NULL;
	}
}

/**
 * @brief Function called for each byte of read message from TCPC chip
 *
 * @param i2c_emul Pointer to PS8xxx emulator
 * @param reg First byte of last write message
 * @param val Pointer where byte to read should be stored
 * @param bytes Number of bytes already read
 *
 * @return 0 on success
 * @return -EIO on invalid read request
 */
static int ps8xxx_emul_tcpc_read_byte(const struct emul *emul, int reg,
				      uint8_t *val, int bytes)
{
	uint16_t reg_val;
	const struct i2c_emul *i2c_emul = emul->bus.i2c;

	LOG_DBG("PS8XXX TCPC 0x%x: read reg 0x%x", i2c_emul->addr, reg);

	switch (reg) {
	case PS8XXX_REG_FW_REV:
	case PS8XXX_REG_I2C_DEBUGGING_ENABLE:
	case PS8XXX_REG_MUX_IN_HPD_ASSERTION:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE0:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE1:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE2:
	case PS8XXX_REG_BIST_CONT_MODE_CTR:
		if (bytes != 0) {
			LOG_ERR("Reading byte %d from 1 byte register 0x%x",
				bytes, reg);
			return -EIO;
		}

		tcpci_emul_get_reg(emul, reg, &reg_val);
		*val = reg_val & 0xff;
		return 0;
	default:
		break;
	}

	return tcpci_emul_read_byte(emul, reg, val, bytes);
}

/**
 * @brief Function called for each byte of write message to TCPC chip
 *
 * @param emul Pointer to PS8xxx emulator
 * @param reg First byte of write message
 * @param val Received byte of write message
 * @param bytes Number of bytes already received
 *
 * @return 0 on success
 * @return -EIO on invalid write request
 */
static int ps8xxx_emul_tcpc_write_byte(const struct emul *emul, int reg,
				       uint8_t val, int bytes)
{
	uint16_t prod_id;
	struct i2c_emul *i2c_emul = emul->bus.i2c;

	LOG_DBG("PS8XXX TCPC 0x%x: write reg 0x%x", i2c_emul->addr, reg);

	tcpci_emul_get_reg(emul, TCPC_REG_PRODUCT_ID, &prod_id);

	switch (reg) {
	case PS8XXX_REG_RP_DETECT_CONTROL:
		/* This register is present only on PS8815 */
		if (prod_id != PS8815_PRODUCT_ID) {
			break;
		}
		__fallthrough;
	case PS8XXX_REG_I2C_DEBUGGING_ENABLE:
	case PS8XXX_REG_MUX_IN_HPD_ASSERTION:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE0:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE1:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE2:
	case PS8XXX_REG_BIST_CONT_MODE_CTR:
		if (bytes != 1) {
			LOG_ERR("Writing byte %d to 1 byte register 0x%x",
				bytes, reg);
			return -EIO;
		}

		tcpci_emul_set_reg(emul, reg, val);
		return 0;
	default:
		break;
	}

	return tcpci_emul_write_byte(emul, reg, val, bytes);
}

/**
 * @brief Function called on the end of write message to TCPC chip
 *
 * @param emul Pointer to PS8xxx emulator
 * @param reg Register which is written
 * @param msg_len Length of handled I2C message
 *
 * @return 0 on success
 * @return -EIO on error
 */
static int ps8xxx_emul_tcpc_finish_write(const struct emul *emul, int reg,
					 int msg_len)
{
	uint16_t prod_id;
	struct i2c_emul *i2c_emul = emul->bus.i2c;

	LOG_DBG("PS8XXX TCPC 0x%x: finish write reg 0x%x", i2c_emul->addr, reg);

	tcpci_emul_get_reg(emul, TCPC_REG_PRODUCT_ID, &prod_id);

	switch (reg) {
	case PS8XXX_REG_RP_DETECT_CONTROL:
		/* This register is present only on PS8815 */
		if (prod_id != PS8815_PRODUCT_ID) {
			break;
		}
		__fallthrough;
	case PS8XXX_REG_I2C_DEBUGGING_ENABLE:
	case PS8XXX_REG_MUX_IN_HPD_ASSERTION:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE0:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE1:
	case PS8XXX_REG_BIST_CONT_MODE_BYTE2:
	case PS8XXX_REG_BIST_CONT_MODE_CTR:
		return 0;
	default:
		break;
	}

	return tcpci_emul_handle_write(emul, reg, msg_len);
}

/**
 * @brief Get currently accessed register, which always equals to selected
 *        register from TCPC chip.
 *
 * @param emul Pointer to TCPCI emulator
 * @param reg First byte of last write message
 * @param bytes Number of bytes already handled from current message
 * @param read If currently handled is read message
 *
 * @return Currently accessed register
 */
static int ps8xxx_emul_tcpc_access_reg(const struct emul *emul, int reg,
				       int bytes, bool read)
{
	return reg;
}

/**
 * @brief Function called on reset
 *
 * @param emul Pointer to PS8xxx emulator
 */
static int ps8xxx_emul_tcpc_reset(const struct emul *emul)
{
	tcpci_emul_set_reg(emul, PS8XXX_REG_I2C_DEBUGGING_ENABLE, 0x31);
	tcpci_emul_set_reg(emul, PS8XXX_REG_MUX_IN_HPD_ASSERTION, 0x00);
	tcpci_emul_set_reg(emul, PS8XXX_REG_BIST_CONT_MODE_BYTE0, 0xff);
	tcpci_emul_set_reg(emul, PS8XXX_REG_BIST_CONT_MODE_BYTE1, 0x0f);
	tcpci_emul_set_reg(emul, PS8XXX_REG_BIST_CONT_MODE_BYTE2, 0x00);
	tcpci_emul_set_reg(emul, PS8XXX_REG_BIST_CONT_MODE_CTR, 0x00);

	return tcpci_emul_reset(emul);
}

/**
 * @brief Function called for each byte of read message
 *
 * @param emul Pointer to PS8xxx emulator
 * @param reg First byte of last write message
 * @param val Pointer where byte to read should be stored
 * @param bytes Number of bytes already read
 *
 * @return 0 on success
 * @return -EIO on invalid read request
 */
static int ps8xxx_emul_read_byte_workhorse(const struct emul *emul, int reg,
					   uint8_t *val, int bytes,
					   enum ps8xxx_emul_port port)
{
	struct tcpc_emul_data *tcpc_data;
	struct ps8xxx_emul_data *data;
	struct i2c_emul *i2c_emul = emul->bus.i2c;
	uint16_t i2c_dbg_reg;

	LOG_DBG("PS8XXX 0x%x: read reg 0x%x", i2c_emul->addr, reg);

	tcpc_data = emul->data;
	data = tcpc_data->chip_data;

	tcpci_emul_get_reg(emul, PS8XXX_REG_I2C_DEBUGGING_ENABLE, &i2c_dbg_reg);
	/* There is no need to enable I2C debug on PS8815 */
	if (data->prod_id != PS8815_PRODUCT_ID && i2c_dbg_reg & 0x1) {
		LOG_ERR("Accessing hidden i2c address without enabling debug");
		return -EIO;
	}

	/* This is only 2 bytes register so handle it separately */
	if (data->prod_id == PS8815_PRODUCT_ID && port == PS8XXX_EMUL_PORT_1 &&
	    reg == PS8815_P1_REG_HW_REVISION) {
		if (bytes > 1) {
			LOG_ERR("Reading more than two bytes from HW rev reg");
			return -EIO;
		}

		*val = (data->hw_rev >> (bytes * 8)) & 0xff;
		return 0;
	}

	if (bytes != 0) {
		LOG_ERR("Reading more than one byte at once");
		return -EIO;
	}

	switch (port) {
	case PS8XXX_EMUL_PORT_0:
		if (data->prod_id == PS8805_PRODUCT_ID &&
		    reg == PS8805_P0_REG_CHIP_REVISION) {
			*val = data->chip_rev;
			return 0;
		}
		if (data->prod_id == PS8815_PRODUCT_ID &&
		    reg == PS8815_P0_REG_ID) {
			*val = data->chip_rev;
			return 0;
		}
		break;
	case PS8XXX_EMUL_PORT_1:
		/* DCI CFG is no available on PS8815 */
		if (data->prod_id != PS8815_PRODUCT_ID &&
		    reg == PS8XXX_P1_REG_MUX_USB_DCI_CFG) {
			*val = data->dci_cfg;
			return 0;
		}
		__fallthrough;
	case PS8XXX_EMUL_PORT_GPIO:
		if (reg == PS8805_REG_GPIO_CONTROL) {
			*val = data->gpio_ctrl;
			return 0;
		}
		__fallthrough;
	case PS8XXX_EMUL_PORT_INVAL:
		LOG_ERR("Invalid I2C address");
		return -EIO;
	}

	LOG_ERR("Reading from reg 0x%x which is WO or undefined", reg);
	return -EIO;
}

static int ps8xxx_emul_p0_read_byte(const struct emul *emul, int reg,
				    uint8_t *val, int bytes)
{
	return ps8xxx_emul_read_byte_workhorse(emul, reg, val, bytes,
					       PS8XXX_EMUL_PORT_0);
}

static int ps8xxx_emul_p1_read_byte(const struct emul *emul, int reg,
				    uint8_t *val, int bytes)
{
	return ps8xxx_emul_read_byte_workhorse(emul, reg, val, bytes,
					       PS8XXX_EMUL_PORT_1);
}

static int ps8xxx_emul_gpio_read_byte(const struct emul *emul, int reg,
				      uint8_t *val, int bytes)
{
	return ps8xxx_emul_read_byte_workhorse(emul, reg, val, bytes,
					       PS8XXX_EMUL_PORT_GPIO);
}

/**
 * @brief Function called for each byte of write message
 *
 * @param emul Pointer to PS8xxx emulator
 * @param reg First byte of write message
 * @param val Received byte of write message
 * @param bytes Number of bytes already received
 *
 * @return 0 on success
 * @return -EIO on invalid write request
 */
static int ps8xxx_emul_write_byte_workhorse(const struct emul *emul, int reg,
					    uint8_t val, int bytes,
					    enum ps8xxx_emul_port port)
{
	struct ps8xxx_emul_data *data;
	struct tcpc_emul_data *tcpc_data;
	const struct i2c_emul *i2c_emul = emul->bus.i2c;
	uint16_t i2c_dbg_reg;

	LOG_DBG("PS8XXX 0x%x: write reg 0x%x", i2c_emul->addr, reg);

	tcpc_data = emul->data;
	data = tcpc_data->chip_data;

	tcpci_emul_get_reg(emul, PS8XXX_REG_I2C_DEBUGGING_ENABLE, &i2c_dbg_reg);
	/* There is no need to enable I2C debug on PS8815 */
	if (data->prod_id != PS8815_PRODUCT_ID && i2c_dbg_reg & 0x1) {
		LOG_ERR("Accessing hidden i2c address without enabling debug");
		return -EIO;
	}

	if (bytes != 1) {
		LOG_ERR("Writing more than one byte at once");
		return -EIO;
	}

	switch (port) {
	case PS8XXX_EMUL_PORT_0:
		break;
	case PS8XXX_EMUL_PORT_1:
		/* DCI CFG is no available on PS8815 */
		if (data->prod_id != PS8815_PRODUCT_ID &&
		    reg == PS8XXX_P1_REG_MUX_USB_DCI_CFG) {
			data->dci_cfg = val;
			return 0;
		}
		__fallthrough;
	case PS8XXX_EMUL_PORT_GPIO:
		if (reg == PS8805_REG_GPIO_CONTROL) {
			data->gpio_ctrl = val;
			return 0;
		}
		__fallthrough;
	case PS8XXX_EMUL_PORT_INVAL:
		LOG_ERR("Invalid I2C address");
		return -EIO;
	}

	LOG_ERR("Writing to reg 0x%x which is RO or undefined", reg);
	return -EIO;
}

static int ps8xxx_emul_p0_write_byte(const struct emul *emul, int reg,
				     uint8_t val, int bytes)
{
	return ps8xxx_emul_write_byte_workhorse(emul, reg, val, bytes,
						PS8XXX_EMUL_PORT_0);
}

static int ps8xxx_emul_p1_write_byte(const struct emul *emul, int reg,
				     uint8_t val, int bytes)
{
	return ps8xxx_emul_write_byte_workhorse(emul, reg, val, bytes,
						PS8XXX_EMUL_PORT_1);
}

static int ps8xxx_emul_gpio_write_byte(const struct emul *emul, int reg,
				       uint8_t val, int bytes)
{
	return ps8xxx_emul_write_byte_workhorse(emul, reg, val, bytes,
						PS8XXX_EMUL_PORT_GPIO);
}

static int i2c_ps8xxx_emul_transfer(const struct emul *target,
				    struct i2c_msg *msgs, int num_msgs,
				    int addr)
{
	struct tcpc_emul_data *tcpc_data = target->data;
	struct ps8xxx_emul_data *ps8_xxx_data = tcpc_data->chip_data;
	const struct ps8xxx_emul_cfg *ps8_xxx_cfg = target->cfg;
	struct i2c_common_emul_data *common_data;

	/* The chip itself */
	if (addr == tcpc_data->i2c_cfg.addr) {
		const struct i2c_common_emul_cfg *common_cfg =
			&tcpc_data->i2c_cfg;
		common_data = &tcpc_data->tcpci_ctx->common;

		return i2c_common_emul_transfer_workhorse(
			target, common_data, common_cfg, msgs, num_msgs, addr);
	}
	/* Subchips */
	else if (addr == ps8_xxx_cfg->gpio_cfg.addr) {
		const struct i2c_common_emul_cfg *common_cfg =
			&ps8_xxx_cfg->gpio_cfg;
		common_data = &ps8_xxx_data->gpio_data;

		return i2c_common_emul_transfer_workhorse(
			target, common_data, common_cfg, msgs, num_msgs, addr);
	} else if (addr == ps8_xxx_cfg->p0_cfg.addr) {
		const struct i2c_common_emul_cfg *common_cfg =
			&ps8_xxx_cfg->p0_cfg;
		common_data = &ps8_xxx_data->p0_data;

		return i2c_common_emul_transfer_workhorse(
			target, common_data, common_cfg, msgs, num_msgs, addr);
	} else if (addr == ps8_xxx_cfg->p1_cfg.addr) {
		const struct i2c_common_emul_cfg *common_cfg =
			&ps8_xxx_cfg->p1_cfg;
		common_data = &ps8_xxx_data->p1_data;

		return i2c_common_emul_transfer_workhorse(
			target, common_data, common_cfg, msgs, num_msgs, addr);
	}

	LOG_ERR("Cannot map address %02x", addr);
	return -EIO;
}

struct i2c_emul_api i2c_ps8xxx_emul_api = {
	.transfer = i2c_ps8xxx_emul_transfer,
};

/**
 * @brief Set up a new PS8xxx emulator
 *
 * This should be called for each PS8xxx device that needs to be
 * emulated. It registers "hidden" I2C devices with the I2C emulation
 * controller and set PS8xxx device operations to associated TCPCI emulator.
 *
 * @param emul Emulation information
 * @param parent Device to emulate
 *
 * @return 0 indicating success (always)
 */
static int ps8xxx_emul_init(const struct emul *emul,
			    const struct device *parent)
{
	struct tcpc_emul_data *tcpc_data = emul->data;
	struct ps8xxx_emul_data *data = tcpc_data->chip_data;
	struct tcpci_ctx *tcpci_ctx = tcpc_data->tcpci_ctx;
	const struct ps8xxx_emul_cfg *cfg = emul->cfg;
	const struct device *i2c_dev;
	int ret = 0;

	i2c_dev = parent;

	tcpci_ctx->common.write_byte = ps8xxx_emul_tcpc_write_byte;
	tcpci_ctx->common.finish_write = ps8xxx_emul_tcpc_finish_write;
	tcpci_ctx->common.read_byte = ps8xxx_emul_tcpc_read_byte;
	tcpci_ctx->common.access_reg = ps8xxx_emul_tcpc_access_reg;

	tcpci_emul_i2c_init(emul, i2c_dev);

	data->p0_data.emul.api = &i2c_ps8xxx_emul_api;
	data->p0_data.emul.addr = cfg->p0_cfg.addr;
	data->p0_data.emul.target = emul;
	data->p0_data.i2c = i2c_dev;
	data->p0_data.cfg = &cfg->p0_cfg;
	i2c_common_emul_init(&data->p0_data);

	data->p1_data.emul.api = &i2c_ps8xxx_emul_api;
	data->p1_data.emul.addr = cfg->p1_cfg.addr;
	data->p1_data.emul.target = emul;
	data->p1_data.i2c = i2c_dev;
	data->p1_data.cfg = &cfg->p1_cfg;
	i2c_common_emul_init(&data->p1_data);

	/* Have to manually register "hidden" addressed chips under overarching
	 * ps8xxx
	 * TODO(b/240564574): Call EMUL_DEFINE for each "hidden" sub-chip.
	 */
	ret |= i2c_emul_register(i2c_dev, &data->p0_data.emul);
	ret |= i2c_emul_register(i2c_dev, &data->p1_data.emul);

	if (cfg->gpio_cfg.addr != 0) {
		data->gpio_data.emul.api = &i2c_ps8xxx_emul_api;
		data->gpio_data.emul.addr = cfg->gpio_cfg.addr;
		data->gpio_data.emul.target = emul;
		data->gpio_data.i2c = i2c_dev;
		data->gpio_data.cfg = &cfg->gpio_cfg;
		ret |= i2c_emul_register(i2c_dev, &data->gpio_data.emul);
		i2c_common_emul_init(&data->gpio_data);
	}

	ret |= ps8xxx_emul_tcpc_reset(emul);

	tcpci_emul_set_reg(emul, TCPC_REG_PRODUCT_ID, data->prod_id);
	/* FW rev is never 0 in a working device. Set arbitrary FW rev. */
	tcpci_emul_set_reg(emul, PS8XXX_REG_FW_REV, 0x31);

	return ret;
}

#define PS8XXX_EMUL(n)                                                \
	static struct ps8xxx_emul_data ps8xxx_emul_data_##n = {		\
		.prod_id = PS8805_PRODUCT_ID,				\
		.p0_data = {						\
			.write_byte = ps8xxx_emul_p0_write_byte,	\
			.read_byte = ps8xxx_emul_p0_read_byte,		\
		},							\
		.p1_data = {						\
			.write_byte = ps8xxx_emul_p1_write_byte,	\
			.read_byte = ps8xxx_emul_p1_read_byte,		\
		},							\
		.gpio_data = {						\
			.write_byte = ps8xxx_emul_gpio_write_byte,	\
			.read_byte = ps8xxx_emul_gpio_read_byte,	\
		},							\
	};    \
                                                                      \
	static const struct ps8xxx_emul_cfg ps8xxx_emul_cfg_##n = {	\
		.p0_cfg = {						\
			.dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \
			.data = &ps8xxx_emul_data_##n.p0_data,		\
			.addr = DT_INST_PROP(n, p0_i2c_addr),		\
		},							\
		.p1_cfg = {						\
			.dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \
			.data = &ps8xxx_emul_data_##n.p1_data,		\
			.addr = DT_INST_PROP(n, p1_i2c_addr),		\
		},							\
		.gpio_cfg = {						\
			.dev_label = DT_NODE_FULL_NAME(DT_DRV_INST(n)), \
			.data = &ps8xxx_emul_data_##n.gpio_data,	\
			.addr = DT_INST_PROP(n, gpio_i2c_addr),		\
		},							\
	}; \
	TCPCI_EMUL_DEFINE(n, ps8xxx_emul_init, &ps8xxx_emul_cfg_##n,  \
			  &ps8xxx_emul_data_##n, &i2c_ps8xxx_emul_api)

DT_INST_FOREACH_STATUS_OKAY(PS8XXX_EMUL)

#ifdef CONFIG_ZTEST_NEW_API
#define PS8XXX_EMUL_RESET_RULE_BEFORE(n) \
	ps8xxx_emul_tcpc_reset(EMUL_DT_GET(DT_DRV_INST(n)));
static void ps8xxx_emul_reset_rule_before(const struct ztest_unit_test *test,
					  void *data)
{
	ARG_UNUSED(test);
	ARG_UNUSED(data);
	DT_INST_FOREACH_STATUS_OKAY(PS8XXX_EMUL_RESET_RULE_BEFORE);
}
ZTEST_RULE(PS8XXX_emul_reset, ps8xxx_emul_reset_rule_before, NULL);
#endif /* CONFIG_ZTEST_NEW_API */

DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE);