@@ -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)
214218void 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 )
0 commit comments