Skip to contentSkip to conversation

Add support for AD4000 series of ADCs

PATCH Superseded Marcelo Schmitt 6 participants 23 messages Jun 29, 2024 – Jul 29, 2024

Subsystems (7)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Marcelo Schmitt

author maintainer
#0 lore raw

v6 cover Add support for AD4000 series of ADCs

This patch series extends the SPI bitbang, gpio, and spi-engine controllers to support configurable MOSI line idle states.
It then introduces the ad4000 driver which uses the MOSI idle configuration to provide improved support for the AD4000 series of ADCs. Documentation is added describing the new extension to the SPI protocol. The currently supported wiring modes for AD4000 devices were documented under IIO documentation directory.

To apply this series, it requires the patches for SPI-Engine SPI_CS_HIGH feature and the patches for devm_spi_optimize_message() helper.

89c2657429c4822a2697077bbb3a8d126d826ced "spi: axi-spi-engine: remove platform_set_drvdata()"
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git/commit/?h=for-6.11&id=89c2657429c4822a2697077bbb3a8d126d826ced

7e74a45c7afdd8a9f82d14fd79ae0383bbaaed1e "spi: add EXPORT_SYMBOL_GPL(devm_spi_optimize_message)"
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git/commit/?h=for-6.11&id=7e74a45c7afdd8a9f82d14fd79ae0383bbaaed1e

d4a0055fdc22381fa256e345095e88d134e354c5 "spi: add devm_spi_optimize_message() helper"
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git/commit/?h=for-6.11&id=d4a0055fdc22381fa256e345095e88d134e354c5

6ecdb0aa4dca62d236a659426e11e6cf302e8f18 "spi: axi-spi-engine: Add SPI_CS_HIGH support"
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git/commit/?h=for-6.11&id=6ecdb0aa4dca62d236a659426e11e6cf302e8f18

Change log v5 -> v6:
[SPI]
spi.h: Removed unused SPI_CONTROLLER_MOSI_IDLE_LOW and SPI_CONTROLLER_MOSI_IDLE_HIGH
spi-summay: Minor nit: inactive -> not asserted
spi-engine: Moved MOSI idle support check to IP core version check section [IIO]
ad4000: Fixed ad4000_read_reg(). *val = st->tx_buf[1]; -> *val = st->rx_buf[1];
ad4000: Use devm_regulator_bulk_get_enable()
ad4000: Use iio_device_claim_direct_scoped() and guard() to protect scale update
ad4000: Fail probe if ad4000_config() fail
ad4000: Moved ad4000_prepare_..._message() closer to probe to reduce scope
ad4000: Added AD4000_SDI_GND and switch case for more accurate error msg
ad4000: Removed unused st->turbo_mode
ad4000: Removed old misleading comments in enum ad4000_sdi
ad4000: A few minor readability and code style nits

Link to v5: https://lore.kernel.org/linux-iio/[email protected]/
Link to v4: https://lore.kernel.org/linux-iio/[email protected]/
Link to v3: https://lore.kernel.org/linux-iio/[email protected]/
Link to v2: https://lore.kernel.org/linux-iio/[email protected]/
Link to v1: https://lore.kernel.org/linux-iio/[email protected]/

Regard using spi_w8r8(), I tried it again and it doesn't work for ad4000. Looks like the smallest transfer size for these devices is 16-bit. From datasheets:
"The AD4000/AD4004/AD4008 configuration register is read from and written to with a 16-bit SPI instruction."

Regarding the sample buffer size, I'm keeping it 32-bits long following suggestion from Nuno and due to a bit of laziness from my side to re-test it. Will change if required.

Thank you to everybody who reviewed these patches. I appreciate your support.

Thanks,
Marcelo

Marcelo Schmitt (7):
spi: Enable controllers to extend the SPI protocol with MOSI idle
configuration
spi: bitbang: Implement support for MOSI idle state configuration
spi: spi-gpio: Add support for MOSI idle state configuration
spi: spi-axi-spi-engine: Add support for MOSI idle configuration
dt-bindings: iio: adc: Add AD4000
iio: adc: Add support for AD4000
docs: iio: Add documentation for AD4000

 .../bindings/iio/adc/adi,ad4000.yaml          | 197 +++++
 Documentation/iio/ad4000.rst                  | 131 ++++
 Documentation/iio/index.rst                   |   1 +
 Documentation/spi/spi-summary.rst             |  83 ++
 MAINTAINERS                                   |   9 +
 drivers/iio/adc/Kconfig                       |  12 +
 drivers/iio/adc/Makefile                      |   1 +
 drivers/iio/adc/ad4000.c                      | 708 ++++++++++++++++++
 drivers/spi/spi-axi-spi-engine.c              |  15 +-
 drivers/spi/spi-bitbang.c                     |  24 +
 drivers/spi/spi-gpio.c                        |  12 +-
 drivers/spi/spi.c                             |   7 +
 include/linux/spi/spi_bitbang.h               |   1 +
 include/uapi/linux/spi/spi.h                  |   5 +-
 14 files changed, 1200 insertions(+), 6 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml
 create mode 100644 Documentation/iio/ad4000.rst
 create mode 100644 drivers/iio/adc/ad4000.c
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v6 1/7 spi: Enable controllers to extend the SPI protocol with MOSI idle configuration

The behavior of an SPI controller data output line (SDO or MOSI or COPI (Controller Output Peripheral Input) for disambiguation) is usually not specified when the controller is not clocking out data on SCLK edges. However, there do exist SPI peripherals that require specific MOSI line state when data is not being clocked out of the controller.

Conventional SPI controllers may set the MOSI line on SCLK edges then bring it low when no data is going out or leave the line the state of the last transfer bit. More elaborated controllers are capable to set the MOSI idle state according to different configurable levels and thus are more suitable for interfacing with demanding peripherals.

Add SPI mode bits to allow peripherals to request explicit MOSI idle state when needed.

When supporting a particular MOSI idle configuration, the data output line state is expected to remain at the configured level when the controller is not clocking out data. When a device that needs a specific MOSI idle state is identified, its driver should request the MOSI idle configuration by setting the proper SPI mode bit.

---
 Documentation/spi/spi-summary.rst | 83 +++++++++++++++++++++++++++++++
 drivers/spi/spi.c                 |  7 +++
 include/uapi/linux/spi/spi.h      |  5 +-
 3 files changed, 93 insertions(+), 2 deletions(-)
Documentation/spi/spi-summary.rst +83 -0
index 7f8accfae6f9..6e21e6f86912 100644
@@ -614,6 +614,89 @@ queue, and then start some asynchronous transfer engine (unless it's
 already running).
 
 
+Extensions to the SPI protocol
+------------------------------
+The fact that SPI doesn't have a formal specification or standard permits chip
+manufacturers to implement the SPI protocol in slightly different ways. In most
+cases, SPI protocol implementations from different vendors are compatible among
+each other. For example, in SPI mode 0 (CPOL=0, CPHA=0) the bus lines may behave
+like the following:
+
+::
+
+  nCSx ___                                                                   ___
+          \_________________________________________________________________/
+          •                                                                 •
+          •                                                                 •
+  SCLK         ___     ___     ___     ___     ___     ___     ___     ___
+       _______/   \___/   \___/   \___/   \___/   \___/   \___/   \___/   \_____
+          •   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ; •
+          •   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ; •
+  MOSI XXX__________         _______                 _______         ________XXX
+  0xA5 XXX__/ 1     \_0_____/ 1     \_0_______0_____/ 1     \_0_____/ 1    \_XXX
+          •       ;       ;       ;       ;       ;       ;       ;       ; •
+          •       ;       ;       ;       ;       ;       ;       ;       ; •
+  MISO XXX__________         _______________________          _______        XXX
+  0xBA XXX__/     1 \_____0_/     1       1       1 \_____0__/    1  \____0__XXX
+
+Legend::
+
+  • marks the start/end of transmission;
+  : marks when data is clocked into the peripheral;
+  ; marks when data is clocked into the controller;
+  X marks when line states are not specified.
+
+In some few cases, chips extend the SPI protocol by specifying line behaviors
+that other SPI protocols don't (e.g. data line state for when CS is not
+asserted). Those distinct SPI protocols, modes, and configurations are supported
+by different SPI mode flags.
+
+MOSI idle state configuration
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Common SPI protocol implementations don't specify any state or behavior for the
+MOSI line when the controller is not clocking out data. However, there do exist
+peripherals that require specific MOSI line state when data is not being clocked
+out. For example, if the peripheral expects the MOSI line to be high when the
+controller is not clocking out data (``SPI_MOSI_IDLE_HIGH``), then a transfer in
+SPI mode 0 would look like the following:
+
+::
+
+  nCSx ___                                                                   ___
+          \_________________________________________________________________/
+          •                                                                 •
+          •                                                                 •
+  SCLK         ___     ___     ___     ___     ___     ___     ___     ___
+       _______/   \___/   \___/   \___/   \___/   \___/   \___/   \___/   \_____
+          •   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ; •
+          •   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ;   :   ; •
+  MOSI _____         _______         _______         _______________         ___
+  0x56      \_0_____/ 1     \_0_____/ 1     \_0_____/ 1       1     \_0_____/
+          •       ;       ;       ;       ;       ;       ;       ;       ; •
+          •       ;       ;       ;       ;       ;       ;       ;       ; •
+  MISO XXX__________         _______________________          _______        XXX
+  0xBA XXX__/     1 \_____0_/     1       1       1 \_____0__/    1  \____0__XXX
+
+Legend::
+
+  • marks the start/end of transmission;
+  : marks when data is clocked into the peripheral;
+  ; marks when data is clocked into the controller;
+  X marks when line states are not specified.
+
+In this extension to the usual SPI protocol, the MOSI line state is specified to
+be kept high when CS is asserted but the controller is not clocking out data to
+the peripheral and also when CS is not asserted.
+
+Peripherals that require this extension must request it by setting the
+``SPI_MOSI_IDLE_HIGH`` bit into the mode attribute of their ``struct
+spi_device`` and call spi_setup(). Controllers that support this extension
+should indicate it by setting ``SPI_MOSI_IDLE_HIGH`` in the mode_bits attribute
+of their ``struct spi_controller``. The configuration to idle MOSI low is
+analogous but uses the ``SPI_MOSI_IDLE_LOW`` mode bit.
+
+
 THANKS TO
 ---------
 Contributors to Linux-SPI discussions include (in alphabetical order,
drivers/spi/spi.c +7 -0
index 93f59ebb5b79..c8ba5e490850 100644
@@ -3942,6 +3942,12 @@ int spi_setup(struct spi_device *spi)
 		(SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
 		 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
 		return -EINVAL;
+	/* Check against conflicting MOSI idle configuration */
+	if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) {
+		dev_err(&spi->dev,
+			"setup: MOSI configured to idle low and high at the same time.\n");
+		return -EINVAL;
+	}
 	/*
 	 * Help drivers fail *cleanly* when they need options
 	 * that aren't supported with their current controller.
@@ -3950,6 +3956,7 @@ int spi_setup(struct spi_device *spi)
 	 */
 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
 				 SPI_NO_TX | SPI_NO_RX);
+
 	ugly_bits = bad_bits &
 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
include/uapi/linux/spi/spi.h +3 -2
index ca56e477d161..ee4ac812b8f8 100644
@@ -28,7 +28,8 @@
 #define	SPI_RX_OCTAL		_BITUL(14)	/* receive with 8 wires */
 #define	SPI_3WIRE_HIZ		_BITUL(15)	/* high impedance turnaround */
 #define	SPI_RX_CPHA_FLIP	_BITUL(16)	/* flip CPHA on Rx only xfer */
-#define SPI_MOSI_IDLE_LOW	_BITUL(17)	/* leave mosi line low when idle */
+#define SPI_MOSI_IDLE_LOW	_BITUL(17)	/* leave MOSI line low when idle */
+#define SPI_MOSI_IDLE_HIGH	_BITUL(18)	/* leave MOSI line high when idle */
 
 /*
  * All the bits defined above should be covered by SPI_MODE_USER_MASK.
@@ -38,6 +39,6 @@
  * These bits must not overlap. A static assert check should make sure of that.
  * If adding extra bits, make sure to increase the bit index below as well.
  */
-#define SPI_MODE_USER_MASK	(_BITUL(18) - 1)
+#define SPI_MODE_USER_MASK	(_BITUL(19) - 1)
 
 #endif /* _UAPI_SPI_H */
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH v6 2/7 spi: bitbang: Implement support for MOSI idle state configuration

In reply to Marcelo Schmitt

Some SPI peripherals may require strict MOSI line state when the controller is not clocking out data.
Implement support for MOSI idle state configuration (low or high) by setting the data output line level on controller setup and after transfers. Bitbang operations now call controller specific set_mosi_idle() call back to set MOSI to its idle state.
The MOSI line is kept at its idle state if no tx buffer is provided.

---
 drivers/spi/spi-bitbang.c       | 24 ++++++++++++++++++++++++
 include/linux/spi/spi_bitbang.h |  1 +
 2 files changed, 25 insertions(+)
drivers/spi/spi-bitbang.c +24 -0
index ca5cc67555c5..8cc522bf444c 100644
@@ -63,21 +63,28 @@ static unsigned bitbang_txrx_8(
 	unsigned flags
 )
 {
+	struct spi_bitbang	*bitbang;
 	unsigned		bits = t->bits_per_word;
 	unsigned		count = t->len;
 	const u8		*tx = t->tx_buf;
 	u8			*rx = t->rx_buf;
 
+	bitbang = spi_controller_get_devdata(spi->controller);
 	while (likely(count > 0)) {
 		u8		word = 0;
 
 		if (tx)
 			word = *tx++;
+		else
+			word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFF : 0;
 		word = txrx_word(spi, ns, word, bits, flags);
 		if (rx)
 			*rx++ = word;
 		count -= 1;
 	}
+	if (bitbang->set_mosi_idle)
+		bitbang->set_mosi_idle(spi);
+
 	return t->len - count;
 }
 
@@ -92,21 +99,28 @@ static unsigned bitbang_txrx_16(
 	unsigned flags
 )
 {
+	struct spi_bitbang	*bitbang;
 	unsigned		bits = t->bits_per_word;
 	unsigned		count = t->len;
 	const u16		*tx = t->tx_buf;
 	u16			*rx = t->rx_buf;
 
+	bitbang = spi_controller_get_devdata(spi->controller);
 	while (likely(count > 1)) {
 		u16		word = 0;
 
 		if (tx)
 			word = *tx++;
+		else
+			word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFF : 0;
 		word = txrx_word(spi, ns, word, bits, flags);
 		if (rx)
 			*rx++ = word;
 		count -= 2;
 	}
+	if (bitbang->set_mosi_idle)
+		bitbang->set_mosi_idle(spi);
+
 	return t->len - count;
 }
 
@@ -121,21 +135,28 @@ static unsigned bitbang_txrx_32(
 	unsigned flags
 )
 {
+	struct spi_bitbang	*bitbang;
 	unsigned		bits = t->bits_per_word;
 	unsigned		count = t->len;
 	const u32		*tx = t->tx_buf;
 	u32			*rx = t->rx_buf;
 
+	bitbang = spi_controller_get_devdata(spi->controller);
 	while (likely(count > 3)) {
 		u32		word = 0;
 
 		if (tx)
 			word = *tx++;
+		else
+			word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFFFFFF : 0;
 		word = txrx_word(spi, ns, word, bits, flags);
 		if (rx)
 			*rx++ = word;
 		count -= 4;
 	}
+	if (bitbang->set_mosi_idle)
+		bitbang->set_mosi_idle(spi);
+
 	return t->len - count;
 }
 
@@ -211,6 +232,9 @@ int spi_bitbang_setup(struct spi_device *spi)
 			goto err_free;
 	}
 
+	if (bitbang->set_mosi_idle)
+		bitbang->set_mosi_idle(spi);
+
 	dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
 
 	return 0;
include/linux/spi/spi_bitbang.h +1 -0
index b930eca2ef7b..1a54b593c691 100644
@@ -22,6 +22,7 @@ struct spi_bitbang {
 #define	BITBANG_CS_ACTIVE	1	/* normally nCS, active low */
 #define	BITBANG_CS_INACTIVE	0
 
+	void	(*set_mosi_idle)(struct spi_device *spi);
 	/* txrx_bufs() may handle dma mapping for transfers that don't
 	 * already have one (transfer.{tx,rx}_dma is zero), or use PIO
 	 */
Signature · 1 line
2.43.0
author maintainer
#8 lore raw

PATCH v6 3/7 spi: spi-gpio: Add support for MOSI idle state configuration

In reply to Marcelo Schmitt

Implement MOSI idle low and MOSI idle high to better support peripherals that request specific MOSI behavior.

---
 drivers/spi/spi-gpio.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/spi/spi-gpio.c +11 -1
index 909cce109bba..d3b8c99f0cb4 100644
@@ -236,6 +236,14 @@ static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
 	}
 }
 
+static void spi_gpio_set_mosi_idle(struct spi_device *spi)
+{
+	struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
+
+	gpiod_set_value_cansleep(spi_gpio->mosi,
+				 !!(spi->mode & SPI_MOSI_IDLE_HIGH));
+}
+
 static int spi_gpio_setup(struct spi_device *spi)
 {
 	struct gpio_desc	*cs;
@@ -411,7 +419,8 @@ static int spi_gpio_probe(struct platform_device *pdev)
 
 	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
 	host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
-			    SPI_CS_HIGH | SPI_LSB_FIRST;
+			  SPI_CS_HIGH | SPI_LSB_FIRST | SPI_MOSI_IDLE_LOW |
+			  SPI_MOSI_IDLE_HIGH;
 	if (!spi_gpio->mosi) {
 		/* HW configuration without MOSI pin
 		 *
@@ -436,6 +445,7 @@ static int spi_gpio_probe(struct platform_device *pdev)
 	host->flags |= SPI_CONTROLLER_GPIO_SS;
 	bb->chipselect = spi_gpio_chipselect;
 	bb->set_line_direction = spi_gpio_set_direction;
+	bb->set_mosi_idle = spi_gpio_set_mosi_idle;
 
 	if (host->flags & SPI_CONTROLLER_NO_TX) {
 		bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
Signature · 1 line
2.43.0
author maintainer
#10 lore raw

PATCH v6 4/7 spi: spi-axi-spi-engine: Add support for MOSI idle configuration

In reply to Marcelo Schmitt

Implement MOSI idle low and MOSI idle high to better support peripherals that request specific MOSI behavior.

---
 drivers/spi/spi-axi-spi-engine.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/spi/spi-axi-spi-engine.c +12 -3
index 03588fac9215..9e8f2108e279 100644
@@ -41,6 +41,7 @@
 #define SPI_ENGINE_CONFIG_CPHA			BIT(0)
 #define SPI_ENGINE_CONFIG_CPOL			BIT(1)
 #define SPI_ENGINE_CONFIG_3WIRE			BIT(2)
+#define SPI_ENGINE_CONFIG_SDO_IDLE_HIGH		BIT(3)
 
 #define SPI_ENGINE_INST_TRANSFER		0x0
 #define SPI_ENGINE_INST_ASSERT			0x1
@@ -137,6 +138,10 @@ static unsigned int spi_engine_get_config(struct spi_device *spi)
 		config |= SPI_ENGINE_CONFIG_CPHA;
 	if (spi->mode & SPI_3WIRE)
 		config |= SPI_ENGINE_CONFIG_3WIRE;
+	if (spi->mode & SPI_MOSI_IDLE_HIGH)
+		config |= SPI_ENGINE_CONFIG_SDO_IDLE_HIGH;
+	if (spi->mode & SPI_MOSI_IDLE_LOW)
+		config &= ~SPI_ENGINE_CONFIG_SDO_IDLE_HIGH;
 
 	return config;
 }
@@ -682,9 +687,13 @@ static int spi_engine_probe(struct platform_device *pdev)
 	host->num_chipselect = 8;
 
 	/* Some features depend of the IP core version. */
-	if (ADI_AXI_PCORE_VER_MINOR(version) >= 2) {
-		host->mode_bits |= SPI_CS_HIGH;
-		host->setup = spi_engine_setup;
+	if (ADI_AXI_PCORE_VER_MAJOR(version) >= 1) {
+		if (ADI_AXI_PCORE_VER_MINOR(version) >= 2) {
+			host->mode_bits |= SPI_CS_HIGH;
+			host->setup = spi_engine_setup;
+		}
+		if (ADI_AXI_PCORE_VER_MINOR(version) >= 3)
+			host->mode_bits |= SPI_MOSI_IDLE_LOW | SPI_MOSI_IDLE_HIGH;
 	}
 
 	if (host->max_speed_hz == 0)
Signature · 1 line
2.43.0
author maintainer
#11 lore raw

PATCH v6 5/7 dt-bindings: iio: adc: Add AD4000

In reply to Marcelo Schmitt

Add device tree documentation for AD4000 series of ADC devices.

---
 .../bindings/iio/adc/adi,ad4000.yaml          | 197 ++++++++++++++++++
 MAINTAINERS                                   |   7 +
 2 files changed, 204 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml
Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml new file +197 -0
new file mode 100644
index 000000000000..e413a9d8d2a2
@@ -0,0 +1,197 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/adi,ad4000.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD4000 and similar Analog to Digital Converters
+
+maintainers:
+  - Marcelo Schmitt <[email protected]>
+
+description: |
+  Analog Devices AD4000 family of Analog to Digital Converters with SPI support.
+  Specifications can be found at:
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad4000-4004-4008.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad4001-4005.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad4002-4006-4010.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad4003-4007-4011.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad4020-4021-4022.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4001.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4003.pdf
+
+$ref: /schemas/spi/spi-peripheral-props.yaml#
+
+properties:
+  compatible:
+    oneOf:
+      - const: adi,ad4000
+      - items:
+          - enum:
+              - adi,ad4004
+              - adi,ad4008
+          - const: adi,ad4000
+
+      - const: adi,ad4001
+      - items:
+          - enum:
+              - adi,ad4005
+          - const: adi,ad4001
+
+      - const: adi,ad4002
+      - items:
+          - enum:
+              - adi,ad4006
+              - adi,ad4010
+          - const: adi,ad4002
+
+      - const: adi,ad4003
+      - items:
+          - enum:
+              - adi,ad4007
+              - adi,ad4011
+          - const: adi,ad4003
+
+      - const: adi,ad4020
+      - items:
+          - enum:
+              - adi,ad4021
+              - adi,ad4022
+          - const: adi,ad4020
+
+      - const: adi,adaq4001
+
+      - const: adi,adaq4003
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 102040816 # for VIO > 2.7 V, 81300813 for VIO > 1.7 V
+
+  adi,sdi-pin:
+    $ref: /schemas/types.yaml#/definitions/string
+    enum: [ high, low, cs, sdi ]
+    default: sdi
+    description:
+      Describes how the ADC SDI pin is wired. A value of "sdi" indicates that
+      the ADC SDI is connected to host SDO. "high" indicates that the ADC SDI
+      pin is hard-wired to logic high (VIO). "low" indicates that it is
+      hard-wired low (GND). "cs" indicates that the ADC SDI pin is connected to
+      the host CS line.
+
+  '#daisy-chained-devices': true
+
+  vdd-supply:
+    description: A 1.8V supply that powers the chip (VDD).
+
+  vio-supply:
+    description:
+      A 1.8V to 5.5V supply for the digital inputs and outputs (VIO).
+
+  ref-supply:
+    description:
+      A 2.5 to 5V supply for the external reference voltage (REF).
+
+  cnv-gpios:
+    description:
+      When provided, this property indicates the GPIO that is connected to the
+      CNV pin.
+    maxItems: 1
+
+  adi,high-z-input:
+    type: boolean
+    description:
+      High-Z mode allows the amplifier and RC filter in front of the ADC to be
+      chosen based on the signal bandwidth of interest, rather than the settling
+      requirements of the switched capacitor SAR ADC inputs.
+
+  adi,gain-milli:
+    description: |
+      The hardware gain applied to the ADC input (in milli units).
+      The gain provided by the ADC input scaler is defined by the hardware
+      connections between chip pins OUT+, R1K-, R1K1-, R1K+, R1K1+, and OUT-.
+      If not present, default to 1000 (no actual gain applied).
+    $ref: /schemas/types.yaml#/definitions/uint16
+    enum: [454, 909, 1000, 1900]
+    default: 1000
+
+  interrupts:
+    description:
+      The SDO pin can also function as a busy indicator. This node should be
+      connected to an interrupt that is triggered when the SDO line goes low
+      while the SDI line is high and the CNV line is low ("3-wire" mode) or the
+      SDI line is low and the CNV line is high ("4-wire" mode); or when the SDO
+      line goes high while the SDI and CNV lines are high (chain mode),
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - vdd-supply
+  - vio-supply
+  - ref-supply
+
+allOf:
+  # The configuration register can only be accessed if SDI is connected to MOSI
+  - if:
+      required:
+        - adi,sdi-pin
+    then:
+      properties:
+        adi,high-z-input: false
+  # chain mode has lower SCLK max rate
+  - if:
+      required:
+        - '#daisy-chained-devices'
+    then:
+      properties:
+        spi-max-frequency:
+          maximum: 50000000 # for VIO > 2.7 V, 40000000 for VIO > 1.7 V
+  # Gain property only applies to ADAQ devices
+  - if:
+      properties:
+        compatible:
+          not:
+            contains:
+              enum:
+                - adi,adaq4001
+                - adi,adaq4003
+    then:
+      properties:
+        adi,gain-milli: false
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+        adc@0 {
+            compatible = "adi,ad4020";
+            reg = <0>;
+            spi-max-frequency = <71000000>;
+            vdd-supply = <&supply_1_8V>;
+            vio-supply = <&supply_1_8V>;
+            ref-supply = <&supply_5V>;
+            adi,sdi-pin = "cs";
+            cnv-gpios = <&gpio0 88 GPIO_ACTIVE_HIGH>;
+        };
+    };
+  - |
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+        adc@0 {
+            compatible = "adi,adaq4003";
+            reg = <0>;
+            spi-max-frequency = <80000000>;
+            vdd-supply = <&supply_1_8V>;
+            vio-supply = <&supply_1_8V>;
+            ref-supply = <&supply_5V>;
+            adi,high-z-input;
+            adi,gain-milli = /bits/ 16 <454>;
+        };
+    };
MAINTAINERS +7 -0
index 9517093d889d..9aa6531f7cf2 100644
@@ -1199,6 +1199,13 @@ W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml
 F:	drivers/iio/dac/ad3552r.c
 
+ANALOG DEVICES INC AD4000 DRIVER
+M:	Marcelo Schmitt <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	https://ez.analog.com/linux-software-drivers
+F:	Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml
+
 ANALOG DEVICES INC AD4130 DRIVER
 M:	Cosmin Tanislav <[email protected]>
 L:	[email protected]
Signature · 1 line
2.43.0
author maintainer
#12 lore raw

PATCH v6 6/7 iio: adc: Add support for AD4000

In reply to Marcelo Schmitt

Add support for AD4000 series of low noise, low power, high speed, successive approximation register (SAR) ADCs.

---
 MAINTAINERS              |   1 +
 drivers/iio/adc/Kconfig  |  12 +
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/ad4000.c | 708 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 722 insertions(+)
 create mode 100644 drivers/iio/adc/ad4000.c
MAINTAINERS +1 -0
index 9aa6531f7cf2..f4ffedada8ea 100644
@@ -1205,6 +1205,7 @@ L:	[email protected]
 S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml
+F:	drivers/iio/adc/ad4000.c
 
 ANALOG DEVICES INC AD4130 DRIVER
 M:	Cosmin Tanislav <[email protected]>
drivers/iio/adc/Kconfig +12 -0
index b8184706c7d1..5bbe843916a3 100644
@@ -21,6 +21,18 @@ config AD_SIGMA_DELTA
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 
+config AD4000
+	tristate "Analog Devices AD4000 ADC Driver"
+	depends on SPI
+	select IIO_BUFFER
+	select IIO_TRIGGERED_BUFFER
+	help
+	  Say yes here to build support for Analog Devices AD4000 high speed
+	  SPI analog to digital converters (ADC).
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called ad4000.
+
 config AD4130
 	tristate "Analog Device AD4130 ADC Driver"
 	depends on SPI
drivers/iio/adc/Makefile +1 -0
index 51298c52b223..f4361df40cca 100644
@@ -6,6 +6,7 @@
 # When adding new entries keep the list in alphabetical order
 obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o
 obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
+obj-$(CONFIG_AD4000) += ad4000.o
 obj-$(CONFIG_AD4130) += ad4130.o
 obj-$(CONFIG_AD7091R) += ad7091r-base.o
 obj-$(CONFIG_AD7091R5) += ad7091r5.o
drivers/iio/adc/ad4000.c new file +708 -0

708 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#19 lore raw

PATCH v6 7/7 docs: iio: Add documentation for AD4000

In reply to Marcelo Schmitt

Document wiring configurations for the AD4000 series of ADCs.

---
 Documentation/iio/ad4000.rst | 131 +++++++++++++++++++++++++++++++++++
 Documentation/iio/index.rst  |   1 +
 MAINTAINERS                  |   1 +
 3 files changed, 133 insertions(+)
 create mode 100644 Documentation/iio/ad4000.rst
Documentation/iio/ad4000.rst new file +131 -0
new file mode 100644
index 000000000000..de8fd3ae6e62
@@ -0,0 +1,131 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+=============
+AD4000 driver
+=============
+
+Device driver for Analog Devices Inc. AD4000 series of ADCs.
+
+Supported devices
+=================
+
+* `AD4000 <https://www.analog.com/AD4000>`_
+* `AD4001 <https://www.analog.com/AD4001>`_
+* `AD4002 <https://www.analog.com/AD4002>`_
+* `AD4003 <https://www.analog.com/AD4003>`_
+* `AD4004 <https://www.analog.com/AD4004>`_
+* `AD4005 <https://www.analog.com/AD4005>`_
+* `AD4006 <https://www.analog.com/AD4006>`_
+* `AD4007 <https://www.analog.com/AD4007>`_
+* `AD4008 <https://www.analog.com/AD4008>`_
+* `AD4010 <https://www.analog.com/AD4010>`_
+* `AD4011 <https://www.analog.com/AD4011>`_
+* `AD4020 <https://www.analog.com/AD4020>`_
+* `AD4021 <https://www.analog.com/AD4021>`_
+* `AD4022 <https://www.analog.com/AD4022>`_
+* `ADAQ4001 <https://www.analog.com/ADAQ4001>`_
+* `ADAQ4003 <https://www.analog.com/ADAQ4003>`_
+
+Wiring connections
+------------------
+
+Devices of the AD4000 series can be connected to the SPI host controller in a
+few different modes.
+
+CS mode, 3-wire turbo mode
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Datasheet "3-wire" mode is what most resembles standard SPI connection which,
+for these devices, comprises of connecting the controller CS line to device CNV
+pin and other SPI lines as usual. This configuration is (misleadingly) called
+"CS Mode, 3-Wire Turbo Mode" connection in datasheets.
+NOTE: The datasheet definition of 3-wire mode for the AD4000 series is NOT the
+same of standard spi-3wire mode.
+This is the only connection mode that allows configuration register access but
+it requires the SPI controller to support the ``SPI_MOSI_IDLE_HIGH`` feature.
+
+Omit the ``adi,sdi-pin`` property in device tree to select this mode.
+
+::
+
+                                         +-------------+
+     + ----------------------------------| SDO         |
+     |                                   |             |
+     |               +-------------------| CS          |
+     |               v                   |             |
+     |    +--------------------+         |     HOST    |
+     |    |        CNV         |         |             |
+     +--->| SDI   AD4000   SDO |-------->| SDI         |
+          |        SCK         |         |             |
+          +--------------------+         |             |
+                    ^                    |             |
+                    +--------------------| SCLK        |
+                                         +-------------+
+
+CS mode, 3-wire, without busy indicator
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Another wiring configuration supported as "3-wire" mode has the SDI pin
+hard-wired to digital input/output interface supply (VIO). In this setup, the
+controller is not required to support ``SPI_MOSI_IDLE_HIGH`` but register access
+is not possible. This connection mode saves one wire and works with any SPI
+controller.
+
+Set the ``adi,sdi-pin`` device tree property to ``"high"`` to select this mode.
+
+::
+
+                                         +-------------+
+                    +--------------------| CS          |
+                    v                    |             |
+    VIO   +--------------------+         |     HOST    |
+     |    |        CNV         |         |             |
+     +--->| SDI   AD4000   SDO |-------->| SDI         |
+          |        SCK         |         |             |
+          +--------------------+         |             |
+                    ^                    |             |
+                    +--------------------| SCLK        |
+                                         +-------------+
+
+Alternatively, a GPIO may be connected to the device CNV pin. This is similar to
+the previous wiring configuration but saves the use of a CS line.
+
+::
+
+                                         +-------------+
+                    +--------------------| GPIO        |
+                    v                    |             |
+    VIO   +--------------------+         |     HOST    |
+     |    |        CNV         |         |             |
+     +--->| SDI   AD4000   SDO |-------->| SDI         |
+          |        SCK         |         |             |
+          +--------------------+         |             |
+                    ^                    |             |
+                    +--------------------| SCLK        |
+                                         +-------------+
+
+CS mode, 4-wire without busy indicator
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+In datasheet "4-wire" mode, the controller CS line is connected to the ADC SDI
+pin and a GPIO is connected to the ADC CNV pin. This connection mode may better
+suit scenarios where multiple ADCs can share one CNV trigger.
+
+Set ``adi,sdi-pin`` to ``"cs"`` to select this mode.
+
+
+::
+
+                                         +-------------+
+     + ----------------------------------| CS          |
+     |                                   |             |
+     |               +-------------------| GPIO        |
+     |               v                   |             |
+     |    +--------------------+         |     HOST    |
+     |    |        CNV         |         |             |
+     +--->| SDI   AD4000   SDO |-------->| SDI         |
+          |        SCK         |         |             |
+          +--------------------+         |             |
+                    ^                    |             |
+                    +--------------------| SCLK        |
+                                         +-------------+
Documentation/iio/index.rst +1 -0
index 4c13bfa2865c..5df157a44923 100644
@@ -17,6 +17,7 @@ Industrial I/O Kernel Drivers
 .. toctree::
    :maxdepth: 1
 
+   ad4000
    ad7944
    adis16475
    adis16480
MAINTAINERS +1 -0
index f4ffedada8ea..a03b3db9157c 100644
@@ -1205,6 +1205,7 @@ L:	[email protected]
 S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml
+F:	Documentation/iio/ad4000.rst
 F:	drivers/iio/adc/ad4000.c
 
 ANALOG DEVICES INC AD4130 DRIVER
Signature · 1 line
2.43.0
maintainer Reviewed-by on v6 1/7
#2 lore raw

Re: [PATCH v6 1/7] spi: Enable controllers to extend the SPI protocol with MOSI idle configuration

In reply to Marcelo Schmitt
Quoted 15 lines · On Sat, 29 Jun 2024 16:04:40 -0300 Marcelo Schmitt…

The behavior of an SPI controller data output line (SDO or MOSI or COPI (Controller Output Peripheral Input) for disambiguation) is usually not specified when the controller is not clocking out data on SCLK edges. However, there do exist SPI peripherals that require specific MOSI line state when data is not being clocked out of the controller.

Conventional SPI controllers may set the MOSI line on SCLK edges then bring it low when no data is going out or leave the line the state of the last transfer bit. More elaborated controllers are capable to set the MOSI idle state according to different configurable levels and thus are more suitable for interfacing with demanding peripherals.

Add SPI mode bits to allow peripherals to request explicit MOSI idle state when needed.

Quoted 8 lines

When supporting a particular MOSI idle configuration, the data output line state is expected to remain at the configured level when the controller is not clocking out data. When a device that needs a specific MOSI idle state is identified, its driver should request the MOSI idle configuration by setting the proper SPI mode bit.

I always like to see some nice ascii art. Very nice documentation.

drivers/spi/spi.c int spi_setup(struct spi_device *spi) · line 3959

Quoted 10 lines
drivers/spi/spi.c +1 -0
index 93f59ebb5b79..c8ba5e490850 100644
@@ -3950,6 +3956,7 @@ int spi_setup(struct spi_device *spi)
 	 */
 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
 				 SPI_NO_TX | SPI_NO_RX);
+

Trivial grumpy comment. Don't touch white space in unrelated code!

Quoted 3 lines
drivers/spi/spi.c +0 -0
 	ugly_bits = bad_bits &
 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
maintainer Reviewed-by on v6 2/7
#6 lore raw

Re: [PATCH v6 2/7] spi: bitbang: Implement support for MOSI idle state configuration

In reply to Marcelo Schmitt
Quoted 7 lines · On Sat, 29 Jun 2024 16:05:05 -0300 Marcelo Schmitt…

Some SPI peripherals may require strict MOSI line state when the controller is not clocking out data.
Implement support for MOSI idle state configuration (low or high) by setting the data output line level on controller setup and after transfers. Bitbang operations now call controller specific set_mosi_idle() call back to set MOSI to its idle state.
The MOSI line is kept at its idle state if no tx buffer is provided.

Slightly odd wrapping - this doesn't warrant 3 paragraphs, so I'd just reflow it into one.

I'm not a fan of counting F's, so would have gone with GENMASK for those but it's not the local style, so fair enough to hard code them. FWIW given it's been a long time since I messed around in SPI controller drivers...

Quoted 4 lines
maintainer Reviewed-by on v6 3/7
#9 lore raw

Re: [PATCH v6 3/7] spi: spi-gpio: Add support for MOSI idle state configuration

In reply to Marcelo Schmitt
Quoted 6 lines · On Sat, 29 Jun 2024 16:05:33 -0300 Marcelo Schmitt…

Implement MOSI idle low and MOSI idle high to better support peripherals that request specific MOSI behavior.

LGTM

maintainer
#13 lore raw

Re: [PATCH v6 6/7] iio: adc: Add support for AD4000

In reply to Marcelo Schmitt
Quoted 4 lines · On Sat, 29 Jun 2024 16:06:59 -0300 Marcelo Schmitt…

Add support for AD4000 series of low noise, low power, high speed, successive approximation register (SAR) ADCs.

Hi Marcelo

A few comments inline. However, the spi_w8r8 etc can easily be a follow up optimization patch (if you agree it's a good improvement) and the other changes are so trivial I could tweak whilst applying.

So unless you have to do a v7 for some other reason this is fine for merging as is - subject to the fact it's not been on list long enough yet and I need Mark to pick up the SPI parts and throw me a tag to pull.

Thanks,

Jonathan

Quoted 20 lines
drivers/iio/adc/ad4000.c new file +16 -0
@@ -0,0 +1,708 @@
 
+
+struct ad4000_state {
+	struct spi_device *spi;
+	struct gpio_desc *cnv_gpio;
+	struct spi_transfer xfers[2];
+	struct spi_message msg;
+	struct mutex lock; /* Protect read modify write cycle */
+	int vref_mv;
+	enum ad4000_sdi sdi_pin;
+	bool span_comp;
+	u16 gain_milli;
+	int scale_tbl[AD4000_SCALE_OPTIONS][2];
+
+	/*
+	 * DMA (thus cache coherency maintenance) requires the transfer buffers
+	 * to live in their own cache lines.

drivers/iio/adc/ad4000.c line 235

Quoted 10 lines
drivers/iio/adc/ad4000.c new file +10 -0
+	 */
+	struct {
+		union {
+			__be16 sample_buf16;
+			__be32 sample_buf32;
+		} data;
+		s64 timestamp __aligned(8);
+	} scan __aligned(IIO_DMA_MINALIGN);
+	u8 tx_buf[2];
+	u8 rx_buf[2];

If you made the spi_w8r8() change suggested below (which uses a bounce buffer in the spi core), rx_buf would be unused and can go away.

Given I think registers accesses on this device are all off the fast path you could even use spi_write_then_read() with zero read size for the register writes and rely on the spi core bounce buffers.
That way tx_buf goes away as well leaving you with the dma safe buffer for only the fast path reads.

Quoted 15 lines
drivers/iio/adc/ad4000.c new file +15 -0
+};
+
+static void ad4000_fill_scale_tbl(struct ad4000_state *st,
+				  struct iio_chan_spec const *chan)
+{
+	int val, tmp0, tmp1;
+	int scale_bits;
+	u64 tmp2;
+
+	/*
+	 * ADCs that output two's complement code have one less bit to express
+	 * voltage magnitude.
+	 */
+	if (chan->scan_type.sign == 's')
+		scale_bits = chan->scan_type.realbits - 1;

drivers/iio/adc/ad4000.c line 260

Quoted 10 lines
drivers/iio/adc/ad4000.c new file +10 -0
+	else
+		scale_bits = chan->scan_type.realbits;
+
+	/*
+	 * The gain is stored as a fraction of 1000 and, as we need to
+	 * divide vref_mv by the gain, we invert the gain/1000 fraction.
+	 * Also multiply by an extra MILLI to preserve precision.
+	 * Thus, we have MILLI * MILLI equals MICRO as fraction numerator.
+	 */
+	val = mult_frac(st->vref_mv, MICRO, st->gain_milli);

If you are rolling a v7 for other reasons, stick some line breaks in here! It's a bit of a mass of text that is hard for my eyes to parse!

Quoted 21 lines
drivers/iio/adc/ad4000.c new file +20 -0
+	/* Would multiply by NANO here but we multiplied by extra MILLI */
+	tmp2 = shift_right((u64)val * MICRO, scale_bits);
+	tmp0 = div_s64_rem(tmp2, NANO, &tmp1);
+	/* Store scale for when span compression is disabled */
+	st->scale_tbl[0][0] = tmp0; /* Integer part */
+	st->scale_tbl[0][1] = abs(tmp1); /* Fractional part */
+	/* Store scale for when span compression is enabled */
+	st->scale_tbl[1][0] = tmp0;
+	/* The integer part is always zero so don't bother to divide it. */
+	if (chan->differential)
+		st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 4, 5);
+	else
+		st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 9, 10);
+}
 
+static int ad4000_read_reg(struct ad4000_state *st, unsigned int *val)
+{
+	struct spi_transfer t = {
+		.tx_buf = st->tx_buf,
+		.rx_buf = st->rx_buf,
+		.len = 2,

drivers/iio/adc/ad4000.c line 298

Quoted 10 lines
drivers/iio/adc/ad4000.c new file +10 -0
+	};
+	int ret;
+
+	st->tx_buf[0] = AD4000_READ_COMMAND;
+	ret = spi_sync_transfer(st->spi, &t, 1);
+	if (ret < 0)
+		return ret;
+
+	*val = st->rx_buf[1];
+	return ret;

I'd be tempted to do

	ssize_t ret;

	ret = spi_w8r8(AD4000_READ_COMMAND);
	if (ret < 0)
		return ret;
	*val = ret;
	
	return 0;
Quoted 21 lines
drivers/iio/adc/ad4000.c new file +19 -0
+}
 
 
+static int ad4000_write_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan, int val, int val2,
+			    long mask)
+{
+	struct ad4000_state *st = iio_priv(indio_dev);
+	unsigned int reg_val;
+	bool span_comp_en;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
+			guard(mutex)(&st->lock);
+
+			ret = ad4000_read_reg(st, &reg_val);
+			if (ret < 0)
+				return ret;
+

drivers/iio/adc/ad4000.c line 424

Quoted 10 lines
drivers/iio/adc/ad4000.c new file +10 -0
+			span_comp_en = val2 == st->scale_tbl[1][1];
+			reg_val &= ~AD4000_CFG_SPAN_COMP;
+			reg_val |= FIELD_PREP(AD4000_CFG_SPAN_COMP, span_comp_en);
+
+			ret = ad4000_write_reg(st, reg_val);
+			if (ret < 0)
+				return ret;
+
+			st->span_comp = span_comp_en;
+			return ret;

If you are spinning for another reason, make it clear this is always good. The spi_write() never returns positive so current code is correct but I had to go check which this would have avoided.

			return 0;

If nothing else comes up, I'll probably tweak whilst applying.

J

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+		}
+		unreachable();
+	default:
+		return -EINVAL;
+	}
+}
#3 lore raw

Re: [PATCH v6 1/7] spi: Enable controllers to extend the SPI protocol with MOSI idle configuration

In reply to Jonathan Cameron
Quoted 35 lines · Jonathan Cameron
Quoted 15 lines · On Sat, 29 Jun 2024 16:04:40 -0300 Marcelo Schmitt…

The behavior of an SPI controller data output line (SDO or MOSI or COPI (Controller Output Peripheral Input) for disambiguation) is usually not specified when the controller is not clocking out data on SCLK edges. However, there do exist SPI peripherals that require specific MOSI line state when data is not being clocked out of the controller.

Conventional SPI controllers may set the MOSI line on SCLK edges then bring it low when no data is going out or leave the line the state of the last transfer bit. More elaborated controllers are capable to set the MOSI idle state according to different configurable levels and thus are more suitable for interfacing with demanding peripherals.

Add SPI mode bits to allow peripherals to request explicit MOSI idle state when needed.

Quoted 8 lines

When supporting a particular MOSI idle configuration, the data output line state is expected to remain at the configured level when the controller is not clocking out data. When a device that needs a specific MOSI idle state is identified, its driver should request the MOSI idle configuration by setting the proper SPI mode bit.

I always like to see some nice ascii art. Very nice documentation.

Quoted 4 lines
drivers/spi/spi.c +0 -0
index 93f59ebb5b79..c8ba5e490850 100644
Quoted 7 lines

drivers/spi/spi.c int spi_setup(struct spi_device *spi) · line 3959

Quoted 5 lines
drivers/spi/spi.c +1 -0
@@ -3950,6 +3956,7 @@ int spi_setup(struct spi_device *spi)
 	 */
 	bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD |
 				 SPI_NO_TX | SPI_NO_RX);
+

Trivial grumpy comment. Don't touch white space in unrelated code!

Ouf, must have slipped through after messing around with spi_setup(). Didn't intend to add that. Fine if that can be removed when applying the patch.

Quoted 4 lines
Quoted 4 lines
drivers/spi/spi.c +0 -0
 	ugly_bits = bad_bits &
 		    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
 		     SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
#14 lore raw

Re: [PATCH v6 6/7] iio: adc: Add support for AD4000

In reply to Jonathan Cameron
Quoted 14 lines · Jonathan Cameron
Quoted 4 lines · On Sat, 29 Jun 2024 16:06:59 -0300 Marcelo Schmitt…

Add support for AD4000 series of low noise, low power, high speed, successive approximation register (SAR) ADCs.

Hi Marcelo

A few comments inline. However, the spi_w8r8 etc can easily be a follow up optimization patch (if you agree it's a good improvement) and the other changes are so trivial I could tweak whilst applying.

...

Quoted 11 lines

drivers/iio/adc/ad4000.c line 260

Quoted 7 lines
drivers/iio/adc/ad4000.c new file +7 -0
+	/*
+	 * The gain is stored as a fraction of 1000 and, as we need to
+	 * divide vref_mv by the gain, we invert the gain/1000 fraction.
+	 * Also multiply by an extra MILLI to preserve precision.
+	 * Thus, we have MILLI * MILLI equals MICRO as fraction numerator.
+	 */
+	val = mult_frac(st->vref_mv, MICRO, st->gain_milli);

If you are rolling a v7 for other reasons, stick some line breaks in here! It's a bit of a mass of text that is hard for my eyes to parse!

Ack

...

Quoted 20 lines
Quoted 7 lines
drivers/iio/adc/ad4000.c new file +6 -0
+static int ad4000_read_reg(struct ad4000_state *st, unsigned int *val)
+{
+	struct spi_transfer t = {
+		.tx_buf = st->tx_buf,
+		.rx_buf = st->rx_buf,
+		.len = 2,

drivers/iio/adc/ad4000.c line 298

Quoted 10 lines
drivers/iio/adc/ad4000.c new file +10 -0
+	};
+	int ret;
+
+	st->tx_buf[0] = AD4000_READ_COMMAND;
+	ret = spi_sync_transfer(st->spi, &t, 1);
+	if (ret < 0)
+		return ret;
+
+	*val = st->rx_buf[1];
+	return ret;

I'd be tempted to do

Quoted 9 lines
	ssize_t ret;

	ret = spi_w8r8(AD4000_READ_COMMAND);
	if (ret < 0)
		return ret;
	*val = ret;
	
	return 0;

I tried this when working on v6. Only difference was I had declared ret as int. Then reg values were not read correctly with spi_w8r8().
I'm either missing something or reg access must be 16-bit transfer. Datasheet sais:
"The AD4000/AD4004/AD4008 configuration register is read from and written to with a 16-bit SPI instruction."
Yet, besides possible delay between first and last 8 SCLK pulses, I don't see any transfer level differences between current and spi_w8r8() versions.

Quoted 2 lines

...

Quoted 11 lines

drivers/iio/adc/ad4000.c line 424

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+			ret = ad4000_write_reg(st, reg_val);
+			if (ret < 0)
+				return ret;
+
+			st->span_comp = span_comp_en;
+			return ret;

If you are spinning for another reason, make it clear this is always good. The spi_write() never returns positive so current code is correct but I had to go check which this would have avoided.

Quoted 1 line
			return 0;

Ack

Quoted 12 lines

If nothing else comes up, I'll probably tweak whilst applying.

J

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+		}
+		unreachable();
+	default:
+		return -EINVAL;
+	}
+}
maintainer
#15 lore raw

Re: [PATCH v6 6/7] iio: adc: Add support for AD4000

Quoted 15 lines · Marcelo Schmitt
Quoted 10 lines · Jonathan Cameron

drivers/iio/adc/ad4000.c line 289

Quoted 5 lines · Marcelo Schmitt
drivers/iio/adc/ad4000.c new file +5 -0
+	struct spi_transfer t = {
+		.tx_buf = st->tx_buf,
+		.rx_buf = st->rx_buf,
+		.len = 2,
+	};

I'd be tempted to do

Quoted 3 lines
	ssize_t ret;

	ret = spi_w8r8(AD4000_READ_COMMAND);
Quoted 8 lines

I tried this when working on v6. Only difference was I had declared ret as int. Then reg values were not read correctly with spi_w8r8().
I'm either missing something or reg access must be 16-bit transfer. Datasheet sais:
"The AD4000/AD4004/AD4008 configuration register is read from and written to with a 16-bit SPI instruction."
Yet, besides possible delay between first and last 8 SCLK pulses, I don't see any transfer level differences between current and spi_w8r8() versions.

It is possible the chip gets upset with the state of the idle line during the RX only or TX only portion of the transfer.

Quoted 30 lines
Quoted 3 lines

...

Quoted 11 lines

drivers/iio/adc/ad4000.c line 424

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+			ret = ad4000_write_reg(st, reg_val);
+			if (ret < 0)
+				return ret;
+
+			st->span_comp = span_comp_en;
+			return ret;

If you are spinning for another reason, make it clear this is always good. The spi_write() never returns positive so current code is correct but I had to go check which this would have avoided.

Quoted 1 line
			return 0;

Ack

Quoted 12 lines

If nothing else comes up, I'll probably tweak whilst applying.

J

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+		}
+		unreachable();
+	default:
+		return -EINVAL;
+	}
+}
maintainer
#16 lore raw

Re: [PATCH v6 6/7] iio: adc: Add support for AD4000

In reply to Marcelo Schmitt
Quoted 60 lines · On Mon, 1 Jul 2024 15:10:54 -0300 Marcelo Schmitt <[email protected]>…
Quoted 14 lines · Jonathan Cameron
Quoted 4 lines · On Sat, 29 Jun 2024 16:06:59 -0300 Marcelo Schmitt…

Add support for AD4000 series of low noise, low power, high speed, successive approximation register (SAR) ADCs.

Hi Marcelo

A few comments inline. However, the spi_w8r8 etc can easily be a follow up optimization patch (if you agree it's a good improvement) and the other changes are so trivial I could tweak whilst applying.

...

Quoted 11 lines

drivers/iio/adc/ad4000.c line 260

Quoted 7 lines
drivers/iio/adc/ad4000.c new file +7 -0
+	/*
+	 * The gain is stored as a fraction of 1000 and, as we need to
+	 * divide vref_mv by the gain, we invert the gain/1000 fraction.
+	 * Also multiply by an extra MILLI to preserve precision.
+	 * Thus, we have MILLI * MILLI equals MICRO as fraction numerator.
+	 */
+	val = mult_frac(st->vref_mv, MICRO, st->gain_milli);  

If you are rolling a v7 for other reasons, stick some line breaks in here! It's a bit of a mass of text that is hard for my eyes to parse!

Ack

...

Quoted 20 lines
Quoted 7 lines
drivers/iio/adc/ad4000.c new file +6 -0
+static int ad4000_read_reg(struct ad4000_state *st, unsigned int *val)
+{
+	struct spi_transfer t = {
+		.tx_buf = st->tx_buf,
+		.rx_buf = st->rx_buf,
+		.len = 2,

drivers/iio/adc/ad4000.c line 298

Quoted 10 lines
drivers/iio/adc/ad4000.c new file +10 -0
+	};
+	int ret;
+
+	st->tx_buf[0] = AD4000_READ_COMMAND;
+	ret = spi_sync_transfer(st->spi, &t, 1);
+	if (ret < 0)
+		return ret;
+
+	*val = st->rx_buf[1];
+	return ret;  

I'd be tempted to do

Quoted 9 lines
	ssize_t ret;

	ret = spi_w8r8(AD4000_READ_COMMAND);
	if (ret < 0)
		return ret;
	*val = ret;
	
	return 0;
Quoted 8 lines

I tried this when working on v6. Only difference was I had declared ret as int. Then reg values were not read correctly with spi_w8r8().
I'm either missing something or reg access must be 16-bit transfer. Datasheet sais:
"The AD4000/AD4004/AD4008 configuration register is read from and written to with a 16-bit SPI instruction."
Yet, besides possible delay between first and last 8 SCLK pulses, I don't see any transfer level differences between current and spi_w8r8() versions.

Ah. If you go around again, throw in a comment so we don't 'fix' it in the future!

Quoted 30 lines
Quoted 3 lines

...

Quoted 11 lines

drivers/iio/adc/ad4000.c line 424

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+			ret = ad4000_write_reg(st, reg_val);
+			if (ret < 0)
+				return ret;
+
+			st->span_comp = span_comp_en;
+			return ret;  

If you are spinning for another reason, make it clear this is always good. The spi_write() never returns positive so current code is correct but I had to go check which this would have avoided.

Quoted 1 line
			return 0;  

Ack

Quoted 12 lines

If nothing else comes up, I'll probably tweak whilst applying.

J

Quoted 6 lines
drivers/iio/adc/ad4000.c new file +6 -0
+		}
+		unreachable();
+	default:
+		return -EINVAL;
+	}
+}  
reviewer Reviewed-by Tested-by on v6 1/7
#4 lore raw

Re: [PATCH v6 1/7] spi: Enable controllers to extend the SPI protocol with MOSI idle configuration

In reply to Marcelo Schmitt
Quoted 21 lines · Marcelo Schmitt

The behavior of an SPI controller data output line (SDO or MOSI or COPI (Controller Output Peripheral Input) for disambiguation) is usually not specified when the controller is not clocking out data on SCLK edges. However, there do exist SPI peripherals that require specific MOSI line state when data is not being clocked out of the controller.

Conventional SPI controllers may set the MOSI line on SCLK edges then bring it low when no data is going out or leave the line the state of the last transfer bit. More elaborated controllers are capable to set the MOSI idle state according to different configurable levels and thus are more suitable for interfacing with demanding peripherals.

Add SPI mode bits to allow peripherals to request explicit MOSI idle state when needed.

When supporting a particular MOSI idle configuration, the data output line state is expected to remain at the configured level when the controller is not clocking out data. When a device that needs a specific MOSI idle state is identified, its driver should request the MOSI idle configuration by setting the proper SPI mode bit.

Quoted 3 lines

---

Tested both valid and invalid combinations of flags and saw expected behavior/error messages in all cases.

reviewer
#7 lore raw

Re: [PATCH v6 2/7] spi: bitbang: Implement support for MOSI idle state configuration

In reply to Marcelo Schmitt
Quoted 8 lines · Marcelo Schmitt

Some SPI peripherals may require strict MOSI line state when the controller is not clocking out data.
Implement support for MOSI idle state configuration (low or high) by setting the data output line level on controller setup and after transfers. Bitbang operations now call controller specific set_mosi_idle() call back to set MOSI to its idle state.
The MOSI line is kept at its idle state if no tx buffer is provided.

Quoted 4 lines

---

FYI, this doesn't apply cleanly to spi-next and needs to be rebased.

(conflicts with https://lore.kernel.org/all/[email protected]/)

Reviewed-by Nuno Sa on v6 6/7
#18 lore raw

Re: [PATCH v6 6/7] iio: adc: Add support for AD4000

In reply to Marcelo Schmitt
Quoted 5 lines · Marcelo Schmitt

Add support for AD4000 series of low noise, low power, high speed, successive approximation register (SAR) ADCs.

---

Hi Marcelo,

LGTM. Only one thing that needs to be addressed. With that,

Quoted 7 lines

 MAINTAINERS              |   1 +
 drivers/iio/adc/Kconfig  |  12 +
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/ad4000.c | 708 +++++++++++++++++++++++++++++++++++++++

 4 files changed, 722 insertions(+)

 create mode 100644 drivers/iio/adc/ad4000.c

...

Review

Quoted 11 lines
drivers/iio/adc/ad4000.c new file +8 -0
+	st->gain_milli = 1000;
+	if (chip->has_hardware_gain &&
+	    device_property_present(dev, "adi,gain-milli")) {
+		ret = device_property_read_u16(dev, "adi,gain-milli",
+					       &st->gain_milli);
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "Failed to read gain

property\n");

(quoted diff) +1 -0
+	}

The above is odd. Why not reading directly device_property_read_u16()? Skip the call to device_property_present().

But most importantly, you're not doing any validation on gain_milli which is an enum (by looking at the bindings). So in theory even 0 would be accepted which would lead to a divide by 0 later on. I would do:

if (chip->has_hardware_gain) {
	ret = device_property_read_u16(...)
	if (!ret) {
		/* validate here for a proper value /*
	}
}

You can also check for ret < 0 and -EINVAL to detect an invalid devicetree parameter instead of completely ignoring return codes (but for non mandatory properties one typically does not care much - up to you)

- Nuno Sá

maintainer
#21 lore raw

Re: (subset) [PATCH v6 0/7] Add support for AD4000 series of ADCs

In reply to Marcelo Schmitt
Quoted 9 lines · Marcelo Schmitt

This patch series extends the SPI bitbang, gpio, and spi-engine controllers to support configurable MOSI line idle states.
It then introduces the ad4000 driver which uses the MOSI idle configuration to provide improved support for the AD4000 series of ADCs. Documentation is added describing the new extension to the SPI protocol. The currently supported wiring modes for AD4000 devices were documented under IIO documentation directory.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/7] spi: Enable controllers to extend the SPI protocol with MOSI idle configuration

      commit: f58872f45c36ded048bccc22701b0986019c24d8
[2/7] spi: bitbang: Implement support for MOSI idle state configuration
      commit: 320f6693097bf89d67f9cabad24a2b911e23073f
[3/7] spi: spi-gpio: Add support for MOSI idle state configuration
      commit: 927d382c7efbcc2206c31fa2f672fa264c0f1d5b
[4/7] spi: spi-axi-spi-engine: Add support for MOSI idle configuration
      commit: a62073f4b2164028fc7c5ae45ceba10c9326cd91

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark

maintainer
#22 lore raw

Re: (subset) [PATCH v6 0/7] Add support for AD4000 series of ADCs

Quoted 17 lines · On Mon, 29 Jul 2024 19:02:23 +0100 Mark Brown <[email protected]> wrote:
Quoted 9 lines · Marcelo Schmitt

This patch series extends the SPI bitbang, gpio, and spi-engine controllers to support configurable MOSI line idle states.
It then introduces the ad4000 driver which uses the MOSI idle configuration to provide improved support for the AD4000 series of ADCs. Documentation is added describing the new extension to the SPI protocol. The currently supported wiring modes for AD4000 devices were documented under IIO documentation directory.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

Quoted 8 lines

[1/7] spi: Enable controllers to extend the SPI protocol with MOSI idle configuration

      commit: f58872f45c36ded048bccc22701b0986019c24d8
[2/7] spi: bitbang: Implement support for MOSI idle state configuration
      commit: 320f6693097bf89d67f9cabad24a2b911e23073f
[3/7] spi: spi-gpio: Add support for MOSI idle state configuration
      commit: 927d382c7efbcc2206c31fa2f672fa264c0f1d5b
[4/7] spi: spi-axi-spi-engine: Add support for MOSI idle configuration
      commit: a62073f4b2164028fc7c5ae45ceba10c9326cd91

Hi Mark,

Any chance of a tag + you seem to have also picked up the ADC dt binding. Patch 5/7. dt-bindings: iio: adc: Add AD4000
which I'm assuming was not intentional.

I think I only need the definition of SPI_MOSI_IDLE_HIGH for 5-7 to build fine.

If needed, I can use a local value for that in the driver and we can follow up with a patch using the main define once the trees come together upstream.

Jonathan

Quoted 20 lines

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark