Skip to content

Commit 570ba04

Browse files
committed
power: supply: core: battery-info: fully switch to fwnode
Also use fwnode based parsing for "ocv-capacity-celsius" and "resistance-temp-table", so that any DT specific bits are removed from the power-supply core. Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]> Link: https://lore.kernel.org/r/20250430-psy-core-convert-to-fwnode-v2-3-f9643b958677@collabora.com Co-developed-by: Hans de Goede <[email protected]> Signed-off-by: Hans de Goede <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]>
1 parent 520c790 commit 570ba04

File tree

1 file changed

+55
-46
lines changed

1 file changed

+55
-46
lines changed

drivers/power/supply/power_supply_core.c

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -585,32 +585,19 @@ int power_supply_get_battery_info(struct power_supply *psy,
585585
{
586586
struct power_supply_resistance_temp_table *resist_table;
587587
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;
591589
const char *value;
592-
int err, len, index;
593-
const __be32 *list;
590+
int err, len, index, proplen;
591+
u32 *propdata __free(kfree) = NULL;
594592
u32 min_max[2];
595593

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);
600597

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);
614601

615602
err = fwnode_property_read_string(fwnode, "compatible", &value);
616603
if (err)
@@ -740,15 +727,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
740727
info->temp_max = min_max[1];
741728
}
742729

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");
752731
if (len < 0 && len != -EINVAL) {
753732
err = len;
754733
goto out_put_node;
@@ -757,13 +736,13 @@ int power_supply_get_battery_info(struct power_supply *psy,
757736
err = -EINVAL;
758737
goto out_put_node;
759738
} 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",
761740
info->ocv_temp, len);
762741
}
763742

764743
for (index = 0; index < len; index++) {
765744
struct power_supply_battery_ocv_table *table;
766-
int i, tab_len, size;
745+
int i, tab_len;
767746

768747
char *propname __free(kfree) = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d",
769748
index);
@@ -772,15 +751,28 @@ int power_supply_get_battery_info(struct power_supply *psy,
772751
err = -ENOMEM;
773752
goto out_put_node;
774753
}
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) {
777756
dev_err(&psy->dev, "failed to get %s\n", propname);
778757
power_supply_put_battery_info(psy, info);
779758
err = -EINVAL;
780759
goto out_put_node;
781760
}
782761

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;
784776
info->ocv_table_size[index] = tab_len;
785777

786778
info->ocv_table[index] = table =
@@ -792,18 +784,36 @@ int power_supply_get_battery_info(struct power_supply *psy,
792784
}
793785

794786
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];
799789
}
800790
}
801791

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;
804795
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+
}
805815

806-
info->resist_table_size = len / (2 * sizeof(__be32));
816+
info->resist_table_size = proplen / 2;
807817
info->resist_table = resist_table = devm_kcalloc(&psy->dev,
808818
info->resist_table_size,
809819
sizeof(*resist_table),
@@ -815,8 +825,8 @@ int power_supply_get_battery_info(struct power_supply *psy,
815825
}
816826

817827
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];
820830
}
821831

822832
out_ret_pointer:
@@ -825,7 +835,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
825835

826836
out_put_node:
827837
fwnode_handle_put(fwnode);
828-
of_node_put(battery_np);
829838
return err;
830839
}
831840
EXPORT_SYMBOL_GPL(power_supply_get_battery_info);

0 commit comments

Comments
 (0)