iio: Add support for TI ADS1X18 ADCs
Subsystems (3)
Kurt Borja
maintainerv2 cover iio: Add support for TI ADS1X18 ADCs
Hi,
This series adds a new driver for TI ADS1X18 SPI devices.
This is my first time contributing to the IIO subsystem and making dt-bindings documentation, so (don't) go easy on me :p.
As explained in Patch 2 changelog, the DRDY interrupt line is shared with the MOSI pin. This awkward quirk is also found on some Analog Devices sigma-delta SPI ADCs, so the interrupt and trigger design is inspired by those.
@ David:
I didn't move enable_irq() and spi_bus_lock() out of .set_trigger_state. I explained some of my reasoning in v1 and I expanded patch 2 changelog on that. If you disagree with this, let me know!
Thank you in advance for your reviews.
---
v2:
- [Patch 1]:
- Move MAINTAINERS change here
- Use generic node names: ads1118@0 -> adc@0
- Rename file to ti,ads1118.yaml -> ti,ads1018.yaml
- Drop ti,gain and ti,datarate
- Add spi-cpha and spi-max-frecuency properties as they are fixed in all models
- Add vdd-supply
- Make interrupts and drdy-gpios optional properties
- [Patch 2]:
- Update probe based on dt-bindings changes
- Rename file to ti-ads1x18.c -> ti-ads1018.c
- Rework ads1018_oneshot(), instead of waiting for IRQ wait an appropriate delay before reading again
- Only alloc and register a trigger if we have an IRQ line
- Drop ads1x18->msg_lock in favor of IIO API locks
- Read conver before enabling and after disabling IRQ to ensure CS state is correct
- Add ads1018_read_locked() which takes an additional argument hold_cs to explicitly control CS state in trigger and buffer
- Fix ADS1X18_CHANNELS_MAX limit 9 -> 10
- Call iio_trigger_notify_done() in all IRQ handler paths
- Drop unused includes
- Drop BIT_U16 and GENMASK_U16 macros
- Drop unnecessary named defines
- Use u8 types in ads1018_chan_data
- Rename some struct members for clarity
- Move tx_buf and rx_buf to the end of struct ads1018
- Rework channel handling to just make everything visible and add ADS1018_VOLT_DIFF_CHAN
- Use .scan_index instead of .address in IIO channels
- v1: https://lore.kernel.org/r/[email protected]
---
Kurt Borja (2):
dt-bindings: iio: adc: Add TI ADS1018/ADS1118
iio: adc: Add ti-ads1018 driver
.../devicetree/bindings/iio/adc/ti,ads1018.yaml | 93 +++ MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ti-ads1018.c | 716 +++++++++++++++++++++ 5 files changed, 829 insertions(+)
---
base-commit: f9e05791642810a0cf6237d39fafd6fec5e0b4bb
change-id: 20251012-ads1x18-0d0779d06690
Signature · 1 line
~ Kurt
Kurt Borja
maintainerPATCH v2 1/2 dt-bindings: iio: adc: Add TI ADS1018/ADS1118
Add documentation for Texas Instruments ADS1018 and ADS1118 analog-to-digital converters.
--- .../devicetree/bindings/iio/adc/ti,ads1018.yaml | 93 ++++++++++++++++++++++ MAINTAINERS | 6 ++ 2 files changed, 99 insertions(+)
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +93 -0
@@ -0,0 +1,93 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/ti,ads1018.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: TI ADS1018/ADS1118 SPI analog to digital converter + +maintainers: + - Kurt Borja <[email protected]> + +description: | + The ADS1018/ADS1118 is a precision, low-power, 12-bit or 16-bit, noise-free, + analog-to-digital converter (ADC). It integrates a programmable gain amplifier + (PGA), voltage reference, oscillator and high-accuracy temperature sensor. + + Datasheets: + - ADS1018: https://www.ti.com/lit/ds/symlink/ads1018.pdf + - ADS1118: https://www.ti.com/lit/ds/symlink/ads1118.pdf + +properties: + compatible: + enum: + - ti,ads1018 + - ti,ads1118 + + reg: + maxItems: 1 + + vdd-supply: true + + spi-max-frequency: + maximum: 4000000 + + spi-cpha: true + + interrupts: + description: DOUT/DRDY (Data Out/Data Ready) line. + maxItems: 1 + + drdy-gpios: + description: + Extra GPIO line connected to DOUT/DRDY (Data Out/Data Ready). This allows + distinguishing between interrupts triggered by the data-ready signal and + interrupts triggered by an SPI transfer. + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + + '#io-channel-cells': + const: 1 + +required: + - compatible + - reg + - vdd-supply + - '#address-cells' + - '#size-cells' + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +unevaluatedProperties: true + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + #include <dt-bindings/gpio/gpio.h> + + spi { + #address-cells = <1>; + #size-cells = <0>; + + adc@0 { + compatible = "ti,ads1118"; + reg = <0>; + + #address-cells = <1>; + #size-cells = <0>; + + spi-max-frequency = <4000000>; + spi-cpha; + + vdd-supply = <&vdd_3v3_reg>; + + interrupts-extended = <&gpio 14 IRQ_TYPE_EDGE_FALLING>; + drdy-gpios = <&gpio 14 GPIO_ACTIVE_LOW>; + }; + };
MAINTAINERS +6 -0
@@ -25646,6 +25646,12 @@ S: Maintained F: Documentation/devicetree/bindings/iio/adc/ti,ads1119.yaml F: drivers/iio/adc/ti-ads1119.c +TI ADS1018 ADC DRIVER +M: Kurt Borja <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml + TI ADS7924 ADC DRIVER M: Hugo Villeneuve <[email protected]> L: [email protected]
Signature · 1 line
2.52.0
Kurt Borja
maintainerPATCH v2 2/2 iio: adc: Add ti-ads1018 driver
In reply to Kurt BorjaAdd ti-ads1018 driver for Texas Instruments ADS1018 and ADS1118 SPI analog-to-digital converters.
These chips' MOSI pin is shared with a data-ready interrupt. Defining this interrupt in devicetree is optional, therefore we only create an IIO trigger if one is found.
Handling this interrupt requires some considerations. When enabling the trigger the CS line is tied low (active), thus we need to hold spi_bus_lock() too, to avoid state corruption. This is done inside the set_trigger_state() callback, to let users use other triggers without wasting a bus lock.
--- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ti-ads1018.c | 716 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 730 insertions(+)
MAINTAINERS +1 -0
@@ -25651,6 +25651,7 @@ M: Kurt Borja <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml +F: drivers/iio/adc/ti-ads1018.c TI ADS7924 ADC DRIVER M: Hugo Villeneuve <[email protected]>
drivers/iio/adc/Kconfig +12 -0
@@ -1664,6 +1664,18 @@ config TI_ADS1015 This driver can also be built as a module. If so, the module will be called ti-ads1015. +config TI_ADS1018 + tristate "Texas Instruments ADS1018 ADC" + depends on SPI + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + If you say yes here you get support for Texas Instruments ADS1018 and + ADS1118 ADC chips. + + This driver can also be built as a module. If so, the module will be + called ti-ads1018. + config TI_ADS1100 tristate "Texas Instruments ADS1100 and ADS1000 ADC" depends on I2C
drivers/iio/adc/Makefile +1 -0
@@ -145,6 +145,7 @@ obj-$(CONFIG_TI_ADC12138) += ti-adc12138.o obj-$(CONFIG_TI_ADC128S052) += ti-adc128s052.o obj-$(CONFIG_TI_ADC161S626) += ti-adc161s626.o obj-$(CONFIG_TI_ADS1015) += ti-ads1015.o +obj-$(CONFIG_TI_ADS1018) += ti-ads1018.o obj-$(CONFIG_TI_ADS1100) += ti-ads1100.o obj-$(CONFIG_TI_ADS1119) += ti-ads1119.o obj-$(CONFIG_TI_ADS124S08) += ti-ads124s08.o
drivers/iio/adc/ti-ads1018.c new file +716 -0
716 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v2 1/2] dt-bindings: iio: adc: Add TI ADS1018/ADS1118
In reply to Kurt BorjaQuoted 7 lines · Kurt Borja
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +7
-0+ + interrupts: + description: DOUT/DRDY (Data Out/Data Ready) line. + maxItems: 1 + + drdy-gpios: + description:
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml line 52
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +10
-0+ Extra GPIO line connected to DOUT/DRDY (Data Out/Data Ready). This allows + distinguishing between interrupts triggered by the data-ready signal and + interrupts triggered by an SPI transfer. + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0
These two properties are not correct now - you do not have any children.
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml line 62
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +10
-0+ + '#io-channel-cells': + const: 1 + +required: + - compatible + - reg + - vdd-supply + - '#address-cells' + - '#size-cells'
And they should not be required.
Best regards,
Krzysztof
Re: [PATCH v2 2/2] iio: adc: Add ti-ads1018 driver
In reply to Kurt BorjaQuoted 12 lines · Kurt Borja
Add ti-ads1018 driver for Texas Instruments ADS1018 and ADS1118 SPI analog-to-digital converters.
These chips' MOSI pin is shared with a data-ready interrupt. Defining this interrupt in devicetree is optional, therefore we only create an IIO trigger if one is found.
Handling this interrupt requires some considerations. When enabling the trigger the CS line is tied low (active), thus we need to hold
spi_bus_lock()too, to avoid state corruption. This is done inside theset_trigger_state()callback, to let users use other triggers without wasting a bus lock.
...
drivers/iio/adc/ti-ads1018.c line 12
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+#include <linux/array_size.h> +#include <linux/bitfield.h> +#include <linux/bitmap.h> +#include <linux/bitops.h> +#include <linux/bits.h>
bits.h and bitops.h are guaranteed by bitmap.h, so
#include <linux/bitmap.h>
is enough for all three.
drivers/iio/adc/ti-ads1018.c line 20
Quoted 8 lines
drivers/iio/adc/ti-ads1018.c new file +8
-0+#include <linux/byteorder/generic.h> +#include <linux/dev_printk.h> +#include <linux/gpio/consumer.h> +#include <linux/interrupt.h> +#include <linux/math.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/spi/spi.h>
+ types.h // E.g., u8 just a page below :-)
Quoted 1 line
+#include <
linux/units.h>
...
drivers/iio/adc/ti-ads1018.c line 201
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+static unsigned long ads1018_calc_delay (struct ads1018 *ads1018) +{ +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +unsigned long mode = chip_info->num_data_rate_mode_to_hz -1 ; +int hz = chip_info->data_rate_mode_to_hz[mode];
Can frequency be negative?
drivers/iio/adc/ti-ads1018.c line 206
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+/* We subtract -10% data-rate error */ + hz -=DIV_ROUND_UP (hz,10 ); + +/* Calculate time per sample in usecs */
Spell it fully "microseconds" or use proper unit with a Greek latter "µs".
Quoted 2 lines
(quoted diff) +2
-0+ return DIV_ROUND_UP(MICROHZ_PER_HZ, hz); +}
...
drivers/iio/adc/ti-ads1018.c line 257
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+static int +__ads1018_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int *val,int *val2,long mask)
Better naming is something like ads1018_read_raw_unlocked() if I got the context right.
drivers/iio/adc/ti-ads1018.c line 264
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +7
-0+{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +u8 drate_mode =ads1018_get_data_rate_mode (ads1018, chan->scan_index); +u8 pga_mode =ads1018_get_pga_mode (ads1018, chan->scan_index); +u8 max_drate_mode = chip_info->num_data_rate_mode_to_hz -1 ; +u16 cnv, cfg =0 ;
Unneeded cfg assignment. See below.
drivers/iio/adc/ti-ads1018.c line 270
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + cfg |=ADS1018_CFG_VALID ; + cfg |=ADS1018_CFG_OS_TRIG ;
cfg = ADS1018_CFG_VALID | ADS1018_CFG_OS_TRIG;
drivers/iio/adc/ti-ads1018.c line 274
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+ cfg |=FIELD_PREP (ADS1018_CFG_MUX_MASK , chan->scan_index); + cfg |=FIELD_PREP (ADS1018_CFG_PGA_MASK , pga_mode); + cfg |=FIELD_PREP (ADS1018_CFG_MODE_MASK ,ADS1018_MODE_ONESHOT ); + cfg |=FIELD_PREP (ADS1018_CFG_DRATE_MASK , max_drate_mode);
+ blank line.
Quoted 36 lines
drivers/iio/adc/ti-ads1018.c new file +36
-0+if (chan->type ==IIO_TEMP ) + cfg |=ADS1018_CFG_TS_MODE_EN ; + + ret =ads1018_oneshot (ads1018, cfg, &cnv); +if (ret) +return ret; + + cnv >>= chan->scan_type.shift; + *val =sign_extend32 (cnv, chan->scan_type.realbits -1 ); + +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : +switch (chan->type) { +case IIO_VOLTAGE : + *val = chip_info->pga_mode_to_gain[pga_mode][0 ]; + *val2 = chip_info->pga_mode_to_gain[pga_mode][1 ]; +return IIO_VAL_INT_PLUS_NANO ; + +case IIO_TEMP : + *val = chip_info->temp_scale[0 ]; + *val2 = chip_info->temp_scale[1 ]; +return IIO_VAL_INT_PLUS_MICRO ; + +default : +return -EOPNOTSUPP ; + } + +case IIO_CHAN_INFO_SAMP_FREQ : + *val = chip_info->data_rate_mode_to_hz[drate_mode]; +return IIO_VAL_INT ; + +default : +return -EOPNOTSUPP ; + } +}
...
drivers/iio/adc/ti-ads1018.c line 352
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+static int +__ads1018_write_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int val,int val2,long mask)
Naming...
drivers/iio/adc/ti-ads1018.c line 359
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +7
-0+{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +const struct ads1018_chip_info *info = ads1018->chip_info; + +switch (mask) { +case IIO_CHAN_INFO_SCALE : +for (int i =0 ; i < info->num_pga_mode_to_gain; i++) {
Why is 'i' signed?
drivers/iio/adc/ti-ads1018.c line 365
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+if (val != info->pga_mode_to_gain[i][0 ] || + val2 != info->pga_mode_to_gain[i][1 ]) +continue ; +ads1018_set_pga_mode (ads1018, chan->scan_index, i); +return 0 ;
This is an invariant to the loop, perhaps
drivers/iio/adc/ti-ads1018.c line 368
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+ } +return -EINVAL ;
for (unsigned int i = 0; i < info->num_pga_mode_to_gain; i++) {
if (val == info->pga_mode_to_gain[i][0] ||
val2 == info->pga_mode_to_gain[i][1])
break;
}
if (i == info->num_pga_mode_to_gain)
return -EINVAL;
ads1018_set_pga_mode(ads1018, chan->scan_index, i);
return 0;This also will follow the traditional pattern: Check for the error cases first.
Review
Quoted 2 lines
(quoted diff) +2
-0+ case IIO_CHAN_INFO_SAMP_FREQ: + for (int i = 0; i < info->num_data_rate_mode_to_hz; i++) {
Ditto, same comments as per previous loop.
Quoted 13 lines
drivers/iio/adc/ti-ads1018.c new file +13
-0+if (val != info->data_rate_mode_to_hz[i]) +continue ; + +ads1018_set_data_rate_mode (ads1018, chan->scan_index, i); +return 0 ; + } + +return -EINVAL ; + +default : +return -EOPNOTSUPP ; + } +}
...
Quoted 11 lines
drivers/iio/adc/ti-ads1018.c new file +11
-0+static int ads1018_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct ads1018 *ads1018 =iio_trigger_get_drvdata (trig); +__be16 cnv; + +/* +* We need to lock the SPI bus and tie CS low (hold_cs) to catch +* data-ready interrupts, otherwise the MISO line enters a Hi-Z state. +*/ + +if (state) {
drivers/iio/adc/ti-ads1018.c line 439
Quoted 10 lines
drivers/iio/adc/ti-ads1018.c new file +10
-0+spi_bus_lock (ads1018->spi->controller); +ads1018_read_locked (ads1018, &cnv, true); +enable_irq (ads1018->drdy_irq); + }else { +disable_irq (ads1018->drdy_irq); +ads1018_read_locked (ads1018, &cnv, false); +spi_bus_unlock (ads1018->spi->controller); + } + +return 0 ;
This function is basically
if (foo)
do this
else
do that
please make just two distinct functions instead.
Quoted 1 line
+}
That's how it will look like:
static void ads1018_set_trigger_enable(struct ads1018 *ads1018)
{
__be16 cnv;
spi_bus_lock(ads1018->spi->controller);
ads1018_read_locked(ads1018, &cnv, true);
enable_irq(ads1018->drdy_irq);
}static void ads1018_set_trigger_disable(struct ads1018 *ads1018)
{
__be16 cnv;
disable_irq(ads1018->drdy_irq);
ads1018_read_locked(ads1018, &cnv, false);
spi_bus_unlock(ads1018->spi->controller);
}static int ads1018_set_trigger_state(struct iio_trigger *trig, bool state)
{
struct ads1018 *ads1018 = iio_trigger_get_drvdata(trig);
/*
* We need to lock the SPI bus and tie CS low (hold_cs) to catch
* data-ready interrupts, otherwise the MISO line enters a Hi-Z state.
*/
if (state)
ads1018_set_trigger_enable(ads1018);
else
ads1018_set_trigger_disable(ads1018);
return 0;
}Now is the question, can we get rid of dummy variables by checking for the pointer in the _read_locked()? If so, the above become as simple as
static void ads1018_set_trigger_enable(struct ads1018 *ads1018)
{
spi_bus_lock(ads1018->spi->controller);
ads1018_read_locked(ads1018, NULL, true);
enable_irq(ads1018->drdy_irq);
}static void ads1018_set_trigger_disable(struct ads1018 *ads1018)
{
disable_irq(ads1018->drdy_irq);
ads1018_read_locked(ads1018, NULL, false);
spi_bus_unlock(ads1018->spi->controller);
}Or if you make _read_locked() to return the data, just ignoring it as in
ads1018_read_locked(ads1018, ...);
...
drivers/iio/adc/ti-ads1018.c line 452
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+static int ads1018_buffer_preenable (struct iio_dev *indio_dev) +{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +unsigned int pga, drate, addr; +u16 cfg =0 ;
Unneeded assignment.
drivers/iio/adc/ti-ads1018.c line 459
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+ addr =find_first_bit (indio_dev->active_scan_mask, +iio_get_masklength (indio_dev)); + pga =ads1018_get_pga_mode (ads1018, addr); + drate =ads1018_get_data_rate_mode (ads1018, addr); + cfg |=ADS1018_CFG_VALID ;
cfg = ADS1018_CFG_VALID;
drivers/iio/adc/ti-ads1018.c line 463
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+ cfg |=FIELD_PREP (ADS1018_CFG_MUX_MASK , addr); + cfg |=FIELD_PREP (ADS1018_CFG_PGA_MASK , pga); + cfg |=FIELD_PREP (ADS1018_CFG_MODE_MASK ,ADS1018_MODE_CONTINUOUS ); + cfg |=FIELD_PREP (ADS1018_CFG_DRATE_MASK , drate);
+ blank line.
Quoted 8 lines
drivers/iio/adc/ti-ads1018.c new file +8
-0+if (chip_info->channels[addr].type ==IIO_TEMP ) + cfg |=ADS1018_CFG_TS_MODE_EN ; + + ads1018->tx_buf[0 ] =cpu_to_be16 (cfg); + ads1018->tx_buf[1 ] =0 ; + +return spi_write (ads1018->spi, ads1018->tx_buf,sizeof (ads1018->tx_buf)); +}
...
Quoted 19 lines
drivers/iio/adc/ti-ads1018.c new file +19
-0+static irqreturn_t ads1018_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct ads1018 *ads1018 =iio_priv (indio_dev); +struct { +__be16 conv; +aligned_s64 ts; + } scan = {}; +int ret; + +if (iio_device_claim_buffer_mode (indio_dev)) + goto out_notify_done; + +if (iio_trigger_using_own (indio_dev)) { +disable_irq (ads1018->drdy_irq); + ret =ads1018_read_locked (ads1018, &scan.conv, true); +enable_irq (ads1018->drdy_irq); + }else {
drivers/iio/adc/ti-ads1018.c line 533
Quoted 10 lines
drivers/iio/adc/ti-ads1018.c new file +9
-0+ ret =spi_read (ads1018->spi, ads1018->rx_buf, +sizeof (ads1018->rx_buf)); + scan.conv = ads1018->rx_buf[0 ]; + } + +iio_device_release_buffer_mode (indio_dev); +if (!ret) +iio_push_to_buffers_with_ts (indio_dev, &scan,sizeof (scan), + pf->timestamp);
if (ret) goto out_notify_done;
// see a comment about traditional pattern somewhere above.
iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), pf->timestamp);
(and yes, leave the above on a single 82 characters long line).
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+out_notify_done: +iio_trigger_notify_done (ads1018->indio_trig); + +return IRQ_HANDLED ; +}
...
drivers/iio/adc/ti-ads1018.c line 544
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+static int ads1018_trigger_setup (struct iio_dev *indio_dev) +{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +struct spi_device *spi = ads1018->spi;
struct device *dev = &spi->dev;
Review
Quoted 1 line
(quoted diff) +1
-0+ int ret;
const char con_id = "drdy";
drivers/iio/adc/ti-ads1018.c line 548
Quoted 2 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+ ads1018->drdy_gpiod =devm_gpiod_get_optional (&spi->dev,"drdy" , +GPIOD_IN );
ads1018->drdy_gpiod = devm_gpiod_get_optional(dev, con_id, GPIOD_IN);
and so on...
// here we already make the diff 0 LoCs
drivers/iio/adc/ti-ads1018.c line 551
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+if (IS_ERR (ads1018->drdy_gpiod)) +return dev_err_probe (&spi->dev,PTR_ERR (ads1018->drdy_gpiod), +"Failed to get 'drdy' GPIO.\n" );
return dev_err_probe(dev, PTR_ERR(ads1018->drdy_gpiod), "Failed to get '%s' GPIO.\n", con_id);
drivers/iio/adc/ti-ads1018.c line 559
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +7
-0+if (spi->irq >0 ) { + ads1018->drdy_irq = spi->irq; + }else if (ads1018->drdy_gpiod) { + ads1018->drdy_irq =gpiod_to_irq (ads1018->drdy_gpiod); +if (ads1018->drdy_irq <0 ) +return dev_err_probe (&spi->dev, ads1018->drdy_irq, +"Failed to get IRQ from 'drdy' GPIO.\n" );
return dev_err_probe(dev, ads1018->drdy_irq, "Failed to get IRQ from '%s' GPIO.\n", con_id);
drivers/iio/adc/ti-ads1018.c line 440
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+ }else { +return 0 ; + }
Can be refactored to have less indentation, though
/* Try to retrieve an IRQ from SPI core then from GPIO */ if (spi->irq > 0) ads1018->drdy_irq = spi->irq; else if (ads1018->drdy_gpiod) ads1018->drdy_irq = gpiod_to_irq(ads1018->drdy_gpiod); if (ads1018->drdy_irq < 0) return dev_err_probe(dev, ads1018->drdy_irq, "Failed to get IRQ from '%s' GPIO.\n", con_id); /* No IRQ is fine, avoid supporting triggers */ if (ads1018->drdy_irq == 0) return 0;
drivers/iio/adc/ti-ads1018.c line 566
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+ ads1018->indio_trig =devm_iio_trigger_alloc (&spi->dev,"%s-dev%d-drdy" , + indio_dev->name, +iio_device_id (indio_dev));
// and here I believe we make it 1 LoC less.
Quoted 17 lines
drivers/iio/adc/ti-ads1018.c new file +17
-0+if (!ads1018->indio_trig) +return -ENOMEM ; + +iio_trigger_set_drvdata (ads1018->indio_trig, ads1018); + ads1018->indio_trig->ops = &ads1018_trigger_ops; + + ret =devm_iio_trigger_register (&spi->dev, ads1018->indio_trig); +if (ret) +return ret; + +/* +* The "data-ready" IRQ line is shared with the MOSI pin, thus we need +* to keep it disabled until we actually request data. +*/ +return devm_request_irq (&spi->dev, ads1018->drdy_irq, ads1018_irq_handler, +IRQF_NO_AUTOEN , ads1018->chip_info->name, ads1018); +}
...
Quoted 13 lines
drivers/iio/adc/ti-ads1018.c new file +13
-0+static int ads1018_spi_probe (struct spi_device *spi) +{ +const struct ads1018_chip_info *info =spi_get_device_match_data (spi); +struct iio_dev *indio_dev; +struct ads1018 *ads1018; +int ret; + + indio_dev =devm_iio_device_alloc (&spi->dev,sizeof (*ads1018)); +if (!indio_dev) +return -ENOMEM ; + + ads1018 =iio_priv (indio_dev); + ads1018->spi = spi;
drivers/iio/adc/ti-ads1018.c line 607
Quoted 10 lines
drivers/iio/adc/ti-ads1018.c new file +10
-0+ ads1018->chip_info = info; +spi_set_drvdata (spi, ads1018); + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->name = info->name; + indio_dev->info = &ads1018_iio_info; + indio_dev->channels = info->channels; + indio_dev->num_channels = info->num_channels; + +for (int i =0 ; i <ADS1018_CHANNELS_MAX ; i++) {
unsigned int
Quoted 21 lines
drivers/iio/adc/ti-ads1018.c new file +21
-0+ ads1018->chan_data[i].data_rate_mode =ADS1018_DRATE_DEFAULT ; + ads1018->chan_data[i].pga_mode =ADS1018_PGA_DEFAULT ; + } + + ads1018->xfer.rx_buf = ads1018->rx_buf; + ads1018->xfer.len =sizeof (ads1018->rx_buf); +spi_message_init_with_transfers (&ads1018->msg_read, &ads1018->xfer,1 ); + + ret =ads1018_trigger_setup (indio_dev); +if (ret) +return ret; + + ret =devm_iio_triggered_buffer_setup (&spi->dev, indio_dev, + iio_pollfunc_store_time, + ads1018_trigger_handler, + &ads1018_buffer_ops); +if (ret) +return ret; + +return devm_iio_device_register (&spi->dev, indio_dev); +}
...
drivers/iio/adc/ti-ads1018.c line 634
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+#define ADS1018_FSR_TO_SCALE (_fsr, _res) \ + { 0, ((_fsr) * MICRO) / BIT(_res) } + +static const int ads1018_gain_table[][2 ] = { +ADS1018_FSR_TO_SCALE (6144 ,11 ),
This won't (hmm... might not? see below for the details) work on 32-bit.
drivers/iio/adc/ti-ads1018.c line 643
Quoted 9 lines
drivers/iio/adc/ti-ads1018.c new file +9
-0+ADS1018_FSR_TO_SCALE (4096 ,11 ), +ADS1018_FSR_TO_SCALE (2048 ,11 ), +ADS1018_FSR_TO_SCALE (1024 ,11 ), +ADS1018_FSR_TO_SCALE (512 ,11 ), + ADS1018_FSR_TO_SCALE(256, 11), +}; + +static const int ads1118_gain_table[][2 ] = { + ADS1018_FSR_TO_SCALE(6144, 15),
Ditto.
drivers/iio/adc/ti-ads1018.c line 649
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+ADS1018_FSR_TO_SCALE (4096 ,15 ), +ADS1018_FSR_TO_SCALE (2048 ,15 ), +ADS1018_FSR_TO_SCALE (1024 ,15 ), +ADS1018_FSR_TO_SCALE (512 ,15 ), + ADS1018_FSR_TO_SCALE(256, 15), +};
To address that you need to divide MICRO at least by 2 (note, by 2⁶ is also possible without loosing in precision).
// Assuming that you want to keep the initialisers as is now:
#define ADS1018_FSR_TO_SCALE(_fsr, _res) \
{ 0, ((_fsr) * (MICRO >> 6)) / BIT((_res) - 6) }OTOH, these all are constants, so the clever compiler (even 32-bit compiler) may do it for you and won't complain as there is no overflow in the result.
TL;DR: Try to compile this with 32-bit compiler on 32-bit machine before changing as suggested.
...
drivers/iio/adc/ti-ads1018.c line 652
Quoted 2 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+static const unsigned int ads1018_data_rate_table[] = { +128 ,250 ,490 ,920 ,1600 ,2400 ,3300
Leave trailing comma.
drivers/iio/adc/ti-ads1018.c line 656
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+}; + +static const unsigned int ads1118_data_rate_table[] = { +8 ,16 ,32 ,64 ,128 ,250 ,475 ,860
Ditto.
Quoted 1 line
+};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] iio: adc: Add ti-ads1018 driver
Quoted 1 line · Andy Shevchenko
On Thu, Nov 27, 2025 at 10:37:11PM -0500, Kurt Borja wrote:
...
Quoted 11 lines
for (unsigned int i = 0; i < info->num_pga_mode_to_gain; i++) {
if (val == info->pga_mode_to_gain[i][0] ||
val2 == info->pga_mode_to_gain[i][1])
break;
}
if (i == info->num_pga_mode_to_gain)
return -EINVAL;
ads1018_set_pga_mode(ads1018, chan->scan_index, i);
return 0;Quoted 1 line
This also will follow the traditional pattern: Check for the error cases first.
Just noticed that in this case the scope of iterator should be out of the loop. Since you have it used more than once, move it to the top of the function.
Quoted 13 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ case IIO_CHAN_INFO_SAMP_FREQ: + for (int i = 0; i < info->num_data_rate_mode_to_hz; i++) {Ditto, same comments as per previous loop.
Quoted 8 lines
drivers/iio/adc/ti-ads1018.c new file +8
-0+if (val != info->data_rate_mode_to_hz[i]) +continue ; + +ads1018_set_data_rate_mode (ads1018, chan->scan_index, i); +return 0 ; + } + +return -EINVAL ;
Signature · 2 lines
With Best Regards, Andy Shevchenko
Kurt Borja
maintainerRe: [PATCH v2 2/2] iio: adc: Add ti-ads1018 driver
In reply to Andy ShevchenkoHi Andy,
Quoted 44 lines · Andy Shevchenko
Quoted 12 lines · Kurt Borja
Add ti-ads1018 driver for Texas Instruments ADS1018 and ADS1118 SPI analog-to-digital converters.
These chips' MOSI pin is shared with a data-ready interrupt. Defining this interrupt in devicetree is optional, therefore we only create an IIO trigger if one is found.
Handling this interrupt requires some considerations. When enabling the trigger the CS line is tied low (active), thus we need to hold
spi_bus_lock()too, to avoid state corruption. This is done inside theset_trigger_state()callback, to let users use other triggers without wasting a bus lock....
drivers/iio/adc/ti-ads1018.c line 12
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+#include <linux/array_size.h> +#include <linux/bitfield.h> +#include <linux/bitmap.h> +#include <linux/bitops.h> +#include <linux/bits.h>
bits.handbitops.hare guaranteed bybitmap.h, so#include <
linux/bitmap.h>is enough for all three.
drivers/iio/adc/ti-ads1018.c line 20
Quoted 8 lines
drivers/iio/adc/ti-ads1018.c new file +8
-0+#include <linux/byteorder/generic.h> +#include <linux/dev_printk.h> +#include <linux/gpio/consumer.h> +#include <linux/interrupt.h> +#include <linux/math.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/spi/spi.h> +
types.h// E.g., u8 just a page below :-)Quoted 1 line
+#include <
linux/units.h>...
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c line 201
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+static unsigned long ads1018_calc_delay (struct ads1018 *ads1018) +{ +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +unsigned long mode = chip_info->num_data_rate_mode_to_hz -1 ; +int hz = chip_info->data_rate_mode_to_hz[mode];Can frequency be negative?
I hope not 0.0
I return data_rate_mode_to_hz[] values directly in __ads1018_read_raw(), which uses int *. That's why I chose int, but I can just make it unsigned if that's preferred.
Quoted 235 lines
drivers/iio/adc/ti-ads1018.c line 206
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+/* We subtract -10% data-rate error */ + hz -=DIV_ROUND_UP (hz,10 ); + +/* Calculate time per sample in usecs */ Spell it fully "microseconds" or use proper unit with a Greek latter "µs".
Quoted 2 lines
(quoted diff) +2
-0+ return DIV_ROUND_UP(MICROHZ_PER_HZ, hz); +}...
drivers/iio/adc/ti-ads1018.c line 257
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+static int +__ads1018_read_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int *val,int *val2,long mask)Better naming is something like
ads1018_read_raw_unlocked()if I got the context right.drivers/iio/adc/ti-ads1018.c line 264
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +7
-0+{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +u8 drate_mode =ads1018_get_data_rate_mode (ads1018, chan->scan_index); +u8 pga_mode =ads1018_get_pga_mode (ads1018, chan->scan_index); +u8 max_drate_mode = chip_info->num_data_rate_mode_to_hz -1 ; +u16 cnv, cfg =0 ;Unneeded cfg assignment. See below.
drivers/iio/adc/ti-ads1018.c line 270
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + cfg |=ADS1018_CFG_VALID ; + cfg |=ADS1018_CFG_OS_TRIG ;cfg = ADS1018_CFG_VALID | ADS1018_CFG_OS_TRIG;drivers/iio/adc/ti-ads1018.c line 274
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+ cfg |=FIELD_PREP (ADS1018_CFG_MUX_MASK , chan->scan_index); + cfg |=FIELD_PREP (ADS1018_CFG_PGA_MASK , pga_mode); + cfg |=FIELD_PREP (ADS1018_CFG_MODE_MASK ,ADS1018_MODE_ONESHOT ); + cfg |=FIELD_PREP (ADS1018_CFG_DRATE_MASK , max_drate_mode);+ blank line.
Quoted 36 lines
drivers/iio/adc/ti-ads1018.c new file +36
-0+if (chan->type ==IIO_TEMP ) + cfg |=ADS1018_CFG_TS_MODE_EN ; + + ret =ads1018_oneshot (ads1018, cfg, &cnv); +if (ret) +return ret; + + cnv >>= chan->scan_type.shift; + *val =sign_extend32 (cnv, chan->scan_type.realbits -1 ); + +return IIO_VAL_INT ; + +case IIO_CHAN_INFO_SCALE : +switch (chan->type) { +case IIO_VOLTAGE : + *val = chip_info->pga_mode_to_gain[pga_mode][0 ]; + *val2 = chip_info->pga_mode_to_gain[pga_mode][1 ]; +return IIO_VAL_INT_PLUS_NANO ; + +case IIO_TEMP : + *val = chip_info->temp_scale[0 ]; + *val2 = chip_info->temp_scale[1 ]; +return IIO_VAL_INT_PLUS_MICRO ; + +default : +return -EOPNOTSUPP ; + } + +case IIO_CHAN_INFO_SAMP_FREQ : + *val = chip_info->data_rate_mode_to_hz[drate_mode]; +return IIO_VAL_INT ; + +default : +return -EOPNOTSUPP ; + } +}...
drivers/iio/adc/ti-ads1018.c line 352
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+static int +__ads1018_write_raw (struct iio_dev *indio_dev,struct iio_chan_spec const *chan, +int val,int val2,long mask)Naming...
drivers/iio/adc/ti-ads1018.c line 359
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +7
-0+{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +const struct ads1018_chip_info *info = ads1018->chip_info; + +switch (mask) { +case IIO_CHAN_INFO_SCALE : +for (int i =0 ; i < info->num_pga_mode_to_gain; i++) {Why is 'i' signed?
drivers/iio/adc/ti-ads1018.c line 365
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+if (val != info->pga_mode_to_gain[i][0 ] || + val2 != info->pga_mode_to_gain[i][1 ]) +continue ; +ads1018_set_pga_mode (ads1018, chan->scan_index, i); +return 0 ;This is an invariant to the loop, perhaps
drivers/iio/adc/ti-ads1018.c line 368
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+ } +return -EINVAL ;for (unsigned int i = 0; i < info->num_pga_mode_to_gain; i++) { if (val == info->pga_mode_to_gain[i][0] || val2 == info->pga_mode_to_gain[i][1]) break; } if (i == info->num_pga_mode_to_gain) return -EINVAL; ads1018_set_pga_mode(ads1018, chan->scan_index, i); return 0;This also will follow the traditional pattern: Check for the error cases first.
Review
Quoted 2 lines
(quoted diff) +2
-0+ case IIO_CHAN_INFO_SAMP_FREQ: + for (int i = 0; i < info->num_data_rate_mode_to_hz; i++) {Ditto, same comments as per previous loop.
Quoted 13 lines
drivers/iio/adc/ti-ads1018.c new file +13
-0+if (val != info->data_rate_mode_to_hz[i]) +continue ; + +ads1018_set_data_rate_mode (ads1018, chan->scan_index, i); +return 0 ; + } + +return -EINVAL ; + +default : +return -EOPNOTSUPP ; + } +}...
Quoted 11 lines
drivers/iio/adc/ti-ads1018.c new file +11
-0+static int ads1018_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct ads1018 *ads1018 =iio_trigger_get_drvdata (trig); +__be16 cnv; + +/* +* We need to lock the SPI bus and tie CS low (hold_cs) to catch +* data-ready interrupts, otherwise the MISO line enters a Hi-Z state. +*/ + +if (state) {drivers/iio/adc/ti-ads1018.c line 439
Quoted 10 lines
drivers/iio/adc/ti-ads1018.c new file +10
-0+spi_bus_lock (ads1018->spi->controller); +ads1018_read_locked (ads1018, &cnv, true); +enable_irq (ads1018->drdy_irq); + }else { +disable_irq (ads1018->drdy_irq); +ads1018_read_locked (ads1018, &cnv, false); +spi_bus_unlock (ads1018->spi->controller); + } + +return 0 ;This function is basically
if (foo)
do this
else
do thatplease make just two distinct functions instead.
Quoted 1 line
+}
That's how it will look like:
static void
ads1018_set_trigger_enable(struct ads1018 *ads1018){ __be16 cnv; spi_bus_lock(ads1018->spi->controller); ads1018_read_locked(ads1018, &cnv, true); enable_irq(ads1018->drdy_irq); }static void
ads1018_set_trigger_disable(struct ads1018 *ads1018){ __be16 cnv; disable_irq(ads1018->drdy_irq); ads1018_read_locked(ads1018, &cnv, false); spi_bus_unlock(ads1018->spi->controller); }static int
ads1018_set_trigger_state(struct iio_trigger *trig, bool state){ struct ads1018 *ads1018 = iio_trigger_get_drvdata(trig); /* * We need to lock the SPI bus and tie CS low (hold_cs) to catch * data-ready interrupts, otherwise the MISO line enters a Hi-Z state. */ if (state) ads1018_set_trigger_enable(ads1018); else ads1018_set_trigger_disable(ads1018); return 0; }Now is the question, can we get rid of dummy variables by checking for the pointer in the
_read_locked()? If so, the above become as simple asstatic void
ads1018_set_trigger_enable(struct ads1018 *ads1018){ spi_bus_lock(ads1018->spi->controller); ads1018_read_locked(ads1018, NULL, true); enable_irq(ads1018->drdy_irq); }static void
ads1018_set_trigger_disable(struct ads1018 *ads1018){ disable_irq(ads1018->drdy_irq); ads1018_read_locked(ads1018, NULL, false); spi_bus_unlock(ads1018->spi->controller); }
Quoted 3 lines
Or if you make
_read_locked()to return the data, just ignoring it as inads1018_read_locked(ads1018, ...);
Sure, I like this.
Quoted 251 lines
...
drivers/iio/adc/ti-ads1018.c line 452
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+static int ads1018_buffer_preenable (struct iio_dev *indio_dev) +{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +unsigned int pga, drate, addr; +u16 cfg =0 ;Unneeded assignment.
drivers/iio/adc/ti-ads1018.c line 459
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+ addr =find_first_bit (indio_dev->active_scan_mask, +iio_get_masklength (indio_dev)); + pga =ads1018_get_pga_mode (ads1018, addr); + drate =ads1018_get_data_rate_mode (ads1018, addr); + cfg |=ADS1018_CFG_VALID ;cfg = ADS1018_CFG_VALID;drivers/iio/adc/ti-ads1018.c line 463
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+ cfg |=FIELD_PREP (ADS1018_CFG_MUX_MASK , addr); + cfg |=FIELD_PREP (ADS1018_CFG_PGA_MASK , pga); + cfg |=FIELD_PREP (ADS1018_CFG_MODE_MASK ,ADS1018_MODE_CONTINUOUS ); + cfg |=FIELD_PREP (ADS1018_CFG_DRATE_MASK , drate);+ blank line.
Quoted 8 lines
drivers/iio/adc/ti-ads1018.c new file +8
-0+if (chip_info->channels[addr].type ==IIO_TEMP ) + cfg |=ADS1018_CFG_TS_MODE_EN ; + + ads1018->tx_buf[0 ] =cpu_to_be16 (cfg); + ads1018->tx_buf[1 ] =0 ; + +return spi_write (ads1018->spi, ads1018->tx_buf,sizeof (ads1018->tx_buf)); +}...
Quoted 19 lines
drivers/iio/adc/ti-ads1018.c new file +19
-0+static irqreturn_t ads1018_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct ads1018 *ads1018 =iio_priv (indio_dev); +struct { +__be16 conv; +aligned_s64 ts; + } scan = {}; +int ret; + +if (iio_device_claim_buffer_mode (indio_dev)) + goto out_notify_done; + +if (iio_trigger_using_own (indio_dev)) { +disable_irq (ads1018->drdy_irq); + ret =ads1018_read_locked (ads1018, &scan.conv, true); +enable_irq (ads1018->drdy_irq); + }else {drivers/iio/adc/ti-ads1018.c line 533
Quoted 10 lines
drivers/iio/adc/ti-ads1018.c new file +9
-0+ ret =spi_read (ads1018->spi, ads1018->rx_buf, +sizeof (ads1018->rx_buf)); + scan.conv = ads1018->rx_buf[0 ]; + } + +iio_device_release_buffer_mode (indio_dev); +if (!ret) +iio_push_to_buffers_with_ts (indio_dev, &scan,sizeof (scan), + pf->timestamp);if (ret) goto out_notify_done;// see a comment about traditional pattern somewhere above.
iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan), pf->timestamp);(and yes, leave the above on a single 82 characters long line).
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+out_notify_done: +iio_trigger_notify_done (ads1018->indio_trig); + +return IRQ_HANDLED ; +}...
drivers/iio/adc/ti-ads1018.c line 544
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+static int ads1018_trigger_setup (struct iio_dev *indio_dev) +{ +struct ads1018 *ads1018 =iio_priv (indio_dev); +struct spi_device *spi = ads1018->spi;struct device *dev = &spi->dev;Review
Quoted 1 line
(quoted diff) +1
-0+ int ret;const char con_id = "drdy";drivers/iio/adc/ti-ads1018.c line 548
Quoted 2 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+ ads1018->drdy_gpiod =devm_gpiod_get_optional (&spi->dev,"drdy" , +GPIOD_IN );ads1018->drdy_gpiod = devm_gpiod_get_optional(dev, con_id, GPIOD_IN);and so on...
// here we already make the diff 0 LoCs
drivers/iio/adc/ti-ads1018.c line 551
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+if (IS_ERR (ads1018->drdy_gpiod)) +return dev_err_probe (&spi->dev,PTR_ERR (ads1018->drdy_gpiod), +"Failed to get 'drdy' GPIO.\n" );return dev_err_probe(dev, PTR_ERR(ads1018->drdy_gpiod), "Failed to get '%s' GPIO.\n", con_id);drivers/iio/adc/ti-ads1018.c line 559
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c new file +7
-0+if (spi->irq >0 ) { + ads1018->drdy_irq = spi->irq; + }else if (ads1018->drdy_gpiod) { + ads1018->drdy_irq =gpiod_to_irq (ads1018->drdy_gpiod); +if (ads1018->drdy_irq <0 ) +return dev_err_probe (&spi->dev, ads1018->drdy_irq, +"Failed to get IRQ from 'drdy' GPIO.\n" );return dev_err_probe(dev, ads1018->drdy_irq, "Failed to get IRQ from '%s' GPIO.\n", con_id);drivers/iio/adc/ti-ads1018.c line 440
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+ }else { +return 0 ; + }Can be refactored to have less indentation, though
/* Try to retrieve an IRQ from SPI core then from GPIO */ if (spi->irq > 0) ads1018->drdy_irq = spi->irq; else if (ads1018->drdy_gpiod) ads1018->drdy_irq = gpiod_to_irq(ads1018->drdy_gpiod); if (ads1018->drdy_irq < 0) return dev_err_probe(dev, ads1018->drdy_irq, "Failed to get IRQ from '%s' GPIO.\n", con_id); /* No IRQ is fine, avoid supporting triggers */ if (ads1018->drdy_irq == 0) return 0;drivers/iio/adc/ti-ads1018.c line 566
Quoted 3 lines
drivers/iio/adc/ti-ads1018.c new file +3
-0+ ads1018->indio_trig =devm_iio_trigger_alloc (&spi->dev,"%s-dev%d-drdy" , + indio_dev->name, +iio_device_id (indio_dev));// and here I believe we make it 1 LoC less.
Quoted 17 lines
drivers/iio/adc/ti-ads1018.c new file +17
-0+if (!ads1018->indio_trig) +return -ENOMEM ; + +iio_trigger_set_drvdata (ads1018->indio_trig, ads1018); + ads1018->indio_trig->ops = &ads1018_trigger_ops; + + ret =devm_iio_trigger_register (&spi->dev, ads1018->indio_trig); +if (ret) +return ret; + +/* +* The "data-ready" IRQ line is shared with the MOSI pin, thus we need +* to keep it disabled until we actually request data. +*/ +return devm_request_irq (&spi->dev, ads1018->drdy_irq, ads1018_irq_handler, +IRQF_NO_AUTOEN , ads1018->chip_info->name, ads1018); +}...
Quoted 13 lines
drivers/iio/adc/ti-ads1018.c new file +13
-0+static int ads1018_spi_probe (struct spi_device *spi) +{ +const struct ads1018_chip_info *info =spi_get_device_match_data (spi); +struct iio_dev *indio_dev; +struct ads1018 *ads1018; +int ret; + + indio_dev =devm_iio_device_alloc (&spi->dev,sizeof (*ads1018)); +if (!indio_dev) +return -ENOMEM ; + + ads1018 =iio_priv (indio_dev); + ads1018->spi = spi;drivers/iio/adc/ti-ads1018.c line 607
Quoted 10 lines
drivers/iio/adc/ti-ads1018.c new file +10
-0+ ads1018->chip_info = info; +spi_set_drvdata (spi, ads1018); + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->name = info->name; + indio_dev->info = &ads1018_iio_info; + indio_dev->channels = info->channels; + indio_dev->num_channels = info->num_channels; + +for (int i =0 ; i <ADS1018_CHANNELS_MAX ; i++) {unsigned int
Quoted 21 lines
drivers/iio/adc/ti-ads1018.c new file +21
-0+ ads1018->chan_data[i].data_rate_mode =ADS1018_DRATE_DEFAULT ; + ads1018->chan_data[i].pga_mode =ADS1018_PGA_DEFAULT ; + } + + ads1018->xfer.rx_buf = ads1018->rx_buf; + ads1018->xfer.len =sizeof (ads1018->rx_buf); +spi_message_init_with_transfers (&ads1018->msg_read, &ads1018->xfer,1 ); + + ret =ads1018_trigger_setup (indio_dev); +if (ret) +return ret; + + ret =devm_iio_triggered_buffer_setup (&spi->dev, indio_dev, + iio_pollfunc_store_time, + ads1018_trigger_handler, + &ads1018_buffer_ops); +if (ret) +return ret; + +return devm_iio_device_register (&spi->dev, indio_dev); +}...
drivers/iio/adc/ti-ads1018.c line 634
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+#define ADS1018_FSR_TO_SCALE (_fsr, _res) \ + { 0, ((_fsr) * MICRO) / BIT(_res) } + +static const int ads1018_gain_table[][2 ] = { +ADS1018_FSR_TO_SCALE (6144 ,11 ),This won't (hmm... might not? see below for the details) work on 32-bit.
drivers/iio/adc/ti-ads1018.c line 643
Quoted 9 lines
drivers/iio/adc/ti-ads1018.c new file +9
-0+ADS1018_FSR_TO_SCALE (4096 ,11 ), +ADS1018_FSR_TO_SCALE (2048 ,11 ), +ADS1018_FSR_TO_SCALE (1024 ,11 ), +ADS1018_FSR_TO_SCALE (512 ,11 ), + ADS1018_FSR_TO_SCALE(256, 11), +}; + +static const int ads1118_gain_table[][2 ] = { + ADS1018_FSR_TO_SCALE(6144, 15),Ditto.
drivers/iio/adc/ti-ads1018.c line 649
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+ADS1018_FSR_TO_SCALE (4096 ,15 ), +ADS1018_FSR_TO_SCALE (2048 ,15 ), +ADS1018_FSR_TO_SCALE (1024 ,15 ), +ADS1018_FSR_TO_SCALE (512 ,15 ), + ADS1018_FSR_TO_SCALE(256, 15), +};To address that you need to divide MICRO at least by 2 (note, by 2⁶ is also possible without loosing in precision).
// Assuming that you want to keep the initialisers as is now:
Quoted 8 lines
#define ADS1018_FSR_TO_SCALE(_fsr, _res) \ { 0, ((_fsr) * (MICRO >> 6)) / BIT((_res) - 6) }OTOH, these all are constants, so the clever compiler (even 32-bit compiler) may do it for you and won't complain as there is no overflow in the result.
TL;DR: Try to compile this with 32-bit compiler on 32-bit machine before changing as suggested.
I didn't consider 32-bit here... I'll test it for v3!
Quoted 8 lines
...
drivers/iio/adc/ti-ads1018.c line 652
Quoted 2 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+static const unsigned int ads1018_data_rate_table[] = { +128 ,250 ,490 ,920 ,1600 ,2400 ,3300 Leave trailing comma.
Quoted 8 lines
drivers/iio/adc/ti-ads1018.c line 656
Quoted 4 lines
drivers/iio/adc/ti-ads1018.c new file +4
-0+}; + +static const unsigned int ads1118_data_rate_table[] = { +8 ,16 ,32 ,64 ,128 ,250 ,475 ,860 Ditto.
Quoted 1 line
+};
Ack, for everything else. Thank you, Andy!
Signature · 1 line
~ Kurt
Kurt Borja
maintainerRe: [PATCH v2 1/2] dt-bindings: iio: adc: Add TI ADS1018/ADS1118
In reply to Krzysztof KozlowskiQuoted 21 lines · Krzysztof Kozlowski
Quoted 7 lines · Kurt Borja
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +7
-0+ + interrupts: + description: DOUT/DRDY (Data Out/Data Ready) line. + maxItems: 1 + + drdy-gpios: + description:Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml line 52
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +10
-0+ Extra GPIO line connected to DOUT/DRDY (Data Out/Data Ready). This allows + distinguishing between interrupts triggered by the data-ready signal and + interrupts triggered by an SPI transfer. + maxItems: 1 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0These two properties are not correct now - you do not have any children.
Quoted 12 lines
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml line 62
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/ti,ads1018.yaml new file +10
-0+ + '#io-channel-cells': + const: 1 + +required: + - compatible + - reg + - vdd-supply + - '#address-cells' + - '#size-cells'And they should not be required.
I'll drop these. Thank you, Krzysztof!
Quoted 4 lines
Best regards,
Krzysztof
Signature · 1 line
~ Kurt
Re: [PATCH v2 2/2] iio: adc: Add ti-ads1018 driver
In reply to Kurt BorjaQuoted 2 lines · Kurt Borja
Quoted 1 line · Andy Shevchenko
On Thu, Nov 27, 2025 at 10:37:11PM -0500, Kurt Borja wrote:
Please, remove context you are agree with, no need to keep long message which is harder to parse for what has been asked, answered, or discussed.
That said, I assume non-commented remarks will be addressed in the next version as suggested.
...
Quoted 8 lines
Quoted 7 lines
drivers/iio/adc/ti-ads1018.c line 201
Quoted 5 lines
drivers/iio/adc/ti-ads1018.c new file +5
-0+static unsigned long ads1018_calc_delay (struct ads1018 *ads1018) +{ +const struct ads1018_chip_info *chip_info = ads1018->chip_info; +unsigned long mode = chip_info->num_data_rate_mode_to_hz -1 ; +int hz = chip_info->data_rate_mode_to_hz[mode];Can frequency be negative?
Quoted 5 lines
I hope not 0.0
I return data_rate_mode_to_hz[] values directly in
__ads1018_read_raw(), which usesint *. That's why I chose int, but I can just make it unsigned if that's preferred.
Keep it unsigned wherever it's possible.
...
Quoted 14 lines
Quoted 14 lines
static void
ads1018_set_trigger_enable(struct ads1018 *ads1018){ spi_bus_lock(ads1018->spi->controller); ads1018_read_locked(ads1018, NULL, true); enable_irq(ads1018->drdy_irq); }static void
ads1018_set_trigger_disable(struct ads1018 *ads1018){ disable_irq(ads1018->drdy_irq); ads1018_read_locked(ads1018, NULL, false); spi_bus_unlock(ads1018->spi->controller); }
Quoted 5 lines
Quoted 3 lines
Or if you make
_read_locked()to return the data, just ignoring it as inads1018_read_locked(ads1018, ...);Sure, I like this.
I noticed that it may return also an error, with that in mind the NULL pointer is the easiest way to go.
...
Quoted 31 lines
Quoted 31 lines
drivers/iio/adc/ti-ads1018.c line 634
Quoted 2 lines
drivers/iio/adc/ti-ads1018.c new file +2
-0+static const int ads1018_gain_table[][2 ] = { +ADS1018_FSR_TO_SCALE (6144 ,11 ),This won't (hmm... might not? see below for the details) work on 32-bit.
drivers/iio/adc/ti-ads1018.c line 643
Quoted 9 lines
drivers/iio/adc/ti-ads1018.c new file +9
-0+ADS1018_FSR_TO_SCALE (4096 ,11 ), +ADS1018_FSR_TO_SCALE (2048 ,11 ), +ADS1018_FSR_TO_SCALE (1024 ,11 ), +ADS1018_FSR_TO_SCALE (512 ,11 ), + ADS1018_FSR_TO_SCALE(256, 11), +}; + +static const int ads1118_gain_table[][2 ] = { + ADS1018_FSR_TO_SCALE(6144, 15),Ditto.
drivers/iio/adc/ti-ads1018.c line 649
Quoted 6 lines
drivers/iio/adc/ti-ads1018.c new file +6
-0+ADS1018_FSR_TO_SCALE (4096 ,15 ), +ADS1018_FSR_TO_SCALE (2048 ,15 ), +ADS1018_FSR_TO_SCALE (1024 ,15 ), +ADS1018_FSR_TO_SCALE (512 ,15 ), + ADS1018_FSR_TO_SCALE(256, 15), +};To address that you need to divide MICRO at least by 2 (note, by 2⁶ is also possible without loosing in precision).
// Assuming that you want to keep the initialisers as is now:
#define ADS1018_FSR_TO_SCALE(_fsr, _res) \ { 0, ((_fsr) * (MICRO >> 6)) / BIT((_res) - 6) }
Quoted 7 lines
Quoted 5 lines
OTOH, these all are constants, so the clever compiler (even 32-bit compiler) may do it for you and won't complain as there is no overflow in the result.
TL;DR: Try to compile this with 32-bit compiler on 32-bit machine before changing as suggested.
I didn't consider 32-bit here... I'll test it for v3!
Every time you have a division in the code with (potentially) bit numbers in the numerator and/or denominator always think about 32-bit case.
...
Quoted 1 line
Ack, for everything else. Thank you, Andy!
Okay, I assumed correctly in the top, but again, please remove the unneeded context when replying.
Signature · 2 lines
With Best Regards, Andy Shevchenko