Skip to content

Commit 3762f58

Browse files
clamor-sdlezcano
authored andcommitted
thermal/drivers/thermal-generic-adc: Add temperature sensor channel
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>
1 parent e881662 commit 3762f58

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

drivers/thermal/thermal-generic-adc.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Author: Laxman Dewangan <ldewangan@nvidia.com>
88
*/
99
#include <linux/iio/consumer.h>
10+
#include <linux/iio/iio.h>
1011
#include <linux/kernel.h>
1112
#include <linux/module.h>
1213
#include <linux/platform_device.h>
@@ -73,6 +74,58 @@ static const struct thermal_zone_device_ops gadc_thermal_ops = {
7374
.get_temp = gadc_thermal_get_temp,
7475
};
7576

77+
static const struct iio_chan_spec gadc_thermal_iio_channels[] = {
78+
{
79+
.type = IIO_TEMP,
80+
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
81+
}
82+
};
83+
84+
static int gadc_thermal_read_raw(struct iio_dev *indio_dev,
85+
struct iio_chan_spec const *chan,
86+
int *val, int *val2, long mask)
87+
{
88+
struct gadc_thermal_info *gtinfo = iio_priv(indio_dev);
89+
int ret;
90+
91+
switch (mask) {
92+
case IIO_CHAN_INFO_PROCESSED:
93+
ret = gadc_thermal_get_temp(gtinfo->tz_dev, val);
94+
if (ret)
95+
return ret;
96+
97+
return IIO_VAL_INT;
98+
99+
default:
100+
return -EINVAL;
101+
}
102+
}
103+
104+
static const struct iio_info gadc_thermal_iio_info = {
105+
.read_raw = gadc_thermal_read_raw,
106+
};
107+
108+
static int gadc_iio_register(struct device *dev, struct gadc_thermal_info *gti)
109+
{
110+
struct gadc_thermal_info *gtinfo;
111+
struct iio_dev *indio_dev;
112+
113+
indio_dev = devm_iio_device_alloc(dev, sizeof(*gtinfo));
114+
if (!indio_dev)
115+
return -ENOMEM;
116+
117+
gtinfo = iio_priv(indio_dev);
118+
memcpy(gtinfo, gti, sizeof(*gtinfo));
119+
120+
indio_dev->name = dev_name(dev);
121+
indio_dev->info = &gadc_thermal_iio_info;
122+
indio_dev->modes = INDIO_DIRECT_MODE;
123+
indio_dev->channels = gadc_thermal_iio_channels;
124+
indio_dev->num_channels = ARRAY_SIZE(gadc_thermal_iio_channels);
125+
126+
return devm_iio_device_register(dev, indio_dev);
127+
}
128+
76129
static int gadc_thermal_read_linear_lookup_table(struct device *dev,
77130
struct gadc_thermal_info *gti)
78131
{
@@ -153,7 +206,7 @@ static int gadc_thermal_probe(struct platform_device *pdev)
153206

154207
devm_thermal_add_hwmon_sysfs(dev, gti->tz_dev);
155208

156-
return 0;
209+
return gadc_iio_register(&pdev->dev, gti);
157210
}
158211

159212
static const struct of_device_id of_adc_thermal_match[] = {

0 commit comments

Comments
 (0)