iio: adc: add LTC2499 features support
Subsystems (3)
v4 cover iio: adc: add LTC2499 features support
This series adds LTC2499 support on top of the existing LTC2497 driver.
It includes:
- a small binding title correction for LTC2499,
- the LTC2499 internal temperature channel,
- the LTC2499 2x conversion speed mode exposed via sampling_frequency.
Validation performed on Raspberry Pi 4 with a DC1012A-A board:
- temperature raw/scale/offset read correctly,
- voltage channels remain sane after temperature reads,
- sampling_frequency 1x/2x switching works,
- pyadi-iio LTC2499 validation passed,
- hardware-in-the-loop M2K validation passed.
Changes in v4:
- The channel table is no longer copied and patched at runtime: two static const tables, one selected in probe.
- struct ltc2497_chip_info has its booleans last.
- Commit messages and code comments cut back.
- Naming settled on speed_2x / has_speed_mode.
Per-patch changelogs are under the --- of each patch.
v3: https://lore.kernel.org/r/[email protected]
Andrei Stancovici (3):
dt-bindings: iio: adc: lltc,ltc2497: add LTC2499 to title
iio: adc: ltc2497: add LTC2499 internal temperature channel
iio: adc: ltc2497: add 2x conversion speed mode
.../bindings/iio/adc/lltc,ltc2497.yaml | 2 +- drivers/iio/adc/ltc2497-core.c | 298 +++++++++++++++--- drivers/iio/adc/ltc2497.c | 42 +++ drivers/iio/adc/ltc2497.h | 31 +- 4 files changed, 320 insertions(+), 53 deletions(-)
base-commit: 36c12dbda81c284d72f3c64689461647497b643b
Signature · 1 line
2.43.0
PATCH v4 1/3 dt-bindings: iio: adc: lltc,ltc2497: add LTC2499 to title
The LTC2499 is already listed in the compatible enum and covered by the description, but was missing from the binding title. Add it so the title matches the set of devices the schema documents. No schema or property change is needed.
---
Changes in v4:
- No change.
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml +1 -1
@@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/iio/adc/lltc,ltc2497.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Linear Technology / Analog Devices LTC2497 and LTC2309 ADC +title: Linear Technology / Analog Devices LTC2497, LTC2499 and LTC2309 ADC maintainers: - Michael Hennerich <[email protected]>
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 1/3] dt-bindings: iio: adc: lltc,ltc2497: add LTC2499 to title
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The commit message claims to update the title to match the set of documented devices, but it…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The commit message claims to update the title to match the set of documented devices, but it fails to include LTC2305. --
commit ed7e1178510ad0fd945eab7467d3323be2dec391
Author: Andrei Stancovici <[email protected]>
dt-bindings: iio: adc: lltc,ltc2497: add LTC2499 to title
The LTC2499 is already listed in the compatible enum and covered by the description, but was missing from the binding title. Add it so the title matches the set of devices the schema documents. No schema or property change is needed.
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml line 7
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml +1
-1@@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/iio/adc/lltc,ltc2497.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Linear Technology / Analog Devices LTC2497 and LTC2309 ADC +title: Linear Technology / Analog Devices LTC2497, LTC2499 and LTC2309 ADC
[Severity: Medium]
The commit message states that adding LTC2499 makes the title match the set of devices the schema documents. However, is this update complete?
Looking at the compatible enum further down in the same file:
properties:
compatible:
enum:
- lltc,ltc2305
- lltc,ltc2309
- lltc,ltc2497
- lltc,ltc2499
LTC2305 is also documented by the schema but remains omitted from the title. Should the title be updated to include LTC2305 as well to fully match the commit message's stated goal?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jonathan Cameron
maintainerRe: [PATCH v4 1/3] dt-bindings: iio: adc: lltc,ltc2497: add LTC2499 to title
In reply to Andrei StancoviciQuoted 19 lines · On Wed, 9 Sep 2026 11:27:50 +0300 Andrei Stancovici…
The LTC2499 is already listed in the compatible enum and covered by the description, but was missing from the binding title. Add it so the title matches the set of devices the schema documents. No schema or property change is needed.
Signed-off-by Andrei Stancovici <[email protected]>---
Changes in v4:
- No change.Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml +0
-0
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml line 7
Quoted 5 lines
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml +1
-1@@ -4,7 +4,7 @@ $id: http://devicetree.org/schemas/iio/adc/lltc,ltc2497.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# -title: Linear Technology / Analog Devices LTC2497 and LTC2309 ADC +title: Linear Technology / Analog Devices LTC2497, LTC2499 and LTC2309 ADC
Sashiko correctly points out the LTC2305 is also supported but not in the title. Can we give up on this and just make it "LTC2497 and similar" like we tend to always end up doing in bindings with a large number of supported parts. Keep the full list for later in the doc.
Quoted 3 lines
Documentation/devicetree/bindings/iio/adc/lltc,ltc2497.yaml +0
-0maintainers: - Michael Hennerich <[email protected]>
PATCH v4 2/3 iio: adc: ltc2497: add LTC2499 internal temperature channel
In reply to Andrei StancoviciThe LTC2499 includes an internal temperature sensor. Expose it as an IIO_TEMP channel with raw, scale and offset.
Scale and offset are derived from the reference voltage, so reading them returns an error if firmware does not describe a vref-supply. As this is a new feature on a part that already works without it, failing probe is not an option.
Selecting the sensor needs a second command byte, and the part keeps the configuration it last latched unless that byte is sent again. Voltage reads on this part therefore also send the second byte.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2499fe.pdf
---
Changes in v4:
- Description cut down to what a reader needs: the sensor, the ABI it exposes and the vref dependency. The PTAT derivation and the num_channels detail are gone; the maths stays as comments next to the code that does it.
- struct ltc2497_chip_info: booleans moved last, as requested, with one deviation I should flag - I hoisted name to the front rather than only shifting the flags down. pahole, LP64, all three instances:
as posted (flags after resolution) 16 B, 2 B hole, 48 B
flags moved last, as requested 24 B, 4 B hole + 6 B tail, 72 B
name, resolution, flags last (v4) 16 B, no hole, 48 B The pre-series struct already had a 4-byte hole after resolution and the flags fell into it, which is why the posted version did not grow. Moving them to the end leaves that hole and adds tail padding. Hoisting the pointer puts the flags last as asked and removes the hole. On ILP32 all three orderings are 12 B. If you would rather have the literal reorder, say so and I will take the 24 B.
- Why the second command byte is sent on every conversion rather than only when the latched configuration already differs: simplicity. It costs one extra byte per conversion, and a conversion takes 150 ms, so tracking the latched state to avoid it does not pay for itself. The code comment now records that the unconditional send is deliberate; the rationale itself is here rather than in the commit message.
- Added the Datasheet: trailer.
- Andy's Reviewed-by is kept. The only code change in this patch since v3 is the member reorder he asked for, plus one sentence of comment.
drivers/iio/adc/ltc2497-core.c | 58 +++++++++++++++++++++++++++++++--- drivers/iio/adc/ltc2497.c | 36 +++++++++++++++++++++ drivers/iio/adc/ltc2497.h | 14 +++++++- 3 files changed, 103 insertions(+), 5 deletions(-)
drivers/iio/adc/ltc2497-core.c +54 -4
@@ -9,9 +9,11 @@#include <linux/delay.h> #include <linux/iio/iio.h> #include <linux/iio/driver.h> +#include <linux/math64.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/regulator/consumer.h> +#include <linux/units.h> #include "ltc2497.h"
@@ -95,10 +97,45 @@ 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_TEMP : +/* +* raw is normalised to 2^(resolution + 1), i.e. +* raw = 2 * DATAOUT24, so the PTAT scale (datasheet +* Vref / 1570 per Kelvin) doubles its denominator and, +* in m°C, becomes Vref_uV / 3140000. +*/ + *val = ret; + *val2 =3140000 ; +return IIO_VAL_FRACTIONAL ; +case IIO_VOLTAGE : + *val = ret / (MICRO /MILLI ); + *val2 = ddata->chip_info->resolution +1 ; +return IIO_VAL_FRACTIONAL_LOG2 ; +default : +return -EINVAL ; + } -return IIO_VAL_FRACTIONAL_LOG2 ; +case IIO_CHAN_INFO_OFFSET : +switch (chan->type) { +case IIO_TEMP : + ret =regulator_get_voltage (ddata->ref); +if (ret <0 ) +return ret; +if (ret ==0 ) +return -EINVAL ; +/* +* 0 °C == 273.15 K must map to raw + offset such that +* (raw + offset) * scale == 0 m°C, i.e. +* offset = -273150 / scale +* = -273150 * 3140000 / Vref_uV +* Computed in 64-bit to avoid overflow. +*/ + *val =div_s64 (ABSOLUTE_ZERO_MILLICELSIUS *3140000LL , ret); +return IIO_VAL_INT ; +default : +return -EINVAL ; + }default :return -EINVAL ;
@@ -126,6 +163,14 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev, .differential = 1, \ } +#define LTC2497_TEMP_CHANNEL { \ + .type = IIO_TEMP, \ + .address = LTC2497_TEMP_ADDR, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_OFFSET), \ +} +static const struct iio_chan_spec ltc2497core_channel[] = {LTC2497_CHAN (0 ,LTC2497_SGL ,"CH0" ),LTC2497_CHAN (1 ,LTC2497_SGL ,"CH1" ),
@@ -159,6 +204,7 @@ static const struct iio_chan_spec ltc2497core_channel[] = {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_TEMP_CHANNEL, }; static const struct iio_info ltc2497core_info = {
@@ -183,7 +229,11 @@ 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); +/* Only the ltc2499 has a temperature channel; it is the last entry. */ +if (ddata->chip_info->has_temp) + indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel); +else + indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel) -1 ; ret = ddata->result_and_measure (ddata,LTC2497_CONFIG_DEFAULT ,NULL );if (ret <0 )
drivers/iio/adc/ltc2497.c +36 -0
@@ -84,6 +84,41 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,return 0 ; } +/* +* Parts with the internal PTAT sensor (LTC2499) latch their converter +* configuration via a second command byte and only re-evaluate it when +* that byte has EN2 set; a single byte, or a second byte with EN2 = 0, +* means "keep previous". A one-byte channel select therefore cannot pull +* the device back out of temperature mode, so a voltage read after a +* temperature read would keep returning the PTAT result. Always drive the +* second byte with EN2 set on these parts: IM = 1 for a temperature read, +* EN2 alone (IM = 0) to (re)select an external input. FA = FB = 0 keeps +* the power-on simultaneous 50/60Hz rejection, whose worst-case +* conversion time the driver's wait already covers. +* +* The byte could be skipped while the latched configuration is already +* the one wanted; it is sent on every conversion for simplicity. +*/ +if (ddata->chip_info->has_temp) { +u8 cmd[2 ]; + +if (address ==LTC2497_TEMP_ADDR ) { + cmd[0 ] =LTC2497_ENABLE |LTC2497_CONFIG_DEFAULT ; + cmd[1 ] =LTC2499_EN2 |LTC2499_IM ; + }else { + cmd[0 ] =LTC2497_ENABLE | address; + cmd[1 ] =LTC2499_EN2 ; + } + + ret =i2c_master_send (st->client, cmd,sizeof (cmd)); +if (ret <0 ) { +dev_err (&st->client->dev,"i2c transfer failed: %pe\n" , +ERR_PTR (ret)); +return ret; + } +return 0 ; + } + ret =i2c_smbus_write_byte (st->client,LTC2497_ENABLE | address);if (ret)
@@ -137,6 +172,7 @@ static const struct ltc2497_chip_info ltc2497_info[] = { [TYPE_LTC2499 ] = { .resolution =24 , .name ="ltc2499" , + .has_temp = true, }, };
drivers/iio/adc/ltc2497.h +13 -1
@@ -4,9 +4,21 @@#define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE#define LTC2497_CONVERSION_TIME_MS 150ULL +/* +* Sentinel passed as `address` to result_and_measure() to request a +* temperature conversion instead of a voltage channel. Valid channel +* addresses fit in 5 bits (0x00–0x1F), so 0xFF is unambiguous. +*/ +#define LTC2497_TEMP_ADDR 0xFF + +/* Second config-byte bits (LTC2499 / LTC2493 only) */ +#define LTC2499_EN2 BIT(7)/* enable second config byte */ +#define LTC2499_IM BIT(6)/* 1 = measure internal temp sensor */ +struct ltc2497_chip_info { -u32 resolution;const char *name; +u32 resolution; +bool has_temp; };struct ltc2497core_driverdata {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 2/3] iio: adc: ltc2497: add LTC2499 internal temperature channel
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The IIO_TEMP channel scale and offset calculations omit the ADC resolution divisor, causing…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The IIO_TEMP channel scale and offset calculations omit the ADC resolution divisor, causing temperature readings to be incorrect by a factor of over 16 million. --
commit 675eebaaf0b1a3975b7d05ae2a5dc9c63db82edc
Author: Andrei Stancovici <[email protected]>
iio: adc: ltc2497: add LTC2499 internal temperature channel
The LTC2499 includes an internal temperature sensor. Expose it as an IIO_TEMP channel with raw, scale and offset.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2499fe.pdf
Quoted 4 lines
drivers/iio/adc/ltc2497-core.c +0
-0
[ ... ]
Quoted 9 lines
drivers/iio/adc/ltc2497-core.c +1
-4@@ -95,10 +97,45 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev,if (ret <0 )return ret; - *val = ret /1000 ; - *val2 = ddata->chip_info->resolution +1 ; - -return IIO_VAL_FRACTIONAL_LOG2 ; +switch (chan->type) {
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 110
Quoted 10 lines
drivers/iio/adc/ltc2497-core.c +10
-0+case IIO_TEMP : +/* +* raw is normalised to 2^(resolution + 1), i.e. +* raw = 2 * DATAOUT24, so the PTAT scale (datasheet +* Vref / 1570 per Kelvin) doubles its denominator and, +* in m°C, becomes Vref_uV / 3140000. +*/ + *val = ret; + *val2 =3140000 ; +return IIO_VAL_FRACTIONAL ;
[Severity: High]
Does this scale calculation in ltc2497core_read_raw() omit the necessary division by the ADC resolution (2^25)?
Without the resolution divisor, the temperature readings could be incorrect by a factor of over 16 million when a userspace application reads the sysfs attributes and applies the standard IIO formula (raw + offset) * scale.
Quoted 14 lines
drivers/iio/adc/ltc2497-core.c +14
-0+case IIO_VOLTAGE : + *val = ret / (MICRO /MILLI ); + *val2 = ddata->chip_info->resolution +1 ; +return IIO_VAL_FRACTIONAL_LOG2 ; +default : +return -EINVAL ; + } + +case IIO_CHAN_INFO_OFFSET : +switch (chan->type) { +case IIO_TEMP : + ret =regulator_get_voltage (ddata->ref); +if (ret <0 ) +return ret;
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 134
Quoted 10 lines
drivers/iio/adc/ltc2497-core.c +10
-0+if (ret ==0 ) +return -EINVAL ; +/* +* 0 °C == 273.15 K must map to raw + offset such that +* (raw + offset) * scale == 0 m°C, i.e. +* offset = -273150 / scale +* = -273150 * 3140000 / Vref_uV +* Computed in 64-bit to avoid overflow. +*/ + *val =div_s64 (ABSOLUTE_ZERO_MILLICELSIUS *3140000LL , ret);
[Severity: High]
Since this offset in ltc2497core_read_raw() is calculated based on the potentially broken scale from above, will this result in an offset that cannot correct for the missing resolution divisor?
Quoted 7 lines
drivers/iio/adc/ltc2497-core.c +4
-0+return IIO_VAL_INT ; +default : +return -EINVAL ; + }default :return -EINVAL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v4 3/3 iio: adc: ltc2497: add 2x conversion speed mode
In reply to Andrei StancoviciThe LTC2499 can convert at twice the output rate by disabling the offset auto-calibration. Expose it through sampling_frequency and sampling_frequency_available on the voltage channels. The part ignores the setting while measuring temperature, so the temperature channel does not carry the attribute.
The wait before a result is read is keyed on the conversion currently in flight, whose duration is fixed by the mode that was active when it started rather than by the mode just selected. Otherwise the first read after a 1x -> 2x switch can reprogram the part while a 1x conversion is still running, which the part NACKs.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2499fe.pdf
---
Changes in v4:
- The channel table is now two static const tables selected in probe, replacing the devm_kmemdup() copy and runtime fix-up of a shared table. The 32 voltage entries moved into a macro that emits the whole table (same shape as DEFINE_AD7380_2_CHANNEL); the move is mechanical and the entries are unchanged. Checked two ways: the preprocessor expansion of the non-speed table is identical to v3 apart from the added info_mask_shared_by_type_available = 0, and the sysfs attribute tree of both parts is byte-identical before and after.
- Comments trimmed: the rounding rationale, the msleep granularity note and the FA/FB aside are gone; the paragraph explaining what 2x actually does is kept.
- Naming settled on speed_2x / has_speed_mode throughout; the v3 mix of speed / sped / SPD is gone.
- Description shortened. The in-flight conversion-time paragraph is kept because it is the part that is not obvious from the code.
- struct ltc2497_chip_info reordered as described in patch 2/3.
- Added the Datasheet: trailer.
drivers/iio/adc/ltc2497-core.c | 242 ++++++++++++++++++++++++++------- drivers/iio/adc/ltc2497.c | 28 ++-- drivers/iio/adc/ltc2497.h | 17 ++- 3 files changed, 228 insertions(+), 59 deletions(-)
drivers/iio/adc/ltc2497-core.c +195 -47
@@ -21,24 +21,54 @@#define LTC2497_DIFF 0#define LTC2497_SIGN BIT(3) -static int ltc2497core_wait_conv (struct ltc2497core_driverdata *ddata) +/* +* Output-rate modes, indexed by ltc2497core_driverdata.speed_2x +* (0 = 1x, the power-on default; 1 = 2x, LTC2499 only). The advertised +* sampling_frequency and the conversion-time budget are two views of the same +* mode, so they are kept in lock-step here. +*/ +static const int ltc2497core_samp_freq_avail[] = { +6 ,800000 ,/* 1x: ~6.8 Hz (1 / t_CONV_1 typ 146.9ms) */ +13 ,600000 ,/* 2x: ~13.6 Hz (1 / t_CONV_2 typ 73.6ms) */ +}; + +static const unsigned int ltc2497core_conv_time_ms_tbl[] = { +LTC2497_CONV_TIME_1X_MS ,/* 1x */ +LTC2499_CONV_TIME_2X_MS ,/* 2x */ +}; + +static unsigned int ltc2497core_conv_time_ms (struct ltc2497core_driverdata *ddata, +u8 address) +{ +/* +* SPD is ignored by the part during a temperature measurement: it +* always converts at 1x, so budget the 1x time regardless of the +* selected voltage-channel mode. +*/ +if (address ==LTC2497_TEMP_ADDR ) +return ltc2497core_conv_time_ms_tbl[0 ]; + +return ltc2497core_conv_time_ms_tbl[ddata->speed_2x]; +} + +static int ltc2497core_wait_conv (struct ltc2497core_driverdata *ddata, +unsigned int conv_time_ms) {s64 time_elapsed; time_elapsed =ktime_ms_delta (ktime_get (), ddata->time_prev); -if (time_elapsed <LTC2497_CONVERSION_TIME_MS ) { +if (time_elapsed < conv_time_ms) {/* delay if conversion time not passed * since last read or write */ -if (msleep_interruptible ( -LTC2497_CONVERSION_TIME_MS - time_elapsed)) +if (msleep_interruptible (conv_time_ms - time_elapsed))return -ERESTARTSYS ;return 0 ; } -if (time_elapsed -LTC2497_CONVERSION_TIME_MS <=0 ) { +if (time_elapsed - conv_time_ms <=0 ) {/* We're in automatic mode - * so the last reading is still not outdated */
@@ -50,9 +80,18 @@ static int ltc2497core_wait_conv(struct ltc2497core_driverdata *ddata)static int ltc2497core_read (struct ltc2497core_driverdata *ddata,u8 address,int *val) { +unsigned int conv_time_ms =ltc2497core_conv_time_ms (ddata, address);int ret; - ret =ltc2497core_wait_conv (ddata); +/* +* Wait for the conversion currently in flight, whose duration was fixed +* by the mode active when it was started (ddata->conv_time_prev). This +* can be longer than the freshly selected mode's time - e.g. a 1x +* conversion is still running when the first 2x read arrives after a +* sampling_frequency change - and reprogramming the device before it +* finishes would be NACKed (-EIO). +*/ + ret =ltc2497core_wait_conv (ddata, ddata->conv_time_prev);if (ret <0 )return ret;
@@ -62,7 +101,17 @@ static int ltc2497core_read(struct ltc2497core_driverdata *ddata, u8 address, inreturn ret; ddata->addr_prev = address; -if (msleep_interruptible (LTC2497_CONVERSION_TIME_MS )) +/* +* The reprogram above starts a conversion in the new mode. +* Record its start time and duration before sleeping, so that if +* msleep_interruptible() is interrupted the conversion state is +* already consistent: the next retry then waits only the time +* remaining from the real start instead of from a stale +* time_prev, which would let it reprogram/read too early. +*/ + ddata->time_prev =ktime_get (); + ddata->conv_time_prev = conv_time_ms; +if (msleep_interruptible (conv_time_ms))return -ERESTARTSYS ; }
@@ -71,6 +120,8 @@ static int ltc2497core_read(struct ltc2497core_driverdata *ddata, u8 address, inreturn ret; ddata->time_prev =ktime_get (); +/* The read above auto-starts the next conversion in the current mode. */ + ddata->conv_time_prev = conv_time_ms;return ret; }
@@ -137,32 +188,113 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev,return -EINVAL ; } +case IIO_CHAN_INFO_SAMP_FREQ : +/* +* Only advertised on the voltage channels of parts with a speed +* mode; the sampling frequency is a property of the selected 1x/2x +* mode, not of an individual conversion. +*/ +mutex_lock (&ddata->lock); + *val = ltc2497core_samp_freq_avail[ddata->speed_2x *2 ]; + *val2 = ltc2497core_samp_freq_avail[ddata->speed_2x *2 +1 ]; +mutex_unlock (&ddata->lock); + +return IIO_VAL_INT_PLUS_MICRO ; +default :return -EINVAL ; } } -#define LTC2497_CHAN (_chan, _addr, _ds_name) { \ +static int ltc2497core_read_avail (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +const int **vals,int *type,int *length, +long mask) +{ +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : + *vals = ltc2497core_samp_freq_avail; + *type =IIO_VAL_INT_PLUS_MICRO ; + *length =ARRAY_SIZE (ltc2497core_samp_freq_avail); +return IIO_AVAIL_LIST ; + +default : +return -EINVAL ; + } +} + +static int ltc2497core_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct ltc2497core_driverdata *ddata =iio_priv (indio_dev); +unsigned int i; +bool speed_2x; + +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : +/* Match the (val, val2) pair against the advertised rates. */ +for (i =0 ; i <ARRAY_SIZE (ltc2497core_samp_freq_avail); i +=2 ) { +if (val == ltc2497core_samp_freq_avail[i] && + val2 == ltc2497core_samp_freq_avail[i +1 ]) +break ; + } +if (i ==ARRAY_SIZE (ltc2497core_samp_freq_avail)) +return -EINVAL ; + + speed_2x = i /2 ; + +mutex_lock (&ddata->lock); + ddata->speed_2x = speed_2x; +/* +* The new speed only takes effect once the second command byte +* is reprogrammed, so force the next read to reprogram rather +* than reuse the value already latched for this address. +* LTC2497_CONFIG_DEFAULT is not a valid channel/temperature +* address, so it is a safe re-arm sentinel (as used at probe). +* +* A conversion started under the old speed may still be in +* flight; its own duration (conv_time_prev), not the new mode's, +* still gates the next reprogram, so the timing state is left +* untouched here. +*/ + ddata->addr_prev =LTC2497_CONFIG_DEFAULT ; +mutex_unlock (&ddata->lock); + +return 0 ; + +default : +return -EINVAL ; + } +} + +#define LTC2497_CHAN (_chan, _addr, _ds_name, _extra_mask) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ .channel = (_chan), \ .address = (_addr | (_chan / 2) | ((_chan & 1) ? LTC2497_SIGN : 0)), \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | (_extra_mask), \ + .info_mask_shared_by_type_available = (_extra_mask), \ .datasheet_name = (_ds_name), \ } -#define LTC2497_CHAN_DIFF (_chan, _addr) { \ +#define LTC2497_CHAN_DIFF (_chan, _addr, _extra_mask) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ .channel = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 1 : 0), \ .channel2 = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 0 : 1),\ .address = (_addr | _chan), \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | (_extra_mask), \ + .info_mask_shared_by_type_available = (_extra_mask), \ .differential = 1, \ } +/* +* SPD is ignored by the part during a temperature measurement, so the +* temperature channel carries no sampling-frequency attribute. +*/ #define LTC2497_TEMP_CHANNEL { \ .type = IIO_TEMP, \ .address = LTC2497_TEMP_ADDR, \
@@ -171,44 +303,55 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev, BIT(IIO_CHAN_INFO_OFFSET), \ } -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_TEMP_CHANNEL , -}; +/* +* Parts with a speed mode advertise sampling_frequency on their voltage +* channels; the two tables are otherwise identical. +*/ +#define LTC2497_DEFINE_CHANNELS (_name, _extra_mask) \ +static const struct iio_chan_spec _name[] = { \ + LTC2497_CHAN(0, LTC2497_SGL, "CH0", _extra_mask), \ + LTC2497_CHAN(1, LTC2497_SGL, "CH1", _extra_mask), \ + LTC2497_CHAN(2, LTC2497_SGL, "CH2", _extra_mask), \ + LTC2497_CHAN(3, LTC2497_SGL, "CH3", _extra_mask), \ + LTC2497_CHAN(4, LTC2497_SGL, "CH4", _extra_mask), \ + LTC2497_CHAN(5, LTC2497_SGL, "CH5", _extra_mask), \ + LTC2497_CHAN(6, LTC2497_SGL, "CH6", _extra_mask), \ + LTC2497_CHAN(7, LTC2497_SGL, "CH7", _extra_mask), \ + LTC2497_CHAN(8, LTC2497_SGL, "CH8", _extra_mask), \ + LTC2497_CHAN(9, LTC2497_SGL, "CH9", _extra_mask), \ + LTC2497_CHAN(10, LTC2497_SGL, "CH10", _extra_mask), \ + LTC2497_CHAN(11, LTC2497_SGL, "CH11", _extra_mask), \ + LTC2497_CHAN(12, LTC2497_SGL, "CH12", _extra_mask), \ + LTC2497_CHAN(13, LTC2497_SGL, "CH13", _extra_mask), \ + LTC2497_CHAN(14, LTC2497_SGL, "CH14", _extra_mask), \ + LTC2497_CHAN(15, LTC2497_SGL, "CH15", _extra_mask), \ + LTC2497_CHAN_DIFF(0, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(1, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(2, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(3, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(4, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(5, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(6, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(7, LTC2497_DIFF, _extra_mask), \ + LTC2497_CHAN_DIFF(0, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(1, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(2, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(3, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(4, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(5, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(6, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_CHAN_DIFF(7, LTC2497_DIFF | LTC2497_SIGN, _extra_mask), \ + LTC2497_TEMP_CHANNEL, \ +} + +LTC2497_DEFINE_CHANNELS (ltc2497core_channel,0 ); +LTC2497_DEFINE_CHANNELS (ltc2497core_channel_samp_freq, +BIT (IIO_CHAN_INFO_SAMP_FREQ ));static const struct iio_info ltc2497core_info = { .read_raw = ltc2497core_read_raw, + .read_avail = ltc2497core_read_avail, + .write_raw = ltc2497core_write_raw, };int ltc2497core_probe (struct device *dev,struct iio_dev *indio_dev)
@@ -228,7 +371,10 @@ 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; +if (ddata->chip_info->has_speed_mode) + indio_dev->channels = ltc2497core_channel_samp_freq; +else + indio_dev->channels = ltc2497core_channel;/* Only the ltc2499 has a temperature channel; it is the last entry. */ if (ddata->chip_info->has_temp) indio_dev->num_channels =ARRAY_SIZE (ltc2497core_channel);
@@ -259,6 +405,8 @@ int ltc2497core_probe(struct device *dev, struct iio_dev *indio_dev) ddata->addr_prev =LTC2497_CONFIG_DEFAULT ; ddata->time_prev =ktime_get (); +/* Power-on default mode is 1x; a conversion is already in flight. */ + ddata->conv_time_prev =LTC2497_CONV_TIME_1X_MS ;mutex_init (&ddata->lock);
drivers/iio/adc/ltc2497.c +17 -11
@@ -85,21 +85,24 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, }/* -* Parts with the internal PTAT sensor (LTC2499) latch their converter -* configuration via a second command byte and only re-evaluate it when -* that byte has EN2 set; a single byte, or a second byte with EN2 = 0, -* means "keep previous". A one-byte channel select therefore cannot pull -* the device back out of temperature mode, so a voltage read after a -* temperature read would keep returning the PTAT result. Always drive the -* second byte with EN2 set on these parts: IM = 1 for a temperature read, -* EN2 alone (IM = 0) to (re)select an external input. FA = FB = 0 keeps -* the power-on simultaneous 50/60Hz rejection, whose worst-case -* conversion time the driver's wait already covers. +* Parts with a second config byte (LTC2499: internal PTAT sensor and/or +* the 2x speed mode) latch their converter configuration from that byte +* and only re-evaluate it when EN2 is set; a single byte, or a second +* byte with EN2 = 0, means "keep previous". A one-byte channel select +* therefore cannot pull the device back out of temperature mode, so a +* voltage read after a temperature read would keep returning the PTAT +* result. Always drive the second byte with EN2 set on these parts: +* - temperature read: IM = 1 (SPD is ignored by the part in +* temperature mode and is left 0 here); +* - voltage read: IM = 0 (external input), plus SPD when 2x is +* selected. +* FA = FB = 0 keeps the power-on simultaneous 50/60Hz rejection, whose +* worst-case conversion time the driver's wait already covers. * * The byte could be skipped while the latched configuration is already * the one wanted; it is sent on every conversion for simplicity. */ -if (ddata->chip_info->has_temp) { +if (ddata->chip_info->has_temp || ddata->chip_info->has_speed_mode) {u8 cmd[2 ];if (address ==LTC2497_TEMP_ADDR ) {
@@ -108,6 +111,8 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata, }else { cmd[0 ] =LTC2497_ENABLE | address; cmd[1 ] =LTC2499_EN2 ; +if (ddata->speed_2x) + cmd[1 ] |=LTC2499_SPD ; } ret =i2c_master_send (st->client, cmd,sizeof (cmd));
@@ -173,6 +178,7 @@ static const struct ltc2497_chip_info ltc2497_info[] = { .resolution = 24, .name = "ltc2499", .has_temp = true, + .has_speed_mode = true, }, };
drivers/iio/adc/ltc2497.h +16 -1
@@ -2,7 +2,17 @@#define LTC2497_ENABLE 0xA0#define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE -#define LTC2497_CONVERSION_TIME_MS 150ULL + +/* +* Conversion-time bounds used to gate reads. Each value is the datasheet +* t_CONV maximum, rounded UP to the next whole millisecond. +* +* The 2x mode (LTC2499_SPD, LTC2499 only) disables the offset auto-calibration +* to roughly double the output rate; adding the 2x wait time is what makes the +* SPD control actually faster. +*/ +#define LTC2497_CONV_TIME_1X_MS 150ULL/* ceil(t_CONV_1 simult. max 149.9) */ +#define LTC2499_CONV_TIME_2X_MS 76ULL/* ceil(t_CONV_2 simult. max 75.1) */ /* * Sentinel passed as `address` to result_and_measure() to request a
@@ -14,11 +24,13 @@/* Second config-byte bits (LTC2499 / LTC2493 only) */ #define LTC2499_EN2 BIT(7)/* enable second config byte */ #define LTC2499_IM BIT(6)/* 1 = measure internal temp sensor */ +#define LTC2499_SPD BIT(3)/* 1 = 2x output rate (offset cal off) */ struct ltc2497_chip_info {const char *name;u32 resolution;bool has_temp; +bool has_speed_mode;/* SPD bit in the 2nd config byte (LTC2499/LTC2493) */ };struct ltc2497core_driverdata {
@@ -28,6 +40,9 @@ struct ltc2497core_driverdata {struct mutex lock;const struct ltc2497_chip_info *chip_info;u8 addr_prev; +bool speed_2x;/* SPD: false = 1x (default), true = 2x */ +/* Conversion time (ms) of the conversion currently in flight. */ +unsigned int conv_time_prev;int (*result_and_measure)(struct ltc2497core_driverdata *ddata,u8 address,int *val); };
Signature · 1 line
2.43.0
Re: [PATCH v4 3/3] iio: adc: ltc2497: add 2x conversion speed mode
Quoted 11 lines · Andrei Stancovici
The LTC2499 can convert at twice the output rate by disabling the offset auto-calibration. Expose it through
sampling_frequencyandsampling_frequency_availableon the voltage channels. The part ignores the setting while measuring temperature, so the temperature channel does not carry the attribute.The wait before a result is read is keyed on the conversion currently in flight, whose duration is fixed by the mode that was active when it started rather than by the mode just selected. Otherwise the first read after a 1x -> 2x switch can reprogram the part while a 1x conversion is still running, which the part NACKs.
...
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 349
Quoted 2 lines
drivers/iio/adc/ltc2497-core.c +2
-0+LTC2497_DEFINE_CHANNELS (ltc2497core_channel_samp_freq, +BIT (IIO_CHAN_INFO_SAMP_FREQ ));
I believe having this on a single line is fine.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v4 3/3] iio: adc: ltc2497: add 2x conversion speed mode
Quoted 13 lines · On Wed, 9 Sep 2026 17:04:57 +0300 Andy Shevchenko…
Quoted 11 lines · Andrei Stancovici
The LTC2499 can convert at twice the output rate by disabling the offset auto-calibration. Expose it through
sampling_frequencyandsampling_frequency_availableon the voltage channels. The part ignores the setting while measuring temperature, so the temperature channel does not carry the attribute.The wait before a result is read is keyed on the conversion currently in flight, whose duration is fixed by the mode that was active when it started rather than by the mode just selected. Otherwise the first read after a 1x -> 2x switch can reprogram the part while a 1x conversion is still running, which the part NACKs.
Quoted 9 lines
Reviewed-by Andy Shevchenko <[email protected]>...
drivers/iio/adc/ltc2497-core.c static int ltc2497core_read_raw(struct iio_dev *indio_dev, · line 349
Quoted 2 lines
drivers/iio/adc/ltc2497-core.c +2
-0+LTC2497_DEFINE_CHANNELS (ltc2497core_channel_samp_freq, +BIT (IIO_CHAN_INFO_SAMP_FREQ ));I believe having this on a single line is fine.
Tweaked.
I've taken the somewhat (currently) unusual step of picking up 2 and 3, but not patch 1. In the past I've always been one to pick up whatever is ready but since that started confusing sashiko I stopped doing so. Here it doesn't matter as patch 1 is independent anyway!
So patches 2 and 3 are on the testing branch of iio.git.
Thanks,
Jonathan