From nobody Tue Nov 26 00:45:27 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