From patchwork Wed Nov 4 19:24:53 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Hilliard X-Patchwork-Id: 3867 Return-Path: X-Original-To: noreply@patchwork.local Delivered-To: noreply@patchwork.local Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mxe881.netcup.net (Postfix) with ESMTPS id D10291C08F2 for ; Wed, 4 Nov 2026 21:49:19 +0100 (CET) Authentication-Results: mxe881; dkim=pass header.d=gmail.com; spf=pass (sender IP is 172.234.253.10) smtp.mailfrom=linux-sunxi+bounces-27208-noreply=patchwork.local@lists.linux.dev smtp.helo=sea.lore.kernel.org Received-SPF: pass (mxe881: domain of lists.linux.dev designates 172.234.253.10 as permitted sender) client-ip=172.234.253.10; envelope-from=linux-sunxi+bounces-27208-noreply=patchwork.local@lists.linux.dev; helo=sea.lore.kernel.org; Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 6C98862B9A for ; Wed, 4 Nov 2026 19:38:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1793820309; b=FnjiC1KH9VEtwIpawJPW02stwcCT7aTg0tFzIJZqkfGNYJu18Sdh63b32GrWVWCb5znv Jft1+pKKLofP7hZXUQRi8YRATzDe/QF2Qc3UdZWSRE1VXBoKUXN3kg4/mLWenEnBGkIZS tlyZ6Eer3xkq9KW+N4YMDd5kYzTGg0TBLo8VdySOR/EwPk+ofG3Dn299yvT52Fp33po4n hAhBxr2wDI53EzqWwcq0O7hJqgFZis5XkKjDGhTXHb7b0cescVAy6jWUxXXpMLgyi0lX6 htebRbNyrM/YKu4U8jkwqekMZkRv70TRtNhDUP5IxzMIRYH7apL4ZxKoH/nO4zv/Wxg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793820309; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature: X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State: X-Gm-Gg:X-Received:Received:From:Date:Subject:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=W9WQdjXi6TY9dJIm31sodN+zbfnNOMbsy6mb4cShhQ4=; b=btuD/9HVq8bZRk93AJYt/xXTy/hyzHR9o/BWFsjS9Vr6rWxK9WgJyYPDwStO3B277+8e AhI8UeupduIVNIM5QrVyM9ZnqcvTaJSs+ytErcMk73UiAoxyRi00TYiJrTFgr+fC3d9OF YIJA9ufwlxNoKy2mqOhmao1OX63uyFSI5EEU+bmwuLI0+g1x0hbbcyvnxBwo54S9fu/rK 5akLahEHEVpfGbE2+lnqY6yhBkuB0YAGy8SRufxcgr/gISAncmRiQAHp9jbPxtYMKTiPC jiFCn54nWAqFy8P/2bWvVfTamUCKSFUDLHChYuxq+zJmyBSX2dr4qiADeJ0u/cLpb1w== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RMlNPbVd; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 63639607F2A7; Wed, 4 Nov 2026 19:25:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RMlNPbVd X-Original-To: linux-sunxi@lists.linux.dev Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793820309; b=bY0FnFxyPCVcYjbzOHus49NMsyybd7lxIR0T+WWzdM5VuEC2uckp7m+zdvYpZ1tEMTPG 2rI1A8iSwW8CKo/adfwxjEp4YLb6mwlvOMQotAsG3eJ+xpUwKD1pbxsLCQ/B+n0g5nYUC K93e5kfenq0RYy8sMKyRTr+oO0mWA1mNfsCHbxEa8m3VSpFAtNz5ffcn29gOlga/2yYgB c5CQCx7AI1xkG3RVbMGOpKhVh+kA9t/My3u1kKzXZmne3hKQab0UqoLnZQspsjedHNMup E9rKX8akh8GDSEnHeIY992eJlvBbTNpOAvX8HyjVOnuN0thMyLEp/3rruQ+BNpNKm0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793820309; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer; bh=W9WQdjXi6TY9dJIm31sodN+zbfnNOMbsy6mb4cShhQ4=; b=uYZzgz6D3ZsFS7zBztS5dhaRymzQqNHljGO+g3CtsNI/Re08jG8LEPuD/cva5FqFQ0gg v1xIj9t/JetYmrgnaBZsbk2gFZJunBThaqQzFj0+lLkJ6JgnTgGHqMzKz2o1BouqmY7Ho mYkJdkKPYzb5KwSKuN+El6DIdYKbOgKXHk6tEsCgKA/LvYS7PRv9MzpuMadwh+7Z3HyU8 PA1JtV5RIwaQF5VeqE3olZJsLbgtqgNAT5j6DHPAJbtCV4gNK5QPu2IBP44oR6m6+mAyJ YDcPur4juoRaVr0wgdDQmfSm/cTFW4ymAFUAlOmzakWe8cJFWEAgRGl//kChLEHA4QQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=RMlNPbVd; arc=none smtp.remote-ip=209.85.167.173 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBDCF607F2A0 for ; Wed, 4 Nov 2026 19:25:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DBDCF607F2A0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-4f55a53898cso590107b6e.3 for ; Wed, 04 Nov 2026 11:25:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793820308; x=1794425108; darn=lists.linux.dev; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=W9WQdjXi6TY9dJIm31sodN+zbfnNOMbsy6mb4cShhQ4=; b=RMlNPbVdoTSVSPG3cWQ53GKtFqaNcLfssg19D7jmcKRZ7Elxzit/WCK0JqV5dPZgJa c5qGXzcqDuu5IRvD/n+aCKtlr/KZTNxORn3jK343TxUlRucSLMisRW2PrMzWBQGxJtV0 b3/VZWvocCLQMBkTTjee1PytzyZuwT3kt86rOZsiivSN/k87oBtXS/+ITGempehzJIY3 KL3b5BG8AA+Qa5yGwtBLtp+wGR6YEomzmSxW5j3ymG/ooaa+MET2+uIILo1KvNHbmLIH t4Wsjkx29LHcjFNuPM6NaUSUl3ztu4zfU3+ap+q7wwxxdM5mCQwILpn1dgxKCe4NJcza HQCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793820308; x=1794425108; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=W9WQdjXi6TY9dJIm31sodN+zbfnNOMbsy6mb4cShhQ4=; b=UegSfT/C984gZKJG76GRogG8lrPG8NQecKWJZrWgo8Lt9okKAU475qbg+GURE8+qha Jr5WoXTm5MN/OQOwhneRVEF36nTPxHEK9kGgubVpDsEhHc5/Q3TB9X/zg1WkWNN5QifW XYITbkyhx/Cwm/PqKnUNwXSL5HouVytYNiqLY21n6U8vAbqs59pRvfNIACZzVZBgFH9d l4XniK3twiCzvNdtApD4yuE+eqm0VeWhUIOMwuMcbjGJ7jMmAtgZrcxTG9FffyxszCPu SdDxW05mR2YvuB8gKdTHg2k1vPZ7Oe1z6QPzYzcPQen85SBsE7K1bs7MfQ17ZkdzOR/u uUJA== X-Forwarded-Encrypted: i=1; AKwUvBxHYArBJBi3Uru8XJJdX47JoXS1KxOqeKJsFb1p2HaI1bFcUeMclMrJBuYEU8pZJtjEPq7a0GS7nGTFVw==@lists.linux.dev X-Gm-Message-State: APuU4Z/HYGHePGAJXaPCl4t/ClASrL0gEUrDAUsWg+xtIADUy1/fbHT1 Gl3qDgQMunHUhi1pY/YCVyTIqMTvAqIjWSCF0mkAlcQJ7Fb0qdtXDqgT X-Gm-Gg: AYBFou2NlDcsKrtEslat1Dl8WfoKZfULo/CGyHtYvWhSUqLdmrntCpcwSSHJ9IwQR1h U0cluESZ5X2MUS88MB3Pa2N0GVPAMPWgfedVlNUfIITa3Q24WWeWvl+YLmqsRySQ5rriSY6Qi4v 0ZLxn5W+gSCRqJFOb2o1TVOF26Ax7XeUrgAvq7z9DimSzVbnkKgycZsVk3yuHzkcCKYoERhKgTL tWCzK99oFN/rR/M7se2oeQRjwUIdf8Rc3YecuThDLziuDcvw0j3gQFtIb0bBxkhI1NdGERLqd8F mgmjZaPKe66luQvBOFweZvez4KKevR6/aGDS/wHBQDUHX6ODeI5A7rDNrx3sflXq7pDb3TUqmnR KxIgLoW98AD0ktcbfrTdf1QU9G3OxLfYHxXs0ZpKE4zs8PDVsZEgLq9NDl7IDGi8GPsNIQTSbVw w4/pKMzzUJ8NTosRLKtyJA4sG2ONc2KMG0+/c1YASwCm5EwmQKIBYaBj2XhUOG8YsNgrw4c+9bh mVX1InXnPYYkWrtxLMIFGlwJqvXd+qW4EYyNgxZIgRmjDwqBxf1W3PLIAaa+TFLGrJjqxylKSmt aK9t2V6qE7mImLwwlSrY5oIIoNbpc4rtQheR6faywcMy4ZKHBVDSGyMD9c7dhFceccXFivxWHEj FdHE/PDoJnNaJGrf+lzLT+nVXNs72P40624twnxVvVrgT9Dqecig/RRQdG3xOJx/vGYUXC5V9lW M6sAw6c/ovMDVJE+jCBGpqM1OvmjK68w8+O3cxQaNCo19Re107PKF9 X-Received: by 2002:a05:6808:14ca:b0:4e8:18ee:eb4d with SMTP id 5614622812f47-5640dce42ddmr14479773b6e.20.1793820307724; Wed, 04 Nov 2026 11:25:07 -0800 (PST) Received: from [127.0.1.1] (174-29-1-49.hlrn.qwest.net. [174.29.1.49]) by smtp.gmail.com with ESMTPSA id 5614622812f47-567a9c7bbcesm2328723b6e.1.2026.11.04.11.25.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Nov 2026 11:25:07 -0800 (PST) From: James Hilliard Date: Wed, 04 Nov 2026 12:24:53 -0700 Subject: [PATCH v12 3/5] pwm: sun8i: export the bypass outputs as clocks X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Message-Id: <20261104-h616-pwm-v8-v12-3-8ed39b0dba69@gmail.com> References: <20261104-h616-pwm-v8-v12-0-8ed39b0dba69@gmail.com> In-Reply-To: <20261104-h616-pwm-v8-v12-0-8ed39b0dba69@gmail.com> To: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Stephen Boyd , Brian Masney , Jerome Brunet , Richard Genoud , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Maxime Ripard Cc: Philipp Zabel , Michael Turquette , Paul Kocialkowski , Thomas Petazzoni , John Stultz , Joao Schim , bigunclemax@gmail.com, linux-pwm@vger.kernel.org, linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, James Hilliard X-Mailer: b4 0.15.2 X-Rspamd-Server: rspamd-worker-8404 X-Spamd-Result: default: False [-1.19 / 15.00]; BAYES_HAM(-5.50)[100.00%]; RBL_SENDERSCORE(2.00)[172.234.253.10:from]; SUSPICIOUS_RECIPS(1.50)[]; DMARC_POLICY_SOFTFAIL(1.00)[gmail.com : SPF not aligned (relaxed), No valid DKIM,none]; MAILLIST(-0.18)[generic]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; HAS_LIST_UNSUB(-0.01)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[bootlin.com:email,sea.lore.kernel.org:helo,sea.lore.kernel.org:rdns]; FORGED_SENDER_MAILLIST(0.00)[]; RCVD_COUNT_FIVE(0.00)[6]; RCPT_COUNT_TWELVE(0.00)[26]; FROM_HAS_DN(0.00)[]; FREEMAIL_CC(0.00)[pengutronix.de,baylibre.com,sys-base.io,bootlin.com,google.com,schimsalabim.eu,gmail.com,vger.kernel.org,lists.infradead.org,lists.linux.dev]; TAGGED_RCPT(0.00)[clk,dt]; FROM_NEQ_ENVFROM(0.00)[jameshilliard1@gmail.com,linux-sunxi@lists.linux.dev]; ASN(0.00)[asn:63949, ipnet:172.234.224.0/19, country:SG]; MID_RHS_MATCH_FROM(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[209.85.167.173:received,100.77.114.92:received,174.29.1.49:received]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[172.234.253.10:from]; TAGGED_FROM(0.00)[bounces-27208-noreply=patchwork.local]; R_SPF_ALLOW(0.00)[+ip4:172.234.253.10]; FORGED_RECIPIENTS_MAILLIST(0.00)[]; ARC_ALLOW(0.00)[subspace.kernel.org:s=arc20261015:i=2]; FREEMAIL_TO(0.00)[kernel.org,redhat.com,baylibre.com,bootlin.com,gmail.com,sholland.org]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: D10291C08F2 X-MORS-Enabled: yes X-MORS-DOMAIN: patchwork.local X-MORS-HOSTING: hosting172546 X-MORS-USER: hosting172546 X-getmail-retrieved-from-mailbox: INBOX From: Richard Genoud Expose each H616 PWM channel's hardware bypass as a CCF clock. Consumers such as the co-packaged AC200/AC300 PHY need a clock rather than PWM duty or polarity control. Keep the pair source protected for the complete prepared lifetime, and arbitrate ownership between PWM requests and clock prepare so both interfaces cannot drive the same output. drivers/clk/clk-pwm.c advertises a fixed frequency without reading back the rounded PWM rate. A 42 ns, 21 ns request does select 24 MHz bypass when it is available. However, with the pair constrained to 100 MHz by its sibling, the PWM rounding rules select a 40 ns, 20 ns waveform (25 MHz), while a pwm-clock configured for 24 MHz still reports 24 MHz. The direct provider instead reports the actual pair rate and makes its shared-rate protection visible to CCF. Consumers must still check the rate they receive. Use CLK_IGNORE_UNUSED to preserve unclaimed firmware bypass outputs, just as the PWM path preserves inherited waveforms. Normal consumer disable still turns the output off and releases its shared gate reference. Readback also runs on unrequested channels exposed through PWM debugfs. An exported-clock consumer can retune and enable a disabled output between the rate query and register snapshot. Retry when the pair's notification generation changes so the returned waveform uses a consistent rate and register state. The CCF rate read waits for changes already in progress; the locked generation check detects changes that start afterward. A temporary exclusive rate hold would instead clamp concurrent clock requests to the old rate, potentially returning success at the wrong frequency. Keep readback non-intrusive and leave sleeping CCF calls outside the register spinlock. PRE notifications invalidate snapshots even if a provider failure does not produce POST_RATE_CHANGE. Rate exclusivity protects frequency, whereas prepare/request arbitration owns an output. An unprepared clock's rate hold therefore constrains PWM rounding without reserving the channel. A consumer taking over an inherited output can prepare and enable it at its current rate, then disable and unprepare it before selecting a different rate. Link: https://lore.kernel.org/all/sashiko-outbox-194955@kernel.org/ Signed-off-by: Richard Genoud Co-developed-by: James Hilliard Signed-off-by: James Hilliard --- drivers/pwm/Kconfig | 3 +- drivers/pwm/pwm-sun8i.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 202 insertions(+), 7 deletions(-) diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index c7c964406da4..48dfb8a8d1e9 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -744,7 +744,8 @@ config PWM_SUN8I depends on HAS_IOMEM && COMMON_CLK help PWM framework driver for Allwinner controllers whose channels share - paired source clocks. + paired source clocks. In addition to PWM operation, each channel's + hardware bypass path can be exported as a clock output. To compile this driver as a module, choose M here: the module will be called pwm-sun8i. diff --git a/drivers/pwm/pwm-sun8i.c b/drivers/pwm/pwm-sun8i.c index 08d92d0da291..fa81330162bc 100644 --- a/drivers/pwm/pwm-sun8i.c +++ b/drivers/pwm/pwm-sun8i.c @@ -114,6 +114,9 @@ * The bypass in PWM mode is used to achieve a 1/2 relative duty cycle with the * fastest clock. * + * SUN8I_PWM_clock_x/y serve for the PWM purpose. + * SUN8I_PWM_bypass_clock_x/y serve for the clock-provider purpose. + * */ /* /div_m is a power-of-two divider limited to /256. */ @@ -130,6 +133,12 @@ static const struct clk_div_table sun8i_pwm_div_m_table[] = { { /* sentinel */ } }; +enum sun8i_pwm_mode { + SUN8I_PWM_MODE_NONE, + SUN8I_PWM_MODE_PWM, + SUN8I_PWM_MODE_CLK, +}; + struct sun8i_pwm_chip; struct sun8i_pwm_pair { @@ -144,8 +153,12 @@ struct sun8i_pwm_pair { }; struct sun8i_pwm_channel { + struct clk_hw bypass_hw; + struct sun8i_pwm_chip *chip; + unsigned int index; /* Separate CCF consumer, held only from PWM request to free. */ struct clk *pair_clk; + enum sun8i_pwm_mode mode; u64 pending_period_ns; bool rate_exclusive; }; @@ -155,7 +168,7 @@ struct sun8i_pwm_chip { struct sun8i_pwm_channel channels[SUN8I_PWM_NPWM]; struct clk *bus_clk; void __iomem *base; - /* Protects shared registers and rate_exclusive. */ + /* Protects shared registers, channel ownership and rate_exclusive. */ spinlock_t lock; }; @@ -329,6 +342,101 @@ static int sun8i_pwm_pair_rate_notifier(struct notifier_block *nb, return active ? NOTIFY_BAD : NOTIFY_OK; } +static inline struct sun8i_pwm_channel * +sun8i_pwm_channel_from_hw(struct clk_hw *hw) +{ + return container_of(hw, struct sun8i_pwm_channel, bypass_hw); +} + +static int sun8i_pwm_bypass_prepare(struct clk_hw *hw) +{ + struct sun8i_pwm_channel *chan = sun8i_pwm_channel_from_hw(hw); + struct sun8i_pwm_chip *sun8i_chip = chan->chip; + + guard(spinlock_irqsave)(&sun8i_chip->lock); + if (chan->mode != SUN8I_PWM_MODE_NONE) + return -EBUSY; + + chan->mode = SUN8I_PWM_MODE_CLK; + return 0; +} + +static void sun8i_pwm_bypass_unprepare(struct clk_hw *hw) +{ + struct sun8i_pwm_channel *chan = sun8i_pwm_channel_from_hw(hw); + struct sun8i_pwm_chip *sun8i_chip = chan->chip; + + guard(spinlock_irqsave)(&sun8i_chip->lock); + if (chan->mode != SUN8I_PWM_MODE_CLK) + return; + + chan->mode = SUN8I_PWM_MODE_NONE; +} + +static int sun8i_pwm_bypass_enable(struct clk_hw *hw) +{ + struct sun8i_pwm_channel *chan = sun8i_pwm_channel_from_hw(hw); + struct sun8i_pwm_chip *sun8i_chip = chan->chip; + unsigned int pair = SUN8I_PWM_PAIR_IDX(chan->index); + u32 pccr, per; + + guard(spinlock_irqsave)(&sun8i_chip->lock); + if (chan->mode != SUN8I_PWM_MODE_CLK) + return -EBUSY; + + pccr = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PCCR(pair)); + per = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PER); + if (!(pccr & BIT(SUN8I_PWM_PCCR_BYPASS_BIT(chan->index))) || + !(per & SUN8I_PWM_ENABLE(chan->index))) { + sun8i_pwm_set_enabled_locked(sun8i_chip, chan->index, false); + sun8i_pwm_set_bypass_locked(sun8i_chip, chan->index, true); + sun8i_pwm_set_enabled_locked(sun8i_chip, chan->index, true); + } + + return 0; +} + +static void sun8i_pwm_bypass_disable(struct clk_hw *hw) +{ + struct sun8i_pwm_channel *chan = sun8i_pwm_channel_from_hw(hw); + struct sun8i_pwm_chip *sun8i_chip = chan->chip; + + guard(spinlock_irqsave)(&sun8i_chip->lock); + if (chan->mode != SUN8I_PWM_MODE_CLK) + return; + + sun8i_pwm_set_enabled_locked(sun8i_chip, chan->index, false); + sun8i_pwm_set_bypass_locked(sun8i_chip, chan->index, false); +} + +static int sun8i_pwm_bypass_is_enabled(struct clk_hw *hw) +{ + struct sun8i_pwm_channel *chan = sun8i_pwm_channel_from_hw(hw); + struct sun8i_pwm_chip *sun8i_chip = chan->chip; + unsigned int pair = SUN8I_PWM_PAIR_IDX(chan->index); + bool enabled; + u32 val; + + guard(spinlock_irqsave)(&sun8i_chip->lock); + + val = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PCCR(pair)); + enabled = (val & SUN8I_PWM_PCCR_GATE) && + (val & BIT(SUN8I_PWM_PCCR_BYPASS_BIT(chan->index))); + + val = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PER); + enabled = enabled && (val & SUN8I_PWM_ENABLE(chan->index)); + + return enabled; +} + +static const struct clk_ops sun8i_pwm_bypass_ops = { + .prepare = sun8i_pwm_bypass_prepare, + .unprepare = sun8i_pwm_bypass_unprepare, + .enable = sun8i_pwm_bypass_enable, + .disable = sun8i_pwm_bypass_disable, + .is_enabled = sun8i_pwm_bypass_is_enabled, +}; + static void sun8i_pwm_put_rate(struct sun8i_pwm_chip *sun8i_chip, unsigned int idx) { @@ -355,8 +463,12 @@ static int sun8i_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) int ret; scoped_guard(spinlock_irqsave, &sun8i_chip->lock) { + if (chan->mode != SUN8I_PWM_MODE_NONE) + return -EBUSY; + was_enabled = sun8i_pwm_channel_is_enabled_locked(sun8i_chip, idx); + chan->mode = SUN8I_PWM_MODE_PWM; } chan->pair_clk = clk_hw_get_clk(parent, NULL); @@ -385,6 +497,8 @@ static int sun8i_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) clk_put(chan->pair_clk); err_clear_clock: chan->pair_clk = NULL; + scoped_guard(spinlock_irqsave, &sun8i_chip->lock) + chan->mode = SUN8I_PWM_MODE_NONE; return ret; } @@ -395,6 +509,9 @@ static void sun8i_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) struct sun8i_pwm_channel *chan = &sun8i_chip->channels[pwm->hwpwm]; scoped_guard(spinlock_irqsave, &sun8i_chip->lock) { + if (chan->mode != SUN8I_PWM_MODE_PWM) + return; + sun8i_pwm_set_enabled_locked(sun8i_chip, pwm->hwpwm, false); sun8i_pwm_set_bypass_locked(sun8i_chip, pwm->hwpwm, false); } @@ -403,6 +520,9 @@ static void sun8i_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) clk_disable_unprepare(chan->pair_clk); clk_put(chan->pair_clk); chan->pair_clk = NULL; + + scoped_guard(spinlock_irqsave, &sun8i_chip->lock) + chan->mode = SUN8I_PWM_MODE_NONE; } static int sun8i_pwm_read_waveform(struct pwm_chip *chip, @@ -412,21 +532,39 @@ static int sun8i_pwm_read_waveform(struct pwm_chip *chip, struct sun8i_pwm_waveform *wfhw = _wfhw; struct sun8i_pwm_chip *sun8i_chip = sun8i_pwm_from_chip(chip); unsigned int pair = SUN8I_PWM_PAIR_IDX(pwm->hwpwm); + struct clk *pair_clk = sun8i_chip->pairs[pair].hw->clk; unsigned long pair_rate; u32 pccr, pcr, pdzcr, per, ppr; - pair_rate = sun8i_pwm_get_pair_rate(sun8i_chip, pwm->hwpwm); - if (pair_rate > U32_MAX) - return -ERANGE; + /* + * An unrequested channel can be retuned and enabled by a clock + * consumer. Retry if its rate changed between the CCF read and the + * register snapshot. A temporary exclusive hold would instead clamp + * concurrent bypass-clock rate requests to the old pair rate. + */ + for (;;) { + unsigned int generation; + + cond_resched(); + generation = sun8i_pwm_get_rate_generation(sun8i_chip, pwm->hwpwm); + /* This waits for any rate change already in progress to finish. */ + pair_rate = clk_get_rate(pair_clk); + + guard(spinlock_irqsave)(&sun8i_chip->lock); + if (generation != sun8i_chip->pairs[pair].rate_generation) + continue; - scoped_guard(spinlock_irqsave, &sun8i_chip->lock) { pccr = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PCCR(pair)); per = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PER); pcr = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PCR(pwm->hwpwm)); ppr = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PPR(pwm->hwpwm)); pdzcr = sun8i_pwm_readl(sun8i_chip, SUN8I_PWM_PDZCR(pair)); + break; } + if (pair_rate > U32_MAX) + return -ERANGE; + *wfhw = (struct sun8i_pwm_waveform) { .enabled = !!(pccr & SUN8I_PWM_PCCR_GATE) && !!(per & SUN8I_PWM_ENABLE(pwm->hwpwm)), @@ -523,7 +661,8 @@ sun8i_pwm_pair_rate_constrained(struct sun8i_pwm_chip *sun8i_chip, unsigned int sibling = idx ^ 1; guard(spinlock_irqsave)(&sun8i_chip->lock); - return sun8i_chip->channels[sibling].rate_exclusive || + return sun8i_chip->channels[sibling].mode == SUN8I_PWM_MODE_CLK || + sun8i_chip->channels[sibling].rate_exclusive || sun8i_pwm_channel_is_enabled_locked(sun8i_chip, sibling); } @@ -1075,6 +1214,42 @@ static int sun8i_pwm_register_pair_clocks(struct device *dev, return 0; } +/* Register the bypass clock for each channel. */ +static int sun8i_pwm_register_bypass_clocks(struct device *dev, + struct sun8i_pwm_chip *sun8i_chip) +{ + for (unsigned int i = 0; i < SUN8I_PWM_NPWM; i++) { + struct sun8i_pwm_channel *chan = &sun8i_chip->channels[i]; + struct clk_hw *parent = sun8i_chip->pairs[SUN8I_PWM_PAIR_IDX(i)].hw; + const char *name; + int ret; + + name = devm_kasprintf(dev, GFP_KERNEL, "%s#pwm-bypass%u", + dev_name(dev), i); + if (!name) + return -ENOMEM; + + chan->chip = sun8i_chip; + chan->index = i; + /* + * Protect the shared rate while prepared. Preserve unclaimed + * firmware outputs through CCF's unused-clock cleanup, as for + * PWM waveforms; only a consumer may turn them off. + */ + chan->bypass_hw.init = + CLK_HW_INIT_HW(name, parent, &sun8i_pwm_bypass_ops, + CLK_SET_RATE_PARENT | CLK_SET_RATE_GATE | + CLK_IGNORE_UNUSED); + + ret = devm_clk_hw_register(dev, &chan->bypass_hw); + if (ret) + return dev_err_probe(dev, ret, + "Failed to register bypass clock %u\n", i); + } + + return 0; +} + /* * A disabled pair gate makes any set PER bits ineffective. Clear those stale * enables before a clock consumer can turn the shared gate on and expose an @@ -1103,6 +1278,17 @@ sun8i_pwm_sanitize_disabled_pairs(struct sun8i_pwm_chip *sun8i_chip) sun8i_pwm_writel(sun8i_chip, per, SUN8I_PWM_PER); } +static struct clk_hw *sun8i_pwm_get_clk_hw(struct of_phandle_args *clkspec, + void *data) +{ + struct sun8i_pwm_chip *sun8i_chip = data; + + if (clkspec->args_count != 1 || clkspec->args[0] >= SUN8I_PWM_NPWM) + return ERR_PTR(-EINVAL); + + return &sun8i_chip->channels[clkspec->args[0]].bypass_hw; +} + static int sun8i_pwm_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -1141,6 +1327,14 @@ static int sun8i_pwm_probe(struct platform_device *pdev) if (ret) return ret; + ret = sun8i_pwm_register_bypass_clocks(dev, sun8i_chip); + if (ret) + return ret; + + ret = devm_of_clk_add_hw_provider(dev, sun8i_pwm_get_clk_hw, sun8i_chip); + if (ret) + return dev_err_probe(dev, ret, "Failed to add HW clock provider\n"); + ret = devm_pwmchip_add(dev, chip); if (ret < 0) return dev_err_probe(dev, ret, "Failed to add PWM chip\n");