Skip to content

Commit 438eb72

Browse files
Josh-Tsaiquinchou77
authored andcommitted
fwk: modified the PD code to support one PD chip
Remove the hard code and use the for-loop to update the PD status. BRANCH=fwk-main BUG=None TEST=Marigold four type-c ports are workable TEST=Marigold right side type-c ports are workable if the PD chip is to 1 TEST=Either support 1 PD chip or 2 PD chips the error recovery feature is workable TEST=Either support 1 PD chip or 2 PD chips that can display the PD version in the setup menu Signed-off-by: Josh-Tsai <josh_tsai@compal.com> (cherry picked from commit bb8e00c)
1 parent ab2d942 commit 438eb72

7 files changed

Lines changed: 213 additions & 133 deletions

File tree

zephyr/program/framework/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ config PD_CCG8_CUSTOMIZE_BATT_MESSAGE
104104
Enable customize battery response for GRL test, EC will write specific
105105
register 0x4f to PD for GET_BATTERY_CAP and GET_BATTERY_STATUS.
106106

107+
config PLATFORM_EC_PD_CHIP_MAX_COUNT
108+
int "The maximum PD chips"
109+
default 2
110+
help
111+
We would like to design the PD function more flexible.
112+
Use this configuration to set the maximum PD chip with mainboard design.
113+
107114
config PD_COMMON_VBUS_CONTROL
108115
bool "Enable CCG common vbus control"
109116
help

zephyr/program/framework/include/cypress_pd_common.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@
233233
/************************************************/
234234
#define PORT_TO_CONTROLLER(x) ((x) >> 1)
235235
#define PORT_TO_CONTROLLER_PORT(x) ((x) & 0x01)
236+
#define PORTS_PER_CONTROLLER 2
237+
#define CONTROLLER_PORT_TO_CHARGE_PORT(controller, port) \
238+
(controller * PORTS_PER_CONTROLLER + port)
236239

237240
/************************************************/
238241
/* CCG6 special setting */
@@ -489,18 +492,18 @@ enum pd_port_role {
489492
PORT_DUALROLE
490493
};
491494

495+
#define PD_CHIP_COUNT CONFIG_PLATFORM_EC_PD_CHIP_MAX_COUNT
492496
enum pd_chip {
493497
PD_CHIP_0,
494498
PD_CHIP_1,
495-
PD_CHIP_COUNT
496499
};
497500

501+
#define PD_PORT_COUNT CONFIG_USB_PD_PORT_MAX_COUNT
498502
enum pd_port {
499503
PD_PORT_0,
500504
PD_PORT_1,
501505
PD_PORT_2,
502506
PD_PORT_3,
503-
PD_PORT_COUNT
504507
};
505508

506509
enum pd_progress {
@@ -977,4 +980,12 @@ int get_pd_alt_mode_status(int port);
977980
*/
978981
void perform_error_recovery(int controller);
979982

983+
/**
984+
* CYPD interrupt pin control.
985+
*
986+
* @param controller The port number for which to retrieve the state.
987+
* @param enable_ndisable enable/disable interrupt.
988+
*/
989+
void cypd_enable_interrupt(int controller, int enable_ndisable);
990+
980991
#endif /* __CROS_EC_CYPRESS_PD_COMMON_H */

zephyr/program/framework/program.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CONFIG_PLATFORM_EC=y
66
CONFIG_CROS_EC=y
77
CONFIG_SHIMMED_TASKS=y
88
CONFIG_SYSCON=y
9+
CONFIG_ASSERT=y
10+
911
# Not sure what overrides the default in zephyr/app/ec/Kconfig. We shouldn't have to override it here
1012
CONFIG_NUM_PREEMPT_PRIORITIES=28
1113
CONFIG_SYSTEM_WORKQUEUE_PRIORITY=27

zephyr/program/framework/src/board_host_command.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static enum ec_status flash_notified(struct host_cmd_handler_args *args)
8383
{
8484

8585
const struct ec_params_flash_notified *p = args->params;
86+
int controller;
8687

8788
switch (p->flags & 0x03) {
8889
case FLASH_FIRMWARE_START:
@@ -91,8 +92,9 @@ static enum ec_status flash_notified(struct host_cmd_handler_args *args)
9192
gpio_disable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_lid_open));
9293

9394
if ((p->flags & FLASH_FLAG_PD) == FLASH_FLAG_PD) {
94-
gpio_disable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_pd_chip0_interrupt));
95-
gpio_disable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_pd_chip1_interrupt));
95+
for (controller = 0; controller < PD_CHIP_COUNT; controller++)
96+
cypd_enable_interrupt(controller, false);
97+
9698
set_pd_fw_update(true);
9799
}
98100
case FLASH_ACCESS_SPI:
@@ -101,9 +103,12 @@ static enum ec_status flash_notified(struct host_cmd_handler_args *args)
101103
case FLASH_FIRMWARE_DONE:
102104
CPRINTS("Flash done, flags:0x%02x", p->flags);
103105
gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_powerbtn));
104-
gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_pd_chip0_interrupt));
105-
gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_pd_chip1_interrupt));
106+
#if DT_NODE_EXISTS(DT_NODELABEL(gpio_lid_sw_l))
107+
/* TODO: should refactor this for each prjects */
106108
gpio_enable_dt_interrupt(GPIO_INT_FROM_NODELABEL(int_lid_open));
109+
#endif
110+
for (controller = 0; controller < PD_CHIP_COUNT; controller++)
111+
cypd_enable_interrupt(controller, true);
107112

108113
set_pd_fw_update(false);
109114
/* resetup PD controllers */

zephyr/program/framework/src/cypd_ccg6.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ int cypd_write_reg8_wait_ack(int controller, int reg, int data)
4545
int expected_ack_mask = 0;
4646
const struct gpio_dt_spec *intr = gpio_get_dt_spec(pd_chip_config[controller].gpio);
4747

48+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
49+
4850
if (reg < 0x1000) {
4951
expected_ack_mask = CCG_DEV_INTR;
5052
cmd_port = -1;
@@ -180,6 +182,8 @@ int cypd_setup(int controller)
180182
};
181183
BUILD_ASSERT(ARRAY_SIZE(cypd_setup_cmds) == CYPD_SETUP_CMDS_LEN);
182184

185+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
186+
183187
/* Make sure the interrupt is not asserted before we start */
184188
if (gpio_pin_get_dt(intr) == 0) {
185189
rv = cypd_get_int(controller, &data);
@@ -214,11 +218,26 @@ int cypd_setup(int controller)
214218
void cypd_update_ac_status(int controller)
215219
{
216220
CPRINTS("Check C%d AC status!", controller);
221+
222+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
223+
217224
if (cypd_write_reg8_wait_ack(controller,
218225
CCG_CUST_C_CTRL_CONTROL_REG, CCG6_AC_AT_PORT))
219226
CPRINTS("CYPD Read AC status fail");
220227
}
221228

229+
__maybe_unused static bool are_cypd_chips_ready(void)
230+
{
231+
int controller;
232+
233+
for (controller = 0; controller < PD_CHIP_COUNT; controller++) {
234+
if (pd_chip_config[controller].state != CCG_STATE_READY)
235+
return EC_ERROR_BUSY;
236+
}
237+
238+
return EC_SUCCESS;
239+
}
240+
222241
__override void cypd_customize_app_setup(int controller)
223242
{
224243
/* After cypd setup complete, check the AC status */
@@ -235,8 +254,7 @@ int check_power_on_port(void)
235254
{
236255
int port;
237256
/* only read CYPD when it ready */
238-
if (!(pd_chip_config[0].state == CCG_STATE_READY &&
239-
pd_chip_config[1].state == CCG_STATE_READY)) {
257+
if (!are_cypd_chips_ready()) {
240258
CPRINTS("CYPD not ready, just delay 100ms to wait");
241259
crec_msleep(100);
242260
}
@@ -315,6 +333,8 @@ void update_system_power_state(int controller)
315333
/* CCG6 does not support power state G3, just for initial state */
316334
static uint8_t pre_state = CCG_POWERSTATE_G3;
317335

336+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
337+
318338
switch (ps) {
319339
case POWER_G3:
320340
case POWER_S5G3:
@@ -368,6 +388,8 @@ void enable_compliance_mode(int controller)
368388
uint32_t debug_register = 0xD0000000;
369389
int debug_ctl = 0x0100;
370390

391+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
392+
371393
/* Write 0xD0000000 to address 0x0048 */
372394
rv = cypd_write_reg_block(controller, CCG_ICL_BB_RETIMER_DAT_REG,
373395
(void *) &debug_register, 4);
@@ -386,6 +408,8 @@ void disable_compliance_mode(int controller)
386408
uint32_t debug_register = 0x00000000;
387409
int debug_ctl = 0x0000;
388410

411+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
412+
389413
/* Write 0x00000000 to address 0x0048 */
390414
rv = cypd_write_reg_block(controller, CCG_ICL_BB_RETIMER_DAT_REG,
391415
(void *) &debug_register, 4);
@@ -404,6 +428,8 @@ void entry_tbt_mode(int controller)
404428
uint8_t force_tbt_mode = 0x01;
405429
int debug_ctl = 0x0100;
406430

431+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
432+
407433
/* Write 0x0100 to address 0x0046 */
408434
rv = cypd_write_reg16(controller, CCG_ICL_BB_RETIMER_CMD_REG, debug_ctl);
409435
if (rv != EC_SUCCESS)
@@ -421,6 +447,8 @@ void exit_tbt_mode(int controller)
421447
uint8_t force_tbt_mode = 0x00;
422448
int debug_ctl = 0x0000;
423449

450+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
451+
424452
/* Write 0x00 to address 0x0040 */
425453
rv = cypd_write_reg8(controller, CCG_ICL_CTRL_REG, force_tbt_mode);
426454
if (rv != EC_SUCCESS)
@@ -437,6 +465,8 @@ int check_tbt_mode(int controller)
437465
int rv;
438466
int data;
439467

468+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
469+
440470
rv = cypd_read_reg8(controller, CCG_ICL_STS_REG, &data);
441471
if (rv != EC_SUCCESS)
442472
CPRINTS("Read CYP5525_ICL_STS_REG fail");
@@ -462,10 +492,8 @@ void cypd_customize_battery_cap(void)
462492
battery_get_disconnect_state();
463493

464494
/* only send status when PD ready */
465-
if (!(pd_chip_config[0].state == CCG_STATE_READY &&
466-
pd_chip_config[1].state == CCG_STATE_READY)) {
495+
if (!are_cypd_chips_ready())
467496
return;
468-
}
469497

470498
if (!battery_can_discharge) {
471499
cypd_batt_update = false;
@@ -518,10 +546,8 @@ void cypd_customize_battery_status(void)
518546
battery_get_params(&batt);
519547

520548
/* only send status when PD ready */
521-
if (!(pd_chip_config[0].state == CCG_STATE_READY &&
522-
pd_chip_config[1].state == CCG_STATE_READY)) {
549+
if (!are_cypd_chips_ready())
523550
return;
524-
}
525551

526552
/* only update data when soc change */
527553
if (batt.state_of_charge == pd_batt_soc)

zephyr/program/framework/src/cypd_ccg8.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ void update_system_power_state(int controller)
195195
{
196196
enum power_state ps = power_get_state();
197197

198+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
199+
198200
switch (ps) {
199201
case POWER_G3:
200202
case POWER_S5G3:
@@ -577,6 +579,8 @@ void cypd_update_epr_state(int controller, int port, int response_len)
577579
uint16_t addr_flags = pd_chip_config[controller].addr_flags;
578580
int port_idx = (controller << 1) + port;
579581

582+
__ASSERT(controller < PD_CHIP_COUNT, "Invalid PD chip controller id in %s.", __func__);
583+
580584
rv = i2c_read_offset16_block(i2c_port, addr_flags,
581585
CCG_READ_DATA_MEMORY_REG(port, 0), data, MIN(response_len, 16));
582586

0 commit comments

Comments
 (0)