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