Skip to content

Commit 31fd4b9

Browse files
committed
thermal/drivers/imx_sc: Rely on the platform data to get the resource id
Currently the imx_sc driver is reimplementing part of the thermal zone parsing from the thermal OF tree code to get the sensor id associated with a thermal zone sensor. The driver platform specific code should know what sensor is present and not rely on the thermal zone description to do a discovery. Well that is arguable but all the other drivers have a per platform data telling what sensor id to use. The imx_sc thermal driver is the only one using a different approach. Not invalid but forcing to keep a specific function 'thermal_zone_of_get_sensor_id()' to get the sensor id for a specific thermal zone as the self-explanatory function tells and having device tree code inside the driver. The thermal OF code had a rework and remains now self-encapsulated with a register/unregister functions and their 'devm' variants, except for the function mentioned above. After investigating, it appears the imx_sc sensor is defined in arch/arm64/boot/dts/freescale/imx8qxp.dtsi: which defines the cpu-thermal zone with the id: IMX_SC_R_SYSTEM This dtsi is included by: - imx8qxp-ai_ml.dts - imx8qxp-colibri.dtsi - imx8qxp-mek.dts The two first ones do not define more thermal zones The third one adds the pmic-thermal0 zone with id: IMX_SC_R_PMIC_0 The thermal OF code returns -ENODEV if the thermal zone registration with a specific id fails because the description is not available in the DT for such a sensor id. In this case we continue with the other ids without bailing out with an error. So we can build for the 'fsl,imx-sc-thermal' a compatible data, an array of sensor ids containing IMX_SC_R_SYSTEM and IMX_SC_R_PMIC_0. The latter won't be found but that will not result in an error but a normal case where we continue the initialization with other ids. Just to clarify, it is what the thermal framework does and what the other drivers are expecting: when a registration fails with -ENODEV this is not an error but a case where the description is not found in the device tree, that be can the entire thermal zones description or a specific thermal zone with an unknown id. There is one small functional change but without impact. When there is no 'thermal-zones' description the probe function was returning '-ENODEV', now it returns zero. When a thermal zone fails to register with an error different from '-ENODEV', the error is detected and returned. Change the code accordingly and remove the OF code from the driver. Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Link: https://lore.kernel.org/r/20220818082316.2717095-1-daniel.lezcano@linaro.org
1 parent 2e70ea7 commit 31fd4b9

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

drivers/thermal/imx_sc_thermal.c

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,68 +76,66 @@ static const struct thermal_zone_device_ops imx_sc_thermal_ops = {
7676

7777
static int imx_sc_thermal_probe(struct platform_device *pdev)
7878
{
79-
struct device_node *np, *child, *sensor_np;
8079
struct imx_sc_sensor *sensor;
81-
int ret;
80+
const int *resource_id;
81+
int i, ret;
8282

8383
ret = imx_scu_get_handle(&thermal_ipc_handle);
8484
if (ret)
8585
return ret;
8686

87-
np = of_find_node_by_name(NULL, "thermal-zones");
88-
if (!np)
89-
return -ENODEV;
87+
resource_id = of_device_get_match_data(&pdev->dev);
88+
if (!resource_id)
89+
return -EINVAL;
9090

91-
sensor_np = of_node_get(pdev->dev.of_node);
91+
for (i = 0; resource_id[i] > 0; i++) {
9292

93-
for_each_available_child_of_node(np, child) {
9493
sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL);
95-
if (!sensor) {
96-
of_node_put(child);
97-
ret = -ENOMEM;
98-
goto put_node;
99-
}
94+
if (!sensor)
95+
return -ENOMEM;
10096

101-
ret = thermal_zone_of_get_sensor_id(child,
102-
sensor_np,
103-
&sensor->resource_id);
104-
if (ret < 0) {
105-
dev_err(&pdev->dev,
106-
"failed to get valid sensor resource id: %d\n",
107-
ret);
108-
of_node_put(child);
109-
break;
110-
}
97+
sensor->resource_id = resource_id[i];
11198

112-
sensor->tzd = devm_thermal_of_zone_register(&pdev->dev,
113-
sensor->resource_id,
114-
sensor,
115-
&imx_sc_thermal_ops);
99+
sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, sensor->resource_id,
100+
sensor, &imx_sc_thermal_ops);
116101
if (IS_ERR(sensor->tzd)) {
117-
dev_err(&pdev->dev, "failed to register thermal zone\n");
102+
/*
103+
* Save the error value before freeing the
104+
* sensor pointer, otherwise we endup with a
105+
* use-after-free error
106+
*/
118107
ret = PTR_ERR(sensor->tzd);
119-
of_node_put(child);
120-
break;
108+
109+
devm_kfree(&pdev->dev, sensor);
110+
111+
/*
112+
* The thermal framework notifies us there is
113+
* no thermal zone description for such a
114+
* sensor id
115+
*/
116+
if (ret == -ENODEV)
117+
continue;
118+
119+
dev_err(&pdev->dev, "failed to register thermal zone\n");
120+
return ret;
121121
}
122122

123123
if (devm_thermal_add_hwmon_sysfs(sensor->tzd))
124124
dev_warn(&pdev->dev, "failed to add hwmon sysfs attributes\n");
125125
}
126126

127-
put_node:
128-
of_node_put(sensor_np);
129-
of_node_put(np);
130-
131-
return ret;
127+
return 0;
132128
}
133129

134130
static int imx_sc_thermal_remove(struct platform_device *pdev)
135131
{
136132
return 0;
137133
}
138134

135+
static int imx_sc_sensors[] = { IMX_SC_R_SYSTEM, IMX_SC_R_PMIC_0, -1 };
136+
139137
static const struct of_device_id imx_sc_thermal_table[] = {
140-
{ .compatible = "fsl,imx-sc-thermal", },
138+
{ .compatible = "fsl,imx-sc-thermal", .data = imx_sc_sensors },
141139
{}
142140
};
143141
MODULE_DEVICE_TABLE(of, imx_sc_thermal_table);

0 commit comments

Comments
 (0)