From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f42.google.com (mail-oo1-f42.google.com [209.85.161.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D48319D090 for ; Fri, 7 Feb 2025 20:09:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958967; cv=none; b=iASIjZteqEGZZsuoa130OZpi5U6MBcvQ4blWQKjCJ1xI70/0jNIuooi5PFQ1KgUmJfADaLcyNc8sP+V7CgYgiAkZSrS+X7bSCkMDn6NGx6nZRHXF96Yw/pq7zsPkW9wXhxJt4nswYfAu3oDvYQlPT4EjDh/ecOs1kKXfFo8bMDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958967; c=relaxed/simple; bh=DexCFkIwb7kCSU7k+T5cJi4HF/AtPekUbcbblQWUBck=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=o/isergl4qeMiI3rdAGx2ZWRtj/ktKqCRGwAZ/g+68SP6BBfUsxTwK6zjZMP0GhVlBdFNwqU8Pghz1OMSjY4Hs1nPdnm59x5WoU7mqBjLpDDB3ZhNqJhUZ5XjtgKu1rAmKyYj+5bOiWoRgYWxZg6Iz/JMdzMlYIeIfryE5bE/lY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=EzeE5Wt6; arc=none smtp.client-ip=209.85.161.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="EzeE5Wt6" Received: by mail-oo1-f42.google.com with SMTP id 006d021491bc7-5fa22d0b88fso1483769eaf.1 for ; Fri, 07 Feb 2025 12:09:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958964; x=1739563764; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NSiA57XtGHi5YPHg4AJxsYI7A3iQnygs8wmimjPUAuY=; b=EzeE5Wt6Wu13eHBRmqBf7vOhBQ/iytxwBziTFHYOmKHTctt3ZPiyzyS7cs2wJM2CTW 2dWTmySrKuvAxQjywYMJfBQ8wj7QXrN4oPZ8sWovpm5KYK2KV4RLQ5bGK8mtnoCm3j/n CqCXLDdd3C8R5Ql7YmEMfdiar9YBsTT2hTiVJvAmRdD8HGDJZlbPyNH3iSBJiDcqYC+o +rBHp38MvTA2U/nGrx7Y59zCP5gbs1WwJijwEPLQUAakjzNRUGZFhRdj0/uE5Aibk2wC HfW/HvDyRdiki23ssHdD8bfXwmNFS/1e6cKe/C95r/K4H23P2VpsJeJSr36tkHkXOKYb wKkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958964; x=1739563764; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NSiA57XtGHi5YPHg4AJxsYI7A3iQnygs8wmimjPUAuY=; b=gC8dxQ+6XyOijitNVdETrez2HWwst+03YY7Z4BD6XrJYCdDLA2uxMVBo71BggMQd7R kLUtwdDmGOf0TPWgPMV1xBeFz6nVUJi9fyEcTAYOqymBJSROC858cvYSsgykK9iwp82K O+2ctRJAnHzOAz2YtcoZc+eleg+DljpCb0PSzJ8JD6K8CyiXrCTO43WyH1JpeayDyHUE cv4Af126OvWoHvgSdbGn92BxIIhLa1GFEXJ8oQYPfKBwTNr6wE3rZ8CARAS1xkQ9VfAt niQvc98Xs5lHa9aBrPlh+nnPlNYPsafu4AQLphslFrvcgmvvdyNU16IyPhsBB0Z7lB+6 OXNQ== X-Forwarded-Encrypted: i=1; AJvYcCWtrxEImPuVkBqXuNOEIs52/h1Dir15oNeLcmzzsZ+SDQEaqsQgQyG/h9q5Vv5wFayEgcNS7eFC0Mw/w/M=@vger.kernel.org X-Gm-Message-State: AOJu0YzdoOhgUo170zjjcKn6Z1N7KkUBZFCulLzFKcHzyNzKJj26JaCw Wkwj+GeL136aTINMWJhGWED4NeUtp3vmRpj0yCxksTFeqONHA+uXcdLR/7Q/I2M= X-Gm-Gg: ASbGnctJ3nT09XpLXEhrNBwHttZg9oM5RnP6dwOh6uHaDulwzOLx1uhS93kngH8oPZq vHcRzei6aRjUDYGOyUNQMbSbLouTx50SyqQiyF8Ou2rjLBzkaC0SofEITBxZEfelcmEJrSBprpC rSiBk78q8n34wZvF7tyY546At7OYRyu3Q87KSFFFXFFQTYzfipV5x9YbaL1Zy8O3y62ZgOd03S3 /GvtwnTwMLL0gnJNaJflPMetHNUbAvdym5iHQfigvkN9WX/UeV3eeX1TI4+X4zh2TYZrtQknqv7 VunqwM2fqtnxQ/sF2/nc1l4lhE+Le3OdGJ39gnzPPJPJT6M= X-Google-Smtp-Source: AGHT+IE7KQ3cuNJVemjQC2JwAlq9Av5fmoKVdn2T75eGuUT7pUNjksv50Biph0SGiEMEvxWKE+hX+Q== X-Received: by 2002:a05:6820:80a:b0:5fa:7bdc:e86 with SMTP id 006d021491bc7-5fc5e74bd59mr2870122eaf.8.1738958964432; Fri, 07 Feb 2025 12:09:24 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:23 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:08:58 -0600 Subject: [PATCH v8 01/17] spi: add basic support for SPI offloading Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-1-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add the basic infrastructure to support SPI offload providers and consumers. SPI offloading is a feature that allows the SPI controller to perform transfers without any CPU intervention. This is useful, e.g. for high-speed data acquisition. SPI controllers with offload support need to implement the get_offload and put_offload callbacks and can use the devm_spi_offload_alloc() to allocate offload instances. SPI peripheral drivers will call devm_spi_offload_get() to get a reference to the matching offload instance. This offload instance can then be attached to a SPI message to request offloading that message. It is expected that SPI controllers with offload support will check for the offload instance in the SPI message in the ctlr->optimize_message() callback and handle it accordingly. CONFIG_SPI_OFFLOAD is intended to be a select-only option. Both consumer and provider drivers should `select SPI_OFFLOAD` in their Kconfig to ensure that the SPI core is built with offload support. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Drop use of PTR_ERR_OR_ZERO(). * Split header into types.h/provider.h/consumer.h. * Remove unused spi_controller_offload_ops forward declaration. v5 changes: * Don't include linux/property.h (moved to later patch). * Only allocate single offload instance instead of array. * Allocate *priv separately to avoid alignment issues. * Add put_offload() callback instead of assuming devm semantics. * Drop struct spi_offload::spi. It was only being used as a flag. * Don't get/put struct spi_offload::provider_dev. * Add MAINTAINERS entry for me as reviewer for anything related to SPI offload. v4 changes: * SPI offload functions moved to a separate file instead of spi.c (spi.c is already too long). * struct spi_offload and devm_spi_offload_get() are back, similar to but improved over v1. This avoids having to pass the function ID string to every function call and re-lookup the offload instance. * offload message prepare/unprepare functions are removed. Instead the existing optimize/unoptimize functions should be used. Setting spi_message::offload pointer is used as a flag to differentiate between an offloaded message and a regular message. v3 changes: * Minor changes to doc comments. * Changed to use phandle array for spi-offloads. * Changed id to string to make use of spi-offload-names. v2 changes: * This is a rework of "spi: add core support for controllers with offload capabilities" from v1. * The spi_offload_get() function that Nuno didn't like is gone. Instead, there is now a mapping callback that uses the new generic devicetree binding to request resources automatically when a SPI device is probed. * The spi_offload_enable/disable() functions for dealing with hardware triggers are deferred to a separate patch. * This leaves adding spi_offload_prepare/unprepare() which have been reworked to be a bit more robust. --- MAINTAINERS | 6 ++ drivers/spi/Kconfig | 3 + drivers/spi/Makefile | 1 + drivers/spi/spi-offload.c | 114 +++++++++++++++++++++++++++++++= ++++ include/linux/spi/offload/consumer.h | 22 +++++++ include/linux/spi/offload/provider.h | 19 ++++++ include/linux/spi/offload/types.h | 43 +++++++++++++ include/linux/spi/spi.h | 17 ++++++ 8 files changed, 225 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index e4f5d8f6858170e63339aaa3380c3845cc08ab84..c37504e2e19f067b9835a28708d= fd1d25700a608 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22342,6 +22342,12 @@ F: Documentation/devicetree/bindings/mtd/jedec,spi= -nor.yaml F: drivers/mtd/spi-nor/ F: include/linux/mtd/spi-nor.h =20 +SPI OFFLOAD +R: David Lechner +F: drivers/spi/spi-offload.c +F: include/linux/spi/spi-offload.h +K: spi_offload + SPI SUBSYSTEM M: Mark Brown L: linux-spi@vger.kernel.org diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index ea8a310329274bb2701e265cd152a56fb4e0f3a7..02064a4e292815ec0213e2e446b= 4f90ed8855a52 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -55,6 +55,9 @@ config SPI_MEM This extension is meant to simplify interaction with SPI memories by providing a high-level interface to send memory-like commands. =20 +config SPI_OFFLOAD + bool + comment "SPI Master Controller Drivers" =20 config SPI_AIROHA_SNFI diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 9db7554c1864bf9b37dcf59c16eb76f5af03a7e8..bb5fc20df21332232533c2e70c0= cc230f6bcf27f 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -10,6 +10,7 @@ ccflags-$(CONFIG_SPI_DEBUG) :=3D -DDEBUG obj-$(CONFIG_SPI_MASTER) +=3D spi.o obj-$(CONFIG_SPI_MEM) +=3D spi-mem.o obj-$(CONFIG_SPI_MUX) +=3D spi-mux.o +obj-$(CONFIG_SPI_OFFLOAD) +=3D spi-offload.o obj-$(CONFIG_SPI_SPIDEV) +=3D spidev.o obj-$(CONFIG_SPI_LOOPBACK_TEST) +=3D spi-loopback-test.o =20 diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c new file mode 100644 index 0000000000000000000000000000000000000000..3a40ef30debf09c6fd7b2c14526= f3e5976e2b21f --- /dev/null +++ b/drivers/spi/spi-offload.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 Analog Devices Inc. + * Copyright (C) 2024 BayLibre, SAS + */ + +/* + * SPI Offloading support. + * + * Some SPI controllers support offloading of SPI transfers. Essentially, = this + * is the ability for a SPI controller to perform SPI transfers with minim= al + * or even no CPU intervention, e.g. via a specialized SPI controller with= a + * hardware trigger or via a conventional SPI controller using a non-Linux= MCU + * processor core to offload the work. + */ + +#define DEFAULT_SYMBOL_NAMESPACE "SPI_OFFLOAD" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct spi_controller_and_offload { + struct spi_controller *controller; + struct spi_offload *offload; +}; + +/** + * devm_spi_offload_alloc() - Allocate offload instance + * @dev: Device for devm purposes and assigned to &struct spi_offload.prov= ider_dev + * @priv_size: Size of private data to allocate + * + * Offload providers should use this to allocate offload instances. + * + * Return: Pointer to new offload instance or error on failure. + */ +struct spi_offload *devm_spi_offload_alloc(struct device *dev, + size_t priv_size) +{ + struct spi_offload *offload; + void *priv; + + offload =3D devm_kzalloc(dev, sizeof(*offload), GFP_KERNEL); + if (!offload) + return ERR_PTR(-ENOMEM); + + priv =3D devm_kzalloc(dev, priv_size, GFP_KERNEL); + if (!priv) + return ERR_PTR(-ENOMEM); + + offload->provider_dev =3D dev; + offload->priv =3D priv; + + return offload; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_alloc); + +static void spi_offload_put(void *data) +{ + struct spi_controller_and_offload *resource =3D data; + + resource->controller->put_offload(resource->offload); + kfree(resource); +} + +/** + * devm_spi_offload_get() - Get an offload instance + * @dev: Device for devm purposes + * @spi: SPI device to use for the transfers + * @config: Offload configuration + * + * Peripheral drivers call this function to get an offload instance that m= eets + * the requirements specified in @config. If no suitable offload instance = is + * available, -ENODEV is returned. + * + * Return: Offload instance or error on failure. + */ +struct spi_offload *devm_spi_offload_get(struct device *dev, + struct spi_device *spi, + const struct spi_offload_config *config) +{ + struct spi_controller_and_offload *resource; + int ret; + + if (!spi || !config) + return ERR_PTR(-EINVAL); + + if (!spi->controller->get_offload) + return ERR_PTR(-ENODEV); + + resource =3D kzalloc(sizeof(*resource), GFP_KERNEL); + if (!resource) + return ERR_PTR(-ENOMEM); + + resource->controller =3D spi->controller; + resource->offload =3D spi->controller->get_offload(spi, config); + if (IS_ERR(resource->offload)) { + kfree(resource); + return resource->offload; + } + + ret =3D devm_add_action_or_reset(dev, spi_offload_put, resource); + if (ret) + return ERR_PTR(ret); + + return resource->offload; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_get); diff --git a/include/linux/spi/offload/consumer.h b/include/linux/spi/offlo= ad/consumer.h new file mode 100644 index 0000000000000000000000000000000000000000..05543dbedf3086fb4befcd149cf= f3c8c70a88825 --- /dev/null +++ b/include/linux/spi/offload/consumer.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2024 Analog Devices Inc. + * Copyright (C) 2024 BayLibre, SAS + */ + +#ifndef __LINUX_SPI_OFFLOAD_CONSUMER_H +#define __LINUX_SPI_OFFLOAD_CONSUMER_H + +#include +#include +#include + +MODULE_IMPORT_NS("SPI_OFFLOAD"); + +struct device; +struct spi_device; + +struct spi_offload *devm_spi_offload_get(struct device *dev, struct spi_de= vice *spi, + const struct spi_offload_config *config); + +#endif /* __LINUX_SPI_OFFLOAD_CONSUMER_H */ diff --git a/include/linux/spi/offload/provider.h b/include/linux/spi/offlo= ad/provider.h new file mode 100644 index 0000000000000000000000000000000000000000..278c4edfcdb7b1f43870ca99d2b= a252bf2820576 --- /dev/null +++ b/include/linux/spi/offload/provider.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2024 Analog Devices Inc. + * Copyright (C) 2024 BayLibre, SAS + */ + +#ifndef __LINUX_SPI_OFFLOAD_PROVIDER_H +#define __LINUX_SPI_OFFLOAD_PROVIDER_H + +#include +#include + +MODULE_IMPORT_NS("SPI_OFFLOAD"); + +struct device; + +struct spi_offload *devm_spi_offload_alloc(struct device *dev, size_t priv= _size); + +#endif /* __LINUX_SPI_OFFLOAD_PROVIDER_H */ diff --git a/include/linux/spi/offload/types.h b/include/linux/spi/offload/= types.h new file mode 100644 index 0000000000000000000000000000000000000000..a74f8d84541b10062353e81a638= f05628b696394 --- /dev/null +++ b/include/linux/spi/offload/types.h @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2024 Analog Devices Inc. + * Copyright (C) 2024 BayLibre, SAS + */ + +#ifndef __LINUX_SPI_OFFLOAD_TYPES_H +#define __LINUX_SPI_OFFLOAD_TYPES_H + +#include + +struct device; + +/* Offload can be triggered by external hardware event. */ +#define SPI_OFFLOAD_CAP_TRIGGER BIT(0) +/* Offload can record and then play back TX data when triggered. */ +#define SPI_OFFLOAD_CAP_TX_STATIC_DATA BIT(1) +/* Offload can get TX data from an external stream source. */ +#define SPI_OFFLOAD_CAP_TX_STREAM_DMA BIT(2) +/* Offload can send RX data to an external stream sink. */ +#define SPI_OFFLOAD_CAP_RX_STREAM_DMA BIT(3) + +/** + * struct spi_offload_config - offload configuration + * + * This is used to request an offload with specific configuration. + */ +struct spi_offload_config { + /** @capability_flags: required capabilities. See %SPI_OFFLOAD_CAP_* */ + u32 capability_flags; +}; + +/** + * struct spi_offload - offload instance + */ +struct spi_offload { + /** @provider_dev: for get/put reference counting */ + struct device *provider_dev; + /** @priv: provider driver private data */ + void *priv; +}; + +#endif /* __LINUX_SPI_OFFLOAD_TYPES_H */ diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 8497f4747e24d4ecd85b74f49609ac1c82c73535..98bdc8c16c20521c0a94e5f72f5= e71c4f6d7d11e 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -31,6 +31,8 @@ struct spi_transfer; struct spi_controller_mem_ops; struct spi_controller_mem_caps; struct spi_message; +struct spi_offload; +struct spi_offload_config; =20 /* * INTERFACES between SPI master-side drivers and SPI slave protocol handl= ers, @@ -496,6 +498,10 @@ extern struct spi_device *spi_new_ancillary_device(str= uct spi_device *spi, u8 ch * @mem_ops: optimized/dedicated operations for interactions with SPI memo= ry. * This field is optional and should only be implemented if the * controller has native support for memory like operations. + * @get_offload: callback for controllers with offload support to get matc= hing + * offload instance. Implementations should return -ENODEV if no match is + * found. + * @put_offload: release the offload instance acquired by @get_offload. * @mem_caps: controller capabilities for the handling of memory operation= s. * @unprepare_message: undo any work done by prepare_message(). * @target_abort: abort the ongoing transfer request on an SPI target cont= roller @@ -740,6 +746,10 @@ struct spi_controller { const struct spi_controller_mem_ops *mem_ops; const struct spi_controller_mem_caps *mem_caps; =20 + struct spi_offload *(*get_offload)(struct spi_device *spi, + const struct spi_offload_config *config); + void (*put_offload)(struct spi_offload *offload); + /* GPIO chip select */ struct gpio_desc **cs_gpiods; bool use_gpio_descriptors; @@ -1108,6 +1118,7 @@ struct spi_transfer { * @state: for use by whichever driver currently owns the message * @opt_state: for use by whichever driver currently owns the message * @resources: for resource management when the SPI message is processed + * @offload: (optional) offload instance used by this message * * A @spi_message is used to execute an atomic sequence of data transfers, * each represented by a struct spi_transfer. The sequence is "atomic" @@ -1168,6 +1179,12 @@ struct spi_message { */ void *opt_state; =20 + /* + * Optional offload instance used by this message. This must be set + * by the peripheral driver before calling spi_optimize_message(). + */ + struct spi_offload *offload; + /* List of spi_res resources when the SPI message is processed */ struct list_head resources; }; --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 455A71A238E for ; Fri, 7 Feb 2025 20:09:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958971; cv=none; b=LOBnYNWYdTxE65vvkFNtYR2UzacBpBPsUYSDxTKFOTwnK0zmynbBU3n7DV/Li63bLUMZBstvR//LVeMBbexMxBnD7UjIhXeSRv6S9psEt4QBUq3hdmOGrPWDL4gYZZl9zN9EBaLGVx8OvZTQVpQyOdfJbI93IgFY7h5TDPdqThI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958971; c=relaxed/simple; bh=queoE3Dg5nPxxqFXS6WHbnUhCzibnN4tklRV8ZsWt+8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XxVwDLFByelzJ6qzW8kOzhyFuOeTPjXC9gvZ+AtEcU7poPGP1otMFB988nJOl4wE+oH38E42HwlqgRAkjm0Qb3Sk2jhcFB7BNR57ydCy/IM5Inuaxqi5LeEEKx5LYDs5C31d9Lb14D7LT3ChZ5XuyTQxfADNTj5cmk2QahOh5Ek= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=1a2IEXTM; arc=none smtp.client-ip=209.85.210.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="1a2IEXTM" Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-71e2a32297dso1327347a34.0 for ; Fri, 07 Feb 2025 12:09:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958967; x=1739563767; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=bWygsU2wJJALZjruh3DSmbdGzB4EfqJmJ4z20++2StM=; b=1a2IEXTM2DDBrcDjV0KXsSJ0kcJi+iqeE86xxj2LYpB13Cvow3MOth4DaSuM0Io/Lu yshOMWkM606gjLTCdsCFGCvNcGD45sgmaQzFervhr+NyvjzelwfZQnDcBZR+7symnFdi rJs0Nq8sunAfN7aNyTsD9kPxopRfaniyw4cwBB0RXo6Xcxs5vYeKRjWuwpbUVwhhzLuM 67ZZFbwUoku4s+FJMZgkuB/Xi/bxzhqpehk+ADcZLyvF4ocakRpO0adsjfe6PKjvfLaa IrFP42RR7d1YkdQeAn3n4o1L19q/tHm295s+LF5UR0onNVV1JNbLyMTFxSeIncSO3iKJ GZuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958967; x=1739563767; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bWygsU2wJJALZjruh3DSmbdGzB4EfqJmJ4z20++2StM=; b=Mh0mctDfPozGC002mG1wwi1GbyFyfZtTyaA3iqoHG2X/dfLL6Bx+NmS2mCaM7Spu2W K9TgwroQX+1zS98EHmWtmPaADY4qaU4i/PY1y4cfvQVBfxyd7L6LQVOGIw74ZdOBOv78 wDCiSr8cA+6ZltJubYOZDdm1gtOOJu//llTzt1GNibD7BTouymr/1rBDFkeRQ6/+rPoG oLwSwcHkSHn8GhO8yi6GkKu+x3T+E7+O+MYM7hq22S6kF/KXAesBwG25qvPqDSJk7LpY TaYfb8LlCaXXdjO43CPs7kk20lRlfcta+1NgjluGqllslO6AAysYqD8AkxKPWaPoQ/d6 urwg== X-Forwarded-Encrypted: i=1; AJvYcCVv9UvVV2DzFleNWsC/7p4bBoUcB38IGrLs8PPxaXs7rsu6EB6WsLlc03N0GIG5bZL98eHnimUIIWPxrQg=@vger.kernel.org X-Gm-Message-State: AOJu0YzdWE6FaS+utsbagt2JvEJfM9XRFw7EZbqZJfoOnfixLcV3JPWv AezCYHWlOQoq4nsBcVKmALcN2b1g3+xlfY3eeV2InPvU5NsKDC1+6JVWE1iI4cA= X-Gm-Gg: ASbGncsO7wYeEdqMBYUKomdA5KTUrkJIDQcnnWbQix9MnfsMNJw2qfEqGKUI0+yfm79 b3pYVJeNGVZG0Gcf+O0KEiluN+Wo2hPDAfJ7JSUY38Hqe8WTvHLs0W1E8fAFEzhUv2Q0HD/MgRF MgrullXgLgFh+AlEUutl3kLVgRYJ4tFujRqioc97L71XvaKUt1rFh9IeqVZNN5rzJcKXuVqEnSN m4DjdlI5WEzGXCMqatG1myFyRmbPYW+JsgYjhyfWSicHy+fVbYPnNeCjR2EQLh2KGzpfwZNW0T/ i6P1quDpQRYGs2BXZoWbUvH8Qf/XK+tbvdFzRicxM8m32ho= X-Google-Smtp-Source: AGHT+IHXmGd6P5OzEUVOtcczBvRH6yGO2aay3wQFFijSISU7plmX1taXlchQWX8jbUF1v+SDK/g0HQ== X-Received: by 2002:a9d:748e:0:b0:71d:e5e2:c648 with SMTP id 46e09a7af769-726ad665eabmr5381854a34.1.1738958967241; Fri, 07 Feb 2025 12:09:27 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:25 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:08:59 -0600 Subject: [PATCH v8 02/17] spi: offload: add support for hardware triggers Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-2-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Extend SPI offloading to support hardware triggers. This allows an arbitrary hardware trigger to be used to start a SPI transfer that was previously set up with spi_optimize_message(). A new struct spi_offload_trigger is introduced that can be used to configure any type of trigger. It has a type discriminator and a union to allow it to be extended in the future. Two trigger types are defined to start with. One is a trigger that indicates that the SPI peripheral is ready to read or write data. The other is a periodic trigger to repeat a SPI message at a fixed rate. There is also a spi_offload_hw_trigger_validate() function that works similar to clk_round_rate(). It basically asks the question of if we enabled the hardware trigger what would the actual parameters be. This can be used to test if the requested trigger type is actually supported by the hardware and for periodic triggers, it can be used to find the actual rate that the hardware is capable of. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Updated for header file split. v5 changes: * Use struct kref instead of struct dev for trigger lifetime management. * Don't use __free() for args.fwnode. * Pass *trigger instead of *priv to all callbacks. * Add new *spi_offload_trigger_get_priv() function. * Use ops instead of priv for "provider is gone" flag. * Combine devm_spi_offload_trigger_alloc() and devm_spi_offload_trigger_register() into one function. * Add kernel-doc comments for public functions. v4 changes: * Added new struct spi_offload_trigger that is a generic struct for any hardware trigger rather than returning a struct clk. * Added new spi_offload_hw_trigger_validate() function. * Dropped extra locking since it was too restrictive. v3 changes: * renamed enable/disable functions to spi_offload_hw_trigger_*mode*_... * added spi_offload_hw_trigger_get_clk() function * fixed missing EXPORT_SYMBOL_GPL v2 changes: * This is split out from "spi: add core support for controllers with offload capabilities". * Added locking for offload trigger to claim exclusive use of the SPI bus. --- drivers/spi/spi-offload.c | 281 +++++++++++++++++++++++++++++++= ++++ include/linux/spi/offload/consumer.h | 12 ++ include/linux/spi/offload/provider.h | 28 ++++ include/linux/spi/offload/types.h | 37 +++++ 4 files changed, 358 insertions(+) diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c index 3a40ef30debf09c6fd7b2c14526f3e5976e2b21f..43582e50e279c4b1b958765fec5= 56aaa91180e55 100644 --- a/drivers/spi/spi-offload.c +++ b/drivers/spi/spi-offload.c @@ -19,7 +19,11 @@ #include #include #include +#include +#include #include +#include +#include #include #include #include @@ -31,6 +35,23 @@ struct spi_controller_and_offload { struct spi_offload *offload; }; =20 +struct spi_offload_trigger { + struct list_head list; + struct kref ref; + struct fwnode_handle *fwnode; + /* synchronizes calling ops and driver registration */ + struct mutex lock; + /* + * If the provider goes away while the consumer still has a reference, + * ops and priv will be set to NULL and all calls will fail with -ENODEV. + */ + const struct spi_offload_trigger_ops *ops; + void *priv; +}; + +static LIST_HEAD(spi_offload_triggers); +static DEFINE_MUTEX(spi_offload_triggers_lock); + /** * devm_spi_offload_alloc() - Allocate offload instance * @dev: Device for devm purposes and assigned to &struct spi_offload.prov= ider_dev @@ -112,3 +133,263 @@ struct spi_offload *devm_spi_offload_get(struct devic= e *dev, return resource->offload; } EXPORT_SYMBOL_GPL(devm_spi_offload_get); + +static void spi_offload_trigger_free(struct kref *ref) +{ + struct spi_offload_trigger *trigger =3D + container_of(ref, struct spi_offload_trigger, ref); + + mutex_destroy(&trigger->lock); + fwnode_handle_put(trigger->fwnode); + kfree(trigger); +} + +static void spi_offload_trigger_put(void *data) +{ + struct spi_offload_trigger *trigger =3D data; + + scoped_guard(mutex, &trigger->lock) + if (trigger->ops && trigger->ops->release) + trigger->ops->release(trigger); + + kref_put(&trigger->ref, spi_offload_trigger_free); +} + +static struct spi_offload_trigger +*spi_offload_trigger_get(enum spi_offload_trigger_type type, + struct fwnode_reference_args *args) +{ + struct spi_offload_trigger *trigger; + bool match =3D false; + int ret; + + guard(mutex)(&spi_offload_triggers_lock); + + list_for_each_entry(trigger, &spi_offload_triggers, list) { + if (trigger->fwnode !=3D args->fwnode) + continue; + + match =3D trigger->ops->match(trigger, type, args->args, args->nargs); + if (match) + break; + } + + if (!match) + return ERR_PTR(-EPROBE_DEFER); + + guard(mutex)(&trigger->lock); + + if (!trigger->ops) + return ERR_PTR(-ENODEV); + + if (trigger->ops->request) { + ret =3D trigger->ops->request(trigger, type, args->args, args->nargs); + if (ret) + return ERR_PTR(ret); + } + + kref_get(&trigger->ref); + + return trigger; +} + +/** + * devm_spi_offload_trigger_get() - Get an offload trigger instance + * @dev: Device for devm purposes. + * @offload: Offload instance connected to a trigger. + * @type: Trigger type to get. + * + * Return: Offload trigger instance or error on failure. + */ +struct spi_offload_trigger +*devm_spi_offload_trigger_get(struct device *dev, + struct spi_offload *offload, + enum spi_offload_trigger_type type) +{ + struct spi_offload_trigger *trigger; + struct fwnode_reference_args args; + int ret; + + ret =3D fwnode_property_get_reference_args(dev_fwnode(offload->provider_d= ev), + "trigger-sources", + "#trigger-source-cells", 0, 0, + &args); + if (ret) + return ERR_PTR(ret); + + trigger =3D spi_offload_trigger_get(type, &args); + fwnode_handle_put(args.fwnode); + if (IS_ERR(trigger)) + return trigger; + + ret =3D devm_add_action_or_reset(dev, spi_offload_trigger_put, trigger); + if (ret) + return ERR_PTR(ret); + + return trigger; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_trigger_get); + +/** + * spi_offload_trigger_validate - Validate the requested trigger + * @trigger: Offload trigger instance + * @config: Trigger config to validate + * + * On success, @config may be modifed to reflect what the hardware can do. + * For example, the frequency of a periodic trigger may be adjusted to the + * nearest supported value. + * + * Callers will likely need to do additional validation of the modified tr= igger + * parameters. + * + * Return: 0 on success, negative error code on failure. + */ +int spi_offload_trigger_validate(struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config) +{ + guard(mutex)(&trigger->lock); + + if (!trigger->ops) + return -ENODEV; + + if (!trigger->ops->validate) + return -EOPNOTSUPP; + + return trigger->ops->validate(trigger, config); +} +EXPORT_SYMBOL_GPL(spi_offload_trigger_validate); + +/** + * spi_offload_trigger_enable - enables trigger for offload + * @offload: Offload instance + * @trigger: Offload trigger instance + * @config: Trigger config to validate + * + * There must be a prepared offload instance with the specified ID (i.e. + * spi_optimize_message() was called with the same offload assigned to the + * message). This will also reserve the bus for exclusive use by the offlo= ad + * instance until the trigger is disabled. Any other attempts to send a + * transfer or lock the bus will fail with -EBUSY during this time. + * + * Calls must be balanced with spi_offload_trigger_disable(). + * + * Context: can sleep + * Return: 0 on success, else a negative error code. + */ +int spi_offload_trigger_enable(struct spi_offload *offload, + struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config) +{ + int ret; + + guard(mutex)(&trigger->lock); + + if (!trigger->ops) + return -ENODEV; + + if (offload->ops && offload->ops->trigger_enable) { + ret =3D offload->ops->trigger_enable(offload); + if (ret) + return ret; + } + + if (trigger->ops->enable) { + ret =3D trigger->ops->enable(trigger, config); + if (ret) { + if (offload->ops->trigger_disable) + offload->ops->trigger_disable(offload); + return ret; + } + } + + return 0; +} +EXPORT_SYMBOL_GPL(spi_offload_trigger_enable); + +/** + * spi_offload_trigger_disable - disables hardware trigger for offload + * @offload: Offload instance + * @trigger: Offload trigger instance + * + * Disables the hardware trigger for the offload instance with the specifi= ed ID + * and releases the bus for use by other clients. + * + * Context: can sleep + */ +void spi_offload_trigger_disable(struct spi_offload *offload, + struct spi_offload_trigger *trigger) +{ + if (offload->ops && offload->ops->trigger_disable) + offload->ops->trigger_disable(offload); + + guard(mutex)(&trigger->lock); + + if (!trigger->ops) + return; + + if (trigger->ops->disable) + trigger->ops->disable(trigger); +} +EXPORT_SYMBOL_GPL(spi_offload_trigger_disable); + +/* Triggers providers */ + +static void spi_offload_trigger_unregister(void *data) +{ + struct spi_offload_trigger *trigger =3D data; + + scoped_guard(mutex, &spi_offload_triggers_lock) + list_del(&trigger->list); + + scoped_guard(mutex, &trigger->lock) { + trigger->priv =3D NULL; + trigger->ops =3D NULL; + } + + kref_put(&trigger->ref, spi_offload_trigger_free); +} + +/** + * devm_spi_offload_trigger_register() - Allocate and register an offload = trigger + * @dev: Device for devm purposes. + * @info: Provider-specific trigger info. + * + * Return: 0 on success, else a negative error code. + */ +int devm_spi_offload_trigger_register(struct device *dev, + struct spi_offload_trigger_info *info) +{ + struct spi_offload_trigger *trigger; + + if (!info->fwnode || !info->ops) + return -EINVAL; + + trigger =3D kzalloc(sizeof(*trigger), GFP_KERNEL); + if (!trigger) + return -ENOMEM; + + kref_init(&trigger->ref); + mutex_init(&trigger->lock); + trigger->fwnode =3D fwnode_handle_get(info->fwnode); + trigger->ops =3D info->ops; + trigger->priv =3D info->priv; + + scoped_guard(mutex, &spi_offload_triggers_lock) + list_add_tail(&trigger->list, &spi_offload_triggers); + + return devm_add_action_or_reset(dev, spi_offload_trigger_unregister, trig= ger); +} +EXPORT_SYMBOL_GPL(devm_spi_offload_trigger_register); + +/** + * spi_offload_trigger_get_priv() - Get the private data for the trigger + * + * @trigger: Offload trigger instance. + * + * Return: Private data for the trigger. + */ +void *spi_offload_trigger_get_priv(struct spi_offload_trigger *trigger) +{ + return trigger->priv; +} +EXPORT_SYMBOL_GPL(spi_offload_trigger_get_priv); diff --git a/include/linux/spi/offload/consumer.h b/include/linux/spi/offlo= ad/consumer.h index 05543dbedf3086fb4befcd149cff3c8c70a88825..5a0ec5303d600728959594bcdbd= 0cb2baeba7c77 100644 --- a/include/linux/spi/offload/consumer.h +++ b/include/linux/spi/offload/consumer.h @@ -19,4 +19,16 @@ struct spi_device; struct spi_offload *devm_spi_offload_get(struct device *dev, struct spi_de= vice *spi, const struct spi_offload_config *config); =20 +struct spi_offload_trigger +*devm_spi_offload_trigger_get(struct device *dev, + struct spi_offload *offload, + enum spi_offload_trigger_type type); +int spi_offload_trigger_validate(struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config); +int spi_offload_trigger_enable(struct spi_offload *offload, + struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config); +void spi_offload_trigger_disable(struct spi_offload *offload, + struct spi_offload_trigger *trigger); + #endif /* __LINUX_SPI_OFFLOAD_CONSUMER_H */ diff --git a/include/linux/spi/offload/provider.h b/include/linux/spi/offlo= ad/provider.h index 278c4edfcdb7b1f43870ca99d2ba252bf2820576..76c7cf65109241f39c6ab72f0e1= 5454a874b6c24 100644 --- a/include/linux/spi/offload/provider.h +++ b/include/linux/spi/offload/provider.h @@ -8,12 +8,40 @@ #define __LINUX_SPI_OFFLOAD_PROVIDER_H =20 #include +#include #include =20 MODULE_IMPORT_NS("SPI_OFFLOAD"); =20 struct device; +struct spi_offload_trigger; =20 struct spi_offload *devm_spi_offload_alloc(struct device *dev, size_t priv= _size); =20 +struct spi_offload_trigger_ops { + bool (*match)(struct spi_offload_trigger *trigger, + enum spi_offload_trigger_type type, u64 *args, u32 nargs); + int (*request)(struct spi_offload_trigger *trigger, + enum spi_offload_trigger_type type, u64 *args, u32 nargs); + void (*release)(struct spi_offload_trigger *trigger); + int (*validate)(struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config); + int (*enable)(struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config); + void (*disable)(struct spi_offload_trigger *trigger); +}; + +struct spi_offload_trigger_info { + /** @fwnode: Provider fwnode, used to match to consumer. */ + struct fwnode_handle *fwnode; + /** @ops: Provider-specific callbacks. */ + const struct spi_offload_trigger_ops *ops; + /** Provider-specific state to be used in callbacks. */ + void *priv; +}; + +int devm_spi_offload_trigger_register(struct device *dev, + struct spi_offload_trigger_info *info); +void *spi_offload_trigger_get_priv(struct spi_offload_trigger *trigger); + #endif /* __LINUX_SPI_OFFLOAD_PROVIDER_H */ diff --git a/include/linux/spi/offload/types.h b/include/linux/spi/offload/= types.h index a74f8d84541b10062353e81a638f05628b696394..7476f2073b02ee0f9edd3ae75e5= 87b075746fa92 100644 --- a/include/linux/spi/offload/types.h +++ b/include/linux/spi/offload/types.h @@ -38,6 +38,43 @@ struct spi_offload { struct device *provider_dev; /** @priv: provider driver private data */ void *priv; + /** @ops: callbacks for offload support */ + const struct spi_offload_ops *ops; +}; + +enum spi_offload_trigger_type { + /* Indication from SPI peripheral that data is read to read. */ + SPI_OFFLOAD_TRIGGER_DATA_READY, + /* Trigger comes from a periodic source such as a clock. */ + SPI_OFFLOAD_TRIGGER_PERIODIC, +}; + +struct spi_offload_trigger_periodic { + u64 frequency_hz; +}; + +struct spi_offload_trigger_config { + /** @type: type discriminator for union */ + enum spi_offload_trigger_type type; + union { + struct spi_offload_trigger_periodic periodic; + }; +}; + +/** + * struct spi_offload_ops - callbacks implemented by offload providers + */ +struct spi_offload_ops { + /** + * @trigger_enable: Optional callback to enable the trigger for the + * given offload instance. + */ + int (*trigger_enable)(struct spi_offload *offload); + /** + * @trigger_disable: Optional callback to disable the trigger for the + * given offload instance. + */ + void (*trigger_disable)(struct spi_offload *offload); }; =20 #endif /* __LINUX_SPI_OFFLOAD_TYPES_H */ --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 113E71A3171 for ; Fri, 7 Feb 2025 20:09:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958971; cv=none; b=jzBUkcAnYhTmCRph1Wzjfdhgmq3PiklRd/qHKVfTW92ZerinH/UxWcurHXf6RWYOhdEWFrgmL9d0wF9mG4LZzCd+/ce88zNT+YK4m5HwX0PimI1g4zyyvtMmdbgnxUZRe5RErYZ4V6Qv1iZODLuhWglsTtGEcobz8or351KlKwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958971; c=relaxed/simple; bh=rt+GniortcrL6xGxBnnoav6poPM6JFl88uJNx+IwPpM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AHGZgx/WSNR97qBzlHGKy5IJjcel/F7PyVghh9zrRFc0mxMiceXUN3dkTDCqvFnXkhrtChc3ZMESop+7X9rgu9hFO779u4W863qWkhLk1Zcnm5XEejNlvpxTu7nVtDH/i6E7D4kcHplQIcpy3JZjatX8+VemI/BR0dj2YEDKwA4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=V2r333t3; arc=none smtp.client-ip=209.85.161.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="V2r333t3" Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-5f31b3db5ecso1160026eaf.0 for ; Fri, 07 Feb 2025 12:09:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958969; x=1739563769; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=0rxdAtHegvFqd0jnw3MaUYjpnQ120eeARkMko1E190M=; b=V2r333t3zEtSQgDRXsFrMqH/gp6HsYU/6GbajFYg7OnmyWlx3XvBoovxujwhrfU4WP 7RqYut8KKJl3VEg3PXbzByYk/wnXeZMe+oE0sO/JigdmAQsO4gzwnVKnhmTcAkGQR+hK gkKmKJZaN4a6nXqdPCujZl3mQhOcPXePpHCDskSwZIg/n9GuECRjk3GPgNKPj+c/bEPa Fbrb8T09CpAW3JAhG8dCxPICWYYW/Pd6W5XwwoKeong9T3+mPcvsrj3T4s2qHvVHf5/7 y71H0urnglNRdTPEdEJLsaS69AAfCqG349cI0OlJaG8hxhmmUsw1eIyPSmy48mON5z2k idKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958969; x=1739563769; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0rxdAtHegvFqd0jnw3MaUYjpnQ120eeARkMko1E190M=; b=qo9ghI47pYfajq8bUGC7opOpxJb8a901PxWLol9pc6HR2Ckd7qnxGyh6OCmcJieWSa vbfwkeYWiWCee7qyYCT107HiK9+w3NeGh7I4u7mqQXywLTyK4anMA81YW4SzuRYLG8qb uSg+N79efkynnAb94pGku+mI5gHNaeAnzPP8VTWLlMMa1o67b57AjUCXKDZpFR7oYnoE 8fpdNpbyA643qCuOg9oUB2zKnlbhnRnlN1T4jJquYvOSiQ/jErJdRFaMidBWDjy8BqVz qMduBNHv+ZUVUt6RyBFI2fSoaq5wNkF5wX9df2uI+ZButm+I++qKVCTMCHLrx/TqXyZH f7Tw== X-Forwarded-Encrypted: i=1; AJvYcCUj+nFbvRvhL9FSJFwiAUA+PuMSK/8a32OndIyDKyVsHdyL1MSHcubCk/B7OhLLlmmLXG3OtIx/VLIUEDA=@vger.kernel.org X-Gm-Message-State: AOJu0YxWDV1+ZU+nw9Cn1/UYuIFMnVFIVSfoKyxzgocWZfkrMaGg93oA 0eCRIBBwgIdddtkR6UHfoiU6o5ZwgXEagDSl2TAzpI2UTbqicrWdhp9eRFxOGF0= X-Gm-Gg: ASbGncv/xOzXxMEWp4NAGIbKDAC6D5hZW/vjKxvtWsUxZAab24noc5kdpSPYAr0NXYD 1jRXZmjUf8AQesb5m3Y1Ee4QlAOBzU5r++oNnDPN3vQRE949KyWeaX8kPeMXhIl/wjTpQtIkR2C ho+K7LXexAyL1NMEXklRWw+NV+rYemyN4AC67iB6O7cTTa32lcUOvqOABhEt8fwjPeyoNM+i7A+ lZ+zcjQkiTIWrfjX/Lx18NxyLw3SjAuRgPyeWkI7IBDjSv7D30P5nAHzvstjiEBGGHNSHG2KTO5 /hWCuU4D9z8OwRnNLAXlIJG5h8JaPWefl1Iun/ySVw2NA/s= X-Google-Smtp-Source: AGHT+IH3nywXAStMc7fVsLOSc4VlZO41pla1zTMf86Ld9YGrejdTYExNnf8u2y6YSKz+yvEx7GllNw== X-Received: by 2002:a05:6820:189a:b0:5fa:7bdc:e88 with SMTP id 006d021491bc7-5fc5e7bc42dmr3039099eaf.7.1738958969117; Fri, 07 Feb 2025 12:09:29 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:27 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:00 -0600 Subject: [PATCH v8 03/17] dt-bindings: trigger-source: add generic PWM trigger source Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-3-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add a new binding for using a PWM signal as a trigger source. The idea here is similar to e.g. "pwm-clock" to allow a trigger source consumer to use a PWM provider as a trigger source. Reviewed-by: Jonathan Cameron Reviewed-by: Rob Herring (Arm) Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Moved file from bindings/spi/ to bindings/trigger-source/ * Updated description to not mention SPI * Dropped $ref: /schemas/spi/trigger-source.yaml# * Swapped order in name to be consistent with "pwm-clock" v5 changes: * Add MAINTAINERS entry v4 changes: new patch in v4 --- .../bindings/trigger-source/pwm-trigger.yaml | 37 ++++++++++++++++++= ++++ MAINTAINERS | 5 +++ 2 files changed, 42 insertions(+) diff --git a/Documentation/devicetree/bindings/trigger-source/pwm-trigger.y= aml b/Documentation/devicetree/bindings/trigger-source/pwm-trigger.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1eac20031dc3cf921aafb8aa37f= 4e4eca1075835 --- /dev/null +++ b/Documentation/devicetree/bindings/trigger-source/pwm-trigger.yaml @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/trigger-source/pwm-trigger.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Generic trigger source using PWM + +description: Remaps a PWM channel as a trigger source. + +maintainers: + - David Lechner + +properties: + compatible: + const: pwm-trigger + + '#trigger-source-cells': + const: 0 + + pwms: + maxItems: 1 + +required: + - compatible + - '#trigger-source-cells' + - pwms + +additionalProperties: false + +examples: + - | + trigger { + compatible =3D "pwm-trigger"; + #trigger-source-cells =3D <0>; + pwms =3D <&pwm 0 1000000 0>; + }; diff --git a/MAINTAINERS b/MAINTAINERS index c37504e2e19f067b9835a28708dfd1d25700a608..d6ac6cdc6278536b5c4f81ff183= 588b491798036 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -24103,6 +24103,11 @@ W: https://github.com/srcres258/linux-doc T: git git://github.com/srcres258/linux-doc.git doc-zh-tw F: Documentation/translations/zh_TW/ =20 +TRIGGER SOURCE - PWM +M: David Lechner +S: Maintained +F: Documentation/devicetree/bindings/trigger-source/pwm-trigger.yaml + TRUSTED SECURITY MODULE (TSM) ATTESTATION REPORTS M: Dan Williams L: linux-coco@lists.linux.dev --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1644D1A83E6 for ; Fri, 7 Feb 2025 20:09:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958973; cv=none; b=aIGdFnstpIN5a/i+wh8RXVbyY9mwGACHWzx/6raXynJ3ceIv9ZVNlRTZxMroKECsFnYmmrfxXh5fcTIzhbLFOIcMnLimklIWoIRNxg425AA2DPPUdsyjstCp9mqWjLxDJAHIZZuPR1RXS+9rlUYwT+GgzPuC1F71jG9bgY0INMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958973; c=relaxed/simple; bh=3RT+o1+T0VbqeXfjdh2+I26pJ7Uif/2+uVUfZuMToWE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Jyl/Q9fBb68Ottl7olk6IZyB/IYQR6nWCtKHIo54xDbBXkM9JSZk5006sxbkdjSIexE5fA+kWXsFYIYem4uG9vRFzinBp825curv4ERTKXWdf6twxjqdt2hRkVZESBttnXrpbE/L2QZbZ+zTBPvYPtpJy1V1gvLSXVYHcLFlBXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=HQEQrEw4; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="HQEQrEw4" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5fc13355a5bso1206814eaf.3 for ; Fri, 07 Feb 2025 12:09:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958971; x=1739563771; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1kk3LC/T2vaIR0Ht/6x/1dzEkwGWvB2I87emNxfb/ik=; b=HQEQrEw42+MnueNJ8Z8UeaK5vJtFWqQFhSrreDl4cVvl00eE4afs4GClD2/Buanzb7 8ATGibNUsCKWG+6tea35f0LdwdzWFAdlNKItoouX5f0BtC5uznCt7VAsly/qk+B1r2PI RpE7XagFIBruXg/BkgBmmx3lpewZgALI/x+37BwTmgQgkZfMSAcswbW5Z6IU/Mfi3Ixp cioIZucs5HOEn8qqXhOaAzmzwoq2sRzpn02dBwoMZdZh9JbF8MVNjSkRxjOeChKX0S6q L20E+GPmotHi66JUNJkG/D05WUCWIkbcP3v55UFAI4pbjMm4GVYnipTnyrhJ6eKHZBeK Q/Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958971; x=1739563771; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1kk3LC/T2vaIR0Ht/6x/1dzEkwGWvB2I87emNxfb/ik=; b=HNSzVvvKk5FvbkXfroUc5QCIawJCOPRl50Ih7C3uwAel3OBIV6YUXoc9EutTTUgBUD atq9Dg4ILNdK56uSOeyv27UDcjmGYZMsOoZm0OdOjiUNHsnC3LtJy/1ny0vOkGVRdC38 VOa2DJog3dtpQ70U2GUkDxDbJA8zPQUiTcXAsbbtv01N6XQYZ4xoxHWsIt1IMUC93cmY jpdVB9C+FFNM2G+e6WOO9IMa28v9CuwPVivbhJOHjCVIKsJmoiwITair1pz26c+5bdvG FOVnC+V8yGVZXqg715Ls5J+ObV0CpyUVZsMsrPQKx/hQNrZ2L4Hcehi2949GxtcmjmtA Z42g== X-Forwarded-Encrypted: i=1; AJvYcCW/Q5HmXETVdnGZa7bb66IqZh+g6xqSY+coMbP/gH5UfNjObEcmSOYAlF6tkp7m8HSFNQUe2LsPsv4oojI=@vger.kernel.org X-Gm-Message-State: AOJu0YxbhlI/Z5dK/o1ANRbcrlY4F3W09sN7XcOks0JTg6WfBEuNg0CA TSbaC5NQm1dGjtIZGKNacfJUu9o5wDKxOOj1+tUxpfV/v4K5hB58oulAvSR3gKg= X-Gm-Gg: ASbGncvPr/3NhegjFOgo0/khiclb7Gsi0J9XvOunbqO60jdq+W3l79qnUVljposGBhH s4OIQMJzjmlCwn8uizzzt4wloRlbnOA3I5NU94Z08jArrT4hTaQh7znFrM9v+xvB/7KA5IM3oFB NCYbmE2bOmoLnyulupe3h8FZh9WWieRjUuidev56eF+/NUDugdefZbHvshvJZqYBracCM6rkBi5 FvhTyo1QonAQq4Q+CpKIYk8uBEaarHt/pSTtADh4OAahoL+cxcul/n/p+5oX1QtZ3d92IegvcOP YYGlAtNezwVZZmXkiHKo5Xm1qL+vZh14G+KhkMHwOYYs/fY= X-Google-Smtp-Source: AGHT+IHHPAH+wQlAPWj4MVF5kQsTJi5JwWku/ib98UqIUvEioLfz44zhppCON7R0C22y2qbkvgZspw== X-Received: by 2002:a05:6820:1ac2:b0:5fa:64c3:2938 with SMTP id 006d021491bc7-5fc5e5c6310mr2949325eaf.2.1738958971025; Fri, 07 Feb 2025 12:09:31 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:29 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:01 -0600 Subject: [PATCH v8 04/17] spi: offload-trigger: add PWM trigger driver Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-4-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add a new driver for a generic PWM trigger for SPI offloads. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Use dev instead of &pdev->dev * Swap order of "pwm" and "trigger" in name to follow "pwm-clock" precedent. v5 changes: * Updated to accommodate changes in other patches in this series. * Add MAINTAINERS entry. v4 changes: new patch in v4 --- MAINTAINERS | 1 + drivers/spi/Kconfig | 12 +++ drivers/spi/Makefile | 3 + drivers/spi/spi-offload-trigger-pwm.c | 162 ++++++++++++++++++++++++++++++= ++++ 4 files changed, 178 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index d6ac6cdc6278536b5c4f81ff183588b491798036..c94061c5012843825b2f6ba9a30= 63f230e5d654e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22344,6 +22344,7 @@ F: include/linux/mtd/spi-nor.h =20 SPI OFFLOAD R: David Lechner +F: drivers/spi/spi-offload-trigger-pwm.c F: drivers/spi/spi-offload.c F: include/linux/spi/spi-offload.h K: spi_offload diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 02064a4e292815ec0213e2e446b4f90ed8855a52..2cfc14be869790f5226130428bb= 7cb40aadfb031 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1320,4 +1320,16 @@ endif # SPI_SLAVE config SPI_DYNAMIC def_bool ACPI || OF_DYNAMIC || SPI_SLAVE =20 +if SPI_OFFLOAD + +comment "SPI Offload triggers" + +config SPI_OFFLOAD_TRIGGER_PWM + tristate "SPI offload trigger using PWM" + depends on PWM + help + Generic SPI offload trigger implemented using PWM output. + +endif # SPI_OFFLOAD + endif # SPI diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index bb5fc20df21332232533c2e70c0cc230f6bcf27f..0068d170bc99c750c13376c4013= 991d927bbac63 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -164,3 +164,6 @@ obj-$(CONFIG_SPI_AMD) +=3D spi-amd.o # SPI slave protocol handlers obj-$(CONFIG_SPI_SLAVE_TIME) +=3D spi-slave-time.o obj-$(CONFIG_SPI_SLAVE_SYSTEM_CONTROL) +=3D spi-slave-system-control.o + +# SPI offload triggers +obj-$(CONFIG_SPI_OFFLOAD_TRIGGER_PWM) +=3D spi-offload-trigger-pwm.o diff --git a/drivers/spi/spi-offload-trigger-pwm.c b/drivers/spi/spi-offloa= d-trigger-pwm.c new file mode 100644 index 0000000000000000000000000000000000000000..b26d4437c589052709a8206f831= 4ffd08355870e --- /dev/null +++ b/drivers/spi/spi-offload-trigger-pwm.c @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 Analog Devices Inc. + * Copyright (C) 2024 BayLibre, SAS + * + * Generic PWM trigger for SPI offload. + */ + +#include +#include +#include +#include +#include + +struct spi_offload_trigger_pwm_state { + struct device *dev; + struct pwm_device *pwm; +}; + +static bool spi_offload_trigger_pwm_match(struct spi_offload_trigger *trig= ger, + enum spi_offload_trigger_type type, + u64 *args, u32 nargs) +{ + if (nargs) + return false; + + return type =3D=3D SPI_OFFLOAD_TRIGGER_PERIODIC; +} + +static int spi_offload_trigger_pwm_validate(struct spi_offload_trigger *tr= igger, + struct spi_offload_trigger_config *config) +{ + struct spi_offload_trigger_pwm_state *st =3D spi_offload_trigger_get_priv= (trigger); + struct spi_offload_trigger_periodic *periodic =3D &config->periodic; + struct pwm_waveform wf =3D { }; + int ret; + + if (config->type !=3D SPI_OFFLOAD_TRIGGER_PERIODIC) + return -EINVAL; + + if (!periodic->frequency_hz) + return -EINVAL; + + wf.period_length_ns =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, periodic->frequenc= y_hz); + /* REVISIT: 50% duty-cycle for now - may add config parameter later */ + wf.duty_length_ns =3D wf.period_length_ns / 2; + + ret =3D pwm_round_waveform_might_sleep(st->pwm, &wf); + if (ret < 0) + return ret; + + periodic->frequency_hz =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, wf.period_lengt= h_ns); + + return 0; +} + +static int spi_offload_trigger_pwm_enable(struct spi_offload_trigger *trig= ger, + struct spi_offload_trigger_config *config) +{ + struct spi_offload_trigger_pwm_state *st =3D spi_offload_trigger_get_priv= (trigger); + struct spi_offload_trigger_periodic *periodic =3D &config->periodic; + struct pwm_waveform wf =3D { }; + + if (config->type !=3D SPI_OFFLOAD_TRIGGER_PERIODIC) + return -EINVAL; + + if (!periodic->frequency_hz) + return -EINVAL; + + wf.period_length_ns =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, periodic->frequenc= y_hz); + /* REVISIT: 50% duty-cycle for now - may add config parameter later */ + wf.duty_length_ns =3D wf.period_length_ns / 2; + + return pwm_set_waveform_might_sleep(st->pwm, &wf, false); +} + +static void spi_offload_trigger_pwm_disable(struct spi_offload_trigger *tr= igger) +{ + struct spi_offload_trigger_pwm_state *st =3D spi_offload_trigger_get_priv= (trigger); + struct pwm_waveform wf; + int ret; + + ret =3D pwm_get_waveform_might_sleep(st->pwm, &wf); + if (ret < 0) { + dev_err(st->dev, "failed to get waveform: %d\n", ret); + return; + } + + wf.duty_length_ns =3D 0; + + ret =3D pwm_set_waveform_might_sleep(st->pwm, &wf, false); + if (ret < 0) + dev_err(st->dev, "failed to disable PWM: %d\n", ret); +} + +static const struct spi_offload_trigger_ops spi_offload_trigger_pwm_ops = =3D { + .match =3D spi_offload_trigger_pwm_match, + .validate =3D spi_offload_trigger_pwm_validate, + .enable =3D spi_offload_trigger_pwm_enable, + .disable =3D spi_offload_trigger_pwm_disable, +}; + +static void spi_offload_trigger_pwm_release(void *data) +{ + pwm_disable(data); +} + +static int spi_offload_trigger_pwm_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct spi_offload_trigger_info info =3D { + .fwnode =3D dev_fwnode(dev), + .ops =3D &spi_offload_trigger_pwm_ops, + }; + struct spi_offload_trigger_pwm_state *st; + struct pwm_state state; + int ret; + + st =3D devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); + if (!st) + return -ENOMEM; + + info.priv =3D st; + st->dev =3D dev; + + st->pwm =3D devm_pwm_get(dev, NULL); + if (IS_ERR(st->pwm)) + return dev_err_probe(dev, PTR_ERR(st->pwm), "failed to get PWM\n"); + + /* init with duty_cycle =3D 0, output enabled to ensure trigger off */ + pwm_init_state(st->pwm, &state); + state.enabled =3D true; + + ret =3D pwm_apply_might_sleep(st->pwm, &state); + if (ret < 0) + return dev_err_probe(dev, ret, "failed to apply PWM state\n"); + + ret =3D devm_add_action_or_reset(dev, spi_offload_trigger_pwm_release, st= ->pwm); + if (ret) + return ret; + + return devm_spi_offload_trigger_register(dev, &info); +} + +static const struct of_device_id spi_offload_trigger_pwm_of_match_table[] = =3D { + { .compatible =3D "pwm-trigger" }, + { } +}; +MODULE_DEVICE_TABLE(of, spi_offload_trigger_pwm_of_match_table); + +static struct platform_driver spi_offload_trigger_pwm_driver =3D { + .driver =3D { + .name =3D "pwm-trigger", + .of_match_table =3D spi_offload_trigger_pwm_of_match_table, + }, + .probe =3D spi_offload_trigger_pwm_probe, +}; +module_platform_driver(spi_offload_trigger_pwm_driver); + +MODULE_AUTHOR("David Lechner "); +MODULE_DESCRIPTION("Generic PWM trigger"); +MODULE_LICENSE("GPL"); --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D167A1ADFE4 for ; Fri, 7 Feb 2025 20:09:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958976; cv=none; b=nNAo8rQ9deUiYNjndoP5h0rzsercLjIl17sHtW5xY6q9YxexXQJbHIbkZjljo5zs21vccyHztaizWj8wtoNGG+VqrQ+EJyIqUxb/uTWlPGT4zb20LQpcBFHKqYzWj8LV4bpg0NsCgVUC04xo7NQcwj7SBSw0ZgvxNhuFnk67fHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958976; c=relaxed/simple; bh=ksRH+xsE5ndXaOQbEaFG6Nxn71f/QpIEGr1SY3yQYAo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AaXbIx77gkd1V9ndYT9WABWYRN2cJQ5v9YcC2cdVw4soU5558lKecTWzGJhq4xR3sxATteBIswur7YVXiqYSZ/XHbWNSUU/JFW65K5SiSEi8hWh5I+Eggpt9ABt97s+bCca4Q/zX1ry+6Zojc05CZoSFf3kS6YjbR+WLfisUKR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=KWWYFPLU; arc=none smtp.client-ip=209.85.210.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="KWWYFPLU" Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-71e2764aa46so1761222a34.2 for ; Fri, 07 Feb 2025 12:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958973; x=1739563773; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=jeedPY/yxMeILi8lrC5AeC6wSQDrSEJfG5XlwStWNys=; b=KWWYFPLUmmqfSok6BJCoqi4aTCRDPEoFOPYzQ5/JJRtW86uqzeeqQea+G3Btb9vWBe uRSELTmj8Sz17nkBX6SOOtqMUSz0aN8p7vxLd+clI1vVArWtuRZqQqrn7FaQ3QDCi/4B 2QLQA9LsIZS8RQxjUmxmX9zUE+jVYFrR86qizPvhobIKgfff5OPEp9MHrejvGmSuFTTa K91RlFzm9OsQ8Adyhj+JwL15rLAT1BK86ZIy7rm73bbeJtZc2GMwIR69jfypXBvy2OzM 6FvcQQL7bZGWA/AjixBEkwf0SBjxSFdsN5/sn1I8Smjz5N035hKgNOwa/Xo7mqtKc4HL fjxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958973; x=1739563773; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jeedPY/yxMeILi8lrC5AeC6wSQDrSEJfG5XlwStWNys=; b=lX0gVa5APP4tk8+dOyrY691TYTsD2gRQ+4M1mP9f0Qoe/JwTzXXuA6TWZVhXtPwmmz I2zGTCzItGvmUglT7c2TSzO3Ui99KbXoFcraLlyCkEEBCR9w1SOFGg2mj4ELu0fJO19C NIMu3uunXUI1cjTdoqWTrnWCXC3NNaLByBIBN8EHUudTXyRKh01f4JWbgTaUxN+8+mVd er4OoKYBEa+xJgqJ2B34quIHF2x166UKAZV9XCnfXs19lFBEVz7XCYYC2WEitUg0Qxob 39CR58dO+yqIM7t6RwTu9GHuNnU9bV7GW0gk3Mvo1lAdUcJ29QDSbsenYvA68YrwJIyR 1Fsg== X-Forwarded-Encrypted: i=1; AJvYcCUjQBjmXyP84WFCALV3bI/nAqxUH11fRczsjWYYg3ews+oDreQtL4YhgI4GEkdsuKFPgrYyfHXyxEp9J4A=@vger.kernel.org X-Gm-Message-State: AOJu0YzDLquX3IB8orlSpwf4CBak9hexJ7IiRzD116Cb/5d7wuAwU/YT RbdF4SmSCMEDlvbIAZK4zXTXaJiK94LK1UUNpBNLnXwy4tiVf1qiEna6U15P0Yo= X-Gm-Gg: ASbGncvtm9P9raTQPvPqUm6uzAcTRgRJbiecVLf+0CN+IDYSXBo1cY6Inl+GMWdJvPx 8Itd4puQWMqpU2UVWmRvtnGZ5CwRL/pcc3s6NsF8It+vhpt6gn6zZA8ZRlRtCz/7zkmaKQlu25Y jZ9BIQRIDOJ1/IpnNggyYCJjd7DBPY9N2dIzynH2V3aNvL3FhIz5NVxzrgu+tSDEYNolky4yJSk sa2r6Cq2kbK3c1pOQErzet2gTDyaKEnKjoXG/UcJ5bX54X3mge+2LYj8nGNzEIL8REJJNWm3ik9 4Y1SurxWCNjd5CvXKiSE8cuxl8xwFhHVqtPryc0NqfcF0xw= X-Google-Smtp-Source: AGHT+IE1ryxCotw0T1fE7BhGPd527F38UG/3XYjYBlRBCNfoaSyKJg91agbDLZBdJ6cr3rXIY1UsWQ== X-Received: by 2002:a05:6830:7001:b0:71d:de27:8d32 with SMTP id 46e09a7af769-726b87b457bmr3273826a34.2.1738958972882; Fri, 07 Feb 2025 12:09:32 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:32 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:02 -0600 Subject: [PATCH v8 05/17] spi: add offload TX/RX streaming APIs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-5-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Most configuration of SPI offloads is handled opaquely using the offload pointer that is passed to the various offload functions. However, there are some offload features that need to be controlled on a per transfer basis. This patch adds a flag field to struct spi_transfer to allow specifying such features. The first feature to be added is the ability to stream data to/from a hardware sink/source rather than using a tx or rx buffer. Additional flags can be added in the future as needed. A flags field is also added to the offload struct for providers to indicate which flags are supported. This allows for generic checking of offload capabilities during __spi_validate() so that each offload provider doesn't have to implement their own validation. As a first users of this streaming capability, getter functions are added to get a DMA channel that is directly connected to the offload. Peripheral drivers will use this to get a DMA channel and configure it to suit their needs. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Update for header file split. * Fix wrong kernel-doc comments. v5 change: * Remove incorrect comment about caller needing to release DMA channels. v4 changes: * DMA API's now automatically release DMA channels instead of leaving it up to the caller. v3 changes: * Added spi_offload_{tx,rx}_stream_get_dma_chan() functions. v2 changes: * This is also split out from "spi: add core support for controllers with offload capabilities". * In the previous version, we were using (void *)-1 as a sentinel value that could be assigned, e.g. to rx_buf. But this was naive since there is core code that would try to dereference this pointer. So instead, we've added a new flags field to the spi_transfer structure for this sort of thing. This also has the advantage of being able to be used in the future for other arbitrary features. --- drivers/spi/spi-offload.c | 70 ++++++++++++++++++++++++++++++++= ++++ drivers/spi/spi.c | 10 ++++++ include/linux/spi/offload/consumer.h | 5 +++ include/linux/spi/offload/types.h | 19 ++++++++++ include/linux/spi/spi.h | 3 ++ 5 files changed, 107 insertions(+) diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c index 43582e50e279c4b1b958765fec556aaa91180e55..df5e963d5ee29d3783355959553= 6a460c530bc81 100644 --- a/drivers/spi/spi-offload.c +++ b/drivers/spi/spi-offload.c @@ -18,6 +18,7 @@ =20 #include #include +#include #include #include #include @@ -332,6 +333,75 @@ void spi_offload_trigger_disable(struct spi_offload *o= ffload, } EXPORT_SYMBOL_GPL(spi_offload_trigger_disable); =20 +static void spi_offload_release_dma_chan(void *chan) +{ + dma_release_channel(chan); +} + +/** + * devm_spi_offload_tx_stream_request_dma_chan - Get the DMA channel info = for the TX stream + * @dev: Device for devm purposes. + * @offload: Offload instance + * + * This is the DMA channel that will provide data to transfers that use the + * %SPI_OFFLOAD_XFER_TX_STREAM offload flag. + * + * Return: Pointer to DMA channel info, or negative error code + */ +struct dma_chan +*devm_spi_offload_tx_stream_request_dma_chan(struct device *dev, + struct spi_offload *offload) +{ + struct dma_chan *chan; + int ret; + + if (!offload->ops || !offload->ops->tx_stream_request_dma_chan) + return ERR_PTR(-EOPNOTSUPP); + + chan =3D offload->ops->tx_stream_request_dma_chan(offload); + if (IS_ERR(chan)) + return chan; + + ret =3D devm_add_action_or_reset(dev, spi_offload_release_dma_chan, chan); + if (ret) + return ERR_PTR(ret); + + return chan; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_tx_stream_request_dma_chan); + +/** + * devm_spi_offload_rx_stream_request_dma_chan - Get the DMA channel info = for the RX stream + * @dev: Device for devm purposes. + * @offload: Offload instance + * + * This is the DMA channel that will receive data from transfers that use = the + * %SPI_OFFLOAD_XFER_RX_STREAM offload flag. + * + * Return: Pointer to DMA channel info, or negative error code + */ +struct dma_chan +*devm_spi_offload_rx_stream_request_dma_chan(struct device *dev, + struct spi_offload *offload) +{ + struct dma_chan *chan; + int ret; + + if (!offload->ops || !offload->ops->rx_stream_request_dma_chan) + return ERR_PTR(-EOPNOTSUPP); + + chan =3D offload->ops->rx_stream_request_dma_chan(offload); + if (IS_ERR(chan)) + return chan; + + ret =3D devm_add_action_or_reset(dev, spi_offload_release_dma_chan, chan); + if (ret) + return ERR_PTR(ret); + + return chan; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_rx_stream_request_dma_chan); + /* Triggers providers */ =20 static void spi_offload_trigger_unregister(void *data) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 68e6758771681bf272314a76b4b600f48e37f7c5..6d63ec291e88cc852fa1160e59a= d3de70fdfc396 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -4155,6 +4156,15 @@ static int __spi_validate(struct spi_device *spi, st= ruct spi_message *message) =20 if (_spi_xfer_word_delay_update(xfer, spi)) return -EINVAL; + + /* Make sure controller supports required offload features. */ + if (xfer->offload_flags) { + if (!message->offload) + return -EINVAL; + + if (xfer->offload_flags & ~message->offload->xfer_flags) + return -EINVAL; + } } =20 message->status =3D -EINPROGRESS; diff --git a/include/linux/spi/offload/consumer.h b/include/linux/spi/offlo= ad/consumer.h index 5a0ec5303d600728959594bcdbd0cb2baeba7c77..cd7d5daa21e69b61c16eba6c10c= 855345a4f3297 100644 --- a/include/linux/spi/offload/consumer.h +++ b/include/linux/spi/offload/consumer.h @@ -31,4 +31,9 @@ int spi_offload_trigger_enable(struct spi_offload *offloa= d, void spi_offload_trigger_disable(struct spi_offload *offload, struct spi_offload_trigger *trigger); =20 +struct dma_chan *devm_spi_offload_tx_stream_request_dma_chan(struct device= *dev, + struct spi_offload *offload); +struct dma_chan *devm_spi_offload_rx_stream_request_dma_chan(struct device= *dev, + struct spi_offload *offload); + #endif /* __LINUX_SPI_OFFLOAD_CONSUMER_H */ diff --git a/include/linux/spi/offload/types.h b/include/linux/spi/offload/= types.h index 7476f2073b02ee0f9edd3ae75e587b075746fa92..86d0e8cb9495bb43e177378b204= 1067de8ea8786 100644 --- a/include/linux/spi/offload/types.h +++ b/include/linux/spi/offload/types.h @@ -11,6 +11,11 @@ =20 struct device; =20 +/* This is write xfer but TX uses external data stream rather than tx_buf.= */ +#define SPI_OFFLOAD_XFER_TX_STREAM BIT(0) +/* This is read xfer but RX uses external data stream rather than rx_buf. = */ +#define SPI_OFFLOAD_XFER_RX_STREAM BIT(1) + /* Offload can be triggered by external hardware event. */ #define SPI_OFFLOAD_CAP_TRIGGER BIT(0) /* Offload can record and then play back TX data when triggered. */ @@ -40,6 +45,8 @@ struct spi_offload { void *priv; /** @ops: callbacks for offload support */ const struct spi_offload_ops *ops; + /** @xfer_flags: %SPI_OFFLOAD_XFER_* flags supported by provider */ + u32 xfer_flags; }; =20 enum spi_offload_trigger_type { @@ -75,6 +82,18 @@ struct spi_offload_ops { * given offload instance. */ void (*trigger_disable)(struct spi_offload *offload); + /** + * @tx_stream_request_dma_chan: Optional callback for controllers that + * have an offload where the TX data stream is connected directly to a + * DMA channel. + */ + struct dma_chan *(*tx_stream_request_dma_chan)(struct spi_offload *offloa= d); + /** + * @rx_stream_request_dma_chan: Optional callback for controllers that + * have an offload where the RX data stream is connected directly to a + * DMA channel. + */ + struct dma_chan *(*rx_stream_request_dma_chan)(struct spi_offload *offloa= d); }; =20 #endif /* __LINUX_SPI_OFFLOAD_TYPES_H */ diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 98bdc8c16c20521c0a94e5f72f5e71c4f6d7d11e..4c087009cf974595f23036b1b7a= 030a45913420c 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -1093,6 +1093,9 @@ struct spi_transfer { =20 u32 effective_speed_hz; =20 + /* Use %SPI_OFFLOAD_XFER_* from spi-offload.h */ + unsigned int offload_flags; + unsigned int ptp_sts_word_pre; unsigned int ptp_sts_word_post; =20 --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f49.google.com (mail-oo1-f49.google.com [209.85.161.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA76D1B4153 for ; Fri, 7 Feb 2025 20:09:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958977; cv=none; b=pl7o4EWAqEb0r5XaP8ywv4N3mj7rzBN+QIlgsH8mQZ+LBExgZ+pJUdrHRQyXVpIrMP8XUBNb1wo3urfr4ilSHXpL7e4bp4+h7eMBLo9WVCaEb2ltmkog+YPLuLTp5BnQMDd1wuD9Bn6YkWBsPLwFtXHs6o8QbD2y0vu4piaxtZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958977; c=relaxed/simple; bh=wIbk5mFn+xLWzifwyYYIf1Y11mKle1UlHYsEBuzuRpg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EHygzyhULOlRTAs7BhRfT3vxrFDY4c3gL/NIP7UzxtnsgniIgH8nuX1+4e8JbhI9u5StqiIqB4xWdPtNmLli6ZIwJziDEedEqLdCy4t5IK9x0cywuNRbauw330r8s1ufcBm2SxMSha514HLTrb2EIy2MNFsVMoHU56RhXXQnlHY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=ZfuC9rmy; arc=none smtp.client-ip=209.85.161.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="ZfuC9rmy" Received: by mail-oo1-f49.google.com with SMTP id 006d021491bc7-5f321876499so1305140eaf.1 for ; Fri, 07 Feb 2025 12:09:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958975; x=1739563775; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=wcwsH9FSGgvZx1I+Qt+xMirbqx1PZqy/STFkdwYNSkc=; b=ZfuC9rmyYw8HCnuaEvsFPnoMWV/OmVUiL8HlxJy+WoSxNZSDlRjvq5FNAMAtLe7158 y+M6tzH8ihYLtD4ifvGCeBZjJkWQRJJsgJ+GCXqM38bELxRVBsXCjFwhjNeat3IjpqDI TjBAFlCIRB6VQ37gRUhX9ViCNIik+gLkRHgNdXOhwk0RGIruKOi6nnBPOta+fYMbddKT I/RPl8l7/BRKwTQN4c3SSW97Lk7i3qEISX2+gGCkzjEaIUR6mrrQk+zaYvPnyJBOfRMF imCzuqlDrwxrDOldm9ddDSyNVREveAdIPPTTNAJZ01TFlYvQkUkRdyP8Y/7o1OGNW13S T3mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958975; x=1739563775; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wcwsH9FSGgvZx1I+Qt+xMirbqx1PZqy/STFkdwYNSkc=; b=SEImWkYembEW1zZtAYrspEHJqT0LIvnx0qMTkZAJtQRZdgx/6kTVhk2qqKGxpXSFKn 63NfgLO7bVdacdWJXcCI8eIaxvRBt7Ha9keFszOhZqi+Ul8xG3UBBcHSfbeZhWTDYo2u yHG4ipRMCcz/aksS9lkH1FCbtgivkd2W9/S4cyGyFrnqiy7SiUZdkgReWzBzFLgsjpJt 6bBZCmBv9kQ8p9ctRf1uySHDZwn/fu9DPWke15va3qkM5gwni5Iu8Zp6J4CdZY4+CeQv 5Un+krnZs23GlEq9+wdQvcDw0fdsiWYdZuuHJgBqves2n4bmvQEFzVxNr7Qs8FnmgQGu xcWA== X-Forwarded-Encrypted: i=1; AJvYcCX6UT5VPl51jsvZGx9tklsVKm4R0LN4YszNPPKEutWAyyoFA/yV0c1lZB1aykVCgYi3mWaUOVsNrYNC9G8=@vger.kernel.org X-Gm-Message-State: AOJu0YyQJ4R5xmjDPn36feQkZ/Ci5/Zt2+jCP8+f/qbUklB7ea62ezzw uyYAtV9SQUqq2bn7nhKqj5OSBrvUeCyOEAW3rKr8ch2c1RZL8XWHm3SKmEkiTu0= X-Gm-Gg: ASbGncsg7bonSoaHIqhCS/5CbhueL5M0NWoNHhl5gBzT2SFf67nDm1EsKZYyizFDUAx dj9oy6xZKsm+f8K8cfg4wS+O/J/E23MNvm3mmk6xEZh7oedcni7/HiXB1OJFZYeo8K/wMa8snlp svuB7LHxjvUR5EY3medYjicGu9V4DjCJkAwr4rQjBnKMuZKzImzKJ2hm5BtUmgujvu829/iiGm9 81ZcJQ/AI1o+OLyIyG5IEh2XvawQtOreegaEk1+VqBswG8Dhg294NjL+iNxigrDBUbycqcf5Z1A 510FzHa0tzCNqMF6pLLMUQIheD0AMDusxeVPeR9Uzo/s/wo= X-Google-Smtp-Source: AGHT+IHnl7iHgGAFvK/NvYE6ivOshF430NsG1tgBvCko0JvmrNF+h8vllEgQRGyP15pnWXp6FnjKLA== X-Received: by 2002:a4a:e654:0:b0:5fb:fe5a:6dd2 with SMTP id 006d021491bc7-5fc51ea2720mr6545559eaf.1.1738958974710; Fri, 07 Feb 2025 12:09:34 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:33 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:03 -0600 Subject: [PATCH v8 06/17] spi: dt-bindings: axi-spi-engine: add SPI offload properties Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-6-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 The AXI SPI Engine has support for hardware offloading capabilities. This includes a connection to a DMA controller for streaming RX or TX data and a trigger input for starting execution of the SPI message programmed in the offload. It is designed to support up to 32 offload instances. Reviewed-by: Jonathan Cameron Reviewed-by: Rob Herring (Arm) Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Drop type $ref for trigger-sources property since it is defined elsewhere now * Undo v5 changes that limited the number of offloads to 1 v5 changes: * Also document offload0-tx DMA names since the hardware can support that now. * Limit the number of offloads to 1 for now since it would require significant hardware changes to actually support more than that. v4 changes: * Dropped #spi-offload-cells property. * Changed subject line. v3 changes: * Added #spi-offload-cells property. * Added properties for triggers and RX data stream connected to DMA. v2 changes: * This is basically a new patch. It partially replaces "dt-bindings: iio: offload: add binding for PWM/DMA triggered buffer". * The controller no longer has an offloads object node and the spi-offloads property is now a standard SPI peripheral property. --- .../bindings/spi/adi,axi-spi-engine.yaml | 24 ++++++++++++++++++= ++++ 1 file changed, 24 insertions(+) diff --git a/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml = b/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml index d48faa42d025b07d72baa61f8946f42acbaf47dc..4b3828eda6cb4c5524570f00033= b081a6e027b09 100644 --- a/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml +++ b/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml @@ -41,6 +41,26 @@ properties: - const: s_axi_aclk - const: spi_clk =20 + trigger-sources: + description: + An array of trigger source phandles for offload instances. The index= in + the array corresponds to the offload instance number. + minItems: 1 + maxItems: 32 + + dmas: + description: + DMA channels connected to the input or output stream interface of an + offload instance. + minItems: 1 + maxItems: 32 + + dma-names: + items: + pattern: "^offload(?:[12]?[0-9]|3[01])-[tr]x$" + minItems: 1 + maxItems: 32 + required: - compatible - reg @@ -59,6 +79,10 @@ examples: clocks =3D <&clkc 15>, <&clkc 15>; clock-names =3D "s_axi_aclk", "spi_clk"; =20 + trigger-sources =3D <&trigger_clock>; + dmas =3D <&dma 0>; + dma-names =3D "offload0-rx"; + #address-cells =3D <1>; #size-cells =3D <0>; =20 --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A07F11B85F8 for ; Fri, 7 Feb 2025 20:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958980; cv=none; b=rdOabFU4Uh/idJnMFXtdEiNxBMLD08x+yLGNllErRD88dUJyZmFAYssepPi1htuln5v019L6k+Ug0HyMo9jvp+Si8Vu2I18bPaFn5fW011rbWjlNzFNkcJbOk1kgLvd+zQePDf6wjVP74EmDKvo1OFb5OWzbZkuqgggnhhJVA00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958980; c=relaxed/simple; bh=NRqFiM3QjO3Fp/YY3y2grSjrL3x/SL2k0bUmxCtGjVY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ol5o0+nt/Voaf2dmAvFYnKELVIhm5uMx6l/akbUbXISa2WZFlP+Y904ACZ5fZG1Rlnru870at5Tb8N904VGc70EhWiL1zH9PAG1Thhdf1hAQGCEJMlV3oI0D2WUukbJvuLRmVhsCez2xbrXVi7EsT4J2iuGpdLVL+phIsF85Nuk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=tFyh9WDJ; arc=none smtp.client-ip=209.85.161.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="tFyh9WDJ" Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-5fc0b7a4e87so1874802eaf.1 for ; Fri, 07 Feb 2025 12:09:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958977; x=1739563777; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ObILQH9FbMWvsQ/CeWvY5SvzqmzkmCw57uFYf0o7RC8=; b=tFyh9WDJ056qG1IIXNTFiV1++OYTtK58tCgea328hJENPSZdpTGbYWOkx2gei8pvZd sGgJx45yR954l4/JrC5dSDKW9JCnjxRq6I6whmF70SHqQtbMC4CpbacbYBXAZxOAcHqV gglYT42X731RPPaU8TRHjygjRe7W5wsvcSSb4U8erhXM2sr9nx+JedX73uULCGJL97z+ zTW5L5GIFczBQ0qZ/+MCHgbavK2GQo6wb0Qm2sL6xEBxCFNbFB0RKigRXLRnyz/G9oYz RhDyzxdBxB+a3eGYrNsEzdU1kbXKntNl4DG1mrqfsTfbkioTE24dctK8eEd4Pp24bexX R30A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958977; x=1739563777; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ObILQH9FbMWvsQ/CeWvY5SvzqmzkmCw57uFYf0o7RC8=; b=QSYv+zCGZjKO0/HA5k38oRiDdZhzhMSMQaHP1iTbdeuTcvklKI6a98CYU2ehSZiJRZ c3MIWLrC6cpfPdV0y40YPMQctl+bRveVJv4Jd5FrX5e/2QZB3WPIhjQfMXF0BTb6Gxva Oyo7Rzf2f/4QudDXaKYjXMPiIZPeoRSwN2BacBDAokyYgJmYPxt/qAUom/DPh3/Pe4fc HfPGbwoo1YDY8scboOqvlLWOmzihZD7dBoj1ynIRYdCsh3YMyulfSwweOY8fkdx0LQbO 2ryp6UoelI8SmW75KI5QsGSRy3Rd5/MOZCTKEsDsW6x+VbTFru+lHMmMO4gz6euFyQEm ax3Q== X-Forwarded-Encrypted: i=1; AJvYcCUD6JoHdogqiSLYEYDyvXoeGYLl8eogQzNf/IeK2ITgnjNyjAqes2gSvZOhc+qo1+ZtUIIsxsJE71KfsjU=@vger.kernel.org X-Gm-Message-State: AOJu0YxCZ5cXWN+C7G2c0OUNxUMyE2SLNavRsarfh9MIPwMQ2ZGciGQs IcaAcNrnvn46XwM5cVtqkA5GnlkdgMY/nuLN9w/SsqqUiUg8EOqKmG5mTNnmdPs= X-Gm-Gg: ASbGncvdY2DYQPctHylSwEOrknPgZJ0GD7M8nvKJA4tHSGmvwLxgcRkVc7yiKYKjDiT njxi++0YAT+PZopMino2HIEeD1zJeNnK155RjPn0BpCB09CCxdkUErFe44DutlFKSUTq1VQkjPe 5vocqn/DD7EFsEmgy9dw7MUDcVZydboSNSYjizau7BiYR3ZhiDwfLoOb5kU+KxoYDCnHQ+EnvrW YU/31lFgJpDLbq2v7hMUs3Clsi7+6QbyGhVH/6MOiiPCWaa4hmw+qyGclK+L7jc1DbwhpFB1WHI xRtRG+RDYGssQiMdFBC/K+x5rJvitUml+1uvvdzdFGRSZas= X-Google-Smtp-Source: AGHT+IGPIrnvIEj1XCuBfKqz49HAprJI9YtGjp+82WbYlRu8zED9HMKJoLYJiPmraNGeix0vBLCuow== X-Received: by 2002:a05:6820:1809:b0:5f6:fcd2:1359 with SMTP id 006d021491bc7-5fc5e6b2a65mr2926706eaf.3.1738958976645; Fri, 07 Feb 2025 12:09:36 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:36 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:04 -0600 Subject: [PATCH v8 07/17] spi: axi-spi-engine: implement offload support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-7-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Implement SPI offload support for the AXI SPI Engine. Currently, the hardware only supports triggering offload transfers with a hardware trigger so attempting to use an offload message in the regular SPI message queue will fail. Also, only allows streaming rx data to an external sink, so attempts to use a rx_buf in the offload message will fail. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v8 changes: * Add missing bitfield.h include (fixes CI failure) v7 changes: none v6 changes: * Update for split spi/offload headers. v5 changes: * Set offload capability flags based on DT properties. * Add support for TX DMA since the hardware supports that now. * Update for changes in other patches in the series. v4 changes: * Adapted to changes in other patches in the series. * Moved trigger enable/disable to same function as offload enable/disable. v3 changes: * Added clk and dma_chan getter callbacks. * Fixed some bugs. v2 changes: This patch has been reworked to accommodate the changes described in all of the other patches. --- drivers/spi/Kconfig | 1 + drivers/spi/spi-axi-spi-engine.c | 315 +++++++++++++++++++++++++++++++++++= +++- 2 files changed, 309 insertions(+), 7 deletions(-) diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 2cfc14be869790f5226130428bb7cb40aadfb031..f496ab127ef011d092f66063e05= 772725ab89771 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -179,6 +179,7 @@ config SPI_AU1550 config SPI_AXI_SPI_ENGINE tristate "Analog Devices AXI SPI Engine controller" depends on HAS_IOMEM + select SPI_OFFLOAD help This enables support for the Analog Devices AXI SPI Engine SPI controll= er. It is part of the SPI Engine framework that is used in some Analog Devi= ces diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-eng= ine.c index 7c252126b33ea83fe6a6e80c6cb87499243069f5..da9840957778579dad3286f493a= bad87ad8a3bfc 100644 --- a/drivers/spi/spi-axi-spi-engine.c +++ b/drivers/spi/spi-axi-spi-engine.c @@ -2,11 +2,15 @@ /* * SPI-Engine SPI controller driver * Copyright 2015 Analog Devices Inc. + * Copyright 2024 BayLibre, SAS * Author: Lars-Peter Clausen */ =20 +#include +#include #include #include +#include #include #include #include @@ -14,9 +18,11 @@ #include #include #include +#include #include #include =20 +#define SPI_ENGINE_REG_OFFLOAD_MEM_ADDR_WIDTH 0x10 #define SPI_ENGINE_REG_RESET 0x40 =20 #define SPI_ENGINE_REG_INT_ENABLE 0x80 @@ -24,6 +30,7 @@ #define SPI_ENGINE_REG_INT_SOURCE 0x88 =20 #define SPI_ENGINE_REG_SYNC_ID 0xc0 +#define SPI_ENGINE_REG_OFFLOAD_SYNC_ID 0xc4 =20 #define SPI_ENGINE_REG_CMD_FIFO_ROOM 0xd0 #define SPI_ENGINE_REG_SDO_FIFO_ROOM 0xd4 @@ -34,10 +41,24 @@ #define SPI_ENGINE_REG_SDI_DATA_FIFO 0xe8 #define SPI_ENGINE_REG_SDI_DATA_FIFO_PEEK 0xec =20 +#define SPI_ENGINE_MAX_NUM_OFFLOADS 32 + +#define SPI_ENGINE_REG_OFFLOAD_CTRL(x) (0x100 + SPI_ENGINE_MAX_NUM_OFFLOA= DS * (x)) +#define SPI_ENGINE_REG_OFFLOAD_STATUS(x) (0x104 + SPI_ENGINE_MAX_NUM_OFFLO= ADS * (x)) +#define SPI_ENGINE_REG_OFFLOAD_RESET(x) (0x108 + SPI_ENGINE_MAX_NUM_OFFLO= ADS * (x)) +#define SPI_ENGINE_REG_OFFLOAD_CMD_FIFO(x) (0x110 + SPI_ENGINE_MAX_NUM_OFF= LOADS * (x)) +#define SPI_ENGINE_REG_OFFLOAD_SDO_FIFO(x) (0x114 + SPI_ENGINE_MAX_NUM_OFF= LOADS * (x)) + +#define SPI_ENGINE_SPI_OFFLOAD_MEM_WIDTH_SDO GENMASK(15, 8) +#define SPI_ENGINE_SPI_OFFLOAD_MEM_WIDTH_CMD GENMASK(7, 0) + #define SPI_ENGINE_INT_CMD_ALMOST_EMPTY BIT(0) #define SPI_ENGINE_INT_SDO_ALMOST_EMPTY BIT(1) #define SPI_ENGINE_INT_SDI_ALMOST_FULL BIT(2) #define SPI_ENGINE_INT_SYNC BIT(3) +#define SPI_ENGINE_INT_OFFLOAD_SYNC BIT(4) + +#define SPI_ENGINE_OFFLOAD_CTRL_ENABLE BIT(0) =20 #define SPI_ENGINE_CONFIG_CPHA BIT(0) #define SPI_ENGINE_CONFIG_CPOL BIT(1) @@ -79,6 +100,10 @@ #define SPI_ENGINE_CMD_CS_INV(flags) \ SPI_ENGINE_CMD(SPI_ENGINE_INST_CS_INV, 0, (flags)) =20 +/* default sizes - can be changed when SPI Engine firmware is compiled */ +#define SPI_ENGINE_OFFLOAD_CMD_FIFO_SIZE 16 +#define SPI_ENGINE_OFFLOAD_SDO_FIFO_SIZE 16 + struct spi_engine_program { unsigned int length; uint16_t instructions[] __counted_by(length); @@ -106,6 +131,17 @@ struct spi_engine_message_state { uint8_t *rx_buf; }; =20 +enum { + SPI_ENGINE_OFFLOAD_FLAG_ASSIGNED, + SPI_ENGINE_OFFLOAD_FLAG_PREPARED, +}; + +struct spi_engine_offload { + struct spi_engine *spi_engine; + unsigned long flags; + unsigned int offload_num; +}; + struct spi_engine { struct clk *clk; struct clk *ref_clk; @@ -118,6 +154,11 @@ struct spi_engine { unsigned int int_enable; /* shadows hardware CS inversion flag state */ u8 cs_inv; + + unsigned int offload_ctrl_mem_size; + unsigned int offload_sdo_mem_size; + struct spi_offload *offload; + u32 offload_caps; }; =20 static void spi_engine_program_add_cmd(struct spi_engine_program *p, @@ -163,9 +204,9 @@ static void spi_engine_gen_xfer(struct spi_engine_progr= am *p, bool dry, unsigned int n =3D min(len, 256U); unsigned int flags =3D 0; =20 - if (xfer->tx_buf) + if (xfer->tx_buf || (xfer->offload_flags & SPI_OFFLOAD_XFER_TX_STREAM)) flags |=3D SPI_ENGINE_TRANSFER_WRITE; - if (xfer->rx_buf) + if (xfer->rx_buf || (xfer->offload_flags & SPI_OFFLOAD_XFER_RX_STREAM)) flags |=3D SPI_ENGINE_TRANSFER_READ; =20 spi_engine_program_add_cmd(p, dry, @@ -217,16 +258,24 @@ static void spi_engine_gen_cs(struct spi_engine_progr= am *p, bool dry, * * NB: This is separate from spi_engine_compile_message() because the latt= er * is called twice and would otherwise result in double-evaluation. + * + * Returns 0 on success, -EINVAL on failure. */ -static void spi_engine_precompile_message(struct spi_message *msg) +static int spi_engine_precompile_message(struct spi_message *msg) { unsigned int clk_div, max_hz =3D msg->spi->controller->max_speed_hz; struct spi_transfer *xfer; =20 list_for_each_entry(xfer, &msg->transfers, transfer_list) { + /* If we have an offload transfer, we can't rx to buffer */ + if (msg->offload && xfer->rx_buf) + return -EINVAL; + clk_div =3D DIV_ROUND_UP(max_hz, xfer->speed_hz); xfer->effective_speed_hz =3D max_hz / min(clk_div, 256U); } + + return 0; } =20 static void spi_engine_compile_message(struct spi_message *msg, bool dry, @@ -521,11 +570,105 @@ static irqreturn_t spi_engine_irq(int irq, void *dev= id) return IRQ_HANDLED; } =20 +static int spi_engine_offload_prepare(struct spi_message *msg) +{ + struct spi_controller *host =3D msg->spi->controller; + struct spi_engine *spi_engine =3D spi_controller_get_devdata(host); + struct spi_engine_program *p =3D msg->opt_state; + struct spi_engine_offload *priv =3D msg->offload->priv; + struct spi_transfer *xfer; + void __iomem *cmd_addr; + void __iomem *sdo_addr; + size_t tx_word_count =3D 0; + unsigned int i; + + if (p->length > spi_engine->offload_ctrl_mem_size) + return -EINVAL; + + /* count total number of tx words in message */ + list_for_each_entry(xfer, &msg->transfers, transfer_list) { + /* no support for reading to rx_buf */ + if (xfer->rx_buf) + return -EINVAL; + + if (!xfer->tx_buf) + continue; + + if (xfer->bits_per_word <=3D 8) + tx_word_count +=3D xfer->len; + else if (xfer->bits_per_word <=3D 16) + tx_word_count +=3D xfer->len / 2; + else + tx_word_count +=3D xfer->len / 4; + } + + if (tx_word_count && !(spi_engine->offload_caps & SPI_OFFLOAD_CAP_TX_STAT= IC_DATA)) + return -EINVAL; + + if (tx_word_count > spi_engine->offload_sdo_mem_size) + return -EINVAL; + + /* + * This protects against calling spi_optimize_message() with an offload + * that has already been prepared with a different message. + */ + if (test_and_set_bit_lock(SPI_ENGINE_OFFLOAD_FLAG_PREPARED, &priv->flags)) + return -EBUSY; + + cmd_addr =3D spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_CMD_FIFO(priv->offload_num); + sdo_addr =3D spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_SDO_FIFO(priv->offload_num); + + list_for_each_entry(xfer, &msg->transfers, transfer_list) { + if (!xfer->tx_buf) + continue; + + if (xfer->bits_per_word <=3D 8) { + const u8 *buf =3D xfer->tx_buf; + + for (i =3D 0; i < xfer->len; i++) + writel_relaxed(buf[i], sdo_addr); + } else if (xfer->bits_per_word <=3D 16) { + const u16 *buf =3D xfer->tx_buf; + + for (i =3D 0; i < xfer->len / 2; i++) + writel_relaxed(buf[i], sdo_addr); + } else { + const u32 *buf =3D xfer->tx_buf; + + for (i =3D 0; i < xfer->len / 4; i++) + writel_relaxed(buf[i], sdo_addr); + } + } + + for (i =3D 0; i < p->length; i++) + writel_relaxed(p->instructions[i], cmd_addr); + + return 0; +} + +static void spi_engine_offload_unprepare(struct spi_offload *offload) +{ + struct spi_engine_offload *priv =3D offload->priv; + struct spi_engine *spi_engine =3D priv->spi_engine; + + writel_relaxed(1, spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_RESET(priv->offload_num)); + writel_relaxed(0, spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_RESET(priv->offload_num)); + + clear_bit_unlock(SPI_ENGINE_OFFLOAD_FLAG_PREPARED, &priv->flags); +} + static int spi_engine_optimize_message(struct spi_message *msg) { struct spi_engine_program p_dry, *p; + int ret; =20 - spi_engine_precompile_message(msg); + ret =3D spi_engine_precompile_message(msg); + if (ret) + return ret; =20 p_dry.length =3D 0; spi_engine_compile_message(msg, true, &p_dry); @@ -537,20 +680,61 @@ static int spi_engine_optimize_message(struct spi_mes= sage *msg) spi_engine_compile_message(msg, false, p); =20 spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SYNC( - AXI_SPI_ENGINE_CUR_MSG_SYNC_ID)); + msg->offload ? 0 : AXI_SPI_ENGINE_CUR_MSG_SYNC_ID)); =20 msg->opt_state =3D p; =20 + if (msg->offload) { + ret =3D spi_engine_offload_prepare(msg); + if (ret) { + msg->opt_state =3D NULL; + kfree(p); + return ret; + } + } + return 0; } =20 static int spi_engine_unoptimize_message(struct spi_message *msg) { + if (msg->offload) + spi_engine_offload_unprepare(msg->offload); + kfree(msg->opt_state); =20 return 0; } =20 +static struct spi_offload +*spi_engine_get_offload(struct spi_device *spi, + const struct spi_offload_config *config) +{ + struct spi_controller *host =3D spi->controller; + struct spi_engine *spi_engine =3D spi_controller_get_devdata(host); + struct spi_engine_offload *priv; + + if (!spi_engine->offload) + return ERR_PTR(-ENODEV); + + if (config->capability_flags & ~spi_engine->offload_caps) + return ERR_PTR(-EINVAL); + + priv =3D spi_engine->offload->priv; + + if (test_and_set_bit_lock(SPI_ENGINE_OFFLOAD_FLAG_ASSIGNED, &priv->flags)) + return ERR_PTR(-EBUSY); + + return spi_engine->offload; +} + +static void spi_engine_put_offload(struct spi_offload *offload) +{ + struct spi_engine_offload *priv =3D offload->priv; + + clear_bit_unlock(SPI_ENGINE_OFFLOAD_FLAG_ASSIGNED, &priv->flags); +} + static int spi_engine_setup(struct spi_device *device) { struct spi_controller *host =3D device->controller; @@ -583,6 +767,12 @@ static int spi_engine_transfer_one_message(struct spi_= controller *host, unsigned int int_enable =3D 0; unsigned long flags; =20 + if (msg->offload) { + dev_err(&host->dev, "Single transfer offload not supported\n"); + msg->status =3D -EOPNOTSUPP; + goto out; + } + /* reinitialize message state for this transfer */ memset(st, 0, sizeof(*st)); st->cmd_buf =3D p->instructions; @@ -632,11 +822,68 @@ static int spi_engine_transfer_one_message(struct spi= _controller *host, trace_spi_transfer_stop(msg, xfer); } =20 +out: spi_finalize_current_message(host); =20 return msg->status; } =20 +static int spi_engine_trigger_enable(struct spi_offload *offload) +{ + struct spi_engine_offload *priv =3D offload->priv; + struct spi_engine *spi_engine =3D priv->spi_engine; + unsigned int reg; + + reg =3D readl_relaxed(spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_CTRL(priv->offload_num)); + reg |=3D SPI_ENGINE_OFFLOAD_CTRL_ENABLE; + writel_relaxed(reg, spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_CTRL(priv->offload_num)); + return 0; +} + +static void spi_engine_trigger_disable(struct spi_offload *offload) +{ + struct spi_engine_offload *priv =3D offload->priv; + struct spi_engine *spi_engine =3D priv->spi_engine; + unsigned int reg; + + reg =3D readl_relaxed(spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_CTRL(priv->offload_num)); + reg &=3D ~SPI_ENGINE_OFFLOAD_CTRL_ENABLE; + writel_relaxed(reg, spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_CTRL(priv->offload_num)); +} + +static struct dma_chan +*spi_engine_tx_stream_request_dma_chan(struct spi_offload *offload) +{ + struct spi_engine_offload *priv =3D offload->priv; + char name[16]; + + snprintf(name, sizeof(name), "offload%u-tx", priv->offload_num); + + return dma_request_chan(offload->provider_dev, name); +} + +static struct dma_chan +*spi_engine_rx_stream_request_dma_chan(struct spi_offload *offload) +{ + struct spi_engine_offload *priv =3D offload->priv; + char name[16]; + + snprintf(name, sizeof(name), "offload%u-rx", priv->offload_num); + + return dma_request_chan(offload->provider_dev, name); +} + +static const struct spi_offload_ops spi_engine_offload_ops =3D { + .trigger_enable =3D spi_engine_trigger_enable, + .trigger_disable =3D spi_engine_trigger_disable, + .tx_stream_request_dma_chan =3D spi_engine_tx_stream_request_dma_chan, + .rx_stream_request_dma_chan =3D spi_engine_rx_stream_request_dma_chan, +}; + static void spi_engine_release_hw(void *p) { struct spi_engine *spi_engine =3D p; @@ -651,8 +898,7 @@ static int spi_engine_probe(struct platform_device *pde= v) struct spi_engine *spi_engine; struct spi_controller *host; unsigned int version; - int irq; - int ret; + int irq, ret; =20 irq =3D platform_get_irq(pdev, 0); if (irq < 0) @@ -667,6 +913,46 @@ static int spi_engine_probe(struct platform_device *pd= ev) spin_lock_init(&spi_engine->lock); init_completion(&spi_engine->msg_complete); =20 + /* + * REVISIT: for now, all SPI Engines only have one offload. In the + * future, this should be read from a memory mapped register to + * determine the number of offloads enabled at HDL compile time. For + * now, we can tell if an offload is present if there is a trigger + * source wired up to it. + */ + if (device_property_present(&pdev->dev, "trigger-sources")) { + struct spi_engine_offload *priv; + + spi_engine->offload =3D + devm_spi_offload_alloc(&pdev->dev, + sizeof(struct spi_engine_offload)); + if (IS_ERR(spi_engine->offload)) + return PTR_ERR(spi_engine->offload); + + priv =3D spi_engine->offload->priv; + priv->spi_engine =3D spi_engine; + priv->offload_num =3D 0; + + spi_engine->offload->ops =3D &spi_engine_offload_ops; + spi_engine->offload_caps =3D SPI_OFFLOAD_CAP_TRIGGER; + + if (device_property_match_string(&pdev->dev, "dma-names", "offload0-rx")= >=3D 0) { + spi_engine->offload_caps |=3D SPI_OFFLOAD_CAP_RX_STREAM_DMA; + spi_engine->offload->xfer_flags |=3D SPI_OFFLOAD_XFER_RX_STREAM; + } + + if (device_property_match_string(&pdev->dev, "dma-names", "offload0-tx")= >=3D 0) { + spi_engine->offload_caps |=3D SPI_OFFLOAD_CAP_TX_STREAM_DMA; + spi_engine->offload->xfer_flags |=3D SPI_OFFLOAD_XFER_TX_STREAM; + } else { + /* + * HDL compile option to enable TX DMA stream also disables + * the SDO memory, so can't do both at the same time. + */ + spi_engine->offload_caps |=3D SPI_OFFLOAD_CAP_TX_STATIC_DATA; + } + } + spi_engine->clk =3D devm_clk_get_enabled(&pdev->dev, "s_axi_aclk"); if (IS_ERR(spi_engine->clk)) return PTR_ERR(spi_engine->clk); @@ -688,6 +974,19 @@ static int spi_engine_probe(struct platform_device *pd= ev) return -ENODEV; } =20 + if (ADI_AXI_PCORE_VER_MINOR(version) >=3D 1) { + unsigned int sizes =3D readl(spi_engine->base + + SPI_ENGINE_REG_OFFLOAD_MEM_ADDR_WIDTH); + + spi_engine->offload_ctrl_mem_size =3D 1 << + FIELD_GET(SPI_ENGINE_SPI_OFFLOAD_MEM_WIDTH_CMD, sizes); + spi_engine->offload_sdo_mem_size =3D 1 << + FIELD_GET(SPI_ENGINE_SPI_OFFLOAD_MEM_WIDTH_SDO, sizes); + } else { + spi_engine->offload_ctrl_mem_size =3D SPI_ENGINE_OFFLOAD_CMD_FIFO_SIZE; + spi_engine->offload_sdo_mem_size =3D SPI_ENGINE_OFFLOAD_SDO_FIFO_SIZE; + } + writel_relaxed(0x00, spi_engine->base + SPI_ENGINE_REG_RESET); writel_relaxed(0xff, spi_engine->base + SPI_ENGINE_REG_INT_PENDING); writel_relaxed(0x00, spi_engine->base + SPI_ENGINE_REG_INT_ENABLE); @@ -709,6 +1008,8 @@ static int spi_engine_probe(struct platform_device *pd= ev) host->transfer_one_message =3D spi_engine_transfer_one_message; host->optimize_message =3D spi_engine_optimize_message; host->unoptimize_message =3D spi_engine_unoptimize_message; + host->get_offload =3D spi_engine_get_offload; + host->put_offload =3D spi_engine_put_offload; host->num_chipselect =3D 8; =20 /* Some features depend of the IP core version. */ --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A42691A0BF1 for ; Fri, 7 Feb 2025 20:09:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958982; cv=none; b=FUE9cCfx4bQ+cJLvwBQdfnZcQYdyjd4QpGE1amCYUH2Yub1fKJZUAFtUZNlf9VYBcAM7Gt/SRnoK/HngIi9+7y7QCX7Dp7p9Dr3AIWn7ifoa2ln7qHTu1nhZLke/z2QU1ZsI5/MUwkMDch9O1R4sotVdMsjmjjrTohbZlYtkR8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958982; c=relaxed/simple; bh=WTRyrDP74Be0UzziUCgrAt1IiqdEqmSclPtrQfncLY4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=B0Du4/ucy3D2SOejoFbVfJvCiJK2PSQuRrP31uBR+m5yPeb1+Pxk+V9zxU1QLkbqLGRDyEymNaV/hgXxjRSgrYhGn7Ddj1otQsFfE7zxg/wLr4iNw/vIyev+BiQjYHqdQplhSitveUOfMEm7LMksau1JK9kC4Rt7Wnz+hAXmTbY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=Y661FKNP; arc=none smtp.client-ip=209.85.210.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="Y661FKNP" Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-71ded02b779so1411206a34.2 for ; Fri, 07 Feb 2025 12:09:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958979; x=1739563779; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=pdMxJLVUyi4W3/XAWZvTW+GcXmKoS+IN/gS3t7thlds=; b=Y661FKNPqJqCh1hpvQ4Tno47aIqz5DvxhlO2GhL2FXNXXH7GU6yQXaxqrkFWWzXm+y irsbyI0mt/jcFkoBAb1CBan9ov3WVxsC4Z4R/ac1P7ENbNmI+8P5yeyHA+gseTVME18T YUmVxDF9WLQ6iIYyUMAqKjtF8fhsj9O4IFq8KojqhbA8kVrc8vlenfAdQD6jWsDJL+Ng nhiBQXGdKODUNvht28P9jqUB5AWQc+2F9HWu3rL7RnlhBjWjXCO08Qs7XmHMkj0pC18T 2Yt4+nZOXNuMHpZm/mlOT96tJXcUjOdh51aN5lmuCDo5KQqJ7rNydp5UwQidgjx3RMzX OzdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958979; x=1739563779; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pdMxJLVUyi4W3/XAWZvTW+GcXmKoS+IN/gS3t7thlds=; b=bOCQ5f1AmivV6YGoZyeIf8K4UQvvhHjxZNENq6GbaNh/oticBppxPWEz4/qyI+o1m8 rA2nhoNrikkDOjYFekFxMwOIksYkYxj9IcdJ8nMjhePvf7eESaVRjD+SB3iIhqcvvu3W H4wF1bc1PLP4aiBl/bBxVWtQKaTJ1I8b73SoZcZuKoim5eNmGpjuZz1caWVnn4ng8wSj /2TVMDDJo6mB8Kxg5Ug09K/lfpxMSOdnVfa1DYZFS2A+XIWR67/0mDqGFKKDYnwROLOY h1OSnwba2HrkhmfYDM777/5f4+2PrY2qbzLfAB9Nm2/fU90+bHvEouPEziER++k6eX9g d6cw== X-Forwarded-Encrypted: i=1; AJvYcCUnyTMZomICYvqZF+RT4gT/gp49zipzGN1XPmp8eAhv1MjzEYZ/an6JVcoIqssDYvDMeZsUnGZ5AgsYY1Y=@vger.kernel.org X-Gm-Message-State: AOJu0Ywo5cm6MDaQwbNCxKq+Vk8uqrVitgyf3XeNcjU1lbAWSbwqZzHV qdl287cgVuAC5A1xPBCaPn1DqknG/OlOEGVyG8bjIPLG3vlq6UoJwIzHAvyNGK4= X-Gm-Gg: ASbGncseBYwX55YwKObzjPwvDF+P/5eJnukCsxkEGOI5AUC3mmO52TLxkDgE0OHlp+8 hAiDJlZCr+eP8t4r2FrZQFzyJScOe4u4LTU9zg0oUJk0ouHFAmue02YbpQu6NinDT7UhQrF4djL pHbXqHbuCCsG90QV1Fr/4RbVbWoiIGBJ4unUKtNp1hXOfrFud9YgE3EWiLO8nQfPRWwEILJuopT 9Knt/jo60PybRoj1DLWhpN1JOBLWD+I1QkiyhphVmDvkQi1J2Ojgxxbj2LqYwX6WRvOoykOoo1J vxNxX1Xk+4wKD0KPEAdGrsCr/DgJV4FM4/kNpEDFsdjOVBc= X-Google-Smtp-Source: AGHT+IH5sh7VymPtzuwCp1Fwk/GTYtt6arEUE6mdjwiN9mTxM2PbOcR5pHPX9Vehw6fyomKaJMOOAw== X-Received: by 2002:a05:6830:698d:b0:71d:fc52:1ab6 with SMTP id 46e09a7af769-726b88a3e27mr2735801a34.23.1738958979574; Fri, 07 Feb 2025 12:09:39 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:38 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:05 -0600 Subject: [PATCH v8 08/17] iio: buffer-dmaengine: split requesting DMA channel from allocating buffer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-8-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Refactor the IIO dmaengine buffer code to split requesting the DMA channel from allocating the buffer. We want to be able to add a new function where the IIO device driver manages the DMA channel, so these two actions need to be separate. To do this, calling dma_request_chan() is moved from iio_dmaengine_buffer_alloc() to iio_dmaengine_buffer_setup_ext(). A new __iio_dmaengine_buffer_setup_ext() helper function is added to simplify error unwinding and will also be used by a new function in a later patch. iio_dmaengine_buffer_free() now only frees the buffer and does not release the DMA channel. A new iio_dmaengine_buffer_teardown() function is added to unwind everything done in iio_dmaengine_buffer_setup_ext(). This keeps things more symmetrical with obvious pairs alloc/free and setup/teardown. Calling dma_get_slave_caps() in iio_dmaengine_buffer_alloc() is moved so that we can avoid any gotos for error unwinding. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Split out from patch that adds the new function * Dropped owns_chan flag * Introduced iio_dmaengine_buffer_teardown() so that iio_dmaengine_buffer_free() doesn't have to manage the DMA channel --- drivers/iio/adc/adi-axi-adc.c | 2 +- drivers/iio/buffer/industrialio-buffer-dmaengine.c | 106 ++++++++++++-----= ---- drivers/iio/dac/adi-axi-dac.c | 2 +- include/linux/iio/buffer-dmaengine.h | 2 +- 4 files changed, 65 insertions(+), 47 deletions(-) diff --git a/drivers/iio/adc/adi-axi-adc.c b/drivers/iio/adc/adi-axi-adc.c index c7357601f0f869e57636f00bb1e26c059c3ab15c..a55db308baabf7b26ea98431cab= 1e6af7fe2a5f3 100644 --- a/drivers/iio/adc/adi-axi-adc.c +++ b/drivers/iio/adc/adi-axi-adc.c @@ -305,7 +305,7 @@ static struct iio_buffer *axi_adc_request_buffer(struct= iio_backend *back, static void axi_adc_free_buffer(struct iio_backend *back, struct iio_buffer *buffer) { - iio_dmaengine_buffer_free(buffer); + iio_dmaengine_buffer_teardown(buffer); } =20 static int axi_adc_reg_access(struct iio_backend *back, unsigned int reg, diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/i= io/buffer/industrialio-buffer-dmaengine.c index 614e1c4189a9cdd5a8d9d8c5ef91566983032951..02847d3962fcbb43ec76167db64= 82ab951f20942 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -206,39 +206,29 @@ static const struct iio_dev_attr *iio_dmaengine_buffe= r_attrs[] =3D { =20 /** * iio_dmaengine_buffer_alloc() - Allocate new buffer which uses DMAengine - * @dev: DMA channel consumer device - * @channel: DMA channel name, typically "rx". + * @chan: DMA channel. * * This allocates a new IIO buffer which internally uses the DMAengine fra= mework - * to perform its transfers. The parent device will be used to request the= DMA - * channel. + * to perform its transfers. * * Once done using the buffer iio_dmaengine_buffer_free() should be used to * release it. */ -static struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev, - const char *channel) +static struct iio_buffer *iio_dmaengine_buffer_alloc(struct dma_chan *chan) { struct dmaengine_buffer *dmaengine_buffer; unsigned int width, src_width, dest_width; struct dma_slave_caps caps; - struct dma_chan *chan; int ret; =20 + ret =3D dma_get_slave_caps(chan, &caps); + if (ret < 0) + return ERR_PTR(ret); + dmaengine_buffer =3D kzalloc(sizeof(*dmaengine_buffer), GFP_KERNEL); if (!dmaengine_buffer) return ERR_PTR(-ENOMEM); =20 - chan =3D dma_request_chan(dev, channel); - if (IS_ERR(chan)) { - ret =3D PTR_ERR(chan); - goto err_free; - } - - ret =3D dma_get_slave_caps(chan, &caps); - if (ret < 0) - goto err_release; - /* Needs to be aligned to the maximum of the minimums */ if (caps.src_addr_widths) src_width =3D __ffs(caps.src_addr_widths); @@ -262,12 +252,6 @@ static struct iio_buffer *iio_dmaengine_buffer_alloc(s= truct device *dev, dmaengine_buffer->queue.buffer.access =3D &iio_dmaengine_buffer_ops; =20 return &dmaengine_buffer->queue.buffer; - -err_release: - dma_release_channel(chan); -err_free: - kfree(dmaengine_buffer); - return ERR_PTR(ret); } =20 /** @@ -276,17 +260,57 @@ static struct iio_buffer *iio_dmaengine_buffer_alloc(= struct device *dev, * * Frees a buffer previously allocated with iio_dmaengine_buffer_alloc(). */ -void iio_dmaengine_buffer_free(struct iio_buffer *buffer) +static void iio_dmaengine_buffer_free(struct iio_buffer *buffer) { struct dmaengine_buffer *dmaengine_buffer =3D iio_buffer_to_dmaengine_buffer(buffer); =20 iio_dma_buffer_exit(&dmaengine_buffer->queue); - dma_release_channel(dmaengine_buffer->chan); - iio_buffer_put(buffer); } -EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_free, "IIO_DMAENGINE_BUFFER"); + +/** + * iio_dmaengine_buffer_teardown() - Releases DMA channel and frees buffer + * @buffer: Buffer to free + * + * Releases the DMA channel and frees the buffer previously setup with + * iio_dmaengine_buffer_setup_ext(). + */ +void iio_dmaengine_buffer_teardown(struct iio_buffer *buffer) +{ + struct dmaengine_buffer *dmaengine_buffer =3D + iio_buffer_to_dmaengine_buffer(buffer); + struct dma_chan *chan =3D dmaengine_buffer->chan; + + iio_dmaengine_buffer_free(buffer); + dma_release_channel(chan); +} +EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_teardown, "IIO_DMAENGINE_BUFFER"= ); + +static struct iio_buffer +*__iio_dmaengine_buffer_setup_ext(struct iio_dev *indio_dev, + struct dma_chan *chan, + enum iio_buffer_direction dir) +{ + struct iio_buffer *buffer; + int ret; + + buffer =3D iio_dmaengine_buffer_alloc(chan); + if (IS_ERR(buffer)) + return ERR_CAST(buffer); + + indio_dev->modes |=3D INDIO_BUFFER_HARDWARE; + + buffer->direction =3D dir; + + ret =3D iio_device_attach_buffer(indio_dev, buffer); + if (ret) { + iio_dmaengine_buffer_free(buffer); + return ERR_PTR(ret); + } + + return buffer; +} =20 /** * iio_dmaengine_buffer_setup_ext() - Setup a DMA buffer for an IIO device @@ -300,7 +324,7 @@ EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_free, "IIO_DM= AENGINE_BUFFER"); * It also appends the INDIO_BUFFER_HARDWARE mode to the supported modes o= f the * IIO device. * - * Once done using the buffer iio_dmaengine_buffer_free() should be used to + * Once done using the buffer iio_dmaengine_buffer_teardown() should be us= ed to * release it. */ struct iio_buffer *iio_dmaengine_buffer_setup_ext(struct device *dev, @@ -308,30 +332,24 @@ struct iio_buffer *iio_dmaengine_buffer_setup_ext(str= uct device *dev, const char *channel, enum iio_buffer_direction dir) { + struct dma_chan *chan; struct iio_buffer *buffer; - int ret; - - buffer =3D iio_dmaengine_buffer_alloc(dev, channel); - if (IS_ERR(buffer)) - return ERR_CAST(buffer); - - indio_dev->modes |=3D INDIO_BUFFER_HARDWARE; =20 - buffer->direction =3D dir; + chan =3D dma_request_chan(dev, channel); + if (IS_ERR(chan)) + return ERR_CAST(chan); =20 - ret =3D iio_device_attach_buffer(indio_dev, buffer); - if (ret) { - iio_dmaengine_buffer_free(buffer); - return ERR_PTR(ret); - } + buffer =3D __iio_dmaengine_buffer_setup_ext(indio_dev, chan, dir); + if (IS_ERR(buffer)) + dma_release_channel(chan); =20 return buffer; } EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_setup_ext, "IIO_DMAENGINE_BUFFER= "); =20 -static void __devm_iio_dmaengine_buffer_free(void *buffer) +static void devm_iio_dmaengine_buffer_teardown(void *buffer) { - iio_dmaengine_buffer_free(buffer); + iio_dmaengine_buffer_teardown(buffer); } =20 /** @@ -357,7 +375,7 @@ int devm_iio_dmaengine_buffer_setup_ext(struct device *= dev, if (IS_ERR(buffer)) return PTR_ERR(buffer); =20 - return devm_add_action_or_reset(dev, __devm_iio_dmaengine_buffer_free, + return devm_add_action_or_reset(dev, devm_iio_dmaengine_buffer_teardown, buffer); } EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup_ext, "IIO_DMAENGINE_B= UFFER"); diff --git a/drivers/iio/dac/adi-axi-dac.c b/drivers/iio/dac/adi-axi-dac.c index ac4c96c4ccf37d752372b57acfcb8ad098930b58..7ed934dfc5fc9f08d59b2b0c335= 37669558ec884 100644 --- a/drivers/iio/dac/adi-axi-dac.c +++ b/drivers/iio/dac/adi-axi-dac.c @@ -168,7 +168,7 @@ static struct iio_buffer *axi_dac_request_buffer(struct= iio_backend *back, static void axi_dac_free_buffer(struct iio_backend *back, struct iio_buffer *buffer) { - iio_dmaengine_buffer_free(buffer); + iio_dmaengine_buffer_teardown(buffer); } =20 enum { diff --git a/include/linux/iio/buffer-dmaengine.h b/include/linux/iio/buffe= r-dmaengine.h index 81d9a19aeb9199dd58bb9d35a91f0ec4b00846df..72a2e3fd8a5bf5e8f27ee226ddd= 92979d233754b 100644 --- a/include/linux/iio/buffer-dmaengine.h +++ b/include/linux/iio/buffer-dmaengine.h @@ -12,7 +12,7 @@ struct iio_dev; struct device; =20 -void iio_dmaengine_buffer_free(struct iio_buffer *buffer); +void iio_dmaengine_buffer_teardown(struct iio_buffer *buffer); struct iio_buffer *iio_dmaengine_buffer_setup_ext(struct device *dev, struct iio_dev *indio_dev, const char *channel, --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 543F01D63CE for ; Fri, 7 Feb 2025 20:09:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958984; cv=none; b=YUwBUTz3fufru2Hub1JBHhD7rBZjBQrWbpXnIjHkq6GYouREq6wYtT9jTLcYzkl0kjZwGJdoknJjYtLlX//fm2xN6XKGKC+iQleOIXj4KaKCXA760pmtspkIODA0FToGFSqInuIjWXOJg0wkukUd1Z1Yk6GDFTIrtsJtonGa0ks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958984; c=relaxed/simple; bh=dzCJ6ZEQTNY+z2WK8kNiO9rUSbrCIMXdjhs/EOFJ84k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sWZMOzm1xznWC5FVnlddyPRycYvy9WIvvEBnr95nFG6RFFOcGlmuN5OMncUR3yuyO9/AsaR9ULXeI1ZfmTGXzgV81EO5HJfx1sGNfSYguNTKwA2Cj1sOoCtzGF3uqGB5Uya6xoxEIU2rkGY+l7+dYwCfVKkpY4Npxgg2Dse6yOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=DI6HWuea; arc=none smtp.client-ip=209.85.210.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="DI6HWuea" Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-71e157a79c8so738561a34.2 for ; Fri, 07 Feb 2025 12:09:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958981; x=1739563781; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NxYYb5ciTauJBruXQRFcV6o6UAzHMKHqZuEnooTWOkc=; b=DI6HWueadtJhGR5uJi9e9KVYO1+A5PR+jFKrX2fKL6yrbH0xI3CTA3i4DKhg17hVxw FzKbCk4Xm9qSM0Wxq8OHel0A3SLoj1EhG2nMOC8nPwYzm4TpZf8pD9KNrmN5oITEpnf3 IeRPpytQHtSPIvmZEMcZUEB5g+gZexgD54P8eVFD8jxmcRoxS+9G4eKiYEZTUU2j2eg4 Fika9spuMefO/ET7BUTU5lg61Mq0m3+FY02jkv36vFIa0vBoFAh7Wizdr6pEeSAINlvV dunDB6wZl+guyngpq5cKk/qfsnkkuZziBFcCSnYKrFxHMxqtj5BYmPc+EIWS4lqTVkZS 02LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958981; x=1739563781; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NxYYb5ciTauJBruXQRFcV6o6UAzHMKHqZuEnooTWOkc=; b=PAhmVO4Kn55aZnHsiuufvua4tcTvs8O1MV7lj0XnAb5gEXMPwK9n1fDrWfKYKs0fUR Qx3WXSr7//UJ+8N4QhnU1Hizsir/IgrFHS9mU4mLg/LOFak8u+iIF74Sp9ELCcJnGw2z 9UbMmOcFxiceHCQIijB2S7AXsPA0r9G6/fT4CypnoDV0rImcGn3noqgsn2p+yix9ZSul ku1LqiUUUplYsvim54HFlCZ7d/Vmwiwjf/YT8IV5MI5Ud9s/kcctp3FAet3wSDGL441A /DpZbmcT+PKA0d14JPK/ZkSDPujHVGe9qPnKD4VtI6KBx07oVIqxNamWYtSYpXngURwJ Z0eg== X-Forwarded-Encrypted: i=1; AJvYcCXnom9G4l0ul1wsCuv5Iy4eZC6sc0GbDvMZ4nSv8+EViha5qRC5zTQsb4N7ADrf/iE0p++akKRHVuWoVOM=@vger.kernel.org X-Gm-Message-State: AOJu0YwBYFYfHNMIflnZp4FJ1fHDw7Y7sya6qtUIhiou4VzpEkpi+Cnd Zf5Ksl+/D8wqZ8j9IDODOtDWC9tTwl8NUMSx0weAW7o6+OpBlN1m22KCvnvvkYU= X-Gm-Gg: ASbGncsktX4Ox0ToFqviyrAyChs+fC2TKkblZLJDISmKkaM6ycm0AwW6GN+xVPwTzxL EderFKiDnsfh8wnNkInDW2PPfl1qXLgPIEgFKfEtwSuejzhDZAPtjdoqXSJxSxGJoOmjVvt5Tce KDqSGKI24eQRhwqcWba3raTiAoJ6L742Hd+zKjqU6D+ZAVLc533wCFGXOzLwSIST6Y4bH1fyvrj LWUuq/wgiVxgjnnNdsZeDjTXDdtWusPDeIMztM+VbxodDCAyL0otzwo4rwzYm17Sw+6IO1laFlI +NQ2utzh/hHah9wSg/qqO53x02k2U6U5OgOfcrbKHiSjaxI= X-Google-Smtp-Source: AGHT+IGK4xYaEcxApiz7m2CUhej55WwAoBbiU+s2jpWUeCLtHUqStHqG/Dhk5gCjtlNGdzV0gWWPyQ== X-Received: by 2002:a05:6830:4701:b0:71e:1392:80b5 with SMTP id 46e09a7af769-726b889856cmr3814221a34.24.1738958981445; Fri, 07 Feb 2025 12:09:41 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:41 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:06 -0600 Subject: [PATCH v8 09/17] iio: buffer-dmaengine: add devm_iio_dmaengine_buffer_setup_with_handle() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-9-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add a new devm_iio_dmaengine_buffer_setup_with_handle() function to handle cases where the DMA channel is managed by the caller rather than being requested and released by the iio_dmaengine module. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Rename from devm_iio_dmaengine_buffer_setup_ext2() * This patch now just adds the new function - prep work was split out to a separate patch v5 changes: none v4 changes: * This replaces "iio: buffer-dmaengine: generalize requesting DMA channel" --- drivers/iio/buffer/industrialio-buffer-dmaengine.c | 38 ++++++++++++++++++= ++++ include/linux/iio/buffer-dmaengine.h | 5 +++ 2 files changed, 43 insertions(+) diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/i= io/buffer/industrialio-buffer-dmaengine.c index 02847d3962fcbb43ec76167db6482ab951f20942..e9d9a7d39fe191c2b6e8c196a08= cdd26cd3a8d4b 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -380,6 +380,44 @@ int devm_iio_dmaengine_buffer_setup_ext(struct device = *dev, } EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup_ext, "IIO_DMAENGINE_B= UFFER"); =20 +static void devm_iio_dmaengine_buffer_free(void *buffer) +{ + iio_dmaengine_buffer_free(buffer); +} + +/** + * devm_iio_dmaengine_buffer_setup_with_handle() - Setup a DMA buffer for = an + * IIO device + * @dev: Device for devm ownership + * @indio_dev: IIO device to which to attach this buffer. + * @chan: DMA channel + * @dir: Direction of buffer (in or out) + * + * This allocates a new IIO buffer with devm_iio_dmaengine_buffer_alloc() + * and attaches it to an IIO device with iio_device_attach_buffer(). + * It also appends the INDIO_BUFFER_HARDWARE mode to the supported modes o= f the + * IIO device. + * + * This is the same as devm_iio_dmaengine_buffer_setup_ext() except that t= he + * caller manages requesting and releasing the DMA channel handle. + */ +int devm_iio_dmaengine_buffer_setup_with_handle(struct device *dev, + struct iio_dev *indio_dev, + struct dma_chan *chan, + enum iio_buffer_direction dir) +{ + struct iio_buffer *buffer; + + buffer =3D __iio_dmaengine_buffer_setup_ext(indio_dev, chan, dir); + if (IS_ERR(buffer)) + return PTR_ERR(buffer); + + return devm_add_action_or_reset(dev, devm_iio_dmaengine_buffer_free, + buffer); +} +EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup_with_handle, + "IIO_DMAENGINE_BUFFER"); + MODULE_AUTHOR("Lars-Peter Clausen "); MODULE_DESCRIPTION("DMA buffer for the IIO framework"); MODULE_LICENSE("GPL"); diff --git a/include/linux/iio/buffer-dmaengine.h b/include/linux/iio/buffe= r-dmaengine.h index 72a2e3fd8a5bf5e8f27ee226ddd92979d233754b..37f27545f69f761c3327c307cc6= 311b02a751096 100644 --- a/include/linux/iio/buffer-dmaengine.h +++ b/include/linux/iio/buffer-dmaengine.h @@ -11,6 +11,7 @@ =20 struct iio_dev; struct device; +struct dma_chan; =20 void iio_dmaengine_buffer_teardown(struct iio_buffer *buffer); struct iio_buffer *iio_dmaengine_buffer_setup_ext(struct device *dev, @@ -26,6 +27,10 @@ int devm_iio_dmaengine_buffer_setup_ext(struct device *d= ev, struct iio_dev *indio_dev, const char *channel, enum iio_buffer_direction dir); +int devm_iio_dmaengine_buffer_setup_with_handle(struct device *dev, + struct iio_dev *indio_dev, + struct dma_chan *chan, + enum iio_buffer_direction dir); =20 #define devm_iio_dmaengine_buffer_setup(dev, indio_dev, channel) \ devm_iio_dmaengine_buffer_setup_ext(dev, indio_dev, channel, \ --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39A1F1A314F for ; Fri, 7 Feb 2025 20:09:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958987; cv=none; b=JqnduiV4xzerigQnhymCQNh536mMiKZNAYH1xATF+RhwEPhfSTy10Xs5a+y8zKmObNCFkL5JrL8ElIFL9AEAQe52t4Uy5NWCjrQ7zOhl0Ip9jjdi+bOutfy8UDH+6nESpNvpCmoHyA3/rWCMSDHfMFm/3Qe0asfHzUuHQS4kUHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958987; c=relaxed/simple; bh=5/ZCGTFk8qr5Qpjjmv0qoJEV9Q10DfHDZbzjSTo2suU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OcmGHpheWMPwNY77OgaJdqxPRHUI5Bq+E/VxDoup8Z5ivkKeZOMb2fTMmiLoGvmOPMwwNtZhCSpI+CCnCiobqJwOuklTt0PRTjdEQ3snWhQ7x+Njro3/s82zKrekDf6ILuDyO7lJ+q0LtR4rZfDGoZ6OmjTVJerXkznJmto0BP0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=em3eQmMW; arc=none smtp.client-ip=209.85.161.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="em3eQmMW" Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-5f4c111991bso1314346eaf.0 for ; Fri, 07 Feb 2025 12:09:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958984; x=1739563784; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=qltVAB3O9f/S/slNVDINiHvuxo8d2MRRS1EWEiDjZ8w=; b=em3eQmMWh57izLwo6bnPibzw9MboUcNCJsSyAsTD/E68XI8sEcc1ZP4UxeTKcW5+Wl a4b1Y9dHPLM4nGkQTzh19hravRkaChGwZRlDVNATYcDFZWvNvBmNSTfl2QCnZEPu5sF0 31QdW7QdqVISLn/FDwsnaNJhMPQjwKiiVEG4NEAW1IPcy3m70k6zQNw8zJv+FlGi4QMF 78QgZ/ezxBT01kB+8PJBcHY4gfhhtdPnPg0Qo38qNuZ3ae5dvKqO0OPvC3492mAhxpKh FUVNkkariWu1cCkOnjh1SmQUgGPI9Yy5cXp6p2aFu0lKHnXAJKznw1w2k6/gmOm+y5Bw fG2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958984; x=1739563784; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qltVAB3O9f/S/slNVDINiHvuxo8d2MRRS1EWEiDjZ8w=; b=CTFSrKv3dlxcS2ab2gUuOPbIZ5h2YYUUWRoMSkuXBFhqDKyIjGX24Tipqtfyj87SZF 6UAfIGk4vrKCBeSICh0iyo1XnpthNifDCFivw+uzmI172ubcwsKgfJ6uUpWwL5AUxwpT W2mC7e5eHqRvGnUy4Z5r7/RQZySsmOpOcPD+o+g5d+DJQSoJQeYy6WJgeoaS8Q0zioIp z8y/Xqamrwvn+RBus9lwvJE63+S4gSyNpU4V0M6MOjZ7aD5DlgniUSPd626tei8cnKzB cIGbLlmJpiBaof6V4xqSVtkK4tRcPEAEgl1BvIj3ewmafbckf8G1CkyicfaLSot7ffh9 6U4w== X-Forwarded-Encrypted: i=1; AJvYcCXa9kJ+txR8jP3d1sOeBlmz4DjpAgrT1OaOUiUsyjryXfGXHN2KDilXhzfYPajLLhdE44qo2r1+VCny51U=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6LpguKg8II1HK4/e7b69rCfPU20jKhrNXuYk38zjradIib8hk Froq9QSG8IHnGul2nhF41tQuqDgNKZ7X2VBh6XK2rVf9Vn/NBAOcOLC0ydVf91w= X-Gm-Gg: ASbGncvZmGYDIOXAjz1wLKaQQf/Q53W0SewFtGnG/kNBs/pbgxznSmeJbeafOKR96lm 6MXLJrmF7eu0Lc6O/gr1xJ3ZcEffRD3DSPESS2nzp2pJ8TC9O+mlgYvDhun8skx94mfsQTdaQ6x A1xI44aF+8aDSSrpDpa1qtXO8dMHeAs7xaKQ3VL0hLpWtPc+BZxR4WVnmd1W0PbeSJpKHl6YyuX 4+Bk5GTyDjC9pskqX7NxHQL/OyxnPjlovuD+YWw8n3f74uADwhBcQ/7JVfYBB0AU0COkjp0wMwi czE9TsrPzI1vyMN7/p4KaTWObPkbyufPVNYjnbZxlvt/NmU= X-Google-Smtp-Source: AGHT+IHIDAGX3BhQWlqRFA1YpXfP5CHdNbSk4wUH4SYQ9UZGnd9dymzKZTDir+HrZG6DMWiSrEhmTg== X-Received: by 2002:a05:6820:491a:b0:5f2:c0df:7ed5 with SMTP id 006d021491bc7-5fc51f8763dmr5806165eaf.3.1738958984295; Fri, 07 Feb 2025 12:09:44 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:43 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:07 -0600 Subject: [PATCH v8 10/17] iio: adc: ad7944: don't use storagebits for sizing Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-10-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Replace use of storagebits with realbits for determining the number of bytes needed for SPI transfers. When adding SPI offload support, storagebits will always be 32 rather than 16 for 16-bit 16-bit chips so we can no longer rely on storagebits being the correct size expected by the SPI framework (it always uses 4 bytes for > 16-bit xfers and 2 bytes for > 8-bit xfers). Instead, derive the correct size from realbits since it will always be correct even when SPI offloading is used. Reviewed-by: Jonathan Cameron Reviewed-vy: Nuno Sa Signed-off-by: David Lechner --- v7 changes: * Make commit message more specific about numbers. v6 changes: none v5 changes: none v4 changes: new patch in v4 --- drivers/iio/adc/ad7944.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/iio/adc/ad7944.c b/drivers/iio/adc/ad7944.c index 0ec9cda10f5f8f61727581b152fb921f2e0b4bff..abfababcea10152fe1faa8afc2e= c1ea2dc40ae52 100644 --- a/drivers/iio/adc/ad7944.c +++ b/drivers/iio/adc/ad7944.c @@ -98,6 +98,9 @@ struct ad7944_chip_info { const struct iio_chan_spec channels[2]; }; =20 +/* get number of bytes for SPI xfer */ +#define AD7944_SPI_BYTES(scan_type) ((scan_type).realbits > 16 ? 4 : 2) + /* * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific c= hip * @_name: The name of the chip @@ -164,7 +167,7 @@ static int ad7944_3wire_cs_mode_init_msg(struct device = *dev, struct ad7944_adc * =20 /* Then we can read the data during the acquisition phase */ xfers[2].rx_buf =3D &adc->sample.raw; - xfers[2].len =3D BITS_TO_BYTES(chan->scan_type.storagebits); + xfers[2].len =3D AD7944_SPI_BYTES(chan->scan_type); xfers[2].bits_per_word =3D chan->scan_type.realbits; =20 spi_message_init_with_transfers(&adc->msg, xfers, 3); @@ -193,7 +196,7 @@ static int ad7944_4wire_mode_init_msg(struct device *de= v, struct ad7944_adc *adc xfers[0].delay.unit =3D SPI_DELAY_UNIT_NSECS; =20 xfers[1].rx_buf =3D &adc->sample.raw; - xfers[1].len =3D BITS_TO_BYTES(chan->scan_type.storagebits); + xfers[1].len =3D AD7944_SPI_BYTES(chan->scan_type); xfers[1].bits_per_word =3D chan->scan_type.realbits; =20 spi_message_init_with_transfers(&adc->msg, xfers, 2); @@ -228,7 +231,7 @@ static int ad7944_chain_mode_init_msg(struct device *de= v, struct ad7944_adc *adc xfers[0].delay.unit =3D SPI_DELAY_UNIT_NSECS; =20 xfers[1].rx_buf =3D adc->chain_mode_buf; - xfers[1].len =3D BITS_TO_BYTES(chan->scan_type.storagebits) * n_chain_dev; + xfers[1].len =3D AD7944_SPI_BYTES(chan->scan_type) * n_chain_dev; xfers[1].bits_per_word =3D chan->scan_type.realbits; =20 spi_message_init_with_transfers(&adc->msg, xfers, 2); @@ -274,12 +277,12 @@ static int ad7944_single_conversion(struct ad7944_adc= *adc, return ret; =20 if (adc->spi_mode =3D=3D AD7944_SPI_MODE_CHAIN) { - if (chan->scan_type.storagebits > 16) + if (chan->scan_type.realbits > 16) *val =3D ((u32 *)adc->chain_mode_buf)[chan->scan_index]; else *val =3D ((u16 *)adc->chain_mode_buf)[chan->scan_index]; } else { - if (chan->scan_type.storagebits > 16) + if (chan->scan_type.realbits > 16) *val =3D adc->sample.raw.u32; else *val =3D adc->sample.raw.u16; @@ -409,8 +412,7 @@ static int ad7944_chain_mode_alloc(struct device *dev, /* 1 word for each voltage channel + aligned u64 for timestamp */ =20 chain_mode_buf_size =3D ALIGN(n_chain_dev * - BITS_TO_BYTES(chan[0].scan_type.storagebits), sizeof(u64)) - + sizeof(u64); + AD7944_SPI_BYTES(chan[0].scan_type), sizeof(u64)) + sizeof(u64); buf =3D devm_kzalloc(dev, chain_mode_buf_size, GFP_KERNEL); if (!buf) return -ENOMEM; --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 364311DDA17 for ; Fri, 7 Feb 2025 20:09:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958989; cv=none; b=pvf+mra2Ks5BdmLTT7FwR4BuqUcedG3PMmOMqwOZGTYYcTpd16b8NZEO+X5Ko2WJ4GNdcnkmMYbdbXaygP2xiJ/8NhZsQH1HKFpUcHhOhqt+WeWmXlOBJCSoVpAfE0U/twM7Cg2tsKZlrG3t4BQulqo74+1q+FDul0MigL87YVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958989; c=relaxed/simple; bh=58uZ3p/lLonCxDjVnymxehCw+BubDL6vsa7KfRwysGs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oy7sD1aQjbbeHCokqAftoL+aIsuNfWDEUVTj8fQZTEQMBHFwDXtAphAMq1osTSL6xc4AqAHh+S6kDV+OmZogUjrxjcDu0DiJ48gkUZNBeXwkS/wHftmiafeuBwLM/b2kHvOZEDPAiiN8Kw4mOj2azE33X0A5MnyW5EpU0m4gDZ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=Al6pcJCu; arc=none smtp.client-ip=209.85.161.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="Al6pcJCu" Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5f4cc48ab37so711645eaf.1 for ; Fri, 07 Feb 2025 12:09:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958986; x=1739563786; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Ut7KuwKbl81SPvG2p9GVXbZ3PqypOuWpM2zTdhcRP0k=; b=Al6pcJCuoliN3EuE3NimMY4HLUdzzBTn1SoWUAL5SoiY/haW+s7Gpt4MV7S5RyYyuY x3H5KBPBGYHdqBnuGPo0E0LskSkapajViI4Hy6YazK0QX4mfHltViDrQf5pEuxDpThuA pb+uaMmQ8cyd9GdWVqgXud/wfyjRmtMfpbl1zP27SqhkVCCmmL5wmJXb00KTz/L4m+VJ PZ8KjVqstaiO1WurrtqE5ZWZFv4nkBkEHl05SIbft3kfn12S7cj3aaz/DQuM2scEkTH1 2FrWp6rfkA4qlEj25tpR+DLntzoNS9rEcvEzk0v/Br3nE9jDqWs/lAXwVDaSK/7j2M+4 nFcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958986; x=1739563786; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ut7KuwKbl81SPvG2p9GVXbZ3PqypOuWpM2zTdhcRP0k=; b=pEVkTHcu6Ocxktamk+4EFCEHPnfFd8ArV00W2jBBRqKINvLuSbn0YslaPyXpn3ii5F 3ASAShGYVvJugeqLH8QZ9LKtGfWWaZlvasj1n+iC1fTJGNcQdakniBWJCj/IR/Q/hC+/ VGqFjZ+z33rZoHUbmd5wj9kXb5bvOx2PmhsbZgbDzJRsXOUeJaqBSBOn0Ltl/NXCk4B2 nUNkLoWd1ja5yYe1qUNZABfRMf7LvynwEszpZXFuVjRxl6Z4s7kENU5/S35idlNppAte araQccQmhjRDaRd40fGe7/HDkDfKfvoJnTY94QXpqEBjDR2ubokgfa+ibQULJVex9NwU vcwg== X-Forwarded-Encrypted: i=1; AJvYcCVW3aGo7rSMet4HFXks8VMmqRC1WoBTFo+TO2RnoFzlXGKuBHAz9UKHeD3ubEwzTSwa15xsWtfQKGzrTyU=@vger.kernel.org X-Gm-Message-State: AOJu0Yzmv2HmV8/Jy9FJ2VKcWnDAnJllZhyvcG36EjKLFWJNTH6ho6Wq X+C7eyFiJSUmeqbgr5v15GKdM1sugfVOO9DUycJx82CXbJPYQzhDGCmhl7+tJ6k= X-Gm-Gg: ASbGnctJgwh1hrJU8IyhvA0qPO3SY5r+312jVpOhyJ0M4ktGvMPxHudhnyAPzfepMci dUulWCngOe5w6yWi1QEuH6IWJUXqcPtUh0MrSS9ChkVq0yYQtbYf+LT8XdsG967f2iBdHFogZ+H Ttmyev614TCWw8sigobDp9jVo7NpZKU6vi1qAXEBICD0Vfj58ySTu4JHPToz+gVZ7PTW2XiIjUO CCrCNV1eN32fNYIlOoQvkxbExLDtNvwRIYio4NW1QMnpUVSEzFnIP66gCl0+KXnwO+f0dbnNk8n isnLhumQnx+MTwKDO6mNdBFMn3fZiD/rxE+rGZpOvYg/3X0= X-Google-Smtp-Source: AGHT+IHRfLgfaHBoTphWWobyJlHuSA0Qn5fxrfJiEVVNs+JspX45/83kZcTQrSb3wDkZnFq0SvqOlA== X-Received: by 2002:a05:6820:16a2:b0:5fa:5c7d:dbff with SMTP id 006d021491bc7-5fc5e70f420mr2638074eaf.3.1738958986229; Fri, 07 Feb 2025 12:09:46 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:44 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:08 -0600 Subject: [PATCH v8 11/17] iio: adc: ad7944: add support for SPI offload Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-11-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add support for SPI offload to the ad7944 driver. This allows reading data at the max sample rate of 2.5 MSPS. Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: * More detailed comments on offload channels info struct. (I didn't put them inside the macro because the multiline comments don't play well with the line continuation "\".) v5 changes: * Remove dev_info() message. * Implement sampling_frequency_available attribute. * Limit max sample rate to chip-specific value. v4 changes: * Adapted to changes in other patches. * Add new separate channel spec for when using SPI offload. * Fixed some nitpicks. v3 changes: * Finished TODOs. * Adapted to changes in other patches. v2 changes: In the previous version, there was a new separate driver for the PWM trigger and DMA hardware buffer. This was deemed too complex so they are moved into the ad7944 driver. It has also been reworked to accommodate for the changes described in the other patches. --- drivers/iio/adc/Kconfig | 1 + drivers/iio/adc/ad7944.c | 291 +++++++++++++++++++++++++++++++++++++++++++= +--- 2 files changed, 276 insertions(+), 16 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index a3e8ac569ce4c6b6b30b48acb265d530aa98e89c..995b9cacbaa964d26424346120c= 139858f93cdcd 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -360,6 +360,7 @@ config AD7923 config AD7944 tristate "Analog Devices AD7944 and similar ADCs driver" depends on SPI + select SPI_OFFLOAD select IIO_BUFFER select IIO_TRIGGERED_BUFFER help diff --git a/drivers/iio/adc/ad7944.c b/drivers/iio/adc/ad7944.c index abfababcea10152fe1faa8afc2ec1ea2dc40ae52..9a7825ea5087af9725613492c42= 020587631403c 100644 --- a/drivers/iio/adc/ad7944.c +++ b/drivers/iio/adc/ad7944.c @@ -16,11 +16,14 @@ #include #include #include +#include #include #include +#include =20 #include #include +#include #include #include =20 @@ -54,6 +57,12 @@ struct ad7944_adc { enum ad7944_spi_mode spi_mode; struct spi_transfer xfers[3]; struct spi_message msg; + struct spi_transfer offload_xfers[2]; + struct spi_message offload_msg; + struct spi_offload *offload; + struct spi_offload_trigger *offload_trigger; + unsigned long offload_trigger_hz; + int sample_freq_range[3]; void *chain_mode_buf; /* Chip-specific timing specifications. */ const struct ad7944_timing_spec *timing_spec; @@ -81,6 +90,8 @@ struct ad7944_adc { =20 /* quite time before CNV rising edge */ #define AD7944_T_QUIET_NS 20 +/* minimum CNV high time to trigger conversion */ +#define AD7944_T_CNVH_NS 10 =20 static const struct ad7944_timing_spec ad7944_timing_spec =3D { .conv_ns =3D 420, @@ -95,7 +106,9 @@ static const struct ad7944_timing_spec ad7986_timing_spe= c =3D { struct ad7944_chip_info { const char *name; const struct ad7944_timing_spec *timing_spec; + u32 max_sample_rate_hz; const struct iio_chan_spec channels[2]; + const struct iio_chan_spec offload_channels[1]; }; =20 /* get number of bytes for SPI xfer */ @@ -105,13 +118,15 @@ struct ad7944_chip_info { * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific c= hip * @_name: The name of the chip * @_ts: The timing specification for the chip + * @_max: The maximum sample rate in Hz * @_bits: The number of bits in the conversion result * @_diff: Whether the chip is true differential or not */ -#define AD7944_DEFINE_CHIP_INFO(_name, _ts, _bits, _diff) \ +#define AD7944_DEFINE_CHIP_INFO(_name, _ts, _max, _bits, _diff) \ static const struct ad7944_chip_info _name##_chip_info =3D { \ .name =3D #_name, \ .timing_spec =3D &_ts##_timing_spec, \ + .max_sample_rate_hz =3D _max, \ .channels =3D { \ { \ .type =3D IIO_VOLTAGE, \ @@ -129,13 +144,43 @@ static const struct ad7944_chip_info _name##_chip_inf= o =3D { \ }, \ IIO_CHAN_SOFT_TIMESTAMP(1), \ }, \ + .offload_channels =3D { \ + { \ + .type =3D IIO_VOLTAGE, \ + .indexed =3D 1, \ + .differential =3D _diff, \ + .channel =3D 0, \ + .channel2 =3D _diff ? 1 : 0, \ + .scan_index =3D 0, \ + .scan_type.sign =3D _diff ? 's' : 'u', \ + .scan_type.realbits =3D _bits, \ + .scan_type.storagebits =3D 32, \ + .scan_type.endianness =3D IIO_CPU, \ + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) \ + | BIT(IIO_CHAN_INFO_SCALE) \ + | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + .info_mask_separate_available =3D \ + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ + }, \ + }, \ } =20 +/* + * Notes on the offload channels: + * - There is no soft timestamp since everything is done in hardware. + * - There is a sampling frequency attribute added. This controls the SPI + * offload trigger. + * - The storagebits value depends on the SPI offload provider. Currently = there + * is only one supported provider, namely the ADI PULSAR ADC HDL project, + * which always uses 32-bit words for data values, even for <=3D 16-bit = ADCs. + * So the value is just hardcoded to 32 for now. + */ + /* pseudo-differential with ground sense */ -AD7944_DEFINE_CHIP_INFO(ad7944, ad7944, 14, 0); -AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 16, 0); +AD7944_DEFINE_CHIP_INFO(ad7944, ad7944, 2.5 * MEGA, 14, 0); +AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 2.5 * MEGA, 16, 0); /* fully differential */ -AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 18, 1); +AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 2 * MEGA, 18, 1); =20 static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944= _adc *adc, const struct iio_chan_spec *chan) @@ -239,6 +284,48 @@ static int ad7944_chain_mode_init_msg(struct device *d= ev, struct ad7944_adc *adc return devm_spi_optimize_message(dev, adc->spi, &adc->msg); } =20 +/* + * Unlike ad7944_3wire_cs_mode_init_msg(), this creates a message that rea= ds + * during the conversion phase instead of the acquisition phase when readi= ng + * a sample from the ADC. This is needed to be able to read at the maximum + * sample rate. It requires the SPI controller to have offload support and= a + * high enough SCLK rate to read the sample during the conversion phase. + */ +static int ad7944_3wire_cs_mode_init_offload_msg(struct device *dev, + struct ad7944_adc *adc, + const struct iio_chan_spec *chan) +{ + struct spi_transfer *xfers =3D adc->offload_xfers; + int ret; + + /* + * CS is tied to CNV and we need a low to high transition to start the + * conversion, so place CNV low for t_QUIET to prepare for this. + */ + xfers[0].delay.value =3D AD7944_T_QUIET_NS; + xfers[0].delay.unit =3D SPI_DELAY_UNIT_NSECS; + /* CNV has to be high for a minimum time to trigger conversion. */ + xfers[0].cs_change =3D 1; + xfers[0].cs_change_delay.value =3D AD7944_T_CNVH_NS; + xfers[0].cs_change_delay.unit =3D SPI_DELAY_UNIT_NSECS; + + /* Then we can read the previous sample during the conversion phase */ + xfers[1].offload_flags =3D SPI_OFFLOAD_XFER_RX_STREAM; + xfers[1].len =3D AD7944_SPI_BYTES(chan->scan_type); + xfers[1].bits_per_word =3D chan->scan_type.realbits; + + spi_message_init_with_transfers(&adc->offload_msg, xfers, + ARRAY_SIZE(adc->offload_xfers)); + + adc->offload_msg.offload =3D adc->offload; + + ret =3D devm_spi_optimize_message(dev, adc->spi, &adc->offload_msg); + if (ret) + return dev_err_probe(dev, ret, "failed to prepare offload msg\n"); + + return 0; +} + /** * ad7944_convert_and_acquire - Perform a single conversion and acquisition * @adc: The ADC device structure @@ -294,6 +381,23 @@ static int ad7944_single_conversion(struct ad7944_adc = *adc, return IIO_VAL_INT; } =20 +static int ad7944_read_avail(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + const int **vals, int *type, int *length, + long mask) +{ + struct ad7944_adc *adc =3D iio_priv(indio_dev); + + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: + *vals =3D adc->sample_freq_range; + *type =3D IIO_VAL_INT; + return IIO_AVAIL_RANGE; + default: + return -EINVAL; + } +} + static int ad7944_read_raw(struct iio_dev *indio_dev, const struct iio_chan_spec *chan, int *val, int *val2, long info) @@ -326,13 +430,104 @@ static int ad7944_read_raw(struct iio_dev *indio_dev, return -EINVAL; } =20 + case IIO_CHAN_INFO_SAMP_FREQ: + *val =3D adc->offload_trigger_hz; + return IIO_VAL_INT; + + default: + return -EINVAL; + } +} + +static int ad7944_set_sample_freq(struct ad7944_adc *adc, int val) +{ + struct spi_offload_trigger_config config =3D { + .type =3D SPI_OFFLOAD_TRIGGER_PERIODIC, + .periodic =3D { + .frequency_hz =3D val, + }, + }; + int ret; + + ret =3D spi_offload_trigger_validate(adc->offload_trigger, &config); + if (ret) + return ret; + + adc->offload_trigger_hz =3D config.periodic.frequency_hz; + + return 0; +} + +static int ad7944_write_raw(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + int val, int val2, long info) +{ + struct ad7944_adc *adc =3D iio_priv(indio_dev); + + switch (info) { + case IIO_CHAN_INFO_SAMP_FREQ: + if (val < 1 || val > adc->sample_freq_range[2]) + return -EINVAL; + + return ad7944_set_sample_freq(adc, val); default: return -EINVAL; } } =20 +static int ad7944_write_raw_get_fmt(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: + return IIO_VAL_INT; + default: + return IIO_VAL_INT_PLUS_MICRO; + } +} + static const struct iio_info ad7944_iio_info =3D { + .read_avail =3D &ad7944_read_avail, .read_raw =3D &ad7944_read_raw, + .write_raw =3D &ad7944_write_raw, + .write_raw_get_fmt =3D &ad7944_write_raw_get_fmt, +}; + +static int ad7944_offload_buffer_postenable(struct iio_dev *indio_dev) +{ + struct ad7944_adc *adc =3D iio_priv(indio_dev); + struct spi_offload_trigger_config config =3D { + .type =3D SPI_OFFLOAD_TRIGGER_PERIODIC, + .periodic =3D { + .frequency_hz =3D adc->offload_trigger_hz, + }, + }; + int ret; + + gpiod_set_value_cansleep(adc->turbo, 1); + + ret =3D spi_offload_trigger_enable(adc->offload, adc->offload_trigger, + &config); + if (ret) + gpiod_set_value_cansleep(adc->turbo, 0); + + return ret; +} + +static int ad7944_offload_buffer_predisable(struct iio_dev *indio_dev) +{ + struct ad7944_adc *adc =3D iio_priv(indio_dev); + + spi_offload_trigger_disable(adc->offload, adc->offload_trigger); + gpiod_set_value_cansleep(adc->turbo, 0); + + return 0; +} + +static const struct iio_buffer_setup_ops ad7944_offload_buffer_setup_ops = =3D { + .postenable =3D &ad7944_offload_buffer_postenable, + .predisable =3D &ad7944_offload_buffer_predisable, }; =20 static irqreturn_t ad7944_trigger_handler(int irq, void *p) @@ -446,6 +641,11 @@ static const char * const ad7944_power_supplies[] =3D { "avdd", "dvdd", "bvdd", "vio" }; =20 +static const struct spi_offload_config ad7944_offload_config =3D { + .capability_flags =3D SPI_OFFLOAD_CAP_TRIGGER | + SPI_OFFLOAD_CAP_RX_STREAM_DMA, +}; + static int ad7944_probe(struct spi_device *spi) { const struct ad7944_chip_info *chip_info; @@ -471,6 +671,10 @@ static int ad7944_probe(struct spi_device *spi) =20 adc->timing_spec =3D chip_info->timing_spec; =20 + adc->sample_freq_range[0] =3D 1; /* min */ + adc->sample_freq_range[1] =3D 1; /* step */ + adc->sample_freq_range[2] =3D chip_info->max_sample_rate_hz; /* max */ + ret =3D device_property_match_property_string(dev, "adi,spi-mode", ad7944_spi_modes, ARRAY_SIZE(ad7944_spi_modes)); @@ -590,20 +794,74 @@ static int ad7944_probe(struct spi_device *spi) indio_dev->modes =3D INDIO_DIRECT_MODE; indio_dev->info =3D &ad7944_iio_info; =20 - if (adc->spi_mode =3D=3D AD7944_SPI_MODE_CHAIN) { - indio_dev->available_scan_masks =3D chain_scan_masks; - indio_dev->channels =3D chain_chan; - indio_dev->num_channels =3D n_chain_dev + 1; + adc->offload =3D devm_spi_offload_get(dev, spi, &ad7944_offload_config); + ret =3D PTR_ERR_OR_ZERO(adc->offload); + if (ret && ret !=3D -ENODEV) + return dev_err_probe(dev, ret, "failed to get offload\n"); + + /* Fall back to low speed usage when no SPI offload available. */ + if (ret =3D=3D -ENODEV) { + if (adc->spi_mode =3D=3D AD7944_SPI_MODE_CHAIN) { + indio_dev->available_scan_masks =3D chain_scan_masks; + indio_dev->channels =3D chain_chan; + indio_dev->num_channels =3D n_chain_dev + 1; + } else { + indio_dev->channels =3D chip_info->channels; + indio_dev->num_channels =3D ARRAY_SIZE(chip_info->channels); + } + + ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, + iio_pollfunc_store_time, + ad7944_trigger_handler, + NULL); + if (ret) + return ret; } else { - indio_dev->channels =3D chip_info->channels; - indio_dev->num_channels =3D ARRAY_SIZE(chip_info->channels); - } + struct dma_chan *rx_dma; =20 - ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, - iio_pollfunc_store_time, - ad7944_trigger_handler, NULL); - if (ret) - return ret; + if (adc->spi_mode !=3D AD7944_SPI_MODE_SINGLE) + return dev_err_probe(dev, -EINVAL, + "offload only supported in single mode\n"); + + indio_dev->setup_ops =3D &ad7944_offload_buffer_setup_ops; + indio_dev->channels =3D chip_info->offload_channels; + indio_dev->num_channels =3D ARRAY_SIZE(chip_info->offload_channels); + + adc->offload_trigger =3D devm_spi_offload_trigger_get(dev, + adc->offload, SPI_OFFLOAD_TRIGGER_PERIODIC); + if (IS_ERR(adc->offload_trigger)) + return dev_err_probe(dev, PTR_ERR(adc->offload_trigger), + "failed to get offload trigger\n"); + + ret =3D ad7944_set_sample_freq(adc, 2 * MEGA); + if (ret) + return dev_err_probe(dev, ret, + "failed to init sample rate\n"); + + rx_dma =3D devm_spi_offload_rx_stream_request_dma_chan(dev, + adc->offload); + if (IS_ERR(rx_dma)) + return dev_err_probe(dev, PTR_ERR(rx_dma), + "failed to get offload RX DMA\n"); + + /* + * REVISIT: ideally, we would confirm that the offload RX DMA + * buffer layout is the same as what is hard-coded in + * offload_channels. Right now, the only supported offload + * is the pulsar_adc project which always uses 32-bit word + * size for data values, regardless of the SPI bits per word. + */ + + ret =3D devm_iio_dmaengine_buffer_setup_with_handle(dev, + indio_dev, rx_dma, IIO_BUFFER_DIRECTION_IN); + if (ret) + return ret; + + ret =3D ad7944_3wire_cs_mode_init_offload_msg(dev, adc, + &chip_info->offload_channels[0]); + if (ret) + return ret; + } =20 return devm_iio_device_register(dev, indio_dev); } @@ -638,3 +896,4 @@ module_spi_driver(ad7944_driver); MODULE_AUTHOR("David Lechner "); MODULE_DESCRIPTION("Analog Devices AD7944 PulSAR ADC family driver"); MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER"); --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEFD71D63CE for ; Fri, 7 Feb 2025 20:09:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958990; cv=none; b=Yv7QGHXIRFBHS7exQB8yY+8SHaDYWRsMm2WvKVOFn/Z6q6CvJPxuCisdBL7mmSA5L/ZPp/qdhR00ZhRSXq0VgMhbYRa7rAZlTkz4jlRG2LEFbNp9gczMiFKVkA4YGKSD4ZlOZpU3QU9BHdCuyNjMS0TtWF4eonU6x9Q0UeVNTnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958990; c=relaxed/simple; bh=jRlTs2hPxFTMWzXud2pwyf/gDJQZpce7x7Xes5PCDq0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=onrzUbtY3Atu+4sgWjcGPC76DnHkPF57MsSC4FiV2UHc6IHWBwcI0SExC9NVhoOC8xk6rc+C1YL1riOx+GKVDiKf2/ATmvP8qPS2/ySg+4IA9DO/2YqAZb1zqqIM3hHEauj114lbneqNCviy9lqyKzxqfeOdY0iI0sBLwVuQMb8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=c/xQHjKJ; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="c/xQHjKJ" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5fa28eaa52cso1381305eaf.2 for ; Fri, 07 Feb 2025 12:09:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958988; x=1739563788; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=m4qOy1E2SWXTRLQzjAjoUUpkXvVJq7HeawRi11wv1CQ=; b=c/xQHjKJuastuL7F4tSshhnKVOt8W5fYRMSzu1kwH8f1mcBTUTV+/CqgYIjyPhbEk8 iyETe7xmqVbt3gFqi+cy9posN6t4MSxXkHVAqwE2ZND+0quXlkijyz63XB4yprJdgpjs hZXD3queVLzUTqNb7KxCVLSiYlyrDtU4QylpE0GrxiUhILKU3o2LTr0z4bBzTLe7Wkd0 LN+bK9OGbDwMc559RsNj3wS6cHHzO1c5yM7kV9R5MkPkL5IWyAbwX2ky5rRuiQApnYsH 1c1mnVo3mRkuayv4+rwjhc6OczSo0GGFPG5BQqM+HkUYtIq1J9yqxRjJZr9wESEwGS01 w33Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958988; x=1739563788; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m4qOy1E2SWXTRLQzjAjoUUpkXvVJq7HeawRi11wv1CQ=; b=aUH4KWg0Ds2ieTcH4kfmeWAUhh41TZWXVHfSqrJENOBo0TdR3uxirrarkm0YukG1SF s3aoKEBkHUGIrfr0F5xQX7qPczbkqV0hsnmSlym3daROlFBnLAmBPpUIu6mnvZwxO/Ac behn/aWn+Mg4XSX2jXj/sSIe/JMksu0EWrpTWjvYHKQ64nstLhMFWdZYOFX7AoQAtBTl BADzxrnGLcvkvK2CVXIa1K/OeQAqAr0wBbdQcLdQwvAxgnsJsZSNbPtJZ248srnHZomB atJo4c1kaOuks8ZtyS831mrIjJnrOTVmH1/+KndQGIt413YVaCysvIlmKtLEYVI8WdhG 23VA== X-Forwarded-Encrypted: i=1; AJvYcCWjHrox4Nj7Zy8mZ2AO3VRS6eN1amOH08rQfGHSIJb2NZrII37/98D3MtsFAyE4NQqehup83aI/6uPpILM=@vger.kernel.org X-Gm-Message-State: AOJu0YwD7H4d8JHxZDj/MUekPFCnyY+EfVTLuC/e+BFNVv7JAD7tPBlZ /YMpEnP1d4OX8g/+IwSnXPFxkDI6Q3EJdauiS5uj0naDf2aLzBO+dKnOf88fZ5PpSJOplwEjdVc W X-Gm-Gg: ASbGncubiU3hX9MWximxpxCLQytPH8N0bgXjj84MqUh6PQU35UFz+CLUfEdiSYv+UsJ Ld+hl670YFJrHaZMpRNoOxmdtchnT4kCuC5lzXlzYJo2xz2mgBOVkMh9Q/6dKD785udatM9lJ1m UWDp1V26DMTSpptDjo+KMiCcFuVDZYLkks1OXMKkYhsXXdQW/NQTW6/qgmiX3eAmdN0qNiU1jnG 30kHnXsLCvSoX/UUech1yE5kyyZG7RyQrQdSpcdUlnED+hOV7sTMDNUaPm6nMI8fjcx5FwUgH1r frGAx7UxKWdxd6GKLISkZpTJYyF6/K2nEPhar3QEHEHMXWI= X-Google-Smtp-Source: AGHT+IG77ZOpZoqliQs6i/d86fRFOW+Nrx505QHjkxGWQhm1CMXLj+So9sv5ltnLehIXmgyoVX5BBg== X-Received: by 2002:a4a:ee14:0:b0:5fa:8925:3d76 with SMTP id 006d021491bc7-5fc5e5c755dmr2828112eaf.1.1738958988093; Fri, 07 Feb 2025 12:09:48 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:46 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:09 -0600 Subject: [PATCH v8 12/17] doc: iio: ad7944: describe offload support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-12-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add a section to the ad7944 documentation describing how to use the driver with SPI offloading. Reviewed-by: Jonathan Cameron Signed-off-by: David Lechner --- v7 changes: none v6 changes: none v5 changes: new patch in v5 --- Documentation/iio/ad7944.rst | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Documentation/iio/ad7944.rst b/Documentation/iio/ad7944.rst index 0d26e56aba8862a8d2ff541012bb4681d05c7bb5..e6dbe4d7f58cffd9f700f931ad2= 641d336526129 100644 --- a/Documentation/iio/ad7944.rst +++ b/Documentation/iio/ad7944.rst @@ -46,6 +46,8 @@ CS mode, 3-wire, without busy indicator To select this mode in the device tree, set the ``adi,spi-mode`` property = to ``"single"`` and omit the ``cnv-gpios`` property. =20 +This is the only wiring configuration supported when using `SPI offload su= pport`_. + CS mode, 4-wire, without busy indicator ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ =20 @@ -106,7 +108,6 @@ Unimplemented features ---------------------- =20 - ``BUSY`` indication -- ``TURBO`` mode =20 =20 Device attributes @@ -147,6 +148,27 @@ AD7986 is a fully-differential ADC and has the followi= ng attributes: In "chain" mode, additional chips will appear as additional voltage input channels, e.g. ``in_voltage2-voltage3_raw``. =20 +SPI offload support +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +To be able to achieve the maximum sample rate, the driver can be used with= the +`AXI SPI Engine`_ to provide SPI offload support. + +.. _AXI SPI Engine: http://analogdevicesinc.github.io/hdl/projects/pulsar_= adc/index.html + +When SPI offload is being used, some attributes will be different. + +* ``trigger`` directory is removed. +* ``in_voltage0_sampling_frequency`` attribute is added for setting the sa= mple + rate. +* ``in_voltage0_sampling_frequency_available`` attribute is added for quer= ying + the max sample rate. +* ``timestamp`` channel is removed. +* Buffer data format may be different compared to when offload is not used, + e.g. the ``in_voltage0_type`` attribute. + +If the ``turbo-gpios`` property is present in the device tree, the driver = will +turn on TURBO during buffered reads and turn it off otherwise. =20 Device buffers =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDF2919F41C for ; Fri, 7 Feb 2025 20:09:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958994; cv=none; b=qJ+MVah8vNQ/oc58pp2gx6HmsFK81MlWeOkshnuotFlHz8MdEvsxx6gRXbZG1mHYcyhEJQIujyrk6u3fEvdfbsBC7vdAEyz2pM3CLhN0ejZCk27LA/6cYOZJ3GcKPN3ZZsSlJBhJCGPJW4VuEBOZfkG2U6HJ4RwgtT7h03GWj1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958994; c=relaxed/simple; bh=NDFiO3U0E1S/5X6Lz//TxxrYJLqr6ccKrjn8lgZTJIQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rLSS98uHD5zHCSDHnBvzgPxEelhDaLP3cwH9C3cCD8jAvR4Fh1kT4lodB/ITaKLfFWueJ/JixQdCsqybyGZGo59qsvai66B3q8OSz+R8qpCORvlHxTEBn4BsDg6IIvkZ8F48S9PJofuCj9l+F6bgblMGFUuynh7FyWKXINbJpwY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=erZFKx72; arc=none smtp.client-ip=209.85.161.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="erZFKx72" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5fa2685c5c0so957251eaf.3 for ; Fri, 07 Feb 2025 12:09:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958991; x=1739563791; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=8iLwNjuSq2Wrxa12NE4/8IEuSETMFdqrFjSUkFlmHIg=; b=erZFKx72jNL8sn3fAjUiq0GvK52KR+iWUPdFTDsNz3v+dBI+sVh5pCH28qyvKAb48U hGc82+B5qZZRHzpSWkJoyeYUwwezp38vZ00D3RW+LV2Yfh84LotZ5EXUFrx0BkNMYum6 mFsh0lYsnRh3EfPnEC1zMdA1MCpmDqaF96XJjH+Qo2QCKZXdMpFzFTz+xXQktfSCLU02 CAclZ0qq/SlNi65mUcbg1kysDayiODUZ+ZCOAHV2wMgWu9SKHOoKoNw3hCq8uoSi2PSf ED/RtUVe0w5nVQqUP4xUW/pmFOCaZDL300IIl1HQSNYDQO/IIUzFvAGYZM6K5Qzf5s0D GJfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958991; x=1739563791; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8iLwNjuSq2Wrxa12NE4/8IEuSETMFdqrFjSUkFlmHIg=; b=XPOUfw/tRprHH2ZBxFMpNJ65W99hugWct4tgkFOACW4t8v33S+wI38/pcephJegH3I sH5SXZMBQd3920Jw5j1dRbH1Iv8Zn5fiywLf1WlHJ+nqJd86y+oLe0BbNyvsWS5NU9Y5 xuMi9NoKgISsoWdNQdeeOL6NX0D8MWKgeYb0gvFOJRC3BnTxRoDSqwybsh7z9+hsU3wJ HCNJPMu9sjrqw7gOd9DwSsQIQY+uiHlnf5SfjIRgVtf8h6vakexcH2REiXFUO/V35e2s YTJHYPrKco/5vqRXYme2UmfKVwlPvjTDZXzdPkhK43UCAGqAhFoK1lmCatTjN8Kw0gdR WjVQ== X-Forwarded-Encrypted: i=1; AJvYcCXmI98tI1tcMO6+qxDYfq8AJYc13CL+s50q1LOx+effOML4h1o1E206SUK36t/owtQK3YUEfn+T1J/+5bQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5VM1Z3VF0Y+UWO2SVplomp/LI80EZzl8+s86gUfvWqSgAmZlL tPwCRHrJzXllDeQDVDiH50qJpOapC9qa23J/P5ezg3aHt6zGwt7ldmvoMR/1cCt1CuYNsg5FudR z X-Gm-Gg: ASbGncvRUTkygNKd1bu24n2l2ZPj1f4bA1Mm70uJnKdM4JHTp8OF/i5egympPuYjoOP dtjYcyqDAI7YAMaBBxc9QgXWGVxajAFt/60nwsPu37Gi6Q+aNxbl23uOq65PSPTe/taZg5jXGoR lfU4FFB+B6JCU7ZS6xzYz0AXyxKTlh4J4NDmagiUjHmuHWuBt7e4eoeIv+O66dG1iQ4h+ZvF5D8 Y3UBINmkbrb4XbInVrgsvo/7ou6x/QhE88B5ARf+XLrYJEOg1x6bv83DlRawiD7KO0IVvefQTT0 ELShQ1B4rjRvI7XjVOBKSpzIeeaGXcE/NEZhJW1ml5qCpBs= X-Google-Smtp-Source: AGHT+IEsnyzcKsdTR3dUVyCVYzDHv54LzczlPeqEofvxzLvMfRB3mcsdxkx1bczd7e2abrb46nmuEQ== X-Received: by 2002:a05:6820:f09:b0:5fa:1ee7:1325 with SMTP id 006d021491bc7-5fc5e746384mr3223073eaf.7.1738958991033; Fri, 07 Feb 2025 12:09:51 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:49 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:10 -0600 Subject: [PATCH v8 13/17] dt-bindings: iio: adc: adi,ad4695: add SPI offload properties Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-13-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add a pwms property to the adi,ad4695 binding to specify an optional PWM output connected to the CNV pin on the ADC. Also add #trigger-source-cells property to allow the BUSY output to be used as a SPI offload trigger source to indicate when a sample is ready to be read. Macros are added to adi,ad4695.h for the cell values to help with readability since they are arbitrary values. Reviewed-by: Jonathan Cameron Reviewed-by: Rob Herring (Arm) Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Drop $ref to trigger-source.yaml * Add maxItems to pwms property v5 changes: * Added macros for cell values v4 changes: new patch in v4 --- Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml | 13 +++++++++++= ++ include/dt-bindings/iio/adc/adi,ad4695.h | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml b/Do= cumentation/devicetree/bindings/iio/adc/adi,ad4695.yaml index 7d2229dee4441e20a7bafc9165fe309ac2e9eada..cbde7a0505d2b5df22c54ca4556= 878bf22e9b4b1 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml @@ -84,6 +84,10 @@ properties: description: The Reset Input (RESET). Should be configured GPIO_ACTIVE= _LOW. maxItems: 1 =20 + pwms: + description: PWM signal connected to the CNV pin. + maxItems: 1 + interrupts: minItems: 1 items: @@ -106,6 +110,15 @@ properties: The first cell is the GPn number: 0 to 3. The second cell takes standard GPIO flags. =20 + '#trigger-source-cells': + description: | + First cell indicates the output signal: 0 =3D BUSY, 1 =3D ALERT. + Second cell indicates which GPn pin is used: 0, 2 or 3. + + For convenience, macros for these values are available in + dt-bindings/iio/adc/adi,ad4695.h. + const: 2 + "#address-cells": const: 1 =20 diff --git a/include/dt-bindings/iio/adc/adi,ad4695.h b/include/dt-bindings= /iio/adc/adi,ad4695.h index 9fbef542bf670015c5b34bfbe1336e7e295bf8ab..fea4525d2710cbf58cd4236d327= 6bad4cb318df9 100644 --- a/include/dt-bindings/iio/adc/adi,ad4695.h +++ b/include/dt-bindings/iio/adc/adi,ad4695.h @@ -6,4 +6,11 @@ #define AD4695_COMMON_MODE_REFGND 0xFF #define AD4695_COMMON_MODE_COM 0xFE =20 +#define AD4695_TRIGGER_EVENT_BUSY 0 +#define AD4695_TRIGGER_EVENT_ALERT 1 + +#define AD4695_TRIGGER_PIN_GP0 0 +#define AD4695_TRIGGER_PIN_GP2 2 +#define AD4695_TRIGGER_PIN_GP3 3 + #endif /* _DT_BINDINGS_ADI_AD4695_H */ --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 523801DEFC5 for ; Fri, 7 Feb 2025 20:09:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958998; cv=none; b=TXXjuu/V0ApEDHTM1Wr13326x3apVTc4dyay4WRmcCYM6bqeWi8CksU44kaJ1jlDnsciTP63Sw4QZkuArFUQjyU0EAIXSmJ67keRU4l+GIciJ0sOaLa4ID1/KEecQT0tSjOSTAMIb/EEmCO7XhniM3dJ2DB98fRodDuFEdBk+CA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958998; c=relaxed/simple; bh=yAcFfVExR7ZSPMT8y80IRCkgS0LFHTjof3q26EEQz28=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=K8NepGRKFZLrM9hOP/QhpdgCrHlHZUO++a9j0G5NfTgKQ8meKOv3wCphC71lD4HzFiGaRRj8aTHrnRwQKD5DDPBJLnh/ulKzY9RxVzII6piPLuMI5q4fmN1vfQNLwzBGoUtX5NTQEhcAl1ZRmaYzyWUJ97dtPBaslIv+coXvxsY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=xUjW3TJv; arc=none smtp.client-ip=209.85.210.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="xUjW3TJv" Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-71e3f291ad6so1768139a34.0 for ; Fri, 07 Feb 2025 12:09:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958992; x=1739563792; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=fDJK3pvpW2GUWlcQwbUvhz+0UyxguDnCwVC4scu0soo=; b=xUjW3TJvDO2nB3BFxOtHi1HIpwL24Zi+cx3QlAngaGP82yldvHMCGVBFKJedjubGTE ILgmKH6uZ2DI00E1mVI+A/QWT6UrXu1mKYOR/XWLlcajCkiHZ3UHlrykAFGfrMIGwcwy Re2RqR6sVQLlmiXeqHwQyDNetjJHJ8ifLodw+X/e5ISHYjgEQ1A9rDsJBD5mWOusP7vY gzsAtOLV2IIr0L9B9DVczVLIIKaX+WyzoQn/696Rlkhh9iewOOQjynutsVmbUpj51RQ4 7WH2CRJm3t6GIcvBmFRXMuFrPGZ5kYfWNIFUXUNbVPFcZHdRGOD/+xHjeRjT5Ovc3cTW GQCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958992; x=1739563792; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fDJK3pvpW2GUWlcQwbUvhz+0UyxguDnCwVC4scu0soo=; b=DOJ/hvcoGkLGl1Dm8yXOu7yGlWiQxcOj2L9xMQresWyrFV779m97l6CesuDY528qit 6gpXWNFcRcQqCWPXZeL5LAof6vgyPEBV1OJ63aD46q3uZtCJV9PJ2MQQszKRUePQAWKd 23b5+vBBYDJnNaodoGMHYqkmXpIPSG/4ggXsYagq9LenYz+3KGl7GUZxCYmvEVKgJfvD 1C7X2A2Ywx2B99D3Brq5KhAPoSkZSfzWDV5TNGD9/CauoCpkoUYiiWgnlEqesuJMVov6 mQl1ZEgHS9o6+l6AqzuGwNTyCsR7/bz7iQy4Gz2waTwZdQoWgO8CKJ3j1D9WiFPvs3Yr PgVg== X-Forwarded-Encrypted: i=1; AJvYcCUpzqXv4xcyfU+0u645TXfiwewe8YUgTknQJn4rbY1IPCLq3kBL/fFc6SUejQHfeEHvjW22LlNoPHGVitY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyd7MtcNEArWYMHjnnvr5YvWjSRufvM3CObueN720bmx47HjRND EdOPGVM/x1tRnf95K7L12iNvmlxFi0WGUPptNBaNbk9cnfACp3PznXmmSXp1Uz4= X-Gm-Gg: ASbGncvCjzV1t8LUd7bG8Ho+6aJfmiosetfaRFJyo6eGtmcS+xGV7N+sdKueXccIaJA B8QZBIDdqtZT1SA/sH6Sx74NywL40HXuxbDl9xkH2BJx5hd968o6HPIizyoAdkUykGV9m1wu/2i B3bk6IIu5P1Z8kQqjjpgyykRTvauQA8yirf+YeAQVi3R/Yn7LkhEHv6K5Jp74bsz/ML1MFibBRU 0gUXQ9Si56wSRwYt7HjMyYU9jyv74nS6O2oLtL70+XHFIG+xaSLfpHEIkG8KlPWUVkMaYHkLpSv bKKp7KH3ScbMNbaW7wBtI/N7NMvJEp/4ZDUF07r0WXYbIjU= X-Google-Smtp-Source: AGHT+IGxtPnBmqEw9Cl0bueyBrGvkVgn+4lAL5Aw1f3fXVb4AA3rh1WlpNcCeRFvbau9+nuGsrTLsw== X-Received: by 2002:a05:6830:6404:b0:71d:eee3:fd22 with SMTP id 46e09a7af769-726b87a23edmr3226746a34.5.1738958992150; Fri, 07 Feb 2025 12:09:52 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:51 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:11 -0600 Subject: [PATCH v8 14/17] iio: adc: ad4695: Add support for SPI offload Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-14-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 Add support for SPI offload to the ad4695 driver. SPI offload allows sampling data at the max sample rate (500kSPS or 1MSPS). This is developed and tested against the ADI example FPGA design for this family of ADCs [1]. [1]: http://analogdevicesinc.github.io/hdl/projects/ad469x_fmc/index.html Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v8 changes: * Fixed using wrong register in ad4695_offload_trigger_request() v7 changes: none v6 changes: * Fixed use of c++ style comments * Moved static const struct definition out of probe function * Changes bits_per_word to always be 19 for future oversampling compatibility (Trevor is working on implementing oversampling support on top of this patch, so we have high confidence this is the correct thing to do) * Fixed wrong xfer->len v5 changes: * Register SCLK speed handling has been split out into a separate series. * Add sampling_frequency_available attribute. * Limit max allowed sampling frequency based on chip info. * Expand explanations of offload enable/disable ordering requirements. * Finish TODO to use macros for phandle arg values. * Don't use dev_info() when falling back to non-offload operation. * Update to accommodate changes in other patches in this series. v4 changes: new patch in v4 --- drivers/iio/adc/Kconfig | 1 + drivers/iio/adc/ad4695.c | 445 +++++++++++++++++++++++++++++++++++++++++++= ++-- 2 files changed, 429 insertions(+), 17 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 995b9cacbaa964d26424346120c139858f93cdcd..ec60b64c46e187e2be18ab1f8ca= 9e6f4f03299f9 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -52,6 +52,7 @@ config AD4695 tristate "Analog Device AD4695 ADC Driver" depends on SPI select IIO_BUFFER + select IIO_BUFFER_DMAENGINE select IIO_TRIGGERED_BUFFER select REGMAP help diff --git a/drivers/iio/adc/ad4695.c b/drivers/iio/adc/ad4695.c index 13cf01d35301be40369571e7dd2aeac1a8148d15..d73d2ff776255c851beb7c9b9ef= 90cef9a5f23e1 100644 --- a/drivers/iio/adc/ad4695.c +++ b/drivers/iio/adc/ad4695.c @@ -19,14 +19,19 @@ #include #include #include +#include #include #include #include #include #include +#include #include +#include #include #include +#include +#include #include #include =20 @@ -66,6 +71,8 @@ #define AD4695_REG_STD_SEQ_CONFIG 0x0024 #define AD4695_REG_GPIO_CTRL 0x0026 #define AD4695_REG_GP_MODE 0x0027 +#define AD4695_REG_GP_MODE_BUSY_GP_SEL BIT(5) +#define AD4695_REG_GP_MODE_BUSY_GP_EN BIT(1) #define AD4695_REG_TEMP_CTRL 0x0029 #define AD4695_REG_TEMP_CTRL_TEMP_EN BIT(0) #define AD4695_REG_CONFIG_IN(n) (0x0030 | (n)) @@ -124,13 +131,22 @@ struct ad4695_channel_config { =20 struct ad4695_state { struct spi_device *spi; + struct spi_offload *offload; + struct spi_offload_trigger *offload_trigger; struct regmap *regmap; struct regmap *regmap16; struct gpio_desc *reset_gpio; + /* currently PWM CNV only supported with SPI offload use */ + struct pwm_device *cnv_pwm; + /* protects against concurrent use of cnv_pwm */ + struct mutex cnv_pwm_lock; + /* offload also requires separate gpio to manually control CNV */ + struct gpio_desc *cnv_gpio; /* voltages channels plus temperature and timestamp */ struct iio_chan_spec iio_chan[AD4695_MAX_CHANNELS + 2]; struct ad4695_channel_config channels_cfg[AD4695_MAX_CHANNELS]; const struct ad4695_chip_info *chip_info; + int sample_freq_range[3]; /* Reference voltage. */ unsigned int vref_mv; /* Common mode input pin voltage. */ @@ -355,6 +371,13 @@ static const struct ad4695_chip_info ad4698_chip_info = =3D { .num_voltage_inputs =3D 8, }; =20 +static void ad4695_cnv_manual_trigger(struct ad4695_state *st) +{ + gpiod_set_value_cansleep(st->cnv_gpio, 1); + ndelay(10); + gpiod_set_value_cansleep(st->cnv_gpio, 0); +} + /** * ad4695_set_single_cycle_mode - Set the device in single cycle mode * @st: The AD4695 state @@ -460,6 +483,17 @@ static int ad4695_exit_conversion_mode(struct ad4695_s= tate *st) */ st->cnv_cmd2 =3D AD4695_CMD_EXIT_CNV_MODE << 3; =20 + if (st->cnv_gpio) { + ad4695_cnv_manual_trigger(st); + + /* + * In this case, CNV is not connected to CS, so we don't need + * the extra CS toggle to trigger the conversion and toggling + * CS would have no effect. + */ + return spi_sync_transfer(st->spi, &xfers[1], 1); + } + return spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); } =20 @@ -687,6 +721,160 @@ static irqreturn_t ad4695_trigger_handler(int irq, vo= id *p) return IRQ_HANDLED; } =20 +static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) +{ + struct ad4695_state *st =3D iio_priv(indio_dev); + struct spi_offload_trigger_config config =3D { + .type =3D SPI_OFFLOAD_TRIGGER_DATA_READY, + }; + struct spi_transfer *xfer =3D &st->buf_read_xfer[0]; + struct pwm_state state; + u8 temp_chan_bit =3D st->chip_info->num_voltage_inputs; + u8 num_slots =3D 0; + u8 temp_en =3D 0; + unsigned int bit; + int ret; + + iio_for_each_active_channel(indio_dev, bit) { + if (bit =3D=3D temp_chan_bit) { + temp_en =3D 1; + continue; + } + + ret =3D regmap_write(st->regmap, AD4695_REG_AS_SLOT(num_slots), + FIELD_PREP(AD4695_REG_AS_SLOT_INX, bit)); + if (ret) + return ret; + + num_slots++; + } + + /* + * For non-offload, we could discard data to work around this + * restriction, but with offload, that is not possible. + */ + if (num_slots < 2) { + dev_err(&st->spi->dev, + "At least two voltage channels must be enabled.\n"); + return -EINVAL; + } + + ret =3D regmap_update_bits(st->regmap, AD4695_REG_TEMP_CTRL, + AD4695_REG_TEMP_CTRL_TEMP_EN, + FIELD_PREP(AD4695_REG_TEMP_CTRL_TEMP_EN, + temp_en)); + if (ret) + return ret; + + /* Each BUSY event means just one sample for one channel is ready. */ + memset(xfer, 0, sizeof(*xfer)); + xfer->offload_flags =3D SPI_OFFLOAD_XFER_RX_STREAM; + /* Using 19 bits per word to allow for possible oversampling */ + xfer->bits_per_word =3D 19; + xfer->len =3D 4; + + spi_message_init_with_transfers(&st->buf_read_msg, xfer, 1); + st->buf_read_msg.offload =3D st->offload; + + ret =3D spi_optimize_message(st->spi, &st->buf_read_msg); + if (ret) + return ret; + + /* + * NB: technically, this is part the SPI offload trigger enable, but it + * doesn't work to call it from the offload trigger enable callback + * because it requires accessing the SPI bus. Calling it from the + * trigger enable callback could cause a deadlock. + */ + ret =3D regmap_set_bits(st->regmap, AD4695_REG_GP_MODE, + AD4695_REG_GP_MODE_BUSY_GP_EN); + if (ret) + goto err_unoptimize_message; + + ret =3D spi_offload_trigger_enable(st->offload, st->offload_trigger, + &config); + if (ret) + goto err_disable_busy_output; + + ret =3D ad4695_enter_advanced_sequencer_mode(st, num_slots); + if (ret) + goto err_offload_trigger_disable; + + guard(mutex)(&st->cnv_pwm_lock); + pwm_get_state(st->cnv_pwm, &state); + /* + * PWM subsystem generally rounds down, so requesting 2x minimum high + * time ensures that we meet the minimum high time in any case. + */ + state.duty_cycle =3D AD4695_T_CNVH_NS * 2; + ret =3D pwm_apply_might_sleep(st->cnv_pwm, &state); + if (ret) + goto err_offload_exit_conversion_mode; + + return 0; + +err_offload_exit_conversion_mode: + /* + * We have to unwind in a different order to avoid triggering offload. + * ad4695_exit_conversion_mode() triggers a conversion, so it has to be + * done after spi_offload_trigger_disable(). + */ + spi_offload_trigger_disable(st->offload, st->offload_trigger); + ad4695_exit_conversion_mode(st); + goto err_disable_busy_output; + +err_offload_trigger_disable: + spi_offload_trigger_disable(st->offload, st->offload_trigger); + +err_disable_busy_output: + regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE, + AD4695_REG_GP_MODE_BUSY_GP_EN); + +err_unoptimize_message: + spi_unoptimize_message(&st->buf_read_msg); + + return ret; +} + +static int ad4695_offload_buffer_predisable(struct iio_dev *indio_dev) +{ + struct ad4695_state *st =3D iio_priv(indio_dev); + struct pwm_state state; + int ret; + + scoped_guard(mutex, &st->cnv_pwm_lock) { + pwm_get_state(st->cnv_pwm, &state); + state.duty_cycle =3D 0; + ret =3D pwm_apply_might_sleep(st->cnv_pwm, &state); + if (ret) + return ret; + } + + spi_offload_trigger_disable(st->offload, st->offload_trigger); + + /* + * ad4695_exit_conversion_mode() triggers a conversion, so it has to be + * done after spi_offload_trigger_disable(). + */ + ret =3D ad4695_exit_conversion_mode(st); + if (ret) + return ret; + + ret =3D regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE, + AD4695_REG_GP_MODE_BUSY_GP_EN); + if (ret) + return ret; + + spi_unoptimize_message(&st->buf_read_msg); + + return 0; +} + +static const struct iio_buffer_setup_ops ad4695_offload_buffer_setup_ops = =3D { + .postenable =3D ad4695_offload_buffer_postenable, + .predisable =3D ad4695_offload_buffer_predisable, +}; + /** * ad4695_read_one_sample - Read a single sample using single-cycle mode * @st: The AD4695 state @@ -718,6 +906,13 @@ static int ad4695_read_one_sample(struct ad4695_state = *st, unsigned int address) if (ret) return ret; =20 + /* + * If CNV is connected to CS, the previous function will have triggered + * the conversion, otherwise, we do it manually. + */ + if (st->cnv_gpio) + ad4695_cnv_manual_trigger(st); + /* * Setting the first channel to the temperature channel isn't supported * in single-cycle mode, so we have to do an extra conversion to read @@ -729,6 +924,13 @@ static int ad4695_read_one_sample(struct ad4695_state = *st, unsigned int address) ret =3D spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); if (ret) return ret; + + /* + * If CNV is connected to CS, the previous function will have + * triggered the conversion, otherwise, we do it manually. + */ + if (st->cnv_gpio) + ad4695_cnv_manual_trigger(st); } =20 /* Then read the result and exit conversion mode. */ @@ -842,11 +1044,34 @@ static int ad4695_read_raw(struct iio_dev *indio_dev, default: return -EINVAL; } + case IIO_CHAN_INFO_SAMP_FREQ: { + struct pwm_state state; + + ret =3D pwm_get_state_hw(st->cnv_pwm, &state); + if (ret) + return ret; + + *val =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, state.period); + + return IIO_VAL_INT; + } default: return -EINVAL; } } =20 +static int ad4695_write_raw_get_fmt(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: + return IIO_VAL_INT; + default: + return IIO_VAL_INT_PLUS_MICRO; + } +} + static int ad4695_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) @@ -900,6 +1125,17 @@ static int ad4695_write_raw(struct iio_dev *indio_dev, default: return -EINVAL; } + case IIO_CHAN_INFO_SAMP_FREQ: { + struct pwm_state state; + + if (val <=3D 0 || val > st->chip_info->max_sample_rate) + return -EINVAL; + + guard(mutex)(&st->cnv_pwm_lock); + pwm_get_state(st->cnv_pwm, &state); + state.period =3D DIV_ROUND_UP_ULL(NSEC_PER_SEC, val); + return pwm_apply_might_sleep(st->cnv_pwm, &state); + } default: return -EINVAL; } @@ -923,6 +1159,7 @@ static int ad4695_read_avail(struct iio_dev *indio_dev, */ S16_MIN / 4, 0, 0, MICRO / 4, S16_MAX / 4, S16_MAX % 4 * MICRO / 4 }; + struct ad4695_state *st =3D iio_priv(indio_dev); =20 switch (mask) { case IIO_CHAN_INFO_CALIBSCALE: @@ -943,6 +1180,10 @@ static int ad4695_read_avail(struct iio_dev *indio_de= v, default: return -EINVAL; } + case IIO_CHAN_INFO_SAMP_FREQ: + *vals =3D st->sample_freq_range; + *type =3D IIO_VAL_INT; + return IIO_AVAIL_RANGE; default: return -EINVAL; } @@ -978,6 +1219,7 @@ static int ad4695_debugfs_reg_access(struct iio_dev *i= ndio_dev, =20 static const struct iio_info ad4695_info =3D { .read_raw =3D &ad4695_read_raw, + .write_raw_get_fmt =3D &ad4695_write_raw_get_fmt, .write_raw =3D &ad4695_write_raw, .read_avail =3D &ad4695_read_avail, .debugfs_reg_access =3D &ad4695_debugfs_reg_access, @@ -1091,26 +1333,166 @@ static int ad4695_parse_channel_cfg(struct ad4695_= state *st) return 0; } =20 +static bool ad4695_offload_trigger_match(struct spi_offload_trigger *trigg= er, + enum spi_offload_trigger_type type, + u64 *args, u32 nargs) +{ + if (type !=3D SPI_OFFLOAD_TRIGGER_DATA_READY) + return false; + + /* + * Requires 2 args: + * args[0] is the trigger event. + * args[1] is the GPIO pin number. + */ + if (nargs !=3D 2 || args[0] !=3D AD4695_TRIGGER_EVENT_BUSY) + return false; + + return true; +} + +static int ad4695_offload_trigger_request(struct spi_offload_trigger *trig= ger, + enum spi_offload_trigger_type type, + u64 *args, u32 nargs) +{ + struct ad4695_state *st =3D spi_offload_trigger_get_priv(trigger); + + /* Should already be validated by match, but just in case. */ + if (nargs !=3D 2) + return -EINVAL; + + /* DT tells us if BUSY event uses GP0 or GP3. */ + if (args[1] =3D=3D AD4695_TRIGGER_PIN_GP3) + return regmap_set_bits(st->regmap, AD4695_REG_GP_MODE, + AD4695_REG_GP_MODE_BUSY_GP_SEL); + + return regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE, + AD4695_REG_GP_MODE_BUSY_GP_SEL); +} + +static int +ad4695_offload_trigger_validate(struct spi_offload_trigger *trigger, + struct spi_offload_trigger_config *config) +{ + if (config->type !=3D SPI_OFFLOAD_TRIGGER_DATA_READY) + return -EINVAL; + + return 0; +} + +/* + * NB: There are no enable/disable callbacks here due to requiring a SPI + * message to enable or disable the BUSY output on the ADC. + */ +static const struct spi_offload_trigger_ops ad4695_offload_trigger_ops =3D= { + .match =3D ad4695_offload_trigger_match, + .request =3D ad4695_offload_trigger_request, + .validate =3D ad4695_offload_trigger_validate, +}; + +static void ad4695_pwm_disable(void *pwm) +{ + pwm_disable(pwm); +} + +static int ad4695_probe_spi_offload(struct iio_dev *indio_dev, + struct ad4695_state *st) +{ + struct device *dev =3D &st->spi->dev; + struct spi_offload_trigger_info trigger_info =3D { + .fwnode =3D dev_fwnode(dev), + .ops =3D &ad4695_offload_trigger_ops, + .priv =3D st, + }; + struct pwm_state pwm_state; + struct dma_chan *rx_dma; + int ret, i; + + indio_dev->num_channels =3D st->chip_info->num_voltage_inputs + 1; + indio_dev->setup_ops =3D &ad4695_offload_buffer_setup_ops; + + if (!st->cnv_gpio) + return dev_err_probe(dev, -ENODEV, + "CNV GPIO is required for SPI offload\n"); + + ret =3D devm_spi_offload_trigger_register(dev, &trigger_info); + if (ret) + return dev_err_probe(dev, ret, + "failed to register offload trigger\n"); + + st->offload_trigger =3D devm_spi_offload_trigger_get(dev, st->offload, + SPI_OFFLOAD_TRIGGER_DATA_READY); + if (IS_ERR(st->offload_trigger)) + return dev_err_probe(dev, PTR_ERR(st->offload_trigger), + "failed to get offload trigger\n"); + + ret =3D devm_mutex_init(dev, &st->cnv_pwm_lock); + if (ret) + return ret; + + st->cnv_pwm =3D devm_pwm_get(dev, NULL); + if (IS_ERR(st->cnv_pwm)) + return dev_err_probe(dev, PTR_ERR(st->cnv_pwm), + "failed to get CNV PWM\n"); + + pwm_init_state(st->cnv_pwm, &pwm_state); + + /* If firmware didn't provide default rate, use 10kHz (arbitrary). */ + if (pwm_state.period =3D=3D 0) + pwm_state.period =3D 100 * MILLI; + + pwm_state.enabled =3D true; + + ret =3D pwm_apply_might_sleep(st->cnv_pwm, &pwm_state); + if (ret) + return dev_err_probe(dev, ret, "failed to apply CNV PWM\n"); + + ret =3D devm_add_action_or_reset(dev, ad4695_pwm_disable, st->cnv_pwm); + if (ret) + return ret; + + rx_dma =3D devm_spi_offload_rx_stream_request_dma_chan(dev, st->offload); + if (IS_ERR(rx_dma)) + return dev_err_probe(dev, PTR_ERR(rx_dma), + "failed to get offload RX DMA\n"); + + for (i =3D 0; i < indio_dev->num_channels; i++) { + struct iio_chan_spec *chan =3D &st->iio_chan[i]; + + /* + * NB: When using offload support, all channels need to have the + * same bits_per_word because they all use the same SPI message + * for reading one sample. In order to prevent breaking + * userspace in the future when oversampling support is added, + * all channels are set read 19 bits with a shift of 3 to mask + * out the extra bits even though we currently only support 16 + * bit samples (oversampling ratio =3D=3D 1). + */ + chan->scan_type.shift =3D 3; + chan->scan_type.storagebits =3D 32; + /* add sample frequency for PWM CNV trigger */ + chan->info_mask_separate |=3D BIT(IIO_CHAN_INFO_SAMP_FREQ); + chan->info_mask_separate_available |=3D BIT(IIO_CHAN_INFO_SAMP_FREQ); + } + + return devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev, + rx_dma, IIO_BUFFER_DIRECTION_IN); +} + +static const struct spi_offload_config ad4695_spi_offload_config =3D { + .capability_flags =3D SPI_OFFLOAD_CAP_TRIGGER | + SPI_OFFLOAD_CAP_RX_STREAM_DMA, +}; + static int ad4695_probe(struct spi_device *spi) { struct device *dev =3D &spi->dev; struct ad4695_state *st; struct iio_dev *indio_dev; - struct gpio_desc *cnv_gpio; bool use_internal_ldo_supply; bool use_internal_ref_buffer; int ret; =20 - cnv_gpio =3D devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW); - if (IS_ERR(cnv_gpio)) - return dev_err_probe(dev, PTR_ERR(cnv_gpio), - "Failed to get CNV GPIO\n"); - - /* Driver currently requires CNV pin to be connected to SPI CS */ - if (cnv_gpio) - return dev_err_probe(dev, -ENODEV, - "CNV GPIO is not supported\n"); - indio_dev =3D devm_iio_device_alloc(dev, sizeof(*st)); if (!indio_dev) return -ENOMEM; @@ -1122,6 +1504,10 @@ static int ad4695_probe(struct spi_device *spi) if (!st->chip_info) return -EINVAL; =20 + st->sample_freq_range[0] =3D 1; /* min */ + st->sample_freq_range[1] =3D 1; /* step */ + st->sample_freq_range[2] =3D st->chip_info->max_sample_rate; /* max */ + st->regmap =3D devm_regmap_init(dev, &ad4695_regmap_bus, st, &ad4695_regmap_config); if (IS_ERR(st->regmap)) @@ -1134,6 +1520,11 @@ static int ad4695_probe(struct spi_device *spi) return dev_err_probe(dev, PTR_ERR(st->regmap16), "Failed to initialize regmap16\n"); =20 + st->cnv_gpio =3D devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW); + if (IS_ERR(st->cnv_gpio)) + return dev_err_probe(dev, PTR_ERR(st->cnv_gpio), + "Failed to get CNV GPIO\n"); + ret =3D devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4695_power_supplies), ad4695_power_supplies); @@ -1261,12 +1652,31 @@ static int ad4695_probe(struct spi_device *spi) indio_dev->channels =3D st->iio_chan; indio_dev->num_channels =3D st->chip_info->num_voltage_inputs + 2; =20 - ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, - iio_pollfunc_store_time, - ad4695_trigger_handler, - &ad4695_buffer_setup_ops); - if (ret) - return ret; + st->offload =3D devm_spi_offload_get(dev, spi, &ad4695_spi_offload_config= ); + ret =3D PTR_ERR_OR_ZERO(st->offload); + if (ret && ret !=3D -ENODEV) + return dev_err_probe(dev, ret, "failed to get SPI offload\n"); + + /* If no SPI offload, fall back to low speed usage. */ + if (ret =3D=3D -ENODEV) { + /* Driver currently requires CNV pin to be connected to SPI CS */ + if (st->cnv_gpio) + return dev_err_probe(dev, -EINVAL, + "CNV GPIO is not supported\n"); + + indio_dev->num_channels =3D st->chip_info->num_voltage_inputs + 2; + + ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, + iio_pollfunc_store_time, + ad4695_trigger_handler, + &ad4695_buffer_setup_ops); + if (ret) + return ret; + } else { + ret =3D ad4695_probe_spi_offload(indio_dev, st); + if (ret) + return ret; + } =20 return devm_iio_device_register(dev, indio_dev); } @@ -1303,3 +1713,4 @@ MODULE_AUTHOR("Ramona Gradinariu "); MODULE_AUTHOR("David Lechner "); MODULE_DESCRIPTION("Analog Devices AD4695 ADC driver"); MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER"); --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 128CA1DF26A for ; Fri, 7 Feb 2025 20:09:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958997; cv=none; b=op+A5K6V3l8nq6GXkNWRiN3kZ8lwHE8uq4+orh9na7r5NU/0Utih36hjuR8FlTawNirMQXDI/MyiKqxqRR3HR3doSAn7GmtHieGCjF9vnwfxQQC5Hh0Qo+uClhPUQHljw+iZUwR2QAoLFKsFBzE4bSPzVPM1K149YgBlC4YAvK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958997; c=relaxed/simple; bh=DhX51StZjS6ZG3+/IYLjnj3db6LuckpDYDUdYX9k1yk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ragHqbLS2lnyx4E12kir1JDIoqnOObdA1QqIPDWhAdPHDF2mD5jSFTsiZtbhKmm9yb+hQszBFP2pbsDSVx3StSipYtYX3m5hQ/UivxOhhI1Pn1onJA7MsM0mSd97RGlomSvUfLJfKQQg9DgdUQlwfGfc8JtJpcySmHbg34rJGHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=UIusOfQF; arc=none smtp.client-ip=209.85.161.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="UIusOfQF" Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-5f6b65c89c4so630875eaf.2 for ; Fri, 07 Feb 2025 12:09:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958994; x=1739563794; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=3uXVd6FnRPX1W8Wg/eWBdOZEvpGo7dHFBW2WCW32Bdw=; b=UIusOfQFpbY0NZvz5tAJ9fg8VlI6XO5rYeDpZsnIEI1hMh/11Fnm2tAsTds3HbjQWt m25ljZfwm3dwa1Ak8jrt/ywVI+R0kCULi1DBT5+T04Jx9wGklL1g4FIlHUbnpnBeFhVO EPXFfVOpQtIqPI7hFU/Mzcz+rLRUTHGVfTMs6UT+C2tsuiS/ymBIFtSA3ZwBCj3YOFNX q2nzi7fspti4uM7BnoJqUluIeJ6gnQB/5xg5H9sU5lLuLG430EEvx5Ln6YbjPHicFd0k dD3VRZAGH4dXbxjywOhHWWrxPQLxBs1imrTA16DJoDO0z9OAY4MEziEaHHftWV2E7Bv4 hb1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958994; x=1739563794; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3uXVd6FnRPX1W8Wg/eWBdOZEvpGo7dHFBW2WCW32Bdw=; b=Jr0lZ7kr1GoUgV+e4rMiZbMDQSR3LJt1Ljdf/5SzbEqCg4sUnZvBH1Z3QqnhkWxfMo McSVlqHXtmjOBLd9FlNUprcjJenCVAXayyZYtxnk/Ubzb3ptZ4qdi08HSSCCnPS3hgRF fzKcXSPjquNl/kOdchhs1abNPEiGzZWyBD87Z7nLupwezGeO8jPSLoOzZuoSsDwgvTIE hopxR+dr3d+sDXtTfIOauTyuVByq0qb6+9iCigbTjR48vW98VaF6cLqYKy6M4WxAk1NX 9nK5YdtqKqZsp2rfezbpYJ8VDBJFmocSl6tDkzKEaKoHlcEmk8VkL6nSoI5bvn0ebPDD yhyA== X-Forwarded-Encrypted: i=1; AJvYcCXV4Unx36o24NLfjPEdept+uaTczHKUibyoDMNZlRrM5bm+ktNKd6zPBqs5xiLNDh/JN4is5zzIHOpsmLk=@vger.kernel.org X-Gm-Message-State: AOJu0YyQcz8bxB0TubXrkXGgR9Am9xU0GiS/eRvR1BKESlikI2YXmUuK DRlAxfVXqr47wyLPhosyXCprPGZzfW5WfoCmYsZ7QBrCyOuNCvgXJG/iNZgbzhk= X-Gm-Gg: ASbGncvylQero1U+ti8a4AJKALmKUGuhlIsznO1PF9pPAMl7jjTlT0TFl4jfB74zBEi tqzajY+US/9ZO8azYvT62kt7zJb2TnHiwsu+5Z6Aur+Z2588yDXx/Wsp0ORvZlYieQT974Pp+XD AUO4w86rPRqKwBQT8yzSui//J0NGSksabOgZ8Uo7SiHcHrR6FOmRA5KlA5H4ATn4yOditYfiLhF Pv/XIy4dqdaFl7nJFK+4SUsNfkCpSdgnPrbCgp92JKKOB5RTKqzqBCYSpVmkznfxW3oO7dT8LWS 2qsW4oENA/51BPeSh+DqzO8mtkjyAs79mNxB0kVzH+BLNaM= X-Google-Smtp-Source: AGHT+IHBvhoA/1vsHrBvSb8E+WSGvN4wz1yxpemeyG9bRp8GQ1qD6XCZmUkIVAeGNsD1uexoiKGc/Q== X-Received: by 2002:a05:6820:80c:b0:5f3:4175:1d7f with SMTP id 006d021491bc7-5fc5e75e777mr2414422eaf.8.1738958994116; Fri, 07 Feb 2025 12:09:54 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:53 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:12 -0600 Subject: [PATCH v8 15/17] doc: iio: ad4695: add SPI offload support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-15-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, David Lechner X-Mailer: b4 0.14.2 Document SPI offload support for the ad4695 driver. Signed-off-by: David Lechner --- v7 changes: none v6 changes: * Fixed double "all" * Don't abbreviate "sampling_frequency" attribute names for clarity. v5 changes: new patch in v5 --- Documentation/iio/ad4695.rst | 68 ++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 68 insertions(+) diff --git a/Documentation/iio/ad4695.rst b/Documentation/iio/ad4695.rst index 9ec8bf466c15bf94cbae2ebdb61875f66af1264f..ead0faadff4b5aede2a34bac52e= 103e375da6f21 100644 --- a/Documentation/iio/ad4695.rst +++ b/Documentation/iio/ad4695.rst @@ -47,6 +47,36 @@ In this mode, CNV and CS are tied together and there is = a single SDO line. To use this mode, in the device tree, omit the ``cnv-gpios`` and ``spi-rx-bus-width`` properties. =20 +SPI offload wiring +^^^^^^^^^^^^^^^^^^ + +When used with a SPI offload, the supported wiring configuration is: + +.. code-block:: + + +-------------+ +-------------+ + | GP0/BUSY |-------->| TRIGGER | + | CS |<--------| CS | + | | | | + | ADC | | SPI | + | | | | + | SDI |<--------| SDO | + | SDO |-------->| SDI | + | SCLK |<--------| SCLK | + | | | | + | | +-------------+ + | CNV |<-----+--| PWM | + | | +--| GPIO | + +-------------+ +-------------+ + +In this case, both the ``cnv-gpios`` and ``pwms`` properties are required. +The ``#trigger-source-cells =3D <2>`` property is also required to connect= back +to the SPI offload. The SPI offload will have ``trigger-sources`` property +with cells to indicate the busy signal and which GPx pin is used, e.g +``<&ad4695 AD4695_TRIGGER_EVENT_BUSY AD4695_TRIGGER_PIN_GP0>``. + +.. seealso:: `SPI offload support`_ + Channel configuration --------------------- =20 @@ -158,6 +188,27 @@ Unimplemented features - GPIO support - CRC support =20 +SPI offload support +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +To be able to achieve the maximum sample rate, the driver can be used with= the +`AXI SPI Engine`_ to provide SPI offload support. + +.. _AXI SPI Engine: http://analogdevicesinc.github.io/hdl/projects/ad469x_= fmc/index.html + +.. seealso:: `SPI offload wiring`_ + +When SPI offload is being used, some attributes will be different. + +* ``trigger`` directory is removed. +* ``in_voltage0_sampling_frequency`` attributes are added for setting the = sample + rate. +* ``in_voltage0_sampling_frequency_available`` attributes are added for qu= erying + the max sample rate. +* ``timestamp`` channel is removed. +* Buffer data format may be different compared to when offload is not used, + e.g. the ``buffer0/in_voltage0_type`` attribute. + Device buffers =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -165,3 +216,20 @@ This driver supports hardware triggered buffers. This = uses the "advanced sequencer" feature of the chip to trigger a burst of conversions. =20 Also see :doc:`iio_devbuf` for more general information. + +Effective sample rate for buffered reads +---------------------------------------- + +When SPI offload is not used, the sample rate is determined by the trigger= that +is manually configured in userspace. All enabled channels will be read in a +burst when the trigger is received. + +When SPI offload is used, the sample rate is configured per channel. All +channels will have the same rate, so only one ``in_voltageY_sampling_frequ= ency`` +attribute needs to be set. Since this rate determines the delay between ea= ch +individual conversion, the effective sample rate for each sample is actual= ly +the sum of the periods of each enabled channel in a buffered read. In other +words, it is the value of the ``in_voltageY_sampling_frequency`` attribute +divided by the number of enabled channels. So if 4 channels are enabled, w= ith +the ``in_voltageY_sampling_frequency`` attributes set to 1 MHz, the effect= ive +sample rate is 250 kHz. --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06B631DF75C for ; Fri, 7 Feb 2025 20:09:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958998; cv=none; b=BgpKyN+BhMSGjrL72Ypw9txzmoIZQ8tbR1zlgpsPqcmFcpsyW8CeC2/i0k08iJEy+/Wxzki196KXT7o3h0NCwH+XklE5817fzr0HgV3LXDouPxNR5z9uEVe6y9s2WTSOofuO14tBcwvO4RreLFA2wffUQq8e63Cda7iKV/rHGNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738958998; c=relaxed/simple; bh=SMPmN8r0eR+8HY2Jj8x2xcYlPFmCLQlrLCIyeBHoKlc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fZ0elaKDOQIxYVeyfhf770yPFZrInnM10wKRFPRLw/p+SsenpJE4pQ35ho7OLL18HQ7cbr3w0RF34KxzsPhvUhvFqrYZEDzamLtYHaul7QA8lXgm/kGFddm8P7Lu/xJLObjerNcdPvbIZZjxdtGIwltPKUdamijnDvBRiYUhUYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=LDYYMqJO; arc=none smtp.client-ip=209.85.161.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="LDYYMqJO" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5f88a93dceeso800557eaf.2 for ; Fri, 07 Feb 2025 12:09:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958996; x=1739563796; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=bhv4ANzzEFKwoR1NSwNAajicR1HfyMZAyaDEFtvhtoA=; b=LDYYMqJOG7yg/xQ2ScnUuEAtiJxxuA/BCwvVLZTh9szN1X+FEF13a++vkakF8Z1L1e hKdRoQKb7RX+GuP/u/kqGLz6kzrZh3kuxxKyReu9h3ItazMmdYlYVpOWUhuMVav4rMe2 jqBKNj/Kgf7ch0gKKKWpQCvGBgSl8YORxjTSxbrd3XE3/5XPKfrKAP386xUyWg9mvzSc fTn8PNlaHmb15kZi2OE1ZWMTnQbVkxUOIWrIQbOt0AdM2PzMeSuIHkDbCVAitlOvK5YO YAkv+94v+Ip05KcXfTgeTsKpcr6vpboJk7UotgL04vQEercKlBwsj4WdX8aT/UwqN4r2 W14A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958996; x=1739563796; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bhv4ANzzEFKwoR1NSwNAajicR1HfyMZAyaDEFtvhtoA=; b=FSx67TjyaI6JAZBHaAdw5iFeYzbYwk4onoOSuu6xj0Us1XxHBRw2etQ0ouAuktAnch xGMV/ZXAJg82Wvj942fISg8Bk0KZqyjoX4qCDggEODW5AWN60hdXpgRbZKaTqroy7yVU VY8scraHprA0Uv2Q0wD+L51MpujvioikMpaThIhvEYpontpkndN5lBpohYnUCYhJ5qGY TaC4CuWtbm7x6R3R8budgo1e1VTmbzpndBXv4NxZEb6HKoW9CoGaEiu3sbMfEmjbrmYk 1ZHLVUqdxBGU5UA9N4LeETD1wqAW/aQXRC8HTi2kERa0kb9PyJ6Nco9VInqS9A1ll9j0 fWIA== X-Forwarded-Encrypted: i=1; AJvYcCWfawhdjMN2txxghdAVmdCuXGsv7F5KGe8Vbeq1xPwJ+efCWL4Qq3J5YLZoSKbVaRr6lSJtyWBR4Rs+f3c=@vger.kernel.org X-Gm-Message-State: AOJu0YwrGVBYlvidq34pMLfHb1FDK33pWDziAHBZ0uLggE04dtoPxqSB KO3h/s3YnMcyxocv3azXil5pnWTT9yUrzFNNQPx6bKjU34LgL296uGZN7Wk6j54= X-Gm-Gg: ASbGncv3HKxdn5PL+B+w8rCTlTFw+vEhxCOneGzWg90MtUsbMHja92Qdc1GVck73q5D Y5G+UVbwCCn1mfkJq5sG6IlQ2/1W0UyzGjtvAJnJeB0E86+ytA3KG72cKwN1+Ok/GmpQlSEph0A gZLb1chZsu2G8aKxZ/CE5bYItFZgjNNyxLTdDgbAPA+9/x4NZRdYaRAP2JEJzyX8EuTmZbs7nwm mi/VMrAUOZENgb7tzGYiykAdWZ0nfmBb/hA2yAGJQWElbyaj+yWFKWUugbKZ1czV8iziitVvEaZ V0moFSfTx9BZxNjNDTgmB8K7j61GFQ4ntqDBK3Nwj056kdA= X-Google-Smtp-Source: AGHT+IEqV/I6/z7/jDgPXn8cMBQ3jLjBLYlnGZUzifVpoGCFn9izQNeuUTDoXrR00+qnRlk+dTe7Pw== X-Received: by 2002:a05:6820:605:b0:5f9:4f8c:868e with SMTP id 006d021491bc7-5fc5e6001ffmr3171575eaf.2.1738958996002; Fri, 07 Feb 2025 12:09:56 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:54 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:13 -0600 Subject: [PATCH v8 16/17] iio: dac: ad5791: sort include directives Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-16-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, David Lechner X-Mailer: b4 0.14.2 Sort includes alphabetically before we add more in a later patch. Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: none v6 changes: new patch in v6 --- drivers/iio/dac/ad5791.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/iio/dac/ad5791.c b/drivers/iio/dac/ad5791.c index 034228a7c059f136cdecfb4683228d0be4c37477..4f28f49071c244bfcd21c7553aa= 3e39e9751490a 100644 --- a/drivers/iio/dac/ad5791.c +++ b/drivers/iio/dac/ad5791.c @@ -6,21 +6,21 @@ * Copyright 2011 Analog Devices Inc. */ =20 -#include -#include -#include +#include #include +#include +#include +#include #include -#include +#include +#include #include +#include #include -#include -#include -#include =20 +#include #include #include -#include =20 #define AD5791_DAC_MASK GENMASK(19, 0) =20 --=20 2.43.0 From nobody Sun Dec 14 19:12:28 2025 Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D48891DFE0C for ; Fri, 7 Feb 2025 20:09:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738959002; cv=none; b=ei9SlyCm2b6AKZFtXFTgFrmx5p/9gawh11R1Cy2Day92TM8195LrgrW+Ec7h2PjJEL5klrLpEv5Mt7Gqma9WPNjO7yE8bCJe4TayA7+BsmuUeTrqDOfnjE79Wc3UX7qmAW4snn2jxma8yMsuHB36X3JNugAz0CX9eDgK9tTw/MA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738959002; c=relaxed/simple; bh=tLizlzLwgQHZVLFutxZhfY3pv9A8Teg53pGXyak+xGE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FDnfGflH2645ZaD6HV65/ILq6W9fDXT3j7CXA1W+7y9fKAT8YTXK8B0tL/PhHvQ1G6KErCy6hcmuXJGCKm0PzLxZ2VycjrkB5DtH8dIGIMtMySOf7QyuRkvE4M0BKm8y1bFDw11w0ozS+WEYph6AyJkRtBofpjmHthVCO5LRILI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=blKpFvjl; arc=none smtp.client-ip=209.85.210.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="blKpFvjl" Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-71e565708beso1503173a34.1 for ; Fri, 07 Feb 2025 12:09:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1738958999; x=1739563799; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=swdhvOmsaXx4i7dmA/GM5tND4Q4MNmxeKJptvQs4OEQ=; b=blKpFvjlXVQfTdNiVN/EcYi85K3CD3hu5RZT7Jh8K91uLtHQ+b+7/Y2HV4JLY45T2i 47FhrDIOesVrVsgth7RyE6GiyEOtt0pOMnqqsDDT0tO6RFYpmhsbkVIwiPB4oxUGCP3j AM0s+BP/SKHMlkj9wifyeT4OsKjz/7PusTPmy3OQL3hQhMB10TdyCNOgUz9cFXADl39M jHtfjIAsj7rtTKnFMmJaQr5Dgz2Tq7+7ZP+xD2FhsHgfEtBorlrLKBEPBlUfL6cYPBzC 3TqGiNhL7YGxEoebQnAn0iFSoHYKX5FWgkVfjTCD2fQhwIynbCk2hQHZ/8zFzAFj491c 3dRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738958999; x=1739563799; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=swdhvOmsaXx4i7dmA/GM5tND4Q4MNmxeKJptvQs4OEQ=; b=uUC7Bh2Q9XMwKOsSCyvbp//oVGzMpj5gxQLev6mXRncscEkirYRhAFjJBnTDsogYzT RqdSjbJDputRzLDJaPr+VlJoysdBmpaIoEzSvCdS6OQHXRKEx1ACTU+tiYl71O48qWZS zaZw0BvaomTbb+oPP59iXPdrzAxz7wyZlzU+9VQNJB8jWXmQWDuJvFxlBDdWXr2WGVSh fx9J6QDDY+msjvr/ewERGBVS+GZBAMF/tjual2YWhy3CdyKhZ8UifjtKMjSbaoGfH1xU qKpk+5GobWEqR1r34duttB6l2ZgwH9mUV2LnJ06qD+n/9iZW3NOjTs3gDuR729SUIESd 5TWg== X-Forwarded-Encrypted: i=1; AJvYcCV3X1hSpIdnf7wgeBu6EtNhg1UWWXtoYcAlhVj5G4NV8NXMNUzBwNf54thnPfhVA2CnfDy1rtSTfyMBHvc=@vger.kernel.org X-Gm-Message-State: AOJu0YxzjBxDIXIjXXbBoH9ak15pkN0tVDPU4Bdpvhzdy91JiaPOseS5 Gm3uE6le4f+TxHwC/QvUYvHGeavaQ8n+4ZOI9C6pWaTKCHmN1g73jQdJXLaM5uM= X-Gm-Gg: ASbGncv59UsEW2jhsgP89e38H11LgLllLq55nmLDeyYvpkrO+zuiqeH6hBKqQnN4mG9 MWQC+5ansmSBeOPki3wsAOKMzca2EHPBAcpFO7avPP/5wMk45nN2ZUmdlU93YVp39XrNT0d/2Dz 3MkFRIANl4Y2moCSGAHKXuxtPtV8KBdlc7GjMPq+DciOId8X313ZwrDEn5eK67a1ovhEeH4eA3B 4G1jIUunmxPF7b3zMg9q7seGwQP3PhGHUWo2p8PVK/xsMQ/n/T6UqhX6mQjjY7zNGwFd1GbmeMr kFxfTdB+o3WAjT1DG8WR5vreYzFW3DfKyL3wdDBpC3zQzsM= X-Google-Smtp-Source: AGHT+IH91tufyShH1cB+QwfqnbPdhPjOBiatDQJckt758pCIvv3nuGpnBSZuIupBhL5FP+01Xc2JEQ== X-Received: by 2002:a05:6830:6c10:b0:71d:eee3:fd26 with SMTP id 46e09a7af769-726b87b7814mr3104475a34.4.1738958998924; Fri, 07 Feb 2025 12:09:58 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5fc544b08d4sm930387eaf.2.2025.02.07.12.09.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 12:09:57 -0800 (PST) From: David Lechner Date: Fri, 07 Feb 2025 14:09:14 -0600 Subject: [PATCH v8 17/17] iio: dac: ad5791: Add offload support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250207-dlech-mainline-spi-engine-offload-2-v8-17-e48a489be48c@baylibre.com> References: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> In-Reply-To: <20250207-dlech-mainline-spi-engine-offload-2-v8-0-e48a489be48c@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, Axel Haslam , Jonathan Cameron , David Lechner X-Mailer: b4 0.14.2 From: Axel Haslam Add SPI offload support to stream TX buffers using DMA. This allows loading samples to the DAC with a rate of 1 MSPS. Signed-off-by: Axel Haslam Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa Signed-off-by: David Lechner --- v7 changes: * Removed extra space character. * Added write_raw_get_fmt callback to avoid having to check val2. * Don't allow sampling frequency of 0 Hz. v6 changes: new patch in v6 --- drivers/iio/dac/Kconfig | 3 + drivers/iio/dac/ad5791.c | 163 +++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 166 insertions(+) diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig index 5690a37267d86ea3ec805adfa32e13c052864061..4811ea973125a0dea1f8a9cdee1= e0c045bc21981 100644 --- a/drivers/iio/dac/Kconfig +++ b/drivers/iio/dac/Kconfig @@ -296,6 +296,9 @@ config AD5770R config AD5791 tristate "Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC SPI drive= r" depends on SPI + select SPI_OFFLOAD + select IIO_BUFFER + select IIO_BUFFER_DMAENGINE help Say yes here to build support for Analog Devices AD5760, AD5780, AD5781, AD5790, AD5791 High Resolution Voltage Output Digital to diff --git a/drivers/iio/dac/ad5791.c b/drivers/iio/dac/ad5791.c index 4f28f49071c244bfcd21c7553aa3e39e9751490a..07848be3f8d5654418ebbac7b33= c14fd1014a2c8 100644 --- a/drivers/iio/dac/ad5791.c +++ b/drivers/iio/dac/ad5791.c @@ -15,9 +15,12 @@ #include #include #include +#include #include #include +#include =20 +#include #include #include #include @@ -64,11 +67,13 @@ * struct ad5791_chip_info - chip specific information * @name: name of the dac chip * @channel: channel specification + * @channel_offload: channel specification for offload * @get_lin_comp: function pointer to the device specific function */ struct ad5791_chip_info { const char *name; const struct iio_chan_spec channel; + const struct iio_chan_spec channel_offload; int (*get_lin_comp)(unsigned int span); }; =20 @@ -81,6 +86,11 @@ struct ad5791_chip_info { * @gpio_clear: clear gpio * @gpio_ldac: load dac gpio * @chip_info: chip model specific constants + * @offload_msg: spi message used for offload + * @offload_xfer: spi transfer used for offload + * @offload: offload device + * @offload_trigger: offload trigger + * @offload_trigger_hz: offload sample rate * @vref_mv: actual reference voltage used * @vref_neg_mv: voltage of the negative supply * @ctrl: control register cache @@ -96,6 +106,11 @@ struct ad5791_state { struct gpio_desc *gpio_clear; struct gpio_desc *gpio_ldac; const struct ad5791_chip_info *chip_info; + struct spi_message offload_msg; + struct spi_transfer offload_xfer; + struct spi_offload *offload; + struct spi_offload_trigger *offload_trigger; + unsigned int offload_trigger_hz; unsigned short vref_mv; unsigned int vref_neg_mv; unsigned ctrl; @@ -232,6 +247,25 @@ static int ad5780_get_lin_comp(unsigned int span) return AD5780_LINCOMP_10_20; } =20 +static int ad5791_set_sample_freq(struct ad5791_state *st, int val) +{ + struct spi_offload_trigger_config config =3D { + .type =3D SPI_OFFLOAD_TRIGGER_PERIODIC, + .periodic =3D { + .frequency_hz =3D val, + }, + }; + int ret; + + ret =3D spi_offload_trigger_validate(st->offload_trigger, &config); + if (ret) + return ret; + + st->offload_trigger_hz =3D config.periodic.frequency_hz; + + return 0; +} + static int ad5791_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, @@ -259,6 +293,9 @@ static int ad5791_read_raw(struct iio_dev *indio_dev, do_div(val64, st->vref_mv); *val =3D -val64; return IIO_VAL_INT; + case IIO_CHAN_INFO_SAMP_FREQ: + *val =3D st->offload_trigger_hz; + return IIO_VAL_INT; default: return -EINVAL; } @@ -299,6 +336,24 @@ static const struct ad5791_chip_info _name##_chip_info= =3D { \ }, \ .ext_info =3D ad5791_ext_info, \ }, \ + .channel_offload =3D { \ + .type =3D IIO_VOLTAGE, \ + .output =3D 1, \ + .indexed =3D 1, \ + .address =3D AD5791_ADDR_DAC0, \ + .channel =3D 0, \ + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type =3D BIT(IIO_CHAN_INFO_SCALE) | \ + BIT(IIO_CHAN_INFO_OFFSET), \ + .info_mask_shared_by_all =3D BIT(IIO_CHAN_INFO_SAMP_FREQ),\ + .scan_type =3D { \ + .sign =3D 'u', \ + .realbits =3D (bits), \ + .storagebits =3D 32, \ + .shift =3D (_shift), \ + }, \ + .ext_info =3D ad5791_ext_info, \ + }, \ } =20 AD5791_DEFINE_CHIP_INFO(ad5760, 16, 4, ad5780_get_lin_comp); @@ -322,14 +377,106 @@ static int ad5791_write_raw(struct iio_dev *indio_de= v, =20 return ad5791_spi_write(st, chan->address, val); =20 + case IIO_CHAN_INFO_SAMP_FREQ: + if (val < 1) + return -EINVAL; + return ad5791_set_sample_freq(st, val); default: return -EINVAL; } } =20 +static int ad5791_write_raw_get_fmt(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: + return IIO_VAL_INT; + default: + return IIO_VAL_INT_PLUS_MICRO; + } +} + +static int ad5791_buffer_preenable(struct iio_dev *indio_dev) +{ + struct ad5791_state *st =3D iio_priv(indio_dev); + struct spi_offload_trigger_config config =3D { + .type =3D SPI_OFFLOAD_TRIGGER_PERIODIC, + .periodic =3D { + .frequency_hz =3D st->offload_trigger_hz, + }, + }; + + if (st->pwr_down) + return -EINVAL; + + return spi_offload_trigger_enable(st->offload, st->offload_trigger, + &config); +} + +static int ad5791_buffer_postdisable(struct iio_dev *indio_dev) +{ + struct ad5791_state *st =3D iio_priv(indio_dev); + + spi_offload_trigger_disable(st->offload, st->offload_trigger); + + return 0; +} + +static const struct iio_buffer_setup_ops ad5791_buffer_setup_ops =3D { + .preenable =3D &ad5791_buffer_preenable, + .postdisable =3D &ad5791_buffer_postdisable, +}; + +static int ad5791_offload_setup(struct iio_dev *indio_dev) +{ + struct ad5791_state *st =3D iio_priv(indio_dev); + struct spi_device *spi =3D st->spi; + struct dma_chan *tx_dma; + int ret; + + st->offload_trigger =3D devm_spi_offload_trigger_get(&spi->dev, + st->offload, SPI_OFFLOAD_TRIGGER_PERIODIC); + if (IS_ERR(st->offload_trigger)) + return dev_err_probe(&spi->dev, PTR_ERR(st->offload_trigger), + "failed to get offload trigger\n"); + + ret =3D ad5791_set_sample_freq(st, 1 * MEGA); + if (ret) + return dev_err_probe(&spi->dev, ret, + "failed to init sample rate\n"); + + tx_dma =3D devm_spi_offload_tx_stream_request_dma_chan(&spi->dev, + st->offload); + if (IS_ERR(tx_dma)) + return dev_err_probe(&spi->dev, PTR_ERR(tx_dma), + "failed to get offload TX DMA\n"); + + ret =3D devm_iio_dmaengine_buffer_setup_with_handle(&spi->dev, + indio_dev, tx_dma, IIO_BUFFER_DIRECTION_OUT); + if (ret) + return ret; + + st->offload_xfer.len =3D 4; + st->offload_xfer.bits_per_word =3D 24; + st->offload_xfer.offload_flags =3D SPI_OFFLOAD_XFER_TX_STREAM; + + spi_message_init_with_transfers(&st->offload_msg, &st->offload_xfer, 1); + st->offload_msg.offload =3D st->offload; + + return devm_spi_optimize_message(&spi->dev, st->spi, &st->offload_msg); +} + static const struct iio_info ad5791_info =3D { .read_raw =3D &ad5791_read_raw, .write_raw =3D &ad5791_write_raw, + .write_raw_get_fmt =3D &ad5791_write_raw_get_fmt, +}; + +static const struct spi_offload_config ad5791_offload_config =3D { + .capability_flags =3D SPI_OFFLOAD_CAP_TRIGGER | + SPI_OFFLOAD_CAP_TX_STREAM_DMA, }; =20 static int ad5791_probe(struct spi_device *spi) @@ -416,6 +563,21 @@ static int ad5791_probe(struct spi_device *spi) indio_dev->channels =3D &st->chip_info->channel; indio_dev->num_channels =3D 1; indio_dev->name =3D st->chip_info->name; + + st->offload =3D devm_spi_offload_get(&spi->dev, spi, &ad5791_offload_conf= ig); + ret =3D PTR_ERR_OR_ZERO(st->offload); + if (ret && ret !=3D -ENODEV) + return dev_err_probe(&spi->dev, ret, "failed to get offload\n"); + + if (ret !=3D -ENODEV) { + indio_dev->channels =3D &st->chip_info->channel_offload; + indio_dev->setup_ops =3D &ad5791_buffer_setup_ops; + ret =3D ad5791_offload_setup(indio_dev); + if (ret) + return dev_err_probe(&spi->dev, ret, + "fail to setup offload\n"); + } + return devm_iio_device_register(&spi->dev, indio_dev); } =20 @@ -452,3 +614,4 @@ module_spi_driver(ad5791_driver); MODULE_AUTHOR("Michael Hennerich "); MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC"= ); MODULE_LICENSE("GPL v2"); +MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER"); --=20 2.43.0