1111#include "battery.h"
1212#include "board_host_command.h"
1313#include "board_led.h"
14+ #include "board_function.h"
1415#include "charge_manager.h"
1516#include "charge_state.h"
1617#include "chipset.h"
@@ -155,6 +156,39 @@ static bool last_kbbl_led_brightness;
155156#endif
156157static int prev_als_lux ;
157158
159+ static bool als_stable ;
160+ static timestamp_t als_init_deadline ;
161+
162+ static int als_lux_get (void )
163+ {
164+ uint16_t real_illuminance = * (uint16_t * )host_get_memmap (EC_MEMMAP_ALS );
165+ timestamp_t now = get_time ();
166+
167+ /*
168+ * before als_task stable return data (als.c common)
169+ * keep 2 second of the lightness to default high
170+ **/
171+ if (chipset_in_state (CHIPSET_STATE_ON ) && !als_stable ) {
172+ if (now .val > als_init_deadline .val + 2 * SECOND ) {
173+ als_stable = true;
174+ als_init_deadline .val = 0 ;
175+ }
176+ real_illuminance = 1000 ;
177+ } else if (chipset_in_state (CHIPSET_STATE_ANY_OFF )) {
178+ als_init_deadline .val = now .val ;
179+ if (als_stable ) {
180+ als_stable = false;
181+ real_illuminance = 0 ;
182+ /* clear als data and set default level for next time bootup */
183+ * (uint16_t * )host_get_memmap (EC_MEMMAP_ALS ) = 0 ;
184+ system_set_bbram (SYSTEM_BBRAM_IDX_FP_LED_LEVEL , FP_LED_HIGH );
185+ update_pwr_led_level ();
186+ }
187+ }
188+
189+ return real_illuminance ;
190+ }
191+
158192int led_get_current_tick_time (void )
159193{
160194 return led_tick_time ;
@@ -189,7 +223,7 @@ int fp_led_auto_is_enable(void)
189223 */
190224void auto_als_led_brightness (void )
191225{
192- int als_lux = hidals_lux_get ();
226+ int als_lux = als_lux_get ();
193227 int led_brightness ;
194228#ifdef CONFIG_PLATFORM_EC_KEYBOARD
195229 int kb_brightness ;
@@ -201,7 +235,8 @@ void auto_als_led_brightness(void)
201235 return ;
202236 prev_als_lux = als_lux ;
203237
204- if (fp_led_auto_is_enable ()) {
238+ if (fp_led_auto_is_enable () &&
239+ chipset_in_state (CHIPSET_STATE_ON )) {
205240 if (als_lux > 130 )
206241 led_brightness = FP_LED_HIGH ;
207242 else if (als_lux > 100 )
@@ -624,7 +659,8 @@ static void led_tick(void)
624659 else
625660 led_tick_time = 200 ;
626661
627- auto_als_led_brightness ();
662+ if (IS_ENABLED (CONFIG_PLATFORM_EC_DEDICATED_ALS ))
663+ auto_als_led_brightness ();
628664
629665 board_led_set_color ();
630666 board_led_apply_color ();
@@ -634,6 +670,18 @@ static void led_tick(void)
634670
635671static void led_hook_init (void )
636672{
673+ uint8_t als_auto ;
674+
675+ system_get_bbram (SYSTEM_BBRAM_IDX_BIOS_FUNCTION , & als_auto );
676+ if (als_auto & ALS_AUTO_FP ) {
677+ /*
678+ * if enable auto als fp, set the default level to high
679+ * and call the update level to update pwm duty
680+ **/
681+ system_set_bbram (SYSTEM_BBRAM_IDX_FP_LED_LEVEL , FP_LED_HIGH );
682+ update_pwr_led_level ();
683+ fp_als_auto_brightness = true;
684+ }
637685 hook_call_deferred (& led_tick_data , 200 * MSEC );
638686}
639687DECLARE_HOOK (HOOK_INIT , led_hook_init , HOOK_PRIO_DEFAULT );
@@ -678,7 +726,9 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
678726 struct ec_response_fp_led_level_v0 * r_v0 = args -> response ;
679727 struct ec_response_fp_led_level_v1 * r_v1 = args -> response ;
680728 uint8_t led_level = FP_LED_HIGH ;
729+ uint8_t als_auto ;
681730
731+ system_get_bbram (SYSTEM_BBRAM_IDX_BIOS_FUNCTION , & als_auto );
682732 /* Returns percentage in HC v0 and v1 */
683733 if (p_v0 -> get_led_level ) {
684734 if (args -> version == 0 ) {
@@ -718,7 +768,7 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
718768 }
719769
720770 if (args -> version == 0 ) {
721- fp_als_auto_brightness = false ;
771+ als_auto &= ~ ALS_AUTO_FP ;
722772 /* HC v0 only allows setting 3 discrete levels */
723773 switch (p_v0 -> set_led_level ) {
724774 case FP_LED_BRIGHTNESS_HIGH :
@@ -738,10 +788,8 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
738788 /* Keep using v0, even though auto is a new value */
739789 /* v1 is for setting custom percentage */
740790 case FP_LED_BRIGHTNESS_AUTO :
741- fp_als_auto_brightness = true;
742- /* If setting to auto, don't need to update the led_level now */
743- /* it'll be updated later in the periodic task based on ALS value */
744- return EC_RES_SUCCESS ;
791+ als_auto |= ALS_AUTO_FP ;
792+ break ;
745793 default :
746794 return EC_RES_INVALID_PARAM ;
747795 }
@@ -750,11 +798,24 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
750798 if (p_v1 -> set_percentage == 0 || p_v1 -> set_percentage > 100 )
751799 return EC_RES_INVALID_PARAM ;
752800 led_level = p_v1 -> set_percentage ;
753- fp_als_auto_brightness = false ;
801+ als_auto &= ~ ALS_AUTO_FP ;
754802 }
755803
756- system_set_bbram (SYSTEM_BBRAM_IDX_FP_LED_LEVEL , led_level );
757- update_pwr_led_level ();
804+ /*
805+ * save option setting for next time boot
806+ * also make this time setting on work.
807+ *
808+ * If setting to auto, don't need to update the led_level now
809+ * it'll be updated later in the periodic task based on ALS value
810+ **/
811+ system_set_bbram (SYSTEM_BBRAM_IDX_BIOS_FUNCTION , als_auto );
812+ if (als_auto & ALS_AUTO_FP ) {
813+ fp_als_auto_brightness = true;
814+ } else {
815+ fp_als_auto_brightness = false;
816+ system_set_bbram (SYSTEM_BBRAM_IDX_FP_LED_LEVEL , led_level );
817+ update_pwr_led_level ();
818+ }
758819
759820 return EC_RES_SUCCESS ;
760821}
0 commit comments