11
11
#include "battery.h"
12
12
#include "board_host_command.h"
13
13
#include "board_led.h"
14
+ #include "board_function.h"
14
15
#include "charge_manager.h"
15
16
#include "charge_state.h"
16
17
#include "chipset.h"
@@ -155,6 +156,39 @@ static bool last_kbbl_led_brightness;
155
156
#endif
156
157
static int prev_als_lux ;
157
158
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
+
158
192
int led_get_current_tick_time (void )
159
193
{
160
194
return led_tick_time ;
@@ -189,7 +223,7 @@ int fp_led_auto_is_enable(void)
189
223
*/
190
224
void auto_als_led_brightness (void )
191
225
{
192
- int als_lux = hidals_lux_get ();
226
+ int als_lux = als_lux_get ();
193
227
int led_brightness ;
194
228
#ifdef CONFIG_PLATFORM_EC_KEYBOARD
195
229
int kb_brightness ;
@@ -201,7 +235,8 @@ void auto_als_led_brightness(void)
201
235
return ;
202
236
prev_als_lux = als_lux ;
203
237
204
- if (fp_led_auto_is_enable ()) {
238
+ if (fp_led_auto_is_enable () &&
239
+ chipset_in_state (CHIPSET_STATE_ON )) {
205
240
if (als_lux > 130 )
206
241
led_brightness = FP_LED_HIGH ;
207
242
else if (als_lux > 100 )
@@ -624,7 +659,8 @@ static void led_tick(void)
624
659
else
625
660
led_tick_time = 200 ;
626
661
627
- auto_als_led_brightness ();
662
+ if (IS_ENABLED (CONFIG_PLATFORM_EC_DEDICATED_ALS ))
663
+ auto_als_led_brightness ();
628
664
629
665
board_led_set_color ();
630
666
board_led_apply_color ();
@@ -634,6 +670,18 @@ static void led_tick(void)
634
670
635
671
static void led_hook_init (void )
636
672
{
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
+ }
637
685
hook_call_deferred (& led_tick_data , 200 * MSEC );
638
686
}
639
687
DECLARE_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)
678
726
struct ec_response_fp_led_level_v0 * r_v0 = args -> response ;
679
727
struct ec_response_fp_led_level_v1 * r_v1 = args -> response ;
680
728
uint8_t led_level = FP_LED_HIGH ;
729
+ uint8_t als_auto ;
681
730
731
+ system_get_bbram (SYSTEM_BBRAM_IDX_BIOS_FUNCTION , & als_auto );
682
732
/* Returns percentage in HC v0 and v1 */
683
733
if (p_v0 -> get_led_level ) {
684
734
if (args -> version == 0 ) {
@@ -718,7 +768,7 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
718
768
}
719
769
720
770
if (args -> version == 0 ) {
721
- fp_als_auto_brightness = false ;
771
+ als_auto &= ~ ALS_AUTO_FP ;
722
772
/* HC v0 only allows setting 3 discrete levels */
723
773
switch (p_v0 -> set_led_level ) {
724
774
case FP_LED_BRIGHTNESS_HIGH :
@@ -738,10 +788,8 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
738
788
/* Keep using v0, even though auto is a new value */
739
789
/* v1 is for setting custom percentage */
740
790
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 ;
745
793
default :
746
794
return EC_RES_INVALID_PARAM ;
747
795
}
@@ -750,11 +798,24 @@ static enum ec_status fp_led_level_control(struct host_cmd_handler_args *args)
750
798
if (p_v1 -> set_percentage == 0 || p_v1 -> set_percentage > 100 )
751
799
return EC_RES_INVALID_PARAM ;
752
800
led_level = p_v1 -> set_percentage ;
753
- fp_als_auto_brightness = false ;
801
+ als_auto &= ~ ALS_AUTO_FP ;
754
802
}
755
803
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
+ }
758
819
759
820
return EC_RES_SUCCESS ;
760
821
}
0 commit comments