From nobody Mon Nov 25 22:40:07 2024 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.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 551D31E7657 for ; Wed, 23 Oct 2024 20:59:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717162; cv=none; b=bL8ScJm/ydnf2CsegMw70qXpK5rjn3jPlhgE+OpJsyLhFq5kGR30OUbgHIdY2Y0lC6o7dtarueLoHD54rUcXJWkbwDbrRciw7Q00d3Rpq9FVY0pA6OvUvKUsaRFOGFVEwQ+Om+5fNuEHEjvYqcS/blXc5zFcj5sLG+fGvE8rEss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717162; c=relaxed/simple; bh=SlCysBgz0D/4+AADJGuHeqX6/WY754v+eDo0HNvP8C4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ifibcRYBqmy9ecxkoxpXdi0B0s/Au2iDhA3lMt/B6TylZB+WpcS7eZ4gXIJZwVmOo7XLLvJZN/m6pa+wtj+kgX5dZZG2Vzy+xUvPUwm3TjGgI/DIbHGq0BusBAFwEBfUpJbq/XM9c1fOVC/FUvukDFPADEnbcrbJy8F3Uhc8E+E= 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=QFhtkj+X; arc=none smtp.client-ip=209.85.210.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="QFhtkj+X" Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-71850708dc9so182369a34.0 for ; Wed, 23 Oct 2024 13:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717159; x=1730321959; 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=8TxSe+My0zxdEn+4gVzAIjVmZG39HxjUCg8p7C0cMnI=; b=QFhtkj+XE0Gjpjnt27RxaEKUTNJeOSO8aweYhrtcy91DlH8n+4JJhQZmNvAZX9oeDP LYpBr+laTTWn/vyjYZc7T3a72llddv5t3RrGHrouFWuPX2U1pbnTsHg3Wg0oHqEaHpHN LosEeO16As6uDIb1YzhM8Ngc1zWWULHSv7PdaaixCUSrjUtcReerInLdVoZl0artcc4A Z6UxrgINOTcMaOCG8VhekcKxOBYxv9W30gZeasqfhOTuNznlkdiYROzlVWpRgfAADtY1 Yw98PITB3WtaYkH7YKr3HAhSRWBvqcyulKsbJMjfHHnExkwbofEpfSWPDi9S7RmN2baQ 3YRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717159; x=1730321959; 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=8TxSe+My0zxdEn+4gVzAIjVmZG39HxjUCg8p7C0cMnI=; b=XrktKM7QVvJ9vUEHzd2VmD+i82WO66TlXyCDJdJBYdOiv7bx7UP75y8JCRAiBXFuAW F9u6eWEylqxpe/zZ2hZS2wwbv1706eQejETSIolrqB/v6Z3+RaKo8L1HbbSbQ+JjTbwU 2GbT+Mlpt8jxnN2dJcSkttipb+qEzaRq4ZqnEFxKyw6NsRacHlaRdueOg7vSWlquLC79 am3uRoEhUwancUh0DF7yo/oc0ckWY0hR+HXO1sCr1PJH410NajGZeaIkD8GwPQ6jrPg2 L9SXJZK+Y9OHbWOVeMo8011pC+UY6OIWbTNB86ZwUdevIKGwOaQWHjnFVVTTh1N8NYYk h0ZQ== X-Forwarded-Encrypted: i=1; AJvYcCU5C+Po1RvN8xEyt6uWI5NoDqpRrmAIMT/ZBgUKocyehW4CaehHXnmnC4Q+wP5Pjk5CrOmjX/HpU0bEXOk=@vger.kernel.org X-Gm-Message-State: AOJu0Ywc/qdrv1h7MjbS4IpcWtA0Xq5rnDSdbKOE55gSnAh+aJPqdhaN 3LyXKuenMSnOX9xWOAzBfKKoYp5RTHbfYPnhs+U2QGutWqCmsYTg7FigNajudteSglzcIUyRKOH C X-Google-Smtp-Source: AGHT+IHLXaj2jzh6xyZqcUUi3HAROcbZJkyHzk/8HYjkhquqvm+FQrkdOY7a4q7lSibR/k9/2Gq7iA== X-Received: by 2002:a05:6830:4981:b0:718:12af:7f01 with SMTP id 46e09a7af769-7184b2e6a7fmr5254614a34.13.1729717159246; Wed, 23 Oct 2024 13:59:19 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:17 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:08 -0500 Subject: [PATCH RFC v4 01/15] pwm: core: export pwm_get_state_hw() 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-1-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Export the pwm_get_state_hw() function. This is useful in cases where we want to know what the hardware is actually doing, rather than what what we requested it should do. Signed-off-by: David Lechner --- v4 changes: new patch in v4 And FYI for Uwe and Jonathan, there are a couple of other series introducing PWM conversion triggers that could make use of this so that the sampling_frequency attribute can return the actual rate rather than the requested rate. Already applied: https://lore.kernel.org/linux-iio/20241015-ad7606_add_iio_backend_support-v= 5-4-654faf1ae08c@baylibre.com/ Under review: https://lore.kernel.org/linux-iio/aea7f92b-3d12-4ced-b1c8-90bcf1d992d3@bayl= ibre.com/T/#m1377d5acd7e996acd1f59038bdd09f0742d3ac35 --- drivers/pwm/core.c | 55 +++++++++++++++++++++++++++++++++++++------------= ---- include/linux/pwm.h | 1 + 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c index 634be56e204b..a214d0165d09 100644 --- a/drivers/pwm/core.c +++ b/drivers/pwm/core.c @@ -718,7 +718,7 @@ int pwm_apply_atomic(struct pwm_device *pwm, const stru= ct pwm_state *state) } EXPORT_SYMBOL_GPL(pwm_apply_atomic); =20 -static int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *stat= e) +static int __pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *st= ate) { struct pwm_chip *chip =3D pwm->chip; const struct pwm_ops *ops =3D chip->ops; @@ -730,29 +730,50 @@ static int pwm_get_state_hw(struct pwm_device *pwm, s= truct pwm_state *state) =20 BUG_ON(WFHWSIZE < ops->sizeof_wfhw); =20 - scoped_guard(pwmchip, chip) { - - ret =3D __pwm_read_waveform(chip, pwm, &wfhw); - if (ret) - return ret; + ret =3D __pwm_read_waveform(chip, pwm, &wfhw); + if (ret) + return ret; =20 - ret =3D __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf); - if (ret) - return ret; - } + ret =3D __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf); + if (ret) + return ret; =20 pwm_wf2state(&wf, state); =20 } else if (ops->get_state) { - scoped_guard(pwmchip, chip) - ret =3D ops->get_state(chip, pwm, state); - + ret =3D ops->get_state(chip, pwm, state); trace_pwm_get(pwm, state, ret); } =20 return ret; } =20 +/** + * pwm_get_state_hw() - get the current PWM state from hardware + * @pwm: PWM device + * @state: state to fill with the current PWM state + * + * Similar to pwm_get_state() but reads the current PWM state from hardware + * instead of the requested state. + * + * Returns: 0 on success or a negative error code on failure. + * Context: May sleep. + */ +int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state) +{ + struct pwm_chip *chip =3D pwm->chip; + + might_sleep(); + + guard(pwmchip)(chip); + + if (!chip->operational) + return -ENODEV; + + return __pwm_get_state_hw(pwm, state); +} +EXPORT_SYMBOL_GPL(pwm_get_state_hw); + /** * pwm_adjust_config() - adjust the current PWM config to the PWM arguments * @pwm: PWM device @@ -906,9 +927,11 @@ static int pwm_device_request(struct pwm_device *pwm, = const char *label) */ struct pwm_state state =3D { 0, }; =20 - err =3D pwm_get_state_hw(pwm, &state); - if (!err) - pwm->state =3D state; + scoped_guard(pwmchip, chip) { + err =3D __pwm_get_state_hw(pwm, &state); + if (!err) + pwm->state =3D state; + } =20 if (IS_ENABLED(CONFIG_PWM_DEBUG)) pwm->last =3D pwm->state; diff --git a/include/linux/pwm.h b/include/linux/pwm.h index f1cb1e5b0a36..5bcbcf2911c3 100644 --- a/include/linux/pwm.h +++ b/include/linux/pwm.h @@ -370,6 +370,7 @@ int pwm_get_waveform_might_sleep(struct pwm_device *pwm= , struct pwm_waveform *wf int pwm_set_waveform_might_sleep(struct pwm_device *pwm, const struct pwm_= waveform *wf, bool exact); int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *= state); int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state= ); +int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state); int pwm_adjust_config(struct pwm_device *pwm); =20 /** --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 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 4B3731EF956 for ; Wed, 23 Oct 2024 20:59:22 +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=1729717164; cv=none; b=C6if471qDcusCi6whYRxbOM3I52VFasDG283dm3I5xIzEHYaFhH8NTcG8QpNoek7gWAWRdoCitIgHmO7uCC93PhcgdoaYO5vZM2Rl76GO5qIzlFOkz2fRAxEyLtt/negPLVeDIWQ7ERzoGCkj9UQISPqsier1ZpxRD2dAbvsZXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717164; c=relaxed/simple; bh=HKTElFd7OXujlNdM4D8yqgrjJL9W/OPjIGHrOikRQG8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GZLgOlqTuIlm7dCKWXOF0n2mXrf0cmzAbUXp4yYxqG6ZErUQK2asUr5jFQo9gnD4JViGp4iVLfGtEHnS8guhO8umACLQ2rvGZAZ8bltejHH+FapmJLfNIAewFRSPDSYE+9pRaKTNIsFYQtF6xy9UYjqz1zOGAfgC+bq9Of6gqwI= 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=zR+XJDM1; 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="zR+XJDM1" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5ebc0c05e25so106549eaf.3 for ; Wed, 23 Oct 2024 13:59:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717161; x=1730321961; 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=mXqLesX8rnPnZthafDRHkFzQvyeLIHy3ztKqxTjOUDs=; b=zR+XJDM1aD0zPf4lTxzOO+KnIPu28dGhi6yxt3hwhQTm/SVfCJTrVoIi+6VEXaukFe 2bt7cFuwmPIFnWAZee7mCrkPQmZlf9ZSzSfeLSk25CRpAXSMABW8A6dU2SuAVg96sx0h 9cumBLcBcZhMqAazbE/sNmIkqOlAYcyfrdMyipppu2kA/bFVsinJpsROf2iXF/kv9L2H 77KxgQ8xLPMCd7uEYTuoDDBfzZS5tDoHNSevE5gn+lGkWIBLOzthroRXtEFVG7cvSpDl UAZVsha4ASZX3A9jjBBQ2GangIMUyWvDI1oKBn/In4H8gdZQA41PM2YnETrjin2A+8dk N/Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717161; x=1730321961; 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=mXqLesX8rnPnZthafDRHkFzQvyeLIHy3ztKqxTjOUDs=; b=DtSAO+XI6VU11MknCBP+TwBrOXJ+8qtM43QKknjhVfCb4creyZjqkN7fDVg0wR7hkB iY3TwKW4JKpJMhv8o/lOBxXhMYGgNSyokkF2d1JRR3icjHzkX4OMXQXFCngSCK7ysAZA 4lw05elCoMiIipvBLmi6VQoA/kd75mwuYWutx2ucOfhYiuzNVMI370wImrbcYT6HUCGm 0PRCQOtPzn6+3XIx5b0BM8fC6oW4j5agS78MMucfd+w94ZRoH165e2KqbNlvqPjCTUO/ Eahpm+457rGFuc7frFATIDQADt4e4jzm8i3hUebUoPhH/d7IwQ9H0g0KHFuoD2DNjzLD I4eA== X-Forwarded-Encrypted: i=1; AJvYcCU6RLrGk/fjfR+8XRHW3auiy6a9YGOS//9+21ZHRMoa61jDyj5s3c82w6aM4Z+QXBjdP2S+mxrJQrLWAKk=@vger.kernel.org X-Gm-Message-State: AOJu0YyKJwjlA/TXhtBv1LDdvWLFmptQYPJ30y7LYY6sRixLFfYB2pk0 El5jxIA42N2NqaqK2GMoxGKT3E8bt9TrYkyczgJvR3QdsNPLIgZ9Gtm0smK6L2g= X-Google-Smtp-Source: AGHT+IHAy/NA0bFNSLRqnV7yJBc3WmP+MzRNe6q3hvfdyUYztm7KyWhryeO4CowXGGIIBPOMMON0Tg== X-Received: by 2002:a05:6820:260d:b0:5eb:b292:bb85 with SMTP id 006d021491bc7-5ebee8e315dmr2260773eaf.6.1729717161294; Wed, 23 Oct 2024 13:59:21 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:20 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:09 -0500 Subject: [PATCH RFC v4 02/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-2-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 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 callback 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 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. Signed-off-by: David Lechner --- 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. --- drivers/spi/Kconfig | 3 ++ drivers/spi/Makefile | 1 + drivers/spi/spi-offload.c | 104 ++++++++++++++++++++++++++++++++++++= ++++ include/linux/spi/spi-offload.h | 64 +++++++++++++++++++++++++ include/linux/spi/spi.h | 16 +++++++ 5 files changed, 188 insertions(+) diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 823797217404..d65074b85f62 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 a9b1bc259b68..6a470eb475a2 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 000000000000..c344cbf50bdb --- /dev/null +++ b/drivers/spi/spi-offload.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024 Analog Devices Inc. + * Copyright (C) 2024 BayLibre, SAS + */ + +#define DEFAULT_SYMBOL_NAMESPACE SPI_OFFLOAD + +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * devm_spi_offload_alloc() - Allocate offload instances + * @dev: Device for devm purposes + * @num_offloads: Number of offloads to allocate + * @priv_size: Size of private data to allocate for each offload + * + * Offload providers should use this to allocate offload instances. + * + * Return: Pointer to array of offloads or error on failure. + */ +struct spi_offload *devm_spi_offload_alloc(struct device *dev, + size_t num_offloads, + size_t priv_size) +{ + struct spi_offload *offloads; + void *privs; + size_t i; + + offloads =3D devm_kcalloc(dev, num_offloads, sizeof(*offloads) + priv_siz= e, + GFP_KERNEL); + if (!offloads) + return ERR_PTR(-ENOMEM); + + privs =3D (void *)(offloads + num_offloads); + + for (i =3D 0; i < num_offloads; i++) { + struct spi_offload *offload =3D offloads + i; + void *priv =3D privs + i * priv_size; + + offload->provider_dev =3D dev; + offload->priv =3D priv; + } + + return offloads; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_alloc); + +static void spi_offload_put(void *data) +{ + struct spi_offload *offload =3D data; + + offload->spi =3D NULL; + put_device(offload->provider_dev); +} + +/** + * 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_offload *offload; + int ret; + + if (!spi || !config) + return ERR_PTR(-EINVAL); + + if (!spi->controller->get_offload) + return ERR_PTR(-ENODEV); + + offload =3D spi->controller->get_offload(spi, config); + if (IS_ERR(offload)) + return offload; + + if (offload->spi) + return ERR_PTR(-EBUSY); + + offload->spi =3D spi; + get_device(offload->provider_dev); + + ret =3D devm_add_action_or_reset(dev, spi_offload_put, offload); + if (ret) + return ERR_PTR(ret); + + return offload; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_get); diff --git a/include/linux/spi/spi-offload.h b/include/linux/spi/spi-offloa= d.h new file mode 100644 index 000000000000..92a557533b83 --- /dev/null +++ b/include/linux/spi/spi-offload.h @@ -0,0 +1,64 @@ +/* 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. + */ + +#ifndef __LINUX_SPI_OFFLOAD_H +#define __LINUX_SPI_OFFLOAD_H + +#include + +MODULE_IMPORT_NS(SPI_OFFLOAD); + +struct device; +struct spi_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; + /** @spi: SPI device that is currently using the offload */ + struct spi_device *spi; + /** @priv: provider driver private data */ + void *priv; +}; + +struct spi_offload *devm_spi_offload_alloc(struct device *dev, + size_t num_offloads, + size_t priv_size); +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_H */ diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 8497f4747e24..c230d6a209ee 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -31,6 +31,9 @@ struct spi_transfer; struct spi_controller_mem_ops; struct spi_controller_mem_caps; struct spi_message; +struct spi_controller_offload_ops; +struct spi_offload; +struct spi_offload_config; =20 /* * INTERFACES between SPI master-side drivers and SPI slave protocol handl= ers, @@ -496,6 +499,9 @@ extern struct spi_device *spi_new_ancillary_device(stru= ct 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. * @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,9 @@ 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); + /* GPIO chip select */ struct gpio_desc **cs_gpiods; bool use_gpio_descriptors; @@ -1108,6 +1117,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 +1178,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 Mon Nov 25 22:40:07 2024 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 790171FAF15 for ; Wed, 23 Oct 2024 20:59:25 +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=1729717167; cv=none; b=nfn96UUORJUZl7RdIpiaVup8Z5iDEqYaoiykHUj0TrRXf/IWODSigCeXW0gtWW8spjgYI6JyS9O1zeNZ2uBQPutpnYIJbzrficwZ0G/MfSgb3jvIPqB0jBM6JTeR06lzakakch+ZuIt1EeVhEMD0ALNQEkHTBtdCAMFzdNojaIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717167; c=relaxed/simple; bh=p6qF5mGDsz6vMJiS+LL9qxqMe04AH3Kc8N5y/6sHtUo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VdjU76vFslFaiA9clA5HQQOs8IaOSWwI5/Yt5ou0CheB9F97eaaxXSCGCYmAXNe0nYy6btsKNU5UDPhdqbPNUntOx7BcXpNt/86lkQjreYPt6Gv9WPToWfuTuwNltURfCECwejlPSgIKuzilMDSHSwYBavI/teOlrc1hWvwmfK0= 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=UxcUNV36; 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="UxcUNV36" Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5eb73ec1e1aso101053eaf.2 for ; Wed, 23 Oct 2024 13:59:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717164; x=1730321964; 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=Cyi99RQueV0J22D6L8nca1luUo2oo0lC/4Jl1o/vrjM=; b=UxcUNV36pyzy9hq5Y3lbs1bPQxYJUGGnxJ3+7Azo6dR1zGb4i0eyxNgOmzGnoyl8MN NWiWqCN4wPJwFmAdvL4A3tB+U3PaC0ZIcfM7cEXeeMfdFOJzUN6eh4gP0Q243yS88/V6 KGQ2eHYjzgA9bPSZyV0yB2IVoehk6YRmFshQ5lwCRgLdtQzz/a8ri0gLXh6mr4LsLggw hqmWcOkO80q82zo+OSNeXuC6JsIKYWbHuWapaBntCI7ZMZgIEUcpyiEdn589yysRqjBW 7zBzCPnLjryjXdWq2lAfrrA4oVt6AePnu3iMtzdSTs3J6ot4OT98JKAMVJJKqJrQaiAT THyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717164; x=1730321964; 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=Cyi99RQueV0J22D6L8nca1luUo2oo0lC/4Jl1o/vrjM=; b=rkV8z8pOZm7BCrXjDDRxk0xhQKrEJLMLV86HHu7X+rzizLJWNPrNk5HBWuH1CK5LQ1 qlxCvVI68bpaDDaZLM0h/tzZtr6IiqkeyqWFIejPM8XWMbXAMzHQsmeZQB/qnIBFqtMr s7DGnA9QSQioxAcfOlmyfWSqnMhsx5bFDBlmU+A9AqaXpC25rsxKa+3wzw4T8Lc2uPuF 4WFrNeupk5kjTHv6eIc9p1XAHS8MomZsKjmWmgAUnJJwty5FG2GaGH3YdvDvSzmQEnNY J9E40bFpqDmywjuRI1cr+MgRwuPN7No+LxC1x/JAiV9BlhHCZhcgyQA0f5npcO0sH5bA xoPw== X-Forwarded-Encrypted: i=1; AJvYcCWGl73CYMNhejje0Ob6BmFL075TfScD/6Oo4n12pffIBezlS0rc8F9tgegPDiwoH/lz9O5T/NkcY+lauAU=@vger.kernel.org X-Gm-Message-State: AOJu0YzREGaqxoGzpas1O8ikyuIbiQNA1EaDkk+V4jN6+goNmXPRGIKa QSskTpr3pwFnyCcauMGaJGoH9vP0ZEa/yl/QPwS/DSrONmYzhXCkZ4RyBpGoBPs= X-Google-Smtp-Source: AGHT+IH/EsQ0tknTvoN94FNxLmCCF0hi6zC7K1hBIZR7SFC/n9cF9xbVrfJFGJqW7iPxRqs2uhP34g== X-Received: by 2002:a05:6820:808:b0:5eb:b282:5351 with SMTP id 006d021491bc7-5ebee4a0773mr3208875eaf.2.1729717164425; Wed, 23 Oct 2024 13:59:24 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:23 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:10 -0500 Subject: [PATCH RFC v4 03/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-3-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 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. Signed-off-by: David Lechner --- In previous versions, we locked the SPI bus when the hardware trigger was enabled, but we found this to be too restrictive. In one use case, to avoid a race condition, we need to enable the SPI offload via a hardware trigger, then write a SPI message to the peripheral to place it into a mode that will generate the trigger. If we did it the other way around, we could miss the first trigger. Another likely use case will be enabling two offloads/triggers at one time on the same device, e.g. a read trigger and a write trigger. So the exclusive bus lock for a single trigger would be too restrictive in this case too. So for now, I'm going with Nuno's suggestion to leave any locking up to the individual controller driver. If we do find we need something more generic in the future, we could add a new spi_bus_lock_exclusive() API that causes spi_bus_lock() to fail instead of waiting and add "locked" versions of trigger enable functions. This would allow a peripheral to claim exclusive use of the bus indefinitely while still being able to do any SPI messaging that it needs. 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 | 266 ++++++++++++++++++++++++++++++++++++= ++++ include/linux/spi/spi-offload.h | 78 ++++++++++++ 2 files changed, 344 insertions(+) diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c index c344cbf50bdb..2a1f9587f27a 100644 --- a/drivers/spi/spi-offload.c +++ b/drivers/spi/spi-offload.c @@ -9,12 +9,26 @@ #include #include #include +#include #include +#include #include #include #include #include =20 +struct spi_offload_trigger { + struct list_head list; + struct device dev; + /* synchronizes calling ops and driver registration */ + struct mutex lock; + 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 instances * @dev: Device for devm purposes @@ -102,3 +116,255 @@ struct spi_offload *devm_spi_offload_get(struct devic= e *dev, return offload; } EXPORT_SYMBOL_GPL(devm_spi_offload_get); + +static void spi_offload_trigger_release(void *data) +{ + struct spi_offload_trigger *trigger =3D data; + + guard(mutex)(&trigger->lock); + if (trigger->priv && trigger->ops->release) + trigger->ops->release(trigger->priv); + + put_device(&trigger->dev); +} + +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; + bool match =3D false; + 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); + + struct fwnode_handle *trigger_fwnode __free(fwnode_handle) =3D args.fwnod= e; + + guard(mutex)(&spi_offload_triggers_lock); + + list_for_each_entry(trigger, &spi_offload_triggers, list) { + if (trigger->dev.fwnode !=3D args.fwnode) + continue; + + match =3D trigger->ops->match(trigger->priv, type, args.args, args.nargs= ); + if (match) + break; + } + + if (!match) + return ERR_PTR(-EPROBE_DEFER); + + guard(mutex)(&trigger->lock); + + if (!trigger->priv) + return ERR_PTR(-ENODEV); + + if (trigger->ops->request) { + ret =3D trigger->ops->request(trigger->priv, type, args.args, args.nargs= ); + if (ret) + return ERR_PTR(ret); + } + + get_device(&trigger->dev); + + ret =3D devm_add_action_or_reset(dev, spi_offload_trigger_release, trigge= r); + 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->priv) + return -ENODEV; + + if (!trigger->ops->validate) + return -EOPNOTSUPP; + + return trigger->ops->validate(trigger->priv, config); +} +EXPORT_SYMBOL_GPL(spi_offload_trigger_validate); + +/** + * spi_offload_trigger_enable - enables trigger for offload + * @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->priv) + return -ENODEV; + + if (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->priv, 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 + * + * 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->trigger_disable) + offload->ops->trigger_disable(offload); + + guard(mutex)(&trigger->lock); + + if (!trigger->priv) + return; + + if (trigger->ops->disable) + trigger->ops->disable(trigger->priv); +} +EXPORT_SYMBOL_GPL(spi_offload_trigger_disable); + +/* Triggers providers */ + +static void spi_offload_trigger_dev_release(struct device *dev) +{ + struct spi_offload_trigger *trigger =3D + container_of(dev, struct spi_offload_trigger, dev); + + mutex_destroy(&trigger->lock); + of_node_put(trigger->dev.of_node); + kfree(trigger); +} + +static void spi_offload_trigger_put(void *data) +{ + struct spi_offload_trigger *trigger =3D data; + + put_device(&trigger->dev); +} + +struct spi_offload_trigger +*devm_spi_offload_trigger_alloc(struct device *dev, + struct spi_offload_trigger_info *info) +{ + struct spi_offload_trigger *trigger; + int ret; + + trigger =3D kzalloc(sizeof(*trigger), GFP_KERNEL); + if (!trigger) + return ERR_PTR(-ENOMEM); + + device_initialize(&trigger->dev); + trigger->dev.parent =3D info->parent; + trigger->dev.fwnode =3D info->fwnode; + trigger->dev.of_node =3D of_node_get(to_of_node(trigger->dev.fwnode)); + trigger->dev.of_node_reused =3D true; + trigger->dev.release =3D spi_offload_trigger_dev_release; + + mutex_init(&trigger->lock); + trigger->ops =3D info->ops; + + ret =3D devm_add_action_or_reset(dev, spi_offload_trigger_put, trigger); + if (ret) + return ERR_PTR(ret); + + ret =3D dev_set_name(&trigger->dev, "%s-%d", info->name, info->id); + if (ret) + return ERR_PTR(ret); + + return trigger; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_trigger_alloc); + +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); + + guard(mutex)(&trigger->lock); + trigger->priv =3D NULL; + device_del(&trigger->dev); +} + +int devm_spi_offload_trigger_register(struct device *dev, + struct spi_offload_trigger *trigger, + void *priv) +{ + int ret; + + ret =3D device_add(&trigger->dev); + if (ret) + return ret; + + trigger->priv =3D priv; + + guard(mutex)(&spi_offload_triggers_lock); + list_add_tail(&trigger->list, &spi_offload_triggers); + + ret =3D devm_add_action_or_reset(dev, spi_offload_trigger_unregister, tri= gger); + if (ret) + return ret; + + return 0; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_trigger_register); diff --git a/include/linux/spi/spi-offload.h b/include/linux/spi/spi-offloa= d.h index 92a557533b83..561cc1fb6f35 100644 --- a/include/linux/spi/spi-offload.h +++ b/include/linux/spi/spi-offload.h @@ -22,6 +22,7 @@ MODULE_IMPORT_NS(SPI_OFFLOAD); =20 struct device; +struct fwnode_handle; struct spi_device; =20 /* Offload can be triggered by external hardware event. */ @@ -53,6 +54,43 @@ struct spi_offload { struct spi_device *spi; /** @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 struct spi_offload *devm_spi_offload_alloc(struct device *dev, @@ -61,4 +99,44 @@ struct spi_offload *devm_spi_offload_alloc(struct device= *dev, 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); + +/* Trigger providers */ + +struct spi_offload_trigger; + +struct spi_offload_trigger_ops { + bool (*match)(void *priv, enum spi_offload_trigger_type type, u64 *args, = u32 nargs); + int (*request)(void *priv, enum spi_offload_trigger_type type, u64 *args,= u32 nargs); + void (*release)(void *priv); + int (*validate)(void *priv, struct spi_offload_trigger_config *config); + int (*enable)(void *priv, struct spi_offload_trigger_config *config); + void (*disable)(void *priv); +}; + +struct spi_offload_trigger_info { + const char *name; + int id; + struct device *parent; + struct fwnode_handle *fwnode; + const struct spi_offload_trigger_ops *ops; +}; + +struct spi_offload_trigger +*devm_spi_offload_trigger_alloc(struct device *dev, + struct spi_offload_trigger_info *info); +int devm_spi_offload_trigger_register(struct device *dev, + struct spi_offload_trigger *trigger, + void *priv); + #endif /* __LINUX_SPI_OFFLOAD_H */ --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 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 60CE31FDFA3 for ; Wed, 23 Oct 2024 20:59:28 +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=1729717171; cv=none; b=XOs44eJuB8OaYJFH17UNOp8sW/rhJJ5dl71hZCd366eh1j+yxaOnIa6L2V+oDIYubKDlMtUQWfA1X4sHVqNbkodRX2nOj6siLHaV05mn++oJjWDN2qT6XA3DvJ7x018tOQUGnAT5PVXRTT2Y9QirzaVkhrl3+0hzuSkhzI1xPR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717171; c=relaxed/simple; bh=/mOovntT5Qe8sUMdAxO+EkWWA3dOzhaFJ1YaLc6k+Ak=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CkqkieF8xHiC0zL+deXGI/qVsQ1VaBFwZgLJ824KVEPc9b0d5am0JU/ArNB8l510wNU7TXxAx7IQtZkoZAHtoTywBNqI6l8mTrp1ypK7YFwMaAxxX+Y6Aq/SJ5Dzp07EoybVjO0rpw4AvXkgpjgHVHdiShals7zHAZrfz3nAkbI= 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=nCjgFh8k; 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="nCjgFh8k" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5ebc1af9137so106644eaf.2 for ; Wed, 23 Oct 2024 13:59:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717167; x=1730321967; 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=EyHtfkBXgcKy8Q9+oyQnrUM8I5ww1iitltHtg1jsJVA=; b=nCjgFh8kEL6BDxn42ubkAI+3BVP6ITgTB74JlBrDSsvNIrCLRuXiosxTpPKOYxq3vc lN21UZ6zLO3SPSpTgLTxYtrwxy1Cpv1Ak1sRCjVWTzFDONwB8AkwFcWhWWF6vK+DJZJs YubpURtheHWzm8ah+nekFdi2G3ttHHFYsYjgdeiPSxZ15TeJR8s3fe69bZolj18aRiNY /WPpC+hIYy4ZwSb/4FeKf6tZ+zIDRe71/gBcZiqKpBeRXtkz9pBj7gj/pQHQz7qtWtli JWQp3hNPYl0ZFboW0bf8WtAtuIohe8kNzqo+91wNv63fyH0CzRt9YueRj+Xirp2oT7pL tI4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717167; x=1730321967; 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=EyHtfkBXgcKy8Q9+oyQnrUM8I5ww1iitltHtg1jsJVA=; b=wxo6yQldliOoJYOMu0CtspvFELdz4JjBfKP5GddWyN4uL9D+/Z3F0ftil92GtZbgsV 3OL1GUbNpIwjmTyVhWymbypU1Le86pIw6AptkX+aUvJuiiigWla0ran+Sn5cdgznkJiP mXbyUE0e2HUwvd4vVBqJ5/9ouo+vqZ6xmbRPUna0TmPHLDJbe6+yt0+KJ803N43IrK24 y6Bf033mL4uVELQ2j6YbquzlLDQdoqZ8ecL9mQwuN0ErwJ1kka4ieF/q4bQ9JT42Cjqw Gniynget90f2UFglc/yNJoB46uy51+F8O01MSZXmzeMZsVHHfHoPqumVRZ75b8UQRWQ4 uWXg== X-Forwarded-Encrypted: i=1; AJvYcCXIQNrGSjjL2Va797NBOYBb+Jh8kej1ZV5lcAVvN7TOOSLr40p74/mYMtL7e202WTPS5YClsNUY3Nzab7M=@vger.kernel.org X-Gm-Message-State: AOJu0Yxi6ga2/s/UvDLcAHTzZt52D3PcE7x+PdvrH8KbgnOI3vqUx6im CL4alh0pOd+zeh7apX59oDYl/vAHWn3Fv8w8R6jB+UuZ51xdYLpITiB1NOROl2w= X-Google-Smtp-Source: AGHT+IHv8HCEhfnp0g05EVhJyJSYYG556S9c4IigSs3Y2hbEvK4bBM3BCxpVknjvtRLUcm1QtQH0QA== X-Received: by 2002:a05:6820:60e:b0:5e7:caf5:ae1f with SMTP id 006d021491bc7-5ebee532b5emr3206665eaf.2.1729717167480; Wed, 23 Oct 2024 13:59:27 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:26 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:11 -0500 Subject: [PATCH RFC v4 04/15] spi: dt-bindings: add trigger-source.yaml 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-4-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Add a new binding for SPI offload trigger sources. Signed-off-by: David Lechner --- v4 changes: new patch in v4. FWIW, this is essentially identical to the leds trigger-source binding. --- .../devicetree/bindings/spi/trigger-source.yaml | 28 ++++++++++++++++++= ++++ 1 file changed, 28 insertions(+) diff --git a/Documentation/devicetree/bindings/spi/trigger-source.yaml b/Do= cumentation/devicetree/bindings/spi/trigger-source.yaml new file mode 100644 index 000000000000..d64367726af2 --- /dev/null +++ b/Documentation/devicetree/bindings/spi/trigger-source.yaml @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/spi/trigger-source.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Trigger source providers for SPI offloads + +maintainers: + - David Lechner + +description: + Each trigger source provider should be represented by a device tree node= . It + may be e.g. a SPI peripheral chip or a clock source. + +properties: + '#trigger-source-cells': + description: + Number of cells in a source trigger. Typically 0 for nodes of simple + trigger sources. For nodes with more than one output signal, the fir= st + cell be used to specify which output signal to use. If the same sign= al is + available on more than one pin, the second cell can be used to speci= fy + which pin to use. + enum: [ 0, 1, 2 ] + +additionalProperties: true + +... --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.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 8107E1FEFBA for ; Wed, 23 Oct 2024 20:59:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717173; cv=none; b=VbAO8fyXSjaQeHV5168Pdbkhdudwbo4hneJOnedgnJqjLC0C1Y6XgsIijCAPVMnv6Cx65FL21vyOWJ4RTNXV8rwu16zkwzHqvzxhKPEri9m4VkUgVrzXdO1q5GuOazyu5U2OZ5Jog9qpHIRDCUIc5E/7TXtbo4Vp38JvUr6IJQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717173; c=relaxed/simple; bh=EhwFAySOu0UzQT81pSGmKbzn70uQw3/Gq0l7793qgAA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TwdvxDSzdzRcpzrNjn3dHBl37RG5lFvjYyV8C5Tfi2L1SZjL0rTb8eZY/eBWXV12M+3ZxF5R17iYGH7dxo0ucDGwRQt+/gIi/DUu9s5aqTR54MLqEHfZTTvTJxM6VojS8ieJtMtmaKzzJgnmH71Rib8B0lHIJaPsiOQGDciOlqY= 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=mCa42Cax; arc=none smtp.client-ip=209.85.161.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="mCa42Cax" Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-5eb70a779baso108269eaf.1 for ; Wed, 23 Oct 2024 13:59:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717170; x=1730321970; 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=qbZ0wRS+DoaTd3bUw74mY2hSGs8UvSjvOOeAQWDWam8=; b=mCa42CaxdB1jAlc44sM720Fjss7NuL1jctnbuU1XENxgsPCRNeP9htWuFtDrgf1sZp ZfVbfMrrcn7g+JBm1kw+hN1e2/jKxCLO7mbe/KcxLtjkS5K+gmS/FkP6iuycQBV/nD6P aYT7ScVPpGxO5OmBuxP8rxJP0QILL7/Stcfc8XhU4oOEEeVtU/2iOWzcBXayX52yykjZ MdMBtK4Rradwl4oaFnNnKCiggZfk2OHW/8rTctM9nfl/HFBESDLaVlkaGjIBlFDrSXe3 LlqD8zcnK3ZObwEi+PXzE093SbxJ6+kbz5R+BgWI5ZGoFAnsOhs6dVxtwgnN/QLYV9KW hM6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717170; x=1730321970; 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=qbZ0wRS+DoaTd3bUw74mY2hSGs8UvSjvOOeAQWDWam8=; b=WAf66ncD3YdAceXdZMX1e7eK3kUNVdIb8kuaDr5QSr69rwWBT0Ybcr/oM7ddqfVA6m ubS2o/EQHydKzX4Im/qWLJOkZ1N7mam85MY2Sw0I6jhXToTVfzHMeuvLbbm5a5EeYd2Q in6voaLeJFKWsYxLOXIrGLCdALeIzquaB7tb42n81Y30bLa58WJL4mzMzmS7SyzvAmwJ rickEwqH65lHTzKeCdg/RyJIJK1As30sR3ANACA1Mh+QUCfxYn2/egdPVvnvNfXr0qE5 4B6j6ap4l9J1FiU9YHvMqazaBdNRh1kePbQTbG4ATazFgwUC09KhXh78xnGKMKIXBYM5 779g== X-Forwarded-Encrypted: i=1; AJvYcCV7zXiQV3y/enQwPUjUmqVcYjr313+xfu6oKMmt9gdqhYg7trFYaDyfsBpyGsNNfpNNGmCRUV3yajW+RKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxHi7QwxlOUhcKkTClSszVkEkbddR4mquMZAJIz49s8WKPV/tHa NK+TYnvEatOg2GXHHw1fdXjVRR4HKbqZxwkzs1XkQdERi4nc6hp7c5Yzw5Ov6Xzz2R5OEZCsR6y q X-Google-Smtp-Source: AGHT+IGpSU6q9EbnjtFTPIfmjpti+RDQ5TmJq7nEwzLoenZew8VbFJCDXbbHeqPv7bl5G4IcllcUxQ== X-Received: by 2002:a05:6820:229a:b0:5eb:c72e:e29c with SMTP id 006d021491bc7-5ebee94cdd8mr3110098eaf.8.1729717170663; Wed, 23 Oct 2024 13:59:30 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:29 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:12 -0500 Subject: [PATCH RFC v4 05/15] spi: dt-bindings: add PWM SPI offload trigger 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-5-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Add a new binding for using a PWM signal as a trigger for SPI offloads. Signed-off-by: David Lechner --- v4 changes: new patch in v4 --- .../devicetree/bindings/spi/trigger-pwm.yaml | 39 ++++++++++++++++++= ++++ 1 file changed, 39 insertions(+) diff --git a/Documentation/devicetree/bindings/spi/trigger-pwm.yaml b/Docum= entation/devicetree/bindings/spi/trigger-pwm.yaml new file mode 100644 index 000000000000..987638aa4732 --- /dev/null +++ b/Documentation/devicetree/bindings/spi/trigger-pwm.yaml @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/spi/trigger-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Generic SPI offload trigger using PWM + +description: Remaps a PWM channel as a trigger source. + +maintainers: + - David Lechner + +$ref: /schemas/spi/trigger-source.yaml# + +properties: + compatible: + const: trigger-pwm + + '#trigger-source-cells': + const: 0 + + pwms: + maxItems: 1 + +required: + - compatible + - '#trigger-source-cells' + - pwms + +additionalProperties: false + +examples: + - | + trigger { + compatible =3D "trigger-pwm"; + #trigger-source-cells =3D <0>; + pwms =3D <&pwm 0 1000000 0>; + }; --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 Received: from mail-oo1-f51.google.com (mail-oo1-f51.google.com [209.85.161.51]) (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 B47F61FF7AD for ; Wed, 23 Oct 2024 20:59:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717176; cv=none; b=ocy7fVrSWwAi+PhIUmYgYAA8YHbqcf9TV7ZwCZJRTKGZ9l40OxNgL/DQYUz+N14ONlqkdm6ydkUemg2linKoO571dKvw78lz37l14I29K0wGllEnddHqQOy5CXQFft2uCWfcCkmExttivnRKL+eipyE4Gprsjeog+pZ9HS1pLzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717176; c=relaxed/simple; bh=6LuqxODFa87GHKbqZm4iCQa79o+y+ZqqHBFoFKpHwas=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LMKYx/r9fC/iqDCWhhYCa3pPj5P5pMayedfe7RLnZck0oepIyAOMlHLBJUoy8pCnCW1wNcXVsL2Yf+Tq9z7dq4vM6cuSHt7bQOhmTpmpUuWFBUM5AzN8fs8XMSp51WskQrY6VPPRvk+HV02ayrMmn0Vi1j471IMqWL83rX4lhas= 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=1zHdq8Qa; arc=none smtp.client-ip=209.85.161.51 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="1zHdq8Qa" Received: by mail-oo1-f51.google.com with SMTP id 006d021491bc7-5ebc1af8e91so105632eaf.1 for ; Wed, 23 Oct 2024 13:59:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717174; x=1730321974; 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=+7eZRQmN+d1Xl5AiVFjBojgvbjgVdZFYfr/t0WGin1o=; b=1zHdq8Qad+iSEdtjTR/1OoiREQdoyujRSmpjrdX123UwpzNtIHd1bMObDJAZoO8SkV vmkhKhwsrYXS+ePVhghtMRdZzNtGzzCyWncpn139qwTZcYNNEOsqA+3xZT0H1mJtQgo8 Vli20+gpdzI/Q09oAudNHMTYCJ2KEF9vE2y1NmNJt7IPNWajnFhtWDe76SnwrSYGSRqP QZlOUxfpCWKw30wRGGIboTT0PfhR5IX9I4dxVBC+eud/l3Bsi7DsuXXQox2hMJG8PuJu kqGNyNhLnOrN6dFnlkypLrodKP+3HAvm4s4ZineXQBNE7vqJDoNUUWF+Qd8BnZWfdcH1 7Qjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717174; x=1730321974; 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=+7eZRQmN+d1Xl5AiVFjBojgvbjgVdZFYfr/t0WGin1o=; b=PBojwJwAta3/5FWDep01q0RhJxZIt8A5qkqWnspgIMtJ3FWUPQQl5cwJekJe1TdUtP qhL162hDPdw1gOEbZ8GJ2nwQ7QPmp1R1SiEJEDQj0xXs8/QM/NamT/mn14a4uSm9lwWh RUcCQ61CBe0gGvZMGEA4RhY4b1gu700KGkZoXfFz0BmXujL/ZnMER0leFF0Rv5vuAEzn CuwHJ2cYbk66MDxQn3HWQEN/ocHYIy3J7TR886vvL8l6WwLi6PgT1hE2ecaZo08+8LRd 2lhvfBRSonscBi+vRVJbyj/p70hBJLqZWlTRRA43Aeb95TFhwMRq01FANvqbYtnMissx nfVg== X-Forwarded-Encrypted: i=1; AJvYcCWjBpIDxf4Ni3OSAHSsAdolZFRzyg/idybcGbtUw0KlJByI32dKa+RJvFA9V4TZESzMoLWtSwPt5tAGWRI=@vger.kernel.org X-Gm-Message-State: AOJu0YzqgeQ3cNki3KPXYZi36Gp+yAb0f9e851r9IUPsCGkQKCjUYnG0 kk16q4X7Ln3qtd4rtlO38o4cL8QGXoGvtHj0Z9Pk25jEOfjLVrCmU13WmvJl8Q0= X-Google-Smtp-Source: AGHT+IGrGeUFNoZ4nfpg0y/vsYc+3tkphmXVxQMQc7RfHzm/YAo//LBkLU7KTfCeOuufF1v+fn0hcg== X-Received: by 2002:a05:6820:1688:b0:5eb:827b:9bbf with SMTP id 006d021491bc7-5ebee9726famr2547696eaf.7.1729717173815; Wed, 23 Oct 2024 13:59:33 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:32 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:13 -0500 Subject: [PATCH RFC v4 06/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-6-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Add a new driver for a generic PWM trigger for SPI offloads. Signed-off-by: David Lechner --- v4 changes: new patch in v4 --- drivers/spi/Kconfig | 12 +++ drivers/spi/Makefile | 3 + drivers/spi/spi-offload-trigger-pwm.c | 169 ++++++++++++++++++++++++++++++= ++++ 3 files changed, 184 insertions(+) diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index d65074b85f62..50d04fa317b7 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -1286,4 +1286,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 6a470eb475a2..3a76b9c61486 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -161,3 +161,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 000000000000..ffb0bf75cace --- /dev/null +++ b/drivers/spi/spi-offload-trigger-pwm.c @@ -0,0 +1,169 @@ +// 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(void *priv, + 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(void *priv, + struct spi_offload_trigger_config *config) +{ + struct spi_offload_trigger_pwm_state *st =3D priv; + 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(void *priv, + struct spi_offload_trigger_config *config) +{ + struct spi_offload_trigger_pwm_state *st =3D priv; + 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(void *priv) +{ + struct spi_offload_trigger_pwm_state *st =3D priv; + 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 { + .name =3D "trigger", + .id =3D 0, + .parent =3D dev, + .fwnode =3D dev_fwnode(dev), + .ops =3D &spi_offload_trigger_pwm_ops, + }; + struct spi_offload_trigger_pwm_state *st; + struct spi_offload_trigger *trigger; + struct pwm_state state; + int ret; + + st =3D devm_kzalloc(&pdev->dev, sizeof(*st), GFP_KERNEL); + if (!st) + return -ENOMEM; + + st->dev =3D dev; + + st->pwm =3D devm_pwm_get(&pdev->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; + + trigger =3D devm_spi_offload_trigger_alloc(dev, &info); + if (IS_ERR(trigger)) + return dev_err_probe(dev, PTR_ERR(trigger), "failed to allocate trigger\= n"); + + return devm_spi_offload_trigger_register(dev, trigger, st); +} + +static const struct of_device_id spi_offload_trigger_pwm_of_match_table[] = =3D { + { .compatible =3D "trigger-pwm" }, + { } +}; +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 "trigger-pwm", + .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 Mon Nov 25 22:40:07 2024 Received: from mail-oo1-f45.google.com (mail-oo1-f45.google.com [209.85.161.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 AF2A91FF7AE for ; Wed, 23 Oct 2024 20:59:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717179; cv=none; b=UtgozoHG6v3xxkNPBMqTOBtpVBSJloAzcqwc00N1A261pTkHqvbMoAZJKML+5zHCA0ZJuMsjwPGc4//XJ0jcoRQKfatLT0uDo6LoimPiVf04E0fLR75c3vTJT78JEofBfhynmShQfWxjzrQyS8B9BXkH//Cxd0t1otLu8qsxwPI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717179; c=relaxed/simple; bh=9Uoi/lsx5lDzmesX/dTqmB11w7iIVMU2MC+rAN+Xo0o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EcpZf07SJeEAOuFeI9hTvI40k93pp/wF+tbswKe3BmHUYy9+zjXPrHoMHTzITsbfGaHq8A6726eoMCV6KsSlg9JJ5SRRoHRzhoR8ecE0disS0oC660AjNbJzl7WlS43hno0eOtLj+hFO3N9/4ZHOVWD8MYB7+I2O+/NutGS7sfk= 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=hvptctuZ; arc=none smtp.client-ip=209.85.161.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="hvptctuZ" Received: by mail-oo1-f45.google.com with SMTP id 006d021491bc7-5eb67d926c4so139974eaf.0 for ; Wed, 23 Oct 2024 13:59:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717177; x=1730321977; 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=MJpBAkzSSYPV4RvQhjaw2/1DFd2kkl7hVgMR3+qjlgE=; b=hvptctuZQ5a5qjfBFSAo2mQX4Dubo1Q1glMKG2CfM3Y6QPqO8RZulJVrHr1fvWu7aj oqWvuydKGganITgmZoH+ssSdHVvszZ6xGtCQpAZ0P+tpoe/LlwvtR3zufBE6aat3GPAO vHoD3hCzUYLx1CfnlYZQKmKafxyFQC3ZfMiGQUkFVcWKxZZhGlfC5AP69LTa+bK0x3A4 HiyMmkZf/GDBWWnwAt33PvLKY0KaZ9ZF3K4U6WGU3Gqwzrh3jb2D4+ijjZuRL/nVNLtB ux77Pc2/7OS5VT9v6/WdY8j+Wno/8MJ2D+kicx8WkJts4iJQAf3EzsStXR4ErjqmVcot 1UkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717177; x=1730321977; 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=MJpBAkzSSYPV4RvQhjaw2/1DFd2kkl7hVgMR3+qjlgE=; b=uYfmYqWDgTnflT8L0Rztodc/PElWcNxUwitl6YdbznC2lWMA5iPzK8BqDUg45wT1Tf VOgw+RnvUP5ObYcx4aAfic+uFSi3/uezwkj93kZ2PaVi2lqcaDHfwMSxF9j5/MTFw9J8 +ogqac+qiDEYACt+4cuz5Nm1hkuglpcvlyJwfAQe/Dq+X7w/zsx0ENRKiij43w6A9/cs /icoZsm3Qwn4dL8IpBCvSW2nLNN4gfTCWJjcC8BI4Zg79BFuAwE15+OJ6Ou653/sTA5k mDuPRdQ+oKEgEFJ4bIBT5iF2/Qz4n8KOKeTWe0dbCPwq6H67FGVJtg1EJyhvJxbV5tOF pSEw== X-Forwarded-Encrypted: i=1; AJvYcCV0mToDf6H7+75AxTsOBCa9Wgl2ACRedJbl6Jg4jIj9c7HVSDt4SZB6xgM6Ucy0wcAr+dFHEESQ/CfGITs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx01tKBr2KtxzqB0pwtkyRUw3Ipf7y2JlWUoyouzU9wtPbNMHiv TMMj4ywtMzvaIEo+YIE/OUEw7KryWO4kg6wQthFwnyzXBVddmZ+ea5qLoAdtccI= X-Google-Smtp-Source: AGHT+IHhgz4z/pNzeGgfxd28esWvpBib5iH9I1o0u1Snz5WDz9JUClIdWIVkcf1KuO2G++R03tqq+Q== X-Received: by 2002:a05:6820:608:b0:5eb:c72e:e29f with SMTP id 006d021491bc7-5ebee0753aemr2275209eaf.1.1729717176874; Wed, 23 Oct 2024 13:59:36 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:35 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:14 -0500 Subject: [PATCH RFC v4 07/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-7-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 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. Signed-off-by: David Lechner --- 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 | 76 +++++++++++++++++++++++++++++++++++++= ++++ drivers/spi/spi.c | 10 ++++++ include/linux/spi/spi-offload.h | 24 +++++++++++++ include/linux/spi/spi.h | 3 ++ 4 files changed, 113 insertions(+) diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c index 2a1f9587f27a..dd4cb3c2e985 100644 --- a/drivers/spi/spi-offload.c +++ b/drivers/spi/spi-offload.c @@ -8,6 +8,7 @@ =20 #include #include +#include #include #include #include @@ -282,6 +283,81 @@ 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); +} + +/** + * spi_offload_tx_stream_request_dma_chan_info - Get the DMA channel info = for the TX stream + * @spi: SPI device + * @id: Function ID if SPI device uses more than one offload or NULL. + * + * This is the DMA channel that will provide data to transfers that use the + * %SPI_OFFLOAD_XFER_TX_STREAM offload flag. + * + * The caller is responsible for calling spi_offload_free_dma_chan_info() = on the + * returned pointer. + * + * 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); + +/** + * spi_offload_rx_stream_request_dma_chan_info - Get the DMA channel info = for the RX stream + * @spi: SPI device + * @id: Function ID if SPI device uses more than one offload or NULL. + * + * This is the DMA channel that will receive data from transfers that use = the + * %SPI_OFFLOAD_XFER_RX_STREAM offload flag. + * + * The caller is responsible for calling spi_offload_free_dma_chan_info() = on the + * returned pointer. + * + * 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_dev_release(struct device *dev) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 7c5e76b15421..cb6184c0ae03 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -4159,6 +4160,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/spi-offload.h b/include/linux/spi/spi-offloa= d.h index 561cc1fb6f35..5d0e29b25977 100644 --- a/include/linux/spi/spi-offload.h +++ b/include/linux/spi/spi-offload.h @@ -25,6 +25,11 @@ struct device; struct fwnode_handle; struct spi_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. */ @@ -56,6 +61,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 { @@ -91,6 +98,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 struct spi_offload *devm_spi_offload_alloc(struct device *dev, @@ -111,6 +130,11 @@ int spi_offload_trigger_enable(struct spi_offload *off= load, 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); + /* Trigger providers */ =20 struct spi_offload_trigger; diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index c230d6a209ee..fbb8b2d0b5ea 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -1092,6 +1092,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 Mon Nov 25 22:40:07 2024 Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.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 B69691EF0A3 for ; Wed, 23 Oct 2024 20:59:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717183; cv=none; b=LcqDJlt5GOqKZCEChXvdmrTnoeZe48yus/nkgKhUg/nfTzzaQ1+wfeAW3pJTPqD6QfAcpmfukQS8JMGD/8U+V0jibe48lRBAW8ljyHCagGUDndB09vxMbo2jZVkhlg7k5F5nZuek8IfrgfCsrzeTrP9KmCbiYbh5ZhgHCBbQODE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717183; c=relaxed/simple; bh=XvhHcdhZNewBCwkuETIKrCtDTE++Iy/v8VbbBamPFVA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cXmnIFTUX8AxwxxQyePcx/AhrLBBrMoEznzgeyGtaBolT6xV+J7otpJAqLHfjdL8fHQkn0PRAph27FA3lS5/Cc4/FdonQ/fxIPzTqztW9WRbx1t5fOHPKu3TcCFuE8ETZjXvq+PKvXI2o1vrBVq2WLix+g4H5Nb7Idpg4VBzxFE= 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=FDB43yQ4; arc=none smtp.client-ip=209.85.210.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="FDB43yQ4" Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-7180bd5b79dso123906a34.2 for ; Wed, 23 Oct 2024 13:59:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717180; x=1730321980; 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=FzgfL+AV5snNpAB4GWcpJiVSPKw5F7YZnAnm2zxfdrA=; b=FDB43yQ4dKyoM2oGw6QJlSQFE9zZtMkCkFUQR2G4wQXFkIK0lNO6OMTRP2st3/seNJ Wu4aWHTbbm2u9YmkOlXMHebys4vxshrSZ0u3+jgpMw5+ebjPS43qoWAq953KWcJRXoOb DMjX/kVbwV8qAIlN44v1epAoW5K0Wn+dkVxoDeVMIZM/MqEU/gAwSvRxzjZjB+Q86o5I x286JZxvOZZ8K7rbYfr1sJvaUFIjtM1uHGjUfrKYJS8Skzu4QmHWq9Xr6DpbOOwnMNEy CjxfzjUWLBmD5xg6QIenOoGG72Hoki3D4B8n3VnjtOk8CJi5XH/pSPZW+l9RJVs9GS0w +/sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717180; x=1730321980; 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=FzgfL+AV5snNpAB4GWcpJiVSPKw5F7YZnAnm2zxfdrA=; b=brp8PCGGM8XXqaHTuq1xFb5L8IDnQK9oW2S72PqAXq/Qz5nY67pqYyhGDJ4obTHIJj z+/IeiT76ykquzKEdKWGQSnVAlzR2+v5ySrrUxG6jiy7jJvY1+j0dMyO9JbPELJ291PW MEQYtIYcp6NQ3H/9n4X5xEUcAuQQPs6iCPm8YL90lSgii7HGH8S/Ry3WsWPgVTsRBJHJ WW6V6bP5T0mBluJEkAlUcMBuyrs1DGlSbjzQlE3To986EJTvBDfI6sm3Ug9R9FKGfTvo ZB7suX2zixCRn5FPYYwbltRYx5O8SPryWNuBdWofXSWXXgmC9yv0QbeXHrJRzDmCFLhp g1AQ== X-Forwarded-Encrypted: i=1; AJvYcCXm8kRlA3Asf7UlT46MKg8U7OXZ2zFk4XsOXguO/FQ+0yA/B/WPQs2aZKu/JrzzOke41Tr2iyyNI5Imgig=@vger.kernel.org X-Gm-Message-State: AOJu0YyYo+KUdmx0+flJDTYtGtmBN9d8y+MmGTqBd4If87+00Q3oEVxa gFT5ZR8VjtibqanH7LwilUfA6EvmrkqzdfroeqIS89dMzL7sgQGx1Qp6fjox7NM= X-Google-Smtp-Source: AGHT+IE+6Nj92WBf/zIljLQ1bBBMRAK9IBhUxA+iPkC8EABeTSSfuFXQ09Vz6VR//yZpf5hYdMYS7g== X-Received: by 2002:a05:6830:3c03:b0:703:7827:6a68 with SMTP id 46e09a7af769-7184b37dd63mr4026236a34.6.1729717179926; Wed, 23 Oct 2024 13:59:39 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:38 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:15 -0500 Subject: [PATCH RFC v4 08/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-8-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 The AXI SPI Engine has support for hardware offloading capabilities. This includes a connection to a DMA controller for streaming RX data and a trigger input for starting execution of the SPI message programmed in the offload. Signed-off-by: David Lechner --- 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 | 22 ++++++++++++++++++= ++++ 1 file changed, 22 insertions(+) diff --git a/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml = b/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml index d48faa42d025..5281b4871209 100644 --- a/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml +++ b/Documentation/devicetree/bindings/spi/adi,axi-spi-engine.yaml @@ -41,6 +41,24 @@ 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. + $ref: /schemas/types.yaml#/definitions/phandle-array + + dmas: + description: + DMA channels connected to the output stream interface of an offload = instance. + minItems: 1 + maxItems: 32 + + dma-names: + minItems: 1 + maxItems: 32 + items: + pattern: "^offload(?:[12]?[0-9]|3[01])-rx$" + required: - compatible - reg @@ -59,6 +77,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 Mon Nov 25 22:40:07 2024 Received: from mail-oo1-f50.google.com (mail-oo1-f50.google.com [209.85.161.50]) (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 19BA42003C2 for ; Wed, 23 Oct 2024 20:59:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717186; cv=none; b=L6dqcftt82CV/r3RxzmR3aF6jSu6n7cn3b6wdYhLjBbOR8cF8LJvOfqguwauPoXJWi3sGJL4f8A4a++fnVpIuSQ4AfC2ZQnShAdHjy7VmEgGdxiulCcyB2W7GU/JdOfTpy/sjtDcn0fUfXES+t9lln7qLqB4pTvNOwiklfo46Rg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717186; c=relaxed/simple; bh=0tN20Qo7iZmMxrMubM9JRyuw2sf4MH8ve9wlkVNucLY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hBPUWSbotsxH0rwAy8R8mdi9FNDIOu1g4C6ivi2YKMCfbpMWIwLua89FxrtmTMdhtGTiDLWPWvDVrArExHvCs3Kq91tMQzF4xbxX20bLFne7CGgR14Og2u55r4/oVxgUtwXuC67xW+Ml/kSb0kUP2Cjo0mqxySMkFae8e8pScSg= 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=vd2vMYiS; arc=none smtp.client-ip=209.85.161.50 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="vd2vMYiS" Received: by mail-oo1-f50.google.com with SMTP id 006d021491bc7-5eb5be68c7dso110204eaf.0 for ; Wed, 23 Oct 2024 13:59:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717183; x=1730321983; 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=p1jZtRWuGDZc3p8oqIaTkwjGWETdlg6Xkaw+OwlJ3/g=; b=vd2vMYiSjLwJgbCjGcJxb4Hi3969snHHseCts37Dit8sok8yAsCRSXxSrRJKibey7K /XsruxjbcyriVcJSZW3+GgWqBgQX/8FiecUPQVuQkanpYLCRxobvoMhEMkvkAltjWeFR JKURh9ObHNdbRTXZuHOCtTFgwMKffan7/MfBuy3SASTO9KZCrI5da3yDu1o7uJS9vUqr VDtRhUybl0OH2ll/6XqkA84zG8XzCWDzbarHXXck68faw8k3eVC09DiShYYYVJ1LajZd KLYLntkOBWZ2rhTgH4177Nj8kKQpTv07cXyf/L0PJtxDHyREHsSHWvuONPwM1zblyMjg pf3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717183; x=1730321983; 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=p1jZtRWuGDZc3p8oqIaTkwjGWETdlg6Xkaw+OwlJ3/g=; b=B1CHhaLOiEzsphERUbdsztiyqSfvU63WV9bbjicLFjBRgsvuTAH+iwcfpmNTK+URMu d5FkeHEKkcBJFxCDm/KgfQ6xc2ewoK/h0Hdt8AXdXK0+Zq2fAm/FUZoBWI05Tdl9EnQ+ Vo/IQAVCbRF8CN3NqRVbwVOLZ5WejEarKaxyFKseygRpWUjEyVPb+A42WB2OfrGS/1vT BQG4XD9b1VutsZhGu6wHlryex335GWjqWbk1T+WnaD1kwGeecBFmIrdYvP+6HQmj1HTo Px9ThmXCV2h6fl164kF0vPW5BFlwpn1+hHHhuAli5lI05geXumk5MqheNu+Jp5XMjFZc jErQ== X-Forwarded-Encrypted: i=1; AJvYcCVoOTy0DDweX8P1UIsqc/+32xL8RNXFRgzkMdGx/n8vyhzHGjS7f7saq9DTuGkEa+Hp3Ut1c401i3h7zb8=@vger.kernel.org X-Gm-Message-State: AOJu0YyFnwFEPGhlmVo7DOZyhHxoPKjWlAcDPpgQAGlnmkAj7AYIkPbK GkNIBYsdmyWJ+aDsgSgIq6qLsEwj+2dHzRh7UCtdvo7A3tfdoDSRe599QsLCt9I= X-Google-Smtp-Source: AGHT+IF8x+aD99RxFMh3FeGHpY5loia0nZ0+Sh4mFEUePMnHFDpbdmLf52YsmuvOEnKoXKJ1PAjaEw== X-Received: by 2002:a05:6820:1628:b0:5eb:c72e:e29b with SMTP id 006d021491bc7-5ebee96e015mr2912369eaf.7.1729717182998; Wed, 23 Oct 2024 13:59:42 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:41 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:16 -0500 Subject: [PATCH RFC v4 09/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-9-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 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. Signed-off-by: David Lechner --- 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 | 273 +++++++++++++++++++++++++++++++++++= +++- 2 files changed, 268 insertions(+), 6 deletions(-) diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 50d04fa317b7..af3143ec5245 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -168,6 +168,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 2d24d762b5bd..1710847d81a1 100644 --- a/drivers/spi/spi-axi-spi-engine.c +++ b/drivers/spi/spi-axi-spi-engine.c @@ -2,11 +2,14 @@ /* * 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 @@ -14,8 +17,10 @@ #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 @@ -23,6 +28,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 @@ -33,10 +39,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) @@ -78,6 +98,14 @@ #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 + +#define SPI_ENGINE_OFFLOAD_CAPS (SPI_OFFLOAD_CAP_TRIGGER | \ + SPI_OFFLOAD_CAP_TX_STATIC_DATA | \ + SPI_OFFLOAD_CAP_RX_STREAM_DMA) + struct spi_engine_program { unsigned int length; uint16_t instructions[] __counted_by(length); @@ -105,6 +133,16 @@ struct spi_engine_message_state { uint8_t *rx_buf; }; =20 +enum { + 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; @@ -117,6 +155,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 *offloads; + unsigned int num_offloads; }; =20 static void spi_engine_program_add_cmd(struct spi_engine_program *p, @@ -164,7 +207,7 @@ static void spi_engine_gen_xfer(struct spi_engine_progr= am *p, bool dry, =20 if (xfer->tx_buf) 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, @@ -220,16 +263,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, @@ -544,11 +595,94 @@ static irqreturn_t spi_engine_irq(int irq, void *devi= d) 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) { + 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_sdo_mem_size) + return -EINVAL; + + 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); @@ -560,20 +694,54 @@ 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); + int i; + + if (config->capability_flags & ~SPI_ENGINE_OFFLOAD_CAPS) + return ERR_PTR(-EINVAL); + + for (i =3D 0; i < spi_engine->num_offloads; i++) { + struct spi_offload *offload =3D &spi_engine->offloads[i]; + struct spi_engine_offload *priv =3D offload->priv; + + if (priv->offload_num =3D=3D spi_get_chipselect(spi, 0)) + return offload; + } + + return ERR_PTR(-ENODEV); +} + static int spi_engine_setup(struct spi_device *device) { struct spi_controller *host =3D device->controller; @@ -606,6 +774,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; @@ -641,11 +815,62 @@ static int spi_engine_transfer_one_message(struct spi= _controller *host, msg->status =3D -ETIMEDOUT; } =20 +out: spi_finalize_current_message(host); =20 return msg->status; } =20 +static int spi_engine_trigger_enable(struct spi_offload *offload) +{ + struct spi_controller *host =3D offload->spi->controller; + struct spi_engine_offload *priv =3D offload->priv; + struct spi_engine *spi_engine =3D priv->spi_engine; + unsigned int reg; + + guard(mutex)(&host->io_mutex); + + 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_controller *host =3D offload->spi->controller; + struct spi_engine_offload *priv =3D offload->priv; + struct spi_engine *spi_engine =3D priv->spi_engine; + unsigned int reg; + + guard(mutex)(&host->io_mutex); + + 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_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, + .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; @@ -660,8 +885,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, i; =20 irq =3D platform_get_irq(pdev, 0); if (irq < 0) @@ -676,6 +900,29 @@ 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. + */ + spi_engine->num_offloads =3D 1; + + spi_engine->offloads =3D + devm_spi_offload_alloc(&pdev->dev, spi_engine->num_offloads, + sizeof(struct spi_engine_offload)); + if (IS_ERR(spi_engine->offloads)) + return PTR_ERR(spi_engine->offloads); + + for (i =3D 0; i < spi_engine->num_offloads; i++) { + struct spi_offload *offload =3D &spi_engine->offloads[i]; + struct spi_engine_offload *offload_priv =3D offload->priv; + + offload->ops =3D &spi_engine_offload_ops; + offload->xfer_flags =3D SPI_OFFLOAD_XFER_RX_STREAM; + offload_priv->spi_engine =3D spi_engine; + offload_priv->offload_num =3D i; + } + 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); @@ -697,6 +944,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); @@ -718,6 +978,7 @@ static int spi_engine_probe(struct platform_device *pde= v) 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->num_chipselect =3D 8; =20 /* Some features depend of the IP core version. */ --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 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 002BE200B82 for ; Wed, 23 Oct 2024 20:59:45 +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=1729717187; cv=none; b=ZMi9bTyU6pEFWgGenbs+5bKo4lj1kWFPh11zPbYDjz3OegVl7wEW36/crFGNqcwBNOhFxoAlp1/qdJMDyKJcJXh7QLFvOLIzopr2DP+YAtfu9NP/3HNrVMKVD+F4J2PISE4jj+UlWVKSkVwkKaq1E21262gMM8M4yt6mwylw5XI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717187; c=relaxed/simple; bh=8WC+VN7A55DeabbVSHIKBLCpzt57GwTTXx9MLZvCANs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iOhfo2w6GlMDT9P+2WCsARbaoMVZCOP7tEWsGcSLmRBhTQdO7oQje2FjVTF5HIfv+19ZgiA6+kzTwD6Wbq43uQYZZV1lZcfUDN9yoj70FIC/aqobbefvjvRz518qWV/vMIW3H351kQyiAyClMos33EGniQbdfEM2yOS3tbWonAs= 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=wiA5mGCC; 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="wiA5mGCC" Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-7180c7a4e02so167209a34.0 for ; Wed, 23 Oct 2024 13:59:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717185; x=1730321985; 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=dziSJ5L8e9Gfyu+zY9UJSJKgJ8ld7SkYEjpA8rh44A8=; b=wiA5mGCC/pBQoSkumLTcsiiC1nDjp7voUPWN8bH7oSxGXPgM+1MmbZpwSv/cq5Vona NmIkKY1sNKLlHkT6S7kGcE5j8ug6wl6rdyW1Ml/zXLDxfmJ1x3EfTjXoXJEmQfqRTZk7 Vnca/Qqy7x1ZsXX6PvfmYAEHri3RUy47oTGvGMMVgk7j9Gyxp3jt01A0vhPcYo6uRFiF j5cOjT/5ahj1XMuGXyRc+WN45sgefweBOOFtg+f0741mti2dLGzCwdSoYVQmeIbMYIJz pE7U+jxwcQW+Rd4VaZzQh1dpTDcapERLUOnPSUqpuw/fsx+TO+uxcosJyVh8XjHx4xji VGsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717185; x=1730321985; 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=dziSJ5L8e9Gfyu+zY9UJSJKgJ8ld7SkYEjpA8rh44A8=; b=Xbh5Kzhg+ABfCfIVX5WBr6elQHnSfg4sOZyE7KB1On0rib/HuYbnjQ+UE6gOUUAgKw 44gU7NSjT9Xx3Wz/8pKE2qMwT6o5Jk4a8h/HnrkBn+pDl8h93O43/cBMGwk0TllfXD+r ZNAo2puTTLODy5JbGSHdndXRkuNinckZiuFbIMzhC2WWz1z8ljALNHi8Xc6mjVXe34rs M/tkUk60YRcH24RW1tuiJptQZmWaiC5uFW3mHCOFU0mBa08OGGw/OTKunS8mZ/PRUEfJ BJ7nLvdM1Tkj38cpGm4dNsWeNx3O6q47NG1tYVS1MkZCgFIG3yiX1WDUCZfc7dKRa01B BL3g== X-Forwarded-Encrypted: i=1; AJvYcCWZUg7zFxKAXcpTkKdeXFn6cuFTYNu8y6p1TsGxmU+LqllSZFh2qHzGkFXDU3oOrEsC64OmBr6+YfXRNc0=@vger.kernel.org X-Gm-Message-State: AOJu0YxndvduTkrkmvy4LPevMyJIDl85Nol2v9KJT+eAs7c+Ub0KBCEM BseZSd920UsOnzHrktZewOov3BzIOMxppAXjuLRgx9hVxAASz91JqALCmRCYC/k= X-Google-Smtp-Source: AGHT+IGx5OB1bCOiAcDzHn/B3rUnWv3xs/ZYAHkPOIih0OURil4uKA5bFpwt6OnHkytn90KhkCzQmw== X-Received: by 2002:a05:6830:6489:b0:718:196c:c811 with SMTP id 46e09a7af769-7184b350ea0mr3741387a34.31.1729717185084; Wed, 23 Oct 2024 13:59:45 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:44 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:17 -0500 Subject: [PATCH RFC v4 10/15] iio: buffer-dmaengine: document iio_dmaengine_buffer_setup_ext 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-10-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 The iio_dmaengine_buffer_setup_ext() function is public and should be documented. Also, while touching this, fix the description of @dev in related functions. @dev does not strictly have to be the parent of the IIO device. It is only passed to dma_request_chan() so strictly speaking, it can be any device that is a valid DMA channel consumer. Signed-off-by: David Lechner --- v4 changes: * This patch is new in v4. Jonathan, I think this patch stands on its own if you want to take it earlier than the rest of this series. --- drivers/iio/buffer/industrialio-buffer-dmaengine.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/i= io/buffer/industrialio-buffer-dmaengine.c index 19af1caf14cd..054af21dfa65 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -206,7 +206,7 @@ static const struct iio_dev_attr *iio_dmaengine_buffer_= attrs[] =3D { =20 /** * iio_dmaengine_buffer_alloc() - Allocate new buffer which uses DMAengine - * @dev: Parent device for the buffer + * @dev: DMA channel consumer device * @channel: DMA channel name, typically "rx". * * This allocates a new IIO buffer which internally uses the DMAengine fra= mework @@ -288,6 +288,21 @@ void iio_dmaengine_buffer_free(struct iio_buffer *buff= er) } EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_free, IIO_DMAENGINE_BUFFER); =20 +/** + * iio_dmaengine_buffer_setup_ext() - Setup a DMA buffer for an IIO device + * @dev: DMA channel consumer device + * @indio_dev: IIO device to which to attach this buffer. + * @channel: DMA channel name, typically "rx". + * @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. + * + * Once done using the buffer iio_dmaengine_buffer_free() should be used to + * release it. + */ struct iio_buffer *iio_dmaengine_buffer_setup_ext(struct device *dev, struct iio_dev *indio_dev, const char *channel, @@ -321,7 +336,7 @@ static void __devm_iio_dmaengine_buffer_free(void *buff= er) =20 /** * devm_iio_dmaengine_buffer_setup_ext() - Setup a DMA buffer for an IIO d= evice - * @dev: Parent device for the buffer + * @dev: Device for devm ownership and DMA channel consumer device * @indio_dev: IIO device to which to attach this buffer. * @channel: DMA channel name, typically "rx". * @dir: Direction of buffer (in or out) --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (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 F1819200CA0 for ; Wed, 23 Oct 2024 20:59:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717191; cv=none; b=uD62osRUQzmwZyx0z+H4GdJxoDBKijqMGJ9J1/1NdwgVqIMlzM6ha1B0zwRBBtnTHK5oOAeuNFiMZ+3ei71B0KcVf7fs2xP2SUQuhKKX2cV9+RILP8xZ2hFD8aDXlqhz3i77afpXsEXMBqlWJ7XcRo7y55HE/bXxijBiuWakDnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717191; c=relaxed/simple; bh=phtGbJKO/KlBEvCVg99uCLCLcEMBa9++5HvqvueHsOE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=EUrUPgTULx7fFE9CV2EQO2a8Qv6RlhOIX1ajWwc0+gAxyXnwwCzTnt7DFMQpF76vQRHGgGHWmMJh8eu/98ub3qzDKDQBG6rZC4jdWaJD7P6DhlO//H+JW5wNpcgZR5vHFgi2dRsQ/fmjNCVw6ObDH0WO95OydWoJc6HlhyYUIVE= 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=J3+2LKLA; arc=none smtp.client-ip=209.85.161.46 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="J3+2LKLA" Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-5eb67d926c4so140037eaf.0 for ; Wed, 23 Oct 2024 13:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717188; x=1730321988; 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=2otSV1rHjKqUFOfq5ASaT/zo6s+UQZpxLSJYnG+uWx4=; b=J3+2LKLA9/pmQK6Aj0HUtQ4bzYE2skcNCOQJcucvIh5db7Mj9epuqquRo7OC3GRUvB TC8LVUyBlsXSLQJahMq/CtNSxzGS7w60ZDnYD1QOjP6pcFW/DCZwyxBHOpz0b5rtvpDR GULmVN/9T+gLP3TLSjjE6tyfrHgK1t2ExTwa3wcif3NJdrEJ1U/MeqEE3y04gjqPD7t+ ka4g0tK/ebr6bCAbveHEAQNZBs1xclJlWkySoP7F9HqfxfwP8sbHu9n453WwfdzFvp0H 6Omeh4AINor6ivyiBue7DVHnZ7eMHyjRlqeWRmTz/YAj8vJnPskmHvXe7Dg1pywPZ5Wp VHKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717188; x=1730321988; 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=2otSV1rHjKqUFOfq5ASaT/zo6s+UQZpxLSJYnG+uWx4=; b=dVePUHWLZ47MB3Lha0tJJMYrTTN+QgxvzC+84guHB7sCeWLDNDkyo4SEyjUsNkJqU9 L6PgJvFAFKl8cNgjJY2+OibcFEJP5R+3HeVFwDUOsx5cFH+i8kTK/rP8dKtTYnUHmKnn vRGW+xarLx5tubt2lnlS+ufSPj4MjPRYmOdQukIo/I1Hs9CNI0BtKMlH94BWQXGSBSqv P8DO08oAbOTcszWB/i1Jk/DcQynVyZM7jDcL87uM1xBAgaP4AMyf+cclGCIFdsVNMQiI d6bsOAJP7QwViefp7ENEDkJuP9YYqRcjV2wigrFdPFv1L/DX0GlmBcf7n5p06FfaslZr i1vA== X-Forwarded-Encrypted: i=1; AJvYcCWliFqfvWWoL7mKyxQHe04gHyFYlABAe3osKyzXD+3CjTAop/v9WLLJ873d+5MsiITusIdUFqbcrzOkmX8=@vger.kernel.org X-Gm-Message-State: AOJu0YzbmPPF3/allcXMJSaQskeTrjvWteLQ9l2PxH6EHPu24kAaXMRZ GV8my8n+O6X3d/fJM+98ETWOXPixDDsPm3LCDF7OqeCgDiGzSs4On7wNoGDeP44= X-Google-Smtp-Source: AGHT+IGn/a4EyLP7/cnnLnEcDl15fgyVWsXOwJaSEtbGGCNRiWWTMbO61peIYd2WVZIRKRwFTzW5RQ== X-Received: by 2002:a05:6820:2293:b0:5d6:ab0:b9a6 with SMTP id 006d021491bc7-5ebee27a8a1mr2399382eaf.4.1729717188136; Wed, 23 Oct 2024 13:59:48 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:46 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:18 -0500 Subject: [PATCH RFC v4 11/15] iio: buffer-dmaengine: add devm_iio_dmaengine_buffer_setup_ext2() 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-11-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Add a new devm_iio_dmaengine_buffer_setup_ext2() function to handle cases where the DMA channel is managed by the caller rather than being requested and released by the iio_dmaengine module. Signed-off-by: David Lechner --- v4 changes: * This replaces "iio: buffer-dmaengine: generalize requesting DMA channel" --- drivers/iio/buffer/industrialio-buffer-dmaengine.c | 107 +++++++++++++++--= ---- include/linux/iio/buffer-dmaengine.h | 5 + 2 files changed, 81 insertions(+), 31 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/i= io/buffer/industrialio-buffer-dmaengine.c index 054af21dfa65..602cb2e147a6 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -33,6 +33,7 @@ struct dmaengine_buffer { struct iio_dma_buffer_queue queue; =20 struct dma_chan *chan; + bool owns_chan; struct list_head active; =20 size_t align; @@ -216,28 +217,23 @@ static const struct iio_dev_attr *iio_dmaengine_buffe= r_attrs[] =3D { * 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, + bool owns_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 dmaengine_buffer =3D kzalloc(sizeof(*dmaengine_buffer), GFP_KERNEL); - if (!dmaengine_buffer) - return ERR_PTR(-ENOMEM); - - chan =3D dma_request_chan(dev, channel); - if (IS_ERR(chan)) { - ret =3D PTR_ERR(chan); - goto err_free; + if (!dmaengine_buffer) { + ret =3D -ENOMEM; + goto err_release; } =20 ret =3D dma_get_slave_caps(chan, &caps); if (ret < 0) - goto err_release; + goto err_free; =20 /* Needs to be aligned to the maximum of the minimums */ if (caps.src_addr_widths) @@ -252,6 +248,7 @@ static struct iio_buffer *iio_dmaengine_buffer_alloc(st= ruct device *dev, =20 INIT_LIST_HEAD(&dmaengine_buffer->active); dmaengine_buffer->chan =3D chan; + dmaengine_buffer->owns_chan =3D owns_chan; dmaengine_buffer->align =3D width; dmaengine_buffer->max_size =3D dma_get_max_seg_size(chan->device->dev); =20 @@ -263,10 +260,12 @@ static struct iio_buffer *iio_dmaengine_buffer_alloc(= struct device *dev, =20 return &dmaengine_buffer->queue.buffer; =20 -err_release: - dma_release_channel(chan); err_free: kfree(dmaengine_buffer); +err_release: + if (owns_chan) + dma_release_channel(chan); + return ERR_PTR(ret); } =20 @@ -282,12 +281,38 @@ void iio_dmaengine_buffer_free(struct iio_buffer *buf= fer) iio_buffer_to_dmaengine_buffer(buffer); =20 iio_dma_buffer_exit(&dmaengine_buffer->queue); - dma_release_channel(dmaengine_buffer->chan); - iio_buffer_put(buffer); + + if (dmaengine_buffer->owns_chan) + dma_release_channel(dmaengine_buffer->chan); } EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_free, IIO_DMAENGINE_BUFFER); =20 +static struct iio_buffer +*__iio_dmaengine_buffer_setup_ext(struct iio_dev *indio_dev, + struct dma_chan *chan, bool owns_chan, + enum iio_buffer_direction dir) +{ + struct iio_buffer *buffer; + int ret; + + buffer =3D iio_dmaengine_buffer_alloc(chan, owns_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; +} + /** * iio_dmaengine_buffer_setup_ext() - Setup a DMA buffer for an IIO device * @dev: DMA channel consumer device @@ -308,24 +333,13 @@ struct iio_buffer *iio_dmaengine_buffer_setup_ext(str= uct device *dev, const char *channel, enum iio_buffer_direction dir) { - 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; - - buffer->direction =3D dir; + struct dma_chan *chan; =20 - ret =3D iio_device_attach_buffer(indio_dev, buffer); - if (ret) { - iio_dmaengine_buffer_free(buffer); - return ERR_PTR(ret); - } + chan =3D dma_request_chan(dev, channel); + if (IS_ERR(chan)) + return ERR_CAST(chan); =20 - return buffer; + return __iio_dmaengine_buffer_setup_ext(indio_dev, chan, true, dir); } EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_setup_ext, IIO_DMAENGINE_BUFFER); =20 @@ -362,6 +376,37 @@ int devm_iio_dmaengine_buffer_setup_ext(struct device = *dev, } EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup_ext, IIO_DMAENGINE_BU= FFER); =20 +/** + * devm_iio_dmaengine_buffer_setup_ext2() - 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. + */ +int devm_iio_dmaengine_buffer_setup_ext2(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, false, 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_ext2, IIO_DMAENGINE_B= UFFER); + 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 81d9a19aeb91..7bdb979b59f2 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_free(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_ext2(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 Mon Nov 25 22:40:07 2024 Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.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 1EDFD200C9F for ; Wed, 23 Oct 2024 20:59:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717193; cv=none; b=ccSIaAFcmx2dNBjXE8vKs3T3Cp0M1qygxQG4mjEHnusxX/3WTGXbhZyaVUMFX4AX3OmoqzPwCgaDchyG6gp0wJtCRmHBWodmzZy/vmMuPc06ehUOBOZTGV4Zv1utnCRx3RVeB3Hc3MpS/vuo2WKJrl7vPNKRo1kcrVY2kz/2srU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717193; c=relaxed/simple; bh=LJljvnvTIWQemQAYeFT3M0jCfTCH8lY2g5JMOE6bALQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BL6g+HRwJOkbjMWuEsWhtJlgOoof7hU2ZiGHLqUhetRUNSoQdyPyJeShv+8Edm0y7sJ66mvAF9qI7GguFwD6OP54MEoSKj/L0rut5lq3PZdOoFGV7zZ30cQboy88dCFSgh7QAPY2AN5qPdMZqth35kSVbxIVVoaKoEGoTP+HJ+g= 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=Dv8N7EVd; arc=none smtp.client-ip=209.85.210.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="Dv8N7EVd" Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-71822fabf4fso152260a34.2 for ; Wed, 23 Oct 2024 13:59:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717190; x=1730321990; 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=TBMr4LNoUZbbXc8Fm5EIMq3q1gHL1OkhKLhsRYFm190=; b=Dv8N7EVdYBrJCLgnHPD7XkNwwYyz/3ioOZu3u/Qq5rKKNVySRA4jb0QJw0TSSE8zh/ hzJWUevqMn3nB/tH5yO4ex5mX7uwBk8b1V/YrGcUjOptNCY/+rUJkkgHbpZS7nAyo3Yz nyuvmcGWf+heTXJ2jgbzFHjBZz2vRp47Ml+/rZLOldmKNufJY2QXVcxJIpAnUYqmJpZg Om92I5hPPt9+00HR23emD1G0ped8nEifz0nLptlhnz6252hsPs7nHwpMC6WRsU+b4pLK mrH9KWqUKpsbv3F620YuwsFTFwXTTUaXtkG8/ez06p3hgmBJTT9UIK//X7Y6t8mwmdx4 6j6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717190; x=1730321990; 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=TBMr4LNoUZbbXc8Fm5EIMq3q1gHL1OkhKLhsRYFm190=; b=fV9Ri3EuZXrIHtRbyRzwMrO0eO0Hi89R6rA+wXdugSVRUYrxh9jAxfMnBy4S0tr654 M9jN+5vdGqbL5cPmfBSef4SXt57E7Fx/Mib7vSenmQQw9j9st7b/48RJkm4j+jt/BWty /jPdS5+kOi6K0O01hQd6B264Hdc/CBKFeIX7X2RdSg9nf/hXHJR/rz0KEHtKMCTj7TU7 k007bSZYcNDM6KHfUJVM0vK95tLvoU1ptJW+5k/vqotjM9KEtRIqdGUiWVzNxcmLqcit j1bg9r29QyT6LaZBa8W9Q1bBgvbPgGJnn79fCZBUyMHMfPjSc3isYgjZoA6Koe7E8+jR tvPw== X-Forwarded-Encrypted: i=1; AJvYcCXyhbBxIDLgNhMmO8GfuQyJGRE7vfDqaNQtfZ0jHo5VU0jLWApXJ9/HeIEk3D7P30F+SygTQvnoGGcVcxw=@vger.kernel.org X-Gm-Message-State: AOJu0YwEFttx09EbEgCWbfc2yLOv53BE/Vmyx3qctC0/G9ilWg3e7c7Z bcBx/hRlcGeP5pwDDkXtKpMnKKqTk9lhh+JeB0aRKzt5/rfVBEh+S+hIHoi0rMM= X-Google-Smtp-Source: AGHT+IHuDa6m4MVjQHpG/uX5mRcogfjWZsE/n+KJJOq3gRYaUpX6Xw3iHUMb0NuLlRdiNjn65Bj43g== X-Received: by 2002:a05:6830:349a:b0:718:8f2:220d with SMTP id 46e09a7af769-7184b2edcfemr4031025a34.14.1729717190190; Wed, 23 Oct 2024 13:59:50 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:48 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:19 -0500 Subject: [PATCH RFC v4 12/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-12-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 Replace use of storagebits with realbits for determining the number of bytes needed for SPI transfers. When adding SPI offload support, storagebits will no longer be guaranteed to be the "best fit" for 16-bit chips so we can no longer rely on storagebits being the correct size expected by the SPI framework. Instead, derive the correct size from realbits since it will always be correct even when SPI offloads are used. Signed-off-by: David Lechner --- 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 a5aea4e9f1a7..6d1202bd55a0 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 Mon Nov 25 22:40:07 2024 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 1EE262038CC for ; Wed, 23 Oct 2024 20:59:53 +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=1729717196; cv=none; b=ce/A2nPaA3mXarq5hPDHgoCr0TW3ilBWEHeCLc9PnFaacRy3WsN+APCd10Yf54kcjMckPufAwaMgySry/Qpu4o3fv0HIQDwWzDSh/aAm2rNqe1gfLKwJoJ16FZPsQF9M0rGBLjpBHTXxpullyt3Enhy9XiwJsphp8ecKvkYLaHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717196; c=relaxed/simple; bh=pfaM/qCZp/7RrJRQByE9+Fl0yQHeVPFno4H9YJjhgqY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Y1eH5niu+qFpYebijUzCMLIqC59d1yVsx0PFAa6IT7C6Qvfl140cRoJv/j9rIStHtInztEy6pTZK/O4yuJJRe/ERHaPFSU6yRdpEsaLeVQ3vWxRUPvzpu4p07PkJMaBKn1OWMtoPDp/J0XRFkn9YSeoY/rIm/qdumYgtNsSyPRo= 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=n6i647jU; 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="n6i647jU" Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5eb67d926c4so140076eaf.0 for ; Wed, 23 Oct 2024 13:59:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717193; x=1730321993; 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=zawaNnQ/tIDSblfd6yVLzHckf/0fbTuRKMQ9RIWxWz0=; b=n6i647jU+UcpqtF1SBDbk359Q5VtrPImqx89BCPxS8h6RvDg6LftxATbQk4ull42BP +n7jvBvn9/sIpsqanPOUYklZUBOVUTQR83v8p4geY06Tt7jrPNtXFFdr6K4ZCitb0cRH 3ejNWNcVSCjy7EPlM0uCGhdUnr869RaTwY0hNo2cN5h6ve8C4QC9M5cv1Jc1JPE1NGVx 4U9xZVX4BHtyR8VuhLajyRJo77T0cxFBYoTjsarxuNjKZ9TCBi/P2XRSgEllFX708zpo y7Lq7jWXLGZulmcRBXqxmQcfhs9Bx7I2WOKLviAT92PIHsRbJi2aJEFDsQ0KsJK8AIGE ibgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717193; x=1730321993; 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=zawaNnQ/tIDSblfd6yVLzHckf/0fbTuRKMQ9RIWxWz0=; b=B4fsy7LLqAdlrGjr/GCOeP13O5QBWnxoA3WlCPIEdpHNlJdV9js4w2beeqlPnVv+5E o90By18fiWCgY8SWNMcf6tIQUIAT/Ht/8jjWI58wb49pt2e3G3Pd3YjjRHtZct/wC0aD 9jNaRKG7eftvWa7c93dsUp0mwBsYE/juFnTkXUApgsjRvYVkPvF44Ix3xq84DI9DiFww bG9yy08eOXVNbf1dLqJv3wWhL5gYJbE7eh6orbJzLtNkJnsgx0x/bV4X5ucF0gqeRNQU XQbZs5BQqp6FNWhX1EB9+uS3QDSXbToA1uxhXRadkZ3pvKX4e+EdIPUCsdt/UB0Mv+jo woxw== X-Forwarded-Encrypted: i=1; AJvYcCVAXrXjn5DBR+9yXHKbedB2wbnlgcGE46BlEk/qfAvdEi/6Rj1sqDrjAfJiJ8YtBD5Bt8Ni9qKKK9UPEGQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yw63fx3gUHq7vYLEIkrztraTKECQkUFUHejoEpD71zLvpTBJvcY AkttjtwaMLuD6BUlOLdVdliV9SzE9Qs/8P+XWCDHa4piUUTjFLar6A6fsWrrWAqiqBvJAzWRlEl q X-Google-Smtp-Source: AGHT+IFWW95edUGhfU3qExtoaLye6C6MtgstZi0ufDn2VZvcRopsiuFPCUaukguXyRmg4R+u8FRDLg== X-Received: by 2002:a4a:bd98:0:b0:5eb:d48d:f172 with SMTP id 006d021491bc7-5ebd86b8577mr4431800eaf.3.1729717193232; Wed, 23 Oct 2024 13:59:53 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:51 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:20 -0500 Subject: [PATCH RFC v4 13/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-13-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 This adds support for SPI offload to the ad7944 driver. This allows reading data at the max sample rate of 2.5 MSPS. Signed-off-by: David Lechner --- 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 | 233 +++++++++++++++++++++++++++++++++++++++++++= +--- 2 files changed, 222 insertions(+), 12 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 179d83aafd8a..92dfb495a8ce 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -346,6 +346,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 6d1202bd55a0..52005bd3174c 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,11 @@ 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; void *chain_mode_buf; /* Chip-specific timing specifications. */ const struct ad7944_timing_spec *timing_spec; @@ -81,6 +89,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, @@ -96,6 +106,7 @@ struct ad7944_chip_info { const char *name; const struct ad7944_timing_spec *timing_spec; const struct iio_chan_spec channels[2]; + const struct iio_chan_spec offload_channels[1]; }; =20 /* get number of bytes for SPI xfer */ @@ -129,6 +140,24 @@ static const struct ad7944_chip_info _name##_chip_info= =3D { \ }, \ IIO_CHAN_SOFT_TIMESTAMP(1), \ }, \ + /* basically the same minus soft timestamp plus sampling freq */\ + .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), \ + }, \ + }, \ } =20 /* pseudo-differential with ground sense */ @@ -239,6 +268,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 @@ -326,6 +397,46 @@ 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 < 0 || val2 < 0) + return -EINVAL; + + return ad7944_set_sample_freq(adc, val); default: return -EINVAL; } @@ -333,6 +444,43 @@ static int ad7944_read_raw(struct iio_dev *indio_dev, =20 static const struct iio_info ad7944_iio_info =3D { .read_raw =3D &ad7944_read_raw, + .write_raw =3D &ad7944_write_raw, +}; + +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 +594,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; @@ -590,20 +743,75 @@ 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"); + + if (ret =3D=3D -ENODEV) { + dev_info(dev, "SPI offload not available\n"); + + 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_ext2( + 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 +846,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 Mon Nov 25 22:40:07 2024 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 41D48204F85 for ; Wed, 23 Oct 2024 20:59:57 +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=1729717198; cv=none; b=YRJO8z3tvmwACQkb0Zc+ddcwihSL9eD21pRTcWqCiQWxvjazq8Gqlm0PeNBVYFoJSuZ/wQT9YrQypTsGSIJi2SGY6Rr3WRlWHiXXoVoWeJujE+lu98SpDsvp7vnCr8iq6YM9bEp+ADMfSqQZl/Y25vrpcTqDQEK4otJbbMPW83M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717198; c=relaxed/simple; bh=2vnDwDqKD1Df0P4m8qAbdOZY07gv8Qe41/xtmVsFRRY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sCJmUJMq/6coTjnPUC0zUXp8HMn3NGxPVOUjopv+u5LXPhl6r6EIz7zi5ZgNSMCxVNK3K6y4Pia+LTrpTicg8inRBJ8EA92LzKdGC53614jDS1QIOEaLZREFme0zjFqKPidCAhmRWWLENQvu2ehCQU+EUtTC44aXcLVQuRYWLK8= 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=a/zK5lHF; 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="a/zK5lHF" Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-7180ab89c58so165512a34.1 for ; Wed, 23 Oct 2024 13:59:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717196; x=1730321996; 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=DfXBayzvmGl7Om53lQChDWQ5oJwLUn4oZetI5ml3dsI=; b=a/zK5lHFes0G51TH9SvSMiPzJlPUsMW/7zQVVufoJ9SE8/vdvYhbiZB3t1JEyyfYFG OHwiz9H5bu3J0ym4OsghCSVjUDHJOFYa3dH0xj3vCRvmeXCei+4PRC9bbvRD8edWEuyi 5+qIrLGf3oYMypAbALREY+VMlJax+zWsg2K+40q9+zYJyJzjRx0hpwBkDAY0fyNmVomD vmVNBW4IZfQsmV9iIJxeq0hxRydntJZZAQMqI3eOmI5+c959VImQ+EjfLLXbAjdJ+LXk RDZ3PqMn0jWmCg7KGtkxcDD5msqzmQTAwcdo4fxOcxV7GgXZ/qcAmntBY5yDxxKLCL6W LvIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717196; x=1730321996; 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=DfXBayzvmGl7Om53lQChDWQ5oJwLUn4oZetI5ml3dsI=; b=ZB5+hcmhYjdwZ8uAJ6PT0KRX4qEDfn/fnsxxIRGQu5Eur9sIPviX3XFMh8Qo7KPLxb YTB4MxaN2kfVQS2XOsPxBphEf+HQjiYE6wtBwLf3W3UvwegO6YqbV7znotB1jk36jHWD +P42/sBgcUnfMcP1IjqouzgcNEAQpzzrU8TGoylI4BX/Ew/Cieoaj0b5EwtrBrohwBQa oqCbaYaxr7N7QPWTxMZv+XLWpNBQcFufT+0jz7YeN5UNurvlCAUucwlZfepYHVXnNv+Q LukWahxH+km7Z3dvCMU8lfs6uQBQfJyTnj+VGMbj/0ykZuugu5MaD0B8TQ3uTNjB2+sx g5QQ== X-Forwarded-Encrypted: i=1; AJvYcCVi6KaJI3Oh4gpsCJ7nXZdNfNYc/BNCTEkTU8kfCOZYxUGAwtYXTSepWZJXUPogXzat8AEJNR6czSVV+YQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxTbX2UFrWb86HvlQrq7P7IIcb+wBV+gbVXakR1BPb10UjoyXLn iEXd8NsDBuRfK9SnA2NaGYsLbJDc9WOB7LHNq3DxYxHkb0Jfvcleh7MutR5ht4g= X-Google-Smtp-Source: AGHT+IHcc2KQj1iOOqWOOogxpx7Ye/U3BnxQ7ARe1DkUoVtDbb5HPRAYcUyzjh6Vi+OdGMJp0G3/SQ== X-Received: by 2002:a05:6830:658d:b0:718:1a11:79e2 with SMTP id 46e09a7af769-7184b2b51eemr4907344a34.13.1729717196368; Wed, 23 Oct 2024 13:59:56 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:54 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:21 -0500 Subject: [PATCH RFC v4 14/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-14-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 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. The $ref for spi-peripheral-props.yaml is moved to keep similar $refs grouped together. Signed-off-by: David Lechner --- v4 changes: new patch in v4 For offload support, this doesn't actually tell the whole story. For some use cases, it would be perfectly reasonable to have a PWM directly connected to the CNV pin, which is why I have made the bindings like this. However, in order to work with the the AXI SPI Engine SPI offload and allow for non-cyclic DMA transfers, the actual signal that is being connected to the CNV pin is: CNV =3D (PWM_OUT && !DMA_FULL) || CNV_GPIO_OUT RFC: Is this binding sufficient for this use case? Or should we consider something that describes it more accurately? The gory details (mostly of interest for those reviewing the driver patch that uses this binding): * The PWM output has to be disabled in hardware by connecting it to an AND gate where the 2nd input comes from the DMA controller. This is necessary to ensure that we can only trigger conversions as long as there is room in the DMA buffer to receive them. If we continue to trigger conversions when the DMA is full, then the advanced sequencer in the ADC chip keeps advancing and we will end up starting the next batch of conversions with a random channel when DMA is no longer full. * To get out of conversion mode, we also have to toggle the CNV pin manually one time. If it wasn't for the DMA mask on the PWM output, we could just use 0% and 100% duty cycle to toggle the CNV pin, but since the PWM output is masked by the DMA, we have to use a GPIO to toggle the CNV pin. The GPIO signal is connected to an input of an OR gate along with the output of the AND gate mentioned above. --- Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml | 13 +++++++++++= -- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml b/Do= cumentation/devicetree/bindings/iio/adc/adi,ad4695.yaml index 310f046e139f..0d767d8b867a 100644 --- a/Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml @@ -18,8 +18,6 @@ description: | * https://www.analog.com/en/products/ad4697.html * https://www.analog.com/en/products/ad4698.html =20 -$ref: /schemas/spi/spi-peripheral-props.yaml# - properties: compatible: enum: @@ -84,6 +82,9 @@ properties: description: The Reset Input (RESET). Should be configured GPIO_ACTIVE= _LOW. maxItems: 1 =20 + pwms: + description: PWM signal connected to the CNV pin. + interrupts: minItems: 1 items: @@ -106,6 +107,12 @@ 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 to 3. + const: 2 + "#address-cells": const: 1 =20 @@ -165,6 +172,8 @@ required: - vio-supply =20 allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + - $ref: /schemas/spi/trigger-source.yaml# - oneOf: - required: - ldo-in-supply --=20 2.43.0 From nobody Mon Nov 25 22:40:07 2024 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 70212205E17 for ; Wed, 23 Oct 2024 21:00:00 +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=1729717203; cv=none; b=HcoqOomYxF/ZR6GAqpxalA4x567K01M4tn2W/kKmwDBLMr0MzweY4DTDrXFELWi9XFzGAdjSqQfrtLuJHq0teyfnhrRcxOrWQucS7lR9Lx090CBKypc6o9bd9hN2jObZz7BUnpVgaAVZQNaR6vG6kSSkwVAxX39p6HocQG47+cw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729717203; c=relaxed/simple; bh=yNmOcChsKq4/9GgjRbr9ryWwkcaHw6cRDMeIchBwwRM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eJYFP1HvAterYF2hWHWzPtGB5Kkb9yww3/e6xHrqJ56zsZjdfLa50ZvwJpbWiDuWfo6gmzP4/+kPQ/vcFJ8ep2/7SMQBmE3lLVUY8VKDfm4I4J9ni5z1aroCyI90mOBW3Jj3LOsmMVqrXdm7AXkGZeYuPIydKCH6uUaAdZGHbkc= 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=bAMCips6; 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="bAMCips6" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5eb67d926c4so140105eaf.0 for ; Wed, 23 Oct 2024 14:00:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1729717199; x=1730321999; 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=NpqewWkBeL5YA+0h8Tu2l8PEEwW8v5sVkJknMJkiYMA=; b=bAMCips633jgJEawwY/nQ8bXGmPfvVDC4w3VlpXYU0EkEACP7j/BlkJ740wQamWwEB NGPIHyNYB53JuHAE2FPtG+xpI3j3k7HQIV2d+Jqsuqj7FoxEj5hdNvAR6idVLmfpuA+1 iBP3M9nRzw5qG0yf23cnpL+StfQCYnL+RU1eLDvV+1wMEz2rg8xRo++s9lSWBHfVBQwV fRsTM1o+GagHK5ulNtyv4COKrPupm7m6qGstsYmIf7X54GOiXzvBtSIPFlxsiTgypv9A 3VegU4r1SwwRDJmjJyKLHJwBXe2h6IUL70ftMMIK6HrnKuLVSxsvZIGoETJvYgapQ/jx 4tOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729717199; x=1730321999; 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=NpqewWkBeL5YA+0h8Tu2l8PEEwW8v5sVkJknMJkiYMA=; b=XKiKDBK0W0d+gxOXJAvfq+4GG3Jmq8IZx2fI0YcdsB0+gpaScjSoQlp0TKbIE80x/x d9TXneCP/jgkSsdZAMxJXcMFMfQwit9eHCa9FyPWa4uLWoiBqSzJxBGTL2fAohjfXTrk 159zWkiQJvrWh/FdUNpWadiDzCOBMmEhnCkTQbh6hB9DK61egsU5U1Y+NojorKT3QnP7 4uPC0hezvekoE8g2Ja6Y+I2yws45pEE/lnUPdonU2Riz2MI/95zKEVyaly9vdhx1p94x tO0oNtJSTlT+eTU74iuvqJ8+9jDWzyyHmRvcVAYiQoVEkXNW3188G+1Wa5fb4AepRlUa H+Zw== X-Forwarded-Encrypted: i=1; AJvYcCWMOGqrqKUGX02fgiwN3v7LVGA0nhLYRrk0T0Y9o66CGO4w4wmBsFgzpNYZSeTBn7UqV7hpyHlEtWxDoik=@vger.kernel.org X-Gm-Message-State: AOJu0YxnV/Fe3DMZZjZM8XaeZ6jl64XcQqTwX2iGDZy61Q3xCQOkgw8o NfDZzwxb6aw2rMkfMbonUdVpQVJ+HNbB5+vvjjGKSURz4JIN4jaTDHCX88Gmamo= X-Google-Smtp-Source: AGHT+IEq8xTH1knR1qZber5eXHa4hS9/pMfWY5RtrwXk/Ht06SeRlMq6G/tOoUSvZ9OAaxDSBg1lsw== X-Received: by 2002:a05:6820:160d:b0:5eb:6b3e:ce7f with SMTP id 006d021491bc7-5ebedeb15f8mr2185894eaf.0.1729717199564; Wed, 23 Oct 2024 13:59:59 -0700 (PDT) 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-5ec02c2c157sm52730eaf.44.2024.10.23.13.59.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Oct 2024 13:59:58 -0700 (PDT) From: David Lechner Date: Wed, 23 Oct 2024 15:59:22 -0500 Subject: [PATCH RFC v4 15/15] 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: <20241023-dlech-mainline-spi-engine-offload-2-v4-15-f8125b99f5a1@baylibre.com> References: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> In-Reply-To: <20241023-dlech-mainline-spi-engine-offload-2-v4-0-f8125b99f5a1@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Cc: 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, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.1 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 Signed-off-by: David Lechner --- drivers/iio/adc/Kconfig | 1 + drivers/iio/adc/ad4695.c | 470 +++++++++++++++++++++++++++++++++++++++++++= ---- 2 files changed, 440 insertions(+), 31 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 92dfb495a8ce..f76a3f62a9ad 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -53,6 +53,7 @@ config AD4695 depends on SPI select REGMAP_SPI select IIO_BUFFER + select IIO_BUFFER_DMAENGINE select IIO_TRIGGERED_BUFFER help Say yes here to build support for Analog Devices AD4695 and similar diff --git a/drivers/iio/adc/ad4695.c b/drivers/iio/adc/ad4695.c index 595ec4158e73..c150851d1fb1 100644 --- a/drivers/iio/adc/ad4695.c +++ b/drivers/iio/adc/ad4695.c @@ -19,14 +19,18 @@ #include #include #include +#include #include #include #include #include #include +#include #include +#include #include #include +#include #include #include =20 @@ -66,6 +70,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)) @@ -87,6 +93,7 @@ =20 /* timing specs */ #define AD4695_T_CONVERT_NS 415 +#define AD4695_T_CNVH_NS 10 #define AD4695_T_WAKEUP_HW_MS 3 #define AD4695_T_WAKEUP_SW_MS 3 #define AD4695_T_REFBUF_MS 100 @@ -121,9 +128,18 @@ struct ad4695_channel_config { =20 struct ad4695_state { struct spi_device *spi; + struct spi_offload *offload; + struct spi_offload_trigger *offload_trigger; + u32 spi_max_speed_hz; 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]; @@ -571,6 +587,167 @@ static irqreturn_t ad4695_trigger_handler(int irq, vo= id *p) return IRQ_HANDLED; } =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); +} + +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; + xfer->bits_per_word =3D 16; + xfer->len =3D 2; + + spi_message_init_with_transfers(&st->buf_read_msg, xfer, 1); + st->buf_read_msg.offload =3D st->offload; + + st->spi->max_speed_hz =3D st->spi_max_speed_hz; + ret =3D spi_optimize_message(st->spi, &st->buf_read_msg); + st->spi->max_speed_hz =3D AD4695_REG_ACCESS_SCLK_HZ; + 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 + * due to issues with ordering with respect to entering/exiting + * conversion mode. + */ + 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: + /* have to unwind in a different order to avoid triggering offload */ + spi_offload_trigger_disable(st->offload, st->offload_trigger); + ad4695_cnv_manual_trigger(st); + 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); + + /* + * We have to trigger on more conversion to ensure that the exit + * conversion mode command works. + */ + ad4695_cnv_manual_trigger(st); + + 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 @@ -583,13 +760,20 @@ static irqreturn_t ad4695_trigger_handler(int irq, vo= id *p) */ static int ad4695_read_one_sample(struct ad4695_state *st, unsigned int ad= dress) { - struct spi_transfer xfer[2] =3D { }; - int ret, i =3D 0; + struct spi_transfer xfer =3D { }; + int ret; =20 ret =3D ad4695_set_single_cycle_mode(st, 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 xfer to read the @@ -598,23 +782,29 @@ static int ad4695_read_one_sample(struct ad4695_state= *st, unsigned int address) if (address =3D=3D AD4695_CMD_TEMP_CHAN) { /* We aren't reading, so we can make this a short xfer. */ st->cnv_cmd2 =3D AD4695_CMD_TEMP_CHAN << 3; - xfer[0].tx_buf =3D &st->cnv_cmd2; - xfer[0].len =3D 1; - xfer[0].cs_change =3D 1; - xfer[0].cs_change_delay.value =3D AD4695_T_CONVERT_NS; - xfer[0].cs_change_delay.unit =3D SPI_DELAY_UNIT_NSECS; + xfer.tx_buf =3D &st->cnv_cmd2; + xfer.len =3D 1; + + ret =3D spi_sync_transfer(st->spi, &xfer, 1); + if (ret) + return ret; =20 - i =3D 1; + /* + * 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. */ st->cnv_cmd =3D AD4695_CMD_EXIT_CNV_MODE << 11; - xfer[i].bits_per_word =3D 16; - xfer[i].tx_buf =3D &st->cnv_cmd; - xfer[i].rx_buf =3D &st->raw_data; - xfer[i].len =3D 2; + xfer.bits_per_word =3D 16; + xfer.tx_buf =3D &st->cnv_cmd; + xfer.rx_buf =3D &st->raw_data; + xfer.len =3D 2; =20 - return spi_sync_transfer(st->spi, xfer, i + 1); + return spi_sync_transfer(st->spi, &xfer, 1); } =20 static int ad4695_read_raw(struct iio_dev *indio_dev, @@ -721,11 +911,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) @@ -779,6 +992,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) + 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; } @@ -857,6 +1081,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, @@ -970,26 +1195,173 @@ static int ad4695_parse_channel_cfg(struct ad4695_s= tate *st) return 0; } =20 +static bool ad4695_offload_trigger_match(void *priv, + enum spi_offload_trigger_type type, + u64 *args, u32 nargs) +{ + if (type !=3D SPI_OFFLOAD_TRIGGER_DATA_READY) + return false; + + // TODO: create macros for args[0] and args[1] + // args[0] is the trigger signal, 0 =3D=3D BUSY + // args[1] is the GPIO pin number, 0 =3D=3D GP0, 3 =3D=3D GP3 + if (nargs !=3D 2 || args[0] !=3D 0) + return false; + + return true; +} + +static int ad4695_offload_trigger_request(void *priv, + enum spi_offload_trigger_type type, + u64 *args, u32 nargs) +{ + struct ad4695_state *st =3D priv; + + /* Should already be validated by match, but just in case. */ + if (nargs !=3D 2) + return -EINVAL; + + /* DT tells us if BUSY signal uses GP0 or GP3 */ + if (args[1] =3D=3D 3) + 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_GPIO_CTRL, + AD4695_REG_GP_MODE_BUSY_GP_SEL); +} + +static int ad4695_offload_trigger_validate(void *priv, 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_restore_spi_max_speed_hz(void *data) +{ + struct ad4695_state *st =3D data; + + st->spi->max_speed_hz =3D st->spi_max_speed_hz; +} + +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 { + .name =3D "data-ready", + .id =3D 0, + .parent =3D dev, + .fwnode =3D dev_fwnode(dev), + .ops =3D &ad4695_offload_trigger_ops, + }; + struct spi_offload_trigger *trigger; + 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"); + + trigger =3D devm_spi_offload_trigger_alloc(dev, &trigger_info); + if (IS_ERR(trigger)) + return dev_err_probe(dev, PTR_ERR(trigger), + "failed to allocate offload trigger\n"); + + ret =3D devm_spi_offload_trigger_register(dev, trigger, st); + 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"); + + /* Currently, only self-provided trigger is supported. */ + if (st->offload_trigger !=3D trigger) + return dev_err_probe(dev, -EINVAL, "offload trigger mismatch\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"); + + /* + * REVISIT: ideally, we would ask the RX DMA stream what the + * buffer layout is. Right now, the only supported offload is + * the ADI ad469x HDL project which always uses 32-bit word + * size for data values, regardless of the SPI bits per word. + */ + + for (i =3D 0; i < indio_dev->num_channels; i++) { + struct iio_chan_spec *chan =3D &st->iio_chan[i]; + + /* update storagebits to match offload capabilities */ + chan->scan_type.storagebits =3D 32; + /* add sample frequency for PWM CNV trigger */ + chan->info_mask_separate |=3D BIT(IIO_CHAN_INFO_SAMP_FREQ); + } + + return devm_iio_dmaengine_buffer_setup_ext2(dev, indio_dev, rx_dma, + IIO_BUFFER_DIRECTION_IN); +} + 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; @@ -1002,8 +1374,13 @@ static int ad4695_probe(struct spi_device *spi) return -EINVAL; =20 /* Registers cannot be read at the max allowable speed */ + st->spi_max_speed_hz =3D spi->max_speed_hz; spi->max_speed_hz =3D AD4695_REG_ACCESS_SCLK_HZ; =20 + ret =3D devm_add_action_or_reset(dev, ad4695_restore_spi_max_speed_hz, st= ); + if (ret) + return ret; + st->regmap =3D devm_regmap_init_spi(spi, &ad4695_regmap_config); if (IS_ERR(st->regmap)) return dev_err_probe(dev, PTR_ERR(st->regmap), @@ -1014,6 +1391,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); @@ -1139,14 +1521,39 @@ static int ad4695_probe(struct spi_device *spi) indio_dev->info =3D &ad4695_info; indio_dev->modes =3D INDIO_DIRECT_MODE; 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; + static const struct spi_offload_config ad4695_offload_config =3D { + .capability_flags =3D SPI_OFFLOAD_CAP_TRIGGER + | SPI_OFFLOAD_CAP_RX_STREAM_DMA, + }; + + st->offload =3D devm_spi_offload_get(dev, spi, &ad4695_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 (ret =3D=3D -ENODEV) { + /* If no SPI offload, fall back to low speed usage. */ + dev_info(dev, "SPI offload not available\n"); + + /* 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); } @@ -1183,3 +1590,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