On Wed, Dec 13, 2023 at 10:47:22AM +0100, Mike Looijmans wrote:
Skeleton driver for the TI ADS1298 medical ADC. This device is...
typically used for ECG and similar measurements. Supports data
acquisition at configurable scale and sampling frequency.
+config TI_ADS1298Huh?!
+ tristate "Texas Instruments ADS1298"
+ depends on SPI
+ select IIO_BUFFER
+ default y
+ help...
+ If you say yes here you get support for Texas Instruments ADS1298
+ medical ADC chips
+
+ This driver can also be built as a module. If so, the module will be
+ called ti-ads1298.
+#include <linux/bitfield.h>Is it used as a proxy? (At least for array_size.h)
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
Please use real headers in such case.
+#include <linux/module.h>This is interesting grouping, but okay, I understand the point.
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/gpio/consumer.h>
+#include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
+#include <linux/iio/iio.h>...
+#include <linux/iio/buffer.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/sysfs.h>
+#define ADS1298_CLK_RATE 2048000Units? _HZ ?
...
+/* Outputs status word and 8 samples of 24 bits each, plus the command byte *//* Outputs status word and 8 24-bit samples, plus the command byte */
a bit shorter.
+#define ADS1298_SPI_RDATA_BUFFER_SIZE ((ADS1298_MAX_CHANNELS + 1) * 3 + 1)...
+#define ADS1298_CHAN(index) \Hmm... does this 3 have a distinct definition?
+{ \
+ .type = IIO_VOLTAGE, \
+ .indexed = 1, \
+ .channel = index, \
+ .address = 3 * index + 4, \
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \Can be written as below
+ BIT(IIO_CHAN_INFO_SCALE), \
.info_mask_separate = \
BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_SCALE), \
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \.info_mask_shared_by_all = \
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
+ .scan_index = index, \...
+ .scan_type = { \
+ .sign = 's', \
+ .realbits = 24, \
+ .storagebits = 32, \
+ .endianness = IIO_BE, \
+ }, \
+}
+static int ads1298_write_cmd(struct ads1298_private *priv, u8 command)sizeof(command) ?
+{
+ struct spi_transfer cmd_xfer = {
+ .tx_buf = priv->cmd_buffer,
+ .rx_buf = priv->cmd_buffer,
+ .len = 1,
+ .speed_hz = ADS1298_SPI_BUS_SPEED_SLOW,.delay = {
+ .delay.value = 2,
+ .delay.unit = SPI_DELAY_UNIT_USECS,
.value = ...
.unit = ...
},
+ };...
+ priv->cmd_buffer[0] = command;
+
+ return spi_sync_transfer(priv->spi, &cmd_xfer, 1);
+}
+ /* Cannot take longer than 40ms (250Hz) */One line?
+ ret = wait_for_completion_timeout(&priv->completion,
+ msecs_to_jiffies(50));
+ if (!ret)...
+ return -ETIMEDOUT;
+ if (cfg & ADS1298_MASK_CONFIG1_HR)Are those magic numbers defined?
+ rate >>= 6; /* HR mode */
+ else
+ rate >>= 7; /* LP mode */
...
+ factor = (rate >> 6) / val;Is it the same 6 semantically as above?
...
+ return IIO_VAL_FRACTIONAL_LOG2;One blank line is enough.
+}
+
+
+ *val = sign_extend32(get_unaligned_be24(Strange indentation.
+ priv->rx_buffer + chan->address), 23);
*val = sign_extend32(get_unaligned_be24(priv->rx_buffer + chan->address),
23);
...
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:Why not using standard pattern?
+ ret = regmap_read(priv->regmap, ADS1298_REG_CONFIG1, val);
+ if (!ret) {
if (ret)
return ret;
(see below)
+ ret = IIO_VAL_INT;Outer parentheses are redundant.
+ *val = (16 << (*val & ADS1298_MASK_CONFIG1_DR));
+ }return IIO_VAL_INT;
+ break;
+ default:return directly.
+ ret = -EINVAL;
+ break;
+ }It will gone.
+ return ret;
...
+static int ads1298_write_raw(struct iio_dev *indio_dev,No need, just return directly.
+ struct iio_chan_spec const *chan, int val,
+ int val2, long mask)
+{
+ struct ads1298_private *priv = iio_priv(indio_dev);
+ int ret;
+ switch (mask) {...
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ ret = ads1298_set_samp_freq(priv, val);
+ break;
+ default:
+ ret = -EINVAL;
+ break;
+ }
+
+ return ret;
+}
+static int ads1298_reg_access(struct iio_dev *indio_dev, unsigned int reg,Perhaps positive conditional?
+ unsigned int writeval, unsigned int *readval)
+{
+ struct ads1298_private *priv = iio_priv(indio_dev);
+ if (!readval)
if (readval)
return readval;
return writeval;
+ return regmap_write(priv->regmap, reg, writeval);...
+
+ return regmap_read(priv->regmap, reg, readval);
+}
+ /* Power down channels that aren't in use */Broken indentation.
+ for (i = 0; i < ADS1298_MAX_CHANNELS; i++) {
+ regmap_update_bits(priv->regmap, ADS1298_REG_CHnSET(i),
+ ADS1298_MASK_CH_PD,
+ test_bit(i, scan_mask) ?
+ 0 : ADS1298_MASK_CH_PD);
+ }...
+static void ads1298_rdata_unmark_busy(struct ads1298_private *priv)Perhaps switch to use guard()?
+{
+ 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 = false;
+ spin_unlock_irqrestore(&priv->irq_busy_lock, flags);
(And scoped_guard() where it makes sense.)
+}Hmm... Wouldn't get_unaligned_..24() work here better?
+ /* Transfer 24-bit value into 32-bit array */
+ memcpy(bounce + 1, data, 3);
+ bounce += 4;If so, you can iterate over u32 members directly without this += 4.
...
+static const char *ads1298_family_name(unsigned int id)GENMASK() ?
+{
+ switch (id & 0xe0) {
+ case 0x80:Can we have these all magics be defined?
+ return "ADS129x";
+ case 0xc0:
+ return "ADS129xR";
+ default:...
+ return "(unknown)";
+ }
+}
+ if ((val & 0x18) == 0x10) {Ditto.
+ dev_info(dev, "Found %s, %d channels\n",Ditto for 0x07.
+ ads1298_family_name(val),
+ 4 + 2 * (val & 0x07));
+ } else {...
+ dev_err(dev, "Unknown ID: 0x%x\n", val);
+ return -ENODEV;
+ }
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));We do !indio_dev in this case.
+ if (indio_dev == NULL)
+ return -ENOMEM;...
+ ret = devm_add_action_or_reset(dev, ads1298_reg_disable,One line?
+ priv->reg_vref);
+ if (ret)...
+ return ret;
+ return dev_err_probe(dev, PTR_ERR(priv->clk),Ditto.
+ "Failed to get clk\n");
...
+ return dev_err_probe(dev, ret,Ditto.
+ "Failed to enable avdd regulator\n");
...
+ ret = devm_add_action_or_reset(dev, ads1298_reg_disable,Ditto.
+ priv->reg_avdd);
...
+ priv->regmap = devm_regmap_init(dev, NULL, priv,Ditto.
+ &ads1298_regmap_config);
...
+ /* Avoid giving all the same name, iio scope doesn't handle that well */IIO
+ indio_dev->name = devm_kasprintf(dev, GFP_KERNEL, "%s@%s",No error check?
+ spi_get_device_id(spi)->name,
+ dev_name(dev));
+ if (reset_gpio) {How do you know it's 2MHz? Is it always the same on all platforms / setups?
+ udelay(1); /* Minimum pulse width is 2 clocks at 2MHz */
+ gpiod_set_value(reset_gpio, 0);Ditto.
+ } 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(9);...
+ ret = devm_request_irq(&spi->dev, spi->irq, &ads1298_interrupt,&spi->dev is different to dev?
+ IRQF_TRIGGER_FALLING, indio_dev->name,...
+ indio_dev);
+ ret = devm_iio_kfifo_buffer_setup(&spi->dev, indio_dev,Ditto.
+ &ads1298_setup_ops);
...
+static const struct spi_device_id ads1298_id[] = {Inner comma is not needed.
+ { "ads1298", },
+ { }...
+};
+static const struct of_device_id ads1298_of_table[] = {Drop the comma in terminator entry.
+ { .compatible = "ti,ads1298" },
+ { },
+};