Skip to content

Commit 0b0300f

Browse files
LeoCX-Tsaiquinchou77
authored andcommitted
fwk: main: fix auto als powerbtn led behavior
when driver is not ready ALS driver won't return lux data this CL change to use ALS sensor lux data, and use a default level(high) if ALS lux data not ready BRANCH=fwk-main BUG=https://app.clickup.com/t/86et2uph0 TEST=on lilac, into setup menu enable power button led option to auto, 1. check when bootup hide the ALS sensor(make lux data to zero) the power btn LED still keep at high level. 2. after bootup hide the ALS sensor and shutdown unit then bootup again power btn LED keep high level 3. after bootup hide the ALS sensor and reset EC by console reboot then bootup again check power btn LED keep high level Signed-off-by: LeoCX_Tsai <[email protected]> (cherry picked from commit 948e3320f634a69d75b6943174cdc21962991fa4)
1 parent a597bf2 commit 0b0300f

File tree

4 files changed

+77
-18
lines changed

4 files changed

+77
-18
lines changed

zephyr/program/framework/include/board_function.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ enum function_type {
1313
TYPE_FLASH = 2,
1414
};
1515

16+
enum bios_option {
17+
/* for bios option switch */
18+
ALS_AUTO_FP = BIT(0),
19+
};
20+
1621
/* set bios function bit to bbram when ACPI_DRIVER_READY */
1722
void bios_function_detect(void);
1823

zephyr/program/framework/include/hid_device.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,4 @@ int hid_target_register(const struct device *dev);
2222

2323
int hid_target_unregister(const struct device *dev);
2424

25-
int hidals_lux_get(void);
26-
2725
#endif /* __CROS_EC_I2C_HID_DEVICE_H */

zephyr/program/framework/src/hid_device.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,6 @@ static int hid_target_read_requested(struct i2c_target_config *config,
809809
return hid_target_read_processed(config, val);
810810
}
811811

812-
int hidals_lux_get(void)
813-
{
814-
return als_sensor.illuminanceValue;
815-
}
816-
817812
static int cmd_hidals_status(int argc, const char **argv)
818813
{
819814
int i;

zephyr/program/framework/src/led.c

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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
156157
static 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+
158192
int led_get_current_tick_time(void)
159193
{
160194
return led_tick_time;
@@ -189,7 +223,7 @@ int fp_led_auto_is_enable(void)
189223
*/
190224
void 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

635671
static 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
}
639687
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)
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

Comments
 (0)