Add support for ADS131E0x ADC family
Subsystems (2)
v3 cover Add support for ADS131E0x ADC family
From: Tomislav Denis <[email protected]>
This patchset adds support for Texas Instruments ADS131E0x analog-to-digital converters family.
Datasheet: https://www.ti.com/lit/ds/symlink/ads131e08.pdf
Changes in v3:
- update driver description in Kconfig
- reorder includes in alphabetical order
- replace all occurrences of sizeof(type) with sizeof(variable)
- validate gain and channel mux values when parsing DT
- use of_device_get_match_data insted spi_get_device_id mapping
- proper handling of devm_regulator_get_optional error
- fetch vref regulator value each time when reading the scale value
- remove spi_device_id table
- fix wrong maximum value in driver bindings
Tomislav Denis (2):
iio: adc: Add driver for Texas Instruments ADS131E0x ADC family
bindings: iio: adc: Add documentation for ADS131E0x ADC driver
.../devicetree/bindings/iio/adc/ti,ads131e08.yaml | 181 ++++ MAINTAINERS | 7 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ti-ads131e08.c | 960 +++++++++++++++++++++ 5 files changed, 1161 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/ti,ads131e08.yaml create mode 100644 drivers/iio/adc/ti-ads131e08.c
Signature · 1 line
2.7.4
PATCH v3 1/2 iio: adc: Add driver for Texas Instruments ADS131E0x ADC family
From: Tomislav Denis <[email protected]>
The ADS131E0x are a family of multichannel, simultaneous sampling, 24-bit, delta-sigma, analog-to-digital converters (ADCs) with a built-in programmable gain amplifier (PGA), internal reference and an onboard oscillator.
--- MAINTAINERS | 6 + drivers/iio/adc/Kconfig | 12 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ti-ads131e08.c | 960 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 979 insertions(+) create mode 100644 drivers/iio/adc/ti-ads131e08.c
MAINTAINERS +6 -0
@@ -17367,6 +17367,12 @@ M: Robert Richter <[email protected]> S: Odd Fixes F: drivers/gpio/gpio-thunderx.c +TI ADS131E0X ADC SERIES DRIVER +M: Tomislav Denis <[email protected]> +L: [email protected] +S: Maintained +F: drivers/iio/adc/ti-ads131e08.c + TI AM437X VPFE DRIVER M: "Lad, Prabhakar" <[email protected]> L: [email protected]
drivers/iio/adc/Kconfig +12 -0
@@ -1151,6 +1151,18 @@ config TI_ADS124S08 This driver can also be built as a module. If so, the module will be called ti-ads124s08. +config TI_ADS131E08 + tristate "Texas Instruments ADS131E08" + depends on SPI && OF + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Say yes here to get support for Texas Instruments ADS131E04, ADS131E06 + and ADS131E08 chips. + + This driver can also be built as a module. If so, the module will be + called ti-ads131e08. + config TI_AM335X_ADC tristate "TI's AM335X ADC driver" depends on MFD_TI_AM335X_TSCADC && HAS_DMA
drivers/iio/adc/Makefile +1 -0
@@ -103,6 +103,7 @@ obj-$(CONFIG_TI_ADS7950) += ti-ads7950.o obj-$(CONFIG_TI_ADS8344) += ti-ads8344.o obj-$(CONFIG_TI_ADS8688) += ti-ads8688.o obj-$(CONFIG_TI_ADS124S08) += ti-ads124s08.o +obj-$(CONFIG_TI_ADS131E08) += ti-ads131e08.o obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o obj-$(CONFIG_TI_TLC4541) += ti-tlc4541.o obj-$(CONFIG_TWL4030_MADC) += twl4030-madc.o
drivers/iio/adc/ti-ads131e08.c new file +960 -0
960 lines not on this page · raw message
Signature · 1 line
2.7.4
Jonathan Cameron
maintainerRe: [PATCH v3 1/2] iio: adc: Add driver for Texas Instruments ADS131E0x ADC family
Quoted 8 lines · On Tue, 12 Jan 2021 10:51:27 +0100 <[email protected]> wrote:
From: Tomislav Denis <[email protected]>
The ADS131E0x are a family of multichannel, simultaneous sampling, 24-bit, delta-sigma, analog-to-digital converters (ADCs) with a built-in programmable gain amplifier (PGA), internal reference and an onboard oscillator.
Signed-off-by Tomislav Denis <[email protected]>
Hi Tomislav,
I previous failed to look in detail at how this driver will interact with triggers that are not it's own data ready signal. There are a few corners around that which need fixing up. You could just not allow that case, but it can be very handy to use an hrtimer trigger to do slow polling for example and it's not that hard to enable.
Although I haven't done the necessary audit I suspect we have a few drivers that don't get this right that got through review :(
A few other minor things below which I'd probably just have tidied up whilst applying if not for the above.
...
Quoted 6 lines
drivers/iio/adc/ti-ads131e08.c new file +0
-0@@ -0,0 +1,960 @@
...
Quoted 62 lines
drivers/iio/adc/ti-ads131e08.c new file +62
-0+ +static int ads131e08_initial_config (struct iio_dev *indio_dev) +{ +struct ads131e08_state *st =iio_priv (indio_dev); +const struct iio_chan_spec *channel = indio_dev->channels; +int ret, i; +u8 active_channels =0 ; + + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_RESET ); +if (ret) +return ret; + +udelay (st->reset_delay_us); + +/* Disable read data in continuous mode (enabled by default) */ + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_SDATAC ); +if (ret) +return ret; + + ret =ads131e08_set_data_rate (st,ADS131E08_DEFAULT_DATA_RATE ); +if (ret) +return ret; + + ret =ads131e08_config_reference_voltage (st); +if (ret) +return ret; + +for (i =0 ; i < indio_dev->num_channels; ++i) { + ret =ads131e08_set_pga_gain (st, channel->channel, + st->channel_config[i].pga_gain); +if (ret) +return ret; + + ret =ads131e08_set_channel_mux (st, channel->channel, + st->channel_config[i].mux); +if (ret) +return ret; + + active_channels |=BIT (channel->channel); + channel++; + } + +/* Power down unused channels */ +for (i =0 ; i < st->info->max_channels; ++i) { +if (!(active_channels &BIT (i))) { + ret =ads131e08_power_down_channel (st, i, true); +if (ret) +return ret; + } + } + +/* Request channel offset calibration */ + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_OFFSETCAL ); +if (ret) +return ret; + +/* +* Channel offset calibration is triggered with first START command. +* Since calibration take more time than settling operation, +* this causes timeout error when command START is sent first +* time (e.g. first call of the ads131e08_read_direct method). +* To avoid this problem offset calibration is triggered here.
drivers/iio/adc/ti-ads131e08.c line 460
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +10
-0+*/ + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_START ); +if (ret) +return ret; + +msleep (ADS131E08_WAIT_OFFSETCAL_MS ); + + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_STOP ); +if (ret) +return ret;
trivial but nicer to just do
return ads131e...
Quoted 40 lines
drivers/iio/adc/ti-ads131e08.c new file +40
-0+ +return 0 ; +} + +static int ads131e08_read_direct (struct iio_dev *indio_dev, +struct iio_chan_spec const *channel,int *value) +{ +struct ads131e08_state *st =iio_priv (indio_dev); +int ret; +u8 num_bits, *src; + +reinit_completion (&st->completion); + + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_START ); +if (ret) +return ret; + + ret =wait_for_completion_timeout (&st->completion, +msecs_to_jiffies (ADS131E08_MAX_SETTLING_TIME_MS )); +if (!ret) +return -ETIMEDOUT ; + + ret =ads131e08_read_data (st, st->readback_len); +if (ret) +return ret; + + ret =ads131e08_exec_cmd (st,ADS131E08_CMD_STOP ); +if (ret) +return ret; + + src = st->rx_buf +ADS131E08_NUM_STATUS_BYTES + + channel->channel *ADS131E08_NUM_DATA_BYTES (st->data_rate); + + num_bits =ADS131E08_NUM_DATA_BITS (st->data_rate); + *value =sign_extend32 ( +get_unaligned_be32 (src) >> (32 - num_bits), num_bits -1 ); + +return ret; +} +
...
drivers/iio/adc/ti-ads131e08.c line 609
Quoted 12 lines
drivers/iio/adc/ti-ads131e08.c new file +12
-0+ +static int ads131e08_set_trigger_state (struct iio_trigger *trig,bool state) +{ +struct iio_dev *indio_dev =iio_trigger_get_drvdata (trig); +struct ads131e08_state *st =iio_priv (indio_dev); +u8 cmd = state ?ADS131E08_CMD_START :ADS131E08_CMD_STOP ; + +return ads131e08_exec_cmd (st, cmd); +} + +static const struct iio_trigger_ops ads131e08_trigger_ops = { + .set_trigger_state = &ads131e08_set_trigger_state,
.validate_device = &iio_trigger_validate_own_device,
(see below for why!)
Quoted 60 lines
drivers/iio/adc/ti-ads131e08.c new file +60
-0+}; + +static irqreturn_t ads131e08_trigger_handler (int irq,void *private) +{ +struct iio_poll_func *pf = private; +struct iio_dev *indio_dev = pf->indio_dev; +struct ads131e08_state *st =iio_priv (indio_dev); +int ret; +unsigned int chn, i =0 ; +u8 *src, *dest; +/* +* The number of data bits per channel depends on the data rate. +* For 32 and 64 ksps data rates, number of data bits per channel +* is 16. This case is not compliant with used (fixed) scan element +* type (be:s24/32>>8). So we use a litle tweek to pack properly +* 16 bits of data into the buffer. +*/ +unsigned int num_bytes =ADS131E08_NUM_DATA_BYTES (st->data_rate); +u8 tweek_offset = num_bytes ==2 ?1 :0 ; + + ret =ads131e08_read_data (st, st->readback_len); +if (ret) + goto out; + +for_each_set_bit (chn, indio_dev->active_scan_mask, + indio_dev->masklength) { + src = st->rx_buf +ADS131E08_NUM_STATUS_BYTES + chn * num_bytes; + dest = st->tmp_buf.data + i *ADS131E08_NUM_STORAGE_BYTES ; + +/* +* Tweek offset is 0: +* +---+---+---+---+ +* |D0 |D1 |D2 | X | (3 data bytes) +* +---+---+---+---+ +* a+0 a+1 a+2 a+3 +* +* Tweek offset is 1: +* +---+---+---+---+ +* |P0 |D0 |D1 | X | (one padding byte and 2 data bytes) +* +---+---+---+---+ +* a+0 a+1 a+2 a+3 +*/ +memcpy (dest + tweek_offset, src, num_bytes); + +/* +* Data conversion from 16 bits of data to 24 bits of data +* is done by sign etension (properly filling padding byte). +*/ +if (tweek_offset) +memset (dest, *src &BIT (7 ) ?0xff :0x00 ,1 ); + + i++; + } + +iio_push_to_buffers_with_timestamp (indio_dev, st->tmp_buf.data, +iio_get_time_ns (indio_dev)); + +out: +iio_trigger_notify_done (indio_dev->trig); +
drivers/iio/adc/ti-ads131e08.c line 679
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +10
-0+return IRQ_HANDLED ; +} + +static irqreturn_t ads131e08_interrupt (int irq,void *private) +{ +struct iio_dev *indio_dev = private; +struct ads131e08_state *st =iio_priv (indio_dev); + +if (iio_buffer_enabled (indio_dev)) +iio_trigger_poll (st->trig);
There is a corner case here we may want to close
I don't think you are currently ensuring that another device isn't using this trigger. In theory that could happen whilst this device doesn't have it's buffer enabled. This corner case is common with devices that use the interrupt for polled reads.
If you add a validate_device() callback to the trigger it will stop that happening.
That will still let this device be used with other triggers (e.g. sysfs or hrtimer) However, I think there is a problem there as well.
Currently you do the start for the device in the set_trigger_state() cb and that won't be called if we are using another device.
So to enable the use of other triggers, you would need to check whether the trigger in use is this one provided by this driver and if it isn't you will need to so the extra steps to initialize a capture on demand (same as read_raw more or less)
Look at iio_trigger_using_own() and the users of that function to see how they handle this.
Quoted 6 lines
drivers/iio/adc/ti-ads131e08.c new file +6
-0+else +complete (&st->completion); + +return IRQ_HANDLED ; +} +
...
Quoted 34 lines
drivers/iio/adc/ti-ads131e08.c new file +34
-0+ +static int ads131e08_probe (struct spi_device *spi) +{ +const struct ads131e08_info *info; +struct iio_dev *indio_dev; +struct ads131e08_state *st; +int ret; +unsigned long adc_clk_hz, adc_clk_ns; + + info =of_device_get_match_data (&spi->dev); +if (!info) { +dev_err (&spi->dev,"failed to get match data\n" ); +return -ENODEV ; + } + + indio_dev =devm_iio_device_alloc (&spi->dev,sizeof (*st)); +if (!indio_dev) { +dev_err (&spi->dev,"failed to allocate IIO device\n" ); +return -ENOMEM ; + } + + st =iio_priv (indio_dev); + st->info = info; + st->spi = spi; + + ret =ads131e08_parse_dt (indio_dev, spi->dev.of_node); +if (ret) +return ret; + + indio_dev->name = st->info->name; + indio_dev->dev.parent = &spi->dev; + indio_dev->dev.of_node = spi->dev.of_node; + indio_dev->info = &ads131e08_iio_info; + indio_dev->modes =INDIO_DIRECT_MODE ;
drivers/iio/adc/ti-ads131e08.c line 840
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +10
-0+ +init_completion (&st->completion); + +if (spi->irq) { + ret =devm_request_irq (&spi->dev, spi->irq, + ads131e08_interrupt, +IRQF_TRIGGER_FALLING |IRQF_ONESHOT , + spi->dev.driver->name, indio_dev); +if (ret) { +dev_err (&spi->dev,"failed to allocate IRQ\n" );
Very small tweak here. dev_err_probe() so that we don't print a message if the return value indicates that we are just deferring whilst waiting for the irq chip driver to load.
Quoted 45 lines
drivers/iio/adc/ti-ads131e08.c new file +45
-0+return ret; + } + }else { +dev_err (&spi->dev,"data ready IRQ missing\n" ); +return -ENODEV ; + } + + st->trig =devm_iio_trigger_alloc (&spi->dev,"%s-dev%d" , + indio_dev->name, indio_dev->id); +if (!st->trig) { +dev_err (&spi->dev,"failed to allocate IIO trigger\n" ); +return -ENOMEM ; + } + + st->trig->ops = &ads131e08_trigger_ops; + st->trig->dev.parent = &spi->dev; +iio_trigger_set_drvdata (st->trig, indio_dev); + ret =devm_iio_trigger_register (&spi->dev, st->trig); +if (ret) { +dev_err (&spi->dev,"failed to register IIO trigger\n" ); +return -ENOMEM ; + } + + indio_dev->trig =iio_trigger_get (st->trig); + + ret =devm_iio_triggered_buffer_setup (&spi->dev, indio_dev, +NULL , &ads131e08_trigger_handler,NULL ); +if (ret) { +dev_err (&spi->dev,"failed to setup IIO buffer\n" ); +return ret; + } + + st->vref_reg =devm_regulator_get_optional (&spi->dev,"vref" ); +if (!IS_ERR (st->vref_reg)) { + ret =regulator_enable (st->vref_reg); +if (ret) { +dev_err (&spi->dev, +"failed to enable external vref supply\n" ); +return ret; + } + + ret =devm_add_action_or_reset (&spi->dev, + ads131e08_regulator_disable, st); +if (ret) { +regulator_disable (st->vref_reg);
drivers/iio/adc/ti-ads131e08.c line 895
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +10
-0+return ret; + } + }else { +if (PTR_ERR (st->vref_reg) != -ENODEV ) +return PTR_ERR (st->vref_reg); + } + + st->adc_clk =devm_clk_get (&spi->dev,"adc-clk" ); +if (IS_ERR (st->adc_clk)) { +dev_err (&spi->dev,"failed to get the ADC clock\n" );
Here again, dev_err_probe() to avoid the message in the -EPROBE_DEFER case.
Quoted 41 lines
drivers/iio/adc/ti-ads131e08.c new file +41
-0+return PTR_ERR (st->adc_clk); + } + + ret =clk_prepare_enable (st->adc_clk); +if (ret) { +dev_err (&spi->dev,"failed to prepare/enable the ADC clock\n" ); +return ret; + } + + ret =devm_add_action_or_reset (&spi->dev, ads131e08_clk_disable, st); +if (ret) { +clk_disable_unprepare (st->adc_clk); +return ret; + } + + adc_clk_hz =clk_get_rate (st->adc_clk); +if (!adc_clk_hz) { +dev_err (&spi->dev,"failed to get the ADC clock rate\n" ); +return -EINVAL ; + } + + adc_clk_ns =NSEC_PER_SEC / adc_clk_hz; + st->sdecode_delay_us =DIV_ROUND_UP ( +ADS131E08_WAIT_SDECODE_CYCLES * adc_clk_ns,1000 ); + st->reset_delay_us =DIV_ROUND_UP ( +ADS131E08_WAIT_RESET_CYCLES * adc_clk_ns,1000 ); + + ret =ads131e08_initial_config (indio_dev); +if (ret) { +dev_err (&spi->dev,"initial configuration failed\n" ); +return ret; + } + + ret =devm_iio_device_register (&spi->dev, indio_dev); +if (ret) { +dev_err (&spi->dev,"failed to register IIO device\n" ); +return ret; + } + +return 0 ; +}
...
Re: [PATCH v3 1/2] iio: adc: Add driver for Texas Instruments ADS131E0x ADC family
In reply to [email protected]Quoted 7 lines · On Tue, Jan 12, 2021 at 2:37 PM <[email protected]> wrote:
From: Tomislav Denis <[email protected]>
The ADS131E0x are a family of multichannel, simultaneous sampling, 24-bit, delta-sigma, analog-to-digital converters (ADCs) with a built-in programmable gain amplifier (PGA), internal reference and an onboard oscillator.
...
drivers/iio/adc/Kconfig config TI_ADS124S08 · line 1156
Quoted 3 lines
drivers/iio/adc/Kconfig +3
-0+config TI_ADS131E08 + tristate "Texas Instruments ADS131E08" + depends on SPI && OF
Why OF?!
Quoted 8 lines
drivers/iio/adc/Kconfig +8
-0+ select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Say yes here to get support for Texas Instruments ADS131E04, ADS131E06 + and ADS131E08 chips. + + This driver can also be built as a module. If so, the module will be + called ti-ads131e08.
...
drivers/iio/adc/ti-ads131e08.c line 8
Quoted 8 lines
drivers/iio/adc/ti-ads131e08.c new file +8
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* Texas Instruments ADS131E0x 4-, 6- and 8-Channel ADCs +* +* Copyright (c) 2020 AVL DiTEST GmbH +* Tomislav Denis <[email protected]> +* +* Datasheet: https://www.ti.com/lit/ds/symlink/ads131e08.pdf
Please, put this as a tag in the commit message as well
drivers/iio/adc/ti-ads131e08.c line 15
Quoted 8 lines
+ */
drivers/iio/adc/ti-ads131e08.c new file +6
-0+ +#include <linux/bitfield.h> +#include <linux/clk.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/of_device.h>
Why?!
drivers/iio/adc/ti-ads131e08.c line 24
Quoted 9 lines
drivers/iio/adc/ti-ads131e08.c new file +9
-0+#include <linux/iio/buffer.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> +#include <linux/regulator/consumer.h> +#include <linux/spi/spi.h> +#include <asm/unaligned.h>
Can we group these to something like (wit blank lines between groups)
linux/*.h
linux/iio*.h
asm/*.h
?
...
drivers/iio/adc/ti-ads131e08.c line 34
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+#define ADS131E08_CMD_RREG (r) (0x20 | (r & GENMASK(4, 0))) +#define ADS131E08_CMD_WREG (r) (0x40 | (r & GENMASK(4, 0)))
BIT(5)
BIT(6)
...
drivers/iio/adc/ti-ads131e08.c line 59
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+#define ADS131E08_VREF_2V4_MV 2400 +#define ADS131E08_VREF_4V_MV 4000
I prefer mV despite this being defined constant.
...
Quoted 5 lines
drivers/iio/adc/ti-ads131e08.c new file +5
-0+struct ads131e08_state { +const struct ads131e08_info *info; +struct spi_device *spi; +struct iio_trigger *trig; +struct clk *adc_clk;
drivers/iio/adc/ti-ads131e08.c line 101
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +9
-0+struct regulator *vref_reg; +struct ads131e08_channel_config *channel_config; +unsigned int data_rate; +unsigned int vref_mv; +unsigned int sdecode_delay_us; +unsigned int reset_delay_us; +unsigned int readback_len; +struct completion completion; +
Redundant blank line. But it's up to you.
Quoted 13 lines
drivers/iio/adc/ti-ads131e08.c new file +13
-0+struct { +u8 data[ADS131E08_NUM_DATA_BYTES_MAX ]; +s64 ts__aligned(8) ; + } tmp_buf; + +u8 tx_buf[3 ]____cacheline_aligned ; +/* +* Add extra one padding byte to be able to access the last channel +* value using u32 pointer +*/ +u8 rx_buf[ADS131E08_NUM_STATUS_BYTES + +ADS131E08_NUM_DATA_BYTES_MAX +1 ]; +};
...
drivers/iio/adc/ti-ads131e08.c line 144
Quoted 9 lines
drivers/iio/adc/ti-ads131e08.c new file +9
-0+static const struct ads131e08_data_rate_desc ads131e08_data_rate_tbl[] = { + { .rate =64 , .reg =0x00 }, + { .rate =32 , .reg =0x01 }, + { .rate =16 , .reg =0x02 }, + { .rate =8 , .reg =0x03 }, + { .rate =4 , .reg =0x04 }, + { .rate =2 , .reg =0x05 }, + { .rate =1 , .reg =0x06 }, +};
Can't you use simple bit operations on this?
rate = BIT(6 - index)
reg = index
...
drivers/iio/adc/ti-ads131e08.c line 153
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+static const struct ads131e08_pga_gain_desc ads131e08_pga_gain_tbl[] = { + { .gain =1 , .reg =0x01 }, + { .gain =2 , .reg =0x02 },
reg == 3 valid?
drivers/iio/adc/ti-ads131e08.c line 157
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+ { .gain = 4, .reg = 0x04 }, + { .gain = 8, .reg = 0x05 }, + { .gain = 12, .reg = 0x06 }, +};
Also can be changed by formula, but I remember that in some cases tables are preferable.
...
drivers/iio/adc/ti-ads131e08.c line 168
Quoted 8 lines
drivers/iio/adc/ti-ads131e08.c new file +8
-0+static int ads131e08_exec_cmd (struct ads131e08_state *st,u8 cmd) +{ +int ret; + + ret =spi_write_then_read (st->spi, &cmd,1 ,NULL ,0 ); +if (ret) { +dev_err (&st->spi->dev, +"%s: SPI write failed for cmd: %02x\n" , __func__, cmd);
__func__ is not needed.
drivers/iio/adc/ti-ads131e08.c line 172
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+return ret; + } + +return 0 ;
The last is redundant, simply
return ret;
instead of both of them.
drivers/iio/adc/ti-ads131e08.c line 178
Quoted 7 lines
drivers/iio/adc/ti-ads131e08.c new file +6
-0+} + +static int ads131e08_read_reg (struct ads131e08_state *st,u8 reg) +{ +int ret; +
Redundant blank line.
Quoted 7 lines
drivers/iio/adc/ti-ads131e08.c new file +7
-0+struct spi_transfer transfer[] = { + { + .tx_buf = &st->tx_buf, + .len =2 , + .delay_usecs = st->sdecode_delay_us, + }, { + .rx_buf = &st->rx_buf,
drivers/iio/adc/ti-ads131e08.c line 195
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +10
-0+ .len = 1, + }, + }; + + st->tx_buf[0 ] =ADS131E08_CMD_RREG (reg); + st->tx_buf[1 ] =0 ; + + ret =spi_sync_transfer (st->spi, transfer,ARRAY_SIZE (transfer)); +if (ret) { +dev_err (&st->spi->dev,"%s: SPI transfer failed\n" , __func__);
__func__ is almost useless. Better to think about the uniqueness of the messages across the driver.
drivers/iio/adc/ti-ads131e08.c line 203
Quoted 8 lines
drivers/iio/adc/ti-ads131e08.c new file +7
-0+return ret; + } + +return st->rx_buf[0 ]; +} +static int ads131e08_write_reg (struct ads131e08_state *st,u8 reg,u8 value) +{
Similar comments as per previous function.
drivers/iio/adc/ti-ads131e08.c line 224
Quoted 8 lines
drivers/iio/adc/ti-ads131e08.c new file +7
-0+ ret =spi_sync_transfer (st->spi, transfer,ARRAY_SIZE (transfer)); +if (ret) { +dev_err (&st->spi->dev,"%s: SPI transfer failed\n" , __func__); +return ret; + } + +return 0 ;
return ret;
drivers/iio/adc/ti-ads131e08.c line 228
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+} + +static int ads131e08_read_data (struct ads131e08_state *st,int rx_len) +{
As above.
Quoted 1 line
+}
...
Quoted 1 line
+ for (i = 0; i <
ARRAY_SIZE(ads131e08_data_rate_tbl); ++i) {
i++ works the same and more regular pattern.
Ditto for other occurrences.
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+if (ads131e08_data_rate_tbl[i].rate == data_rate) +break ; + }
...
drivers/iio/adc/ti-ads131e08.c line 395
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+struct ads131e08_state *st =iio_priv (indio_dev); +const struct iio_chan_spec *channel = indio_dev->channels; +int ret, i; +u8 active_channels =0 ;
Reversed xmas tree order?
Also for the rest of the driver.
...
drivers/iio/adc/ti-ads131e08.c line 433
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+/* Power down unused channels */ +for (i =0 ; i < st->info->max_channels; ++i) { +if (!(active_channels &BIT (i))) {
NIH of for_each_clear_bit()
Quoted 5 lines
drivers/iio/adc/ti-ads131e08.c new file +5
-0+ ret =ads131e08_power_down_channel (st, i, true); +if (ret) +return ret; + } + }
...
drivers/iio/adc/ti-ads131e08.c line 446
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+/* +* Channel offset calibration is triggered with first START command.
the first
Quoted 1 line
+ * Since calibration take more time than settling operation,
takes
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+* this causes timeout error when command START is sent first +* time (e.g. first call of the ads131e08_read_direct method). +* To avoid this problem offset calibration is triggered here. +*/
...
drivers/iio/adc/ti-ads131e08.c line 462
Quoted 5 lines
drivers/iio/adc/ti-ads131e08.c new file +5
-0+ ret =ads131e08_exec_cmd (st,ADS131E08_CMD_STOP ); +if (ret) +return ret; + +return 0 ;
return _exec_cmd(...);
...
drivers/iio/adc/ti-ads131e08.c line 479
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+ ret =wait_for_completion_timeout (&st->completion, +msecs_to_jiffies (ADS131E08_MAX_SETTLING_TIME_MS ));
With a temporary variable is easier to read.
... timeout = msecs_to_jiffies(...);
ret = wait_for_completion_timeout(&st->completion, timeout);
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+if (!ret) +return -ETIMEDOUT ;
...
drivers/iio/adc/ti-ads131e08.c line 496
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+ *value =sign_extend32 ( +get_unaligned_be32 (src) >> (32 - num_bits), num_bits -1 );
One line, please.
Quoted 1 line
+ return ret;
Expected other than 0?
...
drivers/iio/adc/ti-ads131e08.c line 522
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+case IIO_CHAN_INFO_SCALE : +if (!IS_ERR (st->vref_reg)) {
Why not positive conditional?
Quoted 8 lines
drivers/iio/adc/ti-ads131e08.c new file +8
-0+ ret =regulator_get_voltage (st->vref_reg); +if (ret <0 ) +return ret; + + *value = ret /1000 ; + }else { + *value = st->vref_mv; + }
...
Quoted 1 line
+ return
-EINVAL;
Can you use rather
default:
return -EINVAL;
instead?
...
drivers/iio/adc/ti-ads131e08.c line 564
Quoted 12 lines
drivers/iio/adc/ti-ads131e08.c new file +12
-0+switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : + ret =iio_device_claim_direct_mode (indio_dev); +if (ret) +return ret; + + ret =ads131e08_set_data_rate (st, value); +iio_device_release_direct_mode (indio_dev); +return ret; + } + +return -EINVAL ;
Ditto.
...
drivers/iio/adc/ti-ads131e08.c line 584
Quoted 7 lines
drivers/iio/adc/ti-ads131e08.c new file +7
-0+static int ads131e08_debugfs_reg_access (struct iio_dev *indio_dev, +unsigned int reg,unsigned int writeval,unsigned int *readval) +{ +struct ads131e08_state *st =iio_priv (indio_dev); + +if (readval) { +int ret =ads131e08_read_reg (st, (u8 )reg);
Is casting needed?
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+ *readval = ret; +return ret; + }
...
drivers/iio/adc/ti-ads131e08.c line 624
Quoted 5 lines
drivers/iio/adc/ti-ads131e08.c new file +5
-0+/* +* The number of data bits per channel depends on the data rate. +* For 32 and 64 ksps data rates, number of data bits per channel +* is 16. This case is not compliant with used (fixed) scan element +* type (be:s24/32>>8). So we use a litle tweek to pack properly
little tweak
Quoted 2 lines
(quoted diff) +2
-0+ * 16 bits of data into the buffer. + */
...
drivers/iio/adc/ti-ads131e08.c line 635
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+for_each_set_bit (chn, indio_dev->active_scan_mask, + indio_dev->masklength) {
One line.
...
drivers/iio/adc/ti-ads131e08.c line 656
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+/* +* Data conversion from 16 bits of data to 24 bits of data +* is done by sign etension (properly filling padding byte).
extension
drivers/iio/adc/ti-ads131e08.c line 659
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+*/ +if (tweek_offset) +memset (dest, *src &BIT (7 ) ?0xff :0x00 ,1 );
One byte with memset()?!
...
Quoted 1 line
+ ret =
of_property_read_u32(node, "ti,vref-internal", &tmp);
device_property_read_u32()
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+if (ret) + tmp =0 ;
...
Quoted 1 line
+
num_channels=of_get_available_child_count(node);
fwnode API has something OF agnostic.
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+if (num_channels ==0 ) { +dev_err (&st->spi->dev,"no channel children\n" ); +return -ENODEV ; + }
...
drivers/iio/adc/ti-ads131e08.c line 759
Quoted 8 lines
drivers/iio/adc/ti-ads131e08.c new file +5
-0+for_each_available_child_of_node (node, child) { + ret =of_property_read_u32 (child,"reg" , &channel); + ret =of_property_read_u32 (child,"ti,gain" , &tmp); + ret =of_property_read_u32 (child,"ti,mux" , &tmp); + }
Easy to convert to fwnode API.
...
Quoted 1 line
+ info =
of_device_get_match_data(&spi->dev);
device_get_match_data()
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+if (!info) { +dev_err (&spi->dev,"failed to get match data\n" ); +return -ENODEV ; + }
...
Quoted 1 line
+
indio_dev->dev.of_node=spi->dev.of_node;
Do you need to copy fwnode as well?
...
drivers/iio/adc/ti-ads131e08.c line 895
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+ st->adc_clk =devm_clk_get (&spi->dev,"adc-clk" ); +if (IS_ERR (st->adc_clk)) { +dev_err (&spi->dev,"failed to get the ADC clock\n" );
Spam when deferred.
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+return PTR_ERR (st->adc_clk); + }
...
drivers/iio/adc/ti-ads131e08.c line 907
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +9
-0+ ret =clk_prepare_enable (st->adc_clk); +if (ret) { +dev_err (&spi->dev,"failed to prepare/enable the ADC clock\n" ); +return ret; + } + + ret =devm_add_action_or_reset (&spi->dev, ads131e08_clk_disable, st); +if (ret) { +clk_disable_unprepare (st->adc_clk);
"_or_reset" does call this, no?
Review
Quoted 2 lines
(quoted diff) +2
-0+ return ret; + }
Actually both calls can be moved to a separate helper.
...
drivers/iio/adc/ti-ads131e08.c line 919
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+ adc_clk_ns =NSEC_PER_SEC / adc_clk_hz; + st->sdecode_delay_us =DIV_ROUND_UP ( +ADS131E08_WAIT_SDECODE_CYCLES * adc_clk_ns,1000 );
NSEC_PER_USEC
drivers/iio/adc/ti-ads131e08.c line 921
Quoted 2 lines
drivers/iio/adc/ti-ads131e08.c new file +2
-0+ st->reset_delay_us =DIV_ROUND_UP ( +ADS131E08_WAIT_RESET_CYCLES * adc_clk_ns,1000 );
Ditto.
drivers/iio/adc/ti-ads131e08.c line 935
Quoted 7 lines
drivers/iio/adc/ti-ads131e08.c new file +7
-0+ ret =devm_iio_device_register (&spi->dev, indio_dev); +if (ret) { +dev_err (&spi->dev,"failed to register IIO device\n" ); +return ret; + } + +return 0 ;
return devm_iio_device_register(...);
Signature · 2 lines
With Best Regards, Andy Shevchenko
RE: [PATCH v3 1/2] iio: adc: Add driver for Texas Instruments ADS131E0x ADC family
Hi Andy,
First, thanks for the great review. I agree with all of your comments, just one question/comment inline.
Best Regards,
Tomislav
Quoted 18 lines
-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: 21 January 2021 17:42
To: Denis, Tomislav AVL DiTEST <[email protected]>
Cc: Jonathan Cameron <[email protected]>; linux-iio <[email protected]>; devicetree <[email protected]>
Subject: Re: [PATCH v3 1/2] iio: adc: Add driver for Texas Instruments ADS131E0x ADC familyQuoted 7 lines · On Tue, Jan 12, 2021 at 2:37 PM <[email protected]> wrote:
From: Tomislav Denis <[email protected]>
The ADS131E0x are a family of multichannel, simultaneous sampling, 24-bit, delta-sigma, analog-to-digital converters (ADCs) with a built-in programmable gain amplifier (PGA), internal reference and an onboard oscillator.
...
Quoted 24 lines
drivers/iio/adc/ti-ads131e08.c line 144
Quoted 10 lines
drivers/iio/adc/ti-ads131e08.c new file +9
-0+static const struct ads131e08_data_rate_desc ads131e08_data_rate_tbl[] = { + { .rate =64 , .reg =0x00 }, + { .rate =32 , .reg =0x01 }, + { .rate =16 , .reg =0x02 }, + { .rate =8 , .reg =0x03 }, + { .rate =4 , .reg =0x04 }, + { .rate =2 , .reg =0x05 }, + { .rate =1 , .reg =0x06 }, +};Can't you use simple bit operations on this?
rate = BIT(6 - index)
reg = index...
drivers/iio/adc/ti-ads131e08.c line 153
Quoted 3 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+static const struct ads131e08_pga_gain_desc ads131e08_pga_gain_tbl[] = { + { .gain =1 , .reg =0x01 }, + { .gain =2 , .reg =0x02 },reg == 3 valid?
Quoted 9 lines
drivers/iio/adc/ti-ads131e08.c line 157
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +4
-0+ { .gain = 4, .reg = 0x04 }, + { .gain = 8, .reg = 0x05 }, + { .gain = 12, .reg = 0x06 }, +};Also can be changed by formula, but I remember that in some cases tables are preferable.
I would like to keep those tables as they are, because I find them more intuitive and easy understandable!?
Quoted 4 lines
Quoted 4 lines
drivers/iio/adc/ti-ads131e08.c new file +3
-0+static int ads131e08_exec_cmd (struct ads131e08_state *st,u8 cmd) { +int ret; +
....
Quoted 4 lines
--
With Best Regards,
Andy Shevchenko
PATCH v3 2/2 bindings: iio: adc: Add documentation for ADS131E0x ADC driver
In reply to [email protected]From: Tomislav Denis <[email protected]>
Add a device tree binding documentation for Texas Instruments ADS131E0x ADC family driver.
--- .../devicetree/bindings/iio/adc/ti,ads131e08.yaml | 181 +++++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 182 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/ti,ads131e08.yaml
Documentation/devicetree/bindings/iio/adc/ti,ads131e08.yaml new file +181 -0
@@ -0,0 +1,181 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/ti,ads131e08.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Texas Instruments ADS131E0x 4-, 6- and 8-Channel ADCs + +maintainers: + - Tomislav Denis <[email protected]> + +description: | + The ADS131E0x are a family of multichannel, simultaneous sampling, + 24-bit, delta-sigma, analog-to-digital converters (ADCs) with a + built-in programmable gain amplifier (PGA), internal reference + and an onboard oscillator. + The communication with ADC chip is via the SPI bus (mode 1). + + https://www.ti.com/lit/ds/symlink/ads131e08.pdf + +properties: + compatible: + enum: + - ti,ads131e04 + - ti,ads131e06 + - ti,ads131e08 + + reg: + maxItems: 1 + + spi-max-frequency: true + + spi-cpha: true + + clocks: + description: | + Device tree identifier to the clock source (2.048 MHz). + Note: clock source is selected using CLKSEL pin. + maxItems: 1 + + clock-names: + items: + - const: adc-clk + + interrupts: + description: | + IRQ line for the ADC data ready. + maxItems: 1 + + vref-supply: + description: | + Optional external voltage reference. If not supplied, internal voltage + reference is used. + + ti,vref-internal: + description: | + Select the internal voltage reference value. + 0: 2.4V + 1: 4.0V + If this field is left empty, 2.4V is selected. + Note: internal voltage reference is used only if vref-supply is not supplied. + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + default: 0 + + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + +required: + - compatible + - reg + - spi-cpha + - clocks + - clock-names + - interrupts + +patternProperties: + "^channel@([0-7])$": + $ref: "adc.yaml" + type: object + description: | + Represents the external channels which are connected to the ADC. + + properties: + reg: + description: | + The channel number. + Up to 4 channels, numbered from 0 to 3 for ti,ads131e04. + Up to 6 channels, numbered from 0 to 5 for ti,ads131e06. + Up to 8 channels, numbered from 0 to 7 for ti,ads131e08. + items: + minimum: 0 + maximum: 7 + + ti,gain: + description: | + The PGA gain value for the channel. + If this field is left empty, PGA gain 1 is used. + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [1, 2, 4, 8, 12] + default: 1 + + ti,mux: + description: | + Channel input selection(muliplexer). + 0: Normal input. + 1: Input shorted to (VREFP + VREFN) / 2 (for offset or noise measurements). + 3: MVDD (for supply measurement) + 4: Temperature sensor + If this field is left empty, normal input is selected. + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1, 3, 4] + default: 0 + + required: + - reg + + additionalProperties: false + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + + spi { + #address-cells = <1>; + #size-cells = <0>; + + adc@0 { + compatible = "ti,ads131e08"; + reg = <0>; + spi-max-frequency = <1000000>; + spi-cpha; + clocks = <&clk2048k>; + clock-names = "adc-clk"; + interrupt-parent = <&gpio5>; + interrupts = <28 IRQ_TYPE_EDGE_FALLING>; + vref-supply = <&adc_vref>; + + #address-cells = <1>; + #size-cells = <0>; + + channel@0 { + reg = <0>; + }; + + channel@1 { + reg = <1>; + }; + + channel@2 { + reg = <2>; + ti,gain = <2>; + }; + + channel@3 { + reg = <3>; + }; + + channel@4 { + reg = <4>; + }; + + channel@5 { + reg = <5>; + }; + + channel@6 { + reg = <6>; + }; + + channel@7 { + reg = <7>; + ti,mux = <4>; + }; + }; + };
MAINTAINERS +1 -0
@@ -17371,6 +17371,7 @@ TI ADS131E0X ADC SERIES DRIVER M: Tomislav Denis <[email protected]> L: [email protected] S: Maintained +F: Documentation/devicetree/bindings/iio/adc/ti,ads131e08.yaml F: drivers/iio/adc/ti-ads131e08.c TI AM437X VPFE DRIVER
Signature · 1 line
2.7.4