[v1,1/1] thermal: thermal-generic-adc: add temperature sensor function

Message ID 20240210175922.137704-2-clamor95@gmail.com
State New
Headers
Series thermal: thermal-generic-adc: add temp sensor function |

Commit Message

Svyatoslav Ryhel Feb. 10, 2024, 5:59 p.m. UTC
  Existing thermal-generic-adc states to be an adc-based thermal
sensor, which is not entirely true. It provides a thermal sensor
cell, but it does not provide an IIO sensor cell, which would
benefit devices that use adc sensors to detect temperature and
need a custom calibration table.

Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com>
---
 drivers/thermal/thermal-generic-adc.c | 57 ++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)
  

Comments

Rafael J. Wysocki Feb. 13, 2024, 2:07 p.m. UTC | #1
On Sat, Feb 10, 2024 at 6:59 PM Svyatoslav Ryhel <clamor95@gmail.com> wrote:
>
> Existing thermal-generic-adc states to be an adc-based thermal
> sensor, which is not entirely true. It provides a thermal sensor
> cell, but it does not provide an IIO sensor cell, which would
> benefit devices that use adc sensors to detect temperature and
> need a custom calibration table.

After reading this changelog and the subject I have to
reverse-engineer the patch to actually find out what's going on.

In particular, are there any user-visible changes of this and if so,
what are they?

> Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com>
> ---
>  drivers/thermal/thermal-generic-adc.c | 57 ++++++++++++++++++++++++++-
>  1 file changed, 56 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/thermal/thermal-generic-adc.c b/drivers/thermal/thermal-generic-adc.c
> index 1717e4a19dcb..7e87d928512c 100644
> --- a/drivers/thermal/thermal-generic-adc.c
> +++ b/drivers/thermal/thermal-generic-adc.c
> @@ -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,60 @@ 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_channel[] = {
> +       {
> +               .datasheet_name = "temp",
> +               .type = IIO_TEMP,
> +               .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
> +               .indexed = 1,
> +               .channel = 0,
> +       }
> +};
> +
> +static int gadc_thermal_read_raw(struct iio_dev *indio_dev,
> +                                struct iio_chan_spec const *chan,
> +                                int *temp, int *val2, long mask)
> +{
> +       struct gadc_thermal_info *gtinfo = iio_priv(indio_dev);
> +       int ret;
> +
> +       if (mask != IIO_CHAN_INFO_RAW)
> +               return -EINVAL;
> +
> +       ret = gadc_thermal_get_temp(gtinfo->tz_dev, temp);
> +       if (ret < 0)
> +               return ret;
> +
> +       *temp /= 1000;
> +
> +       return IIO_VAL_INT;
> +}
> +
> +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(struct gadc_thermal_info));
> +       if (!indio_dev)
> +               return -ENOMEM;
> +
> +       gtinfo = iio_priv(indio_dev);
> +       memcpy(gtinfo, gti, sizeof(struct gadc_thermal_info));
> +
> +       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_channel;
> +       indio_dev->num_channels = ARRAY_SIZE(gadc_thermal_iio_channel);
> +
> +       return devm_iio_device_register(dev, indio_dev);
> +}
> +
>  static int gadc_thermal_read_linear_lookup_table(struct device *dev,
>                                                  struct gadc_thermal_info *gti)
>  {
> @@ -156,7 +211,7 @@ static int gadc_thermal_probe(struct platform_device *pdev)
>
>         devm_thermal_add_hwmon_sysfs(&pdev->dev, gti->tz_dev);
>
> -       return 0;
> +       return gadc_iio_register(&pdev->dev, gti);
>  }
>
>  static const struct of_device_id of_adc_thermal_match[] = {
> --
> 2.40.1
>
  

Patch

diff --git a/drivers/thermal/thermal-generic-adc.c b/drivers/thermal/thermal-generic-adc.c
index 1717e4a19dcb..7e87d928512c 100644
--- a/drivers/thermal/thermal-generic-adc.c
+++ b/drivers/thermal/thermal-generic-adc.c
@@ -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,60 @@  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_channel[] = {
+	{
+		.datasheet_name = "temp",
+		.type = IIO_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.indexed = 1,
+		.channel = 0,
+	}
+};
+
+static int gadc_thermal_read_raw(struct iio_dev *indio_dev,
+				 struct iio_chan_spec const *chan,
+				 int *temp, int *val2, long mask)
+{
+	struct gadc_thermal_info *gtinfo = iio_priv(indio_dev);
+	int ret;
+
+	if (mask != IIO_CHAN_INFO_RAW)
+		return -EINVAL;
+
+	ret = gadc_thermal_get_temp(gtinfo->tz_dev, temp);
+	if (ret < 0)
+		return ret;
+
+	*temp /= 1000;
+
+	return IIO_VAL_INT;
+}
+
+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(struct gadc_thermal_info));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	gtinfo = iio_priv(indio_dev);
+	memcpy(gtinfo, gti, sizeof(struct gadc_thermal_info));
+
+	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_channel;
+	indio_dev->num_channels = ARRAY_SIZE(gadc_thermal_iio_channel);
+
+	return devm_iio_device_register(dev, indio_dev);
+}
+
 static int gadc_thermal_read_linear_lookup_table(struct device *dev,
 						 struct gadc_thermal_info *gti)
 {
@@ -156,7 +211,7 @@  static int gadc_thermal_probe(struct platform_device *pdev)
 
 	devm_thermal_add_hwmon_sysfs(&pdev->dev, gti->tz_dev);
 
-	return 0;
+	return gadc_iio_register(&pdev->dev, gti);
 }
 
 static const struct of_device_id of_adc_thermal_match[] = {