]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
thermal/drivers/thermal-generic-adc: Add temperature sensor channel
authorSvyatoslav Ryhel <clamor95@gmail.com>
Wed, 3 Sep 2025 16:27:49 +0000 (19:27 +0300)
committerDaniel Lezcano <daniel.lezcano@linaro.org>
Thu, 25 Sep 2025 20:11:00 +0000 (22:11 +0200)
To avoid duplicating sensor functionality and conversion tables, this
design allows converting an ADC IIO channel's output directly into a
temperature IIO channel. This is particularly useful for devices where
hwmon isn't suitable or where temperature data must be accessible through
IIO.

One such device is, for example, the MAX17040 fuel gauge.

Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://lore.kernel.org/r/20250903162749.109910-2-clamor95@gmail.com
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
drivers/thermal/thermal-generic-adc.c

index ee3d0aa31406cdd51955e5f6f19bd8872bc60a4a..7c844589b153b59bba229f15de5e8f925759489e 100644 (file)
@@ -7,6 +7,7 @@
  * Author: Laxman Dewangan <ldewangan@nvidia.com>
  */
 #include <linux/iio/consumer.h>
+#include <linux/iio/iio.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -73,6 +74,58 @@ static const struct thermal_zone_device_ops gadc_thermal_ops = {
        .get_temp = gadc_thermal_get_temp,
 };
 
+static const struct iio_chan_spec gadc_thermal_iio_channels[] = {
+       {
+               .type = IIO_TEMP,
+               .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+       }
+};
+
+static int gadc_thermal_read_raw(struct iio_dev *indio_dev,
+                                struct iio_chan_spec const *chan,
+                                int *val, int *val2, long mask)
+{
+       struct gadc_thermal_info *gtinfo = iio_priv(indio_dev);
+       int ret;
+
+       switch (mask) {
+       case IIO_CHAN_INFO_PROCESSED:
+               ret = gadc_thermal_get_temp(gtinfo->tz_dev, val);
+               if (ret)
+                       return ret;
+
+               return IIO_VAL_INT;
+
+       default:
+               return -EINVAL;
+       }
+}
+
+static const struct iio_info gadc_thermal_iio_info = {
+       .read_raw = gadc_thermal_read_raw,
+};
+
+static int gadc_iio_register(struct device *dev, struct gadc_thermal_info *gti)
+{
+       struct gadc_thermal_info *gtinfo;
+       struct iio_dev *indio_dev;
+
+       indio_dev = devm_iio_device_alloc(dev, sizeof(*gtinfo));
+       if (!indio_dev)
+               return -ENOMEM;
+
+       gtinfo = iio_priv(indio_dev);
+       memcpy(gtinfo, gti, sizeof(*gtinfo));
+
+       indio_dev->name = dev_name(dev);
+       indio_dev->info = &gadc_thermal_iio_info;
+       indio_dev->modes = INDIO_DIRECT_MODE;
+       indio_dev->channels = gadc_thermal_iio_channels;
+       indio_dev->num_channels = ARRAY_SIZE(gadc_thermal_iio_channels);
+
+       return devm_iio_device_register(dev, indio_dev);
+}
+
 static int gadc_thermal_read_linear_lookup_table(struct device *dev,
                                                 struct gadc_thermal_info *gti)
 {
@@ -153,7 +206,7 @@ static int gadc_thermal_probe(struct platform_device *pdev)
 
        devm_thermal_add_hwmon_sysfs(dev, gti->tz_dev);
 
-       return 0;
+       return gadc_iio_register(&pdev->dev, gti);
 }
 
 static const struct of_device_id of_adc_thermal_match[] = {