@@ -72,9 +72,10 @@ LOG_MODULE_REGISTER(hid_target);
7272 */
7373#define ALS_HID_UNIT 0x00
7474
75- #define HID_ALS_MAX 65535
75+ #define HID_ALS_MAX 33000
7676#define HID_ALS_MIN 0
77- #define HID_ALS_SENSITIVITY 10
77+ /* Note sensitivity is scaled by exponent 0.01*/
78+ #define HID_ALS_SENSITIVITY 100
7879
7980/* HID_USAGE_SENSOR_PROPERTY_SENSOR_CONNECTION_TYPE */
8081#define HID_INTEGRATED 1
@@ -438,25 +439,23 @@ void hid_airplane(bool pressed)
438439}
439440#endif
440441
441- static void irq_als (const struct device * dev )
442+
443+ static void hid_target_als_irq (void )
442444{
445+ const struct device * dev = DEVICE_DT_GET (DT_NODELABEL (i2chid1 ));
443446 struct i2c_hid_target_data * data = dev -> data ;
444447
448+ data -> report_id = REPORT_ID_SENSOR ;
445449 gpio_pin_set_dt (data -> alert_gpio , 0 );
446450}
447451
448- static void hid_target_als_irq (void )
449- {
450- irq_als (DEVICE_DT_GET (DT_NODELABEL (i2chid1 )));
451- }
452-
453452void i2c_hid_als_init (void )
454453{
455454 als_feature .connection_type = HID_INTEGRATED ;
456455 als_feature .reporting_state = HID_ALL_EVENTS ;
457456 als_feature .power_state = HID_D0_FULL_POWER ;
458457 als_feature .sensor_state = HID_READY ;
459- als_feature .report_interval = 100 ;
458+ als_feature .report_interval = 1000 ;
460459 als_feature .sensitivity = HID_ALS_SENSITIVITY ;
461460 als_feature .maximum = HID_ALS_MAX ;
462461 als_feature .minimum = HID_ALS_MIN ;
@@ -467,62 +466,67 @@ void i2c_hid_als_init(void)
467466}
468467DECLARE_HOOK (HOOK_INIT , i2c_hid_als_init , HOOK_PRIO_DEFAULT );
469468
470- static int als_polling_mode_count ;
471469void report_illuminance_value (void );
472470DECLARE_DEFERRED (report_illuminance_value );
473471void report_illuminance_value (void )
474472{
475473 uint16_t newIlluminaceValue = * (uint16_t * )host_get_memmap (EC_MEMMAP_ALS );
476- static int granularity ;
477-
474+ int granularity = als_feature . sensitivity * ( HID_ALS_MAX / 10000 ) ;
475+ uint32_t report_interval = als_feature . report_interval ;
478476 /* We need to polling the ALS value at least 6 seconds */
479- if (als_polling_mode_count <= 60 ) {
480- als_polling_mode_count ++ ; /* time base 100ms */
481-
482- /* bypass the EC_MEMMAP_ALS value to input report */
477+ switch (als_feature .reporting_state ) {
478+ case HID_ALL_EVENTS :
479+ case HID_ALL_EVENTS_WAKE :
483480 als_sensor .illuminanceValue = newIlluminaceValue ;
484481 hid_target_als_irq ();
485- } else {
482+ break ;
483+ case HID_THRESHOLD_EVENTS :
484+ case HID_THRESHOLD_EVENTS_WAKE :
486485 if (ABS (als_sensor .illuminanceValue - newIlluminaceValue ) > granularity ) {
487486 als_sensor .illuminanceValue = newIlluminaceValue ;
488487 hid_target_als_irq ();
489488 }
489+ break ;
490+ default :
491+ break ;
490492 }
491- /**
492- * To ensure the best experience the ALS should have a granularity of
493- * at most 1 lux when the ambient light is below 25 lux and a granularity
494- * of at most 4% of the ambient light when it is above 25 lux.
495- * This enable the adaptive brightness algorithm to perform smooth screen
496- * brightness transitions.
497- */
498- if (newIlluminaceValue < 25 )
499- granularity = 1 ;
500- else
501- granularity = newIlluminaceValue * 4 /100 ;
502493
494+ if (report_interval == 0 ) {
495+ /* per hid spec report interval should be sensor default when 0 */
496+ report_interval = 250 ;
497+ }
503498 hook_call_deferred (& report_illuminance_value_data ,
504- (( int ) als_feature . report_interval ) * MSEC );
499+ report_interval * MSEC );
505500
506501}
507502
508503
509504static void als_report_control (uint8_t report_mode )
510505{
511- if (report_mode == 0x01 ) {
512- /* als report mode = polling */
506+ if (report_mode ) {
513507 hook_call_deferred (& report_illuminance_value_data ,
514508 ((int ) als_feature .report_interval ) * MSEC );
515- } else if (report_mode == 0x02 ) {
516- /* als report mode = threshold */
517- ;
518509 } else {
519510 /* stop report als value */
520511 hook_call_deferred (& report_illuminance_value_data , -1 );
521- als_polling_mode_count = 0 ;
522512 }
523513}
524514
525515
516+
517+ static void als_shutdown (void )
518+ {
519+ const struct device * dev = DEVICE_DT_GET (DT_NODELABEL (i2chid1 ));
520+ struct i2c_hid_target_data * data = dev -> data ;
521+
522+ als_feature .power_state = HID_D4_POWER_OFF ;
523+
524+ als_report_control (ALS_REPORT_STOP );
525+
526+ gpio_pin_set_dt (data -> alert_gpio , 1 );
527+ }
528+ DECLARE_HOOK (HOOK_CHIPSET_SHUTDOWN , als_shutdown , HOOK_PRIO_DEFAULT );
529+
526530static void extract_report (size_t len , const uint8_t * buffer , void * data ,
527531 size_t data_len )
528532{
@@ -641,6 +645,11 @@ static int hid_target_process_write(struct i2c_target_config *config)
641645 data -> buffer ,
642646 & als_feature ,
643647 sizeof (struct als_feature_report ));
648+ if (als_feature .power_state == HID_D4_POWER_OFF ) {
649+ als_shutdown ();
650+ } else {
651+ als_report_control (ALS_REPORT_POLLING );
652+ }
644653 break ;
645654 default :
646655 break ;
@@ -657,7 +666,7 @@ static int hid_target_process_write(struct i2c_target_config *config)
657666 i2c_hid_als_init ();
658667 als_report_control (ALS_REPORT_POLLING );
659668 } else
660- als_report_control ( ALS_REPORT_STOP );
669+ als_shutdown ( );
661670
662671 break ;
663672 default :
@@ -769,6 +778,19 @@ static int hid_target_read_requested(struct i2c_target_config *config,
769778}
770779
771780
781+ static int cmd_hid_status (int argc , const char * * argv )
782+ {
783+ ccprintf ("ALS Feature\n" );
784+ ccprintf (" Power:%d\n" , als_feature .power_state );
785+ ccprintf (" Interval:%dms\n" , als_feature .report_interval );
786+ ccprintf (" sensitivity:%d\n" , als_feature .sensitivity );
787+ ccprintf (" report_state:%d\n" , als_feature .reporting_state );
788+
789+ return EC_SUCCESS ;
790+ }
791+ DECLARE_CONSOLE_COMMAND (hidstatus , cmd_hid_status , "" ,
792+ "Get hid device status" );
793+
772794static int hid_target_stop (struct i2c_target_config * config )
773795{
774796 struct i2c_hid_target_data * data = CONTAINER_OF (config ,
@@ -840,7 +862,7 @@ static int i2c_hid_target_init(const struct device *dev)
840862 data -> descriptor_size = cfg -> descriptor_size ;
841863 data -> report_id = 0 ;
842864
843- als_report_control (ALS_REPORT_POLLING );
865+ als_report_control (ALS_REPORT_STOP );
844866 return 0 ;
845867}
846868
0 commit comments