diff mbox series

thermal: k3: Add hwmon support

Message ID 20220210164756.3489848-1-massimiliano.minella@gmail.com (mailing list archive)
State Superseded, archived
Headers show
Series thermal: k3: Add hwmon support | expand

Commit Message

Massimiliano Minella Feb. 10, 2022, 4:47 p.m. UTC
Expose the thermal sensors on K3 AM654 as hwmon devices, so that
temperatures could be read using lm-sensors.
Use devm_add_action_or_reset() to unregister the hwmon interface
automatically.

Signed-off-by: Massimiliano Minella <massimiliano.minella@gmail.com>
---
 drivers/thermal/k3_bandgap.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)


base-commit: e5b54867f47f765fcb439e09ed763b5de617af3e

Comments

Daniel Lezcano Feb. 25, 2022, 11:49 a.m. UTC | #1
On 10/02/2022 17:47, Massimiliano Minella wrote:
> Expose the thermal sensors on K3 AM654 as hwmon devices, so that
> temperatures could be read using lm-sensors.
> Use devm_add_action_or_reset() to unregister the hwmon interface
> automatically.
> 
> Signed-off-by: Massimiliano Minella <massimiliano.minella@gmail.com>
> ---

[ ... ]

>   		}
> +
> +		data[id].tzd->tzp->no_hwmon = false;
> +		ret = thermal_add_hwmon_sysfs(data[id].tzd);
> +		if (ret) {
> +			dev_err(dev, "thermal failed to add hwmon sysfs\n");
> +			goto err_alloc;
> +		}
> +
> +		ret = devm_add_action_or_reset(dev,
> +					       k3_hwmon_action,
> +					       data[id].tzd);
> +		if (ret)
> +			goto err_alloc;

check out devm_thermal_add_hwmon_sysfs

>   	}
>   
>   	platform_set_drvdata(pdev, bgp);
> 
> base-commit: e5b54867f47f765fcb439e09ed763b5de617af3e
Massimiliano Minella Feb. 28, 2022, 4:23 p.m. UTC | #2
Hi Daniel,
thank you for the review.

On Fri, Feb 25, 2022 at 12:49 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
> On 10/02/2022 17:47, Massimiliano Minella wrote:
...
> >               }
> > +
> > +             data[id].tzd->tzp->no_hwmon = false;
> > +             ret = thermal_add_hwmon_sysfs(data[id].tzd);
> > +             if (ret) {
> > +                     dev_err(dev, "thermal failed to add hwmon sysfs\n");
> > +                     goto err_alloc;
> > +             }
> > +
> > +             ret = devm_add_action_or_reset(dev,
> > +                                            k3_hwmon_action,
> > +                                            data[id].tzd);
> > +             if (ret)
> > +                     goto err_alloc;
>
> check out devm_thermal_add_hwmon_sysfs

I've tried to use the devm_thermal_add_hwmon_sysfs but it is not working as I
would have expected: if the driver is built as a module, when the module is
removed the devm_thermal_hwmon_release never gets called.

As a consequence each time the insmod/rmmod sequence is repeated, for every
thermal zone, new temp<N>_crit and temp<N>_input entries appear in the hwmon
sysfs.

Is it a known bug/limitation?

I'm going to have a look at this behavior, so any hint would be appreciated.

In the meantime I can provide V2 of the patch if the described behavior is
acceptable.

Thanks,
Massimiliano
diff mbox series

Patch

diff --git a/drivers/thermal/k3_bandgap.c b/drivers/thermal/k3_bandgap.c
index 35f41e8a0b75..94678085dd5c 100644
--- a/drivers/thermal/k3_bandgap.c
+++ b/drivers/thermal/k3_bandgap.c
@@ -16,6 +16,8 @@ 
 #include <linux/thermal.h>
 #include <linux/types.h>
 
+#include "thermal_hwmon.h"
+
 #define K3_VTM_DEVINFO_PWR0_OFFSET		0x4
 #define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK	0xf0
 #define K3_VTM_TMPSENS0_CTRL_OFFSET	0x80
@@ -149,6 +151,13 @@  static int k3_thermal_get_temp(void *devdata, int *temp)
 	return ret;
 }
 
+static void k3_hwmon_action(void *data)
+{
+	struct thermal_zone_device *zone = data;
+
+	thermal_remove_hwmon_sysfs(zone);
+}
+
 static const struct thermal_zone_of_device_ops k3_of_thermal_ops = {
 	.get_temp = k3_thermal_get_temp,
 };
@@ -219,6 +228,19 @@  static int k3_bandgap_probe(struct platform_device *pdev)
 			ret = PTR_ERR(data[id].tzd);
 			goto err_alloc;
 		}
+
+		data[id].tzd->tzp->no_hwmon = false;
+		ret = thermal_add_hwmon_sysfs(data[id].tzd);
+		if (ret) {
+			dev_err(dev, "thermal failed to add hwmon sysfs\n");
+			goto err_alloc;
+		}
+
+		ret = devm_add_action_or_reset(dev,
+					       k3_hwmon_action,
+					       data[id].tzd);
+		if (ret)
+			goto err_alloc;
 	}
 
 	platform_set_drvdata(pdev, bgp);