Add LTC2495 support
Subsystems (3)
v3 cover Add LTC2495 support
Hi All,
This is the v3 of the patch series adding support for the LTC2495 ADC in the ltc2497 driver, and enabling the internal temperature channel for the LTC2495 and LTC2499.
Thanks to Andy Shevchenko for his quick, helpful, and continued review of the previous version.
In response to the request to check for code size changes, I have run bloat-o-meter. The results confirm no meaningful impact on code size.
Full bloat-o-meter output:
```
./scripts/bloat-o-meter \
before_patch_4/ltc2497-core.o drivers/iio/adc/ltc2497-core.o
add/remove: 0/0 grow/shrink: 1/1 up/down: 1/-1 (0)
Function old new delta
ltc2497core_read_raw 413 414 +1
ltc2497core_probe 374 373 -1
Total: Before=11576, After=11576, chg +0.00%
./scripts/bloat-o-meter \
before_patch_4/ltc2497.o drivers/iio/adc/ltc2497.o
add/remove: 0/0 grow/shrink: 2/0 up/down: 2/0 (2)
Function old new delta
ltc2497_result_and_measure 275 276 +1
ltc2497_probe 158 159 +1
Total: Before=1946, After=1948, chg +0.10%
./scripts/bloat-o-meter \
before_patch_4/ltc2496.o drivers/iio/adc/ltc2496.o
add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0 (0)
Function old new delta
Total: Before=1251, After=1251, chg +0.00%
```Best Regards,
Alper
---
Changes in v3:
- Used the standard kelvin_to_celsius() helper instead of a custom define.
- Corrected macro definition style.
- Renamed LTC2497_CHANNELS and LTC_T_CHAN for clarity.
- Combined all struct layout optimizations into a single patch.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Rewrote all commit messages to use the imperative mood.
- Added a justification for the new compatible string to the device tree binding commit message.
- Removed all unrelated whitespace and formatting changes.
- Removed redundant explicit false initializers from structs.
- Replaced the magic number for Kelvin conversion with a define.
- Improved comments for defines and temperature scaling constants.
- Renamed confusing macros and struct fields to be more descriptive.
- Replaced dynamic channel allocation with a static array approach using a shared macro to improve readability.
- Optimized data structure layouts based on pahole output to remove memory holes.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Implementation Notes
- checkpatch warning: The new static array approach uses a shared macro for the common channels (LTC2497_CHANNELS), which triggers a checkpatch.pl warning: "Macros with complex values should be enclosed in parentheses". However, this will cause a compilitaion error, as an initializer list cannot be parenthesized.
- uV to mV conversion: I could not find a standard macro, so a manual division is used for now. This could be a point of future improvement.
---
Yusuf Alper Bilgin (4):
dt-bindings: iio: adc: ltc2497: add lltc,ltc2495 bindings
iio: adc: ltc2497: add support for LTC2495
iio: adc: ltc2497: add temperature sensor support
iio: adc: ltc2497: reorder struct members to fix memory holes
.../devicetree/bindings/iio/adc/lltc,ltc2497.yaml | 3 + drivers/iio/adc/ltc2497-core.c | 141 +++++++++++++++------ drivers/iio/adc/ltc2497.c | 35 ++++- drivers/iio/adc/ltc2497.h | 23 +++- 4 files changed, 159 insertions(+), 43 deletions(-)
---
base-commit: acbbb5a20971089064ca6b271dd251e629be8d4d
change-id: 20250811-ltc2495-572817c13fd3
Best regards,
Signature · 1 line
Yusuf Alper Bilgin <[email protected]>
PATCH v3 1/4 dt-bindings: iio: adc: ltc2497: add lltc,ltc2495 bindings
Update the binding documentation for LTC2497 to include LTC2495 to enable support for its internal temperature sensor, which requires a different I2C command format and a new IIO channel.
--- Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml | 3 +++ 1 file changed, 3 insertions(+)
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml +3 -0
@@ -17,11 +17,13 @@ description: | https://www.analog.com/media/en/technical-documentation/data-sheets/2309fd.pdf + LTC2495: LTC2497: LTC2499: 16bit ADC supporting up to 16 single ended or 8 differential inputs. I2C interface. + https://www.analog.com/media/en/technical-documentation/data-sheets/2495fe.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/2497fb.pdf https://www.analog.com/media/en/technical-documentation/data-sheets/2499fe.pdf
@@ -29,6 +31,7 @@ properties: compatible: enum: - lltc,ltc2309 + - lltc,ltc2495 - lltc,ltc2497 - lltc,ltc2499
Signature · 1 line
2.43.0
PATCH v3 2/4 iio: adc: ltc2497: add support for LTC2495
In reply to Yusuf Alper BilginUpdate the LTC2497 driver to also support the LTC2495.
--- drivers/iio/adc/ltc2497.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/iio/adc/ltc2497.c +7 -0
@@ -19,6 +19,7 @@#include "ltc2497.h" enum ltc2497_chip_type { +TYPE_LTC2495 ,TYPE_LTC2497 ,TYPE_LTC2499 , };
@@ -131,6 +132,10 @@ static void ltc2497_remove(struct i2c_client *client) }static const struct ltc2497_chip_info ltc2497_info[] = { + [TYPE_LTC2495 ] = { + .resolution =16 , + .name ="ltc2495" , + }, [TYPE_LTC2497 ] = { .resolution =16 , .name =NULL ,
@@ -142,6 +147,7 @@ static const struct ltc2497_chip_info ltc2497_info[] = { };static const struct i2c_device_id ltc2497_id[] = { + {"ltc2495" , (kernel_ulong_t)<c2497_info[TYPE_LTC2495 ] }, {"ltc2497" , (kernel_ulong_t)<c2497_info[TYPE_LTC2497 ] }, {"ltc2499" , (kernel_ulong_t)<c2497_info[TYPE_LTC2499 ] }, { }
@@ -149,6 +155,7 @@ static const struct i2c_device_id ltc2497_id[] = {MODULE_DEVICE_TABLE (i2c, ltc2497_id);static const struct of_device_id ltc2497_of_match[] = { + { .compatible ="lltc,ltc2495" , .data = <c2497_info[TYPE_LTC2495 ] }, { .compatible ="lltc,ltc2497" , .data = <c2497_info[TYPE_LTC2497 ] }, { .compatible ="lltc,ltc2499" , .data = <c2497_info[TYPE_LTC2499 ] }, { }
Signature · 1 line
2.43.0
PATCH v3 3/4 iio: adc: ltc2497: add temperature sensor support
In reply to Yusuf Alper BilginSupport for reading the internal temperature sensor on LTC2495 and LTC2499 via a standard IIO temperature channel.
--- drivers/iio/adc/ltc2497-core.c | 141 ++++++++++++++++++++++++++++++----------- drivers/iio/adc/ltc2497.c | 28 +++++++- drivers/iio/adc/ltc2497.h | 15 +++++ 3 files changed, 145 insertions(+), 39 deletions(-)
drivers/iio/adc/ltc2497-core.c +104 -37
@@ -12,6 +12,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regulator/consumer.h> +#include <linux/units.h> #include "ltc2497.h"
@@ -95,10 +96,53 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev,if (ret <0 )return ret; - *val = ret /1000 ; - *val2 = ddata->chip_info->resolution +1 ; +switch (chan->type) { +case IIO_VOLTAGE : + *val = ret /1000 ; + *val2 = ddata->chip_info->resolution +1 ; + +return IIO_VAL_FRACTIONAL_LOG2 ; + +case IIO_TEMP : +if (!ddata->chip_info->has_temp_channel) +return -EINVAL ; + +/* +* The datasheet formula to get Temperature in Celsius is: +* Temp_C = (DATAOUT * Vref_V / temp_scale) - 273 +* +* To match the IIO framework's model of (raw + offset) * scale, +* and to get the final result in millidegrees Celsius: +* +* Temp_mC = ((DATAOUT * Vref_mV / temp_scale_mV) - 273) * 1000 +* Temp_mC = (DATAOUT - (273 * temp_scale_mV / Vref_mv)) * 1000 * +* Vref_mV / temp_scale_mV +* +* This gives us: +* scale = Vref_mV * 1000 / temp_scale_mV +* offset = -273 * temp_scale / Vref_mV +*/ + *val = ret; + *val2 = ddata->chip_info->temp_scale_mV; + +return IIO_VAL_FRACTIONAL ; + +default : +return -EINVAL ; + } +case IIO_CHAN_INFO_OFFSET : +if (chan->type !=IIO_TEMP ) +return -EINVAL ; + +/* see the calculation above. Offset with (-273 * temp_scale / Vref) */ + ret =regulator_get_voltage (ddata->ref); +if (ret <0 ) +return ret; -return IIO_VAL_FRACTIONAL_LOG2 ; + *val =kelvin_to_celsius (0 ) * ddata->chip_info->temp_scale_mV; + *val2 = ret /1000 ; + +return IIO_VAL_FRACTIONAL ;default :return -EINVAL ;
@@ -126,39 +170,56 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev, .differential = 1, \ } +#define LTC2497_TEMPERATURE_CHAN \ +{ \ + .type = IIO_TEMP, \ + .channel = 0, \ + .address = (LTC2497_ENABLE_TEMPERATURE_CONV), \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \ +} + +#define LTC2497_VOLTAGE_CHANNEL_LIST \ + LTC2497_CHAN(0, LTC2497_SGL, "CH0"), \ + LTC2497_CHAN(1, LTC2497_SGL, "CH1"), \ + LTC2497_CHAN(2, LTC2497_SGL, "CH2"), \ + LTC2497_CHAN(3, LTC2497_SGL, "CH3"), \ + LTC2497_CHAN(4, LTC2497_SGL, "CH4"), \ + LTC2497_CHAN(5, LTC2497_SGL, "CH5"), \ + LTC2497_CHAN(6, LTC2497_SGL, "CH6"), \ + LTC2497_CHAN(7, LTC2497_SGL, "CH7"), \ + LTC2497_CHAN(8, LTC2497_SGL, "CH8"), \ + LTC2497_CHAN(9, LTC2497_SGL, "CH9"), \ + LTC2497_CHAN(10, LTC2497_SGL, "CH10"), \ + LTC2497_CHAN(11, LTC2497_SGL, "CH11"), \ + LTC2497_CHAN(12, LTC2497_SGL, "CH12"), \ + LTC2497_CHAN(13, LTC2497_SGL, "CH13"), \ + LTC2497_CHAN(14, LTC2497_SGL, "CH14"), \ + LTC2497_CHAN(15, LTC2497_SGL, "CH15"), \ + LTC2497_CHAN_DIFF(0, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(1, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(2, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(3, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(4, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(5, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(6, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(7, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(0, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(1, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(2, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(3, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(4, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(5, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(6, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(7, LTC2497_DIFF | LTC2497_SIGN) +static const struct iio_chan_spec ltc2497core_channel[] = { -LTC2497_CHAN (0 ,LTC2497_SGL ,"CH0" ), -LTC2497_CHAN (1 ,LTC2497_SGL ,"CH1" ), -LTC2497_CHAN (2 ,LTC2497_SGL ,"CH2" ), -LTC2497_CHAN (3 ,LTC2497_SGL ,"CH3" ), -LTC2497_CHAN (4 ,LTC2497_SGL ,"CH4" ), -LTC2497_CHAN (5 ,LTC2497_SGL ,"CH5" ), -LTC2497_CHAN (6 ,LTC2497_SGL ,"CH6" ), -LTC2497_CHAN (7 ,LTC2497_SGL ,"CH7" ), -LTC2497_CHAN (8 ,LTC2497_SGL ,"CH8" ), -LTC2497_CHAN (9 ,LTC2497_SGL ,"CH9" ), -LTC2497_CHAN (10 ,LTC2497_SGL ,"CH10" ), -LTC2497_CHAN (11 ,LTC2497_SGL ,"CH11" ), -LTC2497_CHAN (12 ,LTC2497_SGL ,"CH12" ), -LTC2497_CHAN (13 ,LTC2497_SGL ,"CH13" ), -LTC2497_CHAN (14 ,LTC2497_SGL ,"CH14" ), -LTC2497_CHAN (15 ,LTC2497_SGL ,"CH15" ), -LTC2497_CHAN_DIFF (0 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (1 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (2 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (3 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (4 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (5 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (6 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (7 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (0 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (1 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (2 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (3 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (4 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (5 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (6 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (7 ,LTC2497_DIFF |LTC2497_SIGN ), +LTC2497_VOLTAGE_CHANNEL_LIST , +}; + +static const struct iio_chan_spec ltc2497core_channel_with_temperature[] = { +LTC2497_VOLTAGE_CHANNEL_LIST , +LTC2497_TEMPERATURE_CHAN , };static const struct iio_info ltc2497core_info = {
@@ -182,8 +243,14 @@ int ltc2497core_probe(struct device *dev, struct iio_dev *indio_dev) indio_dev->info = <c2497core_info; indio_dev->modes =INDIO_DIRECT_MODE ; - indio_dev->channels = ltc2497core_channel; - indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel); + +if (ddata->chip_info->has_temp_channel) { + indio_dev->channels = ltc2497core_channel_with_temperature; + indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel_with_temperature); + }else { + indio_dev->channels = ltc2497core_channel; + indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel); + } ret = ddata->result_and_measure (ddata,LTC2497_CONFIG_DEFAULT ,NULL );if (ret <0 )
drivers/iio/adc/ltc2497.c +26 -2
@@ -86,8 +86,28 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,return 0 ; } - ret =i2c_smbus_write_byte (st->client, -LTC2497_ENABLE | address); +/* +* Chips with temperature sensor support (e.g., LTC2495/LTC2499) +* require a two-byte command format to select any channel. +* +* To read the internal temperature, LTC2497_ENABLE_TEMPERATURE_CONV +* is sent as the second byte. To read a voltage channel, LTC2497_EN2 +* is sent, which sets the default configuration: simultaneous 50/60Hz +* rejection, 1x speed, and gain=1. +* +* Chips without this feature use a standard single-byte command. +*/ +if (ddata->chip_info->has_temp_channel) { +if (address ==LTC2497_ENABLE_TEMPERATURE_CONV ) + ret =i2c_smbus_write_byte_data (st->client,LTC2497_ENABLE , +LTC2497_ENABLE_TEMPERATURE_CONV ); +else + ret =i2c_smbus_write_byte_data (st->client,LTC2497_ENABLE | address, +LTC2497_EN2 ); + }else { + ret =i2c_smbus_write_byte (st->client,LTC2497_ENABLE | address); + } +if (ret)dev_err (&st->client->dev,"i2c transfer failed: %pe\n" ,ERR_PTR (ret));
@@ -135,6 +155,8 @@ static const struct ltc2497_chip_info ltc2497_info[] = { [TYPE_LTC2495] = { .resolution = 16, .name = "ltc2495", + .has_temp_channel = true, + .temp_scale_mV = 12250, }, [TYPE_LTC2497] = { .resolution = 16,
@@ -143,6 +165,8 @@ static const struct ltc2497_chip_info ltc2497_info[] = { [TYPE_LTC2499 ] = { .resolution =24 , .name ="ltc2499" , + .has_temp_channel = true, + .temp_scale_mV =1570000 , }, };
drivers/iio/adc/ltc2497.h +15 -0
@@ -3,10 +3,25 @@#define LTC2497_ENABLE 0xA0#define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE#define LTC2497_CONVERSION_TIME_MS 150ULL +#define LTC2497_EN2 BIT(7) +/* Enable the internal temperature sensor */ +#define LTC2497_IM BIT(6) +/* Second command byte value to initiate a temperature conversion */ +#define LTC2497_ENABLE_TEMPERATURE_CONV (LTC2497_EN2 | LTC2497_IM)struct ltc2497_chip_info {u32 resolution;const char *name; +/* +* Represents the datasheet constant from the temperature formula: +* T_Kelvin = (DATAOUT * Vref) / temp_scale, where Vref is in Volts. +* +* To allow the driver to use Vref in millivolts for the calculation +* and also to avoid floating points, this stored value represents the +* datasheet constant scaled by 1000. +*/ +u32 temp_scale_mV; +bool has_temp_channel; };struct ltc2497core_driverdata {
Signature · 1 line
2.43.0
PATCH v3 4/4 iio: adc: ltc2497: reorder struct members to fix memory holes
In reply to Yusuf Alper BilginReorder members in the ltc2497_chip_info and ltc2497core_driverdata structs to eliminate memory holes identified by the pahole tool.
Confirm via the bloat-o-meter that this change has no significant impact on the final code size:
| Object File | Total Size Change | |-----------------|-------------------| | ltc2497-core.o | 0 (0.00%) | | ltc2497.o | +2 (+0.10%) | | ltc2496.o | 0 (0.00%) |
--- drivers/iio/adc/ltc2497.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/adc/ltc2497.h +4 -4
@@ -10,8 +10,8 @@#define LTC2497_ENABLE_TEMPERATURE_CONV (LTC2497_EN2 | LTC2497_IM)struct ltc2497_chip_info { -u32 resolution;const char *name; +u32 resolution;/* * Represents the datasheet constant from the temperature formula: * T_Kelvin = (DATAOUT * Vref) / temp_scale, where Vref is in Volts.
@@ -27,12 +27,12 @@ struct ltc2497_chip_info {struct ltc2497core_driverdata {struct regulator *ref;ktime_t time_prev; -/* lock to protect against multiple access to the device */ -struct mutex lock;const struct ltc2497_chip_info *chip_info; -u8 addr_prev;int (*result_and_measure)(struct ltc2497core_driverdata *ddata,u8 address,int *val); +/* lock to protect against multiple access to the device */ +struct mutex lock; +u8 addr_prev; };int ltc2497core_probe (struct device *dev,struct iio_dev *indio_dev);
Signature · 1 line
2.43.0
David Lechner
reviewerRe: [PATCH v3 3/4] iio: adc: ltc2497: add temperature sensor support
In reply to Yusuf Alper BilginQuoted 44 lines · Yusuf Alper Bilgin
Support for reading the internal temperature sensor on LTC2495 and LTC2499 via a standard IIO temperature channel.
Signed-off-by Yusuf Alper Bilgin <[email protected]>--- drivers/iio/adc/ltc2497-core.c | 141 ++++++++++++++++++++++++++++++----------- drivers/iio/adc/ltc2497.c | 28 +++++++- drivers/iio/adc/ltc2497.h | 15 +++++ 3 files changed, 145 insertions(+), 39 deletions(-)drivers/iio/adc/ltc2497-core.c +17
-2@@ -12,6 +12,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/regulator/consumer.h> +#include <linux/units.h> #include "ltc2497.h" @@ -95,10 +96,53 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev,if (ret <0 )return ret; - *val = ret /1000 ; - *val2 = ddata->chip_info->resolution +1 ; +switch (chan->type) { +case IIO_VOLTAGE : + *val = ret /1000 ; + *val2 = ddata->chip_info->resolution +1 ; + +return IIO_VAL_FRACTIONAL_LOG2 ; + +case IIO_TEMP : +if (!ddata->chip_info->has_temp_channel) +return -EINVAL ; + +/* +* The datasheet formula to get Temperature in Celsius is: +* Temp_C = (DATAOUT * Vref_V / temp_scale) - 273 +* +* To match the IIO framework's model of (raw + offset) * scale,
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 124
Quoted 10 lines
drivers/iio/adc/ltc2497-core.c +10
-0+* and to get the final result in millidegrees Celsius: +* +* Temp_mC = ((DATAOUT * Vref_mV / temp_scale_mV) - 273) * 1000 +* Temp_mC = (DATAOUT - (273 * temp_scale_mV / Vref_mv)) * 1000 * +* Vref_mV / temp_scale_mV +* +* This gives us: +* scale = Vref_mV * 1000 / temp_scale_mV +* offset = -273 * temp_scale / Vref_mV +*/
Probably don't need quite so much comments here. TBH, I though just the comment in struct ltc2497_chip_info was enough.
Quoted 26 lines
drivers/iio/adc/ltc2497-core.c +20
-1+ *val = ret; + *val2 = ddata->chip_info->temp_scale_mV; + +return IIO_VAL_FRACTIONAL ; + +default : +return -EINVAL ; + } +case IIO_CHAN_INFO_OFFSET : +if (chan->type !=IIO_TEMP ) +return -EINVAL ; + +/* see the calculation above. Offset with (-273 * temp_scale / Vref) */ + ret =regulator_get_voltage (ddata->ref); +if (ret <0 ) +return ret; -return IIO_VAL_FRACTIONAL_LOG2 ; + *val =kelvin_to_celsius (0 ) * ddata->chip_info->temp_scale_mV; + *val2 = ret /1000 ; + +return IIO_VAL_FRACTIONAL ;default :return -EINVAL ;
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 176
Quoted 7 lines
drivers/iio/adc/ltc2497-core.c +4
-0@@ -126,39 +170,56 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev, .differential = 1, \ } +#define LTC2497_TEMPERATURE_CHAN \ +{ \ + .type = IIO_TEMP, \ + .channel = 0, \
No need for channel if indexed == 0.
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 177
Quoted 1 line
drivers/iio/adc/ltc2497-core.c +1
-0+ .address = (LTC2497_ENABLE_TEMPERATURE_CONV), \
Since this can't be used the same was as the voltage address field, not sure it makes sense to set this. Later, we can just check the channel.type instead.
Quoted 110 lines
drivers/iio/adc/ltc2497-core.c +52
-34+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \ +} + +#define LTC2497_VOLTAGE_CHANNEL_LIST \ + LTC2497_CHAN(0, LTC2497_SGL, "CH0"), \ + LTC2497_CHAN(1, LTC2497_SGL, "CH1"), \ + LTC2497_CHAN(2, LTC2497_SGL, "CH2"), \ + LTC2497_CHAN(3, LTC2497_SGL, "CH3"), \ + LTC2497_CHAN(4, LTC2497_SGL, "CH4"), \ + LTC2497_CHAN(5, LTC2497_SGL, "CH5"), \ + LTC2497_CHAN(6, LTC2497_SGL, "CH6"), \ + LTC2497_CHAN(7, LTC2497_SGL, "CH7"), \ + LTC2497_CHAN(8, LTC2497_SGL, "CH8"), \ + LTC2497_CHAN(9, LTC2497_SGL, "CH9"), \ + LTC2497_CHAN(10, LTC2497_SGL, "CH10"), \ + LTC2497_CHAN(11, LTC2497_SGL, "CH11"), \ + LTC2497_CHAN(12, LTC2497_SGL, "CH12"), \ + LTC2497_CHAN(13, LTC2497_SGL, "CH13"), \ + LTC2497_CHAN(14, LTC2497_SGL, "CH14"), \ + LTC2497_CHAN(15, LTC2497_SGL, "CH15"), \ + LTC2497_CHAN_DIFF(0, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(1, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(2, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(3, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(4, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(5, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(6, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(7, LTC2497_DIFF), \ + LTC2497_CHAN_DIFF(0, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(1, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(2, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(3, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(4, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(5, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(6, LTC2497_DIFF | LTC2497_SIGN), \ + LTC2497_CHAN_DIFF(7, LTC2497_DIFF | LTC2497_SIGN) +static const struct iio_chan_spec ltc2497core_channel[] = { -LTC2497_CHAN (0 ,LTC2497_SGL ,"CH0" ), -LTC2497_CHAN (1 ,LTC2497_SGL ,"CH1" ), -LTC2497_CHAN (2 ,LTC2497_SGL ,"CH2" ), -LTC2497_CHAN (3 ,LTC2497_SGL ,"CH3" ), -LTC2497_CHAN (4 ,LTC2497_SGL ,"CH4" ), -LTC2497_CHAN (5 ,LTC2497_SGL ,"CH5" ), -LTC2497_CHAN (6 ,LTC2497_SGL ,"CH6" ), -LTC2497_CHAN (7 ,LTC2497_SGL ,"CH7" ), -LTC2497_CHAN (8 ,LTC2497_SGL ,"CH8" ), -LTC2497_CHAN (9 ,LTC2497_SGL ,"CH9" ), -LTC2497_CHAN (10 ,LTC2497_SGL ,"CH10" ), -LTC2497_CHAN (11 ,LTC2497_SGL ,"CH11" ), -LTC2497_CHAN (12 ,LTC2497_SGL ,"CH12" ), -LTC2497_CHAN (13 ,LTC2497_SGL ,"CH13" ), -LTC2497_CHAN (14 ,LTC2497_SGL ,"CH14" ), -LTC2497_CHAN (15 ,LTC2497_SGL ,"CH15" ), -LTC2497_CHAN_DIFF (0 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (1 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (2 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (3 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (4 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (5 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (6 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (7 ,LTC2497_DIFF ), -LTC2497_CHAN_DIFF (0 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (1 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (2 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (3 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (4 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (5 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (6 ,LTC2497_DIFF |LTC2497_SIGN ), -LTC2497_CHAN_DIFF (7 ,LTC2497_DIFF |LTC2497_SIGN ), +LTC2497_VOLTAGE_CHANNEL_LIST , +}; + +static const struct iio_chan_spec ltc2497core_channel_with_temperature[] = { +LTC2497_VOLTAGE_CHANNEL_LIST , +LTC2497_TEMPERATURE_CHAN , };static const struct iio_info ltc2497core_info = {@@ -182,8 +243,14 @@ int ltc2497core_probe(struct device *dev, struct iio_dev *indio_dev) indio_dev->info = <c2497core_info; indio_dev->modes =INDIO_DIRECT_MODE ; - indio_dev->channels = ltc2497core_channel; - indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel); + +if (ddata->chip_info->has_temp_channel) { + indio_dev->channels = ltc2497core_channel_with_temperature; + indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel_with_temperature); + }else { + indio_dev->channels = ltc2497core_channel; + indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel); + } ret = ddata->result_and_measure (ddata,LTC2497_CONFIG_DEFAULT ,NULL );if (ret <0 )drivers/iio/adc/ltc2497.c +3
-2@@ -86,8 +86,28 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,return 0 ; } - ret =i2c_smbus_write_byte (st->client, -LTC2497_ENABLE | address); +/* +* Chips with temperature sensor support (e.g., LTC2495/LTC2499) +* require a two-byte command format to select any channel.
drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 101
Quoted 10 lines
drivers/iio/adc/ltc2497.c +10
-0+* +* To read the internal temperature, LTC2497_ENABLE_TEMPERATURE_CONV +* is sent as the second byte. To read a voltage channel, LTC2497_EN2 +* is sent, which sets the default configuration: simultaneous 50/60Hz +* rejection, 1x speed, and gain=1. +* +* Chips without this feature use a standard single-byte command. +*/ +if (ddata->chip_info->has_temp_channel) { +if (address ==LTC2497_ENABLE_TEMPERATURE_CONV )
Here we can just check the channel type.
drivers/iio/adc/ltc2497.c static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, · line 103
Quoted 2 lines
drivers/iio/adc/ltc2497.c +2
-0+ ret =i2c_smbus_write_byte_data (st->client,LTC2497_ENABLE , +LTC2497_ENABLE_TEMPERATURE_CONV );
And use LTC2497_EN2 | LTC2497_IM here.
Quoted 33 lines
drivers/iio/adc/ltc2497.c +11
-0+else + ret =i2c_smbus_write_byte_data (st->client,LTC2497_ENABLE | address, +LTC2497_EN2 ); + }else { + ret =i2c_smbus_write_byte (st->client,LTC2497_ENABLE | address); + } +if (ret)dev_err (&st->client->dev,"i2c transfer failed: %pe\n" ,ERR_PTR (ret));@@ -135,6 +155,8 @@ static const struct ltc2497_chip_info ltc2497_info[] = { [TYPE_LTC2495] = { .resolution = 16, .name = "ltc2495", + .has_temp_channel = true, + .temp_scale_mV = 12250, }, [TYPE_LTC2497] = { .resolution = 16,@@ -143,6 +165,8 @@ static const struct ltc2497_chip_info ltc2497_info[] = { [TYPE_LTC2499 ] = { .resolution =24 , .name ="ltc2499" , + .has_temp_channel = true, + .temp_scale_mV =1570000 , }, };drivers/iio/adc/ltc2497.h +0
-0
drivers/iio/adc/ltc2497.h line 8
Quoted 6 lines
drivers/iio/adc/ltc2497.h +3
-0@@ -3,10 +3,25 @@#define LTC2497_ENABLE 0xA0#define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE#define LTC2497_CONVERSION_TIME_MS 150ULL +#define LTC2497_EN2 BIT(7) +/* Enable the internal temperature sensor */ +#define LTC2497_IM BIT(6)
These should go directly above:
#define LTC2497_SGL BIT(4)
in the .c file. No need to have them in the header.
drivers/iio/adc/ltc2497.h line 10
Quoted 2 lines
drivers/iio/adc/ltc2497.h +2
-0+/* Second command byte value to initiate a temperature conversion */ +#define LTC2497_ENABLE_TEMPERATURE_CONV (LTC2497_EN2 | LTC2497_IM)
This macro seems odd. We usually don't make macros for combined bits like this. And the way it is being used in the code feels a bit odd as well. We can probably rework the code to not need it as suggested in some of the comments above.
Quoted 18 lines
drivers/iio/adc/ltc2497.h +10
-0struct ltc2497_chip_info {u32 resolution;const char *name; +/* +* Represents the datasheet constant from the temperature formula: +* T_Kelvin = (DATAOUT * Vref) / temp_scale, where Vref is in Volts. +* +* To allow the driver to use Vref in millivolts for the calculation +* and also to avoid floating points, this stored value represents the +* datasheet constant scaled by 1000. +*/ +u32 temp_scale_mV; +bool has_temp_channel; };struct ltc2497core_driverdata {
Re: [PATCH v3 3/4] iio: adc: ltc2497: add temperature sensor support
In reply to Yusuf Alper BilginQuoted 8 lines · Yusuf Alper Bilgin
Support for reading the internal temperature sensor on LTC2495 and LTC2499 via a standard IIO temperature channel.
Signed-off-by Yusuf Alper Bilgin <[email protected]>--- drivers/iio/adc/ltc2497-core.c | 141 ++++++++++++++++++++++++++++++----------- drivers/iio/adc/ltc2497.c | 28 +++++++- drivers/iio/adc/ltc2497.h | 15 +++++
Thus should be squashed with previous patch, otherwise previous patch is just incomplete and broken. Unless... your binding is incorrect (I will comment there).
Best regards,
Krzysztof
Re: [PATCH v3 1/4] dt-bindings: iio: adc: ltc2497: add lltc,ltc2495 bindings
In reply to Yusuf Alper BilginQuoted 3 lines · Yusuf Alper Bilgin
Update the binding documentation for LTC2497 to include LTC2495 to enable support for its internal temperature sensor, which requires a different I2C command format and a new IIO channel.
Your patch #2 says they are compatible, so either this patch is wrong (and you are supposed to use fallbacks) or patch #2 is wrong/incomplete.
Best regards,
Krzysztof
Jonathan Cameron
maintainerRe: [PATCH v3 1/4] dt-bindings: iio: adc: ltc2497: add lltc,ltc2495 bindings
Quoted 7 lines · On Fri, 15 Aug 2025 10:57:21 +0200 Krzysztof Kozlowski <[email protected]> wrote:
Quoted 3 lines · Yusuf Alper Bilgin
Update the binding documentation for LTC2497 to include LTC2495 to enable support for its internal temperature sensor, which requires a different I2C command format and a new IIO channel.
Your patch #2 says they are compatible, so either this patch is wrong (and you are supposed to use fallbacks) or patch #2 is wrong/incomplete.
Agreed. Going too fast here, and discussion on v1 wasn't finished.
Jonathan
Quoted 4 lines
Best regards,
Krzysztof