Skip to content

Commit d0c75fa

Browse files
Daniel Lezcanodlezcano
authored andcommitted
thermal/of: Initialize trip points separately
Self contain the trip initialization from the device tree in a single function for the sake of making the code flow more clear. Cc: Alexandre Bailon <[email protected]> Cc: Kevin Hilman <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Daniel Lezcano <[email protected]>
1 parent 14ccb5e commit d0c75fa

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

drivers/thermal/thermal_of.c

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,8 @@ static int of_find_trip_id(struct device_node *np, struct device_node *trip)
693693
*
694694
* Return: 0 on success, proper error code otherwise
695695
*/
696-
static int thermal_of_populate_bind_params(struct device_node *np,
696+
static int thermal_of_populate_bind_params(struct device_node *tz_np,
697+
struct device_node *np,
697698
struct __thermal_bind_params *__tbp)
698699
{
699700
struct of_phandle_args cooling_spec;
@@ -715,7 +716,7 @@ static int thermal_of_populate_bind_params(struct device_node *np,
715716
return -ENODEV;
716717
}
717718

718-
trip_id = of_find_trip_id(np, trip);
719+
trip_id = of_find_trip_id(tz_np, trip);
719720
if (trip_id < 0) {
720721
ret = trip_id;
721722
goto end;
@@ -849,6 +850,53 @@ static int thermal_of_populate_trip(struct device_node *np,
849850
return 0;
850851
}
851852

853+
static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
854+
{
855+
struct thermal_trip *tt;
856+
struct device_node *trips, *trip;
857+
int ret, count;
858+
859+
trips = of_get_child_by_name(np, "trips");
860+
if (!trips) {
861+
pr_err("Failed to find 'trips' node\n");
862+
return ERR_PTR(-EINVAL);
863+
}
864+
865+
count = of_get_child_count(trips);
866+
if (!count) {
867+
pr_err("No trip point defined\n");
868+
ret = -EINVAL;
869+
goto out_of_node_put;
870+
}
871+
872+
tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
873+
if (!tt) {
874+
ret = -ENOMEM;
875+
goto out_of_node_put;
876+
}
877+
878+
*ntrips = count;
879+
880+
count = 0;
881+
for_each_child_of_node(trips, trip) {
882+
ret = thermal_of_populate_trip(trip, &tt[count++]);
883+
if (ret)
884+
goto out_kfree;
885+
}
886+
887+
of_node_put(trips);
888+
889+
return tt;
890+
891+
out_kfree:
892+
kfree(tt);
893+
*ntrips = 0;
894+
out_of_node_put:
895+
of_node_put(trips);
896+
897+
return ERR_PTR(ret);
898+
}
899+
852900
/**
853901
* thermal_of_build_thermal_zone - parse and fill one thermal zone data
854902
* @np: DT node containing a thermal zone node
@@ -867,7 +915,6 @@ static struct __thermal_zone
867915
__init *thermal_of_build_thermal_zone(struct device_node *np)
868916
{
869917
struct device_node *child = NULL, *gchild;
870-
struct device_node *trips;
871918
struct __thermal_zone *tz;
872919
int ret, i;
873920
u32 prop, coef[2];
@@ -909,28 +956,10 @@ __init *thermal_of_build_thermal_zone(struct device_node *np)
909956
tz->offset = 0;
910957
}
911958

912-
/* trips */
913-
trips = of_get_child_by_name(np, "trips");
914-
915-
/* No trips provided */
916-
if (!trips)
959+
tz->trips = thermal_of_trips_init(np, &tz->ntrips);
960+
if (IS_ERR(tz->trips)) {
961+
ret = PTR_ERR(tz->trips);
917962
goto finish;
918-
919-
tz->ntrips = of_get_child_count(trips);
920-
if (tz->ntrips == 0) /* must have at least one child */
921-
goto finish;
922-
923-
tz->trips = kcalloc(tz->ntrips, sizeof(*tz->trips), GFP_KERNEL);
924-
if (!tz->trips) {
925-
ret = -ENOMEM;
926-
goto free_tz;
927-
}
928-
929-
i = 0;
930-
for_each_child_of_node(trips, gchild) {
931-
ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
932-
if (ret)
933-
goto free_trips;
934963
}
935964

936965
/* cooling-maps */
@@ -952,13 +981,14 @@ __init *thermal_of_build_thermal_zone(struct device_node *np)
952981

953982
i = 0;
954983
for_each_child_of_node(child, gchild) {
955-
ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++]);
956-
if (ret)
984+
ret = thermal_of_populate_bind_params(np, gchild, &tz->tbps[i++]);
985+
if (ret) {
986+
of_node_put(gchild);
957987
goto free_tbps;
988+
}
958989
}
959990

960991
finish:
961-
of_node_put(trips);
962992
of_node_put(child);
963993

964994
return tz;
@@ -977,8 +1007,6 @@ __init *thermal_of_build_thermal_zone(struct device_node *np)
9771007
kfree(tz->tbps);
9781008
free_trips:
9791009
kfree(tz->trips);
980-
of_node_put(trips);
981-
of_node_put(gchild);
9821010
free_tz:
9831011
kfree(tz);
9841012
of_node_put(child);

0 commit comments

Comments
 (0)