@@ -585,32 +585,19 @@ int power_supply_get_battery_info(struct power_supply *psy,
585
585
{
586
586
struct power_supply_resistance_temp_table * resist_table ;
587
587
struct power_supply_battery_info * info ;
588
- struct device_node * battery_np = NULL ;
589
- struct fwnode_reference_args args ;
590
- struct fwnode_handle * fwnode = NULL ;
588
+ struct fwnode_handle * srcnode , * fwnode ;
591
589
const char * value ;
592
- int err , len , index ;
593
- const __be32 * list ;
590
+ int err , len , index , proplen ;
591
+ u32 * propdata __free ( kfree ) = NULL ;
594
592
u32 min_max [2 ];
595
593
596
- if (psy -> dev .of_node ) {
597
- battery_np = of_parse_phandle (psy -> dev .of_node , "monitored-battery" , 0 );
598
- if (!battery_np )
599
- return - ENODEV ;
594
+ srcnode = dev_fwnode (& psy -> dev );
595
+ if (!srcnode && psy -> dev .parent )
596
+ srcnode = dev_fwnode (psy -> dev .parent );
600
597
601
- fwnode = fwnode_handle_get (of_fwnode_handle (battery_np ));
602
- } else if (psy -> dev .parent ) {
603
- err = fwnode_property_get_reference_args (
604
- dev_fwnode (psy -> dev .parent ),
605
- "monitored-battery" , NULL , 0 , 0 , & args );
606
- if (err )
607
- return err ;
608
-
609
- fwnode = args .fwnode ;
610
- }
611
-
612
- if (!fwnode )
613
- return - ENOENT ;
598
+ fwnode = fwnode_find_reference (srcnode , "monitored-battery" , 0 );
599
+ if (IS_ERR (fwnode ))
600
+ return PTR_ERR (fwnode );
614
601
615
602
err = fwnode_property_read_string (fwnode , "compatible" , & value );
616
603
if (err )
@@ -740,15 +727,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
740
727
info -> temp_max = min_max [1 ];
741
728
}
742
729
743
- /*
744
- * The below code uses raw of-data parsing to parse
745
- * /schemas/types.yaml#/definitions/uint32-matrix
746
- * data, so for now this is only support with of.
747
- */
748
- if (!battery_np )
749
- goto out_ret_pointer ;
750
-
751
- len = of_property_count_u32_elems (battery_np , "ocv-capacity-celsius" );
730
+ len = fwnode_property_count_u32 (fwnode , "ocv-capacity-celsius" );
752
731
if (len < 0 && len != - EINVAL ) {
753
732
err = len ;
754
733
goto out_put_node ;
@@ -757,13 +736,13 @@ int power_supply_get_battery_info(struct power_supply *psy,
757
736
err = - EINVAL ;
758
737
goto out_put_node ;
759
738
} else if (len > 0 ) {
760
- of_property_read_u32_array ( battery_np , "ocv-capacity-celsius" ,
739
+ fwnode_property_read_u32_array ( fwnode , "ocv-capacity-celsius" ,
761
740
info -> ocv_temp , len );
762
741
}
763
742
764
743
for (index = 0 ; index < len ; index ++ ) {
765
744
struct power_supply_battery_ocv_table * table ;
766
- int i , tab_len , size ;
745
+ int i , tab_len ;
767
746
768
747
char * propname __free (kfree ) = kasprintf (GFP_KERNEL , "ocv-capacity-table-%d" ,
769
748
index );
@@ -772,15 +751,28 @@ int power_supply_get_battery_info(struct power_supply *psy,
772
751
err = - ENOMEM ;
773
752
goto out_put_node ;
774
753
}
775
- list = of_get_property ( battery_np , propname , & size );
776
- if (! list || ! size ) {
754
+ proplen = fwnode_property_count_u32 ( fwnode , propname );
755
+ if (proplen < 0 || proplen % 2 != 0 ) {
777
756
dev_err (& psy -> dev , "failed to get %s\n" , propname );
778
757
power_supply_put_battery_info (psy , info );
779
758
err = - EINVAL ;
780
759
goto out_put_node ;
781
760
}
782
761
783
- tab_len = size / (2 * sizeof (__be32 ));
762
+ u32 * propdata __free (kfree ) = kcalloc (proplen , sizeof (* propdata ), GFP_KERNEL );
763
+ if (!propdata ) {
764
+ power_supply_put_battery_info (psy , info );
765
+ err = - EINVAL ;
766
+ goto out_put_node ;
767
+ }
768
+ err = fwnode_property_read_u32_array (fwnode , propname , propdata , proplen );
769
+ if (err < 0 ) {
770
+ dev_err (& psy -> dev , "failed to get %s\n" , propname );
771
+ power_supply_put_battery_info (psy , info );
772
+ goto out_put_node ;
773
+ }
774
+
775
+ tab_len = proplen / 2 ;
784
776
info -> ocv_table_size [index ] = tab_len ;
785
777
786
778
info -> ocv_table [index ] = table =
@@ -792,18 +784,36 @@ int power_supply_get_battery_info(struct power_supply *psy,
792
784
}
793
785
794
786
for (i = 0 ; i < tab_len ; i ++ ) {
795
- table [i ].ocv = be32_to_cpu (* list );
796
- list ++ ;
797
- table [i ].capacity = be32_to_cpu (* list );
798
- list ++ ;
787
+ table [i ].ocv = propdata [i * 2 ];
788
+ table [i ].capacity = propdata [i * 2 + 1 ];
799
789
}
800
790
}
801
791
802
- list = of_get_property (battery_np , "resistance-temp-table" , & len );
803
- if (!list || !len )
792
+ proplen = fwnode_property_count_u32 (fwnode , "resistance-temp-table" );
793
+ if (proplen == 0 || proplen == - EINVAL ) {
794
+ err = 0 ;
804
795
goto out_ret_pointer ;
796
+ } else if (proplen < 0 || proplen % 2 != 0 ) {
797
+ power_supply_put_battery_info (psy , info );
798
+ err = (proplen < 0 ) ? proplen : - EINVAL ;
799
+ goto out_put_node ;
800
+ }
801
+
802
+ propdata = kcalloc (proplen , sizeof (* propdata ), GFP_KERNEL );
803
+ if (!propdata ) {
804
+ power_supply_put_battery_info (psy , info );
805
+ err = - ENOMEM ;
806
+ goto out_put_node ;
807
+ }
808
+
809
+ err = fwnode_property_read_u32_array (fwnode , "resistance-temp-table" ,
810
+ propdata , proplen );
811
+ if (err < 0 ) {
812
+ power_supply_put_battery_info (psy , info );
813
+ goto out_put_node ;
814
+ }
805
815
806
- info -> resist_table_size = len / ( 2 * sizeof ( __be32 )) ;
816
+ info -> resist_table_size = proplen / 2 ;
807
817
info -> resist_table = resist_table = devm_kcalloc (& psy -> dev ,
808
818
info -> resist_table_size ,
809
819
sizeof (* resist_table ),
@@ -815,8 +825,8 @@ int power_supply_get_battery_info(struct power_supply *psy,
815
825
}
816
826
817
827
for (index = 0 ; index < info -> resist_table_size ; index ++ ) {
818
- resist_table [index ].temp = be32_to_cpu ( * list ++ ) ;
819
- resist_table [index ].resistance = be32_to_cpu ( * list ++ ) ;
828
+ resist_table [index ].temp = propdata [ index * 2 ] ;
829
+ resist_table [index ].resistance = propdata [ index * 2 + 1 ] ;
820
830
}
821
831
822
832
out_ret_pointer :
@@ -825,7 +835,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
825
835
826
836
out_put_node :
827
837
fwnode_handle_put (fwnode );
828
- of_node_put (battery_np );
829
838
return err ;
830
839
}
831
840
EXPORT_SYMBOL_GPL (power_supply_get_battery_info );
0 commit comments