@@ -552,3 +552,142 @@ int check_tbt_mode(int controller)
552552
553553 return data ;
554554}
555+
556+ #ifdef CONFIG_PD_CCG6_CUSTOMIZE_BATT_MESSAGE
557+ /*****************************************************************
558+ * Customize response battery status
559+ ****************************************************************/
560+
561+ static struct pd_battery_cap_t pd_battery_cap ;
562+ static struct pd_battery_status_t pd_battery_status ;
563+ static int pd_batt_soc ;
564+ bool cypd_batt_update ;
565+
566+ void cypd_customize_battery_cap (void )
567+ {
568+ int i ;
569+ uint32_t c , v ;
570+ bool battery_can_discharge = (battery_is_present () == BP_YES ) &
571+ battery_get_disconnect_state ();
572+
573+ /* only send status when PD ready */
574+ if (!(pd_chip_config [0 ].state == CCG_STATE_READY &&
575+ pd_chip_config [1 ].state == CCG_STATE_READY )) {
576+ return ;
577+ }
578+
579+ if (!battery_can_discharge ) {
580+ cypd_batt_update = false;
581+ pd_battery_cap .design_cap = 0x0000 ;
582+ pd_battery_cap .last_full_cap = 0x0000 ;
583+ pd_battery_cap .battery_type = 0x1 ;
584+
585+ } else {
586+ cypd_batt_update = true;
587+ pd_battery_cap .reg = 0 ;
588+ pd_battery_cap .vid = VENDOR_ID ;
589+ pd_battery_cap .pid = PRODUCT_ID ;
590+ pd_battery_cap .battery_type = 0x0 ;
591+
592+ if (battery_design_voltage (& v ) == 0 ) {
593+ if (battery_design_capacity (& c ) == 0 ) {
594+ /*
595+ * Wh = (c * v) / 1000000
596+ * 10th of a Wh = Wh * 10
597+ */
598+ pd_battery_cap .design_cap = DIV_ROUND_NEAREST ((c * v ),
599+ 100000 );
600+ }
601+ if (battery_full_charge_capacity (& c ) == 0 ) {
602+ /*
603+ * Wh = (c * v) / 1000000
604+ * 10th of a Wh = Wh * 10
605+ */
606+ pd_battery_cap .last_full_cap = DIV_ROUND_NEAREST ((c * v ),
607+ 100000 );
608+ }
609+ }
610+ }
611+
612+ for (i = 0 ; i < PD_CHIP_COUNT ; i ++ )
613+ cypd_write8_reg_block (i , CCG_BATTERT_STATE ,
614+ & pd_battery_cap , sizeof (pd_battery_cap ));
615+
616+ }
617+
618+ void cypd_customize_battery_status (void )
619+ {
620+ int i , soc_wh ;
621+ uint8_t batt_info ;
622+ uint32_t c , v ;
623+ struct batt_params batt ;
624+ bool battery_can_discharge = (battery_is_present () == BP_YES ) &
625+ battery_get_disconnect_state ();
626+
627+ battery_get_params (& batt );
628+
629+ /* only send status when PD ready */
630+ if (!(pd_chip_config [0 ].state == CCG_STATE_READY &&
631+ pd_chip_config [1 ].state == CCG_STATE_READY )) {
632+ return ;
633+ }
634+
635+ /* only update data when soc change */
636+ if (batt .state_of_charge == pd_batt_soc )
637+ return ;
638+
639+ pd_batt_soc = batt .state_of_charge ;
640+
641+ if (!battery_can_discharge ) {
642+
643+ pd_battery_status .reg = 0x1 ;
644+ pd_battery_status .battery_info = 0 ;
645+ pd_battery_status .batt_present_cap = 0xFFFF ;
646+
647+ } else {
648+
649+ /**
650+ * if battery didn't set cap info at first time pd init
651+ * need set again when battery ready.
652+ * ex: resume from dead battery, or ac only boot and then plug-in batt
653+ */
654+ if (!cypd_batt_update )
655+ cypd_customize_battery_cap ();
656+
657+ if (battery_design_voltage (& v ) == 0 ) {
658+ if (battery_remaining_capacity (& c ) == 0 ) {
659+ /*
660+ * Wh = (c * v) / 1000000
661+ * 10th of a Wh = Wh * 10
662+ */
663+ soc_wh = DIV_ROUND_NEAREST ((c * v ), 100000 );
664+ }
665+ }
666+
667+ if (battery_status (& c ) != 0 ) {
668+ batt_info = 0 ; /* batt not present */
669+ } else {
670+ if (c & STATUS_FULLY_CHARGED )
671+ /* Fully charged */
672+ batt_info = CCG6_BATT_IS_IDLE | CCG6_BATT_IS_PRESENT ;
673+ else if (c & STATUS_DISCHARGING )
674+ /* Discharging */
675+ batt_info = CCG6_BATT_IS_DISCHARGING | CCG6_BATT_IS_PRESENT ;
676+ else
677+ /* else battery is charging.*/
678+ batt_info = CCG6_BATT_IS_PRESENT ;
679+ }
680+
681+ pd_battery_status .reg = 0x1 ;
682+ pd_battery_status .battery_info = batt_info ;
683+ pd_battery_status .batt_present_cap = soc_wh ;
684+ }
685+
686+ for (i = 0 ; i < PD_CHIP_COUNT ; i ++ )
687+ cypd_write8_reg_block (i , CCG_BATTERT_STATE ,
688+ & pd_battery_status , sizeof (pd_battery_status ));
689+
690+ }
691+ DECLARE_HOOK (HOOK_AC_CHANGE , cypd_customize_battery_status , HOOK_PRIO_DEFAULT );
692+ DECLARE_HOOK (HOOK_BATTERY_SOC_CHANGE , cypd_customize_battery_status , HOOK_PRIO_DEFAULT );
693+ #endif /* CONFIG_PD_CCG6_CUSTOMIZE_BATT_MESSAGE */
0 commit comments