@@ -510,74 +510,50 @@ static int bq25890_get_chip_state(struct bq25890_device *bq,
510
510
return 0 ;
511
511
}
512
512
513
- static bool bq25890_state_changed (struct bq25890_device * bq ,
514
- struct bq25890_state * new_state )
515
- {
516
- struct bq25890_state old_state ;
517
-
518
- mutex_lock (& bq -> lock );
519
- old_state = bq -> state ;
520
- mutex_unlock (& bq -> lock );
521
-
522
- return (old_state .chrg_status != new_state -> chrg_status ||
523
- old_state .chrg_fault != new_state -> chrg_fault ||
524
- old_state .online != new_state -> online ||
525
- old_state .bat_fault != new_state -> bat_fault ||
526
- old_state .boost_fault != new_state -> boost_fault ||
527
- old_state .vsys_status != new_state -> vsys_status );
528
- }
529
-
530
- static void bq25890_handle_state_change (struct bq25890_device * bq ,
531
- struct bq25890_state * new_state )
513
+ static irqreturn_t __bq25890_handle_irq (struct bq25890_device * bq )
532
514
{
515
+ struct bq25890_state new_state ;
533
516
int ret ;
534
- struct bq25890_state old_state ;
535
517
536
- mutex_lock ( & bq -> lock );
537
- old_state = bq -> state ;
538
- mutex_unlock ( & bq -> lock ) ;
518
+ ret = bq25890_get_chip_state ( bq , & new_state );
519
+ if ( ret < 0 )
520
+ return IRQ_NONE ;
539
521
540
- if (!new_state -> online ) { /* power removed */
522
+ if (!memcmp (& bq -> state , & new_state , sizeof (new_state )))
523
+ return IRQ_NONE ;
524
+
525
+ if (!new_state .online && bq -> state .online ) { /* power removed */
541
526
/* disable ADC */
542
527
ret = bq25890_field_write (bq , F_CONV_START , 0 );
543
528
if (ret < 0 )
544
529
goto error ;
545
- } else if (! old_state .online ) { /* power inserted */
530
+ } else if (new_state . online && ! bq -> state .online ) { /* power inserted */
546
531
/* enable ADC, to have control of charge current/voltage */
547
532
ret = bq25890_field_write (bq , F_CONV_START , 1 );
548
533
if (ret < 0 )
549
534
goto error ;
550
535
}
551
536
552
- return ;
537
+ bq -> state = new_state ;
538
+ power_supply_changed (bq -> charger );
553
539
540
+ return IRQ_HANDLED ;
554
541
error :
555
- dev_err (bq -> dev , "Error communicating with the chip.\n" );
542
+ dev_err (bq -> dev , "Error communicating with the chip: %pe\n" ,
543
+ ERR_PTR (ret ));
544
+ return IRQ_HANDLED ;
556
545
}
557
546
558
547
static irqreturn_t bq25890_irq_handler_thread (int irq , void * private )
559
548
{
560
549
struct bq25890_device * bq = private ;
561
- int ret ;
562
- struct bq25890_state state ;
563
-
564
- ret = bq25890_get_chip_state (bq , & state );
565
- if (ret < 0 )
566
- goto handled ;
567
-
568
- if (!bq25890_state_changed (bq , & state ))
569
- goto handled ;
570
-
571
- bq25890_handle_state_change (bq , & state );
550
+ irqreturn_t ret ;
572
551
573
552
mutex_lock (& bq -> lock );
574
- bq -> state = state ;
553
+ ret = __bq25890_handle_irq ( bq ) ;
575
554
mutex_unlock (& bq -> lock );
576
555
577
- power_supply_changed (bq -> charger );
578
-
579
- handled :
580
- return IRQ_HANDLED ;
556
+ return ret ;
581
557
}
582
558
583
559
static int bq25890_chip_reset (struct bq25890_device * bq )
@@ -607,7 +583,6 @@ static int bq25890_hw_init(struct bq25890_device *bq)
607
583
{
608
584
int ret ;
609
585
int i ;
610
- struct bq25890_state state ;
611
586
612
587
const struct {
613
588
enum bq25890_fields id ;
@@ -655,16 +630,12 @@ static int bq25890_hw_init(struct bq25890_device *bq)
655
630
return ret ;
656
631
}
657
632
658
- ret = bq25890_get_chip_state (bq , & state );
633
+ ret = bq25890_get_chip_state (bq , & bq -> state );
659
634
if (ret < 0 ) {
660
635
dev_dbg (bq -> dev , "Get state failed %d\n" , ret );
661
636
return ret ;
662
637
}
663
638
664
- mutex_lock (& bq -> lock );
665
- bq -> state = state ;
666
- mutex_unlock (& bq -> lock );
667
-
668
639
return 0 ;
669
640
}
670
641
@@ -1001,19 +972,16 @@ static int bq25890_suspend(struct device *dev)
1001
972
static int bq25890_resume (struct device * dev )
1002
973
{
1003
974
int ret ;
1004
- struct bq25890_state state ;
1005
975
struct bq25890_device * bq = dev_get_drvdata (dev );
1006
976
1007
- ret = bq25890_get_chip_state (bq , & state );
977
+ mutex_lock (& bq -> lock );
978
+
979
+ ret = bq25890_get_chip_state (bq , & bq -> state );
1008
980
if (ret < 0 )
1009
981
return ret ;
1010
982
1011
- mutex_lock (& bq -> lock );
1012
- bq -> state = state ;
1013
- mutex_unlock (& bq -> lock );
1014
-
1015
983
/* Re-enable ADC only if charger is plugged in. */
1016
- if (state .online ) {
984
+ if (bq -> state .online ) {
1017
985
ret = bq25890_field_write (bq , F_CONV_START , 1 );
1018
986
if (ret < 0 )
1019
987
return ret ;
@@ -1022,6 +990,8 @@ static int bq25890_resume(struct device *dev)
1022
990
/* signal userspace, maybe state changed while suspended */
1023
991
power_supply_changed (bq -> charger );
1024
992
993
+ mutex_unlock (& bq -> lock );
994
+
1025
995
return 0 ;
1026
996
}
1027
997
#endif
0 commit comments