iio: adc: ti-ads1298: Add driver
Quoted 1 line · Andy Shevchenko
On Fri, Feb 02, 2024 at 11:59:01AM +0100, Mike Looijmans wrote:
Hit "Send" by a mistake, here is the full review.
...
Why this can't be the part of drivers/iio/adc/ti-ads124s08.c? Seems to me the command list is the same, registers are different though. Broadly the Q is have you checked other existing drivers if they can be used as a base. If not, perhaps a word in the cover letter is good to have (sorry if I asked this already).
...
Quoted 10 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+#define ADS1298_WAKEUP 0x02 +#define ADS1298_STANDBY 0x04 +#define ADS1298_RESET 0x06 +#define ADS1298_START 0x08 +#define ADS1298_STOP 0x0A +#define ADS1298_RDATAC 0x10 +#define ADS1298_SDATAC 0x11 +#define ADS1298_RDATA 0x12Leftovers.
...
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+#define ADS1298_CLOCKS_TO_USECS(x) \ + (DIV_ROUND_UP((x * MICROHZ_PER_HZ), ADS1298_CLK_RATE_HZ))Wrong place of parentheses, should be "(x) * ...". Don't you need to include
math.h?
...
Quoted 1 line
Quoted 1 line
+#define
ADS1298_MASK_CONFIG1_DR0x07
GENMASK() ?
...
Quoted 10 lines
Quoted 9 lines
(quoted diff) +9
-0+struct ads1298_private { + const struct ads1298_chip_info *chip_info; + struct spi_device *spi; + struct regulator *reg_avdd; + struct regulator *reg_vref; + struct clk *clk; + struct regmap *regmap; + struct completion completion; + struct iio_trigger *trig;
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ struct spi_transfer rdata_xfer; + struct spi_message rdata_msg;
Do you use this outside of the ->probe()? I just ask since I removed some context already...
Quoted 11 lines
Review
Quoted 11 lines
(quoted diff) +11
-0+ spinlock_t irq_busy_lock; /* Handshake between SPI and DRDY irqs */ + int rdata_xfer_busy; + + /* Temporary storage for demuxing data after SPI transfer */ + u32 bounce_buffer[ADS1298_MAX_CHANNELS]; + + /* For synchronous SPI exchanges (read/write registers) */ + u8 cmd_buffer[ADS1298_SPI_CMD_BUFFER_SIZE] __aligned(IIO_DMA_MINALIGN); + + /* Buffer used for incoming SPI data */ + u8 rx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE];
Cacheline aligned?
I see the cmd_buffer, but shouldn't this be also aligned?
Quoted 3 lines
Quoted 3 lines
(quoted diff) +3
-0+ /* Contains the RDATA command and zeroes to clock out */ + u8 tx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE]; +};
...
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+static int ads1298_write_cmd(struct ads1298_private *priv, u8 command) +{ + struct spi_transfer cmd_xfer = {
I would use xfer[]...
Quoted 4 lines
Review
Quoted 4 lines
(quoted diff) +3
-0+ .tx_buf = priv->cmd_buffer, + .rx_buf = priv->cmd_buffer, + .len = sizeof(command),
In other cases you use plain number, perhaps 1 is okay here for consistency's sake?
Quoted 10 lines
Review
Quoted 10 lines
(quoted diff) +10
-0+ .speed_hz = ADS1298_SPI_BUS_SPEED_SLOW, + .delay = { + .value = 2, + .unit = SPI_DELAY_UNIT_USECS, + }, + }; + + priv->cmd_buffer[0] = command; + + return spi_sync_transfer(priv->spi, &cmd_xfer, 1);
...and ARRAY_SIZE(), but either way I'm fine, i.o.w. this is also okay.
Quoted 1 line
Quoted 1 line
+}
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ /* Enable the channel */
Too many spaces before */.
...
Quoted 6 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ /* Cannot take longer than 40ms (250Hz) */ + ret = wait_for_completion_timeout(&priv->completion, msecs_to_jiffies(50)); + if (!ret) + return -ETIMEDOUT; + + return 0;
Can be other way around
if (ret) return 0; return -ETIMEDOUT;
But the original is also okay.
Quoted 1 line
Quoted 1 line
+}
...
Quoted 5 lines
Review
Quoted 5 lines
(quoted diff) +4
-0+ if (priv->clk) + rate = clk_get_rate(priv->clk); + else + rate = ADS1298_CLK_RATE_HZ;
Dead code (here and elsewhere). You probably wanted _optional clk APIs in the probe.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+ if (!rate) + return -EINVAL;
...
Quoted 9 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+ factor = (rate >> ADS1298_SHIFT_DR_HR) / val; + if (factor >= 128) { + cfg = ADS1298_LOWEST_DR; + } else if (factor <= 1) { + cfg = ADS1298_MASK_CONFIG1_HR; /* Fastest possible */ + } else { + cfg = fls(factor) - 1; + cfg |= ADS1298_MASK_CONFIG1_HR; /* Use HR mode */ + }
What about:
factor = (rate >> ADS1298_SHIFT_DR_HR) / val; if (factor >= 128) cfg = ADS1298_LOWEST_DR; else if (factor) cfg = ADS1298_MASK_CONFIG1_HR | ilog2(factor); /* Use HR mode */ else cfg = ADS1298_MASK_CONFIG1_HR; /* Fastest possible */
(will need log2.h to be included)
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ *val = ret / 1000; /* Convert to millivolts */
MILLI ?
...
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ *val = sign_extend32(get_unaligned_be24(priv->rx_buffer + chan->address), + 23);
Second time magic 23, can you define a constant and use it everywhere?
...
Quoted 16 lines
Quoted 15 lines
(quoted diff) +15
-0+static int ads1298_reg_write(void *context, unsigned int reg, unsigned int val) +{ + struct ads1298_private *priv = context; + struct spi_transfer reg_write_xfer = { + .tx_buf = priv->cmd_buffer, + .rx_buf = priv->cmd_buffer, + .len = 3, + .speed_hz = ADS1298_SPI_BUS_SPEED_SLOW, + .delay = { + .value = 2, + .unit = SPI_DELAY_UNIT_USECS, + }, + }; + + priv->cmd_buffer[0] = ADS1298_CMD_WREG | reg;
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ priv->cmd_buffer[1] = 0x0; + priv->cmd_buffer[2] = val;
Sounds to me like put_unaligned_be16().
Quoted 3 lines
Quoted 3 lines
(quoted diff) +3
-0+ + return spi_sync_transfer(priv->spi, ®_write_xfer, 1); +}
...
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ priv->cmd_buffer[0] = ADS1298_CMD_RREG | reg; + priv->cmd_buffer[1] = 0x0; + priv->cmd_buffer[2] = 0;
Ditto.
Quoted 3 lines
Quoted 3 lines
(quoted diff) +3
-0+ ret = spi_sync_transfer(priv->spi, ®_read_xfer, 1); + if (ret) + return ret;
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ *val = priv->cmd_buffer[2];
Just wondering if the above is correct assumption, this probably needs to be get_unaligned_be16().
...
Quoted 6 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ unsigned long flags; + + /* Notify we're no longer waiting for the SPI transfer to complete */ + spin_lock_irqsave(&priv->irq_busy_lock, flags); + priv->rdata_xfer_busy = 0; + spin_unlock_irqrestore(&priv->irq_busy_lock, flags);
Use cleanup.h?
...
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4
-0+static int ads1298_update_scan_mode(struct iio_dev *indio_dev, + const unsigned long *scan_mask) +{ + struct ads1298_private *priv = iio_priv(indio_dev);
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+ unsigned int val; + int ret; + int i; + + /* Make the interrupt routines start with a clean slate */ + ads1298_rdata_unmark_busy(priv); + + /* Power down channels that aren't in use */
This comment does not describe why you need to write to _all_ channels.
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ for (i = 0; i < ADS1298_MAX_CHANNELS; i++) { + val = test_bit(i, scan_mask) ? 0 : ADS1298_MASK_CH_PD;
With above in mind, this perhaps needs to be one of for_each_set_bit(scan_mask) / for_each_clear_bit(scan_mask).
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+ ret = regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(i), + ADS1298_MASK_CH_PD, val); + if (ret) + return ret; + } + + return 0; +}
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ unsigned long flags;
cleanup.h
Quoted 22 lines
Quoted 22 lines
(quoted diff) +22
-0+ int wasbusy; + + spin_lock_irqsave(&priv->irq_busy_lock, flags); + + wasbusy = --priv->rdata_xfer_busy; + if (wasbusy) { + /* + * DRDY interrupt occurred before SPI completion. Start a new + * SPI transaction now to retrieve the data that wasn't latched + * into the ADS1298 chip's transfer buffer yet. + */ + spi_async(priv->spi, &priv->rdata_msg); + /* + * If more than one DRDY took place, there was an overrun. Since + * the sample is already lost, reset the counter to 1 so that + * we will wait for a DRDY interrupt after this SPI transaction. + */ + if (wasbusy > 1) + priv->rdata_xfer_busy = 1; + } + + spin_unlock_irqrestore(&priv->irq_busy_lock, flags);
...
Quoted 1 line
Quoted 1 line
+static
irqreturn_tads1298_interrupt(int irq, void *dev_id)
Ditto.
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ .cache_type = REGCACHE_RBTREE,
Why not MAPPLE TREE?
...
Quoted 9 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+static const char *ads1298_family_name(unsigned int id) +{ + switch (id & ADS1298_MASK_ID_FAMILY) { + case ADS1298_ID_FAMILY_ADS129X: + return "ADS129x"; + case ADS1298_ID_FAMILY_ADS129XR: + return "ADS129xR"; + default: + return "(unknown)";
Hmm... Maybe "" is enough? What is the practice in IIO for this? Jonathan?
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+ } +}
...
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ dev_info(dev, "Found %s, %u channels\n", ads1298_family_name(val), + 4 + 2 * (val & ADS1298_MASK_ID_CHANNELS));
How is this useful? Can't sysfs already give an answer to these Q:s?
...
Quoted 5 lines
Review
Quoted 5 lines
(quoted diff) +4
-0+ if (IS_ERR(priv->reg_vref)) { + if (PTR_ERR(priv->reg_vref) == -ENODEV) + priv->reg_vref = NULL; + else
Redundant if you check for an error cases first.
Quoted 12 lines
Quoted 12 lines
(quoted diff) +12
-0+ return dev_err_probe(dev, PTR_ERR(priv->reg_avdd), + "Failed to get vref regulator\n"); + } else { + ret = regulator_enable(priv->reg_vref); + if (ret) + return ret; + + ret = devm_add_action_or_reset(dev, ads1298_reg_disable, + priv->reg_vref); + if (ret) + return ret; + }
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ priv->clk = devm_clk_get_enabled(dev, "clk");
_optional? See above.
Quoted 4 lines
Review
Quoted 4 lines
(quoted diff) +3
-0+ if (IS_ERR(priv->clk)) + return dev_err_probe(dev, PTR_ERR(priv->clk), + "Failed to get clk\n");
One line? (Even in strict, i.e. 80 limit, mode checkpatch won't complain on it)
...
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ priv->regmap = devm_regmap_init(dev, NULL, priv, + &ads1298_regmap_config);
One line? (It's 81, which we may very well tolerate)
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+ if (IS_ERR(priv->regmap)) + return PTR_ERR(priv->regmap);
...
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ if (reset_gpio) { + udelay(ADS1298_CLOCKS_TO_USECS(2));
Why this delay (the after one is explained, though)?
Quoted 8 lines
Quoted 8 lines
(quoted diff) +8
-0+ gpiod_set_value(reset_gpio, 0); + } else { + ret = ads1298_write_cmd(priv, ADS1298_CMD_RESET); + if (ret) + return dev_err_probe(dev, ret, "RESET failed\n"); + } + /* Wait 18 clock cycles for reset command to complete */ + udelay(ADS1298_CLOCKS_TO_USECS(18));
Signature · 2 lines
With Best Regards, Andy Shevchenko
Assume "yes" to all suggestions not mentioned below.
I'll send out a v3 later.
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko
On Fri, Feb 02, 2024 at 11:59:01AM +0100, Mike Looijmans wrote:
Hit "Send" by a mistake, here is the full review.
...
Quoted 7 lines
Why this can't be the part of
drivers/iio/adc/ti-ads124s08.c? Seems to me the command list is the same, registers are different though. Broadly the Q is have you checked other existing drivers if they can be used as a base. If not, perhaps a word in the cover letter is good to have (sorry if I asked this already)....
The ads124s08 command list is the same, but that's about all that's similar.
Quoted 4 lines
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ struct spi_transfer rdata_xfer; + struct spi_message rdata_msg;Do you use this outside of the ->probe()? I just ask since I removed some context already...
Yes, probe() initializes the structs, they're used in the interrupt routines.
Quoted 14 lines
Quoted 12 lines
Review
Quoted 12 lines
(quoted diff) +11
-0+ spinlock_t irq_busy_lock; /* Handshake between SPI and DRDY irqs */ + int rdata_xfer_busy; + + /* Temporary storage for demuxing data after SPI transfer */ + u32 bounce_buffer[ADS1298_MAX_CHANNELS]; + + /* For synchronous SPI exchanges (read/write registers) */ + u8 cmd_buffer[ADS1298_SPI_CMD_BUFFER_SIZE] __aligned(IIO_DMA_MINALIGN); + + /* Buffer used for incoming SPI data */ + u8 rx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE];Cacheline aligned?
I see thecmd_buffer, but shouldn't this be also aligned?
I understood from Jonathan that that wasn't needed... "My" SPI controller is rather dumb and doesn't even have DMA.
Will take a closer look though.
Quoted 4 lines
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ else + rate = ADS1298_CLK_RATE_HZ;Dead code (here and elsewhere). You probably wanted
_optionalclk APIs in the probe.
Yes "optional" was intended.
Quoted 12 lines
...
Quoted 10 lines
Quoted 10 lines
(quoted diff) +10
-0+static int ads1298_reg_write(void *context, unsigned int reg, unsigned int val) +{ + struct ads1298_private *priv = context; + struct spi_transfer reg_write_xfer = { + .tx_buf = priv->cmd_buffer, + .rx_buf = priv->cmd_buffer, + .len = 3, + .speed_hz = ADS1298_SPI_BUS_SPEED_SLOW, + .delay = { + .value = 2,
Quoted 8 lines
Quoted 7 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ .unit = SPI_DELAY_UNIT_USECS, + }, + }; + + priv->cmd_buffer[0] = ADS1298_CMD_WREG | reg; + priv->cmd_buffer[1] = 0x0; + priv->cmd_buffer[2] = val;Sounds to me like
put_unaligned_be16().
Added comment to explain the first zero is the "number of registers to read/write minus one".
Quoted 11 lines
...
Quoted 6 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ unsigned long flags; + + /* Notify we're no longer waiting for the SPI transfer to complete */ + spin_lock_irqsave(&priv->irq_busy_lock, flags); + priv->rdata_xfer_busy = 0; + spin_unlock_irqrestore(&priv->irq_busy_lock, flags);Use
cleanup.h?...
Will dive into it, is new to me... Looks like C++ "scoped_xxx" at a first glance.
Quoted 14 lines
Quoted 13 lines
Review
Quoted 13 lines
(quoted diff) +12
-0+static int ads1298_update_scan_mode(struct iio_dev *indio_dev, + const unsigned long *scan_mask) +{ + struct ads1298_private *priv = iio_priv(indio_dev); + unsigned int val; + int ret; + int i; + + /* Make the interrupt routines start with a clean slate */ + ads1298_rdata_unmark_busy(priv); + + /* Power down channels that aren't in use */This comment does not describe why you need to write to _all_ channels.
Yeah, need to configure them all. Most common is that the cached value is already okay and no actual write register will happen.
Quoted 5 lines
Quoted 3 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ for (i = 0; i < ADS1298_MAX_CHANNELS; i++) { + val = test_bit(i, scan_mask) ? 0 : ADS1298_MASK_CH_PD;With above in mind, this perhaps needs to be one of
for_each_set_bit(scan_mask)/for_each_clear_bit(scan_mask).
Probably more efficient to access the device registers in a natural order.
Quoted 4 lines
...
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ .cache_type = REGCACHE_RBTREE,Why not MAPPLE TREE?
Reading the description this driver isn't a good candidate - the map isn't sparse and the hardware can do bulk read/write (though the driver doesn't use it).
...
Signature · 10 lines
Mike Looijmans System Expert TOPIC Embedded Products B.V. Materiaalweg 4, 5681 RJ Best The Netherlands T: +31 (0) 499 33 69 69 E: [email protected] W: www.topic.nl
Quoted 9 lines · Mike Looijmans
Quoted 3 lines · Andy Shevchenko
Quoted 1 line
Review
Quoted 1 line
(quoted diff) +1
-0+ .cache_type = REGCACHE_RBTREE,Why not MAPPLE TREE?
Reading the description this driver isn't a good candidate - the map isn't sparse and the hardware can do bulk read/write (though the driver doesn't use it).
If your driver is a good candidate for rbtree it's a good candidate for maple tree. There are very few specialist cases where there's an advantage to sticking with rbtree. The maple cache has no meaningful overhead compared to rbtree for non-sparse regmaps and will generate bulk writes just fine.
Jonathan Cameron
maintainerQuoted 17 lines
Quoted 16 lines
Quoted 14 lines
Quoted 4 lines
(quoted diff) +1
-0+ spinlock_t irq_busy_lock; /* Handshake between SPI and DRDY irqs */Review
Quoted 10 lines
(quoted diff) +10
-0+ int rdata_xfer_busy; + + /* Temporary storage for demuxing data after SPI transfer */ + u32 bounce_buffer[ADS1298_MAX_CHANNELS]; + + /* For synchronous SPI exchanges (read/write registers) */ + u8 cmd_buffer[ADS1298_SPI_CMD_BUFFER_SIZE] __aligned(IIO_DMA_MINALIGN); + + /* Buffer used for incoming SPI data */ + u8 rx_buffer[ADS1298_SPI_RDATA_BUFFER_SIZE];Cacheline aligned?
I see thecmd_buffer, but shouldn't this be also aligned?
Quoted 4 lines
I understood from Jonathan that that wasn't needed... "My" SPI controller is rather dumb and doesn't even have DMA.
Will take a closer look though.
It should be fine. The aim here is to ensure that nothing access data in the same cacheline from the CPU side whilst DMA is ongoing (and unwanted write backs of stale data can occur). As long at the handling isn't very complex, a single marking of the first buffer used for DMA (and being sure there is nothing other than other DMA buffers after it the same cacheline (potentially multiple lines) is enough.
So the __aligend(IIO_DMA_MINALIGN) on cmd_buffer should work for allt hese buffers.